1 //===- TargetLoweringBase.cpp - Implement the TargetLoweringBase class ----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This implements the TargetLoweringBase class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ADT/BitVector.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/Analysis/Loads.h"
21 #include "llvm/Analysis/TargetTransformInfo.h"
22 #include "llvm/CodeGen/Analysis.h"
23 #include "llvm/CodeGen/ISDOpcodes.h"
24 #include "llvm/CodeGen/MachineBasicBlock.h"
25 #include "llvm/CodeGen/MachineFrameInfo.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineInstr.h"
28 #include "llvm/CodeGen/MachineInstrBuilder.h"
29 #include "llvm/CodeGen/MachineMemOperand.h"
30 #include "llvm/CodeGen/MachineOperand.h"
31 #include "llvm/CodeGen/MachineRegisterInfo.h"
32 #include "llvm/CodeGen/RuntimeLibcalls.h"
33 #include "llvm/CodeGen/StackMaps.h"
34 #include "llvm/CodeGen/TargetLowering.h"
35 #include "llvm/CodeGen/TargetOpcodes.h"
36 #include "llvm/CodeGen/TargetRegisterInfo.h"
37 #include "llvm/CodeGen/ValueTypes.h"
38 #include "llvm/IR/Attributes.h"
39 #include "llvm/IR/CallingConv.h"
40 #include "llvm/IR/DataLayout.h"
41 #include "llvm/IR/DerivedTypes.h"
42 #include "llvm/IR/Function.h"
43 #include "llvm/IR/GlobalValue.h"
44 #include "llvm/IR/GlobalVariable.h"
45 #include "llvm/IR/IRBuilder.h"
46 #include "llvm/IR/Module.h"
47 #include "llvm/IR/Type.h"
48 #include "llvm/Support/Casting.h"
49 #include "llvm/Support/CommandLine.h"
50 #include "llvm/Support/Compiler.h"
51 #include "llvm/Support/ErrorHandling.h"
52 #include "llvm/Support/MachineValueType.h"
53 #include "llvm/Support/MathExtras.h"
54 #include "llvm/Target/TargetMachine.h"
55 #include "llvm/Transforms/Utils/SizeOpts.h"
56 #include <algorithm>
57 #include <cassert>
58 #include <cstddef>
59 #include <cstdint>
60 #include <cstring>
61 #include <iterator>
62 #include <string>
63 #include <tuple>
64 #include <utility>
65 
66 using namespace llvm;
67 
68 static cl::opt<bool> JumpIsExpensiveOverride(
69     "jump-is-expensive", cl::init(false),
70     cl::desc("Do not create extra branches to split comparison logic."),
71     cl::Hidden);
72 
73 static cl::opt<unsigned> MinimumJumpTableEntries
74   ("min-jump-table-entries", cl::init(4), cl::Hidden,
75    cl::desc("Set minimum number of entries to use a jump table."));
76 
77 static cl::opt<unsigned> MaximumJumpTableSize
78   ("max-jump-table-size", cl::init(UINT_MAX), cl::Hidden,
79    cl::desc("Set maximum size of jump tables."));
80 
81 /// Minimum jump table density for normal functions.
82 static cl::opt<unsigned>
83     JumpTableDensity("jump-table-density", cl::init(10), cl::Hidden,
84                      cl::desc("Minimum density for building a jump table in "
85                               "a normal function"));
86 
87 /// Minimum jump table density for -Os or -Oz functions.
88 static cl::opt<unsigned> OptsizeJumpTableDensity(
89     "optsize-jump-table-density", cl::init(40), cl::Hidden,
90     cl::desc("Minimum density for building a jump table in "
91              "an optsize function"));
92 
93 // FIXME: This option is only to test if the strict fp operation processed
94 // correctly by preventing mutating strict fp operation to normal fp operation
95 // during development. When the backend supports strict float operation, this
96 // option will be meaningless.
97 static cl::opt<bool> DisableStrictNodeMutation("disable-strictnode-mutation",
98        cl::desc("Don't mutate strict-float node to a legalize node"),
99        cl::init(false), cl::Hidden);
100 
101 static bool darwinHasSinCos(const Triple &TT) {
102   assert(TT.isOSDarwin() && "should be called with darwin triple");
103   // Don't bother with 32 bit x86.
104   if (TT.getArch() == Triple::x86)
105     return false;
106   // Macos < 10.9 has no sincos_stret.
107   if (TT.isMacOSX())
108     return !TT.isMacOSXVersionLT(10, 9) && TT.isArch64Bit();
109   // iOS < 7.0 has no sincos_stret.
110   if (TT.isiOS())
111     return !TT.isOSVersionLT(7, 0);
112   // Any other darwin such as WatchOS/TvOS is new enough.
113   return true;
114 }
115 
116 void TargetLoweringBase::InitLibcalls(const Triple &TT) {
117 #define HANDLE_LIBCALL(code, name) \
118   setLibcallName(RTLIB::code, name);
119 #include "llvm/IR/RuntimeLibcalls.def"
120 #undef HANDLE_LIBCALL
121   // Initialize calling conventions to their default.
122   for (int LC = 0; LC < RTLIB::UNKNOWN_LIBCALL; ++LC)
123     setLibcallCallingConv((RTLIB::Libcall)LC, CallingConv::C);
124 
125   // For IEEE quad-precision libcall names, PPC uses "kf" instead of "tf".
126   if (TT.isPPC()) {
127     setLibcallName(RTLIB::ADD_F128, "__addkf3");
128     setLibcallName(RTLIB::SUB_F128, "__subkf3");
129     setLibcallName(RTLIB::MUL_F128, "__mulkf3");
130     setLibcallName(RTLIB::DIV_F128, "__divkf3");
131     setLibcallName(RTLIB::POWI_F128, "__powikf2");
132     setLibcallName(RTLIB::FPEXT_F32_F128, "__extendsfkf2");
133     setLibcallName(RTLIB::FPEXT_F64_F128, "__extenddfkf2");
134     setLibcallName(RTLIB::FPROUND_F128_F32, "__trunckfsf2");
135     setLibcallName(RTLIB::FPROUND_F128_F64, "__trunckfdf2");
136     setLibcallName(RTLIB::FPTOSINT_F128_I32, "__fixkfsi");
137     setLibcallName(RTLIB::FPTOSINT_F128_I64, "__fixkfdi");
138     setLibcallName(RTLIB::FPTOSINT_F128_I128, "__fixkfti");
139     setLibcallName(RTLIB::FPTOUINT_F128_I32, "__fixunskfsi");
140     setLibcallName(RTLIB::FPTOUINT_F128_I64, "__fixunskfdi");
141     setLibcallName(RTLIB::FPTOUINT_F128_I128, "__fixunskfti");
142     setLibcallName(RTLIB::SINTTOFP_I32_F128, "__floatsikf");
143     setLibcallName(RTLIB::SINTTOFP_I64_F128, "__floatdikf");
144     setLibcallName(RTLIB::SINTTOFP_I128_F128, "__floattikf");
145     setLibcallName(RTLIB::UINTTOFP_I32_F128, "__floatunsikf");
146     setLibcallName(RTLIB::UINTTOFP_I64_F128, "__floatundikf");
147     setLibcallName(RTLIB::UINTTOFP_I128_F128, "__floatuntikf");
148     setLibcallName(RTLIB::OEQ_F128, "__eqkf2");
149     setLibcallName(RTLIB::UNE_F128, "__nekf2");
150     setLibcallName(RTLIB::OGE_F128, "__gekf2");
151     setLibcallName(RTLIB::OLT_F128, "__ltkf2");
152     setLibcallName(RTLIB::OLE_F128, "__lekf2");
153     setLibcallName(RTLIB::OGT_F128, "__gtkf2");
154     setLibcallName(RTLIB::UO_F128, "__unordkf2");
155   }
156 
157   // A few names are different on particular architectures or environments.
158   if (TT.isOSDarwin()) {
159     // For f16/f32 conversions, Darwin uses the standard naming scheme, instead
160     // of the gnueabi-style __gnu_*_ieee.
161     // FIXME: What about other targets?
162     setLibcallName(RTLIB::FPEXT_F16_F32, "__extendhfsf2");
163     setLibcallName(RTLIB::FPROUND_F32_F16, "__truncsfhf2");
164 
165     // Some darwins have an optimized __bzero/bzero function.
166     switch (TT.getArch()) {
167     case Triple::x86:
168     case Triple::x86_64:
169       if (TT.isMacOSX() && !TT.isMacOSXVersionLT(10, 6))
170         setLibcallName(RTLIB::BZERO, "__bzero");
171       break;
172     case Triple::aarch64:
173     case Triple::aarch64_32:
174       setLibcallName(RTLIB::BZERO, "bzero");
175       break;
176     default:
177       break;
178     }
179 
180     if (darwinHasSinCos(TT)) {
181       setLibcallName(RTLIB::SINCOS_STRET_F32, "__sincosf_stret");
182       setLibcallName(RTLIB::SINCOS_STRET_F64, "__sincos_stret");
183       if (TT.isWatchABI()) {
184         setLibcallCallingConv(RTLIB::SINCOS_STRET_F32,
185                               CallingConv::ARM_AAPCS_VFP);
186         setLibcallCallingConv(RTLIB::SINCOS_STRET_F64,
187                               CallingConv::ARM_AAPCS_VFP);
188       }
189     }
190   } else {
191     setLibcallName(RTLIB::FPEXT_F16_F32, "__gnu_h2f_ieee");
192     setLibcallName(RTLIB::FPROUND_F32_F16, "__gnu_f2h_ieee");
193   }
194 
195   if (TT.isGNUEnvironment() || TT.isOSFuchsia() ||
196       (TT.isAndroid() && !TT.isAndroidVersionLT(9))) {
197     setLibcallName(RTLIB::SINCOS_F32, "sincosf");
198     setLibcallName(RTLIB::SINCOS_F64, "sincos");
199     setLibcallName(RTLIB::SINCOS_F80, "sincosl");
200     setLibcallName(RTLIB::SINCOS_F128, "sincosl");
201     setLibcallName(RTLIB::SINCOS_PPCF128, "sincosl");
202   }
203 
204   if (TT.isPS4CPU()) {
205     setLibcallName(RTLIB::SINCOS_F32, "sincosf");
206     setLibcallName(RTLIB::SINCOS_F64, "sincos");
207   }
208 
209   if (TT.isOSOpenBSD()) {
210     setLibcallName(RTLIB::STACKPROTECTOR_CHECK_FAIL, nullptr);
211   }
212 }
213 
214 /// GetFPLibCall - Helper to return the right libcall for the given floating
215 /// point type, or UNKNOWN_LIBCALL if there is none.
216 RTLIB::Libcall RTLIB::getFPLibCall(EVT VT,
217                                    RTLIB::Libcall Call_F32,
218                                    RTLIB::Libcall Call_F64,
219                                    RTLIB::Libcall Call_F80,
220                                    RTLIB::Libcall Call_F128,
221                                    RTLIB::Libcall Call_PPCF128) {
222   return
223     VT == MVT::f32 ? Call_F32 :
224     VT == MVT::f64 ? Call_F64 :
225     VT == MVT::f80 ? Call_F80 :
226     VT == MVT::f128 ? Call_F128 :
227     VT == MVT::ppcf128 ? Call_PPCF128 :
228     RTLIB::UNKNOWN_LIBCALL;
229 }
230 
231 /// getFPEXT - Return the FPEXT_*_* value for the given types, or
232 /// UNKNOWN_LIBCALL if there is none.
233 RTLIB::Libcall RTLIB::getFPEXT(EVT OpVT, EVT RetVT) {
234   if (OpVT == MVT::f16) {
235     if (RetVT == MVT::f32)
236       return FPEXT_F16_F32;
237     if (RetVT == MVT::f64)
238       return FPEXT_F16_F64;
239     if (RetVT == MVT::f128)
240       return FPEXT_F16_F128;
241   } else if (OpVT == MVT::f32) {
242     if (RetVT == MVT::f64)
243       return FPEXT_F32_F64;
244     if (RetVT == MVT::f128)
245       return FPEXT_F32_F128;
246     if (RetVT == MVT::ppcf128)
247       return FPEXT_F32_PPCF128;
248   } else if (OpVT == MVT::f64) {
249     if (RetVT == MVT::f128)
250       return FPEXT_F64_F128;
251     else if (RetVT == MVT::ppcf128)
252       return FPEXT_F64_PPCF128;
253   } else if (OpVT == MVT::f80) {
254     if (RetVT == MVT::f128)
255       return FPEXT_F80_F128;
256   }
257 
258   return UNKNOWN_LIBCALL;
259 }
260 
261 /// getFPROUND - Return the FPROUND_*_* value for the given types, or
262 /// UNKNOWN_LIBCALL if there is none.
263 RTLIB::Libcall RTLIB::getFPROUND(EVT OpVT, EVT RetVT) {
264   if (RetVT == MVT::f16) {
265     if (OpVT == MVT::f32)
266       return FPROUND_F32_F16;
267     if (OpVT == MVT::f64)
268       return FPROUND_F64_F16;
269     if (OpVT == MVT::f80)
270       return FPROUND_F80_F16;
271     if (OpVT == MVT::f128)
272       return FPROUND_F128_F16;
273     if (OpVT == MVT::ppcf128)
274       return FPROUND_PPCF128_F16;
275   } else if (RetVT == MVT::f32) {
276     if (OpVT == MVT::f64)
277       return FPROUND_F64_F32;
278     if (OpVT == MVT::f80)
279       return FPROUND_F80_F32;
280     if (OpVT == MVT::f128)
281       return FPROUND_F128_F32;
282     if (OpVT == MVT::ppcf128)
283       return FPROUND_PPCF128_F32;
284   } else if (RetVT == MVT::f64) {
285     if (OpVT == MVT::f80)
286       return FPROUND_F80_F64;
287     if (OpVT == MVT::f128)
288       return FPROUND_F128_F64;
289     if (OpVT == MVT::ppcf128)
290       return FPROUND_PPCF128_F64;
291   } else if (RetVT == MVT::f80) {
292     if (OpVT == MVT::f128)
293       return FPROUND_F128_F80;
294   }
295 
296   return UNKNOWN_LIBCALL;
297 }
298 
299 /// getFPTOSINT - Return the FPTOSINT_*_* value for the given types, or
300 /// UNKNOWN_LIBCALL if there is none.
301 RTLIB::Libcall RTLIB::getFPTOSINT(EVT OpVT, EVT RetVT) {
302   if (OpVT == MVT::f16) {
303     if (RetVT == MVT::i32)
304       return FPTOSINT_F16_I32;
305     if (RetVT == MVT::i64)
306       return FPTOSINT_F16_I64;
307     if (RetVT == MVT::i128)
308       return FPTOSINT_F16_I128;
309   } else if (OpVT == MVT::f32) {
310     if (RetVT == MVT::i32)
311       return FPTOSINT_F32_I32;
312     if (RetVT == MVT::i64)
313       return FPTOSINT_F32_I64;
314     if (RetVT == MVT::i128)
315       return FPTOSINT_F32_I128;
316   } else if (OpVT == MVT::f64) {
317     if (RetVT == MVT::i32)
318       return FPTOSINT_F64_I32;
319     if (RetVT == MVT::i64)
320       return FPTOSINT_F64_I64;
321     if (RetVT == MVT::i128)
322       return FPTOSINT_F64_I128;
323   } else if (OpVT == MVT::f80) {
324     if (RetVT == MVT::i32)
325       return FPTOSINT_F80_I32;
326     if (RetVT == MVT::i64)
327       return FPTOSINT_F80_I64;
328     if (RetVT == MVT::i128)
329       return FPTOSINT_F80_I128;
330   } else if (OpVT == MVT::f128) {
331     if (RetVT == MVT::i32)
332       return FPTOSINT_F128_I32;
333     if (RetVT == MVT::i64)
334       return FPTOSINT_F128_I64;
335     if (RetVT == MVT::i128)
336       return FPTOSINT_F128_I128;
337   } else if (OpVT == MVT::ppcf128) {
338     if (RetVT == MVT::i32)
339       return FPTOSINT_PPCF128_I32;
340     if (RetVT == MVT::i64)
341       return FPTOSINT_PPCF128_I64;
342     if (RetVT == MVT::i128)
343       return FPTOSINT_PPCF128_I128;
344   }
345   return UNKNOWN_LIBCALL;
346 }
347 
348 /// getFPTOUINT - Return the FPTOUINT_*_* value for the given types, or
349 /// UNKNOWN_LIBCALL if there is none.
350 RTLIB::Libcall RTLIB::getFPTOUINT(EVT OpVT, EVT RetVT) {
351   if (OpVT == MVT::f16) {
352     if (RetVT == MVT::i32)
353       return FPTOUINT_F16_I32;
354     if (RetVT == MVT::i64)
355       return FPTOUINT_F16_I64;
356     if (RetVT == MVT::i128)
357       return FPTOUINT_F16_I128;
358   } else if (OpVT == MVT::f32) {
359     if (RetVT == MVT::i32)
360       return FPTOUINT_F32_I32;
361     if (RetVT == MVT::i64)
362       return FPTOUINT_F32_I64;
363     if (RetVT == MVT::i128)
364       return FPTOUINT_F32_I128;
365   } else if (OpVT == MVT::f64) {
366     if (RetVT == MVT::i32)
367       return FPTOUINT_F64_I32;
368     if (RetVT == MVT::i64)
369       return FPTOUINT_F64_I64;
370     if (RetVT == MVT::i128)
371       return FPTOUINT_F64_I128;
372   } else if (OpVT == MVT::f80) {
373     if (RetVT == MVT::i32)
374       return FPTOUINT_F80_I32;
375     if (RetVT == MVT::i64)
376       return FPTOUINT_F80_I64;
377     if (RetVT == MVT::i128)
378       return FPTOUINT_F80_I128;
379   } else if (OpVT == MVT::f128) {
380     if (RetVT == MVT::i32)
381       return FPTOUINT_F128_I32;
382     if (RetVT == MVT::i64)
383       return FPTOUINT_F128_I64;
384     if (RetVT == MVT::i128)
385       return FPTOUINT_F128_I128;
386   } else if (OpVT == MVT::ppcf128) {
387     if (RetVT == MVT::i32)
388       return FPTOUINT_PPCF128_I32;
389     if (RetVT == MVT::i64)
390       return FPTOUINT_PPCF128_I64;
391     if (RetVT == MVT::i128)
392       return FPTOUINT_PPCF128_I128;
393   }
394   return UNKNOWN_LIBCALL;
395 }
396 
397 /// getSINTTOFP - Return the SINTTOFP_*_* value for the given types, or
398 /// UNKNOWN_LIBCALL if there is none.
399 RTLIB::Libcall RTLIB::getSINTTOFP(EVT OpVT, EVT RetVT) {
400   if (OpVT == MVT::i32) {
401     if (RetVT == MVT::f16)
402       return SINTTOFP_I32_F16;
403     if (RetVT == MVT::f32)
404       return SINTTOFP_I32_F32;
405     if (RetVT == MVT::f64)
406       return SINTTOFP_I32_F64;
407     if (RetVT == MVT::f80)
408       return SINTTOFP_I32_F80;
409     if (RetVT == MVT::f128)
410       return SINTTOFP_I32_F128;
411     if (RetVT == MVT::ppcf128)
412       return SINTTOFP_I32_PPCF128;
413   } else if (OpVT == MVT::i64) {
414     if (RetVT == MVT::f16)
415       return SINTTOFP_I64_F16;
416     if (RetVT == MVT::f32)
417       return SINTTOFP_I64_F32;
418     if (RetVT == MVT::f64)
419       return SINTTOFP_I64_F64;
420     if (RetVT == MVT::f80)
421       return SINTTOFP_I64_F80;
422     if (RetVT == MVT::f128)
423       return SINTTOFP_I64_F128;
424     if (RetVT == MVT::ppcf128)
425       return SINTTOFP_I64_PPCF128;
426   } else if (OpVT == MVT::i128) {
427     if (RetVT == MVT::f16)
428       return SINTTOFP_I128_F16;
429     if (RetVT == MVT::f32)
430       return SINTTOFP_I128_F32;
431     if (RetVT == MVT::f64)
432       return SINTTOFP_I128_F64;
433     if (RetVT == MVT::f80)
434       return SINTTOFP_I128_F80;
435     if (RetVT == MVT::f128)
436       return SINTTOFP_I128_F128;
437     if (RetVT == MVT::ppcf128)
438       return SINTTOFP_I128_PPCF128;
439   }
440   return UNKNOWN_LIBCALL;
441 }
442 
443 /// getUINTTOFP - Return the UINTTOFP_*_* value for the given types, or
444 /// UNKNOWN_LIBCALL if there is none.
445 RTLIB::Libcall RTLIB::getUINTTOFP(EVT OpVT, EVT RetVT) {
446   if (OpVT == MVT::i32) {
447     if (RetVT == MVT::f16)
448       return UINTTOFP_I32_F16;
449     if (RetVT == MVT::f32)
450       return UINTTOFP_I32_F32;
451     if (RetVT == MVT::f64)
452       return UINTTOFP_I32_F64;
453     if (RetVT == MVT::f80)
454       return UINTTOFP_I32_F80;
455     if (RetVT == MVT::f128)
456       return UINTTOFP_I32_F128;
457     if (RetVT == MVT::ppcf128)
458       return UINTTOFP_I32_PPCF128;
459   } else if (OpVT == MVT::i64) {
460     if (RetVT == MVT::f16)
461       return UINTTOFP_I64_F16;
462     if (RetVT == MVT::f32)
463       return UINTTOFP_I64_F32;
464     if (RetVT == MVT::f64)
465       return UINTTOFP_I64_F64;
466     if (RetVT == MVT::f80)
467       return UINTTOFP_I64_F80;
468     if (RetVT == MVT::f128)
469       return UINTTOFP_I64_F128;
470     if (RetVT == MVT::ppcf128)
471       return UINTTOFP_I64_PPCF128;
472   } else if (OpVT == MVT::i128) {
473     if (RetVT == MVT::f16)
474       return UINTTOFP_I128_F16;
475     if (RetVT == MVT::f32)
476       return UINTTOFP_I128_F32;
477     if (RetVT == MVT::f64)
478       return UINTTOFP_I128_F64;
479     if (RetVT == MVT::f80)
480       return UINTTOFP_I128_F80;
481     if (RetVT == MVT::f128)
482       return UINTTOFP_I128_F128;
483     if (RetVT == MVT::ppcf128)
484       return UINTTOFP_I128_PPCF128;
485   }
486   return UNKNOWN_LIBCALL;
487 }
488 
489 RTLIB::Libcall RTLIB::getPOWI(EVT RetVT) {
490   return getFPLibCall(RetVT, POWI_F32, POWI_F64, POWI_F80, POWI_F128,
491                       POWI_PPCF128);
492 }
493 
494 RTLIB::Libcall RTLIB::getOUTLINE_ATOMIC(unsigned Opc, AtomicOrdering Order,
495                                         MVT VT) {
496   unsigned ModeN, ModelN;
497   switch (VT.SimpleTy) {
498   case MVT::i8:
499     ModeN = 0;
500     break;
501   case MVT::i16:
502     ModeN = 1;
503     break;
504   case MVT::i32:
505     ModeN = 2;
506     break;
507   case MVT::i64:
508     ModeN = 3;
509     break;
510   case MVT::i128:
511     ModeN = 4;
512     break;
513   default:
514     return UNKNOWN_LIBCALL;
515   }
516 
517   switch (Order) {
518   case AtomicOrdering::Monotonic:
519     ModelN = 0;
520     break;
521   case AtomicOrdering::Acquire:
522     ModelN = 1;
523     break;
524   case AtomicOrdering::Release:
525     ModelN = 2;
526     break;
527   case AtomicOrdering::AcquireRelease:
528   case AtomicOrdering::SequentiallyConsistent:
529     ModelN = 3;
530     break;
531   default:
532     return UNKNOWN_LIBCALL;
533   }
534 
535 #define LCALLS(A, B)                                                           \
536   { A##B##_RELAX, A##B##_ACQ, A##B##_REL, A##B##_ACQ_REL }
537 #define LCALL5(A)                                                              \
538   LCALLS(A, 1), LCALLS(A, 2), LCALLS(A, 4), LCALLS(A, 8), LCALLS(A, 16)
539   switch (Opc) {
540   case ISD::ATOMIC_CMP_SWAP: {
541     const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_CAS)};
542     return LC[ModeN][ModelN];
543   }
544   case ISD::ATOMIC_SWAP: {
545     const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_SWP)};
546     return LC[ModeN][ModelN];
547   }
548   case ISD::ATOMIC_LOAD_ADD: {
549     const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_LDADD)};
550     return LC[ModeN][ModelN];
551   }
552   case ISD::ATOMIC_LOAD_OR: {
553     const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_LDSET)};
554     return LC[ModeN][ModelN];
555   }
556   case ISD::ATOMIC_LOAD_CLR: {
557     const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_LDCLR)};
558     return LC[ModeN][ModelN];
559   }
560   case ISD::ATOMIC_LOAD_XOR: {
561     const Libcall LC[5][4] = {LCALL5(OUTLINE_ATOMIC_LDEOR)};
562     return LC[ModeN][ModelN];
563   }
564   default:
565     return UNKNOWN_LIBCALL;
566   }
567 #undef LCALLS
568 #undef LCALL5
569 }
570 
571 RTLIB::Libcall RTLIB::getSYNC(unsigned Opc, MVT VT) {
572 #define OP_TO_LIBCALL(Name, Enum)                                              \
573   case Name:                                                                   \
574     switch (VT.SimpleTy) {                                                     \
575     default:                                                                   \
576       return UNKNOWN_LIBCALL;                                                  \
577     case MVT::i8:                                                              \
578       return Enum##_1;                                                         \
579     case MVT::i16:                                                             \
580       return Enum##_2;                                                         \
581     case MVT::i32:                                                             \
582       return Enum##_4;                                                         \
583     case MVT::i64:                                                             \
584       return Enum##_8;                                                         \
585     case MVT::i128:                                                            \
586       return Enum##_16;                                                        \
587     }
588 
589   switch (Opc) {
590     OP_TO_LIBCALL(ISD::ATOMIC_SWAP, SYNC_LOCK_TEST_AND_SET)
591     OP_TO_LIBCALL(ISD::ATOMIC_CMP_SWAP, SYNC_VAL_COMPARE_AND_SWAP)
592     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_ADD, SYNC_FETCH_AND_ADD)
593     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_SUB, SYNC_FETCH_AND_SUB)
594     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_AND, SYNC_FETCH_AND_AND)
595     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_OR, SYNC_FETCH_AND_OR)
596     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_XOR, SYNC_FETCH_AND_XOR)
597     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_NAND, SYNC_FETCH_AND_NAND)
598     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_MAX, SYNC_FETCH_AND_MAX)
599     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_UMAX, SYNC_FETCH_AND_UMAX)
600     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_MIN, SYNC_FETCH_AND_MIN)
601     OP_TO_LIBCALL(ISD::ATOMIC_LOAD_UMIN, SYNC_FETCH_AND_UMIN)
602   }
603 
604 #undef OP_TO_LIBCALL
605 
606   return UNKNOWN_LIBCALL;
607 }
608 
609 RTLIB::Libcall RTLIB::getMEMCPY_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize) {
610   switch (ElementSize) {
611   case 1:
612     return MEMCPY_ELEMENT_UNORDERED_ATOMIC_1;
613   case 2:
614     return MEMCPY_ELEMENT_UNORDERED_ATOMIC_2;
615   case 4:
616     return MEMCPY_ELEMENT_UNORDERED_ATOMIC_4;
617   case 8:
618     return MEMCPY_ELEMENT_UNORDERED_ATOMIC_8;
619   case 16:
620     return MEMCPY_ELEMENT_UNORDERED_ATOMIC_16;
621   default:
622     return UNKNOWN_LIBCALL;
623   }
624 }
625 
626 RTLIB::Libcall RTLIB::getMEMMOVE_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize) {
627   switch (ElementSize) {
628   case 1:
629     return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_1;
630   case 2:
631     return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_2;
632   case 4:
633     return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_4;
634   case 8:
635     return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_8;
636   case 16:
637     return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_16;
638   default:
639     return UNKNOWN_LIBCALL;
640   }
641 }
642 
643 RTLIB::Libcall RTLIB::getMEMSET_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize) {
644   switch (ElementSize) {
645   case 1:
646     return MEMSET_ELEMENT_UNORDERED_ATOMIC_1;
647   case 2:
648     return MEMSET_ELEMENT_UNORDERED_ATOMIC_2;
649   case 4:
650     return MEMSET_ELEMENT_UNORDERED_ATOMIC_4;
651   case 8:
652     return MEMSET_ELEMENT_UNORDERED_ATOMIC_8;
653   case 16:
654     return MEMSET_ELEMENT_UNORDERED_ATOMIC_16;
655   default:
656     return UNKNOWN_LIBCALL;
657   }
658 }
659 
660 /// InitCmpLibcallCCs - Set default comparison libcall CC.
661 static void InitCmpLibcallCCs(ISD::CondCode *CCs) {
662   memset(CCs, ISD::SETCC_INVALID, sizeof(ISD::CondCode)*RTLIB::UNKNOWN_LIBCALL);
663   CCs[RTLIB::OEQ_F32] = ISD::SETEQ;
664   CCs[RTLIB::OEQ_F64] = ISD::SETEQ;
665   CCs[RTLIB::OEQ_F128] = ISD::SETEQ;
666   CCs[RTLIB::OEQ_PPCF128] = ISD::SETEQ;
667   CCs[RTLIB::UNE_F32] = ISD::SETNE;
668   CCs[RTLIB::UNE_F64] = ISD::SETNE;
669   CCs[RTLIB::UNE_F128] = ISD::SETNE;
670   CCs[RTLIB::UNE_PPCF128] = ISD::SETNE;
671   CCs[RTLIB::OGE_F32] = ISD::SETGE;
672   CCs[RTLIB::OGE_F64] = ISD::SETGE;
673   CCs[RTLIB::OGE_F128] = ISD::SETGE;
674   CCs[RTLIB::OGE_PPCF128] = ISD::SETGE;
675   CCs[RTLIB::OLT_F32] = ISD::SETLT;
676   CCs[RTLIB::OLT_F64] = ISD::SETLT;
677   CCs[RTLIB::OLT_F128] = ISD::SETLT;
678   CCs[RTLIB::OLT_PPCF128] = ISD::SETLT;
679   CCs[RTLIB::OLE_F32] = ISD::SETLE;
680   CCs[RTLIB::OLE_F64] = ISD::SETLE;
681   CCs[RTLIB::OLE_F128] = ISD::SETLE;
682   CCs[RTLIB::OLE_PPCF128] = ISD::SETLE;
683   CCs[RTLIB::OGT_F32] = ISD::SETGT;
684   CCs[RTLIB::OGT_F64] = ISD::SETGT;
685   CCs[RTLIB::OGT_F128] = ISD::SETGT;
686   CCs[RTLIB::OGT_PPCF128] = ISD::SETGT;
687   CCs[RTLIB::UO_F32] = ISD::SETNE;
688   CCs[RTLIB::UO_F64] = ISD::SETNE;
689   CCs[RTLIB::UO_F128] = ISD::SETNE;
690   CCs[RTLIB::UO_PPCF128] = ISD::SETNE;
691 }
692 
693 /// NOTE: The TargetMachine owns TLOF.
694 TargetLoweringBase::TargetLoweringBase(const TargetMachine &tm) : TM(tm) {
695   initActions();
696 
697   // Perform these initializations only once.
698   MaxStoresPerMemset = MaxStoresPerMemcpy = MaxStoresPerMemmove =
699       MaxLoadsPerMemcmp = 8;
700   MaxGluedStoresPerMemcpy = 0;
701   MaxStoresPerMemsetOptSize = MaxStoresPerMemcpyOptSize =
702       MaxStoresPerMemmoveOptSize = MaxLoadsPerMemcmpOptSize = 4;
703   HasMultipleConditionRegisters = false;
704   HasExtractBitsInsn = false;
705   JumpIsExpensive = JumpIsExpensiveOverride;
706   PredictableSelectIsExpensive = false;
707   EnableExtLdPromotion = false;
708   StackPointerRegisterToSaveRestore = 0;
709   BooleanContents = UndefinedBooleanContent;
710   BooleanFloatContents = UndefinedBooleanContent;
711   BooleanVectorContents = UndefinedBooleanContent;
712   SchedPreferenceInfo = Sched::ILP;
713   GatherAllAliasesMaxDepth = 18;
714   IsStrictFPEnabled = DisableStrictNodeMutation;
715   // TODO: the default will be switched to 0 in the next commit, along
716   // with the Target-specific changes necessary.
717   MaxAtomicSizeInBitsSupported = 1024;
718 
719   MinCmpXchgSizeInBits = 0;
720   SupportsUnalignedAtomics = false;
721 
722   std::fill(std::begin(LibcallRoutineNames), std::end(LibcallRoutineNames), nullptr);
723 
724   InitLibcalls(TM.getTargetTriple());
725   InitCmpLibcallCCs(CmpLibcallCCs);
726 }
727 
728 void TargetLoweringBase::initActions() {
729   // All operations default to being supported.
730   memset(OpActions, 0, sizeof(OpActions));
731   memset(LoadExtActions, 0, sizeof(LoadExtActions));
732   memset(TruncStoreActions, 0, sizeof(TruncStoreActions));
733   memset(IndexedModeActions, 0, sizeof(IndexedModeActions));
734   memset(CondCodeActions, 0, sizeof(CondCodeActions));
735   std::fill(std::begin(RegClassForVT), std::end(RegClassForVT), nullptr);
736   std::fill(std::begin(TargetDAGCombineArray),
737             std::end(TargetDAGCombineArray), 0);
738 
739   for (MVT VT : MVT::fp_valuetypes()) {
740     MVT IntVT = MVT::getIntegerVT(VT.getFixedSizeInBits());
741     if (IntVT.isValid()) {
742       setOperationAction(ISD::ATOMIC_SWAP, VT, Promote);
743       AddPromotedToType(ISD::ATOMIC_SWAP, VT, IntVT);
744     }
745   }
746 
747   // Set default actions for various operations.
748   for (MVT VT : MVT::all_valuetypes()) {
749     // Default all indexed load / store to expand.
750     for (unsigned IM = (unsigned)ISD::PRE_INC;
751          IM != (unsigned)ISD::LAST_INDEXED_MODE; ++IM) {
752       setIndexedLoadAction(IM, VT, Expand);
753       setIndexedStoreAction(IM, VT, Expand);
754       setIndexedMaskedLoadAction(IM, VT, Expand);
755       setIndexedMaskedStoreAction(IM, VT, Expand);
756     }
757 
758     // Most backends expect to see the node which just returns the value loaded.
759     setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, VT, Expand);
760 
761     // These operations default to expand.
762     setOperationAction(ISD::FGETSIGN, VT, Expand);
763     setOperationAction(ISD::CONCAT_VECTORS, VT, Expand);
764     setOperationAction(ISD::FMINNUM, VT, Expand);
765     setOperationAction(ISD::FMAXNUM, VT, Expand);
766     setOperationAction(ISD::FMINNUM_IEEE, VT, Expand);
767     setOperationAction(ISD::FMAXNUM_IEEE, VT, Expand);
768     setOperationAction(ISD::FMINIMUM, VT, Expand);
769     setOperationAction(ISD::FMAXIMUM, VT, Expand);
770     setOperationAction(ISD::FMAD, VT, Expand);
771     setOperationAction(ISD::SMIN, VT, Expand);
772     setOperationAction(ISD::SMAX, VT, Expand);
773     setOperationAction(ISD::UMIN, VT, Expand);
774     setOperationAction(ISD::UMAX, VT, Expand);
775     setOperationAction(ISD::ABS, VT, Expand);
776     setOperationAction(ISD::FSHL, VT, Expand);
777     setOperationAction(ISD::FSHR, VT, Expand);
778     setOperationAction(ISD::SADDSAT, VT, Expand);
779     setOperationAction(ISD::UADDSAT, VT, Expand);
780     setOperationAction(ISD::SSUBSAT, VT, Expand);
781     setOperationAction(ISD::USUBSAT, VT, Expand);
782     setOperationAction(ISD::SSHLSAT, VT, Expand);
783     setOperationAction(ISD::USHLSAT, VT, Expand);
784     setOperationAction(ISD::SMULFIX, VT, Expand);
785     setOperationAction(ISD::SMULFIXSAT, VT, Expand);
786     setOperationAction(ISD::UMULFIX, VT, Expand);
787     setOperationAction(ISD::UMULFIXSAT, VT, Expand);
788     setOperationAction(ISD::SDIVFIX, VT, Expand);
789     setOperationAction(ISD::SDIVFIXSAT, VT, Expand);
790     setOperationAction(ISD::UDIVFIX, VT, Expand);
791     setOperationAction(ISD::UDIVFIXSAT, VT, Expand);
792     setOperationAction(ISD::FP_TO_SINT_SAT, VT, Expand);
793     setOperationAction(ISD::FP_TO_UINT_SAT, VT, Expand);
794 
795     // Overflow operations default to expand
796     setOperationAction(ISD::SADDO, VT, Expand);
797     setOperationAction(ISD::SSUBO, VT, Expand);
798     setOperationAction(ISD::UADDO, VT, Expand);
799     setOperationAction(ISD::USUBO, VT, Expand);
800     setOperationAction(ISD::SMULO, VT, Expand);
801     setOperationAction(ISD::UMULO, VT, Expand);
802 
803     // ADDCARRY operations default to expand
804     setOperationAction(ISD::ADDCARRY, VT, Expand);
805     setOperationAction(ISD::SUBCARRY, VT, Expand);
806     setOperationAction(ISD::SETCCCARRY, VT, Expand);
807     setOperationAction(ISD::SADDO_CARRY, VT, Expand);
808     setOperationAction(ISD::SSUBO_CARRY, VT, Expand);
809 
810     // ADDC/ADDE/SUBC/SUBE default to expand.
811     setOperationAction(ISD::ADDC, VT, Expand);
812     setOperationAction(ISD::ADDE, VT, Expand);
813     setOperationAction(ISD::SUBC, VT, Expand);
814     setOperationAction(ISD::SUBE, VT, Expand);
815 
816     // Absolute difference
817     setOperationAction(ISD::ABDS, VT, Expand);
818     setOperationAction(ISD::ABDU, VT, Expand);
819 
820     // These default to Expand so they will be expanded to CTLZ/CTTZ by default.
821     setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
822     setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
823 
824     setOperationAction(ISD::BITREVERSE, VT, Expand);
825     setOperationAction(ISD::PARITY, VT, Expand);
826 
827     // These library functions default to expand.
828     setOperationAction(ISD::FROUND, VT, Expand);
829     setOperationAction(ISD::FROUNDEVEN, VT, Expand);
830     setOperationAction(ISD::FPOWI, VT, Expand);
831 
832     // These operations default to expand for vector types.
833     if (VT.isVector()) {
834       setOperationAction(ISD::FCOPYSIGN, VT, Expand);
835       setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
836       setOperationAction(ISD::ANY_EXTEND_VECTOR_INREG, VT, Expand);
837       setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, VT, Expand);
838       setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, VT, Expand);
839       setOperationAction(ISD::SPLAT_VECTOR, VT, Expand);
840     }
841 
842     // Constrained floating-point operations default to expand.
843 #define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN)               \
844     setOperationAction(ISD::STRICT_##DAGN, VT, Expand);
845 #include "llvm/IR/ConstrainedOps.def"
846 
847     // For most targets @llvm.get.dynamic.area.offset just returns 0.
848     setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, VT, Expand);
849 
850     // Vector reduction default to expand.
851     setOperationAction(ISD::VECREDUCE_FADD, VT, Expand);
852     setOperationAction(ISD::VECREDUCE_FMUL, VT, Expand);
853     setOperationAction(ISD::VECREDUCE_ADD, VT, Expand);
854     setOperationAction(ISD::VECREDUCE_MUL, VT, Expand);
855     setOperationAction(ISD::VECREDUCE_AND, VT, Expand);
856     setOperationAction(ISD::VECREDUCE_OR, VT, Expand);
857     setOperationAction(ISD::VECREDUCE_XOR, VT, Expand);
858     setOperationAction(ISD::VECREDUCE_SMAX, VT, Expand);
859     setOperationAction(ISD::VECREDUCE_SMIN, VT, Expand);
860     setOperationAction(ISD::VECREDUCE_UMAX, VT, Expand);
861     setOperationAction(ISD::VECREDUCE_UMIN, VT, Expand);
862     setOperationAction(ISD::VECREDUCE_FMAX, VT, Expand);
863     setOperationAction(ISD::VECREDUCE_FMIN, VT, Expand);
864     setOperationAction(ISD::VECREDUCE_SEQ_FADD, VT, Expand);
865     setOperationAction(ISD::VECREDUCE_SEQ_FMUL, VT, Expand);
866 
867     // Named vector shuffles default to expand.
868     setOperationAction(ISD::VECTOR_SPLICE, VT, Expand);
869   }
870 
871   // Most targets ignore the @llvm.prefetch intrinsic.
872   setOperationAction(ISD::PREFETCH, MVT::Other, Expand);
873 
874   // Most targets also ignore the @llvm.readcyclecounter intrinsic.
875   setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Expand);
876 
877   // ConstantFP nodes default to expand.  Targets can either change this to
878   // Legal, in which case all fp constants are legal, or use isFPImmLegal()
879   // to optimize expansions for certain constants.
880   setOperationAction(ISD::ConstantFP, MVT::f16, Expand);
881   setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
882   setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
883   setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
884   setOperationAction(ISD::ConstantFP, MVT::f128, Expand);
885 
886   // These library functions default to expand.
887   for (MVT VT : {MVT::f32, MVT::f64, MVT::f128}) {
888     setOperationAction(ISD::FCBRT,      VT, Expand);
889     setOperationAction(ISD::FLOG ,      VT, Expand);
890     setOperationAction(ISD::FLOG2,      VT, Expand);
891     setOperationAction(ISD::FLOG10,     VT, Expand);
892     setOperationAction(ISD::FEXP ,      VT, Expand);
893     setOperationAction(ISD::FEXP2,      VT, Expand);
894     setOperationAction(ISD::FFLOOR,     VT, Expand);
895     setOperationAction(ISD::FNEARBYINT, VT, Expand);
896     setOperationAction(ISD::FCEIL,      VT, Expand);
897     setOperationAction(ISD::FRINT,      VT, Expand);
898     setOperationAction(ISD::FTRUNC,     VT, Expand);
899     setOperationAction(ISD::FROUND,     VT, Expand);
900     setOperationAction(ISD::FROUNDEVEN, VT, Expand);
901     setOperationAction(ISD::LROUND,     VT, Expand);
902     setOperationAction(ISD::LLROUND,    VT, Expand);
903     setOperationAction(ISD::LRINT,      VT, Expand);
904     setOperationAction(ISD::LLRINT,     VT, Expand);
905   }
906 
907   // Default ISD::TRAP to expand (which turns it into abort).
908   setOperationAction(ISD::TRAP, MVT::Other, Expand);
909 
910   // On most systems, DEBUGTRAP and TRAP have no difference. The "Expand"
911   // here is to inform DAG Legalizer to replace DEBUGTRAP with TRAP.
912   setOperationAction(ISD::DEBUGTRAP, MVT::Other, Expand);
913 
914   setOperationAction(ISD::UBSANTRAP, MVT::Other, Expand);
915 }
916 
917 MVT TargetLoweringBase::getScalarShiftAmountTy(const DataLayout &DL,
918                                                EVT) const {
919   return MVT::getIntegerVT(DL.getPointerSizeInBits(0));
920 }
921 
922 EVT TargetLoweringBase::getShiftAmountTy(EVT LHSTy, const DataLayout &DL,
923                                          bool LegalTypes) const {
924   assert(LHSTy.isInteger() && "Shift amount is not an integer type!");
925   if (LHSTy.isVector())
926     return LHSTy;
927   return LegalTypes ? getScalarShiftAmountTy(DL, LHSTy)
928                     : getPointerTy(DL);
929 }
930 
931 bool TargetLoweringBase::canOpTrap(unsigned Op, EVT VT) const {
932   assert(isTypeLegal(VT));
933   switch (Op) {
934   default:
935     return false;
936   case ISD::SDIV:
937   case ISD::UDIV:
938   case ISD::SREM:
939   case ISD::UREM:
940     return true;
941   }
942 }
943 
944 bool TargetLoweringBase::isFreeAddrSpaceCast(unsigned SrcAS,
945                                              unsigned DestAS) const {
946   return TM.isNoopAddrSpaceCast(SrcAS, DestAS);
947 }
948 
949 void TargetLoweringBase::setJumpIsExpensive(bool isExpensive) {
950   // If the command-line option was specified, ignore this request.
951   if (!JumpIsExpensiveOverride.getNumOccurrences())
952     JumpIsExpensive = isExpensive;
953 }
954 
955 TargetLoweringBase::LegalizeKind
956 TargetLoweringBase::getTypeConversion(LLVMContext &Context, EVT VT) const {
957   // If this is a simple type, use the ComputeRegisterProp mechanism.
958   if (VT.isSimple()) {
959     MVT SVT = VT.getSimpleVT();
960     assert((unsigned)SVT.SimpleTy < array_lengthof(TransformToType));
961     MVT NVT = TransformToType[SVT.SimpleTy];
962     LegalizeTypeAction LA = ValueTypeActions.getTypeAction(SVT);
963 
964     assert((LA == TypeLegal || LA == TypeSoftenFloat ||
965             LA == TypeSoftPromoteHalf ||
966             (NVT.isVector() ||
967              ValueTypeActions.getTypeAction(NVT) != TypePromoteInteger)) &&
968            "Promote may not follow Expand or Promote");
969 
970     if (LA == TypeSplitVector)
971       return LegalizeKind(LA, EVT(SVT).getHalfNumVectorElementsVT(Context));
972     if (LA == TypeScalarizeVector)
973       return LegalizeKind(LA, SVT.getVectorElementType());
974     return LegalizeKind(LA, NVT);
975   }
976 
977   // Handle Extended Scalar Types.
978   if (!VT.isVector()) {
979     assert(VT.isInteger() && "Float types must be simple");
980     unsigned BitSize = VT.getSizeInBits();
981     // First promote to a power-of-two size, then expand if necessary.
982     if (BitSize < 8 || !isPowerOf2_32(BitSize)) {
983       EVT NVT = VT.getRoundIntegerType(Context);
984       assert(NVT != VT && "Unable to round integer VT");
985       LegalizeKind NextStep = getTypeConversion(Context, NVT);
986       // Avoid multi-step promotion.
987       if (NextStep.first == TypePromoteInteger)
988         return NextStep;
989       // Return rounded integer type.
990       return LegalizeKind(TypePromoteInteger, NVT);
991     }
992 
993     return LegalizeKind(TypeExpandInteger,
994                         EVT::getIntegerVT(Context, VT.getSizeInBits() / 2));
995   }
996 
997   // Handle vector types.
998   ElementCount NumElts = VT.getVectorElementCount();
999   EVT EltVT = VT.getVectorElementType();
1000 
1001   // Vectors with only one element are always scalarized.
1002   if (NumElts.isScalar())
1003     return LegalizeKind(TypeScalarizeVector, EltVT);
1004 
1005   // Try to widen vector elements until the element type is a power of two and
1006   // promote it to a legal type later on, for example:
1007   // <3 x i8> -> <4 x i8> -> <4 x i32>
1008   if (EltVT.isInteger()) {
1009     // Vectors with a number of elements that is not a power of two are always
1010     // widened, for example <3 x i8> -> <4 x i8>.
1011     if (!VT.isPow2VectorType()) {
1012       NumElts = NumElts.coefficientNextPowerOf2();
1013       EVT NVT = EVT::getVectorVT(Context, EltVT, NumElts);
1014       return LegalizeKind(TypeWidenVector, NVT);
1015     }
1016 
1017     // Examine the element type.
1018     LegalizeKind LK = getTypeConversion(Context, EltVT);
1019 
1020     // If type is to be expanded, split the vector.
1021     //  <4 x i140> -> <2 x i140>
1022     if (LK.first == TypeExpandInteger) {
1023       if (VT.getVectorElementCount().isScalable())
1024         return LegalizeKind(TypeScalarizeScalableVector, EltVT);
1025       return LegalizeKind(TypeSplitVector,
1026                           VT.getHalfNumVectorElementsVT(Context));
1027     }
1028 
1029     // Promote the integer element types until a legal vector type is found
1030     // or until the element integer type is too big. If a legal type was not
1031     // found, fallback to the usual mechanism of widening/splitting the
1032     // vector.
1033     EVT OldEltVT = EltVT;
1034     while (true) {
1035       // Increase the bitwidth of the element to the next pow-of-two
1036       // (which is greater than 8 bits).
1037       EltVT = EVT::getIntegerVT(Context, 1 + EltVT.getSizeInBits())
1038                   .getRoundIntegerType(Context);
1039 
1040       // Stop trying when getting a non-simple element type.
1041       // Note that vector elements may be greater than legal vector element
1042       // types. Example: X86 XMM registers hold 64bit element on 32bit
1043       // systems.
1044       if (!EltVT.isSimple())
1045         break;
1046 
1047       // Build a new vector type and check if it is legal.
1048       MVT NVT = MVT::getVectorVT(EltVT.getSimpleVT(), NumElts);
1049       // Found a legal promoted vector type.
1050       if (NVT != MVT() && ValueTypeActions.getTypeAction(NVT) == TypeLegal)
1051         return LegalizeKind(TypePromoteInteger,
1052                             EVT::getVectorVT(Context, EltVT, NumElts));
1053     }
1054 
1055     // Reset the type to the unexpanded type if we did not find a legal vector
1056     // type with a promoted vector element type.
1057     EltVT = OldEltVT;
1058   }
1059 
1060   // Try to widen the vector until a legal type is found.
1061   // If there is no wider legal type, split the vector.
1062   while (true) {
1063     // Round up to the next power of 2.
1064     NumElts = NumElts.coefficientNextPowerOf2();
1065 
1066     // If there is no simple vector type with this many elements then there
1067     // cannot be a larger legal vector type.  Note that this assumes that
1068     // there are no skipped intermediate vector types in the simple types.
1069     if (!EltVT.isSimple())
1070       break;
1071     MVT LargerVector = MVT::getVectorVT(EltVT.getSimpleVT(), NumElts);
1072     if (LargerVector == MVT())
1073       break;
1074 
1075     // If this type is legal then widen the vector.
1076     if (ValueTypeActions.getTypeAction(LargerVector) == TypeLegal)
1077       return LegalizeKind(TypeWidenVector, LargerVector);
1078   }
1079 
1080   // Widen odd vectors to next power of two.
1081   if (!VT.isPow2VectorType()) {
1082     EVT NVT = VT.getPow2VectorType(Context);
1083     return LegalizeKind(TypeWidenVector, NVT);
1084   }
1085 
1086   if (VT.getVectorElementCount() == ElementCount::getScalable(1))
1087     return LegalizeKind(TypeScalarizeScalableVector, EltVT);
1088 
1089   // Vectors with illegal element types are expanded.
1090   EVT NVT = EVT::getVectorVT(Context, EltVT,
1091                              VT.getVectorElementCount().divideCoefficientBy(2));
1092   return LegalizeKind(TypeSplitVector, NVT);
1093 }
1094 
1095 static unsigned getVectorTypeBreakdownMVT(MVT VT, MVT &IntermediateVT,
1096                                           unsigned &NumIntermediates,
1097                                           MVT &RegisterVT,
1098                                           TargetLoweringBase *TLI) {
1099   // Figure out the right, legal destination reg to copy into.
1100   ElementCount EC = VT.getVectorElementCount();
1101   MVT EltTy = VT.getVectorElementType();
1102 
1103   unsigned NumVectorRegs = 1;
1104 
1105   // Scalable vectors cannot be scalarized, so splitting or widening is
1106   // required.
1107   if (VT.isScalableVector() && !isPowerOf2_32(EC.getKnownMinValue()))
1108     llvm_unreachable(
1109         "Splitting or widening of non-power-of-2 MVTs is not implemented.");
1110 
1111   // FIXME: We don't support non-power-of-2-sized vectors for now.
1112   // Ideally we could break down into LHS/RHS like LegalizeDAG does.
1113   if (!isPowerOf2_32(EC.getKnownMinValue())) {
1114     // Split EC to unit size (scalable property is preserved).
1115     NumVectorRegs = EC.getKnownMinValue();
1116     EC = ElementCount::getFixed(1);
1117   }
1118 
1119   // Divide the input until we get to a supported size. This will
1120   // always end up with an EC that represent a scalar or a scalable
1121   // scalar.
1122   while (EC.getKnownMinValue() > 1 &&
1123          !TLI->isTypeLegal(MVT::getVectorVT(EltTy, EC))) {
1124     EC = EC.divideCoefficientBy(2);
1125     NumVectorRegs <<= 1;
1126   }
1127 
1128   NumIntermediates = NumVectorRegs;
1129 
1130   MVT NewVT = MVT::getVectorVT(EltTy, EC);
1131   if (!TLI->isTypeLegal(NewVT))
1132     NewVT = EltTy;
1133   IntermediateVT = NewVT;
1134 
1135   unsigned LaneSizeInBits = NewVT.getScalarSizeInBits();
1136 
1137   // Convert sizes such as i33 to i64.
1138   if (!isPowerOf2_32(LaneSizeInBits))
1139     LaneSizeInBits = NextPowerOf2(LaneSizeInBits);
1140 
1141   MVT DestVT = TLI->getRegisterType(NewVT);
1142   RegisterVT = DestVT;
1143   if (EVT(DestVT).bitsLT(NewVT))    // Value is expanded, e.g. i64 -> i16.
1144     return NumVectorRegs * (LaneSizeInBits / DestVT.getScalarSizeInBits());
1145 
1146   // Otherwise, promotion or legal types use the same number of registers as
1147   // the vector decimated to the appropriate level.
1148   return NumVectorRegs;
1149 }
1150 
1151 /// isLegalRC - Return true if the value types that can be represented by the
1152 /// specified register class are all legal.
1153 bool TargetLoweringBase::isLegalRC(const TargetRegisterInfo &TRI,
1154                                    const TargetRegisterClass &RC) const {
1155   for (auto I = TRI.legalclasstypes_begin(RC); *I != MVT::Other; ++I)
1156     if (isTypeLegal(*I))
1157       return true;
1158   return false;
1159 }
1160 
1161 /// Replace/modify any TargetFrameIndex operands with a targte-dependent
1162 /// sequence of memory operands that is recognized by PrologEpilogInserter.
1163 MachineBasicBlock *
1164 TargetLoweringBase::emitPatchPoint(MachineInstr &InitialMI,
1165                                    MachineBasicBlock *MBB) const {
1166   MachineInstr *MI = &InitialMI;
1167   MachineFunction &MF = *MI->getMF();
1168   MachineFrameInfo &MFI = MF.getFrameInfo();
1169 
1170   // We're handling multiple types of operands here:
1171   // PATCHPOINT MetaArgs - live-in, read only, direct
1172   // STATEPOINT Deopt Spill - live-through, read only, indirect
1173   // STATEPOINT Deopt Alloca - live-through, read only, direct
1174   // (We're currently conservative and mark the deopt slots read/write in
1175   // practice.)
1176   // STATEPOINT GC Spill - live-through, read/write, indirect
1177   // STATEPOINT GC Alloca - live-through, read/write, direct
1178   // The live-in vs live-through is handled already (the live through ones are
1179   // all stack slots), but we need to handle the different type of stackmap
1180   // operands and memory effects here.
1181 
1182   if (!llvm::any_of(MI->operands(),
1183                     [](MachineOperand &Operand) { return Operand.isFI(); }))
1184     return MBB;
1185 
1186   MachineInstrBuilder MIB = BuildMI(MF, MI->getDebugLoc(), MI->getDesc());
1187 
1188   // Inherit previous memory operands.
1189   MIB.cloneMemRefs(*MI);
1190 
1191   for (unsigned i = 0; i < MI->getNumOperands(); ++i) {
1192     MachineOperand &MO = MI->getOperand(i);
1193     if (!MO.isFI()) {
1194       // Index of Def operand this Use it tied to.
1195       // Since Defs are coming before Uses, if Use is tied, then
1196       // index of Def must be smaller that index of that Use.
1197       // Also, Defs preserve their position in new MI.
1198       unsigned TiedTo = i;
1199       if (MO.isReg() && MO.isTied())
1200         TiedTo = MI->findTiedOperandIdx(i);
1201       MIB.add(MO);
1202       if (TiedTo < i)
1203         MIB->tieOperands(TiedTo, MIB->getNumOperands() - 1);
1204       continue;
1205     }
1206 
1207     // foldMemoryOperand builds a new MI after replacing a single FI operand
1208     // with the canonical set of five x86 addressing-mode operands.
1209     int FI = MO.getIndex();
1210 
1211     // Add frame index operands recognized by stackmaps.cpp
1212     if (MFI.isStatepointSpillSlotObjectIndex(FI)) {
1213       // indirect-mem-ref tag, size, #FI, offset.
1214       // Used for spills inserted by StatepointLowering.  This codepath is not
1215       // used for patchpoints/stackmaps at all, for these spilling is done via
1216       // foldMemoryOperand callback only.
1217       assert(MI->getOpcode() == TargetOpcode::STATEPOINT && "sanity");
1218       MIB.addImm(StackMaps::IndirectMemRefOp);
1219       MIB.addImm(MFI.getObjectSize(FI));
1220       MIB.add(MO);
1221       MIB.addImm(0);
1222     } else {
1223       // direct-mem-ref tag, #FI, offset.
1224       // Used by patchpoint, and direct alloca arguments to statepoints
1225       MIB.addImm(StackMaps::DirectMemRefOp);
1226       MIB.add(MO);
1227       MIB.addImm(0);
1228     }
1229 
1230     assert(MIB->mayLoad() && "Folded a stackmap use to a non-load!");
1231 
1232     // Add a new memory operand for this FI.
1233     assert(MFI.getObjectOffset(FI) != -1);
1234 
1235     // Note: STATEPOINT MMOs are added during SelectionDAG.  STACKMAP, and
1236     // PATCHPOINT should be updated to do the same. (TODO)
1237     if (MI->getOpcode() != TargetOpcode::STATEPOINT) {
1238       auto Flags = MachineMemOperand::MOLoad;
1239       MachineMemOperand *MMO = MF.getMachineMemOperand(
1240           MachinePointerInfo::getFixedStack(MF, FI), Flags,
1241           MF.getDataLayout().getPointerSize(), MFI.getObjectAlign(FI));
1242       MIB->addMemOperand(MF, MMO);
1243     }
1244   }
1245   MBB->insert(MachineBasicBlock::iterator(MI), MIB);
1246   MI->eraseFromParent();
1247   return MBB;
1248 }
1249 
1250 /// findRepresentativeClass - Return the largest legal super-reg register class
1251 /// of the register class for the specified type and its associated "cost".
1252 // This function is in TargetLowering because it uses RegClassForVT which would
1253 // need to be moved to TargetRegisterInfo and would necessitate moving
1254 // isTypeLegal over as well - a massive change that would just require
1255 // TargetLowering having a TargetRegisterInfo class member that it would use.
1256 std::pair<const TargetRegisterClass *, uint8_t>
1257 TargetLoweringBase::findRepresentativeClass(const TargetRegisterInfo *TRI,
1258                                             MVT VT) const {
1259   const TargetRegisterClass *RC = RegClassForVT[VT.SimpleTy];
1260   if (!RC)
1261     return std::make_pair(RC, 0);
1262 
1263   // Compute the set of all super-register classes.
1264   BitVector SuperRegRC(TRI->getNumRegClasses());
1265   for (SuperRegClassIterator RCI(RC, TRI); RCI.isValid(); ++RCI)
1266     SuperRegRC.setBitsInMask(RCI.getMask());
1267 
1268   // Find the first legal register class with the largest spill size.
1269   const TargetRegisterClass *BestRC = RC;
1270   for (unsigned i : SuperRegRC.set_bits()) {
1271     const TargetRegisterClass *SuperRC = TRI->getRegClass(i);
1272     // We want the largest possible spill size.
1273     if (TRI->getSpillSize(*SuperRC) <= TRI->getSpillSize(*BestRC))
1274       continue;
1275     if (!isLegalRC(*TRI, *SuperRC))
1276       continue;
1277     BestRC = SuperRC;
1278   }
1279   return std::make_pair(BestRC, 1);
1280 }
1281 
1282 /// computeRegisterProperties - Once all of the register classes are added,
1283 /// this allows us to compute derived properties we expose.
1284 void TargetLoweringBase::computeRegisterProperties(
1285     const TargetRegisterInfo *TRI) {
1286   static_assert(MVT::VALUETYPE_SIZE <= MVT::MAX_ALLOWED_VALUETYPE,
1287                 "Too many value types for ValueTypeActions to hold!");
1288 
1289   // Everything defaults to needing one register.
1290   for (unsigned i = 0; i != MVT::VALUETYPE_SIZE; ++i) {
1291     NumRegistersForVT[i] = 1;
1292     RegisterTypeForVT[i] = TransformToType[i] = (MVT::SimpleValueType)i;
1293   }
1294   // ...except isVoid, which doesn't need any registers.
1295   NumRegistersForVT[MVT::isVoid] = 0;
1296 
1297   // Find the largest integer register class.
1298   unsigned LargestIntReg = MVT::LAST_INTEGER_VALUETYPE;
1299   for (; RegClassForVT[LargestIntReg] == nullptr; --LargestIntReg)
1300     assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
1301 
1302   // Every integer value type larger than this largest register takes twice as
1303   // many registers to represent as the previous ValueType.
1304   for (unsigned ExpandedReg = LargestIntReg + 1;
1305        ExpandedReg <= MVT::LAST_INTEGER_VALUETYPE; ++ExpandedReg) {
1306     NumRegistersForVT[ExpandedReg] = 2*NumRegistersForVT[ExpandedReg-1];
1307     RegisterTypeForVT[ExpandedReg] = (MVT::SimpleValueType)LargestIntReg;
1308     TransformToType[ExpandedReg] = (MVT::SimpleValueType)(ExpandedReg - 1);
1309     ValueTypeActions.setTypeAction((MVT::SimpleValueType)ExpandedReg,
1310                                    TypeExpandInteger);
1311   }
1312 
1313   // Inspect all of the ValueType's smaller than the largest integer
1314   // register to see which ones need promotion.
1315   unsigned LegalIntReg = LargestIntReg;
1316   for (unsigned IntReg = LargestIntReg - 1;
1317        IntReg >= (unsigned)MVT::i1; --IntReg) {
1318     MVT IVT = (MVT::SimpleValueType)IntReg;
1319     if (isTypeLegal(IVT)) {
1320       LegalIntReg = IntReg;
1321     } else {
1322       RegisterTypeForVT[IntReg] = TransformToType[IntReg] =
1323         (MVT::SimpleValueType)LegalIntReg;
1324       ValueTypeActions.setTypeAction(IVT, TypePromoteInteger);
1325     }
1326   }
1327 
1328   // ppcf128 type is really two f64's.
1329   if (!isTypeLegal(MVT::ppcf128)) {
1330     if (isTypeLegal(MVT::f64)) {
1331       NumRegistersForVT[MVT::ppcf128] = 2*NumRegistersForVT[MVT::f64];
1332       RegisterTypeForVT[MVT::ppcf128] = MVT::f64;
1333       TransformToType[MVT::ppcf128] = MVT::f64;
1334       ValueTypeActions.setTypeAction(MVT::ppcf128, TypeExpandFloat);
1335     } else {
1336       NumRegistersForVT[MVT::ppcf128] = NumRegistersForVT[MVT::i128];
1337       RegisterTypeForVT[MVT::ppcf128] = RegisterTypeForVT[MVT::i128];
1338       TransformToType[MVT::ppcf128] = MVT::i128;
1339       ValueTypeActions.setTypeAction(MVT::ppcf128, TypeSoftenFloat);
1340     }
1341   }
1342 
1343   // Decide how to handle f128. If the target does not have native f128 support,
1344   // expand it to i128 and we will be generating soft float library calls.
1345   if (!isTypeLegal(MVT::f128)) {
1346     NumRegistersForVT[MVT::f128] = NumRegistersForVT[MVT::i128];
1347     RegisterTypeForVT[MVT::f128] = RegisterTypeForVT[MVT::i128];
1348     TransformToType[MVT::f128] = MVT::i128;
1349     ValueTypeActions.setTypeAction(MVT::f128, TypeSoftenFloat);
1350   }
1351 
1352   // Decide how to handle f64. If the target does not have native f64 support,
1353   // expand it to i64 and we will be generating soft float library calls.
1354   if (!isTypeLegal(MVT::f64)) {
1355     NumRegistersForVT[MVT::f64] = NumRegistersForVT[MVT::i64];
1356     RegisterTypeForVT[MVT::f64] = RegisterTypeForVT[MVT::i64];
1357     TransformToType[MVT::f64] = MVT::i64;
1358     ValueTypeActions.setTypeAction(MVT::f64, TypeSoftenFloat);
1359   }
1360 
1361   // Decide how to handle f32. If the target does not have native f32 support,
1362   // expand it to i32 and we will be generating soft float library calls.
1363   if (!isTypeLegal(MVT::f32)) {
1364     NumRegistersForVT[MVT::f32] = NumRegistersForVT[MVT::i32];
1365     RegisterTypeForVT[MVT::f32] = RegisterTypeForVT[MVT::i32];
1366     TransformToType[MVT::f32] = MVT::i32;
1367     ValueTypeActions.setTypeAction(MVT::f32, TypeSoftenFloat);
1368   }
1369 
1370   // Decide how to handle f16. If the target does not have native f16 support,
1371   // promote it to f32, because there are no f16 library calls (except for
1372   // conversions).
1373   if (!isTypeLegal(MVT::f16)) {
1374     // Allow targets to control how we legalize half.
1375     if (softPromoteHalfType()) {
1376       NumRegistersForVT[MVT::f16] = NumRegistersForVT[MVT::i16];
1377       RegisterTypeForVT[MVT::f16] = RegisterTypeForVT[MVT::i16];
1378       TransformToType[MVT::f16] = MVT::f32;
1379       ValueTypeActions.setTypeAction(MVT::f16, TypeSoftPromoteHalf);
1380     } else {
1381       NumRegistersForVT[MVT::f16] = NumRegistersForVT[MVT::f32];
1382       RegisterTypeForVT[MVT::f16] = RegisterTypeForVT[MVT::f32];
1383       TransformToType[MVT::f16] = MVT::f32;
1384       ValueTypeActions.setTypeAction(MVT::f16, TypePromoteFloat);
1385     }
1386   }
1387 
1388   // Loop over all of the vector value types to see which need transformations.
1389   for (unsigned i = MVT::FIRST_VECTOR_VALUETYPE;
1390        i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
1391     MVT VT = (MVT::SimpleValueType) i;
1392     if (isTypeLegal(VT))
1393       continue;
1394 
1395     MVT EltVT = VT.getVectorElementType();
1396     ElementCount EC = VT.getVectorElementCount();
1397     bool IsLegalWiderType = false;
1398     bool IsScalable = VT.isScalableVector();
1399     LegalizeTypeAction PreferredAction = getPreferredVectorAction(VT);
1400     switch (PreferredAction) {
1401     case TypePromoteInteger: {
1402       MVT::SimpleValueType EndVT = IsScalable ?
1403                                    MVT::LAST_INTEGER_SCALABLE_VECTOR_VALUETYPE :
1404                                    MVT::LAST_INTEGER_FIXEDLEN_VECTOR_VALUETYPE;
1405       // Try to promote the elements of integer vectors. If no legal
1406       // promotion was found, fall through to the widen-vector method.
1407       for (unsigned nVT = i + 1;
1408            (MVT::SimpleValueType)nVT <= EndVT; ++nVT) {
1409         MVT SVT = (MVT::SimpleValueType) nVT;
1410         // Promote vectors of integers to vectors with the same number
1411         // of elements, with a wider element type.
1412         if (SVT.getScalarSizeInBits() > EltVT.getFixedSizeInBits() &&
1413             SVT.getVectorElementCount() == EC && isTypeLegal(SVT)) {
1414           TransformToType[i] = SVT;
1415           RegisterTypeForVT[i] = SVT;
1416           NumRegistersForVT[i] = 1;
1417           ValueTypeActions.setTypeAction(VT, TypePromoteInteger);
1418           IsLegalWiderType = true;
1419           break;
1420         }
1421       }
1422       if (IsLegalWiderType)
1423         break;
1424       LLVM_FALLTHROUGH;
1425     }
1426 
1427     case TypeWidenVector:
1428       if (isPowerOf2_32(EC.getKnownMinValue())) {
1429         // Try to widen the vector.
1430         for (unsigned nVT = i + 1; nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
1431           MVT SVT = (MVT::SimpleValueType) nVT;
1432           if (SVT.getVectorElementType() == EltVT &&
1433               SVT.isScalableVector() == IsScalable &&
1434               SVT.getVectorElementCount().getKnownMinValue() >
1435                   EC.getKnownMinValue() &&
1436               isTypeLegal(SVT)) {
1437             TransformToType[i] = SVT;
1438             RegisterTypeForVT[i] = SVT;
1439             NumRegistersForVT[i] = 1;
1440             ValueTypeActions.setTypeAction(VT, TypeWidenVector);
1441             IsLegalWiderType = true;
1442             break;
1443           }
1444         }
1445         if (IsLegalWiderType)
1446           break;
1447       } else {
1448         // Only widen to the next power of 2 to keep consistency with EVT.
1449         MVT NVT = VT.getPow2VectorType();
1450         if (isTypeLegal(NVT)) {
1451           TransformToType[i] = NVT;
1452           ValueTypeActions.setTypeAction(VT, TypeWidenVector);
1453           RegisterTypeForVT[i] = NVT;
1454           NumRegistersForVT[i] = 1;
1455           break;
1456         }
1457       }
1458       LLVM_FALLTHROUGH;
1459 
1460     case TypeSplitVector:
1461     case TypeScalarizeVector: {
1462       MVT IntermediateVT;
1463       MVT RegisterVT;
1464       unsigned NumIntermediates;
1465       unsigned NumRegisters = getVectorTypeBreakdownMVT(VT, IntermediateVT,
1466           NumIntermediates, RegisterVT, this);
1467       NumRegistersForVT[i] = NumRegisters;
1468       assert(NumRegistersForVT[i] == NumRegisters &&
1469              "NumRegistersForVT size cannot represent NumRegisters!");
1470       RegisterTypeForVT[i] = RegisterVT;
1471 
1472       MVT NVT = VT.getPow2VectorType();
1473       if (NVT == VT) {
1474         // Type is already a power of 2.  The default action is to split.
1475         TransformToType[i] = MVT::Other;
1476         if (PreferredAction == TypeScalarizeVector)
1477           ValueTypeActions.setTypeAction(VT, TypeScalarizeVector);
1478         else if (PreferredAction == TypeSplitVector)
1479           ValueTypeActions.setTypeAction(VT, TypeSplitVector);
1480         else if (EC.getKnownMinValue() > 1)
1481           ValueTypeActions.setTypeAction(VT, TypeSplitVector);
1482         else
1483           ValueTypeActions.setTypeAction(VT, EC.isScalable()
1484                                                  ? TypeScalarizeScalableVector
1485                                                  : TypeScalarizeVector);
1486       } else {
1487         TransformToType[i] = NVT;
1488         ValueTypeActions.setTypeAction(VT, TypeWidenVector);
1489       }
1490       break;
1491     }
1492     default:
1493       llvm_unreachable("Unknown vector legalization action!");
1494     }
1495   }
1496 
1497   // Determine the 'representative' register class for each value type.
1498   // An representative register class is the largest (meaning one which is
1499   // not a sub-register class / subreg register class) legal register class for
1500   // a group of value types. For example, on i386, i8, i16, and i32
1501   // representative would be GR32; while on x86_64 it's GR64.
1502   for (unsigned i = 0; i != MVT::VALUETYPE_SIZE; ++i) {
1503     const TargetRegisterClass* RRC;
1504     uint8_t Cost;
1505     std::tie(RRC, Cost) = findRepresentativeClass(TRI, (MVT::SimpleValueType)i);
1506     RepRegClassForVT[i] = RRC;
1507     RepRegClassCostForVT[i] = Cost;
1508   }
1509 }
1510 
1511 EVT TargetLoweringBase::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1512                                            EVT VT) const {
1513   assert(!VT.isVector() && "No default SetCC type for vectors!");
1514   return getPointerTy(DL).SimpleTy;
1515 }
1516 
1517 MVT::SimpleValueType TargetLoweringBase::getCmpLibcallReturnType() const {
1518   return MVT::i32; // return the default value
1519 }
1520 
1521 /// getVectorTypeBreakdown - Vector types are broken down into some number of
1522 /// legal first class types.  For example, MVT::v8f32 maps to 2 MVT::v4f32
1523 /// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
1524 /// Similarly, MVT::v2i64 turns into 4 MVT::i32 values with both PPC and X86.
1525 ///
1526 /// This method returns the number of registers needed, and the VT for each
1527 /// register.  It also returns the VT and quantity of the intermediate values
1528 /// before they are promoted/expanded.
1529 unsigned TargetLoweringBase::getVectorTypeBreakdown(LLVMContext &Context,
1530                                                     EVT VT, EVT &IntermediateVT,
1531                                                     unsigned &NumIntermediates,
1532                                                     MVT &RegisterVT) const {
1533   ElementCount EltCnt = VT.getVectorElementCount();
1534 
1535   // If there is a wider vector type with the same element type as this one,
1536   // or a promoted vector type that has the same number of elements which
1537   // are wider, then we should convert to that legal vector type.
1538   // This handles things like <2 x float> -> <4 x float> and
1539   // <4 x i1> -> <4 x i32>.
1540   LegalizeTypeAction TA = getTypeAction(Context, VT);
1541   if (!EltCnt.isScalar() &&
1542       (TA == TypeWidenVector || TA == TypePromoteInteger)) {
1543     EVT RegisterEVT = getTypeToTransformTo(Context, VT);
1544     if (isTypeLegal(RegisterEVT)) {
1545       IntermediateVT = RegisterEVT;
1546       RegisterVT = RegisterEVT.getSimpleVT();
1547       NumIntermediates = 1;
1548       return 1;
1549     }
1550   }
1551 
1552   // Figure out the right, legal destination reg to copy into.
1553   EVT EltTy = VT.getVectorElementType();
1554 
1555   unsigned NumVectorRegs = 1;
1556 
1557   // Scalable vectors cannot be scalarized, so handle the legalisation of the
1558   // types like done elsewhere in SelectionDAG.
1559   if (EltCnt.isScalable()) {
1560     LegalizeKind LK;
1561     EVT PartVT = VT;
1562     do {
1563       // Iterate until we've found a legal (part) type to hold VT.
1564       LK = getTypeConversion(Context, PartVT);
1565       PartVT = LK.second;
1566     } while (LK.first != TypeLegal);
1567 
1568     if (!PartVT.isVector()) {
1569       report_fatal_error(
1570           "Don't know how to legalize this scalable vector type");
1571     }
1572 
1573     NumIntermediates =
1574         divideCeil(VT.getVectorElementCount().getKnownMinValue(),
1575                    PartVT.getVectorElementCount().getKnownMinValue());
1576     IntermediateVT = PartVT;
1577     RegisterVT = getRegisterType(Context, IntermediateVT);
1578     return NumIntermediates;
1579   }
1580 
1581   // FIXME: We don't support non-power-of-2-sized vectors for now.  Ideally
1582   // we could break down into LHS/RHS like LegalizeDAG does.
1583   if (!isPowerOf2_32(EltCnt.getKnownMinValue())) {
1584     NumVectorRegs = EltCnt.getKnownMinValue();
1585     EltCnt = ElementCount::getFixed(1);
1586   }
1587 
1588   // Divide the input until we get to a supported size.  This will always
1589   // end with a scalar if the target doesn't support vectors.
1590   while (EltCnt.getKnownMinValue() > 1 &&
1591          !isTypeLegal(EVT::getVectorVT(Context, EltTy, EltCnt))) {
1592     EltCnt = EltCnt.divideCoefficientBy(2);
1593     NumVectorRegs <<= 1;
1594   }
1595 
1596   NumIntermediates = NumVectorRegs;
1597 
1598   EVT NewVT = EVT::getVectorVT(Context, EltTy, EltCnt);
1599   if (!isTypeLegal(NewVT))
1600     NewVT = EltTy;
1601   IntermediateVT = NewVT;
1602 
1603   MVT DestVT = getRegisterType(Context, NewVT);
1604   RegisterVT = DestVT;
1605 
1606   if (EVT(DestVT).bitsLT(NewVT)) {  // Value is expanded, e.g. i64 -> i16.
1607     TypeSize NewVTSize = NewVT.getSizeInBits();
1608     // Convert sizes such as i33 to i64.
1609     if (!isPowerOf2_32(NewVTSize.getKnownMinSize()))
1610       NewVTSize = NewVTSize.coefficientNextPowerOf2();
1611     return NumVectorRegs*(NewVTSize/DestVT.getSizeInBits());
1612   }
1613 
1614   // Otherwise, promotion or legal types use the same number of registers as
1615   // the vector decimated to the appropriate level.
1616   return NumVectorRegs;
1617 }
1618 
1619 bool TargetLoweringBase::isSuitableForJumpTable(const SwitchInst *SI,
1620                                                 uint64_t NumCases,
1621                                                 uint64_t Range,
1622                                                 ProfileSummaryInfo *PSI,
1623                                                 BlockFrequencyInfo *BFI) const {
1624   // FIXME: This function check the maximum table size and density, but the
1625   // minimum size is not checked. It would be nice if the minimum size is
1626   // also combined within this function. Currently, the minimum size check is
1627   // performed in findJumpTable() in SelectionDAGBuiler and
1628   // getEstimatedNumberOfCaseClusters() in BasicTTIImpl.
1629   const bool OptForSize =
1630       SI->getParent()->getParent()->hasOptSize() ||
1631       llvm::shouldOptimizeForSize(SI->getParent(), PSI, BFI);
1632   const unsigned MinDensity = getMinimumJumpTableDensity(OptForSize);
1633   const unsigned MaxJumpTableSize = getMaximumJumpTableSize();
1634 
1635   // Check whether the number of cases is small enough and
1636   // the range is dense enough for a jump table.
1637   return (OptForSize || Range <= MaxJumpTableSize) &&
1638          (NumCases * 100 >= Range * MinDensity);
1639 }
1640 
1641 /// Get the EVTs and ArgFlags collections that represent the legalized return
1642 /// type of the given function.  This does not require a DAG or a return value,
1643 /// and is suitable for use before any DAGs for the function are constructed.
1644 /// TODO: Move this out of TargetLowering.cpp.
1645 void llvm::GetReturnInfo(CallingConv::ID CC, Type *ReturnType,
1646                          AttributeList attr,
1647                          SmallVectorImpl<ISD::OutputArg> &Outs,
1648                          const TargetLowering &TLI, const DataLayout &DL) {
1649   SmallVector<EVT, 4> ValueVTs;
1650   ComputeValueVTs(TLI, DL, ReturnType, ValueVTs);
1651   unsigned NumValues = ValueVTs.size();
1652   if (NumValues == 0) return;
1653 
1654   for (unsigned j = 0, f = NumValues; j != f; ++j) {
1655     EVT VT = ValueVTs[j];
1656     ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
1657 
1658     if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::SExt))
1659       ExtendKind = ISD::SIGN_EXTEND;
1660     else if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::ZExt))
1661       ExtendKind = ISD::ZERO_EXTEND;
1662 
1663     // FIXME: C calling convention requires the return type to be promoted to
1664     // at least 32-bit. But this is not necessary for non-C calling
1665     // conventions. The frontend should mark functions whose return values
1666     // require promoting with signext or zeroext attributes.
1667     if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
1668       MVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
1669       if (VT.bitsLT(MinVT))
1670         VT = MinVT;
1671     }
1672 
1673     unsigned NumParts =
1674         TLI.getNumRegistersForCallingConv(ReturnType->getContext(), CC, VT);
1675     MVT PartVT =
1676         TLI.getRegisterTypeForCallingConv(ReturnType->getContext(), CC, VT);
1677 
1678     // 'inreg' on function refers to return value
1679     ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
1680     if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::InReg))
1681       Flags.setInReg();
1682 
1683     // Propagate extension type if any
1684     if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::SExt))
1685       Flags.setSExt();
1686     else if (attr.hasAttribute(AttributeList::ReturnIndex, Attribute::ZExt))
1687       Flags.setZExt();
1688 
1689     for (unsigned i = 0; i < NumParts; ++i)
1690       Outs.push_back(ISD::OutputArg(Flags, PartVT, VT, /*isfixed=*/true, 0, 0));
1691   }
1692 }
1693 
1694 /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1695 /// function arguments in the caller parameter area.  This is the actual
1696 /// alignment, not its logarithm.
1697 unsigned TargetLoweringBase::getByValTypeAlignment(Type *Ty,
1698                                                    const DataLayout &DL) const {
1699   return DL.getABITypeAlign(Ty).value();
1700 }
1701 
1702 bool TargetLoweringBase::allowsMemoryAccessForAlignment(
1703     LLVMContext &Context, const DataLayout &DL, EVT VT, unsigned AddrSpace,
1704     Align Alignment, MachineMemOperand::Flags Flags, bool *Fast) const {
1705   // Check if the specified alignment is sufficient based on the data layout.
1706   // TODO: While using the data layout works in practice, a better solution
1707   // would be to implement this check directly (make this a virtual function).
1708   // For example, the ABI alignment may change based on software platform while
1709   // this function should only be affected by hardware implementation.
1710   Type *Ty = VT.getTypeForEVT(Context);
1711   if (VT.isZeroSized() || Alignment >= DL.getABITypeAlign(Ty)) {
1712     // Assume that an access that meets the ABI-specified alignment is fast.
1713     if (Fast != nullptr)
1714       *Fast = true;
1715     return true;
1716   }
1717 
1718   // This is a misaligned access.
1719   return allowsMisalignedMemoryAccesses(VT, AddrSpace, Alignment, Flags, Fast);
1720 }
1721 
1722 bool TargetLoweringBase::allowsMemoryAccessForAlignment(
1723     LLVMContext &Context, const DataLayout &DL, EVT VT,
1724     const MachineMemOperand &MMO, bool *Fast) const {
1725   return allowsMemoryAccessForAlignment(Context, DL, VT, MMO.getAddrSpace(),
1726                                         MMO.getAlign(), MMO.getFlags(), Fast);
1727 }
1728 
1729 bool TargetLoweringBase::allowsMemoryAccess(LLVMContext &Context,
1730                                             const DataLayout &DL, EVT VT,
1731                                             unsigned AddrSpace, Align Alignment,
1732                                             MachineMemOperand::Flags Flags,
1733                                             bool *Fast) const {
1734   return allowsMemoryAccessForAlignment(Context, DL, VT, AddrSpace, Alignment,
1735                                         Flags, Fast);
1736 }
1737 
1738 bool TargetLoweringBase::allowsMemoryAccess(LLVMContext &Context,
1739                                             const DataLayout &DL, EVT VT,
1740                                             const MachineMemOperand &MMO,
1741                                             bool *Fast) const {
1742   return allowsMemoryAccess(Context, DL, VT, MMO.getAddrSpace(), MMO.getAlign(),
1743                             MMO.getFlags(), Fast);
1744 }
1745 
1746 bool TargetLoweringBase::allowsMemoryAccess(LLVMContext &Context,
1747                                             const DataLayout &DL, LLT Ty,
1748                                             const MachineMemOperand &MMO,
1749                                             bool *Fast) const {
1750   return allowsMemoryAccess(Context, DL, getMVTForLLT(Ty), MMO.getAddrSpace(),
1751                             MMO.getAlign(), MMO.getFlags(), Fast);
1752 }
1753 
1754 //===----------------------------------------------------------------------===//
1755 //  TargetTransformInfo Helpers
1756 //===----------------------------------------------------------------------===//
1757 
1758 int TargetLoweringBase::InstructionOpcodeToISD(unsigned Opcode) const {
1759   enum InstructionOpcodes {
1760 #define HANDLE_INST(NUM, OPCODE, CLASS) OPCODE = NUM,
1761 #define LAST_OTHER_INST(NUM) InstructionOpcodesCount = NUM
1762 #include "llvm/IR/Instruction.def"
1763   };
1764   switch (static_cast<InstructionOpcodes>(Opcode)) {
1765   case Ret:            return 0;
1766   case Br:             return 0;
1767   case Switch:         return 0;
1768   case IndirectBr:     return 0;
1769   case Invoke:         return 0;
1770   case CallBr:         return 0;
1771   case Resume:         return 0;
1772   case Unreachable:    return 0;
1773   case CleanupRet:     return 0;
1774   case CatchRet:       return 0;
1775   case CatchPad:       return 0;
1776   case CatchSwitch:    return 0;
1777   case CleanupPad:     return 0;
1778   case FNeg:           return ISD::FNEG;
1779   case Add:            return ISD::ADD;
1780   case FAdd:           return ISD::FADD;
1781   case Sub:            return ISD::SUB;
1782   case FSub:           return ISD::FSUB;
1783   case Mul:            return ISD::MUL;
1784   case FMul:           return ISD::FMUL;
1785   case UDiv:           return ISD::UDIV;
1786   case SDiv:           return ISD::SDIV;
1787   case FDiv:           return ISD::FDIV;
1788   case URem:           return ISD::UREM;
1789   case SRem:           return ISD::SREM;
1790   case FRem:           return ISD::FREM;
1791   case Shl:            return ISD::SHL;
1792   case LShr:           return ISD::SRL;
1793   case AShr:           return ISD::SRA;
1794   case And:            return ISD::AND;
1795   case Or:             return ISD::OR;
1796   case Xor:            return ISD::XOR;
1797   case Alloca:         return 0;
1798   case Load:           return ISD::LOAD;
1799   case Store:          return ISD::STORE;
1800   case GetElementPtr:  return 0;
1801   case Fence:          return 0;
1802   case AtomicCmpXchg:  return 0;
1803   case AtomicRMW:      return 0;
1804   case Trunc:          return ISD::TRUNCATE;
1805   case ZExt:           return ISD::ZERO_EXTEND;
1806   case SExt:           return ISD::SIGN_EXTEND;
1807   case FPToUI:         return ISD::FP_TO_UINT;
1808   case FPToSI:         return ISD::FP_TO_SINT;
1809   case UIToFP:         return ISD::UINT_TO_FP;
1810   case SIToFP:         return ISD::SINT_TO_FP;
1811   case FPTrunc:        return ISD::FP_ROUND;
1812   case FPExt:          return ISD::FP_EXTEND;
1813   case PtrToInt:       return ISD::BITCAST;
1814   case IntToPtr:       return ISD::BITCAST;
1815   case BitCast:        return ISD::BITCAST;
1816   case AddrSpaceCast:  return ISD::ADDRSPACECAST;
1817   case ICmp:           return ISD::SETCC;
1818   case FCmp:           return ISD::SETCC;
1819   case PHI:            return 0;
1820   case Call:           return 0;
1821   case Select:         return ISD::SELECT;
1822   case UserOp1:        return 0;
1823   case UserOp2:        return 0;
1824   case VAArg:          return 0;
1825   case ExtractElement: return ISD::EXTRACT_VECTOR_ELT;
1826   case InsertElement:  return ISD::INSERT_VECTOR_ELT;
1827   case ShuffleVector:  return ISD::VECTOR_SHUFFLE;
1828   case ExtractValue:   return ISD::MERGE_VALUES;
1829   case InsertValue:    return ISD::MERGE_VALUES;
1830   case LandingPad:     return 0;
1831   case Freeze:         return ISD::FREEZE;
1832   }
1833 
1834   llvm_unreachable("Unknown instruction type encountered!");
1835 }
1836 
1837 std::pair<InstructionCost, MVT>
1838 TargetLoweringBase::getTypeLegalizationCost(const DataLayout &DL,
1839                                             Type *Ty) const {
1840   LLVMContext &C = Ty->getContext();
1841   EVT MTy = getValueType(DL, Ty);
1842 
1843   InstructionCost Cost = 1;
1844   // We keep legalizing the type until we find a legal kind. We assume that
1845   // the only operation that costs anything is the split. After splitting
1846   // we need to handle two types.
1847   while (true) {
1848     LegalizeKind LK = getTypeConversion(C, MTy);
1849 
1850     if (LK.first == TypeScalarizeScalableVector)
1851       return std::make_pair(InstructionCost::getInvalid(), MVT::getVT(Ty));
1852 
1853     if (LK.first == TypeLegal)
1854       return std::make_pair(Cost, MTy.getSimpleVT());
1855 
1856     if (LK.first == TypeSplitVector || LK.first == TypeExpandInteger)
1857       Cost *= 2;
1858 
1859     // Do not loop with f128 type.
1860     if (MTy == LK.second)
1861       return std::make_pair(Cost, MTy.getSimpleVT());
1862 
1863     // Keep legalizing the type.
1864     MTy = LK.second;
1865   }
1866 }
1867 
1868 Value *
1869 TargetLoweringBase::getDefaultSafeStackPointerLocation(IRBuilderBase &IRB,
1870                                                        bool UseTLS) const {
1871   // compiler-rt provides a variable with a magic name.  Targets that do not
1872   // link with compiler-rt may also provide such a variable.
1873   Module *M = IRB.GetInsertBlock()->getParent()->getParent();
1874   const char *UnsafeStackPtrVar = "__safestack_unsafe_stack_ptr";
1875   auto UnsafeStackPtr =
1876       dyn_cast_or_null<GlobalVariable>(M->getNamedValue(UnsafeStackPtrVar));
1877 
1878   Type *StackPtrTy = Type::getInt8PtrTy(M->getContext());
1879 
1880   if (!UnsafeStackPtr) {
1881     auto TLSModel = UseTLS ?
1882         GlobalValue::InitialExecTLSModel :
1883         GlobalValue::NotThreadLocal;
1884     // The global variable is not defined yet, define it ourselves.
1885     // We use the initial-exec TLS model because we do not support the
1886     // variable living anywhere other than in the main executable.
1887     UnsafeStackPtr = new GlobalVariable(
1888         *M, StackPtrTy, false, GlobalValue::ExternalLinkage, nullptr,
1889         UnsafeStackPtrVar, nullptr, TLSModel);
1890   } else {
1891     // The variable exists, check its type and attributes.
1892     if (UnsafeStackPtr->getValueType() != StackPtrTy)
1893       report_fatal_error(Twine(UnsafeStackPtrVar) + " must have void* type");
1894     if (UseTLS != UnsafeStackPtr->isThreadLocal())
1895       report_fatal_error(Twine(UnsafeStackPtrVar) + " must " +
1896                          (UseTLS ? "" : "not ") + "be thread-local");
1897   }
1898   return UnsafeStackPtr;
1899 }
1900 
1901 Value *
1902 TargetLoweringBase::getSafeStackPointerLocation(IRBuilderBase &IRB) const {
1903   if (!TM.getTargetTriple().isAndroid())
1904     return getDefaultSafeStackPointerLocation(IRB, true);
1905 
1906   // Android provides a libc function to retrieve the address of the current
1907   // thread's unsafe stack pointer.
1908   Module *M = IRB.GetInsertBlock()->getParent()->getParent();
1909   Type *StackPtrTy = Type::getInt8PtrTy(M->getContext());
1910   FunctionCallee Fn = M->getOrInsertFunction("__safestack_pointer_address",
1911                                              StackPtrTy->getPointerTo(0));
1912   return IRB.CreateCall(Fn);
1913 }
1914 
1915 //===----------------------------------------------------------------------===//
1916 //  Loop Strength Reduction hooks
1917 //===----------------------------------------------------------------------===//
1918 
1919 /// isLegalAddressingMode - Return true if the addressing mode represented
1920 /// by AM is legal for this target, for a load/store of the specified type.
1921 bool TargetLoweringBase::isLegalAddressingMode(const DataLayout &DL,
1922                                                const AddrMode &AM, Type *Ty,
1923                                                unsigned AS, Instruction *I) const {
1924   // The default implementation of this implements a conservative RISCy, r+r and
1925   // r+i addr mode.
1926 
1927   // Allows a sign-extended 16-bit immediate field.
1928   if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
1929     return false;
1930 
1931   // No global is ever allowed as a base.
1932   if (AM.BaseGV)
1933     return false;
1934 
1935   // Only support r+r,
1936   switch (AM.Scale) {
1937   case 0:  // "r+i" or just "i", depending on HasBaseReg.
1938     break;
1939   case 1:
1940     if (AM.HasBaseReg && AM.BaseOffs)  // "r+r+i" is not allowed.
1941       return false;
1942     // Otherwise we have r+r or r+i.
1943     break;
1944   case 2:
1945     if (AM.HasBaseReg || AM.BaseOffs)  // 2*r+r  or  2*r+i is not allowed.
1946       return false;
1947     // Allow 2*r as r+r.
1948     break;
1949   default: // Don't allow n * r
1950     return false;
1951   }
1952 
1953   return true;
1954 }
1955 
1956 //===----------------------------------------------------------------------===//
1957 //  Stack Protector
1958 //===----------------------------------------------------------------------===//
1959 
1960 // For OpenBSD return its special guard variable. Otherwise return nullptr,
1961 // so that SelectionDAG handle SSP.
1962 Value *TargetLoweringBase::getIRStackGuard(IRBuilderBase &IRB) const {
1963   if (getTargetMachine().getTargetTriple().isOSOpenBSD()) {
1964     Module &M = *IRB.GetInsertBlock()->getParent()->getParent();
1965     PointerType *PtrTy = Type::getInt8PtrTy(M.getContext());
1966     Constant *C = M.getOrInsertGlobal("__guard_local", PtrTy);
1967     if (GlobalVariable *G = dyn_cast_or_null<GlobalVariable>(C))
1968       G->setVisibility(GlobalValue::HiddenVisibility);
1969     return C;
1970   }
1971   return nullptr;
1972 }
1973 
1974 // Currently only support "standard" __stack_chk_guard.
1975 // TODO: add LOAD_STACK_GUARD support.
1976 void TargetLoweringBase::insertSSPDeclarations(Module &M) const {
1977   if (!M.getNamedValue("__stack_chk_guard")) {
1978     auto *GV = new GlobalVariable(M, Type::getInt8PtrTy(M.getContext()), false,
1979                                   GlobalVariable::ExternalLinkage, nullptr,
1980                                   "__stack_chk_guard");
1981     if (TM.getRelocationModel() == Reloc::Static &&
1982         !TM.getTargetTriple().isWindowsGNUEnvironment())
1983       GV->setDSOLocal(true);
1984   }
1985 }
1986 
1987 // Currently only support "standard" __stack_chk_guard.
1988 // TODO: add LOAD_STACK_GUARD support.
1989 Value *TargetLoweringBase::getSDagStackGuard(const Module &M) const {
1990   return M.getNamedValue("__stack_chk_guard");
1991 }
1992 
1993 Function *TargetLoweringBase::getSSPStackGuardCheck(const Module &M) const {
1994   return nullptr;
1995 }
1996 
1997 unsigned TargetLoweringBase::getMinimumJumpTableEntries() const {
1998   return MinimumJumpTableEntries;
1999 }
2000 
2001 void TargetLoweringBase::setMinimumJumpTableEntries(unsigned Val) {
2002   MinimumJumpTableEntries = Val;
2003 }
2004 
2005 unsigned TargetLoweringBase::getMinimumJumpTableDensity(bool OptForSize) const {
2006   return OptForSize ? OptsizeJumpTableDensity : JumpTableDensity;
2007 }
2008 
2009 unsigned TargetLoweringBase::getMaximumJumpTableSize() const {
2010   return MaximumJumpTableSize;
2011 }
2012 
2013 void TargetLoweringBase::setMaximumJumpTableSize(unsigned Val) {
2014   MaximumJumpTableSize = Val;
2015 }
2016 
2017 bool TargetLoweringBase::isJumpTableRelative() const {
2018   return getTargetMachine().isPositionIndependent();
2019 }
2020 
2021 //===----------------------------------------------------------------------===//
2022 //  Reciprocal Estimates
2023 //===----------------------------------------------------------------------===//
2024 
2025 /// Get the reciprocal estimate attribute string for a function that will
2026 /// override the target defaults.
2027 static StringRef getRecipEstimateForFunc(MachineFunction &MF) {
2028   const Function &F = MF.getFunction();
2029   return F.getFnAttribute("reciprocal-estimates").getValueAsString();
2030 }
2031 
2032 /// Construct a string for the given reciprocal operation of the given type.
2033 /// This string should match the corresponding option to the front-end's
2034 /// "-mrecip" flag assuming those strings have been passed through in an
2035 /// attribute string. For example, "vec-divf" for a division of a vXf32.
2036 static std::string getReciprocalOpName(bool IsSqrt, EVT VT) {
2037   std::string Name = VT.isVector() ? "vec-" : "";
2038 
2039   Name += IsSqrt ? "sqrt" : "div";
2040 
2041   // TODO: Handle "half" or other float types?
2042   if (VT.getScalarType() == MVT::f64) {
2043     Name += "d";
2044   } else {
2045     assert(VT.getScalarType() == MVT::f32 &&
2046            "Unexpected FP type for reciprocal estimate");
2047     Name += "f";
2048   }
2049 
2050   return Name;
2051 }
2052 
2053 /// Return the character position and value (a single numeric character) of a
2054 /// customized refinement operation in the input string if it exists. Return
2055 /// false if there is no customized refinement step count.
2056 static bool parseRefinementStep(StringRef In, size_t &Position,
2057                                 uint8_t &Value) {
2058   const char RefStepToken = ':';
2059   Position = In.find(RefStepToken);
2060   if (Position == StringRef::npos)
2061     return false;
2062 
2063   StringRef RefStepString = In.substr(Position + 1);
2064   // Allow exactly one numeric character for the additional refinement
2065   // step parameter.
2066   if (RefStepString.size() == 1) {
2067     char RefStepChar = RefStepString[0];
2068     if (isDigit(RefStepChar)) {
2069       Value = RefStepChar - '0';
2070       return true;
2071     }
2072   }
2073   report_fatal_error("Invalid refinement step for -recip.");
2074 }
2075 
2076 /// For the input attribute string, return one of the ReciprocalEstimate enum
2077 /// status values (enabled, disabled, or not specified) for this operation on
2078 /// the specified data type.
2079 static int getOpEnabled(bool IsSqrt, EVT VT, StringRef Override) {
2080   if (Override.empty())
2081     return TargetLoweringBase::ReciprocalEstimate::Unspecified;
2082 
2083   SmallVector<StringRef, 4> OverrideVector;
2084   Override.split(OverrideVector, ',');
2085   unsigned NumArgs = OverrideVector.size();
2086 
2087   // Check if "all", "none", or "default" was specified.
2088   if (NumArgs == 1) {
2089     // Look for an optional setting of the number of refinement steps needed
2090     // for this type of reciprocal operation.
2091     size_t RefPos;
2092     uint8_t RefSteps;
2093     if (parseRefinementStep(Override, RefPos, RefSteps)) {
2094       // Split the string for further processing.
2095       Override = Override.substr(0, RefPos);
2096     }
2097 
2098     // All reciprocal types are enabled.
2099     if (Override == "all")
2100       return TargetLoweringBase::ReciprocalEstimate::Enabled;
2101 
2102     // All reciprocal types are disabled.
2103     if (Override == "none")
2104       return TargetLoweringBase::ReciprocalEstimate::Disabled;
2105 
2106     // Target defaults for enablement are used.
2107     if (Override == "default")
2108       return TargetLoweringBase::ReciprocalEstimate::Unspecified;
2109   }
2110 
2111   // The attribute string may omit the size suffix ('f'/'d').
2112   std::string VTName = getReciprocalOpName(IsSqrt, VT);
2113   std::string VTNameNoSize = VTName;
2114   VTNameNoSize.pop_back();
2115   static const char DisabledPrefix = '!';
2116 
2117   for (StringRef RecipType : OverrideVector) {
2118     size_t RefPos;
2119     uint8_t RefSteps;
2120     if (parseRefinementStep(RecipType, RefPos, RefSteps))
2121       RecipType = RecipType.substr(0, RefPos);
2122 
2123     // Ignore the disablement token for string matching.
2124     bool IsDisabled = RecipType[0] == DisabledPrefix;
2125     if (IsDisabled)
2126       RecipType = RecipType.substr(1);
2127 
2128     if (RecipType.equals(VTName) || RecipType.equals(VTNameNoSize))
2129       return IsDisabled ? TargetLoweringBase::ReciprocalEstimate::Disabled
2130                         : TargetLoweringBase::ReciprocalEstimate::Enabled;
2131   }
2132 
2133   return TargetLoweringBase::ReciprocalEstimate::Unspecified;
2134 }
2135 
2136 /// For the input attribute string, return the customized refinement step count
2137 /// for this operation on the specified data type. If the step count does not
2138 /// exist, return the ReciprocalEstimate enum value for unspecified.
2139 static int getOpRefinementSteps(bool IsSqrt, EVT VT, StringRef Override) {
2140   if (Override.empty())
2141     return TargetLoweringBase::ReciprocalEstimate::Unspecified;
2142 
2143   SmallVector<StringRef, 4> OverrideVector;
2144   Override.split(OverrideVector, ',');
2145   unsigned NumArgs = OverrideVector.size();
2146 
2147   // Check if "all", "default", or "none" was specified.
2148   if (NumArgs == 1) {
2149     // Look for an optional setting of the number of refinement steps needed
2150     // for this type of reciprocal operation.
2151     size_t RefPos;
2152     uint8_t RefSteps;
2153     if (!parseRefinementStep(Override, RefPos, RefSteps))
2154       return TargetLoweringBase::ReciprocalEstimate::Unspecified;
2155 
2156     // Split the string for further processing.
2157     Override = Override.substr(0, RefPos);
2158     assert(Override != "none" &&
2159            "Disabled reciprocals, but specifed refinement steps?");
2160 
2161     // If this is a general override, return the specified number of steps.
2162     if (Override == "all" || Override == "default")
2163       return RefSteps;
2164   }
2165 
2166   // The attribute string may omit the size suffix ('f'/'d').
2167   std::string VTName = getReciprocalOpName(IsSqrt, VT);
2168   std::string VTNameNoSize = VTName;
2169   VTNameNoSize.pop_back();
2170 
2171   for (StringRef RecipType : OverrideVector) {
2172     size_t RefPos;
2173     uint8_t RefSteps;
2174     if (!parseRefinementStep(RecipType, RefPos, RefSteps))
2175       continue;
2176 
2177     RecipType = RecipType.substr(0, RefPos);
2178     if (RecipType.equals(VTName) || RecipType.equals(VTNameNoSize))
2179       return RefSteps;
2180   }
2181 
2182   return TargetLoweringBase::ReciprocalEstimate::Unspecified;
2183 }
2184 
2185 int TargetLoweringBase::getRecipEstimateSqrtEnabled(EVT VT,
2186                                                     MachineFunction &MF) const {
2187   return getOpEnabled(true, VT, getRecipEstimateForFunc(MF));
2188 }
2189 
2190 int TargetLoweringBase::getRecipEstimateDivEnabled(EVT VT,
2191                                                    MachineFunction &MF) const {
2192   return getOpEnabled(false, VT, getRecipEstimateForFunc(MF));
2193 }
2194 
2195 int TargetLoweringBase::getSqrtRefinementSteps(EVT VT,
2196                                                MachineFunction &MF) const {
2197   return getOpRefinementSteps(true, VT, getRecipEstimateForFunc(MF));
2198 }
2199 
2200 int TargetLoweringBase::getDivRefinementSteps(EVT VT,
2201                                               MachineFunction &MF) const {
2202   return getOpRefinementSteps(false, VT, getRecipEstimateForFunc(MF));
2203 }
2204 
2205 void TargetLoweringBase::finalizeLowering(MachineFunction &MF) const {
2206   MF.getRegInfo().freezeReservedRegs(MF);
2207 }
2208 
2209 MachineMemOperand::Flags
2210 TargetLoweringBase::getLoadMemOperandFlags(const LoadInst &LI,
2211                                            const DataLayout &DL) const {
2212   MachineMemOperand::Flags Flags = MachineMemOperand::MOLoad;
2213   if (LI.isVolatile())
2214     Flags |= MachineMemOperand::MOVolatile;
2215 
2216   if (LI.hasMetadata(LLVMContext::MD_nontemporal))
2217     Flags |= MachineMemOperand::MONonTemporal;
2218 
2219   if (LI.hasMetadata(LLVMContext::MD_invariant_load))
2220     Flags |= MachineMemOperand::MOInvariant;
2221 
2222   if (isDereferenceablePointer(LI.getPointerOperand(), LI.getType(), DL))
2223     Flags |= MachineMemOperand::MODereferenceable;
2224 
2225   Flags |= getTargetMMOFlags(LI);
2226   return Flags;
2227 }
2228 
2229 MachineMemOperand::Flags
2230 TargetLoweringBase::getStoreMemOperandFlags(const StoreInst &SI,
2231                                             const DataLayout &DL) const {
2232   MachineMemOperand::Flags Flags = MachineMemOperand::MOStore;
2233 
2234   if (SI.isVolatile())
2235     Flags |= MachineMemOperand::MOVolatile;
2236 
2237   if (SI.hasMetadata(LLVMContext::MD_nontemporal))
2238     Flags |= MachineMemOperand::MONonTemporal;
2239 
2240   // FIXME: Not preserving dereferenceable
2241   Flags |= getTargetMMOFlags(SI);
2242   return Flags;
2243 }
2244 
2245 MachineMemOperand::Flags
2246 TargetLoweringBase::getAtomicMemOperandFlags(const Instruction &AI,
2247                                              const DataLayout &DL) const {
2248   auto Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore;
2249 
2250   if (const AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(&AI)) {
2251     if (RMW->isVolatile())
2252       Flags |= MachineMemOperand::MOVolatile;
2253   } else if (const AtomicCmpXchgInst *CmpX = dyn_cast<AtomicCmpXchgInst>(&AI)) {
2254     if (CmpX->isVolatile())
2255       Flags |= MachineMemOperand::MOVolatile;
2256   } else
2257     llvm_unreachable("not an atomic instruction");
2258 
2259   // FIXME: Not preserving dereferenceable
2260   Flags |= getTargetMMOFlags(AI);
2261   return Flags;
2262 }
2263 
2264 Instruction *TargetLoweringBase::emitLeadingFence(IRBuilderBase &Builder,
2265                                                   Instruction *Inst,
2266                                                   AtomicOrdering Ord) const {
2267   if (isReleaseOrStronger(Ord) && Inst->hasAtomicStore())
2268     return Builder.CreateFence(Ord);
2269   else
2270     return nullptr;
2271 }
2272 
2273 Instruction *TargetLoweringBase::emitTrailingFence(IRBuilderBase &Builder,
2274                                                    Instruction *Inst,
2275                                                    AtomicOrdering Ord) const {
2276   if (isAcquireOrStronger(Ord))
2277     return Builder.CreateFence(Ord);
2278   else
2279     return nullptr;
2280 }
2281 
2282 //===----------------------------------------------------------------------===//
2283 //  GlobalISel Hooks
2284 //===----------------------------------------------------------------------===//
2285 
2286 bool TargetLoweringBase::shouldLocalize(const MachineInstr &MI,
2287                                         const TargetTransformInfo *TTI) const {
2288   auto &MF = *MI.getMF();
2289   auto &MRI = MF.getRegInfo();
2290   // Assuming a spill and reload of a value has a cost of 1 instruction each,
2291   // this helper function computes the maximum number of uses we should consider
2292   // for remat. E.g. on arm64 global addresses take 2 insts to materialize. We
2293   // break even in terms of code size when the original MI has 2 users vs
2294   // choosing to potentially spill. Any more than 2 users we we have a net code
2295   // size increase. This doesn't take into account register pressure though.
2296   auto maxUses = [](unsigned RematCost) {
2297     // A cost of 1 means remats are basically free.
2298     if (RematCost == 1)
2299       return UINT_MAX;
2300     if (RematCost == 2)
2301       return 2U;
2302 
2303     // Remat is too expensive, only sink if there's one user.
2304     if (RematCost > 2)
2305       return 1U;
2306     llvm_unreachable("Unexpected remat cost");
2307   };
2308 
2309   // Helper to walk through uses and terminate if we've reached a limit. Saves
2310   // us spending time traversing uses if all we want to know is if it's >= min.
2311   auto isUsesAtMost = [&](unsigned Reg, unsigned MaxUses) {
2312     unsigned NumUses = 0;
2313     auto UI = MRI.use_instr_nodbg_begin(Reg), UE = MRI.use_instr_nodbg_end();
2314     for (; UI != UE && NumUses < MaxUses; ++UI) {
2315       NumUses++;
2316     }
2317     // If we haven't reached the end yet then there are more than MaxUses users.
2318     return UI == UE;
2319   };
2320 
2321   switch (MI.getOpcode()) {
2322   default:
2323     return false;
2324   // Constants-like instructions should be close to their users.
2325   // We don't want long live-ranges for them.
2326   case TargetOpcode::G_CONSTANT:
2327   case TargetOpcode::G_FCONSTANT:
2328   case TargetOpcode::G_FRAME_INDEX:
2329   case TargetOpcode::G_INTTOPTR:
2330     return true;
2331   case TargetOpcode::G_GLOBAL_VALUE: {
2332     unsigned RematCost = TTI->getGISelRematGlobalCost();
2333     Register Reg = MI.getOperand(0).getReg();
2334     unsigned MaxUses = maxUses(RematCost);
2335     if (MaxUses == UINT_MAX)
2336       return true; // Remats are "free" so always localize.
2337     bool B = isUsesAtMost(Reg, MaxUses);
2338     return B;
2339   }
2340   }
2341 }
2342