xref: /src/contrib/llvm-project/llvm/lib/CodeGen/TargetLoweringBase.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1044eb2f6SDimitry Andric //===- TargetLoweringBase.cpp - Implement the TargetLoweringBase class ----===//
24a16efa3SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
64a16efa3SDimitry Andric //
74a16efa3SDimitry Andric //===----------------------------------------------------------------------===//
84a16efa3SDimitry Andric //
94a16efa3SDimitry Andric // This implements the TargetLoweringBase class.
104a16efa3SDimitry Andric //
114a16efa3SDimitry Andric //===----------------------------------------------------------------------===//
124a16efa3SDimitry Andric 
134a16efa3SDimitry Andric #include "llvm/ADT/BitVector.h"
144a16efa3SDimitry Andric #include "llvm/ADT/STLExtras.h"
15044eb2f6SDimitry Andric #include "llvm/ADT/SmallVector.h"
16b915e9e0SDimitry Andric #include "llvm/ADT/StringExtras.h"
17044eb2f6SDimitry Andric #include "llvm/ADT/StringRef.h"
18044eb2f6SDimitry Andric #include "llvm/ADT/Twine.h"
19cfca06d7SDimitry Andric #include "llvm/Analysis/Loads.h"
20cfca06d7SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h"
214a16efa3SDimitry Andric #include "llvm/CodeGen/Analysis.h"
22044eb2f6SDimitry Andric #include "llvm/CodeGen/ISDOpcodes.h"
23044eb2f6SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
244a16efa3SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
254a16efa3SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
26044eb2f6SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
275ca98fd9SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
28044eb2f6SDimitry Andric #include "llvm/CodeGen/MachineMemOperand.h"
29044eb2f6SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
30a303c417SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
31ac9a064cSDimitry Andric #include "llvm/CodeGen/RuntimeLibcallUtil.h"
325ca98fd9SDimitry Andric #include "llvm/CodeGen/StackMaps.h"
33044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetLowering.h"
34044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h"
35044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
36044eb2f6SDimitry Andric #include "llvm/CodeGen/ValueTypes.h"
37ac9a064cSDimitry Andric #include "llvm/CodeGenTypes/MachineValueType.h"
38044eb2f6SDimitry Andric #include "llvm/IR/Attributes.h"
39044eb2f6SDimitry Andric #include "llvm/IR/CallingConv.h"
404a16efa3SDimitry Andric #include "llvm/IR/DataLayout.h"
414a16efa3SDimitry Andric #include "llvm/IR/DerivedTypes.h"
42044eb2f6SDimitry Andric #include "llvm/IR/Function.h"
43044eb2f6SDimitry Andric #include "llvm/IR/GlobalValue.h"
444a16efa3SDimitry Andric #include "llvm/IR/GlobalVariable.h"
45044eb2f6SDimitry Andric #include "llvm/IR/IRBuilder.h"
46044eb2f6SDimitry Andric #include "llvm/IR/Module.h"
47044eb2f6SDimitry Andric #include "llvm/IR/Type.h"
48044eb2f6SDimitry Andric #include "llvm/Support/Casting.h"
494a16efa3SDimitry Andric #include "llvm/Support/CommandLine.h"
50044eb2f6SDimitry Andric #include "llvm/Support/Compiler.h"
514a16efa3SDimitry Andric #include "llvm/Support/ErrorHandling.h"
524a16efa3SDimitry Andric #include "llvm/Support/MathExtras.h"
534a16efa3SDimitry Andric #include "llvm/Target/TargetMachine.h"
54c0981da4SDimitry Andric #include "llvm/Target/TargetOptions.h"
557fa27ce4SDimitry Andric #include "llvm/TargetParser/Triple.h"
56cfca06d7SDimitry Andric #include "llvm/Transforms/Utils/SizeOpts.h"
57044eb2f6SDimitry Andric #include <algorithm>
58044eb2f6SDimitry Andric #include <cassert>
59044eb2f6SDimitry Andric #include <cstdint>
60044eb2f6SDimitry Andric #include <cstring>
61044eb2f6SDimitry Andric #include <iterator>
62044eb2f6SDimitry Andric #include <string>
63044eb2f6SDimitry Andric #include <tuple>
64044eb2f6SDimitry Andric #include <utility>
65044eb2f6SDimitry Andric 
664a16efa3SDimitry Andric using namespace llvm;
674a16efa3SDimitry Andric 
681a82d4c0SDimitry Andric static cl::opt<bool> JumpIsExpensiveOverride(
691a82d4c0SDimitry Andric     "jump-is-expensive", cl::init(false),
701a82d4c0SDimitry Andric     cl::desc("Do not create extra branches to split comparison logic."),
711a82d4c0SDimitry Andric     cl::Hidden);
721a82d4c0SDimitry Andric 
73b915e9e0SDimitry Andric static cl::opt<unsigned> MinimumJumpTableEntries
74b915e9e0SDimitry Andric   ("min-jump-table-entries", cl::init(4), cl::Hidden,
75b915e9e0SDimitry Andric    cl::desc("Set minimum number of entries to use a jump table."));
76b915e9e0SDimitry Andric 
77b915e9e0SDimitry Andric static cl::opt<unsigned> MaximumJumpTableSize
78e6d15924SDimitry Andric   ("max-jump-table-size", cl::init(UINT_MAX), cl::Hidden,
79e6d15924SDimitry Andric    cl::desc("Set maximum size of jump tables."));
80b915e9e0SDimitry Andric 
81a303c417SDimitry Andric /// Minimum jump table density for normal functions.
82a303c417SDimitry Andric static cl::opt<unsigned>
83a303c417SDimitry Andric     JumpTableDensity("jump-table-density", cl::init(10), cl::Hidden,
84a303c417SDimitry Andric                      cl::desc("Minimum density for building a jump table in "
85a303c417SDimitry Andric                               "a normal function"));
86a303c417SDimitry Andric 
87a303c417SDimitry Andric /// Minimum jump table density for -Os or -Oz functions.
88a303c417SDimitry Andric static cl::opt<unsigned> OptsizeJumpTableDensity(
89a303c417SDimitry Andric     "optsize-jump-table-density", cl::init(40), cl::Hidden,
90a303c417SDimitry Andric     cl::desc("Minimum density for building a jump table in "
91a303c417SDimitry Andric              "an optsize function"));
92a303c417SDimitry Andric 
93706b4fc4SDimitry Andric // FIXME: This option is only to test if the strict fp operation processed
94706b4fc4SDimitry Andric // correctly by preventing mutating strict fp operation to normal fp operation
95706b4fc4SDimitry Andric // during development. When the backend supports strict float operation, this
96706b4fc4SDimitry Andric // option will be meaningless.
97706b4fc4SDimitry Andric static cl::opt<bool> DisableStrictNodeMutation("disable-strictnode-mutation",
98706b4fc4SDimitry Andric        cl::desc("Don't mutate strict-float node to a legalize node"),
99706b4fc4SDimitry Andric        cl::init(false), cl::Hidden);
100706b4fc4SDimitry Andric 
101344a3780SDimitry Andric /// GetFPLibCall - Helper to return the right libcall for the given floating
102344a3780SDimitry Andric /// point type, or UNKNOWN_LIBCALL if there is none.
getFPLibCall(EVT VT,RTLIB::Libcall Call_F32,RTLIB::Libcall Call_F64,RTLIB::Libcall Call_F80,RTLIB::Libcall Call_F128,RTLIB::Libcall Call_PPCF128)103344a3780SDimitry Andric RTLIB::Libcall RTLIB::getFPLibCall(EVT VT,
104344a3780SDimitry Andric                                    RTLIB::Libcall Call_F32,
105344a3780SDimitry Andric                                    RTLIB::Libcall Call_F64,
106344a3780SDimitry Andric                                    RTLIB::Libcall Call_F80,
107344a3780SDimitry Andric                                    RTLIB::Libcall Call_F128,
108344a3780SDimitry Andric                                    RTLIB::Libcall Call_PPCF128) {
109344a3780SDimitry Andric   return
110344a3780SDimitry Andric     VT == MVT::f32 ? Call_F32 :
111344a3780SDimitry Andric     VT == MVT::f64 ? Call_F64 :
112344a3780SDimitry Andric     VT == MVT::f80 ? Call_F80 :
113344a3780SDimitry Andric     VT == MVT::f128 ? Call_F128 :
114344a3780SDimitry Andric     VT == MVT::ppcf128 ? Call_PPCF128 :
115344a3780SDimitry Andric     RTLIB::UNKNOWN_LIBCALL;
116344a3780SDimitry Andric }
117344a3780SDimitry Andric 
1184a16efa3SDimitry Andric /// getFPEXT - Return the FPEXT_*_* value for the given types, or
1194a16efa3SDimitry Andric /// UNKNOWN_LIBCALL if there is none.
getFPEXT(EVT OpVT,EVT RetVT)1204a16efa3SDimitry Andric RTLIB::Libcall RTLIB::getFPEXT(EVT OpVT, EVT RetVT) {
1215ca98fd9SDimitry Andric   if (OpVT == MVT::f16) {
1225ca98fd9SDimitry Andric     if (RetVT == MVT::f32)
1235ca98fd9SDimitry Andric       return FPEXT_F16_F32;
124b60736ecSDimitry Andric     if (RetVT == MVT::f64)
125b60736ecSDimitry Andric       return FPEXT_F16_F64;
126c0981da4SDimitry Andric     if (RetVT == MVT::f80)
127c0981da4SDimitry Andric       return FPEXT_F16_F80;
128b60736ecSDimitry Andric     if (RetVT == MVT::f128)
129b60736ecSDimitry Andric       return FPEXT_F16_F128;
1305ca98fd9SDimitry Andric   } else if (OpVT == MVT::f32) {
1314a16efa3SDimitry Andric     if (RetVT == MVT::f64)
1324a16efa3SDimitry Andric       return FPEXT_F32_F64;
1334a16efa3SDimitry Andric     if (RetVT == MVT::f128)
1344a16efa3SDimitry Andric       return FPEXT_F32_F128;
13501095a5dSDimitry Andric     if (RetVT == MVT::ppcf128)
13601095a5dSDimitry Andric       return FPEXT_F32_PPCF128;
1374a16efa3SDimitry Andric   } else if (OpVT == MVT::f64) {
1384a16efa3SDimitry Andric     if (RetVT == MVT::f128)
1394a16efa3SDimitry Andric       return FPEXT_F64_F128;
14001095a5dSDimitry Andric     else if (RetVT == MVT::ppcf128)
14101095a5dSDimitry Andric       return FPEXT_F64_PPCF128;
142eb11fae6SDimitry Andric   } else if (OpVT == MVT::f80) {
143eb11fae6SDimitry Andric     if (RetVT == MVT::f128)
144eb11fae6SDimitry Andric       return FPEXT_F80_F128;
145ac9a064cSDimitry Andric   } else if (OpVT == MVT::bf16) {
146ac9a064cSDimitry Andric     if (RetVT == MVT::f32)
147ac9a064cSDimitry Andric       return FPEXT_BF16_F32;
1484a16efa3SDimitry Andric   }
1494a16efa3SDimitry Andric 
1504a16efa3SDimitry Andric   return UNKNOWN_LIBCALL;
1514a16efa3SDimitry Andric }
1524a16efa3SDimitry Andric 
1534a16efa3SDimitry Andric /// getFPROUND - Return the FPROUND_*_* value for the given types, or
1544a16efa3SDimitry Andric /// UNKNOWN_LIBCALL if there is none.
getFPROUND(EVT OpVT,EVT RetVT)1554a16efa3SDimitry Andric RTLIB::Libcall RTLIB::getFPROUND(EVT OpVT, EVT RetVT) {
1565ca98fd9SDimitry Andric   if (RetVT == MVT::f16) {
1575ca98fd9SDimitry Andric     if (OpVT == MVT::f32)
1585ca98fd9SDimitry Andric       return FPROUND_F32_F16;
1595ca98fd9SDimitry Andric     if (OpVT == MVT::f64)
1605ca98fd9SDimitry Andric       return FPROUND_F64_F16;
1615ca98fd9SDimitry Andric     if (OpVT == MVT::f80)
1625ca98fd9SDimitry Andric       return FPROUND_F80_F16;
1635ca98fd9SDimitry Andric     if (OpVT == MVT::f128)
1645ca98fd9SDimitry Andric       return FPROUND_F128_F16;
1655ca98fd9SDimitry Andric     if (OpVT == MVT::ppcf128)
1665ca98fd9SDimitry Andric       return FPROUND_PPCF128_F16;
167145449b1SDimitry Andric   } else if (RetVT == MVT::bf16) {
168145449b1SDimitry Andric     if (OpVT == MVT::f32)
169145449b1SDimitry Andric       return FPROUND_F32_BF16;
170145449b1SDimitry Andric     if (OpVT == MVT::f64)
171145449b1SDimitry Andric       return FPROUND_F64_BF16;
1725ca98fd9SDimitry Andric   } else if (RetVT == MVT::f32) {
1734a16efa3SDimitry Andric     if (OpVT == MVT::f64)
1744a16efa3SDimitry Andric       return FPROUND_F64_F32;
1754a16efa3SDimitry Andric     if (OpVT == MVT::f80)
1764a16efa3SDimitry Andric       return FPROUND_F80_F32;
1774a16efa3SDimitry Andric     if (OpVT == MVT::f128)
1784a16efa3SDimitry Andric       return FPROUND_F128_F32;
1794a16efa3SDimitry Andric     if (OpVT == MVT::ppcf128)
1804a16efa3SDimitry Andric       return FPROUND_PPCF128_F32;
1814a16efa3SDimitry Andric   } else if (RetVT == MVT::f64) {
1824a16efa3SDimitry Andric     if (OpVT == MVT::f80)
1834a16efa3SDimitry Andric       return FPROUND_F80_F64;
1844a16efa3SDimitry Andric     if (OpVT == MVT::f128)
1854a16efa3SDimitry Andric       return FPROUND_F128_F64;
1864a16efa3SDimitry Andric     if (OpVT == MVT::ppcf128)
1874a16efa3SDimitry Andric       return FPROUND_PPCF128_F64;
188eb11fae6SDimitry Andric   } else if (RetVT == MVT::f80) {
189eb11fae6SDimitry Andric     if (OpVT == MVT::f128)
190eb11fae6SDimitry Andric       return FPROUND_F128_F80;
1914a16efa3SDimitry Andric   }
1924a16efa3SDimitry Andric 
1934a16efa3SDimitry Andric   return UNKNOWN_LIBCALL;
1944a16efa3SDimitry Andric }
1954a16efa3SDimitry Andric 
1964a16efa3SDimitry Andric /// getFPTOSINT - Return the FPTOSINT_*_* value for the given types, or
1974a16efa3SDimitry Andric /// UNKNOWN_LIBCALL if there is none.
getFPTOSINT(EVT OpVT,EVT RetVT)1984a16efa3SDimitry Andric RTLIB::Libcall RTLIB::getFPTOSINT(EVT OpVT, EVT RetVT) {
199b60736ecSDimitry Andric   if (OpVT == MVT::f16) {
200b60736ecSDimitry Andric     if (RetVT == MVT::i32)
201b60736ecSDimitry Andric       return FPTOSINT_F16_I32;
202b60736ecSDimitry Andric     if (RetVT == MVT::i64)
203b60736ecSDimitry Andric       return FPTOSINT_F16_I64;
204b60736ecSDimitry Andric     if (RetVT == MVT::i128)
205b60736ecSDimitry Andric       return FPTOSINT_F16_I128;
206b60736ecSDimitry Andric   } else if (OpVT == MVT::f32) {
2074a16efa3SDimitry Andric     if (RetVT == MVT::i32)
2084a16efa3SDimitry Andric       return FPTOSINT_F32_I32;
2094a16efa3SDimitry Andric     if (RetVT == MVT::i64)
2104a16efa3SDimitry Andric       return FPTOSINT_F32_I64;
2114a16efa3SDimitry Andric     if (RetVT == MVT::i128)
2124a16efa3SDimitry Andric       return FPTOSINT_F32_I128;
2134a16efa3SDimitry Andric   } else if (OpVT == MVT::f64) {
2144a16efa3SDimitry Andric     if (RetVT == MVT::i32)
2154a16efa3SDimitry Andric       return FPTOSINT_F64_I32;
2164a16efa3SDimitry Andric     if (RetVT == MVT::i64)
2174a16efa3SDimitry Andric       return FPTOSINT_F64_I64;
2184a16efa3SDimitry Andric     if (RetVT == MVT::i128)
2194a16efa3SDimitry Andric       return FPTOSINT_F64_I128;
2204a16efa3SDimitry Andric   } else if (OpVT == MVT::f80) {
2214a16efa3SDimitry Andric     if (RetVT == MVT::i32)
2224a16efa3SDimitry Andric       return FPTOSINT_F80_I32;
2234a16efa3SDimitry Andric     if (RetVT == MVT::i64)
2244a16efa3SDimitry Andric       return FPTOSINT_F80_I64;
2254a16efa3SDimitry Andric     if (RetVT == MVT::i128)
2264a16efa3SDimitry Andric       return FPTOSINT_F80_I128;
2274a16efa3SDimitry Andric   } else if (OpVT == MVT::f128) {
2284a16efa3SDimitry Andric     if (RetVT == MVT::i32)
2294a16efa3SDimitry Andric       return FPTOSINT_F128_I32;
2304a16efa3SDimitry Andric     if (RetVT == MVT::i64)
2314a16efa3SDimitry Andric       return FPTOSINT_F128_I64;
2324a16efa3SDimitry Andric     if (RetVT == MVT::i128)
2334a16efa3SDimitry Andric       return FPTOSINT_F128_I128;
2344a16efa3SDimitry Andric   } else if (OpVT == MVT::ppcf128) {
2354a16efa3SDimitry Andric     if (RetVT == MVT::i32)
2364a16efa3SDimitry Andric       return FPTOSINT_PPCF128_I32;
2374a16efa3SDimitry Andric     if (RetVT == MVT::i64)
2384a16efa3SDimitry Andric       return FPTOSINT_PPCF128_I64;
2394a16efa3SDimitry Andric     if (RetVT == MVT::i128)
2404a16efa3SDimitry Andric       return FPTOSINT_PPCF128_I128;
2414a16efa3SDimitry Andric   }
2424a16efa3SDimitry Andric   return UNKNOWN_LIBCALL;
2434a16efa3SDimitry Andric }
2444a16efa3SDimitry Andric 
2454a16efa3SDimitry Andric /// getFPTOUINT - Return the FPTOUINT_*_* value for the given types, or
2464a16efa3SDimitry Andric /// UNKNOWN_LIBCALL if there is none.
getFPTOUINT(EVT OpVT,EVT RetVT)2474a16efa3SDimitry Andric RTLIB::Libcall RTLIB::getFPTOUINT(EVT OpVT, EVT RetVT) {
248b60736ecSDimitry Andric   if (OpVT == MVT::f16) {
249b60736ecSDimitry Andric     if (RetVT == MVT::i32)
250b60736ecSDimitry Andric       return FPTOUINT_F16_I32;
251b60736ecSDimitry Andric     if (RetVT == MVT::i64)
252b60736ecSDimitry Andric       return FPTOUINT_F16_I64;
253b60736ecSDimitry Andric     if (RetVT == MVT::i128)
254b60736ecSDimitry Andric       return FPTOUINT_F16_I128;
255b60736ecSDimitry Andric   } else if (OpVT == MVT::f32) {
2564a16efa3SDimitry Andric     if (RetVT == MVT::i32)
2574a16efa3SDimitry Andric       return FPTOUINT_F32_I32;
2584a16efa3SDimitry Andric     if (RetVT == MVT::i64)
2594a16efa3SDimitry Andric       return FPTOUINT_F32_I64;
2604a16efa3SDimitry Andric     if (RetVT == MVT::i128)
2614a16efa3SDimitry Andric       return FPTOUINT_F32_I128;
2624a16efa3SDimitry Andric   } else if (OpVT == MVT::f64) {
2634a16efa3SDimitry Andric     if (RetVT == MVT::i32)
2644a16efa3SDimitry Andric       return FPTOUINT_F64_I32;
2654a16efa3SDimitry Andric     if (RetVT == MVT::i64)
2664a16efa3SDimitry Andric       return FPTOUINT_F64_I64;
2674a16efa3SDimitry Andric     if (RetVT == MVT::i128)
2684a16efa3SDimitry Andric       return FPTOUINT_F64_I128;
2694a16efa3SDimitry Andric   } else if (OpVT == MVT::f80) {
2704a16efa3SDimitry Andric     if (RetVT == MVT::i32)
2714a16efa3SDimitry Andric       return FPTOUINT_F80_I32;
2724a16efa3SDimitry Andric     if (RetVT == MVT::i64)
2734a16efa3SDimitry Andric       return FPTOUINT_F80_I64;
2744a16efa3SDimitry Andric     if (RetVT == MVT::i128)
2754a16efa3SDimitry Andric       return FPTOUINT_F80_I128;
2764a16efa3SDimitry Andric   } else if (OpVT == MVT::f128) {
2774a16efa3SDimitry Andric     if (RetVT == MVT::i32)
2784a16efa3SDimitry Andric       return FPTOUINT_F128_I32;
2794a16efa3SDimitry Andric     if (RetVT == MVT::i64)
2804a16efa3SDimitry Andric       return FPTOUINT_F128_I64;
2814a16efa3SDimitry Andric     if (RetVT == MVT::i128)
2824a16efa3SDimitry Andric       return FPTOUINT_F128_I128;
2834a16efa3SDimitry Andric   } else if (OpVT == MVT::ppcf128) {
2844a16efa3SDimitry Andric     if (RetVT == MVT::i32)
2854a16efa3SDimitry Andric       return FPTOUINT_PPCF128_I32;
2864a16efa3SDimitry Andric     if (RetVT == MVT::i64)
2874a16efa3SDimitry Andric       return FPTOUINT_PPCF128_I64;
2884a16efa3SDimitry Andric     if (RetVT == MVT::i128)
2894a16efa3SDimitry Andric       return FPTOUINT_PPCF128_I128;
2904a16efa3SDimitry Andric   }
2914a16efa3SDimitry Andric   return UNKNOWN_LIBCALL;
2924a16efa3SDimitry Andric }
2934a16efa3SDimitry Andric 
2944a16efa3SDimitry Andric /// getSINTTOFP - Return the SINTTOFP_*_* value for the given types, or
2954a16efa3SDimitry Andric /// UNKNOWN_LIBCALL if there is none.
getSINTTOFP(EVT OpVT,EVT RetVT)2964a16efa3SDimitry Andric RTLIB::Libcall RTLIB::getSINTTOFP(EVT OpVT, EVT RetVT) {
2974a16efa3SDimitry Andric   if (OpVT == MVT::i32) {
298b60736ecSDimitry Andric     if (RetVT == MVT::f16)
299b60736ecSDimitry Andric       return SINTTOFP_I32_F16;
3004a16efa3SDimitry Andric     if (RetVT == MVT::f32)
3014a16efa3SDimitry Andric       return SINTTOFP_I32_F32;
3024a16efa3SDimitry Andric     if (RetVT == MVT::f64)
3034a16efa3SDimitry Andric       return SINTTOFP_I32_F64;
3044a16efa3SDimitry Andric     if (RetVT == MVT::f80)
3054a16efa3SDimitry Andric       return SINTTOFP_I32_F80;
3064a16efa3SDimitry Andric     if (RetVT == MVT::f128)
3074a16efa3SDimitry Andric       return SINTTOFP_I32_F128;
3084a16efa3SDimitry Andric     if (RetVT == MVT::ppcf128)
3094a16efa3SDimitry Andric       return SINTTOFP_I32_PPCF128;
3104a16efa3SDimitry Andric   } else if (OpVT == MVT::i64) {
311b60736ecSDimitry Andric     if (RetVT == MVT::f16)
312b60736ecSDimitry Andric       return SINTTOFP_I64_F16;
3134a16efa3SDimitry Andric     if (RetVT == MVT::f32)
3144a16efa3SDimitry Andric       return SINTTOFP_I64_F32;
3154a16efa3SDimitry Andric     if (RetVT == MVT::f64)
3164a16efa3SDimitry Andric       return SINTTOFP_I64_F64;
3174a16efa3SDimitry Andric     if (RetVT == MVT::f80)
3184a16efa3SDimitry Andric       return SINTTOFP_I64_F80;
3194a16efa3SDimitry Andric     if (RetVT == MVT::f128)
3204a16efa3SDimitry Andric       return SINTTOFP_I64_F128;
3214a16efa3SDimitry Andric     if (RetVT == MVT::ppcf128)
3224a16efa3SDimitry Andric       return SINTTOFP_I64_PPCF128;
3234a16efa3SDimitry Andric   } else if (OpVT == MVT::i128) {
324b60736ecSDimitry Andric     if (RetVT == MVT::f16)
325b60736ecSDimitry Andric       return SINTTOFP_I128_F16;
3264a16efa3SDimitry Andric     if (RetVT == MVT::f32)
3274a16efa3SDimitry Andric       return SINTTOFP_I128_F32;
3284a16efa3SDimitry Andric     if (RetVT == MVT::f64)
3294a16efa3SDimitry Andric       return SINTTOFP_I128_F64;
3304a16efa3SDimitry Andric     if (RetVT == MVT::f80)
3314a16efa3SDimitry Andric       return SINTTOFP_I128_F80;
3324a16efa3SDimitry Andric     if (RetVT == MVT::f128)
3334a16efa3SDimitry Andric       return SINTTOFP_I128_F128;
3344a16efa3SDimitry Andric     if (RetVT == MVT::ppcf128)
3354a16efa3SDimitry Andric       return SINTTOFP_I128_PPCF128;
3364a16efa3SDimitry Andric   }
3374a16efa3SDimitry Andric   return UNKNOWN_LIBCALL;
3384a16efa3SDimitry Andric }
3394a16efa3SDimitry Andric 
3404a16efa3SDimitry Andric /// getUINTTOFP - Return the UINTTOFP_*_* value for the given types, or
3414a16efa3SDimitry Andric /// UNKNOWN_LIBCALL if there is none.
getUINTTOFP(EVT OpVT,EVT RetVT)3424a16efa3SDimitry Andric RTLIB::Libcall RTLIB::getUINTTOFP(EVT OpVT, EVT RetVT) {
3434a16efa3SDimitry Andric   if (OpVT == MVT::i32) {
344b60736ecSDimitry Andric     if (RetVT == MVT::f16)
345b60736ecSDimitry Andric       return UINTTOFP_I32_F16;
3464a16efa3SDimitry Andric     if (RetVT == MVT::f32)
3474a16efa3SDimitry Andric       return UINTTOFP_I32_F32;
3484a16efa3SDimitry Andric     if (RetVT == MVT::f64)
3494a16efa3SDimitry Andric       return UINTTOFP_I32_F64;
3504a16efa3SDimitry Andric     if (RetVT == MVT::f80)
3514a16efa3SDimitry Andric       return UINTTOFP_I32_F80;
3524a16efa3SDimitry Andric     if (RetVT == MVT::f128)
3534a16efa3SDimitry Andric       return UINTTOFP_I32_F128;
3544a16efa3SDimitry Andric     if (RetVT == MVT::ppcf128)
3554a16efa3SDimitry Andric       return UINTTOFP_I32_PPCF128;
3564a16efa3SDimitry Andric   } else if (OpVT == MVT::i64) {
357b60736ecSDimitry Andric     if (RetVT == MVT::f16)
358b60736ecSDimitry Andric       return UINTTOFP_I64_F16;
3594a16efa3SDimitry Andric     if (RetVT == MVT::f32)
3604a16efa3SDimitry Andric       return UINTTOFP_I64_F32;
3614a16efa3SDimitry Andric     if (RetVT == MVT::f64)
3624a16efa3SDimitry Andric       return UINTTOFP_I64_F64;
3634a16efa3SDimitry Andric     if (RetVT == MVT::f80)
3644a16efa3SDimitry Andric       return UINTTOFP_I64_F80;
3654a16efa3SDimitry Andric     if (RetVT == MVT::f128)
3664a16efa3SDimitry Andric       return UINTTOFP_I64_F128;
3674a16efa3SDimitry Andric     if (RetVT == MVT::ppcf128)
3684a16efa3SDimitry Andric       return UINTTOFP_I64_PPCF128;
3694a16efa3SDimitry Andric   } else if (OpVT == MVT::i128) {
370b60736ecSDimitry Andric     if (RetVT == MVT::f16)
371b60736ecSDimitry Andric       return UINTTOFP_I128_F16;
3724a16efa3SDimitry Andric     if (RetVT == MVT::f32)
3734a16efa3SDimitry Andric       return UINTTOFP_I128_F32;
3744a16efa3SDimitry Andric     if (RetVT == MVT::f64)
3754a16efa3SDimitry Andric       return UINTTOFP_I128_F64;
3764a16efa3SDimitry Andric     if (RetVT == MVT::f80)
3774a16efa3SDimitry Andric       return UINTTOFP_I128_F80;
3784a16efa3SDimitry Andric     if (RetVT == MVT::f128)
3794a16efa3SDimitry Andric       return UINTTOFP_I128_F128;
3804a16efa3SDimitry Andric     if (RetVT == MVT::ppcf128)
3814a16efa3SDimitry Andric       return UINTTOFP_I128_PPCF128;
3824a16efa3SDimitry Andric   }
3834a16efa3SDimitry Andric   return UNKNOWN_LIBCALL;
3844a16efa3SDimitry Andric }
3854a16efa3SDimitry Andric 
getPOWI(EVT RetVT)386344a3780SDimitry Andric RTLIB::Libcall RTLIB::getPOWI(EVT RetVT) {
387344a3780SDimitry Andric   return getFPLibCall(RetVT, POWI_F32, POWI_F64, POWI_F80, POWI_F128,
388344a3780SDimitry Andric                       POWI_PPCF128);
389344a3780SDimitry Andric }
390344a3780SDimitry Andric 
getLDEXP(EVT RetVT)3917fa27ce4SDimitry Andric RTLIB::Libcall RTLIB::getLDEXP(EVT RetVT) {
3927fa27ce4SDimitry Andric   return getFPLibCall(RetVT, LDEXP_F32, LDEXP_F64, LDEXP_F80, LDEXP_F128,
3937fa27ce4SDimitry Andric                       LDEXP_PPCF128);
3947fa27ce4SDimitry Andric }
3957fa27ce4SDimitry Andric 
getFREXP(EVT RetVT)3967fa27ce4SDimitry Andric RTLIB::Libcall RTLIB::getFREXP(EVT RetVT) {
3977fa27ce4SDimitry Andric   return getFPLibCall(RetVT, FREXP_F32, FREXP_F64, FREXP_F80, FREXP_F128,
3987fa27ce4SDimitry Andric                       FREXP_PPCF128);
3997fa27ce4SDimitry Andric }
4007fa27ce4SDimitry Andric 
getOutlineAtomicHelper(const Libcall (& LC)[5][4],AtomicOrdering Order,uint64_t MemSize)401aca2e42cSDimitry Andric RTLIB::Libcall RTLIB::getOutlineAtomicHelper(const Libcall (&LC)[5][4],
402aca2e42cSDimitry Andric                                              AtomicOrdering Order,
403aca2e42cSDimitry Andric                                              uint64_t MemSize) {
404b60736ecSDimitry Andric   unsigned ModeN, ModelN;
405aca2e42cSDimitry Andric   switch (MemSize) {
406aca2e42cSDimitry Andric   case 1:
407b60736ecSDimitry Andric     ModeN = 0;
408b60736ecSDimitry Andric     break;
409aca2e42cSDimitry Andric   case 2:
410b60736ecSDimitry Andric     ModeN = 1;
411b60736ecSDimitry Andric     break;
412aca2e42cSDimitry Andric   case 4:
413b60736ecSDimitry Andric     ModeN = 2;
414b60736ecSDimitry Andric     break;
415aca2e42cSDimitry Andric   case 8:
416b60736ecSDimitry Andric     ModeN = 3;
417b60736ecSDimitry Andric     break;
418aca2e42cSDimitry Andric   case 16:
419b60736ecSDimitry Andric     ModeN = 4;
420b60736ecSDimitry Andric     break;
421b60736ecSDimitry Andric   default:
422aca2e42cSDimitry Andric     return RTLIB::UNKNOWN_LIBCALL;
423b60736ecSDimitry Andric   }
424b60736ecSDimitry Andric 
425b60736ecSDimitry Andric   switch (Order) {
426b60736ecSDimitry Andric   case AtomicOrdering::Monotonic:
427b60736ecSDimitry Andric     ModelN = 0;
428b60736ecSDimitry Andric     break;
429b60736ecSDimitry Andric   case AtomicOrdering::Acquire:
430b60736ecSDimitry Andric     ModelN = 1;
431b60736ecSDimitry Andric     break;
432b60736ecSDimitry Andric   case AtomicOrdering::Release:
433b60736ecSDimitry Andric     ModelN = 2;
434b60736ecSDimitry Andric     break;
435b60736ecSDimitry Andric   case AtomicOrdering::AcquireRelease:
436b60736ecSDimitry Andric   case AtomicOrdering::SequentiallyConsistent:
437b60736ecSDimitry Andric     ModelN = 3;
438b60736ecSDimitry Andric     break;
439b60736ecSDimitry Andric   default:
440b60736ecSDimitry Andric     return UNKNOWN_LIBCALL;
441b60736ecSDimitry Andric   }
442b60736ecSDimitry Andric 
443aca2e42cSDimitry Andric   return LC[ModeN][ModelN];
444aca2e42cSDimitry Andric }
445aca2e42cSDimitry Andric 
getOUTLINE_ATOMIC(unsigned Opc,AtomicOrdering Order,MVT VT)446aca2e42cSDimitry Andric RTLIB::Libcall RTLIB::getOUTLINE_ATOMIC(unsigned Opc, AtomicOrdering Order,
447aca2e42cSDimitry Andric                                         MVT VT) {
448aca2e42cSDimitry Andric   if (!VT.isScalarInteger())
449aca2e42cSDimitry Andric     return UNKNOWN_LIBCALL;
450aca2e42cSDimitry Andric   uint64_t MemSize = VT.getScalarSizeInBits() / 8;
451aca2e42cSDimitry Andric 
452b60736ecSDimitry Andric #define LCALLS(A, B)                                                           \
453b60736ecSDimitry Andric   { A##B##_RELAX, A##B##_ACQ, A##B##_REL, A##B##_ACQ_REL }
454b60736ecSDimitry Andric #define LCALL5(A)                                                              \
455b60736ecSDimitry Andric   LCALLS(A, 1), LCALLS(A, 2), LCALLS(A, 4), LCALLS(A, 8), LCALLS(A, 16)
456b60736ecSDimitry Andric   switch (Opc) {
457b60736ecSDimitry Andric   case ISD::ATOMIC_CMP_SWAP: {
458b60736ecSDimitry Andric     const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_CAS)};
459aca2e42cSDimitry Andric     return getOutlineAtomicHelper(LC, Order, MemSize);
460b60736ecSDimitry Andric   }
461b60736ecSDimitry Andric   case ISD::ATOMIC_SWAP: {
462b60736ecSDimitry Andric     const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_SWP)};
463aca2e42cSDimitry Andric     return getOutlineAtomicHelper(LC, Order, MemSize);
464b60736ecSDimitry Andric   }
465b60736ecSDimitry Andric   case ISD::ATOMIC_LOAD_ADD: {
466b60736ecSDimitry Andric     const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_LDADD)};
467aca2e42cSDimitry Andric     return getOutlineAtomicHelper(LC, Order, MemSize);
468b60736ecSDimitry Andric   }
469b60736ecSDimitry Andric   case ISD::ATOMIC_LOAD_OR: {
470b60736ecSDimitry Andric     const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_LDSET)};
471aca2e42cSDimitry Andric     return getOutlineAtomicHelper(LC, Order, MemSize);
472b60736ecSDimitry Andric   }
473b60736ecSDimitry Andric   case ISD::ATOMIC_LOAD_CLR: {
474b60736ecSDimitry Andric     const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_LDCLR)};
475aca2e42cSDimitry Andric     return getOutlineAtomicHelper(LC, Order, MemSize);
476b60736ecSDimitry Andric   }
477b60736ecSDimitry Andric   case ISD::ATOMIC_LOAD_XOR: {
478b60736ecSDimitry Andric     const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_LDEOR)};
479aca2e42cSDimitry Andric     return getOutlineAtomicHelper(LC, Order, MemSize);
480b60736ecSDimitry Andric   }
481b60736ecSDimitry Andric   default:
482b60736ecSDimitry Andric     return UNKNOWN_LIBCALL;
483b60736ecSDimitry Andric   }
484b60736ecSDimitry Andric #undef LCALLS
485b60736ecSDimitry Andric #undef LCALL5
486b60736ecSDimitry Andric }
487b60736ecSDimitry Andric 
getSYNC(unsigned Opc,MVT VT)48801095a5dSDimitry Andric RTLIB::Libcall RTLIB::getSYNC(unsigned Opc, MVT VT) {
4895a5ac124SDimitry Andric #define OP_TO_LIBCALL(Name, Enum)                                              \
4905a5ac124SDimitry Andric   case Name:                                                                   \
4915a5ac124SDimitry Andric     switch (VT.SimpleTy) {                                                     \
4925a5ac124SDimitry Andric     default:                                                                   \
4935a5ac124SDimitry Andric       return UNKNOWN_LIBCALL;                                                  \
4945a5ac124SDimitry Andric     case MVT::i8:                                                              \
4955a5ac124SDimitry Andric       return Enum##_1;                                                         \
4965a5ac124SDimitry Andric     case MVT::i16:                                                             \
4975a5ac124SDimitry Andric       return Enum##_2;                                                         \
4985a5ac124SDimitry Andric     case MVT::i32:                                                             \
4995a5ac124SDimitry Andric       return Enum##_4;                                                         \
5005a5ac124SDimitry Andric     case MVT::i64:                                                             \
5015a5ac124SDimitry Andric       return Enum##_8;                                                         \
5025a5ac124SDimitry Andric     case MVT::i128:                                                            \
5035a5ac124SDimitry Andric       return Enum##_16;                                                        \
5045a5ac124SDimitry Andric     }
5055a5ac124SDimitry Andric 
5065a5ac124SDimitry Andric   switch (Opc) {
5075a5ac124SDimitry Andric     OP_TO_LIBCALL(ISD::ATOMIC_SWAP, SYNC_LOCK_TEST_AND_SET)
5085a5ac124SDimitry Andric     OP_TO_LIBCALL(ISD::ATOMIC_CMP_SWAP, SYNC_VAL_COMPARE_AND_SWAP)
5095a5ac124SDimitry Andric     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_ADD, SYNC_FETCH_AND_ADD)
5105a5ac124SDimitry Andric     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_SUB, SYNC_FETCH_AND_SUB)
5115a5ac124SDimitry Andric     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_AND, SYNC_FETCH_AND_AND)
5125a5ac124SDimitry Andric     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_OR, SYNC_FETCH_AND_OR)
5135a5ac124SDimitry Andric     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_XOR, SYNC_FETCH_AND_XOR)
5145a5ac124SDimitry Andric     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_NAND, SYNC_FETCH_AND_NAND)
5155a5ac124SDimitry Andric     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_MAX, SYNC_FETCH_AND_MAX)
5165a5ac124SDimitry Andric     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_UMAX, SYNC_FETCH_AND_UMAX)
5175a5ac124SDimitry Andric     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_MIN, SYNC_FETCH_AND_MIN)
5185a5ac124SDimitry Andric     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_UMIN, SYNC_FETCH_AND_UMIN)
5195a5ac124SDimitry Andric   }
5205a5ac124SDimitry Andric 
5215a5ac124SDimitry Andric #undef OP_TO_LIBCALL
5225a5ac124SDimitry Andric 
5235a5ac124SDimitry Andric   return UNKNOWN_LIBCALL;
5245a5ac124SDimitry Andric }
5255a5ac124SDimitry Andric 
getMEMCPY_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)5267c7aba6eSDimitry Andric RTLIB::Libcall RTLIB::getMEMCPY_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize) {
527b915e9e0SDimitry Andric   switch (ElementSize) {
528b915e9e0SDimitry Andric   case 1:
5297c7aba6eSDimitry Andric     return MEMCPY_ELEMENT_UNORDERED_ATOMIC_1;
530b915e9e0SDimitry Andric   case 2:
5317c7aba6eSDimitry Andric     return MEMCPY_ELEMENT_UNORDERED_ATOMIC_2;
532b915e9e0SDimitry Andric   case 4:
5337c7aba6eSDimitry Andric     return MEMCPY_ELEMENT_UNORDERED_ATOMIC_4;
534b915e9e0SDimitry Andric   case 8:
5357c7aba6eSDimitry Andric     return MEMCPY_ELEMENT_UNORDERED_ATOMIC_8;
536b915e9e0SDimitry Andric   case 16:
5377c7aba6eSDimitry Andric     return MEMCPY_ELEMENT_UNORDERED_ATOMIC_16;
538b915e9e0SDimitry Andric   default:
539b915e9e0SDimitry Andric     return UNKNOWN_LIBCALL;
540b915e9e0SDimitry Andric   }
541b915e9e0SDimitry Andric }
542b915e9e0SDimitry Andric 
getMEMMOVE_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)543ca089b24SDimitry Andric RTLIB::Libcall RTLIB::getMEMMOVE_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize) {
544ca089b24SDimitry Andric   switch (ElementSize) {
545ca089b24SDimitry Andric   case 1:
546ca089b24SDimitry Andric     return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_1;
547ca089b24SDimitry Andric   case 2:
548ca089b24SDimitry Andric     return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_2;
549ca089b24SDimitry Andric   case 4:
550ca089b24SDimitry Andric     return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_4;
551ca089b24SDimitry Andric   case 8:
552ca089b24SDimitry Andric     return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_8;
553ca089b24SDimitry Andric   case 16:
554ca089b24SDimitry Andric     return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_16;
555ca089b24SDimitry Andric   default:
556ca089b24SDimitry Andric     return UNKNOWN_LIBCALL;
557ca089b24SDimitry Andric   }
558ca089b24SDimitry Andric }
559ca089b24SDimitry Andric 
getMEMSET_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)560ca089b24SDimitry Andric RTLIB::Libcall RTLIB::getMEMSET_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize) {
561ca089b24SDimitry Andric   switch (ElementSize) {
562ca089b24SDimitry Andric   case 1:
563ca089b24SDimitry Andric     return MEMSET_ELEMENT_UNORDERED_ATOMIC_1;
564ca089b24SDimitry Andric   case 2:
565ca089b24SDimitry Andric     return MEMSET_ELEMENT_UNORDERED_ATOMIC_2;
566ca089b24SDimitry Andric   case 4:
567ca089b24SDimitry Andric     return MEMSET_ELEMENT_UNORDERED_ATOMIC_4;
568ca089b24SDimitry Andric   case 8:
569ca089b24SDimitry Andric     return MEMSET_ELEMENT_UNORDERED_ATOMIC_8;
570ca089b24SDimitry Andric   case 16:
571ca089b24SDimitry Andric     return MEMSET_ELEMENT_UNORDERED_ATOMIC_16;
572ca089b24SDimitry Andric   default:
573ca089b24SDimitry Andric     return UNKNOWN_LIBCALL;
574ca089b24SDimitry Andric   }
575ca089b24SDimitry Andric }
576ca089b24SDimitry Andric 
initCmpLibcallCCs(ISD::CondCode * CmpLibcallCCs)577ac9a064cSDimitry Andric void RTLIB::initCmpLibcallCCs(ISD::CondCode *CmpLibcallCCs) {
578ac9a064cSDimitry Andric   std::fill(CmpLibcallCCs, CmpLibcallCCs + RTLIB::UNKNOWN_LIBCALL,
579ac9a064cSDimitry Andric             ISD::SETCC_INVALID);
580ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::OEQ_F32] = ISD::SETEQ;
581ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::OEQ_F64] = ISD::SETEQ;
582ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::OEQ_F128] = ISD::SETEQ;
583ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::OEQ_PPCF128] = ISD::SETEQ;
584ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::UNE_F32] = ISD::SETNE;
585ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::UNE_F64] = ISD::SETNE;
586ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::UNE_F128] = ISD::SETNE;
587ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::UNE_PPCF128] = ISD::SETNE;
588ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::OGE_F32] = ISD::SETGE;
589ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::OGE_F64] = ISD::SETGE;
590ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::OGE_F128] = ISD::SETGE;
591ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::OGE_PPCF128] = ISD::SETGE;
592ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::OLT_F32] = ISD::SETLT;
593ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::OLT_F64] = ISD::SETLT;
594ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::OLT_F128] = ISD::SETLT;
595ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::OLT_PPCF128] = ISD::SETLT;
596ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::OLE_F32] = ISD::SETLE;
597ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::OLE_F64] = ISD::SETLE;
598ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::OLE_F128] = ISD::SETLE;
599ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::OLE_PPCF128] = ISD::SETLE;
600ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::OGT_F32] = ISD::SETGT;
601ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::OGT_F64] = ISD::SETGT;
602ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::OGT_F128] = ISD::SETGT;
603ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::OGT_PPCF128] = ISD::SETGT;
604ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::UO_F32] = ISD::SETNE;
605ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::UO_F64] = ISD::SETNE;
606ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::UO_F128] = ISD::SETNE;
607ac9a064cSDimitry Andric   CmpLibcallCCs[RTLIB::UO_PPCF128] = ISD::SETNE;
6084a16efa3SDimitry Andric }
6094a16efa3SDimitry Andric 
61067c32a98SDimitry Andric /// NOTE: The TargetMachine owns TLOF.
TargetLoweringBase(const TargetMachine & tm)611ac9a064cSDimitry Andric TargetLoweringBase::TargetLoweringBase(const TargetMachine &tm)
612ac9a064cSDimitry Andric     : TM(tm), Libcalls(TM.getTargetTriple()) {
61359d6cff9SDimitry Andric   initActions();
61459d6cff9SDimitry Andric 
61559d6cff9SDimitry Andric   // Perform these initializations only once.
616f382538dSDimitry Andric   MaxStoresPerMemset = MaxStoresPerMemcpy = MaxStoresPerMemmove =
617f382538dSDimitry Andric       MaxLoadsPerMemcmp = 8;
618eb11fae6SDimitry Andric   MaxGluedStoresPerMemcpy = 0;
619f382538dSDimitry Andric   MaxStoresPerMemsetOptSize = MaxStoresPerMemcpyOptSize =
620f382538dSDimitry Andric       MaxStoresPerMemmoveOptSize = MaxLoadsPerMemcmpOptSize = 4;
6215ca98fd9SDimitry Andric   HasMultipleConditionRegisters = false;
6225ca98fd9SDimitry Andric   HasExtractBitsInsn = false;
6231a82d4c0SDimitry Andric   JumpIsExpensive = JumpIsExpensiveOverride;
62459d6cff9SDimitry Andric   PredictableSelectIsExpensive = false;
62567c32a98SDimitry Andric   EnableExtLdPromotion = false;
62659d6cff9SDimitry Andric   StackPointerRegisterToSaveRestore = 0;
62759d6cff9SDimitry Andric   BooleanContents = UndefinedBooleanContent;
6285ca98fd9SDimitry Andric   BooleanFloatContents = UndefinedBooleanContent;
62959d6cff9SDimitry Andric   BooleanVectorContents = UndefinedBooleanContent;
63059d6cff9SDimitry Andric   SchedPreferenceInfo = Sched::ILP;
63171d5a254SDimitry Andric   GatherAllAliasesMaxDepth = 18;
632706b4fc4SDimitry Andric   IsStrictFPEnabled = DisableStrictNodeMutation;
6336f8fc217SDimitry Andric   MaxBytesForAlignment = 0;
6344df029ccSDimitry Andric   MaxAtomicSizeInBitsSupported = 0;
63501095a5dSDimitry Andric 
6367fa27ce4SDimitry Andric   // Assume that even with libcalls, no target supports wider than 128 bit
6377fa27ce4SDimitry Andric   // division.
6387fa27ce4SDimitry Andric   MaxDivRemBitWidthSupported = 128;
639e3b55780SDimitry Andric 
640e3b55780SDimitry Andric   MaxLargeFPConvertBitWidthSupported = llvm::IntegerType::MAX_INT_BITS;
641e3b55780SDimitry Andric 
64201095a5dSDimitry Andric   MinCmpXchgSizeInBits = 0;
643044eb2f6SDimitry Andric   SupportsUnalignedAtomics = false;
64401095a5dSDimitry Andric 
645ac9a064cSDimitry Andric   RTLIB::initCmpLibcallCCs(CmpLibcallCCs);
64659d6cff9SDimitry Andric }
64759d6cff9SDimitry Andric 
initActions()64859d6cff9SDimitry Andric void TargetLoweringBase::initActions() {
6494a16efa3SDimitry Andric   // All operations default to being supported.
6504a16efa3SDimitry Andric   memset(OpActions, 0, sizeof(OpActions));
6514a16efa3SDimitry Andric   memset(LoadExtActions, 0, sizeof(LoadExtActions));
6524a16efa3SDimitry Andric   memset(TruncStoreActions, 0, sizeof(TruncStoreActions));
6534a16efa3SDimitry Andric   memset(IndexedModeActions, 0, sizeof(IndexedModeActions));
6544a16efa3SDimitry Andric   memset(CondCodeActions, 0, sizeof(CondCodeActions));
65501095a5dSDimitry Andric   std::fill(std::begin(RegClassForVT), std::end(RegClassForVT), nullptr);
65601095a5dSDimitry Andric   std::fill(std::begin(TargetDAGCombineArray),
65701095a5dSDimitry Andric             std::end(TargetDAGCombineArray), 0);
6584a16efa3SDimitry Andric 
659ac9a064cSDimitry Andric   // Let extending atomic loads be unsupported by default.
660ac9a064cSDimitry Andric   for (MVT ValVT : MVT::all_valuetypes())
661ac9a064cSDimitry Andric     for (MVT MemVT : MVT::all_valuetypes())
662ac9a064cSDimitry Andric       setAtomicLoadExtAction({ISD::SEXTLOAD, ISD::ZEXTLOAD}, ValVT, MemVT,
663ac9a064cSDimitry Andric                              Expand);
664ac9a064cSDimitry Andric 
665145449b1SDimitry Andric   // We're somewhat special casing MVT::i2 and MVT::i4. Ideally we want to
666145449b1SDimitry Andric   // remove this and targets should individually set these types if not legal.
667145449b1SDimitry Andric   for (ISD::NodeType NT : enum_seq(ISD::DELETED_NODE, ISD::BUILTIN_OP_END,
668145449b1SDimitry Andric                                    force_iteration_on_noniterable_enum)) {
669145449b1SDimitry Andric     for (MVT VT : {MVT::i2, MVT::i4})
670145449b1SDimitry Andric       OpActions[(unsigned)VT.SimpleTy][NT] = Expand;
671145449b1SDimitry Andric   }
672145449b1SDimitry Andric   for (MVT AVT : MVT::all_valuetypes()) {
673145449b1SDimitry Andric     for (MVT VT : {MVT::i2, MVT::i4, MVT::v128i2, MVT::v64i4}) {
674145449b1SDimitry Andric       setTruncStoreAction(AVT, VT, Expand);
675145449b1SDimitry Andric       setLoadExtAction(ISD::EXTLOAD, AVT, VT, Expand);
676145449b1SDimitry Andric       setLoadExtAction(ISD::ZEXTLOAD, AVT, VT, Expand);
677145449b1SDimitry Andric     }
678145449b1SDimitry Andric   }
679145449b1SDimitry Andric   for (unsigned IM = (unsigned)ISD::PRE_INC;
680145449b1SDimitry Andric        IM != (unsigned)ISD::LAST_INDEXED_MODE; ++IM) {
681145449b1SDimitry Andric     for (MVT VT : {MVT::i2, MVT::i4}) {
682145449b1SDimitry Andric       setIndexedLoadAction(IM, VT, Expand);
683145449b1SDimitry Andric       setIndexedStoreAction(IM, VT, Expand);
684145449b1SDimitry Andric       setIndexedMaskedLoadAction(IM, VT, Expand);
685145449b1SDimitry Andric       setIndexedMaskedStoreAction(IM, VT, Expand);
686145449b1SDimitry Andric     }
687145449b1SDimitry Andric   }
688145449b1SDimitry Andric 
689e6d15924SDimitry Andric   for (MVT VT : MVT::fp_valuetypes()) {
690b60736ecSDimitry Andric     MVT IntVT = MVT::getIntegerVT(VT.getFixedSizeInBits());
691e6d15924SDimitry Andric     if (IntVT.isValid()) {
692e6d15924SDimitry Andric       setOperationAction(ISD::ATOMIC_SWAP, VT, Promote);
693e6d15924SDimitry Andric       AddPromotedToType(ISD::ATOMIC_SWAP, VT, IntVT);
694e6d15924SDimitry Andric     }
695e6d15924SDimitry Andric   }
696e6d15924SDimitry Andric 
6974a16efa3SDimitry Andric   // Set default actions for various operations.
69867c32a98SDimitry Andric   for (MVT VT : MVT::all_valuetypes()) {
6994a16efa3SDimitry Andric     // Default all indexed load / store to expand.
7004a16efa3SDimitry Andric     for (unsigned IM = (unsigned)ISD::PRE_INC;
7014a16efa3SDimitry Andric          IM != (unsigned)ISD::LAST_INDEXED_MODE; ++IM) {
70267c32a98SDimitry Andric       setIndexedLoadAction(IM, VT, Expand);
70367c32a98SDimitry Andric       setIndexedStoreAction(IM, VT, Expand);
704706b4fc4SDimitry Andric       setIndexedMaskedLoadAction(IM, VT, Expand);
705706b4fc4SDimitry Andric       setIndexedMaskedStoreAction(IM, VT, Expand);
7064a16efa3SDimitry Andric     }
7074a16efa3SDimitry Andric 
7085ca98fd9SDimitry Andric     // Most backends expect to see the node which just returns the value loaded.
70967c32a98SDimitry Andric     setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, VT, Expand);
7105ca98fd9SDimitry Andric 
7114a16efa3SDimitry Andric     // These operations default to expand.
712145449b1SDimitry Andric     setOperationAction({ISD::FGETSIGN,       ISD::CONCAT_VECTORS,
713145449b1SDimitry Andric                         ISD::FMINNUM,        ISD::FMAXNUM,
714145449b1SDimitry Andric                         ISD::FMINNUM_IEEE,   ISD::FMAXNUM_IEEE,
715145449b1SDimitry Andric                         ISD::FMINIMUM,       ISD::FMAXIMUM,
716145449b1SDimitry Andric                         ISD::FMAD,           ISD::SMIN,
717145449b1SDimitry Andric                         ISD::SMAX,           ISD::UMIN,
718145449b1SDimitry Andric                         ISD::UMAX,           ISD::ABS,
719145449b1SDimitry Andric                         ISD::FSHL,           ISD::FSHR,
720145449b1SDimitry Andric                         ISD::SADDSAT,        ISD::UADDSAT,
721145449b1SDimitry Andric                         ISD::SSUBSAT,        ISD::USUBSAT,
722145449b1SDimitry Andric                         ISD::SSHLSAT,        ISD::USHLSAT,
723145449b1SDimitry Andric                         ISD::SMULFIX,        ISD::SMULFIXSAT,
724145449b1SDimitry Andric                         ISD::UMULFIX,        ISD::UMULFIXSAT,
725145449b1SDimitry Andric                         ISD::SDIVFIX,        ISD::SDIVFIXSAT,
726145449b1SDimitry Andric                         ISD::UDIVFIX,        ISD::UDIVFIXSAT,
727145449b1SDimitry Andric                         ISD::FP_TO_SINT_SAT, ISD::FP_TO_UINT_SAT,
728145449b1SDimitry Andric                         ISD::IS_FPCLASS},
729145449b1SDimitry Andric                        VT, Expand);
7305a5ac124SDimitry Andric 
7315a5ac124SDimitry Andric     // Overflow operations default to expand
732145449b1SDimitry Andric     setOperationAction({ISD::SADDO, ISD::SSUBO, ISD::UADDO, ISD::USUBO,
733145449b1SDimitry Andric                         ISD::SMULO, ISD::UMULO},
734145449b1SDimitry Andric                        VT, Expand);
735f8af5cf6SDimitry Andric 
7367fa27ce4SDimitry Andric     // Carry-using overflow operations default to expand.
7377fa27ce4SDimitry Andric     setOperationAction({ISD::UADDO_CARRY, ISD::USUBO_CARRY, ISD::SETCCCARRY,
738145449b1SDimitry Andric                         ISD::SADDO_CARRY, ISD::SSUBO_CARRY},
739145449b1SDimitry Andric                        VT, Expand);
740a303c417SDimitry Andric 
741eb11fae6SDimitry Andric     // ADDC/ADDE/SUBC/SUBE default to expand.
742145449b1SDimitry Andric     setOperationAction({ISD::ADDC, ISD::ADDE, ISD::SUBC, ISD::SUBE}, VT,
743145449b1SDimitry Andric                        Expand);
744145449b1SDimitry Andric 
745ac9a064cSDimitry Andric     // [US]CMP default to expand
746ac9a064cSDimitry Andric     setOperationAction({ISD::UCMP, ISD::SCMP}, VT, Expand);
747ac9a064cSDimitry Andric 
748145449b1SDimitry Andric     // Halving adds
749145449b1SDimitry Andric     setOperationAction(
750145449b1SDimitry Andric         {ISD::AVGFLOORS, ISD::AVGFLOORU, ISD::AVGCEILS, ISD::AVGCEILU}, VT,
751145449b1SDimitry Andric         Expand);
752eb11fae6SDimitry Andric 
753344a3780SDimitry Andric     // Absolute difference
754145449b1SDimitry Andric     setOperationAction({ISD::ABDS, ISD::ABDU}, VT, Expand);
755344a3780SDimitry Andric 
75601095a5dSDimitry Andric     // These default to Expand so they will be expanded to CTLZ/CTTZ by default.
757145449b1SDimitry Andric     setOperationAction({ISD::CTLZ_ZERO_UNDEF, ISD::CTTZ_ZERO_UNDEF}, VT,
758145449b1SDimitry Andric                        Expand);
75901095a5dSDimitry Andric 
760145449b1SDimitry Andric     setOperationAction({ISD::BITREVERSE, ISD::PARITY}, VT, Expand);
761dd58ef01SDimitry Andric 
762f8af5cf6SDimitry Andric     // These library functions default to expand.
763b1c73532SDimitry Andric     setOperationAction({ISD::FROUND, ISD::FPOWI, ISD::FLDEXP, ISD::FFREXP}, VT,
764b1c73532SDimitry Andric                        Expand);
765f8af5cf6SDimitry Andric 
766f8af5cf6SDimitry Andric     // These operations default to expand for vector types.
767145449b1SDimitry Andric     if (VT.isVector())
768b1c73532SDimitry Andric       setOperationAction(
769b1c73532SDimitry Andric           {ISD::FCOPYSIGN, ISD::SIGN_EXTEND_INREG, ISD::ANY_EXTEND_VECTOR_INREG,
770b1c73532SDimitry Andric            ISD::SIGN_EXTEND_VECTOR_INREG, ISD::ZERO_EXTEND_VECTOR_INREG,
771ac9a064cSDimitry Andric            ISD::SPLAT_VECTOR, ISD::LRINT, ISD::LLRINT, ISD::FTAN, ISD::FACOS,
772ac9a064cSDimitry Andric            ISD::FASIN, ISD::FATAN, ISD::FCOSH, ISD::FSINH, ISD::FTANH},
773145449b1SDimitry Andric           VT, Expand);
774dd58ef01SDimitry Andric 
775e6d15924SDimitry Andric       // Constrained floating-point operations default to expand.
776cfca06d7SDimitry Andric #define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN)               \
777706b4fc4SDimitry Andric     setOperationAction(ISD::STRICT_##DAGN, VT, Expand);
778706b4fc4SDimitry Andric #include "llvm/IR/ConstrainedOps.def"
779e6d15924SDimitry Andric 
78001095a5dSDimitry Andric     // For most targets @llvm.get.dynamic.area.offset just returns 0.
781dd58ef01SDimitry Andric     setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, VT, Expand);
782e6d15924SDimitry Andric 
783e6d15924SDimitry Andric     // Vector reduction default to expand.
784145449b1SDimitry Andric     setOperationAction(
785145449b1SDimitry Andric         {ISD::VECREDUCE_FADD, ISD::VECREDUCE_FMUL, ISD::VECREDUCE_ADD,
786145449b1SDimitry Andric          ISD::VECREDUCE_MUL, ISD::VECREDUCE_AND, ISD::VECREDUCE_OR,
787145449b1SDimitry Andric          ISD::VECREDUCE_XOR, ISD::VECREDUCE_SMAX, ISD::VECREDUCE_SMIN,
788145449b1SDimitry Andric          ISD::VECREDUCE_UMAX, ISD::VECREDUCE_UMIN, ISD::VECREDUCE_FMAX,
7897fa27ce4SDimitry Andric          ISD::VECREDUCE_FMIN, ISD::VECREDUCE_FMAXIMUM, ISD::VECREDUCE_FMINIMUM,
7907fa27ce4SDimitry Andric          ISD::VECREDUCE_SEQ_FADD, ISD::VECREDUCE_SEQ_FMUL},
791145449b1SDimitry Andric         VT, Expand);
792344a3780SDimitry Andric 
793344a3780SDimitry Andric     // Named vector shuffles default to expand.
794344a3780SDimitry Andric     setOperationAction(ISD::VECTOR_SPLICE, VT, Expand);
795e3b55780SDimitry Andric 
796ac9a064cSDimitry Andric     // Only some target support this vector operation. Most need to expand it.
797ac9a064cSDimitry Andric     setOperationAction(ISD::VECTOR_COMPRESS, VT, Expand);
798ac9a064cSDimitry Andric 
7997fa27ce4SDimitry Andric     // VP operations default to expand.
8007fa27ce4SDimitry Andric #define BEGIN_REGISTER_VP_SDNODE(SDOPC, ...)                                   \
8017fa27ce4SDimitry Andric     setOperationAction(ISD::SDOPC, VT, Expand);
8027fa27ce4SDimitry Andric #include "llvm/IR/VPIntrinsics.def"
8037fa27ce4SDimitry Andric 
8047fa27ce4SDimitry Andric     // FP environment operations default to expand.
8057fa27ce4SDimitry Andric     setOperationAction(ISD::GET_FPENV, VT, Expand);
8067fa27ce4SDimitry Andric     setOperationAction(ISD::SET_FPENV, VT, Expand);
8077fa27ce4SDimitry Andric     setOperationAction(ISD::RESET_FPENV, VT, Expand);
8084a16efa3SDimitry Andric   }
8094a16efa3SDimitry Andric 
8104a16efa3SDimitry Andric   // Most targets ignore the @llvm.prefetch intrinsic.
8114a16efa3SDimitry Andric   setOperationAction(ISD::PREFETCH, MVT::Other, Expand);
8124a16efa3SDimitry Andric 
813dd58ef01SDimitry Andric   // Most targets also ignore the @llvm.readcyclecounter intrinsic.
814dd58ef01SDimitry Andric   setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Expand);
815dd58ef01SDimitry Andric 
816ac9a064cSDimitry Andric   // Most targets also ignore the @llvm.readsteadycounter intrinsic.
817ac9a064cSDimitry Andric   setOperationAction(ISD::READSTEADYCOUNTER, MVT::i64, Expand);
818ac9a064cSDimitry Andric 
8194a16efa3SDimitry Andric   // ConstantFP nodes default to expand.  Targets can either change this to
8204a16efa3SDimitry Andric   // Legal, in which case all fp constants are legal, or use isFPImmLegal()
8214a16efa3SDimitry Andric   // to optimize expansions for certain constants.
822145449b1SDimitry Andric   setOperationAction(ISD::ConstantFP,
823b1c73532SDimitry Andric                      {MVT::bf16, MVT::f16, MVT::f32, MVT::f64, MVT::f80, MVT::f128},
824145449b1SDimitry Andric                      Expand);
8254a16efa3SDimitry Andric 
8264a16efa3SDimitry Andric   // These library functions default to expand.
827ac9a064cSDimitry Andric   setOperationAction({ISD::FCBRT,      ISD::FLOG,    ISD::FLOG2,  ISD::FLOG10,
828ac9a064cSDimitry Andric                       ISD::FEXP,       ISD::FEXP2,   ISD::FEXP10, ISD::FFLOOR,
829ac9a064cSDimitry Andric                       ISD::FNEARBYINT, ISD::FCEIL,   ISD::FRINT,  ISD::FTRUNC,
830ac9a064cSDimitry Andric                       ISD::LROUND,     ISD::LLROUND, ISD::LRINT,  ISD::LLRINT,
831ac9a064cSDimitry Andric                       ISD::FROUNDEVEN, ISD::FTAN,    ISD::FACOS,  ISD::FASIN,
832ac9a064cSDimitry Andric                       ISD::FATAN,      ISD::FCOSH,   ISD::FSINH,  ISD::FTANH},
833145449b1SDimitry Andric                      {MVT::f32, MVT::f64, MVT::f128}, Expand);
8344a16efa3SDimitry Andric 
835ac9a064cSDimitry Andric   setOperationAction({ISD::FTAN, ISD::FACOS, ISD::FASIN, ISD::FATAN, ISD::FCOSH,
836ac9a064cSDimitry Andric                       ISD::FSINH, ISD::FTANH},
837ac9a064cSDimitry Andric                      MVT::f16, Promote);
8384a16efa3SDimitry Andric   // Default ISD::TRAP to expand (which turns it into abort).
8394a16efa3SDimitry Andric   setOperationAction(ISD::TRAP, MVT::Other, Expand);
8404a16efa3SDimitry Andric 
8414a16efa3SDimitry Andric   // On most systems, DEBUGTRAP and TRAP have no difference. The "Expand"
8424a16efa3SDimitry Andric   // here is to inform DAG Legalizer to replace DEBUGTRAP with TRAP.
8434a16efa3SDimitry Andric   setOperationAction(ISD::DEBUGTRAP, MVT::Other, Expand);
844b60736ecSDimitry Andric 
845b60736ecSDimitry Andric   setOperationAction(ISD::UBSANTRAP, MVT::Other, Expand);
8467fa27ce4SDimitry Andric 
8477fa27ce4SDimitry Andric   setOperationAction(ISD::GET_FPENV_MEM, MVT::Other, Expand);
8487fa27ce4SDimitry Andric   setOperationAction(ISD::SET_FPENV_MEM, MVT::Other, Expand);
849b1c73532SDimitry Andric 
850b1c73532SDimitry Andric   for (MVT VT : {MVT::i8, MVT::i16, MVT::i32, MVT::i64}) {
851b1c73532SDimitry Andric     setOperationAction(ISD::GET_FPMODE, VT, Expand);
852b1c73532SDimitry Andric     setOperationAction(ISD::SET_FPMODE, VT, Expand);
853b1c73532SDimitry Andric   }
854b1c73532SDimitry Andric   setOperationAction(ISD::RESET_FPMODE, MVT::Other, Expand);
855ac9a064cSDimitry Andric 
856ac9a064cSDimitry Andric   // This one by default will call __clear_cache unless the target
857ac9a064cSDimitry Andric   // wants something different.
858ac9a064cSDimitry Andric   setOperationAction(ISD::CLEAR_CACHE, MVT::Other, LibCall);
8594a16efa3SDimitry Andric }
8604a16efa3SDimitry Andric 
getScalarShiftAmountTy(const DataLayout & DL,EVT) const861ee8648bdSDimitry Andric MVT TargetLoweringBase::getScalarShiftAmountTy(const DataLayout &DL,
862ee8648bdSDimitry Andric                                                EVT) const {
863e6d15924SDimitry Andric   return MVT::getIntegerVT(DL.getPointerSizeInBits(0));
864f8af5cf6SDimitry Andric }
865f8af5cf6SDimitry Andric 
getShiftAmountTy(EVT LHSTy,const DataLayout & DL) const866ac9a064cSDimitry Andric EVT TargetLoweringBase::getShiftAmountTy(EVT LHSTy,
867ac9a064cSDimitry Andric                                          const DataLayout &DL) const {
8684a16efa3SDimitry Andric   assert(LHSTy.isInteger() && "Shift amount is not an integer type!");
8694a16efa3SDimitry Andric   if (LHSTy.isVector())
8704a16efa3SDimitry Andric     return LHSTy;
871ac9a064cSDimitry Andric   MVT ShiftVT = getScalarShiftAmountTy(DL, LHSTy);
872c0981da4SDimitry Andric   // If any possible shift value won't fit in the prefered type, just use
873c0981da4SDimitry Andric   // something safe. Assume it will be legalized when the shift is expanded.
874c0981da4SDimitry Andric   if (ShiftVT.getSizeInBits() < Log2_32_Ceil(LHSTy.getSizeInBits()))
875c0981da4SDimitry Andric     ShiftVT = MVT::i32;
876c0981da4SDimitry Andric   assert(ShiftVT.getSizeInBits() >= Log2_32_Ceil(LHSTy.getSizeInBits()) &&
877c0981da4SDimitry Andric          "ShiftVT is still too small!");
878c0981da4SDimitry Andric   return ShiftVT;
8794a16efa3SDimitry Andric }
8804a16efa3SDimitry Andric 
canOpTrap(unsigned Op,EVT VT) const8814a16efa3SDimitry Andric bool TargetLoweringBase::canOpTrap(unsigned Op, EVT VT) const {
8824a16efa3SDimitry Andric   assert(isTypeLegal(VT));
8834a16efa3SDimitry Andric   switch (Op) {
8844a16efa3SDimitry Andric   default:
8854a16efa3SDimitry Andric     return false;
8864a16efa3SDimitry Andric   case ISD::SDIV:
8874a16efa3SDimitry Andric   case ISD::UDIV:
8884a16efa3SDimitry Andric   case ISD::SREM:
8894a16efa3SDimitry Andric   case ISD::UREM:
8904a16efa3SDimitry Andric     return true;
8914a16efa3SDimitry Andric   }
8924a16efa3SDimitry Andric }
8934a16efa3SDimitry Andric 
isFreeAddrSpaceCast(unsigned SrcAS,unsigned DestAS) const894b60736ecSDimitry Andric bool TargetLoweringBase::isFreeAddrSpaceCast(unsigned SrcAS,
895b60736ecSDimitry Andric                                              unsigned DestAS) const {
896b60736ecSDimitry Andric   return TM.isNoopAddrSpaceCast(SrcAS, DestAS);
897b60736ecSDimitry Andric }
898b60736ecSDimitry Andric 
getBitWidthForCttzElements(Type * RetTy,ElementCount EC,bool ZeroIsPoison,const ConstantRange * VScaleRange) const899ac9a064cSDimitry Andric unsigned TargetLoweringBase::getBitWidthForCttzElements(
900ac9a064cSDimitry Andric     Type *RetTy, ElementCount EC, bool ZeroIsPoison,
901ac9a064cSDimitry Andric     const ConstantRange *VScaleRange) const {
902ac9a064cSDimitry Andric   // Find the smallest "sensible" element type to use for the expansion.
903ac9a064cSDimitry Andric   ConstantRange CR(APInt(64, EC.getKnownMinValue()));
904ac9a064cSDimitry Andric   if (EC.isScalable())
905ac9a064cSDimitry Andric     CR = CR.umul_sat(*VScaleRange);
906ac9a064cSDimitry Andric 
907ac9a064cSDimitry Andric   if (ZeroIsPoison)
908ac9a064cSDimitry Andric     CR = CR.subtract(APInt(64, 1));
909ac9a064cSDimitry Andric 
910ac9a064cSDimitry Andric   unsigned EltWidth = RetTy->getScalarSizeInBits();
911ac9a064cSDimitry Andric   EltWidth = std::min(EltWidth, (unsigned)CR.getActiveBits());
912ac9a064cSDimitry Andric   EltWidth = std::max(llvm::bit_ceil(EltWidth), (unsigned)8);
913ac9a064cSDimitry Andric 
914ac9a064cSDimitry Andric   return EltWidth;
915ac9a064cSDimitry Andric }
916ac9a064cSDimitry Andric 
setJumpIsExpensive(bool isExpensive)9171a82d4c0SDimitry Andric void TargetLoweringBase::setJumpIsExpensive(bool isExpensive) {
9181a82d4c0SDimitry Andric   // If the command-line option was specified, ignore this request.
9191a82d4c0SDimitry Andric   if (!JumpIsExpensiveOverride.getNumOccurrences())
9201a82d4c0SDimitry Andric     JumpIsExpensive = isExpensive;
9211a82d4c0SDimitry Andric }
9221a82d4c0SDimitry Andric 
9235a5ac124SDimitry Andric TargetLoweringBase::LegalizeKind
getTypeConversion(LLVMContext & Context,EVT VT) const9245a5ac124SDimitry Andric TargetLoweringBase::getTypeConversion(LLVMContext &Context, EVT VT) const {
9255a5ac124SDimitry Andric   // If this is a simple type, use the ComputeRegisterProp mechanism.
9265a5ac124SDimitry Andric   if (VT.isSimple()) {
9275a5ac124SDimitry Andric     MVT SVT = VT.getSimpleVT();
928e3b55780SDimitry Andric     assert((unsigned)SVT.SimpleTy < std::size(TransformToType));
9295a5ac124SDimitry Andric     MVT NVT = TransformToType[SVT.SimpleTy];
9305a5ac124SDimitry Andric     LegalizeTypeAction LA = ValueTypeActions.getTypeAction(SVT);
9315a5ac124SDimitry Andric 
9325a5ac124SDimitry Andric     assert((LA == TypeLegal || LA == TypeSoftenFloat ||
933cfca06d7SDimitry Andric             LA == TypeSoftPromoteHalf ||
9341d5ae102SDimitry Andric             (NVT.isVector() ||
9351d5ae102SDimitry Andric              ValueTypeActions.getTypeAction(NVT) != TypePromoteInteger)) &&
9365a5ac124SDimitry Andric            "Promote may not follow Expand or Promote");
9375a5ac124SDimitry Andric 
9385a5ac124SDimitry Andric     if (LA == TypeSplitVector)
939b60736ecSDimitry Andric       return LegalizeKind(LA, EVT(SVT).getHalfNumVectorElementsVT(Context));
9405a5ac124SDimitry Andric     if (LA == TypeScalarizeVector)
9415a5ac124SDimitry Andric       return LegalizeKind(LA, SVT.getVectorElementType());
9425a5ac124SDimitry Andric     return LegalizeKind(LA, NVT);
9435a5ac124SDimitry Andric   }
9445a5ac124SDimitry Andric 
9455a5ac124SDimitry Andric   // Handle Extended Scalar Types.
9465a5ac124SDimitry Andric   if (!VT.isVector()) {
9475a5ac124SDimitry Andric     assert(VT.isInteger() && "Float types must be simple");
9485a5ac124SDimitry Andric     unsigned BitSize = VT.getSizeInBits();
9495a5ac124SDimitry Andric     // First promote to a power-of-two size, then expand if necessary.
9505a5ac124SDimitry Andric     if (BitSize < 8 || !isPowerOf2_32(BitSize)) {
9515a5ac124SDimitry Andric       EVT NVT = VT.getRoundIntegerType(Context);
9525a5ac124SDimitry Andric       assert(NVT != VT && "Unable to round integer VT");
9535a5ac124SDimitry Andric       LegalizeKind NextStep = getTypeConversion(Context, NVT);
9545a5ac124SDimitry Andric       // Avoid multi-step promotion.
9555a5ac124SDimitry Andric       if (NextStep.first == TypePromoteInteger)
9565a5ac124SDimitry Andric         return NextStep;
9575a5ac124SDimitry Andric       // Return rounded integer type.
9585a5ac124SDimitry Andric       return LegalizeKind(TypePromoteInteger, NVT);
9595a5ac124SDimitry Andric     }
9605a5ac124SDimitry Andric 
9615a5ac124SDimitry Andric     return LegalizeKind(TypeExpandInteger,
9625a5ac124SDimitry Andric                         EVT::getIntegerVT(Context, VT.getSizeInBits() / 2));
9635a5ac124SDimitry Andric   }
9645a5ac124SDimitry Andric 
9655a5ac124SDimitry Andric   // Handle vector types.
966cfca06d7SDimitry Andric   ElementCount NumElts = VT.getVectorElementCount();
9675a5ac124SDimitry Andric   EVT EltVT = VT.getVectorElementType();
9685a5ac124SDimitry Andric 
9695a5ac124SDimitry Andric   // Vectors with only one element are always scalarized.
970b60736ecSDimitry Andric   if (NumElts.isScalar())
9715a5ac124SDimitry Andric     return LegalizeKind(TypeScalarizeVector, EltVT);
9725a5ac124SDimitry Andric 
9735a5ac124SDimitry Andric   // Try to widen vector elements until the element type is a power of two and
9745a5ac124SDimitry Andric   // promote it to a legal type later on, for example:
9755a5ac124SDimitry Andric   // <3 x i8> -> <4 x i8> -> <4 x i32>
9765a5ac124SDimitry Andric   if (EltVT.isInteger()) {
9775a5ac124SDimitry Andric     // Vectors with a number of elements that is not a power of two are always
9785a5ac124SDimitry Andric     // widened, for example <3 x i8> -> <4 x i8>.
9795a5ac124SDimitry Andric     if (!VT.isPow2VectorType()) {
980b60736ecSDimitry Andric       NumElts = NumElts.coefficientNextPowerOf2();
9815a5ac124SDimitry Andric       EVT NVT = EVT::getVectorVT(Context, EltVT, NumElts);
9825a5ac124SDimitry Andric       return LegalizeKind(TypeWidenVector, NVT);
9835a5ac124SDimitry Andric     }
9845a5ac124SDimitry Andric 
9855a5ac124SDimitry Andric     // Examine the element type.
9865a5ac124SDimitry Andric     LegalizeKind LK = getTypeConversion(Context, EltVT);
9875a5ac124SDimitry Andric 
9885a5ac124SDimitry Andric     // If type is to be expanded, split the vector.
9895a5ac124SDimitry Andric     //  <4 x i140> -> <2 x i140>
990344a3780SDimitry Andric     if (LK.first == TypeExpandInteger) {
991344a3780SDimitry Andric       if (VT.getVectorElementCount().isScalable())
992344a3780SDimitry Andric         return LegalizeKind(TypeScalarizeScalableVector, EltVT);
9935a5ac124SDimitry Andric       return LegalizeKind(TypeSplitVector,
994b60736ecSDimitry Andric                           VT.getHalfNumVectorElementsVT(Context));
995344a3780SDimitry Andric     }
9965a5ac124SDimitry Andric 
9975a5ac124SDimitry Andric     // Promote the integer element types until a legal vector type is found
9985a5ac124SDimitry Andric     // or until the element integer type is too big. If a legal type was not
9995a5ac124SDimitry Andric     // found, fallback to the usual mechanism of widening/splitting the
10005a5ac124SDimitry Andric     // vector.
10015a5ac124SDimitry Andric     EVT OldEltVT = EltVT;
1002044eb2f6SDimitry Andric     while (true) {
10035a5ac124SDimitry Andric       // Increase the bitwidth of the element to the next pow-of-two
10045a5ac124SDimitry Andric       // (which is greater than 8 bits).
10055a5ac124SDimitry Andric       EltVT = EVT::getIntegerVT(Context, 1 + EltVT.getSizeInBits())
10065a5ac124SDimitry Andric                   .getRoundIntegerType(Context);
10075a5ac124SDimitry Andric 
10085a5ac124SDimitry Andric       // Stop trying when getting a non-simple element type.
10095a5ac124SDimitry Andric       // Note that vector elements may be greater than legal vector element
10105a5ac124SDimitry Andric       // types. Example: X86 XMM registers hold 64bit element on 32bit
10115a5ac124SDimitry Andric       // systems.
10125a5ac124SDimitry Andric       if (!EltVT.isSimple())
10135a5ac124SDimitry Andric         break;
10145a5ac124SDimitry Andric 
10155a5ac124SDimitry Andric       // Build a new vector type and check if it is legal.
10165a5ac124SDimitry Andric       MVT NVT = MVT::getVectorVT(EltVT.getSimpleVT(), NumElts);
10175a5ac124SDimitry Andric       // Found a legal promoted vector type.
10185a5ac124SDimitry Andric       if (NVT != MVT() && ValueTypeActions.getTypeAction(NVT) == TypeLegal)
10195a5ac124SDimitry Andric         return LegalizeKind(TypePromoteInteger,
10205a5ac124SDimitry Andric                             EVT::getVectorVT(Context, EltVT, NumElts));
10215a5ac124SDimitry Andric     }
10225a5ac124SDimitry Andric 
10235a5ac124SDimitry Andric     // Reset the type to the unexpanded type if we did not find a legal vector
10245a5ac124SDimitry Andric     // type with a promoted vector element type.
10255a5ac124SDimitry Andric     EltVT = OldEltVT;
10265a5ac124SDimitry Andric   }
10275a5ac124SDimitry Andric 
10285a5ac124SDimitry Andric   // Try to widen the vector until a legal type is found.
10295a5ac124SDimitry Andric   // If there is no wider legal type, split the vector.
1030044eb2f6SDimitry Andric   while (true) {
10315a5ac124SDimitry Andric     // Round up to the next power of 2.
1032b60736ecSDimitry Andric     NumElts = NumElts.coefficientNextPowerOf2();
10335a5ac124SDimitry Andric 
10345a5ac124SDimitry Andric     // If there is no simple vector type with this many elements then there
10355a5ac124SDimitry Andric     // cannot be a larger legal vector type.  Note that this assumes that
10365a5ac124SDimitry Andric     // there are no skipped intermediate vector types in the simple types.
10375a5ac124SDimitry Andric     if (!EltVT.isSimple())
10385a5ac124SDimitry Andric       break;
10395a5ac124SDimitry Andric     MVT LargerVector = MVT::getVectorVT(EltVT.getSimpleVT(), NumElts);
10405a5ac124SDimitry Andric     if (LargerVector == MVT())
10415a5ac124SDimitry Andric       break;
10425a5ac124SDimitry Andric 
10435a5ac124SDimitry Andric     // If this type is legal then widen the vector.
10445a5ac124SDimitry Andric     if (ValueTypeActions.getTypeAction(LargerVector) == TypeLegal)
10455a5ac124SDimitry Andric       return LegalizeKind(TypeWidenVector, LargerVector);
10465a5ac124SDimitry Andric   }
10475a5ac124SDimitry Andric 
10485a5ac124SDimitry Andric   // Widen odd vectors to next power of two.
10495a5ac124SDimitry Andric   if (!VT.isPow2VectorType()) {
10505a5ac124SDimitry Andric     EVT NVT = VT.getPow2VectorType(Context);
10515a5ac124SDimitry Andric     return LegalizeKind(TypeWidenVector, NVT);
10525a5ac124SDimitry Andric   }
10535a5ac124SDimitry Andric 
1054344a3780SDimitry Andric   if (VT.getVectorElementCount() == ElementCount::getScalable(1))
1055344a3780SDimitry Andric     return LegalizeKind(TypeScalarizeScalableVector, EltVT);
1056344a3780SDimitry Andric 
10575a5ac124SDimitry Andric   // Vectors with illegal element types are expanded.
1058b60736ecSDimitry Andric   EVT NVT = EVT::getVectorVT(Context, EltVT,
1059b60736ecSDimitry Andric                              VT.getVectorElementCount().divideCoefficientBy(2));
10605a5ac124SDimitry Andric   return LegalizeKind(TypeSplitVector, NVT);
10615a5ac124SDimitry Andric }
10624a16efa3SDimitry Andric 
getVectorTypeBreakdownMVT(MVT VT,MVT & IntermediateVT,unsigned & NumIntermediates,MVT & RegisterVT,TargetLoweringBase * TLI)10634a16efa3SDimitry Andric static unsigned getVectorTypeBreakdownMVT(MVT VT, MVT &IntermediateVT,
10644a16efa3SDimitry Andric                                           unsigned &NumIntermediates,
10654a16efa3SDimitry Andric                                           MVT &RegisterVT,
10664a16efa3SDimitry Andric                                           TargetLoweringBase *TLI) {
10674a16efa3SDimitry Andric   // Figure out the right, legal destination reg to copy into.
1068cfca06d7SDimitry Andric   ElementCount EC = VT.getVectorElementCount();
10694a16efa3SDimitry Andric   MVT EltTy = VT.getVectorElementType();
10704a16efa3SDimitry Andric 
10714a16efa3SDimitry Andric   unsigned NumVectorRegs = 1;
10724a16efa3SDimitry Andric 
1073cfca06d7SDimitry Andric   // Scalable vectors cannot be scalarized, so splitting or widening is
1074cfca06d7SDimitry Andric   // required.
1075b60736ecSDimitry Andric   if (VT.isScalableVector() && !isPowerOf2_32(EC.getKnownMinValue()))
1076cfca06d7SDimitry Andric     llvm_unreachable(
1077cfca06d7SDimitry Andric         "Splitting or widening of non-power-of-2 MVTs is not implemented.");
1078cfca06d7SDimitry Andric 
1079cfca06d7SDimitry Andric   // FIXME: We don't support non-power-of-2-sized vectors for now.
1080cfca06d7SDimitry Andric   // Ideally we could break down into LHS/RHS like LegalizeDAG does.
1081b60736ecSDimitry Andric   if (!isPowerOf2_32(EC.getKnownMinValue())) {
1082cfca06d7SDimitry Andric     // Split EC to unit size (scalable property is preserved).
1083b60736ecSDimitry Andric     NumVectorRegs = EC.getKnownMinValue();
1084b60736ecSDimitry Andric     EC = ElementCount::getFixed(1);
10854a16efa3SDimitry Andric   }
10864a16efa3SDimitry Andric 
1087cfca06d7SDimitry Andric   // Divide the input until we get to a supported size. This will
1088cfca06d7SDimitry Andric   // always end up with an EC that represent a scalar or a scalable
1089cfca06d7SDimitry Andric   // scalar.
1090b60736ecSDimitry Andric   while (EC.getKnownMinValue() > 1 &&
1091b60736ecSDimitry Andric          !TLI->isTypeLegal(MVT::getVectorVT(EltTy, EC))) {
1092b60736ecSDimitry Andric     EC = EC.divideCoefficientBy(2);
10934a16efa3SDimitry Andric     NumVectorRegs <<= 1;
10944a16efa3SDimitry Andric   }
10954a16efa3SDimitry Andric 
10964a16efa3SDimitry Andric   NumIntermediates = NumVectorRegs;
10974a16efa3SDimitry Andric 
1098cfca06d7SDimitry Andric   MVT NewVT = MVT::getVectorVT(EltTy, EC);
10994a16efa3SDimitry Andric   if (!TLI->isTypeLegal(NewVT))
11004a16efa3SDimitry Andric     NewVT = EltTy;
11014a16efa3SDimitry Andric   IntermediateVT = NewVT;
11024a16efa3SDimitry Andric 
1103b60736ecSDimitry Andric   unsigned LaneSizeInBits = NewVT.getScalarSizeInBits();
11044a16efa3SDimitry Andric 
11054a16efa3SDimitry Andric   // Convert sizes such as i33 to i64.
11067fa27ce4SDimitry Andric   LaneSizeInBits = llvm::bit_ceil(LaneSizeInBits);
11074a16efa3SDimitry Andric 
11084a16efa3SDimitry Andric   MVT DestVT = TLI->getRegisterType(NewVT);
11094a16efa3SDimitry Andric   RegisterVT = DestVT;
11104a16efa3SDimitry Andric   if (EVT(DestVT).bitsLT(NewVT))    // Value is expanded, e.g. i64 -> i16.
1111b60736ecSDimitry Andric     return NumVectorRegs * (LaneSizeInBits / DestVT.getScalarSizeInBits());
11124a16efa3SDimitry Andric 
11134a16efa3SDimitry Andric   // Otherwise, promotion or legal types use the same number of registers as
11144a16efa3SDimitry Andric   // the vector decimated to the appropriate level.
11154a16efa3SDimitry Andric   return NumVectorRegs;
11164a16efa3SDimitry Andric }
11174a16efa3SDimitry Andric 
11184a16efa3SDimitry Andric /// isLegalRC - Return true if the value types that can be represented by the
11194a16efa3SDimitry Andric /// specified register class are all legal.
isLegalRC(const TargetRegisterInfo & TRI,const TargetRegisterClass & RC) const112012f3ca4cSDimitry Andric bool TargetLoweringBase::isLegalRC(const TargetRegisterInfo &TRI,
112112f3ca4cSDimitry Andric                                    const TargetRegisterClass &RC) const {
11224b4fe385SDimitry Andric   for (const auto *I = TRI.legalclasstypes_begin(RC); *I != MVT::Other; ++I)
11234a16efa3SDimitry Andric     if (isTypeLegal(*I))
11244a16efa3SDimitry Andric       return true;
11254a16efa3SDimitry Andric   return false;
11264a16efa3SDimitry Andric }
11274a16efa3SDimitry Andric 
11285ca98fd9SDimitry Andric /// Replace/modify any TargetFrameIndex operands with a targte-dependent
11295ca98fd9SDimitry Andric /// sequence of memory operands that is recognized by PrologEpilogInserter.
11305ca98fd9SDimitry Andric MachineBasicBlock *
emitPatchPoint(MachineInstr & InitialMI,MachineBasicBlock * MBB) const113101095a5dSDimitry Andric TargetLoweringBase::emitPatchPoint(MachineInstr &InitialMI,
11325ca98fd9SDimitry Andric                                    MachineBasicBlock *MBB) const {
113301095a5dSDimitry Andric   MachineInstr *MI = &InitialMI;
1134044eb2f6SDimitry Andric   MachineFunction &MF = *MI->getMF();
1135b915e9e0SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
1136dd58ef01SDimitry Andric 
1137dd58ef01SDimitry Andric   // We're handling multiple types of operands here:
1138dd58ef01SDimitry Andric   // PATCHPOINT MetaArgs - live-in, read only, direct
1139dd58ef01SDimitry Andric   // STATEPOINT Deopt Spill - live-through, read only, indirect
1140dd58ef01SDimitry Andric   // STATEPOINT Deopt Alloca - live-through, read only, direct
1141dd58ef01SDimitry Andric   // (We're currently conservative and mark the deopt slots read/write in
1142dd58ef01SDimitry Andric   // practice.)
1143dd58ef01SDimitry Andric   // STATEPOINT GC Spill - live-through, read/write, indirect
1144dd58ef01SDimitry Andric   // STATEPOINT GC Alloca - live-through, read/write, direct
1145dd58ef01SDimitry Andric   // The live-in vs live-through is handled already (the live through ones are
1146dd58ef01SDimitry Andric   // all stack slots), but we need to handle the different type of stackmap
1147dd58ef01SDimitry Andric   // operands and memory effects here.
11485ca98fd9SDimitry Andric 
114977fc4c14SDimitry Andric   if (llvm::none_of(MI->operands(),
1150cfca06d7SDimitry Andric                     [](MachineOperand &Operand) { return Operand.isFI(); }))
1151cfca06d7SDimitry Andric     return MBB;
1152cfca06d7SDimitry Andric 
1153cfca06d7SDimitry Andric   MachineInstrBuilder MIB = BuildMI(MF, MI->getDebugLoc(), MI->getDesc());
1154cfca06d7SDimitry Andric 
1155cfca06d7SDimitry Andric   // Inherit previous memory operands.
1156cfca06d7SDimitry Andric   MIB.cloneMemRefs(*MI);
1157cfca06d7SDimitry Andric 
1158b60736ecSDimitry Andric   for (unsigned i = 0; i < MI->getNumOperands(); ++i) {
1159b60736ecSDimitry Andric     MachineOperand &MO = MI->getOperand(i);
1160cfca06d7SDimitry Andric     if (!MO.isFI()) {
1161b60736ecSDimitry Andric       // Index of Def operand this Use it tied to.
1162b60736ecSDimitry Andric       // Since Defs are coming before Uses, if Use is tied, then
1163b60736ecSDimitry Andric       // index of Def must be smaller that index of that Use.
1164b60736ecSDimitry Andric       // Also, Defs preserve their position in new MI.
1165b60736ecSDimitry Andric       unsigned TiedTo = i;
1166b60736ecSDimitry Andric       if (MO.isReg() && MO.isTied())
1167b60736ecSDimitry Andric         TiedTo = MI->findTiedOperandIdx(i);
1168cfca06d7SDimitry Andric       MIB.add(MO);
1169b60736ecSDimitry Andric       if (TiedTo < i)
1170b60736ecSDimitry Andric         MIB->tieOperands(TiedTo, MIB->getNumOperands() - 1);
11715ca98fd9SDimitry Andric       continue;
1172cfca06d7SDimitry Andric     }
11735ca98fd9SDimitry Andric 
11745ca98fd9SDimitry Andric     // foldMemoryOperand builds a new MI after replacing a single FI operand
11755ca98fd9SDimitry Andric     // with the canonical set of five x86 addressing-mode operands.
11765ca98fd9SDimitry Andric     int FI = MO.getIndex();
11775ca98fd9SDimitry Andric 
1178dd58ef01SDimitry Andric     // Add frame index operands recognized by stackmaps.cpp
1179dd58ef01SDimitry Andric     if (MFI.isStatepointSpillSlotObjectIndex(FI)) {
1180dd58ef01SDimitry Andric       // indirect-mem-ref tag, size, #FI, offset.
1181dd58ef01SDimitry Andric       // Used for spills inserted by StatepointLowering.  This codepath is not
1182dd58ef01SDimitry Andric       // used for patchpoints/stackmaps at all, for these spilling is done via
1183dd58ef01SDimitry Andric       // foldMemoryOperand callback only.
1184dd58ef01SDimitry Andric       assert(MI->getOpcode() == TargetOpcode::STATEPOINT && "sanity");
1185dd58ef01SDimitry Andric       MIB.addImm(StackMaps::IndirectMemRefOp);
1186dd58ef01SDimitry Andric       MIB.addImm(MFI.getObjectSize(FI));
1187cfca06d7SDimitry Andric       MIB.add(MO);
1188dd58ef01SDimitry Andric       MIB.addImm(0);
1189dd58ef01SDimitry Andric     } else {
1190dd58ef01SDimitry Andric       // direct-mem-ref tag, #FI, offset.
1191dd58ef01SDimitry Andric       // Used by patchpoint, and direct alloca arguments to statepoints
11925ca98fd9SDimitry Andric       MIB.addImm(StackMaps::DirectMemRefOp);
1193cfca06d7SDimitry Andric       MIB.add(MO);
11945ca98fd9SDimitry Andric       MIB.addImm(0);
1195dd58ef01SDimitry Andric     }
11965ca98fd9SDimitry Andric 
11975ca98fd9SDimitry Andric     assert(MIB->mayLoad() && "Folded a stackmap use to a non-load!");
11985ca98fd9SDimitry Andric 
11995ca98fd9SDimitry Andric     // Add a new memory operand for this FI.
12005ca98fd9SDimitry Andric     assert(MFI.getObjectOffset(FI) != -1);
120167c32a98SDimitry Andric 
1202e6d15924SDimitry Andric     // Note: STATEPOINT MMOs are added during SelectionDAG.  STACKMAP, and
1203e6d15924SDimitry Andric     // PATCHPOINT should be updated to do the same. (TODO)
1204e6d15924SDimitry Andric     if (MI->getOpcode() != TargetOpcode::STATEPOINT) {
120501095a5dSDimitry Andric       auto Flags = MachineMemOperand::MOLoad;
120667c32a98SDimitry Andric       MachineMemOperand *MMO = MF.getMachineMemOperand(
1207dd58ef01SDimitry Andric           MachinePointerInfo::getFixedStack(MF, FI), Flags,
1208cfca06d7SDimitry Andric           MF.getDataLayout().getPointerSize(), MFI.getObjectAlign(FI));
12095ca98fd9SDimitry Andric       MIB->addMemOperand(MF, MMO);
1210e6d15924SDimitry Andric     }
12115ca98fd9SDimitry Andric   }
1212cfca06d7SDimitry Andric   MBB->insert(MachineBasicBlock::iterator(MI), MIB);
1213cfca06d7SDimitry Andric   MI->eraseFromParent();
12145ca98fd9SDimitry Andric   return MBB;
12155ca98fd9SDimitry Andric }
12165ca98fd9SDimitry Andric 
12174a16efa3SDimitry Andric /// findRepresentativeClass - Return the largest legal super-reg register class
12184a16efa3SDimitry Andric /// of the register class for the specified type and its associated "cost".
12195a5ac124SDimitry Andric // This function is in TargetLowering because it uses RegClassForVT which would
12205a5ac124SDimitry Andric // need to be moved to TargetRegisterInfo and would necessitate moving
12215a5ac124SDimitry Andric // isTypeLegal over as well - a massive change that would just require
12225a5ac124SDimitry Andric // TargetLowering having a TargetRegisterInfo class member that it would use.
12234a16efa3SDimitry Andric std::pair<const TargetRegisterClass *, uint8_t>
findRepresentativeClass(const TargetRegisterInfo * TRI,MVT VT) const12245a5ac124SDimitry Andric TargetLoweringBase::findRepresentativeClass(const TargetRegisterInfo *TRI,
12255a5ac124SDimitry Andric                                             MVT VT) const {
12264a16efa3SDimitry Andric   const TargetRegisterClass *RC = RegClassForVT[VT.SimpleTy];
12274a16efa3SDimitry Andric   if (!RC)
12284a16efa3SDimitry Andric     return std::make_pair(RC, 0);
12294a16efa3SDimitry Andric 
12304a16efa3SDimitry Andric   // Compute the set of all super-register classes.
12314a16efa3SDimitry Andric   BitVector SuperRegRC(TRI->getNumRegClasses());
12324a16efa3SDimitry Andric   for (SuperRegClassIterator RCI(RC, TRI); RCI.isValid(); ++RCI)
12334a16efa3SDimitry Andric     SuperRegRC.setBitsInMask(RCI.getMask());
12344a16efa3SDimitry Andric 
12354a16efa3SDimitry Andric   // Find the first legal register class with the largest spill size.
12364a16efa3SDimitry Andric   const TargetRegisterClass *BestRC = RC;
12377af96fb3SDimitry Andric   for (unsigned i : SuperRegRC.set_bits()) {
12384a16efa3SDimitry Andric     const TargetRegisterClass *SuperRC = TRI->getRegClass(i);
12394a16efa3SDimitry Andric     // We want the largest possible spill size.
124012f3ca4cSDimitry Andric     if (TRI->getSpillSize(*SuperRC) <= TRI->getSpillSize(*BestRC))
12414a16efa3SDimitry Andric       continue;
124212f3ca4cSDimitry Andric     if (!isLegalRC(*TRI, *SuperRC))
12434a16efa3SDimitry Andric       continue;
12444a16efa3SDimitry Andric     BestRC = SuperRC;
12454a16efa3SDimitry Andric   }
12464a16efa3SDimitry Andric   return std::make_pair(BestRC, 1);
12474a16efa3SDimitry Andric }
12484a16efa3SDimitry Andric 
12494a16efa3SDimitry Andric /// computeRegisterProperties - Once all of the register classes are added,
12504a16efa3SDimitry Andric /// this allows us to compute derived properties we expose.
computeRegisterProperties(const TargetRegisterInfo * TRI)12515a5ac124SDimitry Andric void TargetLoweringBase::computeRegisterProperties(
12525a5ac124SDimitry Andric     const TargetRegisterInfo *TRI) {
12534a16efa3SDimitry Andric   // Everything defaults to needing one register.
1254344a3780SDimitry Andric   for (unsigned i = 0; i != MVT::VALUETYPE_SIZE; ++i) {
12554a16efa3SDimitry Andric     NumRegistersForVT[i] = 1;
12564a16efa3SDimitry Andric     RegisterTypeForVT[i] = TransformToType[i] = (MVT::SimpleValueType)i;
12574a16efa3SDimitry Andric   }
12584a16efa3SDimitry Andric   // ...except isVoid, which doesn't need any registers.
12594a16efa3SDimitry Andric   NumRegistersForVT[MVT::isVoid] = 0;
12604a16efa3SDimitry Andric 
12614a16efa3SDimitry Andric   // Find the largest integer register class.
12624a16efa3SDimitry Andric   unsigned LargestIntReg = MVT::LAST_INTEGER_VALUETYPE;
12635ca98fd9SDimitry Andric   for (; RegClassForVT[LargestIntReg] == nullptr; --LargestIntReg)
12644a16efa3SDimitry Andric     assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
12654a16efa3SDimitry Andric 
12664a16efa3SDimitry Andric   // Every integer value type larger than this largest register takes twice as
12674a16efa3SDimitry Andric   // many registers to represent as the previous ValueType.
12684a16efa3SDimitry Andric   for (unsigned ExpandedReg = LargestIntReg + 1;
12694a16efa3SDimitry Andric        ExpandedReg <= MVT::LAST_INTEGER_VALUETYPE; ++ExpandedReg) {
12704a16efa3SDimitry Andric     NumRegistersForVT[ExpandedReg] = 2*NumRegistersForVT[ExpandedReg-1];
12714a16efa3SDimitry Andric     RegisterTypeForVT[ExpandedReg] = (MVT::SimpleValueType)LargestIntReg;
12724a16efa3SDimitry Andric     TransformToType[ExpandedReg] = (MVT::SimpleValueType)(ExpandedReg - 1);
12734a16efa3SDimitry Andric     ValueTypeActions.setTypeAction((MVT::SimpleValueType)ExpandedReg,
12744a16efa3SDimitry Andric                                    TypeExpandInteger);
12754a16efa3SDimitry Andric   }
12764a16efa3SDimitry Andric 
12774a16efa3SDimitry Andric   // Inspect all of the ValueType's smaller than the largest integer
12784a16efa3SDimitry Andric   // register to see which ones need promotion.
12794a16efa3SDimitry Andric   unsigned LegalIntReg = LargestIntReg;
12804a16efa3SDimitry Andric   for (unsigned IntReg = LargestIntReg - 1;
12814a16efa3SDimitry Andric        IntReg >= (unsigned)MVT::i1; --IntReg) {
12824a16efa3SDimitry Andric     MVT IVT = (MVT::SimpleValueType)IntReg;
12834a16efa3SDimitry Andric     if (isTypeLegal(IVT)) {
12844a16efa3SDimitry Andric       LegalIntReg = IntReg;
12854a16efa3SDimitry Andric     } else {
12864a16efa3SDimitry Andric       RegisterTypeForVT[IntReg] = TransformToType[IntReg] =
1287d8e91e46SDimitry Andric         (MVT::SimpleValueType)LegalIntReg;
12884a16efa3SDimitry Andric       ValueTypeActions.setTypeAction(IVT, TypePromoteInteger);
12894a16efa3SDimitry Andric     }
12904a16efa3SDimitry Andric   }
12914a16efa3SDimitry Andric 
12924a16efa3SDimitry Andric   // ppcf128 type is really two f64's.
12934a16efa3SDimitry Andric   if (!isTypeLegal(MVT::ppcf128)) {
129401095a5dSDimitry Andric     if (isTypeLegal(MVT::f64)) {
12954a16efa3SDimitry Andric       NumRegistersForVT[MVT::ppcf128] = 2*NumRegistersForVT[MVT::f64];
12964a16efa3SDimitry Andric       RegisterTypeForVT[MVT::ppcf128] = MVT::f64;
12974a16efa3SDimitry Andric       TransformToType[MVT::ppcf128] = MVT::f64;
12984a16efa3SDimitry Andric       ValueTypeActions.setTypeAction(MVT::ppcf128, TypeExpandFloat);
129901095a5dSDimitry Andric     } else {
130001095a5dSDimitry Andric       NumRegistersForVT[MVT::ppcf128] = NumRegistersForVT[MVT::i128];
130101095a5dSDimitry Andric       RegisterTypeForVT[MVT::ppcf128] = RegisterTypeForVT[MVT::i128];
130201095a5dSDimitry Andric       TransformToType[MVT::ppcf128] = MVT::i128;
130301095a5dSDimitry Andric       ValueTypeActions.setTypeAction(MVT::ppcf128, TypeSoftenFloat);
130401095a5dSDimitry Andric     }
13054a16efa3SDimitry Andric   }
13064a16efa3SDimitry Andric 
13074a16efa3SDimitry Andric   // Decide how to handle f128. If the target does not have native f128 support,
13084a16efa3SDimitry Andric   // expand it to i128 and we will be generating soft float library calls.
13094a16efa3SDimitry Andric   if (!isTypeLegal(MVT::f128)) {
13104a16efa3SDimitry Andric     NumRegistersForVT[MVT::f128] = NumRegistersForVT[MVT::i128];
13114a16efa3SDimitry Andric     RegisterTypeForVT[MVT::f128] = RegisterTypeForVT[MVT::i128];
13124a16efa3SDimitry Andric     TransformToType[MVT::f128] = MVT::i128;
13134a16efa3SDimitry Andric     ValueTypeActions.setTypeAction(MVT::f128, TypeSoftenFloat);
13144a16efa3SDimitry Andric   }
13154a16efa3SDimitry Andric 
1316e3b55780SDimitry Andric   // Decide how to handle f80. If the target does not have native f80 support,
1317e3b55780SDimitry Andric   // expand it to i96 and we will be generating soft float library calls.
1318e3b55780SDimitry Andric   if (!isTypeLegal(MVT::f80)) {
1319e3b55780SDimitry Andric     NumRegistersForVT[MVT::f80] = 3*NumRegistersForVT[MVT::i32];
1320e3b55780SDimitry Andric     RegisterTypeForVT[MVT::f80] = RegisterTypeForVT[MVT::i32];
1321e3b55780SDimitry Andric     TransformToType[MVT::f80] = MVT::i32;
1322e3b55780SDimitry Andric     ValueTypeActions.setTypeAction(MVT::f80, TypeSoftenFloat);
1323e3b55780SDimitry Andric   }
1324e3b55780SDimitry Andric 
13254a16efa3SDimitry Andric   // Decide how to handle f64. If the target does not have native f64 support,
13264a16efa3SDimitry Andric   // expand it to i64 and we will be generating soft float library calls.
13274a16efa3SDimitry Andric   if (!isTypeLegal(MVT::f64)) {
13284a16efa3SDimitry Andric     NumRegistersForVT[MVT::f64] = NumRegistersForVT[MVT::i64];
13294a16efa3SDimitry Andric     RegisterTypeForVT[MVT::f64] = RegisterTypeForVT[MVT::i64];
13304a16efa3SDimitry Andric     TransformToType[MVT::f64] = MVT::i64;
13314a16efa3SDimitry Andric     ValueTypeActions.setTypeAction(MVT::f64, TypeSoftenFloat);
13324a16efa3SDimitry Andric   }
13334a16efa3SDimitry Andric 
13345a5ac124SDimitry Andric   // Decide how to handle f32. If the target does not have native f32 support,
13355a5ac124SDimitry Andric   // expand it to i32 and we will be generating soft float library calls.
13364a16efa3SDimitry Andric   if (!isTypeLegal(MVT::f32)) {
13374a16efa3SDimitry Andric     NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::i32];
13384a16efa3SDimitry Andric     RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::i32];
13394a16efa3SDimitry Andric     TransformToType[MVT::f32] = MVT::i32;
13404a16efa3SDimitry Andric     ValueTypeActions.setTypeAction(MVT::f32, TypeSoftenFloat);
13414a16efa3SDimitry Andric   }
13424a16efa3SDimitry Andric 
1343dd58ef01SDimitry Andric   // Decide how to handle f16. If the target does not have native f16 support,
1344dd58ef01SDimitry Andric   // promote it to f32, because there are no f16 library calls (except for
1345dd58ef01SDimitry Andric   // conversions).
13465ca98fd9SDimitry Andric   if (!isTypeLegal(MVT::f16)) {
1347cfca06d7SDimitry Andric     // Allow targets to control how we legalize half.
1348ac9a064cSDimitry Andric     bool SoftPromoteHalfType = softPromoteHalfType();
1349ac9a064cSDimitry Andric     bool UseFPRegsForHalfType = !SoftPromoteHalfType || useFPRegsForHalfType();
1350ac9a064cSDimitry Andric 
1351ac9a064cSDimitry Andric     if (!UseFPRegsForHalfType) {
1352cfca06d7SDimitry Andric       NumRegistersForVT[MVT::f16] = NumRegistersForVT[MVT::i16];
1353cfca06d7SDimitry Andric       RegisterTypeForVT[MVT::f16] = RegisterTypeForVT[MVT::i16];
1354cfca06d7SDimitry Andric     } else {
13555a5ac124SDimitry Andric       NumRegistersForVT[MVT::f16] = NumRegistersForVT[MVT::f32];
13565a5ac124SDimitry Andric       RegisterTypeForVT[MVT::f16] = RegisterTypeForVT[MVT::f32];
1357ac9a064cSDimitry Andric     }
13585a5ac124SDimitry Andric     TransformToType[MVT::f16] = MVT::f32;
1359ac9a064cSDimitry Andric     if (SoftPromoteHalfType) {
1360ac9a064cSDimitry Andric       ValueTypeActions.setTypeAction(MVT::f16, TypeSoftPromoteHalf);
1361ac9a064cSDimitry Andric     } else {
13625a5ac124SDimitry Andric       ValueTypeActions.setTypeAction(MVT::f16, TypePromoteFloat);
13635a5ac124SDimitry Andric     }
1364cfca06d7SDimitry Andric   }
13655ca98fd9SDimitry Andric 
1366145449b1SDimitry Andric   // Decide how to handle bf16. If the target does not have native bf16 support,
1367145449b1SDimitry Andric   // promote it to f32, because there are no bf16 library calls (except for
1368145449b1SDimitry Andric   // converting from f32 to bf16).
1369145449b1SDimitry Andric   if (!isTypeLegal(MVT::bf16)) {
1370145449b1SDimitry Andric     NumRegistersForVT[MVT::bf16] = NumRegistersForVT[MVT::f32];
1371145449b1SDimitry Andric     RegisterTypeForVT[MVT::bf16] = RegisterTypeForVT[MVT::f32];
1372145449b1SDimitry Andric     TransformToType[MVT::bf16] = MVT::f32;
1373e3b55780SDimitry Andric     ValueTypeActions.setTypeAction(MVT::bf16, TypeSoftPromoteHalf);
1374145449b1SDimitry Andric   }
1375145449b1SDimitry Andric 
13764a16efa3SDimitry Andric   // Loop over all of the vector value types to see which need transformations.
13774a16efa3SDimitry Andric   for (unsigned i = MVT::FIRST_VECTOR_VALUETYPE;
13784a16efa3SDimitry Andric        i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
13794a16efa3SDimitry Andric     MVT VT = (MVT::SimpleValueType) i;
13805ca98fd9SDimitry Andric     if (isTypeLegal(VT))
13815ca98fd9SDimitry Andric       continue;
13824a16efa3SDimitry Andric 
13834a16efa3SDimitry Andric     MVT EltVT = VT.getVectorElementType();
1384cfca06d7SDimitry Andric     ElementCount EC = VT.getVectorElementCount();
13854a16efa3SDimitry Andric     bool IsLegalWiderType = false;
13861d5ae102SDimitry Andric     bool IsScalable = VT.isScalableVector();
13875ca98fd9SDimitry Andric     LegalizeTypeAction PreferredAction = getPreferredVectorAction(VT);
13885ca98fd9SDimitry Andric     switch (PreferredAction) {
13891d5ae102SDimitry Andric     case TypePromoteInteger: {
13901d5ae102SDimitry Andric       MVT::SimpleValueType EndVT = IsScalable ?
13911d5ae102SDimitry Andric                                    MVT::LAST_INTEGER_SCALABLE_VECTOR_VALUETYPE :
13921d5ae102SDimitry Andric                                    MVT::LAST_INTEGER_FIXEDLEN_VECTOR_VALUETYPE;
13935ca98fd9SDimitry Andric       // Try to promote the elements of integer vectors. If no legal
13945ca98fd9SDimitry Andric       // promotion was found, fall through to the widen-vector method.
13951d5ae102SDimitry Andric       for (unsigned nVT = i + 1;
13961d5ae102SDimitry Andric            (MVT::SimpleValueType)nVT <= EndVT; ++nVT) {
13974a16efa3SDimitry Andric         MVT SVT = (MVT::SimpleValueType) nVT;
13984a16efa3SDimitry Andric         // Promote vectors of integers to vectors with the same number
13994a16efa3SDimitry Andric         // of elements, with a wider element type.
1400b60736ecSDimitry Andric         if (SVT.getScalarSizeInBits() > EltVT.getFixedSizeInBits() &&
1401cfca06d7SDimitry Andric             SVT.getVectorElementCount() == EC && isTypeLegal(SVT)) {
14024a16efa3SDimitry Andric           TransformToType[i] = SVT;
14034a16efa3SDimitry Andric           RegisterTypeForVT[i] = SVT;
14044a16efa3SDimitry Andric           NumRegistersForVT[i] = 1;
14054a16efa3SDimitry Andric           ValueTypeActions.setTypeAction(VT, TypePromoteInteger);
14064a16efa3SDimitry Andric           IsLegalWiderType = true;
14074a16efa3SDimitry Andric           break;
14084a16efa3SDimitry Andric         }
14094a16efa3SDimitry Andric       }
14105ca98fd9SDimitry Andric       if (IsLegalWiderType)
14115ca98fd9SDimitry Andric         break;
1412e3b55780SDimitry Andric       [[fallthrough]];
14131d5ae102SDimitry Andric     }
1414044eb2f6SDimitry Andric 
1415044eb2f6SDimitry Andric     case TypeWidenVector:
1416b60736ecSDimitry Andric       if (isPowerOf2_32(EC.getKnownMinValue())) {
14174a16efa3SDimitry Andric         // Try to widen the vector.
14184a16efa3SDimitry Andric         for (unsigned nVT = i + 1; nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
14194a16efa3SDimitry Andric           MVT SVT = (MVT::SimpleValueType) nVT;
1420cfca06d7SDimitry Andric           if (SVT.getVectorElementType() == EltVT &&
1421cfca06d7SDimitry Andric               SVT.isScalableVector() == IsScalable &&
1422b60736ecSDimitry Andric               SVT.getVectorElementCount().getKnownMinValue() >
1423b60736ecSDimitry Andric                   EC.getKnownMinValue() &&
1424b60736ecSDimitry Andric               isTypeLegal(SVT)) {
14254a16efa3SDimitry Andric             TransformToType[i] = SVT;
14264a16efa3SDimitry Andric             RegisterTypeForVT[i] = SVT;
14274a16efa3SDimitry Andric             NumRegistersForVT[i] = 1;
14284a16efa3SDimitry Andric             ValueTypeActions.setTypeAction(VT, TypeWidenVector);
14294a16efa3SDimitry Andric             IsLegalWiderType = true;
14304a16efa3SDimitry Andric             break;
14314a16efa3SDimitry Andric           }
14324a16efa3SDimitry Andric         }
14335ca98fd9SDimitry Andric         if (IsLegalWiderType)
14345ca98fd9SDimitry Andric           break;
14351d5ae102SDimitry Andric       } else {
14361d5ae102SDimitry Andric         // Only widen to the next power of 2 to keep consistency with EVT.
14371d5ae102SDimitry Andric         MVT NVT = VT.getPow2VectorType();
14381d5ae102SDimitry Andric         if (isTypeLegal(NVT)) {
14391d5ae102SDimitry Andric           TransformToType[i] = NVT;
14401d5ae102SDimitry Andric           ValueTypeActions.setTypeAction(VT, TypeWidenVector);
14411d5ae102SDimitry Andric           RegisterTypeForVT[i] = NVT;
14421d5ae102SDimitry Andric           NumRegistersForVT[i] = 1;
14431d5ae102SDimitry Andric           break;
14441d5ae102SDimitry Andric         }
14451d5ae102SDimitry Andric       }
1446e3b55780SDimitry Andric       [[fallthrough]];
1447044eb2f6SDimitry Andric 
14485ca98fd9SDimitry Andric     case TypeSplitVector:
14495ca98fd9SDimitry Andric     case TypeScalarizeVector: {
14504a16efa3SDimitry Andric       MVT IntermediateVT;
14514a16efa3SDimitry Andric       MVT RegisterVT;
14524a16efa3SDimitry Andric       unsigned NumIntermediates;
1453706b4fc4SDimitry Andric       unsigned NumRegisters = getVectorTypeBreakdownMVT(VT, IntermediateVT,
14545ca98fd9SDimitry Andric           NumIntermediates, RegisterVT, this);
1455706b4fc4SDimitry Andric       NumRegistersForVT[i] = NumRegisters;
1456706b4fc4SDimitry Andric       assert(NumRegistersForVT[i] == NumRegisters &&
1457706b4fc4SDimitry Andric              "NumRegistersForVT size cannot represent NumRegisters!");
14584a16efa3SDimitry Andric       RegisterTypeForVT[i] = RegisterVT;
14594a16efa3SDimitry Andric 
14604a16efa3SDimitry Andric       MVT NVT = VT.getPow2VectorType();
14614a16efa3SDimitry Andric       if (NVT == VT) {
14624a16efa3SDimitry Andric         // Type is already a power of 2.  The default action is to split.
14634a16efa3SDimitry Andric         TransformToType[i] = MVT::Other;
14645ca98fd9SDimitry Andric         if (PreferredAction == TypeScalarizeVector)
14655ca98fd9SDimitry Andric           ValueTypeActions.setTypeAction(VT, TypeScalarizeVector);
146667c32a98SDimitry Andric         else if (PreferredAction == TypeSplitVector)
14675ca98fd9SDimitry Andric           ValueTypeActions.setTypeAction(VT, TypeSplitVector);
1468b60736ecSDimitry Andric         else if (EC.getKnownMinValue() > 1)
1469cfca06d7SDimitry Andric           ValueTypeActions.setTypeAction(VT, TypeSplitVector);
147067c32a98SDimitry Andric         else
1471b60736ecSDimitry Andric           ValueTypeActions.setTypeAction(VT, EC.isScalable()
1472cfca06d7SDimitry Andric                                                  ? TypeScalarizeScalableVector
1473cfca06d7SDimitry Andric                                                  : TypeScalarizeVector);
14744a16efa3SDimitry Andric       } else {
14754a16efa3SDimitry Andric         TransformToType[i] = NVT;
14764a16efa3SDimitry Andric         ValueTypeActions.setTypeAction(VT, TypeWidenVector);
14774a16efa3SDimitry Andric       }
14785ca98fd9SDimitry Andric       break;
14795ca98fd9SDimitry Andric     }
14805ca98fd9SDimitry Andric     default:
14815ca98fd9SDimitry Andric       llvm_unreachable("Unknown vector legalization action!");
14825ca98fd9SDimitry Andric     }
14834a16efa3SDimitry Andric   }
14844a16efa3SDimitry Andric 
14854a16efa3SDimitry Andric   // Determine the 'representative' register class for each value type.
14864a16efa3SDimitry Andric   // An representative register class is the largest (meaning one which is
14874a16efa3SDimitry Andric   // not a sub-register class / subreg register class) legal register class for
14884a16efa3SDimitry Andric   // a group of value types. For example, on i386, i8, i16, and i32
14894a16efa3SDimitry Andric   // representative would be GR32; while on x86_64 it's GR64.
1490344a3780SDimitry Andric   for (unsigned i = 0; i != MVT::VALUETYPE_SIZE; ++i) {
14914a16efa3SDimitry Andric     const TargetRegisterClass* RRC;
14924a16efa3SDimitry Andric     uint8_t Cost;
14935a5ac124SDimitry Andric     std::tie(RRC, Cost) = findRepresentativeClass(TRI, (MVT::SimpleValueType)i);
14944a16efa3SDimitry Andric     RepRegClassForVT[i] = RRC;
14954a16efa3SDimitry Andric     RepRegClassCostForVT[i] = Cost;
14964a16efa3SDimitry Andric   }
14974a16efa3SDimitry Andric }
14984a16efa3SDimitry Andric 
getSetCCResultType(const DataLayout & DL,LLVMContext &,EVT VT) const1499ee8648bdSDimitry Andric EVT TargetLoweringBase::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1500ee8648bdSDimitry Andric                                            EVT VT) const {
15014a16efa3SDimitry Andric   assert(!VT.isVector() && "No default SetCC type for vectors!");
1502ee8648bdSDimitry Andric   return getPointerTy(DL).SimpleTy;
15034a16efa3SDimitry Andric }
15044a16efa3SDimitry Andric 
getCmpLibcallReturnType() const15054a16efa3SDimitry Andric MVT::SimpleValueType TargetLoweringBase::getCmpLibcallReturnType() const {
15064a16efa3SDimitry Andric   return MVT::i32; // return the default value
15074a16efa3SDimitry Andric }
15084a16efa3SDimitry Andric 
15094a16efa3SDimitry Andric /// getVectorTypeBreakdown - Vector types are broken down into some number of
15104a16efa3SDimitry Andric /// legal first class types.  For example, MVT::v8f32 maps to 2 MVT::v4f32
15114a16efa3SDimitry Andric /// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
15124a16efa3SDimitry Andric /// Similarly, MVT::v2i64 turns into 4 MVT::i32 values with both PPC and X86.
15134a16efa3SDimitry Andric ///
15144a16efa3SDimitry Andric /// This method returns the number of registers needed, and the VT for each
15154a16efa3SDimitry Andric /// register.  It also returns the VT and quantity of the intermediate values
15164a16efa3SDimitry Andric /// before they are promoted/expanded.
getVectorTypeBreakdown(LLVMContext & Context,EVT VT,EVT & IntermediateVT,unsigned & NumIntermediates,MVT & RegisterVT) const1517344a3780SDimitry Andric unsigned TargetLoweringBase::getVectorTypeBreakdown(LLVMContext &Context,
1518344a3780SDimitry Andric                                                     EVT VT, EVT &IntermediateVT,
15194a16efa3SDimitry Andric                                                     unsigned &NumIntermediates,
15204a16efa3SDimitry Andric                                                     MVT &RegisterVT) const {
1521cfca06d7SDimitry Andric   ElementCount EltCnt = VT.getVectorElementCount();
15224a16efa3SDimitry Andric 
15234a16efa3SDimitry Andric   // If there is a wider vector type with the same element type as this one,
15244a16efa3SDimitry Andric   // or a promoted vector type that has the same number of elements which
15254a16efa3SDimitry Andric   // are wider, then we should convert to that legal vector type.
15264a16efa3SDimitry Andric   // This handles things like <2 x float> -> <4 x float> and
15274a16efa3SDimitry Andric   // <4 x i1> -> <4 x i32>.
15284a16efa3SDimitry Andric   LegalizeTypeAction TA = getTypeAction(Context, VT);
1529344a3780SDimitry Andric   if (!EltCnt.isScalar() &&
1530b60736ecSDimitry Andric       (TA == TypeWidenVector || TA == TypePromoteInteger)) {
15314a16efa3SDimitry Andric     EVT RegisterEVT = getTypeToTransformTo(Context, VT);
15324a16efa3SDimitry Andric     if (isTypeLegal(RegisterEVT)) {
15334a16efa3SDimitry Andric       IntermediateVT = RegisterEVT;
15344a16efa3SDimitry Andric       RegisterVT = RegisterEVT.getSimpleVT();
15354a16efa3SDimitry Andric       NumIntermediates = 1;
15364a16efa3SDimitry Andric       return 1;
15374a16efa3SDimitry Andric     }
15384a16efa3SDimitry Andric   }
15394a16efa3SDimitry Andric 
15404a16efa3SDimitry Andric   // Figure out the right, legal destination reg to copy into.
15414a16efa3SDimitry Andric   EVT EltTy = VT.getVectorElementType();
15424a16efa3SDimitry Andric 
15434a16efa3SDimitry Andric   unsigned NumVectorRegs = 1;
15444a16efa3SDimitry Andric 
1545cfca06d7SDimitry Andric   // Scalable vectors cannot be scalarized, so handle the legalisation of the
1546cfca06d7SDimitry Andric   // types like done elsewhere in SelectionDAG.
1547c0981da4SDimitry Andric   if (EltCnt.isScalable()) {
1548cfca06d7SDimitry Andric     LegalizeKind LK;
1549cfca06d7SDimitry Andric     EVT PartVT = VT;
1550cfca06d7SDimitry Andric     do {
1551cfca06d7SDimitry Andric       // Iterate until we've found a legal (part) type to hold VT.
1552cfca06d7SDimitry Andric       LK = getTypeConversion(Context, PartVT);
1553cfca06d7SDimitry Andric       PartVT = LK.second;
1554cfca06d7SDimitry Andric     } while (LK.first != TypeLegal);
1555cfca06d7SDimitry Andric 
1556c0981da4SDimitry Andric     if (!PartVT.isVector()) {
1557c0981da4SDimitry Andric       report_fatal_error(
1558c0981da4SDimitry Andric           "Don't know how to legalize this scalable vector type");
1559c0981da4SDimitry Andric     }
1560cfca06d7SDimitry Andric 
1561c0981da4SDimitry Andric     NumIntermediates =
1562c0981da4SDimitry Andric         divideCeil(VT.getVectorElementCount().getKnownMinValue(),
1563c0981da4SDimitry Andric                    PartVT.getVectorElementCount().getKnownMinValue());
1564cfca06d7SDimitry Andric     IntermediateVT = PartVT;
1565cfca06d7SDimitry Andric     RegisterVT = getRegisterType(Context, IntermediateVT);
1566cfca06d7SDimitry Andric     return NumIntermediates;
1567cfca06d7SDimitry Andric   }
1568cfca06d7SDimitry Andric 
1569cfca06d7SDimitry Andric   // FIXME: We don't support non-power-of-2-sized vectors for now.  Ideally
1570cfca06d7SDimitry Andric   // we could break down into LHS/RHS like LegalizeDAG does.
1571b60736ecSDimitry Andric   if (!isPowerOf2_32(EltCnt.getKnownMinValue())) {
1572b60736ecSDimitry Andric     NumVectorRegs = EltCnt.getKnownMinValue();
1573b60736ecSDimitry Andric     EltCnt = ElementCount::getFixed(1);
15744a16efa3SDimitry Andric   }
15754a16efa3SDimitry Andric 
15764a16efa3SDimitry Andric   // Divide the input until we get to a supported size.  This will always
15774a16efa3SDimitry Andric   // end with a scalar if the target doesn't support vectors.
1578b60736ecSDimitry Andric   while (EltCnt.getKnownMinValue() > 1 &&
1579cfca06d7SDimitry Andric          !isTypeLegal(EVT::getVectorVT(Context, EltTy, EltCnt))) {
1580b60736ecSDimitry Andric     EltCnt = EltCnt.divideCoefficientBy(2);
15814a16efa3SDimitry Andric     NumVectorRegs <<= 1;
15824a16efa3SDimitry Andric   }
15834a16efa3SDimitry Andric 
15844a16efa3SDimitry Andric   NumIntermediates = NumVectorRegs;
15854a16efa3SDimitry Andric 
1586cfca06d7SDimitry Andric   EVT NewVT = EVT::getVectorVT(Context, EltTy, EltCnt);
15874a16efa3SDimitry Andric   if (!isTypeLegal(NewVT))
15884a16efa3SDimitry Andric     NewVT = EltTy;
15894a16efa3SDimitry Andric   IntermediateVT = NewVT;
15904a16efa3SDimitry Andric 
15914a16efa3SDimitry Andric   MVT DestVT = getRegisterType(Context, NewVT);
15924a16efa3SDimitry Andric   RegisterVT = DestVT;
15934a16efa3SDimitry Andric 
1594cfca06d7SDimitry Andric   if (EVT(DestVT).bitsLT(NewVT)) {  // Value is expanded, e.g. i64 -> i16.
1595cfca06d7SDimitry Andric     TypeSize NewVTSize = NewVT.getSizeInBits();
15964a16efa3SDimitry Andric     // Convert sizes such as i33 to i64.
15977fa27ce4SDimitry Andric     if (!llvm::has_single_bit<uint32_t>(NewVTSize.getKnownMinValue()))
1598b60736ecSDimitry Andric       NewVTSize = NewVTSize.coefficientNextPowerOf2();
15994a16efa3SDimitry Andric     return NumVectorRegs*(NewVTSize/DestVT.getSizeInBits());
1600cfca06d7SDimitry Andric   }
16014a16efa3SDimitry Andric 
16024a16efa3SDimitry Andric   // Otherwise, promotion or legal types use the same number of registers as
16034a16efa3SDimitry Andric   // the vector decimated to the appropriate level.
16044a16efa3SDimitry Andric   return NumVectorRegs;
16054a16efa3SDimitry Andric }
16064a16efa3SDimitry Andric 
isSuitableForJumpTable(const SwitchInst * SI,uint64_t NumCases,uint64_t Range,ProfileSummaryInfo * PSI,BlockFrequencyInfo * BFI) const1607706b4fc4SDimitry Andric bool TargetLoweringBase::isSuitableForJumpTable(const SwitchInst *SI,
1608706b4fc4SDimitry Andric                                                 uint64_t NumCases,
1609706b4fc4SDimitry Andric                                                 uint64_t Range,
1610706b4fc4SDimitry Andric                                                 ProfileSummaryInfo *PSI,
1611706b4fc4SDimitry Andric                                                 BlockFrequencyInfo *BFI) const {
1612706b4fc4SDimitry Andric   // FIXME: This function check the maximum table size and density, but the
1613706b4fc4SDimitry Andric   // minimum size is not checked. It would be nice if the minimum size is
1614706b4fc4SDimitry Andric   // also combined within this function. Currently, the minimum size check is
1615706b4fc4SDimitry Andric   // performed in findJumpTable() in SelectionDAGBuiler and
1616706b4fc4SDimitry Andric   // getEstimatedNumberOfCaseClusters() in BasicTTIImpl.
1617706b4fc4SDimitry Andric   const bool OptForSize =
1618706b4fc4SDimitry Andric       SI->getParent()->getParent()->hasOptSize() ||
1619706b4fc4SDimitry Andric       llvm::shouldOptimizeForSize(SI->getParent(), PSI, BFI);
1620706b4fc4SDimitry Andric   const unsigned MinDensity = getMinimumJumpTableDensity(OptForSize);
1621706b4fc4SDimitry Andric   const unsigned MaxJumpTableSize = getMaximumJumpTableSize();
1622706b4fc4SDimitry Andric 
1623706b4fc4SDimitry Andric   // Check whether the number of cases is small enough and
1624706b4fc4SDimitry Andric   // the range is dense enough for a jump table.
1625706b4fc4SDimitry Andric   return (OptForSize || Range <= MaxJumpTableSize) &&
1626706b4fc4SDimitry Andric          (NumCases * 100 >= Range * MinDensity);
1627706b4fc4SDimitry Andric }
1628706b4fc4SDimitry Andric 
getPreferredSwitchConditionType(LLVMContext & Context,EVT ConditionVT) const1629145449b1SDimitry Andric MVT TargetLoweringBase::getPreferredSwitchConditionType(LLVMContext &Context,
1630145449b1SDimitry Andric                                                         EVT ConditionVT) const {
1631145449b1SDimitry Andric   return getRegisterType(Context, ConditionVT);
1632145449b1SDimitry Andric }
1633145449b1SDimitry Andric 
16344a16efa3SDimitry Andric /// Get the EVTs and ArgFlags collections that represent the legalized return
16354a16efa3SDimitry Andric /// type of the given function.  This does not require a DAG or a return value,
16364a16efa3SDimitry Andric /// and is suitable for use before any DAGs for the function are constructed.
16374a16efa3SDimitry Andric /// TODO: Move this out of TargetLowering.cpp.
GetReturnInfo(CallingConv::ID CC,Type * ReturnType,AttributeList attr,SmallVectorImpl<ISD::OutputArg> & Outs,const TargetLowering & TLI,const DataLayout & DL)1638b7eb8e35SDimitry Andric void llvm::GetReturnInfo(CallingConv::ID CC, Type *ReturnType,
1639b7eb8e35SDimitry Andric                          AttributeList attr,
16404a16efa3SDimitry Andric                          SmallVectorImpl<ISD::OutputArg> &Outs,
1641ee8648bdSDimitry Andric                          const TargetLowering &TLI, const DataLayout &DL) {
16424a16efa3SDimitry Andric   SmallVector<EVT, 4> ValueVTs;
1643ee8648bdSDimitry Andric   ComputeValueVTs(TLI, DL, ReturnType, ValueVTs);
16444a16efa3SDimitry Andric   unsigned NumValues = ValueVTs.size();
16454a16efa3SDimitry Andric   if (NumValues == 0) return;
16464a16efa3SDimitry Andric 
16474a16efa3SDimitry Andric   for (unsigned j = 0, f = NumValues; j != f; ++j) {
16484a16efa3SDimitry Andric     EVT VT = ValueVTs[j];
16494a16efa3SDimitry Andric     ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
16504a16efa3SDimitry Andric 
1651c0981da4SDimitry Andric     if (attr.hasRetAttr(Attribute::SExt))
16524a16efa3SDimitry Andric       ExtendKind = ISD::SIGN_EXTEND;
1653c0981da4SDimitry Andric     else if (attr.hasRetAttr(Attribute::ZExt))
16544a16efa3SDimitry Andric       ExtendKind = ISD::ZERO_EXTEND;
16554a16efa3SDimitry Andric 
1656ac9a064cSDimitry Andric     if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger())
1657ac9a064cSDimitry Andric       VT = TLI.getTypeForExtReturn(ReturnType->getContext(), VT, ExtendKind);
16584a16efa3SDimitry Andric 
16597ab83427SDimitry Andric     unsigned NumParts =
1660b7eb8e35SDimitry Andric         TLI.getNumRegistersForCallingConv(ReturnType->getContext(), CC, VT);
16617ab83427SDimitry Andric     MVT PartVT =
1662b7eb8e35SDimitry Andric         TLI.getRegisterTypeForCallingConv(ReturnType->getContext(), CC, VT);
16634a16efa3SDimitry Andric 
16644a16efa3SDimitry Andric     // 'inreg' on function refers to return value
16654a16efa3SDimitry Andric     ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
1666c0981da4SDimitry Andric     if (attr.hasRetAttr(Attribute::InReg))
16674a16efa3SDimitry Andric       Flags.setInReg();
16684a16efa3SDimitry Andric 
16694a16efa3SDimitry Andric     // Propagate extension type if any
1670c0981da4SDimitry Andric     if (attr.hasRetAttr(Attribute::SExt))
16714a16efa3SDimitry Andric       Flags.setSExt();
1672c0981da4SDimitry Andric     else if (attr.hasRetAttr(Attribute::ZExt))
16734a16efa3SDimitry Andric       Flags.setZExt();
16744a16efa3SDimitry Andric 
1675ac9a064cSDimitry Andric     for (unsigned i = 0; i < NumParts; ++i) {
1676ac9a064cSDimitry Andric       ISD::ArgFlagsTy OutFlags = Flags;
1677ac9a064cSDimitry Andric       if (NumParts > 1 && i == 0)
1678ac9a064cSDimitry Andric         OutFlags.setSplit();
1679ac9a064cSDimitry Andric       else if (i == NumParts - 1 && i != 0)
1680ac9a064cSDimitry Andric         OutFlags.setSplitEnd();
1681ac9a064cSDimitry Andric 
1682ac9a064cSDimitry Andric       Outs.push_back(
1683ac9a064cSDimitry Andric           ISD::OutputArg(OutFlags, PartVT, VT, /*isfixed=*/true, 0, 0));
1684ac9a064cSDimitry Andric     }
16854a16efa3SDimitry Andric   }
16864a16efa3SDimitry Andric }
16874a16efa3SDimitry Andric 
16884a16efa3SDimitry Andric /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
16894a16efa3SDimitry Andric /// function arguments in the caller parameter area.  This is the actual
16904a16efa3SDimitry Andric /// alignment, not its logarithm.
getByValTypeAlignment(Type * Ty,const DataLayout & DL) const1691c0981da4SDimitry Andric uint64_t TargetLoweringBase::getByValTypeAlignment(Type *Ty,
1692ee8648bdSDimitry Andric                                                    const DataLayout &DL) const {
1693cfca06d7SDimitry Andric   return DL.getABITypeAlign(Ty).value();
16944a16efa3SDimitry Andric }
16954a16efa3SDimitry Andric 
allowsMemoryAccessForAlignment(LLVMContext & Context,const DataLayout & DL,EVT VT,unsigned AddrSpace,Align Alignment,MachineMemOperand::Flags Flags,unsigned * Fast) const16961d5ae102SDimitry Andric bool TargetLoweringBase::allowsMemoryAccessForAlignment(
16971d5ae102SDimitry Andric     LLVMContext &Context, const DataLayout &DL, EVT VT, unsigned AddrSpace,
1698e3b55780SDimitry Andric     Align Alignment, MachineMemOperand::Flags Flags, unsigned *Fast) const {
1699dd58ef01SDimitry Andric   // Check if the specified alignment is sufficient based on the data layout.
1700dd58ef01SDimitry Andric   // TODO: While using the data layout works in practice, a better solution
1701dd58ef01SDimitry Andric   // would be to implement this check directly (make this a virtual function).
1702dd58ef01SDimitry Andric   // For example, the ABI alignment may change based on software platform while
1703dd58ef01SDimitry Andric   // this function should only be affected by hardware implementation.
1704dd58ef01SDimitry Andric   Type *Ty = VT.getTypeForEVT(Context);
1705344a3780SDimitry Andric   if (VT.isZeroSized() || Alignment >= DL.getABITypeAlign(Ty)) {
1706dd58ef01SDimitry Andric     // Assume that an access that meets the ABI-specified alignment is fast.
1707dd58ef01SDimitry Andric     if (Fast != nullptr)
1708e3b55780SDimitry Andric       *Fast = 1;
1709dd58ef01SDimitry Andric     return true;
1710dd58ef01SDimitry Andric   }
1711dd58ef01SDimitry Andric 
1712dd58ef01SDimitry Andric   // This is a misaligned access.
1713344a3780SDimitry Andric   return allowsMisalignedMemoryAccesses(VT, AddrSpace, Alignment, Flags, Fast);
1714e6d15924SDimitry Andric }
1715e6d15924SDimitry Andric 
allowsMemoryAccessForAlignment(LLVMContext & Context,const DataLayout & DL,EVT VT,const MachineMemOperand & MMO,unsigned * Fast) const17161d5ae102SDimitry Andric bool TargetLoweringBase::allowsMemoryAccessForAlignment(
17171d5ae102SDimitry Andric     LLVMContext &Context, const DataLayout &DL, EVT VT,
1718e3b55780SDimitry Andric     const MachineMemOperand &MMO, unsigned *Fast) const {
17191d5ae102SDimitry Andric   return allowsMemoryAccessForAlignment(Context, DL, VT, MMO.getAddrSpace(),
1720cfca06d7SDimitry Andric                                         MMO.getAlign(), MMO.getFlags(), Fast);
17211d5ae102SDimitry Andric }
17221d5ae102SDimitry Andric 
allowsMemoryAccess(LLVMContext & Context,const DataLayout & DL,EVT VT,unsigned AddrSpace,Align Alignment,MachineMemOperand::Flags Flags,unsigned * Fast) const1723cfca06d7SDimitry Andric bool TargetLoweringBase::allowsMemoryAccess(LLVMContext &Context,
1724cfca06d7SDimitry Andric                                             const DataLayout &DL, EVT VT,
1725cfca06d7SDimitry Andric                                             unsigned AddrSpace, Align Alignment,
1726cfca06d7SDimitry Andric                                             MachineMemOperand::Flags Flags,
1727e3b55780SDimitry Andric                                             unsigned *Fast) const {
17281d5ae102SDimitry Andric   return allowsMemoryAccessForAlignment(Context, DL, VT, AddrSpace, Alignment,
17291d5ae102SDimitry Andric                                         Flags, Fast);
17301d5ae102SDimitry Andric }
17311d5ae102SDimitry Andric 
allowsMemoryAccess(LLVMContext & Context,const DataLayout & DL,EVT VT,const MachineMemOperand & MMO,unsigned * Fast) const1732e6d15924SDimitry Andric bool TargetLoweringBase::allowsMemoryAccess(LLVMContext &Context,
1733e6d15924SDimitry Andric                                             const DataLayout &DL, EVT VT,
1734e6d15924SDimitry Andric                                             const MachineMemOperand &MMO,
1735e3b55780SDimitry Andric                                             unsigned *Fast) const {
1736cfca06d7SDimitry Andric   return allowsMemoryAccess(Context, DL, VT, MMO.getAddrSpace(), MMO.getAlign(),
1737cfca06d7SDimitry Andric                             MMO.getFlags(), Fast);
1738dd58ef01SDimitry Andric }
1739dd58ef01SDimitry Andric 
allowsMemoryAccess(LLVMContext & Context,const DataLayout & DL,LLT Ty,const MachineMemOperand & MMO,unsigned * Fast) const1740b60736ecSDimitry Andric bool TargetLoweringBase::allowsMemoryAccess(LLVMContext &Context,
1741b60736ecSDimitry Andric                                             const DataLayout &DL, LLT Ty,
1742b60736ecSDimitry Andric                                             const MachineMemOperand &MMO,
1743e3b55780SDimitry Andric                                             unsigned *Fast) const {
1744c0981da4SDimitry Andric   EVT VT = getApproximateEVTForLLT(Ty, DL, Context);
1745c0981da4SDimitry Andric   return allowsMemoryAccess(Context, DL, VT, MMO.getAddrSpace(), MMO.getAlign(),
1746c0981da4SDimitry Andric                             MMO.getFlags(), Fast);
1747b60736ecSDimitry Andric }
1748b60736ecSDimitry Andric 
17494a16efa3SDimitry Andric //===----------------------------------------------------------------------===//
17504a16efa3SDimitry Andric //  TargetTransformInfo Helpers
17514a16efa3SDimitry Andric //===----------------------------------------------------------------------===//
17524a16efa3SDimitry Andric 
InstructionOpcodeToISD(unsigned Opcode) const17534a16efa3SDimitry Andric int TargetLoweringBase::InstructionOpcodeToISD(unsigned Opcode) const {
17544a16efa3SDimitry Andric   enum InstructionOpcodes {
17554a16efa3SDimitry Andric #define HANDLE_INST(NUM, OPCODE, CLASS) OPCODE = NUM,
17564a16efa3SDimitry Andric #define LAST_OTHER_INST(NUM) InstructionOpcodesCount = NUM
17574a16efa3SDimitry Andric #include "llvm/IR/Instruction.def"
17584a16efa3SDimitry Andric   };
17594a16efa3SDimitry Andric   switch (static_cast<InstructionOpcodes>(Opcode)) {
17604a16efa3SDimitry Andric   case Ret:            return 0;
17614a16efa3SDimitry Andric   case Br:             return 0;
17624a16efa3SDimitry Andric   case Switch:         return 0;
17634a16efa3SDimitry Andric   case IndirectBr:     return 0;
17644a16efa3SDimitry Andric   case Invoke:         return 0;
1765e6d15924SDimitry Andric   case CallBr:         return 0;
17664a16efa3SDimitry Andric   case Resume:         return 0;
17674a16efa3SDimitry Andric   case Unreachable:    return 0;
1768dd58ef01SDimitry Andric   case CleanupRet:     return 0;
1769dd58ef01SDimitry Andric   case CatchRet:       return 0;
1770dd58ef01SDimitry Andric   case CatchPad:       return 0;
1771dd58ef01SDimitry Andric   case CatchSwitch:    return 0;
1772dd58ef01SDimitry Andric   case CleanupPad:     return 0;
1773d8e91e46SDimitry Andric   case FNeg:           return ISD::FNEG;
17744a16efa3SDimitry Andric   case Add:            return ISD::ADD;
17754a16efa3SDimitry Andric   case FAdd:           return ISD::FADD;
17764a16efa3SDimitry Andric   case Sub:            return ISD::SUB;
17774a16efa3SDimitry Andric   case FSub:           return ISD::FSUB;
17784a16efa3SDimitry Andric   case Mul:            return ISD::MUL;
17794a16efa3SDimitry Andric   case FMul:           return ISD::FMUL;
17804a16efa3SDimitry Andric   case UDiv:           return ISD::UDIV;
17815ca98fd9SDimitry Andric   case SDiv:           return ISD::SDIV;
17824a16efa3SDimitry Andric   case FDiv:           return ISD::FDIV;
17834a16efa3SDimitry Andric   case URem:           return ISD::UREM;
17844a16efa3SDimitry Andric   case SRem:           return ISD::SREM;
17854a16efa3SDimitry Andric   case FRem:           return ISD::FREM;
17864a16efa3SDimitry Andric   case Shl:            return ISD::SHL;
17874a16efa3SDimitry Andric   case LShr:           return ISD::SRL;
17884a16efa3SDimitry Andric   case AShr:           return ISD::SRA;
17894a16efa3SDimitry Andric   case And:            return ISD::AND;
17904a16efa3SDimitry Andric   case Or:             return ISD::OR;
17914a16efa3SDimitry Andric   case Xor:            return ISD::XOR;
17924a16efa3SDimitry Andric   case Alloca:         return 0;
17934a16efa3SDimitry Andric   case Load:           return ISD::LOAD;
17944a16efa3SDimitry Andric   case Store:          return ISD::STORE;
17954a16efa3SDimitry Andric   case GetElementPtr:  return 0;
17964a16efa3SDimitry Andric   case Fence:          return 0;
17974a16efa3SDimitry Andric   case AtomicCmpXchg:  return 0;
17984a16efa3SDimitry Andric   case AtomicRMW:      return 0;
17994a16efa3SDimitry Andric   case Trunc:          return ISD::TRUNCATE;
18004a16efa3SDimitry Andric   case ZExt:           return ISD::ZERO_EXTEND;
18014a16efa3SDimitry Andric   case SExt:           return ISD::SIGN_EXTEND;
18024a16efa3SDimitry Andric   case FPToUI:         return ISD::FP_TO_UINT;
18034a16efa3SDimitry Andric   case FPToSI:         return ISD::FP_TO_SINT;
18044a16efa3SDimitry Andric   case UIToFP:         return ISD::UINT_TO_FP;
18054a16efa3SDimitry Andric   case SIToFP:         return ISD::SINT_TO_FP;
18064a16efa3SDimitry Andric   case FPTrunc:        return ISD::FP_ROUND;
18074a16efa3SDimitry Andric   case FPExt:          return ISD::FP_EXTEND;
18084a16efa3SDimitry Andric   case PtrToInt:       return ISD::BITCAST;
18094a16efa3SDimitry Andric   case IntToPtr:       return ISD::BITCAST;
18104a16efa3SDimitry Andric   case BitCast:        return ISD::BITCAST;
1811f8af5cf6SDimitry Andric   case AddrSpaceCast:  return ISD::ADDRSPACECAST;
18124a16efa3SDimitry Andric   case ICmp:           return ISD::SETCC;
18134a16efa3SDimitry Andric   case FCmp:           return ISD::SETCC;
18144a16efa3SDimitry Andric   case PHI:            return 0;
18154a16efa3SDimitry Andric   case Call:           return 0;
18164a16efa3SDimitry Andric   case Select:         return ISD::SELECT;
18174a16efa3SDimitry Andric   case UserOp1:        return 0;
18184a16efa3SDimitry Andric   case UserOp2:        return 0;
18194a16efa3SDimitry Andric   case VAArg:          return 0;
18204a16efa3SDimitry Andric   case ExtractElement: return ISD::EXTRACT_VECTOR_ELT;
18214a16efa3SDimitry Andric   case InsertElement:  return ISD::INSERT_VECTOR_ELT;
18224a16efa3SDimitry Andric   case ShuffleVector:  return ISD::VECTOR_SHUFFLE;
18234a16efa3SDimitry Andric   case ExtractValue:   return ISD::MERGE_VALUES;
18244a16efa3SDimitry Andric   case InsertValue:    return ISD::MERGE_VALUES;
18254a16efa3SDimitry Andric   case LandingPad:     return 0;
1826cfca06d7SDimitry Andric   case Freeze:         return ISD::FREEZE;
18274a16efa3SDimitry Andric   }
18284a16efa3SDimitry Andric 
18294a16efa3SDimitry Andric   llvm_unreachable("Unknown instruction type encountered!");
18304a16efa3SDimitry Andric }
18314a16efa3SDimitry Andric 
1832344a3780SDimitry Andric Value *
getDefaultSafeStackPointerLocation(IRBuilderBase & IRB,bool UseTLS) const1833344a3780SDimitry Andric TargetLoweringBase::getDefaultSafeStackPointerLocation(IRBuilderBase &IRB,
1834b915e9e0SDimitry Andric                                                        bool UseTLS) const {
1835b915e9e0SDimitry Andric   // compiler-rt provides a variable with a magic name.  Targets that do not
1836b915e9e0SDimitry Andric   // link with compiler-rt may also provide such a variable.
1837b915e9e0SDimitry Andric   Module *M = IRB.GetInsertBlock()->getParent()->getParent();
1838b915e9e0SDimitry Andric   const char *UnsafeStackPtrVar = "__safestack_unsafe_stack_ptr";
1839b915e9e0SDimitry Andric   auto UnsafeStackPtr =
1840b915e9e0SDimitry Andric       dyn_cast_or_null<GlobalVariable>(M->getNamedValue(UnsafeStackPtrVar));
1841b915e9e0SDimitry Andric 
1842b1c73532SDimitry Andric   Type *StackPtrTy = PointerType::getUnqual(M->getContext());
1843b915e9e0SDimitry Andric 
1844b915e9e0SDimitry Andric   if (!UnsafeStackPtr) {
1845b915e9e0SDimitry Andric     auto TLSModel = UseTLS ?
1846b915e9e0SDimitry Andric         GlobalValue::InitialExecTLSModel :
1847b915e9e0SDimitry Andric         GlobalValue::NotThreadLocal;
1848b915e9e0SDimitry Andric     // The global variable is not defined yet, define it ourselves.
1849b915e9e0SDimitry Andric     // We use the initial-exec TLS model because we do not support the
1850b915e9e0SDimitry Andric     // variable living anywhere other than in the main executable.
1851b915e9e0SDimitry Andric     UnsafeStackPtr = new GlobalVariable(
1852b915e9e0SDimitry Andric         *M, StackPtrTy, false, GlobalValue::ExternalLinkage, nullptr,
1853b915e9e0SDimitry Andric         UnsafeStackPtrVar, nullptr, TLSModel);
1854b915e9e0SDimitry Andric   } else {
1855b915e9e0SDimitry Andric     // The variable exists, check its type and attributes.
1856b915e9e0SDimitry Andric     if (UnsafeStackPtr->getValueType() != StackPtrTy)
1857b915e9e0SDimitry Andric       report_fatal_error(Twine(UnsafeStackPtrVar) + " must have void* type");
1858b915e9e0SDimitry Andric     if (UseTLS != UnsafeStackPtr->isThreadLocal())
1859b915e9e0SDimitry Andric       report_fatal_error(Twine(UnsafeStackPtrVar) + " must " +
1860b915e9e0SDimitry Andric                          (UseTLS ? "" : "not ") + "be thread-local");
1861b915e9e0SDimitry Andric   }
1862b915e9e0SDimitry Andric   return UnsafeStackPtr;
1863b915e9e0SDimitry Andric }
1864b915e9e0SDimitry Andric 
1865344a3780SDimitry Andric Value *
getSafeStackPointerLocation(IRBuilderBase & IRB) const1866344a3780SDimitry Andric TargetLoweringBase::getSafeStackPointerLocation(IRBuilderBase &IRB) const {
1867dd58ef01SDimitry Andric   if (!TM.getTargetTriple().isAndroid())
1868b915e9e0SDimitry Andric     return getDefaultSafeStackPointerLocation(IRB, true);
1869dd58ef01SDimitry Andric 
1870dd58ef01SDimitry Andric   // Android provides a libc function to retrieve the address of the current
1871dd58ef01SDimitry Andric   // thread's unsafe stack pointer.
1872dd58ef01SDimitry Andric   Module *M = IRB.GetInsertBlock()->getParent()->getParent();
1873b1c73532SDimitry Andric   auto *PtrTy = PointerType::getUnqual(M->getContext());
1874b1c73532SDimitry Andric   FunctionCallee Fn =
1875b1c73532SDimitry Andric       M->getOrInsertFunction("__safestack_pointer_address", PtrTy);
1876dd58ef01SDimitry Andric   return IRB.CreateCall(Fn);
1877dd58ef01SDimitry Andric }
1878dd58ef01SDimitry Andric 
18794a16efa3SDimitry Andric //===----------------------------------------------------------------------===//
18804a16efa3SDimitry Andric //  Loop Strength Reduction hooks
18814a16efa3SDimitry Andric //===----------------------------------------------------------------------===//
18824a16efa3SDimitry Andric 
18834a16efa3SDimitry Andric /// isLegalAddressingMode - Return true if the addressing mode represented
18844a16efa3SDimitry Andric /// by AM is legal for this target, for a load/store of the specified type.
isLegalAddressingMode(const DataLayout & DL,const AddrMode & AM,Type * Ty,unsigned AS,Instruction * I) const1885ee8648bdSDimitry Andric bool TargetLoweringBase::isLegalAddressingMode(const DataLayout &DL,
1886ee8648bdSDimitry Andric                                                const AddrMode &AM, Type *Ty,
1887044eb2f6SDimitry Andric                                                unsigned AS, Instruction *I) const {
18884a16efa3SDimitry Andric   // The default implementation of this implements a conservative RISCy, r+r and
18894a16efa3SDimitry Andric   // r+i addr mode.
18904a16efa3SDimitry Andric 
1891ac9a064cSDimitry Andric   // Scalable offsets not supported
1892ac9a064cSDimitry Andric   if (AM.ScalableOffset)
1893ac9a064cSDimitry Andric     return false;
1894ac9a064cSDimitry Andric 
18954a16efa3SDimitry Andric   // Allows a sign-extended 16-bit immediate field.
18964a16efa3SDimitry Andric   if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
18974a16efa3SDimitry Andric     return false;
18984a16efa3SDimitry Andric 
18994a16efa3SDimitry Andric   // No global is ever allowed as a base.
19004a16efa3SDimitry Andric   if (AM.BaseGV)
19014a16efa3SDimitry Andric     return false;
19024a16efa3SDimitry Andric 
19034a16efa3SDimitry Andric   // Only support r+r,
19044a16efa3SDimitry Andric   switch (AM.Scale) {
19054a16efa3SDimitry Andric   case 0:  // "r+i" or just "i", depending on HasBaseReg.
19064a16efa3SDimitry Andric     break;
19074a16efa3SDimitry Andric   case 1:
19084a16efa3SDimitry Andric     if (AM.HasBaseReg && AM.BaseOffs)  // "r+r+i" is not allowed.
19094a16efa3SDimitry Andric       return false;
19104a16efa3SDimitry Andric     // Otherwise we have r+r or r+i.
19114a16efa3SDimitry Andric     break;
19124a16efa3SDimitry Andric   case 2:
19134a16efa3SDimitry Andric     if (AM.HasBaseReg || AM.BaseOffs)  // 2*r+r  or  2*r+i is not allowed.
19144a16efa3SDimitry Andric       return false;
19154a16efa3SDimitry Andric     // Allow 2*r as r+r.
19164a16efa3SDimitry Andric     break;
19175ca98fd9SDimitry Andric   default: // Don't allow n * r
19185ca98fd9SDimitry Andric     return false;
19194a16efa3SDimitry Andric   }
19204a16efa3SDimitry Andric 
19214a16efa3SDimitry Andric   return true;
19224a16efa3SDimitry Andric }
192301095a5dSDimitry Andric 
192401095a5dSDimitry Andric //===----------------------------------------------------------------------===//
192501095a5dSDimitry Andric //  Stack Protector
192601095a5dSDimitry Andric //===----------------------------------------------------------------------===//
192701095a5dSDimitry Andric 
192801095a5dSDimitry Andric // For OpenBSD return its special guard variable. Otherwise return nullptr,
192901095a5dSDimitry Andric // so that SelectionDAG handle SSP.
getIRStackGuard(IRBuilderBase & IRB) const1930344a3780SDimitry Andric Value *TargetLoweringBase::getIRStackGuard(IRBuilderBase &IRB) const {
193101095a5dSDimitry Andric   if (getTargetMachine().getTargetTriple().isOSOpenBSD()) {
193201095a5dSDimitry Andric     Module &M = *IRB.GetInsertBlock()->getParent()->getParent();
1933b1c73532SDimitry Andric     PointerType *PtrTy = PointerType::getUnqual(M.getContext());
1934b60736ecSDimitry Andric     Constant *C = M.getOrInsertGlobal("__guard_local", PtrTy);
1935b60736ecSDimitry Andric     if (GlobalVariable *G = dyn_cast_or_null<GlobalVariable>(C))
1936b60736ecSDimitry Andric       G->setVisibility(GlobalValue::HiddenVisibility);
1937b60736ecSDimitry Andric     return C;
193801095a5dSDimitry Andric   }
193901095a5dSDimitry Andric   return nullptr;
194001095a5dSDimitry Andric }
194101095a5dSDimitry Andric 
194201095a5dSDimitry Andric // Currently only support "standard" __stack_chk_guard.
194301095a5dSDimitry Andric // TODO: add LOAD_STACK_GUARD support.
insertSSPDeclarations(Module & M) const194401095a5dSDimitry Andric void TargetLoweringBase::insertSSPDeclarations(Module &M) const {
1945b60736ecSDimitry Andric   if (!M.getNamedValue("__stack_chk_guard")) {
1946b1c73532SDimitry Andric     auto *GV = new GlobalVariable(M, PointerType::getUnqual(M.getContext()),
1947b1c73532SDimitry Andric                                   false, GlobalVariable::ExternalLinkage,
1948b1c73532SDimitry Andric                                   nullptr, "__stack_chk_guard");
1949c0981da4SDimitry Andric 
1950c0981da4SDimitry Andric     // FreeBSD has "__stack_chk_guard" defined externally on libc.so
19517fa27ce4SDimitry Andric     if (M.getDirectAccessExternalData() &&
1952c0981da4SDimitry Andric         !TM.getTargetTriple().isWindowsGNUEnvironment() &&
1953ac9a064cSDimitry Andric         !(TM.getTargetTriple().isPPC64() &&
1954ac9a064cSDimitry Andric           TM.getTargetTriple().isOSFreeBSD()) &&
1955b1c73532SDimitry Andric         (!TM.getTargetTriple().isOSDarwin() ||
1956b1c73532SDimitry Andric          TM.getRelocationModel() == Reloc::Static))
1957b60736ecSDimitry Andric       GV->setDSOLocal(true);
1958b60736ecSDimitry Andric   }
195901095a5dSDimitry Andric }
196001095a5dSDimitry Andric 
196101095a5dSDimitry Andric // Currently only support "standard" __stack_chk_guard.
196201095a5dSDimitry Andric // TODO: add LOAD_STACK_GUARD support.
getSDagStackGuard(const Module & M) const196301095a5dSDimitry Andric Value *TargetLoweringBase::getSDagStackGuard(const Module &M) const {
1964eb11fae6SDimitry Andric   return M.getNamedValue("__stack_chk_guard");
196501095a5dSDimitry Andric }
196601095a5dSDimitry Andric 
getSSPStackGuardCheck(const Module & M) const1967e6d15924SDimitry Andric Function *TargetLoweringBase::getSSPStackGuardCheck(const Module &M) const {
196801095a5dSDimitry Andric   return nullptr;
196901095a5dSDimitry Andric }
1970b915e9e0SDimitry Andric 
getMinimumJumpTableEntries() const1971b915e9e0SDimitry Andric unsigned TargetLoweringBase::getMinimumJumpTableEntries() const {
1972b915e9e0SDimitry Andric   return MinimumJumpTableEntries;
1973b915e9e0SDimitry Andric }
1974b915e9e0SDimitry Andric 
setMinimumJumpTableEntries(unsigned Val)1975b915e9e0SDimitry Andric void TargetLoweringBase::setMinimumJumpTableEntries(unsigned Val) {
1976b915e9e0SDimitry Andric   MinimumJumpTableEntries = Val;
1977b915e9e0SDimitry Andric }
1978b915e9e0SDimitry Andric 
getMinimumJumpTableDensity(bool OptForSize) const1979a303c417SDimitry Andric unsigned TargetLoweringBase::getMinimumJumpTableDensity(bool OptForSize) const {
1980a303c417SDimitry Andric   return OptForSize ? OptsizeJumpTableDensity : JumpTableDensity;
1981a303c417SDimitry Andric }
1982a303c417SDimitry Andric 
getMaximumJumpTableSize() const1983b915e9e0SDimitry Andric unsigned TargetLoweringBase::getMaximumJumpTableSize() const {
1984b915e9e0SDimitry Andric   return MaximumJumpTableSize;
1985b915e9e0SDimitry Andric }
1986b915e9e0SDimitry Andric 
setMaximumJumpTableSize(unsigned Val)1987b915e9e0SDimitry Andric void TargetLoweringBase::setMaximumJumpTableSize(unsigned Val) {
1988b915e9e0SDimitry Andric   MaximumJumpTableSize = Val;
1989b915e9e0SDimitry Andric }
1990b915e9e0SDimitry Andric 
isJumpTableRelative() const1991cfca06d7SDimitry Andric bool TargetLoweringBase::isJumpTableRelative() const {
1992cfca06d7SDimitry Andric   return getTargetMachine().isPositionIndependent();
1993cfca06d7SDimitry Andric }
1994cfca06d7SDimitry Andric 
getPrefLoopAlignment(MachineLoop * ML) const1995c0981da4SDimitry Andric Align TargetLoweringBase::getPrefLoopAlignment(MachineLoop *ML) const {
1996c0981da4SDimitry Andric   if (TM.Options.LoopAlignment)
1997c0981da4SDimitry Andric     return Align(TM.Options.LoopAlignment);
1998c0981da4SDimitry Andric   return PrefLoopAlignment;
1999c0981da4SDimitry Andric }
2000c0981da4SDimitry Andric 
getMaxPermittedBytesForAlignment(MachineBasicBlock * MBB) const20016f8fc217SDimitry Andric unsigned TargetLoweringBase::getMaxPermittedBytesForAlignment(
20026f8fc217SDimitry Andric     MachineBasicBlock *MBB) const {
20036f8fc217SDimitry Andric   return MaxBytesForAlignment;
20046f8fc217SDimitry Andric }
20056f8fc217SDimitry Andric 
2006b915e9e0SDimitry Andric //===----------------------------------------------------------------------===//
2007b915e9e0SDimitry Andric //  Reciprocal Estimates
2008b915e9e0SDimitry Andric //===----------------------------------------------------------------------===//
2009b915e9e0SDimitry Andric 
2010b915e9e0SDimitry Andric /// Get the reciprocal estimate attribute string for a function that will
2011b915e9e0SDimitry Andric /// override the target defaults.
getRecipEstimateForFunc(MachineFunction & MF)2012b915e9e0SDimitry Andric static StringRef getRecipEstimateForFunc(MachineFunction &MF) {
2013044eb2f6SDimitry Andric   const Function &F = MF.getFunction();
2014044eb2f6SDimitry Andric   return F.getFnAttribute("reciprocal-estimates").getValueAsString();
2015b915e9e0SDimitry Andric }
2016b915e9e0SDimitry Andric 
2017b915e9e0SDimitry Andric /// Construct a string for the given reciprocal operation of the given type.
2018b915e9e0SDimitry Andric /// This string should match the corresponding option to the front-end's
2019b915e9e0SDimitry Andric /// "-mrecip" flag assuming those strings have been passed through in an
2020b915e9e0SDimitry Andric /// attribute string. For example, "vec-divf" for a division of a vXf32.
getReciprocalOpName(bool IsSqrt,EVT VT)2021b915e9e0SDimitry Andric static std::string getReciprocalOpName(bool IsSqrt, EVT VT) {
2022b915e9e0SDimitry Andric   std::string Name = VT.isVector() ? "vec-" : "";
2023b915e9e0SDimitry Andric 
2024b915e9e0SDimitry Andric   Name += IsSqrt ? "sqrt" : "div";
2025b915e9e0SDimitry Andric 
2026145449b1SDimitry Andric   // TODO: Handle other float types?
2027b915e9e0SDimitry Andric   if (VT.getScalarType() == MVT::f64) {
2028b915e9e0SDimitry Andric     Name += "d";
2029145449b1SDimitry Andric   } else if (VT.getScalarType() == MVT::f16) {
2030145449b1SDimitry Andric     Name += "h";
2031b915e9e0SDimitry Andric   } else {
2032b915e9e0SDimitry Andric     assert(VT.getScalarType() == MVT::f32 &&
2033b915e9e0SDimitry Andric            "Unexpected FP type for reciprocal estimate");
2034b915e9e0SDimitry Andric     Name += "f";
2035b915e9e0SDimitry Andric   }
2036b915e9e0SDimitry Andric 
2037b915e9e0SDimitry Andric   return Name;
2038b915e9e0SDimitry Andric }
2039b915e9e0SDimitry Andric 
2040b915e9e0SDimitry Andric /// Return the character position and value (a single numeric character) of a
2041b915e9e0SDimitry Andric /// customized refinement operation in the input string if it exists. Return
2042b915e9e0SDimitry Andric /// false if there is no customized refinement step count.
parseRefinementStep(StringRef In,size_t & Position,uint8_t & Value)2043b915e9e0SDimitry Andric static bool parseRefinementStep(StringRef In, size_t &Position,
2044b915e9e0SDimitry Andric                                 uint8_t &Value) {
2045b915e9e0SDimitry Andric   const char RefStepToken = ':';
2046b915e9e0SDimitry Andric   Position = In.find(RefStepToken);
2047b915e9e0SDimitry Andric   if (Position == StringRef::npos)
2048b915e9e0SDimitry Andric     return false;
2049b915e9e0SDimitry Andric 
2050b915e9e0SDimitry Andric   StringRef RefStepString = In.substr(Position + 1);
2051b915e9e0SDimitry Andric   // Allow exactly one numeric character for the additional refinement
2052b915e9e0SDimitry Andric   // step parameter.
2053b915e9e0SDimitry Andric   if (RefStepString.size() == 1) {
2054b915e9e0SDimitry Andric     char RefStepChar = RefStepString[0];
2055b60736ecSDimitry Andric     if (isDigit(RefStepChar)) {
2056b915e9e0SDimitry Andric       Value = RefStepChar - '0';
2057b915e9e0SDimitry Andric       return true;
2058b915e9e0SDimitry Andric     }
2059b915e9e0SDimitry Andric   }
2060b915e9e0SDimitry Andric   report_fatal_error("Invalid refinement step for -recip.");
2061b915e9e0SDimitry Andric }
2062b915e9e0SDimitry Andric 
2063b915e9e0SDimitry Andric /// For the input attribute string, return one of the ReciprocalEstimate enum
2064b915e9e0SDimitry Andric /// status values (enabled, disabled, or not specified) for this operation on
2065b915e9e0SDimitry Andric /// the specified data type.
getOpEnabled(bool IsSqrt,EVT VT,StringRef Override)2066b915e9e0SDimitry Andric static int getOpEnabled(bool IsSqrt, EVT VT, StringRef Override) {
2067b915e9e0SDimitry Andric   if (Override.empty())
2068b915e9e0SDimitry Andric     return TargetLoweringBase::ReciprocalEstimate::Unspecified;
2069b915e9e0SDimitry Andric 
2070b915e9e0SDimitry Andric   SmallVector<StringRef, 4> OverrideVector;
2071eb11fae6SDimitry Andric   Override.split(OverrideVector, ',');
2072b915e9e0SDimitry Andric   unsigned NumArgs = OverrideVector.size();
2073b915e9e0SDimitry Andric 
2074b915e9e0SDimitry Andric   // Check if "all", "none", or "default" was specified.
2075b915e9e0SDimitry Andric   if (NumArgs == 1) {
2076b915e9e0SDimitry Andric     // Look for an optional setting of the number of refinement steps needed
2077b915e9e0SDimitry Andric     // for this type of reciprocal operation.
2078b915e9e0SDimitry Andric     size_t RefPos;
2079b915e9e0SDimitry Andric     uint8_t RefSteps;
2080b915e9e0SDimitry Andric     if (parseRefinementStep(Override, RefPos, RefSteps)) {
2081b915e9e0SDimitry Andric       // Split the string for further processing.
2082b915e9e0SDimitry Andric       Override = Override.substr(0, RefPos);
2083b915e9e0SDimitry Andric     }
2084b915e9e0SDimitry Andric 
2085b915e9e0SDimitry Andric     // All reciprocal types are enabled.
2086b915e9e0SDimitry Andric     if (Override == "all")
2087b915e9e0SDimitry Andric       return TargetLoweringBase::ReciprocalEstimate::Enabled;
2088b915e9e0SDimitry Andric 
2089b915e9e0SDimitry Andric     // All reciprocal types are disabled.
2090b915e9e0SDimitry Andric     if (Override == "none")
2091b915e9e0SDimitry Andric       return TargetLoweringBase::ReciprocalEstimate::Disabled;
2092b915e9e0SDimitry Andric 
2093b915e9e0SDimitry Andric     // Target defaults for enablement are used.
2094b915e9e0SDimitry Andric     if (Override == "default")
2095b915e9e0SDimitry Andric       return TargetLoweringBase::ReciprocalEstimate::Unspecified;
2096b915e9e0SDimitry Andric   }
2097b915e9e0SDimitry Andric 
2098b915e9e0SDimitry Andric   // The attribute string may omit the size suffix ('f'/'d').
2099b915e9e0SDimitry Andric   std::string VTName = getReciprocalOpName(IsSqrt, VT);
2100b915e9e0SDimitry Andric   std::string VTNameNoSize = VTName;
2101b915e9e0SDimitry Andric   VTNameNoSize.pop_back();
2102b915e9e0SDimitry Andric   static const char DisabledPrefix = '!';
2103b915e9e0SDimitry Andric 
2104b915e9e0SDimitry Andric   for (StringRef RecipType : OverrideVector) {
2105b915e9e0SDimitry Andric     size_t RefPos;
2106b915e9e0SDimitry Andric     uint8_t RefSteps;
2107b915e9e0SDimitry Andric     if (parseRefinementStep(RecipType, RefPos, RefSteps))
2108b915e9e0SDimitry Andric       RecipType = RecipType.substr(0, RefPos);
2109b915e9e0SDimitry Andric 
2110b915e9e0SDimitry Andric     // Ignore the disablement token for string matching.
2111b915e9e0SDimitry Andric     bool IsDisabled = RecipType[0] == DisabledPrefix;
2112b915e9e0SDimitry Andric     if (IsDisabled)
2113b915e9e0SDimitry Andric       RecipType = RecipType.substr(1);
2114b915e9e0SDimitry Andric 
2115ac9a064cSDimitry Andric     if (RecipType == VTName || RecipType == VTNameNoSize)
2116b915e9e0SDimitry Andric       return IsDisabled ? TargetLoweringBase::ReciprocalEstimate::Disabled
2117b915e9e0SDimitry Andric                         : TargetLoweringBase::ReciprocalEstimate::Enabled;
2118b915e9e0SDimitry Andric   }
2119b915e9e0SDimitry Andric 
2120b915e9e0SDimitry Andric   return TargetLoweringBase::ReciprocalEstimate::Unspecified;
2121b915e9e0SDimitry Andric }
2122b915e9e0SDimitry Andric 
2123b915e9e0SDimitry Andric /// For the input attribute string, return the customized refinement step count
2124b915e9e0SDimitry Andric /// for this operation on the specified data type. If the step count does not
2125b915e9e0SDimitry Andric /// exist, return the ReciprocalEstimate enum value for unspecified.
getOpRefinementSteps(bool IsSqrt,EVT VT,StringRef Override)2126b915e9e0SDimitry Andric static int getOpRefinementSteps(bool IsSqrt, EVT VT, StringRef Override) {
2127b915e9e0SDimitry Andric   if (Override.empty())
2128b915e9e0SDimitry Andric     return TargetLoweringBase::ReciprocalEstimate::Unspecified;
2129b915e9e0SDimitry Andric 
2130b915e9e0SDimitry Andric   SmallVector<StringRef, 4> OverrideVector;
2131eb11fae6SDimitry Andric   Override.split(OverrideVector, ',');
2132b915e9e0SDimitry Andric   unsigned NumArgs = OverrideVector.size();
2133b915e9e0SDimitry Andric 
2134b915e9e0SDimitry Andric   // Check if "all", "default", or "none" was specified.
2135b915e9e0SDimitry Andric   if (NumArgs == 1) {
2136b915e9e0SDimitry Andric     // Look for an optional setting of the number of refinement steps needed
2137b915e9e0SDimitry Andric     // for this type of reciprocal operation.
2138b915e9e0SDimitry Andric     size_t RefPos;
2139b915e9e0SDimitry Andric     uint8_t RefSteps;
2140b915e9e0SDimitry Andric     if (!parseRefinementStep(Override, RefPos, RefSteps))
2141b915e9e0SDimitry Andric       return TargetLoweringBase::ReciprocalEstimate::Unspecified;
2142b915e9e0SDimitry Andric 
2143b915e9e0SDimitry Andric     // Split the string for further processing.
2144b915e9e0SDimitry Andric     Override = Override.substr(0, RefPos);
2145b915e9e0SDimitry Andric     assert(Override != "none" &&
2146b915e9e0SDimitry Andric            "Disabled reciprocals, but specifed refinement steps?");
2147b915e9e0SDimitry Andric 
2148b915e9e0SDimitry Andric     // If this is a general override, return the specified number of steps.
2149b915e9e0SDimitry Andric     if (Override == "all" || Override == "default")
2150b915e9e0SDimitry Andric       return RefSteps;
2151b915e9e0SDimitry Andric   }
2152b915e9e0SDimitry Andric 
2153b915e9e0SDimitry Andric   // The attribute string may omit the size suffix ('f'/'d').
2154b915e9e0SDimitry Andric   std::string VTName = getReciprocalOpName(IsSqrt, VT);
2155b915e9e0SDimitry Andric   std::string VTNameNoSize = VTName;
2156b915e9e0SDimitry Andric   VTNameNoSize.pop_back();
2157b915e9e0SDimitry Andric 
2158b915e9e0SDimitry Andric   for (StringRef RecipType : OverrideVector) {
2159b915e9e0SDimitry Andric     size_t RefPos;
2160b915e9e0SDimitry Andric     uint8_t RefSteps;
2161b915e9e0SDimitry Andric     if (!parseRefinementStep(RecipType, RefPos, RefSteps))
2162b915e9e0SDimitry Andric       continue;
2163b915e9e0SDimitry Andric 
2164b915e9e0SDimitry Andric     RecipType = RecipType.substr(0, RefPos);
2165ac9a064cSDimitry Andric     if (RecipType == VTName || RecipType == VTNameNoSize)
2166b915e9e0SDimitry Andric       return RefSteps;
2167b915e9e0SDimitry Andric   }
2168b915e9e0SDimitry Andric 
2169b915e9e0SDimitry Andric   return TargetLoweringBase::ReciprocalEstimate::Unspecified;
2170b915e9e0SDimitry Andric }
2171b915e9e0SDimitry Andric 
getRecipEstimateSqrtEnabled(EVT VT,MachineFunction & MF) const2172b915e9e0SDimitry Andric int TargetLoweringBase::getRecipEstimateSqrtEnabled(EVT VT,
2173b915e9e0SDimitry Andric                                                     MachineFunction &MF) const {
2174b915e9e0SDimitry Andric   return getOpEnabled(true, VT, getRecipEstimateForFunc(MF));
2175b915e9e0SDimitry Andric }
2176b915e9e0SDimitry Andric 
getRecipEstimateDivEnabled(EVT VT,MachineFunction & MF) const2177b915e9e0SDimitry Andric int TargetLoweringBase::getRecipEstimateDivEnabled(EVT VT,
2178b915e9e0SDimitry Andric                                                    MachineFunction &MF) const {
2179b915e9e0SDimitry Andric   return getOpEnabled(false, VT, getRecipEstimateForFunc(MF));
2180b915e9e0SDimitry Andric }
2181b915e9e0SDimitry Andric 
getSqrtRefinementSteps(EVT VT,MachineFunction & MF) const2182b915e9e0SDimitry Andric int TargetLoweringBase::getSqrtRefinementSteps(EVT VT,
2183b915e9e0SDimitry Andric                                                MachineFunction &MF) const {
2184b915e9e0SDimitry Andric   return getOpRefinementSteps(true, VT, getRecipEstimateForFunc(MF));
2185b915e9e0SDimitry Andric }
2186b915e9e0SDimitry Andric 
getDivRefinementSteps(EVT VT,MachineFunction & MF) const2187b915e9e0SDimitry Andric int TargetLoweringBase::getDivRefinementSteps(EVT VT,
2188b915e9e0SDimitry Andric                                               MachineFunction &MF) const {
2189b915e9e0SDimitry Andric   return getOpRefinementSteps(false, VT, getRecipEstimateForFunc(MF));
2190b915e9e0SDimitry Andric }
2191a303c417SDimitry Andric 
isLoadBitCastBeneficial(EVT LoadVT,EVT BitcastVT,const SelectionDAG & DAG,const MachineMemOperand & MMO) const2192e3b55780SDimitry Andric bool TargetLoweringBase::isLoadBitCastBeneficial(
2193e3b55780SDimitry Andric     EVT LoadVT, EVT BitcastVT, const SelectionDAG &DAG,
2194e3b55780SDimitry Andric     const MachineMemOperand &MMO) const {
2195e3b55780SDimitry Andric   // Single-element vectors are scalarized, so we should generally avoid having
2196e3b55780SDimitry Andric   // any memory operations on such types, as they would get scalarized too.
2197e3b55780SDimitry Andric   if (LoadVT.isFixedLengthVector() && BitcastVT.isFixedLengthVector() &&
2198e3b55780SDimitry Andric       BitcastVT.getVectorNumElements() == 1)
2199e3b55780SDimitry Andric     return false;
2200e3b55780SDimitry Andric 
2201e3b55780SDimitry Andric   // Don't do if we could do an indexed load on the original type, but not on
2202e3b55780SDimitry Andric   // the new one.
2203e3b55780SDimitry Andric   if (!LoadVT.isSimple() || !BitcastVT.isSimple())
2204e3b55780SDimitry Andric     return true;
2205e3b55780SDimitry Andric 
2206e3b55780SDimitry Andric   MVT LoadMVT = LoadVT.getSimpleVT();
2207e3b55780SDimitry Andric 
2208e3b55780SDimitry Andric   // Don't bother doing this if it's just going to be promoted again later, as
2209e3b55780SDimitry Andric   // doing so might interfere with other combines.
2210e3b55780SDimitry Andric   if (getOperationAction(ISD::LOAD, LoadMVT) == Promote &&
2211e3b55780SDimitry Andric       getTypeToPromoteTo(ISD::LOAD, LoadMVT) == BitcastVT.getSimpleVT())
2212e3b55780SDimitry Andric     return false;
2213e3b55780SDimitry Andric 
2214e3b55780SDimitry Andric   unsigned Fast = 0;
2215e3b55780SDimitry Andric   return allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), BitcastVT,
2216e3b55780SDimitry Andric                             MMO, &Fast) &&
2217e3b55780SDimitry Andric          Fast;
2218e3b55780SDimitry Andric }
2219e3b55780SDimitry Andric 
finalizeLowering(MachineFunction & MF) const2220a303c417SDimitry Andric void TargetLoweringBase::finalizeLowering(MachineFunction &MF) const {
2221ac9a064cSDimitry Andric   MF.getRegInfo().freezeReservedRegs();
2222a303c417SDimitry Andric }
2223cfca06d7SDimitry Andric 
getLoadMemOperandFlags(const LoadInst & LI,const DataLayout & DL,AssumptionCache * AC,const TargetLibraryInfo * LibInfo) const2224e3b55780SDimitry Andric MachineMemOperand::Flags TargetLoweringBase::getLoadMemOperandFlags(
2225e3b55780SDimitry Andric     const LoadInst &LI, const DataLayout &DL, AssumptionCache *AC,
2226e3b55780SDimitry Andric     const TargetLibraryInfo *LibInfo) const {
2227cfca06d7SDimitry Andric   MachineMemOperand::Flags Flags = MachineMemOperand::MOLoad;
2228cfca06d7SDimitry Andric   if (LI.isVolatile())
2229cfca06d7SDimitry Andric     Flags |= MachineMemOperand::MOVolatile;
2230cfca06d7SDimitry Andric 
2231cfca06d7SDimitry Andric   if (LI.hasMetadata(LLVMContext::MD_nontemporal))
2232cfca06d7SDimitry Andric     Flags |= MachineMemOperand::MONonTemporal;
2233cfca06d7SDimitry Andric 
2234cfca06d7SDimitry Andric   if (LI.hasMetadata(LLVMContext::MD_invariant_load))
2235cfca06d7SDimitry Andric     Flags |= MachineMemOperand::MOInvariant;
2236cfca06d7SDimitry Andric 
2237e3b55780SDimitry Andric   if (isDereferenceableAndAlignedPointer(LI.getPointerOperand(), LI.getType(),
2238e3b55780SDimitry Andric                                          LI.getAlign(), DL, &LI, AC,
2239e3b55780SDimitry Andric                                          /*DT=*/nullptr, LibInfo))
2240cfca06d7SDimitry Andric     Flags |= MachineMemOperand::MODereferenceable;
2241cfca06d7SDimitry Andric 
2242cfca06d7SDimitry Andric   Flags |= getTargetMMOFlags(LI);
2243cfca06d7SDimitry Andric   return Flags;
2244cfca06d7SDimitry Andric }
2245cfca06d7SDimitry Andric 
2246cfca06d7SDimitry Andric MachineMemOperand::Flags
getStoreMemOperandFlags(const StoreInst & SI,const DataLayout & DL) const2247cfca06d7SDimitry Andric TargetLoweringBase::getStoreMemOperandFlags(const StoreInst &SI,
2248cfca06d7SDimitry Andric                                             const DataLayout &DL) const {
2249cfca06d7SDimitry Andric   MachineMemOperand::Flags Flags = MachineMemOperand::MOStore;
2250cfca06d7SDimitry Andric 
2251cfca06d7SDimitry Andric   if (SI.isVolatile())
2252cfca06d7SDimitry Andric     Flags |= MachineMemOperand::MOVolatile;
2253cfca06d7SDimitry Andric 
2254cfca06d7SDimitry Andric   if (SI.hasMetadata(LLVMContext::MD_nontemporal))
2255cfca06d7SDimitry Andric     Flags |= MachineMemOperand::MONonTemporal;
2256cfca06d7SDimitry Andric 
2257cfca06d7SDimitry Andric   // FIXME: Not preserving dereferenceable
2258cfca06d7SDimitry Andric   Flags |= getTargetMMOFlags(SI);
2259cfca06d7SDimitry Andric   return Flags;
2260cfca06d7SDimitry Andric }
2261cfca06d7SDimitry Andric 
2262cfca06d7SDimitry Andric MachineMemOperand::Flags
getAtomicMemOperandFlags(const Instruction & AI,const DataLayout & DL) const2263cfca06d7SDimitry Andric TargetLoweringBase::getAtomicMemOperandFlags(const Instruction &AI,
2264cfca06d7SDimitry Andric                                              const DataLayout &DL) const {
2265cfca06d7SDimitry Andric   auto Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
2266cfca06d7SDimitry Andric 
2267cfca06d7SDimitry Andric   if (const AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(&AI)) {
2268cfca06d7SDimitry Andric     if (RMW->isVolatile())
2269cfca06d7SDimitry Andric       Flags |= MachineMemOperand::MOVolatile;
2270cfca06d7SDimitry Andric   } else if (const AtomicCmpXchgInst *CmpX = dyn_cast<AtomicCmpXchgInst>(&AI)) {
2271cfca06d7SDimitry Andric     if (CmpX->isVolatile())
2272cfca06d7SDimitry Andric       Flags |= MachineMemOperand::MOVolatile;
2273cfca06d7SDimitry Andric   } else
2274cfca06d7SDimitry Andric     llvm_unreachable("not an atomic instruction");
2275cfca06d7SDimitry Andric 
2276cfca06d7SDimitry Andric   // FIXME: Not preserving dereferenceable
2277cfca06d7SDimitry Andric   Flags |= getTargetMMOFlags(AI);
2278cfca06d7SDimitry Andric   return Flags;
2279cfca06d7SDimitry Andric }
2280cfca06d7SDimitry Andric 
emitLeadingFence(IRBuilderBase & Builder,Instruction * Inst,AtomicOrdering Ord) const2281344a3780SDimitry Andric Instruction *TargetLoweringBase::emitLeadingFence(IRBuilderBase &Builder,
2282344a3780SDimitry Andric                                                   Instruction *Inst,
2283344a3780SDimitry Andric                                                   AtomicOrdering Ord) const {
2284344a3780SDimitry Andric   if (isReleaseOrStronger(Ord) && Inst->hasAtomicStore())
2285344a3780SDimitry Andric     return Builder.CreateFence(Ord);
2286344a3780SDimitry Andric   else
2287344a3780SDimitry Andric     return nullptr;
2288344a3780SDimitry Andric }
2289344a3780SDimitry Andric 
emitTrailingFence(IRBuilderBase & Builder,Instruction * Inst,AtomicOrdering Ord) const2290344a3780SDimitry Andric Instruction *TargetLoweringBase::emitTrailingFence(IRBuilderBase &Builder,
2291344a3780SDimitry Andric                                                    Instruction *Inst,
2292344a3780SDimitry Andric                                                    AtomicOrdering Ord) const {
2293344a3780SDimitry Andric   if (isAcquireOrStronger(Ord))
2294344a3780SDimitry Andric     return Builder.CreateFence(Ord);
2295344a3780SDimitry Andric   else
2296344a3780SDimitry Andric     return nullptr;
2297344a3780SDimitry Andric }
2298344a3780SDimitry Andric 
2299cfca06d7SDimitry Andric //===----------------------------------------------------------------------===//
2300cfca06d7SDimitry Andric //  GlobalISel Hooks
2301cfca06d7SDimitry Andric //===----------------------------------------------------------------------===//
2302cfca06d7SDimitry Andric 
shouldLocalize(const MachineInstr & MI,const TargetTransformInfo * TTI) const2303cfca06d7SDimitry Andric bool TargetLoweringBase::shouldLocalize(const MachineInstr &MI,
2304cfca06d7SDimitry Andric                                         const TargetTransformInfo *TTI) const {
2305cfca06d7SDimitry Andric   auto &MF = *MI.getMF();
2306cfca06d7SDimitry Andric   auto &MRI = MF.getRegInfo();
2307cfca06d7SDimitry Andric   // Assuming a spill and reload of a value has a cost of 1 instruction each,
2308cfca06d7SDimitry Andric   // this helper function computes the maximum number of uses we should consider
2309cfca06d7SDimitry Andric   // for remat. E.g. on arm64 global addresses take 2 insts to materialize. We
2310cfca06d7SDimitry Andric   // break even in terms of code size when the original MI has 2 users vs
2311cfca06d7SDimitry Andric   // choosing to potentially spill. Any more than 2 users we we have a net code
2312cfca06d7SDimitry Andric   // size increase. This doesn't take into account register pressure though.
2313cfca06d7SDimitry Andric   auto maxUses = [](unsigned RematCost) {
2314cfca06d7SDimitry Andric     // A cost of 1 means remats are basically free.
2315cfca06d7SDimitry Andric     if (RematCost == 1)
2316e3b55780SDimitry Andric       return std::numeric_limits<unsigned>::max();
2317cfca06d7SDimitry Andric     if (RematCost == 2)
2318cfca06d7SDimitry Andric       return 2U;
2319cfca06d7SDimitry Andric 
2320cfca06d7SDimitry Andric     // Remat is too expensive, only sink if there's one user.
2321cfca06d7SDimitry Andric     if (RematCost > 2)
2322cfca06d7SDimitry Andric       return 1U;
2323cfca06d7SDimitry Andric     llvm_unreachable("Unexpected remat cost");
2324cfca06d7SDimitry Andric   };
2325cfca06d7SDimitry Andric 
2326cfca06d7SDimitry Andric   switch (MI.getOpcode()) {
2327cfca06d7SDimitry Andric   default:
2328cfca06d7SDimitry Andric     return false;
2329cfca06d7SDimitry Andric   // Constants-like instructions should be close to their users.
2330cfca06d7SDimitry Andric   // We don't want long live-ranges for them.
2331cfca06d7SDimitry Andric   case TargetOpcode::G_CONSTANT:
2332cfca06d7SDimitry Andric   case TargetOpcode::G_FCONSTANT:
2333cfca06d7SDimitry Andric   case TargetOpcode::G_FRAME_INDEX:
2334cfca06d7SDimitry Andric   case TargetOpcode::G_INTTOPTR:
2335cfca06d7SDimitry Andric     return true;
2336cfca06d7SDimitry Andric   case TargetOpcode::G_GLOBAL_VALUE: {
2337cfca06d7SDimitry Andric     unsigned RematCost = TTI->getGISelRematGlobalCost();
2338cfca06d7SDimitry Andric     Register Reg = MI.getOperand(0).getReg();
2339cfca06d7SDimitry Andric     unsigned MaxUses = maxUses(RematCost);
2340cfca06d7SDimitry Andric     if (MaxUses == UINT_MAX)
2341cfca06d7SDimitry Andric       return true; // Remats are "free" so always localize.
2342e3b55780SDimitry Andric     return MRI.hasAtMostUserInstrs(Reg, MaxUses);
2343cfca06d7SDimitry Andric   }
2344cfca06d7SDimitry Andric   }
2345cfca06d7SDimitry Andric }
2346