xref: /netbsd-src/external/apache2/llvm/dist/llvm/lib/CodeGen/TargetLoweringBase.cpp (revision 82d56013d7b633d116a93943de88e08335357a7c)
17330f729Sjoerg //===- TargetLoweringBase.cpp - Implement the TargetLoweringBase class ----===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This implements the TargetLoweringBase class.
107330f729Sjoerg //
117330f729Sjoerg //===----------------------------------------------------------------------===//
127330f729Sjoerg 
137330f729Sjoerg #include "llvm/ADT/BitVector.h"
147330f729Sjoerg #include "llvm/ADT/STLExtras.h"
157330f729Sjoerg #include "llvm/ADT/SmallVector.h"
167330f729Sjoerg #include "llvm/ADT/StringExtras.h"
177330f729Sjoerg #include "llvm/ADT/StringRef.h"
187330f729Sjoerg #include "llvm/ADT/Triple.h"
197330f729Sjoerg #include "llvm/ADT/Twine.h"
20*82d56013Sjoerg #include "llvm/Analysis/Loads.h"
21*82d56013Sjoerg #include "llvm/Analysis/TargetTransformInfo.h"
227330f729Sjoerg #include "llvm/CodeGen/Analysis.h"
237330f729Sjoerg #include "llvm/CodeGen/ISDOpcodes.h"
247330f729Sjoerg #include "llvm/CodeGen/MachineBasicBlock.h"
257330f729Sjoerg #include "llvm/CodeGen/MachineFrameInfo.h"
267330f729Sjoerg #include "llvm/CodeGen/MachineFunction.h"
277330f729Sjoerg #include "llvm/CodeGen/MachineInstr.h"
287330f729Sjoerg #include "llvm/CodeGen/MachineInstrBuilder.h"
297330f729Sjoerg #include "llvm/CodeGen/MachineMemOperand.h"
307330f729Sjoerg #include "llvm/CodeGen/MachineOperand.h"
317330f729Sjoerg #include "llvm/CodeGen/MachineRegisterInfo.h"
327330f729Sjoerg #include "llvm/CodeGen/RuntimeLibcalls.h"
337330f729Sjoerg #include "llvm/CodeGen/StackMaps.h"
347330f729Sjoerg #include "llvm/CodeGen/TargetLowering.h"
357330f729Sjoerg #include "llvm/CodeGen/TargetOpcodes.h"
367330f729Sjoerg #include "llvm/CodeGen/TargetRegisterInfo.h"
377330f729Sjoerg #include "llvm/CodeGen/ValueTypes.h"
387330f729Sjoerg #include "llvm/IR/Attributes.h"
397330f729Sjoerg #include "llvm/IR/CallingConv.h"
407330f729Sjoerg #include "llvm/IR/DataLayout.h"
417330f729Sjoerg #include "llvm/IR/DerivedTypes.h"
427330f729Sjoerg #include "llvm/IR/Function.h"
437330f729Sjoerg #include "llvm/IR/GlobalValue.h"
447330f729Sjoerg #include "llvm/IR/GlobalVariable.h"
457330f729Sjoerg #include "llvm/IR/IRBuilder.h"
467330f729Sjoerg #include "llvm/IR/Module.h"
477330f729Sjoerg #include "llvm/IR/Type.h"
487330f729Sjoerg #include "llvm/Support/Casting.h"
497330f729Sjoerg #include "llvm/Support/CommandLine.h"
507330f729Sjoerg #include "llvm/Support/Compiler.h"
517330f729Sjoerg #include "llvm/Support/ErrorHandling.h"
527330f729Sjoerg #include "llvm/Support/MachineValueType.h"
537330f729Sjoerg #include "llvm/Support/MathExtras.h"
547330f729Sjoerg #include "llvm/Target/TargetMachine.h"
55*82d56013Sjoerg #include "llvm/Transforms/Utils/SizeOpts.h"
567330f729Sjoerg #include <algorithm>
577330f729Sjoerg #include <cassert>
587330f729Sjoerg #include <cstddef>
597330f729Sjoerg #include <cstdint>
607330f729Sjoerg #include <cstring>
617330f729Sjoerg #include <iterator>
627330f729Sjoerg #include <string>
637330f729Sjoerg #include <tuple>
647330f729Sjoerg #include <utility>
657330f729Sjoerg 
667330f729Sjoerg using namespace llvm;
677330f729Sjoerg 
687330f729Sjoerg static cl::opt<bool> JumpIsExpensiveOverride(
697330f729Sjoerg     "jump-is-expensive", cl::init(false),
707330f729Sjoerg     cl::desc("Do not create extra branches to split comparison logic."),
717330f729Sjoerg     cl::Hidden);
727330f729Sjoerg 
737330f729Sjoerg static cl::opt<unsigned> MinimumJumpTableEntries
747330f729Sjoerg   ("min-jump-table-entries", cl::init(4), cl::Hidden,
757330f729Sjoerg    cl::desc("Set minimum number of entries to use a jump table."));
767330f729Sjoerg 
777330f729Sjoerg static cl::opt<unsigned> MaximumJumpTableSize
787330f729Sjoerg   ("max-jump-table-size", cl::init(UINT_MAX), cl::Hidden,
797330f729Sjoerg    cl::desc("Set maximum size of jump tables."));
807330f729Sjoerg 
817330f729Sjoerg /// Minimum jump table density for normal functions.
827330f729Sjoerg static cl::opt<unsigned>
837330f729Sjoerg     JumpTableDensity("jump-table-density", cl::init(10), cl::Hidden,
847330f729Sjoerg                      cl::desc("Minimum density for building a jump table in "
857330f729Sjoerg                               "a normal function"));
867330f729Sjoerg 
877330f729Sjoerg /// Minimum jump table density for -Os or -Oz functions.
887330f729Sjoerg static cl::opt<unsigned> OptsizeJumpTableDensity(
897330f729Sjoerg     "optsize-jump-table-density", cl::init(40), cl::Hidden,
907330f729Sjoerg     cl::desc("Minimum density for building a jump table in "
917330f729Sjoerg              "an optsize function"));
927330f729Sjoerg 
93*82d56013Sjoerg // FIXME: This option is only to test if the strict fp operation processed
94*82d56013Sjoerg // correctly by preventing mutating strict fp operation to normal fp operation
95*82d56013Sjoerg // during development. When the backend supports strict float operation, this
96*82d56013Sjoerg // option will be meaningless.
97*82d56013Sjoerg static cl::opt<bool> DisableStrictNodeMutation("disable-strictnode-mutation",
98*82d56013Sjoerg        cl::desc("Don't mutate strict-float node to a legalize node"),
99*82d56013Sjoerg        cl::init(false), cl::Hidden);
100*82d56013Sjoerg 
darwinHasSinCos(const Triple & TT)1017330f729Sjoerg static bool darwinHasSinCos(const Triple &TT) {
1027330f729Sjoerg   assert(TT.isOSDarwin() && "should be called with darwin triple");
1037330f729Sjoerg   // Don't bother with 32 bit x86.
1047330f729Sjoerg   if (TT.getArch() == Triple::x86)
1057330f729Sjoerg     return false;
1067330f729Sjoerg   // Macos < 10.9 has no sincos_stret.
1077330f729Sjoerg   if (TT.isMacOSX())
1087330f729Sjoerg     return !TT.isMacOSXVersionLT(10, 9) && TT.isArch64Bit();
1097330f729Sjoerg   // iOS < 7.0 has no sincos_stret.
1107330f729Sjoerg   if (TT.isiOS())
1117330f729Sjoerg     return !TT.isOSVersionLT(7, 0);
1127330f729Sjoerg   // Any other darwin such as WatchOS/TvOS is new enough.
1137330f729Sjoerg   return true;
1147330f729Sjoerg }
1157330f729Sjoerg 
InitLibcalls(const Triple & TT)1167330f729Sjoerg void TargetLoweringBase::InitLibcalls(const Triple &TT) {
1177330f729Sjoerg #define HANDLE_LIBCALL(code, name) \
1187330f729Sjoerg   setLibcallName(RTLIB::code, name);
1197330f729Sjoerg #include "llvm/IR/RuntimeLibcalls.def"
1207330f729Sjoerg #undef HANDLE_LIBCALL
1217330f729Sjoerg   // Initialize calling conventions to their default.
1227330f729Sjoerg   for (int LC = 0; LC < RTLIB::UNKNOWN_LIBCALL; ++LC)
1237330f729Sjoerg     setLibcallCallingConv((RTLIB::Libcall)LC, CallingConv::C);
1247330f729Sjoerg 
1257330f729Sjoerg   // For IEEE quad-precision libcall names, PPC uses "kf" instead of "tf".
126*82d56013Sjoerg   if (TT.isPPC()) {
1277330f729Sjoerg     setLibcallName(RTLIB::ADD_F128, "__addkf3");
1287330f729Sjoerg     setLibcallName(RTLIB::SUB_F128, "__subkf3");
1297330f729Sjoerg     setLibcallName(RTLIB::MUL_F128, "__mulkf3");
1307330f729Sjoerg     setLibcallName(RTLIB::DIV_F128, "__divkf3");
131*82d56013Sjoerg     setLibcallName(RTLIB::POWI_F128, "__powikf2");
1327330f729Sjoerg     setLibcallName(RTLIB::FPEXT_F32_F128, "__extendsfkf2");
1337330f729Sjoerg     setLibcallName(RTLIB::FPEXT_F64_F128, "__extenddfkf2");
1347330f729Sjoerg     setLibcallName(RTLIB::FPROUND_F128_F32, "__trunckfsf2");
1357330f729Sjoerg     setLibcallName(RTLIB::FPROUND_F128_F64, "__trunckfdf2");
1367330f729Sjoerg     setLibcallName(RTLIB::FPTOSINT_F128_I32, "__fixkfsi");
1377330f729Sjoerg     setLibcallName(RTLIB::FPTOSINT_F128_I64, "__fixkfdi");
138*82d56013Sjoerg     setLibcallName(RTLIB::FPTOSINT_F128_I128, "__fixkfti");
1397330f729Sjoerg     setLibcallName(RTLIB::FPTOUINT_F128_I32, "__fixunskfsi");
1407330f729Sjoerg     setLibcallName(RTLIB::FPTOUINT_F128_I64, "__fixunskfdi");
141*82d56013Sjoerg     setLibcallName(RTLIB::FPTOUINT_F128_I128, "__fixunskfti");
1427330f729Sjoerg     setLibcallName(RTLIB::SINTTOFP_I32_F128, "__floatsikf");
1437330f729Sjoerg     setLibcallName(RTLIB::SINTTOFP_I64_F128, "__floatdikf");
144*82d56013Sjoerg     setLibcallName(RTLIB::SINTTOFP_I128_F128, "__floattikf");
1457330f729Sjoerg     setLibcallName(RTLIB::UINTTOFP_I32_F128, "__floatunsikf");
1467330f729Sjoerg     setLibcallName(RTLIB::UINTTOFP_I64_F128, "__floatundikf");
147*82d56013Sjoerg     setLibcallName(RTLIB::UINTTOFP_I128_F128, "__floatuntikf");
1487330f729Sjoerg     setLibcallName(RTLIB::OEQ_F128, "__eqkf2");
1497330f729Sjoerg     setLibcallName(RTLIB::UNE_F128, "__nekf2");
1507330f729Sjoerg     setLibcallName(RTLIB::OGE_F128, "__gekf2");
1517330f729Sjoerg     setLibcallName(RTLIB::OLT_F128, "__ltkf2");
1527330f729Sjoerg     setLibcallName(RTLIB::OLE_F128, "__lekf2");
1537330f729Sjoerg     setLibcallName(RTLIB::OGT_F128, "__gtkf2");
1547330f729Sjoerg     setLibcallName(RTLIB::UO_F128, "__unordkf2");
1557330f729Sjoerg   }
1567330f729Sjoerg 
1577330f729Sjoerg   // A few names are different on particular architectures or environments.
1587330f729Sjoerg   if (TT.isOSDarwin()) {
1597330f729Sjoerg     // For f16/f32 conversions, Darwin uses the standard naming scheme, instead
1607330f729Sjoerg     // of the gnueabi-style __gnu_*_ieee.
1617330f729Sjoerg     // FIXME: What about other targets?
1627330f729Sjoerg     setLibcallName(RTLIB::FPEXT_F16_F32, "__extendhfsf2");
1637330f729Sjoerg     setLibcallName(RTLIB::FPROUND_F32_F16, "__truncsfhf2");
1647330f729Sjoerg 
1657330f729Sjoerg     // Some darwins have an optimized __bzero/bzero function.
1667330f729Sjoerg     switch (TT.getArch()) {
1677330f729Sjoerg     case Triple::x86:
1687330f729Sjoerg     case Triple::x86_64:
1697330f729Sjoerg       if (TT.isMacOSX() && !TT.isMacOSXVersionLT(10, 6))
1707330f729Sjoerg         setLibcallName(RTLIB::BZERO, "__bzero");
1717330f729Sjoerg       break;
1727330f729Sjoerg     case Triple::aarch64:
1737330f729Sjoerg     case Triple::aarch64_32:
1747330f729Sjoerg       setLibcallName(RTLIB::BZERO, "bzero");
1757330f729Sjoerg       break;
1767330f729Sjoerg     default:
1777330f729Sjoerg       break;
1787330f729Sjoerg     }
1797330f729Sjoerg 
1807330f729Sjoerg     if (darwinHasSinCos(TT)) {
1817330f729Sjoerg       setLibcallName(RTLIB::SINCOS_STRET_F32, "__sincosf_stret");
1827330f729Sjoerg       setLibcallName(RTLIB::SINCOS_STRET_F64, "__sincos_stret");
1837330f729Sjoerg       if (TT.isWatchABI()) {
1847330f729Sjoerg         setLibcallCallingConv(RTLIB::SINCOS_STRET_F32,
1857330f729Sjoerg                               CallingConv::ARM_AAPCS_VFP);
1867330f729Sjoerg         setLibcallCallingConv(RTLIB::SINCOS_STRET_F64,
1877330f729Sjoerg                               CallingConv::ARM_AAPCS_VFP);
1887330f729Sjoerg       }
1897330f729Sjoerg     }
1907330f729Sjoerg   } else {
1917330f729Sjoerg     setLibcallName(RTLIB::FPEXT_F16_F32, "__gnu_h2f_ieee");
1927330f729Sjoerg     setLibcallName(RTLIB::FPROUND_F32_F16, "__gnu_f2h_ieee");
1937330f729Sjoerg   }
1947330f729Sjoerg 
1957330f729Sjoerg   if (TT.isGNUEnvironment() || TT.isOSFuchsia() ||
1967330f729Sjoerg       (TT.isAndroid() && !TT.isAndroidVersionLT(9))) {
1977330f729Sjoerg     setLibcallName(RTLIB::SINCOS_F32, "sincosf");
1987330f729Sjoerg     setLibcallName(RTLIB::SINCOS_F64, "sincos");
1997330f729Sjoerg     setLibcallName(RTLIB::SINCOS_F80, "sincosl");
2007330f729Sjoerg     setLibcallName(RTLIB::SINCOS_F128, "sincosl");
2017330f729Sjoerg     setLibcallName(RTLIB::SINCOS_PPCF128, "sincosl");
2027330f729Sjoerg   }
2037330f729Sjoerg 
2047330f729Sjoerg   if (TT.isPS4CPU()) {
2057330f729Sjoerg     setLibcallName(RTLIB::SINCOS_F32, "sincosf");
2067330f729Sjoerg     setLibcallName(RTLIB::SINCOS_F64, "sincos");
2077330f729Sjoerg   }
2087330f729Sjoerg 
2097330f729Sjoerg   if (TT.isOSOpenBSD()) {
2107330f729Sjoerg     setLibcallName(RTLIB::STACKPROTECTOR_CHECK_FAIL, nullptr);
2117330f729Sjoerg   }
2127330f729Sjoerg }
2137330f729Sjoerg 
2147330f729Sjoerg /// getFPEXT - Return the FPEXT_*_* value for the given types, or
2157330f729Sjoerg /// UNKNOWN_LIBCALL if there is none.
getFPEXT(EVT OpVT,EVT RetVT)2167330f729Sjoerg RTLIB::Libcall RTLIB::getFPEXT(EVT OpVT, EVT RetVT) {
2177330f729Sjoerg   if (OpVT == MVT::f16) {
2187330f729Sjoerg     if (RetVT == MVT::f32)
2197330f729Sjoerg       return FPEXT_F16_F32;
220*82d56013Sjoerg     if (RetVT == MVT::f64)
221*82d56013Sjoerg       return FPEXT_F16_F64;
222*82d56013Sjoerg     if (RetVT == MVT::f128)
223*82d56013Sjoerg       return FPEXT_F16_F128;
2247330f729Sjoerg   } else if (OpVT == MVT::f32) {
2257330f729Sjoerg     if (RetVT == MVT::f64)
2267330f729Sjoerg       return FPEXT_F32_F64;
2277330f729Sjoerg     if (RetVT == MVT::f128)
2287330f729Sjoerg       return FPEXT_F32_F128;
2297330f729Sjoerg     if (RetVT == MVT::ppcf128)
2307330f729Sjoerg       return FPEXT_F32_PPCF128;
2317330f729Sjoerg   } else if (OpVT == MVT::f64) {
2327330f729Sjoerg     if (RetVT == MVT::f128)
2337330f729Sjoerg       return FPEXT_F64_F128;
2347330f729Sjoerg     else if (RetVT == MVT::ppcf128)
2357330f729Sjoerg       return FPEXT_F64_PPCF128;
2367330f729Sjoerg   } else if (OpVT == MVT::f80) {
2377330f729Sjoerg     if (RetVT == MVT::f128)
2387330f729Sjoerg       return FPEXT_F80_F128;
2397330f729Sjoerg   }
2407330f729Sjoerg 
2417330f729Sjoerg   return UNKNOWN_LIBCALL;
2427330f729Sjoerg }
2437330f729Sjoerg 
2447330f729Sjoerg /// getFPROUND - Return the FPROUND_*_* value for the given types, or
2457330f729Sjoerg /// UNKNOWN_LIBCALL if there is none.
getFPROUND(EVT OpVT,EVT RetVT)2467330f729Sjoerg RTLIB::Libcall RTLIB::getFPROUND(EVT OpVT, EVT RetVT) {
2477330f729Sjoerg   if (RetVT == MVT::f16) {
2487330f729Sjoerg     if (OpVT == MVT::f32)
2497330f729Sjoerg       return FPROUND_F32_F16;
2507330f729Sjoerg     if (OpVT == MVT::f64)
2517330f729Sjoerg       return FPROUND_F64_F16;
2527330f729Sjoerg     if (OpVT == MVT::f80)
2537330f729Sjoerg       return FPROUND_F80_F16;
2547330f729Sjoerg     if (OpVT == MVT::f128)
2557330f729Sjoerg       return FPROUND_F128_F16;
2567330f729Sjoerg     if (OpVT == MVT::ppcf128)
2577330f729Sjoerg       return FPROUND_PPCF128_F16;
2587330f729Sjoerg   } else if (RetVT == MVT::f32) {
2597330f729Sjoerg     if (OpVT == MVT::f64)
2607330f729Sjoerg       return FPROUND_F64_F32;
2617330f729Sjoerg     if (OpVT == MVT::f80)
2627330f729Sjoerg       return FPROUND_F80_F32;
2637330f729Sjoerg     if (OpVT == MVT::f128)
2647330f729Sjoerg       return FPROUND_F128_F32;
2657330f729Sjoerg     if (OpVT == MVT::ppcf128)
2667330f729Sjoerg       return FPROUND_PPCF128_F32;
2677330f729Sjoerg   } else if (RetVT == MVT::f64) {
2687330f729Sjoerg     if (OpVT == MVT::f80)
2697330f729Sjoerg       return FPROUND_F80_F64;
2707330f729Sjoerg     if (OpVT == MVT::f128)
2717330f729Sjoerg       return FPROUND_F128_F64;
2727330f729Sjoerg     if (OpVT == MVT::ppcf128)
2737330f729Sjoerg       return FPROUND_PPCF128_F64;
2747330f729Sjoerg   } else if (RetVT == MVT::f80) {
2757330f729Sjoerg     if (OpVT == MVT::f128)
2767330f729Sjoerg       return FPROUND_F128_F80;
2777330f729Sjoerg   }
2787330f729Sjoerg 
2797330f729Sjoerg   return UNKNOWN_LIBCALL;
2807330f729Sjoerg }
2817330f729Sjoerg 
2827330f729Sjoerg /// getFPTOSINT - Return the FPTOSINT_*_* value for the given types, or
2837330f729Sjoerg /// UNKNOWN_LIBCALL if there is none.
getFPTOSINT(EVT OpVT,EVT RetVT)2847330f729Sjoerg RTLIB::Libcall RTLIB::getFPTOSINT(EVT OpVT, EVT RetVT) {
285*82d56013Sjoerg   if (OpVT == MVT::f16) {
286*82d56013Sjoerg     if (RetVT == MVT::i32)
287*82d56013Sjoerg       return FPTOSINT_F16_I32;
288*82d56013Sjoerg     if (RetVT == MVT::i64)
289*82d56013Sjoerg       return FPTOSINT_F16_I64;
290*82d56013Sjoerg     if (RetVT == MVT::i128)
291*82d56013Sjoerg       return FPTOSINT_F16_I128;
292*82d56013Sjoerg   } else if (OpVT == MVT::f32) {
2937330f729Sjoerg     if (RetVT == MVT::i32)
2947330f729Sjoerg       return FPTOSINT_F32_I32;
2957330f729Sjoerg     if (RetVT == MVT::i64)
2967330f729Sjoerg       return FPTOSINT_F32_I64;
2977330f729Sjoerg     if (RetVT == MVT::i128)
2987330f729Sjoerg       return FPTOSINT_F32_I128;
2997330f729Sjoerg   } else if (OpVT == MVT::f64) {
3007330f729Sjoerg     if (RetVT == MVT::i32)
3017330f729Sjoerg       return FPTOSINT_F64_I32;
3027330f729Sjoerg     if (RetVT == MVT::i64)
3037330f729Sjoerg       return FPTOSINT_F64_I64;
3047330f729Sjoerg     if (RetVT == MVT::i128)
3057330f729Sjoerg       return FPTOSINT_F64_I128;
3067330f729Sjoerg   } else if (OpVT == MVT::f80) {
3077330f729Sjoerg     if (RetVT == MVT::i32)
3087330f729Sjoerg       return FPTOSINT_F80_I32;
3097330f729Sjoerg     if (RetVT == MVT::i64)
3107330f729Sjoerg       return FPTOSINT_F80_I64;
3117330f729Sjoerg     if (RetVT == MVT::i128)
3127330f729Sjoerg       return FPTOSINT_F80_I128;
3137330f729Sjoerg   } else if (OpVT == MVT::f128) {
3147330f729Sjoerg     if (RetVT == MVT::i32)
3157330f729Sjoerg       return FPTOSINT_F128_I32;
3167330f729Sjoerg     if (RetVT == MVT::i64)
3177330f729Sjoerg       return FPTOSINT_F128_I64;
3187330f729Sjoerg     if (RetVT == MVT::i128)
3197330f729Sjoerg       return FPTOSINT_F128_I128;
3207330f729Sjoerg   } else if (OpVT == MVT::ppcf128) {
3217330f729Sjoerg     if (RetVT == MVT::i32)
3227330f729Sjoerg       return FPTOSINT_PPCF128_I32;
3237330f729Sjoerg     if (RetVT == MVT::i64)
3247330f729Sjoerg       return FPTOSINT_PPCF128_I64;
3257330f729Sjoerg     if (RetVT == MVT::i128)
3267330f729Sjoerg       return FPTOSINT_PPCF128_I128;
3277330f729Sjoerg   }
3287330f729Sjoerg   return UNKNOWN_LIBCALL;
3297330f729Sjoerg }
3307330f729Sjoerg 
3317330f729Sjoerg /// getFPTOUINT - Return the FPTOUINT_*_* value for the given types, or
3327330f729Sjoerg /// UNKNOWN_LIBCALL if there is none.
getFPTOUINT(EVT OpVT,EVT RetVT)3337330f729Sjoerg RTLIB::Libcall RTLIB::getFPTOUINT(EVT OpVT, EVT RetVT) {
334*82d56013Sjoerg   if (OpVT == MVT::f16) {
335*82d56013Sjoerg     if (RetVT == MVT::i32)
336*82d56013Sjoerg       return FPTOUINT_F16_I32;
337*82d56013Sjoerg     if (RetVT == MVT::i64)
338*82d56013Sjoerg       return FPTOUINT_F16_I64;
339*82d56013Sjoerg     if (RetVT == MVT::i128)
340*82d56013Sjoerg       return FPTOUINT_F16_I128;
341*82d56013Sjoerg   } else if (OpVT == MVT::f32) {
3427330f729Sjoerg     if (RetVT == MVT::i32)
3437330f729Sjoerg       return FPTOUINT_F32_I32;
3447330f729Sjoerg     if (RetVT == MVT::i64)
3457330f729Sjoerg       return FPTOUINT_F32_I64;
3467330f729Sjoerg     if (RetVT == MVT::i128)
3477330f729Sjoerg       return FPTOUINT_F32_I128;
3487330f729Sjoerg   } else if (OpVT == MVT::f64) {
3497330f729Sjoerg     if (RetVT == MVT::i32)
3507330f729Sjoerg       return FPTOUINT_F64_I32;
3517330f729Sjoerg     if (RetVT == MVT::i64)
3527330f729Sjoerg       return FPTOUINT_F64_I64;
3537330f729Sjoerg     if (RetVT == MVT::i128)
3547330f729Sjoerg       return FPTOUINT_F64_I128;
3557330f729Sjoerg   } else if (OpVT == MVT::f80) {
3567330f729Sjoerg     if (RetVT == MVT::i32)
3577330f729Sjoerg       return FPTOUINT_F80_I32;
3587330f729Sjoerg     if (RetVT == MVT::i64)
3597330f729Sjoerg       return FPTOUINT_F80_I64;
3607330f729Sjoerg     if (RetVT == MVT::i128)
3617330f729Sjoerg       return FPTOUINT_F80_I128;
3627330f729Sjoerg   } else if (OpVT == MVT::f128) {
3637330f729Sjoerg     if (RetVT == MVT::i32)
3647330f729Sjoerg       return FPTOUINT_F128_I32;
3657330f729Sjoerg     if (RetVT == MVT::i64)
3667330f729Sjoerg       return FPTOUINT_F128_I64;
3677330f729Sjoerg     if (RetVT == MVT::i128)
3687330f729Sjoerg       return FPTOUINT_F128_I128;
3697330f729Sjoerg   } else if (OpVT == MVT::ppcf128) {
3707330f729Sjoerg     if (RetVT == MVT::i32)
3717330f729Sjoerg       return FPTOUINT_PPCF128_I32;
3727330f729Sjoerg     if (RetVT == MVT::i64)
3737330f729Sjoerg       return FPTOUINT_PPCF128_I64;
3747330f729Sjoerg     if (RetVT == MVT::i128)
3757330f729Sjoerg       return FPTOUINT_PPCF128_I128;
3767330f729Sjoerg   }
3777330f729Sjoerg   return UNKNOWN_LIBCALL;
3787330f729Sjoerg }
3797330f729Sjoerg 
3807330f729Sjoerg /// getSINTTOFP - Return the SINTTOFP_*_* value for the given types, or
3817330f729Sjoerg /// UNKNOWN_LIBCALL if there is none.
getSINTTOFP(EVT OpVT,EVT RetVT)3827330f729Sjoerg RTLIB::Libcall RTLIB::getSINTTOFP(EVT OpVT, EVT RetVT) {
3837330f729Sjoerg   if (OpVT == MVT::i32) {
384*82d56013Sjoerg     if (RetVT == MVT::f16)
385*82d56013Sjoerg       return SINTTOFP_I32_F16;
3867330f729Sjoerg     if (RetVT == MVT::f32)
3877330f729Sjoerg       return SINTTOFP_I32_F32;
3887330f729Sjoerg     if (RetVT == MVT::f64)
3897330f729Sjoerg       return SINTTOFP_I32_F64;
3907330f729Sjoerg     if (RetVT == MVT::f80)
3917330f729Sjoerg       return SINTTOFP_I32_F80;
3927330f729Sjoerg     if (RetVT == MVT::f128)
3937330f729Sjoerg       return SINTTOFP_I32_F128;
3947330f729Sjoerg     if (RetVT == MVT::ppcf128)
3957330f729Sjoerg       return SINTTOFP_I32_PPCF128;
3967330f729Sjoerg   } else if (OpVT == MVT::i64) {
397*82d56013Sjoerg     if (RetVT == MVT::f16)
398*82d56013Sjoerg       return SINTTOFP_I64_F16;
3997330f729Sjoerg     if (RetVT == MVT::f32)
4007330f729Sjoerg       return SINTTOFP_I64_F32;
4017330f729Sjoerg     if (RetVT == MVT::f64)
4027330f729Sjoerg       return SINTTOFP_I64_F64;
4037330f729Sjoerg     if (RetVT == MVT::f80)
4047330f729Sjoerg       return SINTTOFP_I64_F80;
4057330f729Sjoerg     if (RetVT == MVT::f128)
4067330f729Sjoerg       return SINTTOFP_I64_F128;
4077330f729Sjoerg     if (RetVT == MVT::ppcf128)
4087330f729Sjoerg       return SINTTOFP_I64_PPCF128;
4097330f729Sjoerg   } else if (OpVT == MVT::i128) {
410*82d56013Sjoerg     if (RetVT == MVT::f16)
411*82d56013Sjoerg       return SINTTOFP_I128_F16;
4127330f729Sjoerg     if (RetVT == MVT::f32)
4137330f729Sjoerg       return SINTTOFP_I128_F32;
4147330f729Sjoerg     if (RetVT == MVT::f64)
4157330f729Sjoerg       return SINTTOFP_I128_F64;
4167330f729Sjoerg     if (RetVT == MVT::f80)
4177330f729Sjoerg       return SINTTOFP_I128_F80;
4187330f729Sjoerg     if (RetVT == MVT::f128)
4197330f729Sjoerg       return SINTTOFP_I128_F128;
4207330f729Sjoerg     if (RetVT == MVT::ppcf128)
4217330f729Sjoerg       return SINTTOFP_I128_PPCF128;
4227330f729Sjoerg   }
4237330f729Sjoerg   return UNKNOWN_LIBCALL;
4247330f729Sjoerg }
4257330f729Sjoerg 
4267330f729Sjoerg /// getUINTTOFP - Return the UINTTOFP_*_* value for the given types, or
4277330f729Sjoerg /// UNKNOWN_LIBCALL if there is none.
getUINTTOFP(EVT OpVT,EVT RetVT)4287330f729Sjoerg RTLIB::Libcall RTLIB::getUINTTOFP(EVT OpVT, EVT RetVT) {
4297330f729Sjoerg   if (OpVT == MVT::i32) {
430*82d56013Sjoerg     if (RetVT == MVT::f16)
431*82d56013Sjoerg       return UINTTOFP_I32_F16;
4327330f729Sjoerg     if (RetVT == MVT::f32)
4337330f729Sjoerg       return UINTTOFP_I32_F32;
4347330f729Sjoerg     if (RetVT == MVT::f64)
4357330f729Sjoerg       return UINTTOFP_I32_F64;
4367330f729Sjoerg     if (RetVT == MVT::f80)
4377330f729Sjoerg       return UINTTOFP_I32_F80;
4387330f729Sjoerg     if (RetVT == MVT::f128)
4397330f729Sjoerg       return UINTTOFP_I32_F128;
4407330f729Sjoerg     if (RetVT == MVT::ppcf128)
4417330f729Sjoerg       return UINTTOFP_I32_PPCF128;
4427330f729Sjoerg   } else if (OpVT == MVT::i64) {
443*82d56013Sjoerg     if (RetVT == MVT::f16)
444*82d56013Sjoerg       return UINTTOFP_I64_F16;
4457330f729Sjoerg     if (RetVT == MVT::f32)
4467330f729Sjoerg       return UINTTOFP_I64_F32;
4477330f729Sjoerg     if (RetVT == MVT::f64)
4487330f729Sjoerg       return UINTTOFP_I64_F64;
4497330f729Sjoerg     if (RetVT == MVT::f80)
4507330f729Sjoerg       return UINTTOFP_I64_F80;
4517330f729Sjoerg     if (RetVT == MVT::f128)
4527330f729Sjoerg       return UINTTOFP_I64_F128;
4537330f729Sjoerg     if (RetVT == MVT::ppcf128)
4547330f729Sjoerg       return UINTTOFP_I64_PPCF128;
4557330f729Sjoerg   } else if (OpVT == MVT::i128) {
456*82d56013Sjoerg     if (RetVT == MVT::f16)
457*82d56013Sjoerg       return UINTTOFP_I128_F16;
4587330f729Sjoerg     if (RetVT == MVT::f32)
4597330f729Sjoerg       return UINTTOFP_I128_F32;
4607330f729Sjoerg     if (RetVT == MVT::f64)
4617330f729Sjoerg       return UINTTOFP_I128_F64;
4627330f729Sjoerg     if (RetVT == MVT::f80)
4637330f729Sjoerg       return UINTTOFP_I128_F80;
4647330f729Sjoerg     if (RetVT == MVT::f128)
4657330f729Sjoerg       return UINTTOFP_I128_F128;
4667330f729Sjoerg     if (RetVT == MVT::ppcf128)
4677330f729Sjoerg       return UINTTOFP_I128_PPCF128;
4687330f729Sjoerg   }
4697330f729Sjoerg   return UNKNOWN_LIBCALL;
4707330f729Sjoerg }
4717330f729Sjoerg 
getOUTLINE_ATOMIC(unsigned Opc,AtomicOrdering Order,MVT VT)472*82d56013Sjoerg RTLIB::Libcall RTLIB::getOUTLINE_ATOMIC(unsigned Opc, AtomicOrdering Order,
473*82d56013Sjoerg                                         MVT VT) {
474*82d56013Sjoerg   unsigned ModeN, ModelN;
475*82d56013Sjoerg   switch (VT.SimpleTy) {
476*82d56013Sjoerg   case MVT::i8:
477*82d56013Sjoerg     ModeN = 0;
478*82d56013Sjoerg     break;
479*82d56013Sjoerg   case MVT::i16:
480*82d56013Sjoerg     ModeN = 1;
481*82d56013Sjoerg     break;
482*82d56013Sjoerg   case MVT::i32:
483*82d56013Sjoerg     ModeN = 2;
484*82d56013Sjoerg     break;
485*82d56013Sjoerg   case MVT::i64:
486*82d56013Sjoerg     ModeN = 3;
487*82d56013Sjoerg     break;
488*82d56013Sjoerg   case MVT::i128:
489*82d56013Sjoerg     ModeN = 4;
490*82d56013Sjoerg     break;
491*82d56013Sjoerg   default:
492*82d56013Sjoerg     return UNKNOWN_LIBCALL;
493*82d56013Sjoerg   }
494*82d56013Sjoerg 
495*82d56013Sjoerg   switch (Order) {
496*82d56013Sjoerg   case AtomicOrdering::Monotonic:
497*82d56013Sjoerg     ModelN = 0;
498*82d56013Sjoerg     break;
499*82d56013Sjoerg   case AtomicOrdering::Acquire:
500*82d56013Sjoerg     ModelN = 1;
501*82d56013Sjoerg     break;
502*82d56013Sjoerg   case AtomicOrdering::Release:
503*82d56013Sjoerg     ModelN = 2;
504*82d56013Sjoerg     break;
505*82d56013Sjoerg   case AtomicOrdering::AcquireRelease:
506*82d56013Sjoerg   case AtomicOrdering::SequentiallyConsistent:
507*82d56013Sjoerg     ModelN = 3;
508*82d56013Sjoerg     break;
509*82d56013Sjoerg   default:
510*82d56013Sjoerg     return UNKNOWN_LIBCALL;
511*82d56013Sjoerg   }
512*82d56013Sjoerg 
513*82d56013Sjoerg #define LCALLS(A, B)                                                           \
514*82d56013Sjoerg   { A##B##_RELAX, A##B##_ACQ, A##B##_REL, A##B##_ACQ_REL }
515*82d56013Sjoerg #define LCALL5(A)                                                              \
516*82d56013Sjoerg   LCALLS(A, 1), LCALLS(A, 2), LCALLS(A, 4), LCALLS(A, 8), LCALLS(A, 16)
517*82d56013Sjoerg   switch (Opc) {
518*82d56013Sjoerg   case ISD::ATOMIC_CMP_SWAP: {
519*82d56013Sjoerg     const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_CAS)};
520*82d56013Sjoerg     return LC[ModeN][ModelN];
521*82d56013Sjoerg   }
522*82d56013Sjoerg   case ISD::ATOMIC_SWAP: {
523*82d56013Sjoerg     const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_SWP)};
524*82d56013Sjoerg     return LC[ModeN][ModelN];
525*82d56013Sjoerg   }
526*82d56013Sjoerg   case ISD::ATOMIC_LOAD_ADD: {
527*82d56013Sjoerg     const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_LDADD)};
528*82d56013Sjoerg     return LC[ModeN][ModelN];
529*82d56013Sjoerg   }
530*82d56013Sjoerg   case ISD::ATOMIC_LOAD_OR: {
531*82d56013Sjoerg     const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_LDSET)};
532*82d56013Sjoerg     return LC[ModeN][ModelN];
533*82d56013Sjoerg   }
534*82d56013Sjoerg   case ISD::ATOMIC_LOAD_CLR: {
535*82d56013Sjoerg     const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_LDCLR)};
536*82d56013Sjoerg     return LC[ModeN][ModelN];
537*82d56013Sjoerg   }
538*82d56013Sjoerg   case ISD::ATOMIC_LOAD_XOR: {
539*82d56013Sjoerg     const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_LDEOR)};
540*82d56013Sjoerg     return LC[ModeN][ModelN];
541*82d56013Sjoerg   }
542*82d56013Sjoerg   default:
543*82d56013Sjoerg     return UNKNOWN_LIBCALL;
544*82d56013Sjoerg   }
545*82d56013Sjoerg #undef LCALLS
546*82d56013Sjoerg #undef LCALL5
547*82d56013Sjoerg }
548*82d56013Sjoerg 
getSYNC(unsigned Opc,MVT VT)5497330f729Sjoerg RTLIB::Libcall RTLIB::getSYNC(unsigned Opc, MVT VT) {
5507330f729Sjoerg #define OP_TO_LIBCALL(Name, Enum)                                              \
5517330f729Sjoerg   case Name:                                                                   \
5527330f729Sjoerg     switch (VT.SimpleTy) {                                                     \
5537330f729Sjoerg     default:                                                                   \
5547330f729Sjoerg       return UNKNOWN_LIBCALL;                                                  \
5557330f729Sjoerg     case MVT::i8:                                                              \
5567330f729Sjoerg       return Enum##_1;                                                         \
5577330f729Sjoerg     case MVT::i16:                                                             \
5587330f729Sjoerg       return Enum##_2;                                                         \
5597330f729Sjoerg     case MVT::i32:                                                             \
5607330f729Sjoerg       return Enum##_4;                                                         \
5617330f729Sjoerg     case MVT::i64:                                                             \
5627330f729Sjoerg       return Enum##_8;                                                         \
5637330f729Sjoerg     case MVT::i128:                                                            \
5647330f729Sjoerg       return Enum##_16;                                                        \
5657330f729Sjoerg     }
5667330f729Sjoerg 
5677330f729Sjoerg   switch (Opc) {
5687330f729Sjoerg     OP_TO_LIBCALL(ISD::ATOMIC_SWAP, SYNC_LOCK_TEST_AND_SET)
5697330f729Sjoerg     OP_TO_LIBCALL(ISD::ATOMIC_CMP_SWAP, SYNC_VAL_COMPARE_AND_SWAP)
5707330f729Sjoerg     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_ADD, SYNC_FETCH_AND_ADD)
5717330f729Sjoerg     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_SUB, SYNC_FETCH_AND_SUB)
5727330f729Sjoerg     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_AND, SYNC_FETCH_AND_AND)
5737330f729Sjoerg     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_OR, SYNC_FETCH_AND_OR)
5747330f729Sjoerg     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_XOR, SYNC_FETCH_AND_XOR)
5757330f729Sjoerg     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_NAND, SYNC_FETCH_AND_NAND)
5767330f729Sjoerg     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_MAX, SYNC_FETCH_AND_MAX)
5777330f729Sjoerg     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_UMAX, SYNC_FETCH_AND_UMAX)
5787330f729Sjoerg     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_MIN, SYNC_FETCH_AND_MIN)
5797330f729Sjoerg     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_UMIN, SYNC_FETCH_AND_UMIN)
5807330f729Sjoerg   }
5817330f729Sjoerg 
5827330f729Sjoerg #undef OP_TO_LIBCALL
5837330f729Sjoerg 
5847330f729Sjoerg   return UNKNOWN_LIBCALL;
5857330f729Sjoerg }
5867330f729Sjoerg 
getMEMCPY_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)5877330f729Sjoerg RTLIB::Libcall RTLIB::getMEMCPY_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize) {
5887330f729Sjoerg   switch (ElementSize) {
5897330f729Sjoerg   case 1:
5907330f729Sjoerg     return MEMCPY_ELEMENT_UNORDERED_ATOMIC_1;
5917330f729Sjoerg   case 2:
5927330f729Sjoerg     return MEMCPY_ELEMENT_UNORDERED_ATOMIC_2;
5937330f729Sjoerg   case 4:
5947330f729Sjoerg     return MEMCPY_ELEMENT_UNORDERED_ATOMIC_4;
5957330f729Sjoerg   case 8:
5967330f729Sjoerg     return MEMCPY_ELEMENT_UNORDERED_ATOMIC_8;
5977330f729Sjoerg   case 16:
5987330f729Sjoerg     return MEMCPY_ELEMENT_UNORDERED_ATOMIC_16;
5997330f729Sjoerg   default:
6007330f729Sjoerg     return UNKNOWN_LIBCALL;
6017330f729Sjoerg   }
6027330f729Sjoerg }
6037330f729Sjoerg 
getMEMMOVE_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)6047330f729Sjoerg RTLIB::Libcall RTLIB::getMEMMOVE_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize) {
6057330f729Sjoerg   switch (ElementSize) {
6067330f729Sjoerg   case 1:
6077330f729Sjoerg     return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_1;
6087330f729Sjoerg   case 2:
6097330f729Sjoerg     return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_2;
6107330f729Sjoerg   case 4:
6117330f729Sjoerg     return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_4;
6127330f729Sjoerg   case 8:
6137330f729Sjoerg     return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_8;
6147330f729Sjoerg   case 16:
6157330f729Sjoerg     return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_16;
6167330f729Sjoerg   default:
6177330f729Sjoerg     return UNKNOWN_LIBCALL;
6187330f729Sjoerg   }
6197330f729Sjoerg }
6207330f729Sjoerg 
getMEMSET_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)6217330f729Sjoerg RTLIB::Libcall RTLIB::getMEMSET_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize) {
6227330f729Sjoerg   switch (ElementSize) {
6237330f729Sjoerg   case 1:
6247330f729Sjoerg     return MEMSET_ELEMENT_UNORDERED_ATOMIC_1;
6257330f729Sjoerg   case 2:
6267330f729Sjoerg     return MEMSET_ELEMENT_UNORDERED_ATOMIC_2;
6277330f729Sjoerg   case 4:
6287330f729Sjoerg     return MEMSET_ELEMENT_UNORDERED_ATOMIC_4;
6297330f729Sjoerg   case 8:
6307330f729Sjoerg     return MEMSET_ELEMENT_UNORDERED_ATOMIC_8;
6317330f729Sjoerg   case 16:
6327330f729Sjoerg     return MEMSET_ELEMENT_UNORDERED_ATOMIC_16;
6337330f729Sjoerg   default:
6347330f729Sjoerg     return UNKNOWN_LIBCALL;
6357330f729Sjoerg   }
6367330f729Sjoerg }
6377330f729Sjoerg 
6387330f729Sjoerg /// InitCmpLibcallCCs - Set default comparison libcall CC.
InitCmpLibcallCCs(ISD::CondCode * CCs)6397330f729Sjoerg static void InitCmpLibcallCCs(ISD::CondCode *CCs) {
6407330f729Sjoerg   memset(CCs, ISD::SETCC_INVALID, sizeof(ISD::CondCode)*RTLIB::UNKNOWN_LIBCALL);
6417330f729Sjoerg   CCs[RTLIB::OEQ_F32] = ISD::SETEQ;
6427330f729Sjoerg   CCs[RTLIB::OEQ_F64] = ISD::SETEQ;
6437330f729Sjoerg   CCs[RTLIB::OEQ_F128] = ISD::SETEQ;
6447330f729Sjoerg   CCs[RTLIB::OEQ_PPCF128] = ISD::SETEQ;
6457330f729Sjoerg   CCs[RTLIB::UNE_F32] = ISD::SETNE;
6467330f729Sjoerg   CCs[RTLIB::UNE_F64] = ISD::SETNE;
6477330f729Sjoerg   CCs[RTLIB::UNE_F128] = ISD::SETNE;
6487330f729Sjoerg   CCs[RTLIB::UNE_PPCF128] = ISD::SETNE;
6497330f729Sjoerg   CCs[RTLIB::OGE_F32] = ISD::SETGE;
6507330f729Sjoerg   CCs[RTLIB::OGE_F64] = ISD::SETGE;
6517330f729Sjoerg   CCs[RTLIB::OGE_F128] = ISD::SETGE;
6527330f729Sjoerg   CCs[RTLIB::OGE_PPCF128] = ISD::SETGE;
6537330f729Sjoerg   CCs[RTLIB::OLT_F32] = ISD::SETLT;
6547330f729Sjoerg   CCs[RTLIB::OLT_F64] = ISD::SETLT;
6557330f729Sjoerg   CCs[RTLIB::OLT_F128] = ISD::SETLT;
6567330f729Sjoerg   CCs[RTLIB::OLT_PPCF128] = ISD::SETLT;
6577330f729Sjoerg   CCs[RTLIB::OLE_F32] = ISD::SETLE;
6587330f729Sjoerg   CCs[RTLIB::OLE_F64] = ISD::SETLE;
6597330f729Sjoerg   CCs[RTLIB::OLE_F128] = ISD::SETLE;
6607330f729Sjoerg   CCs[RTLIB::OLE_PPCF128] = ISD::SETLE;
6617330f729Sjoerg   CCs[RTLIB::OGT_F32] = ISD::SETGT;
6627330f729Sjoerg   CCs[RTLIB::OGT_F64] = ISD::SETGT;
6637330f729Sjoerg   CCs[RTLIB::OGT_F128] = ISD::SETGT;
6647330f729Sjoerg   CCs[RTLIB::OGT_PPCF128] = ISD::SETGT;
6657330f729Sjoerg   CCs[RTLIB::UO_F32] = ISD::SETNE;
6667330f729Sjoerg   CCs[RTLIB::UO_F64] = ISD::SETNE;
6677330f729Sjoerg   CCs[RTLIB::UO_F128] = ISD::SETNE;
6687330f729Sjoerg   CCs[RTLIB::UO_PPCF128] = ISD::SETNE;
6697330f729Sjoerg }
6707330f729Sjoerg 
6717330f729Sjoerg /// NOTE: The TargetMachine owns TLOF.
TargetLoweringBase(const TargetMachine & tm)6727330f729Sjoerg TargetLoweringBase::TargetLoweringBase(const TargetMachine &tm) : TM(tm) {
6737330f729Sjoerg   initActions();
6747330f729Sjoerg 
6757330f729Sjoerg   // Perform these initializations only once.
6767330f729Sjoerg   MaxStoresPerMemset = MaxStoresPerMemcpy = MaxStoresPerMemmove =
6777330f729Sjoerg       MaxLoadsPerMemcmp = 8;
6787330f729Sjoerg   MaxGluedStoresPerMemcpy = 0;
6797330f729Sjoerg   MaxStoresPerMemsetOptSize = MaxStoresPerMemcpyOptSize =
6807330f729Sjoerg       MaxStoresPerMemmoveOptSize = MaxLoadsPerMemcmpOptSize = 4;
6817330f729Sjoerg   HasMultipleConditionRegisters = false;
6827330f729Sjoerg   HasExtractBitsInsn = false;
6837330f729Sjoerg   JumpIsExpensive = JumpIsExpensiveOverride;
6847330f729Sjoerg   PredictableSelectIsExpensive = false;
6857330f729Sjoerg   EnableExtLdPromotion = false;
6867330f729Sjoerg   StackPointerRegisterToSaveRestore = 0;
6877330f729Sjoerg   BooleanContents = UndefinedBooleanContent;
6887330f729Sjoerg   BooleanFloatContents = UndefinedBooleanContent;
6897330f729Sjoerg   BooleanVectorContents = UndefinedBooleanContent;
6907330f729Sjoerg   SchedPreferenceInfo = Sched::ILP;
6917330f729Sjoerg   GatherAllAliasesMaxDepth = 18;
692*82d56013Sjoerg   IsStrictFPEnabled = DisableStrictNodeMutation;
6937330f729Sjoerg   // TODO: the default will be switched to 0 in the next commit, along
6947330f729Sjoerg   // with the Target-specific changes necessary.
6957330f729Sjoerg   MaxAtomicSizeInBitsSupported = 1024;
6967330f729Sjoerg 
6977330f729Sjoerg   MinCmpXchgSizeInBits = 0;
6987330f729Sjoerg   SupportsUnalignedAtomics = false;
6997330f729Sjoerg 
7007330f729Sjoerg   std::fill(std::begin(LibcallRoutineNames), std::end(LibcallRoutineNames), nullptr);
7017330f729Sjoerg 
7027330f729Sjoerg   InitLibcalls(TM.getTargetTriple());
7037330f729Sjoerg   InitCmpLibcallCCs(CmpLibcallCCs);
7047330f729Sjoerg }
7057330f729Sjoerg 
initActions()7067330f729Sjoerg void TargetLoweringBase::initActions() {
7077330f729Sjoerg   // All operations default to being supported.
7087330f729Sjoerg   memset(OpActions, 0, sizeof(OpActions));
7097330f729Sjoerg   memset(LoadExtActions, 0, sizeof(LoadExtActions));
7107330f729Sjoerg   memset(TruncStoreActions, 0, sizeof(TruncStoreActions));
7117330f729Sjoerg   memset(IndexedModeActions, 0, sizeof(IndexedModeActions));
7127330f729Sjoerg   memset(CondCodeActions, 0, sizeof(CondCodeActions));
7137330f729Sjoerg   std::fill(std::begin(RegClassForVT), std::end(RegClassForVT), nullptr);
7147330f729Sjoerg   std::fill(std::begin(TargetDAGCombineArray),
7157330f729Sjoerg             std::end(TargetDAGCombineArray), 0);
7167330f729Sjoerg 
7177330f729Sjoerg   for (MVT VT : MVT::fp_valuetypes()) {
718*82d56013Sjoerg     MVT IntVT = MVT::getIntegerVT(VT.getFixedSizeInBits());
7197330f729Sjoerg     if (IntVT.isValid()) {
7207330f729Sjoerg       setOperationAction(ISD::ATOMIC_SWAP, VT, Promote);
7217330f729Sjoerg       AddPromotedToType(ISD::ATOMIC_SWAP, VT, IntVT);
7227330f729Sjoerg     }
7237330f729Sjoerg   }
7247330f729Sjoerg 
7257330f729Sjoerg   // Set default actions for various operations.
7267330f729Sjoerg   for (MVT VT : MVT::all_valuetypes()) {
7277330f729Sjoerg     // Default all indexed load / store to expand.
7287330f729Sjoerg     for (unsigned IM = (unsigned)ISD::PRE_INC;
7297330f729Sjoerg          IM != (unsigned)ISD::LAST_INDEXED_MODE; ++IM) {
7307330f729Sjoerg       setIndexedLoadAction(IM, VT, Expand);
7317330f729Sjoerg       setIndexedStoreAction(IM, VT, Expand);
732*82d56013Sjoerg       setIndexedMaskedLoadAction(IM, VT, Expand);
733*82d56013Sjoerg       setIndexedMaskedStoreAction(IM, VT, Expand);
7347330f729Sjoerg     }
7357330f729Sjoerg 
7367330f729Sjoerg     // Most backends expect to see the node which just returns the value loaded.
7377330f729Sjoerg     setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, VT, Expand);
7387330f729Sjoerg 
7397330f729Sjoerg     // These operations default to expand.
7407330f729Sjoerg     setOperationAction(ISD::FGETSIGN, VT, Expand);
7417330f729Sjoerg     setOperationAction(ISD::CONCAT_VECTORS, VT, Expand);
7427330f729Sjoerg     setOperationAction(ISD::FMINNUM, VT, Expand);
7437330f729Sjoerg     setOperationAction(ISD::FMAXNUM, VT, Expand);
7447330f729Sjoerg     setOperationAction(ISD::FMINNUM_IEEE, VT, Expand);
7457330f729Sjoerg     setOperationAction(ISD::FMAXNUM_IEEE, VT, Expand);
7467330f729Sjoerg     setOperationAction(ISD::FMINIMUM, VT, Expand);
7477330f729Sjoerg     setOperationAction(ISD::FMAXIMUM, VT, Expand);
7487330f729Sjoerg     setOperationAction(ISD::FMAD, VT, Expand);
7497330f729Sjoerg     setOperationAction(ISD::SMIN, VT, Expand);
7507330f729Sjoerg     setOperationAction(ISD::SMAX, VT, Expand);
7517330f729Sjoerg     setOperationAction(ISD::UMIN, VT, Expand);
7527330f729Sjoerg     setOperationAction(ISD::UMAX, VT, Expand);
7537330f729Sjoerg     setOperationAction(ISD::ABS, VT, Expand);
7547330f729Sjoerg     setOperationAction(ISD::FSHL, VT, Expand);
7557330f729Sjoerg     setOperationAction(ISD::FSHR, VT, Expand);
7567330f729Sjoerg     setOperationAction(ISD::SADDSAT, VT, Expand);
7577330f729Sjoerg     setOperationAction(ISD::UADDSAT, VT, Expand);
7587330f729Sjoerg     setOperationAction(ISD::SSUBSAT, VT, Expand);
7597330f729Sjoerg     setOperationAction(ISD::USUBSAT, VT, Expand);
760*82d56013Sjoerg     setOperationAction(ISD::SSHLSAT, VT, Expand);
761*82d56013Sjoerg     setOperationAction(ISD::USHLSAT, VT, Expand);
7627330f729Sjoerg     setOperationAction(ISD::SMULFIX, VT, Expand);
7637330f729Sjoerg     setOperationAction(ISD::SMULFIXSAT, VT, Expand);
7647330f729Sjoerg     setOperationAction(ISD::UMULFIX, VT, Expand);
7657330f729Sjoerg     setOperationAction(ISD::UMULFIXSAT, VT, Expand);
766*82d56013Sjoerg     setOperationAction(ISD::SDIVFIX, VT, Expand);
767*82d56013Sjoerg     setOperationAction(ISD::SDIVFIXSAT, VT, Expand);
768*82d56013Sjoerg     setOperationAction(ISD::UDIVFIX, VT, Expand);
769*82d56013Sjoerg     setOperationAction(ISD::UDIVFIXSAT, VT, Expand);
770*82d56013Sjoerg     setOperationAction(ISD::FP_TO_SINT_SAT, VT, Expand);
771*82d56013Sjoerg     setOperationAction(ISD::FP_TO_UINT_SAT, VT, Expand);
7727330f729Sjoerg 
7737330f729Sjoerg     // Overflow operations default to expand
7747330f729Sjoerg     setOperationAction(ISD::SADDO, VT, Expand);
7757330f729Sjoerg     setOperationAction(ISD::SSUBO, VT, Expand);
7767330f729Sjoerg     setOperationAction(ISD::UADDO, VT, Expand);
7777330f729Sjoerg     setOperationAction(ISD::USUBO, VT, Expand);
7787330f729Sjoerg     setOperationAction(ISD::SMULO, VT, Expand);
7797330f729Sjoerg     setOperationAction(ISD::UMULO, VT, Expand);
7807330f729Sjoerg 
7817330f729Sjoerg     // ADDCARRY operations default to expand
7827330f729Sjoerg     setOperationAction(ISD::ADDCARRY, VT, Expand);
7837330f729Sjoerg     setOperationAction(ISD::SUBCARRY, VT, Expand);
7847330f729Sjoerg     setOperationAction(ISD::SETCCCARRY, VT, Expand);
785*82d56013Sjoerg     setOperationAction(ISD::SADDO_CARRY, VT, Expand);
786*82d56013Sjoerg     setOperationAction(ISD::SSUBO_CARRY, VT, Expand);
7877330f729Sjoerg 
7887330f729Sjoerg     // ADDC/ADDE/SUBC/SUBE default to expand.
7897330f729Sjoerg     setOperationAction(ISD::ADDC, VT, Expand);
7907330f729Sjoerg     setOperationAction(ISD::ADDE, VT, Expand);
7917330f729Sjoerg     setOperationAction(ISD::SUBC, VT, Expand);
7927330f729Sjoerg     setOperationAction(ISD::SUBE, VT, Expand);
7937330f729Sjoerg 
7947330f729Sjoerg     // These default to Expand so they will be expanded to CTLZ/CTTZ by default.
7957330f729Sjoerg     setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
7967330f729Sjoerg     setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
7977330f729Sjoerg 
7987330f729Sjoerg     setOperationAction(ISD::BITREVERSE, VT, Expand);
799*82d56013Sjoerg     setOperationAction(ISD::PARITY, VT, Expand);
8007330f729Sjoerg 
8017330f729Sjoerg     // These library functions default to expand.
8027330f729Sjoerg     setOperationAction(ISD::FROUND, VT, Expand);
803*82d56013Sjoerg     setOperationAction(ISD::FROUNDEVEN, VT, Expand);
8047330f729Sjoerg     setOperationAction(ISD::FPOWI, VT, Expand);
8057330f729Sjoerg 
8067330f729Sjoerg     // These operations default to expand for vector types.
8077330f729Sjoerg     if (VT.isVector()) {
8087330f729Sjoerg       setOperationAction(ISD::FCOPYSIGN, VT, Expand);
809*82d56013Sjoerg       setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
8107330f729Sjoerg       setOperationAction(ISD::ANY_EXTEND_VECTOR_INREG, VT, Expand);
8117330f729Sjoerg       setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, VT, Expand);
8127330f729Sjoerg       setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, VT, Expand);
8137330f729Sjoerg       setOperationAction(ISD::SPLAT_VECTOR, VT, Expand);
8147330f729Sjoerg     }
8157330f729Sjoerg 
8167330f729Sjoerg     // Constrained floating-point operations default to expand.
817*82d56013Sjoerg #define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN)               \
818*82d56013Sjoerg     setOperationAction(ISD::STRICT_##DAGN, VT, Expand);
819*82d56013Sjoerg #include "llvm/IR/ConstrainedOps.def"
8207330f729Sjoerg 
8217330f729Sjoerg     // For most targets @llvm.get.dynamic.area.offset just returns 0.
8227330f729Sjoerg     setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, VT, Expand);
8237330f729Sjoerg 
8247330f729Sjoerg     // Vector reduction default to expand.
8257330f729Sjoerg     setOperationAction(ISD::VECREDUCE_FADD, VT, Expand);
8267330f729Sjoerg     setOperationAction(ISD::VECREDUCE_FMUL, VT, Expand);
8277330f729Sjoerg     setOperationAction(ISD::VECREDUCE_ADD, VT, Expand);
8287330f729Sjoerg     setOperationAction(ISD::VECREDUCE_MUL, VT, Expand);
8297330f729Sjoerg     setOperationAction(ISD::VECREDUCE_AND, VT, Expand);
8307330f729Sjoerg     setOperationAction(ISD::VECREDUCE_OR, VT, Expand);
8317330f729Sjoerg     setOperationAction(ISD::VECREDUCE_XOR, VT, Expand);
8327330f729Sjoerg     setOperationAction(ISD::VECREDUCE_SMAX, VT, Expand);
8337330f729Sjoerg     setOperationAction(ISD::VECREDUCE_SMIN, VT, Expand);
8347330f729Sjoerg     setOperationAction(ISD::VECREDUCE_UMAX, VT, Expand);
8357330f729Sjoerg     setOperationAction(ISD::VECREDUCE_UMIN, VT, Expand);
8367330f729Sjoerg     setOperationAction(ISD::VECREDUCE_FMAX, VT, Expand);
8377330f729Sjoerg     setOperationAction(ISD::VECREDUCE_FMIN, VT, Expand);
838*82d56013Sjoerg     setOperationAction(ISD::VECREDUCE_SEQ_FADD, VT, Expand);
839*82d56013Sjoerg     setOperationAction(ISD::VECREDUCE_SEQ_FMUL, VT, Expand);
840*82d56013Sjoerg 
841*82d56013Sjoerg     // Named vector shuffles default to expand.
842*82d56013Sjoerg     setOperationAction(ISD::VECTOR_SPLICE, VT, Expand);
8437330f729Sjoerg   }
8447330f729Sjoerg 
8457330f729Sjoerg   // Most targets ignore the @llvm.prefetch intrinsic.
8467330f729Sjoerg   setOperationAction(ISD::PREFETCH, MVT::Other, Expand);
8477330f729Sjoerg 
8487330f729Sjoerg   // Most targets also ignore the @llvm.readcyclecounter intrinsic.
8497330f729Sjoerg   setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Expand);
8507330f729Sjoerg 
8517330f729Sjoerg   // ConstantFP nodes default to expand.  Targets can either change this to
8527330f729Sjoerg   // Legal, in which case all fp constants are legal, or use isFPImmLegal()
8537330f729Sjoerg   // to optimize expansions for certain constants.
8547330f729Sjoerg   setOperationAction(ISD::ConstantFP, MVT::f16, Expand);
8557330f729Sjoerg   setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
8567330f729Sjoerg   setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
8577330f729Sjoerg   setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
8587330f729Sjoerg   setOperationAction(ISD::ConstantFP, MVT::f128, Expand);
8597330f729Sjoerg 
8607330f729Sjoerg   // These library functions default to expand.
8617330f729Sjoerg   for (MVT VT : {MVT::f32, MVT::f64, MVT::f128}) {
8627330f729Sjoerg     setOperationAction(ISD::FCBRT,      VT, Expand);
8637330f729Sjoerg     setOperationAction(ISD::FLOG ,      VT, Expand);
8647330f729Sjoerg     setOperationAction(ISD::FLOG2,      VT, Expand);
8657330f729Sjoerg     setOperationAction(ISD::FLOG10,     VT, Expand);
8667330f729Sjoerg     setOperationAction(ISD::FEXP ,      VT, Expand);
8677330f729Sjoerg     setOperationAction(ISD::FEXP2,      VT, Expand);
8687330f729Sjoerg     setOperationAction(ISD::FFLOOR,     VT, Expand);
8697330f729Sjoerg     setOperationAction(ISD::FNEARBYINT, VT, Expand);
8707330f729Sjoerg     setOperationAction(ISD::FCEIL,      VT, Expand);
8717330f729Sjoerg     setOperationAction(ISD::FRINT,      VT, Expand);
8727330f729Sjoerg     setOperationAction(ISD::FTRUNC,     VT, Expand);
8737330f729Sjoerg     setOperationAction(ISD::FROUND,     VT, Expand);
874*82d56013Sjoerg     setOperationAction(ISD::FROUNDEVEN, VT, Expand);
8757330f729Sjoerg     setOperationAction(ISD::LROUND,     VT, Expand);
8767330f729Sjoerg     setOperationAction(ISD::LLROUND,    VT, Expand);
8777330f729Sjoerg     setOperationAction(ISD::LRINT,      VT, Expand);
8787330f729Sjoerg     setOperationAction(ISD::LLRINT,     VT, Expand);
8797330f729Sjoerg   }
8807330f729Sjoerg 
8817330f729Sjoerg   // Default ISD::TRAP to expand (which turns it into abort).
8827330f729Sjoerg   setOperationAction(ISD::TRAP, MVT::Other, Expand);
8837330f729Sjoerg 
8847330f729Sjoerg   // On most systems, DEBUGTRAP and TRAP have no difference. The "Expand"
8857330f729Sjoerg   // here is to inform DAG Legalizer to replace DEBUGTRAP with TRAP.
8867330f729Sjoerg   setOperationAction(ISD::DEBUGTRAP, MVT::Other, Expand);
887*82d56013Sjoerg 
888*82d56013Sjoerg   setOperationAction(ISD::UBSANTRAP, MVT::Other, Expand);
8897330f729Sjoerg }
8907330f729Sjoerg 
getScalarShiftAmountTy(const DataLayout & DL,EVT) const8917330f729Sjoerg MVT TargetLoweringBase::getScalarShiftAmountTy(const DataLayout &DL,
8927330f729Sjoerg                                                EVT) const {
8937330f729Sjoerg   return MVT::getIntegerVT(DL.getPointerSizeInBits(0));
8947330f729Sjoerg }
8957330f729Sjoerg 
getShiftAmountTy(EVT LHSTy,const DataLayout & DL,bool LegalTypes) const8967330f729Sjoerg EVT TargetLoweringBase::getShiftAmountTy(EVT LHSTy, const DataLayout &DL,
8977330f729Sjoerg                                          bool LegalTypes) const {
8987330f729Sjoerg   assert(LHSTy.isInteger() && "Shift amount is not an integer type!");
8997330f729Sjoerg   if (LHSTy.isVector())
9007330f729Sjoerg     return LHSTy;
9017330f729Sjoerg   return LegalTypes ? getScalarShiftAmountTy(DL, LHSTy)
9027330f729Sjoerg                     : getPointerTy(DL);
9037330f729Sjoerg }
9047330f729Sjoerg 
canOpTrap(unsigned Op,EVT VT) const9057330f729Sjoerg bool TargetLoweringBase::canOpTrap(unsigned Op, EVT VT) const {
9067330f729Sjoerg   assert(isTypeLegal(VT));
9077330f729Sjoerg   switch (Op) {
9087330f729Sjoerg   default:
9097330f729Sjoerg     return false;
9107330f729Sjoerg   case ISD::SDIV:
9117330f729Sjoerg   case ISD::UDIV:
9127330f729Sjoerg   case ISD::SREM:
9137330f729Sjoerg   case ISD::UREM:
9147330f729Sjoerg     return true;
9157330f729Sjoerg   }
9167330f729Sjoerg }
9177330f729Sjoerg 
isFreeAddrSpaceCast(unsigned SrcAS,unsigned DestAS) const918*82d56013Sjoerg bool TargetLoweringBase::isFreeAddrSpaceCast(unsigned SrcAS,
919*82d56013Sjoerg                                              unsigned DestAS) const {
920*82d56013Sjoerg   return TM.isNoopAddrSpaceCast(SrcAS, DestAS);
921*82d56013Sjoerg }
922*82d56013Sjoerg 
setJumpIsExpensive(bool isExpensive)9237330f729Sjoerg void TargetLoweringBase::setJumpIsExpensive(bool isExpensive) {
9247330f729Sjoerg   // If the command-line option was specified, ignore this request.
9257330f729Sjoerg   if (!JumpIsExpensiveOverride.getNumOccurrences())
9267330f729Sjoerg     JumpIsExpensive = isExpensive;
9277330f729Sjoerg }
9287330f729Sjoerg 
9297330f729Sjoerg TargetLoweringBase::LegalizeKind
getTypeConversion(LLVMContext & Context,EVT VT) const9307330f729Sjoerg TargetLoweringBase::getTypeConversion(LLVMContext &Context, EVT VT) const {
9317330f729Sjoerg   // If this is a simple type, use the ComputeRegisterProp mechanism.
9327330f729Sjoerg   if (VT.isSimple()) {
9337330f729Sjoerg     MVT SVT = VT.getSimpleVT();
9347330f729Sjoerg     assert((unsigned)SVT.SimpleTy < array_lengthof(TransformToType));
9357330f729Sjoerg     MVT NVT = TransformToType[SVT.SimpleTy];
9367330f729Sjoerg     LegalizeTypeAction LA = ValueTypeActions.getTypeAction(SVT);
9377330f729Sjoerg 
9387330f729Sjoerg     assert((LA == TypeLegal || LA == TypeSoftenFloat ||
939*82d56013Sjoerg             LA == TypeSoftPromoteHalf ||
9407330f729Sjoerg             (NVT.isVector() ||
9417330f729Sjoerg              ValueTypeActions.getTypeAction(NVT) != TypePromoteInteger)) &&
9427330f729Sjoerg            "Promote may not follow Expand or Promote");
9437330f729Sjoerg 
9447330f729Sjoerg     if (LA == TypeSplitVector)
945*82d56013Sjoerg       return LegalizeKind(LA, EVT(SVT).getHalfNumVectorElementsVT(Context));
9467330f729Sjoerg     if (LA == TypeScalarizeVector)
9477330f729Sjoerg       return LegalizeKind(LA, SVT.getVectorElementType());
9487330f729Sjoerg     return LegalizeKind(LA, NVT);
9497330f729Sjoerg   }
9507330f729Sjoerg 
9517330f729Sjoerg   // Handle Extended Scalar Types.
9527330f729Sjoerg   if (!VT.isVector()) {
9537330f729Sjoerg     assert(VT.isInteger() && "Float types must be simple");
9547330f729Sjoerg     unsigned BitSize = VT.getSizeInBits();
9557330f729Sjoerg     // First promote to a power-of-two size, then expand if necessary.
9567330f729Sjoerg     if (BitSize < 8 || !isPowerOf2_32(BitSize)) {
9577330f729Sjoerg       EVT NVT = VT.getRoundIntegerType(Context);
9587330f729Sjoerg       assert(NVT != VT && "Unable to round integer VT");
9597330f729Sjoerg       LegalizeKind NextStep = getTypeConversion(Context, NVT);
9607330f729Sjoerg       // Avoid multi-step promotion.
9617330f729Sjoerg       if (NextStep.first == TypePromoteInteger)
9627330f729Sjoerg         return NextStep;
9637330f729Sjoerg       // Return rounded integer type.
9647330f729Sjoerg       return LegalizeKind(TypePromoteInteger, NVT);
9657330f729Sjoerg     }
9667330f729Sjoerg 
9677330f729Sjoerg     return LegalizeKind(TypeExpandInteger,
9687330f729Sjoerg                         EVT::getIntegerVT(Context, VT.getSizeInBits() / 2));
9697330f729Sjoerg   }
9707330f729Sjoerg 
9717330f729Sjoerg   // Handle vector types.
972*82d56013Sjoerg   ElementCount NumElts = VT.getVectorElementCount();
9737330f729Sjoerg   EVT EltVT = VT.getVectorElementType();
9747330f729Sjoerg 
9757330f729Sjoerg   // Vectors with only one element are always scalarized.
976*82d56013Sjoerg   if (NumElts.isScalar())
9777330f729Sjoerg     return LegalizeKind(TypeScalarizeVector, EltVT);
9787330f729Sjoerg 
9797330f729Sjoerg   // Try to widen vector elements until the element type is a power of two and
9807330f729Sjoerg   // promote it to a legal type later on, for example:
9817330f729Sjoerg   // <3 x i8> -> <4 x i8> -> <4 x i32>
9827330f729Sjoerg   if (EltVT.isInteger()) {
9837330f729Sjoerg     // Vectors with a number of elements that is not a power of two are always
9847330f729Sjoerg     // widened, for example <3 x i8> -> <4 x i8>.
9857330f729Sjoerg     if (!VT.isPow2VectorType()) {
986*82d56013Sjoerg       NumElts = NumElts.coefficientNextPowerOf2();
9877330f729Sjoerg       EVT NVT = EVT::getVectorVT(Context, EltVT, NumElts);
9887330f729Sjoerg       return LegalizeKind(TypeWidenVector, NVT);
9897330f729Sjoerg     }
9907330f729Sjoerg 
9917330f729Sjoerg     // Examine the element type.
9927330f729Sjoerg     LegalizeKind LK = getTypeConversion(Context, EltVT);
9937330f729Sjoerg 
9947330f729Sjoerg     // If type is to be expanded, split the vector.
9957330f729Sjoerg     //  <4 x i140> -> <2 x i140>
996*82d56013Sjoerg     if (LK.first == TypeExpandInteger) {
997*82d56013Sjoerg       if (VT.getVectorElementCount() == ElementCount::getScalable(1))
998*82d56013Sjoerg         report_fatal_error("Cannot legalize this scalable vector");
9997330f729Sjoerg       return LegalizeKind(TypeSplitVector,
1000*82d56013Sjoerg                           VT.getHalfNumVectorElementsVT(Context));
1001*82d56013Sjoerg     }
10027330f729Sjoerg 
10037330f729Sjoerg     // Promote the integer element types until a legal vector type is found
10047330f729Sjoerg     // or until the element integer type is too big. If a legal type was not
10057330f729Sjoerg     // found, fallback to the usual mechanism of widening/splitting the
10067330f729Sjoerg     // vector.
10077330f729Sjoerg     EVT OldEltVT = EltVT;
10087330f729Sjoerg     while (true) {
10097330f729Sjoerg       // Increase the bitwidth of the element to the next pow-of-two
10107330f729Sjoerg       // (which is greater than 8 bits).
10117330f729Sjoerg       EltVT = EVT::getIntegerVT(Context, 1 + EltVT.getSizeInBits())
10127330f729Sjoerg                   .getRoundIntegerType(Context);
10137330f729Sjoerg 
10147330f729Sjoerg       // Stop trying when getting a non-simple element type.
10157330f729Sjoerg       // Note that vector elements may be greater than legal vector element
10167330f729Sjoerg       // types. Example: X86 XMM registers hold 64bit element on 32bit
10177330f729Sjoerg       // systems.
10187330f729Sjoerg       if (!EltVT.isSimple())
10197330f729Sjoerg         break;
10207330f729Sjoerg 
10217330f729Sjoerg       // Build a new vector type and check if it is legal.
10227330f729Sjoerg       MVT NVT = MVT::getVectorVT(EltVT.getSimpleVT(), NumElts);
10237330f729Sjoerg       // Found a legal promoted vector type.
10247330f729Sjoerg       if (NVT != MVT() && ValueTypeActions.getTypeAction(NVT) == TypeLegal)
10257330f729Sjoerg         return LegalizeKind(TypePromoteInteger,
10267330f729Sjoerg                             EVT::getVectorVT(Context, EltVT, NumElts));
10277330f729Sjoerg     }
10287330f729Sjoerg 
10297330f729Sjoerg     // Reset the type to the unexpanded type if we did not find a legal vector
10307330f729Sjoerg     // type with a promoted vector element type.
10317330f729Sjoerg     EltVT = OldEltVT;
10327330f729Sjoerg   }
10337330f729Sjoerg 
10347330f729Sjoerg   // Try to widen the vector until a legal type is found.
10357330f729Sjoerg   // If there is no wider legal type, split the vector.
10367330f729Sjoerg   while (true) {
10377330f729Sjoerg     // Round up to the next power of 2.
1038*82d56013Sjoerg     NumElts = NumElts.coefficientNextPowerOf2();
10397330f729Sjoerg 
10407330f729Sjoerg     // If there is no simple vector type with this many elements then there
10417330f729Sjoerg     // cannot be a larger legal vector type.  Note that this assumes that
10427330f729Sjoerg     // there are no skipped intermediate vector types in the simple types.
10437330f729Sjoerg     if (!EltVT.isSimple())
10447330f729Sjoerg       break;
10457330f729Sjoerg     MVT LargerVector = MVT::getVectorVT(EltVT.getSimpleVT(), NumElts);
10467330f729Sjoerg     if (LargerVector == MVT())
10477330f729Sjoerg       break;
10487330f729Sjoerg 
10497330f729Sjoerg     // If this type is legal then widen the vector.
10507330f729Sjoerg     if (ValueTypeActions.getTypeAction(LargerVector) == TypeLegal)
10517330f729Sjoerg       return LegalizeKind(TypeWidenVector, LargerVector);
10527330f729Sjoerg   }
10537330f729Sjoerg 
10547330f729Sjoerg   // Widen odd vectors to next power of two.
10557330f729Sjoerg   if (!VT.isPow2VectorType()) {
10567330f729Sjoerg     EVT NVT = VT.getPow2VectorType(Context);
10577330f729Sjoerg     return LegalizeKind(TypeWidenVector, NVT);
10587330f729Sjoerg   }
10597330f729Sjoerg 
1060*82d56013Sjoerg   if (VT.getVectorElementCount() == ElementCount::getScalable(1))
1061*82d56013Sjoerg     report_fatal_error("Cannot legalize this vector");
1062*82d56013Sjoerg 
10637330f729Sjoerg   // Vectors with illegal element types are expanded.
1064*82d56013Sjoerg   EVT NVT = EVT::getVectorVT(Context, EltVT,
1065*82d56013Sjoerg                              VT.getVectorElementCount().divideCoefficientBy(2));
10667330f729Sjoerg   return LegalizeKind(TypeSplitVector, NVT);
10677330f729Sjoerg }
10687330f729Sjoerg 
getVectorTypeBreakdownMVT(MVT VT,MVT & IntermediateVT,unsigned & NumIntermediates,MVT & RegisterVT,TargetLoweringBase * TLI)10697330f729Sjoerg static unsigned getVectorTypeBreakdownMVT(MVT VT, MVT &IntermediateVT,
10707330f729Sjoerg                                           unsigned &NumIntermediates,
10717330f729Sjoerg                                           MVT &RegisterVT,
10727330f729Sjoerg                                           TargetLoweringBase *TLI) {
10737330f729Sjoerg   // Figure out the right, legal destination reg to copy into.
1074*82d56013Sjoerg   ElementCount EC = VT.getVectorElementCount();
10757330f729Sjoerg   MVT EltTy = VT.getVectorElementType();
10767330f729Sjoerg 
10777330f729Sjoerg   unsigned NumVectorRegs = 1;
10787330f729Sjoerg 
1079*82d56013Sjoerg   // Scalable vectors cannot be scalarized, so splitting or widening is
1080*82d56013Sjoerg   // required.
1081*82d56013Sjoerg   if (VT.isScalableVector() && !isPowerOf2_32(EC.getKnownMinValue()))
1082*82d56013Sjoerg     llvm_unreachable(
1083*82d56013Sjoerg         "Splitting or widening of non-power-of-2 MVTs is not implemented.");
1084*82d56013Sjoerg 
1085*82d56013Sjoerg   // FIXME: We don't support non-power-of-2-sized vectors for now.
1086*82d56013Sjoerg   // Ideally we could break down into LHS/RHS like LegalizeDAG does.
1087*82d56013Sjoerg   if (!isPowerOf2_32(EC.getKnownMinValue())) {
1088*82d56013Sjoerg     // Split EC to unit size (scalable property is preserved).
1089*82d56013Sjoerg     NumVectorRegs = EC.getKnownMinValue();
1090*82d56013Sjoerg     EC = ElementCount::getFixed(1);
10917330f729Sjoerg   }
10927330f729Sjoerg 
1093*82d56013Sjoerg   // Divide the input until we get to a supported size. This will
1094*82d56013Sjoerg   // always end up with an EC that represent a scalar or a scalable
1095*82d56013Sjoerg   // scalar.
1096*82d56013Sjoerg   while (EC.getKnownMinValue() > 1 &&
1097*82d56013Sjoerg          !TLI->isTypeLegal(MVT::getVectorVT(EltTy, EC))) {
1098*82d56013Sjoerg     EC = EC.divideCoefficientBy(2);
10997330f729Sjoerg     NumVectorRegs <<= 1;
11007330f729Sjoerg   }
11017330f729Sjoerg 
11027330f729Sjoerg   NumIntermediates = NumVectorRegs;
11037330f729Sjoerg 
1104*82d56013Sjoerg   MVT NewVT = MVT::getVectorVT(EltTy, EC);
11057330f729Sjoerg   if (!TLI->isTypeLegal(NewVT))
11067330f729Sjoerg     NewVT = EltTy;
11077330f729Sjoerg   IntermediateVT = NewVT;
11087330f729Sjoerg 
1109*82d56013Sjoerg   unsigned LaneSizeInBits = NewVT.getScalarSizeInBits();
11107330f729Sjoerg 
11117330f729Sjoerg   // Convert sizes such as i33 to i64.
1112*82d56013Sjoerg   if (!isPowerOf2_32(LaneSizeInBits))
1113*82d56013Sjoerg     LaneSizeInBits = NextPowerOf2(LaneSizeInBits);
11147330f729Sjoerg 
11157330f729Sjoerg   MVT DestVT = TLI->getRegisterType(NewVT);
11167330f729Sjoerg   RegisterVT = DestVT;
11177330f729Sjoerg   if (EVT(DestVT).bitsLT(NewVT))    // Value is expanded, e.g. i64 -> i16.
1118*82d56013Sjoerg     return NumVectorRegs * (LaneSizeInBits / DestVT.getScalarSizeInBits());
11197330f729Sjoerg 
11207330f729Sjoerg   // Otherwise, promotion or legal types use the same number of registers as
11217330f729Sjoerg   // the vector decimated to the appropriate level.
11227330f729Sjoerg   return NumVectorRegs;
11237330f729Sjoerg }
11247330f729Sjoerg 
11257330f729Sjoerg /// isLegalRC - Return true if the value types that can be represented by the
11267330f729Sjoerg /// specified register class are all legal.
isLegalRC(const TargetRegisterInfo & TRI,const TargetRegisterClass & RC) const11277330f729Sjoerg bool TargetLoweringBase::isLegalRC(const TargetRegisterInfo &TRI,
11287330f729Sjoerg                                    const TargetRegisterClass &RC) const {
11297330f729Sjoerg   for (auto I = TRI.legalclasstypes_begin(RC); *I != MVT::Other; ++I)
11307330f729Sjoerg     if (isTypeLegal(*I))
11317330f729Sjoerg       return true;
11327330f729Sjoerg   return false;
11337330f729Sjoerg }
11347330f729Sjoerg 
11357330f729Sjoerg /// Replace/modify any TargetFrameIndex operands with a targte-dependent
11367330f729Sjoerg /// sequence of memory operands that is recognized by PrologEpilogInserter.
11377330f729Sjoerg MachineBasicBlock *
emitPatchPoint(MachineInstr & InitialMI,MachineBasicBlock * MBB) const11387330f729Sjoerg TargetLoweringBase::emitPatchPoint(MachineInstr &InitialMI,
11397330f729Sjoerg                                    MachineBasicBlock *MBB) const {
11407330f729Sjoerg   MachineInstr *MI = &InitialMI;
11417330f729Sjoerg   MachineFunction &MF = *MI->getMF();
11427330f729Sjoerg   MachineFrameInfo &MFI = MF.getFrameInfo();
11437330f729Sjoerg 
11447330f729Sjoerg   // We're handling multiple types of operands here:
11457330f729Sjoerg   // PATCHPOINT MetaArgs - live-in, read only, direct
11467330f729Sjoerg   // STATEPOINT Deopt Spill - live-through, read only, indirect
11477330f729Sjoerg   // STATEPOINT Deopt Alloca - live-through, read only, direct
11487330f729Sjoerg   // (We're currently conservative and mark the deopt slots read/write in
11497330f729Sjoerg   // practice.)
11507330f729Sjoerg   // STATEPOINT GC Spill - live-through, read/write, indirect
11517330f729Sjoerg   // STATEPOINT GC Alloca - live-through, read/write, direct
11527330f729Sjoerg   // The live-in vs live-through is handled already (the live through ones are
11537330f729Sjoerg   // all stack slots), but we need to handle the different type of stackmap
11547330f729Sjoerg   // operands and memory effects here.
11557330f729Sjoerg 
1156*82d56013Sjoerg   if (!llvm::any_of(MI->operands(),
1157*82d56013Sjoerg                     [](MachineOperand &Operand) { return Operand.isFI(); }))
1158*82d56013Sjoerg     return MBB;
1159*82d56013Sjoerg 
1160*82d56013Sjoerg   MachineInstrBuilder MIB = BuildMI(MF, MI->getDebugLoc(), MI->getDesc());
1161*82d56013Sjoerg 
1162*82d56013Sjoerg   // Inherit previous memory operands.
1163*82d56013Sjoerg   MIB.cloneMemRefs(*MI);
1164*82d56013Sjoerg 
1165*82d56013Sjoerg   for (unsigned i = 0; i < MI->getNumOperands(); ++i) {
1166*82d56013Sjoerg     MachineOperand &MO = MI->getOperand(i);
1167*82d56013Sjoerg     if (!MO.isFI()) {
1168*82d56013Sjoerg       // Index of Def operand this Use it tied to.
1169*82d56013Sjoerg       // Since Defs are coming before Uses, if Use is tied, then
1170*82d56013Sjoerg       // index of Def must be smaller that index of that Use.
1171*82d56013Sjoerg       // Also, Defs preserve their position in new MI.
1172*82d56013Sjoerg       unsigned TiedTo = i;
1173*82d56013Sjoerg       if (MO.isReg() && MO.isTied())
1174*82d56013Sjoerg         TiedTo = MI->findTiedOperandIdx(i);
1175*82d56013Sjoerg       MIB.add(MO);
1176*82d56013Sjoerg       if (TiedTo < i)
1177*82d56013Sjoerg         MIB->tieOperands(TiedTo, MIB->getNumOperands() - 1);
11787330f729Sjoerg       continue;
1179*82d56013Sjoerg     }
11807330f729Sjoerg 
11817330f729Sjoerg     // foldMemoryOperand builds a new MI after replacing a single FI operand
11827330f729Sjoerg     // with the canonical set of five x86 addressing-mode operands.
11837330f729Sjoerg     int FI = MO.getIndex();
11847330f729Sjoerg 
11857330f729Sjoerg     // Add frame index operands recognized by stackmaps.cpp
11867330f729Sjoerg     if (MFI.isStatepointSpillSlotObjectIndex(FI)) {
11877330f729Sjoerg       // indirect-mem-ref tag, size, #FI, offset.
11887330f729Sjoerg       // Used for spills inserted by StatepointLowering.  This codepath is not
11897330f729Sjoerg       // used for patchpoints/stackmaps at all, for these spilling is done via
11907330f729Sjoerg       // foldMemoryOperand callback only.
11917330f729Sjoerg       assert(MI->getOpcode() == TargetOpcode::STATEPOINT && "sanity");
11927330f729Sjoerg       MIB.addImm(StackMaps::IndirectMemRefOp);
11937330f729Sjoerg       MIB.addImm(MFI.getObjectSize(FI));
1194*82d56013Sjoerg       MIB.add(MO);
11957330f729Sjoerg       MIB.addImm(0);
11967330f729Sjoerg     } else {
11977330f729Sjoerg       // direct-mem-ref tag, #FI, offset.
11987330f729Sjoerg       // Used by patchpoint, and direct alloca arguments to statepoints
11997330f729Sjoerg       MIB.addImm(StackMaps::DirectMemRefOp);
1200*82d56013Sjoerg       MIB.add(MO);
12017330f729Sjoerg       MIB.addImm(0);
12027330f729Sjoerg     }
12037330f729Sjoerg 
12047330f729Sjoerg     assert(MIB->mayLoad() && "Folded a stackmap use to a non-load!");
12057330f729Sjoerg 
12067330f729Sjoerg     // Add a new memory operand for this FI.
12077330f729Sjoerg     assert(MFI.getObjectOffset(FI) != -1);
12087330f729Sjoerg 
12097330f729Sjoerg     // Note: STATEPOINT MMOs are added during SelectionDAG.  STACKMAP, and
12107330f729Sjoerg     // PATCHPOINT should be updated to do the same. (TODO)
12117330f729Sjoerg     if (MI->getOpcode() != TargetOpcode::STATEPOINT) {
12127330f729Sjoerg       auto Flags = MachineMemOperand::MOLoad;
12137330f729Sjoerg       MachineMemOperand *MMO = MF.getMachineMemOperand(
12147330f729Sjoerg           MachinePointerInfo::getFixedStack(MF, FI), Flags,
1215*82d56013Sjoerg           MF.getDataLayout().getPointerSize(), MFI.getObjectAlign(FI));
12167330f729Sjoerg       MIB->addMemOperand(MF, MMO);
12177330f729Sjoerg     }
1218*82d56013Sjoerg   }
12197330f729Sjoerg   MBB->insert(MachineBasicBlock::iterator(MI), MIB);
12207330f729Sjoerg   MI->eraseFromParent();
12217330f729Sjoerg   return MBB;
12227330f729Sjoerg }
12237330f729Sjoerg 
12247330f729Sjoerg /// findRepresentativeClass - Return the largest legal super-reg register class
12257330f729Sjoerg /// of the register class for the specified type and its associated "cost".
12267330f729Sjoerg // This function is in TargetLowering because it uses RegClassForVT which would
12277330f729Sjoerg // need to be moved to TargetRegisterInfo and would necessitate moving
12287330f729Sjoerg // isTypeLegal over as well - a massive change that would just require
12297330f729Sjoerg // TargetLowering having a TargetRegisterInfo class member that it would use.
12307330f729Sjoerg std::pair<const TargetRegisterClass *, uint8_t>
findRepresentativeClass(const TargetRegisterInfo * TRI,MVT VT) const12317330f729Sjoerg TargetLoweringBase::findRepresentativeClass(const TargetRegisterInfo *TRI,
12327330f729Sjoerg                                             MVT VT) const {
12337330f729Sjoerg   const TargetRegisterClass *RC = RegClassForVT[VT.SimpleTy];
12347330f729Sjoerg   if (!RC)
12357330f729Sjoerg     return std::make_pair(RC, 0);
12367330f729Sjoerg 
12377330f729Sjoerg   // Compute the set of all super-register classes.
12387330f729Sjoerg   BitVector SuperRegRC(TRI->getNumRegClasses());
12397330f729Sjoerg   for (SuperRegClassIterator RCI(RC, TRI); RCI.isValid(); ++RCI)
12407330f729Sjoerg     SuperRegRC.setBitsInMask(RCI.getMask());
12417330f729Sjoerg 
12427330f729Sjoerg   // Find the first legal register class with the largest spill size.
12437330f729Sjoerg   const TargetRegisterClass *BestRC = RC;
12447330f729Sjoerg   for (unsigned i : SuperRegRC.set_bits()) {
12457330f729Sjoerg     const TargetRegisterClass *SuperRC = TRI->getRegClass(i);
12467330f729Sjoerg     // We want the largest possible spill size.
12477330f729Sjoerg     if (TRI->getSpillSize(*SuperRC) <= TRI->getSpillSize(*BestRC))
12487330f729Sjoerg       continue;
12497330f729Sjoerg     if (!isLegalRC(*TRI, *SuperRC))
12507330f729Sjoerg       continue;
12517330f729Sjoerg     BestRC = SuperRC;
12527330f729Sjoerg   }
12537330f729Sjoerg   return std::make_pair(BestRC, 1);
12547330f729Sjoerg }
12557330f729Sjoerg 
12567330f729Sjoerg /// computeRegisterProperties - Once all of the register classes are added,
12577330f729Sjoerg /// this allows us to compute derived properties we expose.
computeRegisterProperties(const TargetRegisterInfo * TRI)12587330f729Sjoerg void TargetLoweringBase::computeRegisterProperties(
12597330f729Sjoerg     const TargetRegisterInfo *TRI) {
12607330f729Sjoerg   static_assert(MVT::LAST_VALUETYPE <= MVT::MAX_ALLOWED_VALUETYPE,
12617330f729Sjoerg                 "Too many value types for ValueTypeActions to hold!");
12627330f729Sjoerg 
12637330f729Sjoerg   // Everything defaults to needing one register.
12647330f729Sjoerg   for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i) {
12657330f729Sjoerg     NumRegistersForVT[i] = 1;
12667330f729Sjoerg     RegisterTypeForVT[i] = TransformToType[i] = (MVT::SimpleValueType)i;
12677330f729Sjoerg   }
12687330f729Sjoerg   // ...except isVoid, which doesn't need any registers.
12697330f729Sjoerg   NumRegistersForVT[MVT::isVoid] = 0;
12707330f729Sjoerg 
12717330f729Sjoerg   // Find the largest integer register class.
12727330f729Sjoerg   unsigned LargestIntReg = MVT::LAST_INTEGER_VALUETYPE;
12737330f729Sjoerg   for (; RegClassForVT[LargestIntReg] == nullptr; --LargestIntReg)
12747330f729Sjoerg     assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
12757330f729Sjoerg 
12767330f729Sjoerg   // Every integer value type larger than this largest register takes twice as
12777330f729Sjoerg   // many registers to represent as the previous ValueType.
12787330f729Sjoerg   for (unsigned ExpandedReg = LargestIntReg + 1;
12797330f729Sjoerg        ExpandedReg <= MVT::LAST_INTEGER_VALUETYPE; ++ExpandedReg) {
12807330f729Sjoerg     NumRegistersForVT[ExpandedReg] = 2*NumRegistersForVT[ExpandedReg-1];
12817330f729Sjoerg     RegisterTypeForVT[ExpandedReg] = (MVT::SimpleValueType)LargestIntReg;
12827330f729Sjoerg     TransformToType[ExpandedReg] = (MVT::SimpleValueType)(ExpandedReg - 1);
12837330f729Sjoerg     ValueTypeActions.setTypeAction((MVT::SimpleValueType)ExpandedReg,
12847330f729Sjoerg                                    TypeExpandInteger);
12857330f729Sjoerg   }
12867330f729Sjoerg 
12877330f729Sjoerg   // Inspect all of the ValueType's smaller than the largest integer
12887330f729Sjoerg   // register to see which ones need promotion.
12897330f729Sjoerg   unsigned LegalIntReg = LargestIntReg;
12907330f729Sjoerg   for (unsigned IntReg = LargestIntReg - 1;
12917330f729Sjoerg        IntReg >= (unsigned)MVT::i1; --IntReg) {
12927330f729Sjoerg     MVT IVT = (MVT::SimpleValueType)IntReg;
12937330f729Sjoerg     if (isTypeLegal(IVT)) {
12947330f729Sjoerg       LegalIntReg = IntReg;
12957330f729Sjoerg     } else {
12967330f729Sjoerg       RegisterTypeForVT[IntReg] = TransformToType[IntReg] =
12977330f729Sjoerg         (MVT::SimpleValueType)LegalIntReg;
12987330f729Sjoerg       ValueTypeActions.setTypeAction(IVT, TypePromoteInteger);
12997330f729Sjoerg     }
13007330f729Sjoerg   }
13017330f729Sjoerg 
13027330f729Sjoerg   // ppcf128 type is really two f64's.
13037330f729Sjoerg   if (!isTypeLegal(MVT::ppcf128)) {
13047330f729Sjoerg     if (isTypeLegal(MVT::f64)) {
13057330f729Sjoerg       NumRegistersForVT[MVT::ppcf128] = 2*NumRegistersForVT[MVT::f64];
13067330f729Sjoerg       RegisterTypeForVT[MVT::ppcf128] = MVT::f64;
13077330f729Sjoerg       TransformToType[MVT::ppcf128] = MVT::f64;
13087330f729Sjoerg       ValueTypeActions.setTypeAction(MVT::ppcf128, TypeExpandFloat);
13097330f729Sjoerg     } else {
13107330f729Sjoerg       NumRegistersForVT[MVT::ppcf128] = NumRegistersForVT[MVT::i128];
13117330f729Sjoerg       RegisterTypeForVT[MVT::ppcf128] = RegisterTypeForVT[MVT::i128];
13127330f729Sjoerg       TransformToType[MVT::ppcf128] = MVT::i128;
13137330f729Sjoerg       ValueTypeActions.setTypeAction(MVT::ppcf128, TypeSoftenFloat);
13147330f729Sjoerg     }
13157330f729Sjoerg   }
13167330f729Sjoerg 
13177330f729Sjoerg   // Decide how to handle f128. If the target does not have native f128 support,
13187330f729Sjoerg   // expand it to i128 and we will be generating soft float library calls.
13197330f729Sjoerg   if (!isTypeLegal(MVT::f128)) {
13207330f729Sjoerg     NumRegistersForVT[MVT::f128] = NumRegistersForVT[MVT::i128];
13217330f729Sjoerg     RegisterTypeForVT[MVT::f128] = RegisterTypeForVT[MVT::i128];
13227330f729Sjoerg     TransformToType[MVT::f128] = MVT::i128;
13237330f729Sjoerg     ValueTypeActions.setTypeAction(MVT::f128, TypeSoftenFloat);
13247330f729Sjoerg   }
13257330f729Sjoerg 
13267330f729Sjoerg   // Decide how to handle f64. If the target does not have native f64 support,
13277330f729Sjoerg   // expand it to i64 and we will be generating soft float library calls.
13287330f729Sjoerg   if (!isTypeLegal(MVT::f64)) {
13297330f729Sjoerg     NumRegistersForVT[MVT::f64] = NumRegistersForVT[MVT::i64];
13307330f729Sjoerg     RegisterTypeForVT[MVT::f64] = RegisterTypeForVT[MVT::i64];
13317330f729Sjoerg     TransformToType[MVT::f64] = MVT::i64;
13327330f729Sjoerg     ValueTypeActions.setTypeAction(MVT::f64, TypeSoftenFloat);
13337330f729Sjoerg   }
13347330f729Sjoerg 
13357330f729Sjoerg   // Decide how to handle f32. If the target does not have native f32 support,
13367330f729Sjoerg   // expand it to i32 and we will be generating soft float library calls.
13377330f729Sjoerg   if (!isTypeLegal(MVT::f32)) {
13387330f729Sjoerg     NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::i32];
13397330f729Sjoerg     RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::i32];
13407330f729Sjoerg     TransformToType[MVT::f32] = MVT::i32;
13417330f729Sjoerg     ValueTypeActions.setTypeAction(MVT::f32, TypeSoftenFloat);
13427330f729Sjoerg   }
13437330f729Sjoerg 
13447330f729Sjoerg   // Decide how to handle f16. If the target does not have native f16 support,
13457330f729Sjoerg   // promote it to f32, because there are no f16 library calls (except for
13467330f729Sjoerg   // conversions).
13477330f729Sjoerg   if (!isTypeLegal(MVT::f16)) {
1348*82d56013Sjoerg     // Allow targets to control how we legalize half.
1349*82d56013Sjoerg     if (softPromoteHalfType()) {
1350*82d56013Sjoerg       NumRegistersForVT[MVT::f16] = NumRegistersForVT[MVT::i16];
1351*82d56013Sjoerg       RegisterTypeForVT[MVT::f16] = RegisterTypeForVT[MVT::i16];
1352*82d56013Sjoerg       TransformToType[MVT::f16] = MVT::f32;
1353*82d56013Sjoerg       ValueTypeActions.setTypeAction(MVT::f16, TypeSoftPromoteHalf);
1354*82d56013Sjoerg     } else {
13557330f729Sjoerg       NumRegistersForVT[MVT::f16] = NumRegistersForVT[MVT::f32];
13567330f729Sjoerg       RegisterTypeForVT[MVT::f16] = RegisterTypeForVT[MVT::f32];
13577330f729Sjoerg       TransformToType[MVT::f16] = MVT::f32;
13587330f729Sjoerg       ValueTypeActions.setTypeAction(MVT::f16, TypePromoteFloat);
13597330f729Sjoerg     }
1360*82d56013Sjoerg   }
13617330f729Sjoerg 
13627330f729Sjoerg   // Loop over all of the vector value types to see which need transformations.
13637330f729Sjoerg   for (unsigned i = MVT::FIRST_VECTOR_VALUETYPE;
13647330f729Sjoerg        i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
13657330f729Sjoerg     MVT VT = (MVT::SimpleValueType) i;
13667330f729Sjoerg     if (isTypeLegal(VT))
13677330f729Sjoerg       continue;
13687330f729Sjoerg 
13697330f729Sjoerg     MVT EltVT = VT.getVectorElementType();
1370*82d56013Sjoerg     ElementCount EC = VT.getVectorElementCount();
13717330f729Sjoerg     bool IsLegalWiderType = false;
13727330f729Sjoerg     bool IsScalable = VT.isScalableVector();
13737330f729Sjoerg     LegalizeTypeAction PreferredAction = getPreferredVectorAction(VT);
13747330f729Sjoerg     switch (PreferredAction) {
13757330f729Sjoerg     case TypePromoteInteger: {
13767330f729Sjoerg       MVT::SimpleValueType EndVT = IsScalable ?
13777330f729Sjoerg                                    MVT::LAST_INTEGER_SCALABLE_VECTOR_VALUETYPE :
13787330f729Sjoerg                                    MVT::LAST_INTEGER_FIXEDLEN_VECTOR_VALUETYPE;
13797330f729Sjoerg       // Try to promote the elements of integer vectors. If no legal
13807330f729Sjoerg       // promotion was found, fall through to the widen-vector method.
13817330f729Sjoerg       for (unsigned nVT = i + 1;
13827330f729Sjoerg            (MVT::SimpleValueType)nVT <= EndVT; ++nVT) {
13837330f729Sjoerg         MVT SVT = (MVT::SimpleValueType) nVT;
13847330f729Sjoerg         // Promote vectors of integers to vectors with the same number
13857330f729Sjoerg         // of elements, with a wider element type.
1386*82d56013Sjoerg         if (SVT.getScalarSizeInBits() > EltVT.getFixedSizeInBits() &&
1387*82d56013Sjoerg             SVT.getVectorElementCount() == EC && isTypeLegal(SVT)) {
13887330f729Sjoerg           TransformToType[i] = SVT;
13897330f729Sjoerg           RegisterTypeForVT[i] = SVT;
13907330f729Sjoerg           NumRegistersForVT[i] = 1;
13917330f729Sjoerg           ValueTypeActions.setTypeAction(VT, TypePromoteInteger);
13927330f729Sjoerg           IsLegalWiderType = true;
13937330f729Sjoerg           break;
13947330f729Sjoerg         }
13957330f729Sjoerg       }
13967330f729Sjoerg       if (IsLegalWiderType)
13977330f729Sjoerg         break;
13987330f729Sjoerg       LLVM_FALLTHROUGH;
13997330f729Sjoerg     }
14007330f729Sjoerg 
14017330f729Sjoerg     case TypeWidenVector:
1402*82d56013Sjoerg       if (isPowerOf2_32(EC.getKnownMinValue())) {
14037330f729Sjoerg         // Try to widen the vector.
14047330f729Sjoerg         for (unsigned nVT = i + 1; nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
14057330f729Sjoerg           MVT SVT = (MVT::SimpleValueType) nVT;
1406*82d56013Sjoerg           if (SVT.getVectorElementType() == EltVT &&
1407*82d56013Sjoerg               SVT.isScalableVector() == IsScalable &&
1408*82d56013Sjoerg               SVT.getVectorElementCount().getKnownMinValue() >
1409*82d56013Sjoerg                   EC.getKnownMinValue() &&
1410*82d56013Sjoerg               isTypeLegal(SVT)) {
14117330f729Sjoerg             TransformToType[i] = SVT;
14127330f729Sjoerg             RegisterTypeForVT[i] = SVT;
14137330f729Sjoerg             NumRegistersForVT[i] = 1;
14147330f729Sjoerg             ValueTypeActions.setTypeAction(VT, TypeWidenVector);
14157330f729Sjoerg             IsLegalWiderType = true;
14167330f729Sjoerg             break;
14177330f729Sjoerg           }
14187330f729Sjoerg         }
14197330f729Sjoerg         if (IsLegalWiderType)
14207330f729Sjoerg           break;
14217330f729Sjoerg       } else {
14227330f729Sjoerg         // Only widen to the next power of 2 to keep consistency with EVT.
14237330f729Sjoerg         MVT NVT = VT.getPow2VectorType();
14247330f729Sjoerg         if (isTypeLegal(NVT)) {
14257330f729Sjoerg           TransformToType[i] = NVT;
14267330f729Sjoerg           ValueTypeActions.setTypeAction(VT, TypeWidenVector);
14277330f729Sjoerg           RegisterTypeForVT[i] = NVT;
14287330f729Sjoerg           NumRegistersForVT[i] = 1;
14297330f729Sjoerg           break;
14307330f729Sjoerg         }
14317330f729Sjoerg       }
14327330f729Sjoerg       LLVM_FALLTHROUGH;
14337330f729Sjoerg 
14347330f729Sjoerg     case TypeSplitVector:
14357330f729Sjoerg     case TypeScalarizeVector: {
14367330f729Sjoerg       MVT IntermediateVT;
14377330f729Sjoerg       MVT RegisterVT;
14387330f729Sjoerg       unsigned NumIntermediates;
1439*82d56013Sjoerg       unsigned NumRegisters = getVectorTypeBreakdownMVT(VT, IntermediateVT,
14407330f729Sjoerg           NumIntermediates, RegisterVT, this);
1441*82d56013Sjoerg       NumRegistersForVT[i] = NumRegisters;
1442*82d56013Sjoerg       assert(NumRegistersForVT[i] == NumRegisters &&
1443*82d56013Sjoerg              "NumRegistersForVT size cannot represent NumRegisters!");
14447330f729Sjoerg       RegisterTypeForVT[i] = RegisterVT;
14457330f729Sjoerg 
14467330f729Sjoerg       MVT NVT = VT.getPow2VectorType();
14477330f729Sjoerg       if (NVT == VT) {
14487330f729Sjoerg         // Type is already a power of 2.  The default action is to split.
14497330f729Sjoerg         TransformToType[i] = MVT::Other;
14507330f729Sjoerg         if (PreferredAction == TypeScalarizeVector)
14517330f729Sjoerg           ValueTypeActions.setTypeAction(VT, TypeScalarizeVector);
14527330f729Sjoerg         else if (PreferredAction == TypeSplitVector)
14537330f729Sjoerg           ValueTypeActions.setTypeAction(VT, TypeSplitVector);
1454*82d56013Sjoerg         else if (EC.getKnownMinValue() > 1)
1455*82d56013Sjoerg           ValueTypeActions.setTypeAction(VT, TypeSplitVector);
14567330f729Sjoerg         else
1457*82d56013Sjoerg           ValueTypeActions.setTypeAction(VT, EC.isScalable()
1458*82d56013Sjoerg                                                  ? TypeScalarizeScalableVector
1459*82d56013Sjoerg                                                  : TypeScalarizeVector);
14607330f729Sjoerg       } else {
14617330f729Sjoerg         TransformToType[i] = NVT;
14627330f729Sjoerg         ValueTypeActions.setTypeAction(VT, TypeWidenVector);
14637330f729Sjoerg       }
14647330f729Sjoerg       break;
14657330f729Sjoerg     }
14667330f729Sjoerg     default:
14677330f729Sjoerg       llvm_unreachable("Unknown vector legalization action!");
14687330f729Sjoerg     }
14697330f729Sjoerg   }
14707330f729Sjoerg 
14717330f729Sjoerg   // Determine the 'representative' register class for each value type.
14727330f729Sjoerg   // An representative register class is the largest (meaning one which is
14737330f729Sjoerg   // not a sub-register class / subreg register class) legal register class for
14747330f729Sjoerg   // a group of value types. For example, on i386, i8, i16, and i32
14757330f729Sjoerg   // representative would be GR32; while on x86_64 it's GR64.
14767330f729Sjoerg   for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i) {
14777330f729Sjoerg     const TargetRegisterClass* RRC;
14787330f729Sjoerg     uint8_t Cost;
14797330f729Sjoerg     std::tie(RRC, Cost) = findRepresentativeClass(TRI, (MVT::SimpleValueType)i);
14807330f729Sjoerg     RepRegClassForVT[i] = RRC;
14817330f729Sjoerg     RepRegClassCostForVT[i] = Cost;
14827330f729Sjoerg   }
14837330f729Sjoerg }
14847330f729Sjoerg 
getSetCCResultType(const DataLayout & DL,LLVMContext &,EVT VT) const14857330f729Sjoerg EVT TargetLoweringBase::getSetCCResultType(const DataLayout &DL, LLVMContext &,
14867330f729Sjoerg                                            EVT VT) const {
14877330f729Sjoerg   assert(!VT.isVector() && "No default SetCC type for vectors!");
14887330f729Sjoerg   return getPointerTy(DL).SimpleTy;
14897330f729Sjoerg }
14907330f729Sjoerg 
getCmpLibcallReturnType() const14917330f729Sjoerg MVT::SimpleValueType TargetLoweringBase::getCmpLibcallReturnType() const {
14927330f729Sjoerg   return MVT::i32; // return the default value
14937330f729Sjoerg }
14947330f729Sjoerg 
14957330f729Sjoerg /// getVectorTypeBreakdown - Vector types are broken down into some number of
14967330f729Sjoerg /// legal first class types.  For example, MVT::v8f32 maps to 2 MVT::v4f32
14977330f729Sjoerg /// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
14987330f729Sjoerg /// Similarly, MVT::v2i64 turns into 4 MVT::i32 values with both PPC and X86.
14997330f729Sjoerg ///
15007330f729Sjoerg /// This method returns the number of registers needed, and the VT for each
15017330f729Sjoerg /// register.  It also returns the VT and quantity of the intermediate values
15027330f729Sjoerg /// before they are promoted/expanded.
getVectorTypeBreakdown(LLVMContext & Context,EVT VT,EVT & IntermediateVT,unsigned & NumIntermediates,MVT & RegisterVT) const1503*82d56013Sjoerg unsigned TargetLoweringBase::getVectorTypeBreakdown(LLVMContext &Context,
1504*82d56013Sjoerg                                                     EVT VT, EVT &IntermediateVT,
15057330f729Sjoerg                                                     unsigned &NumIntermediates,
15067330f729Sjoerg                                                     MVT &RegisterVT) const {
1507*82d56013Sjoerg   ElementCount EltCnt = VT.getVectorElementCount();
15087330f729Sjoerg 
15097330f729Sjoerg   // If there is a wider vector type with the same element type as this one,
15107330f729Sjoerg   // or a promoted vector type that has the same number of elements which
15117330f729Sjoerg   // are wider, then we should convert to that legal vector type.
15127330f729Sjoerg   // This handles things like <2 x float> -> <4 x float> and
15137330f729Sjoerg   // <4 x i1> -> <4 x i32>.
15147330f729Sjoerg   LegalizeTypeAction TA = getTypeAction(Context, VT);
1515*82d56013Sjoerg   if (!EltCnt.isScalar() &&
1516*82d56013Sjoerg       (TA == TypeWidenVector || TA == TypePromoteInteger)) {
15177330f729Sjoerg     EVT RegisterEVT = getTypeToTransformTo(Context, VT);
15187330f729Sjoerg     if (isTypeLegal(RegisterEVT)) {
15197330f729Sjoerg       IntermediateVT = RegisterEVT;
15207330f729Sjoerg       RegisterVT = RegisterEVT.getSimpleVT();
15217330f729Sjoerg       NumIntermediates = 1;
15227330f729Sjoerg       return 1;
15237330f729Sjoerg     }
15247330f729Sjoerg   }
15257330f729Sjoerg 
15267330f729Sjoerg   // Figure out the right, legal destination reg to copy into.
15277330f729Sjoerg   EVT EltTy = VT.getVectorElementType();
15287330f729Sjoerg 
15297330f729Sjoerg   unsigned NumVectorRegs = 1;
15307330f729Sjoerg 
1531*82d56013Sjoerg   // Scalable vectors cannot be scalarized, so handle the legalisation of the
1532*82d56013Sjoerg   // types like done elsewhere in SelectionDAG.
1533*82d56013Sjoerg   if (VT.isScalableVector() && !isPowerOf2_32(EltCnt.getKnownMinValue())) {
1534*82d56013Sjoerg     LegalizeKind LK;
1535*82d56013Sjoerg     EVT PartVT = VT;
1536*82d56013Sjoerg     do {
1537*82d56013Sjoerg       // Iterate until we've found a legal (part) type to hold VT.
1538*82d56013Sjoerg       LK = getTypeConversion(Context, PartVT);
1539*82d56013Sjoerg       PartVT = LK.second;
1540*82d56013Sjoerg     } while (LK.first != TypeLegal);
1541*82d56013Sjoerg 
1542*82d56013Sjoerg     NumIntermediates = VT.getVectorElementCount().getKnownMinValue() /
1543*82d56013Sjoerg                        PartVT.getVectorElementCount().getKnownMinValue();
1544*82d56013Sjoerg 
1545*82d56013Sjoerg     // FIXME: This code needs to be extended to handle more complex vector
1546*82d56013Sjoerg     // breakdowns, like nxv7i64 -> nxv8i64 -> 4 x nxv2i64. Currently the only
1547*82d56013Sjoerg     // supported cases are vectors that are broken down into equal parts
1548*82d56013Sjoerg     // such as nxv6i64 -> 3 x nxv2i64.
1549*82d56013Sjoerg     assert((PartVT.getVectorElementCount() * NumIntermediates) ==
1550*82d56013Sjoerg                VT.getVectorElementCount() &&
1551*82d56013Sjoerg            "Expected an integer multiple of PartVT");
1552*82d56013Sjoerg     IntermediateVT = PartVT;
1553*82d56013Sjoerg     RegisterVT = getRegisterType(Context, IntermediateVT);
1554*82d56013Sjoerg     return NumIntermediates;
1555*82d56013Sjoerg   }
1556*82d56013Sjoerg 
1557*82d56013Sjoerg   // FIXME: We don't support non-power-of-2-sized vectors for now.  Ideally
1558*82d56013Sjoerg   // we could break down into LHS/RHS like LegalizeDAG does.
1559*82d56013Sjoerg   if (!isPowerOf2_32(EltCnt.getKnownMinValue())) {
1560*82d56013Sjoerg     NumVectorRegs = EltCnt.getKnownMinValue();
1561*82d56013Sjoerg     EltCnt = ElementCount::getFixed(1);
15627330f729Sjoerg   }
15637330f729Sjoerg 
15647330f729Sjoerg   // Divide the input until we get to a supported size.  This will always
15657330f729Sjoerg   // end with a scalar if the target doesn't support vectors.
1566*82d56013Sjoerg   while (EltCnt.getKnownMinValue() > 1 &&
1567*82d56013Sjoerg          !isTypeLegal(EVT::getVectorVT(Context, EltTy, EltCnt))) {
1568*82d56013Sjoerg     EltCnt = EltCnt.divideCoefficientBy(2);
15697330f729Sjoerg     NumVectorRegs <<= 1;
15707330f729Sjoerg   }
15717330f729Sjoerg 
15727330f729Sjoerg   NumIntermediates = NumVectorRegs;
15737330f729Sjoerg 
1574*82d56013Sjoerg   EVT NewVT = EVT::getVectorVT(Context, EltTy, EltCnt);
15757330f729Sjoerg   if (!isTypeLegal(NewVT))
15767330f729Sjoerg     NewVT = EltTy;
15777330f729Sjoerg   IntermediateVT = NewVT;
15787330f729Sjoerg 
15797330f729Sjoerg   MVT DestVT = getRegisterType(Context, NewVT);
15807330f729Sjoerg   RegisterVT = DestVT;
15817330f729Sjoerg 
1582*82d56013Sjoerg   if (EVT(DestVT).bitsLT(NewVT)) {  // Value is expanded, e.g. i64 -> i16.
1583*82d56013Sjoerg     TypeSize NewVTSize = NewVT.getSizeInBits();
15847330f729Sjoerg     // Convert sizes such as i33 to i64.
1585*82d56013Sjoerg     if (!isPowerOf2_32(NewVTSize.getKnownMinSize()))
1586*82d56013Sjoerg       NewVTSize = NewVTSize.coefficientNextPowerOf2();
15877330f729Sjoerg     return NumVectorRegs*(NewVTSize/DestVT.getSizeInBits());
1588*82d56013Sjoerg   }
15897330f729Sjoerg 
15907330f729Sjoerg   // Otherwise, promotion or legal types use the same number of registers as
15917330f729Sjoerg   // the vector decimated to the appropriate level.
15927330f729Sjoerg   return NumVectorRegs;
15937330f729Sjoerg }
15947330f729Sjoerg 
isSuitableForJumpTable(const SwitchInst * SI,uint64_t NumCases,uint64_t Range,ProfileSummaryInfo * PSI,BlockFrequencyInfo * BFI) const1595*82d56013Sjoerg bool TargetLoweringBase::isSuitableForJumpTable(const SwitchInst *SI,
1596*82d56013Sjoerg                                                 uint64_t NumCases,
1597*82d56013Sjoerg                                                 uint64_t Range,
1598*82d56013Sjoerg                                                 ProfileSummaryInfo *PSI,
1599*82d56013Sjoerg                                                 BlockFrequencyInfo *BFI) const {
1600*82d56013Sjoerg   // FIXME: This function check the maximum table size and density, but the
1601*82d56013Sjoerg   // minimum size is not checked. It would be nice if the minimum size is
1602*82d56013Sjoerg   // also combined within this function. Currently, the minimum size check is
1603*82d56013Sjoerg   // performed in findJumpTable() in SelectionDAGBuiler and
1604*82d56013Sjoerg   // getEstimatedNumberOfCaseClusters() in BasicTTIImpl.
1605*82d56013Sjoerg   const bool OptForSize =
1606*82d56013Sjoerg       SI->getParent()->getParent()->hasOptSize() ||
1607*82d56013Sjoerg       llvm::shouldOptimizeForSize(SI->getParent(), PSI, BFI);
1608*82d56013Sjoerg   const unsigned MinDensity = getMinimumJumpTableDensity(OptForSize);
1609*82d56013Sjoerg   const unsigned MaxJumpTableSize = getMaximumJumpTableSize();
1610*82d56013Sjoerg 
1611*82d56013Sjoerg   // Check whether the number of cases is small enough and
1612*82d56013Sjoerg   // the range is dense enough for a jump table.
1613*82d56013Sjoerg   return (OptForSize || Range <= MaxJumpTableSize) &&
1614*82d56013Sjoerg          (NumCases * 100 >= Range * MinDensity);
1615*82d56013Sjoerg }
1616*82d56013Sjoerg 
16177330f729Sjoerg /// Get the EVTs and ArgFlags collections that represent the legalized return
16187330f729Sjoerg /// type of the given function.  This does not require a DAG or a return value,
16197330f729Sjoerg /// and is suitable for use before any DAGs for the function are constructed.
16207330f729Sjoerg /// 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)16217330f729Sjoerg void llvm::GetReturnInfo(CallingConv::ID CC, Type *ReturnType,
16227330f729Sjoerg                          AttributeList attr,
16237330f729Sjoerg                          SmallVectorImpl<ISD::OutputArg> &Outs,
16247330f729Sjoerg                          const TargetLowering &TLI, const DataLayout &DL) {
16257330f729Sjoerg   SmallVector<EVT, 4> ValueVTs;
16267330f729Sjoerg   ComputeValueVTs(TLI, DL, ReturnType, ValueVTs);
16277330f729Sjoerg   unsigned NumValues = ValueVTs.size();
16287330f729Sjoerg   if (NumValues == 0) return;
16297330f729Sjoerg 
16307330f729Sjoerg   for (unsigned j = 0, f = NumValues; j != f; ++j) {
16317330f729Sjoerg     EVT VT = ValueVTs[j];
16327330f729Sjoerg     ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
16337330f729Sjoerg 
16347330f729Sjoerg     if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::SExt))
16357330f729Sjoerg       ExtendKind = ISD::SIGN_EXTEND;
16367330f729Sjoerg     else if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::ZExt))
16377330f729Sjoerg       ExtendKind = ISD::ZERO_EXTEND;
16387330f729Sjoerg 
16397330f729Sjoerg     // FIXME: C calling convention requires the return type to be promoted to
16407330f729Sjoerg     // at least 32-bit. But this is not necessary for non-C calling
16417330f729Sjoerg     // conventions. The frontend should mark functions whose return values
16427330f729Sjoerg     // require promoting with signext or zeroext attributes.
16437330f729Sjoerg     if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
16447330f729Sjoerg       MVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
16457330f729Sjoerg       if (VT.bitsLT(MinVT))
16467330f729Sjoerg         VT = MinVT;
16477330f729Sjoerg     }
16487330f729Sjoerg 
16497330f729Sjoerg     unsigned NumParts =
16507330f729Sjoerg         TLI.getNumRegistersForCallingConv(ReturnType->getContext(), CC, VT);
16517330f729Sjoerg     MVT PartVT =
16527330f729Sjoerg         TLI.getRegisterTypeForCallingConv(ReturnType->getContext(), CC, VT);
16537330f729Sjoerg 
16547330f729Sjoerg     // 'inreg' on function refers to return value
16557330f729Sjoerg     ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
16567330f729Sjoerg     if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::InReg))
16577330f729Sjoerg       Flags.setInReg();
16587330f729Sjoerg 
16597330f729Sjoerg     // Propagate extension type if any
16607330f729Sjoerg     if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::SExt))
16617330f729Sjoerg       Flags.setSExt();
16627330f729Sjoerg     else if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::ZExt))
16637330f729Sjoerg       Flags.setZExt();
16647330f729Sjoerg 
16657330f729Sjoerg     for (unsigned i = 0; i < NumParts; ++i)
16667330f729Sjoerg       Outs.push_back(ISD::OutputArg(Flags, PartVT, VT, /*isfixed=*/true, 0, 0));
16677330f729Sjoerg   }
16687330f729Sjoerg }
16697330f729Sjoerg 
16707330f729Sjoerg /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
16717330f729Sjoerg /// function arguments in the caller parameter area.  This is the actual
16727330f729Sjoerg /// alignment, not its logarithm.
getByValTypeAlignment(Type * Ty,const DataLayout & DL) const16737330f729Sjoerg unsigned TargetLoweringBase::getByValTypeAlignment(Type *Ty,
16747330f729Sjoerg                                                    const DataLayout &DL) const {
1675*82d56013Sjoerg   return DL.getABITypeAlign(Ty).value();
16767330f729Sjoerg }
16777330f729Sjoerg 
allowsMemoryAccessForAlignment(LLVMContext & Context,const DataLayout & DL,EVT VT,unsigned AddrSpace,Align Alignment,MachineMemOperand::Flags Flags,bool * Fast) const16787330f729Sjoerg bool TargetLoweringBase::allowsMemoryAccessForAlignment(
16797330f729Sjoerg     LLVMContext &Context, const DataLayout &DL, EVT VT, unsigned AddrSpace,
1680*82d56013Sjoerg     Align Alignment, MachineMemOperand::Flags Flags, bool *Fast) const {
16817330f729Sjoerg   // Check if the specified alignment is sufficient based on the data layout.
16827330f729Sjoerg   // TODO: While using the data layout works in practice, a better solution
16837330f729Sjoerg   // would be to implement this check directly (make this a virtual function).
16847330f729Sjoerg   // For example, the ABI alignment may change based on software platform while
16857330f729Sjoerg   // this function should only be affected by hardware implementation.
16867330f729Sjoerg   Type *Ty = VT.getTypeForEVT(Context);
1687*82d56013Sjoerg   if (Alignment >= DL.getABITypeAlign(Ty)) {
16887330f729Sjoerg     // Assume that an access that meets the ABI-specified alignment is fast.
16897330f729Sjoerg     if (Fast != nullptr)
16907330f729Sjoerg       *Fast = true;
16917330f729Sjoerg     return true;
16927330f729Sjoerg   }
16937330f729Sjoerg 
16947330f729Sjoerg   // This is a misaligned access.
16957330f729Sjoerg   return allowsMisalignedMemoryAccesses(VT, AddrSpace, Alignment, Flags, Fast);
16967330f729Sjoerg }
16977330f729Sjoerg 
allowsMemoryAccessForAlignment(LLVMContext & Context,const DataLayout & DL,EVT VT,const MachineMemOperand & MMO,bool * Fast) const16987330f729Sjoerg bool TargetLoweringBase::allowsMemoryAccessForAlignment(
16997330f729Sjoerg     LLVMContext &Context, const DataLayout &DL, EVT VT,
17007330f729Sjoerg     const MachineMemOperand &MMO, bool *Fast) const {
17017330f729Sjoerg   return allowsMemoryAccessForAlignment(Context, DL, VT, MMO.getAddrSpace(),
1702*82d56013Sjoerg                                         MMO.getAlign(), MMO.getFlags(), Fast);
17037330f729Sjoerg }
17047330f729Sjoerg 
allowsMemoryAccess(LLVMContext & Context,const DataLayout & DL,EVT VT,unsigned AddrSpace,Align Alignment,MachineMemOperand::Flags Flags,bool * Fast) const1705*82d56013Sjoerg bool TargetLoweringBase::allowsMemoryAccess(LLVMContext &Context,
1706*82d56013Sjoerg                                             const DataLayout &DL, EVT VT,
1707*82d56013Sjoerg                                             unsigned AddrSpace, Align Alignment,
1708*82d56013Sjoerg                                             MachineMemOperand::Flags Flags,
1709*82d56013Sjoerg                                             bool *Fast) const {
17107330f729Sjoerg   return allowsMemoryAccessForAlignment(Context, DL, VT, AddrSpace, Alignment,
17117330f729Sjoerg                                         Flags, Fast);
17127330f729Sjoerg }
17137330f729Sjoerg 
allowsMemoryAccess(LLVMContext & Context,const DataLayout & DL,EVT VT,const MachineMemOperand & MMO,bool * Fast) const17147330f729Sjoerg bool TargetLoweringBase::allowsMemoryAccess(LLVMContext &Context,
17157330f729Sjoerg                                             const DataLayout &DL, EVT VT,
17167330f729Sjoerg                                             const MachineMemOperand &MMO,
17177330f729Sjoerg                                             bool *Fast) const {
1718*82d56013Sjoerg   return allowsMemoryAccess(Context, DL, VT, MMO.getAddrSpace(), MMO.getAlign(),
1719*82d56013Sjoerg                             MMO.getFlags(), Fast);
17207330f729Sjoerg }
17217330f729Sjoerg 
allowsMemoryAccess(LLVMContext & Context,const DataLayout & DL,LLT Ty,const MachineMemOperand & MMO,bool * Fast) const1722*82d56013Sjoerg bool TargetLoweringBase::allowsMemoryAccess(LLVMContext &Context,
1723*82d56013Sjoerg                                             const DataLayout &DL, LLT Ty,
1724*82d56013Sjoerg                                             const MachineMemOperand &MMO,
1725*82d56013Sjoerg                                             bool *Fast) const {
1726*82d56013Sjoerg   return allowsMemoryAccess(Context, DL, getMVTForLLT(Ty), MMO.getAddrSpace(),
1727*82d56013Sjoerg                             MMO.getAlign(), MMO.getFlags(), Fast);
17287330f729Sjoerg }
17297330f729Sjoerg 
17307330f729Sjoerg //===----------------------------------------------------------------------===//
17317330f729Sjoerg //  TargetTransformInfo Helpers
17327330f729Sjoerg //===----------------------------------------------------------------------===//
17337330f729Sjoerg 
InstructionOpcodeToISD(unsigned Opcode) const17347330f729Sjoerg int TargetLoweringBase::InstructionOpcodeToISD(unsigned Opcode) const {
17357330f729Sjoerg   enum InstructionOpcodes {
17367330f729Sjoerg #define HANDLE_INST(NUM, OPCODE, CLASS) OPCODE = NUM,
17377330f729Sjoerg #define LAST_OTHER_INST(NUM) InstructionOpcodesCount = NUM
17387330f729Sjoerg #include "llvm/IR/Instruction.def"
17397330f729Sjoerg   };
17407330f729Sjoerg   switch (static_cast<InstructionOpcodes>(Opcode)) {
17417330f729Sjoerg   case Ret:            return 0;
17427330f729Sjoerg   case Br:             return 0;
17437330f729Sjoerg   case Switch:         return 0;
17447330f729Sjoerg   case IndirectBr:     return 0;
17457330f729Sjoerg   case Invoke:         return 0;
17467330f729Sjoerg   case CallBr:         return 0;
17477330f729Sjoerg   case Resume:         return 0;
17487330f729Sjoerg   case Unreachable:    return 0;
17497330f729Sjoerg   case CleanupRet:     return 0;
17507330f729Sjoerg   case CatchRet:       return 0;
17517330f729Sjoerg   case CatchPad:       return 0;
17527330f729Sjoerg   case CatchSwitch:    return 0;
17537330f729Sjoerg   case CleanupPad:     return 0;
17547330f729Sjoerg   case FNeg:           return ISD::FNEG;
17557330f729Sjoerg   case Add:            return ISD::ADD;
17567330f729Sjoerg   case FAdd:           return ISD::FADD;
17577330f729Sjoerg   case Sub:            return ISD::SUB;
17587330f729Sjoerg   case FSub:           return ISD::FSUB;
17597330f729Sjoerg   case Mul:            return ISD::MUL;
17607330f729Sjoerg   case FMul:           return ISD::FMUL;
17617330f729Sjoerg   case UDiv:           return ISD::UDIV;
17627330f729Sjoerg   case SDiv:           return ISD::SDIV;
17637330f729Sjoerg   case FDiv:           return ISD::FDIV;
17647330f729Sjoerg   case URem:           return ISD::UREM;
17657330f729Sjoerg   case SRem:           return ISD::SREM;
17667330f729Sjoerg   case FRem:           return ISD::FREM;
17677330f729Sjoerg   case Shl:            return ISD::SHL;
17687330f729Sjoerg   case LShr:           return ISD::SRL;
17697330f729Sjoerg   case AShr:           return ISD::SRA;
17707330f729Sjoerg   case And:            return ISD::AND;
17717330f729Sjoerg   case Or:             return ISD::OR;
17727330f729Sjoerg   case Xor:            return ISD::XOR;
17737330f729Sjoerg   case Alloca:         return 0;
17747330f729Sjoerg   case Load:           return ISD::LOAD;
17757330f729Sjoerg   case Store:          return ISD::STORE;
17767330f729Sjoerg   case GetElementPtr:  return 0;
17777330f729Sjoerg   case Fence:          return 0;
17787330f729Sjoerg   case AtomicCmpXchg:  return 0;
17797330f729Sjoerg   case AtomicRMW:      return 0;
17807330f729Sjoerg   case Trunc:          return ISD::TRUNCATE;
17817330f729Sjoerg   case ZExt:           return ISD::ZERO_EXTEND;
17827330f729Sjoerg   case SExt:           return ISD::SIGN_EXTEND;
17837330f729Sjoerg   case FPToUI:         return ISD::FP_TO_UINT;
17847330f729Sjoerg   case FPToSI:         return ISD::FP_TO_SINT;
17857330f729Sjoerg   case UIToFP:         return ISD::UINT_TO_FP;
17867330f729Sjoerg   case SIToFP:         return ISD::SINT_TO_FP;
17877330f729Sjoerg   case FPTrunc:        return ISD::FP_ROUND;
17887330f729Sjoerg   case FPExt:          return ISD::FP_EXTEND;
17897330f729Sjoerg   case PtrToInt:       return ISD::BITCAST;
17907330f729Sjoerg   case IntToPtr:       return ISD::BITCAST;
17917330f729Sjoerg   case BitCast:        return ISD::BITCAST;
17927330f729Sjoerg   case AddrSpaceCast:  return ISD::ADDRSPACECAST;
17937330f729Sjoerg   case ICmp:           return ISD::SETCC;
17947330f729Sjoerg   case FCmp:           return ISD::SETCC;
17957330f729Sjoerg   case PHI:            return 0;
17967330f729Sjoerg   case Call:           return 0;
17977330f729Sjoerg   case Select:         return ISD::SELECT;
17987330f729Sjoerg   case UserOp1:        return 0;
17997330f729Sjoerg   case UserOp2:        return 0;
18007330f729Sjoerg   case VAArg:          return 0;
18017330f729Sjoerg   case ExtractElement: return ISD::EXTRACT_VECTOR_ELT;
18027330f729Sjoerg   case InsertElement:  return ISD::INSERT_VECTOR_ELT;
18037330f729Sjoerg   case ShuffleVector:  return ISD::VECTOR_SHUFFLE;
18047330f729Sjoerg   case ExtractValue:   return ISD::MERGE_VALUES;
18057330f729Sjoerg   case InsertValue:    return ISD::MERGE_VALUES;
18067330f729Sjoerg   case LandingPad:     return 0;
1807*82d56013Sjoerg   case Freeze:         return ISD::FREEZE;
18087330f729Sjoerg   }
18097330f729Sjoerg 
18107330f729Sjoerg   llvm_unreachable("Unknown instruction type encountered!");
18117330f729Sjoerg }
18127330f729Sjoerg 
1813*82d56013Sjoerg std::pair<InstructionCost, MVT>
getTypeLegalizationCost(const DataLayout & DL,Type * Ty) const18147330f729Sjoerg TargetLoweringBase::getTypeLegalizationCost(const DataLayout &DL,
18157330f729Sjoerg                                             Type *Ty) const {
18167330f729Sjoerg   LLVMContext &C = Ty->getContext();
18177330f729Sjoerg   EVT MTy = getValueType(DL, Ty);
18187330f729Sjoerg 
1819*82d56013Sjoerg   InstructionCost Cost = 1;
18207330f729Sjoerg   // We keep legalizing the type until we find a legal kind. We assume that
18217330f729Sjoerg   // the only operation that costs anything is the split. After splitting
18227330f729Sjoerg   // we need to handle two types.
18237330f729Sjoerg   while (true) {
18247330f729Sjoerg     LegalizeKind LK = getTypeConversion(C, MTy);
18257330f729Sjoerg 
18267330f729Sjoerg     if (LK.first == TypeLegal)
18277330f729Sjoerg       return std::make_pair(Cost, MTy.getSimpleVT());
18287330f729Sjoerg 
18297330f729Sjoerg     if (LK.first == TypeSplitVector || LK.first == TypeExpandInteger)
18307330f729Sjoerg       Cost *= 2;
18317330f729Sjoerg 
18327330f729Sjoerg     // Do not loop with f128 type.
18337330f729Sjoerg     if (MTy == LK.second)
18347330f729Sjoerg       return std::make_pair(Cost, MTy.getSimpleVT());
18357330f729Sjoerg 
18367330f729Sjoerg     // Keep legalizing the type.
18377330f729Sjoerg     MTy = LK.second;
18387330f729Sjoerg   }
18397330f729Sjoerg }
18407330f729Sjoerg 
getDefaultSafeStackPointerLocation(IRBuilder<> & IRB,bool UseTLS) const18417330f729Sjoerg Value *TargetLoweringBase::getDefaultSafeStackPointerLocation(IRBuilder<> &IRB,
18427330f729Sjoerg                                                               bool UseTLS) const {
18437330f729Sjoerg   // compiler-rt provides a variable with a magic name.  Targets that do not
18447330f729Sjoerg   // link with compiler-rt may also provide such a variable.
18457330f729Sjoerg   Module *M = IRB.GetInsertBlock()->getParent()->getParent();
18467330f729Sjoerg   const char *UnsafeStackPtrVar = "__safestack_unsafe_stack_ptr";
18477330f729Sjoerg   auto UnsafeStackPtr =
18487330f729Sjoerg       dyn_cast_or_null<GlobalVariable>(M->getNamedValue(UnsafeStackPtrVar));
18497330f729Sjoerg 
18507330f729Sjoerg   Type *StackPtrTy = Type::getInt8PtrTy(M->getContext());
18517330f729Sjoerg 
18527330f729Sjoerg   if (!UnsafeStackPtr) {
18537330f729Sjoerg     auto TLSModel = UseTLS ?
18547330f729Sjoerg         GlobalValue::InitialExecTLSModel :
18557330f729Sjoerg         GlobalValue::NotThreadLocal;
18567330f729Sjoerg     // The global variable is not defined yet, define it ourselves.
18577330f729Sjoerg     // We use the initial-exec TLS model because we do not support the
18587330f729Sjoerg     // variable living anywhere other than in the main executable.
18597330f729Sjoerg     UnsafeStackPtr = new GlobalVariable(
18607330f729Sjoerg         *M, StackPtrTy, false, GlobalValue::ExternalLinkage, nullptr,
18617330f729Sjoerg         UnsafeStackPtrVar, nullptr, TLSModel);
18627330f729Sjoerg   } else {
18637330f729Sjoerg     // The variable exists, check its type and attributes.
18647330f729Sjoerg     if (UnsafeStackPtr->getValueType() != StackPtrTy)
18657330f729Sjoerg       report_fatal_error(Twine(UnsafeStackPtrVar) + " must have void* type");
18667330f729Sjoerg     if (UseTLS != UnsafeStackPtr->isThreadLocal())
18677330f729Sjoerg       report_fatal_error(Twine(UnsafeStackPtrVar) + " must " +
18687330f729Sjoerg                          (UseTLS ? "" : "not ") + "be thread-local");
18697330f729Sjoerg   }
18707330f729Sjoerg   return UnsafeStackPtr;
18717330f729Sjoerg }
18727330f729Sjoerg 
getSafeStackPointerLocation(IRBuilder<> & IRB) const18737330f729Sjoerg Value *TargetLoweringBase::getSafeStackPointerLocation(IRBuilder<> &IRB) const {
18747330f729Sjoerg   if (!TM.getTargetTriple().isAndroid())
18757330f729Sjoerg     return getDefaultSafeStackPointerLocation(IRB, true);
18767330f729Sjoerg 
18777330f729Sjoerg   // Android provides a libc function to retrieve the address of the current
18787330f729Sjoerg   // thread's unsafe stack pointer.
18797330f729Sjoerg   Module *M = IRB.GetInsertBlock()->getParent()->getParent();
18807330f729Sjoerg   Type *StackPtrTy = Type::getInt8PtrTy(M->getContext());
18817330f729Sjoerg   FunctionCallee Fn = M->getOrInsertFunction("__safestack_pointer_address",
18827330f729Sjoerg                                              StackPtrTy->getPointerTo(0));
18837330f729Sjoerg   return IRB.CreateCall(Fn);
18847330f729Sjoerg }
18857330f729Sjoerg 
18867330f729Sjoerg //===----------------------------------------------------------------------===//
18877330f729Sjoerg //  Loop Strength Reduction hooks
18887330f729Sjoerg //===----------------------------------------------------------------------===//
18897330f729Sjoerg 
18907330f729Sjoerg /// isLegalAddressingMode - Return true if the addressing mode represented
18917330f729Sjoerg /// 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) const18927330f729Sjoerg bool TargetLoweringBase::isLegalAddressingMode(const DataLayout &DL,
18937330f729Sjoerg                                                const AddrMode &AM, Type *Ty,
18947330f729Sjoerg                                                unsigned AS, Instruction *I) const {
18957330f729Sjoerg   // The default implementation of this implements a conservative RISCy, r+r and
18967330f729Sjoerg   // r+i addr mode.
18977330f729Sjoerg 
18987330f729Sjoerg   // Allows a sign-extended 16-bit immediate field.
18997330f729Sjoerg   if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
19007330f729Sjoerg     return false;
19017330f729Sjoerg 
19027330f729Sjoerg   // No global is ever allowed as a base.
19037330f729Sjoerg   if (AM.BaseGV)
19047330f729Sjoerg     return false;
19057330f729Sjoerg 
19067330f729Sjoerg   // Only support r+r,
19077330f729Sjoerg   switch (AM.Scale) {
19087330f729Sjoerg   case 0:  // "r+i" or just "i", depending on HasBaseReg.
19097330f729Sjoerg     break;
19107330f729Sjoerg   case 1:
19117330f729Sjoerg     if (AM.HasBaseReg && AM.BaseOffs)  // "r+r+i" is not allowed.
19127330f729Sjoerg       return false;
19137330f729Sjoerg     // Otherwise we have r+r or r+i.
19147330f729Sjoerg     break;
19157330f729Sjoerg   case 2:
19167330f729Sjoerg     if (AM.HasBaseReg || AM.BaseOffs)  // 2*r+r  or  2*r+i is not allowed.
19177330f729Sjoerg       return false;
19187330f729Sjoerg     // Allow 2*r as r+r.
19197330f729Sjoerg     break;
19207330f729Sjoerg   default: // Don't allow n * r
19217330f729Sjoerg     return false;
19227330f729Sjoerg   }
19237330f729Sjoerg 
19247330f729Sjoerg   return true;
19257330f729Sjoerg }
19267330f729Sjoerg 
19277330f729Sjoerg //===----------------------------------------------------------------------===//
19287330f729Sjoerg //  Stack Protector
19297330f729Sjoerg //===----------------------------------------------------------------------===//
19307330f729Sjoerg 
19317330f729Sjoerg // For OpenBSD return its special guard variable. Otherwise return nullptr,
19327330f729Sjoerg // so that SelectionDAG handle SSP.
getIRStackGuard(IRBuilder<> & IRB) const19337330f729Sjoerg Value *TargetLoweringBase::getIRStackGuard(IRBuilder<> &IRB) const {
19347330f729Sjoerg   if (getTargetMachine().getTargetTriple().isOSOpenBSD()) {
19357330f729Sjoerg     Module &M = *IRB.GetInsertBlock()->getParent()->getParent();
19367330f729Sjoerg     PointerType *PtrTy = Type::getInt8PtrTy(M.getContext());
1937*82d56013Sjoerg     Constant *C = M.getOrInsertGlobal("__guard_local", PtrTy);
1938*82d56013Sjoerg     if (GlobalVariable *G = dyn_cast_or_null<GlobalVariable>(C))
1939*82d56013Sjoerg       G->setVisibility(GlobalValue::HiddenVisibility);
1940*82d56013Sjoerg     return C;
19417330f729Sjoerg   }
19427330f729Sjoerg   return nullptr;
19437330f729Sjoerg }
19447330f729Sjoerg 
19457330f729Sjoerg // Currently only support "standard" __stack_chk_guard.
19467330f729Sjoerg // TODO: add LOAD_STACK_GUARD support.
insertSSPDeclarations(Module & M) const19477330f729Sjoerg void TargetLoweringBase::insertSSPDeclarations(Module &M) const {
1948*82d56013Sjoerg   if (!M.getNamedValue("__stack_chk_guard")) {
1949*82d56013Sjoerg     auto *GV = new GlobalVariable(M, Type::getInt8PtrTy(M.getContext()), false,
1950*82d56013Sjoerg                                   GlobalVariable::ExternalLinkage, nullptr,
1951*82d56013Sjoerg                                   "__stack_chk_guard");
1952*82d56013Sjoerg     if (TM.getRelocationModel() == Reloc::Static &&
1953*82d56013Sjoerg         !TM.getTargetTriple().isWindowsGNUEnvironment())
1954*82d56013Sjoerg       GV->setDSOLocal(true);
1955*82d56013Sjoerg   }
19567330f729Sjoerg }
19577330f729Sjoerg 
19587330f729Sjoerg // Currently only support "standard" __stack_chk_guard.
19597330f729Sjoerg // TODO: add LOAD_STACK_GUARD support.
getSDagStackGuard(const Module & M) const19607330f729Sjoerg Value *TargetLoweringBase::getSDagStackGuard(const Module &M) const {
19617330f729Sjoerg   return M.getNamedValue("__stack_chk_guard");
19627330f729Sjoerg }
19637330f729Sjoerg 
getSSPStackGuardCheck(const Module & M) const19647330f729Sjoerg Function *TargetLoweringBase::getSSPStackGuardCheck(const Module &M) const {
19657330f729Sjoerg   return nullptr;
19667330f729Sjoerg }
19677330f729Sjoerg 
getMinimumJumpTableEntries() const19687330f729Sjoerg unsigned TargetLoweringBase::getMinimumJumpTableEntries() const {
19697330f729Sjoerg   return MinimumJumpTableEntries;
19707330f729Sjoerg }
19717330f729Sjoerg 
setMinimumJumpTableEntries(unsigned Val)19727330f729Sjoerg void TargetLoweringBase::setMinimumJumpTableEntries(unsigned Val) {
19737330f729Sjoerg   MinimumJumpTableEntries = Val;
19747330f729Sjoerg }
19757330f729Sjoerg 
getMinimumJumpTableDensity(bool OptForSize) const19767330f729Sjoerg unsigned TargetLoweringBase::getMinimumJumpTableDensity(bool OptForSize) const {
19777330f729Sjoerg   return OptForSize ? OptsizeJumpTableDensity : JumpTableDensity;
19787330f729Sjoerg }
19797330f729Sjoerg 
getMaximumJumpTableSize() const19807330f729Sjoerg unsigned TargetLoweringBase::getMaximumJumpTableSize() const {
19817330f729Sjoerg   return MaximumJumpTableSize;
19827330f729Sjoerg }
19837330f729Sjoerg 
setMaximumJumpTableSize(unsigned Val)19847330f729Sjoerg void TargetLoweringBase::setMaximumJumpTableSize(unsigned Val) {
19857330f729Sjoerg   MaximumJumpTableSize = Val;
19867330f729Sjoerg }
19877330f729Sjoerg 
isJumpTableRelative() const1988*82d56013Sjoerg bool TargetLoweringBase::isJumpTableRelative() const {
1989*82d56013Sjoerg   return getTargetMachine().isPositionIndependent();
1990*82d56013Sjoerg }
1991*82d56013Sjoerg 
19927330f729Sjoerg //===----------------------------------------------------------------------===//
19937330f729Sjoerg //  Reciprocal Estimates
19947330f729Sjoerg //===----------------------------------------------------------------------===//
19957330f729Sjoerg 
19967330f729Sjoerg /// Get the reciprocal estimate attribute string for a function that will
19977330f729Sjoerg /// override the target defaults.
getRecipEstimateForFunc(MachineFunction & MF)19987330f729Sjoerg static StringRef getRecipEstimateForFunc(MachineFunction &MF) {
19997330f729Sjoerg   const Function &F = MF.getFunction();
20007330f729Sjoerg   return F.getFnAttribute("reciprocal-estimates").getValueAsString();
20017330f729Sjoerg }
20027330f729Sjoerg 
20037330f729Sjoerg /// Construct a string for the given reciprocal operation of the given type.
20047330f729Sjoerg /// This string should match the corresponding option to the front-end's
20057330f729Sjoerg /// "-mrecip" flag assuming those strings have been passed through in an
20067330f729Sjoerg /// attribute string. For example, "vec-divf" for a division of a vXf32.
getReciprocalOpName(bool IsSqrt,EVT VT)20077330f729Sjoerg static std::string getReciprocalOpName(bool IsSqrt, EVT VT) {
20087330f729Sjoerg   std::string Name = VT.isVector() ? "vec-" : "";
20097330f729Sjoerg 
20107330f729Sjoerg   Name += IsSqrt ? "sqrt" : "div";
20117330f729Sjoerg 
20127330f729Sjoerg   // TODO: Handle "half" or other float types?
20137330f729Sjoerg   if (VT.getScalarType() == MVT::f64) {
20147330f729Sjoerg     Name += "d";
20157330f729Sjoerg   } else {
20167330f729Sjoerg     assert(VT.getScalarType() == MVT::f32 &&
20177330f729Sjoerg            "Unexpected FP type for reciprocal estimate");
20187330f729Sjoerg     Name += "f";
20197330f729Sjoerg   }
20207330f729Sjoerg 
20217330f729Sjoerg   return Name;
20227330f729Sjoerg }
20237330f729Sjoerg 
20247330f729Sjoerg /// Return the character position and value (a single numeric character) of a
20257330f729Sjoerg /// customized refinement operation in the input string if it exists. Return
20267330f729Sjoerg /// false if there is no customized refinement step count.
parseRefinementStep(StringRef In,size_t & Position,uint8_t & Value)20277330f729Sjoerg static bool parseRefinementStep(StringRef In, size_t &Position,
20287330f729Sjoerg                                 uint8_t &Value) {
20297330f729Sjoerg   const char RefStepToken = ':';
20307330f729Sjoerg   Position = In.find(RefStepToken);
20317330f729Sjoerg   if (Position == StringRef::npos)
20327330f729Sjoerg     return false;
20337330f729Sjoerg 
20347330f729Sjoerg   StringRef RefStepString = In.substr(Position + 1);
20357330f729Sjoerg   // Allow exactly one numeric character for the additional refinement
20367330f729Sjoerg   // step parameter.
20377330f729Sjoerg   if (RefStepString.size() == 1) {
20387330f729Sjoerg     char RefStepChar = RefStepString[0];
2039*82d56013Sjoerg     if (isDigit(RefStepChar)) {
20407330f729Sjoerg       Value = RefStepChar - '0';
20417330f729Sjoerg       return true;
20427330f729Sjoerg     }
20437330f729Sjoerg   }
20447330f729Sjoerg   report_fatal_error("Invalid refinement step for -recip.");
20457330f729Sjoerg }
20467330f729Sjoerg 
20477330f729Sjoerg /// For the input attribute string, return one of the ReciprocalEstimate enum
20487330f729Sjoerg /// status values (enabled, disabled, or not specified) for this operation on
20497330f729Sjoerg /// the specified data type.
getOpEnabled(bool IsSqrt,EVT VT,StringRef Override)20507330f729Sjoerg static int getOpEnabled(bool IsSqrt, EVT VT, StringRef Override) {
20517330f729Sjoerg   if (Override.empty())
20527330f729Sjoerg     return TargetLoweringBase::ReciprocalEstimate::Unspecified;
20537330f729Sjoerg 
20547330f729Sjoerg   SmallVector<StringRef, 4> OverrideVector;
20557330f729Sjoerg   Override.split(OverrideVector, ',');
20567330f729Sjoerg   unsigned NumArgs = OverrideVector.size();
20577330f729Sjoerg 
20587330f729Sjoerg   // Check if "all", "none", or "default" was specified.
20597330f729Sjoerg   if (NumArgs == 1) {
20607330f729Sjoerg     // Look for an optional setting of the number of refinement steps needed
20617330f729Sjoerg     // for this type of reciprocal operation.
20627330f729Sjoerg     size_t RefPos;
20637330f729Sjoerg     uint8_t RefSteps;
20647330f729Sjoerg     if (parseRefinementStep(Override, RefPos, RefSteps)) {
20657330f729Sjoerg       // Split the string for further processing.
20667330f729Sjoerg       Override = Override.substr(0, RefPos);
20677330f729Sjoerg     }
20687330f729Sjoerg 
20697330f729Sjoerg     // All reciprocal types are enabled.
20707330f729Sjoerg     if (Override == "all")
20717330f729Sjoerg       return TargetLoweringBase::ReciprocalEstimate::Enabled;
20727330f729Sjoerg 
20737330f729Sjoerg     // All reciprocal types are disabled.
20747330f729Sjoerg     if (Override == "none")
20757330f729Sjoerg       return TargetLoweringBase::ReciprocalEstimate::Disabled;
20767330f729Sjoerg 
20777330f729Sjoerg     // Target defaults for enablement are used.
20787330f729Sjoerg     if (Override == "default")
20797330f729Sjoerg       return TargetLoweringBase::ReciprocalEstimate::Unspecified;
20807330f729Sjoerg   }
20817330f729Sjoerg 
20827330f729Sjoerg   // The attribute string may omit the size suffix ('f'/'d').
20837330f729Sjoerg   std::string VTName = getReciprocalOpName(IsSqrt, VT);
20847330f729Sjoerg   std::string VTNameNoSize = VTName;
20857330f729Sjoerg   VTNameNoSize.pop_back();
20867330f729Sjoerg   static const char DisabledPrefix = '!';
20877330f729Sjoerg 
20887330f729Sjoerg   for (StringRef RecipType : OverrideVector) {
20897330f729Sjoerg     size_t RefPos;
20907330f729Sjoerg     uint8_t RefSteps;
20917330f729Sjoerg     if (parseRefinementStep(RecipType, RefPos, RefSteps))
20927330f729Sjoerg       RecipType = RecipType.substr(0, RefPos);
20937330f729Sjoerg 
20947330f729Sjoerg     // Ignore the disablement token for string matching.
20957330f729Sjoerg     bool IsDisabled = RecipType[0] == DisabledPrefix;
20967330f729Sjoerg     if (IsDisabled)
20977330f729Sjoerg       RecipType = RecipType.substr(1);
20987330f729Sjoerg 
20997330f729Sjoerg     if (RecipType.equals(VTName) || RecipType.equals(VTNameNoSize))
21007330f729Sjoerg       return IsDisabled ? TargetLoweringBase::ReciprocalEstimate::Disabled
21017330f729Sjoerg                         : TargetLoweringBase::ReciprocalEstimate::Enabled;
21027330f729Sjoerg   }
21037330f729Sjoerg 
21047330f729Sjoerg   return TargetLoweringBase::ReciprocalEstimate::Unspecified;
21057330f729Sjoerg }
21067330f729Sjoerg 
21077330f729Sjoerg /// For the input attribute string, return the customized refinement step count
21087330f729Sjoerg /// for this operation on the specified data type. If the step count does not
21097330f729Sjoerg /// exist, return the ReciprocalEstimate enum value for unspecified.
getOpRefinementSteps(bool IsSqrt,EVT VT,StringRef Override)21107330f729Sjoerg static int getOpRefinementSteps(bool IsSqrt, EVT VT, StringRef Override) {
21117330f729Sjoerg   if (Override.empty())
21127330f729Sjoerg     return TargetLoweringBase::ReciprocalEstimate::Unspecified;
21137330f729Sjoerg 
21147330f729Sjoerg   SmallVector<StringRef, 4> OverrideVector;
21157330f729Sjoerg   Override.split(OverrideVector, ',');
21167330f729Sjoerg   unsigned NumArgs = OverrideVector.size();
21177330f729Sjoerg 
21187330f729Sjoerg   // Check if "all", "default", or "none" was specified.
21197330f729Sjoerg   if (NumArgs == 1) {
21207330f729Sjoerg     // Look for an optional setting of the number of refinement steps needed
21217330f729Sjoerg     // for this type of reciprocal operation.
21227330f729Sjoerg     size_t RefPos;
21237330f729Sjoerg     uint8_t RefSteps;
21247330f729Sjoerg     if (!parseRefinementStep(Override, RefPos, RefSteps))
21257330f729Sjoerg       return TargetLoweringBase::ReciprocalEstimate::Unspecified;
21267330f729Sjoerg 
21277330f729Sjoerg     // Split the string for further processing.
21287330f729Sjoerg     Override = Override.substr(0, RefPos);
21297330f729Sjoerg     assert(Override != "none" &&
21307330f729Sjoerg            "Disabled reciprocals, but specifed refinement steps?");
21317330f729Sjoerg 
21327330f729Sjoerg     // If this is a general override, return the specified number of steps.
21337330f729Sjoerg     if (Override == "all" || Override == "default")
21347330f729Sjoerg       return RefSteps;
21357330f729Sjoerg   }
21367330f729Sjoerg 
21377330f729Sjoerg   // The attribute string may omit the size suffix ('f'/'d').
21387330f729Sjoerg   std::string VTName = getReciprocalOpName(IsSqrt, VT);
21397330f729Sjoerg   std::string VTNameNoSize = VTName;
21407330f729Sjoerg   VTNameNoSize.pop_back();
21417330f729Sjoerg 
21427330f729Sjoerg   for (StringRef RecipType : OverrideVector) {
21437330f729Sjoerg     size_t RefPos;
21447330f729Sjoerg     uint8_t RefSteps;
21457330f729Sjoerg     if (!parseRefinementStep(RecipType, RefPos, RefSteps))
21467330f729Sjoerg       continue;
21477330f729Sjoerg 
21487330f729Sjoerg     RecipType = RecipType.substr(0, RefPos);
21497330f729Sjoerg     if (RecipType.equals(VTName) || RecipType.equals(VTNameNoSize))
21507330f729Sjoerg       return RefSteps;
21517330f729Sjoerg   }
21527330f729Sjoerg 
21537330f729Sjoerg   return TargetLoweringBase::ReciprocalEstimate::Unspecified;
21547330f729Sjoerg }
21557330f729Sjoerg 
getRecipEstimateSqrtEnabled(EVT VT,MachineFunction & MF) const21567330f729Sjoerg int TargetLoweringBase::getRecipEstimateSqrtEnabled(EVT VT,
21577330f729Sjoerg                                                     MachineFunction &MF) const {
21587330f729Sjoerg   return getOpEnabled(true, VT, getRecipEstimateForFunc(MF));
21597330f729Sjoerg }
21607330f729Sjoerg 
getRecipEstimateDivEnabled(EVT VT,MachineFunction & MF) const21617330f729Sjoerg int TargetLoweringBase::getRecipEstimateDivEnabled(EVT VT,
21627330f729Sjoerg                                                    MachineFunction &MF) const {
21637330f729Sjoerg   return getOpEnabled(false, VT, getRecipEstimateForFunc(MF));
21647330f729Sjoerg }
21657330f729Sjoerg 
getSqrtRefinementSteps(EVT VT,MachineFunction & MF) const21667330f729Sjoerg int TargetLoweringBase::getSqrtRefinementSteps(EVT VT,
21677330f729Sjoerg                                                MachineFunction &MF) const {
21687330f729Sjoerg   return getOpRefinementSteps(true, VT, getRecipEstimateForFunc(MF));
21697330f729Sjoerg }
21707330f729Sjoerg 
getDivRefinementSteps(EVT VT,MachineFunction & MF) const21717330f729Sjoerg int TargetLoweringBase::getDivRefinementSteps(EVT VT,
21727330f729Sjoerg                                               MachineFunction &MF) const {
21737330f729Sjoerg   return getOpRefinementSteps(false, VT, getRecipEstimateForFunc(MF));
21747330f729Sjoerg }
21757330f729Sjoerg 
finalizeLowering(MachineFunction & MF) const21767330f729Sjoerg void TargetLoweringBase::finalizeLowering(MachineFunction &MF) const {
21777330f729Sjoerg   MF.getRegInfo().freezeReservedRegs(MF);
21787330f729Sjoerg }
2179*82d56013Sjoerg 
2180*82d56013Sjoerg MachineMemOperand::Flags
getLoadMemOperandFlags(const LoadInst & LI,const DataLayout & DL) const2181*82d56013Sjoerg TargetLoweringBase::getLoadMemOperandFlags(const LoadInst &LI,
2182*82d56013Sjoerg                                            const DataLayout &DL) const {
2183*82d56013Sjoerg   MachineMemOperand::Flags Flags = MachineMemOperand::MOLoad;
2184*82d56013Sjoerg   if (LI.isVolatile())
2185*82d56013Sjoerg     Flags |= MachineMemOperand::MOVolatile;
2186*82d56013Sjoerg 
2187*82d56013Sjoerg   if (LI.hasMetadata(LLVMContext::MD_nontemporal))
2188*82d56013Sjoerg     Flags |= MachineMemOperand::MONonTemporal;
2189*82d56013Sjoerg 
2190*82d56013Sjoerg   if (LI.hasMetadata(LLVMContext::MD_invariant_load))
2191*82d56013Sjoerg     Flags |= MachineMemOperand::MOInvariant;
2192*82d56013Sjoerg 
2193*82d56013Sjoerg   if (isDereferenceablePointer(LI.getPointerOperand(), LI.getType(), DL))
2194*82d56013Sjoerg     Flags |= MachineMemOperand::MODereferenceable;
2195*82d56013Sjoerg 
2196*82d56013Sjoerg   Flags |= getTargetMMOFlags(LI);
2197*82d56013Sjoerg   return Flags;
2198*82d56013Sjoerg }
2199*82d56013Sjoerg 
2200*82d56013Sjoerg MachineMemOperand::Flags
getStoreMemOperandFlags(const StoreInst & SI,const DataLayout & DL) const2201*82d56013Sjoerg TargetLoweringBase::getStoreMemOperandFlags(const StoreInst &SI,
2202*82d56013Sjoerg                                             const DataLayout &DL) const {
2203*82d56013Sjoerg   MachineMemOperand::Flags Flags = MachineMemOperand::MOStore;
2204*82d56013Sjoerg 
2205*82d56013Sjoerg   if (SI.isVolatile())
2206*82d56013Sjoerg     Flags |= MachineMemOperand::MOVolatile;
2207*82d56013Sjoerg 
2208*82d56013Sjoerg   if (SI.hasMetadata(LLVMContext::MD_nontemporal))
2209*82d56013Sjoerg     Flags |= MachineMemOperand::MONonTemporal;
2210*82d56013Sjoerg 
2211*82d56013Sjoerg   // FIXME: Not preserving dereferenceable
2212*82d56013Sjoerg   Flags |= getTargetMMOFlags(SI);
2213*82d56013Sjoerg   return Flags;
2214*82d56013Sjoerg }
2215*82d56013Sjoerg 
2216*82d56013Sjoerg MachineMemOperand::Flags
getAtomicMemOperandFlags(const Instruction & AI,const DataLayout & DL) const2217*82d56013Sjoerg TargetLoweringBase::getAtomicMemOperandFlags(const Instruction &AI,
2218*82d56013Sjoerg                                              const DataLayout &DL) const {
2219*82d56013Sjoerg   auto Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
2220*82d56013Sjoerg 
2221*82d56013Sjoerg   if (const AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(&AI)) {
2222*82d56013Sjoerg     if (RMW->isVolatile())
2223*82d56013Sjoerg       Flags |= MachineMemOperand::MOVolatile;
2224*82d56013Sjoerg   } else if (const AtomicCmpXchgInst *CmpX = dyn_cast<AtomicCmpXchgInst>(&AI)) {
2225*82d56013Sjoerg     if (CmpX->isVolatile())
2226*82d56013Sjoerg       Flags |= MachineMemOperand::MOVolatile;
2227*82d56013Sjoerg   } else
2228*82d56013Sjoerg     llvm_unreachable("not an atomic instruction");
2229*82d56013Sjoerg 
2230*82d56013Sjoerg   // FIXME: Not preserving dereferenceable
2231*82d56013Sjoerg   Flags |= getTargetMMOFlags(AI);
2232*82d56013Sjoerg   return Flags;
2233*82d56013Sjoerg }
2234*82d56013Sjoerg 
2235*82d56013Sjoerg //===----------------------------------------------------------------------===//
2236*82d56013Sjoerg //  GlobalISel Hooks
2237*82d56013Sjoerg //===----------------------------------------------------------------------===//
2238*82d56013Sjoerg 
shouldLocalize(const MachineInstr & MI,const TargetTransformInfo * TTI) const2239*82d56013Sjoerg bool TargetLoweringBase::shouldLocalize(const MachineInstr &MI,
2240*82d56013Sjoerg                                         const TargetTransformInfo *TTI) const {
2241*82d56013Sjoerg   auto &MF = *MI.getMF();
2242*82d56013Sjoerg   auto &MRI = MF.getRegInfo();
2243*82d56013Sjoerg   // Assuming a spill and reload of a value has a cost of 1 instruction each,
2244*82d56013Sjoerg   // this helper function computes the maximum number of uses we should consider
2245*82d56013Sjoerg   // for remat. E.g. on arm64 global addresses take 2 insts to materialize. We
2246*82d56013Sjoerg   // break even in terms of code size when the original MI has 2 users vs
2247*82d56013Sjoerg   // choosing to potentially spill. Any more than 2 users we we have a net code
2248*82d56013Sjoerg   // size increase. This doesn't take into account register pressure though.
2249*82d56013Sjoerg   auto maxUses = [](unsigned RematCost) {
2250*82d56013Sjoerg     // A cost of 1 means remats are basically free.
2251*82d56013Sjoerg     if (RematCost == 1)
2252*82d56013Sjoerg       return UINT_MAX;
2253*82d56013Sjoerg     if (RematCost == 2)
2254*82d56013Sjoerg       return 2U;
2255*82d56013Sjoerg 
2256*82d56013Sjoerg     // Remat is too expensive, only sink if there's one user.
2257*82d56013Sjoerg     if (RematCost > 2)
2258*82d56013Sjoerg       return 1U;
2259*82d56013Sjoerg     llvm_unreachable("Unexpected remat cost");
2260*82d56013Sjoerg   };
2261*82d56013Sjoerg 
2262*82d56013Sjoerg   // Helper to walk through uses and terminate if we've reached a limit. Saves
2263*82d56013Sjoerg   // us spending time traversing uses if all we want to know is if it's >= min.
2264*82d56013Sjoerg   auto isUsesAtMost = [&](unsigned Reg, unsigned MaxUses) {
2265*82d56013Sjoerg     unsigned NumUses = 0;
2266*82d56013Sjoerg     auto UI = MRI.use_instr_nodbg_begin(Reg), UE = MRI.use_instr_nodbg_end();
2267*82d56013Sjoerg     for (; UI != UE && NumUses < MaxUses; ++UI) {
2268*82d56013Sjoerg       NumUses++;
2269*82d56013Sjoerg     }
2270*82d56013Sjoerg     // If we haven't reached the end yet then there are more than MaxUses users.
2271*82d56013Sjoerg     return UI == UE;
2272*82d56013Sjoerg   };
2273*82d56013Sjoerg 
2274*82d56013Sjoerg   switch (MI.getOpcode()) {
2275*82d56013Sjoerg   default:
2276*82d56013Sjoerg     return false;
2277*82d56013Sjoerg   // Constants-like instructions should be close to their users.
2278*82d56013Sjoerg   // We don't want long live-ranges for them.
2279*82d56013Sjoerg   case TargetOpcode::G_CONSTANT:
2280*82d56013Sjoerg   case TargetOpcode::G_FCONSTANT:
2281*82d56013Sjoerg   case TargetOpcode::G_FRAME_INDEX:
2282*82d56013Sjoerg   case TargetOpcode::G_INTTOPTR:
2283*82d56013Sjoerg     return true;
2284*82d56013Sjoerg   case TargetOpcode::G_GLOBAL_VALUE: {
2285*82d56013Sjoerg     unsigned RematCost = TTI->getGISelRematGlobalCost();
2286*82d56013Sjoerg     Register Reg = MI.getOperand(0).getReg();
2287*82d56013Sjoerg     unsigned MaxUses = maxUses(RematCost);
2288*82d56013Sjoerg     if (MaxUses == UINT_MAX)
2289*82d56013Sjoerg       return true; // Remats are "free" so always localize.
2290*82d56013Sjoerg     bool B = isUsesAtMost(Reg, MaxUses);
2291*82d56013Sjoerg     return B;
2292*82d56013Sjoerg   }
2293*82d56013Sjoerg   }
2294*82d56013Sjoerg }
2295