1 //===- TargetLoweringBase.cpp - Implement the TargetLoweringBase class ----===//
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
7 //===----------------------------------------------------------------------===//
9 // This implements the TargetLoweringBase class.
11 //===----------------------------------------------------------------------===//
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/CodeGen/Analysis.h"
21 #include "llvm/CodeGen/ISDOpcodes.h"
22 #include "llvm/CodeGen/MachineBasicBlock.h"
23 #include "llvm/CodeGen/MachineFrameInfo.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/CodeGen/MachineInstr.h"
26 #include "llvm/CodeGen/MachineInstrBuilder.h"
27 #include "llvm/CodeGen/MachineMemOperand.h"
28 #include "llvm/CodeGen/MachineOperand.h"
29 #include "llvm/CodeGen/MachineRegisterInfo.h"
30 #include "llvm/CodeGen/RuntimeLibcalls.h"
31 #include "llvm/CodeGen/StackMaps.h"
32 #include "llvm/CodeGen/TargetLowering.h"
33 #include "llvm/CodeGen/TargetOpcodes.h"
34 #include "llvm/CodeGen/TargetRegisterInfo.h"
35 #include "llvm/CodeGen/ValueTypes.h"
36 #include "llvm/IR/Attributes.h"
37 #include "llvm/IR/CallingConv.h"
38 #include "llvm/IR/DataLayout.h"
39 #include "llvm/IR/DerivedTypes.h"
40 #include "llvm/IR/Function.h"
41 #include "llvm/IR/GlobalValue.h"
42 #include "llvm/IR/GlobalVariable.h"
43 #include "llvm/IR/IRBuilder.h"
44 #include "llvm/IR/Module.h"
45 #include "llvm/IR/Type.h"
46 #include "llvm/Support/BranchProbability.h"
47 #include "llvm/Support/Casting.h"
48 #include "llvm/Support/CommandLine.h"
49 #include "llvm/Support/Compiler.h"
50 #include "llvm/Support/ErrorHandling.h"
51 #include "llvm/Support/MachineValueType.h"
52 #include "llvm/Support/MathExtras.h"
53 #include "llvm/Target/TargetMachine.h"
66 static cl::opt
<bool> JumpIsExpensiveOverride(
67 "jump-is-expensive", cl::init(false),
68 cl::desc("Do not create extra branches to split comparison logic."),
71 static cl::opt
<unsigned> MinimumJumpTableEntries
72 ("min-jump-table-entries", cl::init(4), cl::Hidden
,
73 cl::desc("Set minimum number of entries to use a jump table."));
75 static cl::opt
<unsigned> MaximumJumpTableSize
76 ("max-jump-table-size", cl::init(UINT_MAX
), cl::Hidden
,
77 cl::desc("Set maximum size of jump tables."));
79 /// Minimum jump table density for normal functions.
80 static cl::opt
<unsigned>
81 JumpTableDensity("jump-table-density", cl::init(10), cl::Hidden
,
82 cl::desc("Minimum density for building a jump table in "
83 "a normal function"));
85 /// Minimum jump table density for -Os or -Oz functions.
86 static cl::opt
<unsigned> OptsizeJumpTableDensity(
87 "optsize-jump-table-density", cl::init(40), cl::Hidden
,
88 cl::desc("Minimum density for building a jump table in "
89 "an optsize function"));
91 static bool darwinHasSinCos(const Triple
&TT
) {
92 assert(TT
.isOSDarwin() && "should be called with darwin triple");
93 // Don't bother with 32 bit x86.
94 if (TT
.getArch() == Triple::x86
)
96 // Macos < 10.9 has no sincos_stret.
98 return !TT
.isMacOSXVersionLT(10, 9) && TT
.isArch64Bit();
99 // iOS < 7.0 has no sincos_stret.
101 return !TT
.isOSVersionLT(7, 0);
102 // Any other darwin such as WatchOS/TvOS is new enough.
106 // Although this default value is arbitrary, it is not random. It is assumed
107 // that a condition that evaluates the same way by a higher percentage than this
108 // is best represented as control flow. Therefore, the default value N should be
109 // set such that the win from N% correct executions is greater than the loss
110 // from (100 - N)% mispredicted executions for the majority of intended targets.
111 static cl::opt
<int> MinPercentageForPredictableBranch(
112 "min-predictable-branch", cl::init(99),
113 cl::desc("Minimum percentage (0-100) that a condition must be either true "
114 "or false to assume that the condition is predictable"),
117 void TargetLoweringBase::InitLibcalls(const Triple
&TT
) {
118 #define HANDLE_LIBCALL(code, name) \
119 setLibcallName(RTLIB::code, name);
120 #include "llvm/IR/RuntimeLibcalls.def"
121 #undef HANDLE_LIBCALL
122 // Initialize calling conventions to their default.
123 for (int LC
= 0; LC
< RTLIB::UNKNOWN_LIBCALL
; ++LC
)
124 setLibcallCallingConv((RTLIB::Libcall
)LC
, CallingConv::C
);
126 // For IEEE quad-precision libcall names, PPC uses "kf" instead of "tf".
127 if (TT
.getArch() == Triple::ppc
|| TT
.isPPC64()) {
128 setLibcallName(RTLIB::ADD_F128
, "__addkf3");
129 setLibcallName(RTLIB::SUB_F128
, "__subkf3");
130 setLibcallName(RTLIB::MUL_F128
, "__mulkf3");
131 setLibcallName(RTLIB::DIV_F128
, "__divkf3");
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::FPTOUINT_F128_I32
, "__fixunskfsi");
139 setLibcallName(RTLIB::FPTOUINT_F128_I64
, "__fixunskfdi");
140 setLibcallName(RTLIB::SINTTOFP_I32_F128
, "__floatsikf");
141 setLibcallName(RTLIB::SINTTOFP_I64_F128
, "__floatdikf");
142 setLibcallName(RTLIB::UINTTOFP_I32_F128
, "__floatunsikf");
143 setLibcallName(RTLIB::UINTTOFP_I64_F128
, "__floatundikf");
144 setLibcallName(RTLIB::OEQ_F128
, "__eqkf2");
145 setLibcallName(RTLIB::UNE_F128
, "__nekf2");
146 setLibcallName(RTLIB::OGE_F128
, "__gekf2");
147 setLibcallName(RTLIB::OLT_F128
, "__ltkf2");
148 setLibcallName(RTLIB::OLE_F128
, "__lekf2");
149 setLibcallName(RTLIB::OGT_F128
, "__gtkf2");
150 setLibcallName(RTLIB::UO_F128
, "__unordkf2");
151 setLibcallName(RTLIB::O_F128
, "__unordkf2");
154 // A few names are different on particular architectures or environments.
155 if (TT
.isOSDarwin()) {
156 // For f16/f32 conversions, Darwin uses the standard naming scheme, instead
157 // of the gnueabi-style __gnu_*_ieee.
158 // FIXME: What about other targets?
159 setLibcallName(RTLIB::FPEXT_F16_F32
, "__extendhfsf2");
160 setLibcallName(RTLIB::FPROUND_F32_F16
, "__truncsfhf2");
162 // Some darwins have an optimized __bzero/bzero function.
163 switch (TT
.getArch()) {
166 if (TT
.isMacOSX() && !TT
.isMacOSXVersionLT(10, 6))
167 setLibcallName(RTLIB::BZERO
, "__bzero");
169 case Triple::aarch64
:
170 setLibcallName(RTLIB::BZERO
, "bzero");
176 if (darwinHasSinCos(TT
)) {
177 setLibcallName(RTLIB::SINCOS_STRET_F32
, "__sincosf_stret");
178 setLibcallName(RTLIB::SINCOS_STRET_F64
, "__sincos_stret");
179 if (TT
.isWatchABI()) {
180 setLibcallCallingConv(RTLIB::SINCOS_STRET_F32
,
181 CallingConv::ARM_AAPCS_VFP
);
182 setLibcallCallingConv(RTLIB::SINCOS_STRET_F64
,
183 CallingConv::ARM_AAPCS_VFP
);
187 setLibcallName(RTLIB::FPEXT_F16_F32
, "__gnu_h2f_ieee");
188 setLibcallName(RTLIB::FPROUND_F32_F16
, "__gnu_f2h_ieee");
191 if (TT
.isGNUEnvironment() || TT
.isOSFuchsia() ||
192 (TT
.isAndroid() && !TT
.isAndroidVersionLT(9))) {
193 setLibcallName(RTLIB::SINCOS_F32
, "sincosf");
194 setLibcallName(RTLIB::SINCOS_F64
, "sincos");
195 setLibcallName(RTLIB::SINCOS_F80
, "sincosl");
196 setLibcallName(RTLIB::SINCOS_F128
, "sincosl");
197 setLibcallName(RTLIB::SINCOS_PPCF128
, "sincosl");
201 setLibcallName(RTLIB::SINCOS_F32
, "sincosf");
202 setLibcallName(RTLIB::SINCOS_F64
, "sincos");
205 if (TT
.isOSOpenBSD()) {
206 setLibcallName(RTLIB::STACKPROTECTOR_CHECK_FAIL
, nullptr);
210 /// getFPEXT - Return the FPEXT_*_* value for the given types, or
211 /// UNKNOWN_LIBCALL if there is none.
212 RTLIB::Libcall
RTLIB::getFPEXT(EVT OpVT
, EVT RetVT
) {
213 if (OpVT
== MVT::f16
) {
214 if (RetVT
== MVT::f32
)
215 return FPEXT_F16_F32
;
216 } else if (OpVT
== MVT::f32
) {
217 if (RetVT
== MVT::f64
)
218 return FPEXT_F32_F64
;
219 if (RetVT
== MVT::f128
)
220 return FPEXT_F32_F128
;
221 if (RetVT
== MVT::ppcf128
)
222 return FPEXT_F32_PPCF128
;
223 } else if (OpVT
== MVT::f64
) {
224 if (RetVT
== MVT::f128
)
225 return FPEXT_F64_F128
;
226 else if (RetVT
== MVT::ppcf128
)
227 return FPEXT_F64_PPCF128
;
228 } else if (OpVT
== MVT::f80
) {
229 if (RetVT
== MVT::f128
)
230 return FPEXT_F80_F128
;
233 return UNKNOWN_LIBCALL
;
236 /// getFPROUND - Return the FPROUND_*_* value for the given types, or
237 /// UNKNOWN_LIBCALL if there is none.
238 RTLIB::Libcall
RTLIB::getFPROUND(EVT OpVT
, EVT RetVT
) {
239 if (RetVT
== MVT::f16
) {
240 if (OpVT
== MVT::f32
)
241 return FPROUND_F32_F16
;
242 if (OpVT
== MVT::f64
)
243 return FPROUND_F64_F16
;
244 if (OpVT
== MVT::f80
)
245 return FPROUND_F80_F16
;
246 if (OpVT
== MVT::f128
)
247 return FPROUND_F128_F16
;
248 if (OpVT
== MVT::ppcf128
)
249 return FPROUND_PPCF128_F16
;
250 } else if (RetVT
== MVT::f32
) {
251 if (OpVT
== MVT::f64
)
252 return FPROUND_F64_F32
;
253 if (OpVT
== MVT::f80
)
254 return FPROUND_F80_F32
;
255 if (OpVT
== MVT::f128
)
256 return FPROUND_F128_F32
;
257 if (OpVT
== MVT::ppcf128
)
258 return FPROUND_PPCF128_F32
;
259 } else if (RetVT
== MVT::f64
) {
260 if (OpVT
== MVT::f80
)
261 return FPROUND_F80_F64
;
262 if (OpVT
== MVT::f128
)
263 return FPROUND_F128_F64
;
264 if (OpVT
== MVT::ppcf128
)
265 return FPROUND_PPCF128_F64
;
266 } else if (RetVT
== MVT::f80
) {
267 if (OpVT
== MVT::f128
)
268 return FPROUND_F128_F80
;
271 return UNKNOWN_LIBCALL
;
274 /// getFPTOSINT - Return the FPTOSINT_*_* value for the given types, or
275 /// UNKNOWN_LIBCALL if there is none.
276 RTLIB::Libcall
RTLIB::getFPTOSINT(EVT OpVT
, EVT RetVT
) {
277 if (OpVT
== MVT::f32
) {
278 if (RetVT
== MVT::i32
)
279 return FPTOSINT_F32_I32
;
280 if (RetVT
== MVT::i64
)
281 return FPTOSINT_F32_I64
;
282 if (RetVT
== MVT::i128
)
283 return FPTOSINT_F32_I128
;
284 } else if (OpVT
== MVT::f64
) {
285 if (RetVT
== MVT::i32
)
286 return FPTOSINT_F64_I32
;
287 if (RetVT
== MVT::i64
)
288 return FPTOSINT_F64_I64
;
289 if (RetVT
== MVT::i128
)
290 return FPTOSINT_F64_I128
;
291 } else if (OpVT
== MVT::f80
) {
292 if (RetVT
== MVT::i32
)
293 return FPTOSINT_F80_I32
;
294 if (RetVT
== MVT::i64
)
295 return FPTOSINT_F80_I64
;
296 if (RetVT
== MVT::i128
)
297 return FPTOSINT_F80_I128
;
298 } else if (OpVT
== MVT::f128
) {
299 if (RetVT
== MVT::i32
)
300 return FPTOSINT_F128_I32
;
301 if (RetVT
== MVT::i64
)
302 return FPTOSINT_F128_I64
;
303 if (RetVT
== MVT::i128
)
304 return FPTOSINT_F128_I128
;
305 } else if (OpVT
== MVT::ppcf128
) {
306 if (RetVT
== MVT::i32
)
307 return FPTOSINT_PPCF128_I32
;
308 if (RetVT
== MVT::i64
)
309 return FPTOSINT_PPCF128_I64
;
310 if (RetVT
== MVT::i128
)
311 return FPTOSINT_PPCF128_I128
;
313 return UNKNOWN_LIBCALL
;
316 /// getFPTOUINT - Return the FPTOUINT_*_* value for the given types, or
317 /// UNKNOWN_LIBCALL if there is none.
318 RTLIB::Libcall
RTLIB::getFPTOUINT(EVT OpVT
, EVT RetVT
) {
319 if (OpVT
== MVT::f32
) {
320 if (RetVT
== MVT::i32
)
321 return FPTOUINT_F32_I32
;
322 if (RetVT
== MVT::i64
)
323 return FPTOUINT_F32_I64
;
324 if (RetVT
== MVT::i128
)
325 return FPTOUINT_F32_I128
;
326 } else if (OpVT
== MVT::f64
) {
327 if (RetVT
== MVT::i32
)
328 return FPTOUINT_F64_I32
;
329 if (RetVT
== MVT::i64
)
330 return FPTOUINT_F64_I64
;
331 if (RetVT
== MVT::i128
)
332 return FPTOUINT_F64_I128
;
333 } else if (OpVT
== MVT::f80
) {
334 if (RetVT
== MVT::i32
)
335 return FPTOUINT_F80_I32
;
336 if (RetVT
== MVT::i64
)
337 return FPTOUINT_F80_I64
;
338 if (RetVT
== MVT::i128
)
339 return FPTOUINT_F80_I128
;
340 } else if (OpVT
== MVT::f128
) {
341 if (RetVT
== MVT::i32
)
342 return FPTOUINT_F128_I32
;
343 if (RetVT
== MVT::i64
)
344 return FPTOUINT_F128_I64
;
345 if (RetVT
== MVT::i128
)
346 return FPTOUINT_F128_I128
;
347 } else if (OpVT
== MVT::ppcf128
) {
348 if (RetVT
== MVT::i32
)
349 return FPTOUINT_PPCF128_I32
;
350 if (RetVT
== MVT::i64
)
351 return FPTOUINT_PPCF128_I64
;
352 if (RetVT
== MVT::i128
)
353 return FPTOUINT_PPCF128_I128
;
355 return UNKNOWN_LIBCALL
;
358 /// getSINTTOFP - Return the SINTTOFP_*_* value for the given types, or
359 /// UNKNOWN_LIBCALL if there is none.
360 RTLIB::Libcall
RTLIB::getSINTTOFP(EVT OpVT
, EVT RetVT
) {
361 if (OpVT
== MVT::i32
) {
362 if (RetVT
== MVT::f32
)
363 return SINTTOFP_I32_F32
;
364 if (RetVT
== MVT::f64
)
365 return SINTTOFP_I32_F64
;
366 if (RetVT
== MVT::f80
)
367 return SINTTOFP_I32_F80
;
368 if (RetVT
== MVT::f128
)
369 return SINTTOFP_I32_F128
;
370 if (RetVT
== MVT::ppcf128
)
371 return SINTTOFP_I32_PPCF128
;
372 } else if (OpVT
== MVT::i64
) {
373 if (RetVT
== MVT::f32
)
374 return SINTTOFP_I64_F32
;
375 if (RetVT
== MVT::f64
)
376 return SINTTOFP_I64_F64
;
377 if (RetVT
== MVT::f80
)
378 return SINTTOFP_I64_F80
;
379 if (RetVT
== MVT::f128
)
380 return SINTTOFP_I64_F128
;
381 if (RetVT
== MVT::ppcf128
)
382 return SINTTOFP_I64_PPCF128
;
383 } else if (OpVT
== MVT::i128
) {
384 if (RetVT
== MVT::f32
)
385 return SINTTOFP_I128_F32
;
386 if (RetVT
== MVT::f64
)
387 return SINTTOFP_I128_F64
;
388 if (RetVT
== MVT::f80
)
389 return SINTTOFP_I128_F80
;
390 if (RetVT
== MVT::f128
)
391 return SINTTOFP_I128_F128
;
392 if (RetVT
== MVT::ppcf128
)
393 return SINTTOFP_I128_PPCF128
;
395 return UNKNOWN_LIBCALL
;
398 /// getUINTTOFP - Return the UINTTOFP_*_* value for the given types, or
399 /// UNKNOWN_LIBCALL if there is none.
400 RTLIB::Libcall
RTLIB::getUINTTOFP(EVT OpVT
, EVT RetVT
) {
401 if (OpVT
== MVT::i32
) {
402 if (RetVT
== MVT::f32
)
403 return UINTTOFP_I32_F32
;
404 if (RetVT
== MVT::f64
)
405 return UINTTOFP_I32_F64
;
406 if (RetVT
== MVT::f80
)
407 return UINTTOFP_I32_F80
;
408 if (RetVT
== MVT::f128
)
409 return UINTTOFP_I32_F128
;
410 if (RetVT
== MVT::ppcf128
)
411 return UINTTOFP_I32_PPCF128
;
412 } else if (OpVT
== MVT::i64
) {
413 if (RetVT
== MVT::f32
)
414 return UINTTOFP_I64_F32
;
415 if (RetVT
== MVT::f64
)
416 return UINTTOFP_I64_F64
;
417 if (RetVT
== MVT::f80
)
418 return UINTTOFP_I64_F80
;
419 if (RetVT
== MVT::f128
)
420 return UINTTOFP_I64_F128
;
421 if (RetVT
== MVT::ppcf128
)
422 return UINTTOFP_I64_PPCF128
;
423 } else if (OpVT
== MVT::i128
) {
424 if (RetVT
== MVT::f32
)
425 return UINTTOFP_I128_F32
;
426 if (RetVT
== MVT::f64
)
427 return UINTTOFP_I128_F64
;
428 if (RetVT
== MVT::f80
)
429 return UINTTOFP_I128_F80
;
430 if (RetVT
== MVT::f128
)
431 return UINTTOFP_I128_F128
;
432 if (RetVT
== MVT::ppcf128
)
433 return UINTTOFP_I128_PPCF128
;
435 return UNKNOWN_LIBCALL
;
438 RTLIB::Libcall
RTLIB::getSYNC(unsigned Opc
, MVT VT
) {
439 #define OP_TO_LIBCALL(Name, Enum) \
441 switch (VT.SimpleTy) { \
443 return UNKNOWN_LIBCALL; \
457 OP_TO_LIBCALL(ISD::ATOMIC_SWAP
, SYNC_LOCK_TEST_AND_SET
)
458 OP_TO_LIBCALL(ISD::ATOMIC_CMP_SWAP
, SYNC_VAL_COMPARE_AND_SWAP
)
459 OP_TO_LIBCALL(ISD::ATOMIC_LOAD_ADD
, SYNC_FETCH_AND_ADD
)
460 OP_TO_LIBCALL(ISD::ATOMIC_LOAD_SUB
, SYNC_FETCH_AND_SUB
)
461 OP_TO_LIBCALL(ISD::ATOMIC_LOAD_AND
, SYNC_FETCH_AND_AND
)
462 OP_TO_LIBCALL(ISD::ATOMIC_LOAD_OR
, SYNC_FETCH_AND_OR
)
463 OP_TO_LIBCALL(ISD::ATOMIC_LOAD_XOR
, SYNC_FETCH_AND_XOR
)
464 OP_TO_LIBCALL(ISD::ATOMIC_LOAD_NAND
, SYNC_FETCH_AND_NAND
)
465 OP_TO_LIBCALL(ISD::ATOMIC_LOAD_MAX
, SYNC_FETCH_AND_MAX
)
466 OP_TO_LIBCALL(ISD::ATOMIC_LOAD_UMAX
, SYNC_FETCH_AND_UMAX
)
467 OP_TO_LIBCALL(ISD::ATOMIC_LOAD_MIN
, SYNC_FETCH_AND_MIN
)
468 OP_TO_LIBCALL(ISD::ATOMIC_LOAD_UMIN
, SYNC_FETCH_AND_UMIN
)
473 return UNKNOWN_LIBCALL
;
476 RTLIB::Libcall
RTLIB::getMEMCPY_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize
) {
477 switch (ElementSize
) {
479 return MEMCPY_ELEMENT_UNORDERED_ATOMIC_1
;
481 return MEMCPY_ELEMENT_UNORDERED_ATOMIC_2
;
483 return MEMCPY_ELEMENT_UNORDERED_ATOMIC_4
;
485 return MEMCPY_ELEMENT_UNORDERED_ATOMIC_8
;
487 return MEMCPY_ELEMENT_UNORDERED_ATOMIC_16
;
489 return UNKNOWN_LIBCALL
;
493 RTLIB::Libcall
RTLIB::getMEMMOVE_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize
) {
494 switch (ElementSize
) {
496 return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_1
;
498 return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_2
;
500 return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_4
;
502 return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_8
;
504 return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_16
;
506 return UNKNOWN_LIBCALL
;
510 RTLIB::Libcall
RTLIB::getMEMSET_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize
) {
511 switch (ElementSize
) {
513 return MEMSET_ELEMENT_UNORDERED_ATOMIC_1
;
515 return MEMSET_ELEMENT_UNORDERED_ATOMIC_2
;
517 return MEMSET_ELEMENT_UNORDERED_ATOMIC_4
;
519 return MEMSET_ELEMENT_UNORDERED_ATOMIC_8
;
521 return MEMSET_ELEMENT_UNORDERED_ATOMIC_16
;
523 return UNKNOWN_LIBCALL
;
527 /// InitCmpLibcallCCs - Set default comparison libcall CC.
528 static void InitCmpLibcallCCs(ISD::CondCode
*CCs
) {
529 memset(CCs
, ISD::SETCC_INVALID
, sizeof(ISD::CondCode
)*RTLIB::UNKNOWN_LIBCALL
);
530 CCs
[RTLIB::OEQ_F32
] = ISD::SETEQ
;
531 CCs
[RTLIB::OEQ_F64
] = ISD::SETEQ
;
532 CCs
[RTLIB::OEQ_F128
] = ISD::SETEQ
;
533 CCs
[RTLIB::OEQ_PPCF128
] = ISD::SETEQ
;
534 CCs
[RTLIB::UNE_F32
] = ISD::SETNE
;
535 CCs
[RTLIB::UNE_F64
] = ISD::SETNE
;
536 CCs
[RTLIB::UNE_F128
] = ISD::SETNE
;
537 CCs
[RTLIB::UNE_PPCF128
] = ISD::SETNE
;
538 CCs
[RTLIB::OGE_F32
] = ISD::SETGE
;
539 CCs
[RTLIB::OGE_F64
] = ISD::SETGE
;
540 CCs
[RTLIB::OGE_F128
] = ISD::SETGE
;
541 CCs
[RTLIB::OGE_PPCF128
] = ISD::SETGE
;
542 CCs
[RTLIB::OLT_F32
] = ISD::SETLT
;
543 CCs
[RTLIB::OLT_F64
] = ISD::SETLT
;
544 CCs
[RTLIB::OLT_F128
] = ISD::SETLT
;
545 CCs
[RTLIB::OLT_PPCF128
] = ISD::SETLT
;
546 CCs
[RTLIB::OLE_F32
] = ISD::SETLE
;
547 CCs
[RTLIB::OLE_F64
] = ISD::SETLE
;
548 CCs
[RTLIB::OLE_F128
] = ISD::SETLE
;
549 CCs
[RTLIB::OLE_PPCF128
] = ISD::SETLE
;
550 CCs
[RTLIB::OGT_F32
] = ISD::SETGT
;
551 CCs
[RTLIB::OGT_F64
] = ISD::SETGT
;
552 CCs
[RTLIB::OGT_F128
] = ISD::SETGT
;
553 CCs
[RTLIB::OGT_PPCF128
] = ISD::SETGT
;
554 CCs
[RTLIB::UO_F32
] = ISD::SETNE
;
555 CCs
[RTLIB::UO_F64
] = ISD::SETNE
;
556 CCs
[RTLIB::UO_F128
] = ISD::SETNE
;
557 CCs
[RTLIB::UO_PPCF128
] = ISD::SETNE
;
558 CCs
[RTLIB::O_F32
] = ISD::SETEQ
;
559 CCs
[RTLIB::O_F64
] = ISD::SETEQ
;
560 CCs
[RTLIB::O_F128
] = ISD::SETEQ
;
561 CCs
[RTLIB::O_PPCF128
] = ISD::SETEQ
;
564 /// NOTE: The TargetMachine owns TLOF.
565 TargetLoweringBase::TargetLoweringBase(const TargetMachine
&tm
) : TM(tm
) {
568 // Perform these initializations only once.
569 MaxStoresPerMemset
= MaxStoresPerMemcpy
= MaxStoresPerMemmove
=
570 MaxLoadsPerMemcmp
= 8;
571 MaxGluedStoresPerMemcpy
= 0;
572 MaxStoresPerMemsetOptSize
= MaxStoresPerMemcpyOptSize
=
573 MaxStoresPerMemmoveOptSize
= MaxLoadsPerMemcmpOptSize
= 4;
574 UseUnderscoreSetJmp
= false;
575 UseUnderscoreLongJmp
= false;
576 HasMultipleConditionRegisters
= false;
577 HasExtractBitsInsn
= false;
578 JumpIsExpensive
= JumpIsExpensiveOverride
;
579 PredictableSelectIsExpensive
= false;
580 EnableExtLdPromotion
= false;
581 StackPointerRegisterToSaveRestore
= 0;
582 BooleanContents
= UndefinedBooleanContent
;
583 BooleanFloatContents
= UndefinedBooleanContent
;
584 BooleanVectorContents
= UndefinedBooleanContent
;
585 SchedPreferenceInfo
= Sched::ILP
;
586 GatherAllAliasesMaxDepth
= 18;
587 // TODO: the default will be switched to 0 in the next commit, along
588 // with the Target-specific changes necessary.
589 MaxAtomicSizeInBitsSupported
= 1024;
591 MinCmpXchgSizeInBits
= 0;
592 SupportsUnalignedAtomics
= false;
594 std::fill(std::begin(LibcallRoutineNames
), std::end(LibcallRoutineNames
), nullptr);
596 InitLibcalls(TM
.getTargetTriple());
597 InitCmpLibcallCCs(CmpLibcallCCs
);
600 void TargetLoweringBase::initActions() {
601 // All operations default to being supported.
602 memset(OpActions
, 0, sizeof(OpActions
));
603 memset(LoadExtActions
, 0, sizeof(LoadExtActions
));
604 memset(TruncStoreActions
, 0, sizeof(TruncStoreActions
));
605 memset(IndexedModeActions
, 0, sizeof(IndexedModeActions
));
606 memset(CondCodeActions
, 0, sizeof(CondCodeActions
));
607 std::fill(std::begin(RegClassForVT
), std::end(RegClassForVT
), nullptr);
608 std::fill(std::begin(TargetDAGCombineArray
),
609 std::end(TargetDAGCombineArray
), 0);
611 for (MVT VT
: MVT::fp_valuetypes()) {
612 MVT IntVT
= MVT::getIntegerVT(VT
.getSizeInBits());
613 if (IntVT
.isValid()) {
614 setOperationAction(ISD::ATOMIC_SWAP
, VT
, Promote
);
615 AddPromotedToType(ISD::ATOMIC_SWAP
, VT
, IntVT
);
619 // Set default actions for various operations.
620 for (MVT VT
: MVT::all_valuetypes()) {
621 // Default all indexed load / store to expand.
622 for (unsigned IM
= (unsigned)ISD::PRE_INC
;
623 IM
!= (unsigned)ISD::LAST_INDEXED_MODE
; ++IM
) {
624 setIndexedLoadAction(IM
, VT
, Expand
);
625 setIndexedStoreAction(IM
, VT
, Expand
);
628 // Most backends expect to see the node which just returns the value loaded.
629 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS
, VT
, Expand
);
631 // These operations default to expand.
632 setOperationAction(ISD::FGETSIGN
, VT
, Expand
);
633 setOperationAction(ISD::CONCAT_VECTORS
, VT
, Expand
);
634 setOperationAction(ISD::FMINNUM
, VT
, Expand
);
635 setOperationAction(ISD::FMAXNUM
, VT
, Expand
);
636 setOperationAction(ISD::FMINNUM_IEEE
, VT
, Expand
);
637 setOperationAction(ISD::FMAXNUM_IEEE
, VT
, Expand
);
638 setOperationAction(ISD::FMINIMUM
, VT
, Expand
);
639 setOperationAction(ISD::FMAXIMUM
, VT
, Expand
);
640 setOperationAction(ISD::FMAD
, VT
, Expand
);
641 setOperationAction(ISD::SMIN
, VT
, Expand
);
642 setOperationAction(ISD::SMAX
, VT
, Expand
);
643 setOperationAction(ISD::UMIN
, VT
, Expand
);
644 setOperationAction(ISD::UMAX
, VT
, Expand
);
645 setOperationAction(ISD::ABS
, VT
, Expand
);
646 setOperationAction(ISD::FSHL
, VT
, Expand
);
647 setOperationAction(ISD::FSHR
, VT
, Expand
);
648 setOperationAction(ISD::SADDSAT
, VT
, Expand
);
649 setOperationAction(ISD::UADDSAT
, VT
, Expand
);
650 setOperationAction(ISD::SSUBSAT
, VT
, Expand
);
651 setOperationAction(ISD::USUBSAT
, VT
, Expand
);
652 setOperationAction(ISD::SMULFIX
, VT
, Expand
);
653 setOperationAction(ISD::SMULFIXSAT
, VT
, Expand
);
654 setOperationAction(ISD::UMULFIX
, VT
, Expand
);
656 // Overflow operations default to expand
657 setOperationAction(ISD::SADDO
, VT
, Expand
);
658 setOperationAction(ISD::SSUBO
, VT
, Expand
);
659 setOperationAction(ISD::UADDO
, VT
, Expand
);
660 setOperationAction(ISD::USUBO
, VT
, Expand
);
661 setOperationAction(ISD::SMULO
, VT
, Expand
);
662 setOperationAction(ISD::UMULO
, VT
, Expand
);
664 // ADDCARRY operations default to expand
665 setOperationAction(ISD::ADDCARRY
, VT
, Expand
);
666 setOperationAction(ISD::SUBCARRY
, VT
, Expand
);
667 setOperationAction(ISD::SETCCCARRY
, VT
, Expand
);
669 // ADDC/ADDE/SUBC/SUBE default to expand.
670 setOperationAction(ISD::ADDC
, VT
, Expand
);
671 setOperationAction(ISD::ADDE
, VT
, Expand
);
672 setOperationAction(ISD::SUBC
, VT
, Expand
);
673 setOperationAction(ISD::SUBE
, VT
, Expand
);
675 // These default to Expand so they will be expanded to CTLZ/CTTZ by default.
676 setOperationAction(ISD::CTLZ_ZERO_UNDEF
, VT
, Expand
);
677 setOperationAction(ISD::CTTZ_ZERO_UNDEF
, VT
, Expand
);
679 setOperationAction(ISD::BITREVERSE
, VT
, Expand
);
681 // These library functions default to expand.
682 setOperationAction(ISD::FROUND
, VT
, Expand
);
683 setOperationAction(ISD::FPOWI
, VT
, Expand
);
685 // These operations default to expand for vector types.
687 setOperationAction(ISD::FCOPYSIGN
, VT
, Expand
);
688 setOperationAction(ISD::ANY_EXTEND_VECTOR_INREG
, VT
, Expand
);
689 setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG
, VT
, Expand
);
690 setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG
, VT
, Expand
);
693 // Constrained floating-point operations default to expand.
694 setOperationAction(ISD::STRICT_FADD
, VT
, Expand
);
695 setOperationAction(ISD::STRICT_FSUB
, VT
, Expand
);
696 setOperationAction(ISD::STRICT_FMUL
, VT
, Expand
);
697 setOperationAction(ISD::STRICT_FDIV
, VT
, Expand
);
698 setOperationAction(ISD::STRICT_FREM
, VT
, Expand
);
699 setOperationAction(ISD::STRICT_FMA
, VT
, Expand
);
700 setOperationAction(ISD::STRICT_FSQRT
, VT
, Expand
);
701 setOperationAction(ISD::STRICT_FPOW
, VT
, Expand
);
702 setOperationAction(ISD::STRICT_FPOWI
, VT
, Expand
);
703 setOperationAction(ISD::STRICT_FSIN
, VT
, Expand
);
704 setOperationAction(ISD::STRICT_FCOS
, VT
, Expand
);
705 setOperationAction(ISD::STRICT_FEXP
, VT
, Expand
);
706 setOperationAction(ISD::STRICT_FEXP2
, VT
, Expand
);
707 setOperationAction(ISD::STRICT_FLOG
, VT
, Expand
);
708 setOperationAction(ISD::STRICT_FLOG10
, VT
, Expand
);
709 setOperationAction(ISD::STRICT_FLOG2
, VT
, Expand
);
710 setOperationAction(ISD::STRICT_FRINT
, VT
, Expand
);
711 setOperationAction(ISD::STRICT_FNEARBYINT
, VT
, Expand
);
712 setOperationAction(ISD::STRICT_FCEIL
, VT
, Expand
);
713 setOperationAction(ISD::STRICT_FFLOOR
, VT
, Expand
);
714 setOperationAction(ISD::STRICT_FROUND
, VT
, Expand
);
715 setOperationAction(ISD::STRICT_FTRUNC
, VT
, Expand
);
716 setOperationAction(ISD::STRICT_FMAXNUM
, VT
, Expand
);
717 setOperationAction(ISD::STRICT_FMINNUM
, VT
, Expand
);
718 setOperationAction(ISD::STRICT_FP_ROUND
, VT
, Expand
);
719 setOperationAction(ISD::STRICT_FP_EXTEND
, VT
, Expand
);
720 setOperationAction(ISD::STRICT_FP_TO_SINT
, VT
, Expand
);
721 setOperationAction(ISD::STRICT_FP_TO_UINT
, VT
, Expand
);
723 // For most targets @llvm.get.dynamic.area.offset just returns 0.
724 setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET
, VT
, Expand
);
726 // Vector reduction default to expand.
727 setOperationAction(ISD::VECREDUCE_FADD
, VT
, Expand
);
728 setOperationAction(ISD::VECREDUCE_FMUL
, VT
, Expand
);
729 setOperationAction(ISD::VECREDUCE_ADD
, VT
, Expand
);
730 setOperationAction(ISD::VECREDUCE_MUL
, VT
, Expand
);
731 setOperationAction(ISD::VECREDUCE_AND
, VT
, Expand
);
732 setOperationAction(ISD::VECREDUCE_OR
, VT
, Expand
);
733 setOperationAction(ISD::VECREDUCE_XOR
, VT
, Expand
);
734 setOperationAction(ISD::VECREDUCE_SMAX
, VT
, Expand
);
735 setOperationAction(ISD::VECREDUCE_SMIN
, VT
, Expand
);
736 setOperationAction(ISD::VECREDUCE_UMAX
, VT
, Expand
);
737 setOperationAction(ISD::VECREDUCE_UMIN
, VT
, Expand
);
738 setOperationAction(ISD::VECREDUCE_FMAX
, VT
, Expand
);
739 setOperationAction(ISD::VECREDUCE_FMIN
, VT
, Expand
);
742 // Most targets ignore the @llvm.prefetch intrinsic.
743 setOperationAction(ISD::PREFETCH
, MVT::Other
, Expand
);
745 // Most targets also ignore the @llvm.readcyclecounter intrinsic.
746 setOperationAction(ISD::READCYCLECOUNTER
, MVT::i64
, Expand
);
748 // ConstantFP nodes default to expand. Targets can either change this to
749 // Legal, in which case all fp constants are legal, or use isFPImmLegal()
750 // to optimize expansions for certain constants.
751 setOperationAction(ISD::ConstantFP
, MVT::f16
, Expand
);
752 setOperationAction(ISD::ConstantFP
, MVT::f32
, Expand
);
753 setOperationAction(ISD::ConstantFP
, MVT::f64
, Expand
);
754 setOperationAction(ISD::ConstantFP
, MVT::f80
, Expand
);
755 setOperationAction(ISD::ConstantFP
, MVT::f128
, Expand
);
757 // These library functions default to expand.
758 for (MVT VT
: {MVT::f32
, MVT::f64
, MVT::f128
}) {
759 setOperationAction(ISD::FCBRT
, VT
, Expand
);
760 setOperationAction(ISD::FLOG
, VT
, Expand
);
761 setOperationAction(ISD::FLOG2
, VT
, Expand
);
762 setOperationAction(ISD::FLOG10
, VT
, Expand
);
763 setOperationAction(ISD::FEXP
, VT
, Expand
);
764 setOperationAction(ISD::FEXP2
, VT
, Expand
);
765 setOperationAction(ISD::FFLOOR
, VT
, Expand
);
766 setOperationAction(ISD::FNEARBYINT
, VT
, Expand
);
767 setOperationAction(ISD::FCEIL
, VT
, Expand
);
768 setOperationAction(ISD::FRINT
, VT
, Expand
);
769 setOperationAction(ISD::FTRUNC
, VT
, Expand
);
770 setOperationAction(ISD::FROUND
, VT
, Expand
);
771 setOperationAction(ISD::LROUND
, VT
, Expand
);
772 setOperationAction(ISD::LLROUND
, VT
, Expand
);
773 setOperationAction(ISD::LRINT
, VT
, Expand
);
774 setOperationAction(ISD::LLRINT
, VT
, Expand
);
777 // Default ISD::TRAP to expand (which turns it into abort).
778 setOperationAction(ISD::TRAP
, MVT::Other
, Expand
);
780 // On most systems, DEBUGTRAP and TRAP have no difference. The "Expand"
781 // here is to inform DAG Legalizer to replace DEBUGTRAP with TRAP.
782 setOperationAction(ISD::DEBUGTRAP
, MVT::Other
, Expand
);
785 MVT
TargetLoweringBase::getScalarShiftAmountTy(const DataLayout
&DL
,
787 return MVT::getIntegerVT(DL
.getPointerSizeInBits(0));
790 EVT
TargetLoweringBase::getShiftAmountTy(EVT LHSTy
, const DataLayout
&DL
,
791 bool LegalTypes
) const {
792 assert(LHSTy
.isInteger() && "Shift amount is not an integer type!");
793 if (LHSTy
.isVector())
795 return LegalTypes
? getScalarShiftAmountTy(DL
, LHSTy
)
799 bool TargetLoweringBase::canOpTrap(unsigned Op
, EVT VT
) const {
800 assert(isTypeLegal(VT
));
812 void TargetLoweringBase::setJumpIsExpensive(bool isExpensive
) {
813 // If the command-line option was specified, ignore this request.
814 if (!JumpIsExpensiveOverride
.getNumOccurrences())
815 JumpIsExpensive
= isExpensive
;
818 TargetLoweringBase::LegalizeKind
819 TargetLoweringBase::getTypeConversion(LLVMContext
&Context
, EVT VT
) const {
820 // If this is a simple type, use the ComputeRegisterProp mechanism.
822 MVT SVT
= VT
.getSimpleVT();
823 assert((unsigned)SVT
.SimpleTy
< array_lengthof(TransformToType
));
824 MVT NVT
= TransformToType
[SVT
.SimpleTy
];
825 LegalizeTypeAction LA
= ValueTypeActions
.getTypeAction(SVT
);
827 assert((LA
== TypeLegal
|| LA
== TypeSoftenFloat
||
829 ValueTypeActions
.getTypeAction(NVT
) != TypePromoteInteger
)) &&
830 "Promote may not follow Expand or Promote");
832 if (LA
== TypeSplitVector
)
833 return LegalizeKind(LA
,
834 EVT::getVectorVT(Context
, SVT
.getVectorElementType(),
835 SVT
.getVectorNumElements() / 2));
836 if (LA
== TypeScalarizeVector
)
837 return LegalizeKind(LA
, SVT
.getVectorElementType());
838 return LegalizeKind(LA
, NVT
);
841 // Handle Extended Scalar Types.
842 if (!VT
.isVector()) {
843 assert(VT
.isInteger() && "Float types must be simple");
844 unsigned BitSize
= VT
.getSizeInBits();
845 // First promote to a power-of-two size, then expand if necessary.
846 if (BitSize
< 8 || !isPowerOf2_32(BitSize
)) {
847 EVT NVT
= VT
.getRoundIntegerType(Context
);
848 assert(NVT
!= VT
&& "Unable to round integer VT");
849 LegalizeKind NextStep
= getTypeConversion(Context
, NVT
);
850 // Avoid multi-step promotion.
851 if (NextStep
.first
== TypePromoteInteger
)
853 // Return rounded integer type.
854 return LegalizeKind(TypePromoteInteger
, NVT
);
857 return LegalizeKind(TypeExpandInteger
,
858 EVT::getIntegerVT(Context
, VT
.getSizeInBits() / 2));
861 // Handle vector types.
862 unsigned NumElts
= VT
.getVectorNumElements();
863 EVT EltVT
= VT
.getVectorElementType();
865 // Vectors with only one element are always scalarized.
867 return LegalizeKind(TypeScalarizeVector
, EltVT
);
869 // Try to widen vector elements until the element type is a power of two and
870 // promote it to a legal type later on, for example:
871 // <3 x i8> -> <4 x i8> -> <4 x i32>
872 if (EltVT
.isInteger()) {
873 // Vectors with a number of elements that is not a power of two are always
874 // widened, for example <3 x i8> -> <4 x i8>.
875 if (!VT
.isPow2VectorType()) {
876 NumElts
= (unsigned)NextPowerOf2(NumElts
);
877 EVT NVT
= EVT::getVectorVT(Context
, EltVT
, NumElts
);
878 return LegalizeKind(TypeWidenVector
, NVT
);
881 // Examine the element type.
882 LegalizeKind LK
= getTypeConversion(Context
, EltVT
);
884 // If type is to be expanded, split the vector.
885 // <4 x i140> -> <2 x i140>
886 if (LK
.first
== TypeExpandInteger
)
887 return LegalizeKind(TypeSplitVector
,
888 EVT::getVectorVT(Context
, EltVT
, NumElts
/ 2));
890 // Promote the integer element types until a legal vector type is found
891 // or until the element integer type is too big. If a legal type was not
892 // found, fallback to the usual mechanism of widening/splitting the
894 EVT OldEltVT
= EltVT
;
896 // Increase the bitwidth of the element to the next pow-of-two
897 // (which is greater than 8 bits).
898 EltVT
= EVT::getIntegerVT(Context
, 1 + EltVT
.getSizeInBits())
899 .getRoundIntegerType(Context
);
901 // Stop trying when getting a non-simple element type.
902 // Note that vector elements may be greater than legal vector element
903 // types. Example: X86 XMM registers hold 64bit element on 32bit
905 if (!EltVT
.isSimple())
908 // Build a new vector type and check if it is legal.
909 MVT NVT
= MVT::getVectorVT(EltVT
.getSimpleVT(), NumElts
);
910 // Found a legal promoted vector type.
911 if (NVT
!= MVT() && ValueTypeActions
.getTypeAction(NVT
) == TypeLegal
)
912 return LegalizeKind(TypePromoteInteger
,
913 EVT::getVectorVT(Context
, EltVT
, NumElts
));
916 // Reset the type to the unexpanded type if we did not find a legal vector
917 // type with a promoted vector element type.
921 // Try to widen the vector until a legal type is found.
922 // If there is no wider legal type, split the vector.
924 // Round up to the next power of 2.
925 NumElts
= (unsigned)NextPowerOf2(NumElts
);
927 // If there is no simple vector type with this many elements then there
928 // cannot be a larger legal vector type. Note that this assumes that
929 // there are no skipped intermediate vector types in the simple types.
930 if (!EltVT
.isSimple())
932 MVT LargerVector
= MVT::getVectorVT(EltVT
.getSimpleVT(), NumElts
);
933 if (LargerVector
== MVT())
936 // If this type is legal then widen the vector.
937 if (ValueTypeActions
.getTypeAction(LargerVector
) == TypeLegal
)
938 return LegalizeKind(TypeWidenVector
, LargerVector
);
941 // Widen odd vectors to next power of two.
942 if (!VT
.isPow2VectorType()) {
943 EVT NVT
= VT
.getPow2VectorType(Context
);
944 return LegalizeKind(TypeWidenVector
, NVT
);
947 // Vectors with illegal element types are expanded.
948 EVT NVT
= EVT::getVectorVT(Context
, EltVT
, VT
.getVectorNumElements() / 2);
949 return LegalizeKind(TypeSplitVector
, NVT
);
952 static unsigned getVectorTypeBreakdownMVT(MVT VT
, MVT
&IntermediateVT
,
953 unsigned &NumIntermediates
,
955 TargetLoweringBase
*TLI
) {
956 // Figure out the right, legal destination reg to copy into.
957 unsigned NumElts
= VT
.getVectorNumElements();
958 MVT EltTy
= VT
.getVectorElementType();
960 unsigned NumVectorRegs
= 1;
962 // FIXME: We don't support non-power-of-2-sized vectors for now. Ideally we
963 // could break down into LHS/RHS like LegalizeDAG does.
964 if (!isPowerOf2_32(NumElts
)) {
965 NumVectorRegs
= NumElts
;
969 // Divide the input until we get to a supported size. This will always
970 // end with a scalar if the target doesn't support vectors.
971 while (NumElts
> 1 && !TLI
->isTypeLegal(MVT::getVectorVT(EltTy
, NumElts
))) {
976 NumIntermediates
= NumVectorRegs
;
978 MVT NewVT
= MVT::getVectorVT(EltTy
, NumElts
);
979 if (!TLI
->isTypeLegal(NewVT
))
981 IntermediateVT
= NewVT
;
983 unsigned NewVTSize
= NewVT
.getSizeInBits();
985 // Convert sizes such as i33 to i64.
986 if (!isPowerOf2_32(NewVTSize
))
987 NewVTSize
= NextPowerOf2(NewVTSize
);
989 MVT DestVT
= TLI
->getRegisterType(NewVT
);
991 if (EVT(DestVT
).bitsLT(NewVT
)) // Value is expanded, e.g. i64 -> i16.
992 return NumVectorRegs
*(NewVTSize
/DestVT
.getSizeInBits());
994 // Otherwise, promotion or legal types use the same number of registers as
995 // the vector decimated to the appropriate level.
996 return NumVectorRegs
;
999 /// isLegalRC - Return true if the value types that can be represented by the
1000 /// specified register class are all legal.
1001 bool TargetLoweringBase::isLegalRC(const TargetRegisterInfo
&TRI
,
1002 const TargetRegisterClass
&RC
) const {
1003 for (auto I
= TRI
.legalclasstypes_begin(RC
); *I
!= MVT::Other
; ++I
)
1004 if (isTypeLegal(*I
))
1009 /// Replace/modify any TargetFrameIndex operands with a targte-dependent
1010 /// sequence of memory operands that is recognized by PrologEpilogInserter.
1012 TargetLoweringBase::emitPatchPoint(MachineInstr
&InitialMI
,
1013 MachineBasicBlock
*MBB
) const {
1014 MachineInstr
*MI
= &InitialMI
;
1015 MachineFunction
&MF
= *MI
->getMF();
1016 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
1018 // We're handling multiple types of operands here:
1019 // PATCHPOINT MetaArgs - live-in, read only, direct
1020 // STATEPOINT Deopt Spill - live-through, read only, indirect
1021 // STATEPOINT Deopt Alloca - live-through, read only, direct
1022 // (We're currently conservative and mark the deopt slots read/write in
1024 // STATEPOINT GC Spill - live-through, read/write, indirect
1025 // STATEPOINT GC Alloca - live-through, read/write, direct
1026 // The live-in vs live-through is handled already (the live through ones are
1027 // all stack slots), but we need to handle the different type of stackmap
1028 // operands and memory effects here.
1030 // MI changes inside this loop as we grow operands.
1031 for(unsigned OperIdx
= 0; OperIdx
!= MI
->getNumOperands(); ++OperIdx
) {
1032 MachineOperand
&MO
= MI
->getOperand(OperIdx
);
1036 // foldMemoryOperand builds a new MI after replacing a single FI operand
1037 // with the canonical set of five x86 addressing-mode operands.
1038 int FI
= MO
.getIndex();
1039 MachineInstrBuilder MIB
= BuildMI(MF
, MI
->getDebugLoc(), MI
->getDesc());
1041 // Copy operands before the frame-index.
1042 for (unsigned i
= 0; i
< OperIdx
; ++i
)
1043 MIB
.add(MI
->getOperand(i
));
1044 // Add frame index operands recognized by stackmaps.cpp
1045 if (MFI
.isStatepointSpillSlotObjectIndex(FI
)) {
1046 // indirect-mem-ref tag, size, #FI, offset.
1047 // Used for spills inserted by StatepointLowering. This codepath is not
1048 // used for patchpoints/stackmaps at all, for these spilling is done via
1049 // foldMemoryOperand callback only.
1050 assert(MI
->getOpcode() == TargetOpcode::STATEPOINT
&& "sanity");
1051 MIB
.addImm(StackMaps::IndirectMemRefOp
);
1052 MIB
.addImm(MFI
.getObjectSize(FI
));
1053 MIB
.add(MI
->getOperand(OperIdx
));
1056 // direct-mem-ref tag, #FI, offset.
1057 // Used by patchpoint, and direct alloca arguments to statepoints
1058 MIB
.addImm(StackMaps::DirectMemRefOp
);
1059 MIB
.add(MI
->getOperand(OperIdx
));
1062 // Copy the operands after the frame index.
1063 for (unsigned i
= OperIdx
+ 1; i
!= MI
->getNumOperands(); ++i
)
1064 MIB
.add(MI
->getOperand(i
));
1066 // Inherit previous memory operands.
1067 MIB
.cloneMemRefs(*MI
);
1068 assert(MIB
->mayLoad() && "Folded a stackmap use to a non-load!");
1070 // Add a new memory operand for this FI.
1071 assert(MFI
.getObjectOffset(FI
) != -1);
1073 // Note: STATEPOINT MMOs are added during SelectionDAG. STACKMAP, and
1074 // PATCHPOINT should be updated to do the same. (TODO)
1075 if (MI
->getOpcode() != TargetOpcode::STATEPOINT
) {
1076 auto Flags
= MachineMemOperand::MOLoad
;
1077 MachineMemOperand
*MMO
= MF
.getMachineMemOperand(
1078 MachinePointerInfo::getFixedStack(MF
, FI
), Flags
,
1079 MF
.getDataLayout().getPointerSize(), MFI
.getObjectAlignment(FI
));
1080 MIB
->addMemOperand(MF
, MMO
);
1083 // Replace the instruction and update the operand index.
1084 MBB
->insert(MachineBasicBlock::iterator(MI
), MIB
);
1085 OperIdx
+= (MIB
->getNumOperands() - MI
->getNumOperands()) - 1;
1086 MI
->eraseFromParent();
1093 TargetLoweringBase::emitXRayCustomEvent(MachineInstr
&MI
,
1094 MachineBasicBlock
*MBB
) const {
1095 assert(MI
.getOpcode() == TargetOpcode::PATCHABLE_EVENT_CALL
&&
1096 "Called emitXRayCustomEvent on the wrong MI!");
1097 auto &MF
= *MI
.getMF();
1098 auto MIB
= BuildMI(MF
, MI
.getDebugLoc(), MI
.getDesc());
1099 for (unsigned OpIdx
= 0; OpIdx
!= MI
.getNumOperands(); ++OpIdx
)
1100 MIB
.add(MI
.getOperand(OpIdx
));
1102 MBB
->insert(MachineBasicBlock::iterator(MI
), MIB
);
1103 MI
.eraseFromParent();
1108 TargetLoweringBase::emitXRayTypedEvent(MachineInstr
&MI
,
1109 MachineBasicBlock
*MBB
) const {
1110 assert(MI
.getOpcode() == TargetOpcode::PATCHABLE_TYPED_EVENT_CALL
&&
1111 "Called emitXRayTypedEvent on the wrong MI!");
1112 auto &MF
= *MI
.getMF();
1113 auto MIB
= BuildMI(MF
, MI
.getDebugLoc(), MI
.getDesc());
1114 for (unsigned OpIdx
= 0; OpIdx
!= MI
.getNumOperands(); ++OpIdx
)
1115 MIB
.add(MI
.getOperand(OpIdx
));
1117 MBB
->insert(MachineBasicBlock::iterator(MI
), MIB
);
1118 MI
.eraseFromParent();
1122 /// findRepresentativeClass - Return the largest legal super-reg register class
1123 /// of the register class for the specified type and its associated "cost".
1124 // This function is in TargetLowering because it uses RegClassForVT which would
1125 // need to be moved to TargetRegisterInfo and would necessitate moving
1126 // isTypeLegal over as well - a massive change that would just require
1127 // TargetLowering having a TargetRegisterInfo class member that it would use.
1128 std::pair
<const TargetRegisterClass
*, uint8_t>
1129 TargetLoweringBase::findRepresentativeClass(const TargetRegisterInfo
*TRI
,
1131 const TargetRegisterClass
*RC
= RegClassForVT
[VT
.SimpleTy
];
1133 return std::make_pair(RC
, 0);
1135 // Compute the set of all super-register classes.
1136 BitVector
SuperRegRC(TRI
->getNumRegClasses());
1137 for (SuperRegClassIterator
RCI(RC
, TRI
); RCI
.isValid(); ++RCI
)
1138 SuperRegRC
.setBitsInMask(RCI
.getMask());
1140 // Find the first legal register class with the largest spill size.
1141 const TargetRegisterClass
*BestRC
= RC
;
1142 for (unsigned i
: SuperRegRC
.set_bits()) {
1143 const TargetRegisterClass
*SuperRC
= TRI
->getRegClass(i
);
1144 // We want the largest possible spill size.
1145 if (TRI
->getSpillSize(*SuperRC
) <= TRI
->getSpillSize(*BestRC
))
1147 if (!isLegalRC(*TRI
, *SuperRC
))
1151 return std::make_pair(BestRC
, 1);
1154 /// computeRegisterProperties - Once all of the register classes are added,
1155 /// this allows us to compute derived properties we expose.
1156 void TargetLoweringBase::computeRegisterProperties(
1157 const TargetRegisterInfo
*TRI
) {
1158 static_assert(MVT::LAST_VALUETYPE
<= MVT::MAX_ALLOWED_VALUETYPE
,
1159 "Too many value types for ValueTypeActions to hold!");
1161 // Everything defaults to needing one register.
1162 for (unsigned i
= 0; i
!= MVT::LAST_VALUETYPE
; ++i
) {
1163 NumRegistersForVT
[i
] = 1;
1164 RegisterTypeForVT
[i
] = TransformToType
[i
] = (MVT::SimpleValueType
)i
;
1166 // ...except isVoid, which doesn't need any registers.
1167 NumRegistersForVT
[MVT::isVoid
] = 0;
1169 // Find the largest integer register class.
1170 unsigned LargestIntReg
= MVT::LAST_INTEGER_VALUETYPE
;
1171 for (; RegClassForVT
[LargestIntReg
] == nullptr; --LargestIntReg
)
1172 assert(LargestIntReg
!= MVT::i1
&& "No integer registers defined!");
1174 // Every integer value type larger than this largest register takes twice as
1175 // many registers to represent as the previous ValueType.
1176 for (unsigned ExpandedReg
= LargestIntReg
+ 1;
1177 ExpandedReg
<= MVT::LAST_INTEGER_VALUETYPE
; ++ExpandedReg
) {
1178 NumRegistersForVT
[ExpandedReg
] = 2*NumRegistersForVT
[ExpandedReg
-1];
1179 RegisterTypeForVT
[ExpandedReg
] = (MVT::SimpleValueType
)LargestIntReg
;
1180 TransformToType
[ExpandedReg
] = (MVT::SimpleValueType
)(ExpandedReg
- 1);
1181 ValueTypeActions
.setTypeAction((MVT::SimpleValueType
)ExpandedReg
,
1185 // Inspect all of the ValueType's smaller than the largest integer
1186 // register to see which ones need promotion.
1187 unsigned LegalIntReg
= LargestIntReg
;
1188 for (unsigned IntReg
= LargestIntReg
- 1;
1189 IntReg
>= (unsigned)MVT::i1
; --IntReg
) {
1190 MVT IVT
= (MVT::SimpleValueType
)IntReg
;
1191 if (isTypeLegal(IVT
)) {
1192 LegalIntReg
= IntReg
;
1194 RegisterTypeForVT
[IntReg
] = TransformToType
[IntReg
] =
1195 (MVT::SimpleValueType
)LegalIntReg
;
1196 ValueTypeActions
.setTypeAction(IVT
, TypePromoteInteger
);
1200 // ppcf128 type is really two f64's.
1201 if (!isTypeLegal(MVT::ppcf128
)) {
1202 if (isTypeLegal(MVT::f64
)) {
1203 NumRegistersForVT
[MVT::ppcf128
] = 2*NumRegistersForVT
[MVT::f64
];
1204 RegisterTypeForVT
[MVT::ppcf128
] = MVT::f64
;
1205 TransformToType
[MVT::ppcf128
] = MVT::f64
;
1206 ValueTypeActions
.setTypeAction(MVT::ppcf128
, TypeExpandFloat
);
1208 NumRegistersForVT
[MVT::ppcf128
] = NumRegistersForVT
[MVT::i128
];
1209 RegisterTypeForVT
[MVT::ppcf128
] = RegisterTypeForVT
[MVT::i128
];
1210 TransformToType
[MVT::ppcf128
] = MVT::i128
;
1211 ValueTypeActions
.setTypeAction(MVT::ppcf128
, TypeSoftenFloat
);
1215 // Decide how to handle f128. If the target does not have native f128 support,
1216 // expand it to i128 and we will be generating soft float library calls.
1217 if (!isTypeLegal(MVT::f128
)) {
1218 NumRegistersForVT
[MVT::f128
] = NumRegistersForVT
[MVT::i128
];
1219 RegisterTypeForVT
[MVT::f128
] = RegisterTypeForVT
[MVT::i128
];
1220 TransformToType
[MVT::f128
] = MVT::i128
;
1221 ValueTypeActions
.setTypeAction(MVT::f128
, TypeSoftenFloat
);
1224 // Decide how to handle f64. If the target does not have native f64 support,
1225 // expand it to i64 and we will be generating soft float library calls.
1226 if (!isTypeLegal(MVT::f64
)) {
1227 NumRegistersForVT
[MVT::f64
] = NumRegistersForVT
[MVT::i64
];
1228 RegisterTypeForVT
[MVT::f64
] = RegisterTypeForVT
[MVT::i64
];
1229 TransformToType
[MVT::f64
] = MVT::i64
;
1230 ValueTypeActions
.setTypeAction(MVT::f64
, TypeSoftenFloat
);
1233 // Decide how to handle f32. If the target does not have native f32 support,
1234 // expand it to i32 and we will be generating soft float library calls.
1235 if (!isTypeLegal(MVT::f32
)) {
1236 NumRegistersForVT
[MVT::f32
] = NumRegistersForVT
[MVT::i32
];
1237 RegisterTypeForVT
[MVT::f32
] = RegisterTypeForVT
[MVT::i32
];
1238 TransformToType
[MVT::f32
] = MVT::i32
;
1239 ValueTypeActions
.setTypeAction(MVT::f32
, TypeSoftenFloat
);
1242 // Decide how to handle f16. If the target does not have native f16 support,
1243 // promote it to f32, because there are no f16 library calls (except for
1245 if (!isTypeLegal(MVT::f16
)) {
1246 NumRegistersForVT
[MVT::f16
] = NumRegistersForVT
[MVT::f32
];
1247 RegisterTypeForVT
[MVT::f16
] = RegisterTypeForVT
[MVT::f32
];
1248 TransformToType
[MVT::f16
] = MVT::f32
;
1249 ValueTypeActions
.setTypeAction(MVT::f16
, TypePromoteFloat
);
1252 // Loop over all of the vector value types to see which need transformations.
1253 for (unsigned i
= MVT::FIRST_VECTOR_VALUETYPE
;
1254 i
<= (unsigned)MVT::LAST_VECTOR_VALUETYPE
; ++i
) {
1255 MVT VT
= (MVT::SimpleValueType
) i
;
1256 if (isTypeLegal(VT
))
1259 MVT EltVT
= VT
.getVectorElementType();
1260 unsigned NElts
= VT
.getVectorNumElements();
1261 bool IsLegalWiderType
= false;
1262 LegalizeTypeAction PreferredAction
= getPreferredVectorAction(VT
);
1263 switch (PreferredAction
) {
1264 case TypePromoteInteger
:
1265 // Try to promote the elements of integer vectors. If no legal
1266 // promotion was found, fall through to the widen-vector method.
1267 for (unsigned nVT
= i
+ 1; nVT
<= MVT::LAST_INTEGER_VECTOR_VALUETYPE
; ++nVT
) {
1268 MVT SVT
= (MVT::SimpleValueType
) nVT
;
1269 // Promote vectors of integers to vectors with the same number
1270 // of elements, with a wider element type.
1271 if (SVT
.getScalarSizeInBits() > EltVT
.getSizeInBits() &&
1272 SVT
.getVectorNumElements() == NElts
&& isTypeLegal(SVT
)) {
1273 TransformToType
[i
] = SVT
;
1274 RegisterTypeForVT
[i
] = SVT
;
1275 NumRegistersForVT
[i
] = 1;
1276 ValueTypeActions
.setTypeAction(VT
, TypePromoteInteger
);
1277 IsLegalWiderType
= true;
1281 if (IsLegalWiderType
)
1285 case TypeWidenVector
:
1286 if (isPowerOf2_32(NElts
)) {
1287 // Try to widen the vector.
1288 for (unsigned nVT
= i
+ 1; nVT
<= MVT::LAST_VECTOR_VALUETYPE
; ++nVT
) {
1289 MVT SVT
= (MVT::SimpleValueType
) nVT
;
1290 if (SVT
.getVectorElementType() == EltVT
1291 && SVT
.getVectorNumElements() > NElts
&& isTypeLegal(SVT
)) {
1292 TransformToType
[i
] = SVT
;
1293 RegisterTypeForVT
[i
] = SVT
;
1294 NumRegistersForVT
[i
] = 1;
1295 ValueTypeActions
.setTypeAction(VT
, TypeWidenVector
);
1296 IsLegalWiderType
= true;
1300 if (IsLegalWiderType
)
1303 // Only widen to the next power of 2 to keep consistency with EVT.
1304 MVT NVT
= VT
.getPow2VectorType();
1305 if (isTypeLegal(NVT
)) {
1306 TransformToType
[i
] = NVT
;
1307 ValueTypeActions
.setTypeAction(VT
, TypeWidenVector
);
1308 RegisterTypeForVT
[i
] = NVT
;
1309 NumRegistersForVT
[i
] = 1;
1315 case TypeSplitVector
:
1316 case TypeScalarizeVector
: {
1319 unsigned NumIntermediates
;
1320 NumRegistersForVT
[i
] = getVectorTypeBreakdownMVT(VT
, IntermediateVT
,
1321 NumIntermediates
, RegisterVT
, this);
1322 RegisterTypeForVT
[i
] = RegisterVT
;
1324 MVT NVT
= VT
.getPow2VectorType();
1326 // Type is already a power of 2. The default action is to split.
1327 TransformToType
[i
] = MVT::Other
;
1328 if (PreferredAction
== TypeScalarizeVector
)
1329 ValueTypeActions
.setTypeAction(VT
, TypeScalarizeVector
);
1330 else if (PreferredAction
== TypeSplitVector
)
1331 ValueTypeActions
.setTypeAction(VT
, TypeSplitVector
);
1333 // Set type action according to the number of elements.
1334 ValueTypeActions
.setTypeAction(VT
, NElts
== 1 ? TypeScalarizeVector
1337 TransformToType
[i
] = NVT
;
1338 ValueTypeActions
.setTypeAction(VT
, TypeWidenVector
);
1343 llvm_unreachable("Unknown vector legalization action!");
1347 // Determine the 'representative' register class for each value type.
1348 // An representative register class is the largest (meaning one which is
1349 // not a sub-register class / subreg register class) legal register class for
1350 // a group of value types. For example, on i386, i8, i16, and i32
1351 // representative would be GR32; while on x86_64 it's GR64.
1352 for (unsigned i
= 0; i
!= MVT::LAST_VALUETYPE
; ++i
) {
1353 const TargetRegisterClass
* RRC
;
1355 std::tie(RRC
, Cost
) = findRepresentativeClass(TRI
, (MVT::SimpleValueType
)i
);
1356 RepRegClassForVT
[i
] = RRC
;
1357 RepRegClassCostForVT
[i
] = Cost
;
1361 EVT
TargetLoweringBase::getSetCCResultType(const DataLayout
&DL
, LLVMContext
&,
1363 assert(!VT
.isVector() && "No default SetCC type for vectors!");
1364 return getPointerTy(DL
).SimpleTy
;
1367 MVT::SimpleValueType
TargetLoweringBase::getCmpLibcallReturnType() const {
1368 return MVT::i32
; // return the default value
1371 /// getVectorTypeBreakdown - Vector types are broken down into some number of
1372 /// legal first class types. For example, MVT::v8f32 maps to 2 MVT::v4f32
1373 /// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
1374 /// Similarly, MVT::v2i64 turns into 4 MVT::i32 values with both PPC and X86.
1376 /// This method returns the number of registers needed, and the VT for each
1377 /// register. It also returns the VT and quantity of the intermediate values
1378 /// before they are promoted/expanded.
1379 unsigned TargetLoweringBase::getVectorTypeBreakdown(LLVMContext
&Context
, EVT VT
,
1380 EVT
&IntermediateVT
,
1381 unsigned &NumIntermediates
,
1382 MVT
&RegisterVT
) const {
1383 unsigned NumElts
= VT
.getVectorNumElements();
1385 // If there is a wider vector type with the same element type as this one,
1386 // or a promoted vector type that has the same number of elements which
1387 // are wider, then we should convert to that legal vector type.
1388 // This handles things like <2 x float> -> <4 x float> and
1389 // <4 x i1> -> <4 x i32>.
1390 LegalizeTypeAction TA
= getTypeAction(Context
, VT
);
1391 if (NumElts
!= 1 && (TA
== TypeWidenVector
|| TA
== TypePromoteInteger
)) {
1392 EVT RegisterEVT
= getTypeToTransformTo(Context
, VT
);
1393 if (isTypeLegal(RegisterEVT
)) {
1394 IntermediateVT
= RegisterEVT
;
1395 RegisterVT
= RegisterEVT
.getSimpleVT();
1396 NumIntermediates
= 1;
1401 // Figure out the right, legal destination reg to copy into.
1402 EVT EltTy
= VT
.getVectorElementType();
1404 unsigned NumVectorRegs
= 1;
1406 // FIXME: We don't support non-power-of-2-sized vectors for now. Ideally we
1407 // could break down into LHS/RHS like LegalizeDAG does.
1408 if (!isPowerOf2_32(NumElts
)) {
1409 NumVectorRegs
= NumElts
;
1413 // Divide the input until we get to a supported size. This will always
1414 // end with a scalar if the target doesn't support vectors.
1415 while (NumElts
> 1 && !isTypeLegal(
1416 EVT::getVectorVT(Context
, EltTy
, NumElts
))) {
1418 NumVectorRegs
<<= 1;
1421 NumIntermediates
= NumVectorRegs
;
1423 EVT NewVT
= EVT::getVectorVT(Context
, EltTy
, NumElts
);
1424 if (!isTypeLegal(NewVT
))
1426 IntermediateVT
= NewVT
;
1428 MVT DestVT
= getRegisterType(Context
, NewVT
);
1429 RegisterVT
= DestVT
;
1430 unsigned NewVTSize
= NewVT
.getSizeInBits();
1432 // Convert sizes such as i33 to i64.
1433 if (!isPowerOf2_32(NewVTSize
))
1434 NewVTSize
= NextPowerOf2(NewVTSize
);
1436 if (EVT(DestVT
).bitsLT(NewVT
)) // Value is expanded, e.g. i64 -> i16.
1437 return NumVectorRegs
*(NewVTSize
/DestVT
.getSizeInBits());
1439 // Otherwise, promotion or legal types use the same number of registers as
1440 // the vector decimated to the appropriate level.
1441 return NumVectorRegs
;
1444 /// Get the EVTs and ArgFlags collections that represent the legalized return
1445 /// type of the given function. This does not require a DAG or a return value,
1446 /// and is suitable for use before any DAGs for the function are constructed.
1447 /// TODO: Move this out of TargetLowering.cpp.
1448 void llvm::GetReturnInfo(CallingConv::ID CC
, Type
*ReturnType
,
1450 SmallVectorImpl
<ISD::OutputArg
> &Outs
,
1451 const TargetLowering
&TLI
, const DataLayout
&DL
) {
1452 SmallVector
<EVT
, 4> ValueVTs
;
1453 ComputeValueVTs(TLI
, DL
, ReturnType
, ValueVTs
);
1454 unsigned NumValues
= ValueVTs
.size();
1455 if (NumValues
== 0) return;
1457 for (unsigned j
= 0, f
= NumValues
; j
!= f
; ++j
) {
1458 EVT VT
= ValueVTs
[j
];
1459 ISD::NodeType ExtendKind
= ISD::ANY_EXTEND
;
1461 if (attr
.hasAttribute(AttributeList::ReturnIndex
, Attribute::SExt
))
1462 ExtendKind
= ISD::SIGN_EXTEND
;
1463 else if (attr
.hasAttribute(AttributeList::ReturnIndex
, Attribute::ZExt
))
1464 ExtendKind
= ISD::ZERO_EXTEND
;
1466 // FIXME: C calling convention requires the return type to be promoted to
1467 // at least 32-bit. But this is not necessary for non-C calling
1468 // conventions. The frontend should mark functions whose return values
1469 // require promoting with signext or zeroext attributes.
1470 if (ExtendKind
!= ISD::ANY_EXTEND
&& VT
.isInteger()) {
1471 MVT MinVT
= TLI
.getRegisterType(ReturnType
->getContext(), MVT::i32
);
1472 if (VT
.bitsLT(MinVT
))
1477 TLI
.getNumRegistersForCallingConv(ReturnType
->getContext(), CC
, VT
);
1479 TLI
.getRegisterTypeForCallingConv(ReturnType
->getContext(), CC
, VT
);
1481 // 'inreg' on function refers to return value
1482 ISD::ArgFlagsTy Flags
= ISD::ArgFlagsTy();
1483 if (attr
.hasAttribute(AttributeList::ReturnIndex
, Attribute::InReg
))
1486 // Propagate extension type if any
1487 if (attr
.hasAttribute(AttributeList::ReturnIndex
, Attribute::SExt
))
1489 else if (attr
.hasAttribute(AttributeList::ReturnIndex
, Attribute::ZExt
))
1492 for (unsigned i
= 0; i
< NumParts
; ++i
)
1493 Outs
.push_back(ISD::OutputArg(Flags
, PartVT
, VT
, /*isfixed=*/true, 0, 0));
1497 /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1498 /// function arguments in the caller parameter area. This is the actual
1499 /// alignment, not its logarithm.
1500 unsigned TargetLoweringBase::getByValTypeAlignment(Type
*Ty
,
1501 const DataLayout
&DL
) const {
1502 return DL
.getABITypeAlignment(Ty
);
1505 bool TargetLoweringBase::allowsMemoryAccess(LLVMContext
&Context
,
1506 const DataLayout
&DL
, EVT VT
,
1509 MachineMemOperand::Flags Flags
,
1511 // Check if the specified alignment is sufficient based on the data layout.
1512 // TODO: While using the data layout works in practice, a better solution
1513 // would be to implement this check directly (make this a virtual function).
1514 // For example, the ABI alignment may change based on software platform while
1515 // this function should only be affected by hardware implementation.
1516 Type
*Ty
= VT
.getTypeForEVT(Context
);
1517 if (Alignment
>= DL
.getABITypeAlignment(Ty
)) {
1518 // Assume that an access that meets the ABI-specified alignment is fast.
1519 if (Fast
!= nullptr)
1524 // This is a misaligned access.
1525 return allowsMisalignedMemoryAccesses(VT
, AddrSpace
, Alignment
, Flags
, Fast
);
1528 bool TargetLoweringBase::allowsMemoryAccess(LLVMContext
&Context
,
1529 const DataLayout
&DL
, EVT VT
,
1530 const MachineMemOperand
&MMO
,
1532 return allowsMemoryAccess(Context
, DL
, VT
, MMO
.getAddrSpace(),
1533 MMO
.getAlignment(), MMO
.getFlags(), Fast
);
1536 BranchProbability
TargetLoweringBase::getPredictableBranchThreshold() const {
1537 return BranchProbability(MinPercentageForPredictableBranch
, 100);
1540 //===----------------------------------------------------------------------===//
1541 // TargetTransformInfo Helpers
1542 //===----------------------------------------------------------------------===//
1544 int TargetLoweringBase::InstructionOpcodeToISD(unsigned Opcode
) const {
1545 enum InstructionOpcodes
{
1546 #define HANDLE_INST(NUM, OPCODE, CLASS) OPCODE = NUM,
1547 #define LAST_OTHER_INST(NUM) InstructionOpcodesCount = NUM
1548 #include "llvm/IR/Instruction.def"
1550 switch (static_cast<InstructionOpcodes
>(Opcode
)) {
1553 case Switch
: return 0;
1554 case IndirectBr
: return 0;
1555 case Invoke
: return 0;
1556 case CallBr
: return 0;
1557 case Resume
: return 0;
1558 case Unreachable
: return 0;
1559 case CleanupRet
: return 0;
1560 case CatchRet
: return 0;
1561 case CatchPad
: return 0;
1562 case CatchSwitch
: return 0;
1563 case CleanupPad
: return 0;
1564 case FNeg
: return ISD::FNEG
;
1565 case Add
: return ISD::ADD
;
1566 case FAdd
: return ISD::FADD
;
1567 case Sub
: return ISD::SUB
;
1568 case FSub
: return ISD::FSUB
;
1569 case Mul
: return ISD::MUL
;
1570 case FMul
: return ISD::FMUL
;
1571 case UDiv
: return ISD::UDIV
;
1572 case SDiv
: return ISD::SDIV
;
1573 case FDiv
: return ISD::FDIV
;
1574 case URem
: return ISD::UREM
;
1575 case SRem
: return ISD::SREM
;
1576 case FRem
: return ISD::FREM
;
1577 case Shl
: return ISD::SHL
;
1578 case LShr
: return ISD::SRL
;
1579 case AShr
: return ISD::SRA
;
1580 case And
: return ISD::AND
;
1581 case Or
: return ISD::OR
;
1582 case Xor
: return ISD::XOR
;
1583 case Alloca
: return 0;
1584 case Load
: return ISD::LOAD
;
1585 case Store
: return ISD::STORE
;
1586 case GetElementPtr
: return 0;
1587 case Fence
: return 0;
1588 case AtomicCmpXchg
: return 0;
1589 case AtomicRMW
: return 0;
1590 case Trunc
: return ISD::TRUNCATE
;
1591 case ZExt
: return ISD::ZERO_EXTEND
;
1592 case SExt
: return ISD::SIGN_EXTEND
;
1593 case FPToUI
: return ISD::FP_TO_UINT
;
1594 case FPToSI
: return ISD::FP_TO_SINT
;
1595 case UIToFP
: return ISD::UINT_TO_FP
;
1596 case SIToFP
: return ISD::SINT_TO_FP
;
1597 case FPTrunc
: return ISD::FP_ROUND
;
1598 case FPExt
: return ISD::FP_EXTEND
;
1599 case PtrToInt
: return ISD::BITCAST
;
1600 case IntToPtr
: return ISD::BITCAST
;
1601 case BitCast
: return ISD::BITCAST
;
1602 case AddrSpaceCast
: return ISD::ADDRSPACECAST
;
1603 case ICmp
: return ISD::SETCC
;
1604 case FCmp
: return ISD::SETCC
;
1606 case Call
: return 0;
1607 case Select
: return ISD::SELECT
;
1608 case UserOp1
: return 0;
1609 case UserOp2
: return 0;
1610 case VAArg
: return 0;
1611 case ExtractElement
: return ISD::EXTRACT_VECTOR_ELT
;
1612 case InsertElement
: return ISD::INSERT_VECTOR_ELT
;
1613 case ShuffleVector
: return ISD::VECTOR_SHUFFLE
;
1614 case ExtractValue
: return ISD::MERGE_VALUES
;
1615 case InsertValue
: return ISD::MERGE_VALUES
;
1616 case LandingPad
: return 0;
1619 llvm_unreachable("Unknown instruction type encountered!");
1623 TargetLoweringBase::getTypeLegalizationCost(const DataLayout
&DL
,
1625 LLVMContext
&C
= Ty
->getContext();
1626 EVT MTy
= getValueType(DL
, Ty
);
1629 // We keep legalizing the type until we find a legal kind. We assume that
1630 // the only operation that costs anything is the split. After splitting
1631 // we need to handle two types.
1633 LegalizeKind LK
= getTypeConversion(C
, MTy
);
1635 if (LK
.first
== TypeLegal
)
1636 return std::make_pair(Cost
, MTy
.getSimpleVT());
1638 if (LK
.first
== TypeSplitVector
|| LK
.first
== TypeExpandInteger
)
1641 // Do not loop with f128 type.
1642 if (MTy
== LK
.second
)
1643 return std::make_pair(Cost
, MTy
.getSimpleVT());
1645 // Keep legalizing the type.
1650 Value
*TargetLoweringBase::getDefaultSafeStackPointerLocation(IRBuilder
<> &IRB
,
1651 bool UseTLS
) const {
1652 // compiler-rt provides a variable with a magic name. Targets that do not
1653 // link with compiler-rt may also provide such a variable.
1654 Module
*M
= IRB
.GetInsertBlock()->getParent()->getParent();
1655 const char *UnsafeStackPtrVar
= "__safestack_unsafe_stack_ptr";
1656 auto UnsafeStackPtr
=
1657 dyn_cast_or_null
<GlobalVariable
>(M
->getNamedValue(UnsafeStackPtrVar
));
1659 Type
*StackPtrTy
= Type::getInt8PtrTy(M
->getContext());
1661 if (!UnsafeStackPtr
) {
1662 auto TLSModel
= UseTLS
?
1663 GlobalValue::InitialExecTLSModel
:
1664 GlobalValue::NotThreadLocal
;
1665 // The global variable is not defined yet, define it ourselves.
1666 // We use the initial-exec TLS model because we do not support the
1667 // variable living anywhere other than in the main executable.
1668 UnsafeStackPtr
= new GlobalVariable(
1669 *M
, StackPtrTy
, false, GlobalValue::ExternalLinkage
, nullptr,
1670 UnsafeStackPtrVar
, nullptr, TLSModel
);
1672 // The variable exists, check its type and attributes.
1673 if (UnsafeStackPtr
->getValueType() != StackPtrTy
)
1674 report_fatal_error(Twine(UnsafeStackPtrVar
) + " must have void* type");
1675 if (UseTLS
!= UnsafeStackPtr
->isThreadLocal())
1676 report_fatal_error(Twine(UnsafeStackPtrVar
) + " must " +
1677 (UseTLS
? "" : "not ") + "be thread-local");
1679 return UnsafeStackPtr
;
1682 Value
*TargetLoweringBase::getSafeStackPointerLocation(IRBuilder
<> &IRB
) const {
1683 if (!TM
.getTargetTriple().isAndroid())
1684 return getDefaultSafeStackPointerLocation(IRB
, true);
1686 // Android provides a libc function to retrieve the address of the current
1687 // thread's unsafe stack pointer.
1688 Module
*M
= IRB
.GetInsertBlock()->getParent()->getParent();
1689 Type
*StackPtrTy
= Type::getInt8PtrTy(M
->getContext());
1690 FunctionCallee Fn
= M
->getOrInsertFunction("__safestack_pointer_address",
1691 StackPtrTy
->getPointerTo(0));
1692 return IRB
.CreateCall(Fn
);
1695 //===----------------------------------------------------------------------===//
1696 // Loop Strength Reduction hooks
1697 //===----------------------------------------------------------------------===//
1699 /// isLegalAddressingMode - Return true if the addressing mode represented
1700 /// by AM is legal for this target, for a load/store of the specified type.
1701 bool TargetLoweringBase::isLegalAddressingMode(const DataLayout
&DL
,
1702 const AddrMode
&AM
, Type
*Ty
,
1703 unsigned AS
, Instruction
*I
) const {
1704 // The default implementation of this implements a conservative RISCy, r+r and
1707 // Allows a sign-extended 16-bit immediate field.
1708 if (AM
.BaseOffs
<= -(1LL << 16) || AM
.BaseOffs
>= (1LL << 16)-1)
1711 // No global is ever allowed as a base.
1715 // Only support r+r,
1717 case 0: // "r+i" or just "i", depending on HasBaseReg.
1720 if (AM
.HasBaseReg
&& AM
.BaseOffs
) // "r+r+i" is not allowed.
1722 // Otherwise we have r+r or r+i.
1725 if (AM
.HasBaseReg
|| AM
.BaseOffs
) // 2*r+r or 2*r+i is not allowed.
1727 // Allow 2*r as r+r.
1729 default: // Don't allow n * r
1736 //===----------------------------------------------------------------------===//
1738 //===----------------------------------------------------------------------===//
1740 // For OpenBSD return its special guard variable. Otherwise return nullptr,
1741 // so that SelectionDAG handle SSP.
1742 Value
*TargetLoweringBase::getIRStackGuard(IRBuilder
<> &IRB
) const {
1743 if (getTargetMachine().getTargetTriple().isOSOpenBSD()) {
1744 Module
&M
= *IRB
.GetInsertBlock()->getParent()->getParent();
1745 PointerType
*PtrTy
= Type::getInt8PtrTy(M
.getContext());
1746 return M
.getOrInsertGlobal("__guard_local", PtrTy
);
1751 // Currently only support "standard" __stack_chk_guard.
1752 // TODO: add LOAD_STACK_GUARD support.
1753 void TargetLoweringBase::insertSSPDeclarations(Module
&M
) const {
1754 if (!M
.getNamedValue("__stack_chk_guard"))
1755 new GlobalVariable(M
, Type::getInt8PtrTy(M
.getContext()), false,
1756 GlobalVariable::ExternalLinkage
,
1757 nullptr, "__stack_chk_guard");
1760 // Currently only support "standard" __stack_chk_guard.
1761 // TODO: add LOAD_STACK_GUARD support.
1762 Value
*TargetLoweringBase::getSDagStackGuard(const Module
&M
) const {
1763 return M
.getNamedValue("__stack_chk_guard");
1766 Function
*TargetLoweringBase::getSSPStackGuardCheck(const Module
&M
) const {
1770 unsigned TargetLoweringBase::getMinimumJumpTableEntries() const {
1771 return MinimumJumpTableEntries
;
1774 void TargetLoweringBase::setMinimumJumpTableEntries(unsigned Val
) {
1775 MinimumJumpTableEntries
= Val
;
1778 unsigned TargetLoweringBase::getMinimumJumpTableDensity(bool OptForSize
) const {
1779 return OptForSize
? OptsizeJumpTableDensity
: JumpTableDensity
;
1782 unsigned TargetLoweringBase::getMaximumJumpTableSize() const {
1783 return MaximumJumpTableSize
;
1786 void TargetLoweringBase::setMaximumJumpTableSize(unsigned Val
) {
1787 MaximumJumpTableSize
= Val
;
1790 //===----------------------------------------------------------------------===//
1791 // Reciprocal Estimates
1792 //===----------------------------------------------------------------------===//
1794 /// Get the reciprocal estimate attribute string for a function that will
1795 /// override the target defaults.
1796 static StringRef
getRecipEstimateForFunc(MachineFunction
&MF
) {
1797 const Function
&F
= MF
.getFunction();
1798 return F
.getFnAttribute("reciprocal-estimates").getValueAsString();
1801 /// Construct a string for the given reciprocal operation of the given type.
1802 /// This string should match the corresponding option to the front-end's
1803 /// "-mrecip" flag assuming those strings have been passed through in an
1804 /// attribute string. For example, "vec-divf" for a division of a vXf32.
1805 static std::string
getReciprocalOpName(bool IsSqrt
, EVT VT
) {
1806 std::string Name
= VT
.isVector() ? "vec-" : "";
1808 Name
+= IsSqrt
? "sqrt" : "div";
1810 // TODO: Handle "half" or other float types?
1811 if (VT
.getScalarType() == MVT::f64
) {
1814 assert(VT
.getScalarType() == MVT::f32
&&
1815 "Unexpected FP type for reciprocal estimate");
1822 /// Return the character position and value (a single numeric character) of a
1823 /// customized refinement operation in the input string if it exists. Return
1824 /// false if there is no customized refinement step count.
1825 static bool parseRefinementStep(StringRef In
, size_t &Position
,
1827 const char RefStepToken
= ':';
1828 Position
= In
.find(RefStepToken
);
1829 if (Position
== StringRef::npos
)
1832 StringRef RefStepString
= In
.substr(Position
+ 1);
1833 // Allow exactly one numeric character for the additional refinement
1835 if (RefStepString
.size() == 1) {
1836 char RefStepChar
= RefStepString
[0];
1837 if (RefStepChar
>= '0' && RefStepChar
<= '9') {
1838 Value
= RefStepChar
- '0';
1842 report_fatal_error("Invalid refinement step for -recip.");
1845 /// For the input attribute string, return one of the ReciprocalEstimate enum
1846 /// status values (enabled, disabled, or not specified) for this operation on
1847 /// the specified data type.
1848 static int getOpEnabled(bool IsSqrt
, EVT VT
, StringRef Override
) {
1849 if (Override
.empty())
1850 return TargetLoweringBase::ReciprocalEstimate::Unspecified
;
1852 SmallVector
<StringRef
, 4> OverrideVector
;
1853 Override
.split(OverrideVector
, ',');
1854 unsigned NumArgs
= OverrideVector
.size();
1856 // Check if "all", "none", or "default" was specified.
1858 // Look for an optional setting of the number of refinement steps needed
1859 // for this type of reciprocal operation.
1862 if (parseRefinementStep(Override
, RefPos
, RefSteps
)) {
1863 // Split the string for further processing.
1864 Override
= Override
.substr(0, RefPos
);
1867 // All reciprocal types are enabled.
1868 if (Override
== "all")
1869 return TargetLoweringBase::ReciprocalEstimate::Enabled
;
1871 // All reciprocal types are disabled.
1872 if (Override
== "none")
1873 return TargetLoweringBase::ReciprocalEstimate::Disabled
;
1875 // Target defaults for enablement are used.
1876 if (Override
== "default")
1877 return TargetLoweringBase::ReciprocalEstimate::Unspecified
;
1880 // The attribute string may omit the size suffix ('f'/'d').
1881 std::string VTName
= getReciprocalOpName(IsSqrt
, VT
);
1882 std::string VTNameNoSize
= VTName
;
1883 VTNameNoSize
.pop_back();
1884 static const char DisabledPrefix
= '!';
1886 for (StringRef RecipType
: OverrideVector
) {
1889 if (parseRefinementStep(RecipType
, RefPos
, RefSteps
))
1890 RecipType
= RecipType
.substr(0, RefPos
);
1892 // Ignore the disablement token for string matching.
1893 bool IsDisabled
= RecipType
[0] == DisabledPrefix
;
1895 RecipType
= RecipType
.substr(1);
1897 if (RecipType
.equals(VTName
) || RecipType
.equals(VTNameNoSize
))
1898 return IsDisabled
? TargetLoweringBase::ReciprocalEstimate::Disabled
1899 : TargetLoweringBase::ReciprocalEstimate::Enabled
;
1902 return TargetLoweringBase::ReciprocalEstimate::Unspecified
;
1905 /// For the input attribute string, return the customized refinement step count
1906 /// for this operation on the specified data type. If the step count does not
1907 /// exist, return the ReciprocalEstimate enum value for unspecified.
1908 static int getOpRefinementSteps(bool IsSqrt
, EVT VT
, StringRef Override
) {
1909 if (Override
.empty())
1910 return TargetLoweringBase::ReciprocalEstimate::Unspecified
;
1912 SmallVector
<StringRef
, 4> OverrideVector
;
1913 Override
.split(OverrideVector
, ',');
1914 unsigned NumArgs
= OverrideVector
.size();
1916 // Check if "all", "default", or "none" was specified.
1918 // Look for an optional setting of the number of refinement steps needed
1919 // for this type of reciprocal operation.
1922 if (!parseRefinementStep(Override
, RefPos
, RefSteps
))
1923 return TargetLoweringBase::ReciprocalEstimate::Unspecified
;
1925 // Split the string for further processing.
1926 Override
= Override
.substr(0, RefPos
);
1927 assert(Override
!= "none" &&
1928 "Disabled reciprocals, but specifed refinement steps?");
1930 // If this is a general override, return the specified number of steps.
1931 if (Override
== "all" || Override
== "default")
1935 // The attribute string may omit the size suffix ('f'/'d').
1936 std::string VTName
= getReciprocalOpName(IsSqrt
, VT
);
1937 std::string VTNameNoSize
= VTName
;
1938 VTNameNoSize
.pop_back();
1940 for (StringRef RecipType
: OverrideVector
) {
1943 if (!parseRefinementStep(RecipType
, RefPos
, RefSteps
))
1946 RecipType
= RecipType
.substr(0, RefPos
);
1947 if (RecipType
.equals(VTName
) || RecipType
.equals(VTNameNoSize
))
1951 return TargetLoweringBase::ReciprocalEstimate::Unspecified
;
1954 int TargetLoweringBase::getRecipEstimateSqrtEnabled(EVT VT
,
1955 MachineFunction
&MF
) const {
1956 return getOpEnabled(true, VT
, getRecipEstimateForFunc(MF
));
1959 int TargetLoweringBase::getRecipEstimateDivEnabled(EVT VT
,
1960 MachineFunction
&MF
) const {
1961 return getOpEnabled(false, VT
, getRecipEstimateForFunc(MF
));
1964 int TargetLoweringBase::getSqrtRefinementSteps(EVT VT
,
1965 MachineFunction
&MF
) const {
1966 return getOpRefinementSteps(true, VT
, getRecipEstimateForFunc(MF
));
1969 int TargetLoweringBase::getDivRefinementSteps(EVT VT
,
1970 MachineFunction
&MF
) const {
1971 return getOpRefinementSteps(false, VT
, getRecipEstimateForFunc(MF
));
1974 void TargetLoweringBase::finalizeLowering(MachineFunction
&MF
) const {
1975 MF
.getRegInfo().freezeReservedRegs(MF
);