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