xref: /llvm-project/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp (revision 0c44115e5120167fc573e36dd878f4f95f5d63e6)
1 //===- SelectionDAGBuilder.cpp - Selection-DAG building -------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This implements routines for translating from LLVM IR into SelectionDAG IR.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "SelectionDAGBuilder.h"
14 #include "SDNodeDbgValue.h"
15 #include "llvm/ADT/APFloat.h"
16 #include "llvm/ADT/APInt.h"
17 #include "llvm/ADT/BitVector.h"
18 #include "llvm/ADT/None.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/SmallSet.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/Triple.h"
25 #include "llvm/ADT/Twine.h"
26 #include "llvm/Analysis/AliasAnalysis.h"
27 #include "llvm/Analysis/BranchProbabilityInfo.h"
28 #include "llvm/Analysis/ConstantFolding.h"
29 #include "llvm/Analysis/EHPersonalities.h"
30 #include "llvm/Analysis/MemoryLocation.h"
31 #include "llvm/Analysis/TargetLibraryInfo.h"
32 #include "llvm/Analysis/ValueTracking.h"
33 #include "llvm/CodeGen/Analysis.h"
34 #include "llvm/CodeGen/FunctionLoweringInfo.h"
35 #include "llvm/CodeGen/GCMetadata.h"
36 #include "llvm/CodeGen/MachineBasicBlock.h"
37 #include "llvm/CodeGen/MachineFrameInfo.h"
38 #include "llvm/CodeGen/MachineFunction.h"
39 #include "llvm/CodeGen/MachineInstrBuilder.h"
40 #include "llvm/CodeGen/MachineInstrBundleIterator.h"
41 #include "llvm/CodeGen/MachineMemOperand.h"
42 #include "llvm/CodeGen/MachineModuleInfo.h"
43 #include "llvm/CodeGen/MachineOperand.h"
44 #include "llvm/CodeGen/MachineRegisterInfo.h"
45 #include "llvm/CodeGen/RuntimeLibcalls.h"
46 #include "llvm/CodeGen/SelectionDAG.h"
47 #include "llvm/CodeGen/SelectionDAGTargetInfo.h"
48 #include "llvm/CodeGen/StackMaps.h"
49 #include "llvm/CodeGen/SwiftErrorValueTracking.h"
50 #include "llvm/CodeGen/TargetFrameLowering.h"
51 #include "llvm/CodeGen/TargetInstrInfo.h"
52 #include "llvm/CodeGen/TargetOpcodes.h"
53 #include "llvm/CodeGen/TargetRegisterInfo.h"
54 #include "llvm/CodeGen/TargetSubtargetInfo.h"
55 #include "llvm/CodeGen/WinEHFuncInfo.h"
56 #include "llvm/IR/Argument.h"
57 #include "llvm/IR/Attributes.h"
58 #include "llvm/IR/BasicBlock.h"
59 #include "llvm/IR/CFG.h"
60 #include "llvm/IR/CallingConv.h"
61 #include "llvm/IR/Constant.h"
62 #include "llvm/IR/ConstantRange.h"
63 #include "llvm/IR/Constants.h"
64 #include "llvm/IR/DataLayout.h"
65 #include "llvm/IR/DebugInfoMetadata.h"
66 #include "llvm/IR/DerivedTypes.h"
67 #include "llvm/IR/DiagnosticInfo.h"
68 #include "llvm/IR/Function.h"
69 #include "llvm/IR/GetElementPtrTypeIterator.h"
70 #include "llvm/IR/InlineAsm.h"
71 #include "llvm/IR/InstrTypes.h"
72 #include "llvm/IR/Instructions.h"
73 #include "llvm/IR/IntrinsicInst.h"
74 #include "llvm/IR/Intrinsics.h"
75 #include "llvm/IR/IntrinsicsAArch64.h"
76 #include "llvm/IR/IntrinsicsWebAssembly.h"
77 #include "llvm/IR/LLVMContext.h"
78 #include "llvm/IR/Metadata.h"
79 #include "llvm/IR/Module.h"
80 #include "llvm/IR/Operator.h"
81 #include "llvm/IR/PatternMatch.h"
82 #include "llvm/IR/Statepoint.h"
83 #include "llvm/IR/Type.h"
84 #include "llvm/IR/User.h"
85 #include "llvm/IR/Value.h"
86 #include "llvm/MC/MCContext.h"
87 #include "llvm/Support/AtomicOrdering.h"
88 #include "llvm/Support/Casting.h"
89 #include "llvm/Support/CommandLine.h"
90 #include "llvm/Support/Compiler.h"
91 #include "llvm/Support/Debug.h"
92 #include "llvm/Support/MathExtras.h"
93 #include "llvm/Support/raw_ostream.h"
94 #include "llvm/Target/TargetIntrinsicInfo.h"
95 #include "llvm/Target/TargetMachine.h"
96 #include "llvm/Target/TargetOptions.h"
97 #include "llvm/Transforms/Utils/Local.h"
98 #include <cstddef>
99 #include <iterator>
100 #include <limits>
101 #include <tuple>
102 
103 using namespace llvm;
104 using namespace PatternMatch;
105 using namespace SwitchCG;
106 
107 #define DEBUG_TYPE "isel"
108 
109 /// LimitFloatPrecision - Generate low-precision inline sequences for
110 /// some float libcalls (6, 8 or 12 bits).
111 static unsigned LimitFloatPrecision;
112 
113 static cl::opt<bool>
114     InsertAssertAlign("insert-assert-align", cl::init(true),
115                       cl::desc("Insert the experimental `assertalign` node."),
116                       cl::ReallyHidden);
117 
118 static cl::opt<unsigned, true>
119     LimitFPPrecision("limit-float-precision",
120                      cl::desc("Generate low-precision inline sequences "
121                               "for some float libcalls"),
122                      cl::location(LimitFloatPrecision), cl::Hidden,
123                      cl::init(0));
124 
125 static cl::opt<unsigned> SwitchPeelThreshold(
126     "switch-peel-threshold", cl::Hidden, cl::init(66),
127     cl::desc("Set the case probability threshold for peeling the case from a "
128              "switch statement. A value greater than 100 will void this "
129              "optimization"));
130 
131 // Limit the width of DAG chains. This is important in general to prevent
132 // DAG-based analysis from blowing up. For example, alias analysis and
133 // load clustering may not complete in reasonable time. It is difficult to
134 // recognize and avoid this situation within each individual analysis, and
135 // future analyses are likely to have the same behavior. Limiting DAG width is
136 // the safe approach and will be especially important with global DAGs.
137 //
138 // MaxParallelChains default is arbitrarily high to avoid affecting
139 // optimization, but could be lowered to improve compile time. Any ld-ld-st-st
140 // sequence over this should have been converted to llvm.memcpy by the
141 // frontend. It is easy to induce this behavior with .ll code such as:
142 // %buffer = alloca [4096 x i8]
143 // %data = load [4096 x i8]* %argPtr
144 // store [4096 x i8] %data, [4096 x i8]* %buffer
145 static const unsigned MaxParallelChains = 64;
146 
147 static SDValue getCopyFromPartsVector(SelectionDAG &DAG, const SDLoc &DL,
148                                       const SDValue *Parts, unsigned NumParts,
149                                       MVT PartVT, EVT ValueVT, const Value *V,
150                                       Optional<CallingConv::ID> CC);
151 
152 /// getCopyFromParts - Create a value that contains the specified legal parts
153 /// combined into the value they represent.  If the parts combine to a type
154 /// larger than ValueVT then AssertOp can be used to specify whether the extra
155 /// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
156 /// (ISD::AssertSext).
157 static SDValue getCopyFromParts(SelectionDAG &DAG, const SDLoc &DL,
158                                 const SDValue *Parts, unsigned NumParts,
159                                 MVT PartVT, EVT ValueVT, const Value *V,
160                                 Optional<CallingConv::ID> CC = None,
161                                 Optional<ISD::NodeType> AssertOp = None) {
162   // Let the target assemble the parts if it wants to
163   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
164   if (SDValue Val = TLI.joinRegisterPartsIntoValue(DAG, DL, Parts, NumParts,
165                                                    PartVT, ValueVT, CC))
166     return Val;
167 
168   if (ValueVT.isVector())
169     return getCopyFromPartsVector(DAG, DL, Parts, NumParts, PartVT, ValueVT, V,
170                                   CC);
171 
172   assert(NumParts > 0 && "No parts to assemble!");
173   SDValue Val = Parts[0];
174 
175   if (NumParts > 1) {
176     // Assemble the value from multiple parts.
177     if (ValueVT.isInteger()) {
178       unsigned PartBits = PartVT.getSizeInBits();
179       unsigned ValueBits = ValueVT.getSizeInBits();
180 
181       // Assemble the power of 2 part.
182       unsigned RoundParts =
183           (NumParts & (NumParts - 1)) ? 1 << Log2_32(NumParts) : NumParts;
184       unsigned RoundBits = PartBits * RoundParts;
185       EVT RoundVT = RoundBits == ValueBits ?
186         ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
187       SDValue Lo, Hi;
188 
189       EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
190 
191       if (RoundParts > 2) {
192         Lo = getCopyFromParts(DAG, DL, Parts, RoundParts / 2,
193                               PartVT, HalfVT, V);
194         Hi = getCopyFromParts(DAG, DL, Parts + RoundParts / 2,
195                               RoundParts / 2, PartVT, HalfVT, V);
196       } else {
197         Lo = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[0]);
198         Hi = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[1]);
199       }
200 
201       if (DAG.getDataLayout().isBigEndian())
202         std::swap(Lo, Hi);
203 
204       Val = DAG.getNode(ISD::BUILD_PAIR, DL, RoundVT, Lo, Hi);
205 
206       if (RoundParts < NumParts) {
207         // Assemble the trailing non-power-of-2 part.
208         unsigned OddParts = NumParts - RoundParts;
209         EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
210         Hi = getCopyFromParts(DAG, DL, Parts + RoundParts, OddParts, PartVT,
211                               OddVT, V, CC);
212 
213         // Combine the round and odd parts.
214         Lo = Val;
215         if (DAG.getDataLayout().isBigEndian())
216           std::swap(Lo, Hi);
217         EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
218         Hi = DAG.getNode(ISD::ANY_EXTEND, DL, TotalVT, Hi);
219         Hi = DAG.getNode(ISD::SHL, DL, TotalVT, Hi,
220                          DAG.getConstant(Lo.getValueSizeInBits(), DL,
221                                          TLI.getShiftAmountTy(
222                                              TotalVT, DAG.getDataLayout())));
223         Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, TotalVT, Lo);
224         Val = DAG.getNode(ISD::OR, DL, TotalVT, Lo, Hi);
225       }
226     } else if (PartVT.isFloatingPoint()) {
227       // FP split into multiple FP parts (for ppcf128)
228       assert(ValueVT == EVT(MVT::ppcf128) && PartVT == MVT::f64 &&
229              "Unexpected split");
230       SDValue Lo, Hi;
231       Lo = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[0]);
232       Hi = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[1]);
233       if (TLI.hasBigEndianPartOrdering(ValueVT, DAG.getDataLayout()))
234         std::swap(Lo, Hi);
235       Val = DAG.getNode(ISD::BUILD_PAIR, DL, ValueVT, Lo, Hi);
236     } else {
237       // FP split into integer parts (soft fp)
238       assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
239              !PartVT.isVector() && "Unexpected split");
240       EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
241       Val = getCopyFromParts(DAG, DL, Parts, NumParts, PartVT, IntVT, V, CC);
242     }
243   }
244 
245   // There is now one part, held in Val.  Correct it to match ValueVT.
246   // PartEVT is the type of the register class that holds the value.
247   // ValueVT is the type of the inline asm operation.
248   EVT PartEVT = Val.getValueType();
249 
250   if (PartEVT == ValueVT)
251     return Val;
252 
253   if (PartEVT.isInteger() && ValueVT.isFloatingPoint() &&
254       ValueVT.bitsLT(PartEVT)) {
255     // For an FP value in an integer part, we need to truncate to the right
256     // width first.
257     PartEVT = EVT::getIntegerVT(*DAG.getContext(),  ValueVT.getSizeInBits());
258     Val = DAG.getNode(ISD::TRUNCATE, DL, PartEVT, Val);
259   }
260 
261   // Handle types that have the same size.
262   if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits())
263     return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
264 
265   // Handle types with different sizes.
266   if (PartEVT.isInteger() && ValueVT.isInteger()) {
267     if (ValueVT.bitsLT(PartEVT)) {
268       // For a truncate, see if we have any information to
269       // indicate whether the truncated bits will always be
270       // zero or sign-extension.
271       if (AssertOp.hasValue())
272         Val = DAG.getNode(*AssertOp, DL, PartEVT, Val,
273                           DAG.getValueType(ValueVT));
274       return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
275     }
276     return DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val);
277   }
278 
279   if (PartEVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
280     // FP_ROUND's are always exact here.
281     if (ValueVT.bitsLT(Val.getValueType()))
282       return DAG.getNode(
283           ISD::FP_ROUND, DL, ValueVT, Val,
284           DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout())));
285 
286     return DAG.getNode(ISD::FP_EXTEND, DL, ValueVT, Val);
287   }
288 
289   // Handle MMX to a narrower integer type by bitcasting MMX to integer and
290   // then truncating.
291   if (PartEVT == MVT::x86mmx && ValueVT.isInteger() &&
292       ValueVT.bitsLT(PartEVT)) {
293     Val = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Val);
294     return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
295   }
296 
297   report_fatal_error("Unknown mismatch in getCopyFromParts!");
298 }
299 
300 static void diagnosePossiblyInvalidConstraint(LLVMContext &Ctx, const Value *V,
301                                               const Twine &ErrMsg) {
302   const Instruction *I = dyn_cast_or_null<Instruction>(V);
303   if (!V)
304     return Ctx.emitError(ErrMsg);
305 
306   const char *AsmError = ", possible invalid constraint for vector type";
307   if (const CallInst *CI = dyn_cast<CallInst>(I))
308     if (CI->isInlineAsm())
309       return Ctx.emitError(I, ErrMsg + AsmError);
310 
311   return Ctx.emitError(I, ErrMsg);
312 }
313 
314 /// getCopyFromPartsVector - Create a value that contains the specified legal
315 /// parts combined into the value they represent.  If the parts combine to a
316 /// type larger than ValueVT then AssertOp can be used to specify whether the
317 /// extra bits are known to be zero (ISD::AssertZext) or sign extended from
318 /// ValueVT (ISD::AssertSext).
319 static SDValue getCopyFromPartsVector(SelectionDAG &DAG, const SDLoc &DL,
320                                       const SDValue *Parts, unsigned NumParts,
321                                       MVT PartVT, EVT ValueVT, const Value *V,
322                                       Optional<CallingConv::ID> CallConv) {
323   assert(ValueVT.isVector() && "Not a vector value");
324   assert(NumParts > 0 && "No parts to assemble!");
325   const bool IsABIRegCopy = CallConv.hasValue();
326 
327   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
328   SDValue Val = Parts[0];
329 
330   // Handle a multi-element vector.
331   if (NumParts > 1) {
332     EVT IntermediateVT;
333     MVT RegisterVT;
334     unsigned NumIntermediates;
335     unsigned NumRegs;
336 
337     if (IsABIRegCopy) {
338       NumRegs = TLI.getVectorTypeBreakdownForCallingConv(
339           *DAG.getContext(), CallConv.getValue(), ValueVT, IntermediateVT,
340           NumIntermediates, RegisterVT);
341     } else {
342       NumRegs =
343           TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
344                                      NumIntermediates, RegisterVT);
345     }
346 
347     assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
348     NumParts = NumRegs; // Silence a compiler warning.
349     assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
350     assert(RegisterVT.getSizeInBits() ==
351            Parts[0].getSimpleValueType().getSizeInBits() &&
352            "Part type sizes don't match!");
353 
354     // Assemble the parts into intermediate operands.
355     SmallVector<SDValue, 8> Ops(NumIntermediates);
356     if (NumIntermediates == NumParts) {
357       // If the register was not expanded, truncate or copy the value,
358       // as appropriate.
359       for (unsigned i = 0; i != NumParts; ++i)
360         Ops[i] = getCopyFromParts(DAG, DL, &Parts[i], 1,
361                                   PartVT, IntermediateVT, V, CallConv);
362     } else if (NumParts > 0) {
363       // If the intermediate type was expanded, build the intermediate
364       // operands from the parts.
365       assert(NumParts % NumIntermediates == 0 &&
366              "Must expand into a divisible number of parts!");
367       unsigned Factor = NumParts / NumIntermediates;
368       for (unsigned i = 0; i != NumIntermediates; ++i)
369         Ops[i] = getCopyFromParts(DAG, DL, &Parts[i * Factor], Factor,
370                                   PartVT, IntermediateVT, V, CallConv);
371     }
372 
373     // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the
374     // intermediate operands.
375     EVT BuiltVectorTy =
376         IntermediateVT.isVector()
377             ? EVT::getVectorVT(
378                   *DAG.getContext(), IntermediateVT.getScalarType(),
379                   IntermediateVT.getVectorElementCount() * NumParts)
380             : EVT::getVectorVT(*DAG.getContext(),
381                                IntermediateVT.getScalarType(),
382                                NumIntermediates);
383     Val = DAG.getNode(IntermediateVT.isVector() ? ISD::CONCAT_VECTORS
384                                                 : ISD::BUILD_VECTOR,
385                       DL, BuiltVectorTy, Ops);
386   }
387 
388   // There is now one part, held in Val.  Correct it to match ValueVT.
389   EVT PartEVT = Val.getValueType();
390 
391   if (PartEVT == ValueVT)
392     return Val;
393 
394   if (PartEVT.isVector()) {
395     // Vector/Vector bitcast.
396     if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits())
397       return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
398 
399     // If the element type of the source/dest vectors are the same, but the
400     // parts vector has more elements than the value vector, then we have a
401     // vector widening case (e.g. <2 x float> -> <4 x float>).  Extract the
402     // elements we want.
403     if (PartEVT.getVectorElementCount() != ValueVT.getVectorElementCount()) {
404       assert((PartEVT.getVectorElementCount().getKnownMinValue() >
405               ValueVT.getVectorElementCount().getKnownMinValue()) &&
406              (PartEVT.getVectorElementCount().isScalable() ==
407               ValueVT.getVectorElementCount().isScalable()) &&
408              "Cannot narrow, it would be a lossy transformation");
409       PartEVT =
410           EVT::getVectorVT(*DAG.getContext(), PartEVT.getVectorElementType(),
411                            ValueVT.getVectorElementCount());
412       Val = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, PartEVT, Val,
413                         DAG.getVectorIdxConstant(0, DL));
414       if (PartEVT == ValueVT)
415         return Val;
416     }
417 
418     // Promoted vector extract
419     return DAG.getAnyExtOrTrunc(Val, DL, ValueVT);
420   }
421 
422   // Trivial bitcast if the types are the same size and the destination
423   // vector type is legal.
424   if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits() &&
425       TLI.isTypeLegal(ValueVT))
426     return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
427 
428   if (ValueVT.getVectorNumElements() != 1) {
429      // Certain ABIs require that vectors are passed as integers. For vectors
430      // are the same size, this is an obvious bitcast.
431      if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits()) {
432        return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
433      } else if (ValueVT.bitsLT(PartEVT)) {
434        const uint64_t ValueSize = ValueVT.getFixedSizeInBits();
435        EVT IntermediateType = EVT::getIntegerVT(*DAG.getContext(), ValueSize);
436        // Drop the extra bits.
437        Val = DAG.getNode(ISD::TRUNCATE, DL, IntermediateType, Val);
438        return DAG.getBitcast(ValueVT, Val);
439      }
440 
441      diagnosePossiblyInvalidConstraint(
442          *DAG.getContext(), V, "non-trivial scalar-to-vector conversion");
443      return DAG.getUNDEF(ValueVT);
444   }
445 
446   // Handle cases such as i8 -> <1 x i1>
447   EVT ValueSVT = ValueVT.getVectorElementType();
448   if (ValueVT.getVectorNumElements() == 1 && ValueSVT != PartEVT) {
449     if (ValueSVT.getSizeInBits() == PartEVT.getSizeInBits())
450       Val = DAG.getNode(ISD::BITCAST, DL, ValueSVT, Val);
451     else
452       Val = ValueVT.isFloatingPoint()
453                 ? DAG.getFPExtendOrRound(Val, DL, ValueSVT)
454                 : DAG.getAnyExtOrTrunc(Val, DL, ValueSVT);
455   }
456 
457   return DAG.getBuildVector(ValueVT, DL, Val);
458 }
459 
460 static void getCopyToPartsVector(SelectionDAG &DAG, const SDLoc &dl,
461                                  SDValue Val, SDValue *Parts, unsigned NumParts,
462                                  MVT PartVT, const Value *V,
463                                  Optional<CallingConv::ID> CallConv);
464 
465 /// getCopyToParts - Create a series of nodes that contain the specified value
466 /// split into legal parts.  If the parts contain more bits than Val, then, for
467 /// integers, ExtendKind can be used to specify how to generate the extra bits.
468 static void getCopyToParts(SelectionDAG &DAG, const SDLoc &DL, SDValue Val,
469                            SDValue *Parts, unsigned NumParts, MVT PartVT,
470                            const Value *V,
471                            Optional<CallingConv::ID> CallConv = None,
472                            ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
473   // Let the target split the parts if it wants to
474   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
475   if (TLI.splitValueIntoRegisterParts(DAG, DL, Val, Parts, NumParts, PartVT,
476                                       CallConv))
477     return;
478   EVT ValueVT = Val.getValueType();
479 
480   // Handle the vector case separately.
481   if (ValueVT.isVector())
482     return getCopyToPartsVector(DAG, DL, Val, Parts, NumParts, PartVT, V,
483                                 CallConv);
484 
485   unsigned PartBits = PartVT.getSizeInBits();
486   unsigned OrigNumParts = NumParts;
487   assert(DAG.getTargetLoweringInfo().isTypeLegal(PartVT) &&
488          "Copying to an illegal type!");
489 
490   if (NumParts == 0)
491     return;
492 
493   assert(!ValueVT.isVector() && "Vector case handled elsewhere");
494   EVT PartEVT = PartVT;
495   if (PartEVT == ValueVT) {
496     assert(NumParts == 1 && "No-op copy with multiple parts!");
497     Parts[0] = Val;
498     return;
499   }
500 
501   if (NumParts * PartBits > ValueVT.getSizeInBits()) {
502     // If the parts cover more bits than the value has, promote the value.
503     if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
504       assert(NumParts == 1 && "Do not know what to promote to!");
505       Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val);
506     } else {
507       if (ValueVT.isFloatingPoint()) {
508         // FP values need to be bitcast, then extended if they are being put
509         // into a larger container.
510         ValueVT = EVT::getIntegerVT(*DAG.getContext(),  ValueVT.getSizeInBits());
511         Val = DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
512       }
513       assert((PartVT.isInteger() || PartVT == MVT::x86mmx) &&
514              ValueVT.isInteger() &&
515              "Unknown mismatch!");
516       ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
517       Val = DAG.getNode(ExtendKind, DL, ValueVT, Val);
518       if (PartVT == MVT::x86mmx)
519         Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
520     }
521   } else if (PartBits == ValueVT.getSizeInBits()) {
522     // Different types of the same size.
523     assert(NumParts == 1 && PartEVT != ValueVT);
524     Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
525   } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
526     // If the parts cover less bits than value has, truncate the value.
527     assert((PartVT.isInteger() || PartVT == MVT::x86mmx) &&
528            ValueVT.isInteger() &&
529            "Unknown mismatch!");
530     ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
531     Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
532     if (PartVT == MVT::x86mmx)
533       Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
534   }
535 
536   // The value may have changed - recompute ValueVT.
537   ValueVT = Val.getValueType();
538   assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
539          "Failed to tile the value with PartVT!");
540 
541   if (NumParts == 1) {
542     if (PartEVT != ValueVT) {
543       diagnosePossiblyInvalidConstraint(*DAG.getContext(), V,
544                                         "scalar-to-vector conversion failed");
545       Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
546     }
547 
548     Parts[0] = Val;
549     return;
550   }
551 
552   // Expand the value into multiple parts.
553   if (NumParts & (NumParts - 1)) {
554     // The number of parts is not a power of 2.  Split off and copy the tail.
555     assert(PartVT.isInteger() && ValueVT.isInteger() &&
556            "Do not know what to expand to!");
557     unsigned RoundParts = 1 << Log2_32(NumParts);
558     unsigned RoundBits = RoundParts * PartBits;
559     unsigned OddParts = NumParts - RoundParts;
560     SDValue OddVal = DAG.getNode(ISD::SRL, DL, ValueVT, Val,
561       DAG.getShiftAmountConstant(RoundBits, ValueVT, DL));
562 
563     getCopyToParts(DAG, DL, OddVal, Parts + RoundParts, OddParts, PartVT, V,
564                    CallConv);
565 
566     if (DAG.getDataLayout().isBigEndian())
567       // The odd parts were reversed by getCopyToParts - unreverse them.
568       std::reverse(Parts + RoundParts, Parts + NumParts);
569 
570     NumParts = RoundParts;
571     ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
572     Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
573   }
574 
575   // The number of parts is a power of 2.  Repeatedly bisect the value using
576   // EXTRACT_ELEMENT.
577   Parts[0] = DAG.getNode(ISD::BITCAST, DL,
578                          EVT::getIntegerVT(*DAG.getContext(),
579                                            ValueVT.getSizeInBits()),
580                          Val);
581 
582   for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
583     for (unsigned i = 0; i < NumParts; i += StepSize) {
584       unsigned ThisBits = StepSize * PartBits / 2;
585       EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
586       SDValue &Part0 = Parts[i];
587       SDValue &Part1 = Parts[i+StepSize/2];
588 
589       Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
590                           ThisVT, Part0, DAG.getIntPtrConstant(1, DL));
591       Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
592                           ThisVT, Part0, DAG.getIntPtrConstant(0, DL));
593 
594       if (ThisBits == PartBits && ThisVT != PartVT) {
595         Part0 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part0);
596         Part1 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part1);
597       }
598     }
599   }
600 
601   if (DAG.getDataLayout().isBigEndian())
602     std::reverse(Parts, Parts + OrigNumParts);
603 }
604 
605 static SDValue widenVectorToPartType(SelectionDAG &DAG, SDValue Val,
606                                      const SDLoc &DL, EVT PartVT) {
607   if (!PartVT.isVector())
608     return SDValue();
609 
610   EVT ValueVT = Val.getValueType();
611   ElementCount PartNumElts = PartVT.getVectorElementCount();
612   ElementCount ValueNumElts = ValueVT.getVectorElementCount();
613 
614   // We only support widening vectors with equivalent element types and
615   // fixed/scalable properties. If a target needs to widen a fixed-length type
616   // to a scalable one, it should be possible to use INSERT_SUBVECTOR below.
617   if (ElementCount::isKnownLE(PartNumElts, ValueNumElts) ||
618       PartNumElts.isScalable() != ValueNumElts.isScalable() ||
619       PartVT.getVectorElementType() != ValueVT.getVectorElementType())
620     return SDValue();
621 
622   // Widening a scalable vector to another scalable vector is done by inserting
623   // the vector into a larger undef one.
624   if (PartNumElts.isScalable())
625     return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, PartVT, DAG.getUNDEF(PartVT),
626                        Val, DAG.getVectorIdxConstant(0, DL));
627 
628   EVT ElementVT = PartVT.getVectorElementType();
629   // Vector widening case, e.g. <2 x float> -> <4 x float>.  Shuffle in
630   // undef elements.
631   SmallVector<SDValue, 16> Ops;
632   DAG.ExtractVectorElements(Val, Ops);
633   SDValue EltUndef = DAG.getUNDEF(ElementVT);
634   Ops.append((PartNumElts - ValueNumElts).getFixedValue(), EltUndef);
635 
636   // FIXME: Use CONCAT for 2x -> 4x.
637   return DAG.getBuildVector(PartVT, DL, Ops);
638 }
639 
640 /// getCopyToPartsVector - Create a series of nodes that contain the specified
641 /// value split into legal parts.
642 static void getCopyToPartsVector(SelectionDAG &DAG, const SDLoc &DL,
643                                  SDValue Val, SDValue *Parts, unsigned NumParts,
644                                  MVT PartVT, const Value *V,
645                                  Optional<CallingConv::ID> CallConv) {
646   EVT ValueVT = Val.getValueType();
647   assert(ValueVT.isVector() && "Not a vector");
648   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
649   const bool IsABIRegCopy = CallConv.hasValue();
650 
651   if (NumParts == 1) {
652     EVT PartEVT = PartVT;
653     if (PartEVT == ValueVT) {
654       // Nothing to do.
655     } else if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
656       // Bitconvert vector->vector case.
657       Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
658     } else if (SDValue Widened = widenVectorToPartType(DAG, Val, DL, PartVT)) {
659       Val = Widened;
660     } else if (PartVT.isVector() &&
661                PartEVT.getVectorElementType().bitsGE(
662                    ValueVT.getVectorElementType()) &&
663                PartEVT.getVectorElementCount() ==
664                    ValueVT.getVectorElementCount()) {
665 
666       // Promoted vector extract
667       Val = DAG.getAnyExtOrTrunc(Val, DL, PartVT);
668     } else if (PartEVT.isVector() &&
669                PartEVT.getVectorElementType() !=
670                    ValueVT.getVectorElementType() &&
671                TLI.getTypeAction(*DAG.getContext(), ValueVT) ==
672                    TargetLowering::TypeWidenVector) {
673       // Combination of widening and promotion.
674       EVT WidenVT =
675           EVT::getVectorVT(*DAG.getContext(), ValueVT.getVectorElementType(),
676                            PartVT.getVectorElementCount());
677       SDValue Widened = widenVectorToPartType(DAG, Val, DL, WidenVT);
678       Val = DAG.getAnyExtOrTrunc(Widened, DL, PartVT);
679     } else {
680       if (ValueVT.getVectorElementCount().isScalar()) {
681         Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, PartVT, Val,
682                           DAG.getVectorIdxConstant(0, DL));
683       } else {
684         uint64_t ValueSize = ValueVT.getFixedSizeInBits();
685         assert(PartVT.getFixedSizeInBits() > ValueSize &&
686                "lossy conversion of vector to scalar type");
687         EVT IntermediateType = EVT::getIntegerVT(*DAG.getContext(), ValueSize);
688         Val = DAG.getBitcast(IntermediateType, Val);
689         Val = DAG.getAnyExtOrTrunc(Val, DL, PartVT);
690       }
691     }
692 
693     assert(Val.getValueType() == PartVT && "Unexpected vector part value type");
694     Parts[0] = Val;
695     return;
696   }
697 
698   // Handle a multi-element vector.
699   EVT IntermediateVT;
700   MVT RegisterVT;
701   unsigned NumIntermediates;
702   unsigned NumRegs;
703   if (IsABIRegCopy) {
704     NumRegs = TLI.getVectorTypeBreakdownForCallingConv(
705         *DAG.getContext(), CallConv.getValue(), ValueVT, IntermediateVT,
706         NumIntermediates, RegisterVT);
707   } else {
708     NumRegs =
709         TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
710                                    NumIntermediates, RegisterVT);
711   }
712 
713   assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
714   NumParts = NumRegs; // Silence a compiler warning.
715   assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
716 
717   assert(IntermediateVT.isScalableVector() == ValueVT.isScalableVector() &&
718          "Mixing scalable and fixed vectors when copying in parts");
719 
720   Optional<ElementCount> DestEltCnt;
721 
722   if (IntermediateVT.isVector())
723     DestEltCnt = IntermediateVT.getVectorElementCount() * NumIntermediates;
724   else
725     DestEltCnt = ElementCount::getFixed(NumIntermediates);
726 
727   EVT BuiltVectorTy = EVT::getVectorVT(
728       *DAG.getContext(), IntermediateVT.getScalarType(), DestEltCnt.getValue());
729 
730   if (ValueVT == BuiltVectorTy) {
731     // Nothing to do.
732   } else if (ValueVT.getSizeInBits() == BuiltVectorTy.getSizeInBits()) {
733     // Bitconvert vector->vector case.
734     Val = DAG.getNode(ISD::BITCAST, DL, BuiltVectorTy, Val);
735   } else {
736     if (BuiltVectorTy.getVectorElementType().bitsGT(
737             ValueVT.getVectorElementType())) {
738       // Integer promotion.
739       ValueVT = EVT::getVectorVT(*DAG.getContext(),
740                                  BuiltVectorTy.getVectorElementType(),
741                                  ValueVT.getVectorElementCount());
742       Val = DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val);
743     }
744 
745     if (SDValue Widened = widenVectorToPartType(DAG, Val, DL, BuiltVectorTy)) {
746       Val = Widened;
747     }
748   }
749 
750   assert(Val.getValueType() == BuiltVectorTy && "Unexpected vector value type");
751 
752   // Split the vector into intermediate operands.
753   SmallVector<SDValue, 8> Ops(NumIntermediates);
754   for (unsigned i = 0; i != NumIntermediates; ++i) {
755     if (IntermediateVT.isVector()) {
756       // This does something sensible for scalable vectors - see the
757       // definition of EXTRACT_SUBVECTOR for further details.
758       unsigned IntermediateNumElts = IntermediateVT.getVectorMinNumElements();
759       Ops[i] =
760           DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, IntermediateVT, Val,
761                       DAG.getVectorIdxConstant(i * IntermediateNumElts, DL));
762     } else {
763       Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntermediateVT, Val,
764                            DAG.getVectorIdxConstant(i, DL));
765     }
766   }
767 
768   // Split the intermediate operands into legal parts.
769   if (NumParts == NumIntermediates) {
770     // If the register was not expanded, promote or copy the value,
771     // as appropriate.
772     for (unsigned i = 0; i != NumParts; ++i)
773       getCopyToParts(DAG, DL, Ops[i], &Parts[i], 1, PartVT, V, CallConv);
774   } else if (NumParts > 0) {
775     // If the intermediate type was expanded, split each the value into
776     // legal parts.
777     assert(NumIntermediates != 0 && "division by zero");
778     assert(NumParts % NumIntermediates == 0 &&
779            "Must expand into a divisible number of parts!");
780     unsigned Factor = NumParts / NumIntermediates;
781     for (unsigned i = 0; i != NumIntermediates; ++i)
782       getCopyToParts(DAG, DL, Ops[i], &Parts[i * Factor], Factor, PartVT, V,
783                      CallConv);
784   }
785 }
786 
787 RegsForValue::RegsForValue(const SmallVector<unsigned, 4> &regs, MVT regvt,
788                            EVT valuevt, Optional<CallingConv::ID> CC)
789     : ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs),
790       RegCount(1, regs.size()), CallConv(CC) {}
791 
792 RegsForValue::RegsForValue(LLVMContext &Context, const TargetLowering &TLI,
793                            const DataLayout &DL, unsigned Reg, Type *Ty,
794                            Optional<CallingConv::ID> CC) {
795   ComputeValueVTs(TLI, DL, Ty, ValueVTs);
796 
797   CallConv = CC;
798 
799   for (EVT ValueVT : ValueVTs) {
800     unsigned NumRegs =
801         isABIMangled()
802             ? TLI.getNumRegistersForCallingConv(Context, CC.getValue(), ValueVT)
803             : TLI.getNumRegisters(Context, ValueVT);
804     MVT RegisterVT =
805         isABIMangled()
806             ? TLI.getRegisterTypeForCallingConv(Context, CC.getValue(), ValueVT)
807             : TLI.getRegisterType(Context, ValueVT);
808     for (unsigned i = 0; i != NumRegs; ++i)
809       Regs.push_back(Reg + i);
810     RegVTs.push_back(RegisterVT);
811     RegCount.push_back(NumRegs);
812     Reg += NumRegs;
813   }
814 }
815 
816 SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG,
817                                       FunctionLoweringInfo &FuncInfo,
818                                       const SDLoc &dl, SDValue &Chain,
819                                       SDValue *Flag, const Value *V) const {
820   // A Value with type {} or [0 x %t] needs no registers.
821   if (ValueVTs.empty())
822     return SDValue();
823 
824   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
825 
826   // Assemble the legal parts into the final values.
827   SmallVector<SDValue, 4> Values(ValueVTs.size());
828   SmallVector<SDValue, 8> Parts;
829   for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
830     // Copy the legal parts from the registers.
831     EVT ValueVT = ValueVTs[Value];
832     unsigned NumRegs = RegCount[Value];
833     MVT RegisterVT = isABIMangled() ? TLI.getRegisterTypeForCallingConv(
834                                           *DAG.getContext(),
835                                           CallConv.getValue(), RegVTs[Value])
836                                     : RegVTs[Value];
837 
838     Parts.resize(NumRegs);
839     for (unsigned i = 0; i != NumRegs; ++i) {
840       SDValue P;
841       if (!Flag) {
842         P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
843       } else {
844         P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
845         *Flag = P.getValue(2);
846       }
847 
848       Chain = P.getValue(1);
849       Parts[i] = P;
850 
851       // If the source register was virtual and if we know something about it,
852       // add an assert node.
853       if (!Register::isVirtualRegister(Regs[Part + i]) ||
854           !RegisterVT.isInteger())
855         continue;
856 
857       const FunctionLoweringInfo::LiveOutInfo *LOI =
858         FuncInfo.GetLiveOutRegInfo(Regs[Part+i]);
859       if (!LOI)
860         continue;
861 
862       unsigned RegSize = RegisterVT.getScalarSizeInBits();
863       unsigned NumSignBits = LOI->NumSignBits;
864       unsigned NumZeroBits = LOI->Known.countMinLeadingZeros();
865 
866       if (NumZeroBits == RegSize) {
867         // The current value is a zero.
868         // Explicitly express that as it would be easier for
869         // optimizations to kick in.
870         Parts[i] = DAG.getConstant(0, dl, RegisterVT);
871         continue;
872       }
873 
874       // FIXME: We capture more information than the dag can represent.  For
875       // now, just use the tightest assertzext/assertsext possible.
876       bool isSExt;
877       EVT FromVT(MVT::Other);
878       if (NumZeroBits) {
879         FromVT = EVT::getIntegerVT(*DAG.getContext(), RegSize - NumZeroBits);
880         isSExt = false;
881       } else if (NumSignBits > 1) {
882         FromVT =
883             EVT::getIntegerVT(*DAG.getContext(), RegSize - NumSignBits + 1);
884         isSExt = true;
885       } else {
886         continue;
887       }
888       // Add an assertion node.
889       assert(FromVT != MVT::Other);
890       Parts[i] = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
891                              RegisterVT, P, DAG.getValueType(FromVT));
892     }
893 
894     Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(), NumRegs,
895                                      RegisterVT, ValueVT, V, CallConv);
896     Part += NumRegs;
897     Parts.clear();
898   }
899 
900   return DAG.getNode(ISD::MERGE_VALUES, dl, DAG.getVTList(ValueVTs), Values);
901 }
902 
903 void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG,
904                                  const SDLoc &dl, SDValue &Chain, SDValue *Flag,
905                                  const Value *V,
906                                  ISD::NodeType PreferredExtendType) const {
907   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
908   ISD::NodeType ExtendKind = PreferredExtendType;
909 
910   // Get the list of the values's legal parts.
911   unsigned NumRegs = Regs.size();
912   SmallVector<SDValue, 8> Parts(NumRegs);
913   for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
914     unsigned NumParts = RegCount[Value];
915 
916     MVT RegisterVT = isABIMangled() ? TLI.getRegisterTypeForCallingConv(
917                                           *DAG.getContext(),
918                                           CallConv.getValue(), RegVTs[Value])
919                                     : RegVTs[Value];
920 
921     if (ExtendKind == ISD::ANY_EXTEND && TLI.isZExtFree(Val, RegisterVT))
922       ExtendKind = ISD::ZERO_EXTEND;
923 
924     getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value), &Parts[Part],
925                    NumParts, RegisterVT, V, CallConv, ExtendKind);
926     Part += NumParts;
927   }
928 
929   // Copy the parts into the registers.
930   SmallVector<SDValue, 8> Chains(NumRegs);
931   for (unsigned i = 0; i != NumRegs; ++i) {
932     SDValue Part;
933     if (!Flag) {
934       Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
935     } else {
936       Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
937       *Flag = Part.getValue(1);
938     }
939 
940     Chains[i] = Part.getValue(0);
941   }
942 
943   if (NumRegs == 1 || Flag)
944     // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
945     // flagged to it. That is the CopyToReg nodes and the user are considered
946     // a single scheduling unit. If we create a TokenFactor and return it as
947     // chain, then the TokenFactor is both a predecessor (operand) of the
948     // user as well as a successor (the TF operands are flagged to the user).
949     // c1, f1 = CopyToReg
950     // c2, f2 = CopyToReg
951     // c3     = TokenFactor c1, c2
952     // ...
953     //        = op c3, ..., f2
954     Chain = Chains[NumRegs-1];
955   else
956     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
957 }
958 
959 void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching,
960                                         unsigned MatchingIdx, const SDLoc &dl,
961                                         SelectionDAG &DAG,
962                                         std::vector<SDValue> &Ops) const {
963   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
964 
965   unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size());
966   if (HasMatching)
967     Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx);
968   else if (!Regs.empty() && Register::isVirtualRegister(Regs.front())) {
969     // Put the register class of the virtual registers in the flag word.  That
970     // way, later passes can recompute register class constraints for inline
971     // assembly as well as normal instructions.
972     // Don't do this for tied operands that can use the regclass information
973     // from the def.
974     const MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
975     const TargetRegisterClass *RC = MRI.getRegClass(Regs.front());
976     Flag = InlineAsm::getFlagWordForRegClass(Flag, RC->getID());
977   }
978 
979   SDValue Res = DAG.getTargetConstant(Flag, dl, MVT::i32);
980   Ops.push_back(Res);
981 
982   if (Code == InlineAsm::Kind_Clobber) {
983     // Clobbers should always have a 1:1 mapping with registers, and may
984     // reference registers that have illegal (e.g. vector) types. Hence, we
985     // shouldn't try to apply any sort of splitting logic to them.
986     assert(Regs.size() == RegVTs.size() && Regs.size() == ValueVTs.size() &&
987            "No 1:1 mapping from clobbers to regs?");
988     Register SP = TLI.getStackPointerRegisterToSaveRestore();
989     (void)SP;
990     for (unsigned I = 0, E = ValueVTs.size(); I != E; ++I) {
991       Ops.push_back(DAG.getRegister(Regs[I], RegVTs[I]));
992       assert(
993           (Regs[I] != SP ||
994            DAG.getMachineFunction().getFrameInfo().hasOpaqueSPAdjustment()) &&
995           "If we clobbered the stack pointer, MFI should know about it.");
996     }
997     return;
998   }
999 
1000   for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
1001     MVT RegisterVT = RegVTs[Value];
1002     unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVTs[Value],
1003                                            RegisterVT);
1004     for (unsigned i = 0; i != NumRegs; ++i) {
1005       assert(Reg < Regs.size() && "Mismatch in # registers expected");
1006       unsigned TheReg = Regs[Reg++];
1007       Ops.push_back(DAG.getRegister(TheReg, RegisterVT));
1008     }
1009   }
1010 }
1011 
1012 SmallVector<std::pair<unsigned, TypeSize>, 4>
1013 RegsForValue::getRegsAndSizes() const {
1014   SmallVector<std::pair<unsigned, TypeSize>, 4> OutVec;
1015   unsigned I = 0;
1016   for (auto CountAndVT : zip_first(RegCount, RegVTs)) {
1017     unsigned RegCount = std::get<0>(CountAndVT);
1018     MVT RegisterVT = std::get<1>(CountAndVT);
1019     TypeSize RegisterSize = RegisterVT.getSizeInBits();
1020     for (unsigned E = I + RegCount; I != E; ++I)
1021       OutVec.push_back(std::make_pair(Regs[I], RegisterSize));
1022   }
1023   return OutVec;
1024 }
1025 
1026 void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis *aa,
1027                                const TargetLibraryInfo *li) {
1028   AA = aa;
1029   GFI = gfi;
1030   LibInfo = li;
1031   Context = DAG.getContext();
1032   LPadToCallSiteMap.clear();
1033   SL->init(DAG.getTargetLoweringInfo(), TM, DAG.getDataLayout());
1034 }
1035 
1036 void SelectionDAGBuilder::clear() {
1037   NodeMap.clear();
1038   UnusedArgNodeMap.clear();
1039   PendingLoads.clear();
1040   PendingExports.clear();
1041   PendingConstrainedFP.clear();
1042   PendingConstrainedFPStrict.clear();
1043   CurInst = nullptr;
1044   HasTailCall = false;
1045   SDNodeOrder = LowestSDNodeOrder;
1046   StatepointLowering.clear();
1047 }
1048 
1049 void SelectionDAGBuilder::clearDanglingDebugInfo() {
1050   DanglingDebugInfoMap.clear();
1051 }
1052 
1053 // Update DAG root to include dependencies on Pending chains.
1054 SDValue SelectionDAGBuilder::updateRoot(SmallVectorImpl<SDValue> &Pending) {
1055   SDValue Root = DAG.getRoot();
1056 
1057   if (Pending.empty())
1058     return Root;
1059 
1060   // Add current root to PendingChains, unless we already indirectly
1061   // depend on it.
1062   if (Root.getOpcode() != ISD::EntryToken) {
1063     unsigned i = 0, e = Pending.size();
1064     for (; i != e; ++i) {
1065       assert(Pending[i].getNode()->getNumOperands() > 1);
1066       if (Pending[i].getNode()->getOperand(0) == Root)
1067         break;  // Don't add the root if we already indirectly depend on it.
1068     }
1069 
1070     if (i == e)
1071       Pending.push_back(Root);
1072   }
1073 
1074   if (Pending.size() == 1)
1075     Root = Pending[0];
1076   else
1077     Root = DAG.getTokenFactor(getCurSDLoc(), Pending);
1078 
1079   DAG.setRoot(Root);
1080   Pending.clear();
1081   return Root;
1082 }
1083 
1084 SDValue SelectionDAGBuilder::getMemoryRoot() {
1085   return updateRoot(PendingLoads);
1086 }
1087 
1088 SDValue SelectionDAGBuilder::getRoot() {
1089   // Chain up all pending constrained intrinsics together with all
1090   // pending loads, by simply appending them to PendingLoads and
1091   // then calling getMemoryRoot().
1092   PendingLoads.reserve(PendingLoads.size() +
1093                        PendingConstrainedFP.size() +
1094                        PendingConstrainedFPStrict.size());
1095   PendingLoads.append(PendingConstrainedFP.begin(),
1096                       PendingConstrainedFP.end());
1097   PendingLoads.append(PendingConstrainedFPStrict.begin(),
1098                       PendingConstrainedFPStrict.end());
1099   PendingConstrainedFP.clear();
1100   PendingConstrainedFPStrict.clear();
1101   return getMemoryRoot();
1102 }
1103 
1104 SDValue SelectionDAGBuilder::getControlRoot() {
1105   // We need to emit pending fpexcept.strict constrained intrinsics,
1106   // so append them to the PendingExports list.
1107   PendingExports.append(PendingConstrainedFPStrict.begin(),
1108                         PendingConstrainedFPStrict.end());
1109   PendingConstrainedFPStrict.clear();
1110   return updateRoot(PendingExports);
1111 }
1112 
1113 void SelectionDAGBuilder::visit(const Instruction &I) {
1114   // Set up outgoing PHI node register values before emitting the terminator.
1115   if (I.isTerminator()) {
1116     HandlePHINodesInSuccessorBlocks(I.getParent());
1117   }
1118 
1119   // Increase the SDNodeOrder if dealing with a non-debug instruction.
1120   if (!isa<DbgInfoIntrinsic>(I))
1121     ++SDNodeOrder;
1122 
1123   CurInst = &I;
1124 
1125   visit(I.getOpcode(), I);
1126 
1127   if (!I.isTerminator() && !HasTailCall &&
1128       !isa<GCStatepointInst>(I)) // statepoints handle their exports internally
1129     CopyToExportRegsIfNeeded(&I);
1130 
1131   CurInst = nullptr;
1132 }
1133 
1134 void SelectionDAGBuilder::visitPHI(const PHINode &) {
1135   llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
1136 }
1137 
1138 void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
1139   // Note: this doesn't use InstVisitor, because it has to work with
1140   // ConstantExpr's in addition to instructions.
1141   switch (Opcode) {
1142   default: llvm_unreachable("Unknown instruction type encountered!");
1143     // Build the switch statement using the Instruction.def file.
1144 #define HANDLE_INST(NUM, OPCODE, CLASS) \
1145     case Instruction::OPCODE: visit##OPCODE((const CLASS&)I); break;
1146 #include "llvm/IR/Instruction.def"
1147   }
1148 }
1149 
1150 void SelectionDAGBuilder::addDanglingDebugInfo(const DbgValueInst *DI,
1151                                                DebugLoc DL, unsigned Order) {
1152   // We treat variadic dbg_values differently at this stage.
1153   if (DI->hasArgList()) {
1154     // For variadic dbg_values we will now insert an undef.
1155     // FIXME: We can potentially recover these!
1156     SmallVector<SDDbgOperand, 2> Locs;
1157     for (const Value *V : DI->getValues()) {
1158       auto Undef = UndefValue::get(V->getType());
1159       Locs.push_back(SDDbgOperand::fromConst(Undef));
1160     }
1161     SDDbgValue *SDV = DAG.getDbgValueList(
1162         DI->getVariable(), DI->getExpression(), Locs, {},
1163         /*IsIndirect=*/false, DL, Order, /*IsVariadic=*/true);
1164     DAG.AddDbgValue(SDV, /*isParameter=*/false);
1165   } else {
1166     // TODO: Dangling debug info will eventually either be resolved or produce
1167     // an Undef DBG_VALUE. However in the resolution case, a gap may appear
1168     // between the original dbg.value location and its resolved DBG_VALUE,
1169     // which we should ideally fill with an extra Undef DBG_VALUE.
1170     assert(DI->getNumVariableLocationOps() == 1 &&
1171            "DbgValueInst without an ArgList should have a single location "
1172            "operand.");
1173     DanglingDebugInfoMap[DI->getValue(0)].emplace_back(DI, DL, Order);
1174   }
1175 }
1176 
1177 void SelectionDAGBuilder::dropDanglingDebugInfo(const DILocalVariable *Variable,
1178                                                 const DIExpression *Expr) {
1179   auto isMatchingDbgValue = [&](DanglingDebugInfo &DDI) {
1180     const DbgValueInst *DI = DDI.getDI();
1181     DIVariable *DanglingVariable = DI->getVariable();
1182     DIExpression *DanglingExpr = DI->getExpression();
1183     if (DanglingVariable == Variable && Expr->fragmentsOverlap(DanglingExpr)) {
1184       LLVM_DEBUG(dbgs() << "Dropping dangling debug info for " << *DI << "\n");
1185       return true;
1186     }
1187     return false;
1188   };
1189 
1190   for (auto &DDIMI : DanglingDebugInfoMap) {
1191     DanglingDebugInfoVector &DDIV = DDIMI.second;
1192 
1193     // If debug info is to be dropped, run it through final checks to see
1194     // whether it can be salvaged.
1195     for (auto &DDI : DDIV)
1196       if (isMatchingDbgValue(DDI))
1197         salvageUnresolvedDbgValue(DDI);
1198 
1199     erase_if(DDIV, isMatchingDbgValue);
1200   }
1201 }
1202 
1203 // resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V,
1204 // generate the debug data structures now that we've seen its definition.
1205 void SelectionDAGBuilder::resolveDanglingDebugInfo(const Value *V,
1206                                                    SDValue Val) {
1207   auto DanglingDbgInfoIt = DanglingDebugInfoMap.find(V);
1208   if (DanglingDbgInfoIt == DanglingDebugInfoMap.end())
1209     return;
1210 
1211   DanglingDebugInfoVector &DDIV = DanglingDbgInfoIt->second;
1212   for (auto &DDI : DDIV) {
1213     const DbgValueInst *DI = DDI.getDI();
1214     assert(!DI->hasArgList() && "Not implemented for variadic dbg_values");
1215     assert(DI && "Ill-formed DanglingDebugInfo");
1216     DebugLoc dl = DDI.getdl();
1217     unsigned ValSDNodeOrder = Val.getNode()->getIROrder();
1218     unsigned DbgSDNodeOrder = DDI.getSDNodeOrder();
1219     DILocalVariable *Variable = DI->getVariable();
1220     DIExpression *Expr = DI->getExpression();
1221     assert(Variable->isValidLocationForIntrinsic(dl) &&
1222            "Expected inlined-at fields to agree");
1223     SDDbgValue *SDV;
1224     if (Val.getNode()) {
1225       // FIXME: I doubt that it is correct to resolve a dangling DbgValue as a
1226       // FuncArgumentDbgValue (it would be hoisted to the function entry, and if
1227       // we couldn't resolve it directly when examining the DbgValue intrinsic
1228       // in the first place we should not be more successful here). Unless we
1229       // have some test case that prove this to be correct we should avoid
1230       // calling EmitFuncArgumentDbgValue here.
1231       if (!EmitFuncArgumentDbgValue(V, Variable, Expr, dl,
1232                                     FuncArgumentDbgValueKind::Value, Val)) {
1233         LLVM_DEBUG(dbgs() << "Resolve dangling debug info [order="
1234                           << DbgSDNodeOrder << "] for:\n  " << *DI << "\n");
1235         LLVM_DEBUG(dbgs() << "  By mapping to:\n    "; Val.dump());
1236         // Increase the SDNodeOrder for the DbgValue here to make sure it is
1237         // inserted after the definition of Val when emitting the instructions
1238         // after ISel. An alternative could be to teach
1239         // ScheduleDAGSDNodes::EmitSchedule to delay the insertion properly.
1240         LLVM_DEBUG(if (ValSDNodeOrder > DbgSDNodeOrder) dbgs()
1241                    << "changing SDNodeOrder from " << DbgSDNodeOrder << " to "
1242                    << ValSDNodeOrder << "\n");
1243         SDV = getDbgValue(Val, Variable, Expr, dl,
1244                           std::max(DbgSDNodeOrder, ValSDNodeOrder));
1245         DAG.AddDbgValue(SDV, false);
1246       } else
1247         LLVM_DEBUG(dbgs() << "Resolved dangling debug info for " << *DI
1248                           << "in EmitFuncArgumentDbgValue\n");
1249     } else {
1250       LLVM_DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
1251       auto Undef = UndefValue::get(DDI.getDI()->getValue(0)->getType());
1252       auto SDV =
1253           DAG.getConstantDbgValue(Variable, Expr, Undef, dl, DbgSDNodeOrder);
1254       DAG.AddDbgValue(SDV, false);
1255     }
1256   }
1257   DDIV.clear();
1258 }
1259 
1260 void SelectionDAGBuilder::salvageUnresolvedDbgValue(DanglingDebugInfo &DDI) {
1261   // TODO: For the variadic implementation, instead of only checking the fail
1262   // state of `handleDebugValue`, we need know specifically which values were
1263   // invalid, so that we attempt to salvage only those values when processing
1264   // a DIArgList.
1265   assert(!DDI.getDI()->hasArgList() &&
1266          "Not implemented for variadic dbg_values");
1267   Value *V = DDI.getDI()->getValue(0);
1268   DILocalVariable *Var = DDI.getDI()->getVariable();
1269   DIExpression *Expr = DDI.getDI()->getExpression();
1270   DebugLoc DL = DDI.getdl();
1271   DebugLoc InstDL = DDI.getDI()->getDebugLoc();
1272   unsigned SDOrder = DDI.getSDNodeOrder();
1273   // Currently we consider only dbg.value intrinsics -- we tell the salvager
1274   // that DW_OP_stack_value is desired.
1275   assert(isa<DbgValueInst>(DDI.getDI()));
1276   bool StackValue = true;
1277 
1278   // Can this Value can be encoded without any further work?
1279   if (handleDebugValue(V, Var, Expr, DL, InstDL, SDOrder, /*IsVariadic=*/false))
1280     return;
1281 
1282   // Attempt to salvage back through as many instructions as possible. Bail if
1283   // a non-instruction is seen, such as a constant expression or global
1284   // variable. FIXME: Further work could recover those too.
1285   while (isa<Instruction>(V)) {
1286     Instruction &VAsInst = *cast<Instruction>(V);
1287     // Temporary "0", awaiting real implementation.
1288     SmallVector<uint64_t, 16> Ops;
1289     SmallVector<Value *, 4> AdditionalValues;
1290     V = salvageDebugInfoImpl(VAsInst, Expr->getNumLocationOperands(), Ops,
1291                              AdditionalValues);
1292     // If we cannot salvage any further, and haven't yet found a suitable debug
1293     // expression, bail out.
1294     if (!V)
1295       break;
1296 
1297     // TODO: If AdditionalValues isn't empty, then the salvage can only be
1298     // represented with a DBG_VALUE_LIST, so we give up. When we have support
1299     // here for variadic dbg_values, remove that condition.
1300     if (!AdditionalValues.empty())
1301       break;
1302 
1303     // New value and expr now represent this debuginfo.
1304     Expr = DIExpression::appendOpsToArg(Expr, Ops, 0, StackValue);
1305 
1306     // Some kind of simplification occurred: check whether the operand of the
1307     // salvaged debug expression can be encoded in this DAG.
1308     if (handleDebugValue(V, Var, Expr, DL, InstDL, SDOrder,
1309                          /*IsVariadic=*/false)) {
1310       LLVM_DEBUG(dbgs() << "Salvaged debug location info for:\n  "
1311                         << DDI.getDI() << "\nBy stripping back to:\n  " << V);
1312       return;
1313     }
1314   }
1315 
1316   // This was the final opportunity to salvage this debug information, and it
1317   // couldn't be done. Place an undef DBG_VALUE at this location to terminate
1318   // any earlier variable location.
1319   auto Undef = UndefValue::get(DDI.getDI()->getValue(0)->getType());
1320   auto SDV = DAG.getConstantDbgValue(Var, Expr, Undef, DL, SDNodeOrder);
1321   DAG.AddDbgValue(SDV, false);
1322 
1323   LLVM_DEBUG(dbgs() << "Dropping debug value info for:\n  " << DDI.getDI()
1324                     << "\n");
1325   LLVM_DEBUG(dbgs() << "  Last seen at:\n    " << *DDI.getDI()->getOperand(0)
1326                     << "\n");
1327 }
1328 
1329 bool SelectionDAGBuilder::handleDebugValue(ArrayRef<const Value *> Values,
1330                                            DILocalVariable *Var,
1331                                            DIExpression *Expr, DebugLoc dl,
1332                                            DebugLoc InstDL, unsigned Order,
1333                                            bool IsVariadic) {
1334   if (Values.empty())
1335     return true;
1336   SmallVector<SDDbgOperand> LocationOps;
1337   SmallVector<SDNode *> Dependencies;
1338   for (const Value *V : Values) {
1339     // Constant value.
1340     if (isa<ConstantInt>(V) || isa<ConstantFP>(V) || isa<UndefValue>(V) ||
1341         isa<ConstantPointerNull>(V)) {
1342       LocationOps.emplace_back(SDDbgOperand::fromConst(V));
1343       continue;
1344     }
1345 
1346     // If the Value is a frame index, we can create a FrameIndex debug value
1347     // without relying on the DAG at all.
1348     if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1349       auto SI = FuncInfo.StaticAllocaMap.find(AI);
1350       if (SI != FuncInfo.StaticAllocaMap.end()) {
1351         LocationOps.emplace_back(SDDbgOperand::fromFrameIdx(SI->second));
1352         continue;
1353       }
1354     }
1355 
1356     // Do not use getValue() in here; we don't want to generate code at
1357     // this point if it hasn't been done yet.
1358     SDValue N = NodeMap[V];
1359     if (!N.getNode() && isa<Argument>(V)) // Check unused arguments map.
1360       N = UnusedArgNodeMap[V];
1361     if (N.getNode()) {
1362       // Only emit func arg dbg value for non-variadic dbg.values for now.
1363       if (!IsVariadic &&
1364           EmitFuncArgumentDbgValue(V, Var, Expr, dl,
1365                                    FuncArgumentDbgValueKind::Value, N))
1366         return true;
1367       if (auto *FISDN = dyn_cast<FrameIndexSDNode>(N.getNode())) {
1368         // Construct a FrameIndexDbgValue for FrameIndexSDNodes so we can
1369         // describe stack slot locations.
1370         //
1371         // Consider "int x = 0; int *px = &x;". There are two kinds of
1372         // interesting debug values here after optimization:
1373         //
1374         //   dbg.value(i32* %px, !"int *px", !DIExpression()), and
1375         //   dbg.value(i32* %px, !"int x", !DIExpression(DW_OP_deref))
1376         //
1377         // Both describe the direct values of their associated variables.
1378         Dependencies.push_back(N.getNode());
1379         LocationOps.emplace_back(SDDbgOperand::fromFrameIdx(FISDN->getIndex()));
1380         continue;
1381       }
1382       LocationOps.emplace_back(
1383           SDDbgOperand::fromNode(N.getNode(), N.getResNo()));
1384       continue;
1385     }
1386 
1387     const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1388     // Special rules apply for the first dbg.values of parameter variables in a
1389     // function. Identify them by the fact they reference Argument Values, that
1390     // they're parameters, and they are parameters of the current function. We
1391     // need to let them dangle until they get an SDNode.
1392     bool IsParamOfFunc =
1393         isa<Argument>(V) && Var->isParameter() && !InstDL.getInlinedAt();
1394     if (IsParamOfFunc)
1395       return false;
1396 
1397     // The value is not used in this block yet (or it would have an SDNode).
1398     // We still want the value to appear for the user if possible -- if it has
1399     // an associated VReg, we can refer to that instead.
1400     auto VMI = FuncInfo.ValueMap.find(V);
1401     if (VMI != FuncInfo.ValueMap.end()) {
1402       unsigned Reg = VMI->second;
1403       // If this is a PHI node, it may be split up into several MI PHI nodes
1404       // (in FunctionLoweringInfo::set).
1405       RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg,
1406                        V->getType(), None);
1407       if (RFV.occupiesMultipleRegs()) {
1408         // FIXME: We could potentially support variadic dbg_values here.
1409         if (IsVariadic)
1410           return false;
1411         unsigned Offset = 0;
1412         unsigned BitsToDescribe = 0;
1413         if (auto VarSize = Var->getSizeInBits())
1414           BitsToDescribe = *VarSize;
1415         if (auto Fragment = Expr->getFragmentInfo())
1416           BitsToDescribe = Fragment->SizeInBits;
1417         for (const auto &RegAndSize : RFV.getRegsAndSizes()) {
1418           // Bail out if all bits are described already.
1419           if (Offset >= BitsToDescribe)
1420             break;
1421           // TODO: handle scalable vectors.
1422           unsigned RegisterSize = RegAndSize.second;
1423           unsigned FragmentSize = (Offset + RegisterSize > BitsToDescribe)
1424                                       ? BitsToDescribe - Offset
1425                                       : RegisterSize;
1426           auto FragmentExpr = DIExpression::createFragmentExpression(
1427               Expr, Offset, FragmentSize);
1428           if (!FragmentExpr)
1429             continue;
1430           SDDbgValue *SDV = DAG.getVRegDbgValue(
1431               Var, *FragmentExpr, RegAndSize.first, false, dl, SDNodeOrder);
1432           DAG.AddDbgValue(SDV, false);
1433           Offset += RegisterSize;
1434         }
1435         return true;
1436       }
1437       // We can use simple vreg locations for variadic dbg_values as well.
1438       LocationOps.emplace_back(SDDbgOperand::fromVReg(Reg));
1439       continue;
1440     }
1441     // We failed to create a SDDbgOperand for V.
1442     return false;
1443   }
1444 
1445   // We have created a SDDbgOperand for each Value in Values.
1446   // Should use Order instead of SDNodeOrder?
1447   assert(!LocationOps.empty());
1448   SDDbgValue *SDV =
1449       DAG.getDbgValueList(Var, Expr, LocationOps, Dependencies,
1450                           /*IsIndirect=*/false, dl, SDNodeOrder, IsVariadic);
1451   DAG.AddDbgValue(SDV, /*isParameter=*/false);
1452   return true;
1453 }
1454 
1455 void SelectionDAGBuilder::resolveOrClearDbgInfo() {
1456   // Try to fixup any remaining dangling debug info -- and drop it if we can't.
1457   for (auto &Pair : DanglingDebugInfoMap)
1458     for (auto &DDI : Pair.second)
1459       salvageUnresolvedDbgValue(DDI);
1460   clearDanglingDebugInfo();
1461 }
1462 
1463 /// getCopyFromRegs - If there was virtual register allocated for the value V
1464 /// emit CopyFromReg of the specified type Ty. Return empty SDValue() otherwise.
1465 SDValue SelectionDAGBuilder::getCopyFromRegs(const Value *V, Type *Ty) {
1466   DenseMap<const Value *, Register>::iterator It = FuncInfo.ValueMap.find(V);
1467   SDValue Result;
1468 
1469   if (It != FuncInfo.ValueMap.end()) {
1470     Register InReg = It->second;
1471 
1472     RegsForValue RFV(*DAG.getContext(), DAG.getTargetLoweringInfo(),
1473                      DAG.getDataLayout(), InReg, Ty,
1474                      None); // This is not an ABI copy.
1475     SDValue Chain = DAG.getEntryNode();
1476     Result = RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, nullptr,
1477                                  V);
1478     resolveDanglingDebugInfo(V, Result);
1479   }
1480 
1481   return Result;
1482 }
1483 
1484 /// getValue - Return an SDValue for the given Value.
1485 SDValue SelectionDAGBuilder::getValue(const Value *V) {
1486   // If we already have an SDValue for this value, use it. It's important
1487   // to do this first, so that we don't create a CopyFromReg if we already
1488   // have a regular SDValue.
1489   SDValue &N = NodeMap[V];
1490   if (N.getNode()) return N;
1491 
1492   // If there's a virtual register allocated and initialized for this
1493   // value, use it.
1494   if (SDValue copyFromReg = getCopyFromRegs(V, V->getType()))
1495     return copyFromReg;
1496 
1497   // Otherwise create a new SDValue and remember it.
1498   SDValue Val = getValueImpl(V);
1499   NodeMap[V] = Val;
1500   resolveDanglingDebugInfo(V, Val);
1501   return Val;
1502 }
1503 
1504 /// getNonRegisterValue - Return an SDValue for the given Value, but
1505 /// don't look in FuncInfo.ValueMap for a virtual register.
1506 SDValue SelectionDAGBuilder::getNonRegisterValue(const Value *V) {
1507   // If we already have an SDValue for this value, use it.
1508   SDValue &N = NodeMap[V];
1509   if (N.getNode()) {
1510     if (isa<ConstantSDNode>(N) || isa<ConstantFPSDNode>(N)) {
1511       // Remove the debug location from the node as the node is about to be used
1512       // in a location which may differ from the original debug location.  This
1513       // is relevant to Constant and ConstantFP nodes because they can appear
1514       // as constant expressions inside PHI nodes.
1515       N->setDebugLoc(DebugLoc());
1516     }
1517     return N;
1518   }
1519 
1520   // Otherwise create a new SDValue and remember it.
1521   SDValue Val = getValueImpl(V);
1522   NodeMap[V] = Val;
1523   resolveDanglingDebugInfo(V, Val);
1524   return Val;
1525 }
1526 
1527 /// getValueImpl - Helper function for getValue and getNonRegisterValue.
1528 /// Create an SDValue for the given value.
1529 SDValue SelectionDAGBuilder::getValueImpl(const Value *V) {
1530   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1531 
1532   if (const Constant *C = dyn_cast<Constant>(V)) {
1533     EVT VT = TLI.getValueType(DAG.getDataLayout(), V->getType(), true);
1534 
1535     if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
1536       return DAG.getConstant(*CI, getCurSDLoc(), VT);
1537 
1538     if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
1539       return DAG.getGlobalAddress(GV, getCurSDLoc(), VT);
1540 
1541     if (isa<ConstantPointerNull>(C)) {
1542       unsigned AS = V->getType()->getPointerAddressSpace();
1543       return DAG.getConstant(0, getCurSDLoc(),
1544                              TLI.getPointerTy(DAG.getDataLayout(), AS));
1545     }
1546 
1547     if (match(C, m_VScale(DAG.getDataLayout())))
1548       return DAG.getVScale(getCurSDLoc(), VT, APInt(VT.getSizeInBits(), 1));
1549 
1550     if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
1551       return DAG.getConstantFP(*CFP, getCurSDLoc(), VT);
1552 
1553     if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
1554       return DAG.getUNDEF(VT);
1555 
1556     if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
1557       visit(CE->getOpcode(), *CE);
1558       SDValue N1 = NodeMap[V];
1559       assert(N1.getNode() && "visit didn't populate the NodeMap!");
1560       return N1;
1561     }
1562 
1563     if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
1564       SmallVector<SDValue, 4> Constants;
1565       for (const Use &U : C->operands()) {
1566         SDNode *Val = getValue(U).getNode();
1567         // If the operand is an empty aggregate, there are no values.
1568         if (!Val) continue;
1569         // Add each leaf value from the operand to the Constants list
1570         // to form a flattened list of all the values.
1571         for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1572           Constants.push_back(SDValue(Val, i));
1573       }
1574 
1575       return DAG.getMergeValues(Constants, getCurSDLoc());
1576     }
1577 
1578     if (const ConstantDataSequential *CDS =
1579           dyn_cast<ConstantDataSequential>(C)) {
1580       SmallVector<SDValue, 4> Ops;
1581       for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
1582         SDNode *Val = getValue(CDS->getElementAsConstant(i)).getNode();
1583         // Add each leaf value from the operand to the Constants list
1584         // to form a flattened list of all the values.
1585         for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1586           Ops.push_back(SDValue(Val, i));
1587       }
1588 
1589       if (isa<ArrayType>(CDS->getType()))
1590         return DAG.getMergeValues(Ops, getCurSDLoc());
1591       return NodeMap[V] = DAG.getBuildVector(VT, getCurSDLoc(), Ops);
1592     }
1593 
1594     if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
1595       assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
1596              "Unknown struct or array constant!");
1597 
1598       SmallVector<EVT, 4> ValueVTs;
1599       ComputeValueVTs(TLI, DAG.getDataLayout(), C->getType(), ValueVTs);
1600       unsigned NumElts = ValueVTs.size();
1601       if (NumElts == 0)
1602         return SDValue(); // empty struct
1603       SmallVector<SDValue, 4> Constants(NumElts);
1604       for (unsigned i = 0; i != NumElts; ++i) {
1605         EVT EltVT = ValueVTs[i];
1606         if (isa<UndefValue>(C))
1607           Constants[i] = DAG.getUNDEF(EltVT);
1608         else if (EltVT.isFloatingPoint())
1609           Constants[i] = DAG.getConstantFP(0, getCurSDLoc(), EltVT);
1610         else
1611           Constants[i] = DAG.getConstant(0, getCurSDLoc(), EltVT);
1612       }
1613 
1614       return DAG.getMergeValues(Constants, getCurSDLoc());
1615     }
1616 
1617     if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
1618       return DAG.getBlockAddress(BA, VT);
1619 
1620     if (const auto *Equiv = dyn_cast<DSOLocalEquivalent>(C))
1621       return getValue(Equiv->getGlobalValue());
1622 
1623     if (const auto *NC = dyn_cast<NoCFIValue>(C))
1624       return getValue(NC->getGlobalValue());
1625 
1626     VectorType *VecTy = cast<VectorType>(V->getType());
1627 
1628     // Now that we know the number and type of the elements, get that number of
1629     // elements into the Ops array based on what kind of constant it is.
1630     if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
1631       SmallVector<SDValue, 16> Ops;
1632       unsigned NumElements = cast<FixedVectorType>(VecTy)->getNumElements();
1633       for (unsigned i = 0; i != NumElements; ++i)
1634         Ops.push_back(getValue(CV->getOperand(i)));
1635 
1636       return NodeMap[V] = DAG.getBuildVector(VT, getCurSDLoc(), Ops);
1637     }
1638 
1639     if (isa<ConstantAggregateZero>(C)) {
1640       EVT EltVT =
1641           TLI.getValueType(DAG.getDataLayout(), VecTy->getElementType());
1642 
1643       SDValue Op;
1644       if (EltVT.isFloatingPoint())
1645         Op = DAG.getConstantFP(0, getCurSDLoc(), EltVT);
1646       else
1647         Op = DAG.getConstant(0, getCurSDLoc(), EltVT);
1648 
1649       if (isa<ScalableVectorType>(VecTy))
1650         return NodeMap[V] = DAG.getSplatVector(VT, getCurSDLoc(), Op);
1651 
1652       SmallVector<SDValue, 16> Ops;
1653       Ops.assign(cast<FixedVectorType>(VecTy)->getNumElements(), Op);
1654       return NodeMap[V] = DAG.getBuildVector(VT, getCurSDLoc(), Ops);
1655     }
1656 
1657     llvm_unreachable("Unknown vector constant");
1658   }
1659 
1660   // If this is a static alloca, generate it as the frameindex instead of
1661   // computation.
1662   if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1663     DenseMap<const AllocaInst*, int>::iterator SI =
1664       FuncInfo.StaticAllocaMap.find(AI);
1665     if (SI != FuncInfo.StaticAllocaMap.end())
1666       return DAG.getFrameIndex(SI->second,
1667                                TLI.getFrameIndexTy(DAG.getDataLayout()));
1668   }
1669 
1670   // If this is an instruction which fast-isel has deferred, select it now.
1671   if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
1672     unsigned InReg = FuncInfo.InitializeRegForValue(Inst);
1673 
1674     RegsForValue RFV(*DAG.getContext(), TLI, DAG.getDataLayout(), InReg,
1675                      Inst->getType(), None);
1676     SDValue Chain = DAG.getEntryNode();
1677     return RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, V);
1678   }
1679 
1680   if (const MetadataAsValue *MD = dyn_cast<MetadataAsValue>(V))
1681     return DAG.getMDNode(cast<MDNode>(MD->getMetadata()));
1682 
1683   if (const auto *BB = dyn_cast<BasicBlock>(V))
1684     return DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
1685 
1686   llvm_unreachable("Can't get register for value!");
1687 }
1688 
1689 void SelectionDAGBuilder::visitCatchPad(const CatchPadInst &I) {
1690   auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
1691   bool IsMSVCCXX = Pers == EHPersonality::MSVC_CXX;
1692   bool IsCoreCLR = Pers == EHPersonality::CoreCLR;
1693   bool IsSEH = isAsynchronousEHPersonality(Pers);
1694   MachineBasicBlock *CatchPadMBB = FuncInfo.MBB;
1695   if (!IsSEH)
1696     CatchPadMBB->setIsEHScopeEntry();
1697   // In MSVC C++ and CoreCLR, catchblocks are funclets and need prologues.
1698   if (IsMSVCCXX || IsCoreCLR)
1699     CatchPadMBB->setIsEHFuncletEntry();
1700 }
1701 
1702 void SelectionDAGBuilder::visitCatchRet(const CatchReturnInst &I) {
1703   // Update machine-CFG edge.
1704   MachineBasicBlock *TargetMBB = FuncInfo.MBBMap[I.getSuccessor()];
1705   FuncInfo.MBB->addSuccessor(TargetMBB);
1706   TargetMBB->setIsEHCatchretTarget(true);
1707   DAG.getMachineFunction().setHasEHCatchret(true);
1708 
1709   auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
1710   bool IsSEH = isAsynchronousEHPersonality(Pers);
1711   if (IsSEH) {
1712     // If this is not a fall-through branch or optimizations are switched off,
1713     // emit the branch.
1714     if (TargetMBB != NextBlock(FuncInfo.MBB) ||
1715         TM.getOptLevel() == CodeGenOpt::None)
1716       DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
1717                               getControlRoot(), DAG.getBasicBlock(TargetMBB)));
1718     return;
1719   }
1720 
1721   // Figure out the funclet membership for the catchret's successor.
1722   // This will be used by the FuncletLayout pass to determine how to order the
1723   // BB's.
1724   // A 'catchret' returns to the outer scope's color.
1725   Value *ParentPad = I.getCatchSwitchParentPad();
1726   const BasicBlock *SuccessorColor;
1727   if (isa<ConstantTokenNone>(ParentPad))
1728     SuccessorColor = &FuncInfo.Fn->getEntryBlock();
1729   else
1730     SuccessorColor = cast<Instruction>(ParentPad)->getParent();
1731   assert(SuccessorColor && "No parent funclet for catchret!");
1732   MachineBasicBlock *SuccessorColorMBB = FuncInfo.MBBMap[SuccessorColor];
1733   assert(SuccessorColorMBB && "No MBB for SuccessorColor!");
1734 
1735   // Create the terminator node.
1736   SDValue Ret = DAG.getNode(ISD::CATCHRET, getCurSDLoc(), MVT::Other,
1737                             getControlRoot(), DAG.getBasicBlock(TargetMBB),
1738                             DAG.getBasicBlock(SuccessorColorMBB));
1739   DAG.setRoot(Ret);
1740 }
1741 
1742 void SelectionDAGBuilder::visitCleanupPad(const CleanupPadInst &CPI) {
1743   // Don't emit any special code for the cleanuppad instruction. It just marks
1744   // the start of an EH scope/funclet.
1745   FuncInfo.MBB->setIsEHScopeEntry();
1746   auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
1747   if (Pers != EHPersonality::Wasm_CXX) {
1748     FuncInfo.MBB->setIsEHFuncletEntry();
1749     FuncInfo.MBB->setIsCleanupFuncletEntry();
1750   }
1751 }
1752 
1753 // In wasm EH, even though a catchpad may not catch an exception if a tag does
1754 // not match, it is OK to add only the first unwind destination catchpad to the
1755 // successors, because there will be at least one invoke instruction within the
1756 // catch scope that points to the next unwind destination, if one exists, so
1757 // CFGSort cannot mess up with BB sorting order.
1758 // (All catchpads with 'catch (type)' clauses have a 'llvm.rethrow' intrinsic
1759 // call within them, and catchpads only consisting of 'catch (...)' have a
1760 // '__cxa_end_catch' call within them, both of which generate invokes in case
1761 // the next unwind destination exists, i.e., the next unwind destination is not
1762 // the caller.)
1763 //
1764 // Having at most one EH pad successor is also simpler and helps later
1765 // transformations.
1766 //
1767 // For example,
1768 // current:
1769 //   invoke void @foo to ... unwind label %catch.dispatch
1770 // catch.dispatch:
1771 //   %0 = catchswitch within ... [label %catch.start] unwind label %next
1772 // catch.start:
1773 //   ...
1774 //   ... in this BB or some other child BB dominated by this BB there will be an
1775 //   invoke that points to 'next' BB as an unwind destination
1776 //
1777 // next: ; We don't need to add this to 'current' BB's successor
1778 //   ...
1779 static void findWasmUnwindDestinations(
1780     FunctionLoweringInfo &FuncInfo, const BasicBlock *EHPadBB,
1781     BranchProbability Prob,
1782     SmallVectorImpl<std::pair<MachineBasicBlock *, BranchProbability>>
1783         &UnwindDests) {
1784   while (EHPadBB) {
1785     const Instruction *Pad = EHPadBB->getFirstNonPHI();
1786     if (isa<CleanupPadInst>(Pad)) {
1787       // Stop on cleanup pads.
1788       UnwindDests.emplace_back(FuncInfo.MBBMap[EHPadBB], Prob);
1789       UnwindDests.back().first->setIsEHScopeEntry();
1790       break;
1791     } else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Pad)) {
1792       // Add the catchpad handlers to the possible destinations. We don't
1793       // continue to the unwind destination of the catchswitch for wasm.
1794       for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) {
1795         UnwindDests.emplace_back(FuncInfo.MBBMap[CatchPadBB], Prob);
1796         UnwindDests.back().first->setIsEHScopeEntry();
1797       }
1798       break;
1799     } else {
1800       continue;
1801     }
1802   }
1803 }
1804 
1805 /// When an invoke or a cleanupret unwinds to the next EH pad, there are
1806 /// many places it could ultimately go. In the IR, we have a single unwind
1807 /// destination, but in the machine CFG, we enumerate all the possible blocks.
1808 /// This function skips over imaginary basic blocks that hold catchswitch
1809 /// instructions, and finds all the "real" machine
1810 /// basic block destinations. As those destinations may not be successors of
1811 /// EHPadBB, here we also calculate the edge probability to those destinations.
1812 /// The passed-in Prob is the edge probability to EHPadBB.
1813 static void findUnwindDestinations(
1814     FunctionLoweringInfo &FuncInfo, const BasicBlock *EHPadBB,
1815     BranchProbability Prob,
1816     SmallVectorImpl<std::pair<MachineBasicBlock *, BranchProbability>>
1817         &UnwindDests) {
1818   EHPersonality Personality =
1819     classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
1820   bool IsMSVCCXX = Personality == EHPersonality::MSVC_CXX;
1821   bool IsCoreCLR = Personality == EHPersonality::CoreCLR;
1822   bool IsWasmCXX = Personality == EHPersonality::Wasm_CXX;
1823   bool IsSEH = isAsynchronousEHPersonality(Personality);
1824 
1825   if (IsWasmCXX) {
1826     findWasmUnwindDestinations(FuncInfo, EHPadBB, Prob, UnwindDests);
1827     assert(UnwindDests.size() <= 1 &&
1828            "There should be at most one unwind destination for wasm");
1829     return;
1830   }
1831 
1832   while (EHPadBB) {
1833     const Instruction *Pad = EHPadBB->getFirstNonPHI();
1834     BasicBlock *NewEHPadBB = nullptr;
1835     if (isa<LandingPadInst>(Pad)) {
1836       // Stop on landingpads. They are not funclets.
1837       UnwindDests.emplace_back(FuncInfo.MBBMap[EHPadBB], Prob);
1838       break;
1839     } else if (isa<CleanupPadInst>(Pad)) {
1840       // Stop on cleanup pads. Cleanups are always funclet entries for all known
1841       // personalities.
1842       UnwindDests.emplace_back(FuncInfo.MBBMap[EHPadBB], Prob);
1843       UnwindDests.back().first->setIsEHScopeEntry();
1844       UnwindDests.back().first->setIsEHFuncletEntry();
1845       break;
1846     } else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Pad)) {
1847       // Add the catchpad handlers to the possible destinations.
1848       for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) {
1849         UnwindDests.emplace_back(FuncInfo.MBBMap[CatchPadBB], Prob);
1850         // For MSVC++ and the CLR, catchblocks are funclets and need prologues.
1851         if (IsMSVCCXX || IsCoreCLR)
1852           UnwindDests.back().first->setIsEHFuncletEntry();
1853         if (!IsSEH)
1854           UnwindDests.back().first->setIsEHScopeEntry();
1855       }
1856       NewEHPadBB = CatchSwitch->getUnwindDest();
1857     } else {
1858       continue;
1859     }
1860 
1861     BranchProbabilityInfo *BPI = FuncInfo.BPI;
1862     if (BPI && NewEHPadBB)
1863       Prob *= BPI->getEdgeProbability(EHPadBB, NewEHPadBB);
1864     EHPadBB = NewEHPadBB;
1865   }
1866 }
1867 
1868 void SelectionDAGBuilder::visitCleanupRet(const CleanupReturnInst &I) {
1869   // Update successor info.
1870   SmallVector<std::pair<MachineBasicBlock *, BranchProbability>, 1> UnwindDests;
1871   auto UnwindDest = I.getUnwindDest();
1872   BranchProbabilityInfo *BPI = FuncInfo.BPI;
1873   BranchProbability UnwindDestProb =
1874       (BPI && UnwindDest)
1875           ? BPI->getEdgeProbability(FuncInfo.MBB->getBasicBlock(), UnwindDest)
1876           : BranchProbability::getZero();
1877   findUnwindDestinations(FuncInfo, UnwindDest, UnwindDestProb, UnwindDests);
1878   for (auto &UnwindDest : UnwindDests) {
1879     UnwindDest.first->setIsEHPad();
1880     addSuccessorWithProb(FuncInfo.MBB, UnwindDest.first, UnwindDest.second);
1881   }
1882   FuncInfo.MBB->normalizeSuccProbs();
1883 
1884   // Create the terminator node.
1885   SDValue Ret =
1886       DAG.getNode(ISD::CLEANUPRET, getCurSDLoc(), MVT::Other, getControlRoot());
1887   DAG.setRoot(Ret);
1888 }
1889 
1890 void SelectionDAGBuilder::visitCatchSwitch(const CatchSwitchInst &CSI) {
1891   report_fatal_error("visitCatchSwitch not yet implemented!");
1892 }
1893 
1894 void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
1895   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1896   auto &DL = DAG.getDataLayout();
1897   SDValue Chain = getControlRoot();
1898   SmallVector<ISD::OutputArg, 8> Outs;
1899   SmallVector<SDValue, 8> OutVals;
1900 
1901   // Calls to @llvm.experimental.deoptimize don't generate a return value, so
1902   // lower
1903   //
1904   //   %val = call <ty> @llvm.experimental.deoptimize()
1905   //   ret <ty> %val
1906   //
1907   // differently.
1908   if (I.getParent()->getTerminatingDeoptimizeCall()) {
1909     LowerDeoptimizingReturn();
1910     return;
1911   }
1912 
1913   if (!FuncInfo.CanLowerReturn) {
1914     unsigned DemoteReg = FuncInfo.DemoteRegister;
1915     const Function *F = I.getParent()->getParent();
1916 
1917     // Emit a store of the return value through the virtual register.
1918     // Leave Outs empty so that LowerReturn won't try to load return
1919     // registers the usual way.
1920     SmallVector<EVT, 1> PtrValueVTs;
1921     ComputeValueVTs(TLI, DL,
1922                     F->getReturnType()->getPointerTo(
1923                         DAG.getDataLayout().getAllocaAddrSpace()),
1924                     PtrValueVTs);
1925 
1926     SDValue RetPtr =
1927         DAG.getCopyFromReg(Chain, getCurSDLoc(), DemoteReg, PtrValueVTs[0]);
1928     SDValue RetOp = getValue(I.getOperand(0));
1929 
1930     SmallVector<EVT, 4> ValueVTs, MemVTs;
1931     SmallVector<uint64_t, 4> Offsets;
1932     ComputeValueVTs(TLI, DL, I.getOperand(0)->getType(), ValueVTs, &MemVTs,
1933                     &Offsets);
1934     unsigned NumValues = ValueVTs.size();
1935 
1936     SmallVector<SDValue, 4> Chains(NumValues);
1937     Align BaseAlign = DL.getPrefTypeAlign(I.getOperand(0)->getType());
1938     for (unsigned i = 0; i != NumValues; ++i) {
1939       // An aggregate return value cannot wrap around the address space, so
1940       // offsets to its parts don't wrap either.
1941       SDValue Ptr = DAG.getObjectPtrOffset(getCurSDLoc(), RetPtr,
1942                                            TypeSize::Fixed(Offsets[i]));
1943 
1944       SDValue Val = RetOp.getValue(RetOp.getResNo() + i);
1945       if (MemVTs[i] != ValueVTs[i])
1946         Val = DAG.getPtrExtOrTrunc(Val, getCurSDLoc(), MemVTs[i]);
1947       Chains[i] = DAG.getStore(
1948           Chain, getCurSDLoc(), Val,
1949           // FIXME: better loc info would be nice.
1950           Ptr, MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()),
1951           commonAlignment(BaseAlign, Offsets[i]));
1952     }
1953 
1954     Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
1955                         MVT::Other, Chains);
1956   } else if (I.getNumOperands() != 0) {
1957     SmallVector<EVT, 4> ValueVTs;
1958     ComputeValueVTs(TLI, DL, I.getOperand(0)->getType(), ValueVTs);
1959     unsigned NumValues = ValueVTs.size();
1960     if (NumValues) {
1961       SDValue RetOp = getValue(I.getOperand(0));
1962 
1963       const Function *F = I.getParent()->getParent();
1964 
1965       bool NeedsRegBlock = TLI.functionArgumentNeedsConsecutiveRegisters(
1966           I.getOperand(0)->getType(), F->getCallingConv(),
1967           /*IsVarArg*/ false, DL);
1968 
1969       ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
1970       if (F->getAttributes().hasRetAttr(Attribute::SExt))
1971         ExtendKind = ISD::SIGN_EXTEND;
1972       else if (F->getAttributes().hasRetAttr(Attribute::ZExt))
1973         ExtendKind = ISD::ZERO_EXTEND;
1974 
1975       LLVMContext &Context = F->getContext();
1976       bool RetInReg = F->getAttributes().hasRetAttr(Attribute::InReg);
1977 
1978       for (unsigned j = 0; j != NumValues; ++j) {
1979         EVT VT = ValueVTs[j];
1980 
1981         if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger())
1982           VT = TLI.getTypeForExtReturn(Context, VT, ExtendKind);
1983 
1984         CallingConv::ID CC = F->getCallingConv();
1985 
1986         unsigned NumParts = TLI.getNumRegistersForCallingConv(Context, CC, VT);
1987         MVT PartVT = TLI.getRegisterTypeForCallingConv(Context, CC, VT);
1988         SmallVector<SDValue, 4> Parts(NumParts);
1989         getCopyToParts(DAG, getCurSDLoc(),
1990                        SDValue(RetOp.getNode(), RetOp.getResNo() + j),
1991                        &Parts[0], NumParts, PartVT, &I, CC, ExtendKind);
1992 
1993         // 'inreg' on function refers to return value
1994         ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
1995         if (RetInReg)
1996           Flags.setInReg();
1997 
1998         if (I.getOperand(0)->getType()->isPointerTy()) {
1999           Flags.setPointer();
2000           Flags.setPointerAddrSpace(
2001               cast<PointerType>(I.getOperand(0)->getType())->getAddressSpace());
2002         }
2003 
2004         if (NeedsRegBlock) {
2005           Flags.setInConsecutiveRegs();
2006           if (j == NumValues - 1)
2007             Flags.setInConsecutiveRegsLast();
2008         }
2009 
2010         // Propagate extension type if any
2011         if (ExtendKind == ISD::SIGN_EXTEND)
2012           Flags.setSExt();
2013         else if (ExtendKind == ISD::ZERO_EXTEND)
2014           Flags.setZExt();
2015 
2016         for (unsigned i = 0; i < NumParts; ++i) {
2017           Outs.push_back(ISD::OutputArg(Flags,
2018                                         Parts[i].getValueType().getSimpleVT(),
2019                                         VT, /*isfixed=*/true, 0, 0));
2020           OutVals.push_back(Parts[i]);
2021         }
2022       }
2023     }
2024   }
2025 
2026   // Push in swifterror virtual register as the last element of Outs. This makes
2027   // sure swifterror virtual register will be returned in the swifterror
2028   // physical register.
2029   const Function *F = I.getParent()->getParent();
2030   if (TLI.supportSwiftError() &&
2031       F->getAttributes().hasAttrSomewhere(Attribute::SwiftError)) {
2032     assert(SwiftError.getFunctionArg() && "Need a swift error argument");
2033     ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
2034     Flags.setSwiftError();
2035     Outs.push_back(ISD::OutputArg(
2036         Flags, /*vt=*/TLI.getPointerTy(DL), /*argvt=*/EVT(TLI.getPointerTy(DL)),
2037         /*isfixed=*/true, /*origidx=*/1, /*partOffs=*/0));
2038     // Create SDNode for the swifterror virtual register.
2039     OutVals.push_back(
2040         DAG.getRegister(SwiftError.getOrCreateVRegUseAt(
2041                             &I, FuncInfo.MBB, SwiftError.getFunctionArg()),
2042                         EVT(TLI.getPointerTy(DL))));
2043   }
2044 
2045   bool isVarArg = DAG.getMachineFunction().getFunction().isVarArg();
2046   CallingConv::ID CallConv =
2047     DAG.getMachineFunction().getFunction().getCallingConv();
2048   Chain = DAG.getTargetLoweringInfo().LowerReturn(
2049       Chain, CallConv, isVarArg, Outs, OutVals, getCurSDLoc(), DAG);
2050 
2051   // Verify that the target's LowerReturn behaved as expected.
2052   assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
2053          "LowerReturn didn't return a valid chain!");
2054 
2055   // Update the DAG with the new chain value resulting from return lowering.
2056   DAG.setRoot(Chain);
2057 }
2058 
2059 /// CopyToExportRegsIfNeeded - If the given value has virtual registers
2060 /// created for it, emit nodes to copy the value into the virtual
2061 /// registers.
2062 void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) {
2063   // Skip empty types
2064   if (V->getType()->isEmptyTy())
2065     return;
2066 
2067   DenseMap<const Value *, Register>::iterator VMI = FuncInfo.ValueMap.find(V);
2068   if (VMI != FuncInfo.ValueMap.end()) {
2069     assert(!V->use_empty() && "Unused value assigned virtual registers!");
2070     CopyValueToVirtualRegister(V, VMI->second);
2071   }
2072 }
2073 
2074 /// ExportFromCurrentBlock - If this condition isn't known to be exported from
2075 /// the current basic block, add it to ValueMap now so that we'll get a
2076 /// CopyTo/FromReg.
2077 void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) {
2078   // No need to export constants.
2079   if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
2080 
2081   // Already exported?
2082   if (FuncInfo.isExportedInst(V)) return;
2083 
2084   unsigned Reg = FuncInfo.InitializeRegForValue(V);
2085   CopyValueToVirtualRegister(V, Reg);
2086 }
2087 
2088 bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V,
2089                                                      const BasicBlock *FromBB) {
2090   // The operands of the setcc have to be in this block.  We don't know
2091   // how to export them from some other block.
2092   if (const Instruction *VI = dyn_cast<Instruction>(V)) {
2093     // Can export from current BB.
2094     if (VI->getParent() == FromBB)
2095       return true;
2096 
2097     // Is already exported, noop.
2098     return FuncInfo.isExportedInst(V);
2099   }
2100 
2101   // If this is an argument, we can export it if the BB is the entry block or
2102   // if it is already exported.
2103   if (isa<Argument>(V)) {
2104     if (FromBB->isEntryBlock())
2105       return true;
2106 
2107     // Otherwise, can only export this if it is already exported.
2108     return FuncInfo.isExportedInst(V);
2109   }
2110 
2111   // Otherwise, constants can always be exported.
2112   return true;
2113 }
2114 
2115 /// Return branch probability calculated by BranchProbabilityInfo for IR blocks.
2116 BranchProbability
2117 SelectionDAGBuilder::getEdgeProbability(const MachineBasicBlock *Src,
2118                                         const MachineBasicBlock *Dst) const {
2119   BranchProbabilityInfo *BPI = FuncInfo.BPI;
2120   const BasicBlock *SrcBB = Src->getBasicBlock();
2121   const BasicBlock *DstBB = Dst->getBasicBlock();
2122   if (!BPI) {
2123     // If BPI is not available, set the default probability as 1 / N, where N is
2124     // the number of successors.
2125     auto SuccSize = std::max<uint32_t>(succ_size(SrcBB), 1);
2126     return BranchProbability(1, SuccSize);
2127   }
2128   return BPI->getEdgeProbability(SrcBB, DstBB);
2129 }
2130 
2131 void SelectionDAGBuilder::addSuccessorWithProb(MachineBasicBlock *Src,
2132                                                MachineBasicBlock *Dst,
2133                                                BranchProbability Prob) {
2134   if (!FuncInfo.BPI)
2135     Src->addSuccessorWithoutProb(Dst);
2136   else {
2137     if (Prob.isUnknown())
2138       Prob = getEdgeProbability(Src, Dst);
2139     Src->addSuccessor(Dst, Prob);
2140   }
2141 }
2142 
2143 static bool InBlock(const Value *V, const BasicBlock *BB) {
2144   if (const Instruction *I = dyn_cast<Instruction>(V))
2145     return I->getParent() == BB;
2146   return true;
2147 }
2148 
2149 /// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
2150 /// This function emits a branch and is used at the leaves of an OR or an
2151 /// AND operator tree.
2152 void
2153 SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond,
2154                                                   MachineBasicBlock *TBB,
2155                                                   MachineBasicBlock *FBB,
2156                                                   MachineBasicBlock *CurBB,
2157                                                   MachineBasicBlock *SwitchBB,
2158                                                   BranchProbability TProb,
2159                                                   BranchProbability FProb,
2160                                                   bool InvertCond) {
2161   const BasicBlock *BB = CurBB->getBasicBlock();
2162 
2163   // If the leaf of the tree is a comparison, merge the condition into
2164   // the caseblock.
2165   if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
2166     // The operands of the cmp have to be in this block.  We don't know
2167     // how to export them from some other block.  If this is the first block
2168     // of the sequence, no exporting is needed.
2169     if (CurBB == SwitchBB ||
2170         (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
2171          isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
2172       ISD::CondCode Condition;
2173       if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
2174         ICmpInst::Predicate Pred =
2175             InvertCond ? IC->getInversePredicate() : IC->getPredicate();
2176         Condition = getICmpCondCode(Pred);
2177       } else {
2178         const FCmpInst *FC = cast<FCmpInst>(Cond);
2179         FCmpInst::Predicate Pred =
2180             InvertCond ? FC->getInversePredicate() : FC->getPredicate();
2181         Condition = getFCmpCondCode(Pred);
2182         if (TM.Options.NoNaNsFPMath)
2183           Condition = getFCmpCodeWithoutNaN(Condition);
2184       }
2185 
2186       CaseBlock CB(Condition, BOp->getOperand(0), BOp->getOperand(1), nullptr,
2187                    TBB, FBB, CurBB, getCurSDLoc(), TProb, FProb);
2188       SL->SwitchCases.push_back(CB);
2189       return;
2190     }
2191   }
2192 
2193   // Create a CaseBlock record representing this branch.
2194   ISD::CondCode Opc = InvertCond ? ISD::SETNE : ISD::SETEQ;
2195   CaseBlock CB(Opc, Cond, ConstantInt::getTrue(*DAG.getContext()),
2196                nullptr, TBB, FBB, CurBB, getCurSDLoc(), TProb, FProb);
2197   SL->SwitchCases.push_back(CB);
2198 }
2199 
2200 void SelectionDAGBuilder::FindMergedConditions(const Value *Cond,
2201                                                MachineBasicBlock *TBB,
2202                                                MachineBasicBlock *FBB,
2203                                                MachineBasicBlock *CurBB,
2204                                                MachineBasicBlock *SwitchBB,
2205                                                Instruction::BinaryOps Opc,
2206                                                BranchProbability TProb,
2207                                                BranchProbability FProb,
2208                                                bool InvertCond) {
2209   // Skip over not part of the tree and remember to invert op and operands at
2210   // next level.
2211   Value *NotCond;
2212   if (match(Cond, m_OneUse(m_Not(m_Value(NotCond)))) &&
2213       InBlock(NotCond, CurBB->getBasicBlock())) {
2214     FindMergedConditions(NotCond, TBB, FBB, CurBB, SwitchBB, Opc, TProb, FProb,
2215                          !InvertCond);
2216     return;
2217   }
2218 
2219   const Instruction *BOp = dyn_cast<Instruction>(Cond);
2220   const Value *BOpOp0, *BOpOp1;
2221   // Compute the effective opcode for Cond, taking into account whether it needs
2222   // to be inverted, e.g.
2223   //   and (not (or A, B)), C
2224   // gets lowered as
2225   //   and (and (not A, not B), C)
2226   Instruction::BinaryOps BOpc = (Instruction::BinaryOps)0;
2227   if (BOp) {
2228     BOpc = match(BOp, m_LogicalAnd(m_Value(BOpOp0), m_Value(BOpOp1)))
2229                ? Instruction::And
2230                : (match(BOp, m_LogicalOr(m_Value(BOpOp0), m_Value(BOpOp1)))
2231                       ? Instruction::Or
2232                       : (Instruction::BinaryOps)0);
2233     if (InvertCond) {
2234       if (BOpc == Instruction::And)
2235         BOpc = Instruction::Or;
2236       else if (BOpc == Instruction::Or)
2237         BOpc = Instruction::And;
2238     }
2239   }
2240 
2241   // If this node is not part of the or/and tree, emit it as a branch.
2242   // Note that all nodes in the tree should have same opcode.
2243   bool BOpIsInOrAndTree = BOpc && BOpc == Opc && BOp->hasOneUse();
2244   if (!BOpIsInOrAndTree || BOp->getParent() != CurBB->getBasicBlock() ||
2245       !InBlock(BOpOp0, CurBB->getBasicBlock()) ||
2246       !InBlock(BOpOp1, CurBB->getBasicBlock())) {
2247     EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB,
2248                                  TProb, FProb, InvertCond);
2249     return;
2250   }
2251 
2252   //  Create TmpBB after CurBB.
2253   MachineFunction::iterator BBI(CurBB);
2254   MachineFunction &MF = DAG.getMachineFunction();
2255   MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
2256   CurBB->getParent()->insert(++BBI, TmpBB);
2257 
2258   if (Opc == Instruction::Or) {
2259     // Codegen X | Y as:
2260     // BB1:
2261     //   jmp_if_X TBB
2262     //   jmp TmpBB
2263     // TmpBB:
2264     //   jmp_if_Y TBB
2265     //   jmp FBB
2266     //
2267 
2268     // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
2269     // The requirement is that
2270     //   TrueProb for BB1 + (FalseProb for BB1 * TrueProb for TmpBB)
2271     //     = TrueProb for original BB.
2272     // Assuming the original probabilities are A and B, one choice is to set
2273     // BB1's probabilities to A/2 and A/2+B, and set TmpBB's probabilities to
2274     // A/(1+B) and 2B/(1+B). This choice assumes that
2275     //   TrueProb for BB1 == FalseProb for BB1 * TrueProb for TmpBB.
2276     // Another choice is to assume TrueProb for BB1 equals to TrueProb for
2277     // TmpBB, but the math is more complicated.
2278 
2279     auto NewTrueProb = TProb / 2;
2280     auto NewFalseProb = TProb / 2 + FProb;
2281     // Emit the LHS condition.
2282     FindMergedConditions(BOpOp0, TBB, TmpBB, CurBB, SwitchBB, Opc, NewTrueProb,
2283                          NewFalseProb, InvertCond);
2284 
2285     // Normalize A/2 and B to get A/(1+B) and 2B/(1+B).
2286     SmallVector<BranchProbability, 2> Probs{TProb / 2, FProb};
2287     BranchProbability::normalizeProbabilities(Probs.begin(), Probs.end());
2288     // Emit the RHS condition into TmpBB.
2289     FindMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
2290                          Probs[1], InvertCond);
2291   } else {
2292     assert(Opc == Instruction::And && "Unknown merge op!");
2293     // Codegen X & Y as:
2294     // BB1:
2295     //   jmp_if_X TmpBB
2296     //   jmp FBB
2297     // TmpBB:
2298     //   jmp_if_Y TBB
2299     //   jmp FBB
2300     //
2301     //  This requires creation of TmpBB after CurBB.
2302 
2303     // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
2304     // The requirement is that
2305     //   FalseProb for BB1 + (TrueProb for BB1 * FalseProb for TmpBB)
2306     //     = FalseProb for original BB.
2307     // Assuming the original probabilities are A and B, one choice is to set
2308     // BB1's probabilities to A+B/2 and B/2, and set TmpBB's probabilities to
2309     // 2A/(1+A) and B/(1+A). This choice assumes that FalseProb for BB1 ==
2310     // TrueProb for BB1 * FalseProb for TmpBB.
2311 
2312     auto NewTrueProb = TProb + FProb / 2;
2313     auto NewFalseProb = FProb / 2;
2314     // Emit the LHS condition.
2315     FindMergedConditions(BOpOp0, TmpBB, FBB, CurBB, SwitchBB, Opc, NewTrueProb,
2316                          NewFalseProb, InvertCond);
2317 
2318     // Normalize A and B/2 to get 2A/(1+A) and B/(1+A).
2319     SmallVector<BranchProbability, 2> Probs{TProb, FProb / 2};
2320     BranchProbability::normalizeProbabilities(Probs.begin(), Probs.end());
2321     // Emit the RHS condition into TmpBB.
2322     FindMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
2323                          Probs[1], InvertCond);
2324   }
2325 }
2326 
2327 /// If the set of cases should be emitted as a series of branches, return true.
2328 /// If we should emit this as a bunch of and/or'd together conditions, return
2329 /// false.
2330 bool
2331 SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases) {
2332   if (Cases.size() != 2) return true;
2333 
2334   // If this is two comparisons of the same values or'd or and'd together, they
2335   // will get folded into a single comparison, so don't emit two blocks.
2336   if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
2337        Cases[0].CmpRHS == Cases[1].CmpRHS) ||
2338       (Cases[0].CmpRHS == Cases[1].CmpLHS &&
2339        Cases[0].CmpLHS == Cases[1].CmpRHS)) {
2340     return false;
2341   }
2342 
2343   // Handle: (X != null) | (Y != null) --> (X|Y) != 0
2344   // Handle: (X == null) & (Y == null) --> (X|Y) == 0
2345   if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
2346       Cases[0].CC == Cases[1].CC &&
2347       isa<Constant>(Cases[0].CmpRHS) &&
2348       cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
2349     if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
2350       return false;
2351     if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
2352       return false;
2353   }
2354 
2355   return true;
2356 }
2357 
2358 void SelectionDAGBuilder::visitBr(const BranchInst &I) {
2359   MachineBasicBlock *BrMBB = FuncInfo.MBB;
2360 
2361   // Update machine-CFG edges.
2362   MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
2363 
2364   if (I.isUnconditional()) {
2365     // Update machine-CFG edges.
2366     BrMBB->addSuccessor(Succ0MBB);
2367 
2368     // If this is not a fall-through branch or optimizations are switched off,
2369     // emit the branch.
2370     if (Succ0MBB != NextBlock(BrMBB) || TM.getOptLevel() == CodeGenOpt::None)
2371       DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
2372                               MVT::Other, getControlRoot(),
2373                               DAG.getBasicBlock(Succ0MBB)));
2374 
2375     return;
2376   }
2377 
2378   // If this condition is one of the special cases we handle, do special stuff
2379   // now.
2380   const Value *CondVal = I.getCondition();
2381   MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
2382 
2383   // If this is a series of conditions that are or'd or and'd together, emit
2384   // this as a sequence of branches instead of setcc's with and/or operations.
2385   // As long as jumps are not expensive (exceptions for multi-use logic ops,
2386   // unpredictable branches, and vector extracts because those jumps are likely
2387   // expensive for any target), this should improve performance.
2388   // For example, instead of something like:
2389   //     cmp A, B
2390   //     C = seteq
2391   //     cmp D, E
2392   //     F = setle
2393   //     or C, F
2394   //     jnz foo
2395   // Emit:
2396   //     cmp A, B
2397   //     je foo
2398   //     cmp D, E
2399   //     jle foo
2400   const Instruction *BOp = dyn_cast<Instruction>(CondVal);
2401   if (!DAG.getTargetLoweringInfo().isJumpExpensive() && BOp &&
2402       BOp->hasOneUse() && !I.hasMetadata(LLVMContext::MD_unpredictable)) {
2403     Value *Vec;
2404     const Value *BOp0, *BOp1;
2405     Instruction::BinaryOps Opcode = (Instruction::BinaryOps)0;
2406     if (match(BOp, m_LogicalAnd(m_Value(BOp0), m_Value(BOp1))))
2407       Opcode = Instruction::And;
2408     else if (match(BOp, m_LogicalOr(m_Value(BOp0), m_Value(BOp1))))
2409       Opcode = Instruction::Or;
2410 
2411     if (Opcode && !(match(BOp0, m_ExtractElt(m_Value(Vec), m_Value())) &&
2412                     match(BOp1, m_ExtractElt(m_Specific(Vec), m_Value())))) {
2413       FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB, Opcode,
2414                            getEdgeProbability(BrMBB, Succ0MBB),
2415                            getEdgeProbability(BrMBB, Succ1MBB),
2416                            /*InvertCond=*/false);
2417       // If the compares in later blocks need to use values not currently
2418       // exported from this block, export them now.  This block should always
2419       // be the first entry.
2420       assert(SL->SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
2421 
2422       // Allow some cases to be rejected.
2423       if (ShouldEmitAsBranches(SL->SwitchCases)) {
2424         for (unsigned i = 1, e = SL->SwitchCases.size(); i != e; ++i) {
2425           ExportFromCurrentBlock(SL->SwitchCases[i].CmpLHS);
2426           ExportFromCurrentBlock(SL->SwitchCases[i].CmpRHS);
2427         }
2428 
2429         // Emit the branch for this block.
2430         visitSwitchCase(SL->SwitchCases[0], BrMBB);
2431         SL->SwitchCases.erase(SL->SwitchCases.begin());
2432         return;
2433       }
2434 
2435       // Okay, we decided not to do this, remove any inserted MBB's and clear
2436       // SwitchCases.
2437       for (unsigned i = 1, e = SL->SwitchCases.size(); i != e; ++i)
2438         FuncInfo.MF->erase(SL->SwitchCases[i].ThisBB);
2439 
2440       SL->SwitchCases.clear();
2441     }
2442   }
2443 
2444   // Create a CaseBlock record representing this branch.
2445   CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
2446                nullptr, Succ0MBB, Succ1MBB, BrMBB, getCurSDLoc());
2447 
2448   // Use visitSwitchCase to actually insert the fast branch sequence for this
2449   // cond branch.
2450   visitSwitchCase(CB, BrMBB);
2451 }
2452 
2453 /// visitSwitchCase - Emits the necessary code to represent a single node in
2454 /// the binary search tree resulting from lowering a switch instruction.
2455 void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB,
2456                                           MachineBasicBlock *SwitchBB) {
2457   SDValue Cond;
2458   SDValue CondLHS = getValue(CB.CmpLHS);
2459   SDLoc dl = CB.DL;
2460 
2461   if (CB.CC == ISD::SETTRUE) {
2462     // Branch or fall through to TrueBB.
2463     addSuccessorWithProb(SwitchBB, CB.TrueBB, CB.TrueProb);
2464     SwitchBB->normalizeSuccProbs();
2465     if (CB.TrueBB != NextBlock(SwitchBB)) {
2466       DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, getControlRoot(),
2467                               DAG.getBasicBlock(CB.TrueBB)));
2468     }
2469     return;
2470   }
2471 
2472   auto &TLI = DAG.getTargetLoweringInfo();
2473   EVT MemVT = TLI.getMemValueType(DAG.getDataLayout(), CB.CmpLHS->getType());
2474 
2475   // Build the setcc now.
2476   if (!CB.CmpMHS) {
2477     // Fold "(X == true)" to X and "(X == false)" to !X to
2478     // handle common cases produced by branch lowering.
2479     if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
2480         CB.CC == ISD::SETEQ)
2481       Cond = CondLHS;
2482     else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
2483              CB.CC == ISD::SETEQ) {
2484       SDValue True = DAG.getConstant(1, dl, CondLHS.getValueType());
2485       Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
2486     } else {
2487       SDValue CondRHS = getValue(CB.CmpRHS);
2488 
2489       // If a pointer's DAG type is larger than its memory type then the DAG
2490       // values are zero-extended. This breaks signed comparisons so truncate
2491       // back to the underlying type before doing the compare.
2492       if (CondLHS.getValueType() != MemVT) {
2493         CondLHS = DAG.getPtrExtOrTrunc(CondLHS, getCurSDLoc(), MemVT);
2494         CondRHS = DAG.getPtrExtOrTrunc(CondRHS, getCurSDLoc(), MemVT);
2495       }
2496       Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, CondRHS, CB.CC);
2497     }
2498   } else {
2499     assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
2500 
2501     const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
2502     const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
2503 
2504     SDValue CmpOp = getValue(CB.CmpMHS);
2505     EVT VT = CmpOp.getValueType();
2506 
2507     if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
2508       Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, dl, VT),
2509                           ISD::SETLE);
2510     } else {
2511       SDValue SUB = DAG.getNode(ISD::SUB, dl,
2512                                 VT, CmpOp, DAG.getConstant(Low, dl, VT));
2513       Cond = DAG.getSetCC(dl, MVT::i1, SUB,
2514                           DAG.getConstant(High-Low, dl, VT), ISD::SETULE);
2515     }
2516   }
2517 
2518   // Update successor info
2519   addSuccessorWithProb(SwitchBB, CB.TrueBB, CB.TrueProb);
2520   // TrueBB and FalseBB are always different unless the incoming IR is
2521   // degenerate. This only happens when running llc on weird IR.
2522   if (CB.TrueBB != CB.FalseBB)
2523     addSuccessorWithProb(SwitchBB, CB.FalseBB, CB.FalseProb);
2524   SwitchBB->normalizeSuccProbs();
2525 
2526   // If the lhs block is the next block, invert the condition so that we can
2527   // fall through to the lhs instead of the rhs block.
2528   if (CB.TrueBB == NextBlock(SwitchBB)) {
2529     std::swap(CB.TrueBB, CB.FalseBB);
2530     SDValue True = DAG.getConstant(1, dl, Cond.getValueType());
2531     Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
2532   }
2533 
2534   SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
2535                                MVT::Other, getControlRoot(), Cond,
2536                                DAG.getBasicBlock(CB.TrueBB));
2537 
2538   // Insert the false branch. Do this even if it's a fall through branch,
2539   // this makes it easier to do DAG optimizations which require inverting
2540   // the branch condition.
2541   BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
2542                        DAG.getBasicBlock(CB.FalseBB));
2543 
2544   DAG.setRoot(BrCond);
2545 }
2546 
2547 /// visitJumpTable - Emit JumpTable node in the current MBB
2548 void SelectionDAGBuilder::visitJumpTable(SwitchCG::JumpTable &JT) {
2549   // Emit the code for the jump table
2550   assert(JT.Reg != -1U && "Should lower JT Header first!");
2551   EVT PTy = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
2552   SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurSDLoc(),
2553                                      JT.Reg, PTy);
2554   SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
2555   SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurSDLoc(),
2556                                     MVT::Other, Index.getValue(1),
2557                                     Table, Index);
2558   DAG.setRoot(BrJumpTable);
2559 }
2560 
2561 /// visitJumpTableHeader - This function emits necessary code to produce index
2562 /// in the JumpTable from switch case.
2563 void SelectionDAGBuilder::visitJumpTableHeader(SwitchCG::JumpTable &JT,
2564                                                JumpTableHeader &JTH,
2565                                                MachineBasicBlock *SwitchBB) {
2566   SDLoc dl = getCurSDLoc();
2567 
2568   // Subtract the lowest switch case value from the value being switched on.
2569   SDValue SwitchOp = getValue(JTH.SValue);
2570   EVT VT = SwitchOp.getValueType();
2571   SDValue Sub = DAG.getNode(ISD::SUB, dl, VT, SwitchOp,
2572                             DAG.getConstant(JTH.First, dl, VT));
2573 
2574   // The SDNode we just created, which holds the value being switched on minus
2575   // the smallest case value, needs to be copied to a virtual register so it
2576   // can be used as an index into the jump table in a subsequent basic block.
2577   // This value may be smaller or larger than the target's pointer type, and
2578   // therefore require extension or truncating.
2579   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2580   SwitchOp = DAG.getZExtOrTrunc(Sub, dl, TLI.getPointerTy(DAG.getDataLayout()));
2581 
2582   unsigned JumpTableReg =
2583       FuncInfo.CreateReg(TLI.getPointerTy(DAG.getDataLayout()));
2584   SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), dl,
2585                                     JumpTableReg, SwitchOp);
2586   JT.Reg = JumpTableReg;
2587 
2588   if (!JTH.FallthroughUnreachable) {
2589     // Emit the range check for the jump table, and branch to the default block
2590     // for the switch statement if the value being switched on exceeds the
2591     // largest case in the switch.
2592     SDValue CMP = DAG.getSetCC(
2593         dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
2594                                    Sub.getValueType()),
2595         Sub, DAG.getConstant(JTH.Last - JTH.First, dl, VT), ISD::SETUGT);
2596 
2597     SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
2598                                  MVT::Other, CopyTo, CMP,
2599                                  DAG.getBasicBlock(JT.Default));
2600 
2601     // Avoid emitting unnecessary branches to the next block.
2602     if (JT.MBB != NextBlock(SwitchBB))
2603       BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
2604                            DAG.getBasicBlock(JT.MBB));
2605 
2606     DAG.setRoot(BrCond);
2607   } else {
2608     // Avoid emitting unnecessary branches to the next block.
2609     if (JT.MBB != NextBlock(SwitchBB))
2610       DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, CopyTo,
2611                               DAG.getBasicBlock(JT.MBB)));
2612     else
2613       DAG.setRoot(CopyTo);
2614   }
2615 }
2616 
2617 /// Create a LOAD_STACK_GUARD node, and let it carry the target specific global
2618 /// variable if there exists one.
2619 static SDValue getLoadStackGuard(SelectionDAG &DAG, const SDLoc &DL,
2620                                  SDValue &Chain) {
2621   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2622   EVT PtrTy = TLI.getPointerTy(DAG.getDataLayout());
2623   EVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout());
2624   MachineFunction &MF = DAG.getMachineFunction();
2625   Value *Global = TLI.getSDagStackGuard(*MF.getFunction().getParent());
2626   MachineSDNode *Node =
2627       DAG.getMachineNode(TargetOpcode::LOAD_STACK_GUARD, DL, PtrTy, Chain);
2628   if (Global) {
2629     MachinePointerInfo MPInfo(Global);
2630     auto Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant |
2631                  MachineMemOperand::MODereferenceable;
2632     MachineMemOperand *MemRef = MF.getMachineMemOperand(
2633         MPInfo, Flags, PtrTy.getSizeInBits() / 8, DAG.getEVTAlign(PtrTy));
2634     DAG.setNodeMemRefs(Node, {MemRef});
2635   }
2636   if (PtrTy != PtrMemTy)
2637     return DAG.getPtrExtOrTrunc(SDValue(Node, 0), DL, PtrMemTy);
2638   return SDValue(Node, 0);
2639 }
2640 
2641 /// Codegen a new tail for a stack protector check ParentMBB which has had its
2642 /// tail spliced into a stack protector check success bb.
2643 ///
2644 /// For a high level explanation of how this fits into the stack protector
2645 /// generation see the comment on the declaration of class
2646 /// StackProtectorDescriptor.
2647 void SelectionDAGBuilder::visitSPDescriptorParent(StackProtectorDescriptor &SPD,
2648                                                   MachineBasicBlock *ParentBB) {
2649 
2650   // First create the loads to the guard/stack slot for the comparison.
2651   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2652   EVT PtrTy = TLI.getPointerTy(DAG.getDataLayout());
2653   EVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout());
2654 
2655   MachineFrameInfo &MFI = ParentBB->getParent()->getFrameInfo();
2656   int FI = MFI.getStackProtectorIndex();
2657 
2658   SDValue Guard;
2659   SDLoc dl = getCurSDLoc();
2660   SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
2661   const Module &M = *ParentBB->getParent()->getFunction().getParent();
2662   Align Align =
2663       DAG.getDataLayout().getPrefTypeAlign(Type::getInt8PtrTy(M.getContext()));
2664 
2665   // Generate code to load the content of the guard slot.
2666   SDValue GuardVal = DAG.getLoad(
2667       PtrMemTy, dl, DAG.getEntryNode(), StackSlotPtr,
2668       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), Align,
2669       MachineMemOperand::MOVolatile);
2670 
2671   if (TLI.useStackGuardXorFP())
2672     GuardVal = TLI.emitStackGuardXorFP(DAG, GuardVal, dl);
2673 
2674   // Retrieve guard check function, nullptr if instrumentation is inlined.
2675   if (const Function *GuardCheckFn = TLI.getSSPStackGuardCheck(M)) {
2676     // The target provides a guard check function to validate the guard value.
2677     // Generate a call to that function with the content of the guard slot as
2678     // argument.
2679     FunctionType *FnTy = GuardCheckFn->getFunctionType();
2680     assert(FnTy->getNumParams() == 1 && "Invalid function signature");
2681 
2682     TargetLowering::ArgListTy Args;
2683     TargetLowering::ArgListEntry Entry;
2684     Entry.Node = GuardVal;
2685     Entry.Ty = FnTy->getParamType(0);
2686     if (GuardCheckFn->hasParamAttribute(0, Attribute::AttrKind::InReg))
2687       Entry.IsInReg = true;
2688     Args.push_back(Entry);
2689 
2690     TargetLowering::CallLoweringInfo CLI(DAG);
2691     CLI.setDebugLoc(getCurSDLoc())
2692         .setChain(DAG.getEntryNode())
2693         .setCallee(GuardCheckFn->getCallingConv(), FnTy->getReturnType(),
2694                    getValue(GuardCheckFn), std::move(Args));
2695 
2696     std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
2697     DAG.setRoot(Result.second);
2698     return;
2699   }
2700 
2701   // If useLoadStackGuardNode returns true, generate LOAD_STACK_GUARD.
2702   // Otherwise, emit a volatile load to retrieve the stack guard value.
2703   SDValue Chain = DAG.getEntryNode();
2704   if (TLI.useLoadStackGuardNode()) {
2705     Guard = getLoadStackGuard(DAG, dl, Chain);
2706   } else {
2707     const Value *IRGuard = TLI.getSDagStackGuard(M);
2708     SDValue GuardPtr = getValue(IRGuard);
2709 
2710     Guard = DAG.getLoad(PtrMemTy, dl, Chain, GuardPtr,
2711                         MachinePointerInfo(IRGuard, 0), Align,
2712                         MachineMemOperand::MOVolatile);
2713   }
2714 
2715   // Perform the comparison via a getsetcc.
2716   SDValue Cmp = DAG.getSetCC(dl, TLI.getSetCCResultType(DAG.getDataLayout(),
2717                                                         *DAG.getContext(),
2718                                                         Guard.getValueType()),
2719                              Guard, GuardVal, ISD::SETNE);
2720 
2721   // If the guard/stackslot do not equal, branch to failure MBB.
2722   SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
2723                                MVT::Other, GuardVal.getOperand(0),
2724                                Cmp, DAG.getBasicBlock(SPD.getFailureMBB()));
2725   // Otherwise branch to success MBB.
2726   SDValue Br = DAG.getNode(ISD::BR, dl,
2727                            MVT::Other, BrCond,
2728                            DAG.getBasicBlock(SPD.getSuccessMBB()));
2729 
2730   DAG.setRoot(Br);
2731 }
2732 
2733 /// Codegen the failure basic block for a stack protector check.
2734 ///
2735 /// A failure stack protector machine basic block consists simply of a call to
2736 /// __stack_chk_fail().
2737 ///
2738 /// For a high level explanation of how this fits into the stack protector
2739 /// generation see the comment on the declaration of class
2740 /// StackProtectorDescriptor.
2741 void
2742 SelectionDAGBuilder::visitSPDescriptorFailure(StackProtectorDescriptor &SPD) {
2743   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2744   TargetLowering::MakeLibCallOptions CallOptions;
2745   CallOptions.setDiscardResult(true);
2746   SDValue Chain =
2747       TLI.makeLibCall(DAG, RTLIB::STACKPROTECTOR_CHECK_FAIL, MVT::isVoid,
2748                       None, CallOptions, getCurSDLoc()).second;
2749   // On PS4, the "return address" must still be within the calling function,
2750   // even if it's at the very end, so emit an explicit TRAP here.
2751   // Passing 'true' for doesNotReturn above won't generate the trap for us.
2752   if (TM.getTargetTriple().isPS4())
2753     Chain = DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, Chain);
2754   // WebAssembly needs an unreachable instruction after a non-returning call,
2755   // because the function return type can be different from __stack_chk_fail's
2756   // return type (void).
2757   if (TM.getTargetTriple().isWasm())
2758     Chain = DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, Chain);
2759 
2760   DAG.setRoot(Chain);
2761 }
2762 
2763 /// visitBitTestHeader - This function emits necessary code to produce value
2764 /// suitable for "bit tests"
2765 void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B,
2766                                              MachineBasicBlock *SwitchBB) {
2767   SDLoc dl = getCurSDLoc();
2768 
2769   // Subtract the minimum value.
2770   SDValue SwitchOp = getValue(B.SValue);
2771   EVT VT = SwitchOp.getValueType();
2772   SDValue RangeSub =
2773       DAG.getNode(ISD::SUB, dl, VT, SwitchOp, DAG.getConstant(B.First, dl, VT));
2774 
2775   // Determine the type of the test operands.
2776   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2777   bool UsePtrType = false;
2778   if (!TLI.isTypeLegal(VT)) {
2779     UsePtrType = true;
2780   } else {
2781     for (unsigned i = 0, e = B.Cases.size(); i != e; ++i)
2782       if (!isUIntN(VT.getSizeInBits(), B.Cases[i].Mask)) {
2783         // Switch table case range are encoded into series of masks.
2784         // Just use pointer type, it's guaranteed to fit.
2785         UsePtrType = true;
2786         break;
2787       }
2788   }
2789   SDValue Sub = RangeSub;
2790   if (UsePtrType) {
2791     VT = TLI.getPointerTy(DAG.getDataLayout());
2792     Sub = DAG.getZExtOrTrunc(Sub, dl, VT);
2793   }
2794 
2795   B.RegVT = VT.getSimpleVT();
2796   B.Reg = FuncInfo.CreateReg(B.RegVT);
2797   SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), dl, B.Reg, Sub);
2798 
2799   MachineBasicBlock* MBB = B.Cases[0].ThisBB;
2800 
2801   if (!B.FallthroughUnreachable)
2802     addSuccessorWithProb(SwitchBB, B.Default, B.DefaultProb);
2803   addSuccessorWithProb(SwitchBB, MBB, B.Prob);
2804   SwitchBB->normalizeSuccProbs();
2805 
2806   SDValue Root = CopyTo;
2807   if (!B.FallthroughUnreachable) {
2808     // Conditional branch to the default block.
2809     SDValue RangeCmp = DAG.getSetCC(dl,
2810         TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
2811                                RangeSub.getValueType()),
2812         RangeSub, DAG.getConstant(B.Range, dl, RangeSub.getValueType()),
2813         ISD::SETUGT);
2814 
2815     Root = DAG.getNode(ISD::BRCOND, dl, MVT::Other, Root, RangeCmp,
2816                        DAG.getBasicBlock(B.Default));
2817   }
2818 
2819   // Avoid emitting unnecessary branches to the next block.
2820   if (MBB != NextBlock(SwitchBB))
2821     Root = DAG.getNode(ISD::BR, dl, MVT::Other, Root, DAG.getBasicBlock(MBB));
2822 
2823   DAG.setRoot(Root);
2824 }
2825 
2826 /// visitBitTestCase - this function produces one "bit test"
2827 void SelectionDAGBuilder::visitBitTestCase(BitTestBlock &BB,
2828                                            MachineBasicBlock* NextMBB,
2829                                            BranchProbability BranchProbToNext,
2830                                            unsigned Reg,
2831                                            BitTestCase &B,
2832                                            MachineBasicBlock *SwitchBB) {
2833   SDLoc dl = getCurSDLoc();
2834   MVT VT = BB.RegVT;
2835   SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), dl, Reg, VT);
2836   SDValue Cmp;
2837   unsigned PopCount = countPopulation(B.Mask);
2838   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2839   if (PopCount == 1) {
2840     // Testing for a single bit; just compare the shift count with what it
2841     // would need to be to shift a 1 bit in that position.
2842     Cmp = DAG.getSetCC(
2843         dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
2844         ShiftOp, DAG.getConstant(countTrailingZeros(B.Mask), dl, VT),
2845         ISD::SETEQ);
2846   } else if (PopCount == BB.Range) {
2847     // There is only one zero bit in the range, test for it directly.
2848     Cmp = DAG.getSetCC(
2849         dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
2850         ShiftOp, DAG.getConstant(countTrailingOnes(B.Mask), dl, VT),
2851         ISD::SETNE);
2852   } else {
2853     // Make desired shift
2854     SDValue SwitchVal = DAG.getNode(ISD::SHL, dl, VT,
2855                                     DAG.getConstant(1, dl, VT), ShiftOp);
2856 
2857     // Emit bit tests and jumps
2858     SDValue AndOp = DAG.getNode(ISD::AND, dl,
2859                                 VT, SwitchVal, DAG.getConstant(B.Mask, dl, VT));
2860     Cmp = DAG.getSetCC(
2861         dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
2862         AndOp, DAG.getConstant(0, dl, VT), ISD::SETNE);
2863   }
2864 
2865   // The branch probability from SwitchBB to B.TargetBB is B.ExtraProb.
2866   addSuccessorWithProb(SwitchBB, B.TargetBB, B.ExtraProb);
2867   // The branch probability from SwitchBB to NextMBB is BranchProbToNext.
2868   addSuccessorWithProb(SwitchBB, NextMBB, BranchProbToNext);
2869   // It is not guaranteed that the sum of B.ExtraProb and BranchProbToNext is
2870   // one as they are relative probabilities (and thus work more like weights),
2871   // and hence we need to normalize them to let the sum of them become one.
2872   SwitchBB->normalizeSuccProbs();
2873 
2874   SDValue BrAnd = DAG.getNode(ISD::BRCOND, dl,
2875                               MVT::Other, getControlRoot(),
2876                               Cmp, DAG.getBasicBlock(B.TargetBB));
2877 
2878   // Avoid emitting unnecessary branches to the next block.
2879   if (NextMBB != NextBlock(SwitchBB))
2880     BrAnd = DAG.getNode(ISD::BR, dl, MVT::Other, BrAnd,
2881                         DAG.getBasicBlock(NextMBB));
2882 
2883   DAG.setRoot(BrAnd);
2884 }
2885 
2886 void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
2887   MachineBasicBlock *InvokeMBB = FuncInfo.MBB;
2888 
2889   // Retrieve successors. Look through artificial IR level blocks like
2890   // catchswitch for successors.
2891   MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
2892   const BasicBlock *EHPadBB = I.getSuccessor(1);
2893 
2894   // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't
2895   // have to do anything here to lower funclet bundles.
2896   assert(!I.hasOperandBundlesOtherThan(
2897              {LLVMContext::OB_deopt, LLVMContext::OB_gc_transition,
2898               LLVMContext::OB_gc_live, LLVMContext::OB_funclet,
2899               LLVMContext::OB_cfguardtarget,
2900               LLVMContext::OB_clang_arc_attachedcall}) &&
2901          "Cannot lower invokes with arbitrary operand bundles yet!");
2902 
2903   const Value *Callee(I.getCalledOperand());
2904   const Function *Fn = dyn_cast<Function>(Callee);
2905   if (isa<InlineAsm>(Callee))
2906     visitInlineAsm(I, EHPadBB);
2907   else if (Fn && Fn->isIntrinsic()) {
2908     switch (Fn->getIntrinsicID()) {
2909     default:
2910       llvm_unreachable("Cannot invoke this intrinsic");
2911     case Intrinsic::donothing:
2912       // Ignore invokes to @llvm.donothing: jump directly to the next BB.
2913     case Intrinsic::seh_try_begin:
2914     case Intrinsic::seh_scope_begin:
2915     case Intrinsic::seh_try_end:
2916     case Intrinsic::seh_scope_end:
2917       break;
2918     case Intrinsic::experimental_patchpoint_void:
2919     case Intrinsic::experimental_patchpoint_i64:
2920       visitPatchpoint(I, EHPadBB);
2921       break;
2922     case Intrinsic::experimental_gc_statepoint:
2923       LowerStatepoint(cast<GCStatepointInst>(I), EHPadBB);
2924       break;
2925     case Intrinsic::wasm_rethrow: {
2926       // This is usually done in visitTargetIntrinsic, but this intrinsic is
2927       // special because it can be invoked, so we manually lower it to a DAG
2928       // node here.
2929       SmallVector<SDValue, 8> Ops;
2930       Ops.push_back(getRoot()); // inchain
2931       const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2932       Ops.push_back(
2933           DAG.getTargetConstant(Intrinsic::wasm_rethrow, getCurSDLoc(),
2934                                 TLI.getPointerTy(DAG.getDataLayout())));
2935       SDVTList VTs = DAG.getVTList(ArrayRef<EVT>({MVT::Other})); // outchain
2936       DAG.setRoot(DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops));
2937       break;
2938     }
2939     }
2940   } else if (I.countOperandBundlesOfType(LLVMContext::OB_deopt)) {
2941     // Currently we do not lower any intrinsic calls with deopt operand bundles.
2942     // Eventually we will support lowering the @llvm.experimental.deoptimize
2943     // intrinsic, and right now there are no plans to support other intrinsics
2944     // with deopt state.
2945     LowerCallSiteWithDeoptBundle(&I, getValue(Callee), EHPadBB);
2946   } else {
2947     LowerCallTo(I, getValue(Callee), false, false, EHPadBB);
2948   }
2949 
2950   // If the value of the invoke is used outside of its defining block, make it
2951   // available as a virtual register.
2952   // We already took care of the exported value for the statepoint instruction
2953   // during call to the LowerStatepoint.
2954   if (!isa<GCStatepointInst>(I)) {
2955     CopyToExportRegsIfNeeded(&I);
2956   }
2957 
2958   SmallVector<std::pair<MachineBasicBlock *, BranchProbability>, 1> UnwindDests;
2959   BranchProbabilityInfo *BPI = FuncInfo.BPI;
2960   BranchProbability EHPadBBProb =
2961       BPI ? BPI->getEdgeProbability(InvokeMBB->getBasicBlock(), EHPadBB)
2962           : BranchProbability::getZero();
2963   findUnwindDestinations(FuncInfo, EHPadBB, EHPadBBProb, UnwindDests);
2964 
2965   // Update successor info.
2966   addSuccessorWithProb(InvokeMBB, Return);
2967   for (auto &UnwindDest : UnwindDests) {
2968     UnwindDest.first->setIsEHPad();
2969     addSuccessorWithProb(InvokeMBB, UnwindDest.first, UnwindDest.second);
2970   }
2971   InvokeMBB->normalizeSuccProbs();
2972 
2973   // Drop into normal successor.
2974   DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, getControlRoot(),
2975                           DAG.getBasicBlock(Return)));
2976 }
2977 
2978 void SelectionDAGBuilder::visitCallBr(const CallBrInst &I) {
2979   MachineBasicBlock *CallBrMBB = FuncInfo.MBB;
2980 
2981   // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't
2982   // have to do anything here to lower funclet bundles.
2983   assert(!I.hasOperandBundlesOtherThan(
2984              {LLVMContext::OB_deopt, LLVMContext::OB_funclet}) &&
2985          "Cannot lower callbrs with arbitrary operand bundles yet!");
2986 
2987   assert(I.isInlineAsm() && "Only know how to handle inlineasm callbr");
2988   visitInlineAsm(I);
2989   CopyToExportRegsIfNeeded(&I);
2990 
2991   // Retrieve successors.
2992   MachineBasicBlock *Return = FuncInfo.MBBMap[I.getDefaultDest()];
2993 
2994   // Update successor info.
2995   addSuccessorWithProb(CallBrMBB, Return, BranchProbability::getOne());
2996   for (unsigned i = 0, e = I.getNumIndirectDests(); i < e; ++i) {
2997     MachineBasicBlock *Target = FuncInfo.MBBMap[I.getIndirectDest(i)];
2998     addSuccessorWithProb(CallBrMBB, Target, BranchProbability::getZero());
2999     Target->setIsInlineAsmBrIndirectTarget();
3000   }
3001   CallBrMBB->normalizeSuccProbs();
3002 
3003   // Drop into default successor.
3004   DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
3005                           MVT::Other, getControlRoot(),
3006                           DAG.getBasicBlock(Return)));
3007 }
3008 
3009 void SelectionDAGBuilder::visitResume(const ResumeInst &RI) {
3010   llvm_unreachable("SelectionDAGBuilder shouldn't visit resume instructions!");
3011 }
3012 
3013 void SelectionDAGBuilder::visitLandingPad(const LandingPadInst &LP) {
3014   assert(FuncInfo.MBB->isEHPad() &&
3015          "Call to landingpad not in landing pad!");
3016 
3017   // If there aren't registers to copy the values into (e.g., during SjLj
3018   // exceptions), then don't bother to create these DAG nodes.
3019   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3020   const Constant *PersonalityFn = FuncInfo.Fn->getPersonalityFn();
3021   if (TLI.getExceptionPointerRegister(PersonalityFn) == 0 &&
3022       TLI.getExceptionSelectorRegister(PersonalityFn) == 0)
3023     return;
3024 
3025   // If landingpad's return type is token type, we don't create DAG nodes
3026   // for its exception pointer and selector value. The extraction of exception
3027   // pointer or selector value from token type landingpads is not currently
3028   // supported.
3029   if (LP.getType()->isTokenTy())
3030     return;
3031 
3032   SmallVector<EVT, 2> ValueVTs;
3033   SDLoc dl = getCurSDLoc();
3034   ComputeValueVTs(TLI, DAG.getDataLayout(), LP.getType(), ValueVTs);
3035   assert(ValueVTs.size() == 2 && "Only two-valued landingpads are supported");
3036 
3037   // Get the two live-in registers as SDValues. The physregs have already been
3038   // copied into virtual registers.
3039   SDValue Ops[2];
3040   if (FuncInfo.ExceptionPointerVirtReg) {
3041     Ops[0] = DAG.getZExtOrTrunc(
3042         DAG.getCopyFromReg(DAG.getEntryNode(), dl,
3043                            FuncInfo.ExceptionPointerVirtReg,
3044                            TLI.getPointerTy(DAG.getDataLayout())),
3045         dl, ValueVTs[0]);
3046   } else {
3047     Ops[0] = DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout()));
3048   }
3049   Ops[1] = DAG.getZExtOrTrunc(
3050       DAG.getCopyFromReg(DAG.getEntryNode(), dl,
3051                          FuncInfo.ExceptionSelectorVirtReg,
3052                          TLI.getPointerTy(DAG.getDataLayout())),
3053       dl, ValueVTs[1]);
3054 
3055   // Merge into one.
3056   SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
3057                             DAG.getVTList(ValueVTs), Ops);
3058   setValue(&LP, Res);
3059 }
3060 
3061 void SelectionDAGBuilder::UpdateSplitBlock(MachineBasicBlock *First,
3062                                            MachineBasicBlock *Last) {
3063   // Update JTCases.
3064   for (JumpTableBlock &JTB : SL->JTCases)
3065     if (JTB.first.HeaderBB == First)
3066       JTB.first.HeaderBB = Last;
3067 
3068   // Update BitTestCases.
3069   for (BitTestBlock &BTB : SL->BitTestCases)
3070     if (BTB.Parent == First)
3071       BTB.Parent = Last;
3072 }
3073 
3074 void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
3075   MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB;
3076 
3077   // Update machine-CFG edges with unique successors.
3078   SmallSet<BasicBlock*, 32> Done;
3079   for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) {
3080     BasicBlock *BB = I.getSuccessor(i);
3081     bool Inserted = Done.insert(BB).second;
3082     if (!Inserted)
3083         continue;
3084 
3085     MachineBasicBlock *Succ = FuncInfo.MBBMap[BB];
3086     addSuccessorWithProb(IndirectBrMBB, Succ);
3087   }
3088   IndirectBrMBB->normalizeSuccProbs();
3089 
3090   DAG.setRoot(DAG.getNode(ISD::BRIND, getCurSDLoc(),
3091                           MVT::Other, getControlRoot(),
3092                           getValue(I.getAddress())));
3093 }
3094 
3095 void SelectionDAGBuilder::visitUnreachable(const UnreachableInst &I) {
3096   if (!DAG.getTarget().Options.TrapUnreachable)
3097     return;
3098 
3099   // We may be able to ignore unreachable behind a noreturn call.
3100   if (DAG.getTarget().Options.NoTrapAfterNoreturn) {
3101     const BasicBlock &BB = *I.getParent();
3102     if (&I != &BB.front()) {
3103       BasicBlock::const_iterator PredI =
3104         std::prev(BasicBlock::const_iterator(&I));
3105       if (const CallInst *Call = dyn_cast<CallInst>(&*PredI)) {
3106         if (Call->doesNotReturn())
3107           return;
3108       }
3109     }
3110   }
3111 
3112   DAG.setRoot(DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, DAG.getRoot()));
3113 }
3114 
3115 void SelectionDAGBuilder::visitUnary(const User &I, unsigned Opcode) {
3116   SDNodeFlags Flags;
3117   if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3118     Flags.copyFMF(*FPOp);
3119 
3120   SDValue Op = getValue(I.getOperand(0));
3121   SDValue UnNodeValue = DAG.getNode(Opcode, getCurSDLoc(), Op.getValueType(),
3122                                     Op, Flags);
3123   setValue(&I, UnNodeValue);
3124 }
3125 
3126 void SelectionDAGBuilder::visitBinary(const User &I, unsigned Opcode) {
3127   SDNodeFlags Flags;
3128   if (auto *OFBinOp = dyn_cast<OverflowingBinaryOperator>(&I)) {
3129     Flags.setNoSignedWrap(OFBinOp->hasNoSignedWrap());
3130     Flags.setNoUnsignedWrap(OFBinOp->hasNoUnsignedWrap());
3131   }
3132   if (auto *ExactOp = dyn_cast<PossiblyExactOperator>(&I))
3133     Flags.setExact(ExactOp->isExact());
3134   if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3135     Flags.copyFMF(*FPOp);
3136 
3137   SDValue Op1 = getValue(I.getOperand(0));
3138   SDValue Op2 = getValue(I.getOperand(1));
3139   SDValue BinNodeValue = DAG.getNode(Opcode, getCurSDLoc(), Op1.getValueType(),
3140                                      Op1, Op2, Flags);
3141   setValue(&I, BinNodeValue);
3142 }
3143 
3144 void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
3145   SDValue Op1 = getValue(I.getOperand(0));
3146   SDValue Op2 = getValue(I.getOperand(1));
3147 
3148   EVT ShiftTy = DAG.getTargetLoweringInfo().getShiftAmountTy(
3149       Op1.getValueType(), DAG.getDataLayout());
3150 
3151   // Coerce the shift amount to the right type if we can. This exposes the
3152   // truncate or zext to optimization early.
3153   if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) {
3154     assert(ShiftTy.getSizeInBits() >= Log2_32_Ceil(Op1.getValueSizeInBits()) &&
3155            "Unexpected shift type");
3156     Op2 = DAG.getZExtOrTrunc(Op2, getCurSDLoc(), ShiftTy);
3157   }
3158 
3159   bool nuw = false;
3160   bool nsw = false;
3161   bool exact = false;
3162 
3163   if (Opcode == ISD::SRL || Opcode == ISD::SRA || Opcode == ISD::SHL) {
3164 
3165     if (const OverflowingBinaryOperator *OFBinOp =
3166             dyn_cast<const OverflowingBinaryOperator>(&I)) {
3167       nuw = OFBinOp->hasNoUnsignedWrap();
3168       nsw = OFBinOp->hasNoSignedWrap();
3169     }
3170     if (const PossiblyExactOperator *ExactOp =
3171             dyn_cast<const PossiblyExactOperator>(&I))
3172       exact = ExactOp->isExact();
3173   }
3174   SDNodeFlags Flags;
3175   Flags.setExact(exact);
3176   Flags.setNoSignedWrap(nsw);
3177   Flags.setNoUnsignedWrap(nuw);
3178   SDValue Res = DAG.getNode(Opcode, getCurSDLoc(), Op1.getValueType(), Op1, Op2,
3179                             Flags);
3180   setValue(&I, Res);
3181 }
3182 
3183 void SelectionDAGBuilder::visitSDiv(const User &I) {
3184   SDValue Op1 = getValue(I.getOperand(0));
3185   SDValue Op2 = getValue(I.getOperand(1));
3186 
3187   SDNodeFlags Flags;
3188   Flags.setExact(isa<PossiblyExactOperator>(&I) &&
3189                  cast<PossiblyExactOperator>(&I)->isExact());
3190   setValue(&I, DAG.getNode(ISD::SDIV, getCurSDLoc(), Op1.getValueType(), Op1,
3191                            Op2, Flags));
3192 }
3193 
3194 void SelectionDAGBuilder::visitICmp(const User &I) {
3195   ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
3196   if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I))
3197     predicate = IC->getPredicate();
3198   else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
3199     predicate = ICmpInst::Predicate(IC->getPredicate());
3200   SDValue Op1 = getValue(I.getOperand(0));
3201   SDValue Op2 = getValue(I.getOperand(1));
3202   ISD::CondCode Opcode = getICmpCondCode(predicate);
3203 
3204   auto &TLI = DAG.getTargetLoweringInfo();
3205   EVT MemVT =
3206       TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
3207 
3208   // If a pointer's DAG type is larger than its memory type then the DAG values
3209   // are zero-extended. This breaks signed comparisons so truncate back to the
3210   // underlying type before doing the compare.
3211   if (Op1.getValueType() != MemVT) {
3212     Op1 = DAG.getPtrExtOrTrunc(Op1, getCurSDLoc(), MemVT);
3213     Op2 = DAG.getPtrExtOrTrunc(Op2, getCurSDLoc(), MemVT);
3214   }
3215 
3216   EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3217                                                         I.getType());
3218   setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Opcode));
3219 }
3220 
3221 void SelectionDAGBuilder::visitFCmp(const User &I) {
3222   FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
3223   if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I))
3224     predicate = FC->getPredicate();
3225   else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
3226     predicate = FCmpInst::Predicate(FC->getPredicate());
3227   SDValue Op1 = getValue(I.getOperand(0));
3228   SDValue Op2 = getValue(I.getOperand(1));
3229 
3230   ISD::CondCode Condition = getFCmpCondCode(predicate);
3231   auto *FPMO = cast<FPMathOperator>(&I);
3232   if (FPMO->hasNoNaNs() || TM.Options.NoNaNsFPMath)
3233     Condition = getFCmpCodeWithoutNaN(Condition);
3234 
3235   SDNodeFlags Flags;
3236   Flags.copyFMF(*FPMO);
3237   SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
3238 
3239   EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3240                                                         I.getType());
3241   setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Condition));
3242 }
3243 
3244 // Check if the condition of the select has one use or two users that are both
3245 // selects with the same condition.
3246 static bool hasOnlySelectUsers(const Value *Cond) {
3247   return llvm::all_of(Cond->users(), [](const Value *V) {
3248     return isa<SelectInst>(V);
3249   });
3250 }
3251 
3252 void SelectionDAGBuilder::visitSelect(const User &I) {
3253   SmallVector<EVT, 4> ValueVTs;
3254   ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
3255                   ValueVTs);
3256   unsigned NumValues = ValueVTs.size();
3257   if (NumValues == 0) return;
3258 
3259   SmallVector<SDValue, 4> Values(NumValues);
3260   SDValue Cond     = getValue(I.getOperand(0));
3261   SDValue LHSVal   = getValue(I.getOperand(1));
3262   SDValue RHSVal   = getValue(I.getOperand(2));
3263   SmallVector<SDValue, 1> BaseOps(1, Cond);
3264   ISD::NodeType OpCode =
3265       Cond.getValueType().isVector() ? ISD::VSELECT : ISD::SELECT;
3266 
3267   bool IsUnaryAbs = false;
3268   bool Negate = false;
3269 
3270   SDNodeFlags Flags;
3271   if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3272     Flags.copyFMF(*FPOp);
3273 
3274   // Min/max matching is only viable if all output VTs are the same.
3275   if (is_splat(ValueVTs)) {
3276     EVT VT = ValueVTs[0];
3277     LLVMContext &Ctx = *DAG.getContext();
3278     auto &TLI = DAG.getTargetLoweringInfo();
3279 
3280     // We care about the legality of the operation after it has been type
3281     // legalized.
3282     while (TLI.getTypeAction(Ctx, VT) != TargetLoweringBase::TypeLegal)
3283       VT = TLI.getTypeToTransformTo(Ctx, VT);
3284 
3285     // If the vselect is legal, assume we want to leave this as a vector setcc +
3286     // vselect. Otherwise, if this is going to be scalarized, we want to see if
3287     // min/max is legal on the scalar type.
3288     bool UseScalarMinMax = VT.isVector() &&
3289       !TLI.isOperationLegalOrCustom(ISD::VSELECT, VT);
3290 
3291     Value *LHS, *RHS;
3292     auto SPR = matchSelectPattern(const_cast<User*>(&I), LHS, RHS);
3293     ISD::NodeType Opc = ISD::DELETED_NODE;
3294     switch (SPR.Flavor) {
3295     case SPF_UMAX:    Opc = ISD::UMAX; break;
3296     case SPF_UMIN:    Opc = ISD::UMIN; break;
3297     case SPF_SMAX:    Opc = ISD::SMAX; break;
3298     case SPF_SMIN:    Opc = ISD::SMIN; break;
3299     case SPF_FMINNUM:
3300       switch (SPR.NaNBehavior) {
3301       case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?");
3302       case SPNB_RETURNS_NAN:   Opc = ISD::FMINIMUM; break;
3303       case SPNB_RETURNS_OTHER: Opc = ISD::FMINNUM; break;
3304       case SPNB_RETURNS_ANY: {
3305         if (TLI.isOperationLegalOrCustom(ISD::FMINNUM, VT))
3306           Opc = ISD::FMINNUM;
3307         else if (TLI.isOperationLegalOrCustom(ISD::FMINIMUM, VT))
3308           Opc = ISD::FMINIMUM;
3309         else if (UseScalarMinMax)
3310           Opc = TLI.isOperationLegalOrCustom(ISD::FMINNUM, VT.getScalarType()) ?
3311             ISD::FMINNUM : ISD::FMINIMUM;
3312         break;
3313       }
3314       }
3315       break;
3316     case SPF_FMAXNUM:
3317       switch (SPR.NaNBehavior) {
3318       case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?");
3319       case SPNB_RETURNS_NAN:   Opc = ISD::FMAXIMUM; break;
3320       case SPNB_RETURNS_OTHER: Opc = ISD::FMAXNUM; break;
3321       case SPNB_RETURNS_ANY:
3322 
3323         if (TLI.isOperationLegalOrCustom(ISD::FMAXNUM, VT))
3324           Opc = ISD::FMAXNUM;
3325         else if (TLI.isOperationLegalOrCustom(ISD::FMAXIMUM, VT))
3326           Opc = ISD::FMAXIMUM;
3327         else if (UseScalarMinMax)
3328           Opc = TLI.isOperationLegalOrCustom(ISD::FMAXNUM, VT.getScalarType()) ?
3329             ISD::FMAXNUM : ISD::FMAXIMUM;
3330         break;
3331       }
3332       break;
3333     case SPF_NABS:
3334       Negate = true;
3335       LLVM_FALLTHROUGH;
3336     case SPF_ABS:
3337       IsUnaryAbs = true;
3338       Opc = ISD::ABS;
3339       break;
3340     default: break;
3341     }
3342 
3343     if (!IsUnaryAbs && Opc != ISD::DELETED_NODE &&
3344         (TLI.isOperationLegalOrCustom(Opc, VT) ||
3345          (UseScalarMinMax &&
3346           TLI.isOperationLegalOrCustom(Opc, VT.getScalarType()))) &&
3347         // If the underlying comparison instruction is used by any other
3348         // instruction, the consumed instructions won't be destroyed, so it is
3349         // not profitable to convert to a min/max.
3350         hasOnlySelectUsers(cast<SelectInst>(I).getCondition())) {
3351       OpCode = Opc;
3352       LHSVal = getValue(LHS);
3353       RHSVal = getValue(RHS);
3354       BaseOps.clear();
3355     }
3356 
3357     if (IsUnaryAbs) {
3358       OpCode = Opc;
3359       LHSVal = getValue(LHS);
3360       BaseOps.clear();
3361     }
3362   }
3363 
3364   if (IsUnaryAbs) {
3365     for (unsigned i = 0; i != NumValues; ++i) {
3366       SDLoc dl = getCurSDLoc();
3367       EVT VT = LHSVal.getNode()->getValueType(LHSVal.getResNo() + i);
3368       Values[i] =
3369           DAG.getNode(OpCode, dl, VT, LHSVal.getValue(LHSVal.getResNo() + i));
3370       if (Negate)
3371         Values[i] = DAG.getNode(ISD::SUB, dl, VT, DAG.getConstant(0, dl, VT),
3372                                 Values[i]);
3373     }
3374   } else {
3375     for (unsigned i = 0; i != NumValues; ++i) {
3376       SmallVector<SDValue, 3> Ops(BaseOps.begin(), BaseOps.end());
3377       Ops.push_back(SDValue(LHSVal.getNode(), LHSVal.getResNo() + i));
3378       Ops.push_back(SDValue(RHSVal.getNode(), RHSVal.getResNo() + i));
3379       Values[i] = DAG.getNode(
3380           OpCode, getCurSDLoc(),
3381           LHSVal.getNode()->getValueType(LHSVal.getResNo() + i), Ops, Flags);
3382     }
3383   }
3384 
3385   setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
3386                            DAG.getVTList(ValueVTs), Values));
3387 }
3388 
3389 void SelectionDAGBuilder::visitTrunc(const User &I) {
3390   // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
3391   SDValue N = getValue(I.getOperand(0));
3392   EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3393                                                         I.getType());
3394   setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), DestVT, N));
3395 }
3396 
3397 void SelectionDAGBuilder::visitZExt(const User &I) {
3398   // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
3399   // ZExt also can't be a cast to bool for same reason. So, nothing much to do
3400   SDValue N = getValue(I.getOperand(0));
3401   EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3402                                                         I.getType());
3403   setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(), DestVT, N));
3404 }
3405 
3406 void SelectionDAGBuilder::visitSExt(const User &I) {
3407   // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
3408   // SExt also can't be a cast to bool for same reason. So, nothing much to do
3409   SDValue N = getValue(I.getOperand(0));
3410   EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3411                                                         I.getType());
3412   setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
3413 }
3414 
3415 void SelectionDAGBuilder::visitFPTrunc(const User &I) {
3416   // FPTrunc is never a no-op cast, no need to check
3417   SDValue N = getValue(I.getOperand(0));
3418   SDLoc dl = getCurSDLoc();
3419   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3420   EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
3421   setValue(&I, DAG.getNode(ISD::FP_ROUND, dl, DestVT, N,
3422                            DAG.getTargetConstant(
3423                                0, dl, TLI.getPointerTy(DAG.getDataLayout()))));
3424 }
3425 
3426 void SelectionDAGBuilder::visitFPExt(const User &I) {
3427   // FPExt is never a no-op cast, no need to check
3428   SDValue N = getValue(I.getOperand(0));
3429   EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3430                                                         I.getType());
3431   setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurSDLoc(), DestVT, N));
3432 }
3433 
3434 void SelectionDAGBuilder::visitFPToUI(const User &I) {
3435   // FPToUI is never a no-op cast, no need to check
3436   SDValue N = getValue(I.getOperand(0));
3437   EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3438                                                         I.getType());
3439   setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurSDLoc(), DestVT, N));
3440 }
3441 
3442 void SelectionDAGBuilder::visitFPToSI(const User &I) {
3443   // FPToSI is never a no-op cast, no need to check
3444   SDValue N = getValue(I.getOperand(0));
3445   EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3446                                                         I.getType());
3447   setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurSDLoc(), DestVT, N));
3448 }
3449 
3450 void SelectionDAGBuilder::visitUIToFP(const User &I) {
3451   // UIToFP is never a no-op cast, no need to check
3452   SDValue N = getValue(I.getOperand(0));
3453   EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3454                                                         I.getType());
3455   setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurSDLoc(), DestVT, N));
3456 }
3457 
3458 void SelectionDAGBuilder::visitSIToFP(const User &I) {
3459   // SIToFP is never a no-op cast, no need to check
3460   SDValue N = getValue(I.getOperand(0));
3461   EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3462                                                         I.getType());
3463   setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurSDLoc(), DestVT, N));
3464 }
3465 
3466 void SelectionDAGBuilder::visitPtrToInt(const User &I) {
3467   // What to do depends on the size of the integer and the size of the pointer.
3468   // We can either truncate, zero extend, or no-op, accordingly.
3469   SDValue N = getValue(I.getOperand(0));
3470   auto &TLI = DAG.getTargetLoweringInfo();
3471   EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3472                                                         I.getType());
3473   EVT PtrMemVT =
3474       TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
3475   N = DAG.getPtrExtOrTrunc(N, getCurSDLoc(), PtrMemVT);
3476   N = DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT);
3477   setValue(&I, N);
3478 }
3479 
3480 void SelectionDAGBuilder::visitIntToPtr(const User &I) {
3481   // What to do depends on the size of the integer and the size of the pointer.
3482   // We can either truncate, zero extend, or no-op, accordingly.
3483   SDValue N = getValue(I.getOperand(0));
3484   auto &TLI = DAG.getTargetLoweringInfo();
3485   EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
3486   EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), I.getType());
3487   N = DAG.getZExtOrTrunc(N, getCurSDLoc(), PtrMemVT);
3488   N = DAG.getPtrExtOrTrunc(N, getCurSDLoc(), DestVT);
3489   setValue(&I, N);
3490 }
3491 
3492 void SelectionDAGBuilder::visitBitCast(const User &I) {
3493   SDValue N = getValue(I.getOperand(0));
3494   SDLoc dl = getCurSDLoc();
3495   EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3496                                                         I.getType());
3497 
3498   // BitCast assures us that source and destination are the same size so this is
3499   // either a BITCAST or a no-op.
3500   if (DestVT != N.getValueType())
3501     setValue(&I, DAG.getNode(ISD::BITCAST, dl,
3502                              DestVT, N)); // convert types.
3503   // Check if the original LLVM IR Operand was a ConstantInt, because getValue()
3504   // might fold any kind of constant expression to an integer constant and that
3505   // is not what we are looking for. Only recognize a bitcast of a genuine
3506   // constant integer as an opaque constant.
3507   else if(ConstantInt *C = dyn_cast<ConstantInt>(I.getOperand(0)))
3508     setValue(&I, DAG.getConstant(C->getValue(), dl, DestVT, /*isTarget=*/false,
3509                                  /*isOpaque*/true));
3510   else
3511     setValue(&I, N);            // noop cast.
3512 }
3513 
3514 void SelectionDAGBuilder::visitAddrSpaceCast(const User &I) {
3515   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3516   const Value *SV = I.getOperand(0);
3517   SDValue N = getValue(SV);
3518   EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
3519 
3520   unsigned SrcAS = SV->getType()->getPointerAddressSpace();
3521   unsigned DestAS = I.getType()->getPointerAddressSpace();
3522 
3523   if (!TM.isNoopAddrSpaceCast(SrcAS, DestAS))
3524     N = DAG.getAddrSpaceCast(getCurSDLoc(), DestVT, N, SrcAS, DestAS);
3525 
3526   setValue(&I, N);
3527 }
3528 
3529 void SelectionDAGBuilder::visitInsertElement(const User &I) {
3530   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3531   SDValue InVec = getValue(I.getOperand(0));
3532   SDValue InVal = getValue(I.getOperand(1));
3533   SDValue InIdx = DAG.getSExtOrTrunc(getValue(I.getOperand(2)), getCurSDLoc(),
3534                                      TLI.getVectorIdxTy(DAG.getDataLayout()));
3535   setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurSDLoc(),
3536                            TLI.getValueType(DAG.getDataLayout(), I.getType()),
3537                            InVec, InVal, InIdx));
3538 }
3539 
3540 void SelectionDAGBuilder::visitExtractElement(const User &I) {
3541   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3542   SDValue InVec = getValue(I.getOperand(0));
3543   SDValue InIdx = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), getCurSDLoc(),
3544                                      TLI.getVectorIdxTy(DAG.getDataLayout()));
3545   setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurSDLoc(),
3546                            TLI.getValueType(DAG.getDataLayout(), I.getType()),
3547                            InVec, InIdx));
3548 }
3549 
3550 void SelectionDAGBuilder::visitShuffleVector(const User &I) {
3551   SDValue Src1 = getValue(I.getOperand(0));
3552   SDValue Src2 = getValue(I.getOperand(1));
3553   ArrayRef<int> Mask;
3554   if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I))
3555     Mask = SVI->getShuffleMask();
3556   else
3557     Mask = cast<ConstantExpr>(I).getShuffleMask();
3558   SDLoc DL = getCurSDLoc();
3559   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3560   EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
3561   EVT SrcVT = Src1.getValueType();
3562 
3563   if (all_of(Mask, [](int Elem) { return Elem == 0; }) &&
3564       VT.isScalableVector()) {
3565     // Canonical splat form of first element of first input vector.
3566     SDValue FirstElt =
3567         DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, SrcVT.getScalarType(), Src1,
3568                     DAG.getVectorIdxConstant(0, DL));
3569     setValue(&I, DAG.getNode(ISD::SPLAT_VECTOR, DL, VT, FirstElt));
3570     return;
3571   }
3572 
3573   // For now, we only handle splats for scalable vectors.
3574   // The DAGCombiner will perform a BUILD_VECTOR -> SPLAT_VECTOR transformation
3575   // for targets that support a SPLAT_VECTOR for non-scalable vector types.
3576   assert(!VT.isScalableVector() && "Unsupported scalable vector shuffle");
3577 
3578   unsigned SrcNumElts = SrcVT.getVectorNumElements();
3579   unsigned MaskNumElts = Mask.size();
3580 
3581   if (SrcNumElts == MaskNumElts) {
3582     setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, Mask));
3583     return;
3584   }
3585 
3586   // Normalize the shuffle vector since mask and vector length don't match.
3587   if (SrcNumElts < MaskNumElts) {
3588     // Mask is longer than the source vectors. We can use concatenate vector to
3589     // make the mask and vectors lengths match.
3590 
3591     if (MaskNumElts % SrcNumElts == 0) {
3592       // Mask length is a multiple of the source vector length.
3593       // Check if the shuffle is some kind of concatenation of the input
3594       // vectors.
3595       unsigned NumConcat = MaskNumElts / SrcNumElts;
3596       bool IsConcat = true;
3597       SmallVector<int, 8> ConcatSrcs(NumConcat, -1);
3598       for (unsigned i = 0; i != MaskNumElts; ++i) {
3599         int Idx = Mask[i];
3600         if (Idx < 0)
3601           continue;
3602         // Ensure the indices in each SrcVT sized piece are sequential and that
3603         // the same source is used for the whole piece.
3604         if ((Idx % SrcNumElts != (i % SrcNumElts)) ||
3605             (ConcatSrcs[i / SrcNumElts] >= 0 &&
3606              ConcatSrcs[i / SrcNumElts] != (int)(Idx / SrcNumElts))) {
3607           IsConcat = false;
3608           break;
3609         }
3610         // Remember which source this index came from.
3611         ConcatSrcs[i / SrcNumElts] = Idx / SrcNumElts;
3612       }
3613 
3614       // The shuffle is concatenating multiple vectors together. Just emit
3615       // a CONCAT_VECTORS operation.
3616       if (IsConcat) {
3617         SmallVector<SDValue, 8> ConcatOps;
3618         for (auto Src : ConcatSrcs) {
3619           if (Src < 0)
3620             ConcatOps.push_back(DAG.getUNDEF(SrcVT));
3621           else if (Src == 0)
3622             ConcatOps.push_back(Src1);
3623           else
3624             ConcatOps.push_back(Src2);
3625         }
3626         setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, ConcatOps));
3627         return;
3628       }
3629     }
3630 
3631     unsigned PaddedMaskNumElts = alignTo(MaskNumElts, SrcNumElts);
3632     unsigned NumConcat = PaddedMaskNumElts / SrcNumElts;
3633     EVT PaddedVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
3634                                     PaddedMaskNumElts);
3635 
3636     // Pad both vectors with undefs to make them the same length as the mask.
3637     SDValue UndefVal = DAG.getUNDEF(SrcVT);
3638 
3639     SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
3640     SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
3641     MOps1[0] = Src1;
3642     MOps2[0] = Src2;
3643 
3644     Src1 = DAG.getNode(ISD::CONCAT_VECTORS, DL, PaddedVT, MOps1);
3645     Src2 = DAG.getNode(ISD::CONCAT_VECTORS, DL, PaddedVT, MOps2);
3646 
3647     // Readjust mask for new input vector length.
3648     SmallVector<int, 8> MappedOps(PaddedMaskNumElts, -1);
3649     for (unsigned i = 0; i != MaskNumElts; ++i) {
3650       int Idx = Mask[i];
3651       if (Idx >= (int)SrcNumElts)
3652         Idx -= SrcNumElts - PaddedMaskNumElts;
3653       MappedOps[i] = Idx;
3654     }
3655 
3656     SDValue Result = DAG.getVectorShuffle(PaddedVT, DL, Src1, Src2, MappedOps);
3657 
3658     // If the concatenated vector was padded, extract a subvector with the
3659     // correct number of elements.
3660     if (MaskNumElts != PaddedMaskNumElts)
3661       Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Result,
3662                            DAG.getVectorIdxConstant(0, DL));
3663 
3664     setValue(&I, Result);
3665     return;
3666   }
3667 
3668   if (SrcNumElts > MaskNumElts) {
3669     // Analyze the access pattern of the vector to see if we can extract
3670     // two subvectors and do the shuffle.
3671     int StartIdx[2] = { -1, -1 };  // StartIdx to extract from
3672     bool CanExtract = true;
3673     for (int Idx : Mask) {
3674       unsigned Input = 0;
3675       if (Idx < 0)
3676         continue;
3677 
3678       if (Idx >= (int)SrcNumElts) {
3679         Input = 1;
3680         Idx -= SrcNumElts;
3681       }
3682 
3683       // If all the indices come from the same MaskNumElts sized portion of
3684       // the sources we can use extract. Also make sure the extract wouldn't
3685       // extract past the end of the source.
3686       int NewStartIdx = alignDown(Idx, MaskNumElts);
3687       if (NewStartIdx + MaskNumElts > SrcNumElts ||
3688           (StartIdx[Input] >= 0 && StartIdx[Input] != NewStartIdx))
3689         CanExtract = false;
3690       // Make sure we always update StartIdx as we use it to track if all
3691       // elements are undef.
3692       StartIdx[Input] = NewStartIdx;
3693     }
3694 
3695     if (StartIdx[0] < 0 && StartIdx[1] < 0) {
3696       setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
3697       return;
3698     }
3699     if (CanExtract) {
3700       // Extract appropriate subvector and generate a vector shuffle
3701       for (unsigned Input = 0; Input < 2; ++Input) {
3702         SDValue &Src = Input == 0 ? Src1 : Src2;
3703         if (StartIdx[Input] < 0)
3704           Src = DAG.getUNDEF(VT);
3705         else {
3706           Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Src,
3707                             DAG.getVectorIdxConstant(StartIdx[Input], DL));
3708         }
3709       }
3710 
3711       // Calculate new mask.
3712       SmallVector<int, 8> MappedOps(Mask.begin(), Mask.end());
3713       for (int &Idx : MappedOps) {
3714         if (Idx >= (int)SrcNumElts)
3715           Idx -= SrcNumElts + StartIdx[1] - MaskNumElts;
3716         else if (Idx >= 0)
3717           Idx -= StartIdx[0];
3718       }
3719 
3720       setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, MappedOps));
3721       return;
3722     }
3723   }
3724 
3725   // We can't use either concat vectors or extract subvectors so fall back to
3726   // replacing the shuffle with extract and build vector.
3727   // to insert and build vector.
3728   EVT EltVT = VT.getVectorElementType();
3729   SmallVector<SDValue,8> Ops;
3730   for (int Idx : Mask) {
3731     SDValue Res;
3732 
3733     if (Idx < 0) {
3734       Res = DAG.getUNDEF(EltVT);
3735     } else {
3736       SDValue &Src = Idx < (int)SrcNumElts ? Src1 : Src2;
3737       if (Idx >= (int)SrcNumElts) Idx -= SrcNumElts;
3738 
3739       Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Src,
3740                         DAG.getVectorIdxConstant(Idx, DL));
3741     }
3742 
3743     Ops.push_back(Res);
3744   }
3745 
3746   setValue(&I, DAG.getBuildVector(VT, DL, Ops));
3747 }
3748 
3749 void SelectionDAGBuilder::visitInsertValue(const User &I) {
3750   ArrayRef<unsigned> Indices;
3751   if (const InsertValueInst *IV = dyn_cast<InsertValueInst>(&I))
3752     Indices = IV->getIndices();
3753   else
3754     Indices = cast<ConstantExpr>(&I)->getIndices();
3755 
3756   const Value *Op0 = I.getOperand(0);
3757   const Value *Op1 = I.getOperand(1);
3758   Type *AggTy = I.getType();
3759   Type *ValTy = Op1->getType();
3760   bool IntoUndef = isa<UndefValue>(Op0);
3761   bool FromUndef = isa<UndefValue>(Op1);
3762 
3763   unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices);
3764 
3765   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3766   SmallVector<EVT, 4> AggValueVTs;
3767   ComputeValueVTs(TLI, DAG.getDataLayout(), AggTy, AggValueVTs);
3768   SmallVector<EVT, 4> ValValueVTs;
3769   ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs);
3770 
3771   unsigned NumAggValues = AggValueVTs.size();
3772   unsigned NumValValues = ValValueVTs.size();
3773   SmallVector<SDValue, 4> Values(NumAggValues);
3774 
3775   // Ignore an insertvalue that produces an empty object
3776   if (!NumAggValues) {
3777     setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
3778     return;
3779   }
3780 
3781   SDValue Agg = getValue(Op0);
3782   unsigned i = 0;
3783   // Copy the beginning value(s) from the original aggregate.
3784   for (; i != LinearIndex; ++i)
3785     Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
3786                 SDValue(Agg.getNode(), Agg.getResNo() + i);
3787   // Copy values from the inserted value(s).
3788   if (NumValValues) {
3789     SDValue Val = getValue(Op1);
3790     for (; i != LinearIndex + NumValValues; ++i)
3791       Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
3792                   SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
3793   }
3794   // Copy remaining value(s) from the original aggregate.
3795   for (; i != NumAggValues; ++i)
3796     Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
3797                 SDValue(Agg.getNode(), Agg.getResNo() + i);
3798 
3799   setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
3800                            DAG.getVTList(AggValueVTs), Values));
3801 }
3802 
3803 void SelectionDAGBuilder::visitExtractValue(const User &I) {
3804   ArrayRef<unsigned> Indices;
3805   if (const ExtractValueInst *EV = dyn_cast<ExtractValueInst>(&I))
3806     Indices = EV->getIndices();
3807   else
3808     Indices = cast<ConstantExpr>(&I)->getIndices();
3809 
3810   const Value *Op0 = I.getOperand(0);
3811   Type *AggTy = Op0->getType();
3812   Type *ValTy = I.getType();
3813   bool OutOfUndef = isa<UndefValue>(Op0);
3814 
3815   unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices);
3816 
3817   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3818   SmallVector<EVT, 4> ValValueVTs;
3819   ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs);
3820 
3821   unsigned NumValValues = ValValueVTs.size();
3822 
3823   // Ignore a extractvalue that produces an empty object
3824   if (!NumValValues) {
3825     setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
3826     return;
3827   }
3828 
3829   SmallVector<SDValue, 4> Values(NumValValues);
3830 
3831   SDValue Agg = getValue(Op0);
3832   // Copy out the selected value(s).
3833   for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
3834     Values[i - LinearIndex] =
3835       OutOfUndef ?
3836         DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
3837         SDValue(Agg.getNode(), Agg.getResNo() + i);
3838 
3839   setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
3840                            DAG.getVTList(ValValueVTs), Values));
3841 }
3842 
3843 void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
3844   Value *Op0 = I.getOperand(0);
3845   // Note that the pointer operand may be a vector of pointers. Take the scalar
3846   // element which holds a pointer.
3847   unsigned AS = Op0->getType()->getScalarType()->getPointerAddressSpace();
3848   SDValue N = getValue(Op0);
3849   SDLoc dl = getCurSDLoc();
3850   auto &TLI = DAG.getTargetLoweringInfo();
3851 
3852   // Normalize Vector GEP - all scalar operands should be converted to the
3853   // splat vector.
3854   bool IsVectorGEP = I.getType()->isVectorTy();
3855   ElementCount VectorElementCount =
3856       IsVectorGEP ? cast<VectorType>(I.getType())->getElementCount()
3857                   : ElementCount::getFixed(0);
3858 
3859   if (IsVectorGEP && !N.getValueType().isVector()) {
3860     LLVMContext &Context = *DAG.getContext();
3861     EVT VT = EVT::getVectorVT(Context, N.getValueType(), VectorElementCount);
3862     if (VectorElementCount.isScalable())
3863       N = DAG.getSplatVector(VT, dl, N);
3864     else
3865       N = DAG.getSplatBuildVector(VT, dl, N);
3866   }
3867 
3868   for (gep_type_iterator GTI = gep_type_begin(&I), E = gep_type_end(&I);
3869        GTI != E; ++GTI) {
3870     const Value *Idx = GTI.getOperand();
3871     if (StructType *StTy = GTI.getStructTypeOrNull()) {
3872       unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue();
3873       if (Field) {
3874         // N = N + Offset
3875         uint64_t Offset =
3876             DAG.getDataLayout().getStructLayout(StTy)->getElementOffset(Field);
3877 
3878         // In an inbounds GEP with an offset that is nonnegative even when
3879         // interpreted as signed, assume there is no unsigned overflow.
3880         SDNodeFlags Flags;
3881         if (int64_t(Offset) >= 0 && cast<GEPOperator>(I).isInBounds())
3882           Flags.setNoUnsignedWrap(true);
3883 
3884         N = DAG.getNode(ISD::ADD, dl, N.getValueType(), N,
3885                         DAG.getConstant(Offset, dl, N.getValueType()), Flags);
3886       }
3887     } else {
3888       // IdxSize is the width of the arithmetic according to IR semantics.
3889       // In SelectionDAG, we may prefer to do arithmetic in a wider bitwidth
3890       // (and fix up the result later).
3891       unsigned IdxSize = DAG.getDataLayout().getIndexSizeInBits(AS);
3892       MVT IdxTy = MVT::getIntegerVT(IdxSize);
3893       TypeSize ElementSize =
3894           DAG.getDataLayout().getTypeAllocSize(GTI.getIndexedType());
3895       // We intentionally mask away the high bits here; ElementSize may not
3896       // fit in IdxTy.
3897       APInt ElementMul(IdxSize, ElementSize.getKnownMinSize());
3898       bool ElementScalable = ElementSize.isScalable();
3899 
3900       // If this is a scalar constant or a splat vector of constants,
3901       // handle it quickly.
3902       const auto *C = dyn_cast<Constant>(Idx);
3903       if (C && isa<VectorType>(C->getType()))
3904         C = C->getSplatValue();
3905 
3906       const auto *CI = dyn_cast_or_null<ConstantInt>(C);
3907       if (CI && CI->isZero())
3908         continue;
3909       if (CI && !ElementScalable) {
3910         APInt Offs = ElementMul * CI->getValue().sextOrTrunc(IdxSize);
3911         LLVMContext &Context = *DAG.getContext();
3912         SDValue OffsVal;
3913         if (IsVectorGEP)
3914           OffsVal = DAG.getConstant(
3915               Offs, dl, EVT::getVectorVT(Context, IdxTy, VectorElementCount));
3916         else
3917           OffsVal = DAG.getConstant(Offs, dl, IdxTy);
3918 
3919         // In an inbounds GEP with an offset that is nonnegative even when
3920         // interpreted as signed, assume there is no unsigned overflow.
3921         SDNodeFlags Flags;
3922         if (Offs.isNonNegative() && cast<GEPOperator>(I).isInBounds())
3923           Flags.setNoUnsignedWrap(true);
3924 
3925         OffsVal = DAG.getSExtOrTrunc(OffsVal, dl, N.getValueType());
3926 
3927         N = DAG.getNode(ISD::ADD, dl, N.getValueType(), N, OffsVal, Flags);
3928         continue;
3929       }
3930 
3931       // N = N + Idx * ElementMul;
3932       SDValue IdxN = getValue(Idx);
3933 
3934       if (!IdxN.getValueType().isVector() && IsVectorGEP) {
3935         EVT VT = EVT::getVectorVT(*Context, IdxN.getValueType(),
3936                                   VectorElementCount);
3937         if (VectorElementCount.isScalable())
3938           IdxN = DAG.getSplatVector(VT, dl, IdxN);
3939         else
3940           IdxN = DAG.getSplatBuildVector(VT, dl, IdxN);
3941       }
3942 
3943       // If the index is smaller or larger than intptr_t, truncate or extend
3944       // it.
3945       IdxN = DAG.getSExtOrTrunc(IdxN, dl, N.getValueType());
3946 
3947       if (ElementScalable) {
3948         EVT VScaleTy = N.getValueType().getScalarType();
3949         SDValue VScale = DAG.getNode(
3950             ISD::VSCALE, dl, VScaleTy,
3951             DAG.getConstant(ElementMul.getZExtValue(), dl, VScaleTy));
3952         if (IsVectorGEP)
3953           VScale = DAG.getSplatVector(N.getValueType(), dl, VScale);
3954         IdxN = DAG.getNode(ISD::MUL, dl, N.getValueType(), IdxN, VScale);
3955       } else {
3956         // If this is a multiply by a power of two, turn it into a shl
3957         // immediately.  This is a very common case.
3958         if (ElementMul != 1) {
3959           if (ElementMul.isPowerOf2()) {
3960             unsigned Amt = ElementMul.logBase2();
3961             IdxN = DAG.getNode(ISD::SHL, dl,
3962                                N.getValueType(), IdxN,
3963                                DAG.getConstant(Amt, dl, IdxN.getValueType()));
3964           } else {
3965             SDValue Scale = DAG.getConstant(ElementMul.getZExtValue(), dl,
3966                                             IdxN.getValueType());
3967             IdxN = DAG.getNode(ISD::MUL, dl,
3968                                N.getValueType(), IdxN, Scale);
3969           }
3970         }
3971       }
3972 
3973       N = DAG.getNode(ISD::ADD, dl,
3974                       N.getValueType(), N, IdxN);
3975     }
3976   }
3977 
3978   MVT PtrTy = TLI.getPointerTy(DAG.getDataLayout(), AS);
3979   MVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout(), AS);
3980   if (IsVectorGEP) {
3981     PtrTy = MVT::getVectorVT(PtrTy, VectorElementCount);
3982     PtrMemTy = MVT::getVectorVT(PtrMemTy, VectorElementCount);
3983   }
3984 
3985   if (PtrMemTy != PtrTy && !cast<GEPOperator>(I).isInBounds())
3986     N = DAG.getPtrExtendInReg(N, dl, PtrMemTy);
3987 
3988   setValue(&I, N);
3989 }
3990 
3991 void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
3992   // If this is a fixed sized alloca in the entry block of the function,
3993   // allocate it statically on the stack.
3994   if (FuncInfo.StaticAllocaMap.count(&I))
3995     return;   // getValue will auto-populate this.
3996 
3997   SDLoc dl = getCurSDLoc();
3998   Type *Ty = I.getAllocatedType();
3999   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4000   auto &DL = DAG.getDataLayout();
4001   TypeSize TySize = DL.getTypeAllocSize(Ty);
4002   MaybeAlign Alignment = std::max(DL.getPrefTypeAlign(Ty), I.getAlign());
4003 
4004   SDValue AllocSize = getValue(I.getArraySize());
4005 
4006   EVT IntPtr = TLI.getPointerTy(DAG.getDataLayout(), DL.getAllocaAddrSpace());
4007   if (AllocSize.getValueType() != IntPtr)
4008     AllocSize = DAG.getZExtOrTrunc(AllocSize, dl, IntPtr);
4009 
4010   if (TySize.isScalable())
4011     AllocSize = DAG.getNode(ISD::MUL, dl, IntPtr, AllocSize,
4012                             DAG.getVScale(dl, IntPtr,
4013                                           APInt(IntPtr.getScalarSizeInBits(),
4014                                                 TySize.getKnownMinValue())));
4015   else
4016     AllocSize =
4017         DAG.getNode(ISD::MUL, dl, IntPtr, AllocSize,
4018                     DAG.getConstant(TySize.getFixedValue(), dl, IntPtr));
4019 
4020   // Handle alignment.  If the requested alignment is less than or equal to
4021   // the stack alignment, ignore it.  If the size is greater than or equal to
4022   // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
4023   Align StackAlign = DAG.getSubtarget().getFrameLowering()->getStackAlign();
4024   if (*Alignment <= StackAlign)
4025     Alignment = None;
4026 
4027   const uint64_t StackAlignMask = StackAlign.value() - 1U;
4028   // Round the size of the allocation up to the stack alignment size
4029   // by add SA-1 to the size. This doesn't overflow because we're computing
4030   // an address inside an alloca.
4031   SDNodeFlags Flags;
4032   Flags.setNoUnsignedWrap(true);
4033   AllocSize = DAG.getNode(ISD::ADD, dl, AllocSize.getValueType(), AllocSize,
4034                           DAG.getConstant(StackAlignMask, dl, IntPtr), Flags);
4035 
4036   // Mask out the low bits for alignment purposes.
4037   AllocSize = DAG.getNode(ISD::AND, dl, AllocSize.getValueType(), AllocSize,
4038                           DAG.getConstant(~StackAlignMask, dl, IntPtr));
4039 
4040   SDValue Ops[] = {
4041       getRoot(), AllocSize,
4042       DAG.getConstant(Alignment ? Alignment->value() : 0, dl, IntPtr)};
4043   SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
4044   SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, dl, VTs, Ops);
4045   setValue(&I, DSA);
4046   DAG.setRoot(DSA.getValue(1));
4047 
4048   assert(FuncInfo.MF->getFrameInfo().hasVarSizedObjects());
4049 }
4050 
4051 void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
4052   if (I.isAtomic())
4053     return visitAtomicLoad(I);
4054 
4055   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4056   const Value *SV = I.getOperand(0);
4057   if (TLI.supportSwiftError()) {
4058     // Swifterror values can come from either a function parameter with
4059     // swifterror attribute or an alloca with swifterror attribute.
4060     if (const Argument *Arg = dyn_cast<Argument>(SV)) {
4061       if (Arg->hasSwiftErrorAttr())
4062         return visitLoadFromSwiftError(I);
4063     }
4064 
4065     if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(SV)) {
4066       if (Alloca->isSwiftError())
4067         return visitLoadFromSwiftError(I);
4068     }
4069   }
4070 
4071   SDValue Ptr = getValue(SV);
4072 
4073   Type *Ty = I.getType();
4074   Align Alignment = I.getAlign();
4075 
4076   AAMDNodes AAInfo = I.getAAMetadata();
4077   const MDNode *Ranges = I.getMetadata(LLVMContext::MD_range);
4078 
4079   SmallVector<EVT, 4> ValueVTs, MemVTs;
4080   SmallVector<uint64_t, 4> Offsets;
4081   ComputeValueVTs(TLI, DAG.getDataLayout(), Ty, ValueVTs, &MemVTs, &Offsets);
4082   unsigned NumValues = ValueVTs.size();
4083   if (NumValues == 0)
4084     return;
4085 
4086   bool isVolatile = I.isVolatile();
4087 
4088   SDValue Root;
4089   bool ConstantMemory = false;
4090   if (isVolatile)
4091     // Serialize volatile loads with other side effects.
4092     Root = getRoot();
4093   else if (NumValues > MaxParallelChains)
4094     Root = getMemoryRoot();
4095   else if (AA &&
4096            AA->pointsToConstantMemory(MemoryLocation(
4097                SV,
4098                LocationSize::precise(DAG.getDataLayout().getTypeStoreSize(Ty)),
4099                AAInfo))) {
4100     // Do not serialize (non-volatile) loads of constant memory with anything.
4101     Root = DAG.getEntryNode();
4102     ConstantMemory = true;
4103   } else {
4104     // Do not serialize non-volatile loads against each other.
4105     Root = DAG.getRoot();
4106   }
4107 
4108   SDLoc dl = getCurSDLoc();
4109 
4110   if (isVolatile)
4111     Root = TLI.prepareVolatileOrAtomicLoad(Root, dl, DAG);
4112 
4113   // An aggregate load cannot wrap around the address space, so offsets to its
4114   // parts don't wrap either.
4115   SDNodeFlags Flags;
4116   Flags.setNoUnsignedWrap(true);
4117 
4118   SmallVector<SDValue, 4> Values(NumValues);
4119   SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues));
4120   EVT PtrVT = Ptr.getValueType();
4121 
4122   MachineMemOperand::Flags MMOFlags
4123     = TLI.getLoadMemOperandFlags(I, DAG.getDataLayout());
4124 
4125   unsigned ChainI = 0;
4126   for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
4127     // Serializing loads here may result in excessive register pressure, and
4128     // TokenFactor places arbitrary choke points on the scheduler. SD scheduling
4129     // could recover a bit by hoisting nodes upward in the chain by recognizing
4130     // they are side-effect free or do not alias. The optimizer should really
4131     // avoid this case by converting large object/array copies to llvm.memcpy
4132     // (MaxParallelChains should always remain as failsafe).
4133     if (ChainI == MaxParallelChains) {
4134       assert(PendingLoads.empty() && "PendingLoads must be serialized first");
4135       SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4136                                   makeArrayRef(Chains.data(), ChainI));
4137       Root = Chain;
4138       ChainI = 0;
4139     }
4140     SDValue A = DAG.getNode(ISD::ADD, dl,
4141                             PtrVT, Ptr,
4142                             DAG.getConstant(Offsets[i], dl, PtrVT),
4143                             Flags);
4144 
4145     SDValue L = DAG.getLoad(MemVTs[i], dl, Root, A,
4146                             MachinePointerInfo(SV, Offsets[i]), Alignment,
4147                             MMOFlags, AAInfo, Ranges);
4148     Chains[ChainI] = L.getValue(1);
4149 
4150     if (MemVTs[i] != ValueVTs[i])
4151       L = DAG.getZExtOrTrunc(L, dl, ValueVTs[i]);
4152 
4153     Values[i] = L;
4154   }
4155 
4156   if (!ConstantMemory) {
4157     SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4158                                 makeArrayRef(Chains.data(), ChainI));
4159     if (isVolatile)
4160       DAG.setRoot(Chain);
4161     else
4162       PendingLoads.push_back(Chain);
4163   }
4164 
4165   setValue(&I, DAG.getNode(ISD::MERGE_VALUES, dl,
4166                            DAG.getVTList(ValueVTs), Values));
4167 }
4168 
4169 void SelectionDAGBuilder::visitStoreToSwiftError(const StoreInst &I) {
4170   assert(DAG.getTargetLoweringInfo().supportSwiftError() &&
4171          "call visitStoreToSwiftError when backend supports swifterror");
4172 
4173   SmallVector<EVT, 4> ValueVTs;
4174   SmallVector<uint64_t, 4> Offsets;
4175   const Value *SrcV = I.getOperand(0);
4176   ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
4177                   SrcV->getType(), ValueVTs, &Offsets);
4178   assert(ValueVTs.size() == 1 && Offsets[0] == 0 &&
4179          "expect a single EVT for swifterror");
4180 
4181   SDValue Src = getValue(SrcV);
4182   // Create a virtual register, then update the virtual register.
4183   Register VReg =
4184       SwiftError.getOrCreateVRegDefAt(&I, FuncInfo.MBB, I.getPointerOperand());
4185   // Chain, DL, Reg, N or Chain, DL, Reg, N, Glue
4186   // Chain can be getRoot or getControlRoot.
4187   SDValue CopyNode = DAG.getCopyToReg(getRoot(), getCurSDLoc(), VReg,
4188                                       SDValue(Src.getNode(), Src.getResNo()));
4189   DAG.setRoot(CopyNode);
4190 }
4191 
4192 void SelectionDAGBuilder::visitLoadFromSwiftError(const LoadInst &I) {
4193   assert(DAG.getTargetLoweringInfo().supportSwiftError() &&
4194          "call visitLoadFromSwiftError when backend supports swifterror");
4195 
4196   assert(!I.isVolatile() &&
4197          !I.hasMetadata(LLVMContext::MD_nontemporal) &&
4198          !I.hasMetadata(LLVMContext::MD_invariant_load) &&
4199          "Support volatile, non temporal, invariant for load_from_swift_error");
4200 
4201   const Value *SV = I.getOperand(0);
4202   Type *Ty = I.getType();
4203   assert(
4204       (!AA ||
4205        !AA->pointsToConstantMemory(MemoryLocation(
4206            SV, LocationSize::precise(DAG.getDataLayout().getTypeStoreSize(Ty)),
4207            I.getAAMetadata()))) &&
4208       "load_from_swift_error should not be constant memory");
4209 
4210   SmallVector<EVT, 4> ValueVTs;
4211   SmallVector<uint64_t, 4> Offsets;
4212   ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), Ty,
4213                   ValueVTs, &Offsets);
4214   assert(ValueVTs.size() == 1 && Offsets[0] == 0 &&
4215          "expect a single EVT for swifterror");
4216 
4217   // Chain, DL, Reg, VT, Glue or Chain, DL, Reg, VT
4218   SDValue L = DAG.getCopyFromReg(
4219       getRoot(), getCurSDLoc(),
4220       SwiftError.getOrCreateVRegUseAt(&I, FuncInfo.MBB, SV), ValueVTs[0]);
4221 
4222   setValue(&I, L);
4223 }
4224 
4225 void SelectionDAGBuilder::visitStore(const StoreInst &I) {
4226   if (I.isAtomic())
4227     return visitAtomicStore(I);
4228 
4229   const Value *SrcV = I.getOperand(0);
4230   const Value *PtrV = I.getOperand(1);
4231 
4232   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4233   if (TLI.supportSwiftError()) {
4234     // Swifterror values can come from either a function parameter with
4235     // swifterror attribute or an alloca with swifterror attribute.
4236     if (const Argument *Arg = dyn_cast<Argument>(PtrV)) {
4237       if (Arg->hasSwiftErrorAttr())
4238         return visitStoreToSwiftError(I);
4239     }
4240 
4241     if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrV)) {
4242       if (Alloca->isSwiftError())
4243         return visitStoreToSwiftError(I);
4244     }
4245   }
4246 
4247   SmallVector<EVT, 4> ValueVTs, MemVTs;
4248   SmallVector<uint64_t, 4> Offsets;
4249   ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
4250                   SrcV->getType(), ValueVTs, &MemVTs, &Offsets);
4251   unsigned NumValues = ValueVTs.size();
4252   if (NumValues == 0)
4253     return;
4254 
4255   // Get the lowered operands. Note that we do this after
4256   // checking if NumResults is zero, because with zero results
4257   // the operands won't have values in the map.
4258   SDValue Src = getValue(SrcV);
4259   SDValue Ptr = getValue(PtrV);
4260 
4261   SDValue Root = I.isVolatile() ? getRoot() : getMemoryRoot();
4262   SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues));
4263   SDLoc dl = getCurSDLoc();
4264   Align Alignment = I.getAlign();
4265   AAMDNodes AAInfo = I.getAAMetadata();
4266 
4267   auto MMOFlags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout());
4268 
4269   // An aggregate load cannot wrap around the address space, so offsets to its
4270   // parts don't wrap either.
4271   SDNodeFlags Flags;
4272   Flags.setNoUnsignedWrap(true);
4273 
4274   unsigned ChainI = 0;
4275   for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
4276     // See visitLoad comments.
4277     if (ChainI == MaxParallelChains) {
4278       SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4279                                   makeArrayRef(Chains.data(), ChainI));
4280       Root = Chain;
4281       ChainI = 0;
4282     }
4283     SDValue Add =
4284         DAG.getMemBasePlusOffset(Ptr, TypeSize::Fixed(Offsets[i]), dl, Flags);
4285     SDValue Val = SDValue(Src.getNode(), Src.getResNo() + i);
4286     if (MemVTs[i] != ValueVTs[i])
4287       Val = DAG.getPtrExtOrTrunc(Val, dl, MemVTs[i]);
4288     SDValue St =
4289         DAG.getStore(Root, dl, Val, Add, MachinePointerInfo(PtrV, Offsets[i]),
4290                      Alignment, MMOFlags, AAInfo);
4291     Chains[ChainI] = St;
4292   }
4293 
4294   SDValue StoreNode = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4295                                   makeArrayRef(Chains.data(), ChainI));
4296   DAG.setRoot(StoreNode);
4297 }
4298 
4299 void SelectionDAGBuilder::visitMaskedStore(const CallInst &I,
4300                                            bool IsCompressing) {
4301   SDLoc sdl = getCurSDLoc();
4302 
4303   auto getMaskedStoreOps = [&](Value *&Ptr, Value *&Mask, Value *&Src0,
4304                                MaybeAlign &Alignment) {
4305     // llvm.masked.store.*(Src0, Ptr, alignment, Mask)
4306     Src0 = I.getArgOperand(0);
4307     Ptr = I.getArgOperand(1);
4308     Alignment = cast<ConstantInt>(I.getArgOperand(2))->getMaybeAlignValue();
4309     Mask = I.getArgOperand(3);
4310   };
4311   auto getCompressingStoreOps = [&](Value *&Ptr, Value *&Mask, Value *&Src0,
4312                                     MaybeAlign &Alignment) {
4313     // llvm.masked.compressstore.*(Src0, Ptr, Mask)
4314     Src0 = I.getArgOperand(0);
4315     Ptr = I.getArgOperand(1);
4316     Mask = I.getArgOperand(2);
4317     Alignment = None;
4318   };
4319 
4320   Value  *PtrOperand, *MaskOperand, *Src0Operand;
4321   MaybeAlign Alignment;
4322   if (IsCompressing)
4323     getCompressingStoreOps(PtrOperand, MaskOperand, Src0Operand, Alignment);
4324   else
4325     getMaskedStoreOps(PtrOperand, MaskOperand, Src0Operand, Alignment);
4326 
4327   SDValue Ptr = getValue(PtrOperand);
4328   SDValue Src0 = getValue(Src0Operand);
4329   SDValue Mask = getValue(MaskOperand);
4330   SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
4331 
4332   EVT VT = Src0.getValueType();
4333   if (!Alignment)
4334     Alignment = DAG.getEVTAlign(VT);
4335 
4336   MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
4337       MachinePointerInfo(PtrOperand), MachineMemOperand::MOStore,
4338       MemoryLocation::UnknownSize, *Alignment, I.getAAMetadata());
4339   SDValue StoreNode =
4340       DAG.getMaskedStore(getMemoryRoot(), sdl, Src0, Ptr, Offset, Mask, VT, MMO,
4341                          ISD::UNINDEXED, false /* Truncating */, IsCompressing);
4342   DAG.setRoot(StoreNode);
4343   setValue(&I, StoreNode);
4344 }
4345 
4346 // Get a uniform base for the Gather/Scatter intrinsic.
4347 // The first argument of the Gather/Scatter intrinsic is a vector of pointers.
4348 // We try to represent it as a base pointer + vector of indices.
4349 // Usually, the vector of pointers comes from a 'getelementptr' instruction.
4350 // The first operand of the GEP may be a single pointer or a vector of pointers
4351 // Example:
4352 //   %gep.ptr = getelementptr i32, <8 x i32*> %vptr, <8 x i32> %ind
4353 //  or
4354 //   %gep.ptr = getelementptr i32, i32* %ptr,        <8 x i32> %ind
4355 // %res = call <8 x i32> @llvm.masked.gather.v8i32(<8 x i32*> %gep.ptr, ..
4356 //
4357 // When the first GEP operand is a single pointer - it is the uniform base we
4358 // are looking for. If first operand of the GEP is a splat vector - we
4359 // extract the splat value and use it as a uniform base.
4360 // In all other cases the function returns 'false'.
4361 static bool getUniformBase(const Value *Ptr, SDValue &Base, SDValue &Index,
4362                            ISD::MemIndexType &IndexType, SDValue &Scale,
4363                            SelectionDAGBuilder *SDB, const BasicBlock *CurBB) {
4364   SelectionDAG& DAG = SDB->DAG;
4365   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4366   const DataLayout &DL = DAG.getDataLayout();
4367 
4368   assert(Ptr->getType()->isVectorTy() && "Unexpected pointer type");
4369 
4370   // Handle splat constant pointer.
4371   if (auto *C = dyn_cast<Constant>(Ptr)) {
4372     C = C->getSplatValue();
4373     if (!C)
4374       return false;
4375 
4376     Base = SDB->getValue(C);
4377 
4378     ElementCount NumElts = cast<VectorType>(Ptr->getType())->getElementCount();
4379     EVT VT = EVT::getVectorVT(*DAG.getContext(), TLI.getPointerTy(DL), NumElts);
4380     Index = DAG.getConstant(0, SDB->getCurSDLoc(), VT);
4381     IndexType = ISD::SIGNED_SCALED;
4382     Scale = DAG.getTargetConstant(1, SDB->getCurSDLoc(), TLI.getPointerTy(DL));
4383     return true;
4384   }
4385 
4386   const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr);
4387   if (!GEP || GEP->getParent() != CurBB)
4388     return false;
4389 
4390   if (GEP->getNumOperands() != 2)
4391     return false;
4392 
4393   const Value *BasePtr = GEP->getPointerOperand();
4394   const Value *IndexVal = GEP->getOperand(GEP->getNumOperands() - 1);
4395 
4396   // Make sure the base is scalar and the index is a vector.
4397   if (BasePtr->getType()->isVectorTy() || !IndexVal->getType()->isVectorTy())
4398     return false;
4399 
4400   Base = SDB->getValue(BasePtr);
4401   Index = SDB->getValue(IndexVal);
4402   IndexType = ISD::SIGNED_SCALED;
4403 
4404   // MGATHER/MSCATTER only support scaling by a power-of-two.
4405   uint64_t ScaleVal = DL.getTypeAllocSize(GEP->getResultElementType());
4406   if (!isPowerOf2_64(ScaleVal))
4407     return false;
4408 
4409   Scale =
4410       DAG.getTargetConstant(ScaleVal, SDB->getCurSDLoc(), TLI.getPointerTy(DL));
4411   return true;
4412 }
4413 
4414 void SelectionDAGBuilder::visitMaskedScatter(const CallInst &I) {
4415   SDLoc sdl = getCurSDLoc();
4416 
4417   // llvm.masked.scatter.*(Src0, Ptrs, alignment, Mask)
4418   const Value *Ptr = I.getArgOperand(1);
4419   SDValue Src0 = getValue(I.getArgOperand(0));
4420   SDValue Mask = getValue(I.getArgOperand(3));
4421   EVT VT = Src0.getValueType();
4422   Align Alignment = cast<ConstantInt>(I.getArgOperand(2))
4423                         ->getMaybeAlignValue()
4424                         .getValueOr(DAG.getEVTAlign(VT.getScalarType()));
4425   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4426 
4427   SDValue Base;
4428   SDValue Index;
4429   ISD::MemIndexType IndexType;
4430   SDValue Scale;
4431   bool UniformBase = getUniformBase(Ptr, Base, Index, IndexType, Scale, this,
4432                                     I.getParent());
4433 
4434   unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
4435   MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
4436       MachinePointerInfo(AS), MachineMemOperand::MOStore,
4437       // TODO: Make MachineMemOperands aware of scalable
4438       // vectors.
4439       MemoryLocation::UnknownSize, Alignment, I.getAAMetadata());
4440   if (!UniformBase) {
4441     Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
4442     Index = getValue(Ptr);
4443     IndexType = ISD::SIGNED_UNSCALED;
4444     Scale = DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
4445   }
4446 
4447   EVT IdxVT = Index.getValueType();
4448   EVT EltTy = IdxVT.getVectorElementType();
4449   if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
4450     EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
4451     Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
4452   }
4453 
4454   SDValue Ops[] = { getMemoryRoot(), Src0, Mask, Base, Index, Scale };
4455   SDValue Scatter = DAG.getMaskedScatter(DAG.getVTList(MVT::Other), VT, sdl,
4456                                          Ops, MMO, IndexType, false);
4457   DAG.setRoot(Scatter);
4458   setValue(&I, Scatter);
4459 }
4460 
4461 void SelectionDAGBuilder::visitMaskedLoad(const CallInst &I, bool IsExpanding) {
4462   SDLoc sdl = getCurSDLoc();
4463 
4464   auto getMaskedLoadOps = [&](Value *&Ptr, Value *&Mask, Value *&Src0,
4465                               MaybeAlign &Alignment) {
4466     // @llvm.masked.load.*(Ptr, alignment, Mask, Src0)
4467     Ptr = I.getArgOperand(0);
4468     Alignment = cast<ConstantInt>(I.getArgOperand(1))->getMaybeAlignValue();
4469     Mask = I.getArgOperand(2);
4470     Src0 = I.getArgOperand(3);
4471   };
4472   auto getExpandingLoadOps = [&](Value *&Ptr, Value *&Mask, Value *&Src0,
4473                                  MaybeAlign &Alignment) {
4474     // @llvm.masked.expandload.*(Ptr, Mask, Src0)
4475     Ptr = I.getArgOperand(0);
4476     Alignment = None;
4477     Mask = I.getArgOperand(1);
4478     Src0 = I.getArgOperand(2);
4479   };
4480 
4481   Value  *PtrOperand, *MaskOperand, *Src0Operand;
4482   MaybeAlign Alignment;
4483   if (IsExpanding)
4484     getExpandingLoadOps(PtrOperand, MaskOperand, Src0Operand, Alignment);
4485   else
4486     getMaskedLoadOps(PtrOperand, MaskOperand, Src0Operand, Alignment);
4487 
4488   SDValue Ptr = getValue(PtrOperand);
4489   SDValue Src0 = getValue(Src0Operand);
4490   SDValue Mask = getValue(MaskOperand);
4491   SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
4492 
4493   EVT VT = Src0.getValueType();
4494   if (!Alignment)
4495     Alignment = DAG.getEVTAlign(VT);
4496 
4497   AAMDNodes AAInfo = I.getAAMetadata();
4498   const MDNode *Ranges = I.getMetadata(LLVMContext::MD_range);
4499 
4500   // Do not serialize masked loads of constant memory with anything.
4501   MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
4502   bool AddToChain = !AA || !AA->pointsToConstantMemory(ML);
4503 
4504   SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
4505 
4506   MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
4507       MachinePointerInfo(PtrOperand), MachineMemOperand::MOLoad,
4508       MemoryLocation::UnknownSize, *Alignment, AAInfo, Ranges);
4509 
4510   SDValue Load =
4511       DAG.getMaskedLoad(VT, sdl, InChain, Ptr, Offset, Mask, Src0, VT, MMO,
4512                         ISD::UNINDEXED, ISD::NON_EXTLOAD, IsExpanding);
4513   if (AddToChain)
4514     PendingLoads.push_back(Load.getValue(1));
4515   setValue(&I, Load);
4516 }
4517 
4518 void SelectionDAGBuilder::visitMaskedGather(const CallInst &I) {
4519   SDLoc sdl = getCurSDLoc();
4520 
4521   // @llvm.masked.gather.*(Ptrs, alignment, Mask, Src0)
4522   const Value *Ptr = I.getArgOperand(0);
4523   SDValue Src0 = getValue(I.getArgOperand(3));
4524   SDValue Mask = getValue(I.getArgOperand(2));
4525 
4526   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4527   EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4528   Align Alignment = cast<ConstantInt>(I.getArgOperand(1))
4529                         ->getMaybeAlignValue()
4530                         .getValueOr(DAG.getEVTAlign(VT.getScalarType()));
4531 
4532   const MDNode *Ranges = I.getMetadata(LLVMContext::MD_range);
4533 
4534   SDValue Root = DAG.getRoot();
4535   SDValue Base;
4536   SDValue Index;
4537   ISD::MemIndexType IndexType;
4538   SDValue Scale;
4539   bool UniformBase = getUniformBase(Ptr, Base, Index, IndexType, Scale, this,
4540                                     I.getParent());
4541   unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
4542   MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
4543       MachinePointerInfo(AS), MachineMemOperand::MOLoad,
4544       // TODO: Make MachineMemOperands aware of scalable
4545       // vectors.
4546       MemoryLocation::UnknownSize, Alignment, I.getAAMetadata(), Ranges);
4547 
4548   if (!UniformBase) {
4549     Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
4550     Index = getValue(Ptr);
4551     IndexType = ISD::SIGNED_UNSCALED;
4552     Scale = DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
4553   }
4554 
4555   EVT IdxVT = Index.getValueType();
4556   EVT EltTy = IdxVT.getVectorElementType();
4557   if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
4558     EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
4559     Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
4560   }
4561 
4562   SDValue Ops[] = { Root, Src0, Mask, Base, Index, Scale };
4563   SDValue Gather = DAG.getMaskedGather(DAG.getVTList(VT, MVT::Other), VT, sdl,
4564                                        Ops, MMO, IndexType, ISD::NON_EXTLOAD);
4565 
4566   PendingLoads.push_back(Gather.getValue(1));
4567   setValue(&I, Gather);
4568 }
4569 
4570 void SelectionDAGBuilder::visitAtomicCmpXchg(const AtomicCmpXchgInst &I) {
4571   SDLoc dl = getCurSDLoc();
4572   AtomicOrdering SuccessOrdering = I.getSuccessOrdering();
4573   AtomicOrdering FailureOrdering = I.getFailureOrdering();
4574   SyncScope::ID SSID = I.getSyncScopeID();
4575 
4576   SDValue InChain = getRoot();
4577 
4578   MVT MemVT = getValue(I.getCompareOperand()).getSimpleValueType();
4579   SDVTList VTs = DAG.getVTList(MemVT, MVT::i1, MVT::Other);
4580 
4581   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4582   auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout());
4583 
4584   MachineFunction &MF = DAG.getMachineFunction();
4585   MachineMemOperand *MMO = MF.getMachineMemOperand(
4586       MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
4587       DAG.getEVTAlign(MemVT), AAMDNodes(), nullptr, SSID, SuccessOrdering,
4588       FailureOrdering);
4589 
4590   SDValue L = DAG.getAtomicCmpSwap(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS,
4591                                    dl, MemVT, VTs, InChain,
4592                                    getValue(I.getPointerOperand()),
4593                                    getValue(I.getCompareOperand()),
4594                                    getValue(I.getNewValOperand()), MMO);
4595 
4596   SDValue OutChain = L.getValue(2);
4597 
4598   setValue(&I, L);
4599   DAG.setRoot(OutChain);
4600 }
4601 
4602 void SelectionDAGBuilder::visitAtomicRMW(const AtomicRMWInst &I) {
4603   SDLoc dl = getCurSDLoc();
4604   ISD::NodeType NT;
4605   switch (I.getOperation()) {
4606   default: llvm_unreachable("Unknown atomicrmw operation");
4607   case AtomicRMWInst::Xchg: NT = ISD::ATOMIC_SWAP; break;
4608   case AtomicRMWInst::Add:  NT = ISD::ATOMIC_LOAD_ADD; break;
4609   case AtomicRMWInst::Sub:  NT = ISD::ATOMIC_LOAD_SUB; break;
4610   case AtomicRMWInst::And:  NT = ISD::ATOMIC_LOAD_AND; break;
4611   case AtomicRMWInst::Nand: NT = ISD::ATOMIC_LOAD_NAND; break;
4612   case AtomicRMWInst::Or:   NT = ISD::ATOMIC_LOAD_OR; break;
4613   case AtomicRMWInst::Xor:  NT = ISD::ATOMIC_LOAD_XOR; break;
4614   case AtomicRMWInst::Max:  NT = ISD::ATOMIC_LOAD_MAX; break;
4615   case AtomicRMWInst::Min:  NT = ISD::ATOMIC_LOAD_MIN; break;
4616   case AtomicRMWInst::UMax: NT = ISD::ATOMIC_LOAD_UMAX; break;
4617   case AtomicRMWInst::UMin: NT = ISD::ATOMIC_LOAD_UMIN; break;
4618   case AtomicRMWInst::FAdd: NT = ISD::ATOMIC_LOAD_FADD; break;
4619   case AtomicRMWInst::FSub: NT = ISD::ATOMIC_LOAD_FSUB; break;
4620   }
4621   AtomicOrdering Ordering = I.getOrdering();
4622   SyncScope::ID SSID = I.getSyncScopeID();
4623 
4624   SDValue InChain = getRoot();
4625 
4626   auto MemVT = getValue(I.getValOperand()).getSimpleValueType();
4627   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4628   auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout());
4629 
4630   MachineFunction &MF = DAG.getMachineFunction();
4631   MachineMemOperand *MMO = MF.getMachineMemOperand(
4632       MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
4633       DAG.getEVTAlign(MemVT), AAMDNodes(), nullptr, SSID, Ordering);
4634 
4635   SDValue L =
4636     DAG.getAtomic(NT, dl, MemVT, InChain,
4637                   getValue(I.getPointerOperand()), getValue(I.getValOperand()),
4638                   MMO);
4639 
4640   SDValue OutChain = L.getValue(1);
4641 
4642   setValue(&I, L);
4643   DAG.setRoot(OutChain);
4644 }
4645 
4646 void SelectionDAGBuilder::visitFence(const FenceInst &I) {
4647   SDLoc dl = getCurSDLoc();
4648   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4649   SDValue Ops[3];
4650   Ops[0] = getRoot();
4651   Ops[1] = DAG.getTargetConstant((unsigned)I.getOrdering(), dl,
4652                                  TLI.getFenceOperandTy(DAG.getDataLayout()));
4653   Ops[2] = DAG.getTargetConstant(I.getSyncScopeID(), dl,
4654                                  TLI.getFenceOperandTy(DAG.getDataLayout()));
4655   DAG.setRoot(DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops));
4656 }
4657 
4658 void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &I) {
4659   SDLoc dl = getCurSDLoc();
4660   AtomicOrdering Order = I.getOrdering();
4661   SyncScope::ID SSID = I.getSyncScopeID();
4662 
4663   SDValue InChain = getRoot();
4664 
4665   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4666   EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4667   EVT MemVT = TLI.getMemValueType(DAG.getDataLayout(), I.getType());
4668 
4669   if (!TLI.supportsUnalignedAtomics() &&
4670       I.getAlignment() < MemVT.getSizeInBits() / 8)
4671     report_fatal_error("Cannot generate unaligned atomic load");
4672 
4673   auto Flags = TLI.getLoadMemOperandFlags(I, DAG.getDataLayout());
4674 
4675   MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
4676       MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
4677       I.getAlign(), AAMDNodes(), nullptr, SSID, Order);
4678 
4679   InChain = TLI.prepareVolatileOrAtomicLoad(InChain, dl, DAG);
4680 
4681   SDValue Ptr = getValue(I.getPointerOperand());
4682 
4683   if (TLI.lowerAtomicLoadAsLoadSDNode(I)) {
4684     // TODO: Once this is better exercised by tests, it should be merged with
4685     // the normal path for loads to prevent future divergence.
4686     SDValue L = DAG.getLoad(MemVT, dl, InChain, Ptr, MMO);
4687     if (MemVT != VT)
4688       L = DAG.getPtrExtOrTrunc(L, dl, VT);
4689 
4690     setValue(&I, L);
4691     SDValue OutChain = L.getValue(1);
4692     if (!I.isUnordered())
4693       DAG.setRoot(OutChain);
4694     else
4695       PendingLoads.push_back(OutChain);
4696     return;
4697   }
4698 
4699   SDValue L = DAG.getAtomic(ISD::ATOMIC_LOAD, dl, MemVT, MemVT, InChain,
4700                             Ptr, MMO);
4701 
4702   SDValue OutChain = L.getValue(1);
4703   if (MemVT != VT)
4704     L = DAG.getPtrExtOrTrunc(L, dl, VT);
4705 
4706   setValue(&I, L);
4707   DAG.setRoot(OutChain);
4708 }
4709 
4710 void SelectionDAGBuilder::visitAtomicStore(const StoreInst &I) {
4711   SDLoc dl = getCurSDLoc();
4712 
4713   AtomicOrdering Ordering = I.getOrdering();
4714   SyncScope::ID SSID = I.getSyncScopeID();
4715 
4716   SDValue InChain = getRoot();
4717 
4718   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4719   EVT MemVT =
4720       TLI.getMemValueType(DAG.getDataLayout(), I.getValueOperand()->getType());
4721 
4722   if (I.getAlignment() < MemVT.getSizeInBits() / 8)
4723     report_fatal_error("Cannot generate unaligned atomic store");
4724 
4725   auto Flags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout());
4726 
4727   MachineFunction &MF = DAG.getMachineFunction();
4728   MachineMemOperand *MMO = MF.getMachineMemOperand(
4729       MachinePointerInfo(I.getPointerOperand()), Flags, MemVT.getStoreSize(),
4730       I.getAlign(), AAMDNodes(), nullptr, SSID, Ordering);
4731 
4732   SDValue Val = getValue(I.getValueOperand());
4733   if (Val.getValueType() != MemVT)
4734     Val = DAG.getPtrExtOrTrunc(Val, dl, MemVT);
4735   SDValue Ptr = getValue(I.getPointerOperand());
4736 
4737   if (TLI.lowerAtomicStoreAsStoreSDNode(I)) {
4738     // TODO: Once this is better exercised by tests, it should be merged with
4739     // the normal path for stores to prevent future divergence.
4740     SDValue S = DAG.getStore(InChain, dl, Val, Ptr, MMO);
4741     DAG.setRoot(S);
4742     return;
4743   }
4744   SDValue OutChain = DAG.getAtomic(ISD::ATOMIC_STORE, dl, MemVT, InChain,
4745                                    Ptr, Val, MMO);
4746 
4747 
4748   DAG.setRoot(OutChain);
4749 }
4750 
4751 /// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
4752 /// node.
4753 void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
4754                                                unsigned Intrinsic) {
4755   // Ignore the callsite's attributes. A specific call site may be marked with
4756   // readnone, but the lowering code will expect the chain based on the
4757   // definition.
4758   const Function *F = I.getCalledFunction();
4759   bool HasChain = !F->doesNotAccessMemory();
4760   bool OnlyLoad = HasChain && F->onlyReadsMemory();
4761 
4762   // Build the operand list.
4763   SmallVector<SDValue, 8> Ops;
4764   if (HasChain) {  // If this intrinsic has side-effects, chainify it.
4765     if (OnlyLoad) {
4766       // We don't need to serialize loads against other loads.
4767       Ops.push_back(DAG.getRoot());
4768     } else {
4769       Ops.push_back(getRoot());
4770     }
4771   }
4772 
4773   // Info is set by getTgtMemInstrinsic
4774   TargetLowering::IntrinsicInfo Info;
4775   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4776   bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I,
4777                                                DAG.getMachineFunction(),
4778                                                Intrinsic);
4779 
4780   // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
4781   if (!IsTgtIntrinsic || Info.opc == ISD::INTRINSIC_VOID ||
4782       Info.opc == ISD::INTRINSIC_W_CHAIN)
4783     Ops.push_back(DAG.getTargetConstant(Intrinsic, getCurSDLoc(),
4784                                         TLI.getPointerTy(DAG.getDataLayout())));
4785 
4786   // Add all operands of the call to the operand list.
4787   for (unsigned i = 0, e = I.arg_size(); i != e; ++i) {
4788     const Value *Arg = I.getArgOperand(i);
4789     if (!I.paramHasAttr(i, Attribute::ImmArg)) {
4790       Ops.push_back(getValue(Arg));
4791       continue;
4792     }
4793 
4794     // Use TargetConstant instead of a regular constant for immarg.
4795     EVT VT = TLI.getValueType(DAG.getDataLayout(), Arg->getType(), true);
4796     if (const ConstantInt *CI = dyn_cast<ConstantInt>(Arg)) {
4797       assert(CI->getBitWidth() <= 64 &&
4798              "large intrinsic immediates not handled");
4799       Ops.push_back(DAG.getTargetConstant(*CI, SDLoc(), VT));
4800     } else {
4801       Ops.push_back(
4802           DAG.getTargetConstantFP(*cast<ConstantFP>(Arg), SDLoc(), VT));
4803     }
4804   }
4805 
4806   SmallVector<EVT, 4> ValueVTs;
4807   ComputeValueVTs(TLI, DAG.getDataLayout(), I.getType(), ValueVTs);
4808 
4809   if (HasChain)
4810     ValueVTs.push_back(MVT::Other);
4811 
4812   SDVTList VTs = DAG.getVTList(ValueVTs);
4813 
4814   // Propagate fast-math-flags from IR to node(s).
4815   SDNodeFlags Flags;
4816   if (auto *FPMO = dyn_cast<FPMathOperator>(&I))
4817     Flags.copyFMF(*FPMO);
4818   SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
4819 
4820   // Create the node.
4821   SDValue Result;
4822   if (IsTgtIntrinsic) {
4823     // This is target intrinsic that touches memory
4824     Result =
4825         DAG.getMemIntrinsicNode(Info.opc, getCurSDLoc(), VTs, Ops, Info.memVT,
4826                                 MachinePointerInfo(Info.ptrVal, Info.offset),
4827                                 Info.align, Info.flags, Info.size,
4828                                 I.getAAMetadata());
4829   } else if (!HasChain) {
4830     Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurSDLoc(), VTs, Ops);
4831   } else if (!I.getType()->isVoidTy()) {
4832     Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurSDLoc(), VTs, Ops);
4833   } else {
4834     Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops);
4835   }
4836 
4837   if (HasChain) {
4838     SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
4839     if (OnlyLoad)
4840       PendingLoads.push_back(Chain);
4841     else
4842       DAG.setRoot(Chain);
4843   }
4844 
4845   if (!I.getType()->isVoidTy()) {
4846     if (!isa<VectorType>(I.getType()))
4847       Result = lowerRangeToAssertZExt(DAG, I, Result);
4848 
4849     MaybeAlign Alignment = I.getRetAlign();
4850     if (!Alignment)
4851       Alignment = F->getAttributes().getRetAlignment();
4852     // Insert `assertalign` node if there's an alignment.
4853     if (InsertAssertAlign && Alignment) {
4854       Result =
4855           DAG.getAssertAlign(getCurSDLoc(), Result, Alignment.valueOrOne());
4856     }
4857 
4858     setValue(&I, Result);
4859   }
4860 }
4861 
4862 /// GetSignificand - Get the significand and build it into a floating-point
4863 /// number with exponent of 1:
4864 ///
4865 ///   Op = (Op & 0x007fffff) | 0x3f800000;
4866 ///
4867 /// where Op is the hexadecimal representation of floating point value.
4868 static SDValue GetSignificand(SelectionDAG &DAG, SDValue Op, const SDLoc &dl) {
4869   SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
4870                            DAG.getConstant(0x007fffff, dl, MVT::i32));
4871   SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
4872                            DAG.getConstant(0x3f800000, dl, MVT::i32));
4873   return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2);
4874 }
4875 
4876 /// GetExponent - Get the exponent:
4877 ///
4878 ///   (float)(int)(((Op & 0x7f800000) >> 23) - 127);
4879 ///
4880 /// where Op is the hexadecimal representation of floating point value.
4881 static SDValue GetExponent(SelectionDAG &DAG, SDValue Op,
4882                            const TargetLowering &TLI, const SDLoc &dl) {
4883   SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
4884                            DAG.getConstant(0x7f800000, dl, MVT::i32));
4885   SDValue t1 = DAG.getNode(
4886       ISD::SRL, dl, MVT::i32, t0,
4887       DAG.getConstant(23, dl,
4888                       TLI.getShiftAmountTy(MVT::i32, DAG.getDataLayout())));
4889   SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
4890                            DAG.getConstant(127, dl, MVT::i32));
4891   return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
4892 }
4893 
4894 /// getF32Constant - Get 32-bit floating point constant.
4895 static SDValue getF32Constant(SelectionDAG &DAG, unsigned Flt,
4896                               const SDLoc &dl) {
4897   return DAG.getConstantFP(APFloat(APFloat::IEEEsingle(), APInt(32, Flt)), dl,
4898                            MVT::f32);
4899 }
4900 
4901 static SDValue getLimitedPrecisionExp2(SDValue t0, const SDLoc &dl,
4902                                        SelectionDAG &DAG) {
4903   // TODO: What fast-math-flags should be set on the floating-point nodes?
4904 
4905   //   IntegerPartOfX = ((int32_t)(t0);
4906   SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
4907 
4908   //   FractionalPartOfX = t0 - (float)IntegerPartOfX;
4909   SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
4910   SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
4911 
4912   //   IntegerPartOfX <<= 23;
4913   IntegerPartOfX =
4914       DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
4915                   DAG.getConstant(23, dl,
4916                                   DAG.getTargetLoweringInfo().getShiftAmountTy(
4917                                       MVT::i32, DAG.getDataLayout())));
4918 
4919   SDValue TwoToFractionalPartOfX;
4920   if (LimitFloatPrecision <= 6) {
4921     // For floating-point precision of 6:
4922     //
4923     //   TwoToFractionalPartOfX =
4924     //     0.997535578f +
4925     //       (0.735607626f + 0.252464424f * x) * x;
4926     //
4927     // error 0.0144103317, which is 6 bits
4928     SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
4929                              getF32Constant(DAG, 0x3e814304, dl));
4930     SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
4931                              getF32Constant(DAG, 0x3f3c50c8, dl));
4932     SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4933     TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
4934                                          getF32Constant(DAG, 0x3f7f5e7e, dl));
4935   } else if (LimitFloatPrecision <= 12) {
4936     // For floating-point precision of 12:
4937     //
4938     //   TwoToFractionalPartOfX =
4939     //     0.999892986f +
4940     //       (0.696457318f +
4941     //         (0.224338339f + 0.792043434e-1f * x) * x) * x;
4942     //
4943     // error 0.000107046256, which is 13 to 14 bits
4944     SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
4945                              getF32Constant(DAG, 0x3da235e3, dl));
4946     SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
4947                              getF32Constant(DAG, 0x3e65b8f3, dl));
4948     SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4949     SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
4950                              getF32Constant(DAG, 0x3f324b07, dl));
4951     SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4952     TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
4953                                          getF32Constant(DAG, 0x3f7ff8fd, dl));
4954   } else { // LimitFloatPrecision <= 18
4955     // For floating-point precision of 18:
4956     //
4957     //   TwoToFractionalPartOfX =
4958     //     0.999999982f +
4959     //       (0.693148872f +
4960     //         (0.240227044f +
4961     //           (0.554906021e-1f +
4962     //             (0.961591928e-2f +
4963     //               (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4964     // error 2.47208000*10^(-7), which is better than 18 bits
4965     SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
4966                              getF32Constant(DAG, 0x3924b03e, dl));
4967     SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
4968                              getF32Constant(DAG, 0x3ab24b87, dl));
4969     SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4970     SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
4971                              getF32Constant(DAG, 0x3c1d8c17, dl));
4972     SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4973     SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
4974                              getF32Constant(DAG, 0x3d634a1d, dl));
4975     SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4976     SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
4977                              getF32Constant(DAG, 0x3e75fe14, dl));
4978     SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4979     SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
4980                               getF32Constant(DAG, 0x3f317234, dl));
4981     SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
4982     TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
4983                                          getF32Constant(DAG, 0x3f800000, dl));
4984   }
4985 
4986   // Add the exponent into the result in integer domain.
4987   SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, TwoToFractionalPartOfX);
4988   return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4989                      DAG.getNode(ISD::ADD, dl, MVT::i32, t13, IntegerPartOfX));
4990 }
4991 
4992 /// expandExp - Lower an exp intrinsic. Handles the special sequences for
4993 /// limited-precision mode.
4994 static SDValue expandExp(const SDLoc &dl, SDValue Op, SelectionDAG &DAG,
4995                          const TargetLowering &TLI, SDNodeFlags Flags) {
4996   if (Op.getValueType() == MVT::f32 &&
4997       LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
4998 
4999     // Put the exponent in the right bit position for later addition to the
5000     // final result:
5001     //
5002     // t0 = Op * log2(e)
5003 
5004     // TODO: What fast-math-flags should be set here?
5005     SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
5006                              DAG.getConstantFP(numbers::log2ef, dl, MVT::f32));
5007     return getLimitedPrecisionExp2(t0, dl, DAG);
5008   }
5009 
5010   // No special expansion.
5011   return DAG.getNode(ISD::FEXP, dl, Op.getValueType(), Op, Flags);
5012 }
5013 
5014 /// expandLog - Lower a log intrinsic. Handles the special sequences for
5015 /// limited-precision mode.
5016 static SDValue expandLog(const SDLoc &dl, SDValue Op, SelectionDAG &DAG,
5017                          const TargetLowering &TLI, SDNodeFlags Flags) {
5018   // TODO: What fast-math-flags should be set on the floating-point nodes?
5019 
5020   if (Op.getValueType() == MVT::f32 &&
5021       LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
5022     SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5023 
5024     // Scale the exponent by log(2).
5025     SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
5026     SDValue LogOfExponent =
5027         DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
5028                     DAG.getConstantFP(numbers::ln2f, dl, MVT::f32));
5029 
5030     // Get the significand and build it into a floating-point number with
5031     // exponent of 1.
5032     SDValue X = GetSignificand(DAG, Op1, dl);
5033 
5034     SDValue LogOfMantissa;
5035     if (LimitFloatPrecision <= 6) {
5036       // For floating-point precision of 6:
5037       //
5038       //   LogofMantissa =
5039       //     -1.1609546f +
5040       //       (1.4034025f - 0.23903021f * x) * x;
5041       //
5042       // error 0.0034276066, which is better than 8 bits
5043       SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5044                                getF32Constant(DAG, 0xbe74c456, dl));
5045       SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5046                                getF32Constant(DAG, 0x3fb3a2b1, dl));
5047       SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5048       LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5049                                   getF32Constant(DAG, 0x3f949a29, dl));
5050     } else if (LimitFloatPrecision <= 12) {
5051       // For floating-point precision of 12:
5052       //
5053       //   LogOfMantissa =
5054       //     -1.7417939f +
5055       //       (2.8212026f +
5056       //         (-1.4699568f +
5057       //           (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
5058       //
5059       // error 0.000061011436, which is 14 bits
5060       SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5061                                getF32Constant(DAG, 0xbd67b6d6, dl));
5062       SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5063                                getF32Constant(DAG, 0x3ee4f4b8, dl));
5064       SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5065       SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5066                                getF32Constant(DAG, 0x3fbc278b, dl));
5067       SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5068       SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5069                                getF32Constant(DAG, 0x40348e95, dl));
5070       SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5071       LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5072                                   getF32Constant(DAG, 0x3fdef31a, dl));
5073     } else { // LimitFloatPrecision <= 18
5074       // For floating-point precision of 18:
5075       //
5076       //   LogOfMantissa =
5077       //     -2.1072184f +
5078       //       (4.2372794f +
5079       //         (-3.7029485f +
5080       //           (2.2781945f +
5081       //             (-0.87823314f +
5082       //               (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
5083       //
5084       // error 0.0000023660568, which is better than 18 bits
5085       SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5086                                getF32Constant(DAG, 0xbc91e5ac, dl));
5087       SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5088                                getF32Constant(DAG, 0x3e4350aa, dl));
5089       SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5090       SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5091                                getF32Constant(DAG, 0x3f60d3e3, dl));
5092       SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5093       SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5094                                getF32Constant(DAG, 0x4011cdf0, dl));
5095       SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5096       SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5097                                getF32Constant(DAG, 0x406cfd1c, dl));
5098       SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5099       SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5100                                getF32Constant(DAG, 0x408797cb, dl));
5101       SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5102       LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
5103                                   getF32Constant(DAG, 0x4006dcab, dl));
5104     }
5105 
5106     return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, LogOfMantissa);
5107   }
5108 
5109   // No special expansion.
5110   return DAG.getNode(ISD::FLOG, dl, Op.getValueType(), Op, Flags);
5111 }
5112 
5113 /// expandLog2 - Lower a log2 intrinsic. Handles the special sequences for
5114 /// limited-precision mode.
5115 static SDValue expandLog2(const SDLoc &dl, SDValue Op, SelectionDAG &DAG,
5116                           const TargetLowering &TLI, SDNodeFlags Flags) {
5117   // TODO: What fast-math-flags should be set on the floating-point nodes?
5118 
5119   if (Op.getValueType() == MVT::f32 &&
5120       LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
5121     SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5122 
5123     // Get the exponent.
5124     SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
5125 
5126     // Get the significand and build it into a floating-point number with
5127     // exponent of 1.
5128     SDValue X = GetSignificand(DAG, Op1, dl);
5129 
5130     // Different possible minimax approximations of significand in
5131     // floating-point for various degrees of accuracy over [1,2].
5132     SDValue Log2ofMantissa;
5133     if (LimitFloatPrecision <= 6) {
5134       // For floating-point precision of 6:
5135       //
5136       //   Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
5137       //
5138       // error 0.0049451742, which is more than 7 bits
5139       SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5140                                getF32Constant(DAG, 0xbeb08fe0, dl));
5141       SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5142                                getF32Constant(DAG, 0x40019463, dl));
5143       SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5144       Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5145                                    getF32Constant(DAG, 0x3fd6633d, dl));
5146     } else if (LimitFloatPrecision <= 12) {
5147       // For floating-point precision of 12:
5148       //
5149       //   Log2ofMantissa =
5150       //     -2.51285454f +
5151       //       (4.07009056f +
5152       //         (-2.12067489f +
5153       //           (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
5154       //
5155       // error 0.0000876136000, which is better than 13 bits
5156       SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5157                                getF32Constant(DAG, 0xbda7262e, dl));
5158       SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5159                                getF32Constant(DAG, 0x3f25280b, dl));
5160       SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5161       SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5162                                getF32Constant(DAG, 0x4007b923, dl));
5163       SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5164       SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5165                                getF32Constant(DAG, 0x40823e2f, dl));
5166       SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5167       Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5168                                    getF32Constant(DAG, 0x4020d29c, dl));
5169     } else { // LimitFloatPrecision <= 18
5170       // For floating-point precision of 18:
5171       //
5172       //   Log2ofMantissa =
5173       //     -3.0400495f +
5174       //       (6.1129976f +
5175       //         (-5.3420409f +
5176       //           (3.2865683f +
5177       //             (-1.2669343f +
5178       //               (0.27515199f -
5179       //                 0.25691327e-1f * x) * x) * x) * x) * x) * x;
5180       //
5181       // error 0.0000018516, which is better than 18 bits
5182       SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5183                                getF32Constant(DAG, 0xbcd2769e, dl));
5184       SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5185                                getF32Constant(DAG, 0x3e8ce0b9, dl));
5186       SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5187       SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5188                                getF32Constant(DAG, 0x3fa22ae7, dl));
5189       SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5190       SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5191                                getF32Constant(DAG, 0x40525723, dl));
5192       SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5193       SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5194                                getF32Constant(DAG, 0x40aaf200, dl));
5195       SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5196       SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5197                                getF32Constant(DAG, 0x40c39dad, dl));
5198       SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5199       Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
5200                                    getF32Constant(DAG, 0x4042902c, dl));
5201     }
5202 
5203     return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log2ofMantissa);
5204   }
5205 
5206   // No special expansion.
5207   return DAG.getNode(ISD::FLOG2, dl, Op.getValueType(), Op, Flags);
5208 }
5209 
5210 /// expandLog10 - Lower a log10 intrinsic. Handles the special sequences for
5211 /// limited-precision mode.
5212 static SDValue expandLog10(const SDLoc &dl, SDValue Op, SelectionDAG &DAG,
5213                            const TargetLowering &TLI, SDNodeFlags Flags) {
5214   // TODO: What fast-math-flags should be set on the floating-point nodes?
5215 
5216   if (Op.getValueType() == MVT::f32 &&
5217       LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
5218     SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5219 
5220     // Scale the exponent by log10(2) [0.30102999f].
5221     SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
5222     SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
5223                                         getF32Constant(DAG, 0x3e9a209a, dl));
5224 
5225     // Get the significand and build it into a floating-point number with
5226     // exponent of 1.
5227     SDValue X = GetSignificand(DAG, Op1, dl);
5228 
5229     SDValue Log10ofMantissa;
5230     if (LimitFloatPrecision <= 6) {
5231       // For floating-point precision of 6:
5232       //
5233       //   Log10ofMantissa =
5234       //     -0.50419619f +
5235       //       (0.60948995f - 0.10380950f * x) * x;
5236       //
5237       // error 0.0014886165, which is 6 bits
5238       SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5239                                getF32Constant(DAG, 0xbdd49a13, dl));
5240       SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5241                                getF32Constant(DAG, 0x3f1c0789, dl));
5242       SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5243       Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5244                                     getF32Constant(DAG, 0x3f011300, dl));
5245     } else if (LimitFloatPrecision <= 12) {
5246       // For floating-point precision of 12:
5247       //
5248       //   Log10ofMantissa =
5249       //     -0.64831180f +
5250       //       (0.91751397f +
5251       //         (-0.31664806f + 0.47637168e-1f * x) * x) * x;
5252       //
5253       // error 0.00019228036, which is better than 12 bits
5254       SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5255                                getF32Constant(DAG, 0x3d431f31, dl));
5256       SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
5257                                getF32Constant(DAG, 0x3ea21fb2, dl));
5258       SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5259       SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5260                                getF32Constant(DAG, 0x3f6ae232, dl));
5261       SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5262       Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
5263                                     getF32Constant(DAG, 0x3f25f7c3, dl));
5264     } else { // LimitFloatPrecision <= 18
5265       // For floating-point precision of 18:
5266       //
5267       //   Log10ofMantissa =
5268       //     -0.84299375f +
5269       //       (1.5327582f +
5270       //         (-1.0688956f +
5271       //           (0.49102474f +
5272       //             (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
5273       //
5274       // error 0.0000037995730, which is better than 18 bits
5275       SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5276                                getF32Constant(DAG, 0x3c5d51ce, dl));
5277       SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
5278                                getF32Constant(DAG, 0x3e00685a, dl));
5279       SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5280       SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5281                                getF32Constant(DAG, 0x3efb6798, dl));
5282       SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5283       SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
5284                                getF32Constant(DAG, 0x3f88d192, dl));
5285       SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5286       SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5287                                getF32Constant(DAG, 0x3fc4316c, dl));
5288       SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5289       Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
5290                                     getF32Constant(DAG, 0x3f57ce70, dl));
5291     }
5292 
5293     return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log10ofMantissa);
5294   }
5295 
5296   // No special expansion.
5297   return DAG.getNode(ISD::FLOG10, dl, Op.getValueType(), Op, Flags);
5298 }
5299 
5300 /// expandExp2 - Lower an exp2 intrinsic. Handles the special sequences for
5301 /// limited-precision mode.
5302 static SDValue expandExp2(const SDLoc &dl, SDValue Op, SelectionDAG &DAG,
5303                           const TargetLowering &TLI, SDNodeFlags Flags) {
5304   if (Op.getValueType() == MVT::f32 &&
5305       LimitFloatPrecision > 0 && LimitFloatPrecision <= 18)
5306     return getLimitedPrecisionExp2(Op, dl, DAG);
5307 
5308   // No special expansion.
5309   return DAG.getNode(ISD::FEXP2, dl, Op.getValueType(), Op, Flags);
5310 }
5311 
5312 /// visitPow - Lower a pow intrinsic. Handles the special sequences for
5313 /// limited-precision mode with x == 10.0f.
5314 static SDValue expandPow(const SDLoc &dl, SDValue LHS, SDValue RHS,
5315                          SelectionDAG &DAG, const TargetLowering &TLI,
5316                          SDNodeFlags Flags) {
5317   bool IsExp10 = false;
5318   if (LHS.getValueType() == MVT::f32 && RHS.getValueType() == MVT::f32 &&
5319       LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
5320     if (ConstantFPSDNode *LHSC = dyn_cast<ConstantFPSDNode>(LHS)) {
5321       APFloat Ten(10.0f);
5322       IsExp10 = LHSC->isExactlyValue(Ten);
5323     }
5324   }
5325 
5326   // TODO: What fast-math-flags should be set on the FMUL node?
5327   if (IsExp10) {
5328     // Put the exponent in the right bit position for later addition to the
5329     // final result:
5330     //
5331     //   #define LOG2OF10 3.3219281f
5332     //   t0 = Op * LOG2OF10;
5333     SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, RHS,
5334                              getF32Constant(DAG, 0x40549a78, dl));
5335     return getLimitedPrecisionExp2(t0, dl, DAG);
5336   }
5337 
5338   // No special expansion.
5339   return DAG.getNode(ISD::FPOW, dl, LHS.getValueType(), LHS, RHS, Flags);
5340 }
5341 
5342 /// ExpandPowI - Expand a llvm.powi intrinsic.
5343 static SDValue ExpandPowI(const SDLoc &DL, SDValue LHS, SDValue RHS,
5344                           SelectionDAG &DAG) {
5345   // If RHS is a constant, we can expand this out to a multiplication tree,
5346   // otherwise we end up lowering to a call to __powidf2 (for example).  When
5347   // optimizing for size, we only want to do this if the expansion would produce
5348   // a small number of multiplies, otherwise we do the full expansion.
5349   if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
5350     // Get the exponent as a positive value.
5351     unsigned Val = RHSC->getSExtValue();
5352     if ((int)Val < 0) Val = -Val;
5353 
5354     // powi(x, 0) -> 1.0
5355     if (Val == 0)
5356       return DAG.getConstantFP(1.0, DL, LHS.getValueType());
5357 
5358     bool OptForSize = DAG.shouldOptForSize();
5359     if (!OptForSize ||
5360         // If optimizing for size, don't insert too many multiplies.
5361         // This inserts up to 5 multiplies.
5362         countPopulation(Val) + Log2_32(Val) < 7) {
5363       // We use the simple binary decomposition method to generate the multiply
5364       // sequence.  There are more optimal ways to do this (for example,
5365       // powi(x,15) generates one more multiply than it should), but this has
5366       // the benefit of being both really simple and much better than a libcall.
5367       SDValue Res;  // Logically starts equal to 1.0
5368       SDValue CurSquare = LHS;
5369       // TODO: Intrinsics should have fast-math-flags that propagate to these
5370       // nodes.
5371       while (Val) {
5372         if (Val & 1) {
5373           if (Res.getNode())
5374             Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
5375           else
5376             Res = CurSquare;  // 1.0*CurSquare.
5377         }
5378 
5379         CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
5380                                 CurSquare, CurSquare);
5381         Val >>= 1;
5382       }
5383 
5384       // If the original was negative, invert the result, producing 1/(x*x*x).
5385       if (RHSC->getSExtValue() < 0)
5386         Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
5387                           DAG.getConstantFP(1.0, DL, LHS.getValueType()), Res);
5388       return Res;
5389     }
5390   }
5391 
5392   // Otherwise, expand to a libcall.
5393   return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
5394 }
5395 
5396 static SDValue expandDivFix(unsigned Opcode, const SDLoc &DL,
5397                             SDValue LHS, SDValue RHS, SDValue Scale,
5398                             SelectionDAG &DAG, const TargetLowering &TLI) {
5399   EVT VT = LHS.getValueType();
5400   bool Signed = Opcode == ISD::SDIVFIX || Opcode == ISD::SDIVFIXSAT;
5401   bool Saturating = Opcode == ISD::SDIVFIXSAT || Opcode == ISD::UDIVFIXSAT;
5402   LLVMContext &Ctx = *DAG.getContext();
5403 
5404   // If the type is legal but the operation isn't, this node might survive all
5405   // the way to operation legalization. If we end up there and we do not have
5406   // the ability to widen the type (if VT*2 is not legal), we cannot expand the
5407   // node.
5408 
5409   // Coax the legalizer into expanding the node during type legalization instead
5410   // by bumping the size by one bit. This will force it to Promote, enabling the
5411   // early expansion and avoiding the need to expand later.
5412 
5413   // We don't have to do this if Scale is 0; that can always be expanded, unless
5414   // it's a saturating signed operation. Those can experience true integer
5415   // division overflow, a case which we must avoid.
5416 
5417   // FIXME: We wouldn't have to do this (or any of the early
5418   // expansion/promotion) if it was possible to expand a libcall of an
5419   // illegal type during operation legalization. But it's not, so things
5420   // get a bit hacky.
5421   unsigned ScaleInt = cast<ConstantSDNode>(Scale)->getZExtValue();
5422   if ((ScaleInt > 0 || (Saturating && Signed)) &&
5423       (TLI.isTypeLegal(VT) ||
5424        (VT.isVector() && TLI.isTypeLegal(VT.getVectorElementType())))) {
5425     TargetLowering::LegalizeAction Action = TLI.getFixedPointOperationAction(
5426         Opcode, VT, ScaleInt);
5427     if (Action != TargetLowering::Legal && Action != TargetLowering::Custom) {
5428       EVT PromVT;
5429       if (VT.isScalarInteger())
5430         PromVT = EVT::getIntegerVT(Ctx, VT.getSizeInBits() + 1);
5431       else if (VT.isVector()) {
5432         PromVT = VT.getVectorElementType();
5433         PromVT = EVT::getIntegerVT(Ctx, PromVT.getSizeInBits() + 1);
5434         PromVT = EVT::getVectorVT(Ctx, PromVT, VT.getVectorElementCount());
5435       } else
5436         llvm_unreachable("Wrong VT for DIVFIX?");
5437       if (Signed) {
5438         LHS = DAG.getSExtOrTrunc(LHS, DL, PromVT);
5439         RHS = DAG.getSExtOrTrunc(RHS, DL, PromVT);
5440       } else {
5441         LHS = DAG.getZExtOrTrunc(LHS, DL, PromVT);
5442         RHS = DAG.getZExtOrTrunc(RHS, DL, PromVT);
5443       }
5444       EVT ShiftTy = TLI.getShiftAmountTy(PromVT, DAG.getDataLayout());
5445       // For saturating operations, we need to shift up the LHS to get the
5446       // proper saturation width, and then shift down again afterwards.
5447       if (Saturating)
5448         LHS = DAG.getNode(ISD::SHL, DL, PromVT, LHS,
5449                           DAG.getConstant(1, DL, ShiftTy));
5450       SDValue Res = DAG.getNode(Opcode, DL, PromVT, LHS, RHS, Scale);
5451       if (Saturating)
5452         Res = DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, PromVT, Res,
5453                           DAG.getConstant(1, DL, ShiftTy));
5454       return DAG.getZExtOrTrunc(Res, DL, VT);
5455     }
5456   }
5457 
5458   return DAG.getNode(Opcode, DL, VT, LHS, RHS, Scale);
5459 }
5460 
5461 // getUnderlyingArgRegs - Find underlying registers used for a truncated,
5462 // bitcasted, or split argument. Returns a list of <Register, size in bits>
5463 static void
5464 getUnderlyingArgRegs(SmallVectorImpl<std::pair<unsigned, TypeSize>> &Regs,
5465                      const SDValue &N) {
5466   switch (N.getOpcode()) {
5467   case ISD::CopyFromReg: {
5468     SDValue Op = N.getOperand(1);
5469     Regs.emplace_back(cast<RegisterSDNode>(Op)->getReg(),
5470                       Op.getValueType().getSizeInBits());
5471     return;
5472   }
5473   case ISD::BITCAST:
5474   case ISD::AssertZext:
5475   case ISD::AssertSext:
5476   case ISD::TRUNCATE:
5477     getUnderlyingArgRegs(Regs, N.getOperand(0));
5478     return;
5479   case ISD::BUILD_PAIR:
5480   case ISD::BUILD_VECTOR:
5481   case ISD::CONCAT_VECTORS:
5482     for (SDValue Op : N->op_values())
5483       getUnderlyingArgRegs(Regs, Op);
5484     return;
5485   default:
5486     return;
5487   }
5488 }
5489 
5490 /// If the DbgValueInst is a dbg_value of a function argument, create the
5491 /// corresponding DBG_VALUE machine instruction for it now.  At the end of
5492 /// instruction selection, they will be inserted to the entry BB.
5493 /// We don't currently support this for variadic dbg_values, as they shouldn't
5494 /// appear for function arguments or in the prologue.
5495 bool SelectionDAGBuilder::EmitFuncArgumentDbgValue(
5496     const Value *V, DILocalVariable *Variable, DIExpression *Expr,
5497     DILocation *DL, FuncArgumentDbgValueKind Kind, const SDValue &N) {
5498   const Argument *Arg = dyn_cast<Argument>(V);
5499   if (!Arg)
5500     return false;
5501 
5502   MachineFunction &MF = DAG.getMachineFunction();
5503   const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo();
5504 
5505   // Helper to create DBG_INSTR_REFs or DBG_VALUEs, depending on what kind
5506   // we've been asked to pursue.
5507   auto MakeVRegDbgValue = [&](Register Reg, DIExpression *FragExpr,
5508                               bool Indirect) {
5509     if (Reg.isVirtual() && MF.useDebugInstrRef()) {
5510       // For VRegs, in instruction referencing mode, create a DBG_INSTR_REF
5511       // pointing at the VReg, which will be patched up later.
5512       auto &Inst = TII->get(TargetOpcode::DBG_INSTR_REF);
5513       auto MIB = BuildMI(MF, DL, Inst);
5514       MIB.addReg(Reg);
5515       MIB.addImm(0);
5516       MIB.addMetadata(Variable);
5517       auto *NewDIExpr = FragExpr;
5518       // We don't have an "Indirect" field in DBG_INSTR_REF, fold that into
5519       // the DIExpression.
5520       if (Indirect)
5521         NewDIExpr = DIExpression::prepend(FragExpr, DIExpression::DerefBefore);
5522       MIB.addMetadata(NewDIExpr);
5523       return MIB;
5524     } else {
5525       // Create a completely standard DBG_VALUE.
5526       auto &Inst = TII->get(TargetOpcode::DBG_VALUE);
5527       return BuildMI(MF, DL, Inst, Indirect, Reg, Variable, FragExpr);
5528     }
5529   };
5530 
5531   if (Kind == FuncArgumentDbgValueKind::Value) {
5532     // ArgDbgValues are hoisted to the beginning of the entry block. So we
5533     // should only emit as ArgDbgValue if the dbg.value intrinsic is found in
5534     // the entry block.
5535     bool IsInEntryBlock = FuncInfo.MBB == &FuncInfo.MF->front();
5536     if (!IsInEntryBlock)
5537       return false;
5538 
5539     // ArgDbgValues are hoisted to the beginning of the entry block.  So we
5540     // should only emit as ArgDbgValue if the dbg.value intrinsic describes a
5541     // variable that also is a param.
5542     //
5543     // Although, if we are at the top of the entry block already, we can still
5544     // emit using ArgDbgValue. This might catch some situations when the
5545     // dbg.value refers to an argument that isn't used in the entry block, so
5546     // any CopyToReg node would be optimized out and the only way to express
5547     // this DBG_VALUE is by using the physical reg (or FI) as done in this
5548     // method.  ArgDbgValues are hoisted to the beginning of the entry block. So
5549     // we should only emit as ArgDbgValue if the Variable is an argument to the
5550     // current function, and the dbg.value intrinsic is found in the entry
5551     // block.
5552     bool VariableIsFunctionInputArg = Variable->isParameter() &&
5553         !DL->getInlinedAt();
5554     bool IsInPrologue = SDNodeOrder == LowestSDNodeOrder;
5555     if (!IsInPrologue && !VariableIsFunctionInputArg)
5556       return false;
5557 
5558     // Here we assume that a function argument on IR level only can be used to
5559     // describe one input parameter on source level. If we for example have
5560     // source code like this
5561     //
5562     //    struct A { long x, y; };
5563     //    void foo(struct A a, long b) {
5564     //      ...
5565     //      b = a.x;
5566     //      ...
5567     //    }
5568     //
5569     // and IR like this
5570     //
5571     //  define void @foo(i32 %a1, i32 %a2, i32 %b)  {
5572     //  entry:
5573     //    call void @llvm.dbg.value(metadata i32 %a1, "a", DW_OP_LLVM_fragment
5574     //    call void @llvm.dbg.value(metadata i32 %a2, "a", DW_OP_LLVM_fragment
5575     //    call void @llvm.dbg.value(metadata i32 %b, "b",
5576     //    ...
5577     //    call void @llvm.dbg.value(metadata i32 %a1, "b"
5578     //    ...
5579     //
5580     // then the last dbg.value is describing a parameter "b" using a value that
5581     // is an argument. But since we already has used %a1 to describe a parameter
5582     // we should not handle that last dbg.value here (that would result in an
5583     // incorrect hoisting of the DBG_VALUE to the function entry).
5584     // Notice that we allow one dbg.value per IR level argument, to accommodate
5585     // for the situation with fragments above.
5586     if (VariableIsFunctionInputArg) {
5587       unsigned ArgNo = Arg->getArgNo();
5588       if (ArgNo >= FuncInfo.DescribedArgs.size())
5589         FuncInfo.DescribedArgs.resize(ArgNo + 1, false);
5590       else if (!IsInPrologue && FuncInfo.DescribedArgs.test(ArgNo))
5591         return false;
5592       FuncInfo.DescribedArgs.set(ArgNo);
5593     }
5594   }
5595 
5596   bool IsIndirect = false;
5597   Optional<MachineOperand> Op;
5598   // Some arguments' frame index is recorded during argument lowering.
5599   int FI = FuncInfo.getArgumentFrameIndex(Arg);
5600   if (FI != std::numeric_limits<int>::max())
5601     Op = MachineOperand::CreateFI(FI);
5602 
5603   SmallVector<std::pair<unsigned, TypeSize>, 8> ArgRegsAndSizes;
5604   if (!Op && N.getNode()) {
5605     getUnderlyingArgRegs(ArgRegsAndSizes, N);
5606     Register Reg;
5607     if (ArgRegsAndSizes.size() == 1)
5608       Reg = ArgRegsAndSizes.front().first;
5609 
5610     if (Reg && Reg.isVirtual()) {
5611       MachineRegisterInfo &RegInfo = MF.getRegInfo();
5612       Register PR = RegInfo.getLiveInPhysReg(Reg);
5613       if (PR)
5614         Reg = PR;
5615     }
5616     if (Reg) {
5617       Op = MachineOperand::CreateReg(Reg, false);
5618       IsIndirect = Kind != FuncArgumentDbgValueKind::Value;
5619     }
5620   }
5621 
5622   if (!Op && N.getNode()) {
5623     // Check if frame index is available.
5624     SDValue LCandidate = peekThroughBitcasts(N);
5625     if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(LCandidate.getNode()))
5626       if (FrameIndexSDNode *FINode =
5627           dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
5628         Op = MachineOperand::CreateFI(FINode->getIndex());
5629   }
5630 
5631   if (!Op) {
5632     // Create a DBG_VALUE for each decomposed value in ArgRegs to cover Reg
5633     auto splitMultiRegDbgValue = [&](ArrayRef<std::pair<unsigned, TypeSize>>
5634                                          SplitRegs) {
5635       unsigned Offset = 0;
5636       for (const auto &RegAndSize : SplitRegs) {
5637         // If the expression is already a fragment, the current register
5638         // offset+size might extend beyond the fragment. In this case, only
5639         // the register bits that are inside the fragment are relevant.
5640         int RegFragmentSizeInBits = RegAndSize.second;
5641         if (auto ExprFragmentInfo = Expr->getFragmentInfo()) {
5642           uint64_t ExprFragmentSizeInBits = ExprFragmentInfo->SizeInBits;
5643           // The register is entirely outside the expression fragment,
5644           // so is irrelevant for debug info.
5645           if (Offset >= ExprFragmentSizeInBits)
5646             break;
5647           // The register is partially outside the expression fragment, only
5648           // the low bits within the fragment are relevant for debug info.
5649           if (Offset + RegFragmentSizeInBits > ExprFragmentSizeInBits) {
5650             RegFragmentSizeInBits = ExprFragmentSizeInBits - Offset;
5651           }
5652         }
5653 
5654         auto FragmentExpr = DIExpression::createFragmentExpression(
5655             Expr, Offset, RegFragmentSizeInBits);
5656         Offset += RegAndSize.second;
5657         // If a valid fragment expression cannot be created, the variable's
5658         // correct value cannot be determined and so it is set as Undef.
5659         if (!FragmentExpr) {
5660           SDDbgValue *SDV = DAG.getConstantDbgValue(
5661               Variable, Expr, UndefValue::get(V->getType()), DL, SDNodeOrder);
5662           DAG.AddDbgValue(SDV, false);
5663           continue;
5664         }
5665         MachineInstr *NewMI =
5666             MakeVRegDbgValue(RegAndSize.first, *FragmentExpr,
5667                              Kind != FuncArgumentDbgValueKind::Value);
5668         FuncInfo.ArgDbgValues.push_back(NewMI);
5669       }
5670     };
5671 
5672     // Check if ValueMap has reg number.
5673     DenseMap<const Value *, Register>::const_iterator
5674       VMI = FuncInfo.ValueMap.find(V);
5675     if (VMI != FuncInfo.ValueMap.end()) {
5676       const auto &TLI = DAG.getTargetLoweringInfo();
5677       RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), VMI->second,
5678                        V->getType(), None);
5679       if (RFV.occupiesMultipleRegs()) {
5680         splitMultiRegDbgValue(RFV.getRegsAndSizes());
5681         return true;
5682       }
5683 
5684       Op = MachineOperand::CreateReg(VMI->second, false);
5685       IsIndirect = Kind != FuncArgumentDbgValueKind::Value;
5686     } else if (ArgRegsAndSizes.size() > 1) {
5687       // This was split due to the calling convention, and no virtual register
5688       // mapping exists for the value.
5689       splitMultiRegDbgValue(ArgRegsAndSizes);
5690       return true;
5691     }
5692   }
5693 
5694   if (!Op)
5695     return false;
5696 
5697   assert(Variable->isValidLocationForIntrinsic(DL) &&
5698          "Expected inlined-at fields to agree");
5699   MachineInstr *NewMI = nullptr;
5700 
5701   if (Op->isReg())
5702     NewMI = MakeVRegDbgValue(Op->getReg(), Expr, IsIndirect);
5703   else
5704     NewMI = BuildMI(MF, DL, TII->get(TargetOpcode::DBG_VALUE), true, *Op,
5705                     Variable, Expr);
5706 
5707   // Otherwise, use ArgDbgValues.
5708   FuncInfo.ArgDbgValues.push_back(NewMI);
5709   return true;
5710 }
5711 
5712 /// Return the appropriate SDDbgValue based on N.
5713 SDDbgValue *SelectionDAGBuilder::getDbgValue(SDValue N,
5714                                              DILocalVariable *Variable,
5715                                              DIExpression *Expr,
5716                                              const DebugLoc &dl,
5717                                              unsigned DbgSDNodeOrder) {
5718   if (auto *FISDN = dyn_cast<FrameIndexSDNode>(N.getNode())) {
5719     // Construct a FrameIndexDbgValue for FrameIndexSDNodes so we can describe
5720     // stack slot locations.
5721     //
5722     // Consider "int x = 0; int *px = &x;". There are two kinds of interesting
5723     // debug values here after optimization:
5724     //
5725     //   dbg.value(i32* %px, !"int *px", !DIExpression()), and
5726     //   dbg.value(i32* %px, !"int x", !DIExpression(DW_OP_deref))
5727     //
5728     // Both describe the direct values of their associated variables.
5729     return DAG.getFrameIndexDbgValue(Variable, Expr, FISDN->getIndex(),
5730                                      /*IsIndirect*/ false, dl, DbgSDNodeOrder);
5731   }
5732   return DAG.getDbgValue(Variable, Expr, N.getNode(), N.getResNo(),
5733                          /*IsIndirect*/ false, dl, DbgSDNodeOrder);
5734 }
5735 
5736 static unsigned FixedPointIntrinsicToOpcode(unsigned Intrinsic) {
5737   switch (Intrinsic) {
5738   case Intrinsic::smul_fix:
5739     return ISD::SMULFIX;
5740   case Intrinsic::umul_fix:
5741     return ISD::UMULFIX;
5742   case Intrinsic::smul_fix_sat:
5743     return ISD::SMULFIXSAT;
5744   case Intrinsic::umul_fix_sat:
5745     return ISD::UMULFIXSAT;
5746   case Intrinsic::sdiv_fix:
5747     return ISD::SDIVFIX;
5748   case Intrinsic::udiv_fix:
5749     return ISD::UDIVFIX;
5750   case Intrinsic::sdiv_fix_sat:
5751     return ISD::SDIVFIXSAT;
5752   case Intrinsic::udiv_fix_sat:
5753     return ISD::UDIVFIXSAT;
5754   default:
5755     llvm_unreachable("Unhandled fixed point intrinsic");
5756   }
5757 }
5758 
5759 void SelectionDAGBuilder::lowerCallToExternalSymbol(const CallInst &I,
5760                                            const char *FunctionName) {
5761   assert(FunctionName && "FunctionName must not be nullptr");
5762   SDValue Callee = DAG.getExternalSymbol(
5763       FunctionName,
5764       DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()));
5765   LowerCallTo(I, Callee, I.isTailCall(), I.isMustTailCall());
5766 }
5767 
5768 /// Given a @llvm.call.preallocated.setup, return the corresponding
5769 /// preallocated call.
5770 static const CallBase *FindPreallocatedCall(const Value *PreallocatedSetup) {
5771   assert(cast<CallBase>(PreallocatedSetup)
5772                  ->getCalledFunction()
5773                  ->getIntrinsicID() == Intrinsic::call_preallocated_setup &&
5774          "expected call_preallocated_setup Value");
5775   for (auto *U : PreallocatedSetup->users()) {
5776     auto *UseCall = cast<CallBase>(U);
5777     const Function *Fn = UseCall->getCalledFunction();
5778     if (!Fn || Fn->getIntrinsicID() != Intrinsic::call_preallocated_arg) {
5779       return UseCall;
5780     }
5781   }
5782   llvm_unreachable("expected corresponding call to preallocated setup/arg");
5783 }
5784 
5785 /// Lower the call to the specified intrinsic function.
5786 void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
5787                                              unsigned Intrinsic) {
5788   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5789   SDLoc sdl = getCurSDLoc();
5790   DebugLoc dl = getCurDebugLoc();
5791   SDValue Res;
5792 
5793   SDNodeFlags Flags;
5794   if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
5795     Flags.copyFMF(*FPOp);
5796 
5797   switch (Intrinsic) {
5798   default:
5799     // By default, turn this into a target intrinsic node.
5800     visitTargetIntrinsic(I, Intrinsic);
5801     return;
5802   case Intrinsic::vscale: {
5803     match(&I, m_VScale(DAG.getDataLayout()));
5804     EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
5805     setValue(&I, DAG.getVScale(sdl, VT, APInt(VT.getSizeInBits(), 1)));
5806     return;
5807   }
5808   case Intrinsic::vastart:  visitVAStart(I); return;
5809   case Intrinsic::vaend:    visitVAEnd(I); return;
5810   case Intrinsic::vacopy:   visitVACopy(I); return;
5811   case Intrinsic::returnaddress:
5812     setValue(&I, DAG.getNode(ISD::RETURNADDR, sdl,
5813                              TLI.getValueType(DAG.getDataLayout(), I.getType()),
5814                              getValue(I.getArgOperand(0))));
5815     return;
5816   case Intrinsic::addressofreturnaddress:
5817     setValue(&I,
5818              DAG.getNode(ISD::ADDROFRETURNADDR, sdl,
5819                          TLI.getValueType(DAG.getDataLayout(), I.getType())));
5820     return;
5821   case Intrinsic::sponentry:
5822     setValue(&I,
5823              DAG.getNode(ISD::SPONENTRY, sdl,
5824                          TLI.getValueType(DAG.getDataLayout(), I.getType())));
5825     return;
5826   case Intrinsic::frameaddress:
5827     setValue(&I, DAG.getNode(ISD::FRAMEADDR, sdl,
5828                              TLI.getFrameIndexTy(DAG.getDataLayout()),
5829                              getValue(I.getArgOperand(0))));
5830     return;
5831   case Intrinsic::read_volatile_register:
5832   case Intrinsic::read_register: {
5833     Value *Reg = I.getArgOperand(0);
5834     SDValue Chain = getRoot();
5835     SDValue RegName =
5836         DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
5837     EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
5838     Res = DAG.getNode(ISD::READ_REGISTER, sdl,
5839       DAG.getVTList(VT, MVT::Other), Chain, RegName);
5840     setValue(&I, Res);
5841     DAG.setRoot(Res.getValue(1));
5842     return;
5843   }
5844   case Intrinsic::write_register: {
5845     Value *Reg = I.getArgOperand(0);
5846     Value *RegValue = I.getArgOperand(1);
5847     SDValue Chain = getRoot();
5848     SDValue RegName =
5849         DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
5850     DAG.setRoot(DAG.getNode(ISD::WRITE_REGISTER, sdl, MVT::Other, Chain,
5851                             RegName, getValue(RegValue)));
5852     return;
5853   }
5854   case Intrinsic::memcpy: {
5855     const auto &MCI = cast<MemCpyInst>(I);
5856     SDValue Op1 = getValue(I.getArgOperand(0));
5857     SDValue Op2 = getValue(I.getArgOperand(1));
5858     SDValue Op3 = getValue(I.getArgOperand(2));
5859     // @llvm.memcpy defines 0 and 1 to both mean no alignment.
5860     Align DstAlign = MCI.getDestAlign().valueOrOne();
5861     Align SrcAlign = MCI.getSourceAlign().valueOrOne();
5862     Align Alignment = commonAlignment(DstAlign, SrcAlign);
5863     bool isVol = MCI.isVolatile();
5864     bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
5865     // FIXME: Support passing different dest/src alignments to the memcpy DAG
5866     // node.
5867     SDValue Root = isVol ? getRoot() : getMemoryRoot();
5868     SDValue MC = DAG.getMemcpy(Root, sdl, Op1, Op2, Op3, Alignment, isVol,
5869                                /* AlwaysInline */ false, isTC,
5870                                MachinePointerInfo(I.getArgOperand(0)),
5871                                MachinePointerInfo(I.getArgOperand(1)),
5872                                I.getAAMetadata());
5873     updateDAGForMaybeTailCall(MC);
5874     return;
5875   }
5876   case Intrinsic::memcpy_inline: {
5877     const auto &MCI = cast<MemCpyInlineInst>(I);
5878     SDValue Dst = getValue(I.getArgOperand(0));
5879     SDValue Src = getValue(I.getArgOperand(1));
5880     SDValue Size = getValue(I.getArgOperand(2));
5881     assert(isa<ConstantSDNode>(Size) && "memcpy_inline needs constant size");
5882     // @llvm.memcpy.inline defines 0 and 1 to both mean no alignment.
5883     Align DstAlign = MCI.getDestAlign().valueOrOne();
5884     Align SrcAlign = MCI.getSourceAlign().valueOrOne();
5885     Align Alignment = commonAlignment(DstAlign, SrcAlign);
5886     bool isVol = MCI.isVolatile();
5887     bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
5888     // FIXME: Support passing different dest/src alignments to the memcpy DAG
5889     // node.
5890     SDValue MC = DAG.getMemcpy(getRoot(), sdl, Dst, Src, Size, Alignment, isVol,
5891                                /* AlwaysInline */ true, isTC,
5892                                MachinePointerInfo(I.getArgOperand(0)),
5893                                MachinePointerInfo(I.getArgOperand(1)),
5894                                I.getAAMetadata());
5895     updateDAGForMaybeTailCall(MC);
5896     return;
5897   }
5898   case Intrinsic::memset: {
5899     const auto &MSI = cast<MemSetInst>(I);
5900     SDValue Op1 = getValue(I.getArgOperand(0));
5901     SDValue Op2 = getValue(I.getArgOperand(1));
5902     SDValue Op3 = getValue(I.getArgOperand(2));
5903     // @llvm.memset defines 0 and 1 to both mean no alignment.
5904     Align Alignment = MSI.getDestAlign().valueOrOne();
5905     bool isVol = MSI.isVolatile();
5906     bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
5907     SDValue Root = isVol ? getRoot() : getMemoryRoot();
5908     SDValue MS = DAG.getMemset(Root, sdl, Op1, Op2, Op3, Alignment, isVol, isTC,
5909                                MachinePointerInfo(I.getArgOperand(0)),
5910                                I.getAAMetadata());
5911     updateDAGForMaybeTailCall(MS);
5912     return;
5913   }
5914   case Intrinsic::memmove: {
5915     const auto &MMI = cast<MemMoveInst>(I);
5916     SDValue Op1 = getValue(I.getArgOperand(0));
5917     SDValue Op2 = getValue(I.getArgOperand(1));
5918     SDValue Op3 = getValue(I.getArgOperand(2));
5919     // @llvm.memmove defines 0 and 1 to both mean no alignment.
5920     Align DstAlign = MMI.getDestAlign().valueOrOne();
5921     Align SrcAlign = MMI.getSourceAlign().valueOrOne();
5922     Align Alignment = commonAlignment(DstAlign, SrcAlign);
5923     bool isVol = MMI.isVolatile();
5924     bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
5925     // FIXME: Support passing different dest/src alignments to the memmove DAG
5926     // node.
5927     SDValue Root = isVol ? getRoot() : getMemoryRoot();
5928     SDValue MM = DAG.getMemmove(Root, sdl, Op1, Op2, Op3, Alignment, isVol,
5929                                 isTC, MachinePointerInfo(I.getArgOperand(0)),
5930                                 MachinePointerInfo(I.getArgOperand(1)),
5931                                 I.getAAMetadata());
5932     updateDAGForMaybeTailCall(MM);
5933     return;
5934   }
5935   case Intrinsic::memcpy_element_unordered_atomic: {
5936     const AtomicMemCpyInst &MI = cast<AtomicMemCpyInst>(I);
5937     SDValue Dst = getValue(MI.getRawDest());
5938     SDValue Src = getValue(MI.getRawSource());
5939     SDValue Length = getValue(MI.getLength());
5940 
5941     unsigned DstAlign = MI.getDestAlignment();
5942     unsigned SrcAlign = MI.getSourceAlignment();
5943     Type *LengthTy = MI.getLength()->getType();
5944     unsigned ElemSz = MI.getElementSizeInBytes();
5945     bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
5946     SDValue MC = DAG.getAtomicMemcpy(getRoot(), sdl, Dst, DstAlign, Src,
5947                                      SrcAlign, Length, LengthTy, ElemSz, isTC,
5948                                      MachinePointerInfo(MI.getRawDest()),
5949                                      MachinePointerInfo(MI.getRawSource()));
5950     updateDAGForMaybeTailCall(MC);
5951     return;
5952   }
5953   case Intrinsic::memmove_element_unordered_atomic: {
5954     auto &MI = cast<AtomicMemMoveInst>(I);
5955     SDValue Dst = getValue(MI.getRawDest());
5956     SDValue Src = getValue(MI.getRawSource());
5957     SDValue Length = getValue(MI.getLength());
5958 
5959     unsigned DstAlign = MI.getDestAlignment();
5960     unsigned SrcAlign = MI.getSourceAlignment();
5961     Type *LengthTy = MI.getLength()->getType();
5962     unsigned ElemSz = MI.getElementSizeInBytes();
5963     bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
5964     SDValue MC = DAG.getAtomicMemmove(getRoot(), sdl, Dst, DstAlign, Src,
5965                                       SrcAlign, Length, LengthTy, ElemSz, isTC,
5966                                       MachinePointerInfo(MI.getRawDest()),
5967                                       MachinePointerInfo(MI.getRawSource()));
5968     updateDAGForMaybeTailCall(MC);
5969     return;
5970   }
5971   case Intrinsic::memset_element_unordered_atomic: {
5972     auto &MI = cast<AtomicMemSetInst>(I);
5973     SDValue Dst = getValue(MI.getRawDest());
5974     SDValue Val = getValue(MI.getValue());
5975     SDValue Length = getValue(MI.getLength());
5976 
5977     unsigned DstAlign = MI.getDestAlignment();
5978     Type *LengthTy = MI.getLength()->getType();
5979     unsigned ElemSz = MI.getElementSizeInBytes();
5980     bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
5981     SDValue MC = DAG.getAtomicMemset(getRoot(), sdl, Dst, DstAlign, Val, Length,
5982                                      LengthTy, ElemSz, isTC,
5983                                      MachinePointerInfo(MI.getRawDest()));
5984     updateDAGForMaybeTailCall(MC);
5985     return;
5986   }
5987   case Intrinsic::call_preallocated_setup: {
5988     const CallBase *PreallocatedCall = FindPreallocatedCall(&I);
5989     SDValue SrcValue = DAG.getSrcValue(PreallocatedCall);
5990     SDValue Res = DAG.getNode(ISD::PREALLOCATED_SETUP, sdl, MVT::Other,
5991                               getRoot(), SrcValue);
5992     setValue(&I, Res);
5993     DAG.setRoot(Res);
5994     return;
5995   }
5996   case Intrinsic::call_preallocated_arg: {
5997     const CallBase *PreallocatedCall = FindPreallocatedCall(I.getOperand(0));
5998     SDValue SrcValue = DAG.getSrcValue(PreallocatedCall);
5999     SDValue Ops[3];
6000     Ops[0] = getRoot();
6001     Ops[1] = SrcValue;
6002     Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl,
6003                                    MVT::i32); // arg index
6004     SDValue Res = DAG.getNode(
6005         ISD::PREALLOCATED_ARG, sdl,
6006         DAG.getVTList(TLI.getPointerTy(DAG.getDataLayout()), MVT::Other), Ops);
6007     setValue(&I, Res);
6008     DAG.setRoot(Res.getValue(1));
6009     return;
6010   }
6011   case Intrinsic::dbg_addr:
6012   case Intrinsic::dbg_declare: {
6013     // Assume dbg.addr and dbg.declare can not currently use DIArgList, i.e.
6014     // they are non-variadic.
6015     const auto &DI = cast<DbgVariableIntrinsic>(I);
6016     assert(!DI.hasArgList() && "Only dbg.value should currently use DIArgList");
6017     DILocalVariable *Variable = DI.getVariable();
6018     DIExpression *Expression = DI.getExpression();
6019     dropDanglingDebugInfo(Variable, Expression);
6020     assert(Variable && "Missing variable");
6021     LLVM_DEBUG(dbgs() << "SelectionDAG visiting debug intrinsic: " << DI
6022                       << "\n");
6023     // Check if address has undef value.
6024     const Value *Address = DI.getVariableLocationOp(0);
6025     if (!Address || isa<UndefValue>(Address) ||
6026         (Address->use_empty() && !isa<Argument>(Address))) {
6027       LLVM_DEBUG(dbgs() << "Dropping debug info for " << DI
6028                         << " (bad/undef/unused-arg address)\n");
6029       return;
6030     }
6031 
6032     bool isParameter = Variable->isParameter() || isa<Argument>(Address);
6033 
6034     // Check if this variable can be described by a frame index, typically
6035     // either as a static alloca or a byval parameter.
6036     int FI = std::numeric_limits<int>::max();
6037     if (const auto *AI =
6038             dyn_cast<AllocaInst>(Address->stripInBoundsConstantOffsets())) {
6039       if (AI->isStaticAlloca()) {
6040         auto I = FuncInfo.StaticAllocaMap.find(AI);
6041         if (I != FuncInfo.StaticAllocaMap.end())
6042           FI = I->second;
6043       }
6044     } else if (const auto *Arg = dyn_cast<Argument>(
6045                    Address->stripInBoundsConstantOffsets())) {
6046       FI = FuncInfo.getArgumentFrameIndex(Arg);
6047     }
6048 
6049     // llvm.dbg.addr is control dependent and always generates indirect
6050     // DBG_VALUE instructions. llvm.dbg.declare is handled as a frame index in
6051     // the MachineFunction variable table.
6052     if (FI != std::numeric_limits<int>::max()) {
6053       if (Intrinsic == Intrinsic::dbg_addr) {
6054         SDDbgValue *SDV = DAG.getFrameIndexDbgValue(
6055             Variable, Expression, FI, getRoot().getNode(), /*IsIndirect*/ true,
6056             dl, SDNodeOrder);
6057         DAG.AddDbgValue(SDV, isParameter);
6058       } else {
6059         LLVM_DEBUG(dbgs() << "Skipping " << DI
6060                           << " (variable info stashed in MF side table)\n");
6061       }
6062       return;
6063     }
6064 
6065     SDValue &N = NodeMap[Address];
6066     if (!N.getNode() && isa<Argument>(Address))
6067       // Check unused arguments map.
6068       N = UnusedArgNodeMap[Address];
6069     SDDbgValue *SDV;
6070     if (N.getNode()) {
6071       if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
6072         Address = BCI->getOperand(0);
6073       // Parameters are handled specially.
6074       auto FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
6075       if (isParameter && FINode) {
6076         // Byval parameter. We have a frame index at this point.
6077         SDV =
6078             DAG.getFrameIndexDbgValue(Variable, Expression, FINode->getIndex(),
6079                                       /*IsIndirect*/ true, dl, SDNodeOrder);
6080       } else if (isa<Argument>(Address)) {
6081         // Address is an argument, so try to emit its dbg value using
6082         // virtual register info from the FuncInfo.ValueMap.
6083         EmitFuncArgumentDbgValue(Address, Variable, Expression, dl,
6084                                  FuncArgumentDbgValueKind::Declare, N);
6085         return;
6086       } else {
6087         SDV = DAG.getDbgValue(Variable, Expression, N.getNode(), N.getResNo(),
6088                               true, dl, SDNodeOrder);
6089       }
6090       DAG.AddDbgValue(SDV, isParameter);
6091     } else {
6092       // If Address is an argument then try to emit its dbg value using
6093       // virtual register info from the FuncInfo.ValueMap.
6094       if (!EmitFuncArgumentDbgValue(Address, Variable, Expression, dl,
6095                                     FuncArgumentDbgValueKind::Declare, N)) {
6096         LLVM_DEBUG(dbgs() << "Dropping debug info for " << DI
6097                           << " (could not emit func-arg dbg_value)\n");
6098       }
6099     }
6100     return;
6101   }
6102   case Intrinsic::dbg_label: {
6103     const DbgLabelInst &DI = cast<DbgLabelInst>(I);
6104     DILabel *Label = DI.getLabel();
6105     assert(Label && "Missing label");
6106 
6107     SDDbgLabel *SDV;
6108     SDV = DAG.getDbgLabel(Label, dl, SDNodeOrder);
6109     DAG.AddDbgLabel(SDV);
6110     return;
6111   }
6112   case Intrinsic::dbg_value: {
6113     const DbgValueInst &DI = cast<DbgValueInst>(I);
6114     assert(DI.getVariable() && "Missing variable");
6115 
6116     DILocalVariable *Variable = DI.getVariable();
6117     DIExpression *Expression = DI.getExpression();
6118     dropDanglingDebugInfo(Variable, Expression);
6119     SmallVector<Value *, 4> Values(DI.getValues());
6120     if (Values.empty())
6121       return;
6122 
6123     if (llvm::is_contained(Values, nullptr))
6124       return;
6125 
6126     bool IsVariadic = DI.hasArgList();
6127     if (!handleDebugValue(Values, Variable, Expression, dl, DI.getDebugLoc(),
6128                           SDNodeOrder, IsVariadic))
6129       addDanglingDebugInfo(&DI, dl, SDNodeOrder);
6130     return;
6131   }
6132 
6133   case Intrinsic::eh_typeid_for: {
6134     // Find the type id for the given typeinfo.
6135     GlobalValue *GV = ExtractTypeInfo(I.getArgOperand(0));
6136     unsigned TypeID = DAG.getMachineFunction().getTypeIDFor(GV);
6137     Res = DAG.getConstant(TypeID, sdl, MVT::i32);
6138     setValue(&I, Res);
6139     return;
6140   }
6141 
6142   case Intrinsic::eh_return_i32:
6143   case Intrinsic::eh_return_i64:
6144     DAG.getMachineFunction().setCallsEHReturn(true);
6145     DAG.setRoot(DAG.getNode(ISD::EH_RETURN, sdl,
6146                             MVT::Other,
6147                             getControlRoot(),
6148                             getValue(I.getArgOperand(0)),
6149                             getValue(I.getArgOperand(1))));
6150     return;
6151   case Intrinsic::eh_unwind_init:
6152     DAG.getMachineFunction().setCallsUnwindInit(true);
6153     return;
6154   case Intrinsic::eh_dwarf_cfa:
6155     setValue(&I, DAG.getNode(ISD::EH_DWARF_CFA, sdl,
6156                              TLI.getPointerTy(DAG.getDataLayout()),
6157                              getValue(I.getArgOperand(0))));
6158     return;
6159   case Intrinsic::eh_sjlj_callsite: {
6160     MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
6161     ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(0));
6162     assert(CI && "Non-constant call site value in eh.sjlj.callsite!");
6163     assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!");
6164 
6165     MMI.setCurrentCallSite(CI->getZExtValue());
6166     return;
6167   }
6168   case Intrinsic::eh_sjlj_functioncontext: {
6169     // Get and store the index of the function context.
6170     MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
6171     AllocaInst *FnCtx =
6172       cast<AllocaInst>(I.getArgOperand(0)->stripPointerCasts());
6173     int FI = FuncInfo.StaticAllocaMap[FnCtx];
6174     MFI.setFunctionContextIndex(FI);
6175     return;
6176   }
6177   case Intrinsic::eh_sjlj_setjmp: {
6178     SDValue Ops[2];
6179     Ops[0] = getRoot();
6180     Ops[1] = getValue(I.getArgOperand(0));
6181     SDValue Op = DAG.getNode(ISD::EH_SJLJ_SETJMP, sdl,
6182                              DAG.getVTList(MVT::i32, MVT::Other), Ops);
6183     setValue(&I, Op.getValue(0));
6184     DAG.setRoot(Op.getValue(1));
6185     return;
6186   }
6187   case Intrinsic::eh_sjlj_longjmp:
6188     DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, sdl, MVT::Other,
6189                             getRoot(), getValue(I.getArgOperand(0))));
6190     return;
6191   case Intrinsic::eh_sjlj_setup_dispatch:
6192     DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_SETUP_DISPATCH, sdl, MVT::Other,
6193                             getRoot()));
6194     return;
6195   case Intrinsic::masked_gather:
6196     visitMaskedGather(I);
6197     return;
6198   case Intrinsic::masked_load:
6199     visitMaskedLoad(I);
6200     return;
6201   case Intrinsic::masked_scatter:
6202     visitMaskedScatter(I);
6203     return;
6204   case Intrinsic::masked_store:
6205     visitMaskedStore(I);
6206     return;
6207   case Intrinsic::masked_expandload:
6208     visitMaskedLoad(I, true /* IsExpanding */);
6209     return;
6210   case Intrinsic::masked_compressstore:
6211     visitMaskedStore(I, true /* IsCompressing */);
6212     return;
6213   case Intrinsic::powi:
6214     setValue(&I, ExpandPowI(sdl, getValue(I.getArgOperand(0)),
6215                             getValue(I.getArgOperand(1)), DAG));
6216     return;
6217   case Intrinsic::log:
6218     setValue(&I, expandLog(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6219     return;
6220   case Intrinsic::log2:
6221     setValue(&I,
6222              expandLog2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6223     return;
6224   case Intrinsic::log10:
6225     setValue(&I,
6226              expandLog10(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6227     return;
6228   case Intrinsic::exp:
6229     setValue(&I, expandExp(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6230     return;
6231   case Intrinsic::exp2:
6232     setValue(&I,
6233              expandExp2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6234     return;
6235   case Intrinsic::pow:
6236     setValue(&I, expandPow(sdl, getValue(I.getArgOperand(0)),
6237                            getValue(I.getArgOperand(1)), DAG, TLI, Flags));
6238     return;
6239   case Intrinsic::sqrt:
6240   case Intrinsic::fabs:
6241   case Intrinsic::sin:
6242   case Intrinsic::cos:
6243   case Intrinsic::floor:
6244   case Intrinsic::ceil:
6245   case Intrinsic::trunc:
6246   case Intrinsic::rint:
6247   case Intrinsic::nearbyint:
6248   case Intrinsic::round:
6249   case Intrinsic::roundeven:
6250   case Intrinsic::canonicalize: {
6251     unsigned Opcode;
6252     switch (Intrinsic) {
6253     default: llvm_unreachable("Impossible intrinsic");  // Can't reach here.
6254     case Intrinsic::sqrt:      Opcode = ISD::FSQRT;      break;
6255     case Intrinsic::fabs:      Opcode = ISD::FABS;       break;
6256     case Intrinsic::sin:       Opcode = ISD::FSIN;       break;
6257     case Intrinsic::cos:       Opcode = ISD::FCOS;       break;
6258     case Intrinsic::floor:     Opcode = ISD::FFLOOR;     break;
6259     case Intrinsic::ceil:      Opcode = ISD::FCEIL;      break;
6260     case Intrinsic::trunc:     Opcode = ISD::FTRUNC;     break;
6261     case Intrinsic::rint:      Opcode = ISD::FRINT;      break;
6262     case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break;
6263     case Intrinsic::round:     Opcode = ISD::FROUND;     break;
6264     case Intrinsic::roundeven: Opcode = ISD::FROUNDEVEN; break;
6265     case Intrinsic::canonicalize: Opcode = ISD::FCANONICALIZE; break;
6266     }
6267 
6268     setValue(&I, DAG.getNode(Opcode, sdl,
6269                              getValue(I.getArgOperand(0)).getValueType(),
6270                              getValue(I.getArgOperand(0)), Flags));
6271     return;
6272   }
6273   case Intrinsic::lround:
6274   case Intrinsic::llround:
6275   case Intrinsic::lrint:
6276   case Intrinsic::llrint: {
6277     unsigned Opcode;
6278     switch (Intrinsic) {
6279     default: llvm_unreachable("Impossible intrinsic");  // Can't reach here.
6280     case Intrinsic::lround:  Opcode = ISD::LROUND;  break;
6281     case Intrinsic::llround: Opcode = ISD::LLROUND; break;
6282     case Intrinsic::lrint:   Opcode = ISD::LRINT;   break;
6283     case Intrinsic::llrint:  Opcode = ISD::LLRINT;  break;
6284     }
6285 
6286     EVT RetVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6287     setValue(&I, DAG.getNode(Opcode, sdl, RetVT,
6288                              getValue(I.getArgOperand(0))));
6289     return;
6290   }
6291   case Intrinsic::minnum:
6292     setValue(&I, DAG.getNode(ISD::FMINNUM, sdl,
6293                              getValue(I.getArgOperand(0)).getValueType(),
6294                              getValue(I.getArgOperand(0)),
6295                              getValue(I.getArgOperand(1)), Flags));
6296     return;
6297   case Intrinsic::maxnum:
6298     setValue(&I, DAG.getNode(ISD::FMAXNUM, sdl,
6299                              getValue(I.getArgOperand(0)).getValueType(),
6300                              getValue(I.getArgOperand(0)),
6301                              getValue(I.getArgOperand(1)), Flags));
6302     return;
6303   case Intrinsic::minimum:
6304     setValue(&I, DAG.getNode(ISD::FMINIMUM, sdl,
6305                              getValue(I.getArgOperand(0)).getValueType(),
6306                              getValue(I.getArgOperand(0)),
6307                              getValue(I.getArgOperand(1)), Flags));
6308     return;
6309   case Intrinsic::maximum:
6310     setValue(&I, DAG.getNode(ISD::FMAXIMUM, sdl,
6311                              getValue(I.getArgOperand(0)).getValueType(),
6312                              getValue(I.getArgOperand(0)),
6313                              getValue(I.getArgOperand(1)), Flags));
6314     return;
6315   case Intrinsic::copysign:
6316     setValue(&I, DAG.getNode(ISD::FCOPYSIGN, sdl,
6317                              getValue(I.getArgOperand(0)).getValueType(),
6318                              getValue(I.getArgOperand(0)),
6319                              getValue(I.getArgOperand(1)), Flags));
6320     return;
6321   case Intrinsic::arithmetic_fence: {
6322     setValue(&I, DAG.getNode(ISD::ARITH_FENCE, sdl,
6323                              getValue(I.getArgOperand(0)).getValueType(),
6324                              getValue(I.getArgOperand(0)), Flags));
6325     return;
6326   }
6327   case Intrinsic::fma:
6328     setValue(&I, DAG.getNode(
6329                      ISD::FMA, sdl, getValue(I.getArgOperand(0)).getValueType(),
6330                      getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)),
6331                      getValue(I.getArgOperand(2)), Flags));
6332     return;
6333 #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC)                         \
6334   case Intrinsic::INTRINSIC:
6335 #include "llvm/IR/ConstrainedOps.def"
6336     visitConstrainedFPIntrinsic(cast<ConstrainedFPIntrinsic>(I));
6337     return;
6338 #define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
6339 #include "llvm/IR/VPIntrinsics.def"
6340     visitVectorPredicationIntrinsic(cast<VPIntrinsic>(I));
6341     return;
6342   case Intrinsic::fptrunc_round: {
6343     // Get the last argument, the metadata and convert it to an integer in the
6344     // call
6345     Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(1))->getMetadata();
6346     Optional<RoundingMode> RoundMode =
6347         convertStrToRoundingMode(cast<MDString>(MD)->getString());
6348 
6349     EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6350 
6351     // Propagate fast-math-flags from IR to node(s).
6352     SDNodeFlags Flags;
6353     Flags.copyFMF(*cast<FPMathOperator>(&I));
6354     SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
6355 
6356     SDValue Result;
6357     Result = DAG.getNode(
6358         ISD::FPTRUNC_ROUND, sdl, VT, getValue(I.getArgOperand(0)),
6359         DAG.getTargetConstant((int)RoundMode.getValue(), sdl,
6360                               TLI.getPointerTy(DAG.getDataLayout())));
6361     setValue(&I, Result);
6362 
6363     return;
6364   }
6365   case Intrinsic::fmuladd: {
6366     EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6367     if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
6368         TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) {
6369       setValue(&I, DAG.getNode(ISD::FMA, sdl,
6370                                getValue(I.getArgOperand(0)).getValueType(),
6371                                getValue(I.getArgOperand(0)),
6372                                getValue(I.getArgOperand(1)),
6373                                getValue(I.getArgOperand(2)), Flags));
6374     } else {
6375       // TODO: Intrinsic calls should have fast-math-flags.
6376       SDValue Mul = DAG.getNode(
6377           ISD::FMUL, sdl, getValue(I.getArgOperand(0)).getValueType(),
6378           getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)), Flags);
6379       SDValue Add = DAG.getNode(ISD::FADD, sdl,
6380                                 getValue(I.getArgOperand(0)).getValueType(),
6381                                 Mul, getValue(I.getArgOperand(2)), Flags);
6382       setValue(&I, Add);
6383     }
6384     return;
6385   }
6386   case Intrinsic::convert_to_fp16:
6387     setValue(&I, DAG.getNode(ISD::BITCAST, sdl, MVT::i16,
6388                              DAG.getNode(ISD::FP_ROUND, sdl, MVT::f16,
6389                                          getValue(I.getArgOperand(0)),
6390                                          DAG.getTargetConstant(0, sdl,
6391                                                                MVT::i32))));
6392     return;
6393   case Intrinsic::convert_from_fp16:
6394     setValue(&I, DAG.getNode(ISD::FP_EXTEND, sdl,
6395                              TLI.getValueType(DAG.getDataLayout(), I.getType()),
6396                              DAG.getNode(ISD::BITCAST, sdl, MVT::f16,
6397                                          getValue(I.getArgOperand(0)))));
6398     return;
6399   case Intrinsic::fptosi_sat: {
6400     EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6401     setValue(&I, DAG.getNode(ISD::FP_TO_SINT_SAT, sdl, VT,
6402                              getValue(I.getArgOperand(0)),
6403                              DAG.getValueType(VT.getScalarType())));
6404     return;
6405   }
6406   case Intrinsic::fptoui_sat: {
6407     EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6408     setValue(&I, DAG.getNode(ISD::FP_TO_UINT_SAT, sdl, VT,
6409                              getValue(I.getArgOperand(0)),
6410                              DAG.getValueType(VT.getScalarType())));
6411     return;
6412   }
6413   case Intrinsic::set_rounding:
6414     Res = DAG.getNode(ISD::SET_ROUNDING, sdl, MVT::Other,
6415                       {getRoot(), getValue(I.getArgOperand(0))});
6416     setValue(&I, Res);
6417     DAG.setRoot(Res.getValue(0));
6418     return;
6419   case Intrinsic::pcmarker: {
6420     SDValue Tmp = getValue(I.getArgOperand(0));
6421     DAG.setRoot(DAG.getNode(ISD::PCMARKER, sdl, MVT::Other, getRoot(), Tmp));
6422     return;
6423   }
6424   case Intrinsic::readcyclecounter: {
6425     SDValue Op = getRoot();
6426     Res = DAG.getNode(ISD::READCYCLECOUNTER, sdl,
6427                       DAG.getVTList(MVT::i64, MVT::Other), Op);
6428     setValue(&I, Res);
6429     DAG.setRoot(Res.getValue(1));
6430     return;
6431   }
6432   case Intrinsic::bitreverse:
6433     setValue(&I, DAG.getNode(ISD::BITREVERSE, sdl,
6434                              getValue(I.getArgOperand(0)).getValueType(),
6435                              getValue(I.getArgOperand(0))));
6436     return;
6437   case Intrinsic::bswap:
6438     setValue(&I, DAG.getNode(ISD::BSWAP, sdl,
6439                              getValue(I.getArgOperand(0)).getValueType(),
6440                              getValue(I.getArgOperand(0))));
6441     return;
6442   case Intrinsic::cttz: {
6443     SDValue Arg = getValue(I.getArgOperand(0));
6444     ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
6445     EVT Ty = Arg.getValueType();
6446     setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTTZ : ISD::CTTZ_ZERO_UNDEF,
6447                              sdl, Ty, Arg));
6448     return;
6449   }
6450   case Intrinsic::ctlz: {
6451     SDValue Arg = getValue(I.getArgOperand(0));
6452     ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
6453     EVT Ty = Arg.getValueType();
6454     setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTLZ : ISD::CTLZ_ZERO_UNDEF,
6455                              sdl, Ty, Arg));
6456     return;
6457   }
6458   case Intrinsic::ctpop: {
6459     SDValue Arg = getValue(I.getArgOperand(0));
6460     EVT Ty = Arg.getValueType();
6461     setValue(&I, DAG.getNode(ISD::CTPOP, sdl, Ty, Arg));
6462     return;
6463   }
6464   case Intrinsic::fshl:
6465   case Intrinsic::fshr: {
6466     bool IsFSHL = Intrinsic == Intrinsic::fshl;
6467     SDValue X = getValue(I.getArgOperand(0));
6468     SDValue Y = getValue(I.getArgOperand(1));
6469     SDValue Z = getValue(I.getArgOperand(2));
6470     EVT VT = X.getValueType();
6471 
6472     if (X == Y) {
6473       auto RotateOpcode = IsFSHL ? ISD::ROTL : ISD::ROTR;
6474       setValue(&I, DAG.getNode(RotateOpcode, sdl, VT, X, Z));
6475     } else {
6476       auto FunnelOpcode = IsFSHL ? ISD::FSHL : ISD::FSHR;
6477       setValue(&I, DAG.getNode(FunnelOpcode, sdl, VT, X, Y, Z));
6478     }
6479     return;
6480   }
6481   case Intrinsic::sadd_sat: {
6482     SDValue Op1 = getValue(I.getArgOperand(0));
6483     SDValue Op2 = getValue(I.getArgOperand(1));
6484     setValue(&I, DAG.getNode(ISD::SADDSAT, sdl, Op1.getValueType(), Op1, Op2));
6485     return;
6486   }
6487   case Intrinsic::uadd_sat: {
6488     SDValue Op1 = getValue(I.getArgOperand(0));
6489     SDValue Op2 = getValue(I.getArgOperand(1));
6490     setValue(&I, DAG.getNode(ISD::UADDSAT, sdl, Op1.getValueType(), Op1, Op2));
6491     return;
6492   }
6493   case Intrinsic::ssub_sat: {
6494     SDValue Op1 = getValue(I.getArgOperand(0));
6495     SDValue Op2 = getValue(I.getArgOperand(1));
6496     setValue(&I, DAG.getNode(ISD::SSUBSAT, sdl, Op1.getValueType(), Op1, Op2));
6497     return;
6498   }
6499   case Intrinsic::usub_sat: {
6500     SDValue Op1 = getValue(I.getArgOperand(0));
6501     SDValue Op2 = getValue(I.getArgOperand(1));
6502     setValue(&I, DAG.getNode(ISD::USUBSAT, sdl, Op1.getValueType(), Op1, Op2));
6503     return;
6504   }
6505   case Intrinsic::sshl_sat: {
6506     SDValue Op1 = getValue(I.getArgOperand(0));
6507     SDValue Op2 = getValue(I.getArgOperand(1));
6508     setValue(&I, DAG.getNode(ISD::SSHLSAT, sdl, Op1.getValueType(), Op1, Op2));
6509     return;
6510   }
6511   case Intrinsic::ushl_sat: {
6512     SDValue Op1 = getValue(I.getArgOperand(0));
6513     SDValue Op2 = getValue(I.getArgOperand(1));
6514     setValue(&I, DAG.getNode(ISD::USHLSAT, sdl, Op1.getValueType(), Op1, Op2));
6515     return;
6516   }
6517   case Intrinsic::smul_fix:
6518   case Intrinsic::umul_fix:
6519   case Intrinsic::smul_fix_sat:
6520   case Intrinsic::umul_fix_sat: {
6521     SDValue Op1 = getValue(I.getArgOperand(0));
6522     SDValue Op2 = getValue(I.getArgOperand(1));
6523     SDValue Op3 = getValue(I.getArgOperand(2));
6524     setValue(&I, DAG.getNode(FixedPointIntrinsicToOpcode(Intrinsic), sdl,
6525                              Op1.getValueType(), Op1, Op2, Op3));
6526     return;
6527   }
6528   case Intrinsic::sdiv_fix:
6529   case Intrinsic::udiv_fix:
6530   case Intrinsic::sdiv_fix_sat:
6531   case Intrinsic::udiv_fix_sat: {
6532     SDValue Op1 = getValue(I.getArgOperand(0));
6533     SDValue Op2 = getValue(I.getArgOperand(1));
6534     SDValue Op3 = getValue(I.getArgOperand(2));
6535     setValue(&I, expandDivFix(FixedPointIntrinsicToOpcode(Intrinsic), sdl,
6536                               Op1, Op2, Op3, DAG, TLI));
6537     return;
6538   }
6539   case Intrinsic::smax: {
6540     SDValue Op1 = getValue(I.getArgOperand(0));
6541     SDValue Op2 = getValue(I.getArgOperand(1));
6542     setValue(&I, DAG.getNode(ISD::SMAX, sdl, Op1.getValueType(), Op1, Op2));
6543     return;
6544   }
6545   case Intrinsic::smin: {
6546     SDValue Op1 = getValue(I.getArgOperand(0));
6547     SDValue Op2 = getValue(I.getArgOperand(1));
6548     setValue(&I, DAG.getNode(ISD::SMIN, sdl, Op1.getValueType(), Op1, Op2));
6549     return;
6550   }
6551   case Intrinsic::umax: {
6552     SDValue Op1 = getValue(I.getArgOperand(0));
6553     SDValue Op2 = getValue(I.getArgOperand(1));
6554     setValue(&I, DAG.getNode(ISD::UMAX, sdl, Op1.getValueType(), Op1, Op2));
6555     return;
6556   }
6557   case Intrinsic::umin: {
6558     SDValue Op1 = getValue(I.getArgOperand(0));
6559     SDValue Op2 = getValue(I.getArgOperand(1));
6560     setValue(&I, DAG.getNode(ISD::UMIN, sdl, Op1.getValueType(), Op1, Op2));
6561     return;
6562   }
6563   case Intrinsic::abs: {
6564     // TODO: Preserve "int min is poison" arg in SDAG?
6565     SDValue Op1 = getValue(I.getArgOperand(0));
6566     setValue(&I, DAG.getNode(ISD::ABS, sdl, Op1.getValueType(), Op1));
6567     return;
6568   }
6569   case Intrinsic::stacksave: {
6570     SDValue Op = getRoot();
6571     EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6572     Res = DAG.getNode(ISD::STACKSAVE, sdl, DAG.getVTList(VT, MVT::Other), Op);
6573     setValue(&I, Res);
6574     DAG.setRoot(Res.getValue(1));
6575     return;
6576   }
6577   case Intrinsic::stackrestore:
6578     Res = getValue(I.getArgOperand(0));
6579     DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, sdl, MVT::Other, getRoot(), Res));
6580     return;
6581   case Intrinsic::get_dynamic_area_offset: {
6582     SDValue Op = getRoot();
6583     EVT PtrTy = TLI.getFrameIndexTy(DAG.getDataLayout());
6584     EVT ResTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
6585     // Result type for @llvm.get.dynamic.area.offset should match PtrTy for
6586     // target.
6587     if (PtrTy.getFixedSizeInBits() < ResTy.getFixedSizeInBits())
6588       report_fatal_error("Wrong result type for @llvm.get.dynamic.area.offset"
6589                          " intrinsic!");
6590     Res = DAG.getNode(ISD::GET_DYNAMIC_AREA_OFFSET, sdl, DAG.getVTList(ResTy),
6591                       Op);
6592     DAG.setRoot(Op);
6593     setValue(&I, Res);
6594     return;
6595   }
6596   case Intrinsic::stackguard: {
6597     MachineFunction &MF = DAG.getMachineFunction();
6598     const Module &M = *MF.getFunction().getParent();
6599     SDValue Chain = getRoot();
6600     if (TLI.useLoadStackGuardNode()) {
6601       Res = getLoadStackGuard(DAG, sdl, Chain);
6602     } else {
6603       EVT PtrTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
6604       const Value *Global = TLI.getSDagStackGuard(M);
6605       Align Align = DAG.getDataLayout().getPrefTypeAlign(Global->getType());
6606       Res = DAG.getLoad(PtrTy, sdl, Chain, getValue(Global),
6607                         MachinePointerInfo(Global, 0), Align,
6608                         MachineMemOperand::MOVolatile);
6609     }
6610     if (TLI.useStackGuardXorFP())
6611       Res = TLI.emitStackGuardXorFP(DAG, Res, sdl);
6612     DAG.setRoot(Chain);
6613     setValue(&I, Res);
6614     return;
6615   }
6616   case Intrinsic::stackprotector: {
6617     // Emit code into the DAG to store the stack guard onto the stack.
6618     MachineFunction &MF = DAG.getMachineFunction();
6619     MachineFrameInfo &MFI = MF.getFrameInfo();
6620     SDValue Src, Chain = getRoot();
6621 
6622     if (TLI.useLoadStackGuardNode())
6623       Src = getLoadStackGuard(DAG, sdl, Chain);
6624     else
6625       Src = getValue(I.getArgOperand(0));   // The guard's value.
6626 
6627     AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
6628 
6629     int FI = FuncInfo.StaticAllocaMap[Slot];
6630     MFI.setStackProtectorIndex(FI);
6631     EVT PtrTy = TLI.getFrameIndexTy(DAG.getDataLayout());
6632 
6633     SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
6634 
6635     // Store the stack protector onto the stack.
6636     Res = DAG.getStore(
6637         Chain, sdl, Src, FIN,
6638         MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
6639         MaybeAlign(), MachineMemOperand::MOVolatile);
6640     setValue(&I, Res);
6641     DAG.setRoot(Res);
6642     return;
6643   }
6644   case Intrinsic::objectsize:
6645     llvm_unreachable("llvm.objectsize.* should have been lowered already");
6646 
6647   case Intrinsic::is_constant:
6648     llvm_unreachable("llvm.is.constant.* should have been lowered already");
6649 
6650   case Intrinsic::annotation:
6651   case Intrinsic::ptr_annotation:
6652   case Intrinsic::launder_invariant_group:
6653   case Intrinsic::strip_invariant_group:
6654     // Drop the intrinsic, but forward the value
6655     setValue(&I, getValue(I.getOperand(0)));
6656     return;
6657 
6658   case Intrinsic::assume:
6659   case Intrinsic::experimental_noalias_scope_decl:
6660   case Intrinsic::var_annotation:
6661   case Intrinsic::sideeffect:
6662     // Discard annotate attributes, noalias scope declarations, assumptions, and
6663     // artificial side-effects.
6664     return;
6665 
6666   case Intrinsic::codeview_annotation: {
6667     // Emit a label associated with this metadata.
6668     MachineFunction &MF = DAG.getMachineFunction();
6669     MCSymbol *Label =
6670         MF.getMMI().getContext().createTempSymbol("annotation", true);
6671     Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(0))->getMetadata();
6672     MF.addCodeViewAnnotation(Label, cast<MDNode>(MD));
6673     Res = DAG.getLabelNode(ISD::ANNOTATION_LABEL, sdl, getRoot(), Label);
6674     DAG.setRoot(Res);
6675     return;
6676   }
6677 
6678   case Intrinsic::init_trampoline: {
6679     const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts());
6680 
6681     SDValue Ops[6];
6682     Ops[0] = getRoot();
6683     Ops[1] = getValue(I.getArgOperand(0));
6684     Ops[2] = getValue(I.getArgOperand(1));
6685     Ops[3] = getValue(I.getArgOperand(2));
6686     Ops[4] = DAG.getSrcValue(I.getArgOperand(0));
6687     Ops[5] = DAG.getSrcValue(F);
6688 
6689     Res = DAG.getNode(ISD::INIT_TRAMPOLINE, sdl, MVT::Other, Ops);
6690 
6691     DAG.setRoot(Res);
6692     return;
6693   }
6694   case Intrinsic::adjust_trampoline:
6695     setValue(&I, DAG.getNode(ISD::ADJUST_TRAMPOLINE, sdl,
6696                              TLI.getPointerTy(DAG.getDataLayout()),
6697                              getValue(I.getArgOperand(0))));
6698     return;
6699   case Intrinsic::gcroot: {
6700     assert(DAG.getMachineFunction().getFunction().hasGC() &&
6701            "only valid in functions with gc specified, enforced by Verifier");
6702     assert(GFI && "implied by previous");
6703     const Value *Alloca = I.getArgOperand(0)->stripPointerCasts();
6704     const Constant *TypeMap = cast<Constant>(I.getArgOperand(1));
6705 
6706     FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
6707     GFI->addStackRoot(FI->getIndex(), TypeMap);
6708     return;
6709   }
6710   case Intrinsic::gcread:
6711   case Intrinsic::gcwrite:
6712     llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
6713   case Intrinsic::flt_rounds:
6714     Res = DAG.getNode(ISD::FLT_ROUNDS_, sdl, {MVT::i32, MVT::Other}, getRoot());
6715     setValue(&I, Res);
6716     DAG.setRoot(Res.getValue(1));
6717     return;
6718 
6719   case Intrinsic::expect:
6720     // Just replace __builtin_expect(exp, c) with EXP.
6721     setValue(&I, getValue(I.getArgOperand(0)));
6722     return;
6723 
6724   case Intrinsic::ubsantrap:
6725   case Intrinsic::debugtrap:
6726   case Intrinsic::trap: {
6727     StringRef TrapFuncName =
6728         I.getAttributes().getFnAttr("trap-func-name").getValueAsString();
6729     if (TrapFuncName.empty()) {
6730       switch (Intrinsic) {
6731       case Intrinsic::trap:
6732         DAG.setRoot(DAG.getNode(ISD::TRAP, sdl, MVT::Other, getRoot()));
6733         break;
6734       case Intrinsic::debugtrap:
6735         DAG.setRoot(DAG.getNode(ISD::DEBUGTRAP, sdl, MVT::Other, getRoot()));
6736         break;
6737       case Intrinsic::ubsantrap:
6738         DAG.setRoot(DAG.getNode(
6739             ISD::UBSANTRAP, sdl, MVT::Other, getRoot(),
6740             DAG.getTargetConstant(
6741                 cast<ConstantInt>(I.getArgOperand(0))->getZExtValue(), sdl,
6742                 MVT::i32)));
6743         break;
6744       default: llvm_unreachable("unknown trap intrinsic");
6745       }
6746       return;
6747     }
6748     TargetLowering::ArgListTy Args;
6749     if (Intrinsic == Intrinsic::ubsantrap) {
6750       Args.push_back(TargetLoweringBase::ArgListEntry());
6751       Args[0].Val = I.getArgOperand(0);
6752       Args[0].Node = getValue(Args[0].Val);
6753       Args[0].Ty = Args[0].Val->getType();
6754     }
6755 
6756     TargetLowering::CallLoweringInfo CLI(DAG);
6757     CLI.setDebugLoc(sdl).setChain(getRoot()).setLibCallee(
6758         CallingConv::C, I.getType(),
6759         DAG.getExternalSymbol(TrapFuncName.data(),
6760                               TLI.getPointerTy(DAG.getDataLayout())),
6761         std::move(Args));
6762 
6763     std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
6764     DAG.setRoot(Result.second);
6765     return;
6766   }
6767 
6768   case Intrinsic::uadd_with_overflow:
6769   case Intrinsic::sadd_with_overflow:
6770   case Intrinsic::usub_with_overflow:
6771   case Intrinsic::ssub_with_overflow:
6772   case Intrinsic::umul_with_overflow:
6773   case Intrinsic::smul_with_overflow: {
6774     ISD::NodeType Op;
6775     switch (Intrinsic) {
6776     default: llvm_unreachable("Impossible intrinsic");  // Can't reach here.
6777     case Intrinsic::uadd_with_overflow: Op = ISD::UADDO; break;
6778     case Intrinsic::sadd_with_overflow: Op = ISD::SADDO; break;
6779     case Intrinsic::usub_with_overflow: Op = ISD::USUBO; break;
6780     case Intrinsic::ssub_with_overflow: Op = ISD::SSUBO; break;
6781     case Intrinsic::umul_with_overflow: Op = ISD::UMULO; break;
6782     case Intrinsic::smul_with_overflow: Op = ISD::SMULO; break;
6783     }
6784     SDValue Op1 = getValue(I.getArgOperand(0));
6785     SDValue Op2 = getValue(I.getArgOperand(1));
6786 
6787     EVT ResultVT = Op1.getValueType();
6788     EVT OverflowVT = MVT::i1;
6789     if (ResultVT.isVector())
6790       OverflowVT = EVT::getVectorVT(
6791           *Context, OverflowVT, ResultVT.getVectorElementCount());
6792 
6793     SDVTList VTs = DAG.getVTList(ResultVT, OverflowVT);
6794     setValue(&I, DAG.getNode(Op, sdl, VTs, Op1, Op2));
6795     return;
6796   }
6797   case Intrinsic::prefetch: {
6798     SDValue Ops[5];
6799     unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
6800     auto Flags = rw == 0 ? MachineMemOperand::MOLoad :MachineMemOperand::MOStore;
6801     Ops[0] = DAG.getRoot();
6802     Ops[1] = getValue(I.getArgOperand(0));
6803     Ops[2] = getValue(I.getArgOperand(1));
6804     Ops[3] = getValue(I.getArgOperand(2));
6805     Ops[4] = getValue(I.getArgOperand(3));
6806     SDValue Result = DAG.getMemIntrinsicNode(
6807         ISD::PREFETCH, sdl, DAG.getVTList(MVT::Other), Ops,
6808         EVT::getIntegerVT(*Context, 8), MachinePointerInfo(I.getArgOperand(0)),
6809         /* align */ None, Flags);
6810 
6811     // Chain the prefetch in parallell with any pending loads, to stay out of
6812     // the way of later optimizations.
6813     PendingLoads.push_back(Result);
6814     Result = getRoot();
6815     DAG.setRoot(Result);
6816     return;
6817   }
6818   case Intrinsic::lifetime_start:
6819   case Intrinsic::lifetime_end: {
6820     bool IsStart = (Intrinsic == Intrinsic::lifetime_start);
6821     // Stack coloring is not enabled in O0, discard region information.
6822     if (TM.getOptLevel() == CodeGenOpt::None)
6823       return;
6824 
6825     const int64_t ObjectSize =
6826         cast<ConstantInt>(I.getArgOperand(0))->getSExtValue();
6827     Value *const ObjectPtr = I.getArgOperand(1);
6828     SmallVector<const Value *, 4> Allocas;
6829     getUnderlyingObjects(ObjectPtr, Allocas);
6830 
6831     for (const Value *Alloca : Allocas) {
6832       const AllocaInst *LifetimeObject = dyn_cast_or_null<AllocaInst>(Alloca);
6833 
6834       // Could not find an Alloca.
6835       if (!LifetimeObject)
6836         continue;
6837 
6838       // First check that the Alloca is static, otherwise it won't have a
6839       // valid frame index.
6840       auto SI = FuncInfo.StaticAllocaMap.find(LifetimeObject);
6841       if (SI == FuncInfo.StaticAllocaMap.end())
6842         return;
6843 
6844       const int FrameIndex = SI->second;
6845       int64_t Offset;
6846       if (GetPointerBaseWithConstantOffset(
6847               ObjectPtr, Offset, DAG.getDataLayout()) != LifetimeObject)
6848         Offset = -1; // Cannot determine offset from alloca to lifetime object.
6849       Res = DAG.getLifetimeNode(IsStart, sdl, getRoot(), FrameIndex, ObjectSize,
6850                                 Offset);
6851       DAG.setRoot(Res);
6852     }
6853     return;
6854   }
6855   case Intrinsic::pseudoprobe: {
6856     auto Guid = cast<ConstantInt>(I.getArgOperand(0))->getZExtValue();
6857     auto Index = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
6858     auto Attr = cast<ConstantInt>(I.getArgOperand(2))->getZExtValue();
6859     Res = DAG.getPseudoProbeNode(sdl, getRoot(), Guid, Index, Attr);
6860     DAG.setRoot(Res);
6861     return;
6862   }
6863   case Intrinsic::invariant_start:
6864     // Discard region information.
6865     setValue(&I,
6866              DAG.getUNDEF(TLI.getValueType(DAG.getDataLayout(), I.getType())));
6867     return;
6868   case Intrinsic::invariant_end:
6869     // Discard region information.
6870     return;
6871   case Intrinsic::clear_cache:
6872     /// FunctionName may be null.
6873     if (const char *FunctionName = TLI.getClearCacheBuiltinName())
6874       lowerCallToExternalSymbol(I, FunctionName);
6875     return;
6876   case Intrinsic::donothing:
6877   case Intrinsic::seh_try_begin:
6878   case Intrinsic::seh_scope_begin:
6879   case Intrinsic::seh_try_end:
6880   case Intrinsic::seh_scope_end:
6881     // ignore
6882     return;
6883   case Intrinsic::experimental_stackmap:
6884     visitStackmap(I);
6885     return;
6886   case Intrinsic::experimental_patchpoint_void:
6887   case Intrinsic::experimental_patchpoint_i64:
6888     visitPatchpoint(I);
6889     return;
6890   case Intrinsic::experimental_gc_statepoint:
6891     LowerStatepoint(cast<GCStatepointInst>(I));
6892     return;
6893   case Intrinsic::experimental_gc_result:
6894     visitGCResult(cast<GCResultInst>(I));
6895     return;
6896   case Intrinsic::experimental_gc_relocate:
6897     visitGCRelocate(cast<GCRelocateInst>(I));
6898     return;
6899   case Intrinsic::instrprof_cover:
6900     llvm_unreachable("instrprof failed to lower a cover");
6901   case Intrinsic::instrprof_increment:
6902     llvm_unreachable("instrprof failed to lower an increment");
6903   case Intrinsic::instrprof_value_profile:
6904     llvm_unreachable("instrprof failed to lower a value profiling call");
6905   case Intrinsic::localescape: {
6906     MachineFunction &MF = DAG.getMachineFunction();
6907     const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo();
6908 
6909     // Directly emit some LOCAL_ESCAPE machine instrs. Label assignment emission
6910     // is the same on all targets.
6911     for (unsigned Idx = 0, E = I.arg_size(); Idx < E; ++Idx) {
6912       Value *Arg = I.getArgOperand(Idx)->stripPointerCasts();
6913       if (isa<ConstantPointerNull>(Arg))
6914         continue; // Skip null pointers. They represent a hole in index space.
6915       AllocaInst *Slot = cast<AllocaInst>(Arg);
6916       assert(FuncInfo.StaticAllocaMap.count(Slot) &&
6917              "can only escape static allocas");
6918       int FI = FuncInfo.StaticAllocaMap[Slot];
6919       MCSymbol *FrameAllocSym =
6920           MF.getMMI().getContext().getOrCreateFrameAllocSymbol(
6921               GlobalValue::dropLLVMManglingEscape(MF.getName()), Idx);
6922       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, dl,
6923               TII->get(TargetOpcode::LOCAL_ESCAPE))
6924           .addSym(FrameAllocSym)
6925           .addFrameIndex(FI);
6926     }
6927 
6928     return;
6929   }
6930 
6931   case Intrinsic::localrecover: {
6932     // i8* @llvm.localrecover(i8* %fn, i8* %fp, i32 %idx)
6933     MachineFunction &MF = DAG.getMachineFunction();
6934 
6935     // Get the symbol that defines the frame offset.
6936     auto *Fn = cast<Function>(I.getArgOperand(0)->stripPointerCasts());
6937     auto *Idx = cast<ConstantInt>(I.getArgOperand(2));
6938     unsigned IdxVal =
6939         unsigned(Idx->getLimitedValue(std::numeric_limits<int>::max()));
6940     MCSymbol *FrameAllocSym =
6941         MF.getMMI().getContext().getOrCreateFrameAllocSymbol(
6942             GlobalValue::dropLLVMManglingEscape(Fn->getName()), IdxVal);
6943 
6944     Value *FP = I.getArgOperand(1);
6945     SDValue FPVal = getValue(FP);
6946     EVT PtrVT = FPVal.getValueType();
6947 
6948     // Create a MCSymbol for the label to avoid any target lowering
6949     // that would make this PC relative.
6950     SDValue OffsetSym = DAG.getMCSymbol(FrameAllocSym, PtrVT);
6951     SDValue OffsetVal =
6952         DAG.getNode(ISD::LOCAL_RECOVER, sdl, PtrVT, OffsetSym);
6953 
6954     // Add the offset to the FP.
6955     SDValue Add = DAG.getMemBasePlusOffset(FPVal, OffsetVal, sdl);
6956     setValue(&I, Add);
6957 
6958     return;
6959   }
6960 
6961   case Intrinsic::eh_exceptionpointer:
6962   case Intrinsic::eh_exceptioncode: {
6963     // Get the exception pointer vreg, copy from it, and resize it to fit.
6964     const auto *CPI = cast<CatchPadInst>(I.getArgOperand(0));
6965     MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
6966     const TargetRegisterClass *PtrRC = TLI.getRegClassFor(PtrVT);
6967     unsigned VReg = FuncInfo.getCatchPadExceptionPointerVReg(CPI, PtrRC);
6968     SDValue N = DAG.getCopyFromReg(DAG.getEntryNode(), sdl, VReg, PtrVT);
6969     if (Intrinsic == Intrinsic::eh_exceptioncode)
6970       N = DAG.getZExtOrTrunc(N, sdl, MVT::i32);
6971     setValue(&I, N);
6972     return;
6973   }
6974   case Intrinsic::xray_customevent: {
6975     // Here we want to make sure that the intrinsic behaves as if it has a
6976     // specific calling convention, and only for x86_64.
6977     // FIXME: Support other platforms later.
6978     const auto &Triple = DAG.getTarget().getTargetTriple();
6979     if (Triple.getArch() != Triple::x86_64)
6980       return;
6981 
6982     SmallVector<SDValue, 8> Ops;
6983 
6984     // We want to say that we always want the arguments in registers.
6985     SDValue LogEntryVal = getValue(I.getArgOperand(0));
6986     SDValue StrSizeVal = getValue(I.getArgOperand(1));
6987     SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
6988     SDValue Chain = getRoot();
6989     Ops.push_back(LogEntryVal);
6990     Ops.push_back(StrSizeVal);
6991     Ops.push_back(Chain);
6992 
6993     // We need to enforce the calling convention for the callsite, so that
6994     // argument ordering is enforced correctly, and that register allocation can
6995     // see that some registers may be assumed clobbered and have to preserve
6996     // them across calls to the intrinsic.
6997     MachineSDNode *MN = DAG.getMachineNode(TargetOpcode::PATCHABLE_EVENT_CALL,
6998                                            sdl, NodeTys, Ops);
6999     SDValue patchableNode = SDValue(MN, 0);
7000     DAG.setRoot(patchableNode);
7001     setValue(&I, patchableNode);
7002     return;
7003   }
7004   case Intrinsic::xray_typedevent: {
7005     // Here we want to make sure that the intrinsic behaves as if it has a
7006     // specific calling convention, and only for x86_64.
7007     // FIXME: Support other platforms later.
7008     const auto &Triple = DAG.getTarget().getTargetTriple();
7009     if (Triple.getArch() != Triple::x86_64)
7010       return;
7011 
7012     SmallVector<SDValue, 8> Ops;
7013 
7014     // We want to say that we always want the arguments in registers.
7015     // It's unclear to me how manipulating the selection DAG here forces callers
7016     // to provide arguments in registers instead of on the stack.
7017     SDValue LogTypeId = getValue(I.getArgOperand(0));
7018     SDValue LogEntryVal = getValue(I.getArgOperand(1));
7019     SDValue StrSizeVal = getValue(I.getArgOperand(2));
7020     SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
7021     SDValue Chain = getRoot();
7022     Ops.push_back(LogTypeId);
7023     Ops.push_back(LogEntryVal);
7024     Ops.push_back(StrSizeVal);
7025     Ops.push_back(Chain);
7026 
7027     // We need to enforce the calling convention for the callsite, so that
7028     // argument ordering is enforced correctly, and that register allocation can
7029     // see that some registers may be assumed clobbered and have to preserve
7030     // them across calls to the intrinsic.
7031     MachineSDNode *MN = DAG.getMachineNode(
7032         TargetOpcode::PATCHABLE_TYPED_EVENT_CALL, sdl, NodeTys, Ops);
7033     SDValue patchableNode = SDValue(MN, 0);
7034     DAG.setRoot(patchableNode);
7035     setValue(&I, patchableNode);
7036     return;
7037   }
7038   case Intrinsic::experimental_deoptimize:
7039     LowerDeoptimizeCall(&I);
7040     return;
7041   case Intrinsic::experimental_stepvector:
7042     visitStepVector(I);
7043     return;
7044   case Intrinsic::vector_reduce_fadd:
7045   case Intrinsic::vector_reduce_fmul:
7046   case Intrinsic::vector_reduce_add:
7047   case Intrinsic::vector_reduce_mul:
7048   case Intrinsic::vector_reduce_and:
7049   case Intrinsic::vector_reduce_or:
7050   case Intrinsic::vector_reduce_xor:
7051   case Intrinsic::vector_reduce_smax:
7052   case Intrinsic::vector_reduce_smin:
7053   case Intrinsic::vector_reduce_umax:
7054   case Intrinsic::vector_reduce_umin:
7055   case Intrinsic::vector_reduce_fmax:
7056   case Intrinsic::vector_reduce_fmin:
7057     visitVectorReduce(I, Intrinsic);
7058     return;
7059 
7060   case Intrinsic::icall_branch_funnel: {
7061     SmallVector<SDValue, 16> Ops;
7062     Ops.push_back(getValue(I.getArgOperand(0)));
7063 
7064     int64_t Offset;
7065     auto *Base = dyn_cast<GlobalObject>(GetPointerBaseWithConstantOffset(
7066         I.getArgOperand(1), Offset, DAG.getDataLayout()));
7067     if (!Base)
7068       report_fatal_error(
7069           "llvm.icall.branch.funnel operand must be a GlobalValue");
7070     Ops.push_back(DAG.getTargetGlobalAddress(Base, sdl, MVT::i64, 0));
7071 
7072     struct BranchFunnelTarget {
7073       int64_t Offset;
7074       SDValue Target;
7075     };
7076     SmallVector<BranchFunnelTarget, 8> Targets;
7077 
7078     for (unsigned Op = 1, N = I.arg_size(); Op != N; Op += 2) {
7079       auto *ElemBase = dyn_cast<GlobalObject>(GetPointerBaseWithConstantOffset(
7080           I.getArgOperand(Op), Offset, DAG.getDataLayout()));
7081       if (ElemBase != Base)
7082         report_fatal_error("all llvm.icall.branch.funnel operands must refer "
7083                            "to the same GlobalValue");
7084 
7085       SDValue Val = getValue(I.getArgOperand(Op + 1));
7086       auto *GA = dyn_cast<GlobalAddressSDNode>(Val);
7087       if (!GA)
7088         report_fatal_error(
7089             "llvm.icall.branch.funnel operand must be a GlobalValue");
7090       Targets.push_back({Offset, DAG.getTargetGlobalAddress(
7091                                      GA->getGlobal(), sdl, Val.getValueType(),
7092                                      GA->getOffset())});
7093     }
7094     llvm::sort(Targets,
7095                [](const BranchFunnelTarget &T1, const BranchFunnelTarget &T2) {
7096                  return T1.Offset < T2.Offset;
7097                });
7098 
7099     for (auto &T : Targets) {
7100       Ops.push_back(DAG.getTargetConstant(T.Offset, sdl, MVT::i32));
7101       Ops.push_back(T.Target);
7102     }
7103 
7104     Ops.push_back(DAG.getRoot()); // Chain
7105     SDValue N(DAG.getMachineNode(TargetOpcode::ICALL_BRANCH_FUNNEL, sdl,
7106                                  MVT::Other, Ops),
7107               0);
7108     DAG.setRoot(N);
7109     setValue(&I, N);
7110     HasTailCall = true;
7111     return;
7112   }
7113 
7114   case Intrinsic::wasm_landingpad_index:
7115     // Information this intrinsic contained has been transferred to
7116     // MachineFunction in SelectionDAGISel::PrepareEHLandingPad. We can safely
7117     // delete it now.
7118     return;
7119 
7120   case Intrinsic::aarch64_settag:
7121   case Intrinsic::aarch64_settag_zero: {
7122     const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
7123     bool ZeroMemory = Intrinsic == Intrinsic::aarch64_settag_zero;
7124     SDValue Val = TSI.EmitTargetCodeForSetTag(
7125         DAG, sdl, getRoot(), getValue(I.getArgOperand(0)),
7126         getValue(I.getArgOperand(1)), MachinePointerInfo(I.getArgOperand(0)),
7127         ZeroMemory);
7128     DAG.setRoot(Val);
7129     setValue(&I, Val);
7130     return;
7131   }
7132   case Intrinsic::ptrmask: {
7133     SDValue Ptr = getValue(I.getOperand(0));
7134     SDValue Const = getValue(I.getOperand(1));
7135 
7136     EVT PtrVT = Ptr.getValueType();
7137     setValue(&I, DAG.getNode(ISD::AND, sdl, PtrVT, Ptr,
7138                              DAG.getZExtOrTrunc(Const, sdl, PtrVT)));
7139     return;
7140   }
7141   case Intrinsic::get_active_lane_mask: {
7142     EVT CCVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7143     SDValue Index = getValue(I.getOperand(0));
7144     EVT ElementVT = Index.getValueType();
7145 
7146     if (!TLI.shouldExpandGetActiveLaneMask(CCVT, ElementVT)) {
7147       visitTargetIntrinsic(I, Intrinsic);
7148       return;
7149     }
7150 
7151     SDValue TripCount = getValue(I.getOperand(1));
7152     auto VecTy = CCVT.changeVectorElementType(ElementVT);
7153 
7154     SDValue VectorIndex, VectorTripCount;
7155     if (VecTy.isScalableVector()) {
7156       VectorIndex = DAG.getSplatVector(VecTy, sdl, Index);
7157       VectorTripCount = DAG.getSplatVector(VecTy, sdl, TripCount);
7158     } else {
7159       VectorIndex = DAG.getSplatBuildVector(VecTy, sdl, Index);
7160       VectorTripCount = DAG.getSplatBuildVector(VecTy, sdl, TripCount);
7161     }
7162     SDValue VectorStep = DAG.getStepVector(sdl, VecTy);
7163     SDValue VectorInduction = DAG.getNode(
7164         ISD::UADDSAT, sdl, VecTy, VectorIndex, VectorStep);
7165     SDValue SetCC = DAG.getSetCC(sdl, CCVT, VectorInduction,
7166                                  VectorTripCount, ISD::CondCode::SETULT);
7167     setValue(&I, SetCC);
7168     return;
7169   }
7170   case Intrinsic::experimental_vector_insert: {
7171     SDValue Vec = getValue(I.getOperand(0));
7172     SDValue SubVec = getValue(I.getOperand(1));
7173     SDValue Index = getValue(I.getOperand(2));
7174 
7175     // The intrinsic's index type is i64, but the SDNode requires an index type
7176     // suitable for the target. Convert the index as required.
7177     MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
7178     if (Index.getValueType() != VectorIdxTy)
7179       Index = DAG.getVectorIdxConstant(
7180           cast<ConstantSDNode>(Index)->getZExtValue(), sdl);
7181 
7182     EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7183     setValue(&I, DAG.getNode(ISD::INSERT_SUBVECTOR, sdl, ResultVT, Vec, SubVec,
7184                              Index));
7185     return;
7186   }
7187   case Intrinsic::experimental_vector_extract: {
7188     SDValue Vec = getValue(I.getOperand(0));
7189     SDValue Index = getValue(I.getOperand(1));
7190     EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7191 
7192     // The intrinsic's index type is i64, but the SDNode requires an index type
7193     // suitable for the target. Convert the index as required.
7194     MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
7195     if (Index.getValueType() != VectorIdxTy)
7196       Index = DAG.getVectorIdxConstant(
7197           cast<ConstantSDNode>(Index)->getZExtValue(), sdl);
7198 
7199     setValue(&I,
7200              DAG.getNode(ISD::EXTRACT_SUBVECTOR, sdl, ResultVT, Vec, Index));
7201     return;
7202   }
7203   case Intrinsic::experimental_vector_reverse:
7204     visitVectorReverse(I);
7205     return;
7206   case Intrinsic::experimental_vector_splice:
7207     visitVectorSplice(I);
7208     return;
7209   }
7210 }
7211 
7212 void SelectionDAGBuilder::visitConstrainedFPIntrinsic(
7213     const ConstrainedFPIntrinsic &FPI) {
7214   SDLoc sdl = getCurSDLoc();
7215 
7216   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7217   SmallVector<EVT, 4> ValueVTs;
7218   ComputeValueVTs(TLI, DAG.getDataLayout(), FPI.getType(), ValueVTs);
7219   ValueVTs.push_back(MVT::Other); // Out chain
7220 
7221   // We do not need to serialize constrained FP intrinsics against
7222   // each other or against (nonvolatile) loads, so they can be
7223   // chained like loads.
7224   SDValue Chain = DAG.getRoot();
7225   SmallVector<SDValue, 4> Opers;
7226   Opers.push_back(Chain);
7227   if (FPI.isUnaryOp()) {
7228     Opers.push_back(getValue(FPI.getArgOperand(0)));
7229   } else if (FPI.isTernaryOp()) {
7230     Opers.push_back(getValue(FPI.getArgOperand(0)));
7231     Opers.push_back(getValue(FPI.getArgOperand(1)));
7232     Opers.push_back(getValue(FPI.getArgOperand(2)));
7233   } else {
7234     Opers.push_back(getValue(FPI.getArgOperand(0)));
7235     Opers.push_back(getValue(FPI.getArgOperand(1)));
7236   }
7237 
7238   auto pushOutChain = [this](SDValue Result, fp::ExceptionBehavior EB) {
7239     assert(Result.getNode()->getNumValues() == 2);
7240 
7241     // Push node to the appropriate list so that future instructions can be
7242     // chained up correctly.
7243     SDValue OutChain = Result.getValue(1);
7244     switch (EB) {
7245     case fp::ExceptionBehavior::ebIgnore:
7246       // The only reason why ebIgnore nodes still need to be chained is that
7247       // they might depend on the current rounding mode, and therefore must
7248       // not be moved across instruction that may change that mode.
7249       LLVM_FALLTHROUGH;
7250     case fp::ExceptionBehavior::ebMayTrap:
7251       // These must not be moved across calls or instructions that may change
7252       // floating-point exception masks.
7253       PendingConstrainedFP.push_back(OutChain);
7254       break;
7255     case fp::ExceptionBehavior::ebStrict:
7256       // These must not be moved across calls or instructions that may change
7257       // floating-point exception masks or read floating-point exception flags.
7258       // In addition, they cannot be optimized out even if unused.
7259       PendingConstrainedFPStrict.push_back(OutChain);
7260       break;
7261     }
7262   };
7263 
7264   SDVTList VTs = DAG.getVTList(ValueVTs);
7265   fp::ExceptionBehavior EB = FPI.getExceptionBehavior().getValue();
7266 
7267   SDNodeFlags Flags;
7268   if (EB == fp::ExceptionBehavior::ebIgnore)
7269     Flags.setNoFPExcept(true);
7270 
7271   if (auto *FPOp = dyn_cast<FPMathOperator>(&FPI))
7272     Flags.copyFMF(*FPOp);
7273 
7274   unsigned Opcode;
7275   switch (FPI.getIntrinsicID()) {
7276   default: llvm_unreachable("Impossible intrinsic");  // Can't reach here.
7277 #define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN)               \
7278   case Intrinsic::INTRINSIC:                                                   \
7279     Opcode = ISD::STRICT_##DAGN;                                               \
7280     break;
7281 #include "llvm/IR/ConstrainedOps.def"
7282   case Intrinsic::experimental_constrained_fmuladd: {
7283     Opcode = ISD::STRICT_FMA;
7284     // Break fmuladd into fmul and fadd.
7285     if (TM.Options.AllowFPOpFusion == FPOpFusion::Strict ||
7286         !TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(),
7287                                         ValueVTs[0])) {
7288       Opers.pop_back();
7289       SDValue Mul = DAG.getNode(ISD::STRICT_FMUL, sdl, VTs, Opers, Flags);
7290       pushOutChain(Mul, EB);
7291       Opcode = ISD::STRICT_FADD;
7292       Opers.clear();
7293       Opers.push_back(Mul.getValue(1));
7294       Opers.push_back(Mul.getValue(0));
7295       Opers.push_back(getValue(FPI.getArgOperand(2)));
7296     }
7297     break;
7298   }
7299   }
7300 
7301   // A few strict DAG nodes carry additional operands that are not
7302   // set up by the default code above.
7303   switch (Opcode) {
7304   default: break;
7305   case ISD::STRICT_FP_ROUND:
7306     Opers.push_back(
7307         DAG.getTargetConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout())));
7308     break;
7309   case ISD::STRICT_FSETCC:
7310   case ISD::STRICT_FSETCCS: {
7311     auto *FPCmp = dyn_cast<ConstrainedFPCmpIntrinsic>(&FPI);
7312     ISD::CondCode Condition = getFCmpCondCode(FPCmp->getPredicate());
7313     if (TM.Options.NoNaNsFPMath)
7314       Condition = getFCmpCodeWithoutNaN(Condition);
7315     Opers.push_back(DAG.getCondCode(Condition));
7316     break;
7317   }
7318   }
7319 
7320   SDValue Result = DAG.getNode(Opcode, sdl, VTs, Opers, Flags);
7321   pushOutChain(Result, EB);
7322 
7323   SDValue FPResult = Result.getValue(0);
7324   setValue(&FPI, FPResult);
7325 }
7326 
7327 static unsigned getISDForVPIntrinsic(const VPIntrinsic &VPIntrin) {
7328   Optional<unsigned> ResOPC;
7329   switch (VPIntrin.getIntrinsicID()) {
7330 #define HELPER_MAP_VPID_TO_VPSD(VPID, VPSD)                                    \
7331   case Intrinsic::VPID:                                                        \
7332     ResOPC = ISD::VPSD;                                                        \
7333     break;
7334 #include "llvm/IR/VPIntrinsics.def"
7335   }
7336 
7337   if (!ResOPC.hasValue())
7338     llvm_unreachable(
7339         "Inconsistency: no SDNode available for this VPIntrinsic!");
7340 
7341   if (*ResOPC == ISD::VP_REDUCE_SEQ_FADD ||
7342       *ResOPC == ISD::VP_REDUCE_SEQ_FMUL) {
7343     if (VPIntrin.getFastMathFlags().allowReassoc())
7344       return *ResOPC == ISD::VP_REDUCE_SEQ_FADD ? ISD::VP_REDUCE_FADD
7345                                                 : ISD::VP_REDUCE_FMUL;
7346   }
7347 
7348   return ResOPC.getValue();
7349 }
7350 
7351 void SelectionDAGBuilder::visitVPLoadGather(const VPIntrinsic &VPIntrin, EVT VT,
7352                                             SmallVector<SDValue, 7> &OpValues,
7353                                             bool IsGather) {
7354   SDLoc DL = getCurSDLoc();
7355   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7356   Value *PtrOperand = VPIntrin.getArgOperand(0);
7357   MaybeAlign Alignment = VPIntrin.getPointerAlignment();
7358   AAMDNodes AAInfo = VPIntrin.getAAMetadata();
7359   const MDNode *Ranges = VPIntrin.getMetadata(LLVMContext::MD_range);
7360   SDValue LD;
7361   bool AddToChain = true;
7362   if (!IsGather) {
7363     // Do not serialize variable-length loads of constant memory with
7364     // anything.
7365     if (!Alignment)
7366       Alignment = DAG.getEVTAlign(VT);
7367     MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
7368     AddToChain = !AA || !AA->pointsToConstantMemory(ML);
7369     SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
7370     MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7371         MachinePointerInfo(PtrOperand), MachineMemOperand::MOLoad,
7372         MemoryLocation::UnknownSize, *Alignment, AAInfo, Ranges);
7373     LD = DAG.getLoadVP(VT, DL, InChain, OpValues[0], OpValues[1], OpValues[2],
7374                        MMO, false /*IsExpanding */);
7375   } else {
7376     if (!Alignment)
7377       Alignment = DAG.getEVTAlign(VT.getScalarType());
7378     unsigned AS =
7379         PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
7380     MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7381         MachinePointerInfo(AS), MachineMemOperand::MOLoad,
7382         MemoryLocation::UnknownSize, *Alignment, AAInfo, Ranges);
7383     SDValue Base, Index, Scale;
7384     ISD::MemIndexType IndexType;
7385     bool UniformBase = getUniformBase(PtrOperand, Base, Index, IndexType, Scale,
7386                                       this, VPIntrin.getParent());
7387     if (!UniformBase) {
7388       Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout()));
7389       Index = getValue(PtrOperand);
7390       IndexType = ISD::SIGNED_UNSCALED;
7391       Scale =
7392           DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()));
7393     }
7394     EVT IdxVT = Index.getValueType();
7395     EVT EltTy = IdxVT.getVectorElementType();
7396     if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
7397       EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
7398       Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
7399     }
7400     LD = DAG.getGatherVP(
7401         DAG.getVTList(VT, MVT::Other), VT, DL,
7402         {DAG.getRoot(), Base, Index, Scale, OpValues[1], OpValues[2]}, MMO,
7403         IndexType);
7404   }
7405   if (AddToChain)
7406     PendingLoads.push_back(LD.getValue(1));
7407   setValue(&VPIntrin, LD);
7408 }
7409 
7410 void SelectionDAGBuilder::visitVPStoreScatter(const VPIntrinsic &VPIntrin,
7411                                               SmallVector<SDValue, 7> &OpValues,
7412                                               bool IsScatter) {
7413   SDLoc DL = getCurSDLoc();
7414   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7415   Value *PtrOperand = VPIntrin.getArgOperand(1);
7416   EVT VT = OpValues[0].getValueType();
7417   MaybeAlign Alignment = VPIntrin.getPointerAlignment();
7418   AAMDNodes AAInfo = VPIntrin.getAAMetadata();
7419   SDValue ST;
7420   if (!IsScatter) {
7421     if (!Alignment)
7422       Alignment = DAG.getEVTAlign(VT);
7423     SDValue Ptr = OpValues[1];
7424     SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
7425     MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7426         MachinePointerInfo(PtrOperand), MachineMemOperand::MOStore,
7427         MemoryLocation::UnknownSize, *Alignment, AAInfo);
7428     ST = DAG.getStoreVP(getMemoryRoot(), DL, OpValues[0], Ptr, Offset,
7429                         OpValues[2], OpValues[3], VT, MMO, ISD::UNINDEXED,
7430                         /* IsTruncating */ false, /*IsCompressing*/ false);
7431   } else {
7432     if (!Alignment)
7433       Alignment = DAG.getEVTAlign(VT.getScalarType());
7434     unsigned AS =
7435         PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
7436     MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7437         MachinePointerInfo(AS), MachineMemOperand::MOStore,
7438         MemoryLocation::UnknownSize, *Alignment, AAInfo);
7439     SDValue Base, Index, Scale;
7440     ISD::MemIndexType IndexType;
7441     bool UniformBase = getUniformBase(PtrOperand, Base, Index, IndexType, Scale,
7442                                       this, VPIntrin.getParent());
7443     if (!UniformBase) {
7444       Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout()));
7445       Index = getValue(PtrOperand);
7446       IndexType = ISD::SIGNED_UNSCALED;
7447       Scale =
7448           DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()));
7449     }
7450     EVT IdxVT = Index.getValueType();
7451     EVT EltTy = IdxVT.getVectorElementType();
7452     if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
7453       EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
7454       Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
7455     }
7456     ST = DAG.getScatterVP(DAG.getVTList(MVT::Other), VT, DL,
7457                           {getMemoryRoot(), OpValues[0], Base, Index, Scale,
7458                            OpValues[2], OpValues[3]},
7459                           MMO, IndexType);
7460   }
7461   DAG.setRoot(ST);
7462   setValue(&VPIntrin, ST);
7463 }
7464 
7465 void SelectionDAGBuilder::visitVPStridedLoad(
7466     const VPIntrinsic &VPIntrin, EVT VT, SmallVectorImpl<SDValue> &OpValues) {
7467   SDLoc DL = getCurSDLoc();
7468   Value *PtrOperand = VPIntrin.getArgOperand(0);
7469   MaybeAlign Alignment = VPIntrin.getPointerAlignment();
7470   if (!Alignment)
7471     Alignment = DAG.getEVTAlign(VT.getScalarType());
7472   AAMDNodes AAInfo = VPIntrin.getAAMetadata();
7473   const MDNode *Ranges = VPIntrin.getMetadata(LLVMContext::MD_range);
7474   MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
7475   bool AddToChain = !AA || !AA->pointsToConstantMemory(ML);
7476   SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
7477   MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7478       MachinePointerInfo(PtrOperand), MachineMemOperand::MOLoad,
7479       MemoryLocation::UnknownSize, *Alignment, AAInfo, Ranges);
7480 
7481   SDValue LD = DAG.getStridedLoadVP(VT, DL, InChain, OpValues[0], OpValues[1],
7482                                     OpValues[2], OpValues[3], MMO,
7483                                     false /*IsExpanding*/);
7484 
7485   if (AddToChain)
7486     PendingLoads.push_back(LD.getValue(1));
7487   setValue(&VPIntrin, LD);
7488 }
7489 
7490 void SelectionDAGBuilder::visitVPStridedStore(
7491     const VPIntrinsic &VPIntrin, SmallVectorImpl<SDValue> &OpValues) {
7492   SDLoc DL = getCurSDLoc();
7493   Value *PtrOperand = VPIntrin.getArgOperand(1);
7494   EVT VT = OpValues[0].getValueType();
7495   MaybeAlign Alignment = VPIntrin.getPointerAlignment();
7496   if (!Alignment)
7497     Alignment = DAG.getEVTAlign(VT.getScalarType());
7498   AAMDNodes AAInfo = VPIntrin.getAAMetadata();
7499   MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7500       MachinePointerInfo(PtrOperand), MachineMemOperand::MOStore,
7501       MemoryLocation::UnknownSize, *Alignment, AAInfo);
7502 
7503   SDValue ST = DAG.getStridedStoreVP(
7504       getMemoryRoot(), DL, OpValues[0], OpValues[1],
7505       DAG.getUNDEF(OpValues[1].getValueType()), OpValues[2], OpValues[3],
7506       OpValues[4], VT, MMO, ISD::UNINDEXED, /*IsTruncating*/ false,
7507       /*IsCompressing*/ false);
7508 
7509   DAG.setRoot(ST);
7510   setValue(&VPIntrin, ST);
7511 }
7512 
7513 void SelectionDAGBuilder::visitVPCmp(const VPCmpIntrinsic &VPIntrin) {
7514   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7515   SDLoc DL = getCurSDLoc();
7516 
7517   ISD::CondCode Condition;
7518   CmpInst::Predicate CondCode = VPIntrin.getPredicate();
7519   bool IsFP = VPIntrin.getOperand(0)->getType()->isFPOrFPVectorTy();
7520   if (IsFP) {
7521     // FIXME: Regular fcmps are FPMathOperators which may have fast-math (nnan)
7522     // flags, but calls that don't return floating-point types can't be
7523     // FPMathOperators, like vp.fcmp. This affects constrained fcmp too.
7524     Condition = getFCmpCondCode(CondCode);
7525     if (TM.Options.NoNaNsFPMath)
7526       Condition = getFCmpCodeWithoutNaN(Condition);
7527   } else {
7528     Condition = getICmpCondCode(CondCode);
7529   }
7530 
7531   SDValue Op1 = getValue(VPIntrin.getOperand(0));
7532   SDValue Op2 = getValue(VPIntrin.getOperand(1));
7533   // #2 is the condition code
7534   SDValue MaskOp = getValue(VPIntrin.getOperand(3));
7535   SDValue EVL = getValue(VPIntrin.getOperand(4));
7536   MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
7537   assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
7538          "Unexpected target EVL type");
7539   EVL = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, EVL);
7540 
7541   EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
7542                                                         VPIntrin.getType());
7543   setValue(&VPIntrin,
7544            DAG.getSetCCVP(DL, DestVT, Op1, Op2, Condition, MaskOp, EVL));
7545 }
7546 
7547 void SelectionDAGBuilder::visitVectorPredicationIntrinsic(
7548     const VPIntrinsic &VPIntrin) {
7549   SDLoc DL = getCurSDLoc();
7550   unsigned Opcode = getISDForVPIntrinsic(VPIntrin);
7551 
7552   auto IID = VPIntrin.getIntrinsicID();
7553 
7554   if (const auto *CmpI = dyn_cast<VPCmpIntrinsic>(&VPIntrin))
7555     return visitVPCmp(*CmpI);
7556 
7557   SmallVector<EVT, 4> ValueVTs;
7558   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7559   ComputeValueVTs(TLI, DAG.getDataLayout(), VPIntrin.getType(), ValueVTs);
7560   SDVTList VTs = DAG.getVTList(ValueVTs);
7561 
7562   auto EVLParamPos = VPIntrinsic::getVectorLengthParamPos(IID);
7563 
7564   MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
7565   assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
7566          "Unexpected target EVL type");
7567 
7568   // Request operands.
7569   SmallVector<SDValue, 7> OpValues;
7570   for (unsigned I = 0; I < VPIntrin.arg_size(); ++I) {
7571     auto Op = getValue(VPIntrin.getArgOperand(I));
7572     if (I == EVLParamPos)
7573       Op = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, Op);
7574     OpValues.push_back(Op);
7575   }
7576 
7577   switch (Opcode) {
7578   default: {
7579     SDValue Result = DAG.getNode(Opcode, DL, VTs, OpValues);
7580     setValue(&VPIntrin, Result);
7581     break;
7582   }
7583   case ISD::VP_LOAD:
7584   case ISD::VP_GATHER:
7585     visitVPLoadGather(VPIntrin, ValueVTs[0], OpValues,
7586                       Opcode == ISD::VP_GATHER);
7587     break;
7588   case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
7589     visitVPStridedLoad(VPIntrin, ValueVTs[0], OpValues);
7590     break;
7591   case ISD::VP_STORE:
7592   case ISD::VP_SCATTER:
7593     visitVPStoreScatter(VPIntrin, OpValues, Opcode == ISD::VP_SCATTER);
7594     break;
7595   case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
7596     visitVPStridedStore(VPIntrin, OpValues);
7597     break;
7598   }
7599 }
7600 
7601 SDValue SelectionDAGBuilder::lowerStartEH(SDValue Chain,
7602                                           const BasicBlock *EHPadBB,
7603                                           MCSymbol *&BeginLabel) {
7604   MachineFunction &MF = DAG.getMachineFunction();
7605   MachineModuleInfo &MMI = MF.getMMI();
7606 
7607   // Insert a label before the invoke call to mark the try range.  This can be
7608   // used to detect deletion of the invoke via the MachineModuleInfo.
7609   BeginLabel = MMI.getContext().createTempSymbol();
7610 
7611   // For SjLj, keep track of which landing pads go with which invokes
7612   // so as to maintain the ordering of pads in the LSDA.
7613   unsigned CallSiteIndex = MMI.getCurrentCallSite();
7614   if (CallSiteIndex) {
7615     MF.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
7616     LPadToCallSiteMap[FuncInfo.MBBMap[EHPadBB]].push_back(CallSiteIndex);
7617 
7618     // Now that the call site is handled, stop tracking it.
7619     MMI.setCurrentCallSite(0);
7620   }
7621 
7622   return DAG.getEHLabel(getCurSDLoc(), Chain, BeginLabel);
7623 }
7624 
7625 SDValue SelectionDAGBuilder::lowerEndEH(SDValue Chain, const InvokeInst *II,
7626                                         const BasicBlock *EHPadBB,
7627                                         MCSymbol *BeginLabel) {
7628   assert(BeginLabel && "BeginLabel should've been set");
7629 
7630   MachineFunction &MF = DAG.getMachineFunction();
7631   MachineModuleInfo &MMI = MF.getMMI();
7632 
7633   // Insert a label at the end of the invoke call to mark the try range.  This
7634   // can be used to detect deletion of the invoke via the MachineModuleInfo.
7635   MCSymbol *EndLabel = MMI.getContext().createTempSymbol();
7636   Chain = DAG.getEHLabel(getCurSDLoc(), Chain, EndLabel);
7637 
7638   // Inform MachineModuleInfo of range.
7639   auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
7640   // There is a platform (e.g. wasm) that uses funclet style IR but does not
7641   // actually use outlined funclets and their LSDA info style.
7642   if (MF.hasEHFunclets() && isFuncletEHPersonality(Pers)) {
7643     assert(II && "II should've been set");
7644     WinEHFuncInfo *EHInfo = MF.getWinEHFuncInfo();
7645     EHInfo->addIPToStateRange(II, BeginLabel, EndLabel);
7646   } else if (!isScopedEHPersonality(Pers)) {
7647     assert(EHPadBB);
7648     MF.addInvoke(FuncInfo.MBBMap[EHPadBB], BeginLabel, EndLabel);
7649   }
7650 
7651   return Chain;
7652 }
7653 
7654 std::pair<SDValue, SDValue>
7655 SelectionDAGBuilder::lowerInvokable(TargetLowering::CallLoweringInfo &CLI,
7656                                     const BasicBlock *EHPadBB) {
7657   MCSymbol *BeginLabel = nullptr;
7658 
7659   if (EHPadBB) {
7660     // Both PendingLoads and PendingExports must be flushed here;
7661     // this call might not return.
7662     (void)getRoot();
7663     DAG.setRoot(lowerStartEH(getControlRoot(), EHPadBB, BeginLabel));
7664     CLI.setChain(getRoot());
7665   }
7666 
7667   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7668   std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
7669 
7670   assert((CLI.IsTailCall || Result.second.getNode()) &&
7671          "Non-null chain expected with non-tail call!");
7672   assert((Result.second.getNode() || !Result.first.getNode()) &&
7673          "Null value expected with tail call!");
7674 
7675   if (!Result.second.getNode()) {
7676     // As a special case, a null chain means that a tail call has been emitted
7677     // and the DAG root is already updated.
7678     HasTailCall = true;
7679 
7680     // Since there's no actual continuation from this block, nothing can be
7681     // relying on us setting vregs for them.
7682     PendingExports.clear();
7683   } else {
7684     DAG.setRoot(Result.second);
7685   }
7686 
7687   if (EHPadBB) {
7688     DAG.setRoot(lowerEndEH(getRoot(), cast_or_null<InvokeInst>(CLI.CB), EHPadBB,
7689                            BeginLabel));
7690   }
7691 
7692   return Result;
7693 }
7694 
7695 void SelectionDAGBuilder::LowerCallTo(const CallBase &CB, SDValue Callee,
7696                                       bool isTailCall,
7697                                       bool isMustTailCall,
7698                                       const BasicBlock *EHPadBB) {
7699   auto &DL = DAG.getDataLayout();
7700   FunctionType *FTy = CB.getFunctionType();
7701   Type *RetTy = CB.getType();
7702 
7703   TargetLowering::ArgListTy Args;
7704   Args.reserve(CB.arg_size());
7705 
7706   const Value *SwiftErrorVal = nullptr;
7707   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7708 
7709   if (isTailCall) {
7710     // Avoid emitting tail calls in functions with the disable-tail-calls
7711     // attribute.
7712     auto *Caller = CB.getParent()->getParent();
7713     if (Caller->getFnAttribute("disable-tail-calls").getValueAsString() ==
7714         "true" && !isMustTailCall)
7715       isTailCall = false;
7716 
7717     // We can't tail call inside a function with a swifterror argument. Lowering
7718     // does not support this yet. It would have to move into the swifterror
7719     // register before the call.
7720     if (TLI.supportSwiftError() &&
7721         Caller->getAttributes().hasAttrSomewhere(Attribute::SwiftError))
7722       isTailCall = false;
7723   }
7724 
7725   for (auto I = CB.arg_begin(), E = CB.arg_end(); I != E; ++I) {
7726     TargetLowering::ArgListEntry Entry;
7727     const Value *V = *I;
7728 
7729     // Skip empty types
7730     if (V->getType()->isEmptyTy())
7731       continue;
7732 
7733     SDValue ArgNode = getValue(V);
7734     Entry.Node = ArgNode; Entry.Ty = V->getType();
7735 
7736     Entry.setAttributes(&CB, I - CB.arg_begin());
7737 
7738     // Use swifterror virtual register as input to the call.
7739     if (Entry.IsSwiftError && TLI.supportSwiftError()) {
7740       SwiftErrorVal = V;
7741       // We find the virtual register for the actual swifterror argument.
7742       // Instead of using the Value, we use the virtual register instead.
7743       Entry.Node =
7744           DAG.getRegister(SwiftError.getOrCreateVRegUseAt(&CB, FuncInfo.MBB, V),
7745                           EVT(TLI.getPointerTy(DL)));
7746     }
7747 
7748     Args.push_back(Entry);
7749 
7750     // If we have an explicit sret argument that is an Instruction, (i.e., it
7751     // might point to function-local memory), we can't meaningfully tail-call.
7752     if (Entry.IsSRet && isa<Instruction>(V))
7753       isTailCall = false;
7754   }
7755 
7756   // If call site has a cfguardtarget operand bundle, create and add an
7757   // additional ArgListEntry.
7758   if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_cfguardtarget)) {
7759     TargetLowering::ArgListEntry Entry;
7760     Value *V = Bundle->Inputs[0];
7761     SDValue ArgNode = getValue(V);
7762     Entry.Node = ArgNode;
7763     Entry.Ty = V->getType();
7764     Entry.IsCFGuardTarget = true;
7765     Args.push_back(Entry);
7766   }
7767 
7768   // Check if target-independent constraints permit a tail call here.
7769   // Target-dependent constraints are checked within TLI->LowerCallTo.
7770   if (isTailCall && !isInTailCallPosition(CB, DAG.getTarget()))
7771     isTailCall = false;
7772 
7773   // Disable tail calls if there is an swifterror argument. Targets have not
7774   // been updated to support tail calls.
7775   if (TLI.supportSwiftError() && SwiftErrorVal)
7776     isTailCall = false;
7777 
7778   TargetLowering::CallLoweringInfo CLI(DAG);
7779   CLI.setDebugLoc(getCurSDLoc())
7780       .setChain(getRoot())
7781       .setCallee(RetTy, FTy, Callee, std::move(Args), CB)
7782       .setTailCall(isTailCall)
7783       .setConvergent(CB.isConvergent())
7784       .setIsPreallocated(
7785           CB.countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0);
7786   std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
7787 
7788   if (Result.first.getNode()) {
7789     Result.first = lowerRangeToAssertZExt(DAG, CB, Result.first);
7790     setValue(&CB, Result.first);
7791   }
7792 
7793   // The last element of CLI.InVals has the SDValue for swifterror return.
7794   // Here we copy it to a virtual register and update SwiftErrorMap for
7795   // book-keeping.
7796   if (SwiftErrorVal && TLI.supportSwiftError()) {
7797     // Get the last element of InVals.
7798     SDValue Src = CLI.InVals.back();
7799     Register VReg =
7800         SwiftError.getOrCreateVRegDefAt(&CB, FuncInfo.MBB, SwiftErrorVal);
7801     SDValue CopyNode = CLI.DAG.getCopyToReg(Result.second, CLI.DL, VReg, Src);
7802     DAG.setRoot(CopyNode);
7803   }
7804 }
7805 
7806 static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
7807                              SelectionDAGBuilder &Builder) {
7808   // Check to see if this load can be trivially constant folded, e.g. if the
7809   // input is from a string literal.
7810   if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
7811     // Cast pointer to the type we really want to load.
7812     Type *LoadTy =
7813         Type::getIntNTy(PtrVal->getContext(), LoadVT.getScalarSizeInBits());
7814     if (LoadVT.isVector())
7815       LoadTy = FixedVectorType::get(LoadTy, LoadVT.getVectorNumElements());
7816 
7817     LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput),
7818                                          PointerType::getUnqual(LoadTy));
7819 
7820     if (const Constant *LoadCst =
7821             ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
7822                                          LoadTy, Builder.DAG.getDataLayout()))
7823       return Builder.getValue(LoadCst);
7824   }
7825 
7826   // Otherwise, we have to emit the load.  If the pointer is to unfoldable but
7827   // still constant memory, the input chain can be the entry node.
7828   SDValue Root;
7829   bool ConstantMemory = false;
7830 
7831   // Do not serialize (non-volatile) loads of constant memory with anything.
7832   if (Builder.AA && Builder.AA->pointsToConstantMemory(PtrVal)) {
7833     Root = Builder.DAG.getEntryNode();
7834     ConstantMemory = true;
7835   } else {
7836     // Do not serialize non-volatile loads against each other.
7837     Root = Builder.DAG.getRoot();
7838   }
7839 
7840   SDValue Ptr = Builder.getValue(PtrVal);
7841   SDValue LoadVal =
7842       Builder.DAG.getLoad(LoadVT, Builder.getCurSDLoc(), Root, Ptr,
7843                           MachinePointerInfo(PtrVal), Align(1));
7844 
7845   if (!ConstantMemory)
7846     Builder.PendingLoads.push_back(LoadVal.getValue(1));
7847   return LoadVal;
7848 }
7849 
7850 /// Record the value for an instruction that produces an integer result,
7851 /// converting the type where necessary.
7852 void SelectionDAGBuilder::processIntegerCallValue(const Instruction &I,
7853                                                   SDValue Value,
7854                                                   bool IsSigned) {
7855   EVT VT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
7856                                                     I.getType(), true);
7857   if (IsSigned)
7858     Value = DAG.getSExtOrTrunc(Value, getCurSDLoc(), VT);
7859   else
7860     Value = DAG.getZExtOrTrunc(Value, getCurSDLoc(), VT);
7861   setValue(&I, Value);
7862 }
7863 
7864 /// See if we can lower a memcmp/bcmp call into an optimized form. If so, return
7865 /// true and lower it. Otherwise return false, and it will be lowered like a
7866 /// normal call.
7867 /// The caller already checked that \p I calls the appropriate LibFunc with a
7868 /// correct prototype.
7869 bool SelectionDAGBuilder::visitMemCmpBCmpCall(const CallInst &I) {
7870   const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
7871   const Value *Size = I.getArgOperand(2);
7872   const ConstantInt *CSize = dyn_cast<ConstantInt>(Size);
7873   if (CSize && CSize->getZExtValue() == 0) {
7874     EVT CallVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
7875                                                           I.getType(), true);
7876     setValue(&I, DAG.getConstant(0, getCurSDLoc(), CallVT));
7877     return true;
7878   }
7879 
7880   const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
7881   std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForMemcmp(
7882       DAG, getCurSDLoc(), DAG.getRoot(), getValue(LHS), getValue(RHS),
7883       getValue(Size), MachinePointerInfo(LHS), MachinePointerInfo(RHS));
7884   if (Res.first.getNode()) {
7885     processIntegerCallValue(I, Res.first, true);
7886     PendingLoads.push_back(Res.second);
7887     return true;
7888   }
7889 
7890   // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS)  != 0
7891   // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS)  != 0
7892   if (!CSize || !isOnlyUsedInZeroEqualityComparison(&I))
7893     return false;
7894 
7895   // If the target has a fast compare for the given size, it will return a
7896   // preferred load type for that size. Require that the load VT is legal and
7897   // that the target supports unaligned loads of that type. Otherwise, return
7898   // INVALID.
7899   auto hasFastLoadsAndCompare = [&](unsigned NumBits) {
7900     const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7901     MVT LVT = TLI.hasFastEqualityCompare(NumBits);
7902     if (LVT != MVT::INVALID_SIMPLE_VALUE_TYPE) {
7903       // TODO: Handle 5 byte compare as 4-byte + 1 byte.
7904       // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
7905       // TODO: Check alignment of src and dest ptrs.
7906       unsigned DstAS = LHS->getType()->getPointerAddressSpace();
7907       unsigned SrcAS = RHS->getType()->getPointerAddressSpace();
7908       if (!TLI.isTypeLegal(LVT) ||
7909           !TLI.allowsMisalignedMemoryAccesses(LVT, SrcAS) ||
7910           !TLI.allowsMisalignedMemoryAccesses(LVT, DstAS))
7911         LVT = MVT::INVALID_SIMPLE_VALUE_TYPE;
7912     }
7913 
7914     return LVT;
7915   };
7916 
7917   // This turns into unaligned loads. We only do this if the target natively
7918   // supports the MVT we'll be loading or if it is small enough (<= 4) that
7919   // we'll only produce a small number of byte loads.
7920   MVT LoadVT;
7921   unsigned NumBitsToCompare = CSize->getZExtValue() * 8;
7922   switch (NumBitsToCompare) {
7923   default:
7924     return false;
7925   case 16:
7926     LoadVT = MVT::i16;
7927     break;
7928   case 32:
7929     LoadVT = MVT::i32;
7930     break;
7931   case 64:
7932   case 128:
7933   case 256:
7934     LoadVT = hasFastLoadsAndCompare(NumBitsToCompare);
7935     break;
7936   }
7937 
7938   if (LoadVT == MVT::INVALID_SIMPLE_VALUE_TYPE)
7939     return false;
7940 
7941   SDValue LoadL = getMemCmpLoad(LHS, LoadVT, *this);
7942   SDValue LoadR = getMemCmpLoad(RHS, LoadVT, *this);
7943 
7944   // Bitcast to a wide integer type if the loads are vectors.
7945   if (LoadVT.isVector()) {
7946     EVT CmpVT = EVT::getIntegerVT(LHS->getContext(), LoadVT.getSizeInBits());
7947     LoadL = DAG.getBitcast(CmpVT, LoadL);
7948     LoadR = DAG.getBitcast(CmpVT, LoadR);
7949   }
7950 
7951   SDValue Cmp = DAG.getSetCC(getCurSDLoc(), MVT::i1, LoadL, LoadR, ISD::SETNE);
7952   processIntegerCallValue(I, Cmp, false);
7953   return true;
7954 }
7955 
7956 /// See if we can lower a memchr call into an optimized form. If so, return
7957 /// true and lower it. Otherwise return false, and it will be lowered like a
7958 /// normal call.
7959 /// The caller already checked that \p I calls the appropriate LibFunc with a
7960 /// correct prototype.
7961 bool SelectionDAGBuilder::visitMemChrCall(const CallInst &I) {
7962   const Value *Src = I.getArgOperand(0);
7963   const Value *Char = I.getArgOperand(1);
7964   const Value *Length = I.getArgOperand(2);
7965 
7966   const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
7967   std::pair<SDValue, SDValue> Res =
7968     TSI.EmitTargetCodeForMemchr(DAG, getCurSDLoc(), DAG.getRoot(),
7969                                 getValue(Src), getValue(Char), getValue(Length),
7970                                 MachinePointerInfo(Src));
7971   if (Res.first.getNode()) {
7972     setValue(&I, Res.first);
7973     PendingLoads.push_back(Res.second);
7974     return true;
7975   }
7976 
7977   return false;
7978 }
7979 
7980 /// See if we can lower a mempcpy call into an optimized form. If so, return
7981 /// true and lower it. Otherwise return false, and it will be lowered like a
7982 /// normal call.
7983 /// The caller already checked that \p I calls the appropriate LibFunc with a
7984 /// correct prototype.
7985 bool SelectionDAGBuilder::visitMemPCpyCall(const CallInst &I) {
7986   SDValue Dst = getValue(I.getArgOperand(0));
7987   SDValue Src = getValue(I.getArgOperand(1));
7988   SDValue Size = getValue(I.getArgOperand(2));
7989 
7990   Align DstAlign = DAG.InferPtrAlign(Dst).valueOrOne();
7991   Align SrcAlign = DAG.InferPtrAlign(Src).valueOrOne();
7992   // DAG::getMemcpy needs Alignment to be defined.
7993   Align Alignment = std::min(DstAlign, SrcAlign);
7994 
7995   bool isVol = false;
7996   SDLoc sdl = getCurSDLoc();
7997 
7998   // In the mempcpy context we need to pass in a false value for isTailCall
7999   // because the return pointer needs to be adjusted by the size of
8000   // the copied memory.
8001   SDValue Root = isVol ? getRoot() : getMemoryRoot();
8002   SDValue MC = DAG.getMemcpy(Root, sdl, Dst, Src, Size, Alignment, isVol, false,
8003                              /*isTailCall=*/false,
8004                              MachinePointerInfo(I.getArgOperand(0)),
8005                              MachinePointerInfo(I.getArgOperand(1)),
8006                              I.getAAMetadata());
8007   assert(MC.getNode() != nullptr &&
8008          "** memcpy should not be lowered as TailCall in mempcpy context **");
8009   DAG.setRoot(MC);
8010 
8011   // Check if Size needs to be truncated or extended.
8012   Size = DAG.getSExtOrTrunc(Size, sdl, Dst.getValueType());
8013 
8014   // Adjust return pointer to point just past the last dst byte.
8015   SDValue DstPlusSize = DAG.getNode(ISD::ADD, sdl, Dst.getValueType(),
8016                                     Dst, Size);
8017   setValue(&I, DstPlusSize);
8018   return true;
8019 }
8020 
8021 /// See if we can lower a strcpy call into an optimized form.  If so, return
8022 /// true and lower it, otherwise return false and it will be lowered like a
8023 /// normal call.
8024 /// The caller already checked that \p I calls the appropriate LibFunc with a
8025 /// correct prototype.
8026 bool SelectionDAGBuilder::visitStrCpyCall(const CallInst &I, bool isStpcpy) {
8027   const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
8028 
8029   const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
8030   std::pair<SDValue, SDValue> Res =
8031     TSI.EmitTargetCodeForStrcpy(DAG, getCurSDLoc(), getRoot(),
8032                                 getValue(Arg0), getValue(Arg1),
8033                                 MachinePointerInfo(Arg0),
8034                                 MachinePointerInfo(Arg1), isStpcpy);
8035   if (Res.first.getNode()) {
8036     setValue(&I, Res.first);
8037     DAG.setRoot(Res.second);
8038     return true;
8039   }
8040 
8041   return false;
8042 }
8043 
8044 /// See if we can lower a strcmp call into an optimized form.  If so, return
8045 /// true and lower it, otherwise return false and it will be lowered like a
8046 /// normal call.
8047 /// The caller already checked that \p I calls the appropriate LibFunc with a
8048 /// correct prototype.
8049 bool SelectionDAGBuilder::visitStrCmpCall(const CallInst &I) {
8050   const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
8051 
8052   const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
8053   std::pair<SDValue, SDValue> Res =
8054     TSI.EmitTargetCodeForStrcmp(DAG, getCurSDLoc(), DAG.getRoot(),
8055                                 getValue(Arg0), getValue(Arg1),
8056                                 MachinePointerInfo(Arg0),
8057                                 MachinePointerInfo(Arg1));
8058   if (Res.first.getNode()) {
8059     processIntegerCallValue(I, Res.first, true);
8060     PendingLoads.push_back(Res.second);
8061     return true;
8062   }
8063 
8064   return false;
8065 }
8066 
8067 /// See if we can lower a strlen call into an optimized form.  If so, return
8068 /// true and lower it, otherwise return false and it will be lowered like a
8069 /// normal call.
8070 /// The caller already checked that \p I calls the appropriate LibFunc with a
8071 /// correct prototype.
8072 bool SelectionDAGBuilder::visitStrLenCall(const CallInst &I) {
8073   const Value *Arg0 = I.getArgOperand(0);
8074 
8075   const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
8076   std::pair<SDValue, SDValue> Res =
8077     TSI.EmitTargetCodeForStrlen(DAG, getCurSDLoc(), DAG.getRoot(),
8078                                 getValue(Arg0), MachinePointerInfo(Arg0));
8079   if (Res.first.getNode()) {
8080     processIntegerCallValue(I, Res.first, false);
8081     PendingLoads.push_back(Res.second);
8082     return true;
8083   }
8084 
8085   return false;
8086 }
8087 
8088 /// See if we can lower a strnlen call into an optimized form.  If so, return
8089 /// true and lower it, otherwise return false and it will be lowered like a
8090 /// normal call.
8091 /// The caller already checked that \p I calls the appropriate LibFunc with a
8092 /// correct prototype.
8093 bool SelectionDAGBuilder::visitStrNLenCall(const CallInst &I) {
8094   const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
8095 
8096   const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
8097   std::pair<SDValue, SDValue> Res =
8098     TSI.EmitTargetCodeForStrnlen(DAG, getCurSDLoc(), DAG.getRoot(),
8099                                  getValue(Arg0), getValue(Arg1),
8100                                  MachinePointerInfo(Arg0));
8101   if (Res.first.getNode()) {
8102     processIntegerCallValue(I, Res.first, false);
8103     PendingLoads.push_back(Res.second);
8104     return true;
8105   }
8106 
8107   return false;
8108 }
8109 
8110 /// See if we can lower a unary floating-point operation into an SDNode with
8111 /// the specified Opcode.  If so, return true and lower it, otherwise return
8112 /// false and it will be lowered like a normal call.
8113 /// The caller already checked that \p I calls the appropriate LibFunc with a
8114 /// correct prototype.
8115 bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I,
8116                                               unsigned Opcode) {
8117   // We already checked this call's prototype; verify it doesn't modify errno.
8118   if (!I.onlyReadsMemory())
8119     return false;
8120 
8121   SDNodeFlags Flags;
8122   Flags.copyFMF(cast<FPMathOperator>(I));
8123 
8124   SDValue Tmp = getValue(I.getArgOperand(0));
8125   setValue(&I,
8126            DAG.getNode(Opcode, getCurSDLoc(), Tmp.getValueType(), Tmp, Flags));
8127   return true;
8128 }
8129 
8130 /// See if we can lower a binary floating-point operation into an SDNode with
8131 /// the specified Opcode. If so, return true and lower it. Otherwise return
8132 /// false, and it will be lowered like a normal call.
8133 /// The caller already checked that \p I calls the appropriate LibFunc with a
8134 /// correct prototype.
8135 bool SelectionDAGBuilder::visitBinaryFloatCall(const CallInst &I,
8136                                                unsigned Opcode) {
8137   // We already checked this call's prototype; verify it doesn't modify errno.
8138   if (!I.onlyReadsMemory())
8139     return false;
8140 
8141   SDNodeFlags Flags;
8142   Flags.copyFMF(cast<FPMathOperator>(I));
8143 
8144   SDValue Tmp0 = getValue(I.getArgOperand(0));
8145   SDValue Tmp1 = getValue(I.getArgOperand(1));
8146   EVT VT = Tmp0.getValueType();
8147   setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), VT, Tmp0, Tmp1, Flags));
8148   return true;
8149 }
8150 
8151 void SelectionDAGBuilder::visitCall(const CallInst &I) {
8152   // Handle inline assembly differently.
8153   if (I.isInlineAsm()) {
8154     visitInlineAsm(I);
8155     return;
8156   }
8157 
8158   if (Function *F = I.getCalledFunction()) {
8159     diagnoseDontCall(I);
8160 
8161     if (F->isDeclaration()) {
8162       // Is this an LLVM intrinsic or a target-specific intrinsic?
8163       unsigned IID = F->getIntrinsicID();
8164       if (!IID)
8165         if (const TargetIntrinsicInfo *II = TM.getIntrinsicInfo())
8166           IID = II->getIntrinsicID(F);
8167 
8168       if (IID) {
8169         visitIntrinsicCall(I, IID);
8170         return;
8171       }
8172     }
8173 
8174     // Check for well-known libc/libm calls.  If the function is internal, it
8175     // can't be a library call.  Don't do the check if marked as nobuiltin for
8176     // some reason or the call site requires strict floating point semantics.
8177     LibFunc Func;
8178     if (!I.isNoBuiltin() && !I.isStrictFP() && !F->hasLocalLinkage() &&
8179         F->hasName() && LibInfo->getLibFunc(*F, Func) &&
8180         LibInfo->hasOptimizedCodeGen(Func)) {
8181       switch (Func) {
8182       default: break;
8183       case LibFunc_bcmp:
8184         if (visitMemCmpBCmpCall(I))
8185           return;
8186         break;
8187       case LibFunc_copysign:
8188       case LibFunc_copysignf:
8189       case LibFunc_copysignl:
8190         // We already checked this call's prototype; verify it doesn't modify
8191         // errno.
8192         if (I.onlyReadsMemory()) {
8193           SDValue LHS = getValue(I.getArgOperand(0));
8194           SDValue RHS = getValue(I.getArgOperand(1));
8195           setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurSDLoc(),
8196                                    LHS.getValueType(), LHS, RHS));
8197           return;
8198         }
8199         break;
8200       case LibFunc_fabs:
8201       case LibFunc_fabsf:
8202       case LibFunc_fabsl:
8203         if (visitUnaryFloatCall(I, ISD::FABS))
8204           return;
8205         break;
8206       case LibFunc_fmin:
8207       case LibFunc_fminf:
8208       case LibFunc_fminl:
8209         if (visitBinaryFloatCall(I, ISD::FMINNUM))
8210           return;
8211         break;
8212       case LibFunc_fmax:
8213       case LibFunc_fmaxf:
8214       case LibFunc_fmaxl:
8215         if (visitBinaryFloatCall(I, ISD::FMAXNUM))
8216           return;
8217         break;
8218       case LibFunc_sin:
8219       case LibFunc_sinf:
8220       case LibFunc_sinl:
8221         if (visitUnaryFloatCall(I, ISD::FSIN))
8222           return;
8223         break;
8224       case LibFunc_cos:
8225       case LibFunc_cosf:
8226       case LibFunc_cosl:
8227         if (visitUnaryFloatCall(I, ISD::FCOS))
8228           return;
8229         break;
8230       case LibFunc_sqrt:
8231       case LibFunc_sqrtf:
8232       case LibFunc_sqrtl:
8233       case LibFunc_sqrt_finite:
8234       case LibFunc_sqrtf_finite:
8235       case LibFunc_sqrtl_finite:
8236         if (visitUnaryFloatCall(I, ISD::FSQRT))
8237           return;
8238         break;
8239       case LibFunc_floor:
8240       case LibFunc_floorf:
8241       case LibFunc_floorl:
8242         if (visitUnaryFloatCall(I, ISD::FFLOOR))
8243           return;
8244         break;
8245       case LibFunc_nearbyint:
8246       case LibFunc_nearbyintf:
8247       case LibFunc_nearbyintl:
8248         if (visitUnaryFloatCall(I, ISD::FNEARBYINT))
8249           return;
8250         break;
8251       case LibFunc_ceil:
8252       case LibFunc_ceilf:
8253       case LibFunc_ceill:
8254         if (visitUnaryFloatCall(I, ISD::FCEIL))
8255           return;
8256         break;
8257       case LibFunc_rint:
8258       case LibFunc_rintf:
8259       case LibFunc_rintl:
8260         if (visitUnaryFloatCall(I, ISD::FRINT))
8261           return;
8262         break;
8263       case LibFunc_round:
8264       case LibFunc_roundf:
8265       case LibFunc_roundl:
8266         if (visitUnaryFloatCall(I, ISD::FROUND))
8267           return;
8268         break;
8269       case LibFunc_trunc:
8270       case LibFunc_truncf:
8271       case LibFunc_truncl:
8272         if (visitUnaryFloatCall(I, ISD::FTRUNC))
8273           return;
8274         break;
8275       case LibFunc_log2:
8276       case LibFunc_log2f:
8277       case LibFunc_log2l:
8278         if (visitUnaryFloatCall(I, ISD::FLOG2))
8279           return;
8280         break;
8281       case LibFunc_exp2:
8282       case LibFunc_exp2f:
8283       case LibFunc_exp2l:
8284         if (visitUnaryFloatCall(I, ISD::FEXP2))
8285           return;
8286         break;
8287       case LibFunc_memcmp:
8288         if (visitMemCmpBCmpCall(I))
8289           return;
8290         break;
8291       case LibFunc_mempcpy:
8292         if (visitMemPCpyCall(I))
8293           return;
8294         break;
8295       case LibFunc_memchr:
8296         if (visitMemChrCall(I))
8297           return;
8298         break;
8299       case LibFunc_strcpy:
8300         if (visitStrCpyCall(I, false))
8301           return;
8302         break;
8303       case LibFunc_stpcpy:
8304         if (visitStrCpyCall(I, true))
8305           return;
8306         break;
8307       case LibFunc_strcmp:
8308         if (visitStrCmpCall(I))
8309           return;
8310         break;
8311       case LibFunc_strlen:
8312         if (visitStrLenCall(I))
8313           return;
8314         break;
8315       case LibFunc_strnlen:
8316         if (visitStrNLenCall(I))
8317           return;
8318         break;
8319       }
8320     }
8321   }
8322 
8323   // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't
8324   // have to do anything here to lower funclet bundles.
8325   // CFGuardTarget bundles are lowered in LowerCallTo.
8326   assert(!I.hasOperandBundlesOtherThan(
8327              {LLVMContext::OB_deopt, LLVMContext::OB_funclet,
8328               LLVMContext::OB_cfguardtarget, LLVMContext::OB_preallocated,
8329               LLVMContext::OB_clang_arc_attachedcall}) &&
8330          "Cannot lower calls with arbitrary operand bundles!");
8331 
8332   SDValue Callee = getValue(I.getCalledOperand());
8333 
8334   if (I.countOperandBundlesOfType(LLVMContext::OB_deopt))
8335     LowerCallSiteWithDeoptBundle(&I, Callee, nullptr);
8336   else
8337     // Check if we can potentially perform a tail call. More detailed checking
8338     // is be done within LowerCallTo, after more information about the call is
8339     // known.
8340     LowerCallTo(I, Callee, I.isTailCall(), I.isMustTailCall());
8341 }
8342 
8343 namespace {
8344 
8345 /// AsmOperandInfo - This contains information for each constraint that we are
8346 /// lowering.
8347 class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo {
8348 public:
8349   /// CallOperand - If this is the result output operand or a clobber
8350   /// this is null, otherwise it is the incoming operand to the CallInst.
8351   /// This gets modified as the asm is processed.
8352   SDValue CallOperand;
8353 
8354   /// AssignedRegs - If this is a register or register class operand, this
8355   /// contains the set of register corresponding to the operand.
8356   RegsForValue AssignedRegs;
8357 
8358   explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info)
8359     : TargetLowering::AsmOperandInfo(info), CallOperand(nullptr, 0) {
8360   }
8361 
8362   /// Whether or not this operand accesses memory
8363   bool hasMemory(const TargetLowering &TLI) const {
8364     // Indirect operand accesses access memory.
8365     if (isIndirect)
8366       return true;
8367 
8368     for (const auto &Code : Codes)
8369       if (TLI.getConstraintType(Code) == TargetLowering::C_Memory)
8370         return true;
8371 
8372     return false;
8373   }
8374 
8375   /// getCallOperandValEVT - Return the EVT of the Value* that this operand
8376   /// corresponds to.  If there is no Value* for this operand, it returns
8377   /// MVT::Other.
8378   EVT getCallOperandValEVT(LLVMContext &Context, const TargetLowering &TLI,
8379                            const DataLayout &DL,
8380                            llvm::Type *ParamElemType) const {
8381     if (!CallOperandVal) return MVT::Other;
8382 
8383     if (isa<BasicBlock>(CallOperandVal))
8384       return TLI.getProgramPointerTy(DL);
8385 
8386     llvm::Type *OpTy = CallOperandVal->getType();
8387 
8388     // FIXME: code duplicated from TargetLowering::ParseConstraints().
8389     // If this is an indirect operand, the operand is a pointer to the
8390     // accessed type.
8391     if (isIndirect) {
8392       OpTy = ParamElemType;
8393       assert(OpTy && "Indirect operand must have elementtype attribute");
8394     }
8395 
8396     // Look for vector wrapped in a struct. e.g. { <16 x i8> }.
8397     if (StructType *STy = dyn_cast<StructType>(OpTy))
8398       if (STy->getNumElements() == 1)
8399         OpTy = STy->getElementType(0);
8400 
8401     // If OpTy is not a single value, it may be a struct/union that we
8402     // can tile with integers.
8403     if (!OpTy->isSingleValueType() && OpTy->isSized()) {
8404       unsigned BitSize = DL.getTypeSizeInBits(OpTy);
8405       switch (BitSize) {
8406       default: break;
8407       case 1:
8408       case 8:
8409       case 16:
8410       case 32:
8411       case 64:
8412       case 128:
8413         OpTy = IntegerType::get(Context, BitSize);
8414         break;
8415       }
8416     }
8417 
8418     return TLI.getAsmOperandValueType(DL, OpTy, true);
8419   }
8420 };
8421 
8422 
8423 } // end anonymous namespace
8424 
8425 /// Make sure that the output operand \p OpInfo and its corresponding input
8426 /// operand \p MatchingOpInfo have compatible constraint types (otherwise error
8427 /// out).
8428 static void patchMatchingInput(const SDISelAsmOperandInfo &OpInfo,
8429                                SDISelAsmOperandInfo &MatchingOpInfo,
8430                                SelectionDAG &DAG) {
8431   if (OpInfo.ConstraintVT == MatchingOpInfo.ConstraintVT)
8432     return;
8433 
8434   const TargetRegisterInfo *TRI = DAG.getSubtarget().getRegisterInfo();
8435   const auto &TLI = DAG.getTargetLoweringInfo();
8436 
8437   std::pair<unsigned, const TargetRegisterClass *> MatchRC =
8438       TLI.getRegForInlineAsmConstraint(TRI, OpInfo.ConstraintCode,
8439                                        OpInfo.ConstraintVT);
8440   std::pair<unsigned, const TargetRegisterClass *> InputRC =
8441       TLI.getRegForInlineAsmConstraint(TRI, MatchingOpInfo.ConstraintCode,
8442                                        MatchingOpInfo.ConstraintVT);
8443   if ((OpInfo.ConstraintVT.isInteger() !=
8444        MatchingOpInfo.ConstraintVT.isInteger()) ||
8445       (MatchRC.second != InputRC.second)) {
8446     // FIXME: error out in a more elegant fashion
8447     report_fatal_error("Unsupported asm: input constraint"
8448                        " with a matching output constraint of"
8449                        " incompatible type!");
8450   }
8451   MatchingOpInfo.ConstraintVT = OpInfo.ConstraintVT;
8452 }
8453 
8454 /// Get a direct memory input to behave well as an indirect operand.
8455 /// This may introduce stores, hence the need for a \p Chain.
8456 /// \return The (possibly updated) chain.
8457 static SDValue getAddressForMemoryInput(SDValue Chain, const SDLoc &Location,
8458                                         SDISelAsmOperandInfo &OpInfo,
8459                                         SelectionDAG &DAG) {
8460   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8461 
8462   // If we don't have an indirect input, put it in the constpool if we can,
8463   // otherwise spill it to a stack slot.
8464   // TODO: This isn't quite right. We need to handle these according to
8465   // the addressing mode that the constraint wants. Also, this may take
8466   // an additional register for the computation and we don't want that
8467   // either.
8468 
8469   // If the operand is a float, integer, or vector constant, spill to a
8470   // constant pool entry to get its address.
8471   const Value *OpVal = OpInfo.CallOperandVal;
8472   if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
8473       isa<ConstantVector>(OpVal) || isa<ConstantDataVector>(OpVal)) {
8474     OpInfo.CallOperand = DAG.getConstantPool(
8475         cast<Constant>(OpVal), TLI.getPointerTy(DAG.getDataLayout()));
8476     return Chain;
8477   }
8478 
8479   // Otherwise, create a stack slot and emit a store to it before the asm.
8480   Type *Ty = OpVal->getType();
8481   auto &DL = DAG.getDataLayout();
8482   uint64_t TySize = DL.getTypeAllocSize(Ty);
8483   MachineFunction &MF = DAG.getMachineFunction();
8484   int SSFI = MF.getFrameInfo().CreateStackObject(
8485       TySize, DL.getPrefTypeAlign(Ty), false);
8486   SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getFrameIndexTy(DL));
8487   Chain = DAG.getTruncStore(Chain, Location, OpInfo.CallOperand, StackSlot,
8488                             MachinePointerInfo::getFixedStack(MF, SSFI),
8489                             TLI.getMemValueType(DL, Ty));
8490   OpInfo.CallOperand = StackSlot;
8491 
8492   return Chain;
8493 }
8494 
8495 /// GetRegistersForValue - Assign registers (virtual or physical) for the
8496 /// specified operand.  We prefer to assign virtual registers, to allow the
8497 /// register allocator to handle the assignment process.  However, if the asm
8498 /// uses features that we can't model on machineinstrs, we have SDISel do the
8499 /// allocation.  This produces generally horrible, but correct, code.
8500 ///
8501 ///   OpInfo describes the operand
8502 ///   RefOpInfo describes the matching operand if any, the operand otherwise
8503 static llvm::Optional<unsigned>
8504 getRegistersForValue(SelectionDAG &DAG, const SDLoc &DL,
8505                      SDISelAsmOperandInfo &OpInfo,
8506                      SDISelAsmOperandInfo &RefOpInfo) {
8507   LLVMContext &Context = *DAG.getContext();
8508   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8509 
8510   MachineFunction &MF = DAG.getMachineFunction();
8511   SmallVector<unsigned, 4> Regs;
8512   const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
8513 
8514   // No work to do for memory/address operands.
8515   if (OpInfo.ConstraintType == TargetLowering::C_Memory ||
8516       OpInfo.ConstraintType == TargetLowering::C_Address)
8517     return None;
8518 
8519   // If this is a constraint for a single physreg, or a constraint for a
8520   // register class, find it.
8521   unsigned AssignedReg;
8522   const TargetRegisterClass *RC;
8523   std::tie(AssignedReg, RC) = TLI.getRegForInlineAsmConstraint(
8524       &TRI, RefOpInfo.ConstraintCode, RefOpInfo.ConstraintVT);
8525   // RC is unset only on failure. Return immediately.
8526   if (!RC)
8527     return None;
8528 
8529   // Get the actual register value type.  This is important, because the user
8530   // may have asked for (e.g.) the AX register in i32 type.  We need to
8531   // remember that AX is actually i16 to get the right extension.
8532   const MVT RegVT = *TRI.legalclasstypes_begin(*RC);
8533 
8534   if (OpInfo.ConstraintVT != MVT::Other && RegVT != MVT::Untyped) {
8535     // If this is an FP operand in an integer register (or visa versa), or more
8536     // generally if the operand value disagrees with the register class we plan
8537     // to stick it in, fix the operand type.
8538     //
8539     // If this is an input value, the bitcast to the new type is done now.
8540     // Bitcast for output value is done at the end of visitInlineAsm().
8541     if ((OpInfo.Type == InlineAsm::isOutput ||
8542          OpInfo.Type == InlineAsm::isInput) &&
8543         !TRI.isTypeLegalForClass(*RC, OpInfo.ConstraintVT)) {
8544       // Try to convert to the first EVT that the reg class contains.  If the
8545       // types are identical size, use a bitcast to convert (e.g. two differing
8546       // vector types).  Note: output bitcast is done at the end of
8547       // visitInlineAsm().
8548       if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
8549         // Exclude indirect inputs while they are unsupported because the code
8550         // to perform the load is missing and thus OpInfo.CallOperand still
8551         // refers to the input address rather than the pointed-to value.
8552         if (OpInfo.Type == InlineAsm::isInput && !OpInfo.isIndirect)
8553           OpInfo.CallOperand =
8554               DAG.getNode(ISD::BITCAST, DL, RegVT, OpInfo.CallOperand);
8555         OpInfo.ConstraintVT = RegVT;
8556         // If the operand is an FP value and we want it in integer registers,
8557         // use the corresponding integer type. This turns an f64 value into
8558         // i64, which can be passed with two i32 values on a 32-bit machine.
8559       } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
8560         MVT VT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits());
8561         if (OpInfo.Type == InlineAsm::isInput)
8562           OpInfo.CallOperand =
8563               DAG.getNode(ISD::BITCAST, DL, VT, OpInfo.CallOperand);
8564         OpInfo.ConstraintVT = VT;
8565       }
8566     }
8567   }
8568 
8569   // No need to allocate a matching input constraint since the constraint it's
8570   // matching to has already been allocated.
8571   if (OpInfo.isMatchingInputConstraint())
8572     return None;
8573 
8574   EVT ValueVT = OpInfo.ConstraintVT;
8575   if (OpInfo.ConstraintVT == MVT::Other)
8576     ValueVT = RegVT;
8577 
8578   // Initialize NumRegs.
8579   unsigned NumRegs = 1;
8580   if (OpInfo.ConstraintVT != MVT::Other)
8581     NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT, RegVT);
8582 
8583   // If this is a constraint for a specific physical register, like {r17},
8584   // assign it now.
8585 
8586   // If this associated to a specific register, initialize iterator to correct
8587   // place. If virtual, make sure we have enough registers
8588 
8589   // Initialize iterator if necessary
8590   TargetRegisterClass::iterator I = RC->begin();
8591   MachineRegisterInfo &RegInfo = MF.getRegInfo();
8592 
8593   // Do not check for single registers.
8594   if (AssignedReg) {
8595     I = std::find(I, RC->end(), AssignedReg);
8596     if (I == RC->end()) {
8597       // RC does not contain the selected register, which indicates a
8598       // mismatch between the register and the required type/bitwidth.
8599       return {AssignedReg};
8600     }
8601   }
8602 
8603   for (; NumRegs; --NumRegs, ++I) {
8604     assert(I != RC->end() && "Ran out of registers to allocate!");
8605     Register R = AssignedReg ? Register(*I) : RegInfo.createVirtualRegister(RC);
8606     Regs.push_back(R);
8607   }
8608 
8609   OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
8610   return None;
8611 }
8612 
8613 static unsigned
8614 findMatchingInlineAsmOperand(unsigned OperandNo,
8615                              const std::vector<SDValue> &AsmNodeOperands) {
8616   // Scan until we find the definition we already emitted of this operand.
8617   unsigned CurOp = InlineAsm::Op_FirstOperand;
8618   for (; OperandNo; --OperandNo) {
8619     // Advance to the next operand.
8620     unsigned OpFlag =
8621         cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
8622     assert((InlineAsm::isRegDefKind(OpFlag) ||
8623             InlineAsm::isRegDefEarlyClobberKind(OpFlag) ||
8624             InlineAsm::isMemKind(OpFlag)) &&
8625            "Skipped past definitions?");
8626     CurOp += InlineAsm::getNumOperandRegisters(OpFlag) + 1;
8627   }
8628   return CurOp;
8629 }
8630 
8631 namespace {
8632 
8633 class ExtraFlags {
8634   unsigned Flags = 0;
8635 
8636 public:
8637   explicit ExtraFlags(const CallBase &Call) {
8638     const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
8639     if (IA->hasSideEffects())
8640       Flags |= InlineAsm::Extra_HasSideEffects;
8641     if (IA->isAlignStack())
8642       Flags |= InlineAsm::Extra_IsAlignStack;
8643     if (Call.isConvergent())
8644       Flags |= InlineAsm::Extra_IsConvergent;
8645     Flags |= IA->getDialect() * InlineAsm::Extra_AsmDialect;
8646   }
8647 
8648   void update(const TargetLowering::AsmOperandInfo &OpInfo) {
8649     // Ideally, we would only check against memory constraints.  However, the
8650     // meaning of an Other constraint can be target-specific and we can't easily
8651     // reason about it.  Therefore, be conservative and set MayLoad/MayStore
8652     // for Other constraints as well.
8653     if (OpInfo.ConstraintType == TargetLowering::C_Memory ||
8654         OpInfo.ConstraintType == TargetLowering::C_Other) {
8655       if (OpInfo.Type == InlineAsm::isInput)
8656         Flags |= InlineAsm::Extra_MayLoad;
8657       else if (OpInfo.Type == InlineAsm::isOutput)
8658         Flags |= InlineAsm::Extra_MayStore;
8659       else if (OpInfo.Type == InlineAsm::isClobber)
8660         Flags |= (InlineAsm::Extra_MayLoad | InlineAsm::Extra_MayStore);
8661     }
8662   }
8663 
8664   unsigned get() const { return Flags; }
8665 };
8666 
8667 } // end anonymous namespace
8668 
8669 /// visitInlineAsm - Handle a call to an InlineAsm object.
8670 void SelectionDAGBuilder::visitInlineAsm(const CallBase &Call,
8671                                          const BasicBlock *EHPadBB) {
8672   const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
8673 
8674   /// ConstraintOperands - Information about all of the constraints.
8675   SmallVector<SDISelAsmOperandInfo, 16> ConstraintOperands;
8676 
8677   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8678   TargetLowering::AsmOperandInfoVector TargetConstraints = TLI.ParseConstraints(
8679       DAG.getDataLayout(), DAG.getSubtarget().getRegisterInfo(), Call);
8680 
8681   // First Pass: Calculate HasSideEffects and ExtraFlags (AlignStack,
8682   // AsmDialect, MayLoad, MayStore).
8683   bool HasSideEffect = IA->hasSideEffects();
8684   ExtraFlags ExtraInfo(Call);
8685 
8686   unsigned ArgNo = 0;   // ArgNo - The argument of the CallInst.
8687   unsigned ResNo = 0;   // ResNo - The result number of the next output.
8688   for (auto &T : TargetConstraints) {
8689     ConstraintOperands.push_back(SDISelAsmOperandInfo(T));
8690     SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
8691 
8692     // Compute the value type for each operand.
8693     if (OpInfo.hasArg()) {
8694       OpInfo.CallOperandVal = Call.getArgOperand(ArgNo);
8695       OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
8696       Type *ParamElemTy = Call.getParamElementType(ArgNo);
8697       EVT VT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI,
8698                                            DAG.getDataLayout(), ParamElemTy);
8699       OpInfo.ConstraintVT = VT.isSimple() ? VT.getSimpleVT() : MVT::Other;
8700       ArgNo++;
8701     } else if (OpInfo.Type == InlineAsm::isOutput && !OpInfo.isIndirect) {
8702       // The return value of the call is this value.  As such, there is no
8703       // corresponding argument.
8704       assert(!Call.getType()->isVoidTy() && "Bad inline asm!");
8705       if (StructType *STy = dyn_cast<StructType>(Call.getType())) {
8706         OpInfo.ConstraintVT = TLI.getSimpleValueType(
8707             DAG.getDataLayout(), STy->getElementType(ResNo));
8708       } else {
8709         assert(ResNo == 0 && "Asm only has one result!");
8710         OpInfo.ConstraintVT = TLI.getAsmOperandValueType(
8711             DAG.getDataLayout(), Call.getType()).getSimpleVT();
8712       }
8713       ++ResNo;
8714     } else {
8715       OpInfo.ConstraintVT = MVT::Other;
8716     }
8717 
8718     if (!HasSideEffect)
8719       HasSideEffect = OpInfo.hasMemory(TLI);
8720 
8721     // Determine if this InlineAsm MayLoad or MayStore based on the constraints.
8722     // FIXME: Could we compute this on OpInfo rather than T?
8723 
8724     // Compute the constraint code and ConstraintType to use.
8725     TLI.ComputeConstraintToUse(T, SDValue());
8726 
8727     if (T.ConstraintType == TargetLowering::C_Immediate &&
8728         OpInfo.CallOperand && !isa<ConstantSDNode>(OpInfo.CallOperand))
8729       // We've delayed emitting a diagnostic like the "n" constraint because
8730       // inlining could cause an integer showing up.
8731       return emitInlineAsmError(Call, "constraint '" + Twine(T.ConstraintCode) +
8732                                           "' expects an integer constant "
8733                                           "expression");
8734 
8735     ExtraInfo.update(T);
8736   }
8737 
8738   // We won't need to flush pending loads if this asm doesn't touch
8739   // memory and is nonvolatile.
8740   SDValue Flag, Chain = (HasSideEffect) ? getRoot() : DAG.getRoot();
8741 
8742   bool EmitEHLabels = isa<InvokeInst>(Call) && IA->canThrow();
8743   if (EmitEHLabels) {
8744     assert(EHPadBB && "InvokeInst must have an EHPadBB");
8745   }
8746   bool IsCallBr = isa<CallBrInst>(Call);
8747 
8748   if (IsCallBr || EmitEHLabels) {
8749     // If this is a callbr or invoke we need to flush pending exports since
8750     // inlineasm_br and invoke are terminators.
8751     // We need to do this before nodes are glued to the inlineasm_br node.
8752     Chain = getControlRoot();
8753   }
8754 
8755   MCSymbol *BeginLabel = nullptr;
8756   if (EmitEHLabels) {
8757     Chain = lowerStartEH(Chain, EHPadBB, BeginLabel);
8758   }
8759 
8760   // Second pass over the constraints: compute which constraint option to use.
8761   for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
8762     // If this is an output operand with a matching input operand, look up the
8763     // matching input. If their types mismatch, e.g. one is an integer, the
8764     // other is floating point, or their sizes are different, flag it as an
8765     // error.
8766     if (OpInfo.hasMatchingInput()) {
8767       SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
8768       patchMatchingInput(OpInfo, Input, DAG);
8769     }
8770 
8771     // Compute the constraint code and ConstraintType to use.
8772     TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
8773 
8774     if ((OpInfo.ConstraintType == TargetLowering::C_Memory &&
8775          OpInfo.Type == InlineAsm::isClobber) ||
8776         OpInfo.ConstraintType == TargetLowering::C_Address)
8777       continue;
8778 
8779     // If this is a memory input, and if the operand is not indirect, do what we
8780     // need to provide an address for the memory input.
8781     if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
8782         !OpInfo.isIndirect) {
8783       assert((OpInfo.isMultipleAlternative ||
8784               (OpInfo.Type == InlineAsm::isInput)) &&
8785              "Can only indirectify direct input operands!");
8786 
8787       // Memory operands really want the address of the value.
8788       Chain = getAddressForMemoryInput(Chain, getCurSDLoc(), OpInfo, DAG);
8789 
8790       // There is no longer a Value* corresponding to this operand.
8791       OpInfo.CallOperandVal = nullptr;
8792 
8793       // It is now an indirect operand.
8794       OpInfo.isIndirect = true;
8795     }
8796 
8797   }
8798 
8799   // AsmNodeOperands - The operands for the ISD::INLINEASM node.
8800   std::vector<SDValue> AsmNodeOperands;
8801   AsmNodeOperands.push_back(SDValue());  // reserve space for input chain
8802   AsmNodeOperands.push_back(DAG.getTargetExternalSymbol(
8803       IA->getAsmString().c_str(), TLI.getProgramPointerTy(DAG.getDataLayout())));
8804 
8805   // If we have a !srcloc metadata node associated with it, we want to attach
8806   // this to the ultimately generated inline asm machineinstr.  To do this, we
8807   // pass in the third operand as this (potentially null) inline asm MDNode.
8808   const MDNode *SrcLoc = Call.getMetadata("srcloc");
8809   AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
8810 
8811   // Remember the HasSideEffect, AlignStack, AsmDialect, MayLoad and MayStore
8812   // bits as operand 3.
8813   AsmNodeOperands.push_back(DAG.getTargetConstant(
8814       ExtraInfo.get(), getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
8815 
8816   // Third pass: Loop over operands to prepare DAG-level operands.. As part of
8817   // this, assign virtual and physical registers for inputs and otput.
8818   for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
8819     // Assign Registers.
8820     SDISelAsmOperandInfo &RefOpInfo =
8821         OpInfo.isMatchingInputConstraint()
8822             ? ConstraintOperands[OpInfo.getMatchedOperand()]
8823             : OpInfo;
8824     const auto RegError =
8825         getRegistersForValue(DAG, getCurSDLoc(), OpInfo, RefOpInfo);
8826     if (RegError.hasValue()) {
8827       const MachineFunction &MF = DAG.getMachineFunction();
8828       const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
8829       const char *RegName = TRI.getName(RegError.getValue());
8830       emitInlineAsmError(Call, "register '" + Twine(RegName) +
8831                                    "' allocated for constraint '" +
8832                                    Twine(OpInfo.ConstraintCode) +
8833                                    "' does not match required type");
8834       return;
8835     }
8836 
8837     auto DetectWriteToReservedRegister = [&]() {
8838       const MachineFunction &MF = DAG.getMachineFunction();
8839       const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
8840       for (unsigned Reg : OpInfo.AssignedRegs.Regs) {
8841         if (Register::isPhysicalRegister(Reg) &&
8842             TRI.isInlineAsmReadOnlyReg(MF, Reg)) {
8843           const char *RegName = TRI.getName(Reg);
8844           emitInlineAsmError(Call, "write to reserved register '" +
8845                                        Twine(RegName) + "'");
8846           return true;
8847         }
8848       }
8849       return false;
8850     };
8851     assert((OpInfo.ConstraintType != TargetLowering::C_Address ||
8852             (OpInfo.Type == InlineAsm::isInput &&
8853              !OpInfo.isMatchingInputConstraint())) &&
8854            "Only address as input operand is allowed.");
8855 
8856     switch (OpInfo.Type) {
8857     case InlineAsm::isOutput:
8858       if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
8859         unsigned ConstraintID =
8860             TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
8861         assert(ConstraintID != InlineAsm::Constraint_Unknown &&
8862                "Failed to convert memory constraint code to constraint id.");
8863 
8864         // Add information to the INLINEASM node to know about this output.
8865         unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
8866         OpFlags = InlineAsm::getFlagWordForMem(OpFlags, ConstraintID);
8867         AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags, getCurSDLoc(),
8868                                                         MVT::i32));
8869         AsmNodeOperands.push_back(OpInfo.CallOperand);
8870       } else {
8871         // Otherwise, this outputs to a register (directly for C_Register /
8872         // C_RegisterClass, and a target-defined fashion for
8873         // C_Immediate/C_Other). Find a register that we can use.
8874         if (OpInfo.AssignedRegs.Regs.empty()) {
8875           emitInlineAsmError(
8876               Call, "couldn't allocate output register for constraint '" +
8877                         Twine(OpInfo.ConstraintCode) + "'");
8878           return;
8879         }
8880 
8881         if (DetectWriteToReservedRegister())
8882           return;
8883 
8884         // Add information to the INLINEASM node to know that this register is
8885         // set.
8886         OpInfo.AssignedRegs.AddInlineAsmOperands(
8887             OpInfo.isEarlyClobber ? InlineAsm::Kind_RegDefEarlyClobber
8888                                   : InlineAsm::Kind_RegDef,
8889             false, 0, getCurSDLoc(), DAG, AsmNodeOperands);
8890       }
8891       break;
8892 
8893     case InlineAsm::isInput: {
8894       SDValue InOperandVal = OpInfo.CallOperand;
8895 
8896       if (OpInfo.isMatchingInputConstraint()) {
8897         // If this is required to match an output register we have already set,
8898         // just use its register.
8899         auto CurOp = findMatchingInlineAsmOperand(OpInfo.getMatchedOperand(),
8900                                                   AsmNodeOperands);
8901         unsigned OpFlag =
8902           cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
8903         if (InlineAsm::isRegDefKind(OpFlag) ||
8904             InlineAsm::isRegDefEarlyClobberKind(OpFlag)) {
8905           // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
8906           if (OpInfo.isIndirect) {
8907             // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
8908             emitInlineAsmError(Call, "inline asm not supported yet: "
8909                                      "don't know how to handle tied "
8910                                      "indirect register inputs");
8911             return;
8912           }
8913 
8914           SmallVector<unsigned, 4> Regs;
8915           MachineFunction &MF = DAG.getMachineFunction();
8916           MachineRegisterInfo &MRI = MF.getRegInfo();
8917           const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
8918           auto *R = cast<RegisterSDNode>(AsmNodeOperands[CurOp+1]);
8919           Register TiedReg = R->getReg();
8920           MVT RegVT = R->getSimpleValueType(0);
8921           const TargetRegisterClass *RC =
8922               TiedReg.isVirtual()     ? MRI.getRegClass(TiedReg)
8923               : RegVT != MVT::Untyped ? TLI.getRegClassFor(RegVT)
8924                                       : TRI.getMinimalPhysRegClass(TiedReg);
8925           unsigned NumRegs = InlineAsm::getNumOperandRegisters(OpFlag);
8926           for (unsigned i = 0; i != NumRegs; ++i)
8927             Regs.push_back(MRI.createVirtualRegister(RC));
8928 
8929           RegsForValue MatchedRegs(Regs, RegVT, InOperandVal.getValueType());
8930 
8931           SDLoc dl = getCurSDLoc();
8932           // Use the produced MatchedRegs object to
8933           MatchedRegs.getCopyToRegs(InOperandVal, DAG, dl, Chain, &Flag, &Call);
8934           MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse,
8935                                            true, OpInfo.getMatchedOperand(), dl,
8936                                            DAG, AsmNodeOperands);
8937           break;
8938         }
8939 
8940         assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!");
8941         assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 &&
8942                "Unexpected number of operands");
8943         // Add information to the INLINEASM node to know about this input.
8944         // See InlineAsm.h isUseOperandTiedToDef.
8945         OpFlag = InlineAsm::convertMemFlagWordToMatchingFlagWord(OpFlag);
8946         OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag,
8947                                                     OpInfo.getMatchedOperand());
8948         AsmNodeOperands.push_back(DAG.getTargetConstant(
8949             OpFlag, getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
8950         AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
8951         break;
8952       }
8953 
8954       // Treat indirect 'X' constraint as memory.
8955       if (OpInfo.ConstraintType == TargetLowering::C_Other &&
8956           OpInfo.isIndirect)
8957         OpInfo.ConstraintType = TargetLowering::C_Memory;
8958 
8959       if (OpInfo.ConstraintType == TargetLowering::C_Immediate ||
8960           OpInfo.ConstraintType == TargetLowering::C_Other) {
8961         std::vector<SDValue> Ops;
8962         TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode,
8963                                           Ops, DAG);
8964         if (Ops.empty()) {
8965           if (OpInfo.ConstraintType == TargetLowering::C_Immediate)
8966             if (isa<ConstantSDNode>(InOperandVal)) {
8967               emitInlineAsmError(Call, "value out of range for constraint '" +
8968                                            Twine(OpInfo.ConstraintCode) + "'");
8969               return;
8970             }
8971 
8972           emitInlineAsmError(Call,
8973                              "invalid operand for inline asm constraint '" +
8974                                  Twine(OpInfo.ConstraintCode) + "'");
8975           return;
8976         }
8977 
8978         // Add information to the INLINEASM node to know about this input.
8979         unsigned ResOpType =
8980           InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size());
8981         AsmNodeOperands.push_back(DAG.getTargetConstant(
8982             ResOpType, getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
8983         llvm::append_range(AsmNodeOperands, Ops);
8984         break;
8985       }
8986 
8987       if (OpInfo.ConstraintType == TargetLowering::C_Memory ||
8988           OpInfo.ConstraintType == TargetLowering::C_Address) {
8989         assert((OpInfo.isIndirect ||
8990                 OpInfo.ConstraintType != TargetLowering::C_Memory) &&
8991                "Operand must be indirect to be a mem!");
8992         assert(InOperandVal.getValueType() ==
8993                    TLI.getPointerTy(DAG.getDataLayout()) &&
8994                "Memory operands expect pointer values");
8995 
8996         unsigned ConstraintID =
8997             TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
8998         assert(ConstraintID != InlineAsm::Constraint_Unknown &&
8999                "Failed to convert memory constraint code to constraint id.");
9000 
9001         // Add information to the INLINEASM node to know about this input.
9002         unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
9003         ResOpType = InlineAsm::getFlagWordForMem(ResOpType, ConstraintID);
9004         AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
9005                                                         getCurSDLoc(),
9006                                                         MVT::i32));
9007         AsmNodeOperands.push_back(InOperandVal);
9008         break;
9009       }
9010 
9011       assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
9012               OpInfo.ConstraintType == TargetLowering::C_Register) &&
9013              "Unknown constraint type!");
9014 
9015       // TODO: Support this.
9016       if (OpInfo.isIndirect) {
9017         emitInlineAsmError(
9018             Call, "Don't know how to handle indirect register inputs yet "
9019                   "for constraint '" +
9020                       Twine(OpInfo.ConstraintCode) + "'");
9021         return;
9022       }
9023 
9024       // Copy the input into the appropriate registers.
9025       if (OpInfo.AssignedRegs.Regs.empty()) {
9026         emitInlineAsmError(Call,
9027                            "couldn't allocate input reg for constraint '" +
9028                                Twine(OpInfo.ConstraintCode) + "'");
9029         return;
9030       }
9031 
9032       if (DetectWriteToReservedRegister())
9033         return;
9034 
9035       SDLoc dl = getCurSDLoc();
9036 
9037       OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, dl, Chain, &Flag,
9038                                         &Call);
9039 
9040       OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0,
9041                                                dl, DAG, AsmNodeOperands);
9042       break;
9043     }
9044     case InlineAsm::isClobber:
9045       // Add the clobbered value to the operand list, so that the register
9046       // allocator is aware that the physreg got clobbered.
9047       if (!OpInfo.AssignedRegs.Regs.empty())
9048         OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_Clobber,
9049                                                  false, 0, getCurSDLoc(), DAG,
9050                                                  AsmNodeOperands);
9051       break;
9052     }
9053   }
9054 
9055   // Finish up input operands.  Set the input chain and add the flag last.
9056   AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
9057   if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
9058 
9059   unsigned ISDOpc = IsCallBr ? ISD::INLINEASM_BR : ISD::INLINEASM;
9060   Chain = DAG.getNode(ISDOpc, getCurSDLoc(),
9061                       DAG.getVTList(MVT::Other, MVT::Glue), AsmNodeOperands);
9062   Flag = Chain.getValue(1);
9063 
9064   // Do additional work to generate outputs.
9065 
9066   SmallVector<EVT, 1> ResultVTs;
9067   SmallVector<SDValue, 1> ResultValues;
9068   SmallVector<SDValue, 8> OutChains;
9069 
9070   llvm::Type *CallResultType = Call.getType();
9071   ArrayRef<Type *> ResultTypes;
9072   if (StructType *StructResult = dyn_cast<StructType>(CallResultType))
9073     ResultTypes = StructResult->elements();
9074   else if (!CallResultType->isVoidTy())
9075     ResultTypes = makeArrayRef(CallResultType);
9076 
9077   auto CurResultType = ResultTypes.begin();
9078   auto handleRegAssign = [&](SDValue V) {
9079     assert(CurResultType != ResultTypes.end() && "Unexpected value");
9080     assert((*CurResultType)->isSized() && "Unexpected unsized type");
9081     EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), *CurResultType);
9082     ++CurResultType;
9083     // If the type of the inline asm call site return value is different but has
9084     // same size as the type of the asm output bitcast it.  One example of this
9085     // is for vectors with different width / number of elements.  This can
9086     // happen for register classes that can contain multiple different value
9087     // types.  The preg or vreg allocated may not have the same VT as was
9088     // expected.
9089     //
9090     // This can also happen for a return value that disagrees with the register
9091     // class it is put in, eg. a double in a general-purpose register on a
9092     // 32-bit machine.
9093     if (ResultVT != V.getValueType() &&
9094         ResultVT.getSizeInBits() == V.getValueSizeInBits())
9095       V = DAG.getNode(ISD::BITCAST, getCurSDLoc(), ResultVT, V);
9096     else if (ResultVT != V.getValueType() && ResultVT.isInteger() &&
9097              V.getValueType().isInteger()) {
9098       // If a result value was tied to an input value, the computed result
9099       // may have a wider width than the expected result.  Extract the
9100       // relevant portion.
9101       V = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), ResultVT, V);
9102     }
9103     assert(ResultVT == V.getValueType() && "Asm result value mismatch!");
9104     ResultVTs.push_back(ResultVT);
9105     ResultValues.push_back(V);
9106   };
9107 
9108   // Deal with output operands.
9109   for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
9110     if (OpInfo.Type == InlineAsm::isOutput) {
9111       SDValue Val;
9112       // Skip trivial output operands.
9113       if (OpInfo.AssignedRegs.Regs.empty())
9114         continue;
9115 
9116       switch (OpInfo.ConstraintType) {
9117       case TargetLowering::C_Register:
9118       case TargetLowering::C_RegisterClass:
9119         Val = OpInfo.AssignedRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
9120                                                   Chain, &Flag, &Call);
9121         break;
9122       case TargetLowering::C_Immediate:
9123       case TargetLowering::C_Other:
9124         Val = TLI.LowerAsmOutputForConstraint(Chain, Flag, getCurSDLoc(),
9125                                               OpInfo, DAG);
9126         break;
9127       case TargetLowering::C_Memory:
9128         break; // Already handled.
9129       case TargetLowering::C_Address:
9130         break; // Silence warning.
9131       case TargetLowering::C_Unknown:
9132         assert(false && "Unexpected unknown constraint");
9133       }
9134 
9135       // Indirect output manifest as stores. Record output chains.
9136       if (OpInfo.isIndirect) {
9137         const Value *Ptr = OpInfo.CallOperandVal;
9138         assert(Ptr && "Expected value CallOperandVal for indirect asm operand");
9139         SDValue Store = DAG.getStore(Chain, getCurSDLoc(), Val, getValue(Ptr),
9140                                      MachinePointerInfo(Ptr));
9141         OutChains.push_back(Store);
9142       } else {
9143         // generate CopyFromRegs to associated registers.
9144         assert(!Call.getType()->isVoidTy() && "Bad inline asm!");
9145         if (Val.getOpcode() == ISD::MERGE_VALUES) {
9146           for (const SDValue &V : Val->op_values())
9147             handleRegAssign(V);
9148         } else
9149           handleRegAssign(Val);
9150       }
9151     }
9152   }
9153 
9154   // Set results.
9155   if (!ResultValues.empty()) {
9156     assert(CurResultType == ResultTypes.end() &&
9157            "Mismatch in number of ResultTypes");
9158     assert(ResultValues.size() == ResultTypes.size() &&
9159            "Mismatch in number of output operands in asm result");
9160 
9161     SDValue V = DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
9162                             DAG.getVTList(ResultVTs), ResultValues);
9163     setValue(&Call, V);
9164   }
9165 
9166   // Collect store chains.
9167   if (!OutChains.empty())
9168     Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, OutChains);
9169 
9170   if (EmitEHLabels) {
9171     Chain = lowerEndEH(Chain, cast<InvokeInst>(&Call), EHPadBB, BeginLabel);
9172   }
9173 
9174   // Only Update Root if inline assembly has a memory effect.
9175   if (ResultValues.empty() || HasSideEffect || !OutChains.empty() || IsCallBr ||
9176       EmitEHLabels)
9177     DAG.setRoot(Chain);
9178 }
9179 
9180 void SelectionDAGBuilder::emitInlineAsmError(const CallBase &Call,
9181                                              const Twine &Message) {
9182   LLVMContext &Ctx = *DAG.getContext();
9183   Ctx.emitError(&Call, Message);
9184 
9185   // Make sure we leave the DAG in a valid state
9186   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9187   SmallVector<EVT, 1> ValueVTs;
9188   ComputeValueVTs(TLI, DAG.getDataLayout(), Call.getType(), ValueVTs);
9189 
9190   if (ValueVTs.empty())
9191     return;
9192 
9193   SmallVector<SDValue, 1> Ops;
9194   for (unsigned i = 0, e = ValueVTs.size(); i != e; ++i)
9195     Ops.push_back(DAG.getUNDEF(ValueVTs[i]));
9196 
9197   setValue(&Call, DAG.getMergeValues(Ops, getCurSDLoc()));
9198 }
9199 
9200 void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
9201   DAG.setRoot(DAG.getNode(ISD::VASTART, getCurSDLoc(),
9202                           MVT::Other, getRoot(),
9203                           getValue(I.getArgOperand(0)),
9204                           DAG.getSrcValue(I.getArgOperand(0))));
9205 }
9206 
9207 void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
9208   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9209   const DataLayout &DL = DAG.getDataLayout();
9210   SDValue V = DAG.getVAArg(
9211       TLI.getMemValueType(DAG.getDataLayout(), I.getType()), getCurSDLoc(),
9212       getRoot(), getValue(I.getOperand(0)), DAG.getSrcValue(I.getOperand(0)),
9213       DL.getABITypeAlign(I.getType()).value());
9214   DAG.setRoot(V.getValue(1));
9215 
9216   if (I.getType()->isPointerTy())
9217     V = DAG.getPtrExtOrTrunc(
9218         V, getCurSDLoc(), TLI.getValueType(DAG.getDataLayout(), I.getType()));
9219   setValue(&I, V);
9220 }
9221 
9222 void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
9223   DAG.setRoot(DAG.getNode(ISD::VAEND, getCurSDLoc(),
9224                           MVT::Other, getRoot(),
9225                           getValue(I.getArgOperand(0)),
9226                           DAG.getSrcValue(I.getArgOperand(0))));
9227 }
9228 
9229 void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
9230   DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurSDLoc(),
9231                           MVT::Other, getRoot(),
9232                           getValue(I.getArgOperand(0)),
9233                           getValue(I.getArgOperand(1)),
9234                           DAG.getSrcValue(I.getArgOperand(0)),
9235                           DAG.getSrcValue(I.getArgOperand(1))));
9236 }
9237 
9238 SDValue SelectionDAGBuilder::lowerRangeToAssertZExt(SelectionDAG &DAG,
9239                                                     const Instruction &I,
9240                                                     SDValue Op) {
9241   const MDNode *Range = I.getMetadata(LLVMContext::MD_range);
9242   if (!Range)
9243     return Op;
9244 
9245   ConstantRange CR = getConstantRangeFromMetadata(*Range);
9246   if (CR.isFullSet() || CR.isEmptySet() || CR.isUpperWrapped())
9247     return Op;
9248 
9249   APInt Lo = CR.getUnsignedMin();
9250   if (!Lo.isMinValue())
9251     return Op;
9252 
9253   APInt Hi = CR.getUnsignedMax();
9254   unsigned Bits = std::max(Hi.getActiveBits(),
9255                            static_cast<unsigned>(IntegerType::MIN_INT_BITS));
9256 
9257   EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), Bits);
9258 
9259   SDLoc SL = getCurSDLoc();
9260 
9261   SDValue ZExt = DAG.getNode(ISD::AssertZext, SL, Op.getValueType(), Op,
9262                              DAG.getValueType(SmallVT));
9263   unsigned NumVals = Op.getNode()->getNumValues();
9264   if (NumVals == 1)
9265     return ZExt;
9266 
9267   SmallVector<SDValue, 4> Ops;
9268 
9269   Ops.push_back(ZExt);
9270   for (unsigned I = 1; I != NumVals; ++I)
9271     Ops.push_back(Op.getValue(I));
9272 
9273   return DAG.getMergeValues(Ops, SL);
9274 }
9275 
9276 /// Populate a CallLowerinInfo (into \p CLI) based on the properties of
9277 /// the call being lowered.
9278 ///
9279 /// This is a helper for lowering intrinsics that follow a target calling
9280 /// convention or require stack pointer adjustment. Only a subset of the
9281 /// intrinsic's operands need to participate in the calling convention.
9282 void SelectionDAGBuilder::populateCallLoweringInfo(
9283     TargetLowering::CallLoweringInfo &CLI, const CallBase *Call,
9284     unsigned ArgIdx, unsigned NumArgs, SDValue Callee, Type *ReturnTy,
9285     bool IsPatchPoint) {
9286   TargetLowering::ArgListTy Args;
9287   Args.reserve(NumArgs);
9288 
9289   // Populate the argument list.
9290   // Attributes for args start at offset 1, after the return attribute.
9291   for (unsigned ArgI = ArgIdx, ArgE = ArgIdx + NumArgs;
9292        ArgI != ArgE; ++ArgI) {
9293     const Value *V = Call->getOperand(ArgI);
9294 
9295     assert(!V->getType()->isEmptyTy() && "Empty type passed to intrinsic.");
9296 
9297     TargetLowering::ArgListEntry Entry;
9298     Entry.Node = getValue(V);
9299     Entry.Ty = V->getType();
9300     Entry.setAttributes(Call, ArgI);
9301     Args.push_back(Entry);
9302   }
9303 
9304   CLI.setDebugLoc(getCurSDLoc())
9305       .setChain(getRoot())
9306       .setCallee(Call->getCallingConv(), ReturnTy, Callee, std::move(Args))
9307       .setDiscardResult(Call->use_empty())
9308       .setIsPatchPoint(IsPatchPoint)
9309       .setIsPreallocated(
9310           Call->countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0);
9311 }
9312 
9313 /// Add a stack map intrinsic call's live variable operands to a stackmap
9314 /// or patchpoint target node's operand list.
9315 ///
9316 /// Constants are converted to TargetConstants purely as an optimization to
9317 /// avoid constant materialization and register allocation.
9318 ///
9319 /// FrameIndex operands are converted to TargetFrameIndex so that ISEL does not
9320 /// generate addess computation nodes, and so FinalizeISel can convert the
9321 /// TargetFrameIndex into a DirectMemRefOp StackMap location. This avoids
9322 /// address materialization and register allocation, but may also be required
9323 /// for correctness. If a StackMap (or PatchPoint) intrinsic directly uses an
9324 /// alloca in the entry block, then the runtime may assume that the alloca's
9325 /// StackMap location can be read immediately after compilation and that the
9326 /// location is valid at any point during execution (this is similar to the
9327 /// assumption made by the llvm.gcroot intrinsic). If the alloca's location were
9328 /// only available in a register, then the runtime would need to trap when
9329 /// execution reaches the StackMap in order to read the alloca's location.
9330 static void addStackMapLiveVars(const CallBase &Call, unsigned StartIdx,
9331                                 const SDLoc &DL, SmallVectorImpl<SDValue> &Ops,
9332                                 SelectionDAGBuilder &Builder) {
9333   for (unsigned i = StartIdx, e = Call.arg_size(); i != e; ++i) {
9334     SDValue OpVal = Builder.getValue(Call.getArgOperand(i));
9335     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(OpVal)) {
9336       Ops.push_back(
9337         Builder.DAG.getTargetConstant(StackMaps::ConstantOp, DL, MVT::i64));
9338       Ops.push_back(
9339         Builder.DAG.getTargetConstant(C->getSExtValue(), DL, MVT::i64));
9340     } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(OpVal)) {
9341       const TargetLowering &TLI = Builder.DAG.getTargetLoweringInfo();
9342       Ops.push_back(Builder.DAG.getTargetFrameIndex(
9343           FI->getIndex(), TLI.getFrameIndexTy(Builder.DAG.getDataLayout())));
9344     } else
9345       Ops.push_back(OpVal);
9346   }
9347 }
9348 
9349 /// Lower llvm.experimental.stackmap directly to its target opcode.
9350 void SelectionDAGBuilder::visitStackmap(const CallInst &CI) {
9351   // void @llvm.experimental.stackmap(i32 <id>, i32 <numShadowBytes>,
9352   //                                  [live variables...])
9353 
9354   assert(CI.getType()->isVoidTy() && "Stackmap cannot return a value.");
9355 
9356   SDValue Chain, InFlag, Callee, NullPtr;
9357   SmallVector<SDValue, 32> Ops;
9358 
9359   SDLoc DL = getCurSDLoc();
9360   Callee = getValue(CI.getCalledOperand());
9361   NullPtr = DAG.getIntPtrConstant(0, DL, true);
9362 
9363   // The stackmap intrinsic only records the live variables (the arguments
9364   // passed to it) and emits NOPS (if requested). Unlike the patchpoint
9365   // intrinsic, this won't be lowered to a function call. This means we don't
9366   // have to worry about calling conventions and target specific lowering code.
9367   // Instead we perform the call lowering right here.
9368   //
9369   // chain, flag = CALLSEQ_START(chain, 0, 0)
9370   // chain, flag = STACKMAP(id, nbytes, ..., chain, flag)
9371   // chain, flag = CALLSEQ_END(chain, 0, 0, flag)
9372   //
9373   Chain = DAG.getCALLSEQ_START(getRoot(), 0, 0, DL);
9374   InFlag = Chain.getValue(1);
9375 
9376   // Add the <id> and <numBytes> constants.
9377   SDValue IDVal = getValue(CI.getOperand(PatchPointOpers::IDPos));
9378   Ops.push_back(DAG.getTargetConstant(
9379                   cast<ConstantSDNode>(IDVal)->getZExtValue(), DL, MVT::i64));
9380   SDValue NBytesVal = getValue(CI.getOperand(PatchPointOpers::NBytesPos));
9381   Ops.push_back(DAG.getTargetConstant(
9382                   cast<ConstantSDNode>(NBytesVal)->getZExtValue(), DL,
9383                   MVT::i32));
9384 
9385   // Push live variables for the stack map.
9386   addStackMapLiveVars(CI, 2, DL, Ops, *this);
9387 
9388   // We are not pushing any register mask info here on the operands list,
9389   // because the stackmap doesn't clobber anything.
9390 
9391   // Push the chain and the glue flag.
9392   Ops.push_back(Chain);
9393   Ops.push_back(InFlag);
9394 
9395   // Create the STACKMAP node.
9396   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
9397   SDNode *SM = DAG.getMachineNode(TargetOpcode::STACKMAP, DL, NodeTys, Ops);
9398   Chain = SDValue(SM, 0);
9399   InFlag = Chain.getValue(1);
9400 
9401   Chain = DAG.getCALLSEQ_END(Chain, NullPtr, NullPtr, InFlag, DL);
9402 
9403   // Stackmaps don't generate values, so nothing goes into the NodeMap.
9404 
9405   // Set the root to the target-lowered call chain.
9406   DAG.setRoot(Chain);
9407 
9408   // Inform the Frame Information that we have a stackmap in this function.
9409   FuncInfo.MF->getFrameInfo().setHasStackMap();
9410 }
9411 
9412 /// Lower llvm.experimental.patchpoint directly to its target opcode.
9413 void SelectionDAGBuilder::visitPatchpoint(const CallBase &CB,
9414                                           const BasicBlock *EHPadBB) {
9415   // void|i64 @llvm.experimental.patchpoint.void|i64(i64 <id>,
9416   //                                                 i32 <numBytes>,
9417   //                                                 i8* <target>,
9418   //                                                 i32 <numArgs>,
9419   //                                                 [Args...],
9420   //                                                 [live variables...])
9421 
9422   CallingConv::ID CC = CB.getCallingConv();
9423   bool IsAnyRegCC = CC == CallingConv::AnyReg;
9424   bool HasDef = !CB.getType()->isVoidTy();
9425   SDLoc dl = getCurSDLoc();
9426   SDValue Callee = getValue(CB.getArgOperand(PatchPointOpers::TargetPos));
9427 
9428   // Handle immediate and symbolic callees.
9429   if (auto* ConstCallee = dyn_cast<ConstantSDNode>(Callee))
9430     Callee = DAG.getIntPtrConstant(ConstCallee->getZExtValue(), dl,
9431                                    /*isTarget=*/true);
9432   else if (auto* SymbolicCallee = dyn_cast<GlobalAddressSDNode>(Callee))
9433     Callee =  DAG.getTargetGlobalAddress(SymbolicCallee->getGlobal(),
9434                                          SDLoc(SymbolicCallee),
9435                                          SymbolicCallee->getValueType(0));
9436 
9437   // Get the real number of arguments participating in the call <numArgs>
9438   SDValue NArgVal = getValue(CB.getArgOperand(PatchPointOpers::NArgPos));
9439   unsigned NumArgs = cast<ConstantSDNode>(NArgVal)->getZExtValue();
9440 
9441   // Skip the four meta args: <id>, <numNopBytes>, <target>, <numArgs>
9442   // Intrinsics include all meta-operands up to but not including CC.
9443   unsigned NumMetaOpers = PatchPointOpers::CCPos;
9444   assert(CB.arg_size() >= NumMetaOpers + NumArgs &&
9445          "Not enough arguments provided to the patchpoint intrinsic");
9446 
9447   // For AnyRegCC the arguments are lowered later on manually.
9448   unsigned NumCallArgs = IsAnyRegCC ? 0 : NumArgs;
9449   Type *ReturnTy =
9450       IsAnyRegCC ? Type::getVoidTy(*DAG.getContext()) : CB.getType();
9451 
9452   TargetLowering::CallLoweringInfo CLI(DAG);
9453   populateCallLoweringInfo(CLI, &CB, NumMetaOpers, NumCallArgs, Callee,
9454                            ReturnTy, true);
9455   std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
9456 
9457   SDNode *CallEnd = Result.second.getNode();
9458   if (HasDef && (CallEnd->getOpcode() == ISD::CopyFromReg))
9459     CallEnd = CallEnd->getOperand(0).getNode();
9460 
9461   /// Get a call instruction from the call sequence chain.
9462   /// Tail calls are not allowed.
9463   assert(CallEnd->getOpcode() == ISD::CALLSEQ_END &&
9464          "Expected a callseq node.");
9465   SDNode *Call = CallEnd->getOperand(0).getNode();
9466   bool HasGlue = Call->getGluedNode();
9467 
9468   // Replace the target specific call node with the patchable intrinsic.
9469   SmallVector<SDValue, 8> Ops;
9470 
9471   // Add the <id> and <numBytes> constants.
9472   SDValue IDVal = getValue(CB.getArgOperand(PatchPointOpers::IDPos));
9473   Ops.push_back(DAG.getTargetConstant(
9474                   cast<ConstantSDNode>(IDVal)->getZExtValue(), dl, MVT::i64));
9475   SDValue NBytesVal = getValue(CB.getArgOperand(PatchPointOpers::NBytesPos));
9476   Ops.push_back(DAG.getTargetConstant(
9477                   cast<ConstantSDNode>(NBytesVal)->getZExtValue(), dl,
9478                   MVT::i32));
9479 
9480   // Add the callee.
9481   Ops.push_back(Callee);
9482 
9483   // Adjust <numArgs> to account for any arguments that have been passed on the
9484   // stack instead.
9485   // Call Node: Chain, Target, {Args}, RegMask, [Glue]
9486   unsigned NumCallRegArgs = Call->getNumOperands() - (HasGlue ? 4 : 3);
9487   NumCallRegArgs = IsAnyRegCC ? NumArgs : NumCallRegArgs;
9488   Ops.push_back(DAG.getTargetConstant(NumCallRegArgs, dl, MVT::i32));
9489 
9490   // Add the calling convention
9491   Ops.push_back(DAG.getTargetConstant((unsigned)CC, dl, MVT::i32));
9492 
9493   // Add the arguments we omitted previously. The register allocator should
9494   // place these in any free register.
9495   if (IsAnyRegCC)
9496     for (unsigned i = NumMetaOpers, e = NumMetaOpers + NumArgs; i != e; ++i)
9497       Ops.push_back(getValue(CB.getArgOperand(i)));
9498 
9499   // Push the arguments from the call instruction up to the register mask.
9500   SDNode::op_iterator e = HasGlue ? Call->op_end()-2 : Call->op_end()-1;
9501   Ops.append(Call->op_begin() + 2, e);
9502 
9503   // Push live variables for the stack map.
9504   addStackMapLiveVars(CB, NumMetaOpers + NumArgs, dl, Ops, *this);
9505 
9506   // Push the register mask info.
9507   if (HasGlue)
9508     Ops.push_back(*(Call->op_end()-2));
9509   else
9510     Ops.push_back(*(Call->op_end()-1));
9511 
9512   // Push the chain (this is originally the first operand of the call, but
9513   // becomes now the last or second to last operand).
9514   Ops.push_back(*(Call->op_begin()));
9515 
9516   // Push the glue flag (last operand).
9517   if (HasGlue)
9518     Ops.push_back(*(Call->op_end()-1));
9519 
9520   SDVTList NodeTys;
9521   if (IsAnyRegCC && HasDef) {
9522     // Create the return types based on the intrinsic definition
9523     const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9524     SmallVector<EVT, 3> ValueVTs;
9525     ComputeValueVTs(TLI, DAG.getDataLayout(), CB.getType(), ValueVTs);
9526     assert(ValueVTs.size() == 1 && "Expected only one return value type.");
9527 
9528     // There is always a chain and a glue type at the end
9529     ValueVTs.push_back(MVT::Other);
9530     ValueVTs.push_back(MVT::Glue);
9531     NodeTys = DAG.getVTList(ValueVTs);
9532   } else
9533     NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
9534 
9535   // Replace the target specific call node with a PATCHPOINT node.
9536   MachineSDNode *MN = DAG.getMachineNode(TargetOpcode::PATCHPOINT,
9537                                          dl, NodeTys, Ops);
9538 
9539   // Update the NodeMap.
9540   if (HasDef) {
9541     if (IsAnyRegCC)
9542       setValue(&CB, SDValue(MN, 0));
9543     else
9544       setValue(&CB, Result.first);
9545   }
9546 
9547   // Fixup the consumers of the intrinsic. The chain and glue may be used in the
9548   // call sequence. Furthermore the location of the chain and glue can change
9549   // when the AnyReg calling convention is used and the intrinsic returns a
9550   // value.
9551   if (IsAnyRegCC && HasDef) {
9552     SDValue From[] = {SDValue(Call, 0), SDValue(Call, 1)};
9553     SDValue To[] = {SDValue(MN, 1), SDValue(MN, 2)};
9554     DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
9555   } else
9556     DAG.ReplaceAllUsesWith(Call, MN);
9557   DAG.DeleteNode(Call);
9558 
9559   // Inform the Frame Information that we have a patchpoint in this function.
9560   FuncInfo.MF->getFrameInfo().setHasPatchPoint();
9561 }
9562 
9563 void SelectionDAGBuilder::visitVectorReduce(const CallInst &I,
9564                                             unsigned Intrinsic) {
9565   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9566   SDValue Op1 = getValue(I.getArgOperand(0));
9567   SDValue Op2;
9568   if (I.arg_size() > 1)
9569     Op2 = getValue(I.getArgOperand(1));
9570   SDLoc dl = getCurSDLoc();
9571   EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
9572   SDValue Res;
9573   SDNodeFlags SDFlags;
9574   if (auto *FPMO = dyn_cast<FPMathOperator>(&I))
9575     SDFlags.copyFMF(*FPMO);
9576 
9577   switch (Intrinsic) {
9578   case Intrinsic::vector_reduce_fadd:
9579     if (SDFlags.hasAllowReassociation())
9580       Res = DAG.getNode(ISD::FADD, dl, VT, Op1,
9581                         DAG.getNode(ISD::VECREDUCE_FADD, dl, VT, Op2, SDFlags),
9582                         SDFlags);
9583     else
9584       Res = DAG.getNode(ISD::VECREDUCE_SEQ_FADD, dl, VT, Op1, Op2, SDFlags);
9585     break;
9586   case Intrinsic::vector_reduce_fmul:
9587     if (SDFlags.hasAllowReassociation())
9588       Res = DAG.getNode(ISD::FMUL, dl, VT, Op1,
9589                         DAG.getNode(ISD::VECREDUCE_FMUL, dl, VT, Op2, SDFlags),
9590                         SDFlags);
9591     else
9592       Res = DAG.getNode(ISD::VECREDUCE_SEQ_FMUL, dl, VT, Op1, Op2, SDFlags);
9593     break;
9594   case Intrinsic::vector_reduce_add:
9595     Res = DAG.getNode(ISD::VECREDUCE_ADD, dl, VT, Op1);
9596     break;
9597   case Intrinsic::vector_reduce_mul:
9598     Res = DAG.getNode(ISD::VECREDUCE_MUL, dl, VT, Op1);
9599     break;
9600   case Intrinsic::vector_reduce_and:
9601     Res = DAG.getNode(ISD::VECREDUCE_AND, dl, VT, Op1);
9602     break;
9603   case Intrinsic::vector_reduce_or:
9604     Res = DAG.getNode(ISD::VECREDUCE_OR, dl, VT, Op1);
9605     break;
9606   case Intrinsic::vector_reduce_xor:
9607     Res = DAG.getNode(ISD::VECREDUCE_XOR, dl, VT, Op1);
9608     break;
9609   case Intrinsic::vector_reduce_smax:
9610     Res = DAG.getNode(ISD::VECREDUCE_SMAX, dl, VT, Op1);
9611     break;
9612   case Intrinsic::vector_reduce_smin:
9613     Res = DAG.getNode(ISD::VECREDUCE_SMIN, dl, VT, Op1);
9614     break;
9615   case Intrinsic::vector_reduce_umax:
9616     Res = DAG.getNode(ISD::VECREDUCE_UMAX, dl, VT, Op1);
9617     break;
9618   case Intrinsic::vector_reduce_umin:
9619     Res = DAG.getNode(ISD::VECREDUCE_UMIN, dl, VT, Op1);
9620     break;
9621   case Intrinsic::vector_reduce_fmax:
9622     Res = DAG.getNode(ISD::VECREDUCE_FMAX, dl, VT, Op1, SDFlags);
9623     break;
9624   case Intrinsic::vector_reduce_fmin:
9625     Res = DAG.getNode(ISD::VECREDUCE_FMIN, dl, VT, Op1, SDFlags);
9626     break;
9627   default:
9628     llvm_unreachable("Unhandled vector reduce intrinsic");
9629   }
9630   setValue(&I, Res);
9631 }
9632 
9633 /// Returns an AttributeList representing the attributes applied to the return
9634 /// value of the given call.
9635 static AttributeList getReturnAttrs(TargetLowering::CallLoweringInfo &CLI) {
9636   SmallVector<Attribute::AttrKind, 2> Attrs;
9637   if (CLI.RetSExt)
9638     Attrs.push_back(Attribute::SExt);
9639   if (CLI.RetZExt)
9640     Attrs.push_back(Attribute::ZExt);
9641   if (CLI.IsInReg)
9642     Attrs.push_back(Attribute::InReg);
9643 
9644   return AttributeList::get(CLI.RetTy->getContext(), AttributeList::ReturnIndex,
9645                             Attrs);
9646 }
9647 
9648 /// TargetLowering::LowerCallTo - This is the default LowerCallTo
9649 /// implementation, which just calls LowerCall.
9650 /// FIXME: When all targets are
9651 /// migrated to using LowerCall, this hook should be integrated into SDISel.
9652 std::pair<SDValue, SDValue>
9653 TargetLowering::LowerCallTo(TargetLowering::CallLoweringInfo &CLI) const {
9654   // Handle the incoming return values from the call.
9655   CLI.Ins.clear();
9656   Type *OrigRetTy = CLI.RetTy;
9657   SmallVector<EVT, 4> RetTys;
9658   SmallVector<uint64_t, 4> Offsets;
9659   auto &DL = CLI.DAG.getDataLayout();
9660   ComputeValueVTs(*this, DL, CLI.RetTy, RetTys, &Offsets);
9661 
9662   if (CLI.IsPostTypeLegalization) {
9663     // If we are lowering a libcall after legalization, split the return type.
9664     SmallVector<EVT, 4> OldRetTys;
9665     SmallVector<uint64_t, 4> OldOffsets;
9666     RetTys.swap(OldRetTys);
9667     Offsets.swap(OldOffsets);
9668 
9669     for (size_t i = 0, e = OldRetTys.size(); i != e; ++i) {
9670       EVT RetVT = OldRetTys[i];
9671       uint64_t Offset = OldOffsets[i];
9672       MVT RegisterVT = getRegisterType(CLI.RetTy->getContext(), RetVT);
9673       unsigned NumRegs = getNumRegisters(CLI.RetTy->getContext(), RetVT);
9674       unsigned RegisterVTByteSZ = RegisterVT.getSizeInBits() / 8;
9675       RetTys.append(NumRegs, RegisterVT);
9676       for (unsigned j = 0; j != NumRegs; ++j)
9677         Offsets.push_back(Offset + j * RegisterVTByteSZ);
9678     }
9679   }
9680 
9681   SmallVector<ISD::OutputArg, 4> Outs;
9682   GetReturnInfo(CLI.CallConv, CLI.RetTy, getReturnAttrs(CLI), Outs, *this, DL);
9683 
9684   bool CanLowerReturn =
9685       this->CanLowerReturn(CLI.CallConv, CLI.DAG.getMachineFunction(),
9686                            CLI.IsVarArg, Outs, CLI.RetTy->getContext());
9687 
9688   SDValue DemoteStackSlot;
9689   int DemoteStackIdx = -100;
9690   if (!CanLowerReturn) {
9691     // FIXME: equivalent assert?
9692     // assert(!CS.hasInAllocaArgument() &&
9693     //        "sret demotion is incompatible with inalloca");
9694     uint64_t TySize = DL.getTypeAllocSize(CLI.RetTy);
9695     Align Alignment = DL.getPrefTypeAlign(CLI.RetTy);
9696     MachineFunction &MF = CLI.DAG.getMachineFunction();
9697     DemoteStackIdx =
9698         MF.getFrameInfo().CreateStackObject(TySize, Alignment, false);
9699     Type *StackSlotPtrType = PointerType::get(CLI.RetTy,
9700                                               DL.getAllocaAddrSpace());
9701 
9702     DemoteStackSlot = CLI.DAG.getFrameIndex(DemoteStackIdx, getFrameIndexTy(DL));
9703     ArgListEntry Entry;
9704     Entry.Node = DemoteStackSlot;
9705     Entry.Ty = StackSlotPtrType;
9706     Entry.IsSExt = false;
9707     Entry.IsZExt = false;
9708     Entry.IsInReg = false;
9709     Entry.IsSRet = true;
9710     Entry.IsNest = false;
9711     Entry.IsByVal = false;
9712     Entry.IsByRef = false;
9713     Entry.IsReturned = false;
9714     Entry.IsSwiftSelf = false;
9715     Entry.IsSwiftAsync = false;
9716     Entry.IsSwiftError = false;
9717     Entry.IsCFGuardTarget = false;
9718     Entry.Alignment = Alignment;
9719     CLI.getArgs().insert(CLI.getArgs().begin(), Entry);
9720     CLI.NumFixedArgs += 1;
9721     CLI.RetTy = Type::getVoidTy(CLI.RetTy->getContext());
9722 
9723     // sret demotion isn't compatible with tail-calls, since the sret argument
9724     // points into the callers stack frame.
9725     CLI.IsTailCall = false;
9726   } else {
9727     bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
9728         CLI.RetTy, CLI.CallConv, CLI.IsVarArg, DL);
9729     for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
9730       ISD::ArgFlagsTy Flags;
9731       if (NeedsRegBlock) {
9732         Flags.setInConsecutiveRegs();
9733         if (I == RetTys.size() - 1)
9734           Flags.setInConsecutiveRegsLast();
9735       }
9736       EVT VT = RetTys[I];
9737       MVT RegisterVT = getRegisterTypeForCallingConv(CLI.RetTy->getContext(),
9738                                                      CLI.CallConv, VT);
9739       unsigned NumRegs = getNumRegistersForCallingConv(CLI.RetTy->getContext(),
9740                                                        CLI.CallConv, VT);
9741       for (unsigned i = 0; i != NumRegs; ++i) {
9742         ISD::InputArg MyFlags;
9743         MyFlags.Flags = Flags;
9744         MyFlags.VT = RegisterVT;
9745         MyFlags.ArgVT = VT;
9746         MyFlags.Used = CLI.IsReturnValueUsed;
9747         if (CLI.RetTy->isPointerTy()) {
9748           MyFlags.Flags.setPointer();
9749           MyFlags.Flags.setPointerAddrSpace(
9750               cast<PointerType>(CLI.RetTy)->getAddressSpace());
9751         }
9752         if (CLI.RetSExt)
9753           MyFlags.Flags.setSExt();
9754         if (CLI.RetZExt)
9755           MyFlags.Flags.setZExt();
9756         if (CLI.IsInReg)
9757           MyFlags.Flags.setInReg();
9758         CLI.Ins.push_back(MyFlags);
9759       }
9760     }
9761   }
9762 
9763   // We push in swifterror return as the last element of CLI.Ins.
9764   ArgListTy &Args = CLI.getArgs();
9765   if (supportSwiftError()) {
9766     for (const ArgListEntry &Arg : Args) {
9767       if (Arg.IsSwiftError) {
9768         ISD::InputArg MyFlags;
9769         MyFlags.VT = getPointerTy(DL);
9770         MyFlags.ArgVT = EVT(getPointerTy(DL));
9771         MyFlags.Flags.setSwiftError();
9772         CLI.Ins.push_back(MyFlags);
9773       }
9774     }
9775   }
9776 
9777   // Handle all of the outgoing arguments.
9778   CLI.Outs.clear();
9779   CLI.OutVals.clear();
9780   for (unsigned i = 0, e = Args.size(); i != e; ++i) {
9781     SmallVector<EVT, 4> ValueVTs;
9782     ComputeValueVTs(*this, DL, Args[i].Ty, ValueVTs);
9783     // FIXME: Split arguments if CLI.IsPostTypeLegalization
9784     Type *FinalType = Args[i].Ty;
9785     if (Args[i].IsByVal)
9786       FinalType = Args[i].IndirectType;
9787     bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
9788         FinalType, CLI.CallConv, CLI.IsVarArg, DL);
9789     for (unsigned Value = 0, NumValues = ValueVTs.size(); Value != NumValues;
9790          ++Value) {
9791       EVT VT = ValueVTs[Value];
9792       Type *ArgTy = VT.getTypeForEVT(CLI.RetTy->getContext());
9793       SDValue Op = SDValue(Args[i].Node.getNode(),
9794                            Args[i].Node.getResNo() + Value);
9795       ISD::ArgFlagsTy Flags;
9796 
9797       // Certain targets (such as MIPS), may have a different ABI alignment
9798       // for a type depending on the context. Give the target a chance to
9799       // specify the alignment it wants.
9800       const Align OriginalAlignment(getABIAlignmentForCallingConv(ArgTy, DL));
9801       Flags.setOrigAlign(OriginalAlignment);
9802 
9803       if (Args[i].Ty->isPointerTy()) {
9804         Flags.setPointer();
9805         Flags.setPointerAddrSpace(
9806             cast<PointerType>(Args[i].Ty)->getAddressSpace());
9807       }
9808       if (Args[i].IsZExt)
9809         Flags.setZExt();
9810       if (Args[i].IsSExt)
9811         Flags.setSExt();
9812       if (Args[i].IsInReg) {
9813         // If we are using vectorcall calling convention, a structure that is
9814         // passed InReg - is surely an HVA
9815         if (CLI.CallConv == CallingConv::X86_VectorCall &&
9816             isa<StructType>(FinalType)) {
9817           // The first value of a structure is marked
9818           if (0 == Value)
9819             Flags.setHvaStart();
9820           Flags.setHva();
9821         }
9822         // Set InReg Flag
9823         Flags.setInReg();
9824       }
9825       if (Args[i].IsSRet)
9826         Flags.setSRet();
9827       if (Args[i].IsSwiftSelf)
9828         Flags.setSwiftSelf();
9829       if (Args[i].IsSwiftAsync)
9830         Flags.setSwiftAsync();
9831       if (Args[i].IsSwiftError)
9832         Flags.setSwiftError();
9833       if (Args[i].IsCFGuardTarget)
9834         Flags.setCFGuardTarget();
9835       if (Args[i].IsByVal)
9836         Flags.setByVal();
9837       if (Args[i].IsByRef)
9838         Flags.setByRef();
9839       if (Args[i].IsPreallocated) {
9840         Flags.setPreallocated();
9841         // Set the byval flag for CCAssignFn callbacks that don't know about
9842         // preallocated.  This way we can know how many bytes we should've
9843         // allocated and how many bytes a callee cleanup function will pop.  If
9844         // we port preallocated to more targets, we'll have to add custom
9845         // preallocated handling in the various CC lowering callbacks.
9846         Flags.setByVal();
9847       }
9848       if (Args[i].IsInAlloca) {
9849         Flags.setInAlloca();
9850         // Set the byval flag for CCAssignFn callbacks that don't know about
9851         // inalloca.  This way we can know how many bytes we should've allocated
9852         // and how many bytes a callee cleanup function will pop.  If we port
9853         // inalloca to more targets, we'll have to add custom inalloca handling
9854         // in the various CC lowering callbacks.
9855         Flags.setByVal();
9856       }
9857       Align MemAlign;
9858       if (Args[i].IsByVal || Args[i].IsInAlloca || Args[i].IsPreallocated) {
9859         unsigned FrameSize = DL.getTypeAllocSize(Args[i].IndirectType);
9860         Flags.setByValSize(FrameSize);
9861 
9862         // info is not there but there are cases it cannot get right.
9863         if (auto MA = Args[i].Alignment)
9864           MemAlign = *MA;
9865         else
9866           MemAlign = Align(getByValTypeAlignment(Args[i].IndirectType, DL));
9867       } else if (auto MA = Args[i].Alignment) {
9868         MemAlign = *MA;
9869       } else {
9870         MemAlign = OriginalAlignment;
9871       }
9872       Flags.setMemAlign(MemAlign);
9873       if (Args[i].IsNest)
9874         Flags.setNest();
9875       if (NeedsRegBlock)
9876         Flags.setInConsecutiveRegs();
9877 
9878       MVT PartVT = getRegisterTypeForCallingConv(CLI.RetTy->getContext(),
9879                                                  CLI.CallConv, VT);
9880       unsigned NumParts = getNumRegistersForCallingConv(CLI.RetTy->getContext(),
9881                                                         CLI.CallConv, VT);
9882       SmallVector<SDValue, 4> Parts(NumParts);
9883       ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
9884 
9885       if (Args[i].IsSExt)
9886         ExtendKind = ISD::SIGN_EXTEND;
9887       else if (Args[i].IsZExt)
9888         ExtendKind = ISD::ZERO_EXTEND;
9889 
9890       // Conservatively only handle 'returned' on non-vectors that can be lowered,
9891       // for now.
9892       if (Args[i].IsReturned && !Op.getValueType().isVector() &&
9893           CanLowerReturn) {
9894         assert((CLI.RetTy == Args[i].Ty ||
9895                 (CLI.RetTy->isPointerTy() && Args[i].Ty->isPointerTy() &&
9896                  CLI.RetTy->getPointerAddressSpace() ==
9897                      Args[i].Ty->getPointerAddressSpace())) &&
9898                RetTys.size() == NumValues && "unexpected use of 'returned'");
9899         // Before passing 'returned' to the target lowering code, ensure that
9900         // either the register MVT and the actual EVT are the same size or that
9901         // the return value and argument are extended in the same way; in these
9902         // cases it's safe to pass the argument register value unchanged as the
9903         // return register value (although it's at the target's option whether
9904         // to do so)
9905         // TODO: allow code generation to take advantage of partially preserved
9906         // registers rather than clobbering the entire register when the
9907         // parameter extension method is not compatible with the return
9908         // extension method
9909         if ((NumParts * PartVT.getSizeInBits() == VT.getSizeInBits()) ||
9910             (ExtendKind != ISD::ANY_EXTEND && CLI.RetSExt == Args[i].IsSExt &&
9911              CLI.RetZExt == Args[i].IsZExt))
9912           Flags.setReturned();
9913       }
9914 
9915       getCopyToParts(CLI.DAG, CLI.DL, Op, &Parts[0], NumParts, PartVT, CLI.CB,
9916                      CLI.CallConv, ExtendKind);
9917 
9918       for (unsigned j = 0; j != NumParts; ++j) {
9919         // if it isn't first piece, alignment must be 1
9920         // For scalable vectors the scalable part is currently handled
9921         // by individual targets, so we just use the known minimum size here.
9922         ISD::OutputArg MyFlags(
9923             Flags, Parts[j].getValueType().getSimpleVT(), VT,
9924             i < CLI.NumFixedArgs, i,
9925             j * Parts[j].getValueType().getStoreSize().getKnownMinSize());
9926         if (NumParts > 1 && j == 0)
9927           MyFlags.Flags.setSplit();
9928         else if (j != 0) {
9929           MyFlags.Flags.setOrigAlign(Align(1));
9930           if (j == NumParts - 1)
9931             MyFlags.Flags.setSplitEnd();
9932         }
9933 
9934         CLI.Outs.push_back(MyFlags);
9935         CLI.OutVals.push_back(Parts[j]);
9936       }
9937 
9938       if (NeedsRegBlock && Value == NumValues - 1)
9939         CLI.Outs[CLI.Outs.size() - 1].Flags.setInConsecutiveRegsLast();
9940     }
9941   }
9942 
9943   SmallVector<SDValue, 4> InVals;
9944   CLI.Chain = LowerCall(CLI, InVals);
9945 
9946   // Update CLI.InVals to use outside of this function.
9947   CLI.InVals = InVals;
9948 
9949   // Verify that the target's LowerCall behaved as expected.
9950   assert(CLI.Chain.getNode() && CLI.Chain.getValueType() == MVT::Other &&
9951          "LowerCall didn't return a valid chain!");
9952   assert((!CLI.IsTailCall || InVals.empty()) &&
9953          "LowerCall emitted a return value for a tail call!");
9954   assert((CLI.IsTailCall || InVals.size() == CLI.Ins.size()) &&
9955          "LowerCall didn't emit the correct number of values!");
9956 
9957   // For a tail call, the return value is merely live-out and there aren't
9958   // any nodes in the DAG representing it. Return a special value to
9959   // indicate that a tail call has been emitted and no more Instructions
9960   // should be processed in the current block.
9961   if (CLI.IsTailCall) {
9962     CLI.DAG.setRoot(CLI.Chain);
9963     return std::make_pair(SDValue(), SDValue());
9964   }
9965 
9966 #ifndef NDEBUG
9967   for (unsigned i = 0, e = CLI.Ins.size(); i != e; ++i) {
9968     assert(InVals[i].getNode() && "LowerCall emitted a null value!");
9969     assert(EVT(CLI.Ins[i].VT) == InVals[i].getValueType() &&
9970            "LowerCall emitted a value with the wrong type!");
9971   }
9972 #endif
9973 
9974   SmallVector<SDValue, 4> ReturnValues;
9975   if (!CanLowerReturn) {
9976     // The instruction result is the result of loading from the
9977     // hidden sret parameter.
9978     SmallVector<EVT, 1> PVTs;
9979     Type *PtrRetTy = OrigRetTy->getPointerTo(DL.getAllocaAddrSpace());
9980 
9981     ComputeValueVTs(*this, DL, PtrRetTy, PVTs);
9982     assert(PVTs.size() == 1 && "Pointers should fit in one register");
9983     EVT PtrVT = PVTs[0];
9984 
9985     unsigned NumValues = RetTys.size();
9986     ReturnValues.resize(NumValues);
9987     SmallVector<SDValue, 4> Chains(NumValues);
9988 
9989     // An aggregate return value cannot wrap around the address space, so
9990     // offsets to its parts don't wrap either.
9991     SDNodeFlags Flags;
9992     Flags.setNoUnsignedWrap(true);
9993 
9994     MachineFunction &MF = CLI.DAG.getMachineFunction();
9995     Align HiddenSRetAlign = MF.getFrameInfo().getObjectAlign(DemoteStackIdx);
9996     for (unsigned i = 0; i < NumValues; ++i) {
9997       SDValue Add = CLI.DAG.getNode(ISD::ADD, CLI.DL, PtrVT, DemoteStackSlot,
9998                                     CLI.DAG.getConstant(Offsets[i], CLI.DL,
9999                                                         PtrVT), Flags);
10000       SDValue L = CLI.DAG.getLoad(
10001           RetTys[i], CLI.DL, CLI.Chain, Add,
10002           MachinePointerInfo::getFixedStack(CLI.DAG.getMachineFunction(),
10003                                             DemoteStackIdx, Offsets[i]),
10004           HiddenSRetAlign);
10005       ReturnValues[i] = L;
10006       Chains[i] = L.getValue(1);
10007     }
10008 
10009     CLI.Chain = CLI.DAG.getNode(ISD::TokenFactor, CLI.DL, MVT::Other, Chains);
10010   } else {
10011     // Collect the legal value parts into potentially illegal values
10012     // that correspond to the original function's return values.
10013     Optional<ISD::NodeType> AssertOp;
10014     if (CLI.RetSExt)
10015       AssertOp = ISD::AssertSext;
10016     else if (CLI.RetZExt)
10017       AssertOp = ISD::AssertZext;
10018     unsigned CurReg = 0;
10019     for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
10020       EVT VT = RetTys[I];
10021       MVT RegisterVT = getRegisterTypeForCallingConv(CLI.RetTy->getContext(),
10022                                                      CLI.CallConv, VT);
10023       unsigned NumRegs = getNumRegistersForCallingConv(CLI.RetTy->getContext(),
10024                                                        CLI.CallConv, VT);
10025 
10026       ReturnValues.push_back(getCopyFromParts(CLI.DAG, CLI.DL, &InVals[CurReg],
10027                                               NumRegs, RegisterVT, VT, nullptr,
10028                                               CLI.CallConv, AssertOp));
10029       CurReg += NumRegs;
10030     }
10031 
10032     // For a function returning void, there is no return value. We can't create
10033     // such a node, so we just return a null return value in that case. In
10034     // that case, nothing will actually look at the value.
10035     if (ReturnValues.empty())
10036       return std::make_pair(SDValue(), CLI.Chain);
10037   }
10038 
10039   SDValue Res = CLI.DAG.getNode(ISD::MERGE_VALUES, CLI.DL,
10040                                 CLI.DAG.getVTList(RetTys), ReturnValues);
10041   return std::make_pair(Res, CLI.Chain);
10042 }
10043 
10044 /// Places new result values for the node in Results (their number
10045 /// and types must exactly match those of the original return values of
10046 /// the node), or leaves Results empty, which indicates that the node is not
10047 /// to be custom lowered after all.
10048 void TargetLowering::LowerOperationWrapper(SDNode *N,
10049                                            SmallVectorImpl<SDValue> &Results,
10050                                            SelectionDAG &DAG) const {
10051   SDValue Res = LowerOperation(SDValue(N, 0), DAG);
10052 
10053   if (!Res.getNode())
10054     return;
10055 
10056   // If the original node has one result, take the return value from
10057   // LowerOperation as is. It might not be result number 0.
10058   if (N->getNumValues() == 1) {
10059     Results.push_back(Res);
10060     return;
10061   }
10062 
10063   // If the original node has multiple results, then the return node should
10064   // have the same number of results.
10065   assert((N->getNumValues() == Res->getNumValues()) &&
10066       "Lowering returned the wrong number of results!");
10067 
10068   // Places new result values base on N result number.
10069   for (unsigned I = 0, E = N->getNumValues(); I != E; ++I)
10070     Results.push_back(Res.getValue(I));
10071 }
10072 
10073 SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
10074   llvm_unreachable("LowerOperation not implemented for this target!");
10075 }
10076 
10077 void SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V,
10078                                                      unsigned Reg,
10079                                                      ISD::NodeType ExtendType) {
10080   SDValue Op = getNonRegisterValue(V);
10081   assert((Op.getOpcode() != ISD::CopyFromReg ||
10082           cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
10083          "Copy from a reg to the same reg!");
10084   assert(!Register::isPhysicalRegister(Reg) && "Is a physreg");
10085 
10086   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10087   // If this is an InlineAsm we have to match the registers required, not the
10088   // notional registers required by the type.
10089 
10090   RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg, V->getType(),
10091                    None); // This is not an ABI copy.
10092   SDValue Chain = DAG.getEntryNode();
10093 
10094   if (ExtendType == ISD::ANY_EXTEND) {
10095     auto PreferredExtendIt = FuncInfo.PreferredExtendType.find(V);
10096     if (PreferredExtendIt != FuncInfo.PreferredExtendType.end())
10097       ExtendType = PreferredExtendIt->second;
10098   }
10099   RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, nullptr, V, ExtendType);
10100   PendingExports.push_back(Chain);
10101 }
10102 
10103 #include "llvm/CodeGen/SelectionDAGISel.h"
10104 
10105 /// isOnlyUsedInEntryBlock - If the specified argument is only used in the
10106 /// entry block, return true.  This includes arguments used by switches, since
10107 /// the switch may expand into multiple basic blocks.
10108 static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel) {
10109   // With FastISel active, we may be splitting blocks, so force creation
10110   // of virtual registers for all non-dead arguments.
10111   if (FastISel)
10112     return A->use_empty();
10113 
10114   const BasicBlock &Entry = A->getParent()->front();
10115   for (const User *U : A->users())
10116     if (cast<Instruction>(U)->getParent() != &Entry || isa<SwitchInst>(U))
10117       return false;  // Use not in entry block.
10118 
10119   return true;
10120 }
10121 
10122 using ArgCopyElisionMapTy =
10123     DenseMap<const Argument *,
10124              std::pair<const AllocaInst *, const StoreInst *>>;
10125 
10126 /// Scan the entry block of the function in FuncInfo for arguments that look
10127 /// like copies into a local alloca. Record any copied arguments in
10128 /// ArgCopyElisionCandidates.
10129 static void
10130 findArgumentCopyElisionCandidates(const DataLayout &DL,
10131                                   FunctionLoweringInfo *FuncInfo,
10132                                   ArgCopyElisionMapTy &ArgCopyElisionCandidates) {
10133   // Record the state of every static alloca used in the entry block. Argument
10134   // allocas are all used in the entry block, so we need approximately as many
10135   // entries as we have arguments.
10136   enum StaticAllocaInfo { Unknown, Clobbered, Elidable };
10137   SmallDenseMap<const AllocaInst *, StaticAllocaInfo, 8> StaticAllocas;
10138   unsigned NumArgs = FuncInfo->Fn->arg_size();
10139   StaticAllocas.reserve(NumArgs * 2);
10140 
10141   auto GetInfoIfStaticAlloca = [&](const Value *V) -> StaticAllocaInfo * {
10142     if (!V)
10143       return nullptr;
10144     V = V->stripPointerCasts();
10145     const auto *AI = dyn_cast<AllocaInst>(V);
10146     if (!AI || !AI->isStaticAlloca() || !FuncInfo->StaticAllocaMap.count(AI))
10147       return nullptr;
10148     auto Iter = StaticAllocas.insert({AI, Unknown});
10149     return &Iter.first->second;
10150   };
10151 
10152   // Look for stores of arguments to static allocas. Look through bitcasts and
10153   // GEPs to handle type coercions, as long as the alloca is fully initialized
10154   // by the store. Any non-store use of an alloca escapes it and any subsequent
10155   // unanalyzed store might write it.
10156   // FIXME: Handle structs initialized with multiple stores.
10157   for (const Instruction &I : FuncInfo->Fn->getEntryBlock()) {
10158     // Look for stores, and handle non-store uses conservatively.
10159     const auto *SI = dyn_cast<StoreInst>(&I);
10160     if (!SI) {
10161       // We will look through cast uses, so ignore them completely.
10162       if (I.isCast())
10163         continue;
10164       // Ignore debug info and pseudo op intrinsics, they don't escape or store
10165       // to allocas.
10166       if (I.isDebugOrPseudoInst())
10167         continue;
10168       // This is an unknown instruction. Assume it escapes or writes to all
10169       // static alloca operands.
10170       for (const Use &U : I.operands()) {
10171         if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(U))
10172           *Info = StaticAllocaInfo::Clobbered;
10173       }
10174       continue;
10175     }
10176 
10177     // If the stored value is a static alloca, mark it as escaped.
10178     if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(SI->getValueOperand()))
10179       *Info = StaticAllocaInfo::Clobbered;
10180 
10181     // Check if the destination is a static alloca.
10182     const Value *Dst = SI->getPointerOperand()->stripPointerCasts();
10183     StaticAllocaInfo *Info = GetInfoIfStaticAlloca(Dst);
10184     if (!Info)
10185       continue;
10186     const AllocaInst *AI = cast<AllocaInst>(Dst);
10187 
10188     // Skip allocas that have been initialized or clobbered.
10189     if (*Info != StaticAllocaInfo::Unknown)
10190       continue;
10191 
10192     // Check if the stored value is an argument, and that this store fully
10193     // initializes the alloca.
10194     // If the argument type has padding bits we can't directly forward a pointer
10195     // as the upper bits may contain garbage.
10196     // Don't elide copies from the same argument twice.
10197     const Value *Val = SI->getValueOperand()->stripPointerCasts();
10198     const auto *Arg = dyn_cast<Argument>(Val);
10199     if (!Arg || Arg->hasPassPointeeByValueCopyAttr() ||
10200         Arg->getType()->isEmptyTy() ||
10201         DL.getTypeStoreSize(Arg->getType()) !=
10202             DL.getTypeAllocSize(AI->getAllocatedType()) ||
10203         !DL.typeSizeEqualsStoreSize(Arg->getType()) ||
10204         ArgCopyElisionCandidates.count(Arg)) {
10205       *Info = StaticAllocaInfo::Clobbered;
10206       continue;
10207     }
10208 
10209     LLVM_DEBUG(dbgs() << "Found argument copy elision candidate: " << *AI
10210                       << '\n');
10211 
10212     // Mark this alloca and store for argument copy elision.
10213     *Info = StaticAllocaInfo::Elidable;
10214     ArgCopyElisionCandidates.insert({Arg, {AI, SI}});
10215 
10216     // Stop scanning if we've seen all arguments. This will happen early in -O0
10217     // builds, which is useful, because -O0 builds have large entry blocks and
10218     // many allocas.
10219     if (ArgCopyElisionCandidates.size() == NumArgs)
10220       break;
10221   }
10222 }
10223 
10224 /// Try to elide argument copies from memory into a local alloca. Succeeds if
10225 /// ArgVal is a load from a suitable fixed stack object.
10226 static void tryToElideArgumentCopy(
10227     FunctionLoweringInfo &FuncInfo, SmallVectorImpl<SDValue> &Chains,
10228     DenseMap<int, int> &ArgCopyElisionFrameIndexMap,
10229     SmallPtrSetImpl<const Instruction *> &ElidedArgCopyInstrs,
10230     ArgCopyElisionMapTy &ArgCopyElisionCandidates, const Argument &Arg,
10231     SDValue ArgVal, bool &ArgHasUses) {
10232   // Check if this is a load from a fixed stack object.
10233   auto *LNode = dyn_cast<LoadSDNode>(ArgVal);
10234   if (!LNode)
10235     return;
10236   auto *FINode = dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode());
10237   if (!FINode)
10238     return;
10239 
10240   // Check that the fixed stack object is the right size and alignment.
10241   // Look at the alignment that the user wrote on the alloca instead of looking
10242   // at the stack object.
10243   auto ArgCopyIter = ArgCopyElisionCandidates.find(&Arg);
10244   assert(ArgCopyIter != ArgCopyElisionCandidates.end());
10245   const AllocaInst *AI = ArgCopyIter->second.first;
10246   int FixedIndex = FINode->getIndex();
10247   int &AllocaIndex = FuncInfo.StaticAllocaMap[AI];
10248   int OldIndex = AllocaIndex;
10249   MachineFrameInfo &MFI = FuncInfo.MF->getFrameInfo();
10250   if (MFI.getObjectSize(FixedIndex) != MFI.getObjectSize(OldIndex)) {
10251     LLVM_DEBUG(
10252         dbgs() << "  argument copy elision failed due to bad fixed stack "
10253                   "object size\n");
10254     return;
10255   }
10256   Align RequiredAlignment = AI->getAlign();
10257   if (MFI.getObjectAlign(FixedIndex) < RequiredAlignment) {
10258     LLVM_DEBUG(dbgs() << "  argument copy elision failed: alignment of alloca "
10259                          "greater than stack argument alignment ("
10260                       << DebugStr(RequiredAlignment) << " vs "
10261                       << DebugStr(MFI.getObjectAlign(FixedIndex)) << ")\n");
10262     return;
10263   }
10264 
10265   // Perform the elision. Delete the old stack object and replace its only use
10266   // in the variable info map. Mark the stack object as mutable.
10267   LLVM_DEBUG({
10268     dbgs() << "Eliding argument copy from " << Arg << " to " << *AI << '\n'
10269            << "  Replacing frame index " << OldIndex << " with " << FixedIndex
10270            << '\n';
10271   });
10272   MFI.RemoveStackObject(OldIndex);
10273   MFI.setIsImmutableObjectIndex(FixedIndex, false);
10274   AllocaIndex = FixedIndex;
10275   ArgCopyElisionFrameIndexMap.insert({OldIndex, FixedIndex});
10276   Chains.push_back(ArgVal.getValue(1));
10277 
10278   // Avoid emitting code for the store implementing the copy.
10279   const StoreInst *SI = ArgCopyIter->second.second;
10280   ElidedArgCopyInstrs.insert(SI);
10281 
10282   // Check for uses of the argument again so that we can avoid exporting ArgVal
10283   // if it is't used by anything other than the store.
10284   for (const Value *U : Arg.users()) {
10285     if (U != SI) {
10286       ArgHasUses = true;
10287       break;
10288     }
10289   }
10290 }
10291 
10292 void SelectionDAGISel::LowerArguments(const Function &F) {
10293   SelectionDAG &DAG = SDB->DAG;
10294   SDLoc dl = SDB->getCurSDLoc();
10295   const DataLayout &DL = DAG.getDataLayout();
10296   SmallVector<ISD::InputArg, 16> Ins;
10297 
10298   // In Naked functions we aren't going to save any registers.
10299   if (F.hasFnAttribute(Attribute::Naked))
10300     return;
10301 
10302   if (!FuncInfo->CanLowerReturn) {
10303     // Put in an sret pointer parameter before all the other parameters.
10304     SmallVector<EVT, 1> ValueVTs;
10305     ComputeValueVTs(*TLI, DAG.getDataLayout(),
10306                     F.getReturnType()->getPointerTo(
10307                         DAG.getDataLayout().getAllocaAddrSpace()),
10308                     ValueVTs);
10309 
10310     // NOTE: Assuming that a pointer will never break down to more than one VT
10311     // or one register.
10312     ISD::ArgFlagsTy Flags;
10313     Flags.setSRet();
10314     MVT RegisterVT = TLI->getRegisterType(*DAG.getContext(), ValueVTs[0]);
10315     ISD::InputArg RetArg(Flags, RegisterVT, ValueVTs[0], true,
10316                          ISD::InputArg::NoArgIndex, 0);
10317     Ins.push_back(RetArg);
10318   }
10319 
10320   // Look for stores of arguments to static allocas. Mark such arguments with a
10321   // flag to ask the target to give us the memory location of that argument if
10322   // available.
10323   ArgCopyElisionMapTy ArgCopyElisionCandidates;
10324   findArgumentCopyElisionCandidates(DL, FuncInfo.get(),
10325                                     ArgCopyElisionCandidates);
10326 
10327   // Set up the incoming argument description vector.
10328   for (const Argument &Arg : F.args()) {
10329     unsigned ArgNo = Arg.getArgNo();
10330     SmallVector<EVT, 4> ValueVTs;
10331     ComputeValueVTs(*TLI, DAG.getDataLayout(), Arg.getType(), ValueVTs);
10332     bool isArgValueUsed = !Arg.use_empty();
10333     unsigned PartBase = 0;
10334     Type *FinalType = Arg.getType();
10335     if (Arg.hasAttribute(Attribute::ByVal))
10336       FinalType = Arg.getParamByValType();
10337     bool NeedsRegBlock = TLI->functionArgumentNeedsConsecutiveRegisters(
10338         FinalType, F.getCallingConv(), F.isVarArg(), DL);
10339     for (unsigned Value = 0, NumValues = ValueVTs.size();
10340          Value != NumValues; ++Value) {
10341       EVT VT = ValueVTs[Value];
10342       Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
10343       ISD::ArgFlagsTy Flags;
10344 
10345 
10346       if (Arg.getType()->isPointerTy()) {
10347         Flags.setPointer();
10348         Flags.setPointerAddrSpace(
10349             cast<PointerType>(Arg.getType())->getAddressSpace());
10350       }
10351       if (Arg.hasAttribute(Attribute::ZExt))
10352         Flags.setZExt();
10353       if (Arg.hasAttribute(Attribute::SExt))
10354         Flags.setSExt();
10355       if (Arg.hasAttribute(Attribute::InReg)) {
10356         // If we are using vectorcall calling convention, a structure that is
10357         // passed InReg - is surely an HVA
10358         if (F.getCallingConv() == CallingConv::X86_VectorCall &&
10359             isa<StructType>(Arg.getType())) {
10360           // The first value of a structure is marked
10361           if (0 == Value)
10362             Flags.setHvaStart();
10363           Flags.setHva();
10364         }
10365         // Set InReg Flag
10366         Flags.setInReg();
10367       }
10368       if (Arg.hasAttribute(Attribute::StructRet))
10369         Flags.setSRet();
10370       if (Arg.hasAttribute(Attribute::SwiftSelf))
10371         Flags.setSwiftSelf();
10372       if (Arg.hasAttribute(Attribute::SwiftAsync))
10373         Flags.setSwiftAsync();
10374       if (Arg.hasAttribute(Attribute::SwiftError))
10375         Flags.setSwiftError();
10376       if (Arg.hasAttribute(Attribute::ByVal))
10377         Flags.setByVal();
10378       if (Arg.hasAttribute(Attribute::ByRef))
10379         Flags.setByRef();
10380       if (Arg.hasAttribute(Attribute::InAlloca)) {
10381         Flags.setInAlloca();
10382         // Set the byval flag for CCAssignFn callbacks that don't know about
10383         // inalloca.  This way we can know how many bytes we should've allocated
10384         // and how many bytes a callee cleanup function will pop.  If we port
10385         // inalloca to more targets, we'll have to add custom inalloca handling
10386         // in the various CC lowering callbacks.
10387         Flags.setByVal();
10388       }
10389       if (Arg.hasAttribute(Attribute::Preallocated)) {
10390         Flags.setPreallocated();
10391         // Set the byval flag for CCAssignFn callbacks that don't know about
10392         // preallocated.  This way we can know how many bytes we should've
10393         // allocated and how many bytes a callee cleanup function will pop.  If
10394         // we port preallocated to more targets, we'll have to add custom
10395         // preallocated handling in the various CC lowering callbacks.
10396         Flags.setByVal();
10397       }
10398 
10399       // Certain targets (such as MIPS), may have a different ABI alignment
10400       // for a type depending on the context. Give the target a chance to
10401       // specify the alignment it wants.
10402       const Align OriginalAlignment(
10403           TLI->getABIAlignmentForCallingConv(ArgTy, DL));
10404       Flags.setOrigAlign(OriginalAlignment);
10405 
10406       Align MemAlign;
10407       Type *ArgMemTy = nullptr;
10408       if (Flags.isByVal() || Flags.isInAlloca() || Flags.isPreallocated() ||
10409           Flags.isByRef()) {
10410         if (!ArgMemTy)
10411           ArgMemTy = Arg.getPointeeInMemoryValueType();
10412 
10413         uint64_t MemSize = DL.getTypeAllocSize(ArgMemTy);
10414 
10415         // For in-memory arguments, size and alignment should be passed from FE.
10416         // BE will guess if this info is not there but there are cases it cannot
10417         // get right.
10418         if (auto ParamAlign = Arg.getParamStackAlign())
10419           MemAlign = *ParamAlign;
10420         else if ((ParamAlign = Arg.getParamAlign()))
10421           MemAlign = *ParamAlign;
10422         else
10423           MemAlign = Align(TLI->getByValTypeAlignment(ArgMemTy, DL));
10424         if (Flags.isByRef())
10425           Flags.setByRefSize(MemSize);
10426         else
10427           Flags.setByValSize(MemSize);
10428       } else if (auto ParamAlign = Arg.getParamStackAlign()) {
10429         MemAlign = *ParamAlign;
10430       } else {
10431         MemAlign = OriginalAlignment;
10432       }
10433       Flags.setMemAlign(MemAlign);
10434 
10435       if (Arg.hasAttribute(Attribute::Nest))
10436         Flags.setNest();
10437       if (NeedsRegBlock)
10438         Flags.setInConsecutiveRegs();
10439       if (ArgCopyElisionCandidates.count(&Arg))
10440         Flags.setCopyElisionCandidate();
10441       if (Arg.hasAttribute(Attribute::Returned))
10442         Flags.setReturned();
10443 
10444       MVT RegisterVT = TLI->getRegisterTypeForCallingConv(
10445           *CurDAG->getContext(), F.getCallingConv(), VT);
10446       unsigned NumRegs = TLI->getNumRegistersForCallingConv(
10447           *CurDAG->getContext(), F.getCallingConv(), VT);
10448       for (unsigned i = 0; i != NumRegs; ++i) {
10449         // For scalable vectors, use the minimum size; individual targets
10450         // are responsible for handling scalable vector arguments and
10451         // return values.
10452         ISD::InputArg MyFlags(Flags, RegisterVT, VT, isArgValueUsed,
10453                  ArgNo, PartBase+i*RegisterVT.getStoreSize().getKnownMinSize());
10454         if (NumRegs > 1 && i == 0)
10455           MyFlags.Flags.setSplit();
10456         // if it isn't first piece, alignment must be 1
10457         else if (i > 0) {
10458           MyFlags.Flags.setOrigAlign(Align(1));
10459           if (i == NumRegs - 1)
10460             MyFlags.Flags.setSplitEnd();
10461         }
10462         Ins.push_back(MyFlags);
10463       }
10464       if (NeedsRegBlock && Value == NumValues - 1)
10465         Ins[Ins.size() - 1].Flags.setInConsecutiveRegsLast();
10466       PartBase += VT.getStoreSize().getKnownMinSize();
10467     }
10468   }
10469 
10470   // Call the target to set up the argument values.
10471   SmallVector<SDValue, 8> InVals;
10472   SDValue NewRoot = TLI->LowerFormalArguments(
10473       DAG.getRoot(), F.getCallingConv(), F.isVarArg(), Ins, dl, DAG, InVals);
10474 
10475   // Verify that the target's LowerFormalArguments behaved as expected.
10476   assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
10477          "LowerFormalArguments didn't return a valid chain!");
10478   assert(InVals.size() == Ins.size() &&
10479          "LowerFormalArguments didn't emit the correct number of values!");
10480   LLVM_DEBUG({
10481     for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
10482       assert(InVals[i].getNode() &&
10483              "LowerFormalArguments emitted a null value!");
10484       assert(EVT(Ins[i].VT) == InVals[i].getValueType() &&
10485              "LowerFormalArguments emitted a value with the wrong type!");
10486     }
10487   });
10488 
10489   // Update the DAG with the new chain value resulting from argument lowering.
10490   DAG.setRoot(NewRoot);
10491 
10492   // Set up the argument values.
10493   unsigned i = 0;
10494   if (!FuncInfo->CanLowerReturn) {
10495     // Create a virtual register for the sret pointer, and put in a copy
10496     // from the sret argument into it.
10497     SmallVector<EVT, 1> ValueVTs;
10498     ComputeValueVTs(*TLI, DAG.getDataLayout(),
10499                     F.getReturnType()->getPointerTo(
10500                         DAG.getDataLayout().getAllocaAddrSpace()),
10501                     ValueVTs);
10502     MVT VT = ValueVTs[0].getSimpleVT();
10503     MVT RegVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
10504     Optional<ISD::NodeType> AssertOp = None;
10505     SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1, RegVT, VT,
10506                                         nullptr, F.getCallingConv(), AssertOp);
10507 
10508     MachineFunction& MF = SDB->DAG.getMachineFunction();
10509     MachineRegisterInfo& RegInfo = MF.getRegInfo();
10510     Register SRetReg =
10511         RegInfo.createVirtualRegister(TLI->getRegClassFor(RegVT));
10512     FuncInfo->DemoteRegister = SRetReg;
10513     NewRoot =
10514         SDB->DAG.getCopyToReg(NewRoot, SDB->getCurSDLoc(), SRetReg, ArgValue);
10515     DAG.setRoot(NewRoot);
10516 
10517     // i indexes lowered arguments.  Bump it past the hidden sret argument.
10518     ++i;
10519   }
10520 
10521   SmallVector<SDValue, 4> Chains;
10522   DenseMap<int, int> ArgCopyElisionFrameIndexMap;
10523   for (const Argument &Arg : F.args()) {
10524     SmallVector<SDValue, 4> ArgValues;
10525     SmallVector<EVT, 4> ValueVTs;
10526     ComputeValueVTs(*TLI, DAG.getDataLayout(), Arg.getType(), ValueVTs);
10527     unsigned NumValues = ValueVTs.size();
10528     if (NumValues == 0)
10529       continue;
10530 
10531     bool ArgHasUses = !Arg.use_empty();
10532 
10533     // Elide the copying store if the target loaded this argument from a
10534     // suitable fixed stack object.
10535     if (Ins[i].Flags.isCopyElisionCandidate()) {
10536       tryToElideArgumentCopy(*FuncInfo, Chains, ArgCopyElisionFrameIndexMap,
10537                              ElidedArgCopyInstrs, ArgCopyElisionCandidates, Arg,
10538                              InVals[i], ArgHasUses);
10539     }
10540 
10541     // If this argument is unused then remember its value. It is used to generate
10542     // debugging information.
10543     bool isSwiftErrorArg =
10544         TLI->supportSwiftError() &&
10545         Arg.hasAttribute(Attribute::SwiftError);
10546     if (!ArgHasUses && !isSwiftErrorArg) {
10547       SDB->setUnusedArgValue(&Arg, InVals[i]);
10548 
10549       // Also remember any frame index for use in FastISel.
10550       if (FrameIndexSDNode *FI =
10551           dyn_cast<FrameIndexSDNode>(InVals[i].getNode()))
10552         FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
10553     }
10554 
10555     for (unsigned Val = 0; Val != NumValues; ++Val) {
10556       EVT VT = ValueVTs[Val];
10557       MVT PartVT = TLI->getRegisterTypeForCallingConv(*CurDAG->getContext(),
10558                                                       F.getCallingConv(), VT);
10559       unsigned NumParts = TLI->getNumRegistersForCallingConv(
10560           *CurDAG->getContext(), F.getCallingConv(), VT);
10561 
10562       // Even an apparent 'unused' swifterror argument needs to be returned. So
10563       // we do generate a copy for it that can be used on return from the
10564       // function.
10565       if (ArgHasUses || isSwiftErrorArg) {
10566         Optional<ISD::NodeType> AssertOp;
10567         if (Arg.hasAttribute(Attribute::SExt))
10568           AssertOp = ISD::AssertSext;
10569         else if (Arg.hasAttribute(Attribute::ZExt))
10570           AssertOp = ISD::AssertZext;
10571 
10572         ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i], NumParts,
10573                                              PartVT, VT, nullptr,
10574                                              F.getCallingConv(), AssertOp));
10575       }
10576 
10577       i += NumParts;
10578     }
10579 
10580     // We don't need to do anything else for unused arguments.
10581     if (ArgValues.empty())
10582       continue;
10583 
10584     // Note down frame index.
10585     if (FrameIndexSDNode *FI =
10586         dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode()))
10587       FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
10588 
10589     SDValue Res = DAG.getMergeValues(makeArrayRef(ArgValues.data(), NumValues),
10590                                      SDB->getCurSDLoc());
10591 
10592     SDB->setValue(&Arg, Res);
10593     if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::BUILD_PAIR) {
10594       // We want to associate the argument with the frame index, among
10595       // involved operands, that correspond to the lowest address. The
10596       // getCopyFromParts function, called earlier, is swapping the order of
10597       // the operands to BUILD_PAIR depending on endianness. The result of
10598       // that swapping is that the least significant bits of the argument will
10599       // be in the first operand of the BUILD_PAIR node, and the most
10600       // significant bits will be in the second operand.
10601       unsigned LowAddressOp = DAG.getDataLayout().isBigEndian() ? 1 : 0;
10602       if (LoadSDNode *LNode =
10603           dyn_cast<LoadSDNode>(Res.getOperand(LowAddressOp).getNode()))
10604         if (FrameIndexSDNode *FI =
10605             dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
10606           FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
10607     }
10608 
10609     // Analyses past this point are naive and don't expect an assertion.
10610     if (Res.getOpcode() == ISD::AssertZext)
10611       Res = Res.getOperand(0);
10612 
10613     // Update the SwiftErrorVRegDefMap.
10614     if (Res.getOpcode() == ISD::CopyFromReg && isSwiftErrorArg) {
10615       unsigned Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
10616       if (Register::isVirtualRegister(Reg))
10617         SwiftError->setCurrentVReg(FuncInfo->MBB, SwiftError->getFunctionArg(),
10618                                    Reg);
10619     }
10620 
10621     // If this argument is live outside of the entry block, insert a copy from
10622     // wherever we got it to the vreg that other BB's will reference it as.
10623     if (Res.getOpcode() == ISD::CopyFromReg) {
10624       // If we can, though, try to skip creating an unnecessary vreg.
10625       // FIXME: This isn't very clean... it would be nice to make this more
10626       // general.
10627       unsigned Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
10628       if (Register::isVirtualRegister(Reg)) {
10629         FuncInfo->ValueMap[&Arg] = Reg;
10630         continue;
10631       }
10632     }
10633     if (!isOnlyUsedInEntryBlock(&Arg, TM.Options.EnableFastISel)) {
10634       FuncInfo->InitializeRegForValue(&Arg);
10635       SDB->CopyToExportRegsIfNeeded(&Arg);
10636     }
10637   }
10638 
10639   if (!Chains.empty()) {
10640     Chains.push_back(NewRoot);
10641     NewRoot = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
10642   }
10643 
10644   DAG.setRoot(NewRoot);
10645 
10646   assert(i == InVals.size() && "Argument register count mismatch!");
10647 
10648   // If any argument copy elisions occurred and we have debug info, update the
10649   // stale frame indices used in the dbg.declare variable info table.
10650   MachineFunction::VariableDbgInfoMapTy &DbgDeclareInfo = MF->getVariableDbgInfo();
10651   if (!DbgDeclareInfo.empty() && !ArgCopyElisionFrameIndexMap.empty()) {
10652     for (MachineFunction::VariableDbgInfo &VI : DbgDeclareInfo) {
10653       auto I = ArgCopyElisionFrameIndexMap.find(VI.Slot);
10654       if (I != ArgCopyElisionFrameIndexMap.end())
10655         VI.Slot = I->second;
10656     }
10657   }
10658 
10659   // Finally, if the target has anything special to do, allow it to do so.
10660   emitFunctionEntryCode();
10661 }
10662 
10663 /// Handle PHI nodes in successor blocks.  Emit code into the SelectionDAG to
10664 /// ensure constants are generated when needed.  Remember the virtual registers
10665 /// that need to be added to the Machine PHI nodes as input.  We cannot just
10666 /// directly add them, because expansion might result in multiple MBB's for one
10667 /// BB.  As such, the start of the BB might correspond to a different MBB than
10668 /// the end.
10669 void
10670 SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
10671   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10672   const Instruction *TI = LLVMBB->getTerminator();
10673 
10674   SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
10675 
10676   // Check PHI nodes in successors that expect a value to be available from this
10677   // block.
10678   for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
10679     const BasicBlock *SuccBB = TI->getSuccessor(succ);
10680     if (!isa<PHINode>(SuccBB->begin())) continue;
10681     MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
10682 
10683     // If this terminator has multiple identical successors (common for
10684     // switches), only handle each succ once.
10685     if (!SuccsHandled.insert(SuccMBB).second)
10686       continue;
10687 
10688     MachineBasicBlock::iterator MBBI = SuccMBB->begin();
10689 
10690     // At this point we know that there is a 1-1 correspondence between LLVM PHI
10691     // nodes and Machine PHI nodes, but the incoming operands have not been
10692     // emitted yet.
10693     for (const PHINode &PN : SuccBB->phis()) {
10694       // Ignore dead phi's.
10695       if (PN.use_empty())
10696         continue;
10697 
10698       // Skip empty types
10699       if (PN.getType()->isEmptyTy())
10700         continue;
10701 
10702       unsigned Reg;
10703       const Value *PHIOp = PN.getIncomingValueForBlock(LLVMBB);
10704 
10705       if (const Constant *C = dyn_cast<Constant>(PHIOp)) {
10706         unsigned &RegOut = ConstantsOut[C];
10707         if (RegOut == 0) {
10708           RegOut = FuncInfo.CreateRegs(C);
10709           // We need to zero/sign extend ConstantInt phi operands to match
10710           // assumptions in FunctionLoweringInfo::ComputePHILiveOutRegInfo.
10711           ISD::NodeType ExtendType = ISD::ANY_EXTEND;
10712           if (auto *CI = dyn_cast<ConstantInt>(C))
10713             ExtendType = TLI.signExtendConstant(CI) ? ISD::SIGN_EXTEND
10714                                                     : ISD::ZERO_EXTEND;
10715           CopyValueToVirtualRegister(C, RegOut, ExtendType);
10716         }
10717         Reg = RegOut;
10718       } else {
10719         DenseMap<const Value *, Register>::iterator I =
10720           FuncInfo.ValueMap.find(PHIOp);
10721         if (I != FuncInfo.ValueMap.end())
10722           Reg = I->second;
10723         else {
10724           assert(isa<AllocaInst>(PHIOp) &&
10725                  FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
10726                  "Didn't codegen value into a register!??");
10727           Reg = FuncInfo.CreateRegs(PHIOp);
10728           CopyValueToVirtualRegister(PHIOp, Reg);
10729         }
10730       }
10731 
10732       // Remember that this register needs to added to the machine PHI node as
10733       // the input for this MBB.
10734       SmallVector<EVT, 4> ValueVTs;
10735       ComputeValueVTs(TLI, DAG.getDataLayout(), PN.getType(), ValueVTs);
10736       for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
10737         EVT VT = ValueVTs[vti];
10738         unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
10739         for (unsigned i = 0, e = NumRegisters; i != e; ++i)
10740           FuncInfo.PHINodesToUpdate.push_back(
10741               std::make_pair(&*MBBI++, Reg + i));
10742         Reg += NumRegisters;
10743       }
10744     }
10745   }
10746 
10747   ConstantsOut.clear();
10748 }
10749 
10750 MachineBasicBlock *SelectionDAGBuilder::NextBlock(MachineBasicBlock *MBB) {
10751   MachineFunction::iterator I(MBB);
10752   if (++I == FuncInfo.MF->end())
10753     return nullptr;
10754   return &*I;
10755 }
10756 
10757 /// During lowering new call nodes can be created (such as memset, etc.).
10758 /// Those will become new roots of the current DAG, but complications arise
10759 /// when they are tail calls. In such cases, the call lowering will update
10760 /// the root, but the builder still needs to know that a tail call has been
10761 /// lowered in order to avoid generating an additional return.
10762 void SelectionDAGBuilder::updateDAGForMaybeTailCall(SDValue MaybeTC) {
10763   // If the node is null, we do have a tail call.
10764   if (MaybeTC.getNode() != nullptr)
10765     DAG.setRoot(MaybeTC);
10766   else
10767     HasTailCall = true;
10768 }
10769 
10770 void SelectionDAGBuilder::lowerWorkItem(SwitchWorkListItem W, Value *Cond,
10771                                         MachineBasicBlock *SwitchMBB,
10772                                         MachineBasicBlock *DefaultMBB) {
10773   MachineFunction *CurMF = FuncInfo.MF;
10774   MachineBasicBlock *NextMBB = nullptr;
10775   MachineFunction::iterator BBI(W.MBB);
10776   if (++BBI != FuncInfo.MF->end())
10777     NextMBB = &*BBI;
10778 
10779   unsigned Size = W.LastCluster - W.FirstCluster + 1;
10780 
10781   BranchProbabilityInfo *BPI = FuncInfo.BPI;
10782 
10783   if (Size == 2 && W.MBB == SwitchMBB) {
10784     // If any two of the cases has the same destination, and if one value
10785     // is the same as the other, but has one bit unset that the other has set,
10786     // use bit manipulation to do two compares at once.  For example:
10787     // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
10788     // TODO: This could be extended to merge any 2 cases in switches with 3
10789     // cases.
10790     // TODO: Handle cases where W.CaseBB != SwitchBB.
10791     CaseCluster &Small = *W.FirstCluster;
10792     CaseCluster &Big = *W.LastCluster;
10793 
10794     if (Small.Low == Small.High && Big.Low == Big.High &&
10795         Small.MBB == Big.MBB) {
10796       const APInt &SmallValue = Small.Low->getValue();
10797       const APInt &BigValue = Big.Low->getValue();
10798 
10799       // Check that there is only one bit different.
10800       APInt CommonBit = BigValue ^ SmallValue;
10801       if (CommonBit.isPowerOf2()) {
10802         SDValue CondLHS = getValue(Cond);
10803         EVT VT = CondLHS.getValueType();
10804         SDLoc DL = getCurSDLoc();
10805 
10806         SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS,
10807                                  DAG.getConstant(CommonBit, DL, VT));
10808         SDValue Cond = DAG.getSetCC(
10809             DL, MVT::i1, Or, DAG.getConstant(BigValue | SmallValue, DL, VT),
10810             ISD::SETEQ);
10811 
10812         // Update successor info.
10813         // Both Small and Big will jump to Small.BB, so we sum up the
10814         // probabilities.
10815         addSuccessorWithProb(SwitchMBB, Small.MBB, Small.Prob + Big.Prob);
10816         if (BPI)
10817           addSuccessorWithProb(
10818               SwitchMBB, DefaultMBB,
10819               // The default destination is the first successor in IR.
10820               BPI->getEdgeProbability(SwitchMBB->getBasicBlock(), (unsigned)0));
10821         else
10822           addSuccessorWithProb(SwitchMBB, DefaultMBB);
10823 
10824         // Insert the true branch.
10825         SDValue BrCond =
10826             DAG.getNode(ISD::BRCOND, DL, MVT::Other, getControlRoot(), Cond,
10827                         DAG.getBasicBlock(Small.MBB));
10828         // Insert the false branch.
10829         BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond,
10830                              DAG.getBasicBlock(DefaultMBB));
10831 
10832         DAG.setRoot(BrCond);
10833         return;
10834       }
10835     }
10836   }
10837 
10838   if (TM.getOptLevel() != CodeGenOpt::None) {
10839     // Here, we order cases by probability so the most likely case will be
10840     // checked first. However, two clusters can have the same probability in
10841     // which case their relative ordering is non-deterministic. So we use Low
10842     // as a tie-breaker as clusters are guaranteed to never overlap.
10843     llvm::sort(W.FirstCluster, W.LastCluster + 1,
10844                [](const CaseCluster &a, const CaseCluster &b) {
10845       return a.Prob != b.Prob ?
10846              a.Prob > b.Prob :
10847              a.Low->getValue().slt(b.Low->getValue());
10848     });
10849 
10850     // Rearrange the case blocks so that the last one falls through if possible
10851     // without changing the order of probabilities.
10852     for (CaseClusterIt I = W.LastCluster; I > W.FirstCluster; ) {
10853       --I;
10854       if (I->Prob > W.LastCluster->Prob)
10855         break;
10856       if (I->Kind == CC_Range && I->MBB == NextMBB) {
10857         std::swap(*I, *W.LastCluster);
10858         break;
10859       }
10860     }
10861   }
10862 
10863   // Compute total probability.
10864   BranchProbability DefaultProb = W.DefaultProb;
10865   BranchProbability UnhandledProbs = DefaultProb;
10866   for (CaseClusterIt I = W.FirstCluster; I <= W.LastCluster; ++I)
10867     UnhandledProbs += I->Prob;
10868 
10869   MachineBasicBlock *CurMBB = W.MBB;
10870   for (CaseClusterIt I = W.FirstCluster, E = W.LastCluster; I <= E; ++I) {
10871     bool FallthroughUnreachable = false;
10872     MachineBasicBlock *Fallthrough;
10873     if (I == W.LastCluster) {
10874       // For the last cluster, fall through to the default destination.
10875       Fallthrough = DefaultMBB;
10876       FallthroughUnreachable = isa<UnreachableInst>(
10877           DefaultMBB->getBasicBlock()->getFirstNonPHIOrDbg());
10878     } else {
10879       Fallthrough = CurMF->CreateMachineBasicBlock(CurMBB->getBasicBlock());
10880       CurMF->insert(BBI, Fallthrough);
10881       // Put Cond in a virtual register to make it available from the new blocks.
10882       ExportFromCurrentBlock(Cond);
10883     }
10884     UnhandledProbs -= I->Prob;
10885 
10886     switch (I->Kind) {
10887       case CC_JumpTable: {
10888         // FIXME: Optimize away range check based on pivot comparisons.
10889         JumpTableHeader *JTH = &SL->JTCases[I->JTCasesIndex].first;
10890         SwitchCG::JumpTable *JT = &SL->JTCases[I->JTCasesIndex].second;
10891 
10892         // The jump block hasn't been inserted yet; insert it here.
10893         MachineBasicBlock *JumpMBB = JT->MBB;
10894         CurMF->insert(BBI, JumpMBB);
10895 
10896         auto JumpProb = I->Prob;
10897         auto FallthroughProb = UnhandledProbs;
10898 
10899         // If the default statement is a target of the jump table, we evenly
10900         // distribute the default probability to successors of CurMBB. Also
10901         // update the probability on the edge from JumpMBB to Fallthrough.
10902         for (MachineBasicBlock::succ_iterator SI = JumpMBB->succ_begin(),
10903                                               SE = JumpMBB->succ_end();
10904              SI != SE; ++SI) {
10905           if (*SI == DefaultMBB) {
10906             JumpProb += DefaultProb / 2;
10907             FallthroughProb -= DefaultProb / 2;
10908             JumpMBB->setSuccProbability(SI, DefaultProb / 2);
10909             JumpMBB->normalizeSuccProbs();
10910             break;
10911           }
10912         }
10913 
10914         if (FallthroughUnreachable)
10915           JTH->FallthroughUnreachable = true;
10916 
10917         if (!JTH->FallthroughUnreachable)
10918           addSuccessorWithProb(CurMBB, Fallthrough, FallthroughProb);
10919         addSuccessorWithProb(CurMBB, JumpMBB, JumpProb);
10920         CurMBB->normalizeSuccProbs();
10921 
10922         // The jump table header will be inserted in our current block, do the
10923         // range check, and fall through to our fallthrough block.
10924         JTH->HeaderBB = CurMBB;
10925         JT->Default = Fallthrough; // FIXME: Move Default to JumpTableHeader.
10926 
10927         // If we're in the right place, emit the jump table header right now.
10928         if (CurMBB == SwitchMBB) {
10929           visitJumpTableHeader(*JT, *JTH, SwitchMBB);
10930           JTH->Emitted = true;
10931         }
10932         break;
10933       }
10934       case CC_BitTests: {
10935         // FIXME: Optimize away range check based on pivot comparisons.
10936         BitTestBlock *BTB = &SL->BitTestCases[I->BTCasesIndex];
10937 
10938         // The bit test blocks haven't been inserted yet; insert them here.
10939         for (BitTestCase &BTC : BTB->Cases)
10940           CurMF->insert(BBI, BTC.ThisBB);
10941 
10942         // Fill in fields of the BitTestBlock.
10943         BTB->Parent = CurMBB;
10944         BTB->Default = Fallthrough;
10945 
10946         BTB->DefaultProb = UnhandledProbs;
10947         // If the cases in bit test don't form a contiguous range, we evenly
10948         // distribute the probability on the edge to Fallthrough to two
10949         // successors of CurMBB.
10950         if (!BTB->ContiguousRange) {
10951           BTB->Prob += DefaultProb / 2;
10952           BTB->DefaultProb -= DefaultProb / 2;
10953         }
10954 
10955         if (FallthroughUnreachable)
10956           BTB->FallthroughUnreachable = true;
10957 
10958         // If we're in the right place, emit the bit test header right now.
10959         if (CurMBB == SwitchMBB) {
10960           visitBitTestHeader(*BTB, SwitchMBB);
10961           BTB->Emitted = true;
10962         }
10963         break;
10964       }
10965       case CC_Range: {
10966         const Value *RHS, *LHS, *MHS;
10967         ISD::CondCode CC;
10968         if (I->Low == I->High) {
10969           // Check Cond == I->Low.
10970           CC = ISD::SETEQ;
10971           LHS = Cond;
10972           RHS=I->Low;
10973           MHS = nullptr;
10974         } else {
10975           // Check I->Low <= Cond <= I->High.
10976           CC = ISD::SETLE;
10977           LHS = I->Low;
10978           MHS = Cond;
10979           RHS = I->High;
10980         }
10981 
10982         // If Fallthrough is unreachable, fold away the comparison.
10983         if (FallthroughUnreachable)
10984           CC = ISD::SETTRUE;
10985 
10986         // The false probability is the sum of all unhandled cases.
10987         CaseBlock CB(CC, LHS, RHS, MHS, I->MBB, Fallthrough, CurMBB,
10988                      getCurSDLoc(), I->Prob, UnhandledProbs);
10989 
10990         if (CurMBB == SwitchMBB)
10991           visitSwitchCase(CB, SwitchMBB);
10992         else
10993           SL->SwitchCases.push_back(CB);
10994 
10995         break;
10996       }
10997     }
10998     CurMBB = Fallthrough;
10999   }
11000 }
11001 
11002 unsigned SelectionDAGBuilder::caseClusterRank(const CaseCluster &CC,
11003                                               CaseClusterIt First,
11004                                               CaseClusterIt Last) {
11005   return std::count_if(First, Last + 1, [&](const CaseCluster &X) {
11006     if (X.Prob != CC.Prob)
11007       return X.Prob > CC.Prob;
11008 
11009     // Ties are broken by comparing the case value.
11010     return X.Low->getValue().slt(CC.Low->getValue());
11011   });
11012 }
11013 
11014 void SelectionDAGBuilder::splitWorkItem(SwitchWorkList &WorkList,
11015                                         const SwitchWorkListItem &W,
11016                                         Value *Cond,
11017                                         MachineBasicBlock *SwitchMBB) {
11018   assert(W.FirstCluster->Low->getValue().slt(W.LastCluster->Low->getValue()) &&
11019          "Clusters not sorted?");
11020 
11021   assert(W.LastCluster - W.FirstCluster + 1 >= 2 && "Too small to split!");
11022 
11023   // Balance the tree based on branch probabilities to create a near-optimal (in
11024   // terms of search time given key frequency) binary search tree. See e.g. Kurt
11025   // Mehlhorn "Nearly Optimal Binary Search Trees" (1975).
11026   CaseClusterIt LastLeft = W.FirstCluster;
11027   CaseClusterIt FirstRight = W.LastCluster;
11028   auto LeftProb = LastLeft->Prob + W.DefaultProb / 2;
11029   auto RightProb = FirstRight->Prob + W.DefaultProb / 2;
11030 
11031   // Move LastLeft and FirstRight towards each other from opposite directions to
11032   // find a partitioning of the clusters which balances the probability on both
11033   // sides. If LeftProb and RightProb are equal, alternate which side is
11034   // taken to ensure 0-probability nodes are distributed evenly.
11035   unsigned I = 0;
11036   while (LastLeft + 1 < FirstRight) {
11037     if (LeftProb < RightProb || (LeftProb == RightProb && (I & 1)))
11038       LeftProb += (++LastLeft)->Prob;
11039     else
11040       RightProb += (--FirstRight)->Prob;
11041     I++;
11042   }
11043 
11044   while (true) {
11045     // Our binary search tree differs from a typical BST in that ours can have up
11046     // to three values in each leaf. The pivot selection above doesn't take that
11047     // into account, which means the tree might require more nodes and be less
11048     // efficient. We compensate for this here.
11049 
11050     unsigned NumLeft = LastLeft - W.FirstCluster + 1;
11051     unsigned NumRight = W.LastCluster - FirstRight + 1;
11052 
11053     if (std::min(NumLeft, NumRight) < 3 && std::max(NumLeft, NumRight) > 3) {
11054       // If one side has less than 3 clusters, and the other has more than 3,
11055       // consider taking a cluster from the other side.
11056 
11057       if (NumLeft < NumRight) {
11058         // Consider moving the first cluster on the right to the left side.
11059         CaseCluster &CC = *FirstRight;
11060         unsigned RightSideRank = caseClusterRank(CC, FirstRight, W.LastCluster);
11061         unsigned LeftSideRank = caseClusterRank(CC, W.FirstCluster, LastLeft);
11062         if (LeftSideRank <= RightSideRank) {
11063           // Moving the cluster to the left does not demote it.
11064           ++LastLeft;
11065           ++FirstRight;
11066           continue;
11067         }
11068       } else {
11069         assert(NumRight < NumLeft);
11070         // Consider moving the last element on the left to the right side.
11071         CaseCluster &CC = *LastLeft;
11072         unsigned LeftSideRank = caseClusterRank(CC, W.FirstCluster, LastLeft);
11073         unsigned RightSideRank = caseClusterRank(CC, FirstRight, W.LastCluster);
11074         if (RightSideRank <= LeftSideRank) {
11075           // Moving the cluster to the right does not demot it.
11076           --LastLeft;
11077           --FirstRight;
11078           continue;
11079         }
11080       }
11081     }
11082     break;
11083   }
11084 
11085   assert(LastLeft + 1 == FirstRight);
11086   assert(LastLeft >= W.FirstCluster);
11087   assert(FirstRight <= W.LastCluster);
11088 
11089   // Use the first element on the right as pivot since we will make less-than
11090   // comparisons against it.
11091   CaseClusterIt PivotCluster = FirstRight;
11092   assert(PivotCluster > W.FirstCluster);
11093   assert(PivotCluster <= W.LastCluster);
11094 
11095   CaseClusterIt FirstLeft = W.FirstCluster;
11096   CaseClusterIt LastRight = W.LastCluster;
11097 
11098   const ConstantInt *Pivot = PivotCluster->Low;
11099 
11100   // New blocks will be inserted immediately after the current one.
11101   MachineFunction::iterator BBI(W.MBB);
11102   ++BBI;
11103 
11104   // We will branch to the LHS if Value < Pivot. If LHS is a single cluster,
11105   // we can branch to its destination directly if it's squeezed exactly in
11106   // between the known lower bound and Pivot - 1.
11107   MachineBasicBlock *LeftMBB;
11108   if (FirstLeft == LastLeft && FirstLeft->Kind == CC_Range &&
11109       FirstLeft->Low == W.GE &&
11110       (FirstLeft->High->getValue() + 1LL) == Pivot->getValue()) {
11111     LeftMBB = FirstLeft->MBB;
11112   } else {
11113     LeftMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
11114     FuncInfo.MF->insert(BBI, LeftMBB);
11115     WorkList.push_back(
11116         {LeftMBB, FirstLeft, LastLeft, W.GE, Pivot, W.DefaultProb / 2});
11117     // Put Cond in a virtual register to make it available from the new blocks.
11118     ExportFromCurrentBlock(Cond);
11119   }
11120 
11121   // Similarly, we will branch to the RHS if Value >= Pivot. If RHS is a
11122   // single cluster, RHS.Low == Pivot, and we can branch to its destination
11123   // directly if RHS.High equals the current upper bound.
11124   MachineBasicBlock *RightMBB;
11125   if (FirstRight == LastRight && FirstRight->Kind == CC_Range &&
11126       W.LT && (FirstRight->High->getValue() + 1ULL) == W.LT->getValue()) {
11127     RightMBB = FirstRight->MBB;
11128   } else {
11129     RightMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
11130     FuncInfo.MF->insert(BBI, RightMBB);
11131     WorkList.push_back(
11132         {RightMBB, FirstRight, LastRight, Pivot, W.LT, W.DefaultProb / 2});
11133     // Put Cond in a virtual register to make it available from the new blocks.
11134     ExportFromCurrentBlock(Cond);
11135   }
11136 
11137   // Create the CaseBlock record that will be used to lower the branch.
11138   CaseBlock CB(ISD::SETLT, Cond, Pivot, nullptr, LeftMBB, RightMBB, W.MBB,
11139                getCurSDLoc(), LeftProb, RightProb);
11140 
11141   if (W.MBB == SwitchMBB)
11142     visitSwitchCase(CB, SwitchMBB);
11143   else
11144     SL->SwitchCases.push_back(CB);
11145 }
11146 
11147 // Scale CaseProb after peeling a case with the probablity of PeeledCaseProb
11148 // from the swith statement.
11149 static BranchProbability scaleCaseProbality(BranchProbability CaseProb,
11150                                             BranchProbability PeeledCaseProb) {
11151   if (PeeledCaseProb == BranchProbability::getOne())
11152     return BranchProbability::getZero();
11153   BranchProbability SwitchProb = PeeledCaseProb.getCompl();
11154 
11155   uint32_t Numerator = CaseProb.getNumerator();
11156   uint32_t Denominator = SwitchProb.scale(CaseProb.getDenominator());
11157   return BranchProbability(Numerator, std::max(Numerator, Denominator));
11158 }
11159 
11160 // Try to peel the top probability case if it exceeds the threshold.
11161 // Return current MachineBasicBlock for the switch statement if the peeling
11162 // does not occur.
11163 // If the peeling is performed, return the newly created MachineBasicBlock
11164 // for the peeled switch statement. Also update Clusters to remove the peeled
11165 // case. PeeledCaseProb is the BranchProbability for the peeled case.
11166 MachineBasicBlock *SelectionDAGBuilder::peelDominantCaseCluster(
11167     const SwitchInst &SI, CaseClusterVector &Clusters,
11168     BranchProbability &PeeledCaseProb) {
11169   MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
11170   // Don't perform if there is only one cluster or optimizing for size.
11171   if (SwitchPeelThreshold > 100 || !FuncInfo.BPI || Clusters.size() < 2 ||
11172       TM.getOptLevel() == CodeGenOpt::None ||
11173       SwitchMBB->getParent()->getFunction().hasMinSize())
11174     return SwitchMBB;
11175 
11176   BranchProbability TopCaseProb = BranchProbability(SwitchPeelThreshold, 100);
11177   unsigned PeeledCaseIndex = 0;
11178   bool SwitchPeeled = false;
11179   for (unsigned Index = 0; Index < Clusters.size(); ++Index) {
11180     CaseCluster &CC = Clusters[Index];
11181     if (CC.Prob < TopCaseProb)
11182       continue;
11183     TopCaseProb = CC.Prob;
11184     PeeledCaseIndex = Index;
11185     SwitchPeeled = true;
11186   }
11187   if (!SwitchPeeled)
11188     return SwitchMBB;
11189 
11190   LLVM_DEBUG(dbgs() << "Peeled one top case in switch stmt, prob: "
11191                     << TopCaseProb << "\n");
11192 
11193   // Record the MBB for the peeled switch statement.
11194   MachineFunction::iterator BBI(SwitchMBB);
11195   ++BBI;
11196   MachineBasicBlock *PeeledSwitchMBB =
11197       FuncInfo.MF->CreateMachineBasicBlock(SwitchMBB->getBasicBlock());
11198   FuncInfo.MF->insert(BBI, PeeledSwitchMBB);
11199 
11200   ExportFromCurrentBlock(SI.getCondition());
11201   auto PeeledCaseIt = Clusters.begin() + PeeledCaseIndex;
11202   SwitchWorkListItem W = {SwitchMBB, PeeledCaseIt, PeeledCaseIt,
11203                           nullptr,   nullptr,      TopCaseProb.getCompl()};
11204   lowerWorkItem(W, SI.getCondition(), SwitchMBB, PeeledSwitchMBB);
11205 
11206   Clusters.erase(PeeledCaseIt);
11207   for (CaseCluster &CC : Clusters) {
11208     LLVM_DEBUG(
11209         dbgs() << "Scale the probablity for one cluster, before scaling: "
11210                << CC.Prob << "\n");
11211     CC.Prob = scaleCaseProbality(CC.Prob, TopCaseProb);
11212     LLVM_DEBUG(dbgs() << "After scaling: " << CC.Prob << "\n");
11213   }
11214   PeeledCaseProb = TopCaseProb;
11215   return PeeledSwitchMBB;
11216 }
11217 
11218 void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
11219   // Extract cases from the switch.
11220   BranchProbabilityInfo *BPI = FuncInfo.BPI;
11221   CaseClusterVector Clusters;
11222   Clusters.reserve(SI.getNumCases());
11223   for (auto I : SI.cases()) {
11224     MachineBasicBlock *Succ = FuncInfo.MBBMap[I.getCaseSuccessor()];
11225     const ConstantInt *CaseVal = I.getCaseValue();
11226     BranchProbability Prob =
11227         BPI ? BPI->getEdgeProbability(SI.getParent(), I.getSuccessorIndex())
11228             : BranchProbability(1, SI.getNumCases() + 1);
11229     Clusters.push_back(CaseCluster::range(CaseVal, CaseVal, Succ, Prob));
11230   }
11231 
11232   MachineBasicBlock *DefaultMBB = FuncInfo.MBBMap[SI.getDefaultDest()];
11233 
11234   // Cluster adjacent cases with the same destination. We do this at all
11235   // optimization levels because it's cheap to do and will make codegen faster
11236   // if there are many clusters.
11237   sortAndRangeify(Clusters);
11238 
11239   // The branch probablity of the peeled case.
11240   BranchProbability PeeledCaseProb = BranchProbability::getZero();
11241   MachineBasicBlock *PeeledSwitchMBB =
11242       peelDominantCaseCluster(SI, Clusters, PeeledCaseProb);
11243 
11244   // If there is only the default destination, jump there directly.
11245   MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
11246   if (Clusters.empty()) {
11247     assert(PeeledSwitchMBB == SwitchMBB);
11248     SwitchMBB->addSuccessor(DefaultMBB);
11249     if (DefaultMBB != NextBlock(SwitchMBB)) {
11250       DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
11251                               getControlRoot(), DAG.getBasicBlock(DefaultMBB)));
11252     }
11253     return;
11254   }
11255 
11256   SL->findJumpTables(Clusters, &SI, DefaultMBB, DAG.getPSI(), DAG.getBFI());
11257   SL->findBitTestClusters(Clusters, &SI);
11258 
11259   LLVM_DEBUG({
11260     dbgs() << "Case clusters: ";
11261     for (const CaseCluster &C : Clusters) {
11262       if (C.Kind == CC_JumpTable)
11263         dbgs() << "JT:";
11264       if (C.Kind == CC_BitTests)
11265         dbgs() << "BT:";
11266 
11267       C.Low->getValue().print(dbgs(), true);
11268       if (C.Low != C.High) {
11269         dbgs() << '-';
11270         C.High->getValue().print(dbgs(), true);
11271       }
11272       dbgs() << ' ';
11273     }
11274     dbgs() << '\n';
11275   });
11276 
11277   assert(!Clusters.empty());
11278   SwitchWorkList WorkList;
11279   CaseClusterIt First = Clusters.begin();
11280   CaseClusterIt Last = Clusters.end() - 1;
11281   auto DefaultProb = getEdgeProbability(PeeledSwitchMBB, DefaultMBB);
11282   // Scale the branchprobability for DefaultMBB if the peel occurs and
11283   // DefaultMBB is not replaced.
11284   if (PeeledCaseProb != BranchProbability::getZero() &&
11285       DefaultMBB == FuncInfo.MBBMap[SI.getDefaultDest()])
11286     DefaultProb = scaleCaseProbality(DefaultProb, PeeledCaseProb);
11287   WorkList.push_back(
11288       {PeeledSwitchMBB, First, Last, nullptr, nullptr, DefaultProb});
11289 
11290   while (!WorkList.empty()) {
11291     SwitchWorkListItem W = WorkList.pop_back_val();
11292     unsigned NumClusters = W.LastCluster - W.FirstCluster + 1;
11293 
11294     if (NumClusters > 3 && TM.getOptLevel() != CodeGenOpt::None &&
11295         !DefaultMBB->getParent()->getFunction().hasMinSize()) {
11296       // For optimized builds, lower large range as a balanced binary tree.
11297       splitWorkItem(WorkList, W, SI.getCondition(), SwitchMBB);
11298       continue;
11299     }
11300 
11301     lowerWorkItem(W, SI.getCondition(), SwitchMBB, DefaultMBB);
11302   }
11303 }
11304 
11305 void SelectionDAGBuilder::visitStepVector(const CallInst &I) {
11306   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11307   auto DL = getCurSDLoc();
11308   EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
11309   setValue(&I, DAG.getStepVector(DL, ResultVT));
11310 }
11311 
11312 void SelectionDAGBuilder::visitVectorReverse(const CallInst &I) {
11313   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11314   EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
11315 
11316   SDLoc DL = getCurSDLoc();
11317   SDValue V = getValue(I.getOperand(0));
11318   assert(VT == V.getValueType() && "Malformed vector.reverse!");
11319 
11320   if (VT.isScalableVector()) {
11321     setValue(&I, DAG.getNode(ISD::VECTOR_REVERSE, DL, VT, V));
11322     return;
11323   }
11324 
11325   // Use VECTOR_SHUFFLE for the fixed-length vector
11326   // to maintain existing behavior.
11327   SmallVector<int, 8> Mask;
11328   unsigned NumElts = VT.getVectorMinNumElements();
11329   for (unsigned i = 0; i != NumElts; ++i)
11330     Mask.push_back(NumElts - 1 - i);
11331 
11332   setValue(&I, DAG.getVectorShuffle(VT, DL, V, DAG.getUNDEF(VT), Mask));
11333 }
11334 
11335 void SelectionDAGBuilder::visitFreeze(const FreezeInst &I) {
11336   SmallVector<EVT, 4> ValueVTs;
11337   ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
11338                   ValueVTs);
11339   unsigned NumValues = ValueVTs.size();
11340   if (NumValues == 0) return;
11341 
11342   SmallVector<SDValue, 4> Values(NumValues);
11343   SDValue Op = getValue(I.getOperand(0));
11344 
11345   for (unsigned i = 0; i != NumValues; ++i)
11346     Values[i] = DAG.getNode(ISD::FREEZE, getCurSDLoc(), ValueVTs[i],
11347                             SDValue(Op.getNode(), Op.getResNo() + i));
11348 
11349   setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
11350                            DAG.getVTList(ValueVTs), Values));
11351 }
11352 
11353 void SelectionDAGBuilder::visitVectorSplice(const CallInst &I) {
11354   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11355   EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
11356 
11357   SDLoc DL = getCurSDLoc();
11358   SDValue V1 = getValue(I.getOperand(0));
11359   SDValue V2 = getValue(I.getOperand(1));
11360   int64_t Imm = cast<ConstantInt>(I.getOperand(2))->getSExtValue();
11361 
11362   // VECTOR_SHUFFLE doesn't support a scalable mask so use a dedicated node.
11363   if (VT.isScalableVector()) {
11364     MVT IdxVT = TLI.getVectorIdxTy(DAG.getDataLayout());
11365     setValue(&I, DAG.getNode(ISD::VECTOR_SPLICE, DL, VT, V1, V2,
11366                              DAG.getConstant(Imm, DL, IdxVT)));
11367     return;
11368   }
11369 
11370   unsigned NumElts = VT.getVectorNumElements();
11371 
11372   uint64_t Idx = (NumElts + Imm) % NumElts;
11373 
11374   // Use VECTOR_SHUFFLE to maintain original behaviour for fixed-length vectors.
11375   SmallVector<int, 8> Mask;
11376   for (unsigned i = 0; i < NumElts; ++i)
11377     Mask.push_back(Idx + i);
11378   setValue(&I, DAG.getVectorShuffle(VT, DL, V1, V2, Mask));
11379 }
11380