10b57cec5SDimitry Andric //===- TargetLoweringBase.cpp - Implement the TargetLoweringBase class ----===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This implements the TargetLoweringBase class.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h"
140b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
150b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
160b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h"
170b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
180b57cec5SDimitry Andric #include "llvm/ADT/Triple.h"
190b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
205ffd83dbSDimitry Andric #include "llvm/Analysis/Loads.h"
215ffd83dbSDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/Analysis.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/ISDOpcodes.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
280b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/MachineMemOperand.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
310b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
320b57cec5SDimitry Andric #include "llvm/CodeGen/RuntimeLibcalls.h"
330b57cec5SDimitry Andric #include "llvm/CodeGen/StackMaps.h"
340b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h"
350b57cec5SDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h"
360b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
370b57cec5SDimitry Andric #include "llvm/CodeGen/ValueTypes.h"
380b57cec5SDimitry Andric #include "llvm/IR/Attributes.h"
390b57cec5SDimitry Andric #include "llvm/IR/CallingConv.h"
400b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
410b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h"
420b57cec5SDimitry Andric #include "llvm/IR/Function.h"
430b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h"
440b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h"
450b57cec5SDimitry Andric #include "llvm/IR/IRBuilder.h"
460b57cec5SDimitry Andric #include "llvm/IR/Module.h"
470b57cec5SDimitry Andric #include "llvm/IR/Type.h"
480b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
490b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
500b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
510b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
520b57cec5SDimitry Andric #include "llvm/Support/MachineValueType.h"
530b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
540b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
555ffd83dbSDimitry Andric #include "llvm/Transforms/Utils/SizeOpts.h"
560b57cec5SDimitry Andric #include <algorithm>
570b57cec5SDimitry Andric #include <cassert>
580b57cec5SDimitry Andric #include <cstddef>
590b57cec5SDimitry Andric #include <cstdint>
600b57cec5SDimitry Andric #include <cstring>
610b57cec5SDimitry Andric #include <iterator>
620b57cec5SDimitry Andric #include <string>
630b57cec5SDimitry Andric #include <tuple>
640b57cec5SDimitry Andric #include <utility>
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric using namespace llvm;
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric static cl::opt<bool> JumpIsExpensiveOverride(
690b57cec5SDimitry Andric     "jump-is-expensive", cl::init(false),
700b57cec5SDimitry Andric     cl::desc("Do not create extra branches to split comparison logic."),
710b57cec5SDimitry Andric     cl::Hidden);
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric static cl::opt<unsigned> MinimumJumpTableEntries
740b57cec5SDimitry Andric   ("min-jump-table-entries", cl::init(4), cl::Hidden,
750b57cec5SDimitry Andric    cl::desc("Set minimum number of entries to use a jump table."));
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric static cl::opt<unsigned> MaximumJumpTableSize
780b57cec5SDimitry Andric   ("max-jump-table-size", cl::init(UINT_MAX), cl::Hidden,
790b57cec5SDimitry Andric    cl::desc("Set maximum size of jump tables."));
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric /// Minimum jump table density for normal functions.
820b57cec5SDimitry Andric static cl::opt<unsigned>
830b57cec5SDimitry Andric     JumpTableDensity("jump-table-density", cl::init(10), cl::Hidden,
840b57cec5SDimitry Andric                      cl::desc("Minimum density for building a jump table in "
850b57cec5SDimitry Andric                               "a normal function"));
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric /// Minimum jump table density for -Os or -Oz functions.
880b57cec5SDimitry Andric static cl::opt<unsigned> OptsizeJumpTableDensity(
890b57cec5SDimitry Andric     "optsize-jump-table-density", cl::init(40), cl::Hidden,
900b57cec5SDimitry Andric     cl::desc("Minimum density for building a jump table in "
910b57cec5SDimitry Andric              "an optsize function"));
920b57cec5SDimitry Andric 
93480093f4SDimitry Andric // FIXME: This option is only to test if the strict fp operation processed
94480093f4SDimitry Andric // correctly by preventing mutating strict fp operation to normal fp operation
95480093f4SDimitry Andric // during development. When the backend supports strict float operation, this
96480093f4SDimitry Andric // option will be meaningless.
97480093f4SDimitry Andric static cl::opt<bool> DisableStrictNodeMutation("disable-strictnode-mutation",
98480093f4SDimitry Andric        cl::desc("Don't mutate strict-float node to a legalize node"),
99480093f4SDimitry Andric        cl::init(false), cl::Hidden);
100480093f4SDimitry Andric 
darwinHasSinCos(const Triple & TT)1010b57cec5SDimitry Andric static bool darwinHasSinCos(const Triple &TT) {
1020b57cec5SDimitry Andric   assert(TT.isOSDarwin() && "should be called with darwin triple");
1030b57cec5SDimitry Andric   // Don't bother with 32 bit x86.
1040b57cec5SDimitry Andric   if (TT.getArch() == Triple::x86)
1050b57cec5SDimitry Andric     return false;
1060b57cec5SDimitry Andric   // Macos < 10.9 has no sincos_stret.
1070b57cec5SDimitry Andric   if (TT.isMacOSX())
1080b57cec5SDimitry Andric     return !TT.isMacOSXVersionLT(10, 9) && TT.isArch64Bit();
1090b57cec5SDimitry Andric   // iOS < 7.0 has no sincos_stret.
1100b57cec5SDimitry Andric   if (TT.isiOS())
1110b57cec5SDimitry Andric     return !TT.isOSVersionLT(7, 0);
1120b57cec5SDimitry Andric   // Any other darwin such as WatchOS/TvOS is new enough.
1130b57cec5SDimitry Andric   return true;
1140b57cec5SDimitry Andric }
1150b57cec5SDimitry Andric 
InitLibcalls(const Triple & TT)1160b57cec5SDimitry Andric void TargetLoweringBase::InitLibcalls(const Triple &TT) {
1170b57cec5SDimitry Andric #define HANDLE_LIBCALL(code, name) \
1180b57cec5SDimitry Andric   setLibcallName(RTLIB::code, name);
1190b57cec5SDimitry Andric #include "llvm/IR/RuntimeLibcalls.def"
1200b57cec5SDimitry Andric #undef HANDLE_LIBCALL
1210b57cec5SDimitry Andric   // Initialize calling conventions to their default.
1220b57cec5SDimitry Andric   for (int LC = 0; LC < RTLIB::UNKNOWN_LIBCALL; ++LC)
1230b57cec5SDimitry Andric     setLibcallCallingConv((RTLIB::Libcall)LC, CallingConv::C);
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric   // For IEEE quad-precision libcall names, PPC uses "kf" instead of "tf".
126af732203SDimitry Andric   if (TT.isPPC()) {
1270b57cec5SDimitry Andric     setLibcallName(RTLIB::ADD_F128, "__addkf3");
1280b57cec5SDimitry Andric     setLibcallName(RTLIB::SUB_F128, "__subkf3");
1290b57cec5SDimitry Andric     setLibcallName(RTLIB::MUL_F128, "__mulkf3");
1300b57cec5SDimitry Andric     setLibcallName(RTLIB::DIV_F128, "__divkf3");
131af732203SDimitry Andric     setLibcallName(RTLIB::POWI_F128, "__powikf2");
1320b57cec5SDimitry Andric     setLibcallName(RTLIB::FPEXT_F32_F128, "__extendsfkf2");
1330b57cec5SDimitry Andric     setLibcallName(RTLIB::FPEXT_F64_F128, "__extenddfkf2");
1340b57cec5SDimitry Andric     setLibcallName(RTLIB::FPROUND_F128_F32, "__trunckfsf2");
1350b57cec5SDimitry Andric     setLibcallName(RTLIB::FPROUND_F128_F64, "__trunckfdf2");
1360b57cec5SDimitry Andric     setLibcallName(RTLIB::FPTOSINT_F128_I32, "__fixkfsi");
1370b57cec5SDimitry Andric     setLibcallName(RTLIB::FPTOSINT_F128_I64, "__fixkfdi");
138af732203SDimitry Andric     setLibcallName(RTLIB::FPTOSINT_F128_I128, "__fixkfti");
1390b57cec5SDimitry Andric     setLibcallName(RTLIB::FPTOUINT_F128_I32, "__fixunskfsi");
1400b57cec5SDimitry Andric     setLibcallName(RTLIB::FPTOUINT_F128_I64, "__fixunskfdi");
141af732203SDimitry Andric     setLibcallName(RTLIB::FPTOUINT_F128_I128, "__fixunskfti");
1420b57cec5SDimitry Andric     setLibcallName(RTLIB::SINTTOFP_I32_F128, "__floatsikf");
1430b57cec5SDimitry Andric     setLibcallName(RTLIB::SINTTOFP_I64_F128, "__floatdikf");
144af732203SDimitry Andric     setLibcallName(RTLIB::SINTTOFP_I128_F128, "__floattikf");
1450b57cec5SDimitry Andric     setLibcallName(RTLIB::UINTTOFP_I32_F128, "__floatunsikf");
1460b57cec5SDimitry Andric     setLibcallName(RTLIB::UINTTOFP_I64_F128, "__floatundikf");
147af732203SDimitry Andric     setLibcallName(RTLIB::UINTTOFP_I128_F128, "__floatuntikf");
1480b57cec5SDimitry Andric     setLibcallName(RTLIB::OEQ_F128, "__eqkf2");
1490b57cec5SDimitry Andric     setLibcallName(RTLIB::UNE_F128, "__nekf2");
1500b57cec5SDimitry Andric     setLibcallName(RTLIB::OGE_F128, "__gekf2");
1510b57cec5SDimitry Andric     setLibcallName(RTLIB::OLT_F128, "__ltkf2");
1520b57cec5SDimitry Andric     setLibcallName(RTLIB::OLE_F128, "__lekf2");
1530b57cec5SDimitry Andric     setLibcallName(RTLIB::OGT_F128, "__gtkf2");
1540b57cec5SDimitry Andric     setLibcallName(RTLIB::UO_F128, "__unordkf2");
1550b57cec5SDimitry Andric   }
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric   // A few names are different on particular architectures or environments.
1580b57cec5SDimitry Andric   if (TT.isOSDarwin()) {
1590b57cec5SDimitry Andric     // For f16/f32 conversions, Darwin uses the standard naming scheme, instead
1600b57cec5SDimitry Andric     // of the gnueabi-style __gnu_*_ieee.
1610b57cec5SDimitry Andric     // FIXME: What about other targets?
1620b57cec5SDimitry Andric     setLibcallName(RTLIB::FPEXT_F16_F32, "__extendhfsf2");
1630b57cec5SDimitry Andric     setLibcallName(RTLIB::FPROUND_F32_F16, "__truncsfhf2");
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric     // Some darwins have an optimized __bzero/bzero function.
1660b57cec5SDimitry Andric     switch (TT.getArch()) {
1670b57cec5SDimitry Andric     case Triple::x86:
1680b57cec5SDimitry Andric     case Triple::x86_64:
1690b57cec5SDimitry Andric       if (TT.isMacOSX() && !TT.isMacOSXVersionLT(10, 6))
1700b57cec5SDimitry Andric         setLibcallName(RTLIB::BZERO, "__bzero");
1710b57cec5SDimitry Andric       break;
1720b57cec5SDimitry Andric     case Triple::aarch64:
1738bcb0991SDimitry Andric     case Triple::aarch64_32:
1740b57cec5SDimitry Andric       setLibcallName(RTLIB::BZERO, "bzero");
1750b57cec5SDimitry Andric       break;
1760b57cec5SDimitry Andric     default:
1770b57cec5SDimitry Andric       break;
1780b57cec5SDimitry Andric     }
1790b57cec5SDimitry Andric 
1800b57cec5SDimitry Andric     if (darwinHasSinCos(TT)) {
1810b57cec5SDimitry Andric       setLibcallName(RTLIB::SINCOS_STRET_F32, "__sincosf_stret");
1820b57cec5SDimitry Andric       setLibcallName(RTLIB::SINCOS_STRET_F64, "__sincos_stret");
1830b57cec5SDimitry Andric       if (TT.isWatchABI()) {
1840b57cec5SDimitry Andric         setLibcallCallingConv(RTLIB::SINCOS_STRET_F32,
1850b57cec5SDimitry Andric                               CallingConv::ARM_AAPCS_VFP);
1860b57cec5SDimitry Andric         setLibcallCallingConv(RTLIB::SINCOS_STRET_F64,
1870b57cec5SDimitry Andric                               CallingConv::ARM_AAPCS_VFP);
1880b57cec5SDimitry Andric       }
1890b57cec5SDimitry Andric     }
1900b57cec5SDimitry Andric   } else {
1910b57cec5SDimitry Andric     setLibcallName(RTLIB::FPEXT_F16_F32, "__gnu_h2f_ieee");
1920b57cec5SDimitry Andric     setLibcallName(RTLIB::FPROUND_F32_F16, "__gnu_f2h_ieee");
1930b57cec5SDimitry Andric   }
1940b57cec5SDimitry Andric 
1950b57cec5SDimitry Andric   if (TT.isGNUEnvironment() || TT.isOSFuchsia() ||
1960b57cec5SDimitry Andric       (TT.isAndroid() && !TT.isAndroidVersionLT(9))) {
1970b57cec5SDimitry Andric     setLibcallName(RTLIB::SINCOS_F32, "sincosf");
1980b57cec5SDimitry Andric     setLibcallName(RTLIB::SINCOS_F64, "sincos");
1990b57cec5SDimitry Andric     setLibcallName(RTLIB::SINCOS_F80, "sincosl");
2000b57cec5SDimitry Andric     setLibcallName(RTLIB::SINCOS_F128, "sincosl");
2010b57cec5SDimitry Andric     setLibcallName(RTLIB::SINCOS_PPCF128, "sincosl");
2020b57cec5SDimitry Andric   }
2030b57cec5SDimitry Andric 
2048bcb0991SDimitry Andric   if (TT.isPS4CPU()) {
2058bcb0991SDimitry Andric     setLibcallName(RTLIB::SINCOS_F32, "sincosf");
2068bcb0991SDimitry Andric     setLibcallName(RTLIB::SINCOS_F64, "sincos");
2078bcb0991SDimitry Andric   }
2088bcb0991SDimitry Andric 
2090b57cec5SDimitry Andric   if (TT.isOSOpenBSD()) {
2100b57cec5SDimitry Andric     setLibcallName(RTLIB::STACKPROTECTOR_CHECK_FAIL, nullptr);
2110b57cec5SDimitry Andric   }
2120b57cec5SDimitry Andric }
2130b57cec5SDimitry Andric 
214*5f7ddb14SDimitry Andric /// GetFPLibCall - Helper to return the right libcall for the given floating
215*5f7ddb14SDimitry Andric /// point type, or UNKNOWN_LIBCALL if there is none.
getFPLibCall(EVT VT,RTLIB::Libcall Call_F32,RTLIB::Libcall Call_F64,RTLIB::Libcall Call_F80,RTLIB::Libcall Call_F128,RTLIB::Libcall Call_PPCF128)216*5f7ddb14SDimitry Andric RTLIB::Libcall RTLIB::getFPLibCall(EVT VT,
217*5f7ddb14SDimitry Andric                                    RTLIB::Libcall Call_F32,
218*5f7ddb14SDimitry Andric                                    RTLIB::Libcall Call_F64,
219*5f7ddb14SDimitry Andric                                    RTLIB::Libcall Call_F80,
220*5f7ddb14SDimitry Andric                                    RTLIB::Libcall Call_F128,
221*5f7ddb14SDimitry Andric                                    RTLIB::Libcall Call_PPCF128) {
222*5f7ddb14SDimitry Andric   return
223*5f7ddb14SDimitry Andric     VT == MVT::f32 ? Call_F32 :
224*5f7ddb14SDimitry Andric     VT == MVT::f64 ? Call_F64 :
225*5f7ddb14SDimitry Andric     VT == MVT::f80 ? Call_F80 :
226*5f7ddb14SDimitry Andric     VT == MVT::f128 ? Call_F128 :
227*5f7ddb14SDimitry Andric     VT == MVT::ppcf128 ? Call_PPCF128 :
228*5f7ddb14SDimitry Andric     RTLIB::UNKNOWN_LIBCALL;
229*5f7ddb14SDimitry Andric }
230*5f7ddb14SDimitry Andric 
2310b57cec5SDimitry Andric /// getFPEXT - Return the FPEXT_*_* value for the given types, or
2320b57cec5SDimitry Andric /// UNKNOWN_LIBCALL if there is none.
getFPEXT(EVT OpVT,EVT RetVT)2330b57cec5SDimitry Andric RTLIB::Libcall RTLIB::getFPEXT(EVT OpVT, EVT RetVT) {
2340b57cec5SDimitry Andric   if (OpVT == MVT::f16) {
2350b57cec5SDimitry Andric     if (RetVT == MVT::f32)
2360b57cec5SDimitry Andric       return FPEXT_F16_F32;
237af732203SDimitry Andric     if (RetVT == MVT::f64)
238af732203SDimitry Andric       return FPEXT_F16_F64;
239af732203SDimitry Andric     if (RetVT == MVT::f128)
240af732203SDimitry Andric       return FPEXT_F16_F128;
2410b57cec5SDimitry Andric   } else if (OpVT == MVT::f32) {
2420b57cec5SDimitry Andric     if (RetVT == MVT::f64)
2430b57cec5SDimitry Andric       return FPEXT_F32_F64;
2440b57cec5SDimitry Andric     if (RetVT == MVT::f128)
2450b57cec5SDimitry Andric       return FPEXT_F32_F128;
2460b57cec5SDimitry Andric     if (RetVT == MVT::ppcf128)
2470b57cec5SDimitry Andric       return FPEXT_F32_PPCF128;
2480b57cec5SDimitry Andric   } else if (OpVT == MVT::f64) {
2490b57cec5SDimitry Andric     if (RetVT == MVT::f128)
2500b57cec5SDimitry Andric       return FPEXT_F64_F128;
2510b57cec5SDimitry Andric     else if (RetVT == MVT::ppcf128)
2520b57cec5SDimitry Andric       return FPEXT_F64_PPCF128;
2530b57cec5SDimitry Andric   } else if (OpVT == MVT::f80) {
2540b57cec5SDimitry Andric     if (RetVT == MVT::f128)
2550b57cec5SDimitry Andric       return FPEXT_F80_F128;
2560b57cec5SDimitry Andric   }
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric   return UNKNOWN_LIBCALL;
2590b57cec5SDimitry Andric }
2600b57cec5SDimitry Andric 
2610b57cec5SDimitry Andric /// getFPROUND - Return the FPROUND_*_* value for the given types, or
2620b57cec5SDimitry Andric /// UNKNOWN_LIBCALL if there is none.
getFPROUND(EVT OpVT,EVT RetVT)2630b57cec5SDimitry Andric RTLIB::Libcall RTLIB::getFPROUND(EVT OpVT, EVT RetVT) {
2640b57cec5SDimitry Andric   if (RetVT == MVT::f16) {
2650b57cec5SDimitry Andric     if (OpVT == MVT::f32)
2660b57cec5SDimitry Andric       return FPROUND_F32_F16;
2670b57cec5SDimitry Andric     if (OpVT == MVT::f64)
2680b57cec5SDimitry Andric       return FPROUND_F64_F16;
2690b57cec5SDimitry Andric     if (OpVT == MVT::f80)
2700b57cec5SDimitry Andric       return FPROUND_F80_F16;
2710b57cec5SDimitry Andric     if (OpVT == MVT::f128)
2720b57cec5SDimitry Andric       return FPROUND_F128_F16;
2730b57cec5SDimitry Andric     if (OpVT == MVT::ppcf128)
2740b57cec5SDimitry Andric       return FPROUND_PPCF128_F16;
2750b57cec5SDimitry Andric   } else if (RetVT == MVT::f32) {
2760b57cec5SDimitry Andric     if (OpVT == MVT::f64)
2770b57cec5SDimitry Andric       return FPROUND_F64_F32;
2780b57cec5SDimitry Andric     if (OpVT == MVT::f80)
2790b57cec5SDimitry Andric       return FPROUND_F80_F32;
2800b57cec5SDimitry Andric     if (OpVT == MVT::f128)
2810b57cec5SDimitry Andric       return FPROUND_F128_F32;
2820b57cec5SDimitry Andric     if (OpVT == MVT::ppcf128)
2830b57cec5SDimitry Andric       return FPROUND_PPCF128_F32;
2840b57cec5SDimitry Andric   } else if (RetVT == MVT::f64) {
2850b57cec5SDimitry Andric     if (OpVT == MVT::f80)
2860b57cec5SDimitry Andric       return FPROUND_F80_F64;
2870b57cec5SDimitry Andric     if (OpVT == MVT::f128)
2880b57cec5SDimitry Andric       return FPROUND_F128_F64;
2890b57cec5SDimitry Andric     if (OpVT == MVT::ppcf128)
2900b57cec5SDimitry Andric       return FPROUND_PPCF128_F64;
2910b57cec5SDimitry Andric   } else if (RetVT == MVT::f80) {
2920b57cec5SDimitry Andric     if (OpVT == MVT::f128)
2930b57cec5SDimitry Andric       return FPROUND_F128_F80;
2940b57cec5SDimitry Andric   }
2950b57cec5SDimitry Andric 
2960b57cec5SDimitry Andric   return UNKNOWN_LIBCALL;
2970b57cec5SDimitry Andric }
2980b57cec5SDimitry Andric 
2990b57cec5SDimitry Andric /// getFPTOSINT - Return the FPTOSINT_*_* value for the given types, or
3000b57cec5SDimitry Andric /// UNKNOWN_LIBCALL if there is none.
getFPTOSINT(EVT OpVT,EVT RetVT)3010b57cec5SDimitry Andric RTLIB::Libcall RTLIB::getFPTOSINT(EVT OpVT, EVT RetVT) {
302af732203SDimitry Andric   if (OpVT == MVT::f16) {
303af732203SDimitry Andric     if (RetVT == MVT::i32)
304af732203SDimitry Andric       return FPTOSINT_F16_I32;
305af732203SDimitry Andric     if (RetVT == MVT::i64)
306af732203SDimitry Andric       return FPTOSINT_F16_I64;
307af732203SDimitry Andric     if (RetVT == MVT::i128)
308af732203SDimitry Andric       return FPTOSINT_F16_I128;
309af732203SDimitry Andric   } else if (OpVT == MVT::f32) {
3100b57cec5SDimitry Andric     if (RetVT == MVT::i32)
3110b57cec5SDimitry Andric       return FPTOSINT_F32_I32;
3120b57cec5SDimitry Andric     if (RetVT == MVT::i64)
3130b57cec5SDimitry Andric       return FPTOSINT_F32_I64;
3140b57cec5SDimitry Andric     if (RetVT == MVT::i128)
3150b57cec5SDimitry Andric       return FPTOSINT_F32_I128;
3160b57cec5SDimitry Andric   } else if (OpVT == MVT::f64) {
3170b57cec5SDimitry Andric     if (RetVT == MVT::i32)
3180b57cec5SDimitry Andric       return FPTOSINT_F64_I32;
3190b57cec5SDimitry Andric     if (RetVT == MVT::i64)
3200b57cec5SDimitry Andric       return FPTOSINT_F64_I64;
3210b57cec5SDimitry Andric     if (RetVT == MVT::i128)
3220b57cec5SDimitry Andric       return FPTOSINT_F64_I128;
3230b57cec5SDimitry Andric   } else if (OpVT == MVT::f80) {
3240b57cec5SDimitry Andric     if (RetVT == MVT::i32)
3250b57cec5SDimitry Andric       return FPTOSINT_F80_I32;
3260b57cec5SDimitry Andric     if (RetVT == MVT::i64)
3270b57cec5SDimitry Andric       return FPTOSINT_F80_I64;
3280b57cec5SDimitry Andric     if (RetVT == MVT::i128)
3290b57cec5SDimitry Andric       return FPTOSINT_F80_I128;
3300b57cec5SDimitry Andric   } else if (OpVT == MVT::f128) {
3310b57cec5SDimitry Andric     if (RetVT == MVT::i32)
3320b57cec5SDimitry Andric       return FPTOSINT_F128_I32;
3330b57cec5SDimitry Andric     if (RetVT == MVT::i64)
3340b57cec5SDimitry Andric       return FPTOSINT_F128_I64;
3350b57cec5SDimitry Andric     if (RetVT == MVT::i128)
3360b57cec5SDimitry Andric       return FPTOSINT_F128_I128;
3370b57cec5SDimitry Andric   } else if (OpVT == MVT::ppcf128) {
3380b57cec5SDimitry Andric     if (RetVT == MVT::i32)
3390b57cec5SDimitry Andric       return FPTOSINT_PPCF128_I32;
3400b57cec5SDimitry Andric     if (RetVT == MVT::i64)
3410b57cec5SDimitry Andric       return FPTOSINT_PPCF128_I64;
3420b57cec5SDimitry Andric     if (RetVT == MVT::i128)
3430b57cec5SDimitry Andric       return FPTOSINT_PPCF128_I128;
3440b57cec5SDimitry Andric   }
3450b57cec5SDimitry Andric   return UNKNOWN_LIBCALL;
3460b57cec5SDimitry Andric }
3470b57cec5SDimitry Andric 
3480b57cec5SDimitry Andric /// getFPTOUINT - Return the FPTOUINT_*_* value for the given types, or
3490b57cec5SDimitry Andric /// UNKNOWN_LIBCALL if there is none.
getFPTOUINT(EVT OpVT,EVT RetVT)3500b57cec5SDimitry Andric RTLIB::Libcall RTLIB::getFPTOUINT(EVT OpVT, EVT RetVT) {
351af732203SDimitry Andric   if (OpVT == MVT::f16) {
352af732203SDimitry Andric     if (RetVT == MVT::i32)
353af732203SDimitry Andric       return FPTOUINT_F16_I32;
354af732203SDimitry Andric     if (RetVT == MVT::i64)
355af732203SDimitry Andric       return FPTOUINT_F16_I64;
356af732203SDimitry Andric     if (RetVT == MVT::i128)
357af732203SDimitry Andric       return FPTOUINT_F16_I128;
358af732203SDimitry Andric   } else if (OpVT == MVT::f32) {
3590b57cec5SDimitry Andric     if (RetVT == MVT::i32)
3600b57cec5SDimitry Andric       return FPTOUINT_F32_I32;
3610b57cec5SDimitry Andric     if (RetVT == MVT::i64)
3620b57cec5SDimitry Andric       return FPTOUINT_F32_I64;
3630b57cec5SDimitry Andric     if (RetVT == MVT::i128)
3640b57cec5SDimitry Andric       return FPTOUINT_F32_I128;
3650b57cec5SDimitry Andric   } else if (OpVT == MVT::f64) {
3660b57cec5SDimitry Andric     if (RetVT == MVT::i32)
3670b57cec5SDimitry Andric       return FPTOUINT_F64_I32;
3680b57cec5SDimitry Andric     if (RetVT == MVT::i64)
3690b57cec5SDimitry Andric       return FPTOUINT_F64_I64;
3700b57cec5SDimitry Andric     if (RetVT == MVT::i128)
3710b57cec5SDimitry Andric       return FPTOUINT_F64_I128;
3720b57cec5SDimitry Andric   } else if (OpVT == MVT::f80) {
3730b57cec5SDimitry Andric     if (RetVT == MVT::i32)
3740b57cec5SDimitry Andric       return FPTOUINT_F80_I32;
3750b57cec5SDimitry Andric     if (RetVT == MVT::i64)
3760b57cec5SDimitry Andric       return FPTOUINT_F80_I64;
3770b57cec5SDimitry Andric     if (RetVT == MVT::i128)
3780b57cec5SDimitry Andric       return FPTOUINT_F80_I128;
3790b57cec5SDimitry Andric   } else if (OpVT == MVT::f128) {
3800b57cec5SDimitry Andric     if (RetVT == MVT::i32)
3810b57cec5SDimitry Andric       return FPTOUINT_F128_I32;
3820b57cec5SDimitry Andric     if (RetVT == MVT::i64)
3830b57cec5SDimitry Andric       return FPTOUINT_F128_I64;
3840b57cec5SDimitry Andric     if (RetVT == MVT::i128)
3850b57cec5SDimitry Andric       return FPTOUINT_F128_I128;
3860b57cec5SDimitry Andric   } else if (OpVT == MVT::ppcf128) {
3870b57cec5SDimitry Andric     if (RetVT == MVT::i32)
3880b57cec5SDimitry Andric       return FPTOUINT_PPCF128_I32;
3890b57cec5SDimitry Andric     if (RetVT == MVT::i64)
3900b57cec5SDimitry Andric       return FPTOUINT_PPCF128_I64;
3910b57cec5SDimitry Andric     if (RetVT == MVT::i128)
3920b57cec5SDimitry Andric       return FPTOUINT_PPCF128_I128;
3930b57cec5SDimitry Andric   }
3940b57cec5SDimitry Andric   return UNKNOWN_LIBCALL;
3950b57cec5SDimitry Andric }
3960b57cec5SDimitry Andric 
3970b57cec5SDimitry Andric /// getSINTTOFP - Return the SINTTOFP_*_* value for the given types, or
3980b57cec5SDimitry Andric /// UNKNOWN_LIBCALL if there is none.
getSINTTOFP(EVT OpVT,EVT RetVT)3990b57cec5SDimitry Andric RTLIB::Libcall RTLIB::getSINTTOFP(EVT OpVT, EVT RetVT) {
4000b57cec5SDimitry Andric   if (OpVT == MVT::i32) {
401af732203SDimitry Andric     if (RetVT == MVT::f16)
402af732203SDimitry Andric       return SINTTOFP_I32_F16;
4030b57cec5SDimitry Andric     if (RetVT == MVT::f32)
4040b57cec5SDimitry Andric       return SINTTOFP_I32_F32;
4050b57cec5SDimitry Andric     if (RetVT == MVT::f64)
4060b57cec5SDimitry Andric       return SINTTOFP_I32_F64;
4070b57cec5SDimitry Andric     if (RetVT == MVT::f80)
4080b57cec5SDimitry Andric       return SINTTOFP_I32_F80;
4090b57cec5SDimitry Andric     if (RetVT == MVT::f128)
4100b57cec5SDimitry Andric       return SINTTOFP_I32_F128;
4110b57cec5SDimitry Andric     if (RetVT == MVT::ppcf128)
4120b57cec5SDimitry Andric       return SINTTOFP_I32_PPCF128;
4130b57cec5SDimitry Andric   } else if (OpVT == MVT::i64) {
414af732203SDimitry Andric     if (RetVT == MVT::f16)
415af732203SDimitry Andric       return SINTTOFP_I64_F16;
4160b57cec5SDimitry Andric     if (RetVT == MVT::f32)
4170b57cec5SDimitry Andric       return SINTTOFP_I64_F32;
4180b57cec5SDimitry Andric     if (RetVT == MVT::f64)
4190b57cec5SDimitry Andric       return SINTTOFP_I64_F64;
4200b57cec5SDimitry Andric     if (RetVT == MVT::f80)
4210b57cec5SDimitry Andric       return SINTTOFP_I64_F80;
4220b57cec5SDimitry Andric     if (RetVT == MVT::f128)
4230b57cec5SDimitry Andric       return SINTTOFP_I64_F128;
4240b57cec5SDimitry Andric     if (RetVT == MVT::ppcf128)
4250b57cec5SDimitry Andric       return SINTTOFP_I64_PPCF128;
4260b57cec5SDimitry Andric   } else if (OpVT == MVT::i128) {
427af732203SDimitry Andric     if (RetVT == MVT::f16)
428af732203SDimitry Andric       return SINTTOFP_I128_F16;
4290b57cec5SDimitry Andric     if (RetVT == MVT::f32)
4300b57cec5SDimitry Andric       return SINTTOFP_I128_F32;
4310b57cec5SDimitry Andric     if (RetVT == MVT::f64)
4320b57cec5SDimitry Andric       return SINTTOFP_I128_F64;
4330b57cec5SDimitry Andric     if (RetVT == MVT::f80)
4340b57cec5SDimitry Andric       return SINTTOFP_I128_F80;
4350b57cec5SDimitry Andric     if (RetVT == MVT::f128)
4360b57cec5SDimitry Andric       return SINTTOFP_I128_F128;
4370b57cec5SDimitry Andric     if (RetVT == MVT::ppcf128)
4380b57cec5SDimitry Andric       return SINTTOFP_I128_PPCF128;
4390b57cec5SDimitry Andric   }
4400b57cec5SDimitry Andric   return UNKNOWN_LIBCALL;
4410b57cec5SDimitry Andric }
4420b57cec5SDimitry Andric 
4430b57cec5SDimitry Andric /// getUINTTOFP - Return the UINTTOFP_*_* value for the given types, or
4440b57cec5SDimitry Andric /// UNKNOWN_LIBCALL if there is none.
getUINTTOFP(EVT OpVT,EVT RetVT)4450b57cec5SDimitry Andric RTLIB::Libcall RTLIB::getUINTTOFP(EVT OpVT, EVT RetVT) {
4460b57cec5SDimitry Andric   if (OpVT == MVT::i32) {
447af732203SDimitry Andric     if (RetVT == MVT::f16)
448af732203SDimitry Andric       return UINTTOFP_I32_F16;
4490b57cec5SDimitry Andric     if (RetVT == MVT::f32)
4500b57cec5SDimitry Andric       return UINTTOFP_I32_F32;
4510b57cec5SDimitry Andric     if (RetVT == MVT::f64)
4520b57cec5SDimitry Andric       return UINTTOFP_I32_F64;
4530b57cec5SDimitry Andric     if (RetVT == MVT::f80)
4540b57cec5SDimitry Andric       return UINTTOFP_I32_F80;
4550b57cec5SDimitry Andric     if (RetVT == MVT::f128)
4560b57cec5SDimitry Andric       return UINTTOFP_I32_F128;
4570b57cec5SDimitry Andric     if (RetVT == MVT::ppcf128)
4580b57cec5SDimitry Andric       return UINTTOFP_I32_PPCF128;
4590b57cec5SDimitry Andric   } else if (OpVT == MVT::i64) {
460af732203SDimitry Andric     if (RetVT == MVT::f16)
461af732203SDimitry Andric       return UINTTOFP_I64_F16;
4620b57cec5SDimitry Andric     if (RetVT == MVT::f32)
4630b57cec5SDimitry Andric       return UINTTOFP_I64_F32;
4640b57cec5SDimitry Andric     if (RetVT == MVT::f64)
4650b57cec5SDimitry Andric       return UINTTOFP_I64_F64;
4660b57cec5SDimitry Andric     if (RetVT == MVT::f80)
4670b57cec5SDimitry Andric       return UINTTOFP_I64_F80;
4680b57cec5SDimitry Andric     if (RetVT == MVT::f128)
4690b57cec5SDimitry Andric       return UINTTOFP_I64_F128;
4700b57cec5SDimitry Andric     if (RetVT == MVT::ppcf128)
4710b57cec5SDimitry Andric       return UINTTOFP_I64_PPCF128;
4720b57cec5SDimitry Andric   } else if (OpVT == MVT::i128) {
473af732203SDimitry Andric     if (RetVT == MVT::f16)
474af732203SDimitry Andric       return UINTTOFP_I128_F16;
4750b57cec5SDimitry Andric     if (RetVT == MVT::f32)
4760b57cec5SDimitry Andric       return UINTTOFP_I128_F32;
4770b57cec5SDimitry Andric     if (RetVT == MVT::f64)
4780b57cec5SDimitry Andric       return UINTTOFP_I128_F64;
4790b57cec5SDimitry Andric     if (RetVT == MVT::f80)
4800b57cec5SDimitry Andric       return UINTTOFP_I128_F80;
4810b57cec5SDimitry Andric     if (RetVT == MVT::f128)
4820b57cec5SDimitry Andric       return UINTTOFP_I128_F128;
4830b57cec5SDimitry Andric     if (RetVT == MVT::ppcf128)
4840b57cec5SDimitry Andric       return UINTTOFP_I128_PPCF128;
4850b57cec5SDimitry Andric   }
4860b57cec5SDimitry Andric   return UNKNOWN_LIBCALL;
4870b57cec5SDimitry Andric }
4880b57cec5SDimitry Andric 
getPOWI(EVT RetVT)489*5f7ddb14SDimitry Andric RTLIB::Libcall RTLIB::getPOWI(EVT RetVT) {
490*5f7ddb14SDimitry Andric   return getFPLibCall(RetVT, POWI_F32, POWI_F64, POWI_F80, POWI_F128,
491*5f7ddb14SDimitry Andric                       POWI_PPCF128);
492*5f7ddb14SDimitry Andric }
493*5f7ddb14SDimitry Andric 
getOUTLINE_ATOMIC(unsigned Opc,AtomicOrdering Order,MVT VT)494af732203SDimitry Andric RTLIB::Libcall RTLIB::getOUTLINE_ATOMIC(unsigned Opc, AtomicOrdering Order,
495af732203SDimitry Andric                                         MVT VT) {
496af732203SDimitry Andric   unsigned ModeN, ModelN;
497af732203SDimitry Andric   switch (VT.SimpleTy) {
498af732203SDimitry Andric   case MVT::i8:
499af732203SDimitry Andric     ModeN = 0;
500af732203SDimitry Andric     break;
501af732203SDimitry Andric   case MVT::i16:
502af732203SDimitry Andric     ModeN = 1;
503af732203SDimitry Andric     break;
504af732203SDimitry Andric   case MVT::i32:
505af732203SDimitry Andric     ModeN = 2;
506af732203SDimitry Andric     break;
507af732203SDimitry Andric   case MVT::i64:
508af732203SDimitry Andric     ModeN = 3;
509af732203SDimitry Andric     break;
510af732203SDimitry Andric   case MVT::i128:
511af732203SDimitry Andric     ModeN = 4;
512af732203SDimitry Andric     break;
513af732203SDimitry Andric   default:
514af732203SDimitry Andric     return UNKNOWN_LIBCALL;
515af732203SDimitry Andric   }
516af732203SDimitry Andric 
517af732203SDimitry Andric   switch (Order) {
518af732203SDimitry Andric   case AtomicOrdering::Monotonic:
519af732203SDimitry Andric     ModelN = 0;
520af732203SDimitry Andric     break;
521af732203SDimitry Andric   case AtomicOrdering::Acquire:
522af732203SDimitry Andric     ModelN = 1;
523af732203SDimitry Andric     break;
524af732203SDimitry Andric   case AtomicOrdering::Release:
525af732203SDimitry Andric     ModelN = 2;
526af732203SDimitry Andric     break;
527af732203SDimitry Andric   case AtomicOrdering::AcquireRelease:
528af732203SDimitry Andric   case AtomicOrdering::SequentiallyConsistent:
529af732203SDimitry Andric     ModelN = 3;
530af732203SDimitry Andric     break;
531af732203SDimitry Andric   default:
532af732203SDimitry Andric     return UNKNOWN_LIBCALL;
533af732203SDimitry Andric   }
534af732203SDimitry Andric 
535af732203SDimitry Andric #define LCALLS(A, B)                                                           \
536af732203SDimitry Andric   { A##B##_RELAX, A##B##_ACQ, A##B##_REL, A##B##_ACQ_REL }
537af732203SDimitry Andric #define LCALL5(A)                                                              \
538af732203SDimitry Andric   LCALLS(A, 1), LCALLS(A, 2), LCALLS(A, 4), LCALLS(A, 8), LCALLS(A, 16)
539af732203SDimitry Andric   switch (Opc) {
540af732203SDimitry Andric   case ISD::ATOMIC_CMP_SWAP: {
541af732203SDimitry Andric     const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_CAS)};
542af732203SDimitry Andric     return LC[ModeN][ModelN];
543af732203SDimitry Andric   }
544af732203SDimitry Andric   case ISD::ATOMIC_SWAP: {
545af732203SDimitry Andric     const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_SWP)};
546af732203SDimitry Andric     return LC[ModeN][ModelN];
547af732203SDimitry Andric   }
548af732203SDimitry Andric   case ISD::ATOMIC_LOAD_ADD: {
549af732203SDimitry Andric     const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_LDADD)};
550af732203SDimitry Andric     return LC[ModeN][ModelN];
551af732203SDimitry Andric   }
552af732203SDimitry Andric   case ISD::ATOMIC_LOAD_OR: {
553af732203SDimitry Andric     const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_LDSET)};
554af732203SDimitry Andric     return LC[ModeN][ModelN];
555af732203SDimitry Andric   }
556af732203SDimitry Andric   case ISD::ATOMIC_LOAD_CLR: {
557af732203SDimitry Andric     const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_LDCLR)};
558af732203SDimitry Andric     return LC[ModeN][ModelN];
559af732203SDimitry Andric   }
560af732203SDimitry Andric   case ISD::ATOMIC_LOAD_XOR: {
561af732203SDimitry Andric     const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_LDEOR)};
562af732203SDimitry Andric     return LC[ModeN][ModelN];
563af732203SDimitry Andric   }
564af732203SDimitry Andric   default:
565af732203SDimitry Andric     return UNKNOWN_LIBCALL;
566af732203SDimitry Andric   }
567af732203SDimitry Andric #undef LCALLS
568af732203SDimitry Andric #undef LCALL5
569af732203SDimitry Andric }
570af732203SDimitry Andric 
getSYNC(unsigned Opc,MVT VT)5710b57cec5SDimitry Andric RTLIB::Libcall RTLIB::getSYNC(unsigned Opc, MVT VT) {
5720b57cec5SDimitry Andric #define OP_TO_LIBCALL(Name, Enum)                                              \
5730b57cec5SDimitry Andric   case Name:                                                                   \
5740b57cec5SDimitry Andric     switch (VT.SimpleTy) {                                                     \
5750b57cec5SDimitry Andric     default:                                                                   \
5760b57cec5SDimitry Andric       return UNKNOWN_LIBCALL;                                                  \
5770b57cec5SDimitry Andric     case MVT::i8:                                                              \
5780b57cec5SDimitry Andric       return Enum##_1;                                                         \
5790b57cec5SDimitry Andric     case MVT::i16:                                                             \
5800b57cec5SDimitry Andric       return Enum##_2;                                                         \
5810b57cec5SDimitry Andric     case MVT::i32:                                                             \
5820b57cec5SDimitry Andric       return Enum##_4;                                                         \
5830b57cec5SDimitry Andric     case MVT::i64:                                                             \
5840b57cec5SDimitry Andric       return Enum##_8;                                                         \
5850b57cec5SDimitry Andric     case MVT::i128:                                                            \
5860b57cec5SDimitry Andric       return Enum##_16;                                                        \
5870b57cec5SDimitry Andric     }
5880b57cec5SDimitry Andric 
5890b57cec5SDimitry Andric   switch (Opc) {
5900b57cec5SDimitry Andric     OP_TO_LIBCALL(ISD::ATOMIC_SWAP, SYNC_LOCK_TEST_AND_SET)
5910b57cec5SDimitry Andric     OP_TO_LIBCALL(ISD::ATOMIC_CMP_SWAP, SYNC_VAL_COMPARE_AND_SWAP)
5920b57cec5SDimitry Andric     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_ADD, SYNC_FETCH_AND_ADD)
5930b57cec5SDimitry Andric     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_SUB, SYNC_FETCH_AND_SUB)
5940b57cec5SDimitry Andric     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_AND, SYNC_FETCH_AND_AND)
5950b57cec5SDimitry Andric     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_OR, SYNC_FETCH_AND_OR)
5960b57cec5SDimitry Andric     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_XOR, SYNC_FETCH_AND_XOR)
5970b57cec5SDimitry Andric     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_NAND, SYNC_FETCH_AND_NAND)
5980b57cec5SDimitry Andric     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_MAX, SYNC_FETCH_AND_MAX)
5990b57cec5SDimitry Andric     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_UMAX, SYNC_FETCH_AND_UMAX)
6000b57cec5SDimitry Andric     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_MIN, SYNC_FETCH_AND_MIN)
6010b57cec5SDimitry Andric     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_UMIN, SYNC_FETCH_AND_UMIN)
6020b57cec5SDimitry Andric   }
6030b57cec5SDimitry Andric 
6040b57cec5SDimitry Andric #undef OP_TO_LIBCALL
6050b57cec5SDimitry Andric 
6060b57cec5SDimitry Andric   return UNKNOWN_LIBCALL;
6070b57cec5SDimitry Andric }
6080b57cec5SDimitry Andric 
getMEMCPY_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)6090b57cec5SDimitry Andric RTLIB::Libcall RTLIB::getMEMCPY_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize) {
6100b57cec5SDimitry Andric   switch (ElementSize) {
6110b57cec5SDimitry Andric   case 1:
6120b57cec5SDimitry Andric     return MEMCPY_ELEMENT_UNORDERED_ATOMIC_1;
6130b57cec5SDimitry Andric   case 2:
6140b57cec5SDimitry Andric     return MEMCPY_ELEMENT_UNORDERED_ATOMIC_2;
6150b57cec5SDimitry Andric   case 4:
6160b57cec5SDimitry Andric     return MEMCPY_ELEMENT_UNORDERED_ATOMIC_4;
6170b57cec5SDimitry Andric   case 8:
6180b57cec5SDimitry Andric     return MEMCPY_ELEMENT_UNORDERED_ATOMIC_8;
6190b57cec5SDimitry Andric   case 16:
6200b57cec5SDimitry Andric     return MEMCPY_ELEMENT_UNORDERED_ATOMIC_16;
6210b57cec5SDimitry Andric   default:
6220b57cec5SDimitry Andric     return UNKNOWN_LIBCALL;
6230b57cec5SDimitry Andric   }
6240b57cec5SDimitry Andric }
6250b57cec5SDimitry Andric 
getMEMMOVE_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)6260b57cec5SDimitry Andric RTLIB::Libcall RTLIB::getMEMMOVE_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize) {
6270b57cec5SDimitry Andric   switch (ElementSize) {
6280b57cec5SDimitry Andric   case 1:
6290b57cec5SDimitry Andric     return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_1;
6300b57cec5SDimitry Andric   case 2:
6310b57cec5SDimitry Andric     return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_2;
6320b57cec5SDimitry Andric   case 4:
6330b57cec5SDimitry Andric     return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_4;
6340b57cec5SDimitry Andric   case 8:
6350b57cec5SDimitry Andric     return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_8;
6360b57cec5SDimitry Andric   case 16:
6370b57cec5SDimitry Andric     return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_16;
6380b57cec5SDimitry Andric   default:
6390b57cec5SDimitry Andric     return UNKNOWN_LIBCALL;
6400b57cec5SDimitry Andric   }
6410b57cec5SDimitry Andric }
6420b57cec5SDimitry Andric 
getMEMSET_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)6430b57cec5SDimitry Andric RTLIB::Libcall RTLIB::getMEMSET_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize) {
6440b57cec5SDimitry Andric   switch (ElementSize) {
6450b57cec5SDimitry Andric   case 1:
6460b57cec5SDimitry Andric     return MEMSET_ELEMENT_UNORDERED_ATOMIC_1;
6470b57cec5SDimitry Andric   case 2:
6480b57cec5SDimitry Andric     return MEMSET_ELEMENT_UNORDERED_ATOMIC_2;
6490b57cec5SDimitry Andric   case 4:
6500b57cec5SDimitry Andric     return MEMSET_ELEMENT_UNORDERED_ATOMIC_4;
6510b57cec5SDimitry Andric   case 8:
6520b57cec5SDimitry Andric     return MEMSET_ELEMENT_UNORDERED_ATOMIC_8;
6530b57cec5SDimitry Andric   case 16:
6540b57cec5SDimitry Andric     return MEMSET_ELEMENT_UNORDERED_ATOMIC_16;
6550b57cec5SDimitry Andric   default:
6560b57cec5SDimitry Andric     return UNKNOWN_LIBCALL;
6570b57cec5SDimitry Andric   }
6580b57cec5SDimitry Andric }
6590b57cec5SDimitry Andric 
6600b57cec5SDimitry Andric /// InitCmpLibcallCCs - Set default comparison libcall CC.
InitCmpLibcallCCs(ISD::CondCode * CCs)6610b57cec5SDimitry Andric static void InitCmpLibcallCCs(ISD::CondCode *CCs) {
6620b57cec5SDimitry Andric   memset(CCs, ISD::SETCC_INVALID, sizeof(ISD::CondCode)*RTLIB::UNKNOWN_LIBCALL);
6630b57cec5SDimitry Andric   CCs[RTLIB::OEQ_F32] = ISD::SETEQ;
6640b57cec5SDimitry Andric   CCs[RTLIB::OEQ_F64] = ISD::SETEQ;
6650b57cec5SDimitry Andric   CCs[RTLIB::OEQ_F128] = ISD::SETEQ;
6660b57cec5SDimitry Andric   CCs[RTLIB::OEQ_PPCF128] = ISD::SETEQ;
6670b57cec5SDimitry Andric   CCs[RTLIB::UNE_F32] = ISD::SETNE;
6680b57cec5SDimitry Andric   CCs[RTLIB::UNE_F64] = ISD::SETNE;
6690b57cec5SDimitry Andric   CCs[RTLIB::UNE_F128] = ISD::SETNE;
6700b57cec5SDimitry Andric   CCs[RTLIB::UNE_PPCF128] = ISD::SETNE;
6710b57cec5SDimitry Andric   CCs[RTLIB::OGE_F32] = ISD::SETGE;
6720b57cec5SDimitry Andric   CCs[RTLIB::OGE_F64] = ISD::SETGE;
6730b57cec5SDimitry Andric   CCs[RTLIB::OGE_F128] = ISD::SETGE;
6740b57cec5SDimitry Andric   CCs[RTLIB::OGE_PPCF128] = ISD::SETGE;
6750b57cec5SDimitry Andric   CCs[RTLIB::OLT_F32] = ISD::SETLT;
6760b57cec5SDimitry Andric   CCs[RTLIB::OLT_F64] = ISD::SETLT;
6770b57cec5SDimitry Andric   CCs[RTLIB::OLT_F128] = ISD::SETLT;
6780b57cec5SDimitry Andric   CCs[RTLIB::OLT_PPCF128] = ISD::SETLT;
6790b57cec5SDimitry Andric   CCs[RTLIB::OLE_F32] = ISD::SETLE;
6800b57cec5SDimitry Andric   CCs[RTLIB::OLE_F64] = ISD::SETLE;
6810b57cec5SDimitry Andric   CCs[RTLIB::OLE_F128] = ISD::SETLE;
6820b57cec5SDimitry Andric   CCs[RTLIB::OLE_PPCF128] = ISD::SETLE;
6830b57cec5SDimitry Andric   CCs[RTLIB::OGT_F32] = ISD::SETGT;
6840b57cec5SDimitry Andric   CCs[RTLIB::OGT_F64] = ISD::SETGT;
6850b57cec5SDimitry Andric   CCs[RTLIB::OGT_F128] = ISD::SETGT;
6860b57cec5SDimitry Andric   CCs[RTLIB::OGT_PPCF128] = ISD::SETGT;
6870b57cec5SDimitry Andric   CCs[RTLIB::UO_F32] = ISD::SETNE;
6880b57cec5SDimitry Andric   CCs[RTLIB::UO_F64] = ISD::SETNE;
6890b57cec5SDimitry Andric   CCs[RTLIB::UO_F128] = ISD::SETNE;
6900b57cec5SDimitry Andric   CCs[RTLIB::UO_PPCF128] = ISD::SETNE;
6910b57cec5SDimitry Andric }
6920b57cec5SDimitry Andric 
6930b57cec5SDimitry Andric /// NOTE: The TargetMachine owns TLOF.
TargetLoweringBase(const TargetMachine & tm)6940b57cec5SDimitry Andric TargetLoweringBase::TargetLoweringBase(const TargetMachine &tm) : TM(tm) {
6950b57cec5SDimitry Andric   initActions();
6960b57cec5SDimitry Andric 
6970b57cec5SDimitry Andric   // Perform these initializations only once.
6980b57cec5SDimitry Andric   MaxStoresPerMemset = MaxStoresPerMemcpy = MaxStoresPerMemmove =
6990b57cec5SDimitry Andric       MaxLoadsPerMemcmp = 8;
7000b57cec5SDimitry Andric   MaxGluedStoresPerMemcpy = 0;
7010b57cec5SDimitry Andric   MaxStoresPerMemsetOptSize = MaxStoresPerMemcpyOptSize =
7020b57cec5SDimitry Andric       MaxStoresPerMemmoveOptSize = MaxLoadsPerMemcmpOptSize = 4;
7030b57cec5SDimitry Andric   HasMultipleConditionRegisters = false;
7040b57cec5SDimitry Andric   HasExtractBitsInsn = false;
7050b57cec5SDimitry Andric   JumpIsExpensive = JumpIsExpensiveOverride;
7060b57cec5SDimitry Andric   PredictableSelectIsExpensive = false;
7070b57cec5SDimitry Andric   EnableExtLdPromotion = false;
7080b57cec5SDimitry Andric   StackPointerRegisterToSaveRestore = 0;
7090b57cec5SDimitry Andric   BooleanContents = UndefinedBooleanContent;
7100b57cec5SDimitry Andric   BooleanFloatContents = UndefinedBooleanContent;
7110b57cec5SDimitry Andric   BooleanVectorContents = UndefinedBooleanContent;
7120b57cec5SDimitry Andric   SchedPreferenceInfo = Sched::ILP;
7130b57cec5SDimitry Andric   GatherAllAliasesMaxDepth = 18;
714480093f4SDimitry Andric   IsStrictFPEnabled = DisableStrictNodeMutation;
7150b57cec5SDimitry Andric   // TODO: the default will be switched to 0 in the next commit, along
7160b57cec5SDimitry Andric   // with the Target-specific changes necessary.
7170b57cec5SDimitry Andric   MaxAtomicSizeInBitsSupported = 1024;
7180b57cec5SDimitry Andric 
7190b57cec5SDimitry Andric   MinCmpXchgSizeInBits = 0;
7200b57cec5SDimitry Andric   SupportsUnalignedAtomics = false;
7210b57cec5SDimitry Andric 
7220b57cec5SDimitry Andric   std::fill(std::begin(LibcallRoutineNames), std::end(LibcallRoutineNames), nullptr);
7230b57cec5SDimitry Andric 
7240b57cec5SDimitry Andric   InitLibcalls(TM.getTargetTriple());
7250b57cec5SDimitry Andric   InitCmpLibcallCCs(CmpLibcallCCs);
7260b57cec5SDimitry Andric }
7270b57cec5SDimitry Andric 
initActions()7280b57cec5SDimitry Andric void TargetLoweringBase::initActions() {
7290b57cec5SDimitry Andric   // All operations default to being supported.
7300b57cec5SDimitry Andric   memset(OpActions, 0, sizeof(OpActions));
7310b57cec5SDimitry Andric   memset(LoadExtActions, 0, sizeof(LoadExtActions));
7320b57cec5SDimitry Andric   memset(TruncStoreActions, 0, sizeof(TruncStoreActions));
7330b57cec5SDimitry Andric   memset(IndexedModeActions, 0, sizeof(IndexedModeActions));
7340b57cec5SDimitry Andric   memset(CondCodeActions, 0, sizeof(CondCodeActions));
7350b57cec5SDimitry Andric   std::fill(std::begin(RegClassForVT), std::end(RegClassForVT), nullptr);
7360b57cec5SDimitry Andric   std::fill(std::begin(TargetDAGCombineArray),
7370b57cec5SDimitry Andric             std::end(TargetDAGCombineArray), 0);
7380b57cec5SDimitry Andric 
7390b57cec5SDimitry Andric   for (MVT VT : MVT::fp_valuetypes()) {
740af732203SDimitry Andric     MVT IntVT = MVT::getIntegerVT(VT.getFixedSizeInBits());
7410b57cec5SDimitry Andric     if (IntVT.isValid()) {
7420b57cec5SDimitry Andric       setOperationAction(ISD::ATOMIC_SWAP, VT, Promote);
7430b57cec5SDimitry Andric       AddPromotedToType(ISD::ATOMIC_SWAP, VT, IntVT);
7440b57cec5SDimitry Andric     }
7450b57cec5SDimitry Andric   }
7460b57cec5SDimitry Andric 
7470b57cec5SDimitry Andric   // Set default actions for various operations.
7480b57cec5SDimitry Andric   for (MVT VT : MVT::all_valuetypes()) {
7490b57cec5SDimitry Andric     // Default all indexed load / store to expand.
7500b57cec5SDimitry Andric     for (unsigned IM = (unsigned)ISD::PRE_INC;
7510b57cec5SDimitry Andric          IM != (unsigned)ISD::LAST_INDEXED_MODE; ++IM) {
7520b57cec5SDimitry Andric       setIndexedLoadAction(IM, VT, Expand);
7530b57cec5SDimitry Andric       setIndexedStoreAction(IM, VT, Expand);
754480093f4SDimitry Andric       setIndexedMaskedLoadAction(IM, VT, Expand);
755480093f4SDimitry Andric       setIndexedMaskedStoreAction(IM, VT, Expand);
7560b57cec5SDimitry Andric     }
7570b57cec5SDimitry Andric 
7580b57cec5SDimitry Andric     // Most backends expect to see the node which just returns the value loaded.
7590b57cec5SDimitry Andric     setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, VT, Expand);
7600b57cec5SDimitry Andric 
7610b57cec5SDimitry Andric     // These operations default to expand.
7620b57cec5SDimitry Andric     setOperationAction(ISD::FGETSIGN, VT, Expand);
7630b57cec5SDimitry Andric     setOperationAction(ISD::CONCAT_VECTORS, VT, Expand);
7640b57cec5SDimitry Andric     setOperationAction(ISD::FMINNUM, VT, Expand);
7650b57cec5SDimitry Andric     setOperationAction(ISD::FMAXNUM, VT, Expand);
7660b57cec5SDimitry Andric     setOperationAction(ISD::FMINNUM_IEEE, VT, Expand);
7670b57cec5SDimitry Andric     setOperationAction(ISD::FMAXNUM_IEEE, VT, Expand);
7680b57cec5SDimitry Andric     setOperationAction(ISD::FMINIMUM, VT, Expand);
7690b57cec5SDimitry Andric     setOperationAction(ISD::FMAXIMUM, VT, Expand);
7700b57cec5SDimitry Andric     setOperationAction(ISD::FMAD, VT, Expand);
7710b57cec5SDimitry Andric     setOperationAction(ISD::SMIN, VT, Expand);
7720b57cec5SDimitry Andric     setOperationAction(ISD::SMAX, VT, Expand);
7730b57cec5SDimitry Andric     setOperationAction(ISD::UMIN, VT, Expand);
7740b57cec5SDimitry Andric     setOperationAction(ISD::UMAX, VT, Expand);
7750b57cec5SDimitry Andric     setOperationAction(ISD::ABS, VT, Expand);
7760b57cec5SDimitry Andric     setOperationAction(ISD::FSHL, VT, Expand);
7770b57cec5SDimitry Andric     setOperationAction(ISD::FSHR, VT, Expand);
7780b57cec5SDimitry Andric     setOperationAction(ISD::SADDSAT, VT, Expand);
7790b57cec5SDimitry Andric     setOperationAction(ISD::UADDSAT, VT, Expand);
7800b57cec5SDimitry Andric     setOperationAction(ISD::SSUBSAT, VT, Expand);
7810b57cec5SDimitry Andric     setOperationAction(ISD::USUBSAT, VT, Expand);
782af732203SDimitry Andric     setOperationAction(ISD::SSHLSAT, VT, Expand);
783af732203SDimitry Andric     setOperationAction(ISD::USHLSAT, VT, Expand);
7840b57cec5SDimitry Andric     setOperationAction(ISD::SMULFIX, VT, Expand);
7850b57cec5SDimitry Andric     setOperationAction(ISD::SMULFIXSAT, VT, Expand);
7860b57cec5SDimitry Andric     setOperationAction(ISD::UMULFIX, VT, Expand);
7878bcb0991SDimitry Andric     setOperationAction(ISD::UMULFIXSAT, VT, Expand);
788480093f4SDimitry Andric     setOperationAction(ISD::SDIVFIX, VT, Expand);
7895ffd83dbSDimitry Andric     setOperationAction(ISD::SDIVFIXSAT, VT, Expand);
790480093f4SDimitry Andric     setOperationAction(ISD::UDIVFIX, VT, Expand);
7915ffd83dbSDimitry Andric     setOperationAction(ISD::UDIVFIXSAT, VT, Expand);
792af732203SDimitry Andric     setOperationAction(ISD::FP_TO_SINT_SAT, VT, Expand);
793af732203SDimitry Andric     setOperationAction(ISD::FP_TO_UINT_SAT, VT, Expand);
7940b57cec5SDimitry Andric 
7950b57cec5SDimitry Andric     // Overflow operations default to expand
7960b57cec5SDimitry Andric     setOperationAction(ISD::SADDO, VT, Expand);
7970b57cec5SDimitry Andric     setOperationAction(ISD::SSUBO, VT, Expand);
7980b57cec5SDimitry Andric     setOperationAction(ISD::UADDO, VT, Expand);
7990b57cec5SDimitry Andric     setOperationAction(ISD::USUBO, VT, Expand);
8000b57cec5SDimitry Andric     setOperationAction(ISD::SMULO, VT, Expand);
8010b57cec5SDimitry Andric     setOperationAction(ISD::UMULO, VT, Expand);
8020b57cec5SDimitry Andric 
8030b57cec5SDimitry Andric     // ADDCARRY operations default to expand
8040b57cec5SDimitry Andric     setOperationAction(ISD::ADDCARRY, VT, Expand);
8050b57cec5SDimitry Andric     setOperationAction(ISD::SUBCARRY, VT, Expand);
8060b57cec5SDimitry Andric     setOperationAction(ISD::SETCCCARRY, VT, Expand);
807af732203SDimitry Andric     setOperationAction(ISD::SADDO_CARRY, VT, Expand);
808af732203SDimitry Andric     setOperationAction(ISD::SSUBO_CARRY, VT, Expand);
8090b57cec5SDimitry Andric 
8100b57cec5SDimitry Andric     // ADDC/ADDE/SUBC/SUBE default to expand.
8110b57cec5SDimitry Andric     setOperationAction(ISD::ADDC, VT, Expand);
8120b57cec5SDimitry Andric     setOperationAction(ISD::ADDE, VT, Expand);
8130b57cec5SDimitry Andric     setOperationAction(ISD::SUBC, VT, Expand);
8140b57cec5SDimitry Andric     setOperationAction(ISD::SUBE, VT, Expand);
8150b57cec5SDimitry Andric 
816*5f7ddb14SDimitry Andric     // Absolute difference
817*5f7ddb14SDimitry Andric     setOperationAction(ISD::ABDS, VT, Expand);
818*5f7ddb14SDimitry Andric     setOperationAction(ISD::ABDU, VT, Expand);
819*5f7ddb14SDimitry Andric 
8200b57cec5SDimitry Andric     // These default to Expand so they will be expanded to CTLZ/CTTZ by default.
8210b57cec5SDimitry Andric     setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
8220b57cec5SDimitry Andric     setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
8230b57cec5SDimitry Andric 
8240b57cec5SDimitry Andric     setOperationAction(ISD::BITREVERSE, VT, Expand);
825af732203SDimitry Andric     setOperationAction(ISD::PARITY, VT, Expand);
8260b57cec5SDimitry Andric 
8270b57cec5SDimitry Andric     // These library functions default to expand.
8280b57cec5SDimitry Andric     setOperationAction(ISD::FROUND, VT, Expand);
8295ffd83dbSDimitry Andric     setOperationAction(ISD::FROUNDEVEN, VT, Expand);
8300b57cec5SDimitry Andric     setOperationAction(ISD::FPOWI, VT, Expand);
8310b57cec5SDimitry Andric 
8320b57cec5SDimitry Andric     // These operations default to expand for vector types.
8330b57cec5SDimitry Andric     if (VT.isVector()) {
8340b57cec5SDimitry Andric       setOperationAction(ISD::FCOPYSIGN, VT, Expand);
835480093f4SDimitry Andric       setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
8360b57cec5SDimitry Andric       setOperationAction(ISD::ANY_EXTEND_VECTOR_INREG, VT, Expand);
8370b57cec5SDimitry Andric       setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, VT, Expand);
8380b57cec5SDimitry Andric       setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, VT, Expand);
8398bcb0991SDimitry Andric       setOperationAction(ISD::SPLAT_VECTOR, VT, Expand);
8400b57cec5SDimitry Andric     }
8410b57cec5SDimitry Andric 
8420b57cec5SDimitry Andric     // Constrained floating-point operations default to expand.
8435ffd83dbSDimitry Andric #define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN)               \
844480093f4SDimitry Andric     setOperationAction(ISD::STRICT_##DAGN, VT, Expand);
845480093f4SDimitry Andric #include "llvm/IR/ConstrainedOps.def"
8460b57cec5SDimitry Andric 
8470b57cec5SDimitry Andric     // For most targets @llvm.get.dynamic.area.offset just returns 0.
8480b57cec5SDimitry Andric     setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, VT, Expand);
8490b57cec5SDimitry Andric 
8500b57cec5SDimitry Andric     // Vector reduction default to expand.
8510b57cec5SDimitry Andric     setOperationAction(ISD::VECREDUCE_FADD, VT, Expand);
8520b57cec5SDimitry Andric     setOperationAction(ISD::VECREDUCE_FMUL, VT, Expand);
8530b57cec5SDimitry Andric     setOperationAction(ISD::VECREDUCE_ADD, VT, Expand);
8540b57cec5SDimitry Andric     setOperationAction(ISD::VECREDUCE_MUL, VT, Expand);
8550b57cec5SDimitry Andric     setOperationAction(ISD::VECREDUCE_AND, VT, Expand);
8560b57cec5SDimitry Andric     setOperationAction(ISD::VECREDUCE_OR, VT, Expand);
8570b57cec5SDimitry Andric     setOperationAction(ISD::VECREDUCE_XOR, VT, Expand);
8580b57cec5SDimitry Andric     setOperationAction(ISD::VECREDUCE_SMAX, VT, Expand);
8590b57cec5SDimitry Andric     setOperationAction(ISD::VECREDUCE_SMIN, VT, Expand);
8600b57cec5SDimitry Andric     setOperationAction(ISD::VECREDUCE_UMAX, VT, Expand);
8610b57cec5SDimitry Andric     setOperationAction(ISD::VECREDUCE_UMIN, VT, Expand);
8620b57cec5SDimitry Andric     setOperationAction(ISD::VECREDUCE_FMAX, VT, Expand);
8630b57cec5SDimitry Andric     setOperationAction(ISD::VECREDUCE_FMIN, VT, Expand);
864af732203SDimitry Andric     setOperationAction(ISD::VECREDUCE_SEQ_FADD, VT, Expand);
865af732203SDimitry Andric     setOperationAction(ISD::VECREDUCE_SEQ_FMUL, VT, Expand);
866*5f7ddb14SDimitry Andric 
867*5f7ddb14SDimitry Andric     // Named vector shuffles default to expand.
868*5f7ddb14SDimitry Andric     setOperationAction(ISD::VECTOR_SPLICE, VT, Expand);
8690b57cec5SDimitry Andric   }
8700b57cec5SDimitry Andric 
8710b57cec5SDimitry Andric   // Most targets ignore the @llvm.prefetch intrinsic.
8720b57cec5SDimitry Andric   setOperationAction(ISD::PREFETCH, MVT::Other, Expand);
8730b57cec5SDimitry Andric 
8740b57cec5SDimitry Andric   // Most targets also ignore the @llvm.readcyclecounter intrinsic.
8750b57cec5SDimitry Andric   setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Expand);
8760b57cec5SDimitry Andric 
8770b57cec5SDimitry Andric   // ConstantFP nodes default to expand.  Targets can either change this to
8780b57cec5SDimitry Andric   // Legal, in which case all fp constants are legal, or use isFPImmLegal()
8790b57cec5SDimitry Andric   // to optimize expansions for certain constants.
8800b57cec5SDimitry Andric   setOperationAction(ISD::ConstantFP, MVT::f16, Expand);
8810b57cec5SDimitry Andric   setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
8820b57cec5SDimitry Andric   setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
8830b57cec5SDimitry Andric   setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
8840b57cec5SDimitry Andric   setOperationAction(ISD::ConstantFP, MVT::f128, Expand);
8850b57cec5SDimitry Andric 
8860b57cec5SDimitry Andric   // These library functions default to expand.
8870b57cec5SDimitry Andric   for (MVT VT : {MVT::f32, MVT::f64, MVT::f128}) {
8880b57cec5SDimitry Andric     setOperationAction(ISD::FCBRT,      VT, Expand);
8890b57cec5SDimitry Andric     setOperationAction(ISD::FLOG ,      VT, Expand);
8900b57cec5SDimitry Andric     setOperationAction(ISD::FLOG2,      VT, Expand);
8910b57cec5SDimitry Andric     setOperationAction(ISD::FLOG10,     VT, Expand);
8920b57cec5SDimitry Andric     setOperationAction(ISD::FEXP ,      VT, Expand);
8930b57cec5SDimitry Andric     setOperationAction(ISD::FEXP2,      VT, Expand);
8940b57cec5SDimitry Andric     setOperationAction(ISD::FFLOOR,     VT, Expand);
8950b57cec5SDimitry Andric     setOperationAction(ISD::FNEARBYINT, VT, Expand);
8960b57cec5SDimitry Andric     setOperationAction(ISD::FCEIL,      VT, Expand);
8970b57cec5SDimitry Andric     setOperationAction(ISD::FRINT,      VT, Expand);
8980b57cec5SDimitry Andric     setOperationAction(ISD::FTRUNC,     VT, Expand);
8990b57cec5SDimitry Andric     setOperationAction(ISD::FROUND,     VT, Expand);
9005ffd83dbSDimitry Andric     setOperationAction(ISD::FROUNDEVEN, VT, Expand);
9010b57cec5SDimitry Andric     setOperationAction(ISD::LROUND,     VT, Expand);
9020b57cec5SDimitry Andric     setOperationAction(ISD::LLROUND,    VT, Expand);
9030b57cec5SDimitry Andric     setOperationAction(ISD::LRINT,      VT, Expand);
9040b57cec5SDimitry Andric     setOperationAction(ISD::LLRINT,     VT, Expand);
9050b57cec5SDimitry Andric   }
9060b57cec5SDimitry Andric 
9070b57cec5SDimitry Andric   // Default ISD::TRAP to expand (which turns it into abort).
9080b57cec5SDimitry Andric   setOperationAction(ISD::TRAP, MVT::Other, Expand);
9090b57cec5SDimitry Andric 
9100b57cec5SDimitry Andric   // On most systems, DEBUGTRAP and TRAP have no difference. The "Expand"
9110b57cec5SDimitry Andric   // here is to inform DAG Legalizer to replace DEBUGTRAP with TRAP.
9120b57cec5SDimitry Andric   setOperationAction(ISD::DEBUGTRAP, MVT::Other, Expand);
913af732203SDimitry Andric 
914af732203SDimitry Andric   setOperationAction(ISD::UBSANTRAP, MVT::Other, Expand);
9150b57cec5SDimitry Andric }
9160b57cec5SDimitry Andric 
getScalarShiftAmountTy(const DataLayout & DL,EVT) const9170b57cec5SDimitry Andric MVT TargetLoweringBase::getScalarShiftAmountTy(const DataLayout &DL,
9180b57cec5SDimitry Andric                                                EVT) const {
9190b57cec5SDimitry Andric   return MVT::getIntegerVT(DL.getPointerSizeInBits(0));
9200b57cec5SDimitry Andric }
9210b57cec5SDimitry Andric 
getShiftAmountTy(EVT LHSTy,const DataLayout & DL,bool LegalTypes) const9220b57cec5SDimitry Andric EVT TargetLoweringBase::getShiftAmountTy(EVT LHSTy, const DataLayout &DL,
9230b57cec5SDimitry Andric                                          bool LegalTypes) const {
9240b57cec5SDimitry Andric   assert(LHSTy.isInteger() && "Shift amount is not an integer type!");
9250b57cec5SDimitry Andric   if (LHSTy.isVector())
9260b57cec5SDimitry Andric     return LHSTy;
9270b57cec5SDimitry Andric   return LegalTypes ? getScalarShiftAmountTy(DL, LHSTy)
9280b57cec5SDimitry Andric                     : getPointerTy(DL);
9290b57cec5SDimitry Andric }
9300b57cec5SDimitry Andric 
canOpTrap(unsigned Op,EVT VT) const9310b57cec5SDimitry Andric bool TargetLoweringBase::canOpTrap(unsigned Op, EVT VT) const {
9320b57cec5SDimitry Andric   assert(isTypeLegal(VT));
9330b57cec5SDimitry Andric   switch (Op) {
9340b57cec5SDimitry Andric   default:
9350b57cec5SDimitry Andric     return false;
9360b57cec5SDimitry Andric   case ISD::SDIV:
9370b57cec5SDimitry Andric   case ISD::UDIV:
9380b57cec5SDimitry Andric   case ISD::SREM:
9390b57cec5SDimitry Andric   case ISD::UREM:
9400b57cec5SDimitry Andric     return true;
9410b57cec5SDimitry Andric   }
9420b57cec5SDimitry Andric }
9430b57cec5SDimitry Andric 
isFreeAddrSpaceCast(unsigned SrcAS,unsigned DestAS) const944af732203SDimitry Andric bool TargetLoweringBase::isFreeAddrSpaceCast(unsigned SrcAS,
945af732203SDimitry Andric                                              unsigned DestAS) const {
946af732203SDimitry Andric   return TM.isNoopAddrSpaceCast(SrcAS, DestAS);
947af732203SDimitry Andric }
948af732203SDimitry Andric 
setJumpIsExpensive(bool isExpensive)9490b57cec5SDimitry Andric void TargetLoweringBase::setJumpIsExpensive(bool isExpensive) {
9500b57cec5SDimitry Andric   // If the command-line option was specified, ignore this request.
9510b57cec5SDimitry Andric   if (!JumpIsExpensiveOverride.getNumOccurrences())
9520b57cec5SDimitry Andric     JumpIsExpensive = isExpensive;
9530b57cec5SDimitry Andric }
9540b57cec5SDimitry Andric 
9550b57cec5SDimitry Andric TargetLoweringBase::LegalizeKind
getTypeConversion(LLVMContext & Context,EVT VT) const9560b57cec5SDimitry Andric TargetLoweringBase::getTypeConversion(LLVMContext &Context, EVT VT) const {
9570b57cec5SDimitry Andric   // If this is a simple type, use the ComputeRegisterProp mechanism.
9580b57cec5SDimitry Andric   if (VT.isSimple()) {
9590b57cec5SDimitry Andric     MVT SVT = VT.getSimpleVT();
9600b57cec5SDimitry Andric     assert((unsigned)SVT.SimpleTy < array_lengthof(TransformToType));
9610b57cec5SDimitry Andric     MVT NVT = TransformToType[SVT.SimpleTy];
9620b57cec5SDimitry Andric     LegalizeTypeAction LA = ValueTypeActions.getTypeAction(SVT);
9630b57cec5SDimitry Andric 
9640b57cec5SDimitry Andric     assert((LA == TypeLegal || LA == TypeSoftenFloat ||
9655ffd83dbSDimitry Andric             LA == TypeSoftPromoteHalf ||
9668bcb0991SDimitry Andric             (NVT.isVector() ||
9678bcb0991SDimitry Andric              ValueTypeActions.getTypeAction(NVT) != TypePromoteInteger)) &&
9680b57cec5SDimitry Andric            "Promote may not follow Expand or Promote");
9690b57cec5SDimitry Andric 
9700b57cec5SDimitry Andric     if (LA == TypeSplitVector)
971af732203SDimitry Andric       return LegalizeKind(LA, EVT(SVT).getHalfNumVectorElementsVT(Context));
9720b57cec5SDimitry Andric     if (LA == TypeScalarizeVector)
9730b57cec5SDimitry Andric       return LegalizeKind(LA, SVT.getVectorElementType());
9740b57cec5SDimitry Andric     return LegalizeKind(LA, NVT);
9750b57cec5SDimitry Andric   }
9760b57cec5SDimitry Andric 
9770b57cec5SDimitry Andric   // Handle Extended Scalar Types.
9780b57cec5SDimitry Andric   if (!VT.isVector()) {
9790b57cec5SDimitry Andric     assert(VT.isInteger() && "Float types must be simple");
9800b57cec5SDimitry Andric     unsigned BitSize = VT.getSizeInBits();
9810b57cec5SDimitry Andric     // First promote to a power-of-two size, then expand if necessary.
9820b57cec5SDimitry Andric     if (BitSize < 8 || !isPowerOf2_32(BitSize)) {
9830b57cec5SDimitry Andric       EVT NVT = VT.getRoundIntegerType(Context);
9840b57cec5SDimitry Andric       assert(NVT != VT && "Unable to round integer VT");
9850b57cec5SDimitry Andric       LegalizeKind NextStep = getTypeConversion(Context, NVT);
9860b57cec5SDimitry Andric       // Avoid multi-step promotion.
9870b57cec5SDimitry Andric       if (NextStep.first == TypePromoteInteger)
9880b57cec5SDimitry Andric         return NextStep;
9890b57cec5SDimitry Andric       // Return rounded integer type.
9900b57cec5SDimitry Andric       return LegalizeKind(TypePromoteInteger, NVT);
9910b57cec5SDimitry Andric     }
9920b57cec5SDimitry Andric 
9930b57cec5SDimitry Andric     return LegalizeKind(TypeExpandInteger,
9940b57cec5SDimitry Andric                         EVT::getIntegerVT(Context, VT.getSizeInBits() / 2));
9950b57cec5SDimitry Andric   }
9960b57cec5SDimitry Andric 
9970b57cec5SDimitry Andric   // Handle vector types.
9985ffd83dbSDimitry Andric   ElementCount NumElts = VT.getVectorElementCount();
9990b57cec5SDimitry Andric   EVT EltVT = VT.getVectorElementType();
10000b57cec5SDimitry Andric 
10010b57cec5SDimitry Andric   // Vectors with only one element are always scalarized.
1002af732203SDimitry Andric   if (NumElts.isScalar())
10030b57cec5SDimitry Andric     return LegalizeKind(TypeScalarizeVector, EltVT);
10040b57cec5SDimitry Andric 
10050b57cec5SDimitry Andric   // Try to widen vector elements until the element type is a power of two and
10060b57cec5SDimitry Andric   // promote it to a legal type later on, for example:
10070b57cec5SDimitry Andric   // <3 x i8> -> <4 x i8> -> <4 x i32>
10080b57cec5SDimitry Andric   if (EltVT.isInteger()) {
10090b57cec5SDimitry Andric     // Vectors with a number of elements that is not a power of two are always
10100b57cec5SDimitry Andric     // widened, for example <3 x i8> -> <4 x i8>.
10110b57cec5SDimitry Andric     if (!VT.isPow2VectorType()) {
1012af732203SDimitry Andric       NumElts = NumElts.coefficientNextPowerOf2();
10130b57cec5SDimitry Andric       EVT NVT = EVT::getVectorVT(Context, EltVT, NumElts);
10140b57cec5SDimitry Andric       return LegalizeKind(TypeWidenVector, NVT);
10150b57cec5SDimitry Andric     }
10160b57cec5SDimitry Andric 
10170b57cec5SDimitry Andric     // Examine the element type.
10180b57cec5SDimitry Andric     LegalizeKind LK = getTypeConversion(Context, EltVT);
10190b57cec5SDimitry Andric 
10200b57cec5SDimitry Andric     // If type is to be expanded, split the vector.
10210b57cec5SDimitry Andric     //  <4 x i140> -> <2 x i140>
1022*5f7ddb14SDimitry Andric     if (LK.first == TypeExpandInteger) {
1023*5f7ddb14SDimitry Andric       if (VT.getVectorElementCount().isScalable())
1024*5f7ddb14SDimitry Andric         return LegalizeKind(TypeScalarizeScalableVector, EltVT);
10250b57cec5SDimitry Andric       return LegalizeKind(TypeSplitVector,
1026af732203SDimitry Andric                           VT.getHalfNumVectorElementsVT(Context));
1027*5f7ddb14SDimitry Andric     }
10280b57cec5SDimitry Andric 
10290b57cec5SDimitry Andric     // Promote the integer element types until a legal vector type is found
10300b57cec5SDimitry Andric     // or until the element integer type is too big. If a legal type was not
10310b57cec5SDimitry Andric     // found, fallback to the usual mechanism of widening/splitting the
10320b57cec5SDimitry Andric     // vector.
10330b57cec5SDimitry Andric     EVT OldEltVT = EltVT;
10340b57cec5SDimitry Andric     while (true) {
10350b57cec5SDimitry Andric       // Increase the bitwidth of the element to the next pow-of-two
10360b57cec5SDimitry Andric       // (which is greater than 8 bits).
10370b57cec5SDimitry Andric       EltVT = EVT::getIntegerVT(Context, 1 + EltVT.getSizeInBits())
10380b57cec5SDimitry Andric                   .getRoundIntegerType(Context);
10390b57cec5SDimitry Andric 
10400b57cec5SDimitry Andric       // Stop trying when getting a non-simple element type.
10410b57cec5SDimitry Andric       // Note that vector elements may be greater than legal vector element
10420b57cec5SDimitry Andric       // types. Example: X86 XMM registers hold 64bit element on 32bit
10430b57cec5SDimitry Andric       // systems.
10440b57cec5SDimitry Andric       if (!EltVT.isSimple())
10450b57cec5SDimitry Andric         break;
10460b57cec5SDimitry Andric 
10470b57cec5SDimitry Andric       // Build a new vector type and check if it is legal.
10480b57cec5SDimitry Andric       MVT NVT = MVT::getVectorVT(EltVT.getSimpleVT(), NumElts);
10490b57cec5SDimitry Andric       // Found a legal promoted vector type.
10500b57cec5SDimitry Andric       if (NVT != MVT() && ValueTypeActions.getTypeAction(NVT) == TypeLegal)
10510b57cec5SDimitry Andric         return LegalizeKind(TypePromoteInteger,
10520b57cec5SDimitry Andric                             EVT::getVectorVT(Context, EltVT, NumElts));
10530b57cec5SDimitry Andric     }
10540b57cec5SDimitry Andric 
10550b57cec5SDimitry Andric     // Reset the type to the unexpanded type if we did not find a legal vector
10560b57cec5SDimitry Andric     // type with a promoted vector element type.
10570b57cec5SDimitry Andric     EltVT = OldEltVT;
10580b57cec5SDimitry Andric   }
10590b57cec5SDimitry Andric 
10600b57cec5SDimitry Andric   // Try to widen the vector until a legal type is found.
10610b57cec5SDimitry Andric   // If there is no wider legal type, split the vector.
10620b57cec5SDimitry Andric   while (true) {
10630b57cec5SDimitry Andric     // Round up to the next power of 2.
1064af732203SDimitry Andric     NumElts = NumElts.coefficientNextPowerOf2();
10650b57cec5SDimitry Andric 
10660b57cec5SDimitry Andric     // If there is no simple vector type with this many elements then there
10670b57cec5SDimitry Andric     // cannot be a larger legal vector type.  Note that this assumes that
10680b57cec5SDimitry Andric     // there are no skipped intermediate vector types in the simple types.
10690b57cec5SDimitry Andric     if (!EltVT.isSimple())
10700b57cec5SDimitry Andric       break;
10710b57cec5SDimitry Andric     MVT LargerVector = MVT::getVectorVT(EltVT.getSimpleVT(), NumElts);
10720b57cec5SDimitry Andric     if (LargerVector == MVT())
10730b57cec5SDimitry Andric       break;
10740b57cec5SDimitry Andric 
10750b57cec5SDimitry Andric     // If this type is legal then widen the vector.
10760b57cec5SDimitry Andric     if (ValueTypeActions.getTypeAction(LargerVector) == TypeLegal)
10770b57cec5SDimitry Andric       return LegalizeKind(TypeWidenVector, LargerVector);
10780b57cec5SDimitry Andric   }
10790b57cec5SDimitry Andric 
10800b57cec5SDimitry Andric   // Widen odd vectors to next power of two.
10810b57cec5SDimitry Andric   if (!VT.isPow2VectorType()) {
10820b57cec5SDimitry Andric     EVT NVT = VT.getPow2VectorType(Context);
10830b57cec5SDimitry Andric     return LegalizeKind(TypeWidenVector, NVT);
10840b57cec5SDimitry Andric   }
10850b57cec5SDimitry Andric 
1086*5f7ddb14SDimitry Andric   if (VT.getVectorElementCount() == ElementCount::getScalable(1))
1087*5f7ddb14SDimitry Andric     return LegalizeKind(TypeScalarizeScalableVector, EltVT);
1088*5f7ddb14SDimitry Andric 
10890b57cec5SDimitry Andric   // Vectors with illegal element types are expanded.
1090af732203SDimitry Andric   EVT NVT = EVT::getVectorVT(Context, EltVT,
1091af732203SDimitry Andric                              VT.getVectorElementCount().divideCoefficientBy(2));
10920b57cec5SDimitry Andric   return LegalizeKind(TypeSplitVector, NVT);
10930b57cec5SDimitry Andric }
10940b57cec5SDimitry Andric 
getVectorTypeBreakdownMVT(MVT VT,MVT & IntermediateVT,unsigned & NumIntermediates,MVT & RegisterVT,TargetLoweringBase * TLI)10950b57cec5SDimitry Andric static unsigned getVectorTypeBreakdownMVT(MVT VT, MVT &IntermediateVT,
10960b57cec5SDimitry Andric                                           unsigned &NumIntermediates,
10970b57cec5SDimitry Andric                                           MVT &RegisterVT,
10980b57cec5SDimitry Andric                                           TargetLoweringBase *TLI) {
10990b57cec5SDimitry Andric   // Figure out the right, legal destination reg to copy into.
11005ffd83dbSDimitry Andric   ElementCount EC = VT.getVectorElementCount();
11010b57cec5SDimitry Andric   MVT EltTy = VT.getVectorElementType();
11020b57cec5SDimitry Andric 
11030b57cec5SDimitry Andric   unsigned NumVectorRegs = 1;
11040b57cec5SDimitry Andric 
11055ffd83dbSDimitry Andric   // Scalable vectors cannot be scalarized, so splitting or widening is
11065ffd83dbSDimitry Andric   // required.
1107af732203SDimitry Andric   if (VT.isScalableVector() && !isPowerOf2_32(EC.getKnownMinValue()))
11085ffd83dbSDimitry Andric     llvm_unreachable(
11095ffd83dbSDimitry Andric         "Splitting or widening of non-power-of-2 MVTs is not implemented.");
11105ffd83dbSDimitry Andric 
11115ffd83dbSDimitry Andric   // FIXME: We don't support non-power-of-2-sized vectors for now.
11125ffd83dbSDimitry Andric   // Ideally we could break down into LHS/RHS like LegalizeDAG does.
1113af732203SDimitry Andric   if (!isPowerOf2_32(EC.getKnownMinValue())) {
11145ffd83dbSDimitry Andric     // Split EC to unit size (scalable property is preserved).
1115af732203SDimitry Andric     NumVectorRegs = EC.getKnownMinValue();
1116af732203SDimitry Andric     EC = ElementCount::getFixed(1);
11170b57cec5SDimitry Andric   }
11180b57cec5SDimitry Andric 
11195ffd83dbSDimitry Andric   // Divide the input until we get to a supported size. This will
11205ffd83dbSDimitry Andric   // always end up with an EC that represent a scalar or a scalable
11215ffd83dbSDimitry Andric   // scalar.
1122af732203SDimitry Andric   while (EC.getKnownMinValue() > 1 &&
1123af732203SDimitry Andric          !TLI->isTypeLegal(MVT::getVectorVT(EltTy, EC))) {
1124af732203SDimitry Andric     EC = EC.divideCoefficientBy(2);
11250b57cec5SDimitry Andric     NumVectorRegs <<= 1;
11260b57cec5SDimitry Andric   }
11270b57cec5SDimitry Andric 
11280b57cec5SDimitry Andric   NumIntermediates = NumVectorRegs;
11290b57cec5SDimitry Andric 
11305ffd83dbSDimitry Andric   MVT NewVT = MVT::getVectorVT(EltTy, EC);
11310b57cec5SDimitry Andric   if (!TLI->isTypeLegal(NewVT))
11320b57cec5SDimitry Andric     NewVT = EltTy;
11330b57cec5SDimitry Andric   IntermediateVT = NewVT;
11340b57cec5SDimitry Andric 
1135af732203SDimitry Andric   unsigned LaneSizeInBits = NewVT.getScalarSizeInBits();
11360b57cec5SDimitry Andric 
11370b57cec5SDimitry Andric   // Convert sizes such as i33 to i64.
11385ffd83dbSDimitry Andric   if (!isPowerOf2_32(LaneSizeInBits))
11395ffd83dbSDimitry Andric     LaneSizeInBits = NextPowerOf2(LaneSizeInBits);
11400b57cec5SDimitry Andric 
11410b57cec5SDimitry Andric   MVT DestVT = TLI->getRegisterType(NewVT);
11420b57cec5SDimitry Andric   RegisterVT = DestVT;
11430b57cec5SDimitry Andric   if (EVT(DestVT).bitsLT(NewVT))    // Value is expanded, e.g. i64 -> i16.
1144af732203SDimitry Andric     return NumVectorRegs * (LaneSizeInBits / DestVT.getScalarSizeInBits());
11450b57cec5SDimitry Andric 
11460b57cec5SDimitry Andric   // Otherwise, promotion or legal types use the same number of registers as
11470b57cec5SDimitry Andric   // the vector decimated to the appropriate level.
11480b57cec5SDimitry Andric   return NumVectorRegs;
11490b57cec5SDimitry Andric }
11500b57cec5SDimitry Andric 
11510b57cec5SDimitry Andric /// isLegalRC - Return true if the value types that can be represented by the
11520b57cec5SDimitry Andric /// specified register class are all legal.
isLegalRC(const TargetRegisterInfo & TRI,const TargetRegisterClass & RC) const11530b57cec5SDimitry Andric bool TargetLoweringBase::isLegalRC(const TargetRegisterInfo &TRI,
11540b57cec5SDimitry Andric                                    const TargetRegisterClass &RC) const {
11550b57cec5SDimitry Andric   for (auto I = TRI.legalclasstypes_begin(RC); *I != MVT::Other; ++I)
11560b57cec5SDimitry Andric     if (isTypeLegal(*I))
11570b57cec5SDimitry Andric       return true;
11580b57cec5SDimitry Andric   return false;
11590b57cec5SDimitry Andric }
11600b57cec5SDimitry Andric 
11610b57cec5SDimitry Andric /// Replace/modify any TargetFrameIndex operands with a targte-dependent
11620b57cec5SDimitry Andric /// sequence of memory operands that is recognized by PrologEpilogInserter.
11630b57cec5SDimitry Andric MachineBasicBlock *
emitPatchPoint(MachineInstr & InitialMI,MachineBasicBlock * MBB) const11640b57cec5SDimitry Andric TargetLoweringBase::emitPatchPoint(MachineInstr &InitialMI,
11650b57cec5SDimitry Andric                                    MachineBasicBlock *MBB) const {
11660b57cec5SDimitry Andric   MachineInstr *MI = &InitialMI;
11670b57cec5SDimitry Andric   MachineFunction &MF = *MI->getMF();
11680b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
11690b57cec5SDimitry Andric 
11700b57cec5SDimitry Andric   // We're handling multiple types of operands here:
11710b57cec5SDimitry Andric   // PATCHPOINT MetaArgs - live-in, read only, direct
11720b57cec5SDimitry Andric   // STATEPOINT Deopt Spill - live-through, read only, indirect
11730b57cec5SDimitry Andric   // STATEPOINT Deopt Alloca - live-through, read only, direct
11740b57cec5SDimitry Andric   // (We're currently conservative and mark the deopt slots read/write in
11750b57cec5SDimitry Andric   // practice.)
11760b57cec5SDimitry Andric   // STATEPOINT GC Spill - live-through, read/write, indirect
11770b57cec5SDimitry Andric   // STATEPOINT GC Alloca - live-through, read/write, direct
11780b57cec5SDimitry Andric   // The live-in vs live-through is handled already (the live through ones are
11790b57cec5SDimitry Andric   // all stack slots), but we need to handle the different type of stackmap
11800b57cec5SDimitry Andric   // operands and memory effects here.
11810b57cec5SDimitry Andric 
11825ffd83dbSDimitry Andric   if (!llvm::any_of(MI->operands(),
11835ffd83dbSDimitry Andric                     [](MachineOperand &Operand) { return Operand.isFI(); }))
11845ffd83dbSDimitry Andric     return MBB;
11855ffd83dbSDimitry Andric 
11865ffd83dbSDimitry Andric   MachineInstrBuilder MIB = BuildMI(MF, MI->getDebugLoc(), MI->getDesc());
11875ffd83dbSDimitry Andric 
11885ffd83dbSDimitry Andric   // Inherit previous memory operands.
11895ffd83dbSDimitry Andric   MIB.cloneMemRefs(*MI);
11905ffd83dbSDimitry Andric 
1191af732203SDimitry Andric   for (unsigned i = 0; i < MI->getNumOperands(); ++i) {
1192af732203SDimitry Andric     MachineOperand &MO = MI->getOperand(i);
11935ffd83dbSDimitry Andric     if (!MO.isFI()) {
1194af732203SDimitry Andric       // Index of Def operand this Use it tied to.
1195af732203SDimitry Andric       // Since Defs are coming before Uses, if Use is tied, then
1196af732203SDimitry Andric       // index of Def must be smaller that index of that Use.
1197af732203SDimitry Andric       // Also, Defs preserve their position in new MI.
1198af732203SDimitry Andric       unsigned TiedTo = i;
1199af732203SDimitry Andric       if (MO.isReg() && MO.isTied())
1200af732203SDimitry Andric         TiedTo = MI->findTiedOperandIdx(i);
12015ffd83dbSDimitry Andric       MIB.add(MO);
1202af732203SDimitry Andric       if (TiedTo < i)
1203af732203SDimitry Andric         MIB->tieOperands(TiedTo, MIB->getNumOperands() - 1);
12040b57cec5SDimitry Andric       continue;
12055ffd83dbSDimitry Andric     }
12060b57cec5SDimitry Andric 
12070b57cec5SDimitry Andric     // foldMemoryOperand builds a new MI after replacing a single FI operand
12080b57cec5SDimitry Andric     // with the canonical set of five x86 addressing-mode operands.
12090b57cec5SDimitry Andric     int FI = MO.getIndex();
12100b57cec5SDimitry Andric 
12110b57cec5SDimitry Andric     // Add frame index operands recognized by stackmaps.cpp
12120b57cec5SDimitry Andric     if (MFI.isStatepointSpillSlotObjectIndex(FI)) {
12130b57cec5SDimitry Andric       // indirect-mem-ref tag, size, #FI, offset.
12140b57cec5SDimitry Andric       // Used for spills inserted by StatepointLowering.  This codepath is not
12150b57cec5SDimitry Andric       // used for patchpoints/stackmaps at all, for these spilling is done via
12160b57cec5SDimitry Andric       // foldMemoryOperand callback only.
12170b57cec5SDimitry Andric       assert(MI->getOpcode() == TargetOpcode::STATEPOINT && "sanity");
12180b57cec5SDimitry Andric       MIB.addImm(StackMaps::IndirectMemRefOp);
12190b57cec5SDimitry Andric       MIB.addImm(MFI.getObjectSize(FI));
12205ffd83dbSDimitry Andric       MIB.add(MO);
12210b57cec5SDimitry Andric       MIB.addImm(0);
12220b57cec5SDimitry Andric     } else {
12230b57cec5SDimitry Andric       // direct-mem-ref tag, #FI, offset.
12240b57cec5SDimitry Andric       // Used by patchpoint, and direct alloca arguments to statepoints
12250b57cec5SDimitry Andric       MIB.addImm(StackMaps::DirectMemRefOp);
12265ffd83dbSDimitry Andric       MIB.add(MO);
12270b57cec5SDimitry Andric       MIB.addImm(0);
12280b57cec5SDimitry Andric     }
12290b57cec5SDimitry Andric 
12300b57cec5SDimitry Andric     assert(MIB->mayLoad() && "Folded a stackmap use to a non-load!");
12310b57cec5SDimitry Andric 
12320b57cec5SDimitry Andric     // Add a new memory operand for this FI.
12330b57cec5SDimitry Andric     assert(MFI.getObjectOffset(FI) != -1);
12340b57cec5SDimitry Andric 
12350b57cec5SDimitry Andric     // Note: STATEPOINT MMOs are added during SelectionDAG.  STACKMAP, and
12360b57cec5SDimitry Andric     // PATCHPOINT should be updated to do the same. (TODO)
12370b57cec5SDimitry Andric     if (MI->getOpcode() != TargetOpcode::STATEPOINT) {
12380b57cec5SDimitry Andric       auto Flags = MachineMemOperand::MOLoad;
12390b57cec5SDimitry Andric       MachineMemOperand *MMO = MF.getMachineMemOperand(
12400b57cec5SDimitry Andric           MachinePointerInfo::getFixedStack(MF, FI), Flags,
12415ffd83dbSDimitry Andric           MF.getDataLayout().getPointerSize(), MFI.getObjectAlign(FI));
12420b57cec5SDimitry Andric       MIB->addMemOperand(MF, MMO);
12430b57cec5SDimitry Andric     }
12440b57cec5SDimitry Andric   }
12455ffd83dbSDimitry Andric   MBB->insert(MachineBasicBlock::iterator(MI), MIB);
12465ffd83dbSDimitry Andric   MI->eraseFromParent();
12470b57cec5SDimitry Andric   return MBB;
12480b57cec5SDimitry Andric }
12490b57cec5SDimitry Andric 
12500b57cec5SDimitry Andric /// findRepresentativeClass - Return the largest legal super-reg register class
12510b57cec5SDimitry Andric /// of the register class for the specified type and its associated "cost".
12520b57cec5SDimitry Andric // This function is in TargetLowering because it uses RegClassForVT which would
12530b57cec5SDimitry Andric // need to be moved to TargetRegisterInfo and would necessitate moving
12540b57cec5SDimitry Andric // isTypeLegal over as well - a massive change that would just require
12550b57cec5SDimitry Andric // TargetLowering having a TargetRegisterInfo class member that it would use.
12560b57cec5SDimitry Andric std::pair<const TargetRegisterClass *, uint8_t>
findRepresentativeClass(const TargetRegisterInfo * TRI,MVT VT) const12570b57cec5SDimitry Andric TargetLoweringBase::findRepresentativeClass(const TargetRegisterInfo *TRI,
12580b57cec5SDimitry Andric                                             MVT VT) const {
12590b57cec5SDimitry Andric   const TargetRegisterClass *RC = RegClassForVT[VT.SimpleTy];
12600b57cec5SDimitry Andric   if (!RC)
12610b57cec5SDimitry Andric     return std::make_pair(RC, 0);
12620b57cec5SDimitry Andric 
12630b57cec5SDimitry Andric   // Compute the set of all super-register classes.
12640b57cec5SDimitry Andric   BitVector SuperRegRC(TRI->getNumRegClasses());
12650b57cec5SDimitry Andric   for (SuperRegClassIterator RCI(RC, TRI); RCI.isValid(); ++RCI)
12660b57cec5SDimitry Andric     SuperRegRC.setBitsInMask(RCI.getMask());
12670b57cec5SDimitry Andric 
12680b57cec5SDimitry Andric   // Find the first legal register class with the largest spill size.
12690b57cec5SDimitry Andric   const TargetRegisterClass *BestRC = RC;
12700b57cec5SDimitry Andric   for (unsigned i : SuperRegRC.set_bits()) {
12710b57cec5SDimitry Andric     const TargetRegisterClass *SuperRC = TRI->getRegClass(i);
12720b57cec5SDimitry Andric     // We want the largest possible spill size.
12730b57cec5SDimitry Andric     if (TRI->getSpillSize(*SuperRC) <= TRI->getSpillSize(*BestRC))
12740b57cec5SDimitry Andric       continue;
12750b57cec5SDimitry Andric     if (!isLegalRC(*TRI, *SuperRC))
12760b57cec5SDimitry Andric       continue;
12770b57cec5SDimitry Andric     BestRC = SuperRC;
12780b57cec5SDimitry Andric   }
12790b57cec5SDimitry Andric   return std::make_pair(BestRC, 1);
12800b57cec5SDimitry Andric }
12810b57cec5SDimitry Andric 
12820b57cec5SDimitry Andric /// computeRegisterProperties - Once all of the register classes are added,
12830b57cec5SDimitry Andric /// this allows us to compute derived properties we expose.
computeRegisterProperties(const TargetRegisterInfo * TRI)12840b57cec5SDimitry Andric void TargetLoweringBase::computeRegisterProperties(
12850b57cec5SDimitry Andric     const TargetRegisterInfo *TRI) {
1286*5f7ddb14SDimitry Andric   static_assert(MVT::VALUETYPE_SIZE <= MVT::MAX_ALLOWED_VALUETYPE,
12870b57cec5SDimitry Andric                 "Too many value types for ValueTypeActions to hold!");
12880b57cec5SDimitry Andric 
12890b57cec5SDimitry Andric   // Everything defaults to needing one register.
1290*5f7ddb14SDimitry Andric   for (unsigned i = 0; i != MVT::VALUETYPE_SIZE; ++i) {
12910b57cec5SDimitry Andric     NumRegistersForVT[i] = 1;
12920b57cec5SDimitry Andric     RegisterTypeForVT[i] = TransformToType[i] = (MVT::SimpleValueType)i;
12930b57cec5SDimitry Andric   }
12940b57cec5SDimitry Andric   // ...except isVoid, which doesn't need any registers.
12950b57cec5SDimitry Andric   NumRegistersForVT[MVT::isVoid] = 0;
12960b57cec5SDimitry Andric 
12970b57cec5SDimitry Andric   // Find the largest integer register class.
12980b57cec5SDimitry Andric   unsigned LargestIntReg = MVT::LAST_INTEGER_VALUETYPE;
12990b57cec5SDimitry Andric   for (; RegClassForVT[LargestIntReg] == nullptr; --LargestIntReg)
13000b57cec5SDimitry Andric     assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
13010b57cec5SDimitry Andric 
13020b57cec5SDimitry Andric   // Every integer value type larger than this largest register takes twice as
13030b57cec5SDimitry Andric   // many registers to represent as the previous ValueType.
13040b57cec5SDimitry Andric   for (unsigned ExpandedReg = LargestIntReg + 1;
13050b57cec5SDimitry Andric        ExpandedReg <= MVT::LAST_INTEGER_VALUETYPE; ++ExpandedReg) {
13060b57cec5SDimitry Andric     NumRegistersForVT[ExpandedReg] = 2*NumRegistersForVT[ExpandedReg-1];
13070b57cec5SDimitry Andric     RegisterTypeForVT[ExpandedReg] = (MVT::SimpleValueType)LargestIntReg;
13080b57cec5SDimitry Andric     TransformToType[ExpandedReg] = (MVT::SimpleValueType)(ExpandedReg - 1);
13090b57cec5SDimitry Andric     ValueTypeActions.setTypeAction((MVT::SimpleValueType)ExpandedReg,
13100b57cec5SDimitry Andric                                    TypeExpandInteger);
13110b57cec5SDimitry Andric   }
13120b57cec5SDimitry Andric 
13130b57cec5SDimitry Andric   // Inspect all of the ValueType's smaller than the largest integer
13140b57cec5SDimitry Andric   // register to see which ones need promotion.
13150b57cec5SDimitry Andric   unsigned LegalIntReg = LargestIntReg;
13160b57cec5SDimitry Andric   for (unsigned IntReg = LargestIntReg - 1;
13170b57cec5SDimitry Andric        IntReg >= (unsigned)MVT::i1; --IntReg) {
13180b57cec5SDimitry Andric     MVT IVT = (MVT::SimpleValueType)IntReg;
13190b57cec5SDimitry Andric     if (isTypeLegal(IVT)) {
13200b57cec5SDimitry Andric       LegalIntReg = IntReg;
13210b57cec5SDimitry Andric     } else {
13220b57cec5SDimitry Andric       RegisterTypeForVT[IntReg] = TransformToType[IntReg] =
13230b57cec5SDimitry Andric         (MVT::SimpleValueType)LegalIntReg;
13240b57cec5SDimitry Andric       ValueTypeActions.setTypeAction(IVT, TypePromoteInteger);
13250b57cec5SDimitry Andric     }
13260b57cec5SDimitry Andric   }
13270b57cec5SDimitry Andric 
13280b57cec5SDimitry Andric   // ppcf128 type is really two f64's.
13290b57cec5SDimitry Andric   if (!isTypeLegal(MVT::ppcf128)) {
13300b57cec5SDimitry Andric     if (isTypeLegal(MVT::f64)) {
13310b57cec5SDimitry Andric       NumRegistersForVT[MVT::ppcf128] = 2*NumRegistersForVT[MVT::f64];
13320b57cec5SDimitry Andric       RegisterTypeForVT[MVT::ppcf128] = MVT::f64;
13330b57cec5SDimitry Andric       TransformToType[MVT::ppcf128] = MVT::f64;
13340b57cec5SDimitry Andric       ValueTypeActions.setTypeAction(MVT::ppcf128, TypeExpandFloat);
13350b57cec5SDimitry Andric     } else {
13360b57cec5SDimitry Andric       NumRegistersForVT[MVT::ppcf128] = NumRegistersForVT[MVT::i128];
13370b57cec5SDimitry Andric       RegisterTypeForVT[MVT::ppcf128] = RegisterTypeForVT[MVT::i128];
13380b57cec5SDimitry Andric       TransformToType[MVT::ppcf128] = MVT::i128;
13390b57cec5SDimitry Andric       ValueTypeActions.setTypeAction(MVT::ppcf128, TypeSoftenFloat);
13400b57cec5SDimitry Andric     }
13410b57cec5SDimitry Andric   }
13420b57cec5SDimitry Andric 
13430b57cec5SDimitry Andric   // Decide how to handle f128. If the target does not have native f128 support,
13440b57cec5SDimitry Andric   // expand it to i128 and we will be generating soft float library calls.
13450b57cec5SDimitry Andric   if (!isTypeLegal(MVT::f128)) {
13460b57cec5SDimitry Andric     NumRegistersForVT[MVT::f128] = NumRegistersForVT[MVT::i128];
13470b57cec5SDimitry Andric     RegisterTypeForVT[MVT::f128] = RegisterTypeForVT[MVT::i128];
13480b57cec5SDimitry Andric     TransformToType[MVT::f128] = MVT::i128;
13490b57cec5SDimitry Andric     ValueTypeActions.setTypeAction(MVT::f128, TypeSoftenFloat);
13500b57cec5SDimitry Andric   }
13510b57cec5SDimitry Andric 
13520b57cec5SDimitry Andric   // Decide how to handle f64. If the target does not have native f64 support,
13530b57cec5SDimitry Andric   // expand it to i64 and we will be generating soft float library calls.
13540b57cec5SDimitry Andric   if (!isTypeLegal(MVT::f64)) {
13550b57cec5SDimitry Andric     NumRegistersForVT[MVT::f64] = NumRegistersForVT[MVT::i64];
13560b57cec5SDimitry Andric     RegisterTypeForVT[MVT::f64] = RegisterTypeForVT[MVT::i64];
13570b57cec5SDimitry Andric     TransformToType[MVT::f64] = MVT::i64;
13580b57cec5SDimitry Andric     ValueTypeActions.setTypeAction(MVT::f64, TypeSoftenFloat);
13590b57cec5SDimitry Andric   }
13600b57cec5SDimitry Andric 
13610b57cec5SDimitry Andric   // Decide how to handle f32. If the target does not have native f32 support,
13620b57cec5SDimitry Andric   // expand it to i32 and we will be generating soft float library calls.
13630b57cec5SDimitry Andric   if (!isTypeLegal(MVT::f32)) {
13640b57cec5SDimitry Andric     NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::i32];
13650b57cec5SDimitry Andric     RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::i32];
13660b57cec5SDimitry Andric     TransformToType[MVT::f32] = MVT::i32;
13670b57cec5SDimitry Andric     ValueTypeActions.setTypeAction(MVT::f32, TypeSoftenFloat);
13680b57cec5SDimitry Andric   }
13690b57cec5SDimitry Andric 
13700b57cec5SDimitry Andric   // Decide how to handle f16. If the target does not have native f16 support,
13710b57cec5SDimitry Andric   // promote it to f32, because there are no f16 library calls (except for
13720b57cec5SDimitry Andric   // conversions).
13730b57cec5SDimitry Andric   if (!isTypeLegal(MVT::f16)) {
13745ffd83dbSDimitry Andric     // Allow targets to control how we legalize half.
13755ffd83dbSDimitry Andric     if (softPromoteHalfType()) {
13765ffd83dbSDimitry Andric       NumRegistersForVT[MVT::f16] = NumRegistersForVT[MVT::i16];
13775ffd83dbSDimitry Andric       RegisterTypeForVT[MVT::f16] = RegisterTypeForVT[MVT::i16];
13785ffd83dbSDimitry Andric       TransformToType[MVT::f16] = MVT::f32;
13795ffd83dbSDimitry Andric       ValueTypeActions.setTypeAction(MVT::f16, TypeSoftPromoteHalf);
13805ffd83dbSDimitry Andric     } else {
13810b57cec5SDimitry Andric       NumRegistersForVT[MVT::f16] = NumRegistersForVT[MVT::f32];
13820b57cec5SDimitry Andric       RegisterTypeForVT[MVT::f16] = RegisterTypeForVT[MVT::f32];
13830b57cec5SDimitry Andric       TransformToType[MVT::f16] = MVT::f32;
13840b57cec5SDimitry Andric       ValueTypeActions.setTypeAction(MVT::f16, TypePromoteFloat);
13850b57cec5SDimitry Andric     }
13865ffd83dbSDimitry Andric   }
13870b57cec5SDimitry Andric 
13880b57cec5SDimitry Andric   // Loop over all of the vector value types to see which need transformations.
13890b57cec5SDimitry Andric   for (unsigned i = MVT::FIRST_VECTOR_VALUETYPE;
13900b57cec5SDimitry Andric        i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
13910b57cec5SDimitry Andric     MVT VT = (MVT::SimpleValueType) i;
13920b57cec5SDimitry Andric     if (isTypeLegal(VT))
13930b57cec5SDimitry Andric       continue;
13940b57cec5SDimitry Andric 
13950b57cec5SDimitry Andric     MVT EltVT = VT.getVectorElementType();
13965ffd83dbSDimitry Andric     ElementCount EC = VT.getVectorElementCount();
13970b57cec5SDimitry Andric     bool IsLegalWiderType = false;
13988bcb0991SDimitry Andric     bool IsScalable = VT.isScalableVector();
13990b57cec5SDimitry Andric     LegalizeTypeAction PreferredAction = getPreferredVectorAction(VT);
14000b57cec5SDimitry Andric     switch (PreferredAction) {
14018bcb0991SDimitry Andric     case TypePromoteInteger: {
14028bcb0991SDimitry Andric       MVT::SimpleValueType EndVT = IsScalable ?
14038bcb0991SDimitry Andric                                    MVT::LAST_INTEGER_SCALABLE_VECTOR_VALUETYPE :
14048bcb0991SDimitry Andric                                    MVT::LAST_INTEGER_FIXEDLEN_VECTOR_VALUETYPE;
14050b57cec5SDimitry Andric       // Try to promote the elements of integer vectors. If no legal
14060b57cec5SDimitry Andric       // promotion was found, fall through to the widen-vector method.
14078bcb0991SDimitry Andric       for (unsigned nVT = i + 1;
14088bcb0991SDimitry Andric            (MVT::SimpleValueType)nVT <= EndVT; ++nVT) {
14090b57cec5SDimitry Andric         MVT SVT = (MVT::SimpleValueType) nVT;
14100b57cec5SDimitry Andric         // Promote vectors of integers to vectors with the same number
14110b57cec5SDimitry Andric         // of elements, with a wider element type.
1412af732203SDimitry Andric         if (SVT.getScalarSizeInBits() > EltVT.getFixedSizeInBits() &&
14135ffd83dbSDimitry Andric             SVT.getVectorElementCount() == EC && isTypeLegal(SVT)) {
14140b57cec5SDimitry Andric           TransformToType[i] = SVT;
14150b57cec5SDimitry Andric           RegisterTypeForVT[i] = SVT;
14160b57cec5SDimitry Andric           NumRegistersForVT[i] = 1;
14170b57cec5SDimitry Andric           ValueTypeActions.setTypeAction(VT, TypePromoteInteger);
14180b57cec5SDimitry Andric           IsLegalWiderType = true;
14190b57cec5SDimitry Andric           break;
14200b57cec5SDimitry Andric         }
14210b57cec5SDimitry Andric       }
14220b57cec5SDimitry Andric       if (IsLegalWiderType)
14230b57cec5SDimitry Andric         break;
14240b57cec5SDimitry Andric       LLVM_FALLTHROUGH;
14258bcb0991SDimitry Andric     }
14260b57cec5SDimitry Andric 
14270b57cec5SDimitry Andric     case TypeWidenVector:
1428af732203SDimitry Andric       if (isPowerOf2_32(EC.getKnownMinValue())) {
14290b57cec5SDimitry Andric         // Try to widen the vector.
14300b57cec5SDimitry Andric         for (unsigned nVT = i + 1; nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
14310b57cec5SDimitry Andric           MVT SVT = (MVT::SimpleValueType) nVT;
14325ffd83dbSDimitry Andric           if (SVT.getVectorElementType() == EltVT &&
14335ffd83dbSDimitry Andric               SVT.isScalableVector() == IsScalable &&
1434af732203SDimitry Andric               SVT.getVectorElementCount().getKnownMinValue() >
1435af732203SDimitry Andric                   EC.getKnownMinValue() &&
1436af732203SDimitry Andric               isTypeLegal(SVT)) {
14370b57cec5SDimitry Andric             TransformToType[i] = SVT;
14380b57cec5SDimitry Andric             RegisterTypeForVT[i] = SVT;
14390b57cec5SDimitry Andric             NumRegistersForVT[i] = 1;
14400b57cec5SDimitry Andric             ValueTypeActions.setTypeAction(VT, TypeWidenVector);
14410b57cec5SDimitry Andric             IsLegalWiderType = true;
14420b57cec5SDimitry Andric             break;
14430b57cec5SDimitry Andric           }
14440b57cec5SDimitry Andric         }
14450b57cec5SDimitry Andric         if (IsLegalWiderType)
14460b57cec5SDimitry Andric           break;
14478bcb0991SDimitry Andric       } else {
14488bcb0991SDimitry Andric         // Only widen to the next power of 2 to keep consistency with EVT.
14498bcb0991SDimitry Andric         MVT NVT = VT.getPow2VectorType();
14508bcb0991SDimitry Andric         if (isTypeLegal(NVT)) {
14518bcb0991SDimitry Andric           TransformToType[i] = NVT;
14528bcb0991SDimitry Andric           ValueTypeActions.setTypeAction(VT, TypeWidenVector);
14538bcb0991SDimitry Andric           RegisterTypeForVT[i] = NVT;
14548bcb0991SDimitry Andric           NumRegistersForVT[i] = 1;
14558bcb0991SDimitry Andric           break;
14568bcb0991SDimitry Andric         }
14578bcb0991SDimitry Andric       }
14580b57cec5SDimitry Andric       LLVM_FALLTHROUGH;
14590b57cec5SDimitry Andric 
14600b57cec5SDimitry Andric     case TypeSplitVector:
14610b57cec5SDimitry Andric     case TypeScalarizeVector: {
14620b57cec5SDimitry Andric       MVT IntermediateVT;
14630b57cec5SDimitry Andric       MVT RegisterVT;
14640b57cec5SDimitry Andric       unsigned NumIntermediates;
1465480093f4SDimitry Andric       unsigned NumRegisters = getVectorTypeBreakdownMVT(VT, IntermediateVT,
14660b57cec5SDimitry Andric           NumIntermediates, RegisterVT, this);
1467480093f4SDimitry Andric       NumRegistersForVT[i] = NumRegisters;
1468480093f4SDimitry Andric       assert(NumRegistersForVT[i] == NumRegisters &&
1469480093f4SDimitry Andric              "NumRegistersForVT size cannot represent NumRegisters!");
14700b57cec5SDimitry Andric       RegisterTypeForVT[i] = RegisterVT;
14710b57cec5SDimitry Andric 
14720b57cec5SDimitry Andric       MVT NVT = VT.getPow2VectorType();
14730b57cec5SDimitry Andric       if (NVT == VT) {
14740b57cec5SDimitry Andric         // Type is already a power of 2.  The default action is to split.
14750b57cec5SDimitry Andric         TransformToType[i] = MVT::Other;
14760b57cec5SDimitry Andric         if (PreferredAction == TypeScalarizeVector)
14770b57cec5SDimitry Andric           ValueTypeActions.setTypeAction(VT, TypeScalarizeVector);
14780b57cec5SDimitry Andric         else if (PreferredAction == TypeSplitVector)
14790b57cec5SDimitry Andric           ValueTypeActions.setTypeAction(VT, TypeSplitVector);
1480af732203SDimitry Andric         else if (EC.getKnownMinValue() > 1)
14815ffd83dbSDimitry Andric           ValueTypeActions.setTypeAction(VT, TypeSplitVector);
14820b57cec5SDimitry Andric         else
1483af732203SDimitry Andric           ValueTypeActions.setTypeAction(VT, EC.isScalable()
14845ffd83dbSDimitry Andric                                                  ? TypeScalarizeScalableVector
14855ffd83dbSDimitry Andric                                                  : TypeScalarizeVector);
14860b57cec5SDimitry Andric       } else {
14870b57cec5SDimitry Andric         TransformToType[i] = NVT;
14880b57cec5SDimitry Andric         ValueTypeActions.setTypeAction(VT, TypeWidenVector);
14890b57cec5SDimitry Andric       }
14900b57cec5SDimitry Andric       break;
14910b57cec5SDimitry Andric     }
14920b57cec5SDimitry Andric     default:
14930b57cec5SDimitry Andric       llvm_unreachable("Unknown vector legalization action!");
14940b57cec5SDimitry Andric     }
14950b57cec5SDimitry Andric   }
14960b57cec5SDimitry Andric 
14970b57cec5SDimitry Andric   // Determine the 'representative' register class for each value type.
14980b57cec5SDimitry Andric   // An representative register class is the largest (meaning one which is
14990b57cec5SDimitry Andric   // not a sub-register class / subreg register class) legal register class for
15000b57cec5SDimitry Andric   // a group of value types. For example, on i386, i8, i16, and i32
15010b57cec5SDimitry Andric   // representative would be GR32; while on x86_64 it's GR64.
1502*5f7ddb14SDimitry Andric   for (unsigned i = 0; i != MVT::VALUETYPE_SIZE; ++i) {
15030b57cec5SDimitry Andric     const TargetRegisterClass* RRC;
15040b57cec5SDimitry Andric     uint8_t Cost;
15050b57cec5SDimitry Andric     std::tie(RRC, Cost) = findRepresentativeClass(TRI, (MVT::SimpleValueType)i);
15060b57cec5SDimitry Andric     RepRegClassForVT[i] = RRC;
15070b57cec5SDimitry Andric     RepRegClassCostForVT[i] = Cost;
15080b57cec5SDimitry Andric   }
15090b57cec5SDimitry Andric }
15100b57cec5SDimitry Andric 
getSetCCResultType(const DataLayout & DL,LLVMContext &,EVT VT) const15110b57cec5SDimitry Andric EVT TargetLoweringBase::getSetCCResultType(const DataLayout &DL, LLVMContext &,
15120b57cec5SDimitry Andric                                            EVT VT) const {
15130b57cec5SDimitry Andric   assert(!VT.isVector() && "No default SetCC type for vectors!");
15140b57cec5SDimitry Andric   return getPointerTy(DL).SimpleTy;
15150b57cec5SDimitry Andric }
15160b57cec5SDimitry Andric 
getCmpLibcallReturnType() const15170b57cec5SDimitry Andric MVT::SimpleValueType TargetLoweringBase::getCmpLibcallReturnType() const {
15180b57cec5SDimitry Andric   return MVT::i32; // return the default value
15190b57cec5SDimitry Andric }
15200b57cec5SDimitry Andric 
15210b57cec5SDimitry Andric /// getVectorTypeBreakdown - Vector types are broken down into some number of
15220b57cec5SDimitry Andric /// legal first class types.  For example, MVT::v8f32 maps to 2 MVT::v4f32
15230b57cec5SDimitry Andric /// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
15240b57cec5SDimitry Andric /// Similarly, MVT::v2i64 turns into 4 MVT::i32 values with both PPC and X86.
15250b57cec5SDimitry Andric ///
15260b57cec5SDimitry Andric /// This method returns the number of registers needed, and the VT for each
15270b57cec5SDimitry Andric /// register.  It also returns the VT and quantity of the intermediate values
15280b57cec5SDimitry Andric /// before they are promoted/expanded.
getVectorTypeBreakdown(LLVMContext & Context,EVT VT,EVT & IntermediateVT,unsigned & NumIntermediates,MVT & RegisterVT) const1529*5f7ddb14SDimitry Andric unsigned TargetLoweringBase::getVectorTypeBreakdown(LLVMContext &Context,
1530*5f7ddb14SDimitry Andric                                                     EVT VT, EVT &IntermediateVT,
15310b57cec5SDimitry Andric                                                     unsigned &NumIntermediates,
15320b57cec5SDimitry Andric                                                     MVT &RegisterVT) const {
15335ffd83dbSDimitry Andric   ElementCount EltCnt = VT.getVectorElementCount();
15340b57cec5SDimitry Andric 
15350b57cec5SDimitry Andric   // If there is a wider vector type with the same element type as this one,
15360b57cec5SDimitry Andric   // or a promoted vector type that has the same number of elements which
15370b57cec5SDimitry Andric   // are wider, then we should convert to that legal vector type.
15380b57cec5SDimitry Andric   // This handles things like <2 x float> -> <4 x float> and
15390b57cec5SDimitry Andric   // <4 x i1> -> <4 x i32>.
15400b57cec5SDimitry Andric   LegalizeTypeAction TA = getTypeAction(Context, VT);
1541*5f7ddb14SDimitry Andric   if (!EltCnt.isScalar() &&
1542af732203SDimitry Andric       (TA == TypeWidenVector || TA == TypePromoteInteger)) {
15430b57cec5SDimitry Andric     EVT RegisterEVT = getTypeToTransformTo(Context, VT);
15440b57cec5SDimitry Andric     if (isTypeLegal(RegisterEVT)) {
15450b57cec5SDimitry Andric       IntermediateVT = RegisterEVT;
15460b57cec5SDimitry Andric       RegisterVT = RegisterEVT.getSimpleVT();
15470b57cec5SDimitry Andric       NumIntermediates = 1;
15480b57cec5SDimitry Andric       return 1;
15490b57cec5SDimitry Andric     }
15500b57cec5SDimitry Andric   }
15510b57cec5SDimitry Andric 
15520b57cec5SDimitry Andric   // Figure out the right, legal destination reg to copy into.
15530b57cec5SDimitry Andric   EVT EltTy = VT.getVectorElementType();
15540b57cec5SDimitry Andric 
15550b57cec5SDimitry Andric   unsigned NumVectorRegs = 1;
15560b57cec5SDimitry Andric 
15575ffd83dbSDimitry Andric   // Scalable vectors cannot be scalarized, so handle the legalisation of the
15585ffd83dbSDimitry Andric   // types like done elsewhere in SelectionDAG.
1559af732203SDimitry Andric   if (VT.isScalableVector() && !isPowerOf2_32(EltCnt.getKnownMinValue())) {
15605ffd83dbSDimitry Andric     LegalizeKind LK;
15615ffd83dbSDimitry Andric     EVT PartVT = VT;
15625ffd83dbSDimitry Andric     do {
15635ffd83dbSDimitry Andric       // Iterate until we've found a legal (part) type to hold VT.
15645ffd83dbSDimitry Andric       LK = getTypeConversion(Context, PartVT);
15655ffd83dbSDimitry Andric       PartVT = LK.second;
15665ffd83dbSDimitry Andric     } while (LK.first != TypeLegal);
15675ffd83dbSDimitry Andric 
1568af732203SDimitry Andric     NumIntermediates = VT.getVectorElementCount().getKnownMinValue() /
1569af732203SDimitry Andric                        PartVT.getVectorElementCount().getKnownMinValue();
15705ffd83dbSDimitry Andric 
15715ffd83dbSDimitry Andric     // FIXME: This code needs to be extended to handle more complex vector
15725ffd83dbSDimitry Andric     // breakdowns, like nxv7i64 -> nxv8i64 -> 4 x nxv2i64. Currently the only
15735ffd83dbSDimitry Andric     // supported cases are vectors that are broken down into equal parts
15745ffd83dbSDimitry Andric     // such as nxv6i64 -> 3 x nxv2i64.
1575af732203SDimitry Andric     assert((PartVT.getVectorElementCount() * NumIntermediates) ==
1576af732203SDimitry Andric                VT.getVectorElementCount() &&
15775ffd83dbSDimitry Andric            "Expected an integer multiple of PartVT");
15785ffd83dbSDimitry Andric     IntermediateVT = PartVT;
15795ffd83dbSDimitry Andric     RegisterVT = getRegisterType(Context, IntermediateVT);
15805ffd83dbSDimitry Andric     return NumIntermediates;
15815ffd83dbSDimitry Andric   }
15825ffd83dbSDimitry Andric 
15835ffd83dbSDimitry Andric   // FIXME: We don't support non-power-of-2-sized vectors for now.  Ideally
15845ffd83dbSDimitry Andric   // we could break down into LHS/RHS like LegalizeDAG does.
1585af732203SDimitry Andric   if (!isPowerOf2_32(EltCnt.getKnownMinValue())) {
1586af732203SDimitry Andric     NumVectorRegs = EltCnt.getKnownMinValue();
1587af732203SDimitry Andric     EltCnt = ElementCount::getFixed(1);
15880b57cec5SDimitry Andric   }
15890b57cec5SDimitry Andric 
15900b57cec5SDimitry Andric   // Divide the input until we get to a supported size.  This will always
15910b57cec5SDimitry Andric   // end with a scalar if the target doesn't support vectors.
1592af732203SDimitry Andric   while (EltCnt.getKnownMinValue() > 1 &&
15935ffd83dbSDimitry Andric          !isTypeLegal(EVT::getVectorVT(Context, EltTy, EltCnt))) {
1594af732203SDimitry Andric     EltCnt = EltCnt.divideCoefficientBy(2);
15950b57cec5SDimitry Andric     NumVectorRegs <<= 1;
15960b57cec5SDimitry Andric   }
15970b57cec5SDimitry Andric 
15980b57cec5SDimitry Andric   NumIntermediates = NumVectorRegs;
15990b57cec5SDimitry Andric 
16005ffd83dbSDimitry Andric   EVT NewVT = EVT::getVectorVT(Context, EltTy, EltCnt);
16010b57cec5SDimitry Andric   if (!isTypeLegal(NewVT))
16020b57cec5SDimitry Andric     NewVT = EltTy;
16030b57cec5SDimitry Andric   IntermediateVT = NewVT;
16040b57cec5SDimitry Andric 
16050b57cec5SDimitry Andric   MVT DestVT = getRegisterType(Context, NewVT);
16060b57cec5SDimitry Andric   RegisterVT = DestVT;
16070b57cec5SDimitry Andric 
16085ffd83dbSDimitry Andric   if (EVT(DestVT).bitsLT(NewVT)) {  // Value is expanded, e.g. i64 -> i16.
16095ffd83dbSDimitry Andric     TypeSize NewVTSize = NewVT.getSizeInBits();
16100b57cec5SDimitry Andric     // Convert sizes such as i33 to i64.
16115ffd83dbSDimitry Andric     if (!isPowerOf2_32(NewVTSize.getKnownMinSize()))
1612af732203SDimitry Andric       NewVTSize = NewVTSize.coefficientNextPowerOf2();
16130b57cec5SDimitry Andric     return NumVectorRegs*(NewVTSize/DestVT.getSizeInBits());
16145ffd83dbSDimitry Andric   }
16150b57cec5SDimitry Andric 
16160b57cec5SDimitry Andric   // Otherwise, promotion or legal types use the same number of registers as
16170b57cec5SDimitry Andric   // the vector decimated to the appropriate level.
16180b57cec5SDimitry Andric   return NumVectorRegs;
16190b57cec5SDimitry Andric }
16200b57cec5SDimitry Andric 
isSuitableForJumpTable(const SwitchInst * SI,uint64_t NumCases,uint64_t Range,ProfileSummaryInfo * PSI,BlockFrequencyInfo * BFI) const1621480093f4SDimitry Andric bool TargetLoweringBase::isSuitableForJumpTable(const SwitchInst *SI,
1622480093f4SDimitry Andric                                                 uint64_t NumCases,
1623480093f4SDimitry Andric                                                 uint64_t Range,
1624480093f4SDimitry Andric                                                 ProfileSummaryInfo *PSI,
1625480093f4SDimitry Andric                                                 BlockFrequencyInfo *BFI) const {
1626480093f4SDimitry Andric   // FIXME: This function check the maximum table size and density, but the
1627480093f4SDimitry Andric   // minimum size is not checked. It would be nice if the minimum size is
1628480093f4SDimitry Andric   // also combined within this function. Currently, the minimum size check is
1629480093f4SDimitry Andric   // performed in findJumpTable() in SelectionDAGBuiler and
1630480093f4SDimitry Andric   // getEstimatedNumberOfCaseClusters() in BasicTTIImpl.
1631480093f4SDimitry Andric   const bool OptForSize =
1632480093f4SDimitry Andric       SI->getParent()->getParent()->hasOptSize() ||
1633480093f4SDimitry Andric       llvm::shouldOptimizeForSize(SI->getParent(), PSI, BFI);
1634480093f4SDimitry Andric   const unsigned MinDensity = getMinimumJumpTableDensity(OptForSize);
1635480093f4SDimitry Andric   const unsigned MaxJumpTableSize = getMaximumJumpTableSize();
1636480093f4SDimitry Andric 
1637480093f4SDimitry Andric   // Check whether the number of cases is small enough and
1638480093f4SDimitry Andric   // the range is dense enough for a jump table.
1639480093f4SDimitry Andric   return (OptForSize || Range <= MaxJumpTableSize) &&
1640480093f4SDimitry Andric          (NumCases * 100 >= Range * MinDensity);
1641480093f4SDimitry Andric }
1642480093f4SDimitry Andric 
16430b57cec5SDimitry Andric /// Get the EVTs and ArgFlags collections that represent the legalized return
16440b57cec5SDimitry Andric /// type of the given function.  This does not require a DAG or a return value,
16450b57cec5SDimitry Andric /// and is suitable for use before any DAGs for the function are constructed.
16460b57cec5SDimitry Andric /// TODO: Move this out of TargetLowering.cpp.
GetReturnInfo(CallingConv::ID CC,Type * ReturnType,AttributeList attr,SmallVectorImpl<ISD::OutputArg> & Outs,const TargetLowering & TLI,const DataLayout & DL)16470b57cec5SDimitry Andric void llvm::GetReturnInfo(CallingConv::ID CC, Type *ReturnType,
16480b57cec5SDimitry Andric                          AttributeList attr,
16490b57cec5SDimitry Andric                          SmallVectorImpl<ISD::OutputArg> &Outs,
16500b57cec5SDimitry Andric                          const TargetLowering &TLI, const DataLayout &DL) {
16510b57cec5SDimitry Andric   SmallVector<EVT, 4> ValueVTs;
16520b57cec5SDimitry Andric   ComputeValueVTs(TLI, DL, ReturnType, ValueVTs);
16530b57cec5SDimitry Andric   unsigned NumValues = ValueVTs.size();
16540b57cec5SDimitry Andric   if (NumValues == 0) return;
16550b57cec5SDimitry Andric 
16560b57cec5SDimitry Andric   for (unsigned j = 0, f = NumValues; j != f; ++j) {
16570b57cec5SDimitry Andric     EVT VT = ValueVTs[j];
16580b57cec5SDimitry Andric     ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
16590b57cec5SDimitry Andric 
16600b57cec5SDimitry Andric     if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::SExt))
16610b57cec5SDimitry Andric       ExtendKind = ISD::SIGN_EXTEND;
16620b57cec5SDimitry Andric     else if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::ZExt))
16630b57cec5SDimitry Andric       ExtendKind = ISD::ZERO_EXTEND;
16640b57cec5SDimitry Andric 
16650b57cec5SDimitry Andric     // FIXME: C calling convention requires the return type to be promoted to
16660b57cec5SDimitry Andric     // at least 32-bit. But this is not necessary for non-C calling
16670b57cec5SDimitry Andric     // conventions. The frontend should mark functions whose return values
16680b57cec5SDimitry Andric     // require promoting with signext or zeroext attributes.
16690b57cec5SDimitry Andric     if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
16700b57cec5SDimitry Andric       MVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
16710b57cec5SDimitry Andric       if (VT.bitsLT(MinVT))
16720b57cec5SDimitry Andric         VT = MinVT;
16730b57cec5SDimitry Andric     }
16740b57cec5SDimitry Andric 
16750b57cec5SDimitry Andric     unsigned NumParts =
16760b57cec5SDimitry Andric         TLI.getNumRegistersForCallingConv(ReturnType->getContext(), CC, VT);
16770b57cec5SDimitry Andric     MVT PartVT =
16780b57cec5SDimitry Andric         TLI.getRegisterTypeForCallingConv(ReturnType->getContext(), CC, VT);
16790b57cec5SDimitry Andric 
16800b57cec5SDimitry Andric     // 'inreg' on function refers to return value
16810b57cec5SDimitry Andric     ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
16820b57cec5SDimitry Andric     if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::InReg))
16830b57cec5SDimitry Andric       Flags.setInReg();
16840b57cec5SDimitry Andric 
16850b57cec5SDimitry Andric     // Propagate extension type if any
16860b57cec5SDimitry Andric     if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::SExt))
16870b57cec5SDimitry Andric       Flags.setSExt();
16880b57cec5SDimitry Andric     else if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::ZExt))
16890b57cec5SDimitry Andric       Flags.setZExt();
16900b57cec5SDimitry Andric 
16910b57cec5SDimitry Andric     for (unsigned i = 0; i < NumParts; ++i)
16920b57cec5SDimitry Andric       Outs.push_back(ISD::OutputArg(Flags, PartVT, VT, /*isfixed=*/true, 0, 0));
16930b57cec5SDimitry Andric   }
16940b57cec5SDimitry Andric }
16950b57cec5SDimitry Andric 
16960b57cec5SDimitry Andric /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
16970b57cec5SDimitry Andric /// function arguments in the caller parameter area.  This is the actual
16980b57cec5SDimitry Andric /// alignment, not its logarithm.
getByValTypeAlignment(Type * Ty,const DataLayout & DL) const16990b57cec5SDimitry Andric unsigned TargetLoweringBase::getByValTypeAlignment(Type *Ty,
17000b57cec5SDimitry Andric                                                    const DataLayout &DL) const {
17015ffd83dbSDimitry Andric   return DL.getABITypeAlign(Ty).value();
17020b57cec5SDimitry Andric }
17030b57cec5SDimitry Andric 
allowsMemoryAccessForAlignment(LLVMContext & Context,const DataLayout & DL,EVT VT,unsigned AddrSpace,Align Alignment,MachineMemOperand::Flags Flags,bool * Fast) const17048bcb0991SDimitry Andric bool TargetLoweringBase::allowsMemoryAccessForAlignment(
17058bcb0991SDimitry Andric     LLVMContext &Context, const DataLayout &DL, EVT VT, unsigned AddrSpace,
17065ffd83dbSDimitry Andric     Align Alignment, MachineMemOperand::Flags Flags, bool *Fast) const {
17070b57cec5SDimitry Andric   // Check if the specified alignment is sufficient based on the data layout.
17080b57cec5SDimitry Andric   // TODO: While using the data layout works in practice, a better solution
17090b57cec5SDimitry Andric   // would be to implement this check directly (make this a virtual function).
17100b57cec5SDimitry Andric   // For example, the ABI alignment may change based on software platform while
17110b57cec5SDimitry Andric   // this function should only be affected by hardware implementation.
17120b57cec5SDimitry Andric   Type *Ty = VT.getTypeForEVT(Context);
1713*5f7ddb14SDimitry Andric   if (VT.isZeroSized() || Alignment >= DL.getABITypeAlign(Ty)) {
17140b57cec5SDimitry Andric     // Assume that an access that meets the ABI-specified alignment is fast.
17150b57cec5SDimitry Andric     if (Fast != nullptr)
17160b57cec5SDimitry Andric       *Fast = true;
17170b57cec5SDimitry Andric     return true;
17180b57cec5SDimitry Andric   }
17190b57cec5SDimitry Andric 
17200b57cec5SDimitry Andric   // This is a misaligned access.
1721*5f7ddb14SDimitry Andric   return allowsMisalignedMemoryAccesses(VT, AddrSpace, Alignment, Flags, Fast);
17220b57cec5SDimitry Andric }
17230b57cec5SDimitry Andric 
allowsMemoryAccessForAlignment(LLVMContext & Context,const DataLayout & DL,EVT VT,const MachineMemOperand & MMO,bool * Fast) const17248bcb0991SDimitry Andric bool TargetLoweringBase::allowsMemoryAccessForAlignment(
17258bcb0991SDimitry Andric     LLVMContext &Context, const DataLayout &DL, EVT VT,
17268bcb0991SDimitry Andric     const MachineMemOperand &MMO, bool *Fast) const {
17278bcb0991SDimitry Andric   return allowsMemoryAccessForAlignment(Context, DL, VT, MMO.getAddrSpace(),
17285ffd83dbSDimitry Andric                                         MMO.getAlign(), MMO.getFlags(), Fast);
17298bcb0991SDimitry Andric }
17308bcb0991SDimitry Andric 
allowsMemoryAccess(LLVMContext & Context,const DataLayout & DL,EVT VT,unsigned AddrSpace,Align Alignment,MachineMemOperand::Flags Flags,bool * Fast) const17315ffd83dbSDimitry Andric bool TargetLoweringBase::allowsMemoryAccess(LLVMContext &Context,
17325ffd83dbSDimitry Andric                                             const DataLayout &DL, EVT VT,
17335ffd83dbSDimitry Andric                                             unsigned AddrSpace, Align Alignment,
17345ffd83dbSDimitry Andric                                             MachineMemOperand::Flags Flags,
17355ffd83dbSDimitry Andric                                             bool *Fast) const {
17368bcb0991SDimitry Andric   return allowsMemoryAccessForAlignment(Context, DL, VT, AddrSpace, Alignment,
17378bcb0991SDimitry Andric                                         Flags, Fast);
17388bcb0991SDimitry Andric }
17398bcb0991SDimitry Andric 
allowsMemoryAccess(LLVMContext & Context,const DataLayout & DL,EVT VT,const MachineMemOperand & MMO,bool * Fast) const17400b57cec5SDimitry Andric bool TargetLoweringBase::allowsMemoryAccess(LLVMContext &Context,
17410b57cec5SDimitry Andric                                             const DataLayout &DL, EVT VT,
17420b57cec5SDimitry Andric                                             const MachineMemOperand &MMO,
17430b57cec5SDimitry Andric                                             bool *Fast) const {
17445ffd83dbSDimitry Andric   return allowsMemoryAccess(Context, DL, VT, MMO.getAddrSpace(), MMO.getAlign(),
17455ffd83dbSDimitry Andric                             MMO.getFlags(), Fast);
17460b57cec5SDimitry Andric }
17470b57cec5SDimitry Andric 
allowsMemoryAccess(LLVMContext & Context,const DataLayout & DL,LLT Ty,const MachineMemOperand & MMO,bool * Fast) const1748af732203SDimitry Andric bool TargetLoweringBase::allowsMemoryAccess(LLVMContext &Context,
1749af732203SDimitry Andric                                             const DataLayout &DL, LLT Ty,
1750af732203SDimitry Andric                                             const MachineMemOperand &MMO,
1751af732203SDimitry Andric                                             bool *Fast) const {
1752af732203SDimitry Andric   return allowsMemoryAccess(Context, DL, getMVTForLLT(Ty), MMO.getAddrSpace(),
1753af732203SDimitry Andric                             MMO.getAlign(), MMO.getFlags(), Fast);
1754af732203SDimitry Andric }
1755af732203SDimitry Andric 
17560b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
17570b57cec5SDimitry Andric //  TargetTransformInfo Helpers
17580b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
17590b57cec5SDimitry Andric 
InstructionOpcodeToISD(unsigned Opcode) const17600b57cec5SDimitry Andric int TargetLoweringBase::InstructionOpcodeToISD(unsigned Opcode) const {
17610b57cec5SDimitry Andric   enum InstructionOpcodes {
17620b57cec5SDimitry Andric #define HANDLE_INST(NUM, OPCODE, CLASS) OPCODE = NUM,
17630b57cec5SDimitry Andric #define LAST_OTHER_INST(NUM) InstructionOpcodesCount = NUM
17640b57cec5SDimitry Andric #include "llvm/IR/Instruction.def"
17650b57cec5SDimitry Andric   };
17660b57cec5SDimitry Andric   switch (static_cast<InstructionOpcodes>(Opcode)) {
17670b57cec5SDimitry Andric   case Ret:            return 0;
17680b57cec5SDimitry Andric   case Br:             return 0;
17690b57cec5SDimitry Andric   case Switch:         return 0;
17700b57cec5SDimitry Andric   case IndirectBr:     return 0;
17710b57cec5SDimitry Andric   case Invoke:         return 0;
17720b57cec5SDimitry Andric   case CallBr:         return 0;
17730b57cec5SDimitry Andric   case Resume:         return 0;
17740b57cec5SDimitry Andric   case Unreachable:    return 0;
17750b57cec5SDimitry Andric   case CleanupRet:     return 0;
17760b57cec5SDimitry Andric   case CatchRet:       return 0;
17770b57cec5SDimitry Andric   case CatchPad:       return 0;
17780b57cec5SDimitry Andric   case CatchSwitch:    return 0;
17790b57cec5SDimitry Andric   case CleanupPad:     return 0;
17800b57cec5SDimitry Andric   case FNeg:           return ISD::FNEG;
17810b57cec5SDimitry Andric   case Add:            return ISD::ADD;
17820b57cec5SDimitry Andric   case FAdd:           return ISD::FADD;
17830b57cec5SDimitry Andric   case Sub:            return ISD::SUB;
17840b57cec5SDimitry Andric   case FSub:           return ISD::FSUB;
17850b57cec5SDimitry Andric   case Mul:            return ISD::MUL;
17860b57cec5SDimitry Andric   case FMul:           return ISD::FMUL;
17870b57cec5SDimitry Andric   case UDiv:           return ISD::UDIV;
17880b57cec5SDimitry Andric   case SDiv:           return ISD::SDIV;
17890b57cec5SDimitry Andric   case FDiv:           return ISD::FDIV;
17900b57cec5SDimitry Andric   case URem:           return ISD::UREM;
17910b57cec5SDimitry Andric   case SRem:           return ISD::SREM;
17920b57cec5SDimitry Andric   case FRem:           return ISD::FREM;
17930b57cec5SDimitry Andric   case Shl:            return ISD::SHL;
17940b57cec5SDimitry Andric   case LShr:           return ISD::SRL;
17950b57cec5SDimitry Andric   case AShr:           return ISD::SRA;
17960b57cec5SDimitry Andric   case And:            return ISD::AND;
17970b57cec5SDimitry Andric   case Or:             return ISD::OR;
17980b57cec5SDimitry Andric   case Xor:            return ISD::XOR;
17990b57cec5SDimitry Andric   case Alloca:         return 0;
18000b57cec5SDimitry Andric   case Load:           return ISD::LOAD;
18010b57cec5SDimitry Andric   case Store:          return ISD::STORE;
18020b57cec5SDimitry Andric   case GetElementPtr:  return 0;
18030b57cec5SDimitry Andric   case Fence:          return 0;
18040b57cec5SDimitry Andric   case AtomicCmpXchg:  return 0;
18050b57cec5SDimitry Andric   case AtomicRMW:      return 0;
18060b57cec5SDimitry Andric   case Trunc:          return ISD::TRUNCATE;
18070b57cec5SDimitry Andric   case ZExt:           return ISD::ZERO_EXTEND;
18080b57cec5SDimitry Andric   case SExt:           return ISD::SIGN_EXTEND;
18090b57cec5SDimitry Andric   case FPToUI:         return ISD::FP_TO_UINT;
18100b57cec5SDimitry Andric   case FPToSI:         return ISD::FP_TO_SINT;
18110b57cec5SDimitry Andric   case UIToFP:         return ISD::UINT_TO_FP;
18120b57cec5SDimitry Andric   case SIToFP:         return ISD::SINT_TO_FP;
18130b57cec5SDimitry Andric   case FPTrunc:        return ISD::FP_ROUND;
18140b57cec5SDimitry Andric   case FPExt:          return ISD::FP_EXTEND;
18150b57cec5SDimitry Andric   case PtrToInt:       return ISD::BITCAST;
18160b57cec5SDimitry Andric   case IntToPtr:       return ISD::BITCAST;
18170b57cec5SDimitry Andric   case BitCast:        return ISD::BITCAST;
18180b57cec5SDimitry Andric   case AddrSpaceCast:  return ISD::ADDRSPACECAST;
18190b57cec5SDimitry Andric   case ICmp:           return ISD::SETCC;
18200b57cec5SDimitry Andric   case FCmp:           return ISD::SETCC;
18210b57cec5SDimitry Andric   case PHI:            return 0;
18220b57cec5SDimitry Andric   case Call:           return 0;
18230b57cec5SDimitry Andric   case Select:         return ISD::SELECT;
18240b57cec5SDimitry Andric   case UserOp1:        return 0;
18250b57cec5SDimitry Andric   case UserOp2:        return 0;
18260b57cec5SDimitry Andric   case VAArg:          return 0;
18270b57cec5SDimitry Andric   case ExtractElement: return ISD::EXTRACT_VECTOR_ELT;
18280b57cec5SDimitry Andric   case InsertElement:  return ISD::INSERT_VECTOR_ELT;
18290b57cec5SDimitry Andric   case ShuffleVector:  return ISD::VECTOR_SHUFFLE;
18300b57cec5SDimitry Andric   case ExtractValue:   return ISD::MERGE_VALUES;
18310b57cec5SDimitry Andric   case InsertValue:    return ISD::MERGE_VALUES;
18320b57cec5SDimitry Andric   case LandingPad:     return 0;
18335ffd83dbSDimitry Andric   case Freeze:         return ISD::FREEZE;
18340b57cec5SDimitry Andric   }
18350b57cec5SDimitry Andric 
18360b57cec5SDimitry Andric   llvm_unreachable("Unknown instruction type encountered!");
18370b57cec5SDimitry Andric }
18380b57cec5SDimitry Andric 
1839*5f7ddb14SDimitry Andric std::pair<InstructionCost, MVT>
getTypeLegalizationCost(const DataLayout & DL,Type * Ty) const18400b57cec5SDimitry Andric TargetLoweringBase::getTypeLegalizationCost(const DataLayout &DL,
18410b57cec5SDimitry Andric                                             Type *Ty) const {
18420b57cec5SDimitry Andric   LLVMContext &C = Ty->getContext();
18430b57cec5SDimitry Andric   EVT MTy = getValueType(DL, Ty);
18440b57cec5SDimitry Andric 
1845*5f7ddb14SDimitry Andric   InstructionCost Cost = 1;
18460b57cec5SDimitry Andric   // We keep legalizing the type until we find a legal kind. We assume that
18470b57cec5SDimitry Andric   // the only operation that costs anything is the split. After splitting
18480b57cec5SDimitry Andric   // we need to handle two types.
18490b57cec5SDimitry Andric   while (true) {
18500b57cec5SDimitry Andric     LegalizeKind LK = getTypeConversion(C, MTy);
18510b57cec5SDimitry Andric 
1852*5f7ddb14SDimitry Andric     if (LK.first == TypeScalarizeScalableVector)
1853*5f7ddb14SDimitry Andric       return std::make_pair(InstructionCost::getInvalid(), MVT::getVT(Ty));
1854*5f7ddb14SDimitry Andric 
18550b57cec5SDimitry Andric     if (LK.first == TypeLegal)
18560b57cec5SDimitry Andric       return std::make_pair(Cost, MTy.getSimpleVT());
18570b57cec5SDimitry Andric 
18580b57cec5SDimitry Andric     if (LK.first == TypeSplitVector || LK.first == TypeExpandInteger)
18590b57cec5SDimitry Andric       Cost *= 2;
18600b57cec5SDimitry Andric 
18610b57cec5SDimitry Andric     // Do not loop with f128 type.
18620b57cec5SDimitry Andric     if (MTy == LK.second)
18630b57cec5SDimitry Andric       return std::make_pair(Cost, MTy.getSimpleVT());
18640b57cec5SDimitry Andric 
18650b57cec5SDimitry Andric     // Keep legalizing the type.
18660b57cec5SDimitry Andric     MTy = LK.second;
18670b57cec5SDimitry Andric   }
18680b57cec5SDimitry Andric }
18690b57cec5SDimitry Andric 
1870*5f7ddb14SDimitry Andric Value *
getDefaultSafeStackPointerLocation(IRBuilderBase & IRB,bool UseTLS) const1871*5f7ddb14SDimitry Andric TargetLoweringBase::getDefaultSafeStackPointerLocation(IRBuilderBase &IRB,
18720b57cec5SDimitry Andric                                                        bool UseTLS) const {
18730b57cec5SDimitry Andric   // compiler-rt provides a variable with a magic name.  Targets that do not
18740b57cec5SDimitry Andric   // link with compiler-rt may also provide such a variable.
18750b57cec5SDimitry Andric   Module *M = IRB.GetInsertBlock()->getParent()->getParent();
18760b57cec5SDimitry Andric   const char *UnsafeStackPtrVar = "__safestack_unsafe_stack_ptr";
18770b57cec5SDimitry Andric   auto UnsafeStackPtr =
18780b57cec5SDimitry Andric       dyn_cast_or_null<GlobalVariable>(M->getNamedValue(UnsafeStackPtrVar));
18790b57cec5SDimitry Andric 
18800b57cec5SDimitry Andric   Type *StackPtrTy = Type::getInt8PtrTy(M->getContext());
18810b57cec5SDimitry Andric 
18820b57cec5SDimitry Andric   if (!UnsafeStackPtr) {
18830b57cec5SDimitry Andric     auto TLSModel = UseTLS ?
18840b57cec5SDimitry Andric         GlobalValue::InitialExecTLSModel :
18850b57cec5SDimitry Andric         GlobalValue::NotThreadLocal;
18860b57cec5SDimitry Andric     // The global variable is not defined yet, define it ourselves.
18870b57cec5SDimitry Andric     // We use the initial-exec TLS model because we do not support the
18880b57cec5SDimitry Andric     // variable living anywhere other than in the main executable.
18890b57cec5SDimitry Andric     UnsafeStackPtr = new GlobalVariable(
18900b57cec5SDimitry Andric         *M, StackPtrTy, false, GlobalValue::ExternalLinkage, nullptr,
18910b57cec5SDimitry Andric         UnsafeStackPtrVar, nullptr, TLSModel);
18920b57cec5SDimitry Andric   } else {
18930b57cec5SDimitry Andric     // The variable exists, check its type and attributes.
18940b57cec5SDimitry Andric     if (UnsafeStackPtr->getValueType() != StackPtrTy)
18950b57cec5SDimitry Andric       report_fatal_error(Twine(UnsafeStackPtrVar) + " must have void* type");
18960b57cec5SDimitry Andric     if (UseTLS != UnsafeStackPtr->isThreadLocal())
18970b57cec5SDimitry Andric       report_fatal_error(Twine(UnsafeStackPtrVar) + " must " +
18980b57cec5SDimitry Andric                          (UseTLS ? "" : "not ") + "be thread-local");
18990b57cec5SDimitry Andric   }
19000b57cec5SDimitry Andric   return UnsafeStackPtr;
19010b57cec5SDimitry Andric }
19020b57cec5SDimitry Andric 
1903*5f7ddb14SDimitry Andric Value *
getSafeStackPointerLocation(IRBuilderBase & IRB) const1904*5f7ddb14SDimitry Andric TargetLoweringBase::getSafeStackPointerLocation(IRBuilderBase &IRB) const {
19050b57cec5SDimitry Andric   if (!TM.getTargetTriple().isAndroid())
19060b57cec5SDimitry Andric     return getDefaultSafeStackPointerLocation(IRB, true);
19070b57cec5SDimitry Andric 
19080b57cec5SDimitry Andric   // Android provides a libc function to retrieve the address of the current
19090b57cec5SDimitry Andric   // thread's unsafe stack pointer.
19100b57cec5SDimitry Andric   Module *M = IRB.GetInsertBlock()->getParent()->getParent();
19110b57cec5SDimitry Andric   Type *StackPtrTy = Type::getInt8PtrTy(M->getContext());
19120b57cec5SDimitry Andric   FunctionCallee Fn = M->getOrInsertFunction("__safestack_pointer_address",
19130b57cec5SDimitry Andric                                              StackPtrTy->getPointerTo(0));
19140b57cec5SDimitry Andric   return IRB.CreateCall(Fn);
19150b57cec5SDimitry Andric }
19160b57cec5SDimitry Andric 
19170b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
19180b57cec5SDimitry Andric //  Loop Strength Reduction hooks
19190b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
19200b57cec5SDimitry Andric 
19210b57cec5SDimitry Andric /// isLegalAddressingMode - Return true if the addressing mode represented
19220b57cec5SDimitry Andric /// by AM is legal for this target, for a load/store of the specified type.
isLegalAddressingMode(const DataLayout & DL,const AddrMode & AM,Type * Ty,unsigned AS,Instruction * I) const19230b57cec5SDimitry Andric bool TargetLoweringBase::isLegalAddressingMode(const DataLayout &DL,
19240b57cec5SDimitry Andric                                                const AddrMode &AM, Type *Ty,
19250b57cec5SDimitry Andric                                                unsigned AS, Instruction *I) const {
19260b57cec5SDimitry Andric   // The default implementation of this implements a conservative RISCy, r+r and
19270b57cec5SDimitry Andric   // r+i addr mode.
19280b57cec5SDimitry Andric 
19290b57cec5SDimitry Andric   // Allows a sign-extended 16-bit immediate field.
19300b57cec5SDimitry Andric   if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
19310b57cec5SDimitry Andric     return false;
19320b57cec5SDimitry Andric 
19330b57cec5SDimitry Andric   // No global is ever allowed as a base.
19340b57cec5SDimitry Andric   if (AM.BaseGV)
19350b57cec5SDimitry Andric     return false;
19360b57cec5SDimitry Andric 
19370b57cec5SDimitry Andric   // Only support r+r,
19380b57cec5SDimitry Andric   switch (AM.Scale) {
19390b57cec5SDimitry Andric   case 0:  // "r+i" or just "i", depending on HasBaseReg.
19400b57cec5SDimitry Andric     break;
19410b57cec5SDimitry Andric   case 1:
19420b57cec5SDimitry Andric     if (AM.HasBaseReg && AM.BaseOffs)  // "r+r+i" is not allowed.
19430b57cec5SDimitry Andric       return false;
19440b57cec5SDimitry Andric     // Otherwise we have r+r or r+i.
19450b57cec5SDimitry Andric     break;
19460b57cec5SDimitry Andric   case 2:
19470b57cec5SDimitry Andric     if (AM.HasBaseReg || AM.BaseOffs)  // 2*r+r  or  2*r+i is not allowed.
19480b57cec5SDimitry Andric       return false;
19490b57cec5SDimitry Andric     // Allow 2*r as r+r.
19500b57cec5SDimitry Andric     break;
19510b57cec5SDimitry Andric   default: // Don't allow n * r
19520b57cec5SDimitry Andric     return false;
19530b57cec5SDimitry Andric   }
19540b57cec5SDimitry Andric 
19550b57cec5SDimitry Andric   return true;
19560b57cec5SDimitry Andric }
19570b57cec5SDimitry Andric 
19580b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
19590b57cec5SDimitry Andric //  Stack Protector
19600b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
19610b57cec5SDimitry Andric 
19620b57cec5SDimitry Andric // For OpenBSD return its special guard variable. Otherwise return nullptr,
19630b57cec5SDimitry Andric // so that SelectionDAG handle SSP.
getIRStackGuard(IRBuilderBase & IRB) const1964*5f7ddb14SDimitry Andric Value *TargetLoweringBase::getIRStackGuard(IRBuilderBase &IRB) const {
19650b57cec5SDimitry Andric   if (getTargetMachine().getTargetTriple().isOSOpenBSD()) {
19660b57cec5SDimitry Andric     Module &M = *IRB.GetInsertBlock()->getParent()->getParent();
19670b57cec5SDimitry Andric     PointerType *PtrTy = Type::getInt8PtrTy(M.getContext());
196816d6b3b3SDimitry Andric     Constant *C = M.getOrInsertGlobal("__guard_local", PtrTy);
196916d6b3b3SDimitry Andric     if (GlobalVariable *G = dyn_cast_or_null<GlobalVariable>(C))
197016d6b3b3SDimitry Andric       G->setVisibility(GlobalValue::HiddenVisibility);
197116d6b3b3SDimitry Andric     return C;
19720b57cec5SDimitry Andric   }
19730b57cec5SDimitry Andric   return nullptr;
19740b57cec5SDimitry Andric }
19750b57cec5SDimitry Andric 
19760b57cec5SDimitry Andric // Currently only support "standard" __stack_chk_guard.
19770b57cec5SDimitry Andric // TODO: add LOAD_STACK_GUARD support.
insertSSPDeclarations(Module & M) const19780b57cec5SDimitry Andric void TargetLoweringBase::insertSSPDeclarations(Module &M) const {
1979af732203SDimitry Andric   if (!M.getNamedValue("__stack_chk_guard")) {
1980af732203SDimitry Andric     auto *GV = new GlobalVariable(M, Type::getInt8PtrTy(M.getContext()), false,
1981af732203SDimitry Andric                                   GlobalVariable::ExternalLinkage, nullptr,
1982af732203SDimitry Andric                                   "__stack_chk_guard");
1983af732203SDimitry Andric     if (TM.getRelocationModel() == Reloc::Static &&
1984e8e5d75eSAlfredo Dal'Ava Junior         !TM.getTargetTriple().isWindowsGNUEnvironment() &&
1985e8e5d75eSAlfredo Dal'Ava Junior 	!(TM.getTargetTriple().isPPC64() && TM.getTargetTriple().isOSFreeBSD()))
1986af732203SDimitry Andric       GV->setDSOLocal(true);
1987af732203SDimitry Andric   }
19880b57cec5SDimitry Andric }
19890b57cec5SDimitry Andric 
19900b57cec5SDimitry Andric // Currently only support "standard" __stack_chk_guard.
19910b57cec5SDimitry Andric // TODO: add LOAD_STACK_GUARD support.
getSDagStackGuard(const Module & M) const19920b57cec5SDimitry Andric Value *TargetLoweringBase::getSDagStackGuard(const Module &M) const {
19930b57cec5SDimitry Andric   return M.getNamedValue("__stack_chk_guard");
19940b57cec5SDimitry Andric }
19950b57cec5SDimitry Andric 
getSSPStackGuardCheck(const Module & M) const19960b57cec5SDimitry Andric Function *TargetLoweringBase::getSSPStackGuardCheck(const Module &M) const {
19970b57cec5SDimitry Andric   return nullptr;
19980b57cec5SDimitry Andric }
19990b57cec5SDimitry Andric 
getMinimumJumpTableEntries() const20000b57cec5SDimitry Andric unsigned TargetLoweringBase::getMinimumJumpTableEntries() const {
20010b57cec5SDimitry Andric   return MinimumJumpTableEntries;
20020b57cec5SDimitry Andric }
20030b57cec5SDimitry Andric 
setMinimumJumpTableEntries(unsigned Val)20040b57cec5SDimitry Andric void TargetLoweringBase::setMinimumJumpTableEntries(unsigned Val) {
20050b57cec5SDimitry Andric   MinimumJumpTableEntries = Val;
20060b57cec5SDimitry Andric }
20070b57cec5SDimitry Andric 
getMinimumJumpTableDensity(bool OptForSize) const20080b57cec5SDimitry Andric unsigned TargetLoweringBase::getMinimumJumpTableDensity(bool OptForSize) const {
20090b57cec5SDimitry Andric   return OptForSize ? OptsizeJumpTableDensity : JumpTableDensity;
20100b57cec5SDimitry Andric }
20110b57cec5SDimitry Andric 
getMaximumJumpTableSize() const20120b57cec5SDimitry Andric unsigned TargetLoweringBase::getMaximumJumpTableSize() const {
20130b57cec5SDimitry Andric   return MaximumJumpTableSize;
20140b57cec5SDimitry Andric }
20150b57cec5SDimitry Andric 
setMaximumJumpTableSize(unsigned Val)20160b57cec5SDimitry Andric void TargetLoweringBase::setMaximumJumpTableSize(unsigned Val) {
20170b57cec5SDimitry Andric   MaximumJumpTableSize = Val;
20180b57cec5SDimitry Andric }
20190b57cec5SDimitry Andric 
isJumpTableRelative() const20205ffd83dbSDimitry Andric bool TargetLoweringBase::isJumpTableRelative() const {
20215ffd83dbSDimitry Andric   return getTargetMachine().isPositionIndependent();
20225ffd83dbSDimitry Andric }
20235ffd83dbSDimitry Andric 
20240b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
20250b57cec5SDimitry Andric //  Reciprocal Estimates
20260b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
20270b57cec5SDimitry Andric 
20280b57cec5SDimitry Andric /// Get the reciprocal estimate attribute string for a function that will
20290b57cec5SDimitry Andric /// override the target defaults.
getRecipEstimateForFunc(MachineFunction & MF)20300b57cec5SDimitry Andric static StringRef getRecipEstimateForFunc(MachineFunction &MF) {
20310b57cec5SDimitry Andric   const Function &F = MF.getFunction();
20320b57cec5SDimitry Andric   return F.getFnAttribute("reciprocal-estimates").getValueAsString();
20330b57cec5SDimitry Andric }
20340b57cec5SDimitry Andric 
20350b57cec5SDimitry Andric /// Construct a string for the given reciprocal operation of the given type.
20360b57cec5SDimitry Andric /// This string should match the corresponding option to the front-end's
20370b57cec5SDimitry Andric /// "-mrecip" flag assuming those strings have been passed through in an
20380b57cec5SDimitry Andric /// attribute string. For example, "vec-divf" for a division of a vXf32.
getReciprocalOpName(bool IsSqrt,EVT VT)20390b57cec5SDimitry Andric static std::string getReciprocalOpName(bool IsSqrt, EVT VT) {
20400b57cec5SDimitry Andric   std::string Name = VT.isVector() ? "vec-" : "";
20410b57cec5SDimitry Andric 
20420b57cec5SDimitry Andric   Name += IsSqrt ? "sqrt" : "div";
20430b57cec5SDimitry Andric 
20440b57cec5SDimitry Andric   // TODO: Handle "half" or other float types?
20450b57cec5SDimitry Andric   if (VT.getScalarType() == MVT::f64) {
20460b57cec5SDimitry Andric     Name += "d";
20470b57cec5SDimitry Andric   } else {
20480b57cec5SDimitry Andric     assert(VT.getScalarType() == MVT::f32 &&
20490b57cec5SDimitry Andric            "Unexpected FP type for reciprocal estimate");
20500b57cec5SDimitry Andric     Name += "f";
20510b57cec5SDimitry Andric   }
20520b57cec5SDimitry Andric 
20530b57cec5SDimitry Andric   return Name;
20540b57cec5SDimitry Andric }
20550b57cec5SDimitry Andric 
20560b57cec5SDimitry Andric /// Return the character position and value (a single numeric character) of a
20570b57cec5SDimitry Andric /// customized refinement operation in the input string if it exists. Return
20580b57cec5SDimitry Andric /// false if there is no customized refinement step count.
parseRefinementStep(StringRef In,size_t & Position,uint8_t & Value)20590b57cec5SDimitry Andric static bool parseRefinementStep(StringRef In, size_t &Position,
20600b57cec5SDimitry Andric                                 uint8_t &Value) {
20610b57cec5SDimitry Andric   const char RefStepToken = ':';
20620b57cec5SDimitry Andric   Position = In.find(RefStepToken);
20630b57cec5SDimitry Andric   if (Position == StringRef::npos)
20640b57cec5SDimitry Andric     return false;
20650b57cec5SDimitry Andric 
20660b57cec5SDimitry Andric   StringRef RefStepString = In.substr(Position + 1);
20670b57cec5SDimitry Andric   // Allow exactly one numeric character for the additional refinement
20680b57cec5SDimitry Andric   // step parameter.
20690b57cec5SDimitry Andric   if (RefStepString.size() == 1) {
20700b57cec5SDimitry Andric     char RefStepChar = RefStepString[0];
2071af732203SDimitry Andric     if (isDigit(RefStepChar)) {
20720b57cec5SDimitry Andric       Value = RefStepChar - '0';
20730b57cec5SDimitry Andric       return true;
20740b57cec5SDimitry Andric     }
20750b57cec5SDimitry Andric   }
20760b57cec5SDimitry Andric   report_fatal_error("Invalid refinement step for -recip.");
20770b57cec5SDimitry Andric }
20780b57cec5SDimitry Andric 
20790b57cec5SDimitry Andric /// For the input attribute string, return one of the ReciprocalEstimate enum
20800b57cec5SDimitry Andric /// status values (enabled, disabled, or not specified) for this operation on
20810b57cec5SDimitry Andric /// the specified data type.
getOpEnabled(bool IsSqrt,EVT VT,StringRef Override)20820b57cec5SDimitry Andric static int getOpEnabled(bool IsSqrt, EVT VT, StringRef Override) {
20830b57cec5SDimitry Andric   if (Override.empty())
20840b57cec5SDimitry Andric     return TargetLoweringBase::ReciprocalEstimate::Unspecified;
20850b57cec5SDimitry Andric 
20860b57cec5SDimitry Andric   SmallVector<StringRef, 4> OverrideVector;
20870b57cec5SDimitry Andric   Override.split(OverrideVector, ',');
20880b57cec5SDimitry Andric   unsigned NumArgs = OverrideVector.size();
20890b57cec5SDimitry Andric 
20900b57cec5SDimitry Andric   // Check if "all", "none", or "default" was specified.
20910b57cec5SDimitry Andric   if (NumArgs == 1) {
20920b57cec5SDimitry Andric     // Look for an optional setting of the number of refinement steps needed
20930b57cec5SDimitry Andric     // for this type of reciprocal operation.
20940b57cec5SDimitry Andric     size_t RefPos;
20950b57cec5SDimitry Andric     uint8_t RefSteps;
20960b57cec5SDimitry Andric     if (parseRefinementStep(Override, RefPos, RefSteps)) {
20970b57cec5SDimitry Andric       // Split the string for further processing.
20980b57cec5SDimitry Andric       Override = Override.substr(0, RefPos);
20990b57cec5SDimitry Andric     }
21000b57cec5SDimitry Andric 
21010b57cec5SDimitry Andric     // All reciprocal types are enabled.
21020b57cec5SDimitry Andric     if (Override == "all")
21030b57cec5SDimitry Andric       return TargetLoweringBase::ReciprocalEstimate::Enabled;
21040b57cec5SDimitry Andric 
21050b57cec5SDimitry Andric     // All reciprocal types are disabled.
21060b57cec5SDimitry Andric     if (Override == "none")
21070b57cec5SDimitry Andric       return TargetLoweringBase::ReciprocalEstimate::Disabled;
21080b57cec5SDimitry Andric 
21090b57cec5SDimitry Andric     // Target defaults for enablement are used.
21100b57cec5SDimitry Andric     if (Override == "default")
21110b57cec5SDimitry Andric       return TargetLoweringBase::ReciprocalEstimate::Unspecified;
21120b57cec5SDimitry Andric   }
21130b57cec5SDimitry Andric 
21140b57cec5SDimitry Andric   // The attribute string may omit the size suffix ('f'/'d').
21150b57cec5SDimitry Andric   std::string VTName = getReciprocalOpName(IsSqrt, VT);
21160b57cec5SDimitry Andric   std::string VTNameNoSize = VTName;
21170b57cec5SDimitry Andric   VTNameNoSize.pop_back();
21180b57cec5SDimitry Andric   static const char DisabledPrefix = '!';
21190b57cec5SDimitry Andric 
21200b57cec5SDimitry Andric   for (StringRef RecipType : OverrideVector) {
21210b57cec5SDimitry Andric     size_t RefPos;
21220b57cec5SDimitry Andric     uint8_t RefSteps;
21230b57cec5SDimitry Andric     if (parseRefinementStep(RecipType, RefPos, RefSteps))
21240b57cec5SDimitry Andric       RecipType = RecipType.substr(0, RefPos);
21250b57cec5SDimitry Andric 
21260b57cec5SDimitry Andric     // Ignore the disablement token for string matching.
21270b57cec5SDimitry Andric     bool IsDisabled = RecipType[0] == DisabledPrefix;
21280b57cec5SDimitry Andric     if (IsDisabled)
21290b57cec5SDimitry Andric       RecipType = RecipType.substr(1);
21300b57cec5SDimitry Andric 
21310b57cec5SDimitry Andric     if (RecipType.equals(VTName) || RecipType.equals(VTNameNoSize))
21320b57cec5SDimitry Andric       return IsDisabled ? TargetLoweringBase::ReciprocalEstimate::Disabled
21330b57cec5SDimitry Andric                         : TargetLoweringBase::ReciprocalEstimate::Enabled;
21340b57cec5SDimitry Andric   }
21350b57cec5SDimitry Andric 
21360b57cec5SDimitry Andric   return TargetLoweringBase::ReciprocalEstimate::Unspecified;
21370b57cec5SDimitry Andric }
21380b57cec5SDimitry Andric 
21390b57cec5SDimitry Andric /// For the input attribute string, return the customized refinement step count
21400b57cec5SDimitry Andric /// for this operation on the specified data type. If the step count does not
21410b57cec5SDimitry Andric /// exist, return the ReciprocalEstimate enum value for unspecified.
getOpRefinementSteps(bool IsSqrt,EVT VT,StringRef Override)21420b57cec5SDimitry Andric static int getOpRefinementSteps(bool IsSqrt, EVT VT, StringRef Override) {
21430b57cec5SDimitry Andric   if (Override.empty())
21440b57cec5SDimitry Andric     return TargetLoweringBase::ReciprocalEstimate::Unspecified;
21450b57cec5SDimitry Andric 
21460b57cec5SDimitry Andric   SmallVector<StringRef, 4> OverrideVector;
21470b57cec5SDimitry Andric   Override.split(OverrideVector, ',');
21480b57cec5SDimitry Andric   unsigned NumArgs = OverrideVector.size();
21490b57cec5SDimitry Andric 
21500b57cec5SDimitry Andric   // Check if "all", "default", or "none" was specified.
21510b57cec5SDimitry Andric   if (NumArgs == 1) {
21520b57cec5SDimitry Andric     // Look for an optional setting of the number of refinement steps needed
21530b57cec5SDimitry Andric     // for this type of reciprocal operation.
21540b57cec5SDimitry Andric     size_t RefPos;
21550b57cec5SDimitry Andric     uint8_t RefSteps;
21560b57cec5SDimitry Andric     if (!parseRefinementStep(Override, RefPos, RefSteps))
21570b57cec5SDimitry Andric       return TargetLoweringBase::ReciprocalEstimate::Unspecified;
21580b57cec5SDimitry Andric 
21590b57cec5SDimitry Andric     // Split the string for further processing.
21600b57cec5SDimitry Andric     Override = Override.substr(0, RefPos);
21610b57cec5SDimitry Andric     assert(Override != "none" &&
21620b57cec5SDimitry Andric            "Disabled reciprocals, but specifed refinement steps?");
21630b57cec5SDimitry Andric 
21640b57cec5SDimitry Andric     // If this is a general override, return the specified number of steps.
21650b57cec5SDimitry Andric     if (Override == "all" || Override == "default")
21660b57cec5SDimitry Andric       return RefSteps;
21670b57cec5SDimitry Andric   }
21680b57cec5SDimitry Andric 
21690b57cec5SDimitry Andric   // The attribute string may omit the size suffix ('f'/'d').
21700b57cec5SDimitry Andric   std::string VTName = getReciprocalOpName(IsSqrt, VT);
21710b57cec5SDimitry Andric   std::string VTNameNoSize = VTName;
21720b57cec5SDimitry Andric   VTNameNoSize.pop_back();
21730b57cec5SDimitry Andric 
21740b57cec5SDimitry Andric   for (StringRef RecipType : OverrideVector) {
21750b57cec5SDimitry Andric     size_t RefPos;
21760b57cec5SDimitry Andric     uint8_t RefSteps;
21770b57cec5SDimitry Andric     if (!parseRefinementStep(RecipType, RefPos, RefSteps))
21780b57cec5SDimitry Andric       continue;
21790b57cec5SDimitry Andric 
21800b57cec5SDimitry Andric     RecipType = RecipType.substr(0, RefPos);
21810b57cec5SDimitry Andric     if (RecipType.equals(VTName) || RecipType.equals(VTNameNoSize))
21820b57cec5SDimitry Andric       return RefSteps;
21830b57cec5SDimitry Andric   }
21840b57cec5SDimitry Andric 
21850b57cec5SDimitry Andric   return TargetLoweringBase::ReciprocalEstimate::Unspecified;
21860b57cec5SDimitry Andric }
21870b57cec5SDimitry Andric 
getRecipEstimateSqrtEnabled(EVT VT,MachineFunction & MF) const21880b57cec5SDimitry Andric int TargetLoweringBase::getRecipEstimateSqrtEnabled(EVT VT,
21890b57cec5SDimitry Andric                                                     MachineFunction &MF) const {
21900b57cec5SDimitry Andric   return getOpEnabled(true, VT, getRecipEstimateForFunc(MF));
21910b57cec5SDimitry Andric }
21920b57cec5SDimitry Andric 
getRecipEstimateDivEnabled(EVT VT,MachineFunction & MF) const21930b57cec5SDimitry Andric int TargetLoweringBase::getRecipEstimateDivEnabled(EVT VT,
21940b57cec5SDimitry Andric                                                    MachineFunction &MF) const {
21950b57cec5SDimitry Andric   return getOpEnabled(false, VT, getRecipEstimateForFunc(MF));
21960b57cec5SDimitry Andric }
21970b57cec5SDimitry Andric 
getSqrtRefinementSteps(EVT VT,MachineFunction & MF) const21980b57cec5SDimitry Andric int TargetLoweringBase::getSqrtRefinementSteps(EVT VT,
21990b57cec5SDimitry Andric                                                MachineFunction &MF) const {
22000b57cec5SDimitry Andric   return getOpRefinementSteps(true, VT, getRecipEstimateForFunc(MF));
22010b57cec5SDimitry Andric }
22020b57cec5SDimitry Andric 
getDivRefinementSteps(EVT VT,MachineFunction & MF) const22030b57cec5SDimitry Andric int TargetLoweringBase::getDivRefinementSteps(EVT VT,
22040b57cec5SDimitry Andric                                               MachineFunction &MF) const {
22050b57cec5SDimitry Andric   return getOpRefinementSteps(false, VT, getRecipEstimateForFunc(MF));
22060b57cec5SDimitry Andric }
22070b57cec5SDimitry Andric 
finalizeLowering(MachineFunction & MF) const22080b57cec5SDimitry Andric void TargetLoweringBase::finalizeLowering(MachineFunction &MF) const {
22090b57cec5SDimitry Andric   MF.getRegInfo().freezeReservedRegs(MF);
22100b57cec5SDimitry Andric }
22115ffd83dbSDimitry Andric 
22125ffd83dbSDimitry Andric MachineMemOperand::Flags
getLoadMemOperandFlags(const LoadInst & LI,const DataLayout & DL) const22135ffd83dbSDimitry Andric TargetLoweringBase::getLoadMemOperandFlags(const LoadInst &LI,
22145ffd83dbSDimitry Andric                                            const DataLayout &DL) const {
22155ffd83dbSDimitry Andric   MachineMemOperand::Flags Flags = MachineMemOperand::MOLoad;
22165ffd83dbSDimitry Andric   if (LI.isVolatile())
22175ffd83dbSDimitry Andric     Flags |= MachineMemOperand::MOVolatile;
22185ffd83dbSDimitry Andric 
22195ffd83dbSDimitry Andric   if (LI.hasMetadata(LLVMContext::MD_nontemporal))
22205ffd83dbSDimitry Andric     Flags |= MachineMemOperand::MONonTemporal;
22215ffd83dbSDimitry Andric 
22225ffd83dbSDimitry Andric   if (LI.hasMetadata(LLVMContext::MD_invariant_load))
22235ffd83dbSDimitry Andric     Flags |= MachineMemOperand::MOInvariant;
22245ffd83dbSDimitry Andric 
22255ffd83dbSDimitry Andric   if (isDereferenceablePointer(LI.getPointerOperand(), LI.getType(), DL))
22265ffd83dbSDimitry Andric     Flags |= MachineMemOperand::MODereferenceable;
22275ffd83dbSDimitry Andric 
22285ffd83dbSDimitry Andric   Flags |= getTargetMMOFlags(LI);
22295ffd83dbSDimitry Andric   return Flags;
22305ffd83dbSDimitry Andric }
22315ffd83dbSDimitry Andric 
22325ffd83dbSDimitry Andric MachineMemOperand::Flags
getStoreMemOperandFlags(const StoreInst & SI,const DataLayout & DL) const22335ffd83dbSDimitry Andric TargetLoweringBase::getStoreMemOperandFlags(const StoreInst &SI,
22345ffd83dbSDimitry Andric                                             const DataLayout &DL) const {
22355ffd83dbSDimitry Andric   MachineMemOperand::Flags Flags = MachineMemOperand::MOStore;
22365ffd83dbSDimitry Andric 
22375ffd83dbSDimitry Andric   if (SI.isVolatile())
22385ffd83dbSDimitry Andric     Flags |= MachineMemOperand::MOVolatile;
22395ffd83dbSDimitry Andric 
22405ffd83dbSDimitry Andric   if (SI.hasMetadata(LLVMContext::MD_nontemporal))
22415ffd83dbSDimitry Andric     Flags |= MachineMemOperand::MONonTemporal;
22425ffd83dbSDimitry Andric 
22435ffd83dbSDimitry Andric   // FIXME: Not preserving dereferenceable
22445ffd83dbSDimitry Andric   Flags |= getTargetMMOFlags(SI);
22455ffd83dbSDimitry Andric   return Flags;
22465ffd83dbSDimitry Andric }
22475ffd83dbSDimitry Andric 
22485ffd83dbSDimitry Andric MachineMemOperand::Flags
getAtomicMemOperandFlags(const Instruction & AI,const DataLayout & DL) const22495ffd83dbSDimitry Andric TargetLoweringBase::getAtomicMemOperandFlags(const Instruction &AI,
22505ffd83dbSDimitry Andric                                              const DataLayout &DL) const {
22515ffd83dbSDimitry Andric   auto Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
22525ffd83dbSDimitry Andric 
22535ffd83dbSDimitry Andric   if (const AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(&AI)) {
22545ffd83dbSDimitry Andric     if (RMW->isVolatile())
22555ffd83dbSDimitry Andric       Flags |= MachineMemOperand::MOVolatile;
22565ffd83dbSDimitry Andric   } else if (const AtomicCmpXchgInst *CmpX = dyn_cast<AtomicCmpXchgInst>(&AI)) {
22575ffd83dbSDimitry Andric     if (CmpX->isVolatile())
22585ffd83dbSDimitry Andric       Flags |= MachineMemOperand::MOVolatile;
22595ffd83dbSDimitry Andric   } else
22605ffd83dbSDimitry Andric     llvm_unreachable("not an atomic instruction");
22615ffd83dbSDimitry Andric 
22625ffd83dbSDimitry Andric   // FIXME: Not preserving dereferenceable
22635ffd83dbSDimitry Andric   Flags |= getTargetMMOFlags(AI);
22645ffd83dbSDimitry Andric   return Flags;
22655ffd83dbSDimitry Andric }
22665ffd83dbSDimitry Andric 
emitLeadingFence(IRBuilderBase & Builder,Instruction * Inst,AtomicOrdering Ord) const2267*5f7ddb14SDimitry Andric Instruction *TargetLoweringBase::emitLeadingFence(IRBuilderBase &Builder,
2268*5f7ddb14SDimitry Andric                                                   Instruction *Inst,
2269*5f7ddb14SDimitry Andric                                                   AtomicOrdering Ord) const {
2270*5f7ddb14SDimitry Andric   if (isReleaseOrStronger(Ord) && Inst->hasAtomicStore())
2271*5f7ddb14SDimitry Andric     return Builder.CreateFence(Ord);
2272*5f7ddb14SDimitry Andric   else
2273*5f7ddb14SDimitry Andric     return nullptr;
2274*5f7ddb14SDimitry Andric }
2275*5f7ddb14SDimitry Andric 
emitTrailingFence(IRBuilderBase & Builder,Instruction * Inst,AtomicOrdering Ord) const2276*5f7ddb14SDimitry Andric Instruction *TargetLoweringBase::emitTrailingFence(IRBuilderBase &Builder,
2277*5f7ddb14SDimitry Andric                                                    Instruction *Inst,
2278*5f7ddb14SDimitry Andric                                                    AtomicOrdering Ord) const {
2279*5f7ddb14SDimitry Andric   if (isAcquireOrStronger(Ord))
2280*5f7ddb14SDimitry Andric     return Builder.CreateFence(Ord);
2281*5f7ddb14SDimitry Andric   else
2282*5f7ddb14SDimitry Andric     return nullptr;
2283*5f7ddb14SDimitry Andric }
2284*5f7ddb14SDimitry Andric 
22855ffd83dbSDimitry Andric //===----------------------------------------------------------------------===//
22865ffd83dbSDimitry Andric //  GlobalISel Hooks
22875ffd83dbSDimitry Andric //===----------------------------------------------------------------------===//
22885ffd83dbSDimitry Andric 
shouldLocalize(const MachineInstr & MI,const TargetTransformInfo * TTI) const22895ffd83dbSDimitry Andric bool TargetLoweringBase::shouldLocalize(const MachineInstr &MI,
22905ffd83dbSDimitry Andric                                         const TargetTransformInfo *TTI) const {
22915ffd83dbSDimitry Andric   auto &MF = *MI.getMF();
22925ffd83dbSDimitry Andric   auto &MRI = MF.getRegInfo();
22935ffd83dbSDimitry Andric   // Assuming a spill and reload of a value has a cost of 1 instruction each,
22945ffd83dbSDimitry Andric   // this helper function computes the maximum number of uses we should consider
22955ffd83dbSDimitry Andric   // for remat. E.g. on arm64 global addresses take 2 insts to materialize. We
22965ffd83dbSDimitry Andric   // break even in terms of code size when the original MI has 2 users vs
22975ffd83dbSDimitry Andric   // choosing to potentially spill. Any more than 2 users we we have a net code
22985ffd83dbSDimitry Andric   // size increase. This doesn't take into account register pressure though.
22995ffd83dbSDimitry Andric   auto maxUses = [](unsigned RematCost) {
23005ffd83dbSDimitry Andric     // A cost of 1 means remats are basically free.
23015ffd83dbSDimitry Andric     if (RematCost == 1)
23025ffd83dbSDimitry Andric       return UINT_MAX;
23035ffd83dbSDimitry Andric     if (RematCost == 2)
23045ffd83dbSDimitry Andric       return 2U;
23055ffd83dbSDimitry Andric 
23065ffd83dbSDimitry Andric     // Remat is too expensive, only sink if there's one user.
23075ffd83dbSDimitry Andric     if (RematCost > 2)
23085ffd83dbSDimitry Andric       return 1U;
23095ffd83dbSDimitry Andric     llvm_unreachable("Unexpected remat cost");
23105ffd83dbSDimitry Andric   };
23115ffd83dbSDimitry Andric 
23125ffd83dbSDimitry Andric   // Helper to walk through uses and terminate if we've reached a limit. Saves
23135ffd83dbSDimitry Andric   // us spending time traversing uses if all we want to know is if it's >= min.
23145ffd83dbSDimitry Andric   auto isUsesAtMost = [&](unsigned Reg, unsigned MaxUses) {
23155ffd83dbSDimitry Andric     unsigned NumUses = 0;
23165ffd83dbSDimitry Andric     auto UI = MRI.use_instr_nodbg_begin(Reg), UE = MRI.use_instr_nodbg_end();
23175ffd83dbSDimitry Andric     for (; UI != UE && NumUses < MaxUses; ++UI) {
23185ffd83dbSDimitry Andric       NumUses++;
23195ffd83dbSDimitry Andric     }
23205ffd83dbSDimitry Andric     // If we haven't reached the end yet then there are more than MaxUses users.
23215ffd83dbSDimitry Andric     return UI == UE;
23225ffd83dbSDimitry Andric   };
23235ffd83dbSDimitry Andric 
23245ffd83dbSDimitry Andric   switch (MI.getOpcode()) {
23255ffd83dbSDimitry Andric   default:
23265ffd83dbSDimitry Andric     return false;
23275ffd83dbSDimitry Andric   // Constants-like instructions should be close to their users.
23285ffd83dbSDimitry Andric   // We don't want long live-ranges for them.
23295ffd83dbSDimitry Andric   case TargetOpcode::G_CONSTANT:
23305ffd83dbSDimitry Andric   case TargetOpcode::G_FCONSTANT:
23315ffd83dbSDimitry Andric   case TargetOpcode::G_FRAME_INDEX:
23325ffd83dbSDimitry Andric   case TargetOpcode::G_INTTOPTR:
23335ffd83dbSDimitry Andric     return true;
23345ffd83dbSDimitry Andric   case TargetOpcode::G_GLOBAL_VALUE: {
23355ffd83dbSDimitry Andric     unsigned RematCost = TTI->getGISelRematGlobalCost();
23365ffd83dbSDimitry Andric     Register Reg = MI.getOperand(0).getReg();
23375ffd83dbSDimitry Andric     unsigned MaxUses = maxUses(RematCost);
23385ffd83dbSDimitry Andric     if (MaxUses == UINT_MAX)
23395ffd83dbSDimitry Andric       return true; // Remats are "free" so always localize.
23405ffd83dbSDimitry Andric     bool B = isUsesAtMost(Reg, MaxUses);
23415ffd83dbSDimitry Andric     return B;
23425ffd83dbSDimitry Andric   }
23435ffd83dbSDimitry Andric   }
23445ffd83dbSDimitry Andric }
2345