1 //===-- SelectionDAGBuilder.cpp - Selection-DAG building ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This implements routines for translating from LLVM IR into SelectionDAG IR.
12 //===----------------------------------------------------------------------===//
14 #include "SelectionDAGBuilder.h"
15 #include "SDNodeDbgValue.h"
16 #include "llvm/ADT/BitVector.h"
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/ADT/SmallSet.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/Analysis/AliasAnalysis.h"
21 #include "llvm/Analysis/BranchProbabilityInfo.h"
22 #include "llvm/Analysis/ConstantFolding.h"
23 #include "llvm/Analysis/TargetLibraryInfo.h"
24 #include "llvm/Analysis/ValueTracking.h"
25 #include "llvm/Analysis/VectorUtils.h"
26 #include "llvm/CodeGen/FastISel.h"
27 #include "llvm/CodeGen/FunctionLoweringInfo.h"
28 #include "llvm/CodeGen/GCMetadata.h"
29 #include "llvm/CodeGen/GCStrategy.h"
30 #include "llvm/CodeGen/MachineFrameInfo.h"
31 #include "llvm/CodeGen/MachineFunction.h"
32 #include "llvm/CodeGen/MachineInstrBuilder.h"
33 #include "llvm/CodeGen/MachineJumpTableInfo.h"
34 #include "llvm/CodeGen/MachineModuleInfo.h"
35 #include "llvm/CodeGen/MachineRegisterInfo.h"
36 #include "llvm/CodeGen/SelectionDAG.h"
37 #include "llvm/CodeGen/StackMaps.h"
38 #include "llvm/CodeGen/WinEHFuncInfo.h"
39 #include "llvm/IR/CallingConv.h"
40 #include "llvm/IR/Constants.h"
41 #include "llvm/IR/DataLayout.h"
42 #include "llvm/IR/DebugInfo.h"
43 #include "llvm/IR/DerivedTypes.h"
44 #include "llvm/IR/Function.h"
45 #include "llvm/IR/GlobalVariable.h"
46 #include "llvm/IR/InlineAsm.h"
47 #include "llvm/IR/Instructions.h"
48 #include "llvm/IR/IntrinsicInst.h"
49 #include "llvm/IR/Intrinsics.h"
50 #include "llvm/IR/LLVMContext.h"
51 #include "llvm/IR/Module.h"
52 #include "llvm/IR/Statepoint.h"
53 #include "llvm/MC/MCSymbol.h"
54 #include "llvm/Support/CommandLine.h"
55 #include "llvm/Support/Debug.h"
56 #include "llvm/Support/ErrorHandling.h"
57 #include "llvm/Support/MathExtras.h"
58 #include "llvm/Support/raw_ostream.h"
59 #include "llvm/Target/TargetFrameLowering.h"
60 #include "llvm/Target/TargetInstrInfo.h"
61 #include "llvm/Target/TargetIntrinsicInfo.h"
62 #include "llvm/Target/TargetLowering.h"
63 #include "llvm/Target/TargetOptions.h"
64 #include "llvm/Target/TargetSelectionDAGInfo.h"
65 #include "llvm/Target/TargetSubtargetInfo.h"
70 #define DEBUG_TYPE "isel"
72 /// LimitFloatPrecision - Generate low-precision inline sequences for
73 /// some float libcalls (6, 8 or 12 bits).
74 static unsigned LimitFloatPrecision;
76 static cl::opt<unsigned, true>
77 LimitFPPrecision("limit-float-precision",
78 cl::desc("Generate low-precision inline sequences "
79 "for some float libcalls"),
80 cl::location(LimitFloatPrecision),
84 EnableFMFInDAG("enable-fmf-dag", cl::init(true), cl::Hidden,
85 cl::desc("Enable fast-math-flags for DAG nodes"));
87 // Limit the width of DAG chains. This is important in general to prevent
88 // DAG-based analysis from blowing up. For example, alias analysis and
89 // load clustering may not complete in reasonable time. It is difficult to
90 // recognize and avoid this situation within each individual analysis, and
91 // future analyses are likely to have the same behavior. Limiting DAG width is
92 // the safe approach and will be especially important with global DAGs.
94 // MaxParallelChains default is arbitrarily high to avoid affecting
95 // optimization, but could be lowered to improve compile time. Any ld-ld-st-st
96 // sequence over this should have been converted to llvm.memcpy by the
97 // frontend. It easy to induce this behavior with .ll code such as:
98 // %buffer = alloca [4096 x i8]
99 // %data = load [4096 x i8]* %argPtr
100 // store [4096 x i8] %data, [4096 x i8]* %buffer
101 static const unsigned MaxParallelChains = 64;
103 static SDValue getCopyFromPartsVector(SelectionDAG &DAG, SDLoc DL,
104 const SDValue *Parts, unsigned NumParts,
105 MVT PartVT, EVT ValueVT, const Value *V);
107 /// getCopyFromParts - Create a value that contains the specified legal parts
108 /// combined into the value they represent. If the parts combine to a type
109 /// larger then ValueVT then AssertOp can be used to specify whether the extra
110 /// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
111 /// (ISD::AssertSext).
112 static SDValue getCopyFromParts(SelectionDAG &DAG, SDLoc DL,
113 const SDValue *Parts,
114 unsigned NumParts, MVT PartVT, EVT ValueVT,
116 ISD::NodeType AssertOp = ISD::DELETED_NODE) {
117 if (ValueVT.isVector())
118 return getCopyFromPartsVector(DAG, DL, Parts, NumParts,
121 assert(NumParts > 0 && "No parts to assemble!");
122 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
123 SDValue Val = Parts[0];
126 // Assemble the value from multiple parts.
127 if (ValueVT.isInteger()) {
128 unsigned PartBits = PartVT.getSizeInBits();
129 unsigned ValueBits = ValueVT.getSizeInBits();
131 // Assemble the power of 2 part.
132 unsigned RoundParts = NumParts & (NumParts - 1) ?
133 1 << Log2_32(NumParts) : NumParts;
134 unsigned RoundBits = PartBits * RoundParts;
135 EVT RoundVT = RoundBits == ValueBits ?
136 ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
139 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
141 if (RoundParts > 2) {
142 Lo = getCopyFromParts(DAG, DL, Parts, RoundParts / 2,
144 Hi = getCopyFromParts(DAG, DL, Parts + RoundParts / 2,
145 RoundParts / 2, PartVT, HalfVT, V);
147 Lo = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[0]);
148 Hi = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[1]);
151 if (DAG.getDataLayout().isBigEndian())
154 Val = DAG.getNode(ISD::BUILD_PAIR, DL, RoundVT, Lo, Hi);
156 if (RoundParts < NumParts) {
157 // Assemble the trailing non-power-of-2 part.
158 unsigned OddParts = NumParts - RoundParts;
159 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
160 Hi = getCopyFromParts(DAG, DL,
161 Parts + RoundParts, OddParts, PartVT, OddVT, V);
163 // Combine the round and odd parts.
165 if (DAG.getDataLayout().isBigEndian())
167 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
168 Hi = DAG.getNode(ISD::ANY_EXTEND, DL, TotalVT, Hi);
170 DAG.getNode(ISD::SHL, DL, TotalVT, Hi,
171 DAG.getConstant(Lo.getValueType().getSizeInBits(), DL,
172 TLI.getPointerTy(DAG.getDataLayout())));
173 Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, TotalVT, Lo);
174 Val = DAG.getNode(ISD::OR, DL, TotalVT, Lo, Hi);
176 } else if (PartVT.isFloatingPoint()) {
177 // FP split into multiple FP parts (for ppcf128)
178 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == MVT::f64 &&
181 Lo = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[0]);
182 Hi = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[1]);
183 if (TLI.hasBigEndianPartOrdering(ValueVT, DAG.getDataLayout()))
185 Val = DAG.getNode(ISD::BUILD_PAIR, DL, ValueVT, Lo, Hi);
187 // FP split into integer parts (soft fp)
188 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
189 !PartVT.isVector() && "Unexpected split");
190 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
191 Val = getCopyFromParts(DAG, DL, Parts, NumParts, PartVT, IntVT, V);
195 // There is now one part, held in Val. Correct it to match ValueVT.
196 EVT PartEVT = Val.getValueType();
198 if (PartEVT == ValueVT)
201 if (PartEVT.isInteger() && ValueVT.isFloatingPoint() &&
202 ValueVT.bitsLT(PartEVT)) {
203 // For an FP value in an integer part, we need to truncate to the right
205 PartEVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
206 Val = DAG.getNode(ISD::TRUNCATE, DL, PartEVT, Val);
209 if (PartEVT.isInteger() && ValueVT.isInteger()) {
210 if (ValueVT.bitsLT(PartEVT)) {
211 // For a truncate, see if we have any information to
212 // indicate whether the truncated bits will always be
213 // zero or sign-extension.
214 if (AssertOp != ISD::DELETED_NODE)
215 Val = DAG.getNode(AssertOp, DL, PartEVT, Val,
216 DAG.getValueType(ValueVT));
217 return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
219 return DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val);
222 if (PartEVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
223 // FP_ROUND's are always exact here.
224 if (ValueVT.bitsLT(Val.getValueType()))
226 ISD::FP_ROUND, DL, ValueVT, Val,
227 DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout())));
229 return DAG.getNode(ISD::FP_EXTEND, DL, ValueVT, Val);
232 if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits())
233 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
235 llvm_unreachable("Unknown mismatch!");
238 static void diagnosePossiblyInvalidConstraint(LLVMContext &Ctx, const Value *V,
239 const Twine &ErrMsg) {
240 const Instruction *I = dyn_cast_or_null<Instruction>(V);
242 return Ctx.emitError(ErrMsg);
244 const char *AsmError = ", possible invalid constraint for vector type";
245 if (const CallInst *CI = dyn_cast<CallInst>(I))
246 if (isa<InlineAsm>(CI->getCalledValue()))
247 return Ctx.emitError(I, ErrMsg + AsmError);
249 return Ctx.emitError(I, ErrMsg);
252 /// getCopyFromPartsVector - Create a value that contains the specified legal
253 /// parts combined into the value they represent. If the parts combine to a
254 /// type larger then ValueVT then AssertOp can be used to specify whether the
255 /// extra bits are known to be zero (ISD::AssertZext) or sign extended from
256 /// ValueVT (ISD::AssertSext).
257 static SDValue getCopyFromPartsVector(SelectionDAG &DAG, SDLoc DL,
258 const SDValue *Parts, unsigned NumParts,
259 MVT PartVT, EVT ValueVT, const Value *V) {
260 assert(ValueVT.isVector() && "Not a vector value");
261 assert(NumParts > 0 && "No parts to assemble!");
262 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
263 SDValue Val = Parts[0];
265 // Handle a multi-element vector.
269 unsigned NumIntermediates;
271 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
272 NumIntermediates, RegisterVT);
273 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
274 NumParts = NumRegs; // Silence a compiler warning.
275 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
276 assert(RegisterVT.getSizeInBits() ==
277 Parts[0].getSimpleValueType().getSizeInBits() &&
278 "Part type sizes don't match!");
280 // Assemble the parts into intermediate operands.
281 SmallVector<SDValue, 8> Ops(NumIntermediates);
282 if (NumIntermediates == NumParts) {
283 // If the register was not expanded, truncate or copy the value,
285 for (unsigned i = 0; i != NumParts; ++i)
286 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i], 1,
287 PartVT, IntermediateVT, V);
288 } else if (NumParts > 0) {
289 // If the intermediate type was expanded, build the intermediate
290 // operands from the parts.
291 assert(NumParts % NumIntermediates == 0 &&
292 "Must expand into a divisible number of parts!");
293 unsigned Factor = NumParts / NumIntermediates;
294 for (unsigned i = 0; i != NumIntermediates; ++i)
295 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i * Factor], Factor,
296 PartVT, IntermediateVT, V);
299 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the
300 // intermediate operands.
301 Val = DAG.getNode(IntermediateVT.isVector() ? ISD::CONCAT_VECTORS
306 // There is now one part, held in Val. Correct it to match ValueVT.
307 EVT PartEVT = Val.getValueType();
309 if (PartEVT == ValueVT)
312 if (PartEVT.isVector()) {
313 // If the element type of the source/dest vectors are the same, but the
314 // parts vector has more elements than the value vector, then we have a
315 // vector widening case (e.g. <2 x float> -> <4 x float>). Extract the
317 if (PartEVT.getVectorElementType() == ValueVT.getVectorElementType()) {
318 assert(PartEVT.getVectorNumElements() > ValueVT.getVectorNumElements() &&
319 "Cannot narrow, it would be a lossy transformation");
321 ISD::EXTRACT_SUBVECTOR, DL, ValueVT, Val,
322 DAG.getConstant(0, DL, TLI.getVectorIdxTy(DAG.getDataLayout())));
325 // Vector/Vector bitcast.
326 if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits())
327 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
329 assert(PartEVT.getVectorNumElements() == ValueVT.getVectorNumElements() &&
330 "Cannot handle this kind of promotion");
331 // Promoted vector extract
332 return DAG.getAnyExtOrTrunc(Val, DL, ValueVT);
336 // Trivial bitcast if the types are the same size and the destination
337 // vector type is legal.
338 if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits() &&
339 TLI.isTypeLegal(ValueVT))
340 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
342 // Handle cases such as i8 -> <1 x i1>
343 if (ValueVT.getVectorNumElements() != 1) {
344 diagnosePossiblyInvalidConstraint(*DAG.getContext(), V,
345 "non-trivial scalar-to-vector conversion");
346 return DAG.getUNDEF(ValueVT);
349 if (ValueVT.getVectorNumElements() == 1 &&
350 ValueVT.getVectorElementType() != PartEVT)
351 Val = DAG.getAnyExtOrTrunc(Val, DL, ValueVT.getScalarType());
353 return DAG.getNode(ISD::BUILD_VECTOR, DL, ValueVT, Val);
356 static void getCopyToPartsVector(SelectionDAG &DAG, SDLoc dl,
357 SDValue Val, SDValue *Parts, unsigned NumParts,
358 MVT PartVT, const Value *V);
360 /// getCopyToParts - Create a series of nodes that contain the specified value
361 /// split into legal parts. If the parts contain more bits than Val, then, for
362 /// integers, ExtendKind can be used to specify how to generate the extra bits.
363 static void getCopyToParts(SelectionDAG &DAG, SDLoc DL,
364 SDValue Val, SDValue *Parts, unsigned NumParts,
365 MVT PartVT, const Value *V,
366 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
367 EVT ValueVT = Val.getValueType();
369 // Handle the vector case separately.
370 if (ValueVT.isVector())
371 return getCopyToPartsVector(DAG, DL, Val, Parts, NumParts, PartVT, V);
373 unsigned PartBits = PartVT.getSizeInBits();
374 unsigned OrigNumParts = NumParts;
375 assert(DAG.getTargetLoweringInfo().isTypeLegal(PartVT) &&
376 "Copying to an illegal type!");
381 assert(!ValueVT.isVector() && "Vector case handled elsewhere");
382 EVT PartEVT = PartVT;
383 if (PartEVT == ValueVT) {
384 assert(NumParts == 1 && "No-op copy with multiple parts!");
389 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
390 // If the parts cover more bits than the value has, promote the value.
391 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
392 assert(NumParts == 1 && "Do not know what to promote to!");
393 Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val);
395 if (ValueVT.isFloatingPoint()) {
396 // FP values need to be bitcast, then extended if they are being put
397 // into a larger container.
398 ValueVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
399 Val = DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
401 assert((PartVT.isInteger() || PartVT == MVT::x86mmx) &&
402 ValueVT.isInteger() &&
403 "Unknown mismatch!");
404 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
405 Val = DAG.getNode(ExtendKind, DL, ValueVT, Val);
406 if (PartVT == MVT::x86mmx)
407 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
409 } else if (PartBits == ValueVT.getSizeInBits()) {
410 // Different types of the same size.
411 assert(NumParts == 1 && PartEVT != ValueVT);
412 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
413 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
414 // If the parts cover less bits than value has, truncate the value.
415 assert((PartVT.isInteger() || PartVT == MVT::x86mmx) &&
416 ValueVT.isInteger() &&
417 "Unknown mismatch!");
418 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
419 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
420 if (PartVT == MVT::x86mmx)
421 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
424 // The value may have changed - recompute ValueVT.
425 ValueVT = Val.getValueType();
426 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
427 "Failed to tile the value with PartVT!");
430 if (PartEVT != ValueVT)
431 diagnosePossiblyInvalidConstraint(*DAG.getContext(), V,
432 "scalar-to-vector conversion failed");
438 // Expand the value into multiple parts.
439 if (NumParts & (NumParts - 1)) {
440 // The number of parts is not a power of 2. Split off and copy the tail.
441 assert(PartVT.isInteger() && ValueVT.isInteger() &&
442 "Do not know what to expand to!");
443 unsigned RoundParts = 1 << Log2_32(NumParts);
444 unsigned RoundBits = RoundParts * PartBits;
445 unsigned OddParts = NumParts - RoundParts;
446 SDValue OddVal = DAG.getNode(ISD::SRL, DL, ValueVT, Val,
447 DAG.getIntPtrConstant(RoundBits, DL));
448 getCopyToParts(DAG, DL, OddVal, Parts + RoundParts, OddParts, PartVT, V);
450 if (DAG.getDataLayout().isBigEndian())
451 // The odd parts were reversed by getCopyToParts - unreverse them.
452 std::reverse(Parts + RoundParts, Parts + NumParts);
454 NumParts = RoundParts;
455 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
456 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
459 // The number of parts is a power of 2. Repeatedly bisect the value using
461 Parts[0] = DAG.getNode(ISD::BITCAST, DL,
462 EVT::getIntegerVT(*DAG.getContext(),
463 ValueVT.getSizeInBits()),
466 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
467 for (unsigned i = 0; i < NumParts; i += StepSize) {
468 unsigned ThisBits = StepSize * PartBits / 2;
469 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
470 SDValue &Part0 = Parts[i];
471 SDValue &Part1 = Parts[i+StepSize/2];
473 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
474 ThisVT, Part0, DAG.getIntPtrConstant(1, DL));
475 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
476 ThisVT, Part0, DAG.getIntPtrConstant(0, DL));
478 if (ThisBits == PartBits && ThisVT != PartVT) {
479 Part0 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part0);
480 Part1 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part1);
485 if (DAG.getDataLayout().isBigEndian())
486 std::reverse(Parts, Parts + OrigNumParts);
490 /// getCopyToPartsVector - Create a series of nodes that contain the specified
491 /// value split into legal parts.
492 static void getCopyToPartsVector(SelectionDAG &DAG, SDLoc DL,
493 SDValue Val, SDValue *Parts, unsigned NumParts,
494 MVT PartVT, const Value *V) {
495 EVT ValueVT = Val.getValueType();
496 assert(ValueVT.isVector() && "Not a vector");
497 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
500 EVT PartEVT = PartVT;
501 if (PartEVT == ValueVT) {
503 } else if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
504 // Bitconvert vector->vector case.
505 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
506 } else if (PartVT.isVector() &&
507 PartEVT.getVectorElementType() == ValueVT.getVectorElementType() &&
508 PartEVT.getVectorNumElements() > ValueVT.getVectorNumElements()) {
509 EVT ElementVT = PartVT.getVectorElementType();
510 // Vector widening case, e.g. <2 x float> -> <4 x float>. Shuffle in
512 SmallVector<SDValue, 16> Ops;
513 for (unsigned i = 0, e = ValueVT.getVectorNumElements(); i != e; ++i)
514 Ops.push_back(DAG.getNode(
515 ISD::EXTRACT_VECTOR_ELT, DL, ElementVT, Val,
516 DAG.getConstant(i, DL, TLI.getVectorIdxTy(DAG.getDataLayout()))));
518 for (unsigned i = ValueVT.getVectorNumElements(),
519 e = PartVT.getVectorNumElements(); i != e; ++i)
520 Ops.push_back(DAG.getUNDEF(ElementVT));
522 Val = DAG.getNode(ISD::BUILD_VECTOR, DL, PartVT, Ops);
524 // FIXME: Use CONCAT for 2x -> 4x.
526 //SDValue UndefElts = DAG.getUNDEF(VectorTy);
527 //Val = DAG.getNode(ISD::CONCAT_VECTORS, DL, PartVT, Val, UndefElts);
528 } else if (PartVT.isVector() &&
529 PartEVT.getVectorElementType().bitsGE(
530 ValueVT.getVectorElementType()) &&
531 PartEVT.getVectorNumElements() == ValueVT.getVectorNumElements()) {
533 // Promoted vector extract
534 Val = DAG.getAnyExtOrTrunc(Val, DL, PartVT);
536 // Vector -> scalar conversion.
537 assert(ValueVT.getVectorNumElements() == 1 &&
538 "Only trivial vector-to-scalar conversions should get here!");
540 ISD::EXTRACT_VECTOR_ELT, DL, PartVT, Val,
541 DAG.getConstant(0, DL, TLI.getVectorIdxTy(DAG.getDataLayout())));
543 Val = DAG.getAnyExtOrTrunc(Val, DL, PartVT);
550 // Handle a multi-element vector.
553 unsigned NumIntermediates;
554 unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT,
556 NumIntermediates, RegisterVT);
557 unsigned NumElements = ValueVT.getVectorNumElements();
559 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
560 NumParts = NumRegs; // Silence a compiler warning.
561 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
563 // Split the vector into intermediate operands.
564 SmallVector<SDValue, 8> Ops(NumIntermediates);
565 for (unsigned i = 0; i != NumIntermediates; ++i) {
566 if (IntermediateVT.isVector())
568 DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, IntermediateVT, Val,
569 DAG.getConstant(i * (NumElements / NumIntermediates), DL,
570 TLI.getVectorIdxTy(DAG.getDataLayout())));
572 Ops[i] = DAG.getNode(
573 ISD::EXTRACT_VECTOR_ELT, DL, IntermediateVT, Val,
574 DAG.getConstant(i, DL, TLI.getVectorIdxTy(DAG.getDataLayout())));
577 // Split the intermediate operands into legal parts.
578 if (NumParts == NumIntermediates) {
579 // If the register was not expanded, promote or copy the value,
581 for (unsigned i = 0; i != NumParts; ++i)
582 getCopyToParts(DAG, DL, Ops[i], &Parts[i], 1, PartVT, V);
583 } else if (NumParts > 0) {
584 // If the intermediate type was expanded, split each the value into
586 assert(NumIntermediates != 0 && "division by zero");
587 assert(NumParts % NumIntermediates == 0 &&
588 "Must expand into a divisible number of parts!");
589 unsigned Factor = NumParts / NumIntermediates;
590 for (unsigned i = 0; i != NumIntermediates; ++i)
591 getCopyToParts(DAG, DL, Ops[i], &Parts[i*Factor], Factor, PartVT, V);
595 RegsForValue::RegsForValue() {}
597 RegsForValue::RegsForValue(const SmallVector<unsigned, 4> ®s, MVT regvt,
599 : ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {}
601 RegsForValue::RegsForValue(LLVMContext &Context, const TargetLowering &TLI,
602 const DataLayout &DL, unsigned Reg, Type *Ty) {
603 ComputeValueVTs(TLI, DL, Ty, ValueVTs);
605 for (EVT ValueVT : ValueVTs) {
606 unsigned NumRegs = TLI.getNumRegisters(Context, ValueVT);
607 MVT RegisterVT = TLI.getRegisterType(Context, ValueVT);
608 for (unsigned i = 0; i != NumRegs; ++i)
609 Regs.push_back(Reg + i);
610 RegVTs.push_back(RegisterVT);
615 /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
616 /// this value and returns the result as a ValueVT value. This uses
617 /// Chain/Flag as the input and updates them for the output Chain/Flag.
618 /// If the Flag pointer is NULL, no flag is used.
619 SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG,
620 FunctionLoweringInfo &FuncInfo,
622 SDValue &Chain, SDValue *Flag,
623 const Value *V) const {
624 // A Value with type {} or [0 x %t] needs no registers.
625 if (ValueVTs.empty())
628 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
630 // Assemble the legal parts into the final values.
631 SmallVector<SDValue, 4> Values(ValueVTs.size());
632 SmallVector<SDValue, 8> Parts;
633 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
634 // Copy the legal parts from the registers.
635 EVT ValueVT = ValueVTs[Value];
636 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVT);
637 MVT RegisterVT = RegVTs[Value];
639 Parts.resize(NumRegs);
640 for (unsigned i = 0; i != NumRegs; ++i) {
643 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
645 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
646 *Flag = P.getValue(2);
649 Chain = P.getValue(1);
652 // If the source register was virtual and if we know something about it,
653 // add an assert node.
654 if (!TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) ||
655 !RegisterVT.isInteger() || RegisterVT.isVector())
658 const FunctionLoweringInfo::LiveOutInfo *LOI =
659 FuncInfo.GetLiveOutRegInfo(Regs[Part+i]);
663 unsigned RegSize = RegisterVT.getSizeInBits();
664 unsigned NumSignBits = LOI->NumSignBits;
665 unsigned NumZeroBits = LOI->KnownZero.countLeadingOnes();
667 if (NumZeroBits == RegSize) {
668 // The current value is a zero.
669 // Explicitly express that as it would be easier for
670 // optimizations to kick in.
671 Parts[i] = DAG.getConstant(0, dl, RegisterVT);
675 // FIXME: We capture more information than the dag can represent. For
676 // now, just use the tightest assertzext/assertsext possible.
678 EVT FromVT(MVT::Other);
679 if (NumSignBits == RegSize)
680 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
681 else if (NumZeroBits >= RegSize-1)
682 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
683 else if (NumSignBits > RegSize-8)
684 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
685 else if (NumZeroBits >= RegSize-8)
686 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
687 else if (NumSignBits > RegSize-16)
688 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
689 else if (NumZeroBits >= RegSize-16)
690 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
691 else if (NumSignBits > RegSize-32)
692 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
693 else if (NumZeroBits >= RegSize-32)
694 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
698 // Add an assertion node.
699 assert(FromVT != MVT::Other);
700 Parts[i] = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
701 RegisterVT, P, DAG.getValueType(FromVT));
704 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
705 NumRegs, RegisterVT, ValueVT, V);
710 return DAG.getNode(ISD::MERGE_VALUES, dl, DAG.getVTList(ValueVTs), Values);
713 /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
714 /// specified value into the registers specified by this object. This uses
715 /// Chain/Flag as the input and updates them for the output Chain/Flag.
716 /// If the Flag pointer is NULL, no flag is used.
717 void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, SDLoc dl,
718 SDValue &Chain, SDValue *Flag, const Value *V,
719 ISD::NodeType PreferredExtendType) const {
720 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
721 ISD::NodeType ExtendKind = PreferredExtendType;
723 // Get the list of the values's legal parts.
724 unsigned NumRegs = Regs.size();
725 SmallVector<SDValue, 8> Parts(NumRegs);
726 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
727 EVT ValueVT = ValueVTs[Value];
728 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), ValueVT);
729 MVT RegisterVT = RegVTs[Value];
731 if (ExtendKind == ISD::ANY_EXTEND && TLI.isZExtFree(Val, RegisterVT))
732 ExtendKind = ISD::ZERO_EXTEND;
734 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value),
735 &Parts[Part], NumParts, RegisterVT, V, ExtendKind);
739 // Copy the parts into the registers.
740 SmallVector<SDValue, 8> Chains(NumRegs);
741 for (unsigned i = 0; i != NumRegs; ++i) {
744 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
746 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
747 *Flag = Part.getValue(1);
750 Chains[i] = Part.getValue(0);
753 if (NumRegs == 1 || Flag)
754 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
755 // flagged to it. That is the CopyToReg nodes and the user are considered
756 // a single scheduling unit. If we create a TokenFactor and return it as
757 // chain, then the TokenFactor is both a predecessor (operand) of the
758 // user as well as a successor (the TF operands are flagged to the user).
759 // c1, f1 = CopyToReg
760 // c2, f2 = CopyToReg
761 // c3 = TokenFactor c1, c2
764 Chain = Chains[NumRegs-1];
766 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
769 /// AddInlineAsmOperands - Add this value to the specified inlineasm node
770 /// operand list. This adds the code marker and includes the number of
771 /// values added into it.
772 void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching,
773 unsigned MatchingIdx, SDLoc dl,
775 std::vector<SDValue> &Ops) const {
776 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
778 unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size());
780 Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx);
781 else if (!Regs.empty() &&
782 TargetRegisterInfo::isVirtualRegister(Regs.front())) {
783 // Put the register class of the virtual registers in the flag word. That
784 // way, later passes can recompute register class constraints for inline
785 // assembly as well as normal instructions.
786 // Don't do this for tied operands that can use the regclass information
788 const MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
789 const TargetRegisterClass *RC = MRI.getRegClass(Regs.front());
790 Flag = InlineAsm::getFlagWordForRegClass(Flag, RC->getID());
793 SDValue Res = DAG.getTargetConstant(Flag, dl, MVT::i32);
796 unsigned SP = TLI.getStackPointerRegisterToSaveRestore();
797 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
798 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
799 MVT RegisterVT = RegVTs[Value];
800 for (unsigned i = 0; i != NumRegs; ++i) {
801 assert(Reg < Regs.size() && "Mismatch in # registers expected");
802 unsigned TheReg = Regs[Reg++];
803 Ops.push_back(DAG.getRegister(TheReg, RegisterVT));
805 if (TheReg == SP && Code == InlineAsm::Kind_Clobber) {
806 // If we clobbered the stack pointer, MFI should know about it.
807 assert(DAG.getMachineFunction().getFrameInfo()->
808 hasOpaqueSPAdjustment());
814 void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa,
815 const TargetLibraryInfo *li) {
819 DL = &DAG.getDataLayout();
820 Context = DAG.getContext();
821 LPadToCallSiteMap.clear();
824 /// clear - Clear out the current SelectionDAG and the associated
825 /// state and prepare this SelectionDAGBuilder object to be used
826 /// for a new block. This doesn't clear out information about
827 /// additional blocks that are needed to complete switch lowering
828 /// or PHI node updating; that information is cleared out as it is
830 void SelectionDAGBuilder::clear() {
832 UnusedArgNodeMap.clear();
833 PendingLoads.clear();
834 PendingExports.clear();
837 SDNodeOrder = LowestSDNodeOrder;
838 StatepointLowering.clear();
841 /// clearDanglingDebugInfo - Clear the dangling debug information
842 /// map. This function is separated from the clear so that debug
843 /// information that is dangling in a basic block can be properly
844 /// resolved in a different basic block. This allows the
845 /// SelectionDAG to resolve dangling debug information attached
847 void SelectionDAGBuilder::clearDanglingDebugInfo() {
848 DanglingDebugInfoMap.clear();
851 /// getRoot - Return the current virtual root of the Selection DAG,
852 /// flushing any PendingLoad items. This must be done before emitting
853 /// a store or any other node that may need to be ordered after any
854 /// prior load instructions.
856 SDValue SelectionDAGBuilder::getRoot() {
857 if (PendingLoads.empty())
858 return DAG.getRoot();
860 if (PendingLoads.size() == 1) {
861 SDValue Root = PendingLoads[0];
863 PendingLoads.clear();
867 // Otherwise, we have to make a token factor node.
868 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other,
870 PendingLoads.clear();
875 /// getControlRoot - Similar to getRoot, but instead of flushing all the
876 /// PendingLoad items, flush all the PendingExports items. It is necessary
877 /// to do this before emitting a terminator instruction.
879 SDValue SelectionDAGBuilder::getControlRoot() {
880 SDValue Root = DAG.getRoot();
882 if (PendingExports.empty())
885 // Turn all of the CopyToReg chains into one factored node.
886 if (Root.getOpcode() != ISD::EntryToken) {
887 unsigned i = 0, e = PendingExports.size();
888 for (; i != e; ++i) {
889 assert(PendingExports[i].getNode()->getNumOperands() > 1);
890 if (PendingExports[i].getNode()->getOperand(0) == Root)
891 break; // Don't add the root if we already indirectly depend on it.
895 PendingExports.push_back(Root);
898 Root = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other,
900 PendingExports.clear();
905 void SelectionDAGBuilder::visit(const Instruction &I) {
906 // Set up outgoing PHI node register values before emitting the terminator.
907 if (isa<TerminatorInst>(&I))
908 HandlePHINodesInSuccessorBlocks(I.getParent());
914 visit(I.getOpcode(), I);
916 if (!isa<TerminatorInst>(&I) && !HasTailCall &&
917 !isStatepoint(&I)) // statepoints handle their exports internally
918 CopyToExportRegsIfNeeded(&I);
923 void SelectionDAGBuilder::visitPHI(const PHINode &) {
924 llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
927 void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
928 // Note: this doesn't use InstVisitor, because it has to work with
929 // ConstantExpr's in addition to instructions.
931 default: llvm_unreachable("Unknown instruction type encountered!");
932 // Build the switch statement using the Instruction.def file.
933 #define HANDLE_INST(NUM, OPCODE, CLASS) \
934 case Instruction::OPCODE: visit##OPCODE((const CLASS&)I); break;
935 #include "llvm/IR/Instruction.def"
939 // resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V,
940 // generate the debug data structures now that we've seen its definition.
941 void SelectionDAGBuilder::resolveDanglingDebugInfo(const Value *V,
943 DanglingDebugInfo &DDI = DanglingDebugInfoMap[V];
945 const DbgValueInst *DI = DDI.getDI();
946 DebugLoc dl = DDI.getdl();
947 unsigned DbgSDNodeOrder = DDI.getSDNodeOrder();
948 DILocalVariable *Variable = DI->getVariable();
949 DIExpression *Expr = DI->getExpression();
950 assert(Variable->isValidLocationForIntrinsic(dl) &&
951 "Expected inlined-at fields to agree");
952 uint64_t Offset = DI->getOffset();
955 if (!EmitFuncArgumentDbgValue(V, Variable, Expr, dl, Offset, false,
957 SDV = DAG.getDbgValue(Variable, Expr, Val.getNode(), Val.getResNo(),
958 false, Offset, dl, DbgSDNodeOrder);
959 DAG.AddDbgValue(SDV, Val.getNode(), false);
962 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
963 DanglingDebugInfoMap[V] = DanglingDebugInfo();
967 /// getCopyFromRegs - If there was virtual register allocated for the value V
968 /// emit CopyFromReg of the specified type Ty. Return empty SDValue() otherwise.
969 SDValue SelectionDAGBuilder::getCopyFromRegs(const Value *V, Type *Ty) {
970 DenseMap<const Value *, unsigned>::iterator It = FuncInfo.ValueMap.find(V);
973 if (It != FuncInfo.ValueMap.end()) {
974 unsigned InReg = It->second;
975 RegsForValue RFV(*DAG.getContext(), DAG.getTargetLoweringInfo(),
976 DAG.getDataLayout(), InReg, Ty);
977 SDValue Chain = DAG.getEntryNode();
978 Result = RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, V);
979 resolveDanglingDebugInfo(V, Result);
985 /// getValue - Return an SDValue for the given Value.
986 SDValue SelectionDAGBuilder::getValue(const Value *V) {
987 // If we already have an SDValue for this value, use it. It's important
988 // to do this first, so that we don't create a CopyFromReg if we already
989 // have a regular SDValue.
990 SDValue &N = NodeMap[V];
991 if (N.getNode()) return N;
993 // If there's a virtual register allocated and initialized for this
995 SDValue copyFromReg = getCopyFromRegs(V, V->getType());
996 if (copyFromReg.getNode()) {
1000 // Otherwise create a new SDValue and remember it.
1001 SDValue Val = getValueImpl(V);
1003 resolveDanglingDebugInfo(V, Val);
1007 // Return true if SDValue exists for the given Value
1008 bool SelectionDAGBuilder::findValue(const Value *V) const {
1009 return (NodeMap.find(V) != NodeMap.end()) ||
1010 (FuncInfo.ValueMap.find(V) != FuncInfo.ValueMap.end());
1013 /// getNonRegisterValue - Return an SDValue for the given Value, but
1014 /// don't look in FuncInfo.ValueMap for a virtual register.
1015 SDValue SelectionDAGBuilder::getNonRegisterValue(const Value *V) {
1016 // If we already have an SDValue for this value, use it.
1017 SDValue &N = NodeMap[V];
1019 if (isa<ConstantSDNode>(N) || isa<ConstantFPSDNode>(N)) {
1020 // Remove the debug location from the node as the node is about to be used
1021 // in a location which may differ from the original debug location. This
1022 // is relevant to Constant and ConstantFP nodes because they can appear
1023 // as constant expressions inside PHI nodes.
1024 N->setDebugLoc(DebugLoc());
1029 // Otherwise create a new SDValue and remember it.
1030 SDValue Val = getValueImpl(V);
1032 resolveDanglingDebugInfo(V, Val);
1036 /// getValueImpl - Helper function for getValue and getNonRegisterValue.
1037 /// Create an SDValue for the given value.
1038 SDValue SelectionDAGBuilder::getValueImpl(const Value *V) {
1039 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1041 if (const Constant *C = dyn_cast<Constant>(V)) {
1042 EVT VT = TLI.getValueType(DAG.getDataLayout(), V->getType(), true);
1044 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
1045 return DAG.getConstant(*CI, getCurSDLoc(), VT);
1047 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
1048 return DAG.getGlobalAddress(GV, getCurSDLoc(), VT);
1050 if (isa<ConstantPointerNull>(C)) {
1051 unsigned AS = V->getType()->getPointerAddressSpace();
1052 return DAG.getConstant(0, getCurSDLoc(),
1053 TLI.getPointerTy(DAG.getDataLayout(), AS));
1056 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
1057 return DAG.getConstantFP(*CFP, getCurSDLoc(), VT);
1059 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
1060 return DAG.getUNDEF(VT);
1062 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
1063 visit(CE->getOpcode(), *CE);
1064 SDValue N1 = NodeMap[V];
1065 assert(N1.getNode() && "visit didn't populate the NodeMap!");
1069 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
1070 SmallVector<SDValue, 4> Constants;
1071 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
1073 SDNode *Val = getValue(*OI).getNode();
1074 // If the operand is an empty aggregate, there are no values.
1076 // Add each leaf value from the operand to the Constants list
1077 // to form a flattened list of all the values.
1078 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1079 Constants.push_back(SDValue(Val, i));
1082 return DAG.getMergeValues(Constants, getCurSDLoc());
1085 if (const ConstantDataSequential *CDS =
1086 dyn_cast<ConstantDataSequential>(C)) {
1087 SmallVector<SDValue, 4> Ops;
1088 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
1089 SDNode *Val = getValue(CDS->getElementAsConstant(i)).getNode();
1090 // Add each leaf value from the operand to the Constants list
1091 // to form a flattened list of all the values.
1092 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1093 Ops.push_back(SDValue(Val, i));
1096 if (isa<ArrayType>(CDS->getType()))
1097 return DAG.getMergeValues(Ops, getCurSDLoc());
1098 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(),
1102 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
1103 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
1104 "Unknown struct or array constant!");
1106 SmallVector<EVT, 4> ValueVTs;
1107 ComputeValueVTs(TLI, DAG.getDataLayout(), C->getType(), ValueVTs);
1108 unsigned NumElts = ValueVTs.size();
1110 return SDValue(); // empty struct
1111 SmallVector<SDValue, 4> Constants(NumElts);
1112 for (unsigned i = 0; i != NumElts; ++i) {
1113 EVT EltVT = ValueVTs[i];
1114 if (isa<UndefValue>(C))
1115 Constants[i] = DAG.getUNDEF(EltVT);
1116 else if (EltVT.isFloatingPoint())
1117 Constants[i] = DAG.getConstantFP(0, getCurSDLoc(), EltVT);
1119 Constants[i] = DAG.getConstant(0, getCurSDLoc(), EltVT);
1122 return DAG.getMergeValues(Constants, getCurSDLoc());
1125 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
1126 return DAG.getBlockAddress(BA, VT);
1128 VectorType *VecTy = cast<VectorType>(V->getType());
1129 unsigned NumElements = VecTy->getNumElements();
1131 // Now that we know the number and type of the elements, get that number of
1132 // elements into the Ops array based on what kind of constant it is.
1133 SmallVector<SDValue, 16> Ops;
1134 if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
1135 for (unsigned i = 0; i != NumElements; ++i)
1136 Ops.push_back(getValue(CV->getOperand(i)));
1138 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
1140 TLI.getValueType(DAG.getDataLayout(), VecTy->getElementType());
1143 if (EltVT.isFloatingPoint())
1144 Op = DAG.getConstantFP(0, getCurSDLoc(), EltVT);
1146 Op = DAG.getConstant(0, getCurSDLoc(), EltVT);
1147 Ops.assign(NumElements, Op);
1150 // Create a BUILD_VECTOR node.
1151 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(), VT, Ops);
1154 // If this is a static alloca, generate it as the frameindex instead of
1156 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1157 DenseMap<const AllocaInst*, int>::iterator SI =
1158 FuncInfo.StaticAllocaMap.find(AI);
1159 if (SI != FuncInfo.StaticAllocaMap.end())
1160 return DAG.getFrameIndex(SI->second,
1161 TLI.getPointerTy(DAG.getDataLayout()));
1164 // If this is an instruction which fast-isel has deferred, select it now.
1165 if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
1166 unsigned InReg = FuncInfo.InitializeRegForValue(Inst);
1167 RegsForValue RFV(*DAG.getContext(), TLI, DAG.getDataLayout(), InReg,
1169 SDValue Chain = DAG.getEntryNode();
1170 return RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, V);
1173 llvm_unreachable("Can't get register for value!");
1176 void SelectionDAGBuilder::visitCatchPad(const CatchPadInst &I) {
1177 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
1178 bool IsMSVCCXX = Pers == EHPersonality::MSVC_CXX;
1179 bool IsCoreCLR = Pers == EHPersonality::CoreCLR;
1180 MachineBasicBlock *CatchPadMBB = FuncInfo.MBB;
1181 // In MSVC C++ and CoreCLR, catchblocks are funclets and need prologues.
1182 if (IsMSVCCXX || IsCoreCLR)
1183 CatchPadMBB->setIsEHFuncletEntry();
1185 DAG.setRoot(DAG.getNode(ISD::CATCHPAD, getCurSDLoc(), MVT::Other, getControlRoot()));
1188 void SelectionDAGBuilder::visitCatchRet(const CatchReturnInst &I) {
1189 // Update machine-CFG edge.
1190 MachineBasicBlock *TargetMBB = FuncInfo.MBBMap[I.getSuccessor()];
1191 FuncInfo.MBB->addSuccessor(TargetMBB);
1193 auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
1194 bool IsSEH = isAsynchronousEHPersonality(Pers);
1196 // If this is not a fall-through branch or optimizations are switched off,
1198 if (TargetMBB != NextBlock(FuncInfo.MBB) ||
1199 TM.getOptLevel() == CodeGenOpt::None)
1200 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
1201 getControlRoot(), DAG.getBasicBlock(TargetMBB)));
1205 // Figure out the funclet membership for the catchret's successor.
1206 // This will be used by the FuncletLayout pass to determine how to order the
1208 WinEHFuncInfo *EHInfo = DAG.getMachineFunction().getWinEHFuncInfo();
1209 const BasicBlock *SuccessorColor = EHInfo->CatchRetSuccessorColorMap[&I];
1210 assert(SuccessorColor && "No parent funclet for catchret!");
1211 MachineBasicBlock *SuccessorColorMBB = FuncInfo.MBBMap[SuccessorColor];
1212 assert(SuccessorColorMBB && "No MBB for SuccessorColor!");
1214 // Create the terminator node.
1215 SDValue Ret = DAG.getNode(ISD::CATCHRET, getCurSDLoc(), MVT::Other,
1216 getControlRoot(), DAG.getBasicBlock(TargetMBB),
1217 DAG.getBasicBlock(SuccessorColorMBB));
1221 void SelectionDAGBuilder::visitCleanupPad(const CleanupPadInst &CPI) {
1222 // Don't emit any special code for the cleanuppad instruction. It just marks
1223 // the start of a funclet.
1224 FuncInfo.MBB->setIsEHFuncletEntry();
1225 FuncInfo.MBB->setIsCleanupFuncletEntry();
1228 /// When an invoke or a cleanupret unwinds to the next EH pad, there are
1229 /// many places it could ultimately go. In the IR, we have a single unwind
1230 /// destination, but in the machine CFG, we enumerate all the possible blocks.
1231 /// This function skips over imaginary basic blocks that hold catchswitch
1232 /// instructions, and finds all the "real" machine
1233 /// basic block destinations. As those destinations may not be successors of
1234 /// EHPadBB, here we also calculate the edge probability to those destinations.
1235 /// The passed-in Prob is the edge probability to EHPadBB.
1236 static void findUnwindDestinations(
1237 FunctionLoweringInfo &FuncInfo, const BasicBlock *EHPadBB,
1238 BranchProbability Prob,
1239 SmallVectorImpl<std::pair<MachineBasicBlock *, BranchProbability>>
1241 EHPersonality Personality =
1242 classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
1243 bool IsMSVCCXX = Personality == EHPersonality::MSVC_CXX;
1244 bool IsCoreCLR = Personality == EHPersonality::CoreCLR;
1247 const Instruction *Pad = EHPadBB->getFirstNonPHI();
1248 BasicBlock *NewEHPadBB = nullptr;
1249 if (isa<LandingPadInst>(Pad)) {
1250 // Stop on landingpads. They are not funclets.
1251 UnwindDests.emplace_back(FuncInfo.MBBMap[EHPadBB], Prob);
1253 } else if (isa<CleanupPadInst>(Pad)) {
1254 // Stop on cleanup pads. Cleanups are always funclet entries for all known
1256 UnwindDests.emplace_back(FuncInfo.MBBMap[EHPadBB], Prob);
1257 UnwindDests.back().first->setIsEHFuncletEntry();
1259 } else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Pad)) {
1260 // Add the catchpad handlers to the possible destinations.
1261 for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) {
1262 UnwindDests.emplace_back(FuncInfo.MBBMap[CatchPadBB], Prob);
1263 // For MSVC++ and the CLR, catchblocks are funclets and need prologues.
1264 if (IsMSVCCXX || IsCoreCLR)
1265 UnwindDests.back().first->setIsEHFuncletEntry();
1267 NewEHPadBB = CatchSwitch->getUnwindDest();
1272 BranchProbabilityInfo *BPI = FuncInfo.BPI;
1273 if (BPI && NewEHPadBB)
1274 Prob *= BPI->getEdgeProbability(EHPadBB, NewEHPadBB);
1275 EHPadBB = NewEHPadBB;
1279 void SelectionDAGBuilder::visitCleanupRet(const CleanupReturnInst &I) {
1280 // Update successor info.
1281 SmallVector<std::pair<MachineBasicBlock *, BranchProbability>, 1> UnwindDests;
1282 auto UnwindDest = I.getUnwindDest();
1283 BranchProbabilityInfo *BPI = FuncInfo.BPI;
1284 BranchProbability UnwindDestProb =
1286 ? BPI->getEdgeProbability(FuncInfo.MBB->getBasicBlock(), UnwindDest)
1287 : BranchProbability::getZero();
1288 findUnwindDestinations(FuncInfo, UnwindDest, UnwindDestProb, UnwindDests);
1289 for (auto &UnwindDest : UnwindDests) {
1290 UnwindDest.first->setIsEHPad();
1291 addSuccessorWithProb(FuncInfo.MBB, UnwindDest.first, UnwindDest.second);
1293 FuncInfo.MBB->normalizeSuccProbs();
1295 // Create the terminator node.
1297 DAG.getNode(ISD::CLEANUPRET, getCurSDLoc(), MVT::Other, getControlRoot());
1301 void SelectionDAGBuilder::visitCatchSwitch(const CatchSwitchInst &CSI) {
1302 report_fatal_error("visitCatchSwitch not yet implemented!");
1305 void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
1306 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1307 auto &DL = DAG.getDataLayout();
1308 SDValue Chain = getControlRoot();
1309 SmallVector<ISD::OutputArg, 8> Outs;
1310 SmallVector<SDValue, 8> OutVals;
1312 if (!FuncInfo.CanLowerReturn) {
1313 unsigned DemoteReg = FuncInfo.DemoteRegister;
1314 const Function *F = I.getParent()->getParent();
1316 // Emit a store of the return value through the virtual register.
1317 // Leave Outs empty so that LowerReturn won't try to load return
1318 // registers the usual way.
1319 SmallVector<EVT, 1> PtrValueVTs;
1320 ComputeValueVTs(TLI, DL, PointerType::getUnqual(F->getReturnType()),
1323 SDValue RetPtr = DAG.getCopyFromReg(DAG.getEntryNode(), getCurSDLoc(),
1324 DemoteReg, PtrValueVTs[0]);
1325 SDValue RetOp = getValue(I.getOperand(0));
1327 SmallVector<EVT, 4> ValueVTs;
1328 SmallVector<uint64_t, 4> Offsets;
1329 ComputeValueVTs(TLI, DL, I.getOperand(0)->getType(), ValueVTs, &Offsets);
1330 unsigned NumValues = ValueVTs.size();
1332 SmallVector<SDValue, 4> Chains(NumValues);
1333 for (unsigned i = 0; i != NumValues; ++i) {
1334 SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(),
1335 RetPtr.getValueType(), RetPtr,
1336 DAG.getIntPtrConstant(Offsets[i],
1339 DAG.getStore(Chain, getCurSDLoc(),
1340 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
1341 // FIXME: better loc info would be nice.
1342 Add, MachinePointerInfo(), false, false, 0);
1345 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
1346 MVT::Other, Chains);
1347 } else if (I.getNumOperands() != 0) {
1348 SmallVector<EVT, 4> ValueVTs;
1349 ComputeValueVTs(TLI, DL, I.getOperand(0)->getType(), ValueVTs);
1350 unsigned NumValues = ValueVTs.size();
1352 SDValue RetOp = getValue(I.getOperand(0));
1354 const Function *F = I.getParent()->getParent();
1356 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
1357 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
1359 ExtendKind = ISD::SIGN_EXTEND;
1360 else if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
1362 ExtendKind = ISD::ZERO_EXTEND;
1364 LLVMContext &Context = F->getContext();
1365 bool RetInReg = F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
1368 for (unsigned j = 0; j != NumValues; ++j) {
1369 EVT VT = ValueVTs[j];
1371 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger())
1372 VT = TLI.getTypeForExtArgOrReturn(Context, VT, ExtendKind);
1374 unsigned NumParts = TLI.getNumRegisters(Context, VT);
1375 MVT PartVT = TLI.getRegisterType(Context, VT);
1376 SmallVector<SDValue, 4> Parts(NumParts);
1377 getCopyToParts(DAG, getCurSDLoc(),
1378 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
1379 &Parts[0], NumParts, PartVT, &I, ExtendKind);
1381 // 'inreg' on function refers to return value
1382 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
1386 // Propagate extension type if any
1387 if (ExtendKind == ISD::SIGN_EXTEND)
1389 else if (ExtendKind == ISD::ZERO_EXTEND)
1392 for (unsigned i = 0; i < NumParts; ++i) {
1393 Outs.push_back(ISD::OutputArg(Flags, Parts[i].getValueType(),
1394 VT, /*isfixed=*/true, 0, 0));
1395 OutVals.push_back(Parts[i]);
1401 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
1402 CallingConv::ID CallConv =
1403 DAG.getMachineFunction().getFunction()->getCallingConv();
1404 Chain = DAG.getTargetLoweringInfo().LowerReturn(
1405 Chain, CallConv, isVarArg, Outs, OutVals, getCurSDLoc(), DAG);
1407 // Verify that the target's LowerReturn behaved as expected.
1408 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
1409 "LowerReturn didn't return a valid chain!");
1411 // Update the DAG with the new chain value resulting from return lowering.
1415 /// CopyToExportRegsIfNeeded - If the given value has virtual registers
1416 /// created for it, emit nodes to copy the value into the virtual
1418 void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) {
1420 if (V->getType()->isEmptyTy())
1423 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
1424 if (VMI != FuncInfo.ValueMap.end()) {
1425 assert(!V->use_empty() && "Unused value assigned virtual registers!");
1426 CopyValueToVirtualRegister(V, VMI->second);
1430 /// ExportFromCurrentBlock - If this condition isn't known to be exported from
1431 /// the current basic block, add it to ValueMap now so that we'll get a
1433 void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) {
1434 // No need to export constants.
1435 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
1437 // Already exported?
1438 if (FuncInfo.isExportedInst(V)) return;
1440 unsigned Reg = FuncInfo.InitializeRegForValue(V);
1441 CopyValueToVirtualRegister(V, Reg);
1444 bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V,
1445 const BasicBlock *FromBB) {
1446 // The operands of the setcc have to be in this block. We don't know
1447 // how to export them from some other block.
1448 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
1449 // Can export from current BB.
1450 if (VI->getParent() == FromBB)
1453 // Is already exported, noop.
1454 return FuncInfo.isExportedInst(V);
1457 // If this is an argument, we can export it if the BB is the entry block or
1458 // if it is already exported.
1459 if (isa<Argument>(V)) {
1460 if (FromBB == &FromBB->getParent()->getEntryBlock())
1463 // Otherwise, can only export this if it is already exported.
1464 return FuncInfo.isExportedInst(V);
1467 // Otherwise, constants can always be exported.
1471 /// Return branch probability calculated by BranchProbabilityInfo for IR blocks.
1473 SelectionDAGBuilder::getEdgeProbability(const MachineBasicBlock *Src,
1474 const MachineBasicBlock *Dst) const {
1475 BranchProbabilityInfo *BPI = FuncInfo.BPI;
1476 const BasicBlock *SrcBB = Src->getBasicBlock();
1477 const BasicBlock *DstBB = Dst->getBasicBlock();
1479 // If BPI is not available, set the default probability as 1 / N, where N is
1480 // the number of successors.
1481 auto SuccSize = std::max<uint32_t>(
1482 std::distance(succ_begin(SrcBB), succ_end(SrcBB)), 1);
1483 return BranchProbability(1, SuccSize);
1485 return BPI->getEdgeProbability(SrcBB, DstBB);
1488 void SelectionDAGBuilder::addSuccessorWithProb(MachineBasicBlock *Src,
1489 MachineBasicBlock *Dst,
1490 BranchProbability Prob) {
1492 Src->addSuccessorWithoutProb(Dst);
1494 if (Prob.isUnknown())
1495 Prob = getEdgeProbability(Src, Dst);
1496 Src->addSuccessor(Dst, Prob);
1500 static bool InBlock(const Value *V, const BasicBlock *BB) {
1501 if (const Instruction *I = dyn_cast<Instruction>(V))
1502 return I->getParent() == BB;
1506 /// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
1507 /// This function emits a branch and is used at the leaves of an OR or an
1508 /// AND operator tree.
1511 SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond,
1512 MachineBasicBlock *TBB,
1513 MachineBasicBlock *FBB,
1514 MachineBasicBlock *CurBB,
1515 MachineBasicBlock *SwitchBB,
1516 BranchProbability TProb,
1517 BranchProbability FProb) {
1518 const BasicBlock *BB = CurBB->getBasicBlock();
1520 // If the leaf of the tree is a comparison, merge the condition into
1522 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
1523 // The operands of the cmp have to be in this block. We don't know
1524 // how to export them from some other block. If this is the first block
1525 // of the sequence, no exporting is needed.
1526 if (CurBB == SwitchBB ||
1527 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1528 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
1529 ISD::CondCode Condition;
1530 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
1531 Condition = getICmpCondCode(IC->getPredicate());
1533 const FCmpInst *FC = cast<FCmpInst>(Cond);
1534 Condition = getFCmpCondCode(FC->getPredicate());
1535 if (TM.Options.NoNaNsFPMath)
1536 Condition = getFCmpCodeWithoutNaN(Condition);
1539 CaseBlock CB(Condition, BOp->getOperand(0), BOp->getOperand(1), nullptr,
1540 TBB, FBB, CurBB, TProb, FProb);
1541 SwitchCases.push_back(CB);
1546 // Create a CaseBlock record representing this branch.
1547 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
1548 nullptr, TBB, FBB, CurBB, TProb, FProb);
1549 SwitchCases.push_back(CB);
1552 /// FindMergedConditions - If Cond is an expression like
1553 void SelectionDAGBuilder::FindMergedConditions(const Value *Cond,
1554 MachineBasicBlock *TBB,
1555 MachineBasicBlock *FBB,
1556 MachineBasicBlock *CurBB,
1557 MachineBasicBlock *SwitchBB,
1558 Instruction::BinaryOps Opc,
1559 BranchProbability TProb,
1560 BranchProbability FProb) {
1561 // If this node is not part of the or/and tree, emit it as a branch.
1562 const Instruction *BOp = dyn_cast<Instruction>(Cond);
1563 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
1564 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1565 BOp->getParent() != CurBB->getBasicBlock() ||
1566 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1567 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
1568 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB,
1573 // Create TmpBB after CurBB.
1574 MachineFunction::iterator BBI(CurBB);
1575 MachineFunction &MF = DAG.getMachineFunction();
1576 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1577 CurBB->getParent()->insert(++BBI, TmpBB);
1579 if (Opc == Instruction::Or) {
1580 // Codegen X | Y as:
1589 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
1590 // The requirement is that
1591 // TrueProb for BB1 + (FalseProb for BB1 * TrueProb for TmpBB)
1592 // = TrueProb for original BB.
1593 // Assuming the original probabilities are A and B, one choice is to set
1594 // BB1's probabilities to A/2 and A/2+B, and set TmpBB's probabilities to
1595 // A/(1+B) and 2B/(1+B). This choice assumes that
1596 // TrueProb for BB1 == FalseProb for BB1 * TrueProb for TmpBB.
1597 // Another choice is to assume TrueProb for BB1 equals to TrueProb for
1598 // TmpBB, but the math is more complicated.
1600 auto NewTrueProb = TProb / 2;
1601 auto NewFalseProb = TProb / 2 + FProb;
1602 // Emit the LHS condition.
1603 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc,
1604 NewTrueProb, NewFalseProb);
1606 // Normalize A/2 and B to get A/(1+B) and 2B/(1+B).
1607 SmallVector<BranchProbability, 2> Probs{TProb / 2, FProb};
1608 BranchProbability::normalizeProbabilities(Probs.begin(), Probs.end());
1609 // Emit the RHS condition into TmpBB.
1610 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc,
1611 Probs[0], Probs[1]);
1613 assert(Opc == Instruction::And && "Unknown merge op!");
1614 // Codegen X & Y as:
1622 // This requires creation of TmpBB after CurBB.
1624 // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
1625 // The requirement is that
1626 // FalseProb for BB1 + (TrueProb for BB1 * FalseProb for TmpBB)
1627 // = FalseProb for original BB.
1628 // Assuming the original probabilities are A and B, one choice is to set
1629 // BB1's probabilities to A+B/2 and B/2, and set TmpBB's probabilities to
1630 // 2A/(1+A) and B/(1+A). This choice assumes that FalseProb for BB1 ==
1631 // TrueProb for BB1 * FalseProb for TmpBB.
1633 auto NewTrueProb = TProb + FProb / 2;
1634 auto NewFalseProb = FProb / 2;
1635 // Emit the LHS condition.
1636 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc,
1637 NewTrueProb, NewFalseProb);
1639 // Normalize A and B/2 to get 2A/(1+A) and B/(1+A).
1640 SmallVector<BranchProbability, 2> Probs{TProb, FProb / 2};
1641 BranchProbability::normalizeProbabilities(Probs.begin(), Probs.end());
1642 // Emit the RHS condition into TmpBB.
1643 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc,
1644 Probs[0], Probs[1]);
1648 /// If the set of cases should be emitted as a series of branches, return true.
1649 /// If we should emit this as a bunch of and/or'd together conditions, return
1652 SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases) {
1653 if (Cases.size() != 2) return true;
1655 // If this is two comparisons of the same values or'd or and'd together, they
1656 // will get folded into a single comparison, so don't emit two blocks.
1657 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1658 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1659 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1660 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1664 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
1665 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
1666 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
1667 Cases[0].CC == Cases[1].CC &&
1668 isa<Constant>(Cases[0].CmpRHS) &&
1669 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
1670 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
1672 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
1679 void SelectionDAGBuilder::visitBr(const BranchInst &I) {
1680 MachineBasicBlock *BrMBB = FuncInfo.MBB;
1682 // Update machine-CFG edges.
1683 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1685 if (I.isUnconditional()) {
1686 // Update machine-CFG edges.
1687 BrMBB->addSuccessor(Succ0MBB);
1689 // If this is not a fall-through branch or optimizations are switched off,
1691 if (Succ0MBB != NextBlock(BrMBB) || TM.getOptLevel() == CodeGenOpt::None)
1692 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
1693 MVT::Other, getControlRoot(),
1694 DAG.getBasicBlock(Succ0MBB)));
1699 // If this condition is one of the special cases we handle, do special stuff
1701 const Value *CondVal = I.getCondition();
1702 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1704 // If this is a series of conditions that are or'd or and'd together, emit
1705 // this as a sequence of branches instead of setcc's with and/or operations.
1706 // As long as jumps are not expensive, this should improve performance.
1707 // For example, instead of something like:
1720 if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
1721 Instruction::BinaryOps Opcode = BOp->getOpcode();
1722 if (!DAG.getTargetLoweringInfo().isJumpExpensive() && BOp->hasOneUse() &&
1723 !I.getMetadata(LLVMContext::MD_unpredictable) &&
1724 (Opcode == Instruction::And || Opcode == Instruction::Or)) {
1725 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB,
1727 getEdgeProbability(BrMBB, Succ0MBB),
1728 getEdgeProbability(BrMBB, Succ1MBB));
1729 // If the compares in later blocks need to use values not currently
1730 // exported from this block, export them now. This block should always
1731 // be the first entry.
1732 assert(SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
1734 // Allow some cases to be rejected.
1735 if (ShouldEmitAsBranches(SwitchCases)) {
1736 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1737 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1738 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1741 // Emit the branch for this block.
1742 visitSwitchCase(SwitchCases[0], BrMBB);
1743 SwitchCases.erase(SwitchCases.begin());
1747 // Okay, we decided not to do this, remove any inserted MBB's and clear
1749 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
1750 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
1752 SwitchCases.clear();
1756 // Create a CaseBlock record representing this branch.
1757 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
1758 nullptr, Succ0MBB, Succ1MBB, BrMBB);
1760 // Use visitSwitchCase to actually insert the fast branch sequence for this
1762 visitSwitchCase(CB, BrMBB);
1765 /// visitSwitchCase - Emits the necessary code to represent a single node in
1766 /// the binary search tree resulting from lowering a switch instruction.
1767 void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB,
1768 MachineBasicBlock *SwitchBB) {
1770 SDValue CondLHS = getValue(CB.CmpLHS);
1771 SDLoc dl = getCurSDLoc();
1773 // Build the setcc now.
1775 // Fold "(X == true)" to X and "(X == false)" to !X to
1776 // handle common cases produced by branch lowering.
1777 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
1778 CB.CC == ISD::SETEQ)
1780 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
1781 CB.CC == ISD::SETEQ) {
1782 SDValue True = DAG.getConstant(1, dl, CondLHS.getValueType());
1783 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
1785 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
1787 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1789 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1790 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
1792 SDValue CmpOp = getValue(CB.CmpMHS);
1793 EVT VT = CmpOp.getValueType();
1795 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
1796 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, dl, VT),
1799 SDValue SUB = DAG.getNode(ISD::SUB, dl,
1800 VT, CmpOp, DAG.getConstant(Low, dl, VT));
1801 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
1802 DAG.getConstant(High-Low, dl, VT), ISD::SETULE);
1806 // Update successor info
1807 addSuccessorWithProb(SwitchBB, CB.TrueBB, CB.TrueProb);
1808 // TrueBB and FalseBB are always different unless the incoming IR is
1809 // degenerate. This only happens when running llc on weird IR.
1810 if (CB.TrueBB != CB.FalseBB)
1811 addSuccessorWithProb(SwitchBB, CB.FalseBB, CB.FalseProb);
1812 SwitchBB->normalizeSuccProbs();
1814 // If the lhs block is the next block, invert the condition so that we can
1815 // fall through to the lhs instead of the rhs block.
1816 if (CB.TrueBB == NextBlock(SwitchBB)) {
1817 std::swap(CB.TrueBB, CB.FalseBB);
1818 SDValue True = DAG.getConstant(1, dl, Cond.getValueType());
1819 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
1822 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
1823 MVT::Other, getControlRoot(), Cond,
1824 DAG.getBasicBlock(CB.TrueBB));
1826 // Insert the false branch. Do this even if it's a fall through branch,
1827 // this makes it easier to do DAG optimizations which require inverting
1828 // the branch condition.
1829 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1830 DAG.getBasicBlock(CB.FalseBB));
1832 DAG.setRoot(BrCond);
1835 /// visitJumpTable - Emit JumpTable node in the current MBB
1836 void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
1837 // Emit the code for the jump table
1838 assert(JT.Reg != -1U && "Should lower JT Header first!");
1839 EVT PTy = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
1840 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurSDLoc(),
1842 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
1843 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurSDLoc(),
1844 MVT::Other, Index.getValue(1),
1846 DAG.setRoot(BrJumpTable);
1849 /// visitJumpTableHeader - This function emits necessary code to produce index
1850 /// in the JumpTable from switch case.
1851 void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
1852 JumpTableHeader &JTH,
1853 MachineBasicBlock *SwitchBB) {
1854 SDLoc dl = getCurSDLoc();
1856 // Subtract the lowest switch case value from the value being switched on and
1857 // conditional branch to default mbb if the result is greater than the
1858 // difference between smallest and largest cases.
1859 SDValue SwitchOp = getValue(JTH.SValue);
1860 EVT VT = SwitchOp.getValueType();
1861 SDValue Sub = DAG.getNode(ISD::SUB, dl, VT, SwitchOp,
1862 DAG.getConstant(JTH.First, dl, VT));
1864 // The SDNode we just created, which holds the value being switched on minus
1865 // the smallest case value, needs to be copied to a virtual register so it
1866 // can be used as an index into the jump table in a subsequent basic block.
1867 // This value may be smaller or larger than the target's pointer type, and
1868 // therefore require extension or truncating.
1869 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1870 SwitchOp = DAG.getZExtOrTrunc(Sub, dl, TLI.getPointerTy(DAG.getDataLayout()));
1872 unsigned JumpTableReg =
1873 FuncInfo.CreateReg(TLI.getPointerTy(DAG.getDataLayout()));
1874 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), dl,
1875 JumpTableReg, SwitchOp);
1876 JT.Reg = JumpTableReg;
1878 // Emit the range check for the jump table, and branch to the default block
1879 // for the switch statement if the value being switched on exceeds the largest
1880 // case in the switch.
1881 SDValue CMP = DAG.getSetCC(
1882 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
1883 Sub.getValueType()),
1884 Sub, DAG.getConstant(JTH.Last - JTH.First, dl, VT), ISD::SETUGT);
1886 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
1887 MVT::Other, CopyTo, CMP,
1888 DAG.getBasicBlock(JT.Default));
1890 // Avoid emitting unnecessary branches to the next block.
1891 if (JT.MBB != NextBlock(SwitchBB))
1892 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1893 DAG.getBasicBlock(JT.MBB));
1895 DAG.setRoot(BrCond);
1898 /// Codegen a new tail for a stack protector check ParentMBB which has had its
1899 /// tail spliced into a stack protector check success bb.
1901 /// For a high level explanation of how this fits into the stack protector
1902 /// generation see the comment on the declaration of class
1903 /// StackProtectorDescriptor.
1904 void SelectionDAGBuilder::visitSPDescriptorParent(StackProtectorDescriptor &SPD,
1905 MachineBasicBlock *ParentBB) {
1907 // First create the loads to the guard/stack slot for the comparison.
1908 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1909 EVT PtrTy = TLI.getPointerTy(DAG.getDataLayout());
1911 MachineFrameInfo *MFI = ParentBB->getParent()->getFrameInfo();
1912 int FI = MFI->getStackProtectorIndex();
1914 const Value *IRGuard = SPD.getGuard();
1915 SDValue GuardPtr = getValue(IRGuard);
1916 SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
1918 unsigned Align = DL->getPrefTypeAlignment(IRGuard->getType());
1921 SDLoc dl = getCurSDLoc();
1923 // If GuardReg is set and useLoadStackGuardNode returns true, retrieve the
1924 // guard value from the virtual register holding the value. Otherwise, emit a
1925 // volatile load to retrieve the stack guard value.
1926 unsigned GuardReg = SPD.getGuardReg();
1928 if (GuardReg && TLI.useLoadStackGuardNode())
1929 Guard = DAG.getCopyFromReg(DAG.getEntryNode(), dl, GuardReg,
1932 Guard = DAG.getLoad(PtrTy, dl, DAG.getEntryNode(),
1933 GuardPtr, MachinePointerInfo(IRGuard, 0),
1934 true, false, false, Align);
1936 SDValue StackSlot = DAG.getLoad(
1937 PtrTy, dl, DAG.getEntryNode(), StackSlotPtr,
1938 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), true,
1939 false, false, Align);
1941 // Perform the comparison via a subtract/getsetcc.
1942 EVT VT = Guard.getValueType();
1943 SDValue Sub = DAG.getNode(ISD::SUB, dl, VT, Guard, StackSlot);
1945 SDValue Cmp = DAG.getSetCC(dl, TLI.getSetCCResultType(DAG.getDataLayout(),
1947 Sub.getValueType()),
1948 Sub, DAG.getConstant(0, dl, VT), ISD::SETNE);
1950 // If the sub is not 0, then we know the guard/stackslot do not equal, so
1951 // branch to failure MBB.
1952 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
1953 MVT::Other, StackSlot.getOperand(0),
1954 Cmp, DAG.getBasicBlock(SPD.getFailureMBB()));
1955 // Otherwise branch to success MBB.
1956 SDValue Br = DAG.getNode(ISD::BR, dl,
1958 DAG.getBasicBlock(SPD.getSuccessMBB()));
1963 /// Codegen the failure basic block for a stack protector check.
1965 /// A failure stack protector machine basic block consists simply of a call to
1966 /// __stack_chk_fail().
1968 /// For a high level explanation of how this fits into the stack protector
1969 /// generation see the comment on the declaration of class
1970 /// StackProtectorDescriptor.
1972 SelectionDAGBuilder::visitSPDescriptorFailure(StackProtectorDescriptor &SPD) {
1973 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1975 TLI.makeLibCall(DAG, RTLIB::STACKPROTECTOR_CHECK_FAIL, MVT::isVoid,
1976 None, false, getCurSDLoc(), false, false).second;
1980 /// visitBitTestHeader - This function emits necessary code to produce value
1981 /// suitable for "bit tests"
1982 void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B,
1983 MachineBasicBlock *SwitchBB) {
1984 SDLoc dl = getCurSDLoc();
1986 // Subtract the minimum value
1987 SDValue SwitchOp = getValue(B.SValue);
1988 EVT VT = SwitchOp.getValueType();
1989 SDValue Sub = DAG.getNode(ISD::SUB, dl, VT, SwitchOp,
1990 DAG.getConstant(B.First, dl, VT));
1993 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1994 SDValue RangeCmp = DAG.getSetCC(
1995 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
1996 Sub.getValueType()),
1997 Sub, DAG.getConstant(B.Range, dl, VT), ISD::SETUGT);
1999 // Determine the type of the test operands.
2000 bool UsePtrType = false;
2001 if (!TLI.isTypeLegal(VT))
2004 for (unsigned i = 0, e = B.Cases.size(); i != e; ++i)
2005 if (!isUIntN(VT.getSizeInBits(), B.Cases[i].Mask)) {
2006 // Switch table case range are encoded into series of masks.
2007 // Just use pointer type, it's guaranteed to fit.
2013 VT = TLI.getPointerTy(DAG.getDataLayout());
2014 Sub = DAG.getZExtOrTrunc(Sub, dl, VT);
2017 B.RegVT = VT.getSimpleVT();
2018 B.Reg = FuncInfo.CreateReg(B.RegVT);
2019 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), dl, B.Reg, Sub);
2021 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
2023 addSuccessorWithProb(SwitchBB, B.Default, B.DefaultProb);
2024 addSuccessorWithProb(SwitchBB, MBB, B.Prob);
2025 SwitchBB->normalizeSuccProbs();
2027 SDValue BrRange = DAG.getNode(ISD::BRCOND, dl,
2028 MVT::Other, CopyTo, RangeCmp,
2029 DAG.getBasicBlock(B.Default));
2031 // Avoid emitting unnecessary branches to the next block.
2032 if (MBB != NextBlock(SwitchBB))
2033 BrRange = DAG.getNode(ISD::BR, dl, MVT::Other, BrRange,
2034 DAG.getBasicBlock(MBB));
2036 DAG.setRoot(BrRange);
2039 /// visitBitTestCase - this function produces one "bit test"
2040 void SelectionDAGBuilder::visitBitTestCase(BitTestBlock &BB,
2041 MachineBasicBlock* NextMBB,
2042 BranchProbability BranchProbToNext,
2045 MachineBasicBlock *SwitchBB) {
2046 SDLoc dl = getCurSDLoc();
2048 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), dl, Reg, VT);
2050 unsigned PopCount = countPopulation(B.Mask);
2051 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2052 if (PopCount == 1) {
2053 // Testing for a single bit; just compare the shift count with what it
2054 // would need to be to shift a 1 bit in that position.
2056 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
2057 ShiftOp, DAG.getConstant(countTrailingZeros(B.Mask), dl, VT),
2059 } else if (PopCount == BB.Range) {
2060 // There is only one zero bit in the range, test for it directly.
2062 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
2063 ShiftOp, DAG.getConstant(countTrailingOnes(B.Mask), dl, VT),
2066 // Make desired shift
2067 SDValue SwitchVal = DAG.getNode(ISD::SHL, dl, VT,
2068 DAG.getConstant(1, dl, VT), ShiftOp);
2070 // Emit bit tests and jumps
2071 SDValue AndOp = DAG.getNode(ISD::AND, dl,
2072 VT, SwitchVal, DAG.getConstant(B.Mask, dl, VT));
2074 dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
2075 AndOp, DAG.getConstant(0, dl, VT), ISD::SETNE);
2078 // The branch probability from SwitchBB to B.TargetBB is B.ExtraProb.
2079 addSuccessorWithProb(SwitchBB, B.TargetBB, B.ExtraProb);
2080 // The branch probability from SwitchBB to NextMBB is BranchProbToNext.
2081 addSuccessorWithProb(SwitchBB, NextMBB, BranchProbToNext);
2082 // It is not guaranteed that the sum of B.ExtraProb and BranchProbToNext is
2083 // one as they are relative probabilities (and thus work more like weights),
2084 // and hence we need to normalize them to let the sum of them become one.
2085 SwitchBB->normalizeSuccProbs();
2087 SDValue BrAnd = DAG.getNode(ISD::BRCOND, dl,
2088 MVT::Other, getControlRoot(),
2089 Cmp, DAG.getBasicBlock(B.TargetBB));
2091 // Avoid emitting unnecessary branches to the next block.
2092 if (NextMBB != NextBlock(SwitchBB))
2093 BrAnd = DAG.getNode(ISD::BR, dl, MVT::Other, BrAnd,
2094 DAG.getBasicBlock(NextMBB));
2099 void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
2100 MachineBasicBlock *InvokeMBB = FuncInfo.MBB;
2102 // Retrieve successors. Look through artificial IR level blocks like
2103 // catchswitch for successors.
2104 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
2105 const BasicBlock *EHPadBB = I.getSuccessor(1);
2107 const Value *Callee(I.getCalledValue());
2108 const Function *Fn = dyn_cast<Function>(Callee);
2109 if (isa<InlineAsm>(Callee))
2111 else if (Fn && Fn->isIntrinsic()) {
2112 switch (Fn->getIntrinsicID()) {
2114 llvm_unreachable("Cannot invoke this intrinsic");
2115 case Intrinsic::donothing:
2116 // Ignore invokes to @llvm.donothing: jump directly to the next BB.
2118 case Intrinsic::experimental_patchpoint_void:
2119 case Intrinsic::experimental_patchpoint_i64:
2120 visitPatchpoint(&I, EHPadBB);
2122 case Intrinsic::experimental_gc_statepoint:
2123 LowerStatepoint(ImmutableStatepoint(&I), EHPadBB);
2127 LowerCallTo(&I, getValue(Callee), false, EHPadBB);
2129 // If the value of the invoke is used outside of its defining block, make it
2130 // available as a virtual register.
2131 // We already took care of the exported value for the statepoint instruction
2132 // during call to the LowerStatepoint.
2133 if (!isStatepoint(I)) {
2134 CopyToExportRegsIfNeeded(&I);
2137 SmallVector<std::pair<MachineBasicBlock *, BranchProbability>, 1> UnwindDests;
2138 BranchProbabilityInfo *BPI = FuncInfo.BPI;
2139 BranchProbability EHPadBBProb =
2140 BPI ? BPI->getEdgeProbability(InvokeMBB->getBasicBlock(), EHPadBB)
2141 : BranchProbability::getZero();
2142 findUnwindDestinations(FuncInfo, EHPadBB, EHPadBBProb, UnwindDests);
2144 // Update successor info.
2145 addSuccessorWithProb(InvokeMBB, Return);
2146 for (auto &UnwindDest : UnwindDests) {
2147 UnwindDest.first->setIsEHPad();
2148 addSuccessorWithProb(InvokeMBB, UnwindDest.first, UnwindDest.second);
2150 InvokeMBB->normalizeSuccProbs();
2152 // Drop into normal successor.
2153 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
2154 MVT::Other, getControlRoot(),
2155 DAG.getBasicBlock(Return)));
2158 void SelectionDAGBuilder::visitResume(const ResumeInst &RI) {
2159 llvm_unreachable("SelectionDAGBuilder shouldn't visit resume instructions!");
2162 void SelectionDAGBuilder::visitLandingPad(const LandingPadInst &LP) {
2163 assert(FuncInfo.MBB->isEHPad() &&
2164 "Call to landingpad not in landing pad!");
2166 MachineBasicBlock *MBB = FuncInfo.MBB;
2167 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
2168 AddLandingPadInfo(LP, MMI, MBB);
2170 // If there aren't registers to copy the values into (e.g., during SjLj
2171 // exceptions), then don't bother to create these DAG nodes.
2172 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2173 const Constant *PersonalityFn = FuncInfo.Fn->getPersonalityFn();
2174 if (TLI.getExceptionPointerRegister(PersonalityFn) == 0 &&
2175 TLI.getExceptionSelectorRegister(PersonalityFn) == 0)
2178 // If landingpad's return type is token type, we don't create DAG nodes
2179 // for its exception pointer and selector value. The extraction of exception
2180 // pointer or selector value from token type landingpads is not currently
2182 if (LP.getType()->isTokenTy())
2185 SmallVector<EVT, 2> ValueVTs;
2186 SDLoc dl = getCurSDLoc();
2187 ComputeValueVTs(TLI, DAG.getDataLayout(), LP.getType(), ValueVTs);
2188 assert(ValueVTs.size() == 2 && "Only two-valued landingpads are supported");
2190 // Get the two live-in registers as SDValues. The physregs have already been
2191 // copied into virtual registers.
2193 if (FuncInfo.ExceptionPointerVirtReg) {
2194 Ops[0] = DAG.getZExtOrTrunc(
2195 DAG.getCopyFromReg(DAG.getEntryNode(), dl,
2196 FuncInfo.ExceptionPointerVirtReg,
2197 TLI.getPointerTy(DAG.getDataLayout())),
2200 Ops[0] = DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout()));
2202 Ops[1] = DAG.getZExtOrTrunc(
2203 DAG.getCopyFromReg(DAG.getEntryNode(), dl,
2204 FuncInfo.ExceptionSelectorVirtReg,
2205 TLI.getPointerTy(DAG.getDataLayout())),
2209 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
2210 DAG.getVTList(ValueVTs), Ops);
2214 void SelectionDAGBuilder::sortAndRangeify(CaseClusterVector &Clusters) {
2216 for (const CaseCluster &CC : Clusters)
2217 assert(CC.Low == CC.High && "Input clusters must be single-case");
2220 std::sort(Clusters.begin(), Clusters.end(),
2221 [](const CaseCluster &a, const CaseCluster &b) {
2222 return a.Low->getValue().slt(b.Low->getValue());
2225 // Merge adjacent clusters with the same destination.
2226 const unsigned N = Clusters.size();
2227 unsigned DstIndex = 0;
2228 for (unsigned SrcIndex = 0; SrcIndex < N; ++SrcIndex) {
2229 CaseCluster &CC = Clusters[SrcIndex];
2230 const ConstantInt *CaseVal = CC.Low;
2231 MachineBasicBlock *Succ = CC.MBB;
2233 if (DstIndex != 0 && Clusters[DstIndex - 1].MBB == Succ &&
2234 (CaseVal->getValue() - Clusters[DstIndex - 1].High->getValue()) == 1) {
2235 // If this case has the same successor and is a neighbour, merge it into
2236 // the previous cluster.
2237 Clusters[DstIndex - 1].High = CaseVal;
2238 Clusters[DstIndex - 1].Prob += CC.Prob;
2240 std::memmove(&Clusters[DstIndex++], &Clusters[SrcIndex],
2241 sizeof(Clusters[SrcIndex]));
2244 Clusters.resize(DstIndex);
2247 void SelectionDAGBuilder::UpdateSplitBlock(MachineBasicBlock *First,
2248 MachineBasicBlock *Last) {
2250 for (unsigned i = 0, e = JTCases.size(); i != e; ++i)
2251 if (JTCases[i].first.HeaderBB == First)
2252 JTCases[i].first.HeaderBB = Last;
2254 // Update BitTestCases.
2255 for (unsigned i = 0, e = BitTestCases.size(); i != e; ++i)
2256 if (BitTestCases[i].Parent == First)
2257 BitTestCases[i].Parent = Last;
2260 void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
2261 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB;
2263 // Update machine-CFG edges with unique successors.
2264 SmallSet<BasicBlock*, 32> Done;
2265 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) {
2266 BasicBlock *BB = I.getSuccessor(i);
2267 bool Inserted = Done.insert(BB).second;
2271 MachineBasicBlock *Succ = FuncInfo.MBBMap[BB];
2272 addSuccessorWithProb(IndirectBrMBB, Succ);
2274 IndirectBrMBB->normalizeSuccProbs();
2276 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurSDLoc(),
2277 MVT::Other, getControlRoot(),
2278 getValue(I.getAddress())));
2281 void SelectionDAGBuilder::visitUnreachable(const UnreachableInst &I) {
2282 if (DAG.getTarget().Options.TrapUnreachable)
2284 DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, DAG.getRoot()));
2287 void SelectionDAGBuilder::visitFSub(const User &I) {
2288 // -0.0 - X --> fneg
2289 Type *Ty = I.getType();
2290 if (isa<Constant>(I.getOperand(0)) &&
2291 I.getOperand(0) == ConstantFP::getZeroValueForNegation(Ty)) {
2292 SDValue Op2 = getValue(I.getOperand(1));
2293 setValue(&I, DAG.getNode(ISD::FNEG, getCurSDLoc(),
2294 Op2.getValueType(), Op2));
2298 visitBinary(I, ISD::FSUB);
2301 void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) {
2302 SDValue Op1 = getValue(I.getOperand(0));
2303 SDValue Op2 = getValue(I.getOperand(1));
2310 if (const OverflowingBinaryOperator *OFBinOp =
2311 dyn_cast<const OverflowingBinaryOperator>(&I)) {
2312 nuw = OFBinOp->hasNoUnsignedWrap();
2313 nsw = OFBinOp->hasNoSignedWrap();
2315 if (const PossiblyExactOperator *ExactOp =
2316 dyn_cast<const PossiblyExactOperator>(&I))
2317 exact = ExactOp->isExact();
2318 if (const FPMathOperator *FPOp = dyn_cast<const FPMathOperator>(&I))
2319 FMF = FPOp->getFastMathFlags();
2322 Flags.setExact(exact);
2323 Flags.setNoSignedWrap(nsw);
2324 Flags.setNoUnsignedWrap(nuw);
2325 if (EnableFMFInDAG) {
2326 Flags.setAllowReciprocal(FMF.allowReciprocal());
2327 Flags.setNoInfs(FMF.noInfs());
2328 Flags.setNoNaNs(FMF.noNaNs());
2329 Flags.setNoSignedZeros(FMF.noSignedZeros());
2330 Flags.setUnsafeAlgebra(FMF.unsafeAlgebra());
2332 SDValue BinNodeValue = DAG.getNode(OpCode, getCurSDLoc(), Op1.getValueType(),
2334 setValue(&I, BinNodeValue);
2337 void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
2338 SDValue Op1 = getValue(I.getOperand(0));
2339 SDValue Op2 = getValue(I.getOperand(1));
2341 EVT ShiftTy = DAG.getTargetLoweringInfo().getShiftAmountTy(
2342 Op2.getValueType(), DAG.getDataLayout());
2344 // Coerce the shift amount to the right type if we can.
2345 if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) {
2346 unsigned ShiftSize = ShiftTy.getSizeInBits();
2347 unsigned Op2Size = Op2.getValueType().getSizeInBits();
2348 SDLoc DL = getCurSDLoc();
2350 // If the operand is smaller than the shift count type, promote it.
2351 if (ShiftSize > Op2Size)
2352 Op2 = DAG.getNode(ISD::ZERO_EXTEND, DL, ShiftTy, Op2);
2354 // If the operand is larger than the shift count type but the shift
2355 // count type has enough bits to represent any shift value, truncate
2356 // it now. This is a common case and it exposes the truncate to
2357 // optimization early.
2358 else if (ShiftSize >= Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2359 Op2 = DAG.getNode(ISD::TRUNCATE, DL, ShiftTy, Op2);
2360 // Otherwise we'll need to temporarily settle for some other convenient
2361 // type. Type legalization will make adjustments once the shiftee is split.
2363 Op2 = DAG.getZExtOrTrunc(Op2, DL, MVT::i32);
2370 if (Opcode == ISD::SRL || Opcode == ISD::SRA || Opcode == ISD::SHL) {
2372 if (const OverflowingBinaryOperator *OFBinOp =
2373 dyn_cast<const OverflowingBinaryOperator>(&I)) {
2374 nuw = OFBinOp->hasNoUnsignedWrap();
2375 nsw = OFBinOp->hasNoSignedWrap();
2377 if (const PossiblyExactOperator *ExactOp =
2378 dyn_cast<const PossiblyExactOperator>(&I))
2379 exact = ExactOp->isExact();
2382 Flags.setExact(exact);
2383 Flags.setNoSignedWrap(nsw);
2384 Flags.setNoUnsignedWrap(nuw);
2385 SDValue Res = DAG.getNode(Opcode, getCurSDLoc(), Op1.getValueType(), Op1, Op2,
2390 void SelectionDAGBuilder::visitSDiv(const User &I) {
2391 SDValue Op1 = getValue(I.getOperand(0));
2392 SDValue Op2 = getValue(I.getOperand(1));
2395 Flags.setExact(isa<PossiblyExactOperator>(&I) &&
2396 cast<PossiblyExactOperator>(&I)->isExact());
2397 setValue(&I, DAG.getNode(ISD::SDIV, getCurSDLoc(), Op1.getValueType(), Op1,
2401 void SelectionDAGBuilder::visitICmp(const User &I) {
2402 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
2403 if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I))
2404 predicate = IC->getPredicate();
2405 else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
2406 predicate = ICmpInst::Predicate(IC->getPredicate());
2407 SDValue Op1 = getValue(I.getOperand(0));
2408 SDValue Op2 = getValue(I.getOperand(1));
2409 ISD::CondCode Opcode = getICmpCondCode(predicate);
2411 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
2413 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Opcode));
2416 void SelectionDAGBuilder::visitFCmp(const User &I) {
2417 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
2418 if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I))
2419 predicate = FC->getPredicate();
2420 else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
2421 predicate = FCmpInst::Predicate(FC->getPredicate());
2422 SDValue Op1 = getValue(I.getOperand(0));
2423 SDValue Op2 = getValue(I.getOperand(1));
2424 ISD::CondCode Condition = getFCmpCondCode(predicate);
2426 // FIXME: Fcmp instructions have fast-math-flags in IR, so we should use them.
2427 // FIXME: We should propagate the fast-math-flags to the DAG node itself for
2428 // further optimization, but currently FMF is only applicable to binary nodes.
2429 if (TM.Options.NoNaNsFPMath)
2430 Condition = getFCmpCodeWithoutNaN(Condition);
2431 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
2433 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Condition));
2436 void SelectionDAGBuilder::visitSelect(const User &I) {
2437 SmallVector<EVT, 4> ValueVTs;
2438 ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
2440 unsigned NumValues = ValueVTs.size();
2441 if (NumValues == 0) return;
2443 SmallVector<SDValue, 4> Values(NumValues);
2444 SDValue Cond = getValue(I.getOperand(0));
2445 SDValue LHSVal = getValue(I.getOperand(1));
2446 SDValue RHSVal = getValue(I.getOperand(2));
2447 auto BaseOps = {Cond};
2448 ISD::NodeType OpCode = Cond.getValueType().isVector() ?
2449 ISD::VSELECT : ISD::SELECT;
2451 // Min/max matching is only viable if all output VTs are the same.
2452 if (std::equal(ValueVTs.begin(), ValueVTs.end(), ValueVTs.begin())) {
2453 EVT VT = ValueVTs[0];
2454 LLVMContext &Ctx = *DAG.getContext();
2455 auto &TLI = DAG.getTargetLoweringInfo();
2457 // We care about the legality of the operation after it has been type
2459 while (TLI.getTypeAction(Ctx, VT) != TargetLoweringBase::TypeLegal &&
2460 VT != TLI.getTypeToTransformTo(Ctx, VT))
2461 VT = TLI.getTypeToTransformTo(Ctx, VT);
2463 // If the vselect is legal, assume we want to leave this as a vector setcc +
2464 // vselect. Otherwise, if this is going to be scalarized, we want to see if
2465 // min/max is legal on the scalar type.
2466 bool UseScalarMinMax = VT.isVector() &&
2467 !TLI.isOperationLegalOrCustom(ISD::VSELECT, VT);
2470 auto SPR = matchSelectPattern(const_cast<User*>(&I), LHS, RHS);
2471 ISD::NodeType Opc = ISD::DELETED_NODE;
2472 switch (SPR.Flavor) {
2473 case SPF_UMAX: Opc = ISD::UMAX; break;
2474 case SPF_UMIN: Opc = ISD::UMIN; break;
2475 case SPF_SMAX: Opc = ISD::SMAX; break;
2476 case SPF_SMIN: Opc = ISD::SMIN; break;
2478 switch (SPR.NaNBehavior) {
2479 case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?");
2480 case SPNB_RETURNS_NAN: Opc = ISD::FMINNAN; break;
2481 case SPNB_RETURNS_OTHER: Opc = ISD::FMINNUM; break;
2482 case SPNB_RETURNS_ANY: {
2483 if (TLI.isOperationLegalOrCustom(ISD::FMINNUM, VT))
2485 else if (TLI.isOperationLegalOrCustom(ISD::FMINNAN, VT))
2487 else if (UseScalarMinMax)
2488 Opc = TLI.isOperationLegalOrCustom(ISD::FMINNUM, VT.getScalarType()) ?
2489 ISD::FMINNUM : ISD::FMINNAN;
2495 switch (SPR.NaNBehavior) {
2496 case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?");
2497 case SPNB_RETURNS_NAN: Opc = ISD::FMAXNAN; break;
2498 case SPNB_RETURNS_OTHER: Opc = ISD::FMAXNUM; break;
2499 case SPNB_RETURNS_ANY:
2501 if (TLI.isOperationLegalOrCustom(ISD::FMAXNUM, VT))
2503 else if (TLI.isOperationLegalOrCustom(ISD::FMAXNAN, VT))
2505 else if (UseScalarMinMax)
2506 Opc = TLI.isOperationLegalOrCustom(ISD::FMAXNUM, VT.getScalarType()) ?
2507 ISD::FMAXNUM : ISD::FMAXNAN;
2514 if (Opc != ISD::DELETED_NODE &&
2515 (TLI.isOperationLegalOrCustom(Opc, VT) ||
2517 TLI.isOperationLegalOrCustom(Opc, VT.getScalarType()))) &&
2518 // If the underlying comparison instruction is used by any other
2519 // instruction, the consumed instructions won't be destroyed, so it is
2520 // not profitable to convert to a min/max.
2521 cast<SelectInst>(&I)->getCondition()->hasOneUse()) {
2523 LHSVal = getValue(LHS);
2524 RHSVal = getValue(RHS);
2529 for (unsigned i = 0; i != NumValues; ++i) {
2530 SmallVector<SDValue, 3> Ops(BaseOps.begin(), BaseOps.end());
2531 Ops.push_back(SDValue(LHSVal.getNode(), LHSVal.getResNo() + i));
2532 Ops.push_back(SDValue(RHSVal.getNode(), RHSVal.getResNo() + i));
2533 Values[i] = DAG.getNode(OpCode, getCurSDLoc(),
2534 LHSVal.getNode()->getValueType(LHSVal.getResNo()+i),
2538 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
2539 DAG.getVTList(ValueVTs), Values));
2542 void SelectionDAGBuilder::visitTrunc(const User &I) {
2543 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2544 SDValue N = getValue(I.getOperand(0));
2545 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
2547 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), DestVT, N));
2550 void SelectionDAGBuilder::visitZExt(const User &I) {
2551 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2552 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2553 SDValue N = getValue(I.getOperand(0));
2554 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
2556 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(), DestVT, N));
2559 void SelectionDAGBuilder::visitSExt(const User &I) {
2560 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2561 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2562 SDValue N = getValue(I.getOperand(0));
2563 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
2565 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
2568 void SelectionDAGBuilder::visitFPTrunc(const User &I) {
2569 // FPTrunc is never a no-op cast, no need to check
2570 SDValue N = getValue(I.getOperand(0));
2571 SDLoc dl = getCurSDLoc();
2572 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2573 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
2574 setValue(&I, DAG.getNode(ISD::FP_ROUND, dl, DestVT, N,
2575 DAG.getTargetConstant(
2576 0, dl, TLI.getPointerTy(DAG.getDataLayout()))));
2579 void SelectionDAGBuilder::visitFPExt(const User &I) {
2580 // FPExt is never a no-op cast, no need to check
2581 SDValue N = getValue(I.getOperand(0));
2582 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
2584 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurSDLoc(), DestVT, N));
2587 void SelectionDAGBuilder::visitFPToUI(const User &I) {
2588 // FPToUI is never a no-op cast, no need to check
2589 SDValue N = getValue(I.getOperand(0));
2590 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
2592 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurSDLoc(), DestVT, N));
2595 void SelectionDAGBuilder::visitFPToSI(const User &I) {
2596 // FPToSI is never a no-op cast, no need to check
2597 SDValue N = getValue(I.getOperand(0));
2598 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
2600 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurSDLoc(), DestVT, N));
2603 void SelectionDAGBuilder::visitUIToFP(const User &I) {
2604 // UIToFP is never a no-op cast, no need to check
2605 SDValue N = getValue(I.getOperand(0));
2606 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
2608 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurSDLoc(), DestVT, N));
2611 void SelectionDAGBuilder::visitSIToFP(const User &I) {
2612 // SIToFP is never a no-op cast, no need to check
2613 SDValue N = getValue(I.getOperand(0));
2614 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
2616 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurSDLoc(), DestVT, N));
2619 void SelectionDAGBuilder::visitPtrToInt(const User &I) {
2620 // What to do depends on the size of the integer and the size of the pointer.
2621 // We can either truncate, zero extend, or no-op, accordingly.
2622 SDValue N = getValue(I.getOperand(0));
2623 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
2625 setValue(&I, DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT));
2628 void SelectionDAGBuilder::visitIntToPtr(const User &I) {
2629 // What to do depends on the size of the integer and the size of the pointer.
2630 // We can either truncate, zero extend, or no-op, accordingly.
2631 SDValue N = getValue(I.getOperand(0));
2632 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
2634 setValue(&I, DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT));
2637 void SelectionDAGBuilder::visitBitCast(const User &I) {
2638 SDValue N = getValue(I.getOperand(0));
2639 SDLoc dl = getCurSDLoc();
2640 EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
2643 // BitCast assures us that source and destination are the same size so this is
2644 // either a BITCAST or a no-op.
2645 if (DestVT != N.getValueType())
2646 setValue(&I, DAG.getNode(ISD::BITCAST, dl,
2647 DestVT, N)); // convert types.
2648 // Check if the original LLVM IR Operand was a ConstantInt, because getValue()
2649 // might fold any kind of constant expression to an integer constant and that
2650 // is not what we are looking for. Only regcognize a bitcast of a genuine
2651 // constant integer as an opaque constant.
2652 else if(ConstantInt *C = dyn_cast<ConstantInt>(I.getOperand(0)))
2653 setValue(&I, DAG.getConstant(C->getValue(), dl, DestVT, /*isTarget=*/false,
2656 setValue(&I, N); // noop cast.
2659 void SelectionDAGBuilder::visitAddrSpaceCast(const User &I) {
2660 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2661 const Value *SV = I.getOperand(0);
2662 SDValue N = getValue(SV);
2663 EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
2665 unsigned SrcAS = SV->getType()->getPointerAddressSpace();
2666 unsigned DestAS = I.getType()->getPointerAddressSpace();
2668 if (!TLI.isNoopAddrSpaceCast(SrcAS, DestAS))
2669 N = DAG.getAddrSpaceCast(getCurSDLoc(), DestVT, N, SrcAS, DestAS);
2674 void SelectionDAGBuilder::visitInsertElement(const User &I) {
2675 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2676 SDValue InVec = getValue(I.getOperand(0));
2677 SDValue InVal = getValue(I.getOperand(1));
2678 SDValue InIdx = DAG.getSExtOrTrunc(getValue(I.getOperand(2)), getCurSDLoc(),
2679 TLI.getVectorIdxTy(DAG.getDataLayout()));
2680 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurSDLoc(),
2681 TLI.getValueType(DAG.getDataLayout(), I.getType()),
2682 InVec, InVal, InIdx));
2685 void SelectionDAGBuilder::visitExtractElement(const User &I) {
2686 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2687 SDValue InVec = getValue(I.getOperand(0));
2688 SDValue InIdx = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), getCurSDLoc(),
2689 TLI.getVectorIdxTy(DAG.getDataLayout()));
2690 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurSDLoc(),
2691 TLI.getValueType(DAG.getDataLayout(), I.getType()),
2695 // Utility for visitShuffleVector - Return true if every element in Mask,
2696 // beginning from position Pos and ending in Pos+Size, falls within the
2697 // specified sequential range [L, L+Pos). or is undef.
2698 static bool isSequentialInRange(const SmallVectorImpl<int> &Mask,
2699 unsigned Pos, unsigned Size, int Low) {
2700 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
2701 if (Mask[i] >= 0 && Mask[i] != Low)
2706 void SelectionDAGBuilder::visitShuffleVector(const User &I) {
2707 SDValue Src1 = getValue(I.getOperand(0));
2708 SDValue Src2 = getValue(I.getOperand(1));
2710 SmallVector<int, 8> Mask;
2711 ShuffleVectorInst::getShuffleMask(cast<Constant>(I.getOperand(2)), Mask);
2712 unsigned MaskNumElts = Mask.size();
2714 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2715 EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
2716 EVT SrcVT = Src1.getValueType();
2717 unsigned SrcNumElts = SrcVT.getVectorNumElements();
2719 if (SrcNumElts == MaskNumElts) {
2720 setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2,
2725 // Normalize the shuffle vector since mask and vector length don't match.
2726 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2727 // Mask is longer than the source vectors and is a multiple of the source
2728 // vectors. We can use concatenate vector to make the mask and vectors
2730 if (SrcNumElts*2 == MaskNumElts) {
2731 // First check for Src1 in low and Src2 in high
2732 if (isSequentialInRange(Mask, 0, SrcNumElts, 0) &&
2733 isSequentialInRange(Mask, SrcNumElts, SrcNumElts, SrcNumElts)) {
2734 // The shuffle is concatenating two vectors together.
2735 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurSDLoc(),
2739 // Then check for Src2 in low and Src1 in high
2740 if (isSequentialInRange(Mask, 0, SrcNumElts, SrcNumElts) &&
2741 isSequentialInRange(Mask, SrcNumElts, SrcNumElts, 0)) {
2742 // The shuffle is concatenating two vectors together.
2743 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurSDLoc(),
2749 // Pad both vectors with undefs to make them the same length as the mask.
2750 unsigned NumConcat = MaskNumElts / SrcNumElts;
2751 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2752 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
2753 SDValue UndefVal = DAG.getUNDEF(SrcVT);
2755 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2756 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
2760 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2761 getCurSDLoc(), VT, MOps1);
2762 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2763 getCurSDLoc(), VT, MOps2);
2765 // Readjust mask for new input vector length.
2766 SmallVector<int, 8> MappedOps;
2767 for (unsigned i = 0; i != MaskNumElts; ++i) {
2769 if (Idx >= (int)SrcNumElts)
2770 Idx -= SrcNumElts - MaskNumElts;
2771 MappedOps.push_back(Idx);
2774 setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2,
2779 if (SrcNumElts > MaskNumElts) {
2780 // Analyze the access pattern of the vector to see if we can extract
2781 // two subvectors and do the shuffle. The analysis is done by calculating
2782 // the range of elements the mask access on both vectors.
2783 int MinRange[2] = { static_cast<int>(SrcNumElts),
2784 static_cast<int>(SrcNumElts)};
2785 int MaxRange[2] = {-1, -1};
2787 for (unsigned i = 0; i != MaskNumElts; ++i) {
2793 if (Idx >= (int)SrcNumElts) {
2797 if (Idx > MaxRange[Input])
2798 MaxRange[Input] = Idx;
2799 if (Idx < MinRange[Input])
2800 MinRange[Input] = Idx;
2803 // Check if the access is smaller than the vector size and can we find
2804 // a reasonable extract index.
2805 int RangeUse[2] = { -1, -1 }; // 0 = Unused, 1 = Extract, -1 = Can not
2807 int StartIdx[2]; // StartIdx to extract from
2808 for (unsigned Input = 0; Input < 2; ++Input) {
2809 if (MinRange[Input] >= (int)SrcNumElts && MaxRange[Input] < 0) {
2810 RangeUse[Input] = 0; // Unused
2811 StartIdx[Input] = 0;
2815 // Find a good start index that is a multiple of the mask length. Then
2816 // see if the rest of the elements are in range.
2817 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
2818 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
2819 StartIdx[Input] + MaskNumElts <= SrcNumElts)
2820 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
2823 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
2824 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
2827 if (RangeUse[0] >= 0 && RangeUse[1] >= 0) {
2828 // Extract appropriate subvector and generate a vector shuffle
2829 for (unsigned Input = 0; Input < 2; ++Input) {
2830 SDValue &Src = Input == 0 ? Src1 : Src2;
2831 if (RangeUse[Input] == 0)
2832 Src = DAG.getUNDEF(VT);
2834 SDLoc dl = getCurSDLoc();
2836 ISD::EXTRACT_SUBVECTOR, dl, VT, Src,
2837 DAG.getConstant(StartIdx[Input], dl,
2838 TLI.getVectorIdxTy(DAG.getDataLayout())));
2842 // Calculate new mask.
2843 SmallVector<int, 8> MappedOps;
2844 for (unsigned i = 0; i != MaskNumElts; ++i) {
2847 if (Idx < (int)SrcNumElts)
2850 Idx -= SrcNumElts + StartIdx[1] - MaskNumElts;
2852 MappedOps.push_back(Idx);
2855 setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2,
2861 // We can't use either concat vectors or extract subvectors so fall back to
2862 // replacing the shuffle with extract and build vector.
2863 // to insert and build vector.
2864 EVT EltVT = VT.getVectorElementType();
2865 EVT IdxVT = TLI.getVectorIdxTy(DAG.getDataLayout());
2866 SDLoc dl = getCurSDLoc();
2867 SmallVector<SDValue,8> Ops;
2868 for (unsigned i = 0; i != MaskNumElts; ++i) {
2873 Res = DAG.getUNDEF(EltVT);
2875 SDValue &Src = Idx < (int)SrcNumElts ? Src1 : Src2;
2876 if (Idx >= (int)SrcNumElts) Idx -= SrcNumElts;
2878 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
2879 EltVT, Src, DAG.getConstant(Idx, dl, IdxVT));
2885 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Ops));
2888 void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
2889 const Value *Op0 = I.getOperand(0);
2890 const Value *Op1 = I.getOperand(1);
2891 Type *AggTy = I.getType();
2892 Type *ValTy = Op1->getType();
2893 bool IntoUndef = isa<UndefValue>(Op0);
2894 bool FromUndef = isa<UndefValue>(Op1);
2896 unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices());
2898 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2899 SmallVector<EVT, 4> AggValueVTs;
2900 ComputeValueVTs(TLI, DAG.getDataLayout(), AggTy, AggValueVTs);
2901 SmallVector<EVT, 4> ValValueVTs;
2902 ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs);
2904 unsigned NumAggValues = AggValueVTs.size();
2905 unsigned NumValValues = ValValueVTs.size();
2906 SmallVector<SDValue, 4> Values(NumAggValues);
2908 // Ignore an insertvalue that produces an empty object
2909 if (!NumAggValues) {
2910 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
2914 SDValue Agg = getValue(Op0);
2916 // Copy the beginning value(s) from the original aggregate.
2917 for (; i != LinearIndex; ++i)
2918 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
2919 SDValue(Agg.getNode(), Agg.getResNo() + i);
2920 // Copy values from the inserted value(s).
2922 SDValue Val = getValue(Op1);
2923 for (; i != LinearIndex + NumValValues; ++i)
2924 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
2925 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2927 // Copy remaining value(s) from the original aggregate.
2928 for (; i != NumAggValues; ++i)
2929 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
2930 SDValue(Agg.getNode(), Agg.getResNo() + i);
2932 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
2933 DAG.getVTList(AggValueVTs), Values));
2936 void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
2937 const Value *Op0 = I.getOperand(0);
2938 Type *AggTy = Op0->getType();
2939 Type *ValTy = I.getType();
2940 bool OutOfUndef = isa<UndefValue>(Op0);
2942 unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices());
2944 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2945 SmallVector<EVT, 4> ValValueVTs;
2946 ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs);
2948 unsigned NumValValues = ValValueVTs.size();
2950 // Ignore a extractvalue that produces an empty object
2951 if (!NumValValues) {
2952 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
2956 SmallVector<SDValue, 4> Values(NumValValues);
2958 SDValue Agg = getValue(Op0);
2959 // Copy out the selected value(s).
2960 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2961 Values[i - LinearIndex] =
2963 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
2964 SDValue(Agg.getNode(), Agg.getResNo() + i);
2966 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
2967 DAG.getVTList(ValValueVTs), Values));
2970 void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
2971 Value *Op0 = I.getOperand(0);
2972 // Note that the pointer operand may be a vector of pointers. Take the scalar
2973 // element which holds a pointer.
2974 Type *Ty = Op0->getType()->getScalarType();
2975 unsigned AS = Ty->getPointerAddressSpace();
2976 SDValue N = getValue(Op0);
2977 SDLoc dl = getCurSDLoc();
2979 // Normalize Vector GEP - all scalar operands should be converted to the
2981 unsigned VectorWidth = I.getType()->isVectorTy() ?
2982 cast<VectorType>(I.getType())->getVectorNumElements() : 0;
2984 if (VectorWidth && !N.getValueType().isVector()) {
2985 MVT VT = MVT::getVectorVT(N.getValueType().getSimpleVT(), VectorWidth);
2986 SmallVector<SDValue, 16> Ops(VectorWidth, N);
2987 N = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Ops);
2989 for (GetElementPtrInst::const_op_iterator OI = I.op_begin()+1, E = I.op_end();
2991 const Value *Idx = *OI;
2992 if (StructType *StTy = dyn_cast<StructType>(Ty)) {
2993 unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue();
2996 uint64_t Offset = DL->getStructLayout(StTy)->getElementOffset(Field);
2997 N = DAG.getNode(ISD::ADD, dl, N.getValueType(), N,
2998 DAG.getConstant(Offset, dl, N.getValueType()));
3001 Ty = StTy->getElementType(Field);
3003 Ty = cast<SequentialType>(Ty)->getElementType();
3005 DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout(), AS);
3006 unsigned PtrSize = PtrTy.getSizeInBits();
3007 APInt ElementSize(PtrSize, DL->getTypeAllocSize(Ty));
3009 // If this is a scalar constant or a splat vector of constants,
3010 // handle it quickly.
3011 const auto *CI = dyn_cast<ConstantInt>(Idx);
3012 if (!CI && isa<ConstantDataVector>(Idx) &&
3013 cast<ConstantDataVector>(Idx)->getSplatValue())
3014 CI = cast<ConstantInt>(cast<ConstantDataVector>(Idx)->getSplatValue());
3019 APInt Offs = ElementSize * CI->getValue().sextOrTrunc(PtrSize);
3020 SDValue OffsVal = VectorWidth ?
3021 DAG.getConstant(Offs, dl, MVT::getVectorVT(PtrTy, VectorWidth)) :
3022 DAG.getConstant(Offs, dl, PtrTy);
3023 N = DAG.getNode(ISD::ADD, dl, N.getValueType(), N, OffsVal);
3027 // N = N + Idx * ElementSize;
3028 SDValue IdxN = getValue(Idx);
3030 if (!IdxN.getValueType().isVector() && VectorWidth) {
3031 MVT VT = MVT::getVectorVT(IdxN.getValueType().getSimpleVT(), VectorWidth);
3032 SmallVector<SDValue, 16> Ops(VectorWidth, IdxN);
3033 IdxN = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Ops);
3035 // If the index is smaller or larger than intptr_t, truncate or extend
3037 IdxN = DAG.getSExtOrTrunc(IdxN, dl, N.getValueType());
3039 // If this is a multiply by a power of two, turn it into a shl
3040 // immediately. This is a very common case.
3041 if (ElementSize != 1) {
3042 if (ElementSize.isPowerOf2()) {
3043 unsigned Amt = ElementSize.logBase2();
3044 IdxN = DAG.getNode(ISD::SHL, dl,
3045 N.getValueType(), IdxN,
3046 DAG.getConstant(Amt, dl, IdxN.getValueType()));
3048 SDValue Scale = DAG.getConstant(ElementSize, dl, IdxN.getValueType());
3049 IdxN = DAG.getNode(ISD::MUL, dl,
3050 N.getValueType(), IdxN, Scale);
3054 N = DAG.getNode(ISD::ADD, dl,
3055 N.getValueType(), N, IdxN);
3062 void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
3063 // If this is a fixed sized alloca in the entry block of the function,
3064 // allocate it statically on the stack.
3065 if (FuncInfo.StaticAllocaMap.count(&I))
3066 return; // getValue will auto-populate this.
3068 SDLoc dl = getCurSDLoc();
3069 Type *Ty = I.getAllocatedType();
3070 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3071 auto &DL = DAG.getDataLayout();
3072 uint64_t TySize = DL.getTypeAllocSize(Ty);
3074 std::max((unsigned)DL.getPrefTypeAlignment(Ty), I.getAlignment());
3076 SDValue AllocSize = getValue(I.getArraySize());
3078 EVT IntPtr = TLI.getPointerTy(DAG.getDataLayout());
3079 if (AllocSize.getValueType() != IntPtr)
3080 AllocSize = DAG.getZExtOrTrunc(AllocSize, dl, IntPtr);
3082 AllocSize = DAG.getNode(ISD::MUL, dl, IntPtr,
3084 DAG.getConstant(TySize, dl, IntPtr));
3086 // Handle alignment. If the requested alignment is less than or equal to
3087 // the stack alignment, ignore it. If the size is greater than or equal to
3088 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
3089 unsigned StackAlign =
3090 DAG.getSubtarget().getFrameLowering()->getStackAlignment();
3091 if (Align <= StackAlign)
3094 // Round the size of the allocation up to the stack alignment size
3095 // by add SA-1 to the size.
3096 AllocSize = DAG.getNode(ISD::ADD, dl,
3097 AllocSize.getValueType(), AllocSize,
3098 DAG.getIntPtrConstant(StackAlign - 1, dl));
3100 // Mask out the low bits for alignment purposes.
3101 AllocSize = DAG.getNode(ISD::AND, dl,
3102 AllocSize.getValueType(), AllocSize,
3103 DAG.getIntPtrConstant(~(uint64_t)(StackAlign - 1),
3106 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align, dl) };
3107 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
3108 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, dl, VTs, Ops);
3110 DAG.setRoot(DSA.getValue(1));
3112 assert(FuncInfo.MF->getFrameInfo()->hasVarSizedObjects());
3115 void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
3117 return visitAtomicLoad(I);
3119 const Value *SV = I.getOperand(0);
3120 SDValue Ptr = getValue(SV);
3122 Type *Ty = I.getType();
3124 bool isVolatile = I.isVolatile();
3125 bool isNonTemporal = I.getMetadata(LLVMContext::MD_nontemporal) != nullptr;
3127 // The IR notion of invariant_load only guarantees that all *non-faulting*
3128 // invariant loads result in the same value. The MI notion of invariant load
3129 // guarantees that the load can be legally moved to any location within its
3130 // containing function. The MI notion of invariant_load is stronger than the
3131 // IR notion of invariant_load -- an MI invariant_load is an IR invariant_load
3132 // with a guarantee that the location being loaded from is dereferenceable
3133 // throughout the function's lifetime.
3135 bool isInvariant = I.getMetadata(LLVMContext::MD_invariant_load) != nullptr &&
3136 isDereferenceablePointer(SV, DAG.getDataLayout());
3137 unsigned Alignment = I.getAlignment();
3140 I.getAAMetadata(AAInfo);
3141 const MDNode *Ranges = I.getMetadata(LLVMContext::MD_range);
3143 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3144 SmallVector<EVT, 4> ValueVTs;
3145 SmallVector<uint64_t, 4> Offsets;
3146 ComputeValueVTs(TLI, DAG.getDataLayout(), Ty, ValueVTs, &Offsets);
3147 unsigned NumValues = ValueVTs.size();
3152 bool ConstantMemory = false;
3153 if (isVolatile || NumValues > MaxParallelChains)
3154 // Serialize volatile loads with other side effects.
3156 else if (AA->pointsToConstantMemory(MemoryLocation(
3157 SV, DAG.getDataLayout().getTypeStoreSize(Ty), AAInfo))) {
3158 // Do not serialize (non-volatile) loads of constant memory with anything.
3159 Root = DAG.getEntryNode();
3160 ConstantMemory = true;
3162 // Do not serialize non-volatile loads against each other.
3163 Root = DAG.getRoot();
3166 SDLoc dl = getCurSDLoc();
3169 Root = TLI.prepareVolatileOrAtomicLoad(Root, dl, DAG);
3171 SmallVector<SDValue, 4> Values(NumValues);
3172 SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues));
3173 EVT PtrVT = Ptr.getValueType();
3174 unsigned ChainI = 0;
3175 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
3176 // Serializing loads here may result in excessive register pressure, and
3177 // TokenFactor places arbitrary choke points on the scheduler. SD scheduling
3178 // could recover a bit by hoisting nodes upward in the chain by recognizing
3179 // they are side-effect free or do not alias. The optimizer should really
3180 // avoid this case by converting large object/array copies to llvm.memcpy
3181 // (MaxParallelChains should always remain as failsafe).
3182 if (ChainI == MaxParallelChains) {
3183 assert(PendingLoads.empty() && "PendingLoads must be serialized first");
3184 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
3185 makeArrayRef(Chains.data(), ChainI));
3189 SDValue A = DAG.getNode(ISD::ADD, dl,
3191 DAG.getConstant(Offsets[i], dl, PtrVT));
3192 SDValue L = DAG.getLoad(ValueVTs[i], dl, Root,
3193 A, MachinePointerInfo(SV, Offsets[i]), isVolatile,
3194 isNonTemporal, isInvariant, Alignment, AAInfo,
3198 Chains[ChainI] = L.getValue(1);
3201 if (!ConstantMemory) {
3202 SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
3203 makeArrayRef(Chains.data(), ChainI));
3207 PendingLoads.push_back(Chain);