xref: /llvm-project/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp (revision 1488fb4153367b9c20e8c0ca7aa40bc8437ea3d3)
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/STLExtras.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/SmallSet.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/Analysis/AliasAnalysis.h"
24 #include "llvm/Analysis/BranchProbabilityInfo.h"
25 #include "llvm/Analysis/ConstantFolding.h"
26 #include "llvm/Analysis/Loads.h"
27 #include "llvm/Analysis/MemoryLocation.h"
28 #include "llvm/Analysis/TargetLibraryInfo.h"
29 #include "llvm/Analysis/TargetTransformInfo.h"
30 #include "llvm/Analysis/ValueTracking.h"
31 #include "llvm/Analysis/VectorUtils.h"
32 #include "llvm/CodeGen/Analysis.h"
33 #include "llvm/CodeGen/AssignmentTrackingAnalysis.h"
34 #include "llvm/CodeGen/CodeGenCommonISel.h"
35 #include "llvm/CodeGen/FunctionLoweringInfo.h"
36 #include "llvm/CodeGen/GCMetadata.h"
37 #include "llvm/CodeGen/ISDOpcodes.h"
38 #include "llvm/CodeGen/MachineBasicBlock.h"
39 #include "llvm/CodeGen/MachineFrameInfo.h"
40 #include "llvm/CodeGen/MachineFunction.h"
41 #include "llvm/CodeGen/MachineInstrBuilder.h"
42 #include "llvm/CodeGen/MachineInstrBundleIterator.h"
43 #include "llvm/CodeGen/MachineMemOperand.h"
44 #include "llvm/CodeGen/MachineModuleInfo.h"
45 #include "llvm/CodeGen/MachineOperand.h"
46 #include "llvm/CodeGen/MachineRegisterInfo.h"
47 #include "llvm/CodeGen/RuntimeLibcalls.h"
48 #include "llvm/CodeGen/SelectionDAG.h"
49 #include "llvm/CodeGen/SelectionDAGTargetInfo.h"
50 #include "llvm/CodeGen/StackMaps.h"
51 #include "llvm/CodeGen/SwiftErrorValueTracking.h"
52 #include "llvm/CodeGen/TargetFrameLowering.h"
53 #include "llvm/CodeGen/TargetInstrInfo.h"
54 #include "llvm/CodeGen/TargetOpcodes.h"
55 #include "llvm/CodeGen/TargetRegisterInfo.h"
56 #include "llvm/CodeGen/TargetSubtargetInfo.h"
57 #include "llvm/CodeGen/WinEHFuncInfo.h"
58 #include "llvm/IR/Argument.h"
59 #include "llvm/IR/Attributes.h"
60 #include "llvm/IR/BasicBlock.h"
61 #include "llvm/IR/CFG.h"
62 #include "llvm/IR/CallingConv.h"
63 #include "llvm/IR/Constant.h"
64 #include "llvm/IR/ConstantRange.h"
65 #include "llvm/IR/Constants.h"
66 #include "llvm/IR/DataLayout.h"
67 #include "llvm/IR/DebugInfo.h"
68 #include "llvm/IR/DebugInfoMetadata.h"
69 #include "llvm/IR/DerivedTypes.h"
70 #include "llvm/IR/DiagnosticInfo.h"
71 #include "llvm/IR/EHPersonalities.h"
72 #include "llvm/IR/Function.h"
73 #include "llvm/IR/GetElementPtrTypeIterator.h"
74 #include "llvm/IR/InlineAsm.h"
75 #include "llvm/IR/InstrTypes.h"
76 #include "llvm/IR/Instructions.h"
77 #include "llvm/IR/IntrinsicInst.h"
78 #include "llvm/IR/Intrinsics.h"
79 #include "llvm/IR/IntrinsicsAArch64.h"
80 #include "llvm/IR/IntrinsicsAMDGPU.h"
81 #include "llvm/IR/IntrinsicsWebAssembly.h"
82 #include "llvm/IR/LLVMContext.h"
83 #include "llvm/IR/MemoryModelRelaxationAnnotations.h"
84 #include "llvm/IR/Metadata.h"
85 #include "llvm/IR/Module.h"
86 #include "llvm/IR/Operator.h"
87 #include "llvm/IR/PatternMatch.h"
88 #include "llvm/IR/Statepoint.h"
89 #include "llvm/IR/Type.h"
90 #include "llvm/IR/User.h"
91 #include "llvm/IR/Value.h"
92 #include "llvm/MC/MCContext.h"
93 #include "llvm/Support/AtomicOrdering.h"
94 #include "llvm/Support/Casting.h"
95 #include "llvm/Support/CommandLine.h"
96 #include "llvm/Support/Compiler.h"
97 #include "llvm/Support/Debug.h"
98 #include "llvm/Support/InstructionCost.h"
99 #include "llvm/Support/MathExtras.h"
100 #include "llvm/Support/raw_ostream.h"
101 #include "llvm/Target/TargetIntrinsicInfo.h"
102 #include "llvm/Target/TargetMachine.h"
103 #include "llvm/Target/TargetOptions.h"
104 #include "llvm/TargetParser/Triple.h"
105 #include "llvm/Transforms/Utils/Local.h"
106 #include <cstddef>
107 #include <iterator>
108 #include <limits>
109 #include <optional>
110 #include <tuple>
111 
112 using namespace llvm;
113 using namespace PatternMatch;
114 using namespace SwitchCG;
115 
116 #define DEBUG_TYPE "isel"
117 
118 /// LimitFloatPrecision - Generate low-precision inline sequences for
119 /// some float libcalls (6, 8 or 12 bits).
120 static unsigned LimitFloatPrecision;
121 
122 static cl::opt<bool>
123     InsertAssertAlign("insert-assert-align", cl::init(true),
124                       cl::desc("Insert the experimental `assertalign` node."),
125                       cl::ReallyHidden);
126 
127 static cl::opt<unsigned, true>
128     LimitFPPrecision("limit-float-precision",
129                      cl::desc("Generate low-precision inline sequences "
130                               "for some float libcalls"),
131                      cl::location(LimitFloatPrecision), cl::Hidden,
132                      cl::init(0));
133 
134 static cl::opt<unsigned> SwitchPeelThreshold(
135     "switch-peel-threshold", cl::Hidden, cl::init(66),
136     cl::desc("Set the case probability threshold for peeling the case from a "
137              "switch statement. A value greater than 100 will void this "
138              "optimization"));
139 
140 // Limit the width of DAG chains. This is important in general to prevent
141 // DAG-based analysis from blowing up. For example, alias analysis and
142 // load clustering may not complete in reasonable time. It is difficult to
143 // recognize and avoid this situation within each individual analysis, and
144 // future analyses are likely to have the same behavior. Limiting DAG width is
145 // the safe approach and will be especially important with global DAGs.
146 //
147 // MaxParallelChains default is arbitrarily high to avoid affecting
148 // optimization, but could be lowered to improve compile time. Any ld-ld-st-st
149 // sequence over this should have been converted to llvm.memcpy by the
150 // frontend. It is easy to induce this behavior with .ll code such as:
151 // %buffer = alloca [4096 x i8]
152 // %data = load [4096 x i8]* %argPtr
153 // store [4096 x i8] %data, [4096 x i8]* %buffer
154 static const unsigned MaxParallelChains = 64;
155 
156 static SDValue getCopyFromPartsVector(SelectionDAG &DAG, const SDLoc &DL,
157                                       const SDValue *Parts, unsigned NumParts,
158                                       MVT PartVT, EVT ValueVT, const Value *V,
159                                       SDValue InChain,
160                                       std::optional<CallingConv::ID> CC);
161 
162 /// getCopyFromParts - Create a value that contains the specified legal parts
163 /// combined into the value they represent.  If the parts combine to a type
164 /// larger than ValueVT then AssertOp can be used to specify whether the extra
165 /// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
166 /// (ISD::AssertSext).
167 static SDValue
168 getCopyFromParts(SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts,
169                  unsigned NumParts, MVT PartVT, EVT ValueVT, const Value *V,
170                  SDValue InChain,
171                  std::optional<CallingConv::ID> CC = std::nullopt,
172                  std::optional<ISD::NodeType> AssertOp = std::nullopt) {
173   // Let the target assemble the parts if it wants to
174   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
175   if (SDValue Val = TLI.joinRegisterPartsIntoValue(DAG, DL, Parts, NumParts,
176                                                    PartVT, ValueVT, CC))
177     return Val;
178 
179   if (ValueVT.isVector())
180     return getCopyFromPartsVector(DAG, DL, Parts, NumParts, PartVT, ValueVT, V,
181                                   InChain, CC);
182 
183   assert(NumParts > 0 && "No parts to assemble!");
184   SDValue Val = Parts[0];
185 
186   if (NumParts > 1) {
187     // Assemble the value from multiple parts.
188     if (ValueVT.isInteger()) {
189       unsigned PartBits = PartVT.getSizeInBits();
190       unsigned ValueBits = ValueVT.getSizeInBits();
191 
192       // Assemble the power of 2 part.
193       unsigned RoundParts = llvm::bit_floor(NumParts);
194       unsigned RoundBits = PartBits * RoundParts;
195       EVT RoundVT = RoundBits == ValueBits ?
196         ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
197       SDValue Lo, Hi;
198 
199       EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
200 
201       if (RoundParts > 2) {
202         Lo = getCopyFromParts(DAG, DL, Parts, RoundParts / 2, PartVT, HalfVT, V,
203                               InChain);
204         Hi = getCopyFromParts(DAG, DL, Parts + RoundParts / 2, RoundParts / 2,
205                               PartVT, HalfVT, V, InChain);
206       } else {
207         Lo = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[0]);
208         Hi = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[1]);
209       }
210 
211       if (DAG.getDataLayout().isBigEndian())
212         std::swap(Lo, Hi);
213 
214       Val = DAG.getNode(ISD::BUILD_PAIR, DL, RoundVT, Lo, Hi);
215 
216       if (RoundParts < NumParts) {
217         // Assemble the trailing non-power-of-2 part.
218         unsigned OddParts = NumParts - RoundParts;
219         EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
220         Hi = getCopyFromParts(DAG, DL, Parts + RoundParts, OddParts, PartVT,
221                               OddVT, V, InChain, CC);
222 
223         // Combine the round and odd parts.
224         Lo = Val;
225         if (DAG.getDataLayout().isBigEndian())
226           std::swap(Lo, Hi);
227         EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
228         Hi = DAG.getNode(ISD::ANY_EXTEND, DL, TotalVT, Hi);
229         Hi = DAG.getNode(ISD::SHL, DL, TotalVT, Hi,
230                          DAG.getConstant(Lo.getValueSizeInBits(), DL,
231                                          TLI.getShiftAmountTy(
232                                              TotalVT, DAG.getDataLayout())));
233         Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, TotalVT, Lo);
234         Val = DAG.getNode(ISD::OR, DL, TotalVT, Lo, Hi);
235       }
236     } else if (PartVT.isFloatingPoint()) {
237       // FP split into multiple FP parts (for ppcf128)
238       assert(ValueVT == EVT(MVT::ppcf128) && PartVT == MVT::f64 &&
239              "Unexpected split");
240       SDValue Lo, Hi;
241       Lo = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[0]);
242       Hi = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[1]);
243       if (TLI.hasBigEndianPartOrdering(ValueVT, DAG.getDataLayout()))
244         std::swap(Lo, Hi);
245       Val = DAG.getNode(ISD::BUILD_PAIR, DL, ValueVT, Lo, Hi);
246     } else {
247       // FP split into integer parts (soft fp)
248       assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
249              !PartVT.isVector() && "Unexpected split");
250       EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
251       Val = getCopyFromParts(DAG, DL, Parts, NumParts, PartVT, IntVT, V,
252                              InChain, CC);
253     }
254   }
255 
256   // There is now one part, held in Val.  Correct it to match ValueVT.
257   // PartEVT is the type of the register class that holds the value.
258   // ValueVT is the type of the inline asm operation.
259   EVT PartEVT = Val.getValueType();
260 
261   if (PartEVT == ValueVT)
262     return Val;
263 
264   if (PartEVT.isInteger() && ValueVT.isFloatingPoint() &&
265       ValueVT.bitsLT(PartEVT)) {
266     // For an FP value in an integer part, we need to truncate to the right
267     // width first.
268     PartEVT = EVT::getIntegerVT(*DAG.getContext(),  ValueVT.getSizeInBits());
269     Val = DAG.getNode(ISD::TRUNCATE, DL, PartEVT, Val);
270   }
271 
272   // Handle types that have the same size.
273   if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits())
274     return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
275 
276   // Handle types with different sizes.
277   if (PartEVT.isInteger() && ValueVT.isInteger()) {
278     if (ValueVT.bitsLT(PartEVT)) {
279       // For a truncate, see if we have any information to
280       // indicate whether the truncated bits will always be
281       // zero or sign-extension.
282       if (AssertOp)
283         Val = DAG.getNode(*AssertOp, DL, PartEVT, Val,
284                           DAG.getValueType(ValueVT));
285       return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
286     }
287     return DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val);
288   }
289 
290   if (PartEVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
291     // FP_ROUND's are always exact here.
292     if (ValueVT.bitsLT(Val.getValueType())) {
293 
294       SDValue NoChange =
295           DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()));
296 
297       if (DAG.getMachineFunction().getFunction().getAttributes().hasFnAttr(
298               llvm::Attribute::StrictFP)) {
299         return DAG.getNode(ISD::STRICT_FP_ROUND, DL,
300                            DAG.getVTList(ValueVT, MVT::Other), InChain, Val,
301                            NoChange);
302       }
303 
304       return DAG.getNode(ISD::FP_ROUND, DL, ValueVT, Val, NoChange);
305     }
306 
307     return DAG.getNode(ISD::FP_EXTEND, DL, ValueVT, Val);
308   }
309 
310   // Handle MMX to a narrower integer type by bitcasting MMX to integer and
311   // then truncating.
312   if (PartEVT == MVT::x86mmx && ValueVT.isInteger() &&
313       ValueVT.bitsLT(PartEVT)) {
314     Val = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Val);
315     return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
316   }
317 
318   report_fatal_error("Unknown mismatch in getCopyFromParts!");
319 }
320 
321 static void diagnosePossiblyInvalidConstraint(LLVMContext &Ctx, const Value *V,
322                                               const Twine &ErrMsg) {
323   const Instruction *I = dyn_cast_or_null<Instruction>(V);
324   if (!V)
325     return Ctx.emitError(ErrMsg);
326 
327   const char *AsmError = ", possible invalid constraint for vector type";
328   if (const CallInst *CI = dyn_cast<CallInst>(I))
329     if (CI->isInlineAsm())
330       return Ctx.emitError(I, ErrMsg + AsmError);
331 
332   return Ctx.emitError(I, ErrMsg);
333 }
334 
335 /// getCopyFromPartsVector - Create a value that contains the specified legal
336 /// parts combined into the value they represent.  If the parts combine to a
337 /// type larger than ValueVT then AssertOp can be used to specify whether the
338 /// extra bits are known to be zero (ISD::AssertZext) or sign extended from
339 /// ValueVT (ISD::AssertSext).
340 static SDValue getCopyFromPartsVector(SelectionDAG &DAG, const SDLoc &DL,
341                                       const SDValue *Parts, unsigned NumParts,
342                                       MVT PartVT, EVT ValueVT, const Value *V,
343                                       SDValue InChain,
344                                       std::optional<CallingConv::ID> CallConv) {
345   assert(ValueVT.isVector() && "Not a vector value");
346   assert(NumParts > 0 && "No parts to assemble!");
347   const bool IsABIRegCopy = CallConv.has_value();
348 
349   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
350   SDValue Val = Parts[0];
351 
352   // Handle a multi-element vector.
353   if (NumParts > 1) {
354     EVT IntermediateVT;
355     MVT RegisterVT;
356     unsigned NumIntermediates;
357     unsigned NumRegs;
358 
359     if (IsABIRegCopy) {
360       NumRegs = TLI.getVectorTypeBreakdownForCallingConv(
361           *DAG.getContext(), *CallConv, ValueVT, IntermediateVT,
362           NumIntermediates, RegisterVT);
363     } else {
364       NumRegs =
365           TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
366                                      NumIntermediates, RegisterVT);
367     }
368 
369     assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
370     NumParts = NumRegs; // Silence a compiler warning.
371     assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
372     assert(RegisterVT.getSizeInBits() ==
373            Parts[0].getSimpleValueType().getSizeInBits() &&
374            "Part type sizes don't match!");
375 
376     // Assemble the parts into intermediate operands.
377     SmallVector<SDValue, 8> Ops(NumIntermediates);
378     if (NumIntermediates == NumParts) {
379       // If the register was not expanded, truncate or copy the value,
380       // as appropriate.
381       for (unsigned i = 0; i != NumParts; ++i)
382         Ops[i] = getCopyFromParts(DAG, DL, &Parts[i], 1, PartVT, IntermediateVT,
383                                   V, InChain, CallConv);
384     } else if (NumParts > 0) {
385       // If the intermediate type was expanded, build the intermediate
386       // operands from the parts.
387       assert(NumParts % NumIntermediates == 0 &&
388              "Must expand into a divisible number of parts!");
389       unsigned Factor = NumParts / NumIntermediates;
390       for (unsigned i = 0; i != NumIntermediates; ++i)
391         Ops[i] = getCopyFromParts(DAG, DL, &Parts[i * Factor], Factor, PartVT,
392                                   IntermediateVT, V, InChain, CallConv);
393     }
394 
395     // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the
396     // intermediate operands.
397     EVT BuiltVectorTy =
398         IntermediateVT.isVector()
399             ? EVT::getVectorVT(
400                   *DAG.getContext(), IntermediateVT.getScalarType(),
401                   IntermediateVT.getVectorElementCount() * NumParts)
402             : EVT::getVectorVT(*DAG.getContext(),
403                                IntermediateVT.getScalarType(),
404                                NumIntermediates);
405     Val = DAG.getNode(IntermediateVT.isVector() ? ISD::CONCAT_VECTORS
406                                                 : ISD::BUILD_VECTOR,
407                       DL, BuiltVectorTy, Ops);
408   }
409 
410   // There is now one part, held in Val.  Correct it to match ValueVT.
411   EVT PartEVT = Val.getValueType();
412 
413   if (PartEVT == ValueVT)
414     return Val;
415 
416   if (PartEVT.isVector()) {
417     // Vector/Vector bitcast.
418     if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits())
419       return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
420 
421     // If the parts vector has more elements than the value vector, then we
422     // have a vector widening case (e.g. <2 x float> -> <4 x float>).
423     // Extract the elements we want.
424     if (PartEVT.getVectorElementCount() != ValueVT.getVectorElementCount()) {
425       assert((PartEVT.getVectorElementCount().getKnownMinValue() >
426               ValueVT.getVectorElementCount().getKnownMinValue()) &&
427              (PartEVT.getVectorElementCount().isScalable() ==
428               ValueVT.getVectorElementCount().isScalable()) &&
429              "Cannot narrow, it would be a lossy transformation");
430       PartEVT =
431           EVT::getVectorVT(*DAG.getContext(), PartEVT.getVectorElementType(),
432                            ValueVT.getVectorElementCount());
433       Val = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, PartEVT, Val,
434                         DAG.getVectorIdxConstant(0, DL));
435       if (PartEVT == ValueVT)
436         return Val;
437       if (PartEVT.isInteger() && ValueVT.isFloatingPoint())
438         return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
439 
440       // Vector/Vector bitcast (e.g. <2 x bfloat> -> <2 x half>).
441       if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits())
442         return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
443     }
444 
445     // Promoted vector extract
446     return DAG.getAnyExtOrTrunc(Val, DL, ValueVT);
447   }
448 
449   // Trivial bitcast if the types are the same size and the destination
450   // vector type is legal.
451   if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits() &&
452       TLI.isTypeLegal(ValueVT))
453     return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
454 
455   if (ValueVT.getVectorNumElements() != 1) {
456      // Certain ABIs require that vectors are passed as integers. For vectors
457      // are the same size, this is an obvious bitcast.
458      if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits()) {
459        return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
460      } else if (ValueVT.bitsLT(PartEVT)) {
461        const uint64_t ValueSize = ValueVT.getFixedSizeInBits();
462        EVT IntermediateType = EVT::getIntegerVT(*DAG.getContext(), ValueSize);
463        // Drop the extra bits.
464        Val = DAG.getNode(ISD::TRUNCATE, DL, IntermediateType, Val);
465        return DAG.getBitcast(ValueVT, Val);
466      }
467 
468      diagnosePossiblyInvalidConstraint(
469          *DAG.getContext(), V, "non-trivial scalar-to-vector conversion");
470      return DAG.getUNDEF(ValueVT);
471   }
472 
473   // Handle cases such as i8 -> <1 x i1>
474   EVT ValueSVT = ValueVT.getVectorElementType();
475   if (ValueVT.getVectorNumElements() == 1 && ValueSVT != PartEVT) {
476     unsigned ValueSize = ValueSVT.getSizeInBits();
477     if (ValueSize == PartEVT.getSizeInBits()) {
478       Val = DAG.getNode(ISD::BITCAST, DL, ValueSVT, Val);
479     } else if (ValueSVT.isFloatingPoint() && PartEVT.isInteger()) {
480       // It's possible a scalar floating point type gets softened to integer and
481       // then promoted to a larger integer. If PartEVT is the larger integer
482       // we need to truncate it and then bitcast to the FP type.
483       assert(ValueSVT.bitsLT(PartEVT) && "Unexpected types");
484       EVT IntermediateType = EVT::getIntegerVT(*DAG.getContext(), ValueSize);
485       Val = DAG.getNode(ISD::TRUNCATE, DL, IntermediateType, Val);
486       Val = DAG.getBitcast(ValueSVT, Val);
487     } else {
488       Val = ValueVT.isFloatingPoint()
489                 ? DAG.getFPExtendOrRound(Val, DL, ValueSVT)
490                 : DAG.getAnyExtOrTrunc(Val, DL, ValueSVT);
491     }
492   }
493 
494   return DAG.getBuildVector(ValueVT, DL, Val);
495 }
496 
497 static void getCopyToPartsVector(SelectionDAG &DAG, const SDLoc &dl,
498                                  SDValue Val, SDValue *Parts, unsigned NumParts,
499                                  MVT PartVT, const Value *V,
500                                  std::optional<CallingConv::ID> CallConv);
501 
502 /// getCopyToParts - Create a series of nodes that contain the specified value
503 /// split into legal parts.  If the parts contain more bits than Val, then, for
504 /// integers, ExtendKind can be used to specify how to generate the extra bits.
505 static void
506 getCopyToParts(SelectionDAG &DAG, const SDLoc &DL, SDValue Val, SDValue *Parts,
507                unsigned NumParts, MVT PartVT, const Value *V,
508                std::optional<CallingConv::ID> CallConv = std::nullopt,
509                ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
510   // Let the target split the parts if it wants to
511   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
512   if (TLI.splitValueIntoRegisterParts(DAG, DL, Val, Parts, NumParts, PartVT,
513                                       CallConv))
514     return;
515   EVT ValueVT = Val.getValueType();
516 
517   // Handle the vector case separately.
518   if (ValueVT.isVector())
519     return getCopyToPartsVector(DAG, DL, Val, Parts, NumParts, PartVT, V,
520                                 CallConv);
521 
522   unsigned OrigNumParts = NumParts;
523   assert(DAG.getTargetLoweringInfo().isTypeLegal(PartVT) &&
524          "Copying to an illegal type!");
525 
526   if (NumParts == 0)
527     return;
528 
529   assert(!ValueVT.isVector() && "Vector case handled elsewhere");
530   EVT PartEVT = PartVT;
531   if (PartEVT == ValueVT) {
532     assert(NumParts == 1 && "No-op copy with multiple parts!");
533     Parts[0] = Val;
534     return;
535   }
536 
537   unsigned PartBits = PartVT.getSizeInBits();
538   if (NumParts * PartBits > ValueVT.getSizeInBits()) {
539     // If the parts cover more bits than the value has, promote the value.
540     if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
541       assert(NumParts == 1 && "Do not know what to promote to!");
542       Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val);
543     } else {
544       if (ValueVT.isFloatingPoint()) {
545         // FP values need to be bitcast, then extended if they are being put
546         // into a larger container.
547         ValueVT = EVT::getIntegerVT(*DAG.getContext(),  ValueVT.getSizeInBits());
548         Val = DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
549       }
550       assert((PartVT.isInteger() || PartVT == MVT::x86mmx) &&
551              ValueVT.isInteger() &&
552              "Unknown mismatch!");
553       ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
554       Val = DAG.getNode(ExtendKind, DL, ValueVT, Val);
555       if (PartVT == MVT::x86mmx)
556         Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
557     }
558   } else if (PartBits == ValueVT.getSizeInBits()) {
559     // Different types of the same size.
560     assert(NumParts == 1 && PartEVT != ValueVT);
561     Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
562   } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
563     // If the parts cover less bits than value has, truncate the value.
564     assert((PartVT.isInteger() || PartVT == MVT::x86mmx) &&
565            ValueVT.isInteger() &&
566            "Unknown mismatch!");
567     ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
568     Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
569     if (PartVT == MVT::x86mmx)
570       Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
571   }
572 
573   // The value may have changed - recompute ValueVT.
574   ValueVT = Val.getValueType();
575   assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
576          "Failed to tile the value with PartVT!");
577 
578   if (NumParts == 1) {
579     if (PartEVT != ValueVT) {
580       diagnosePossiblyInvalidConstraint(*DAG.getContext(), V,
581                                         "scalar-to-vector conversion failed");
582       Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
583     }
584 
585     Parts[0] = Val;
586     return;
587   }
588 
589   // Expand the value into multiple parts.
590   if (NumParts & (NumParts - 1)) {
591     // The number of parts is not a power of 2.  Split off and copy the tail.
592     assert(PartVT.isInteger() && ValueVT.isInteger() &&
593            "Do not know what to expand to!");
594     unsigned RoundParts = llvm::bit_floor(NumParts);
595     unsigned RoundBits = RoundParts * PartBits;
596     unsigned OddParts = NumParts - RoundParts;
597     SDValue OddVal = DAG.getNode(ISD::SRL, DL, ValueVT, Val,
598       DAG.getShiftAmountConstant(RoundBits, ValueVT, DL));
599 
600     getCopyToParts(DAG, DL, OddVal, Parts + RoundParts, OddParts, PartVT, V,
601                    CallConv);
602 
603     if (DAG.getDataLayout().isBigEndian())
604       // The odd parts were reversed by getCopyToParts - unreverse them.
605       std::reverse(Parts + RoundParts, Parts + NumParts);
606 
607     NumParts = RoundParts;
608     ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
609     Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
610   }
611 
612   // The number of parts is a power of 2.  Repeatedly bisect the value using
613   // EXTRACT_ELEMENT.
614   Parts[0] = DAG.getNode(ISD::BITCAST, DL,
615                          EVT::getIntegerVT(*DAG.getContext(),
616                                            ValueVT.getSizeInBits()),
617                          Val);
618 
619   for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
620     for (unsigned i = 0; i < NumParts; i += StepSize) {
621       unsigned ThisBits = StepSize * PartBits / 2;
622       EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
623       SDValue &Part0 = Parts[i];
624       SDValue &Part1 = Parts[i+StepSize/2];
625 
626       Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
627                           ThisVT, Part0, DAG.getIntPtrConstant(1, DL));
628       Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
629                           ThisVT, Part0, DAG.getIntPtrConstant(0, DL));
630 
631       if (ThisBits == PartBits && ThisVT != PartVT) {
632         Part0 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part0);
633         Part1 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part1);
634       }
635     }
636   }
637 
638   if (DAG.getDataLayout().isBigEndian())
639     std::reverse(Parts, Parts + OrigNumParts);
640 }
641 
642 static SDValue widenVectorToPartType(SelectionDAG &DAG, SDValue Val,
643                                      const SDLoc &DL, EVT PartVT) {
644   if (!PartVT.isVector())
645     return SDValue();
646 
647   EVT ValueVT = Val.getValueType();
648   EVT PartEVT = PartVT.getVectorElementType();
649   EVT ValueEVT = ValueVT.getVectorElementType();
650   ElementCount PartNumElts = PartVT.getVectorElementCount();
651   ElementCount ValueNumElts = ValueVT.getVectorElementCount();
652 
653   // We only support widening vectors with equivalent element types and
654   // fixed/scalable properties. If a target needs to widen a fixed-length type
655   // to a scalable one, it should be possible to use INSERT_SUBVECTOR below.
656   if (ElementCount::isKnownLE(PartNumElts, ValueNumElts) ||
657       PartNumElts.isScalable() != ValueNumElts.isScalable())
658     return SDValue();
659 
660   // Have a try for bf16 because some targets share its ABI with fp16.
661   if (ValueEVT == MVT::bf16 && PartEVT == MVT::f16) {
662     assert(DAG.getTargetLoweringInfo().isTypeLegal(PartVT) &&
663            "Cannot widen to illegal type");
664     Val = DAG.getNode(ISD::BITCAST, DL,
665                       ValueVT.changeVectorElementType(MVT::f16), Val);
666   } else if (PartEVT != ValueEVT) {
667     return SDValue();
668   }
669 
670   // Widening a scalable vector to another scalable vector is done by inserting
671   // the vector into a larger undef one.
672   if (PartNumElts.isScalable())
673     return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, PartVT, DAG.getUNDEF(PartVT),
674                        Val, DAG.getVectorIdxConstant(0, DL));
675 
676   // Vector widening case, e.g. <2 x float> -> <4 x float>.  Shuffle in
677   // undef elements.
678   SmallVector<SDValue, 16> Ops;
679   DAG.ExtractVectorElements(Val, Ops);
680   SDValue EltUndef = DAG.getUNDEF(PartEVT);
681   Ops.append((PartNumElts - ValueNumElts).getFixedValue(), EltUndef);
682 
683   // FIXME: Use CONCAT for 2x -> 4x.
684   return DAG.getBuildVector(PartVT, DL, Ops);
685 }
686 
687 /// getCopyToPartsVector - Create a series of nodes that contain the specified
688 /// value split into legal parts.
689 static void getCopyToPartsVector(SelectionDAG &DAG, const SDLoc &DL,
690                                  SDValue Val, SDValue *Parts, unsigned NumParts,
691                                  MVT PartVT, const Value *V,
692                                  std::optional<CallingConv::ID> CallConv) {
693   EVT ValueVT = Val.getValueType();
694   assert(ValueVT.isVector() && "Not a vector");
695   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
696   const bool IsABIRegCopy = CallConv.has_value();
697 
698   if (NumParts == 1) {
699     EVT PartEVT = PartVT;
700     if (PartEVT == ValueVT) {
701       // Nothing to do.
702     } else if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
703       // Bitconvert vector->vector case.
704       Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
705     } else if (SDValue Widened = widenVectorToPartType(DAG, Val, DL, PartVT)) {
706       Val = Widened;
707     } else if (PartVT.isVector() &&
708                PartEVT.getVectorElementType().bitsGE(
709                    ValueVT.getVectorElementType()) &&
710                PartEVT.getVectorElementCount() ==
711                    ValueVT.getVectorElementCount()) {
712 
713       // Promoted vector extract
714       Val = DAG.getAnyExtOrTrunc(Val, DL, PartVT);
715     } else if (PartEVT.isVector() &&
716                PartEVT.getVectorElementType() !=
717                    ValueVT.getVectorElementType() &&
718                TLI.getTypeAction(*DAG.getContext(), ValueVT) ==
719                    TargetLowering::TypeWidenVector) {
720       // Combination of widening and promotion.
721       EVT WidenVT =
722           EVT::getVectorVT(*DAG.getContext(), ValueVT.getVectorElementType(),
723                            PartVT.getVectorElementCount());
724       SDValue Widened = widenVectorToPartType(DAG, Val, DL, WidenVT);
725       Val = DAG.getAnyExtOrTrunc(Widened, DL, PartVT);
726     } else {
727       // Don't extract an integer from a float vector. This can happen if the
728       // FP type gets softened to integer and then promoted. The promotion
729       // prevents it from being picked up by the earlier bitcast case.
730       if (ValueVT.getVectorElementCount().isScalar() &&
731           (!ValueVT.isFloatingPoint() || !PartVT.isInteger())) {
732         // If we reach this condition and PartVT is FP, this means that
733         // ValueVT is also FP and both have a different size, otherwise we
734         // would have bitcasted them. Producing an EXTRACT_VECTOR_ELT here
735         // would be invalid since that would mean the smaller FP type has to
736         // be extended to the larger one.
737         if (PartVT.isFloatingPoint()) {
738           Val = DAG.getBitcast(ValueVT.getScalarType(), Val);
739           Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val);
740         } else
741           Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, PartVT, Val,
742                             DAG.getVectorIdxConstant(0, DL));
743       } else {
744         uint64_t ValueSize = ValueVT.getFixedSizeInBits();
745         assert(PartVT.getFixedSizeInBits() > ValueSize &&
746                "lossy conversion of vector to scalar type");
747         EVT IntermediateType = EVT::getIntegerVT(*DAG.getContext(), ValueSize);
748         Val = DAG.getBitcast(IntermediateType, Val);
749         Val = DAG.getAnyExtOrTrunc(Val, DL, PartVT);
750       }
751     }
752 
753     assert(Val.getValueType() == PartVT && "Unexpected vector part value type");
754     Parts[0] = Val;
755     return;
756   }
757 
758   // Handle a multi-element vector.
759   EVT IntermediateVT;
760   MVT RegisterVT;
761   unsigned NumIntermediates;
762   unsigned NumRegs;
763   if (IsABIRegCopy) {
764     NumRegs = TLI.getVectorTypeBreakdownForCallingConv(
765         *DAG.getContext(), *CallConv, ValueVT, IntermediateVT, NumIntermediates,
766         RegisterVT);
767   } else {
768     NumRegs =
769         TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
770                                    NumIntermediates, RegisterVT);
771   }
772 
773   assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
774   NumParts = NumRegs; // Silence a compiler warning.
775   assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
776 
777   assert(IntermediateVT.isScalableVector() == ValueVT.isScalableVector() &&
778          "Mixing scalable and fixed vectors when copying in parts");
779 
780   std::optional<ElementCount> DestEltCnt;
781 
782   if (IntermediateVT.isVector())
783     DestEltCnt = IntermediateVT.getVectorElementCount() * NumIntermediates;
784   else
785     DestEltCnt = ElementCount::getFixed(NumIntermediates);
786 
787   EVT BuiltVectorTy = EVT::getVectorVT(
788       *DAG.getContext(), IntermediateVT.getScalarType(), *DestEltCnt);
789 
790   if (ValueVT == BuiltVectorTy) {
791     // Nothing to do.
792   } else if (ValueVT.getSizeInBits() == BuiltVectorTy.getSizeInBits()) {
793     // Bitconvert vector->vector case.
794     Val = DAG.getNode(ISD::BITCAST, DL, BuiltVectorTy, Val);
795   } else {
796     if (BuiltVectorTy.getVectorElementType().bitsGT(
797             ValueVT.getVectorElementType())) {
798       // Integer promotion.
799       ValueVT = EVT::getVectorVT(*DAG.getContext(),
800                                  BuiltVectorTy.getVectorElementType(),
801                                  ValueVT.getVectorElementCount());
802       Val = DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val);
803     }
804 
805     if (SDValue Widened = widenVectorToPartType(DAG, Val, DL, BuiltVectorTy)) {
806       Val = Widened;
807     }
808   }
809 
810   assert(Val.getValueType() == BuiltVectorTy && "Unexpected vector value type");
811 
812   // Split the vector into intermediate operands.
813   SmallVector<SDValue, 8> Ops(NumIntermediates);
814   for (unsigned i = 0; i != NumIntermediates; ++i) {
815     if (IntermediateVT.isVector()) {
816       // This does something sensible for scalable vectors - see the
817       // definition of EXTRACT_SUBVECTOR for further details.
818       unsigned IntermediateNumElts = IntermediateVT.getVectorMinNumElements();
819       Ops[i] =
820           DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, IntermediateVT, Val,
821                       DAG.getVectorIdxConstant(i * IntermediateNumElts, DL));
822     } else {
823       Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntermediateVT, Val,
824                            DAG.getVectorIdxConstant(i, DL));
825     }
826   }
827 
828   // Split the intermediate operands into legal parts.
829   if (NumParts == NumIntermediates) {
830     // If the register was not expanded, promote or copy the value,
831     // as appropriate.
832     for (unsigned i = 0; i != NumParts; ++i)
833       getCopyToParts(DAG, DL, Ops[i], &Parts[i], 1, PartVT, V, CallConv);
834   } else if (NumParts > 0) {
835     // If the intermediate type was expanded, split each the value into
836     // legal parts.
837     assert(NumIntermediates != 0 && "division by zero");
838     assert(NumParts % NumIntermediates == 0 &&
839            "Must expand into a divisible number of parts!");
840     unsigned Factor = NumParts / NumIntermediates;
841     for (unsigned i = 0; i != NumIntermediates; ++i)
842       getCopyToParts(DAG, DL, Ops[i], &Parts[i * Factor], Factor, PartVT, V,
843                      CallConv);
844   }
845 }
846 
847 RegsForValue::RegsForValue(const SmallVector<unsigned, 4> &regs, MVT regvt,
848                            EVT valuevt, std::optional<CallingConv::ID> CC)
849     : ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs),
850       RegCount(1, regs.size()), CallConv(CC) {}
851 
852 RegsForValue::RegsForValue(LLVMContext &Context, const TargetLowering &TLI,
853                            const DataLayout &DL, unsigned Reg, Type *Ty,
854                            std::optional<CallingConv::ID> CC) {
855   ComputeValueVTs(TLI, DL, Ty, ValueVTs);
856 
857   CallConv = CC;
858 
859   for (EVT ValueVT : ValueVTs) {
860     unsigned NumRegs =
861         isABIMangled()
862             ? TLI.getNumRegistersForCallingConv(Context, *CC, ValueVT)
863             : TLI.getNumRegisters(Context, ValueVT);
864     MVT RegisterVT =
865         isABIMangled()
866             ? TLI.getRegisterTypeForCallingConv(Context, *CC, ValueVT)
867             : TLI.getRegisterType(Context, ValueVT);
868     for (unsigned i = 0; i != NumRegs; ++i)
869       Regs.push_back(Reg + i);
870     RegVTs.push_back(RegisterVT);
871     RegCount.push_back(NumRegs);
872     Reg += NumRegs;
873   }
874 }
875 
876 SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG,
877                                       FunctionLoweringInfo &FuncInfo,
878                                       const SDLoc &dl, SDValue &Chain,
879                                       SDValue *Glue, const Value *V) const {
880   // A Value with type {} or [0 x %t] needs no registers.
881   if (ValueVTs.empty())
882     return SDValue();
883 
884   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
885 
886   // Assemble the legal parts into the final values.
887   SmallVector<SDValue, 4> Values(ValueVTs.size());
888   SmallVector<SDValue, 8> Parts;
889   for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
890     // Copy the legal parts from the registers.
891     EVT ValueVT = ValueVTs[Value];
892     unsigned NumRegs = RegCount[Value];
893     MVT RegisterVT = isABIMangled()
894                          ? TLI.getRegisterTypeForCallingConv(
895                                *DAG.getContext(), *CallConv, RegVTs[Value])
896                          : RegVTs[Value];
897 
898     Parts.resize(NumRegs);
899     for (unsigned i = 0; i != NumRegs; ++i) {
900       SDValue P;
901       if (!Glue) {
902         P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
903       } else {
904         P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Glue);
905         *Glue = P.getValue(2);
906       }
907 
908       Chain = P.getValue(1);
909       Parts[i] = P;
910 
911       // If the source register was virtual and if we know something about it,
912       // add an assert node.
913       if (!Register::isVirtualRegister(Regs[Part + i]) ||
914           !RegisterVT.isInteger())
915         continue;
916 
917       const FunctionLoweringInfo::LiveOutInfo *LOI =
918         FuncInfo.GetLiveOutRegInfo(Regs[Part+i]);
919       if (!LOI)
920         continue;
921 
922       unsigned RegSize = RegisterVT.getScalarSizeInBits();
923       unsigned NumSignBits = LOI->NumSignBits;
924       unsigned NumZeroBits = LOI->Known.countMinLeadingZeros();
925 
926       if (NumZeroBits == RegSize) {
927         // The current value is a zero.
928         // Explicitly express that as it would be easier for
929         // optimizations to kick in.
930         Parts[i] = DAG.getConstant(0, dl, RegisterVT);
931         continue;
932       }
933 
934       // FIXME: We capture more information than the dag can represent.  For
935       // now, just use the tightest assertzext/assertsext possible.
936       bool isSExt;
937       EVT FromVT(MVT::Other);
938       if (NumZeroBits) {
939         FromVT = EVT::getIntegerVT(*DAG.getContext(), RegSize - NumZeroBits);
940         isSExt = false;
941       } else if (NumSignBits > 1) {
942         FromVT =
943             EVT::getIntegerVT(*DAG.getContext(), RegSize - NumSignBits + 1);
944         isSExt = true;
945       } else {
946         continue;
947       }
948       // Add an assertion node.
949       assert(FromVT != MVT::Other);
950       Parts[i] = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
951                              RegisterVT, P, DAG.getValueType(FromVT));
952     }
953 
954     Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(), NumRegs,
955                                      RegisterVT, ValueVT, V, Chain, CallConv);
956     Part += NumRegs;
957     Parts.clear();
958   }
959 
960   return DAG.getNode(ISD::MERGE_VALUES, dl, DAG.getVTList(ValueVTs), Values);
961 }
962 
963 void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG,
964                                  const SDLoc &dl, SDValue &Chain, SDValue *Glue,
965                                  const Value *V,
966                                  ISD::NodeType PreferredExtendType) const {
967   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
968   ISD::NodeType ExtendKind = PreferredExtendType;
969 
970   // Get the list of the values's legal parts.
971   unsigned NumRegs = Regs.size();
972   SmallVector<SDValue, 8> Parts(NumRegs);
973   for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
974     unsigned NumParts = RegCount[Value];
975 
976     MVT RegisterVT = isABIMangled()
977                          ? TLI.getRegisterTypeForCallingConv(
978                                *DAG.getContext(), *CallConv, RegVTs[Value])
979                          : RegVTs[Value];
980 
981     if (ExtendKind == ISD::ANY_EXTEND && TLI.isZExtFree(Val, RegisterVT))
982       ExtendKind = ISD::ZERO_EXTEND;
983 
984     getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value), &Parts[Part],
985                    NumParts, RegisterVT, V, CallConv, ExtendKind);
986     Part += NumParts;
987   }
988 
989   // Copy the parts into the registers.
990   SmallVector<SDValue, 8> Chains(NumRegs);
991   for (unsigned i = 0; i != NumRegs; ++i) {
992     SDValue Part;
993     if (!Glue) {
994       Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
995     } else {
996       Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Glue);
997       *Glue = Part.getValue(1);
998     }
999 
1000     Chains[i] = Part.getValue(0);
1001   }
1002 
1003   if (NumRegs == 1 || Glue)
1004     // If NumRegs > 1 && Glue is used then the use of the last CopyToReg is
1005     // flagged to it. That is the CopyToReg nodes and the user are considered
1006     // a single scheduling unit. If we create a TokenFactor and return it as
1007     // chain, then the TokenFactor is both a predecessor (operand) of the
1008     // user as well as a successor (the TF operands are flagged to the user).
1009     // c1, f1 = CopyToReg
1010     // c2, f2 = CopyToReg
1011     // c3     = TokenFactor c1, c2
1012     // ...
1013     //        = op c3, ..., f2
1014     Chain = Chains[NumRegs-1];
1015   else
1016     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
1017 }
1018 
1019 void RegsForValue::AddInlineAsmOperands(InlineAsm::Kind Code, bool HasMatching,
1020                                         unsigned MatchingIdx, const SDLoc &dl,
1021                                         SelectionDAG &DAG,
1022                                         std::vector<SDValue> &Ops) const {
1023   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1024 
1025   InlineAsm::Flag Flag(Code, Regs.size());
1026   if (HasMatching)
1027     Flag.setMatchingOp(MatchingIdx);
1028   else if (!Regs.empty() && Register::isVirtualRegister(Regs.front())) {
1029     // Put the register class of the virtual registers in the flag word.  That
1030     // way, later passes can recompute register class constraints for inline
1031     // assembly as well as normal instructions.
1032     // Don't do this for tied operands that can use the regclass information
1033     // from the def.
1034     const MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1035     const TargetRegisterClass *RC = MRI.getRegClass(Regs.front());
1036     Flag.setRegClass(RC->getID());
1037   }
1038 
1039   SDValue Res = DAG.getTargetConstant(Flag, dl, MVT::i32);
1040   Ops.push_back(Res);
1041 
1042   if (Code == InlineAsm::Kind::Clobber) {
1043     // Clobbers should always have a 1:1 mapping with registers, and may
1044     // reference registers that have illegal (e.g. vector) types. Hence, we
1045     // shouldn't try to apply any sort of splitting logic to them.
1046     assert(Regs.size() == RegVTs.size() && Regs.size() == ValueVTs.size() &&
1047            "No 1:1 mapping from clobbers to regs?");
1048     Register SP = TLI.getStackPointerRegisterToSaveRestore();
1049     (void)SP;
1050     for (unsigned I = 0, E = ValueVTs.size(); I != E; ++I) {
1051       Ops.push_back(DAG.getRegister(Regs[I], RegVTs[I]));
1052       assert(
1053           (Regs[I] != SP ||
1054            DAG.getMachineFunction().getFrameInfo().hasOpaqueSPAdjustment()) &&
1055           "If we clobbered the stack pointer, MFI should know about it.");
1056     }
1057     return;
1058   }
1059 
1060   for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
1061     MVT RegisterVT = RegVTs[Value];
1062     unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVTs[Value],
1063                                            RegisterVT);
1064     for (unsigned i = 0; i != NumRegs; ++i) {
1065       assert(Reg < Regs.size() && "Mismatch in # registers expected");
1066       unsigned TheReg = Regs[Reg++];
1067       Ops.push_back(DAG.getRegister(TheReg, RegisterVT));
1068     }
1069   }
1070 }
1071 
1072 SmallVector<std::pair<unsigned, TypeSize>, 4>
1073 RegsForValue::getRegsAndSizes() const {
1074   SmallVector<std::pair<unsigned, TypeSize>, 4> OutVec;
1075   unsigned I = 0;
1076   for (auto CountAndVT : zip_first(RegCount, RegVTs)) {
1077     unsigned RegCount = std::get<0>(CountAndVT);
1078     MVT RegisterVT = std::get<1>(CountAndVT);
1079     TypeSize RegisterSize = RegisterVT.getSizeInBits();
1080     for (unsigned E = I + RegCount; I != E; ++I)
1081       OutVec.push_back(std::make_pair(Regs[I], RegisterSize));
1082   }
1083   return OutVec;
1084 }
1085 
1086 void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis *aa,
1087                                AssumptionCache *ac,
1088                                const TargetLibraryInfo *li) {
1089   AA = aa;
1090   AC = ac;
1091   GFI = gfi;
1092   LibInfo = li;
1093   Context = DAG.getContext();
1094   LPadToCallSiteMap.clear();
1095   SL->init(DAG.getTargetLoweringInfo(), TM, DAG.getDataLayout());
1096   AssignmentTrackingEnabled = isAssignmentTrackingEnabled(
1097       *DAG.getMachineFunction().getFunction().getParent());
1098 }
1099 
1100 void SelectionDAGBuilder::clear() {
1101   NodeMap.clear();
1102   UnusedArgNodeMap.clear();
1103   PendingLoads.clear();
1104   PendingExports.clear();
1105   PendingConstrainedFP.clear();
1106   PendingConstrainedFPStrict.clear();
1107   CurInst = nullptr;
1108   HasTailCall = false;
1109   SDNodeOrder = LowestSDNodeOrder;
1110   StatepointLowering.clear();
1111 }
1112 
1113 void SelectionDAGBuilder::clearDanglingDebugInfo() {
1114   DanglingDebugInfoMap.clear();
1115 }
1116 
1117 // Update DAG root to include dependencies on Pending chains.
1118 SDValue SelectionDAGBuilder::updateRoot(SmallVectorImpl<SDValue> &Pending) {
1119   SDValue Root = DAG.getRoot();
1120 
1121   if (Pending.empty())
1122     return Root;
1123 
1124   // Add current root to PendingChains, unless we already indirectly
1125   // depend on it.
1126   if (Root.getOpcode() != ISD::EntryToken) {
1127     unsigned i = 0, e = Pending.size();
1128     for (; i != e; ++i) {
1129       assert(Pending[i].getNode()->getNumOperands() > 1);
1130       if (Pending[i].getNode()->getOperand(0) == Root)
1131         break;  // Don't add the root if we already indirectly depend on it.
1132     }
1133 
1134     if (i == e)
1135       Pending.push_back(Root);
1136   }
1137 
1138   if (Pending.size() == 1)
1139     Root = Pending[0];
1140   else
1141     Root = DAG.getTokenFactor(getCurSDLoc(), Pending);
1142 
1143   DAG.setRoot(Root);
1144   Pending.clear();
1145   return Root;
1146 }
1147 
1148 SDValue SelectionDAGBuilder::getMemoryRoot() {
1149   return updateRoot(PendingLoads);
1150 }
1151 
1152 SDValue SelectionDAGBuilder::getRoot() {
1153   // Chain up all pending constrained intrinsics together with all
1154   // pending loads, by simply appending them to PendingLoads and
1155   // then calling getMemoryRoot().
1156   PendingLoads.reserve(PendingLoads.size() +
1157                        PendingConstrainedFP.size() +
1158                        PendingConstrainedFPStrict.size());
1159   PendingLoads.append(PendingConstrainedFP.begin(),
1160                       PendingConstrainedFP.end());
1161   PendingLoads.append(PendingConstrainedFPStrict.begin(),
1162                       PendingConstrainedFPStrict.end());
1163   PendingConstrainedFP.clear();
1164   PendingConstrainedFPStrict.clear();
1165   return getMemoryRoot();
1166 }
1167 
1168 SDValue SelectionDAGBuilder::getControlRoot() {
1169   // We need to emit pending fpexcept.strict constrained intrinsics,
1170   // so append them to the PendingExports list.
1171   PendingExports.append(PendingConstrainedFPStrict.begin(),
1172                         PendingConstrainedFPStrict.end());
1173   PendingConstrainedFPStrict.clear();
1174   return updateRoot(PendingExports);
1175 }
1176 
1177 void SelectionDAGBuilder::handleDebugDeclare(Value *Address,
1178                                              DILocalVariable *Variable,
1179                                              DIExpression *Expression,
1180                                              DebugLoc DL) {
1181   assert(Variable && "Missing variable");
1182 
1183   // Check if address has undef value.
1184   if (!Address || isa<UndefValue>(Address) ||
1185       (Address->use_empty() && !isa<Argument>(Address))) {
1186     LLVM_DEBUG(
1187         dbgs()
1188         << "dbg_declare: Dropping debug info (bad/undef/unused-arg address)\n");
1189     return;
1190   }
1191 
1192   bool IsParameter = Variable->isParameter() || isa<Argument>(Address);
1193 
1194   SDValue &N = NodeMap[Address];
1195   if (!N.getNode() && isa<Argument>(Address))
1196     // Check unused arguments map.
1197     N = UnusedArgNodeMap[Address];
1198   SDDbgValue *SDV;
1199   if (N.getNode()) {
1200     if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
1201       Address = BCI->getOperand(0);
1202     // Parameters are handled specially.
1203     auto *FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
1204     if (IsParameter && FINode) {
1205       // Byval parameter. We have a frame index at this point.
1206       SDV = DAG.getFrameIndexDbgValue(Variable, Expression, FINode->getIndex(),
1207                                       /*IsIndirect*/ true, DL, SDNodeOrder);
1208     } else if (isa<Argument>(Address)) {
1209       // Address is an argument, so try to emit its dbg value using
1210       // virtual register info from the FuncInfo.ValueMap.
1211       EmitFuncArgumentDbgValue(Address, Variable, Expression, DL,
1212                                FuncArgumentDbgValueKind::Declare, N);
1213       return;
1214     } else {
1215       SDV = DAG.getDbgValue(Variable, Expression, N.getNode(), N.getResNo(),
1216                             true, DL, SDNodeOrder);
1217     }
1218     DAG.AddDbgValue(SDV, IsParameter);
1219   } else {
1220     // If Address is an argument then try to emit its dbg value using
1221     // virtual register info from the FuncInfo.ValueMap.
1222     if (!EmitFuncArgumentDbgValue(Address, Variable, Expression, DL,
1223                                   FuncArgumentDbgValueKind::Declare, N)) {
1224       LLVM_DEBUG(dbgs() << "dbg_declare: Dropping debug info"
1225                         << " (could not emit func-arg dbg_value)\n");
1226     }
1227   }
1228   return;
1229 }
1230 
1231 void SelectionDAGBuilder::visitDbgInfo(const Instruction &I) {
1232   // Add SDDbgValue nodes for any var locs here. Do so before updating
1233   // SDNodeOrder, as this mapping is {Inst -> Locs BEFORE Inst}.
1234   if (FunctionVarLocs const *FnVarLocs = DAG.getFunctionVarLocs()) {
1235     // Add SDDbgValue nodes for any var locs here. Do so before updating
1236     // SDNodeOrder, as this mapping is {Inst -> Locs BEFORE Inst}.
1237     for (auto It = FnVarLocs->locs_begin(&I), End = FnVarLocs->locs_end(&I);
1238          It != End; ++It) {
1239       auto *Var = FnVarLocs->getDILocalVariable(It->VariableID);
1240       dropDanglingDebugInfo(Var, It->Expr);
1241       if (It->Values.isKillLocation(It->Expr)) {
1242         handleKillDebugValue(Var, It->Expr, It->DL, SDNodeOrder);
1243         continue;
1244       }
1245       SmallVector<Value *> Values(It->Values.location_ops());
1246       if (!handleDebugValue(Values, Var, It->Expr, It->DL, SDNodeOrder,
1247                             It->Values.hasArgList())) {
1248         SmallVector<Value *, 4> Vals;
1249         for (Value *V : It->Values.location_ops())
1250           Vals.push_back(V);
1251         addDanglingDebugInfo(Vals,
1252                              FnVarLocs->getDILocalVariable(It->VariableID),
1253                              It->Expr, Vals.size() > 1, It->DL, SDNodeOrder);
1254       }
1255     }
1256   }
1257 
1258   // We must skip DbgVariableRecords if they've already been processed above as
1259   // we have just emitted the debug values resulting from assignment tracking
1260   // analysis, making any existing DbgVariableRecords redundant (and probably
1261   // less correct). We still need to process DbgLabelRecords. This does sink
1262   // DbgLabelRecords to the bottom of the group of debug records. That sholdn't
1263   // be important as it does so deterministcally and ordering between
1264   // DbgLabelRecords and DbgVariableRecords is immaterial (other than for MIR/IR
1265   // printing).
1266   bool SkipDbgVariableRecords = DAG.getFunctionVarLocs();
1267   // Is there is any debug-info attached to this instruction, in the form of
1268   // DbgRecord non-instruction debug-info records.
1269   for (DbgRecord &DR : I.getDbgRecordRange()) {
1270     if (DbgLabelRecord *DLR = dyn_cast<DbgLabelRecord>(&DR)) {
1271       assert(DLR->getLabel() && "Missing label");
1272       SDDbgLabel *SDV =
1273           DAG.getDbgLabel(DLR->getLabel(), DLR->getDebugLoc(), SDNodeOrder);
1274       DAG.AddDbgLabel(SDV);
1275       continue;
1276     }
1277 
1278     if (SkipDbgVariableRecords)
1279       continue;
1280     DbgVariableRecord &DVR = cast<DbgVariableRecord>(DR);
1281     DILocalVariable *Variable = DVR.getVariable();
1282     DIExpression *Expression = DVR.getExpression();
1283     dropDanglingDebugInfo(Variable, Expression);
1284 
1285     if (DVR.getType() == DbgVariableRecord::LocationType::Declare) {
1286       if (FuncInfo.PreprocessedDVRDeclares.contains(&DVR))
1287         continue;
1288       LLVM_DEBUG(dbgs() << "SelectionDAG visiting dbg_declare: " << DVR
1289                         << "\n");
1290       handleDebugDeclare(DVR.getVariableLocationOp(0), Variable, Expression,
1291                          DVR.getDebugLoc());
1292       continue;
1293     }
1294 
1295     // A DbgVariableRecord with no locations is a kill location.
1296     SmallVector<Value *, 4> Values(DVR.location_ops());
1297     if (Values.empty()) {
1298       handleKillDebugValue(Variable, Expression, DVR.getDebugLoc(),
1299                            SDNodeOrder);
1300       continue;
1301     }
1302 
1303     // A DbgVariableRecord with an undef or absent location is also a kill
1304     // location.
1305     if (llvm::any_of(Values,
1306                      [](Value *V) { return !V || isa<UndefValue>(V); })) {
1307       handleKillDebugValue(Variable, Expression, DVR.getDebugLoc(),
1308                            SDNodeOrder);
1309       continue;
1310     }
1311 
1312     bool IsVariadic = DVR.hasArgList();
1313     if (!handleDebugValue(Values, Variable, Expression, DVR.getDebugLoc(),
1314                           SDNodeOrder, IsVariadic)) {
1315       addDanglingDebugInfo(Values, Variable, Expression, IsVariadic,
1316                            DVR.getDebugLoc(), SDNodeOrder);
1317     }
1318   }
1319 }
1320 
1321 void SelectionDAGBuilder::visit(const Instruction &I) {
1322   visitDbgInfo(I);
1323 
1324   // Set up outgoing PHI node register values before emitting the terminator.
1325   if (I.isTerminator()) {
1326     HandlePHINodesInSuccessorBlocks(I.getParent());
1327   }
1328 
1329   // Increase the SDNodeOrder if dealing with a non-debug instruction.
1330   if (!isa<DbgInfoIntrinsic>(I))
1331     ++SDNodeOrder;
1332 
1333   CurInst = &I;
1334 
1335   // Set inserted listener only if required.
1336   bool NodeInserted = false;
1337   std::unique_ptr<SelectionDAG::DAGNodeInsertedListener> InsertedListener;
1338   MDNode *PCSectionsMD = I.getMetadata(LLVMContext::MD_pcsections);
1339   MDNode *MMRA = I.getMetadata(LLVMContext::MD_mmra);
1340   if (PCSectionsMD || MMRA) {
1341     InsertedListener = std::make_unique<SelectionDAG::DAGNodeInsertedListener>(
1342         DAG, [&](SDNode *) { NodeInserted = true; });
1343   }
1344 
1345   visit(I.getOpcode(), I);
1346 
1347   if (!I.isTerminator() && !HasTailCall &&
1348       !isa<GCStatepointInst>(I)) // statepoints handle their exports internally
1349     CopyToExportRegsIfNeeded(&I);
1350 
1351   // Handle metadata.
1352   if (PCSectionsMD || MMRA) {
1353     auto It = NodeMap.find(&I);
1354     if (It != NodeMap.end()) {
1355       if (PCSectionsMD)
1356         DAG.addPCSections(It->second.getNode(), PCSectionsMD);
1357       if (MMRA)
1358         DAG.addMMRAMetadata(It->second.getNode(), MMRA);
1359     } else if (NodeInserted) {
1360       // This should not happen; if it does, don't let it go unnoticed so we can
1361       // fix it. Relevant visit*() function is probably missing a setValue().
1362       errs() << "warning: loosing !pcsections and/or !mmra metadata ["
1363              << I.getModule()->getName() << "]\n";
1364       LLVM_DEBUG(I.dump());
1365       assert(false);
1366     }
1367   }
1368 
1369   CurInst = nullptr;
1370 }
1371 
1372 void SelectionDAGBuilder::visitPHI(const PHINode &) {
1373   llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
1374 }
1375 
1376 void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
1377   // Note: this doesn't use InstVisitor, because it has to work with
1378   // ConstantExpr's in addition to instructions.
1379   switch (Opcode) {
1380   default: llvm_unreachable("Unknown instruction type encountered!");
1381     // Build the switch statement using the Instruction.def file.
1382 #define HANDLE_INST(NUM, OPCODE, CLASS) \
1383     case Instruction::OPCODE: visit##OPCODE((const CLASS&)I); break;
1384 #include "llvm/IR/Instruction.def"
1385   }
1386 }
1387 
1388 static bool handleDanglingVariadicDebugInfo(SelectionDAG &DAG,
1389                                             DILocalVariable *Variable,
1390                                             DebugLoc DL, unsigned Order,
1391                                             SmallVectorImpl<Value *> &Values,
1392                                             DIExpression *Expression) {
1393   // For variadic dbg_values we will now insert an undef.
1394   // FIXME: We can potentially recover these!
1395   SmallVector<SDDbgOperand, 2> Locs;
1396   for (const Value *V : Values) {
1397     auto *Undef = UndefValue::get(V->getType());
1398     Locs.push_back(SDDbgOperand::fromConst(Undef));
1399   }
1400   SDDbgValue *SDV = DAG.getDbgValueList(Variable, Expression, Locs, {},
1401                                         /*IsIndirect=*/false, DL, Order,
1402                                         /*IsVariadic=*/true);
1403   DAG.AddDbgValue(SDV, /*isParameter=*/false);
1404   return true;
1405 }
1406 
1407 void SelectionDAGBuilder::addDanglingDebugInfo(SmallVectorImpl<Value *> &Values,
1408                                                DILocalVariable *Var,
1409                                                DIExpression *Expr,
1410                                                bool IsVariadic, DebugLoc DL,
1411                                                unsigned Order) {
1412   if (IsVariadic) {
1413     handleDanglingVariadicDebugInfo(DAG, Var, DL, Order, Values, Expr);
1414     return;
1415   }
1416   // TODO: Dangling debug info will eventually either be resolved or produce
1417   // an Undef DBG_VALUE. However in the resolution case, a gap may appear
1418   // between the original dbg.value location and its resolved DBG_VALUE,
1419   // which we should ideally fill with an extra Undef DBG_VALUE.
1420   assert(Values.size() == 1);
1421   DanglingDebugInfoMap[Values[0]].emplace_back(Var, Expr, DL, Order);
1422 }
1423 
1424 void SelectionDAGBuilder::dropDanglingDebugInfo(const DILocalVariable *Variable,
1425                                                 const DIExpression *Expr) {
1426   auto isMatchingDbgValue = [&](DanglingDebugInfo &DDI) {
1427     DIVariable *DanglingVariable = DDI.getVariable();
1428     DIExpression *DanglingExpr = DDI.getExpression();
1429     if (DanglingVariable == Variable && Expr->fragmentsOverlap(DanglingExpr)) {
1430       LLVM_DEBUG(dbgs() << "Dropping dangling debug info for "
1431                         << printDDI(nullptr, DDI) << "\n");
1432       return true;
1433     }
1434     return false;
1435   };
1436 
1437   for (auto &DDIMI : DanglingDebugInfoMap) {
1438     DanglingDebugInfoVector &DDIV = DDIMI.second;
1439 
1440     // If debug info is to be dropped, run it through final checks to see
1441     // whether it can be salvaged.
1442     for (auto &DDI : DDIV)
1443       if (isMatchingDbgValue(DDI))
1444         salvageUnresolvedDbgValue(DDIMI.first, DDI);
1445 
1446     erase_if(DDIV, isMatchingDbgValue);
1447   }
1448 }
1449 
1450 // resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V,
1451 // generate the debug data structures now that we've seen its definition.
1452 void SelectionDAGBuilder::resolveDanglingDebugInfo(const Value *V,
1453                                                    SDValue Val) {
1454   auto DanglingDbgInfoIt = DanglingDebugInfoMap.find(V);
1455   if (DanglingDbgInfoIt == DanglingDebugInfoMap.end())
1456     return;
1457 
1458   DanglingDebugInfoVector &DDIV = DanglingDbgInfoIt->second;
1459   for (auto &DDI : DDIV) {
1460     DebugLoc DL = DDI.getDebugLoc();
1461     unsigned ValSDNodeOrder = Val.getNode()->getIROrder();
1462     unsigned DbgSDNodeOrder = DDI.getSDNodeOrder();
1463     DILocalVariable *Variable = DDI.getVariable();
1464     DIExpression *Expr = DDI.getExpression();
1465     assert(Variable->isValidLocationForIntrinsic(DL) &&
1466            "Expected inlined-at fields to agree");
1467     SDDbgValue *SDV;
1468     if (Val.getNode()) {
1469       // FIXME: I doubt that it is correct to resolve a dangling DbgValue as a
1470       // FuncArgumentDbgValue (it would be hoisted to the function entry, and if
1471       // we couldn't resolve it directly when examining the DbgValue intrinsic
1472       // in the first place we should not be more successful here). Unless we
1473       // have some test case that prove this to be correct we should avoid
1474       // calling EmitFuncArgumentDbgValue here.
1475       if (!EmitFuncArgumentDbgValue(V, Variable, Expr, DL,
1476                                     FuncArgumentDbgValueKind::Value, Val)) {
1477         LLVM_DEBUG(dbgs() << "Resolve dangling debug info for "
1478                           << printDDI(V, DDI) << "\n");
1479         LLVM_DEBUG(dbgs() << "  By mapping to:\n    "; Val.dump());
1480         // Increase the SDNodeOrder for the DbgValue here to make sure it is
1481         // inserted after the definition of Val when emitting the instructions
1482         // after ISel. An alternative could be to teach
1483         // ScheduleDAGSDNodes::EmitSchedule to delay the insertion properly.
1484         LLVM_DEBUG(if (ValSDNodeOrder > DbgSDNodeOrder) dbgs()
1485                    << "changing SDNodeOrder from " << DbgSDNodeOrder << " to "
1486                    << ValSDNodeOrder << "\n");
1487         SDV = getDbgValue(Val, Variable, Expr, DL,
1488                           std::max(DbgSDNodeOrder, ValSDNodeOrder));
1489         DAG.AddDbgValue(SDV, false);
1490       } else
1491         LLVM_DEBUG(dbgs() << "Resolved dangling debug info for "
1492                           << printDDI(V, DDI)
1493                           << " in EmitFuncArgumentDbgValue\n");
1494     } else {
1495       LLVM_DEBUG(dbgs() << "Dropping debug info for " << printDDI(V, DDI)
1496                         << "\n");
1497       auto Undef = UndefValue::get(V->getType());
1498       auto SDV =
1499           DAG.getConstantDbgValue(Variable, Expr, Undef, DL, DbgSDNodeOrder);
1500       DAG.AddDbgValue(SDV, false);
1501     }
1502   }
1503   DDIV.clear();
1504 }
1505 
1506 void SelectionDAGBuilder::salvageUnresolvedDbgValue(const Value *V,
1507                                                     DanglingDebugInfo &DDI) {
1508   // TODO: For the variadic implementation, instead of only checking the fail
1509   // state of `handleDebugValue`, we need know specifically which values were
1510   // invalid, so that we attempt to salvage only those values when processing
1511   // a DIArgList.
1512   const Value *OrigV = V;
1513   DILocalVariable *Var = DDI.getVariable();
1514   DIExpression *Expr = DDI.getExpression();
1515   DebugLoc DL = DDI.getDebugLoc();
1516   unsigned SDOrder = DDI.getSDNodeOrder();
1517 
1518   // Currently we consider only dbg.value intrinsics -- we tell the salvager
1519   // that DW_OP_stack_value is desired.
1520   bool StackValue = true;
1521 
1522   // Can this Value can be encoded without any further work?
1523   if (handleDebugValue(V, Var, Expr, DL, SDOrder, /*IsVariadic=*/false))
1524     return;
1525 
1526   // Attempt to salvage back through as many instructions as possible. Bail if
1527   // a non-instruction is seen, such as a constant expression or global
1528   // variable. FIXME: Further work could recover those too.
1529   while (isa<Instruction>(V)) {
1530     const Instruction &VAsInst = *cast<const Instruction>(V);
1531     // Temporary "0", awaiting real implementation.
1532     SmallVector<uint64_t, 16> Ops;
1533     SmallVector<Value *, 4> AdditionalValues;
1534     V = salvageDebugInfoImpl(const_cast<Instruction &>(VAsInst),
1535                              Expr->getNumLocationOperands(), Ops,
1536                              AdditionalValues);
1537     // If we cannot salvage any further, and haven't yet found a suitable debug
1538     // expression, bail out.
1539     if (!V)
1540       break;
1541 
1542     // TODO: If AdditionalValues isn't empty, then the salvage can only be
1543     // represented with a DBG_VALUE_LIST, so we give up. When we have support
1544     // here for variadic dbg_values, remove that condition.
1545     if (!AdditionalValues.empty())
1546       break;
1547 
1548     // New value and expr now represent this debuginfo.
1549     Expr = DIExpression::appendOpsToArg(Expr, Ops, 0, StackValue);
1550 
1551     // Some kind of simplification occurred: check whether the operand of the
1552     // salvaged debug expression can be encoded in this DAG.
1553     if (handleDebugValue(V, Var, Expr, DL, SDOrder, /*IsVariadic=*/false)) {
1554       LLVM_DEBUG(
1555           dbgs() << "Salvaged debug location info for:\n  " << *Var << "\n"
1556                  << *OrigV << "\nBy stripping back to:\n  " << *V << "\n");
1557       return;
1558     }
1559   }
1560 
1561   // This was the final opportunity to salvage this debug information, and it
1562   // couldn't be done. Place an undef DBG_VALUE at this location to terminate
1563   // any earlier variable location.
1564   assert(OrigV && "V shouldn't be null");
1565   auto *Undef = UndefValue::get(OrigV->getType());
1566   auto *SDV = DAG.getConstantDbgValue(Var, Expr, Undef, DL, SDNodeOrder);
1567   DAG.AddDbgValue(SDV, false);
1568   LLVM_DEBUG(dbgs() << "Dropping debug value info for:\n  "
1569                     << printDDI(OrigV, DDI) << "\n");
1570 }
1571 
1572 void SelectionDAGBuilder::handleKillDebugValue(DILocalVariable *Var,
1573                                                DIExpression *Expr,
1574                                                DebugLoc DbgLoc,
1575                                                unsigned Order) {
1576   Value *Poison = PoisonValue::get(Type::getInt1Ty(*Context));
1577   DIExpression *NewExpr =
1578       const_cast<DIExpression *>(DIExpression::convertToUndefExpression(Expr));
1579   handleDebugValue(Poison, Var, NewExpr, DbgLoc, Order,
1580                    /*IsVariadic*/ false);
1581 }
1582 
1583 bool SelectionDAGBuilder::handleDebugValue(ArrayRef<const Value *> Values,
1584                                            DILocalVariable *Var,
1585                                            DIExpression *Expr, DebugLoc DbgLoc,
1586                                            unsigned Order, bool IsVariadic) {
1587   if (Values.empty())
1588     return true;
1589 
1590   // Filter EntryValue locations out early.
1591   if (visitEntryValueDbgValue(Values, Var, Expr, DbgLoc))
1592     return true;
1593 
1594   SmallVector<SDDbgOperand> LocationOps;
1595   SmallVector<SDNode *> Dependencies;
1596   for (const Value *V : Values) {
1597     // Constant value.
1598     if (isa<ConstantInt>(V) || isa<ConstantFP>(V) || isa<UndefValue>(V) ||
1599         isa<ConstantPointerNull>(V)) {
1600       LocationOps.emplace_back(SDDbgOperand::fromConst(V));
1601       continue;
1602     }
1603 
1604     // Look through IntToPtr constants.
1605     if (auto *CE = dyn_cast<ConstantExpr>(V))
1606       if (CE->getOpcode() == Instruction::IntToPtr) {
1607         LocationOps.emplace_back(SDDbgOperand::fromConst(CE->getOperand(0)));
1608         continue;
1609       }
1610 
1611     // If the Value is a frame index, we can create a FrameIndex debug value
1612     // without relying on the DAG at all.
1613     if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1614       auto SI = FuncInfo.StaticAllocaMap.find(AI);
1615       if (SI != FuncInfo.StaticAllocaMap.end()) {
1616         LocationOps.emplace_back(SDDbgOperand::fromFrameIdx(SI->second));
1617         continue;
1618       }
1619     }
1620 
1621     // Do not use getValue() in here; we don't want to generate code at
1622     // this point if it hasn't been done yet.
1623     SDValue N = NodeMap[V];
1624     if (!N.getNode() && isa<Argument>(V)) // Check unused arguments map.
1625       N = UnusedArgNodeMap[V];
1626     if (N.getNode()) {
1627       // Only emit func arg dbg value for non-variadic dbg.values for now.
1628       if (!IsVariadic &&
1629           EmitFuncArgumentDbgValue(V, Var, Expr, DbgLoc,
1630                                    FuncArgumentDbgValueKind::Value, N))
1631         return true;
1632       if (auto *FISDN = dyn_cast<FrameIndexSDNode>(N.getNode())) {
1633         // Construct a FrameIndexDbgValue for FrameIndexSDNodes so we can
1634         // describe stack slot locations.
1635         //
1636         // Consider "int x = 0; int *px = &x;". There are two kinds of
1637         // interesting debug values here after optimization:
1638         //
1639         //   dbg.value(i32* %px, !"int *px", !DIExpression()), and
1640         //   dbg.value(i32* %px, !"int x", !DIExpression(DW_OP_deref))
1641         //
1642         // Both describe the direct values of their associated variables.
1643         Dependencies.push_back(N.getNode());
1644         LocationOps.emplace_back(SDDbgOperand::fromFrameIdx(FISDN->getIndex()));
1645         continue;
1646       }
1647       LocationOps.emplace_back(
1648           SDDbgOperand::fromNode(N.getNode(), N.getResNo()));
1649       continue;
1650     }
1651 
1652     const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1653     // Special rules apply for the first dbg.values of parameter variables in a
1654     // function. Identify them by the fact they reference Argument Values, that
1655     // they're parameters, and they are parameters of the current function. We
1656     // need to let them dangle until they get an SDNode.
1657     bool IsParamOfFunc =
1658         isa<Argument>(V) && Var->isParameter() && !DbgLoc.getInlinedAt();
1659     if (IsParamOfFunc)
1660       return false;
1661 
1662     // The value is not used in this block yet (or it would have an SDNode).
1663     // We still want the value to appear for the user if possible -- if it has
1664     // an associated VReg, we can refer to that instead.
1665     auto VMI = FuncInfo.ValueMap.find(V);
1666     if (VMI != FuncInfo.ValueMap.end()) {
1667       unsigned Reg = VMI->second;
1668       // If this is a PHI node, it may be split up into several MI PHI nodes
1669       // (in FunctionLoweringInfo::set).
1670       RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg,
1671                        V->getType(), std::nullopt);
1672       if (RFV.occupiesMultipleRegs()) {
1673         // FIXME: We could potentially support variadic dbg_values here.
1674         if (IsVariadic)
1675           return false;
1676         unsigned Offset = 0;
1677         unsigned BitsToDescribe = 0;
1678         if (auto VarSize = Var->getSizeInBits())
1679           BitsToDescribe = *VarSize;
1680         if (auto Fragment = Expr->getFragmentInfo())
1681           BitsToDescribe = Fragment->SizeInBits;
1682         for (const auto &RegAndSize : RFV.getRegsAndSizes()) {
1683           // Bail out if all bits are described already.
1684           if (Offset >= BitsToDescribe)
1685             break;
1686           // TODO: handle scalable vectors.
1687           unsigned RegisterSize = RegAndSize.second;
1688           unsigned FragmentSize = (Offset + RegisterSize > BitsToDescribe)
1689                                       ? BitsToDescribe - Offset
1690                                       : RegisterSize;
1691           auto FragmentExpr = DIExpression::createFragmentExpression(
1692               Expr, Offset, FragmentSize);
1693           if (!FragmentExpr)
1694             continue;
1695           SDDbgValue *SDV = DAG.getVRegDbgValue(
1696               Var, *FragmentExpr, RegAndSize.first, false, DbgLoc, Order);
1697           DAG.AddDbgValue(SDV, false);
1698           Offset += RegisterSize;
1699         }
1700         return true;
1701       }
1702       // We can use simple vreg locations for variadic dbg_values as well.
1703       LocationOps.emplace_back(SDDbgOperand::fromVReg(Reg));
1704       continue;
1705     }
1706     // We failed to create a SDDbgOperand for V.
1707     return false;
1708   }
1709 
1710   // We have created a SDDbgOperand for each Value in Values.
1711   assert(!LocationOps.empty());
1712   SDDbgValue *SDV =
1713       DAG.getDbgValueList(Var, Expr, LocationOps, Dependencies,
1714                           /*IsIndirect=*/false, DbgLoc, Order, IsVariadic);
1715   DAG.AddDbgValue(SDV, /*isParameter=*/false);
1716   return true;
1717 }
1718 
1719 void SelectionDAGBuilder::resolveOrClearDbgInfo() {
1720   // Try to fixup any remaining dangling debug info -- and drop it if we can't.
1721   for (auto &Pair : DanglingDebugInfoMap)
1722     for (auto &DDI : Pair.second)
1723       salvageUnresolvedDbgValue(const_cast<Value *>(Pair.first), DDI);
1724   clearDanglingDebugInfo();
1725 }
1726 
1727 /// getCopyFromRegs - If there was virtual register allocated for the value V
1728 /// emit CopyFromReg of the specified type Ty. Return empty SDValue() otherwise.
1729 SDValue SelectionDAGBuilder::getCopyFromRegs(const Value *V, Type *Ty) {
1730   DenseMap<const Value *, Register>::iterator It = FuncInfo.ValueMap.find(V);
1731   SDValue Result;
1732 
1733   if (It != FuncInfo.ValueMap.end()) {
1734     Register InReg = It->second;
1735 
1736     RegsForValue RFV(*DAG.getContext(), DAG.getTargetLoweringInfo(),
1737                      DAG.getDataLayout(), InReg, Ty,
1738                      std::nullopt); // This is not an ABI copy.
1739     SDValue Chain = DAG.getEntryNode();
1740     Result = RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, nullptr,
1741                                  V);
1742     resolveDanglingDebugInfo(V, Result);
1743   }
1744 
1745   return Result;
1746 }
1747 
1748 /// getValue - Return an SDValue for the given Value.
1749 SDValue SelectionDAGBuilder::getValue(const Value *V) {
1750   // If we already have an SDValue for this value, use it. It's important
1751   // to do this first, so that we don't create a CopyFromReg if we already
1752   // have a regular SDValue.
1753   SDValue &N = NodeMap[V];
1754   if (N.getNode()) return N;
1755 
1756   // If there's a virtual register allocated and initialized for this
1757   // value, use it.
1758   if (SDValue copyFromReg = getCopyFromRegs(V, V->getType()))
1759     return copyFromReg;
1760 
1761   // Otherwise create a new SDValue and remember it.
1762   SDValue Val = getValueImpl(V);
1763   NodeMap[V] = Val;
1764   resolveDanglingDebugInfo(V, Val);
1765   return Val;
1766 }
1767 
1768 /// getNonRegisterValue - Return an SDValue for the given Value, but
1769 /// don't look in FuncInfo.ValueMap for a virtual register.
1770 SDValue SelectionDAGBuilder::getNonRegisterValue(const Value *V) {
1771   // If we already have an SDValue for this value, use it.
1772   SDValue &N = NodeMap[V];
1773   if (N.getNode()) {
1774     if (isIntOrFPConstant(N)) {
1775       // Remove the debug location from the node as the node is about to be used
1776       // in a location which may differ from the original debug location.  This
1777       // is relevant to Constant and ConstantFP nodes because they can appear
1778       // as constant expressions inside PHI nodes.
1779       N->setDebugLoc(DebugLoc());
1780     }
1781     return N;
1782   }
1783 
1784   // Otherwise create a new SDValue and remember it.
1785   SDValue Val = getValueImpl(V);
1786   NodeMap[V] = Val;
1787   resolveDanglingDebugInfo(V, Val);
1788   return Val;
1789 }
1790 
1791 /// getValueImpl - Helper function for getValue and getNonRegisterValue.
1792 /// Create an SDValue for the given value.
1793 SDValue SelectionDAGBuilder::getValueImpl(const Value *V) {
1794   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1795 
1796   if (const Constant *C = dyn_cast<Constant>(V)) {
1797     EVT VT = TLI.getValueType(DAG.getDataLayout(), V->getType(), true);
1798 
1799     if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
1800       return DAG.getConstant(*CI, getCurSDLoc(), VT);
1801 
1802     if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
1803       return DAG.getGlobalAddress(GV, getCurSDLoc(), VT);
1804 
1805     if (const ConstantPtrAuth *CPA = dyn_cast<ConstantPtrAuth>(C)) {
1806       return DAG.getNode(ISD::PtrAuthGlobalAddress, getCurSDLoc(), VT,
1807                          getValue(CPA->getPointer()), getValue(CPA->getKey()),
1808                          getValue(CPA->getAddrDiscriminator()),
1809                          getValue(CPA->getDiscriminator()));
1810     }
1811 
1812     if (isa<ConstantPointerNull>(C)) {
1813       unsigned AS = V->getType()->getPointerAddressSpace();
1814       return DAG.getConstant(0, getCurSDLoc(),
1815                              TLI.getPointerTy(DAG.getDataLayout(), AS));
1816     }
1817 
1818     if (match(C, m_VScale()))
1819       return DAG.getVScale(getCurSDLoc(), VT, APInt(VT.getSizeInBits(), 1));
1820 
1821     if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
1822       return DAG.getConstantFP(*CFP, getCurSDLoc(), VT);
1823 
1824     if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
1825       return DAG.getUNDEF(VT);
1826 
1827     if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
1828       visit(CE->getOpcode(), *CE);
1829       SDValue N1 = NodeMap[V];
1830       assert(N1.getNode() && "visit didn't populate the NodeMap!");
1831       return N1;
1832     }
1833 
1834     if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
1835       SmallVector<SDValue, 4> Constants;
1836       for (const Use &U : C->operands()) {
1837         SDNode *Val = getValue(U).getNode();
1838         // If the operand is an empty aggregate, there are no values.
1839         if (!Val) continue;
1840         // Add each leaf value from the operand to the Constants list
1841         // to form a flattened list of all the values.
1842         for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1843           Constants.push_back(SDValue(Val, i));
1844       }
1845 
1846       return DAG.getMergeValues(Constants, getCurSDLoc());
1847     }
1848 
1849     if (const ConstantDataSequential *CDS =
1850           dyn_cast<ConstantDataSequential>(C)) {
1851       SmallVector<SDValue, 4> Ops;
1852       for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
1853         SDNode *Val = getValue(CDS->getElementAsConstant(i)).getNode();
1854         // Add each leaf value from the operand to the Constants list
1855         // to form a flattened list of all the values.
1856         for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1857           Ops.push_back(SDValue(Val, i));
1858       }
1859 
1860       if (isa<ArrayType>(CDS->getType()))
1861         return DAG.getMergeValues(Ops, getCurSDLoc());
1862       return NodeMap[V] = DAG.getBuildVector(VT, getCurSDLoc(), Ops);
1863     }
1864 
1865     if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
1866       assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
1867              "Unknown struct or array constant!");
1868 
1869       SmallVector<EVT, 4> ValueVTs;
1870       ComputeValueVTs(TLI, DAG.getDataLayout(), C->getType(), ValueVTs);
1871       unsigned NumElts = ValueVTs.size();
1872       if (NumElts == 0)
1873         return SDValue(); // empty struct
1874       SmallVector<SDValue, 4> Constants(NumElts);
1875       for (unsigned i = 0; i != NumElts; ++i) {
1876         EVT EltVT = ValueVTs[i];
1877         if (isa<UndefValue>(C))
1878           Constants[i] = DAG.getUNDEF(EltVT);
1879         else if (EltVT.isFloatingPoint())
1880           Constants[i] = DAG.getConstantFP(0, getCurSDLoc(), EltVT);
1881         else
1882           Constants[i] = DAG.getConstant(0, getCurSDLoc(), EltVT);
1883       }
1884 
1885       return DAG.getMergeValues(Constants, getCurSDLoc());
1886     }
1887 
1888     if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
1889       return DAG.getBlockAddress(BA, VT);
1890 
1891     if (const auto *Equiv = dyn_cast<DSOLocalEquivalent>(C))
1892       return getValue(Equiv->getGlobalValue());
1893 
1894     if (const auto *NC = dyn_cast<NoCFIValue>(C))
1895       return getValue(NC->getGlobalValue());
1896 
1897     if (VT == MVT::aarch64svcount) {
1898       assert(C->isNullValue() && "Can only zero this target type!");
1899       return DAG.getNode(ISD::BITCAST, getCurSDLoc(), VT,
1900                          DAG.getConstant(0, getCurSDLoc(), MVT::nxv16i1));
1901     }
1902 
1903     VectorType *VecTy = cast<VectorType>(V->getType());
1904 
1905     // Now that we know the number and type of the elements, get that number of
1906     // elements into the Ops array based on what kind of constant it is.
1907     if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
1908       SmallVector<SDValue, 16> Ops;
1909       unsigned NumElements = cast<FixedVectorType>(VecTy)->getNumElements();
1910       for (unsigned i = 0; i != NumElements; ++i)
1911         Ops.push_back(getValue(CV->getOperand(i)));
1912 
1913       return NodeMap[V] = DAG.getBuildVector(VT, getCurSDLoc(), Ops);
1914     }
1915 
1916     if (isa<ConstantAggregateZero>(C)) {
1917       EVT EltVT =
1918           TLI.getValueType(DAG.getDataLayout(), VecTy->getElementType());
1919 
1920       SDValue Op;
1921       if (EltVT.isFloatingPoint())
1922         Op = DAG.getConstantFP(0, getCurSDLoc(), EltVT);
1923       else
1924         Op = DAG.getConstant(0, getCurSDLoc(), EltVT);
1925 
1926       return NodeMap[V] = DAG.getSplat(VT, getCurSDLoc(), Op);
1927     }
1928 
1929     llvm_unreachable("Unknown vector constant");
1930   }
1931 
1932   // If this is a static alloca, generate it as the frameindex instead of
1933   // computation.
1934   if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1935     DenseMap<const AllocaInst*, int>::iterator SI =
1936       FuncInfo.StaticAllocaMap.find(AI);
1937     if (SI != FuncInfo.StaticAllocaMap.end())
1938       return DAG.getFrameIndex(
1939           SI->second, TLI.getValueType(DAG.getDataLayout(), AI->getType()));
1940   }
1941 
1942   // If this is an instruction which fast-isel has deferred, select it now.
1943   if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
1944     Register InReg = FuncInfo.InitializeRegForValue(Inst);
1945 
1946     RegsForValue RFV(*DAG.getContext(), TLI, DAG.getDataLayout(), InReg,
1947                      Inst->getType(), std::nullopt);
1948     SDValue Chain = DAG.getEntryNode();
1949     return RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, V);
1950   }
1951 
1952   if (const MetadataAsValue *MD = dyn_cast<MetadataAsValue>(V))
1953     return DAG.getMDNode(cast<MDNode>(MD->getMetadata()));
1954 
1955   if (const auto *BB = dyn_cast<BasicBlock>(V))
1956     return DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
1957 
1958   llvm_unreachable("Can't get register for value!");
1959 }
1960 
1961 void SelectionDAGBuilder::visitCatchPad(const CatchPadInst &I) {
1962   auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
1963   bool IsMSVCCXX = Pers == EHPersonality::MSVC_CXX;
1964   bool IsCoreCLR = Pers == EHPersonality::CoreCLR;
1965   bool IsSEH = isAsynchronousEHPersonality(Pers);
1966   MachineBasicBlock *CatchPadMBB = FuncInfo.MBB;
1967   if (!IsSEH)
1968     CatchPadMBB->setIsEHScopeEntry();
1969   // In MSVC C++ and CoreCLR, catchblocks are funclets and need prologues.
1970   if (IsMSVCCXX || IsCoreCLR)
1971     CatchPadMBB->setIsEHFuncletEntry();
1972 }
1973 
1974 void SelectionDAGBuilder::visitCatchRet(const CatchReturnInst &I) {
1975   // Update machine-CFG edge.
1976   MachineBasicBlock *TargetMBB = FuncInfo.MBBMap[I.getSuccessor()];
1977   FuncInfo.MBB->addSuccessor(TargetMBB);
1978   TargetMBB->setIsEHCatchretTarget(true);
1979   DAG.getMachineFunction().setHasEHCatchret(true);
1980 
1981   auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
1982   bool IsSEH = isAsynchronousEHPersonality(Pers);
1983   if (IsSEH) {
1984     // If this is not a fall-through branch or optimizations are switched off,
1985     // emit the branch.
1986     if (TargetMBB != NextBlock(FuncInfo.MBB) ||
1987         TM.getOptLevel() == CodeGenOptLevel::None)
1988       DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
1989                               getControlRoot(), DAG.getBasicBlock(TargetMBB)));
1990     return;
1991   }
1992 
1993   // Figure out the funclet membership for the catchret's successor.
1994   // This will be used by the FuncletLayout pass to determine how to order the
1995   // BB's.
1996   // A 'catchret' returns to the outer scope's color.
1997   Value *ParentPad = I.getCatchSwitchParentPad();
1998   const BasicBlock *SuccessorColor;
1999   if (isa<ConstantTokenNone>(ParentPad))
2000     SuccessorColor = &FuncInfo.Fn->getEntryBlock();
2001   else
2002     SuccessorColor = cast<Instruction>(ParentPad)->getParent();
2003   assert(SuccessorColor && "No parent funclet for catchret!");
2004   MachineBasicBlock *SuccessorColorMBB = FuncInfo.MBBMap[SuccessorColor];
2005   assert(SuccessorColorMBB && "No MBB for SuccessorColor!");
2006 
2007   // Create the terminator node.
2008   SDValue Ret = DAG.getNode(ISD::CATCHRET, getCurSDLoc(), MVT::Other,
2009                             getControlRoot(), DAG.getBasicBlock(TargetMBB),
2010                             DAG.getBasicBlock(SuccessorColorMBB));
2011   DAG.setRoot(Ret);
2012 }
2013 
2014 void SelectionDAGBuilder::visitCleanupPad(const CleanupPadInst &CPI) {
2015   // Don't emit any special code for the cleanuppad instruction. It just marks
2016   // the start of an EH scope/funclet.
2017   FuncInfo.MBB->setIsEHScopeEntry();
2018   auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
2019   if (Pers != EHPersonality::Wasm_CXX) {
2020     FuncInfo.MBB->setIsEHFuncletEntry();
2021     FuncInfo.MBB->setIsCleanupFuncletEntry();
2022   }
2023 }
2024 
2025 // In wasm EH, even though a catchpad may not catch an exception if a tag does
2026 // not match, it is OK to add only the first unwind destination catchpad to the
2027 // successors, because there will be at least one invoke instruction within the
2028 // catch scope that points to the next unwind destination, if one exists, so
2029 // CFGSort cannot mess up with BB sorting order.
2030 // (All catchpads with 'catch (type)' clauses have a 'llvm.rethrow' intrinsic
2031 // call within them, and catchpads only consisting of 'catch (...)' have a
2032 // '__cxa_end_catch' call within them, both of which generate invokes in case
2033 // the next unwind destination exists, i.e., the next unwind destination is not
2034 // the caller.)
2035 //
2036 // Having at most one EH pad successor is also simpler and helps later
2037 // transformations.
2038 //
2039 // For example,
2040 // current:
2041 //   invoke void @foo to ... unwind label %catch.dispatch
2042 // catch.dispatch:
2043 //   %0 = catchswitch within ... [label %catch.start] unwind label %next
2044 // catch.start:
2045 //   ...
2046 //   ... in this BB or some other child BB dominated by this BB there will be an
2047 //   invoke that points to 'next' BB as an unwind destination
2048 //
2049 // next: ; We don't need to add this to 'current' BB's successor
2050 //   ...
2051 static void findWasmUnwindDestinations(
2052     FunctionLoweringInfo &FuncInfo, const BasicBlock *EHPadBB,
2053     BranchProbability Prob,
2054     SmallVectorImpl<std::pair<MachineBasicBlock *, BranchProbability>>
2055         &UnwindDests) {
2056   while (EHPadBB) {
2057     const Instruction *Pad = EHPadBB->getFirstNonPHI();
2058     if (isa<CleanupPadInst>(Pad)) {
2059       // Stop on cleanup pads.
2060       UnwindDests.emplace_back(FuncInfo.MBBMap[EHPadBB], Prob);
2061       UnwindDests.back().first->setIsEHScopeEntry();
2062       break;
2063     } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Pad)) {
2064       // Add the catchpad handlers to the possible destinations. We don't
2065       // continue to the unwind destination of the catchswitch for wasm.
2066       for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) {
2067         UnwindDests.emplace_back(FuncInfo.MBBMap[CatchPadBB], Prob);
2068         UnwindDests.back().first->setIsEHScopeEntry();
2069       }
2070       break;
2071     } else {
2072       continue;
2073     }
2074   }
2075 }
2076 
2077 /// When an invoke or a cleanupret unwinds to the next EH pad, there are
2078 /// many places it could ultimately go. In the IR, we have a single unwind
2079 /// destination, but in the machine CFG, we enumerate all the possible blocks.
2080 /// This function skips over imaginary basic blocks that hold catchswitch
2081 /// instructions, and finds all the "real" machine
2082 /// basic block destinations. As those destinations may not be successors of
2083 /// EHPadBB, here we also calculate the edge probability to those destinations.
2084 /// The passed-in Prob is the edge probability to EHPadBB.
2085 static void findUnwindDestinations(
2086     FunctionLoweringInfo &FuncInfo, const BasicBlock *EHPadBB,
2087     BranchProbability Prob,
2088     SmallVectorImpl<std::pair<MachineBasicBlock *, BranchProbability>>
2089         &UnwindDests) {
2090   EHPersonality Personality =
2091     classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
2092   bool IsMSVCCXX = Personality == EHPersonality::MSVC_CXX;
2093   bool IsCoreCLR = Personality == EHPersonality::CoreCLR;
2094   bool IsWasmCXX = Personality == EHPersonality::Wasm_CXX;
2095   bool IsSEH = isAsynchronousEHPersonality(Personality);
2096 
2097   if (IsWasmCXX) {
2098     findWasmUnwindDestinations(FuncInfo, EHPadBB, Prob, UnwindDests);
2099     assert(UnwindDests.size() <= 1 &&
2100            "There should be at most one unwind destination for wasm");
2101     return;
2102   }
2103 
2104   while (EHPadBB) {
2105     const Instruction *Pad = EHPadBB->getFirstNonPHI();
2106     BasicBlock *NewEHPadBB = nullptr;
2107     if (isa<LandingPadInst>(Pad)) {
2108       // Stop on landingpads. They are not funclets.
2109       UnwindDests.emplace_back(FuncInfo.MBBMap[EHPadBB], Prob);
2110       break;
2111     } else if (isa<CleanupPadInst>(Pad)) {
2112       // Stop on cleanup pads. Cleanups are always funclet entries for all known
2113       // personalities.
2114       UnwindDests.emplace_back(FuncInfo.MBBMap[EHPadBB], Prob);
2115       UnwindDests.back().first->setIsEHScopeEntry();
2116       UnwindDests.back().first->setIsEHFuncletEntry();
2117       break;
2118     } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Pad)) {
2119       // Add the catchpad handlers to the possible destinations.
2120       for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) {
2121         UnwindDests.emplace_back(FuncInfo.MBBMap[CatchPadBB], Prob);
2122         // For MSVC++ and the CLR, catchblocks are funclets and need prologues.
2123         if (IsMSVCCXX || IsCoreCLR)
2124           UnwindDests.back().first->setIsEHFuncletEntry();
2125         if (!IsSEH)
2126           UnwindDests.back().first->setIsEHScopeEntry();
2127       }
2128       NewEHPadBB = CatchSwitch->getUnwindDest();
2129     } else {
2130       continue;
2131     }
2132 
2133     BranchProbabilityInfo *BPI = FuncInfo.BPI;
2134     if (BPI && NewEHPadBB)
2135       Prob *= BPI->getEdgeProbability(EHPadBB, NewEHPadBB);
2136     EHPadBB = NewEHPadBB;
2137   }
2138 }
2139 
2140 void SelectionDAGBuilder::visitCleanupRet(const CleanupReturnInst &I) {
2141   // Update successor info.
2142   SmallVector<std::pair<MachineBasicBlock *, BranchProbability>, 1> UnwindDests;
2143   auto UnwindDest = I.getUnwindDest();
2144   BranchProbabilityInfo *BPI = FuncInfo.BPI;
2145   BranchProbability UnwindDestProb =
2146       (BPI && UnwindDest)
2147           ? BPI->getEdgeProbability(FuncInfo.MBB->getBasicBlock(), UnwindDest)
2148           : BranchProbability::getZero();
2149   findUnwindDestinations(FuncInfo, UnwindDest, UnwindDestProb, UnwindDests);
2150   for (auto &UnwindDest : UnwindDests) {
2151     UnwindDest.first->setIsEHPad();
2152     addSuccessorWithProb(FuncInfo.MBB, UnwindDest.first, UnwindDest.second);
2153   }
2154   FuncInfo.MBB->normalizeSuccProbs();
2155 
2156   // Create the terminator node.
2157   SDValue Ret =
2158       DAG.getNode(ISD::CLEANUPRET, getCurSDLoc(), MVT::Other, getControlRoot());
2159   DAG.setRoot(Ret);
2160 }
2161 
2162 void SelectionDAGBuilder::visitCatchSwitch(const CatchSwitchInst &CSI) {
2163   report_fatal_error("visitCatchSwitch not yet implemented!");
2164 }
2165 
2166 void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
2167   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2168   auto &DL = DAG.getDataLayout();
2169   SDValue Chain = getControlRoot();
2170   SmallVector<ISD::OutputArg, 8> Outs;
2171   SmallVector<SDValue, 8> OutVals;
2172 
2173   // Calls to @llvm.experimental.deoptimize don't generate a return value, so
2174   // lower
2175   //
2176   //   %val = call <ty> @llvm.experimental.deoptimize()
2177   //   ret <ty> %val
2178   //
2179   // differently.
2180   if (I.getParent()->getTerminatingDeoptimizeCall()) {
2181     LowerDeoptimizingReturn();
2182     return;
2183   }
2184 
2185   if (!FuncInfo.CanLowerReturn) {
2186     unsigned DemoteReg = FuncInfo.DemoteRegister;
2187     const Function *F = I.getParent()->getParent();
2188 
2189     // Emit a store of the return value through the virtual register.
2190     // Leave Outs empty so that LowerReturn won't try to load return
2191     // registers the usual way.
2192     SmallVector<EVT, 1> PtrValueVTs;
2193     ComputeValueVTs(TLI, DL,
2194                     PointerType::get(F->getContext(),
2195                                      DAG.getDataLayout().getAllocaAddrSpace()),
2196                     PtrValueVTs);
2197 
2198     SDValue RetPtr =
2199         DAG.getCopyFromReg(Chain, getCurSDLoc(), DemoteReg, PtrValueVTs[0]);
2200     SDValue RetOp = getValue(I.getOperand(0));
2201 
2202     SmallVector<EVT, 4> ValueVTs, MemVTs;
2203     SmallVector<uint64_t, 4> Offsets;
2204     ComputeValueVTs(TLI, DL, I.getOperand(0)->getType(), ValueVTs, &MemVTs,
2205                     &Offsets, 0);
2206     unsigned NumValues = ValueVTs.size();
2207 
2208     SmallVector<SDValue, 4> Chains(NumValues);
2209     Align BaseAlign = DL.getPrefTypeAlign(I.getOperand(0)->getType());
2210     for (unsigned i = 0; i != NumValues; ++i) {
2211       // An aggregate return value cannot wrap around the address space, so
2212       // offsets to its parts don't wrap either.
2213       SDValue Ptr = DAG.getObjectPtrOffset(getCurSDLoc(), RetPtr,
2214                                            TypeSize::getFixed(Offsets[i]));
2215 
2216       SDValue Val = RetOp.getValue(RetOp.getResNo() + i);
2217       if (MemVTs[i] != ValueVTs[i])
2218         Val = DAG.getPtrExtOrTrunc(Val, getCurSDLoc(), MemVTs[i]);
2219       Chains[i] = DAG.getStore(
2220           Chain, getCurSDLoc(), Val,
2221           // FIXME: better loc info would be nice.
2222           Ptr, MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()),
2223           commonAlignment(BaseAlign, Offsets[i]));
2224     }
2225 
2226     Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
2227                         MVT::Other, Chains);
2228   } else if (I.getNumOperands() != 0) {
2229     SmallVector<EVT, 4> ValueVTs;
2230     ComputeValueVTs(TLI, DL, I.getOperand(0)->getType(), ValueVTs);
2231     unsigned NumValues = ValueVTs.size();
2232     if (NumValues) {
2233       SDValue RetOp = getValue(I.getOperand(0));
2234 
2235       const Function *F = I.getParent()->getParent();
2236 
2237       bool NeedsRegBlock = TLI.functionArgumentNeedsConsecutiveRegisters(
2238           I.getOperand(0)->getType(), F->getCallingConv(),
2239           /*IsVarArg*/ false, DL);
2240 
2241       ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
2242       if (F->getAttributes().hasRetAttr(Attribute::SExt))
2243         ExtendKind = ISD::SIGN_EXTEND;
2244       else if (F->getAttributes().hasRetAttr(Attribute::ZExt))
2245         ExtendKind = ISD::ZERO_EXTEND;
2246 
2247       LLVMContext &Context = F->getContext();
2248       bool RetInReg = F->getAttributes().hasRetAttr(Attribute::InReg);
2249 
2250       for (unsigned j = 0; j != NumValues; ++j) {
2251         EVT VT = ValueVTs[j];
2252 
2253         if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger())
2254           VT = TLI.getTypeForExtReturn(Context, VT, ExtendKind);
2255 
2256         CallingConv::ID CC = F->getCallingConv();
2257 
2258         unsigned NumParts = TLI.getNumRegistersForCallingConv(Context, CC, VT);
2259         MVT PartVT = TLI.getRegisterTypeForCallingConv(Context, CC, VT);
2260         SmallVector<SDValue, 4> Parts(NumParts);
2261         getCopyToParts(DAG, getCurSDLoc(),
2262                        SDValue(RetOp.getNode(), RetOp.getResNo() + j),
2263                        &Parts[0], NumParts, PartVT, &I, CC, ExtendKind);
2264 
2265         // 'inreg' on function refers to return value
2266         ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
2267         if (RetInReg)
2268           Flags.setInReg();
2269 
2270         if (I.getOperand(0)->getType()->isPointerTy()) {
2271           Flags.setPointer();
2272           Flags.setPointerAddrSpace(
2273               cast<PointerType>(I.getOperand(0)->getType())->getAddressSpace());
2274         }
2275 
2276         if (NeedsRegBlock) {
2277           Flags.setInConsecutiveRegs();
2278           if (j == NumValues - 1)
2279             Flags.setInConsecutiveRegsLast();
2280         }
2281 
2282         // Propagate extension type if any
2283         if (ExtendKind == ISD::SIGN_EXTEND)
2284           Flags.setSExt();
2285         else if (ExtendKind == ISD::ZERO_EXTEND)
2286           Flags.setZExt();
2287 
2288         for (unsigned i = 0; i < NumParts; ++i) {
2289           Outs.push_back(ISD::OutputArg(Flags,
2290                                         Parts[i].getValueType().getSimpleVT(),
2291                                         VT, /*isfixed=*/true, 0, 0));
2292           OutVals.push_back(Parts[i]);
2293         }
2294       }
2295     }
2296   }
2297 
2298   // Push in swifterror virtual register as the last element of Outs. This makes
2299   // sure swifterror virtual register will be returned in the swifterror
2300   // physical register.
2301   const Function *F = I.getParent()->getParent();
2302   if (TLI.supportSwiftError() &&
2303       F->getAttributes().hasAttrSomewhere(Attribute::SwiftError)) {
2304     assert(SwiftError.getFunctionArg() && "Need a swift error argument");
2305     ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
2306     Flags.setSwiftError();
2307     Outs.push_back(ISD::OutputArg(
2308         Flags, /*vt=*/TLI.getPointerTy(DL), /*argvt=*/EVT(TLI.getPointerTy(DL)),
2309         /*isfixed=*/true, /*origidx=*/1, /*partOffs=*/0));
2310     // Create SDNode for the swifterror virtual register.
2311     OutVals.push_back(
2312         DAG.getRegister(SwiftError.getOrCreateVRegUseAt(
2313                             &I, FuncInfo.MBB, SwiftError.getFunctionArg()),
2314                         EVT(TLI.getPointerTy(DL))));
2315   }
2316 
2317   bool isVarArg = DAG.getMachineFunction().getFunction().isVarArg();
2318   CallingConv::ID CallConv =
2319     DAG.getMachineFunction().getFunction().getCallingConv();
2320   Chain = DAG.getTargetLoweringInfo().LowerReturn(
2321       Chain, CallConv, isVarArg, Outs, OutVals, getCurSDLoc(), DAG);
2322 
2323   // Verify that the target's LowerReturn behaved as expected.
2324   assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
2325          "LowerReturn didn't return a valid chain!");
2326 
2327   // Update the DAG with the new chain value resulting from return lowering.
2328   DAG.setRoot(Chain);
2329 }
2330 
2331 /// CopyToExportRegsIfNeeded - If the given value has virtual registers
2332 /// created for it, emit nodes to copy the value into the virtual
2333 /// registers.
2334 void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) {
2335   // Skip empty types
2336   if (V->getType()->isEmptyTy())
2337     return;
2338 
2339   DenseMap<const Value *, Register>::iterator VMI = FuncInfo.ValueMap.find(V);
2340   if (VMI != FuncInfo.ValueMap.end()) {
2341     assert((!V->use_empty() || isa<CallBrInst>(V)) &&
2342            "Unused value assigned virtual registers!");
2343     CopyValueToVirtualRegister(V, VMI->second);
2344   }
2345 }
2346 
2347 /// ExportFromCurrentBlock - If this condition isn't known to be exported from
2348 /// the current basic block, add it to ValueMap now so that we'll get a
2349 /// CopyTo/FromReg.
2350 void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) {
2351   // No need to export constants.
2352   if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
2353 
2354   // Already exported?
2355   if (FuncInfo.isExportedInst(V)) return;
2356 
2357   Register Reg = FuncInfo.InitializeRegForValue(V);
2358   CopyValueToVirtualRegister(V, Reg);
2359 }
2360 
2361 bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V,
2362                                                      const BasicBlock *FromBB) {
2363   // The operands of the setcc have to be in this block.  We don't know
2364   // how to export them from some other block.
2365   if (const Instruction *VI = dyn_cast<Instruction>(V)) {
2366     // Can export from current BB.
2367     if (VI->getParent() == FromBB)
2368       return true;
2369 
2370     // Is already exported, noop.
2371     return FuncInfo.isExportedInst(V);
2372   }
2373 
2374   // If this is an argument, we can export it if the BB is the entry block or
2375   // if it is already exported.
2376   if (isa<Argument>(V)) {
2377     if (FromBB->isEntryBlock())
2378       return true;
2379 
2380     // Otherwise, can only export this if it is already exported.
2381     return FuncInfo.isExportedInst(V);
2382   }
2383 
2384   // Otherwise, constants can always be exported.
2385   return true;
2386 }
2387 
2388 /// Return branch probability calculated by BranchProbabilityInfo for IR blocks.
2389 BranchProbability
2390 SelectionDAGBuilder::getEdgeProbability(const MachineBasicBlock *Src,
2391                                         const MachineBasicBlock *Dst) const {
2392   BranchProbabilityInfo *BPI = FuncInfo.BPI;
2393   const BasicBlock *SrcBB = Src->getBasicBlock();
2394   const BasicBlock *DstBB = Dst->getBasicBlock();
2395   if (!BPI) {
2396     // If BPI is not available, set the default probability as 1 / N, where N is
2397     // the number of successors.
2398     auto SuccSize = std::max<uint32_t>(succ_size(SrcBB), 1);
2399     return BranchProbability(1, SuccSize);
2400   }
2401   return BPI->getEdgeProbability(SrcBB, DstBB);
2402 }
2403 
2404 void SelectionDAGBuilder::addSuccessorWithProb(MachineBasicBlock *Src,
2405                                                MachineBasicBlock *Dst,
2406                                                BranchProbability Prob) {
2407   if (!FuncInfo.BPI)
2408     Src->addSuccessorWithoutProb(Dst);
2409   else {
2410     if (Prob.isUnknown())
2411       Prob = getEdgeProbability(Src, Dst);
2412     Src->addSuccessor(Dst, Prob);
2413   }
2414 }
2415 
2416 static bool InBlock(const Value *V, const BasicBlock *BB) {
2417   if (const Instruction *I = dyn_cast<Instruction>(V))
2418     return I->getParent() == BB;
2419   return true;
2420 }
2421 
2422 /// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
2423 /// This function emits a branch and is used at the leaves of an OR or an
2424 /// AND operator tree.
2425 void
2426 SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond,
2427                                                   MachineBasicBlock *TBB,
2428                                                   MachineBasicBlock *FBB,
2429                                                   MachineBasicBlock *CurBB,
2430                                                   MachineBasicBlock *SwitchBB,
2431                                                   BranchProbability TProb,
2432                                                   BranchProbability FProb,
2433                                                   bool InvertCond) {
2434   const BasicBlock *BB = CurBB->getBasicBlock();
2435 
2436   // If the leaf of the tree is a comparison, merge the condition into
2437   // the caseblock.
2438   if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
2439     // The operands of the cmp have to be in this block.  We don't know
2440     // how to export them from some other block.  If this is the first block
2441     // of the sequence, no exporting is needed.
2442     if (CurBB == SwitchBB ||
2443         (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
2444          isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
2445       ISD::CondCode Condition;
2446       if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
2447         ICmpInst::Predicate Pred =
2448             InvertCond ? IC->getInversePredicate() : IC->getPredicate();
2449         Condition = getICmpCondCode(Pred);
2450       } else {
2451         const FCmpInst *FC = cast<FCmpInst>(Cond);
2452         FCmpInst::Predicate Pred =
2453             InvertCond ? FC->getInversePredicate() : FC->getPredicate();
2454         Condition = getFCmpCondCode(Pred);
2455         if (TM.Options.NoNaNsFPMath)
2456           Condition = getFCmpCodeWithoutNaN(Condition);
2457       }
2458 
2459       CaseBlock CB(Condition, BOp->getOperand(0), BOp->getOperand(1), nullptr,
2460                    TBB, FBB, CurBB, getCurSDLoc(), TProb, FProb);
2461       SL->SwitchCases.push_back(CB);
2462       return;
2463     }
2464   }
2465 
2466   // Create a CaseBlock record representing this branch.
2467   ISD::CondCode Opc = InvertCond ? ISD::SETNE : ISD::SETEQ;
2468   CaseBlock CB(Opc, Cond, ConstantInt::getTrue(*DAG.getContext()),
2469                nullptr, TBB, FBB, CurBB, getCurSDLoc(), TProb, FProb);
2470   SL->SwitchCases.push_back(CB);
2471 }
2472 
2473 // Collect dependencies on V recursively. This is used for the cost analysis in
2474 // `shouldKeepJumpConditionsTogether`.
2475 static bool collectInstructionDeps(
2476     SmallMapVector<const Instruction *, bool, 8> *Deps, const Value *V,
2477     SmallMapVector<const Instruction *, bool, 8> *Necessary = nullptr,
2478     unsigned Depth = 0) {
2479   // Return false if we have an incomplete count.
2480   if (Depth >= SelectionDAG::MaxRecursionDepth)
2481     return false;
2482 
2483   auto *I = dyn_cast<Instruction>(V);
2484   if (I == nullptr)
2485     return true;
2486 
2487   if (Necessary != nullptr) {
2488     // This instruction is necessary for the other side of the condition so
2489     // don't count it.
2490     if (Necessary->contains(I))
2491       return true;
2492   }
2493 
2494   // Already added this dep.
2495   if (!Deps->try_emplace(I, false).second)
2496     return true;
2497 
2498   for (unsigned OpIdx = 0, E = I->getNumOperands(); OpIdx < E; ++OpIdx)
2499     if (!collectInstructionDeps(Deps, I->getOperand(OpIdx), Necessary,
2500                                 Depth + 1))
2501       return false;
2502   return true;
2503 }
2504 
2505 bool SelectionDAGBuilder::shouldKeepJumpConditionsTogether(
2506     const FunctionLoweringInfo &FuncInfo, const BranchInst &I,
2507     Instruction::BinaryOps Opc, const Value *Lhs, const Value *Rhs,
2508     TargetLoweringBase::CondMergingParams Params) const {
2509   if (I.getNumSuccessors() != 2)
2510     return false;
2511 
2512   if (!I.isConditional())
2513     return false;
2514 
2515   if (Params.BaseCost < 0)
2516     return false;
2517 
2518   // Baseline cost.
2519   InstructionCost CostThresh = Params.BaseCost;
2520 
2521   BranchProbabilityInfo *BPI = nullptr;
2522   if (Params.LikelyBias || Params.UnlikelyBias)
2523     BPI = FuncInfo.BPI;
2524   if (BPI != nullptr) {
2525     // See if we are either likely to get an early out or compute both lhs/rhs
2526     // of the condition.
2527     BasicBlock *IfFalse = I.getSuccessor(0);
2528     BasicBlock *IfTrue = I.getSuccessor(1);
2529 
2530     std::optional<bool> Likely;
2531     if (BPI->isEdgeHot(I.getParent(), IfTrue))
2532       Likely = true;
2533     else if (BPI->isEdgeHot(I.getParent(), IfFalse))
2534       Likely = false;
2535 
2536     if (Likely) {
2537       if (Opc == (*Likely ? Instruction::And : Instruction::Or))
2538         // Its likely we will have to compute both lhs and rhs of condition
2539         CostThresh += Params.LikelyBias;
2540       else {
2541         if (Params.UnlikelyBias < 0)
2542           return false;
2543         // Its likely we will get an early out.
2544         CostThresh -= Params.UnlikelyBias;
2545       }
2546     }
2547   }
2548 
2549   if (CostThresh <= 0)
2550     return false;
2551 
2552   // Collect "all" instructions that lhs condition is dependent on.
2553   // Use map for stable iteration (to avoid non-determanism of iteration of
2554   // SmallPtrSet). The `bool` value is just a dummy.
2555   SmallMapVector<const Instruction *, bool, 8> LhsDeps, RhsDeps;
2556   collectInstructionDeps(&LhsDeps, Lhs);
2557   // Collect "all" instructions that rhs condition is dependent on AND are
2558   // dependencies of lhs. This gives us an estimate on which instructions we
2559   // stand to save by splitting the condition.
2560   if (!collectInstructionDeps(&RhsDeps, Rhs, &LhsDeps))
2561     return false;
2562   // Add the compare instruction itself unless its a dependency on the LHS.
2563   if (const auto *RhsI = dyn_cast<Instruction>(Rhs))
2564     if (!LhsDeps.contains(RhsI))
2565       RhsDeps.try_emplace(RhsI, false);
2566 
2567   const auto &TLI = DAG.getTargetLoweringInfo();
2568   const auto &TTI =
2569       TLI.getTargetMachine().getTargetTransformInfo(*I.getFunction());
2570 
2571   InstructionCost CostOfIncluding = 0;
2572   // See if this instruction will need to computed independently of whether RHS
2573   // is.
2574   Value *BrCond = I.getCondition();
2575   auto ShouldCountInsn = [&RhsDeps, &BrCond](const Instruction *Ins) {
2576     for (const auto *U : Ins->users()) {
2577       // If user is independent of RHS calculation we don't need to count it.
2578       if (auto *UIns = dyn_cast<Instruction>(U))
2579         if (UIns != BrCond && !RhsDeps.contains(UIns))
2580           return false;
2581     }
2582     return true;
2583   };
2584 
2585   // Prune instructions from RHS Deps that are dependencies of unrelated
2586   // instructions. The value (SelectionDAG::MaxRecursionDepth) is fairly
2587   // arbitrary and just meant to cap the how much time we spend in the pruning
2588   // loop. Its highly unlikely to come into affect.
2589   const unsigned MaxPruneIters = SelectionDAG::MaxRecursionDepth;
2590   // Stop after a certain point. No incorrectness from including too many
2591   // instructions.
2592   for (unsigned PruneIters = 0; PruneIters < MaxPruneIters; ++PruneIters) {
2593     const Instruction *ToDrop = nullptr;
2594     for (const auto &InsPair : RhsDeps) {
2595       if (!ShouldCountInsn(InsPair.first)) {
2596         ToDrop = InsPair.first;
2597         break;
2598       }
2599     }
2600     if (ToDrop == nullptr)
2601       break;
2602     RhsDeps.erase(ToDrop);
2603   }
2604 
2605   for (const auto &InsPair : RhsDeps) {
2606     // Finally accumulate latency that we can only attribute to computing the
2607     // RHS condition. Use latency because we are essentially trying to calculate
2608     // the cost of the dependency chain.
2609     // Possible TODO: We could try to estimate ILP and make this more precise.
2610     CostOfIncluding +=
2611         TTI.getInstructionCost(InsPair.first, TargetTransformInfo::TCK_Latency);
2612 
2613     if (CostOfIncluding > CostThresh)
2614       return false;
2615   }
2616   return true;
2617 }
2618 
2619 void SelectionDAGBuilder::FindMergedConditions(const Value *Cond,
2620                                                MachineBasicBlock *TBB,
2621                                                MachineBasicBlock *FBB,
2622                                                MachineBasicBlock *CurBB,
2623                                                MachineBasicBlock *SwitchBB,
2624                                                Instruction::BinaryOps Opc,
2625                                                BranchProbability TProb,
2626                                                BranchProbability FProb,
2627                                                bool InvertCond) {
2628   // Skip over not part of the tree and remember to invert op and operands at
2629   // next level.
2630   Value *NotCond;
2631   if (match(Cond, m_OneUse(m_Not(m_Value(NotCond)))) &&
2632       InBlock(NotCond, CurBB->getBasicBlock())) {
2633     FindMergedConditions(NotCond, TBB, FBB, CurBB, SwitchBB, Opc, TProb, FProb,
2634                          !InvertCond);
2635     return;
2636   }
2637 
2638   const Instruction *BOp = dyn_cast<Instruction>(Cond);
2639   const Value *BOpOp0, *BOpOp1;
2640   // Compute the effective opcode for Cond, taking into account whether it needs
2641   // to be inverted, e.g.
2642   //   and (not (or A, B)), C
2643   // gets lowered as
2644   //   and (and (not A, not B), C)
2645   Instruction::BinaryOps BOpc = (Instruction::BinaryOps)0;
2646   if (BOp) {
2647     BOpc = match(BOp, m_LogicalAnd(m_Value(BOpOp0), m_Value(BOpOp1)))
2648                ? Instruction::And
2649                : (match(BOp, m_LogicalOr(m_Value(BOpOp0), m_Value(BOpOp1)))
2650                       ? Instruction::Or
2651                       : (Instruction::BinaryOps)0);
2652     if (InvertCond) {
2653       if (BOpc == Instruction::And)
2654         BOpc = Instruction::Or;
2655       else if (BOpc == Instruction::Or)
2656         BOpc = Instruction::And;
2657     }
2658   }
2659 
2660   // If this node is not part of the or/and tree, emit it as a branch.
2661   // Note that all nodes in the tree should have same opcode.
2662   bool BOpIsInOrAndTree = BOpc && BOpc == Opc && BOp->hasOneUse();
2663   if (!BOpIsInOrAndTree || BOp->getParent() != CurBB->getBasicBlock() ||
2664       !InBlock(BOpOp0, CurBB->getBasicBlock()) ||
2665       !InBlock(BOpOp1, CurBB->getBasicBlock())) {
2666     EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB,
2667                                  TProb, FProb, InvertCond);
2668     return;
2669   }
2670 
2671   //  Create TmpBB after CurBB.
2672   MachineFunction::iterator BBI(CurBB);
2673   MachineFunction &MF = DAG.getMachineFunction();
2674   MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
2675   CurBB->getParent()->insert(++BBI, TmpBB);
2676 
2677   if (Opc == Instruction::Or) {
2678     // Codegen X | Y as:
2679     // BB1:
2680     //   jmp_if_X TBB
2681     //   jmp TmpBB
2682     // TmpBB:
2683     //   jmp_if_Y TBB
2684     //   jmp FBB
2685     //
2686 
2687     // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
2688     // The requirement is that
2689     //   TrueProb for BB1 + (FalseProb for BB1 * TrueProb for TmpBB)
2690     //     = TrueProb for original BB.
2691     // Assuming the original probabilities are A and B, one choice is to set
2692     // BB1's probabilities to A/2 and A/2+B, and set TmpBB's probabilities to
2693     // A/(1+B) and 2B/(1+B). This choice assumes that
2694     //   TrueProb for BB1 == FalseProb for BB1 * TrueProb for TmpBB.
2695     // Another choice is to assume TrueProb for BB1 equals to TrueProb for
2696     // TmpBB, but the math is more complicated.
2697 
2698     auto NewTrueProb = TProb / 2;
2699     auto NewFalseProb = TProb / 2 + FProb;
2700     // Emit the LHS condition.
2701     FindMergedConditions(BOpOp0, TBB, TmpBB, CurBB, SwitchBB, Opc, NewTrueProb,
2702                          NewFalseProb, InvertCond);
2703 
2704     // Normalize A/2 and B to get A/(1+B) and 2B/(1+B).
2705     SmallVector<BranchProbability, 2> Probs{TProb / 2, FProb};
2706     BranchProbability::normalizeProbabilities(Probs.begin(), Probs.end());
2707     // Emit the RHS condition into TmpBB.
2708     FindMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
2709                          Probs[1], InvertCond);
2710   } else {
2711     assert(Opc == Instruction::And && "Unknown merge op!");
2712     // Codegen X & Y as:
2713     // BB1:
2714     //   jmp_if_X TmpBB
2715     //   jmp FBB
2716     // TmpBB:
2717     //   jmp_if_Y TBB
2718     //   jmp FBB
2719     //
2720     //  This requires creation of TmpBB after CurBB.
2721 
2722     // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
2723     // The requirement is that
2724     //   FalseProb for BB1 + (TrueProb for BB1 * FalseProb for TmpBB)
2725     //     = FalseProb for original BB.
2726     // Assuming the original probabilities are A and B, one choice is to set
2727     // BB1's probabilities to A+B/2 and B/2, and set TmpBB's probabilities to
2728     // 2A/(1+A) and B/(1+A). This choice assumes that FalseProb for BB1 ==
2729     // TrueProb for BB1 * FalseProb for TmpBB.
2730 
2731     auto NewTrueProb = TProb + FProb / 2;
2732     auto NewFalseProb = FProb / 2;
2733     // Emit the LHS condition.
2734     FindMergedConditions(BOpOp0, TmpBB, FBB, CurBB, SwitchBB, Opc, NewTrueProb,
2735                          NewFalseProb, InvertCond);
2736 
2737     // Normalize A and B/2 to get 2A/(1+A) and B/(1+A).
2738     SmallVector<BranchProbability, 2> Probs{TProb, FProb / 2};
2739     BranchProbability::normalizeProbabilities(Probs.begin(), Probs.end());
2740     // Emit the RHS condition into TmpBB.
2741     FindMergedConditions(BOpOp1, TBB, FBB, TmpBB, SwitchBB, Opc, Probs[0],
2742                          Probs[1], InvertCond);
2743   }
2744 }
2745 
2746 /// If the set of cases should be emitted as a series of branches, return true.
2747 /// If we should emit this as a bunch of and/or'd together conditions, return
2748 /// false.
2749 bool
2750 SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases) {
2751   if (Cases.size() != 2) return true;
2752 
2753   // If this is two comparisons of the same values or'd or and'd together, they
2754   // will get folded into a single comparison, so don't emit two blocks.
2755   if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
2756        Cases[0].CmpRHS == Cases[1].CmpRHS) ||
2757       (Cases[0].CmpRHS == Cases[1].CmpLHS &&
2758        Cases[0].CmpLHS == Cases[1].CmpRHS)) {
2759     return false;
2760   }
2761 
2762   // Handle: (X != null) | (Y != null) --> (X|Y) != 0
2763   // Handle: (X == null) & (Y == null) --> (X|Y) == 0
2764   if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
2765       Cases[0].CC == Cases[1].CC &&
2766       isa<Constant>(Cases[0].CmpRHS) &&
2767       cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
2768     if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
2769       return false;
2770     if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
2771       return false;
2772   }
2773 
2774   return true;
2775 }
2776 
2777 void SelectionDAGBuilder::visitBr(const BranchInst &I) {
2778   MachineBasicBlock *BrMBB = FuncInfo.MBB;
2779 
2780   // Update machine-CFG edges.
2781   MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
2782 
2783   if (I.isUnconditional()) {
2784     // Update machine-CFG edges.
2785     BrMBB->addSuccessor(Succ0MBB);
2786 
2787     // If this is not a fall-through branch or optimizations are switched off,
2788     // emit the branch.
2789     if (Succ0MBB != NextBlock(BrMBB) ||
2790         TM.getOptLevel() == CodeGenOptLevel::None) {
2791       auto Br = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
2792                             getControlRoot(), DAG.getBasicBlock(Succ0MBB));
2793       setValue(&I, Br);
2794       DAG.setRoot(Br);
2795     }
2796 
2797     return;
2798   }
2799 
2800   // If this condition is one of the special cases we handle, do special stuff
2801   // now.
2802   const Value *CondVal = I.getCondition();
2803   MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
2804 
2805   // If this is a series of conditions that are or'd or and'd together, emit
2806   // this as a sequence of branches instead of setcc's with and/or operations.
2807   // As long as jumps are not expensive (exceptions for multi-use logic ops,
2808   // unpredictable branches, and vector extracts because those jumps are likely
2809   // expensive for any target), this should improve performance.
2810   // For example, instead of something like:
2811   //     cmp A, B
2812   //     C = seteq
2813   //     cmp D, E
2814   //     F = setle
2815   //     or C, F
2816   //     jnz foo
2817   // Emit:
2818   //     cmp A, B
2819   //     je foo
2820   //     cmp D, E
2821   //     jle foo
2822   const Instruction *BOp = dyn_cast<Instruction>(CondVal);
2823   if (!DAG.getTargetLoweringInfo().isJumpExpensive() && BOp &&
2824       BOp->hasOneUse() && !I.hasMetadata(LLVMContext::MD_unpredictable)) {
2825     Value *Vec;
2826     const Value *BOp0, *BOp1;
2827     Instruction::BinaryOps Opcode = (Instruction::BinaryOps)0;
2828     if (match(BOp, m_LogicalAnd(m_Value(BOp0), m_Value(BOp1))))
2829       Opcode = Instruction::And;
2830     else if (match(BOp, m_LogicalOr(m_Value(BOp0), m_Value(BOp1))))
2831       Opcode = Instruction::Or;
2832 
2833     if (Opcode &&
2834         !(match(BOp0, m_ExtractElt(m_Value(Vec), m_Value())) &&
2835           match(BOp1, m_ExtractElt(m_Specific(Vec), m_Value()))) &&
2836         !shouldKeepJumpConditionsTogether(
2837             FuncInfo, I, Opcode, BOp0, BOp1,
2838             DAG.getTargetLoweringInfo().getJumpConditionMergingParams(
2839                 Opcode, BOp0, BOp1))) {
2840       FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB, Opcode,
2841                            getEdgeProbability(BrMBB, Succ0MBB),
2842                            getEdgeProbability(BrMBB, Succ1MBB),
2843                            /*InvertCond=*/false);
2844       // If the compares in later blocks need to use values not currently
2845       // exported from this block, export them now.  This block should always
2846       // be the first entry.
2847       assert(SL->SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
2848 
2849       // Allow some cases to be rejected.
2850       if (ShouldEmitAsBranches(SL->SwitchCases)) {
2851         for (unsigned i = 1, e = SL->SwitchCases.size(); i != e; ++i) {
2852           ExportFromCurrentBlock(SL->SwitchCases[i].CmpLHS);
2853           ExportFromCurrentBlock(SL->SwitchCases[i].CmpRHS);
2854         }
2855 
2856         // Emit the branch for this block.
2857         visitSwitchCase(SL->SwitchCases[0], BrMBB);
2858         SL->SwitchCases.erase(SL->SwitchCases.begin());
2859         return;
2860       }
2861 
2862       // Okay, we decided not to do this, remove any inserted MBB's and clear
2863       // SwitchCases.
2864       for (unsigned i = 1, e = SL->SwitchCases.size(); i != e; ++i)
2865         FuncInfo.MF->erase(SL->SwitchCases[i].ThisBB);
2866 
2867       SL->SwitchCases.clear();
2868     }
2869   }
2870 
2871   // Create a CaseBlock record representing this branch.
2872   CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
2873                nullptr, Succ0MBB, Succ1MBB, BrMBB, getCurSDLoc());
2874 
2875   // Use visitSwitchCase to actually insert the fast branch sequence for this
2876   // cond branch.
2877   visitSwitchCase(CB, BrMBB);
2878 }
2879 
2880 /// visitSwitchCase - Emits the necessary code to represent a single node in
2881 /// the binary search tree resulting from lowering a switch instruction.
2882 void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB,
2883                                           MachineBasicBlock *SwitchBB) {
2884   SDValue Cond;
2885   SDValue CondLHS = getValue(CB.CmpLHS);
2886   SDLoc dl = CB.DL;
2887 
2888   if (CB.CC == ISD::SETTRUE) {
2889     // Branch or fall through to TrueBB.
2890     addSuccessorWithProb(SwitchBB, CB.TrueBB, CB.TrueProb);
2891     SwitchBB->normalizeSuccProbs();
2892     if (CB.TrueBB != NextBlock(SwitchBB)) {
2893       DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, getControlRoot(),
2894                               DAG.getBasicBlock(CB.TrueBB)));
2895     }
2896     return;
2897   }
2898 
2899   auto &TLI = DAG.getTargetLoweringInfo();
2900   EVT MemVT = TLI.getMemValueType(DAG.getDataLayout(), CB.CmpLHS->getType());
2901 
2902   // Build the setcc now.
2903   if (!CB.CmpMHS) {
2904     // Fold "(X == true)" to X and "(X == false)" to !X to
2905     // handle common cases produced by branch lowering.
2906     if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
2907         CB.CC == ISD::SETEQ)
2908       Cond = CondLHS;
2909     else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
2910              CB.CC == ISD::SETEQ) {
2911       SDValue True = DAG.getConstant(1, dl, CondLHS.getValueType());
2912       Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
2913     } else {
2914       SDValue CondRHS = getValue(CB.CmpRHS);
2915 
2916       // If a pointer's DAG type is larger than its memory type then the DAG
2917       // values are zero-extended. This breaks signed comparisons so truncate
2918       // back to the underlying type before doing the compare.
2919       if (CondLHS.getValueType() != MemVT) {
2920         CondLHS = DAG.getPtrExtOrTrunc(CondLHS, getCurSDLoc(), MemVT);
2921         CondRHS = DAG.getPtrExtOrTrunc(CondRHS, getCurSDLoc(), MemVT);
2922       }
2923       Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, CondRHS, CB.CC);
2924     }
2925   } else {
2926     assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
2927 
2928     const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
2929     const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
2930 
2931     SDValue CmpOp = getValue(CB.CmpMHS);
2932     EVT VT = CmpOp.getValueType();
2933 
2934     if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
2935       Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, dl, VT),
2936                           ISD::SETLE);
2937     } else {
2938       SDValue SUB = DAG.getNode(ISD::SUB, dl,
2939                                 VT, CmpOp, DAG.getConstant(Low, dl, VT));
2940       Cond = DAG.getSetCC(dl, MVT::i1, SUB,
2941                           DAG.getConstant(High-Low, dl, VT), ISD::SETULE);
2942     }
2943   }
2944 
2945   // Update successor info
2946   addSuccessorWithProb(SwitchBB, CB.TrueBB, CB.TrueProb);
2947   // TrueBB and FalseBB are always different unless the incoming IR is
2948   // degenerate. This only happens when running llc on weird IR.
2949   if (CB.TrueBB != CB.FalseBB)
2950     addSuccessorWithProb(SwitchBB, CB.FalseBB, CB.FalseProb);
2951   SwitchBB->normalizeSuccProbs();
2952 
2953   // If the lhs block is the next block, invert the condition so that we can
2954   // fall through to the lhs instead of the rhs block.
2955   if (CB.TrueBB == NextBlock(SwitchBB)) {
2956     std::swap(CB.TrueBB, CB.FalseBB);
2957     SDValue True = DAG.getConstant(1, dl, Cond.getValueType());
2958     Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
2959   }
2960 
2961   SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
2962                                MVT::Other, getControlRoot(), Cond,
2963                                DAG.getBasicBlock(CB.TrueBB));
2964 
2965   setValue(CurInst, BrCond);
2966 
2967   // Insert the false branch. Do this even if it's a fall through branch,
2968   // this makes it easier to do DAG optimizations which require inverting
2969   // the branch condition.
2970   BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
2971                        DAG.getBasicBlock(CB.FalseBB));
2972 
2973   DAG.setRoot(BrCond);
2974 }
2975 
2976 /// visitJumpTable - Emit JumpTable node in the current MBB
2977 void SelectionDAGBuilder::visitJumpTable(SwitchCG::JumpTable &JT) {
2978   // Emit the code for the jump table
2979   assert(JT.SL && "Should set SDLoc for SelectionDAG!");
2980   assert(JT.Reg != -1U && "Should lower JT Header first!");
2981   EVT PTy = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
2982   SDValue Index = DAG.getCopyFromReg(getControlRoot(), *JT.SL, JT.Reg, PTy);
2983   SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
2984   SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, *JT.SL, MVT::Other,
2985                                     Index.getValue(1), Table, Index);
2986   DAG.setRoot(BrJumpTable);
2987 }
2988 
2989 /// visitJumpTableHeader - This function emits necessary code to produce index
2990 /// in the JumpTable from switch case.
2991 void SelectionDAGBuilder::visitJumpTableHeader(SwitchCG::JumpTable &JT,
2992                                                JumpTableHeader &JTH,
2993                                                MachineBasicBlock *SwitchBB) {
2994   assert(JT.SL && "Should set SDLoc for SelectionDAG!");
2995   const SDLoc &dl = *JT.SL;
2996 
2997   // Subtract the lowest switch case value from the value being switched on.
2998   SDValue SwitchOp = getValue(JTH.SValue);
2999   EVT VT = SwitchOp.getValueType();
3000   SDValue Sub = DAG.getNode(ISD::SUB, dl, VT, SwitchOp,
3001                             DAG.getConstant(JTH.First, dl, VT));
3002 
3003   // The SDNode we just created, which holds the value being switched on minus
3004   // the smallest case value, needs to be copied to a virtual register so it
3005   // can be used as an index into the jump table in a subsequent basic block.
3006   // This value may be smaller or larger than the target's pointer type, and
3007   // therefore require extension or truncating.
3008   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3009   SwitchOp = DAG.getZExtOrTrunc(Sub, dl, TLI.getPointerTy(DAG.getDataLayout()));
3010 
3011   unsigned JumpTableReg =
3012       FuncInfo.CreateReg(TLI.getPointerTy(DAG.getDataLayout()));
3013   SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), dl,
3014                                     JumpTableReg, SwitchOp);
3015   JT.Reg = JumpTableReg;
3016 
3017   if (!JTH.FallthroughUnreachable) {
3018     // Emit the range check for the jump table, and branch to the default block
3019     // for the switch statement if the value being switched on exceeds the
3020     // largest case in the switch.
3021     SDValue CMP = DAG.getSetCC(
3022         dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
3023                                    Sub.getValueType()),
3024         Sub, DAG.getConstant(JTH.Last - JTH.First, dl, VT), ISD::SETUGT);
3025 
3026     SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
3027                                  MVT::Other, CopyTo, CMP,
3028                                  DAG.getBasicBlock(JT.Default));
3029 
3030     // Avoid emitting unnecessary branches to the next block.
3031     if (JT.MBB != NextBlock(SwitchBB))
3032       BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
3033                            DAG.getBasicBlock(JT.MBB));
3034 
3035     DAG.setRoot(BrCond);
3036   } else {
3037     // Avoid emitting unnecessary branches to the next block.
3038     if (JT.MBB != NextBlock(SwitchBB))
3039       DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, CopyTo,
3040                               DAG.getBasicBlock(JT.MBB)));
3041     else
3042       DAG.setRoot(CopyTo);
3043   }
3044 }
3045 
3046 /// Create a LOAD_STACK_GUARD node, and let it carry the target specific global
3047 /// variable if there exists one.
3048 static SDValue getLoadStackGuard(SelectionDAG &DAG, const SDLoc &DL,
3049                                  SDValue &Chain) {
3050   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3051   EVT PtrTy = TLI.getPointerTy(DAG.getDataLayout());
3052   EVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout());
3053   MachineFunction &MF = DAG.getMachineFunction();
3054   Value *Global = TLI.getSDagStackGuard(*MF.getFunction().getParent());
3055   MachineSDNode *Node =
3056       DAG.getMachineNode(TargetOpcode::LOAD_STACK_GUARD, DL, PtrTy, Chain);
3057   if (Global) {
3058     MachinePointerInfo MPInfo(Global);
3059     auto Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant |
3060                  MachineMemOperand::MODereferenceable;
3061     MachineMemOperand *MemRef = MF.getMachineMemOperand(
3062         MPInfo, Flags, LocationSize::precise(PtrTy.getSizeInBits() / 8),
3063         DAG.getEVTAlign(PtrTy));
3064     DAG.setNodeMemRefs(Node, {MemRef});
3065   }
3066   if (PtrTy != PtrMemTy)
3067     return DAG.getPtrExtOrTrunc(SDValue(Node, 0), DL, PtrMemTy);
3068   return SDValue(Node, 0);
3069 }
3070 
3071 /// Codegen a new tail for a stack protector check ParentMBB which has had its
3072 /// tail spliced into a stack protector check success bb.
3073 ///
3074 /// For a high level explanation of how this fits into the stack protector
3075 /// generation see the comment on the declaration of class
3076 /// StackProtectorDescriptor.
3077 void SelectionDAGBuilder::visitSPDescriptorParent(StackProtectorDescriptor &SPD,
3078                                                   MachineBasicBlock *ParentBB) {
3079 
3080   // First create the loads to the guard/stack slot for the comparison.
3081   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3082   EVT PtrTy = TLI.getPointerTy(DAG.getDataLayout());
3083   EVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout());
3084 
3085   MachineFrameInfo &MFI = ParentBB->getParent()->getFrameInfo();
3086   int FI = MFI.getStackProtectorIndex();
3087 
3088   SDValue Guard;
3089   SDLoc dl = getCurSDLoc();
3090   SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
3091   const Module &M = *ParentBB->getParent()->getFunction().getParent();
3092   Align Align =
3093       DAG.getDataLayout().getPrefTypeAlign(PointerType::get(M.getContext(), 0));
3094 
3095   // Generate code to load the content of the guard slot.
3096   SDValue GuardVal = DAG.getLoad(
3097       PtrMemTy, dl, DAG.getEntryNode(), StackSlotPtr,
3098       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), Align,
3099       MachineMemOperand::MOVolatile);
3100 
3101   if (TLI.useStackGuardXorFP())
3102     GuardVal = TLI.emitStackGuardXorFP(DAG, GuardVal, dl);
3103 
3104   // Retrieve guard check function, nullptr if instrumentation is inlined.
3105   if (const Function *GuardCheckFn = TLI.getSSPStackGuardCheck(M)) {
3106     // The target provides a guard check function to validate the guard value.
3107     // Generate a call to that function with the content of the guard slot as
3108     // argument.
3109     FunctionType *FnTy = GuardCheckFn->getFunctionType();
3110     assert(FnTy->getNumParams() == 1 && "Invalid function signature");
3111 
3112     TargetLowering::ArgListTy Args;
3113     TargetLowering::ArgListEntry Entry;
3114     Entry.Node = GuardVal;
3115     Entry.Ty = FnTy->getParamType(0);
3116     if (GuardCheckFn->hasParamAttribute(0, Attribute::AttrKind::InReg))
3117       Entry.IsInReg = true;
3118     Args.push_back(Entry);
3119 
3120     TargetLowering::CallLoweringInfo CLI(DAG);
3121     CLI.setDebugLoc(getCurSDLoc())
3122         .setChain(DAG.getEntryNode())
3123         .setCallee(GuardCheckFn->getCallingConv(), FnTy->getReturnType(),
3124                    getValue(GuardCheckFn), std::move(Args));
3125 
3126     std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
3127     DAG.setRoot(Result.second);
3128     return;
3129   }
3130 
3131   // If useLoadStackGuardNode returns true, generate LOAD_STACK_GUARD.
3132   // Otherwise, emit a volatile load to retrieve the stack guard value.
3133   SDValue Chain = DAG.getEntryNode();
3134   if (TLI.useLoadStackGuardNode()) {
3135     Guard = getLoadStackGuard(DAG, dl, Chain);
3136   } else {
3137     const Value *IRGuard = TLI.getSDagStackGuard(M);
3138     SDValue GuardPtr = getValue(IRGuard);
3139 
3140     Guard = DAG.getLoad(PtrMemTy, dl, Chain, GuardPtr,
3141                         MachinePointerInfo(IRGuard, 0), Align,
3142                         MachineMemOperand::MOVolatile);
3143   }
3144 
3145   // Perform the comparison via a getsetcc.
3146   SDValue Cmp = DAG.getSetCC(dl, TLI.getSetCCResultType(DAG.getDataLayout(),
3147                                                         *DAG.getContext(),
3148                                                         Guard.getValueType()),
3149                              Guard, GuardVal, ISD::SETNE);
3150 
3151   // If the guard/stackslot do not equal, branch to failure MBB.
3152   SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
3153                                MVT::Other, GuardVal.getOperand(0),
3154                                Cmp, DAG.getBasicBlock(SPD.getFailureMBB()));
3155   // Otherwise branch to success MBB.
3156   SDValue Br = DAG.getNode(ISD::BR, dl,
3157                            MVT::Other, BrCond,
3158                            DAG.getBasicBlock(SPD.getSuccessMBB()));
3159 
3160   DAG.setRoot(Br);
3161 }
3162 
3163 /// Codegen the failure basic block for a stack protector check.
3164 ///
3165 /// A failure stack protector machine basic block consists simply of a call to
3166 /// __stack_chk_fail().
3167 ///
3168 /// For a high level explanation of how this fits into the stack protector
3169 /// generation see the comment on the declaration of class
3170 /// StackProtectorDescriptor.
3171 void
3172 SelectionDAGBuilder::visitSPDescriptorFailure(StackProtectorDescriptor &SPD) {
3173   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3174   TargetLowering::MakeLibCallOptions CallOptions;
3175   CallOptions.setDiscardResult(true);
3176   SDValue Chain =
3177       TLI.makeLibCall(DAG, RTLIB::STACKPROTECTOR_CHECK_FAIL, MVT::isVoid,
3178                       std::nullopt, CallOptions, getCurSDLoc())
3179           .second;
3180   // On PS4/PS5, the "return address" must still be within the calling
3181   // function, even if it's at the very end, so emit an explicit TRAP here.
3182   // Passing 'true' for doesNotReturn above won't generate the trap for us.
3183   if (TM.getTargetTriple().isPS())
3184     Chain = DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, Chain);
3185   // WebAssembly needs an unreachable instruction after a non-returning call,
3186   // because the function return type can be different from __stack_chk_fail's
3187   // return type (void).
3188   if (TM.getTargetTriple().isWasm())
3189     Chain = DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, Chain);
3190 
3191   DAG.setRoot(Chain);
3192 }
3193 
3194 /// visitBitTestHeader - This function emits necessary code to produce value
3195 /// suitable for "bit tests"
3196 void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B,
3197                                              MachineBasicBlock *SwitchBB) {
3198   SDLoc dl = getCurSDLoc();
3199 
3200   // Subtract the minimum value.
3201   SDValue SwitchOp = getValue(B.SValue);
3202   EVT VT = SwitchOp.getValueType();
3203   SDValue RangeSub =
3204       DAG.getNode(ISD::SUB, dl, VT, SwitchOp, DAG.getConstant(B.First, dl, VT));
3205 
3206   // Determine the type of the test operands.
3207   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3208   bool UsePtrType = false;
3209   if (!TLI.isTypeLegal(VT)) {
3210     UsePtrType = true;
3211   } else {
3212     for (unsigned i = 0, e = B.Cases.size(); i != e; ++i)
3213       if (!isUIntN(VT.getSizeInBits(), B.Cases[i].Mask)) {
3214         // Switch table case range are encoded into series of masks.
3215         // Just use pointer type, it's guaranteed to fit.
3216         UsePtrType = true;
3217         break;
3218       }
3219   }
3220   SDValue Sub = RangeSub;
3221   if (UsePtrType) {
3222     VT = TLI.getPointerTy(DAG.getDataLayout());
3223     Sub = DAG.getZExtOrTrunc(Sub, dl, VT);
3224   }
3225 
3226   B.RegVT = VT.getSimpleVT();
3227   B.Reg = FuncInfo.CreateReg(B.RegVT);
3228   SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), dl, B.Reg, Sub);
3229 
3230   MachineBasicBlock* MBB = B.Cases[0].ThisBB;
3231 
3232   if (!B.FallthroughUnreachable)
3233     addSuccessorWithProb(SwitchBB, B.Default, B.DefaultProb);
3234   addSuccessorWithProb(SwitchBB, MBB, B.Prob);
3235   SwitchBB->normalizeSuccProbs();
3236 
3237   SDValue Root = CopyTo;
3238   if (!B.FallthroughUnreachable) {
3239     // Conditional branch to the default block.
3240     SDValue RangeCmp = DAG.getSetCC(dl,
3241         TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(),
3242                                RangeSub.getValueType()),
3243         RangeSub, DAG.getConstant(B.Range, dl, RangeSub.getValueType()),
3244         ISD::SETUGT);
3245 
3246     Root = DAG.getNode(ISD::BRCOND, dl, MVT::Other, Root, RangeCmp,
3247                        DAG.getBasicBlock(B.Default));
3248   }
3249 
3250   // Avoid emitting unnecessary branches to the next block.
3251   if (MBB != NextBlock(SwitchBB))
3252     Root = DAG.getNode(ISD::BR, dl, MVT::Other, Root, DAG.getBasicBlock(MBB));
3253 
3254   DAG.setRoot(Root);
3255 }
3256 
3257 /// visitBitTestCase - this function produces one "bit test"
3258 void SelectionDAGBuilder::visitBitTestCase(BitTestBlock &BB,
3259                                            MachineBasicBlock* NextMBB,
3260                                            BranchProbability BranchProbToNext,
3261                                            unsigned Reg,
3262                                            BitTestCase &B,
3263                                            MachineBasicBlock *SwitchBB) {
3264   SDLoc dl = getCurSDLoc();
3265   MVT VT = BB.RegVT;
3266   SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), dl, Reg, VT);
3267   SDValue Cmp;
3268   unsigned PopCount = llvm::popcount(B.Mask);
3269   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3270   if (PopCount == 1) {
3271     // Testing for a single bit; just compare the shift count with what it
3272     // would need to be to shift a 1 bit in that position.
3273     Cmp = DAG.getSetCC(
3274         dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3275         ShiftOp, DAG.getConstant(llvm::countr_zero(B.Mask), dl, VT),
3276         ISD::SETEQ);
3277   } else if (PopCount == BB.Range) {
3278     // There is only one zero bit in the range, test for it directly.
3279     Cmp = DAG.getSetCC(
3280         dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3281         ShiftOp, DAG.getConstant(llvm::countr_one(B.Mask), dl, VT), ISD::SETNE);
3282   } else {
3283     // Make desired shift
3284     SDValue SwitchVal = DAG.getNode(ISD::SHL, dl, VT,
3285                                     DAG.getConstant(1, dl, VT), ShiftOp);
3286 
3287     // Emit bit tests and jumps
3288     SDValue AndOp = DAG.getNode(ISD::AND, dl,
3289                                 VT, SwitchVal, DAG.getConstant(B.Mask, dl, VT));
3290     Cmp = DAG.getSetCC(
3291         dl, TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT),
3292         AndOp, DAG.getConstant(0, dl, VT), ISD::SETNE);
3293   }
3294 
3295   // The branch probability from SwitchBB to B.TargetBB is B.ExtraProb.
3296   addSuccessorWithProb(SwitchBB, B.TargetBB, B.ExtraProb);
3297   // The branch probability from SwitchBB to NextMBB is BranchProbToNext.
3298   addSuccessorWithProb(SwitchBB, NextMBB, BranchProbToNext);
3299   // It is not guaranteed that the sum of B.ExtraProb and BranchProbToNext is
3300   // one as they are relative probabilities (and thus work more like weights),
3301   // and hence we need to normalize them to let the sum of them become one.
3302   SwitchBB->normalizeSuccProbs();
3303 
3304   SDValue BrAnd = DAG.getNode(ISD::BRCOND, dl,
3305                               MVT::Other, getControlRoot(),
3306                               Cmp, DAG.getBasicBlock(B.TargetBB));
3307 
3308   // Avoid emitting unnecessary branches to the next block.
3309   if (NextMBB != NextBlock(SwitchBB))
3310     BrAnd = DAG.getNode(ISD::BR, dl, MVT::Other, BrAnd,
3311                         DAG.getBasicBlock(NextMBB));
3312 
3313   DAG.setRoot(BrAnd);
3314 }
3315 
3316 void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
3317   MachineBasicBlock *InvokeMBB = FuncInfo.MBB;
3318 
3319   // Retrieve successors. Look through artificial IR level blocks like
3320   // catchswitch for successors.
3321   MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
3322   const BasicBlock *EHPadBB = I.getSuccessor(1);
3323   MachineBasicBlock *EHPadMBB = FuncInfo.MBBMap[EHPadBB];
3324 
3325   // Deopt and ptrauth bundles are lowered in helper functions, and we don't
3326   // have to do anything here to lower funclet bundles.
3327   assert(!I.hasOperandBundlesOtherThan(
3328              {LLVMContext::OB_deopt, LLVMContext::OB_gc_transition,
3329               LLVMContext::OB_gc_live, LLVMContext::OB_funclet,
3330               LLVMContext::OB_cfguardtarget, LLVMContext::OB_ptrauth,
3331               LLVMContext::OB_clang_arc_attachedcall}) &&
3332          "Cannot lower invokes with arbitrary operand bundles yet!");
3333 
3334   const Value *Callee(I.getCalledOperand());
3335   const Function *Fn = dyn_cast<Function>(Callee);
3336   if (isa<InlineAsm>(Callee))
3337     visitInlineAsm(I, EHPadBB);
3338   else if (Fn && Fn->isIntrinsic()) {
3339     switch (Fn->getIntrinsicID()) {
3340     default:
3341       llvm_unreachable("Cannot invoke this intrinsic");
3342     case Intrinsic::donothing:
3343       // Ignore invokes to @llvm.donothing: jump directly to the next BB.
3344     case Intrinsic::seh_try_begin:
3345     case Intrinsic::seh_scope_begin:
3346     case Intrinsic::seh_try_end:
3347     case Intrinsic::seh_scope_end:
3348       if (EHPadMBB)
3349           // a block referenced by EH table
3350           // so dtor-funclet not removed by opts
3351           EHPadMBB->setMachineBlockAddressTaken();
3352       break;
3353     case Intrinsic::experimental_patchpoint_void:
3354     case Intrinsic::experimental_patchpoint:
3355       visitPatchpoint(I, EHPadBB);
3356       break;
3357     case Intrinsic::experimental_gc_statepoint:
3358       LowerStatepoint(cast<GCStatepointInst>(I), EHPadBB);
3359       break;
3360     case Intrinsic::wasm_rethrow: {
3361       // This is usually done in visitTargetIntrinsic, but this intrinsic is
3362       // special because it can be invoked, so we manually lower it to a DAG
3363       // node here.
3364       SmallVector<SDValue, 8> Ops;
3365       Ops.push_back(getControlRoot()); // inchain for the terminator node
3366       const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3367       Ops.push_back(
3368           DAG.getTargetConstant(Intrinsic::wasm_rethrow, getCurSDLoc(),
3369                                 TLI.getPointerTy(DAG.getDataLayout())));
3370       SDVTList VTs = DAG.getVTList(ArrayRef<EVT>({MVT::Other})); // outchain
3371       DAG.setRoot(DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops));
3372       break;
3373     }
3374     }
3375   } else if (I.hasDeoptState()) {
3376     // Currently we do not lower any intrinsic calls with deopt operand bundles.
3377     // Eventually we will support lowering the @llvm.experimental.deoptimize
3378     // intrinsic, and right now there are no plans to support other intrinsics
3379     // with deopt state.
3380     LowerCallSiteWithDeoptBundle(&I, getValue(Callee), EHPadBB);
3381   } else if (I.countOperandBundlesOfType(LLVMContext::OB_ptrauth)) {
3382     LowerCallSiteWithPtrAuthBundle(cast<CallBase>(I), EHPadBB);
3383   } else {
3384     LowerCallTo(I, getValue(Callee), false, false, EHPadBB);
3385   }
3386 
3387   // If the value of the invoke is used outside of its defining block, make it
3388   // available as a virtual register.
3389   // We already took care of the exported value for the statepoint instruction
3390   // during call to the LowerStatepoint.
3391   if (!isa<GCStatepointInst>(I)) {
3392     CopyToExportRegsIfNeeded(&I);
3393   }
3394 
3395   SmallVector<std::pair<MachineBasicBlock *, BranchProbability>, 1> UnwindDests;
3396   BranchProbabilityInfo *BPI = FuncInfo.BPI;
3397   BranchProbability EHPadBBProb =
3398       BPI ? BPI->getEdgeProbability(InvokeMBB->getBasicBlock(), EHPadBB)
3399           : BranchProbability::getZero();
3400   findUnwindDestinations(FuncInfo, EHPadBB, EHPadBBProb, UnwindDests);
3401 
3402   // Update successor info.
3403   addSuccessorWithProb(InvokeMBB, Return);
3404   for (auto &UnwindDest : UnwindDests) {
3405     UnwindDest.first->setIsEHPad();
3406     addSuccessorWithProb(InvokeMBB, UnwindDest.first, UnwindDest.second);
3407   }
3408   InvokeMBB->normalizeSuccProbs();
3409 
3410   // Drop into normal successor.
3411   DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, getControlRoot(),
3412                           DAG.getBasicBlock(Return)));
3413 }
3414 
3415 void SelectionDAGBuilder::visitCallBr(const CallBrInst &I) {
3416   MachineBasicBlock *CallBrMBB = FuncInfo.MBB;
3417 
3418   // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't
3419   // have to do anything here to lower funclet bundles.
3420   assert(!I.hasOperandBundlesOtherThan(
3421              {LLVMContext::OB_deopt, LLVMContext::OB_funclet}) &&
3422          "Cannot lower callbrs with arbitrary operand bundles yet!");
3423 
3424   assert(I.isInlineAsm() && "Only know how to handle inlineasm callbr");
3425   visitInlineAsm(I);
3426   CopyToExportRegsIfNeeded(&I);
3427 
3428   // Retrieve successors.
3429   SmallPtrSet<BasicBlock *, 8> Dests;
3430   Dests.insert(I.getDefaultDest());
3431   MachineBasicBlock *Return = FuncInfo.MBBMap[I.getDefaultDest()];
3432 
3433   // Update successor info.
3434   addSuccessorWithProb(CallBrMBB, Return, BranchProbability::getOne());
3435   for (unsigned i = 0, e = I.getNumIndirectDests(); i < e; ++i) {
3436     BasicBlock *Dest = I.getIndirectDest(i);
3437     MachineBasicBlock *Target = FuncInfo.MBBMap[Dest];
3438     Target->setIsInlineAsmBrIndirectTarget();
3439     Target->setMachineBlockAddressTaken();
3440     Target->setLabelMustBeEmitted();
3441     // Don't add duplicate machine successors.
3442     if (Dests.insert(Dest).second)
3443       addSuccessorWithProb(CallBrMBB, Target, BranchProbability::getZero());
3444   }
3445   CallBrMBB->normalizeSuccProbs();
3446 
3447   // Drop into default successor.
3448   DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
3449                           MVT::Other, getControlRoot(),
3450                           DAG.getBasicBlock(Return)));
3451 }
3452 
3453 void SelectionDAGBuilder::visitResume(const ResumeInst &RI) {
3454   llvm_unreachable("SelectionDAGBuilder shouldn't visit resume instructions!");
3455 }
3456 
3457 void SelectionDAGBuilder::visitLandingPad(const LandingPadInst &LP) {
3458   assert(FuncInfo.MBB->isEHPad() &&
3459          "Call to landingpad not in landing pad!");
3460 
3461   // If there aren't registers to copy the values into (e.g., during SjLj
3462   // exceptions), then don't bother to create these DAG nodes.
3463   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3464   const Constant *PersonalityFn = FuncInfo.Fn->getPersonalityFn();
3465   if (TLI.getExceptionPointerRegister(PersonalityFn) == 0 &&
3466       TLI.getExceptionSelectorRegister(PersonalityFn) == 0)
3467     return;
3468 
3469   // If landingpad's return type is token type, we don't create DAG nodes
3470   // for its exception pointer and selector value. The extraction of exception
3471   // pointer or selector value from token type landingpads is not currently
3472   // supported.
3473   if (LP.getType()->isTokenTy())
3474     return;
3475 
3476   SmallVector<EVT, 2> ValueVTs;
3477   SDLoc dl = getCurSDLoc();
3478   ComputeValueVTs(TLI, DAG.getDataLayout(), LP.getType(), ValueVTs);
3479   assert(ValueVTs.size() == 2 && "Only two-valued landingpads are supported");
3480 
3481   // Get the two live-in registers as SDValues. The physregs have already been
3482   // copied into virtual registers.
3483   SDValue Ops[2];
3484   if (FuncInfo.ExceptionPointerVirtReg) {
3485     Ops[0] = DAG.getZExtOrTrunc(
3486         DAG.getCopyFromReg(DAG.getEntryNode(), dl,
3487                            FuncInfo.ExceptionPointerVirtReg,
3488                            TLI.getPointerTy(DAG.getDataLayout())),
3489         dl, ValueVTs[0]);
3490   } else {
3491     Ops[0] = DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout()));
3492   }
3493   Ops[1] = DAG.getZExtOrTrunc(
3494       DAG.getCopyFromReg(DAG.getEntryNode(), dl,
3495                          FuncInfo.ExceptionSelectorVirtReg,
3496                          TLI.getPointerTy(DAG.getDataLayout())),
3497       dl, ValueVTs[1]);
3498 
3499   // Merge into one.
3500   SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
3501                             DAG.getVTList(ValueVTs), Ops);
3502   setValue(&LP, Res);
3503 }
3504 
3505 void SelectionDAGBuilder::UpdateSplitBlock(MachineBasicBlock *First,
3506                                            MachineBasicBlock *Last) {
3507   // Update JTCases.
3508   for (JumpTableBlock &JTB : SL->JTCases)
3509     if (JTB.first.HeaderBB == First)
3510       JTB.first.HeaderBB = Last;
3511 
3512   // Update BitTestCases.
3513   for (BitTestBlock &BTB : SL->BitTestCases)
3514     if (BTB.Parent == First)
3515       BTB.Parent = Last;
3516 }
3517 
3518 void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
3519   MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB;
3520 
3521   // Update machine-CFG edges with unique successors.
3522   SmallSet<BasicBlock*, 32> Done;
3523   for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) {
3524     BasicBlock *BB = I.getSuccessor(i);
3525     bool Inserted = Done.insert(BB).second;
3526     if (!Inserted)
3527         continue;
3528 
3529     MachineBasicBlock *Succ = FuncInfo.MBBMap[BB];
3530     addSuccessorWithProb(IndirectBrMBB, Succ);
3531   }
3532   IndirectBrMBB->normalizeSuccProbs();
3533 
3534   DAG.setRoot(DAG.getNode(ISD::BRIND, getCurSDLoc(),
3535                           MVT::Other, getControlRoot(),
3536                           getValue(I.getAddress())));
3537 }
3538 
3539 void SelectionDAGBuilder::visitUnreachable(const UnreachableInst &I) {
3540   if (!DAG.getTarget().Options.TrapUnreachable)
3541     return;
3542 
3543   // We may be able to ignore unreachable behind a noreturn call.
3544   if (DAG.getTarget().Options.NoTrapAfterNoreturn) {
3545     if (const CallInst *Call = dyn_cast_or_null<CallInst>(I.getPrevNode())) {
3546       if (Call->doesNotReturn())
3547         return;
3548     }
3549   }
3550 
3551   DAG.setRoot(DAG.getNode(ISD::TRAP, getCurSDLoc(), MVT::Other, DAG.getRoot()));
3552 }
3553 
3554 void SelectionDAGBuilder::visitUnary(const User &I, unsigned Opcode) {
3555   SDNodeFlags Flags;
3556   if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3557     Flags.copyFMF(*FPOp);
3558 
3559   SDValue Op = getValue(I.getOperand(0));
3560   SDValue UnNodeValue = DAG.getNode(Opcode, getCurSDLoc(), Op.getValueType(),
3561                                     Op, Flags);
3562   setValue(&I, UnNodeValue);
3563 }
3564 
3565 void SelectionDAGBuilder::visitBinary(const User &I, unsigned Opcode) {
3566   SDNodeFlags Flags;
3567   if (auto *OFBinOp = dyn_cast<OverflowingBinaryOperator>(&I)) {
3568     Flags.setNoSignedWrap(OFBinOp->hasNoSignedWrap());
3569     Flags.setNoUnsignedWrap(OFBinOp->hasNoUnsignedWrap());
3570   }
3571   if (auto *ExactOp = dyn_cast<PossiblyExactOperator>(&I))
3572     Flags.setExact(ExactOp->isExact());
3573   if (auto *DisjointOp = dyn_cast<PossiblyDisjointInst>(&I))
3574     Flags.setDisjoint(DisjointOp->isDisjoint());
3575   if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3576     Flags.copyFMF(*FPOp);
3577 
3578   SDValue Op1 = getValue(I.getOperand(0));
3579   SDValue Op2 = getValue(I.getOperand(1));
3580   SDValue BinNodeValue = DAG.getNode(Opcode, getCurSDLoc(), Op1.getValueType(),
3581                                      Op1, Op2, Flags);
3582   setValue(&I, BinNodeValue);
3583 }
3584 
3585 void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
3586   SDValue Op1 = getValue(I.getOperand(0));
3587   SDValue Op2 = getValue(I.getOperand(1));
3588 
3589   EVT ShiftTy = DAG.getTargetLoweringInfo().getShiftAmountTy(
3590       Op1.getValueType(), DAG.getDataLayout());
3591 
3592   // Coerce the shift amount to the right type if we can. This exposes the
3593   // truncate or zext to optimization early.
3594   if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) {
3595     assert(ShiftTy.getSizeInBits() >= Log2_32_Ceil(Op1.getValueSizeInBits()) &&
3596            "Unexpected shift type");
3597     Op2 = DAG.getZExtOrTrunc(Op2, getCurSDLoc(), ShiftTy);
3598   }
3599 
3600   bool nuw = false;
3601   bool nsw = false;
3602   bool exact = false;
3603 
3604   if (Opcode == ISD::SRL || Opcode == ISD::SRA || Opcode == ISD::SHL) {
3605 
3606     if (const OverflowingBinaryOperator *OFBinOp =
3607             dyn_cast<const OverflowingBinaryOperator>(&I)) {
3608       nuw = OFBinOp->hasNoUnsignedWrap();
3609       nsw = OFBinOp->hasNoSignedWrap();
3610     }
3611     if (const PossiblyExactOperator *ExactOp =
3612             dyn_cast<const PossiblyExactOperator>(&I))
3613       exact = ExactOp->isExact();
3614   }
3615   SDNodeFlags Flags;
3616   Flags.setExact(exact);
3617   Flags.setNoSignedWrap(nsw);
3618   Flags.setNoUnsignedWrap(nuw);
3619   SDValue Res = DAG.getNode(Opcode, getCurSDLoc(), Op1.getValueType(), Op1, Op2,
3620                             Flags);
3621   setValue(&I, Res);
3622 }
3623 
3624 void SelectionDAGBuilder::visitSDiv(const User &I) {
3625   SDValue Op1 = getValue(I.getOperand(0));
3626   SDValue Op2 = getValue(I.getOperand(1));
3627 
3628   SDNodeFlags Flags;
3629   Flags.setExact(isa<PossiblyExactOperator>(&I) &&
3630                  cast<PossiblyExactOperator>(&I)->isExact());
3631   setValue(&I, DAG.getNode(ISD::SDIV, getCurSDLoc(), Op1.getValueType(), Op1,
3632                            Op2, Flags));
3633 }
3634 
3635 void SelectionDAGBuilder::visitICmp(const ICmpInst &I) {
3636   ICmpInst::Predicate predicate = I.getPredicate();
3637   SDValue Op1 = getValue(I.getOperand(0));
3638   SDValue Op2 = getValue(I.getOperand(1));
3639   ISD::CondCode Opcode = getICmpCondCode(predicate);
3640 
3641   auto &TLI = DAG.getTargetLoweringInfo();
3642   EVT MemVT =
3643       TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
3644 
3645   // If a pointer's DAG type is larger than its memory type then the DAG values
3646   // are zero-extended. This breaks signed comparisons so truncate back to the
3647   // underlying type before doing the compare.
3648   if (Op1.getValueType() != MemVT) {
3649     Op1 = DAG.getPtrExtOrTrunc(Op1, getCurSDLoc(), MemVT);
3650     Op2 = DAG.getPtrExtOrTrunc(Op2, getCurSDLoc(), MemVT);
3651   }
3652 
3653   EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3654                                                         I.getType());
3655   setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Opcode));
3656 }
3657 
3658 void SelectionDAGBuilder::visitFCmp(const FCmpInst &I) {
3659   FCmpInst::Predicate predicate = I.getPredicate();
3660   SDValue Op1 = getValue(I.getOperand(0));
3661   SDValue Op2 = getValue(I.getOperand(1));
3662 
3663   ISD::CondCode Condition = getFCmpCondCode(predicate);
3664   auto *FPMO = cast<FPMathOperator>(&I);
3665   if (FPMO->hasNoNaNs() || TM.Options.NoNaNsFPMath)
3666     Condition = getFCmpCodeWithoutNaN(Condition);
3667 
3668   SDNodeFlags Flags;
3669   Flags.copyFMF(*FPMO);
3670   SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
3671 
3672   EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3673                                                         I.getType());
3674   setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Condition));
3675 }
3676 
3677 // Check if the condition of the select has one use or two users that are both
3678 // selects with the same condition.
3679 static bool hasOnlySelectUsers(const Value *Cond) {
3680   return llvm::all_of(Cond->users(), [](const Value *V) {
3681     return isa<SelectInst>(V);
3682   });
3683 }
3684 
3685 void SelectionDAGBuilder::visitSelect(const User &I) {
3686   SmallVector<EVT, 4> ValueVTs;
3687   ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
3688                   ValueVTs);
3689   unsigned NumValues = ValueVTs.size();
3690   if (NumValues == 0) return;
3691 
3692   SmallVector<SDValue, 4> Values(NumValues);
3693   SDValue Cond     = getValue(I.getOperand(0));
3694   SDValue LHSVal   = getValue(I.getOperand(1));
3695   SDValue RHSVal   = getValue(I.getOperand(2));
3696   SmallVector<SDValue, 1> BaseOps(1, Cond);
3697   ISD::NodeType OpCode =
3698       Cond.getValueType().isVector() ? ISD::VSELECT : ISD::SELECT;
3699 
3700   bool IsUnaryAbs = false;
3701   bool Negate = false;
3702 
3703   SDNodeFlags Flags;
3704   if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
3705     Flags.copyFMF(*FPOp);
3706 
3707   Flags.setUnpredictable(
3708       cast<SelectInst>(I).getMetadata(LLVMContext::MD_unpredictable));
3709 
3710   // Min/max matching is only viable if all output VTs are the same.
3711   if (all_equal(ValueVTs)) {
3712     EVT VT = ValueVTs[0];
3713     LLVMContext &Ctx = *DAG.getContext();
3714     auto &TLI = DAG.getTargetLoweringInfo();
3715 
3716     // We care about the legality of the operation after it has been type
3717     // legalized.
3718     while (TLI.getTypeAction(Ctx, VT) != TargetLoweringBase::TypeLegal)
3719       VT = TLI.getTypeToTransformTo(Ctx, VT);
3720 
3721     // If the vselect is legal, assume we want to leave this as a vector setcc +
3722     // vselect. Otherwise, if this is going to be scalarized, we want to see if
3723     // min/max is legal on the scalar type.
3724     bool UseScalarMinMax = VT.isVector() &&
3725       !TLI.isOperationLegalOrCustom(ISD::VSELECT, VT);
3726 
3727     // ValueTracking's select pattern matching does not account for -0.0,
3728     // so we can't lower to FMINIMUM/FMAXIMUM because those nodes specify that
3729     // -0.0 is less than +0.0.
3730     Value *LHS, *RHS;
3731     auto SPR = matchSelectPattern(const_cast<User*>(&I), LHS, RHS);
3732     ISD::NodeType Opc = ISD::DELETED_NODE;
3733     switch (SPR.Flavor) {
3734     case SPF_UMAX:    Opc = ISD::UMAX; break;
3735     case SPF_UMIN:    Opc = ISD::UMIN; break;
3736     case SPF_SMAX:    Opc = ISD::SMAX; break;
3737     case SPF_SMIN:    Opc = ISD::SMIN; break;
3738     case SPF_FMINNUM:
3739       switch (SPR.NaNBehavior) {
3740       case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?");
3741       case SPNB_RETURNS_NAN: break;
3742       case SPNB_RETURNS_OTHER: Opc = ISD::FMINNUM; break;
3743       case SPNB_RETURNS_ANY:
3744         if (TLI.isOperationLegalOrCustom(ISD::FMINNUM, VT) ||
3745             (UseScalarMinMax &&
3746              TLI.isOperationLegalOrCustom(ISD::FMINNUM, VT.getScalarType())))
3747           Opc = ISD::FMINNUM;
3748         break;
3749       }
3750       break;
3751     case SPF_FMAXNUM:
3752       switch (SPR.NaNBehavior) {
3753       case SPNB_NA: llvm_unreachable("No NaN behavior for FP op?");
3754       case SPNB_RETURNS_NAN: break;
3755       case SPNB_RETURNS_OTHER: Opc = ISD::FMAXNUM; break;
3756       case SPNB_RETURNS_ANY:
3757         if (TLI.isOperationLegalOrCustom(ISD::FMAXNUM, VT) ||
3758             (UseScalarMinMax &&
3759              TLI.isOperationLegalOrCustom(ISD::FMAXNUM, VT.getScalarType())))
3760           Opc = ISD::FMAXNUM;
3761         break;
3762       }
3763       break;
3764     case SPF_NABS:
3765       Negate = true;
3766       [[fallthrough]];
3767     case SPF_ABS:
3768       IsUnaryAbs = true;
3769       Opc = ISD::ABS;
3770       break;
3771     default: break;
3772     }
3773 
3774     if (!IsUnaryAbs && Opc != ISD::DELETED_NODE &&
3775         (TLI.isOperationLegalOrCustomOrPromote(Opc, VT) ||
3776          (UseScalarMinMax &&
3777           TLI.isOperationLegalOrCustom(Opc, VT.getScalarType()))) &&
3778         // If the underlying comparison instruction is used by any other
3779         // instruction, the consumed instructions won't be destroyed, so it is
3780         // not profitable to convert to a min/max.
3781         hasOnlySelectUsers(cast<SelectInst>(I).getCondition())) {
3782       OpCode = Opc;
3783       LHSVal = getValue(LHS);
3784       RHSVal = getValue(RHS);
3785       BaseOps.clear();
3786     }
3787 
3788     if (IsUnaryAbs) {
3789       OpCode = Opc;
3790       LHSVal = getValue(LHS);
3791       BaseOps.clear();
3792     }
3793   }
3794 
3795   if (IsUnaryAbs) {
3796     for (unsigned i = 0; i != NumValues; ++i) {
3797       SDLoc dl = getCurSDLoc();
3798       EVT VT = LHSVal.getNode()->getValueType(LHSVal.getResNo() + i);
3799       Values[i] =
3800           DAG.getNode(OpCode, dl, VT, LHSVal.getValue(LHSVal.getResNo() + i));
3801       if (Negate)
3802         Values[i] = DAG.getNegative(Values[i], dl, VT);
3803     }
3804   } else {
3805     for (unsigned i = 0; i != NumValues; ++i) {
3806       SmallVector<SDValue, 3> Ops(BaseOps.begin(), BaseOps.end());
3807       Ops.push_back(SDValue(LHSVal.getNode(), LHSVal.getResNo() + i));
3808       Ops.push_back(SDValue(RHSVal.getNode(), RHSVal.getResNo() + i));
3809       Values[i] = DAG.getNode(
3810           OpCode, getCurSDLoc(),
3811           LHSVal.getNode()->getValueType(LHSVal.getResNo() + i), Ops, Flags);
3812     }
3813   }
3814 
3815   setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
3816                            DAG.getVTList(ValueVTs), Values));
3817 }
3818 
3819 void SelectionDAGBuilder::visitTrunc(const User &I) {
3820   // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
3821   SDValue N = getValue(I.getOperand(0));
3822   EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3823                                                         I.getType());
3824   setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), DestVT, N));
3825 }
3826 
3827 void SelectionDAGBuilder::visitZExt(const User &I) {
3828   // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
3829   // ZExt also can't be a cast to bool for same reason. So, nothing much to do
3830   SDValue N = getValue(I.getOperand(0));
3831   auto &TLI = DAG.getTargetLoweringInfo();
3832   EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
3833 
3834   SDNodeFlags Flags;
3835   if (auto *PNI = dyn_cast<PossiblyNonNegInst>(&I))
3836     Flags.setNonNeg(PNI->hasNonNeg());
3837 
3838   // Eagerly use nonneg information to canonicalize towards sign_extend if
3839   // that is the target's preference.
3840   // TODO: Let the target do this later.
3841   if (Flags.hasNonNeg() &&
3842       TLI.isSExtCheaperThanZExt(N.getValueType(), DestVT)) {
3843     setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
3844     return;
3845   }
3846 
3847   setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(), DestVT, N, Flags));
3848 }
3849 
3850 void SelectionDAGBuilder::visitSExt(const User &I) {
3851   // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
3852   // SExt also can't be a cast to bool for same reason. So, nothing much to do
3853   SDValue N = getValue(I.getOperand(0));
3854   EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3855                                                         I.getType());
3856   setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
3857 }
3858 
3859 void SelectionDAGBuilder::visitFPTrunc(const User &I) {
3860   // FPTrunc is never a no-op cast, no need to check
3861   SDValue N = getValue(I.getOperand(0));
3862   SDLoc dl = getCurSDLoc();
3863   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3864   EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
3865   setValue(&I, DAG.getNode(ISD::FP_ROUND, dl, DestVT, N,
3866                            DAG.getTargetConstant(
3867                                0, dl, TLI.getPointerTy(DAG.getDataLayout()))));
3868 }
3869 
3870 void SelectionDAGBuilder::visitFPExt(const User &I) {
3871   // FPExt is never a no-op cast, no need to check
3872   SDValue N = getValue(I.getOperand(0));
3873   EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3874                                                         I.getType());
3875   setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurSDLoc(), DestVT, N));
3876 }
3877 
3878 void SelectionDAGBuilder::visitFPToUI(const User &I) {
3879   // FPToUI is never a no-op cast, no need to check
3880   SDValue N = getValue(I.getOperand(0));
3881   EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3882                                                         I.getType());
3883   setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurSDLoc(), DestVT, N));
3884 }
3885 
3886 void SelectionDAGBuilder::visitFPToSI(const User &I) {
3887   // FPToSI is never a no-op cast, no need to check
3888   SDValue N = getValue(I.getOperand(0));
3889   EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3890                                                         I.getType());
3891   setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurSDLoc(), DestVT, N));
3892 }
3893 
3894 void SelectionDAGBuilder::visitUIToFP(const User &I) {
3895   // UIToFP is never a no-op cast, no need to check
3896   SDValue N = getValue(I.getOperand(0));
3897   EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3898                                                         I.getType());
3899   SDNodeFlags Flags;
3900   if (auto *PNI = dyn_cast<PossiblyNonNegInst>(&I))
3901     Flags.setNonNeg(PNI->hasNonNeg());
3902 
3903   setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurSDLoc(), DestVT, N, Flags));
3904 }
3905 
3906 void SelectionDAGBuilder::visitSIToFP(const User &I) {
3907   // SIToFP is never a no-op cast, no need to check
3908   SDValue N = getValue(I.getOperand(0));
3909   EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3910                                                         I.getType());
3911   setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurSDLoc(), DestVT, N));
3912 }
3913 
3914 void SelectionDAGBuilder::visitPtrToInt(const User &I) {
3915   // What to do depends on the size of the integer and the size of the pointer.
3916   // We can either truncate, zero extend, or no-op, accordingly.
3917   SDValue N = getValue(I.getOperand(0));
3918   auto &TLI = DAG.getTargetLoweringInfo();
3919   EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3920                                                         I.getType());
3921   EVT PtrMemVT =
3922       TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
3923   N = DAG.getPtrExtOrTrunc(N, getCurSDLoc(), PtrMemVT);
3924   N = DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT);
3925   setValue(&I, N);
3926 }
3927 
3928 void SelectionDAGBuilder::visitIntToPtr(const User &I) {
3929   // What to do depends on the size of the integer and the size of the pointer.
3930   // We can either truncate, zero extend, or no-op, accordingly.
3931   SDValue N = getValue(I.getOperand(0));
3932   auto &TLI = DAG.getTargetLoweringInfo();
3933   EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
3934   EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), I.getType());
3935   N = DAG.getZExtOrTrunc(N, getCurSDLoc(), PtrMemVT);
3936   N = DAG.getPtrExtOrTrunc(N, getCurSDLoc(), DestVT);
3937   setValue(&I, N);
3938 }
3939 
3940 void SelectionDAGBuilder::visitBitCast(const User &I) {
3941   SDValue N = getValue(I.getOperand(0));
3942   SDLoc dl = getCurSDLoc();
3943   EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
3944                                                         I.getType());
3945 
3946   // BitCast assures us that source and destination are the same size so this is
3947   // either a BITCAST or a no-op.
3948   if (DestVT != N.getValueType())
3949     setValue(&I, DAG.getNode(ISD::BITCAST, dl,
3950                              DestVT, N)); // convert types.
3951   // Check if the original LLVM IR Operand was a ConstantInt, because getValue()
3952   // might fold any kind of constant expression to an integer constant and that
3953   // is not what we are looking for. Only recognize a bitcast of a genuine
3954   // constant integer as an opaque constant.
3955   else if(ConstantInt *C = dyn_cast<ConstantInt>(I.getOperand(0)))
3956     setValue(&I, DAG.getConstant(C->getValue(), dl, DestVT, /*isTarget=*/false,
3957                                  /*isOpaque*/true));
3958   else
3959     setValue(&I, N);            // noop cast.
3960 }
3961 
3962 void SelectionDAGBuilder::visitAddrSpaceCast(const User &I) {
3963   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3964   const Value *SV = I.getOperand(0);
3965   SDValue N = getValue(SV);
3966   EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
3967 
3968   unsigned SrcAS = SV->getType()->getPointerAddressSpace();
3969   unsigned DestAS = I.getType()->getPointerAddressSpace();
3970 
3971   if (!TM.isNoopAddrSpaceCast(SrcAS, DestAS))
3972     N = DAG.getAddrSpaceCast(getCurSDLoc(), DestVT, N, SrcAS, DestAS);
3973 
3974   setValue(&I, N);
3975 }
3976 
3977 void SelectionDAGBuilder::visitInsertElement(const User &I) {
3978   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3979   SDValue InVec = getValue(I.getOperand(0));
3980   SDValue InVal = getValue(I.getOperand(1));
3981   SDValue InIdx = DAG.getZExtOrTrunc(getValue(I.getOperand(2)), getCurSDLoc(),
3982                                      TLI.getVectorIdxTy(DAG.getDataLayout()));
3983   setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurSDLoc(),
3984                            TLI.getValueType(DAG.getDataLayout(), I.getType()),
3985                            InVec, InVal, InIdx));
3986 }
3987 
3988 void SelectionDAGBuilder::visitExtractElement(const User &I) {
3989   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3990   SDValue InVec = getValue(I.getOperand(0));
3991   SDValue InIdx = DAG.getZExtOrTrunc(getValue(I.getOperand(1)), getCurSDLoc(),
3992                                      TLI.getVectorIdxTy(DAG.getDataLayout()));
3993   setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurSDLoc(),
3994                            TLI.getValueType(DAG.getDataLayout(), I.getType()),
3995                            InVec, InIdx));
3996 }
3997 
3998 void SelectionDAGBuilder::visitShuffleVector(const User &I) {
3999   SDValue Src1 = getValue(I.getOperand(0));
4000   SDValue Src2 = getValue(I.getOperand(1));
4001   ArrayRef<int> Mask;
4002   if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I))
4003     Mask = SVI->getShuffleMask();
4004   else
4005     Mask = cast<ConstantExpr>(I).getShuffleMask();
4006   SDLoc DL = getCurSDLoc();
4007   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4008   EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
4009   EVT SrcVT = Src1.getValueType();
4010 
4011   if (all_of(Mask, [](int Elem) { return Elem == 0; }) &&
4012       VT.isScalableVector()) {
4013     // Canonical splat form of first element of first input vector.
4014     SDValue FirstElt =
4015         DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, SrcVT.getScalarType(), Src1,
4016                     DAG.getVectorIdxConstant(0, DL));
4017     setValue(&I, DAG.getNode(ISD::SPLAT_VECTOR, DL, VT, FirstElt));
4018     return;
4019   }
4020 
4021   // For now, we only handle splats for scalable vectors.
4022   // The DAGCombiner will perform a BUILD_VECTOR -> SPLAT_VECTOR transformation
4023   // for targets that support a SPLAT_VECTOR for non-scalable vector types.
4024   assert(!VT.isScalableVector() && "Unsupported scalable vector shuffle");
4025 
4026   unsigned SrcNumElts = SrcVT.getVectorNumElements();
4027   unsigned MaskNumElts = Mask.size();
4028 
4029   if (SrcNumElts == MaskNumElts) {
4030     setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, Mask));
4031     return;
4032   }
4033 
4034   // Normalize the shuffle vector since mask and vector length don't match.
4035   if (SrcNumElts < MaskNumElts) {
4036     // Mask is longer than the source vectors. We can use concatenate vector to
4037     // make the mask and vectors lengths match.
4038 
4039     if (MaskNumElts % SrcNumElts == 0) {
4040       // Mask length is a multiple of the source vector length.
4041       // Check if the shuffle is some kind of concatenation of the input
4042       // vectors.
4043       unsigned NumConcat = MaskNumElts / SrcNumElts;
4044       bool IsConcat = true;
4045       SmallVector<int, 8> ConcatSrcs(NumConcat, -1);
4046       for (unsigned i = 0; i != MaskNumElts; ++i) {
4047         int Idx = Mask[i];
4048         if (Idx < 0)
4049           continue;
4050         // Ensure the indices in each SrcVT sized piece are sequential and that
4051         // the same source is used for the whole piece.
4052         if ((Idx % SrcNumElts != (i % SrcNumElts)) ||
4053             (ConcatSrcs[i / SrcNumElts] >= 0 &&
4054              ConcatSrcs[i / SrcNumElts] != (int)(Idx / SrcNumElts))) {
4055           IsConcat = false;
4056           break;
4057         }
4058         // Remember which source this index came from.
4059         ConcatSrcs[i / SrcNumElts] = Idx / SrcNumElts;
4060       }
4061 
4062       // The shuffle is concatenating multiple vectors together. Just emit
4063       // a CONCAT_VECTORS operation.
4064       if (IsConcat) {
4065         SmallVector<SDValue, 8> ConcatOps;
4066         for (auto Src : ConcatSrcs) {
4067           if (Src < 0)
4068             ConcatOps.push_back(DAG.getUNDEF(SrcVT));
4069           else if (Src == 0)
4070             ConcatOps.push_back(Src1);
4071           else
4072             ConcatOps.push_back(Src2);
4073         }
4074         setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, ConcatOps));
4075         return;
4076       }
4077     }
4078 
4079     unsigned PaddedMaskNumElts = alignTo(MaskNumElts, SrcNumElts);
4080     unsigned NumConcat = PaddedMaskNumElts / SrcNumElts;
4081     EVT PaddedVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(),
4082                                     PaddedMaskNumElts);
4083 
4084     // Pad both vectors with undefs to make them the same length as the mask.
4085     SDValue UndefVal = DAG.getUNDEF(SrcVT);
4086 
4087     SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
4088     SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
4089     MOps1[0] = Src1;
4090     MOps2[0] = Src2;
4091 
4092     Src1 = DAG.getNode(ISD::CONCAT_VECTORS, DL, PaddedVT, MOps1);
4093     Src2 = DAG.getNode(ISD::CONCAT_VECTORS, DL, PaddedVT, MOps2);
4094 
4095     // Readjust mask for new input vector length.
4096     SmallVector<int, 8> MappedOps(PaddedMaskNumElts, -1);
4097     for (unsigned i = 0; i != MaskNumElts; ++i) {
4098       int Idx = Mask[i];
4099       if (Idx >= (int)SrcNumElts)
4100         Idx -= SrcNumElts - PaddedMaskNumElts;
4101       MappedOps[i] = Idx;
4102     }
4103 
4104     SDValue Result = DAG.getVectorShuffle(PaddedVT, DL, Src1, Src2, MappedOps);
4105 
4106     // If the concatenated vector was padded, extract a subvector with the
4107     // correct number of elements.
4108     if (MaskNumElts != PaddedMaskNumElts)
4109       Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Result,
4110                            DAG.getVectorIdxConstant(0, DL));
4111 
4112     setValue(&I, Result);
4113     return;
4114   }
4115 
4116   if (SrcNumElts > MaskNumElts) {
4117     // Analyze the access pattern of the vector to see if we can extract
4118     // two subvectors and do the shuffle.
4119     int StartIdx[2] = { -1, -1 };  // StartIdx to extract from
4120     bool CanExtract = true;
4121     for (int Idx : Mask) {
4122       unsigned Input = 0;
4123       if (Idx < 0)
4124         continue;
4125 
4126       if (Idx >= (int)SrcNumElts) {
4127         Input = 1;
4128         Idx -= SrcNumElts;
4129       }
4130 
4131       // If all the indices come from the same MaskNumElts sized portion of
4132       // the sources we can use extract. Also make sure the extract wouldn't
4133       // extract past the end of the source.
4134       int NewStartIdx = alignDown(Idx, MaskNumElts);
4135       if (NewStartIdx + MaskNumElts > SrcNumElts ||
4136           (StartIdx[Input] >= 0 && StartIdx[Input] != NewStartIdx))
4137         CanExtract = false;
4138       // Make sure we always update StartIdx as we use it to track if all
4139       // elements are undef.
4140       StartIdx[Input] = NewStartIdx;
4141     }
4142 
4143     if (StartIdx[0] < 0 && StartIdx[1] < 0) {
4144       setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
4145       return;
4146     }
4147     if (CanExtract) {
4148       // Extract appropriate subvector and generate a vector shuffle
4149       for (unsigned Input = 0; Input < 2; ++Input) {
4150         SDValue &Src = Input == 0 ? Src1 : Src2;
4151         if (StartIdx[Input] < 0)
4152           Src = DAG.getUNDEF(VT);
4153         else {
4154           Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Src,
4155                             DAG.getVectorIdxConstant(StartIdx[Input], DL));
4156         }
4157       }
4158 
4159       // Calculate new mask.
4160       SmallVector<int, 8> MappedOps(Mask);
4161       for (int &Idx : MappedOps) {
4162         if (Idx >= (int)SrcNumElts)
4163           Idx -= SrcNumElts + StartIdx[1] - MaskNumElts;
4164         else if (Idx >= 0)
4165           Idx -= StartIdx[0];
4166       }
4167 
4168       setValue(&I, DAG.getVectorShuffle(VT, DL, Src1, Src2, MappedOps));
4169       return;
4170     }
4171   }
4172 
4173   // We can't use either concat vectors or extract subvectors so fall back to
4174   // replacing the shuffle with extract and build vector.
4175   // to insert and build vector.
4176   EVT EltVT = VT.getVectorElementType();
4177   SmallVector<SDValue,8> Ops;
4178   for (int Idx : Mask) {
4179     SDValue Res;
4180 
4181     if (Idx < 0) {
4182       Res = DAG.getUNDEF(EltVT);
4183     } else {
4184       SDValue &Src = Idx < (int)SrcNumElts ? Src1 : Src2;
4185       if (Idx >= (int)SrcNumElts) Idx -= SrcNumElts;
4186 
4187       Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Src,
4188                         DAG.getVectorIdxConstant(Idx, DL));
4189     }
4190 
4191     Ops.push_back(Res);
4192   }
4193 
4194   setValue(&I, DAG.getBuildVector(VT, DL, Ops));
4195 }
4196 
4197 void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
4198   ArrayRef<unsigned> Indices = I.getIndices();
4199   const Value *Op0 = I.getOperand(0);
4200   const Value *Op1 = I.getOperand(1);
4201   Type *AggTy = I.getType();
4202   Type *ValTy = Op1->getType();
4203   bool IntoUndef = isa<UndefValue>(Op0);
4204   bool FromUndef = isa<UndefValue>(Op1);
4205 
4206   unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices);
4207 
4208   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4209   SmallVector<EVT, 4> AggValueVTs;
4210   ComputeValueVTs(TLI, DAG.getDataLayout(), AggTy, AggValueVTs);
4211   SmallVector<EVT, 4> ValValueVTs;
4212   ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs);
4213 
4214   unsigned NumAggValues = AggValueVTs.size();
4215   unsigned NumValValues = ValValueVTs.size();
4216   SmallVector<SDValue, 4> Values(NumAggValues);
4217 
4218   // Ignore an insertvalue that produces an empty object
4219   if (!NumAggValues) {
4220     setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
4221     return;
4222   }
4223 
4224   SDValue Agg = getValue(Op0);
4225   unsigned i = 0;
4226   // Copy the beginning value(s) from the original aggregate.
4227   for (; i != LinearIndex; ++i)
4228     Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4229                 SDValue(Agg.getNode(), Agg.getResNo() + i);
4230   // Copy values from the inserted value(s).
4231   if (NumValValues) {
4232     SDValue Val = getValue(Op1);
4233     for (; i != LinearIndex + NumValValues; ++i)
4234       Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4235                   SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
4236   }
4237   // Copy remaining value(s) from the original aggregate.
4238   for (; i != NumAggValues; ++i)
4239     Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
4240                 SDValue(Agg.getNode(), Agg.getResNo() + i);
4241 
4242   setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
4243                            DAG.getVTList(AggValueVTs), Values));
4244 }
4245 
4246 void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
4247   ArrayRef<unsigned> Indices = I.getIndices();
4248   const Value *Op0 = I.getOperand(0);
4249   Type *AggTy = Op0->getType();
4250   Type *ValTy = I.getType();
4251   bool OutOfUndef = isa<UndefValue>(Op0);
4252 
4253   unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices);
4254 
4255   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4256   SmallVector<EVT, 4> ValValueVTs;
4257   ComputeValueVTs(TLI, DAG.getDataLayout(), ValTy, ValValueVTs);
4258 
4259   unsigned NumValValues = ValValueVTs.size();
4260 
4261   // Ignore a extractvalue that produces an empty object
4262   if (!NumValValues) {
4263     setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
4264     return;
4265   }
4266 
4267   SmallVector<SDValue, 4> Values(NumValValues);
4268 
4269   SDValue Agg = getValue(Op0);
4270   // Copy out the selected value(s).
4271   for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
4272     Values[i - LinearIndex] =
4273       OutOfUndef ?
4274         DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
4275         SDValue(Agg.getNode(), Agg.getResNo() + i);
4276 
4277   setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
4278                            DAG.getVTList(ValValueVTs), Values));
4279 }
4280 
4281 void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
4282   Value *Op0 = I.getOperand(0);
4283   // Note that the pointer operand may be a vector of pointers. Take the scalar
4284   // element which holds a pointer.
4285   unsigned AS = Op0->getType()->getScalarType()->getPointerAddressSpace();
4286   SDValue N = getValue(Op0);
4287   SDLoc dl = getCurSDLoc();
4288   auto &TLI = DAG.getTargetLoweringInfo();
4289 
4290   // Normalize Vector GEP - all scalar operands should be converted to the
4291   // splat vector.
4292   bool IsVectorGEP = I.getType()->isVectorTy();
4293   ElementCount VectorElementCount =
4294       IsVectorGEP ? cast<VectorType>(I.getType())->getElementCount()
4295                   : ElementCount::getFixed(0);
4296 
4297   if (IsVectorGEP && !N.getValueType().isVector()) {
4298     LLVMContext &Context = *DAG.getContext();
4299     EVT VT = EVT::getVectorVT(Context, N.getValueType(), VectorElementCount);
4300     N = DAG.getSplat(VT, dl, N);
4301   }
4302 
4303   for (gep_type_iterator GTI = gep_type_begin(&I), E = gep_type_end(&I);
4304        GTI != E; ++GTI) {
4305     const Value *Idx = GTI.getOperand();
4306     if (StructType *StTy = GTI.getStructTypeOrNull()) {
4307       unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue();
4308       if (Field) {
4309         // N = N + Offset
4310         uint64_t Offset =
4311             DAG.getDataLayout().getStructLayout(StTy)->getElementOffset(Field);
4312 
4313         // In an inbounds GEP with an offset that is nonnegative even when
4314         // interpreted as signed, assume there is no unsigned overflow.
4315         SDNodeFlags Flags;
4316         if (int64_t(Offset) >= 0 && cast<GEPOperator>(I).isInBounds())
4317           Flags.setNoUnsignedWrap(true);
4318 
4319         N = DAG.getNode(ISD::ADD, dl, N.getValueType(), N,
4320                         DAG.getConstant(Offset, dl, N.getValueType()), Flags);
4321       }
4322     } else {
4323       // IdxSize is the width of the arithmetic according to IR semantics.
4324       // In SelectionDAG, we may prefer to do arithmetic in a wider bitwidth
4325       // (and fix up the result later).
4326       unsigned IdxSize = DAG.getDataLayout().getIndexSizeInBits(AS);
4327       MVT IdxTy = MVT::getIntegerVT(IdxSize);
4328       TypeSize ElementSize =
4329           GTI.getSequentialElementStride(DAG.getDataLayout());
4330       // We intentionally mask away the high bits here; ElementSize may not
4331       // fit in IdxTy.
4332       APInt ElementMul(IdxSize, ElementSize.getKnownMinValue());
4333       bool ElementScalable = ElementSize.isScalable();
4334 
4335       // If this is a scalar constant or a splat vector of constants,
4336       // handle it quickly.
4337       const auto *C = dyn_cast<Constant>(Idx);
4338       if (C && isa<VectorType>(C->getType()))
4339         C = C->getSplatValue();
4340 
4341       const auto *CI = dyn_cast_or_null<ConstantInt>(C);
4342       if (CI && CI->isZero())
4343         continue;
4344       if (CI && !ElementScalable) {
4345         APInt Offs = ElementMul * CI->getValue().sextOrTrunc(IdxSize);
4346         LLVMContext &Context = *DAG.getContext();
4347         SDValue OffsVal;
4348         if (IsVectorGEP)
4349           OffsVal = DAG.getConstant(
4350               Offs, dl, EVT::getVectorVT(Context, IdxTy, VectorElementCount));
4351         else
4352           OffsVal = DAG.getConstant(Offs, dl, IdxTy);
4353 
4354         // In an inbounds GEP with an offset that is nonnegative even when
4355         // interpreted as signed, assume there is no unsigned overflow.
4356         SDNodeFlags Flags;
4357         if (Offs.isNonNegative() && cast<GEPOperator>(I).isInBounds())
4358           Flags.setNoUnsignedWrap(true);
4359 
4360         OffsVal = DAG.getSExtOrTrunc(OffsVal, dl, N.getValueType());
4361 
4362         N = DAG.getNode(ISD::ADD, dl, N.getValueType(), N, OffsVal, Flags);
4363         continue;
4364       }
4365 
4366       // N = N + Idx * ElementMul;
4367       SDValue IdxN = getValue(Idx);
4368 
4369       if (!IdxN.getValueType().isVector() && IsVectorGEP) {
4370         EVT VT = EVT::getVectorVT(*Context, IdxN.getValueType(),
4371                                   VectorElementCount);
4372         IdxN = DAG.getSplat(VT, dl, IdxN);
4373       }
4374 
4375       // If the index is smaller or larger than intptr_t, truncate or extend
4376       // it.
4377       IdxN = DAG.getSExtOrTrunc(IdxN, dl, N.getValueType());
4378 
4379       if (ElementScalable) {
4380         EVT VScaleTy = N.getValueType().getScalarType();
4381         SDValue VScale = DAG.getNode(
4382             ISD::VSCALE, dl, VScaleTy,
4383             DAG.getConstant(ElementMul.getZExtValue(), dl, VScaleTy));
4384         if (IsVectorGEP)
4385           VScale = DAG.getSplatVector(N.getValueType(), dl, VScale);
4386         IdxN = DAG.getNode(ISD::MUL, dl, N.getValueType(), IdxN, VScale);
4387       } else {
4388         // If this is a multiply by a power of two, turn it into a shl
4389         // immediately.  This is a very common case.
4390         if (ElementMul != 1) {
4391           if (ElementMul.isPowerOf2()) {
4392             unsigned Amt = ElementMul.logBase2();
4393             IdxN = DAG.getNode(ISD::SHL, dl,
4394                                N.getValueType(), IdxN,
4395                                DAG.getConstant(Amt, dl, IdxN.getValueType()));
4396           } else {
4397             SDValue Scale = DAG.getConstant(ElementMul.getZExtValue(), dl,
4398                                             IdxN.getValueType());
4399             IdxN = DAG.getNode(ISD::MUL, dl,
4400                                N.getValueType(), IdxN, Scale);
4401           }
4402         }
4403       }
4404 
4405       N = DAG.getNode(ISD::ADD, dl,
4406                       N.getValueType(), N, IdxN);
4407     }
4408   }
4409 
4410   MVT PtrTy = TLI.getPointerTy(DAG.getDataLayout(), AS);
4411   MVT PtrMemTy = TLI.getPointerMemTy(DAG.getDataLayout(), AS);
4412   if (IsVectorGEP) {
4413     PtrTy = MVT::getVectorVT(PtrTy, VectorElementCount);
4414     PtrMemTy = MVT::getVectorVT(PtrMemTy, VectorElementCount);
4415   }
4416 
4417   if (PtrMemTy != PtrTy && !cast<GEPOperator>(I).isInBounds())
4418     N = DAG.getPtrExtendInReg(N, dl, PtrMemTy);
4419 
4420   setValue(&I, N);
4421 }
4422 
4423 void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
4424   // If this is a fixed sized alloca in the entry block of the function,
4425   // allocate it statically on the stack.
4426   if (FuncInfo.StaticAllocaMap.count(&I))
4427     return;   // getValue will auto-populate this.
4428 
4429   SDLoc dl = getCurSDLoc();
4430   Type *Ty = I.getAllocatedType();
4431   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4432   auto &DL = DAG.getDataLayout();
4433   TypeSize TySize = DL.getTypeAllocSize(Ty);
4434   MaybeAlign Alignment = std::max(DL.getPrefTypeAlign(Ty), I.getAlign());
4435 
4436   SDValue AllocSize = getValue(I.getArraySize());
4437 
4438   EVT IntPtr = TLI.getPointerTy(DL, I.getAddressSpace());
4439   if (AllocSize.getValueType() != IntPtr)
4440     AllocSize = DAG.getZExtOrTrunc(AllocSize, dl, IntPtr);
4441 
4442   if (TySize.isScalable())
4443     AllocSize = DAG.getNode(ISD::MUL, dl, IntPtr, AllocSize,
4444                             DAG.getVScale(dl, IntPtr,
4445                                           APInt(IntPtr.getScalarSizeInBits(),
4446                                                 TySize.getKnownMinValue())));
4447   else {
4448     SDValue TySizeValue =
4449         DAG.getConstant(TySize.getFixedValue(), dl, MVT::getIntegerVT(64));
4450     AllocSize = DAG.getNode(ISD::MUL, dl, IntPtr, AllocSize,
4451                             DAG.getZExtOrTrunc(TySizeValue, dl, IntPtr));
4452   }
4453 
4454   // Handle alignment.  If the requested alignment is less than or equal to
4455   // the stack alignment, ignore it.  If the size is greater than or equal to
4456   // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
4457   Align StackAlign = DAG.getSubtarget().getFrameLowering()->getStackAlign();
4458   if (*Alignment <= StackAlign)
4459     Alignment = std::nullopt;
4460 
4461   const uint64_t StackAlignMask = StackAlign.value() - 1U;
4462   // Round the size of the allocation up to the stack alignment size
4463   // by add SA-1 to the size. This doesn't overflow because we're computing
4464   // an address inside an alloca.
4465   SDNodeFlags Flags;
4466   Flags.setNoUnsignedWrap(true);
4467   AllocSize = DAG.getNode(ISD::ADD, dl, AllocSize.getValueType(), AllocSize,
4468                           DAG.getConstant(StackAlignMask, dl, IntPtr), Flags);
4469 
4470   // Mask out the low bits for alignment purposes.
4471   AllocSize = DAG.getNode(ISD::AND, dl, AllocSize.getValueType(), AllocSize,
4472                           DAG.getConstant(~StackAlignMask, dl, IntPtr));
4473 
4474   SDValue Ops[] = {
4475       getRoot(), AllocSize,
4476       DAG.getConstant(Alignment ? Alignment->value() : 0, dl, IntPtr)};
4477   SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
4478   SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, dl, VTs, Ops);
4479   setValue(&I, DSA);
4480   DAG.setRoot(DSA.getValue(1));
4481 
4482   assert(FuncInfo.MF->getFrameInfo().hasVarSizedObjects());
4483 }
4484 
4485 static const MDNode *getRangeMetadata(const Instruction &I) {
4486   // If !noundef is not present, then !range violation results in a poison
4487   // value rather than immediate undefined behavior. In theory, transferring
4488   // these annotations to SDAG is fine, but in practice there are key SDAG
4489   // transforms that are known not to be poison-safe, such as folding logical
4490   // and/or to bitwise and/or. For now, only transfer !range if !noundef is
4491   // also present.
4492   if (!I.hasMetadata(LLVMContext::MD_noundef))
4493     return nullptr;
4494   return I.getMetadata(LLVMContext::MD_range);
4495 }
4496 
4497 static std::optional<ConstantRange> getRange(const Instruction &I) {
4498   if (const auto *CB = dyn_cast<CallBase>(&I)) {
4499     // see comment in getRangeMetadata about this check
4500     if (CB->hasRetAttr(Attribute::NoUndef))
4501       return CB->getRange();
4502   }
4503   if (const MDNode *Range = getRangeMetadata(I))
4504     return getConstantRangeFromMetadata(*Range);
4505   return std::nullopt;
4506 }
4507 
4508 void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
4509   if (I.isAtomic())
4510     return visitAtomicLoad(I);
4511 
4512   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4513   const Value *SV = I.getOperand(0);
4514   if (TLI.supportSwiftError()) {
4515     // Swifterror values can come from either a function parameter with
4516     // swifterror attribute or an alloca with swifterror attribute.
4517     if (const Argument *Arg = dyn_cast<Argument>(SV)) {
4518       if (Arg->hasSwiftErrorAttr())
4519         return visitLoadFromSwiftError(I);
4520     }
4521 
4522     if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(SV)) {
4523       if (Alloca->isSwiftError())
4524         return visitLoadFromSwiftError(I);
4525     }
4526   }
4527 
4528   SDValue Ptr = getValue(SV);
4529 
4530   Type *Ty = I.getType();
4531   SmallVector<EVT, 4> ValueVTs, MemVTs;
4532   SmallVector<TypeSize, 4> Offsets;
4533   ComputeValueVTs(TLI, DAG.getDataLayout(), Ty, ValueVTs, &MemVTs, &Offsets);
4534   unsigned NumValues = ValueVTs.size();
4535   if (NumValues == 0)
4536     return;
4537 
4538   Align Alignment = I.getAlign();
4539   AAMDNodes AAInfo = I.getAAMetadata();
4540   const MDNode *Ranges = getRangeMetadata(I);
4541   bool isVolatile = I.isVolatile();
4542   MachineMemOperand::Flags MMOFlags =
4543       TLI.getLoadMemOperandFlags(I, DAG.getDataLayout(), AC, LibInfo);
4544 
4545   SDValue Root;
4546   bool ConstantMemory = false;
4547   if (isVolatile)
4548     // Serialize volatile loads with other side effects.
4549     Root = getRoot();
4550   else if (NumValues > MaxParallelChains)
4551     Root = getMemoryRoot();
4552   else if (AA &&
4553            AA->pointsToConstantMemory(MemoryLocation(
4554                SV,
4555                LocationSize::precise(DAG.getDataLayout().getTypeStoreSize(Ty)),
4556                AAInfo))) {
4557     // Do not serialize (non-volatile) loads of constant memory with anything.
4558     Root = DAG.getEntryNode();
4559     ConstantMemory = true;
4560     MMOFlags |= MachineMemOperand::MOInvariant;
4561   } else {
4562     // Do not serialize non-volatile loads against each other.
4563     Root = DAG.getRoot();
4564   }
4565 
4566   SDLoc dl = getCurSDLoc();
4567 
4568   if (isVolatile)
4569     Root = TLI.prepareVolatileOrAtomicLoad(Root, dl, DAG);
4570 
4571   SmallVector<SDValue, 4> Values(NumValues);
4572   SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues));
4573 
4574   unsigned ChainI = 0;
4575   for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
4576     // Serializing loads here may result in excessive register pressure, and
4577     // TokenFactor places arbitrary choke points on the scheduler. SD scheduling
4578     // could recover a bit by hoisting nodes upward in the chain by recognizing
4579     // they are side-effect free or do not alias. The optimizer should really
4580     // avoid this case by converting large object/array copies to llvm.memcpy
4581     // (MaxParallelChains should always remain as failsafe).
4582     if (ChainI == MaxParallelChains) {
4583       assert(PendingLoads.empty() && "PendingLoads must be serialized first");
4584       SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4585                                   ArrayRef(Chains.data(), ChainI));
4586       Root = Chain;
4587       ChainI = 0;
4588     }
4589 
4590     // TODO: MachinePointerInfo only supports a fixed length offset.
4591     MachinePointerInfo PtrInfo =
4592         !Offsets[i].isScalable() || Offsets[i].isZero()
4593             ? MachinePointerInfo(SV, Offsets[i].getKnownMinValue())
4594             : MachinePointerInfo();
4595 
4596     SDValue A = DAG.getObjectPtrOffset(dl, Ptr, Offsets[i]);
4597     SDValue L = DAG.getLoad(MemVTs[i], dl, Root, A, PtrInfo, Alignment,
4598                             MMOFlags, AAInfo, Ranges);
4599     Chains[ChainI] = L.getValue(1);
4600 
4601     if (MemVTs[i] != ValueVTs[i])
4602       L = DAG.getPtrExtOrTrunc(L, dl, ValueVTs[i]);
4603 
4604     Values[i] = L;
4605   }
4606 
4607   if (!ConstantMemory) {
4608     SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4609                                 ArrayRef(Chains.data(), ChainI));
4610     if (isVolatile)
4611       DAG.setRoot(Chain);
4612     else
4613       PendingLoads.push_back(Chain);
4614   }
4615 
4616   setValue(&I, DAG.getNode(ISD::MERGE_VALUES, dl,
4617                            DAG.getVTList(ValueVTs), Values));
4618 }
4619 
4620 void SelectionDAGBuilder::visitStoreToSwiftError(const StoreInst &I) {
4621   assert(DAG.getTargetLoweringInfo().supportSwiftError() &&
4622          "call visitStoreToSwiftError when backend supports swifterror");
4623 
4624   SmallVector<EVT, 4> ValueVTs;
4625   SmallVector<uint64_t, 4> Offsets;
4626   const Value *SrcV = I.getOperand(0);
4627   ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
4628                   SrcV->getType(), ValueVTs, &Offsets, 0);
4629   assert(ValueVTs.size() == 1 && Offsets[0] == 0 &&
4630          "expect a single EVT for swifterror");
4631 
4632   SDValue Src = getValue(SrcV);
4633   // Create a virtual register, then update the virtual register.
4634   Register VReg =
4635       SwiftError.getOrCreateVRegDefAt(&I, FuncInfo.MBB, I.getPointerOperand());
4636   // Chain, DL, Reg, N or Chain, DL, Reg, N, Glue
4637   // Chain can be getRoot or getControlRoot.
4638   SDValue CopyNode = DAG.getCopyToReg(getRoot(), getCurSDLoc(), VReg,
4639                                       SDValue(Src.getNode(), Src.getResNo()));
4640   DAG.setRoot(CopyNode);
4641 }
4642 
4643 void SelectionDAGBuilder::visitLoadFromSwiftError(const LoadInst &I) {
4644   assert(DAG.getTargetLoweringInfo().supportSwiftError() &&
4645          "call visitLoadFromSwiftError when backend supports swifterror");
4646 
4647   assert(!I.isVolatile() &&
4648          !I.hasMetadata(LLVMContext::MD_nontemporal) &&
4649          !I.hasMetadata(LLVMContext::MD_invariant_load) &&
4650          "Support volatile, non temporal, invariant for load_from_swift_error");
4651 
4652   const Value *SV = I.getOperand(0);
4653   Type *Ty = I.getType();
4654   assert(
4655       (!AA ||
4656        !AA->pointsToConstantMemory(MemoryLocation(
4657            SV, LocationSize::precise(DAG.getDataLayout().getTypeStoreSize(Ty)),
4658            I.getAAMetadata()))) &&
4659       "load_from_swift_error should not be constant memory");
4660 
4661   SmallVector<EVT, 4> ValueVTs;
4662   SmallVector<uint64_t, 4> Offsets;
4663   ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), Ty,
4664                   ValueVTs, &Offsets, 0);
4665   assert(ValueVTs.size() == 1 && Offsets[0] == 0 &&
4666          "expect a single EVT for swifterror");
4667 
4668   // Chain, DL, Reg, VT, Glue or Chain, DL, Reg, VT
4669   SDValue L = DAG.getCopyFromReg(
4670       getRoot(), getCurSDLoc(),
4671       SwiftError.getOrCreateVRegUseAt(&I, FuncInfo.MBB, SV), ValueVTs[0]);
4672 
4673   setValue(&I, L);
4674 }
4675 
4676 void SelectionDAGBuilder::visitStore(const StoreInst &I) {
4677   if (I.isAtomic())
4678     return visitAtomicStore(I);
4679 
4680   const Value *SrcV = I.getOperand(0);
4681   const Value *PtrV = I.getOperand(1);
4682 
4683   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4684   if (TLI.supportSwiftError()) {
4685     // Swifterror values can come from either a function parameter with
4686     // swifterror attribute or an alloca with swifterror attribute.
4687     if (const Argument *Arg = dyn_cast<Argument>(PtrV)) {
4688       if (Arg->hasSwiftErrorAttr())
4689         return visitStoreToSwiftError(I);
4690     }
4691 
4692     if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrV)) {
4693       if (Alloca->isSwiftError())
4694         return visitStoreToSwiftError(I);
4695     }
4696   }
4697 
4698   SmallVector<EVT, 4> ValueVTs, MemVTs;
4699   SmallVector<TypeSize, 4> Offsets;
4700   ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(),
4701                   SrcV->getType(), ValueVTs, &MemVTs, &Offsets);
4702   unsigned NumValues = ValueVTs.size();
4703   if (NumValues == 0)
4704     return;
4705 
4706   // Get the lowered operands. Note that we do this after
4707   // checking if NumResults is zero, because with zero results
4708   // the operands won't have values in the map.
4709   SDValue Src = getValue(SrcV);
4710   SDValue Ptr = getValue(PtrV);
4711 
4712   SDValue Root = I.isVolatile() ? getRoot() : getMemoryRoot();
4713   SmallVector<SDValue, 4> Chains(std::min(MaxParallelChains, NumValues));
4714   SDLoc dl = getCurSDLoc();
4715   Align Alignment = I.getAlign();
4716   AAMDNodes AAInfo = I.getAAMetadata();
4717 
4718   auto MMOFlags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout());
4719 
4720   unsigned ChainI = 0;
4721   for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
4722     // See visitLoad comments.
4723     if (ChainI == MaxParallelChains) {
4724       SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4725                                   ArrayRef(Chains.data(), ChainI));
4726       Root = Chain;
4727       ChainI = 0;
4728     }
4729 
4730     // TODO: MachinePointerInfo only supports a fixed length offset.
4731     MachinePointerInfo PtrInfo =
4732         !Offsets[i].isScalable() || Offsets[i].isZero()
4733             ? MachinePointerInfo(PtrV, Offsets[i].getKnownMinValue())
4734             : MachinePointerInfo();
4735 
4736     SDValue Add = DAG.getObjectPtrOffset(dl, Ptr, Offsets[i]);
4737     SDValue Val = SDValue(Src.getNode(), Src.getResNo() + i);
4738     if (MemVTs[i] != ValueVTs[i])
4739       Val = DAG.getPtrExtOrTrunc(Val, dl, MemVTs[i]);
4740     SDValue St =
4741         DAG.getStore(Root, dl, Val, Add, PtrInfo, Alignment, MMOFlags, AAInfo);
4742     Chains[ChainI] = St;
4743   }
4744 
4745   SDValue StoreNode = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
4746                                   ArrayRef(Chains.data(), ChainI));
4747   setValue(&I, StoreNode);
4748   DAG.setRoot(StoreNode);
4749 }
4750 
4751 void SelectionDAGBuilder::visitMaskedStore(const CallInst &I,
4752                                            bool IsCompressing) {
4753   SDLoc sdl = getCurSDLoc();
4754 
4755   auto getMaskedStoreOps = [&](Value *&Ptr, Value *&Mask, Value *&Src0,
4756                                Align &Alignment) {
4757     // llvm.masked.store.*(Src0, Ptr, alignment, Mask)
4758     Src0 = I.getArgOperand(0);
4759     Ptr = I.getArgOperand(1);
4760     Alignment = cast<ConstantInt>(I.getArgOperand(2))->getAlignValue();
4761     Mask = I.getArgOperand(3);
4762   };
4763   auto getCompressingStoreOps = [&](Value *&Ptr, Value *&Mask, Value *&Src0,
4764                                     Align &Alignment) {
4765     // llvm.masked.compressstore.*(Src0, Ptr, Mask)
4766     Src0 = I.getArgOperand(0);
4767     Ptr = I.getArgOperand(1);
4768     Mask = I.getArgOperand(2);
4769     Alignment = I.getParamAlign(1).valueOrOne();
4770   };
4771 
4772   Value  *PtrOperand, *MaskOperand, *Src0Operand;
4773   Align Alignment;
4774   if (IsCompressing)
4775     getCompressingStoreOps(PtrOperand, MaskOperand, Src0Operand, Alignment);
4776   else
4777     getMaskedStoreOps(PtrOperand, MaskOperand, Src0Operand, Alignment);
4778 
4779   SDValue Ptr = getValue(PtrOperand);
4780   SDValue Src0 = getValue(Src0Operand);
4781   SDValue Mask = getValue(MaskOperand);
4782   SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
4783 
4784   EVT VT = Src0.getValueType();
4785 
4786   auto MMOFlags = MachineMemOperand::MOStore;
4787   if (I.hasMetadata(LLVMContext::MD_nontemporal))
4788     MMOFlags |= MachineMemOperand::MONonTemporal;
4789 
4790   MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
4791       MachinePointerInfo(PtrOperand), MMOFlags,
4792       LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata());
4793 
4794   const auto &TLI = DAG.getTargetLoweringInfo();
4795   const auto &TTI =
4796       TLI.getTargetMachine().getTargetTransformInfo(*I.getFunction());
4797   SDValue StoreNode =
4798       !IsCompressing && TTI.hasConditionalLoadStoreForType(
4799                             I.getArgOperand(0)->getType()->getScalarType())
4800           ? TLI.visitMaskedStore(DAG, sdl, getMemoryRoot(), MMO, Ptr, Src0,
4801                                  Mask)
4802           : DAG.getMaskedStore(getMemoryRoot(), sdl, Src0, Ptr, Offset, Mask,
4803                                VT, MMO, ISD::UNINDEXED, /*Truncating=*/false,
4804                                IsCompressing);
4805   DAG.setRoot(StoreNode);
4806   setValue(&I, StoreNode);
4807 }
4808 
4809 // Get a uniform base for the Gather/Scatter intrinsic.
4810 // The first argument of the Gather/Scatter intrinsic is a vector of pointers.
4811 // We try to represent it as a base pointer + vector of indices.
4812 // Usually, the vector of pointers comes from a 'getelementptr' instruction.
4813 // The first operand of the GEP may be a single pointer or a vector of pointers
4814 // Example:
4815 //   %gep.ptr = getelementptr i32, <8 x i32*> %vptr, <8 x i32> %ind
4816 //  or
4817 //   %gep.ptr = getelementptr i32, i32* %ptr,        <8 x i32> %ind
4818 // %res = call <8 x i32> @llvm.masked.gather.v8i32(<8 x i32*> %gep.ptr, ..
4819 //
4820 // When the first GEP operand is a single pointer - it is the uniform base we
4821 // are looking for. If first operand of the GEP is a splat vector - we
4822 // extract the splat value and use it as a uniform base.
4823 // In all other cases the function returns 'false'.
4824 static bool getUniformBase(const Value *Ptr, SDValue &Base, SDValue &Index,
4825                            ISD::MemIndexType &IndexType, SDValue &Scale,
4826                            SelectionDAGBuilder *SDB, const BasicBlock *CurBB,
4827                            uint64_t ElemSize) {
4828   SelectionDAG& DAG = SDB->DAG;
4829   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4830   const DataLayout &DL = DAG.getDataLayout();
4831 
4832   assert(Ptr->getType()->isVectorTy() && "Unexpected pointer type");
4833 
4834   // Handle splat constant pointer.
4835   if (auto *C = dyn_cast<Constant>(Ptr)) {
4836     C = C->getSplatValue();
4837     if (!C)
4838       return false;
4839 
4840     Base = SDB->getValue(C);
4841 
4842     ElementCount NumElts = cast<VectorType>(Ptr->getType())->getElementCount();
4843     EVT VT = EVT::getVectorVT(*DAG.getContext(), TLI.getPointerTy(DL), NumElts);
4844     Index = DAG.getConstant(0, SDB->getCurSDLoc(), VT);
4845     IndexType = ISD::SIGNED_SCALED;
4846     Scale = DAG.getTargetConstant(1, SDB->getCurSDLoc(), TLI.getPointerTy(DL));
4847     return true;
4848   }
4849 
4850   const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr);
4851   if (!GEP || GEP->getParent() != CurBB)
4852     return false;
4853 
4854   if (GEP->getNumOperands() != 2)
4855     return false;
4856 
4857   const Value *BasePtr = GEP->getPointerOperand();
4858   const Value *IndexVal = GEP->getOperand(GEP->getNumOperands() - 1);
4859 
4860   // Make sure the base is scalar and the index is a vector.
4861   if (BasePtr->getType()->isVectorTy() || !IndexVal->getType()->isVectorTy())
4862     return false;
4863 
4864   TypeSize ScaleVal = DL.getTypeAllocSize(GEP->getResultElementType());
4865   if (ScaleVal.isScalable())
4866     return false;
4867 
4868   // Target may not support the required addressing mode.
4869   if (ScaleVal != 1 &&
4870       !TLI.isLegalScaleForGatherScatter(ScaleVal.getFixedValue(), ElemSize))
4871     return false;
4872 
4873   Base = SDB->getValue(BasePtr);
4874   Index = SDB->getValue(IndexVal);
4875   IndexType = ISD::SIGNED_SCALED;
4876 
4877   Scale =
4878       DAG.getTargetConstant(ScaleVal, SDB->getCurSDLoc(), TLI.getPointerTy(DL));
4879   return true;
4880 }
4881 
4882 void SelectionDAGBuilder::visitMaskedScatter(const CallInst &I) {
4883   SDLoc sdl = getCurSDLoc();
4884 
4885   // llvm.masked.scatter.*(Src0, Ptrs, alignment, Mask)
4886   const Value *Ptr = I.getArgOperand(1);
4887   SDValue Src0 = getValue(I.getArgOperand(0));
4888   SDValue Mask = getValue(I.getArgOperand(3));
4889   EVT VT = Src0.getValueType();
4890   Align Alignment = cast<ConstantInt>(I.getArgOperand(2))
4891                         ->getMaybeAlignValue()
4892                         .value_or(DAG.getEVTAlign(VT.getScalarType()));
4893   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4894 
4895   SDValue Base;
4896   SDValue Index;
4897   ISD::MemIndexType IndexType;
4898   SDValue Scale;
4899   bool UniformBase = getUniformBase(Ptr, Base, Index, IndexType, Scale, this,
4900                                     I.getParent(), VT.getScalarStoreSize());
4901 
4902   unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
4903   MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
4904       MachinePointerInfo(AS), MachineMemOperand::MOStore,
4905       LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata());
4906   if (!UniformBase) {
4907     Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
4908     Index = getValue(Ptr);
4909     IndexType = ISD::SIGNED_SCALED;
4910     Scale = DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
4911   }
4912 
4913   EVT IdxVT = Index.getValueType();
4914   EVT EltTy = IdxVT.getVectorElementType();
4915   if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
4916     EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
4917     Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
4918   }
4919 
4920   SDValue Ops[] = { getMemoryRoot(), Src0, Mask, Base, Index, Scale };
4921   SDValue Scatter = DAG.getMaskedScatter(DAG.getVTList(MVT::Other), VT, sdl,
4922                                          Ops, MMO, IndexType, false);
4923   DAG.setRoot(Scatter);
4924   setValue(&I, Scatter);
4925 }
4926 
4927 void SelectionDAGBuilder::visitMaskedLoad(const CallInst &I, bool IsExpanding) {
4928   SDLoc sdl = getCurSDLoc();
4929 
4930   auto getMaskedLoadOps = [&](Value *&Ptr, Value *&Mask, Value *&Src0,
4931                               Align &Alignment) {
4932     // @llvm.masked.load.*(Ptr, alignment, Mask, Src0)
4933     Ptr = I.getArgOperand(0);
4934     Alignment = cast<ConstantInt>(I.getArgOperand(1))->getAlignValue();
4935     Mask = I.getArgOperand(2);
4936     Src0 = I.getArgOperand(3);
4937   };
4938   auto getExpandingLoadOps = [&](Value *&Ptr, Value *&Mask, Value *&Src0,
4939                                  Align &Alignment) {
4940     // @llvm.masked.expandload.*(Ptr, Mask, Src0)
4941     Ptr = I.getArgOperand(0);
4942     Alignment = I.getParamAlign(0).valueOrOne();
4943     Mask = I.getArgOperand(1);
4944     Src0 = I.getArgOperand(2);
4945   };
4946 
4947   Value  *PtrOperand, *MaskOperand, *Src0Operand;
4948   Align Alignment;
4949   if (IsExpanding)
4950     getExpandingLoadOps(PtrOperand, MaskOperand, Src0Operand, Alignment);
4951   else
4952     getMaskedLoadOps(PtrOperand, MaskOperand, Src0Operand, Alignment);
4953 
4954   SDValue Ptr = getValue(PtrOperand);
4955   SDValue Src0 = getValue(Src0Operand);
4956   SDValue Mask = getValue(MaskOperand);
4957   SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
4958 
4959   EVT VT = Src0.getValueType();
4960   AAMDNodes AAInfo = I.getAAMetadata();
4961   const MDNode *Ranges = getRangeMetadata(I);
4962 
4963   // Do not serialize masked loads of constant memory with anything.
4964   MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
4965   bool AddToChain = !AA || !AA->pointsToConstantMemory(ML);
4966 
4967   SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
4968 
4969   auto MMOFlags = MachineMemOperand::MOLoad;
4970   if (I.hasMetadata(LLVMContext::MD_nontemporal))
4971     MMOFlags |= MachineMemOperand::MONonTemporal;
4972 
4973   MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
4974       MachinePointerInfo(PtrOperand), MMOFlags,
4975       LocationSize::beforeOrAfterPointer(), Alignment, AAInfo, Ranges);
4976 
4977   const auto &TLI = DAG.getTargetLoweringInfo();
4978   const auto &TTI =
4979       TLI.getTargetMachine().getTargetTransformInfo(*I.getFunction());
4980   // The Load/Res may point to different values and both of them are output
4981   // variables.
4982   SDValue Load;
4983   SDValue Res;
4984   if (!IsExpanding && TTI.hasConditionalLoadStoreForType(
4985                           Src0Operand->getType()->getScalarType()))
4986     Res = TLI.visitMaskedLoad(DAG, sdl, InChain, MMO, Load, Ptr, Src0, Mask);
4987   else
4988     Res = Load =
4989         DAG.getMaskedLoad(VT, sdl, InChain, Ptr, Offset, Mask, Src0, VT, MMO,
4990                           ISD::UNINDEXED, ISD::NON_EXTLOAD, IsExpanding);
4991   if (AddToChain)
4992     PendingLoads.push_back(Load.getValue(1));
4993   setValue(&I, Res);
4994 }
4995 
4996 void SelectionDAGBuilder::visitMaskedGather(const CallInst &I) {
4997   SDLoc sdl = getCurSDLoc();
4998 
4999   // @llvm.masked.gather.*(Ptrs, alignment, Mask, Src0)
5000   const Value *Ptr = I.getArgOperand(0);
5001   SDValue Src0 = getValue(I.getArgOperand(3));
5002   SDValue Mask = getValue(I.getArgOperand(2));
5003 
5004   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5005   EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
5006   Align Alignment = cast<ConstantInt>(I.getArgOperand(1))
5007                         ->getMaybeAlignValue()
5008                         .value_or(DAG.getEVTAlign(VT.getScalarType()));
5009 
5010   const MDNode *Ranges = getRangeMetadata(I);
5011 
5012   SDValue Root = DAG.getRoot();
5013   SDValue Base;
5014   SDValue Index;
5015   ISD::MemIndexType IndexType;
5016   SDValue Scale;
5017   bool UniformBase = getUniformBase(Ptr, Base, Index, IndexType, Scale, this,
5018                                     I.getParent(), VT.getScalarStoreSize());
5019   unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
5020   MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5021       MachinePointerInfo(AS), MachineMemOperand::MOLoad,
5022       LocationSize::beforeOrAfterPointer(), Alignment, I.getAAMetadata(),
5023       Ranges);
5024 
5025   if (!UniformBase) {
5026     Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5027     Index = getValue(Ptr);
5028     IndexType = ISD::SIGNED_SCALED;
5029     Scale = DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
5030   }
5031 
5032   EVT IdxVT = Index.getValueType();
5033   EVT EltTy = IdxVT.getVectorElementType();
5034   if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
5035     EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
5036     Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
5037   }
5038 
5039   SDValue Ops[] = { Root, Src0, Mask, Base, Index, Scale };
5040   SDValue Gather = DAG.getMaskedGather(DAG.getVTList(VT, MVT::Other), VT, sdl,
5041                                        Ops, MMO, IndexType, ISD::NON_EXTLOAD);
5042 
5043   PendingLoads.push_back(Gather.getValue(1));
5044   setValue(&I, Gather);
5045 }
5046 
5047 void SelectionDAGBuilder::visitAtomicCmpXchg(const AtomicCmpXchgInst &I) {
5048   SDLoc dl = getCurSDLoc();
5049   AtomicOrdering SuccessOrdering = I.getSuccessOrdering();
5050   AtomicOrdering FailureOrdering = I.getFailureOrdering();
5051   SyncScope::ID SSID = I.getSyncScopeID();
5052 
5053   SDValue InChain = getRoot();
5054 
5055   MVT MemVT = getValue(I.getCompareOperand()).getSimpleValueType();
5056   SDVTList VTs = DAG.getVTList(MemVT, MVT::i1, MVT::Other);
5057 
5058   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5059   auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout());
5060 
5061   MachineFunction &MF = DAG.getMachineFunction();
5062   MachineMemOperand *MMO = MF.getMachineMemOperand(
5063       MachinePointerInfo(I.getPointerOperand()), Flags,
5064       LocationSize::precise(MemVT.getStoreSize()), DAG.getEVTAlign(MemVT),
5065       AAMDNodes(), nullptr, SSID, SuccessOrdering, FailureOrdering);
5066 
5067   SDValue L = DAG.getAtomicCmpSwap(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS,
5068                                    dl, MemVT, VTs, InChain,
5069                                    getValue(I.getPointerOperand()),
5070                                    getValue(I.getCompareOperand()),
5071                                    getValue(I.getNewValOperand()), MMO);
5072 
5073   SDValue OutChain = L.getValue(2);
5074 
5075   setValue(&I, L);
5076   DAG.setRoot(OutChain);
5077 }
5078 
5079 void SelectionDAGBuilder::visitAtomicRMW(const AtomicRMWInst &I) {
5080   SDLoc dl = getCurSDLoc();
5081   ISD::NodeType NT;
5082   switch (I.getOperation()) {
5083   default: llvm_unreachable("Unknown atomicrmw operation");
5084   case AtomicRMWInst::Xchg: NT = ISD::ATOMIC_SWAP; break;
5085   case AtomicRMWInst::Add:  NT = ISD::ATOMIC_LOAD_ADD; break;
5086   case AtomicRMWInst::Sub:  NT = ISD::ATOMIC_LOAD_SUB; break;
5087   case AtomicRMWInst::And:  NT = ISD::ATOMIC_LOAD_AND; break;
5088   case AtomicRMWInst::Nand: NT = ISD::ATOMIC_LOAD_NAND; break;
5089   case AtomicRMWInst::Or:   NT = ISD::ATOMIC_LOAD_OR; break;
5090   case AtomicRMWInst::Xor:  NT = ISD::ATOMIC_LOAD_XOR; break;
5091   case AtomicRMWInst::Max:  NT = ISD::ATOMIC_LOAD_MAX; break;
5092   case AtomicRMWInst::Min:  NT = ISD::ATOMIC_LOAD_MIN; break;
5093   case AtomicRMWInst::UMax: NT = ISD::ATOMIC_LOAD_UMAX; break;
5094   case AtomicRMWInst::UMin: NT = ISD::ATOMIC_LOAD_UMIN; break;
5095   case AtomicRMWInst::FAdd: NT = ISD::ATOMIC_LOAD_FADD; break;
5096   case AtomicRMWInst::FSub: NT = ISD::ATOMIC_LOAD_FSUB; break;
5097   case AtomicRMWInst::FMax: NT = ISD::ATOMIC_LOAD_FMAX; break;
5098   case AtomicRMWInst::FMin: NT = ISD::ATOMIC_LOAD_FMIN; break;
5099   case AtomicRMWInst::UIncWrap:
5100     NT = ISD::ATOMIC_LOAD_UINC_WRAP;
5101     break;
5102   case AtomicRMWInst::UDecWrap:
5103     NT = ISD::ATOMIC_LOAD_UDEC_WRAP;
5104     break;
5105   }
5106   AtomicOrdering Ordering = I.getOrdering();
5107   SyncScope::ID SSID = I.getSyncScopeID();
5108 
5109   SDValue InChain = getRoot();
5110 
5111   auto MemVT = getValue(I.getValOperand()).getSimpleValueType();
5112   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5113   auto Flags = TLI.getAtomicMemOperandFlags(I, DAG.getDataLayout());
5114 
5115   MachineFunction &MF = DAG.getMachineFunction();
5116   MachineMemOperand *MMO = MF.getMachineMemOperand(
5117       MachinePointerInfo(I.getPointerOperand()), Flags,
5118       LocationSize::precise(MemVT.getStoreSize()), DAG.getEVTAlign(MemVT),
5119       AAMDNodes(), nullptr, SSID, Ordering);
5120 
5121   SDValue L =
5122     DAG.getAtomic(NT, dl, MemVT, InChain,
5123                   getValue(I.getPointerOperand()), getValue(I.getValOperand()),
5124                   MMO);
5125 
5126   SDValue OutChain = L.getValue(1);
5127 
5128   setValue(&I, L);
5129   DAG.setRoot(OutChain);
5130 }
5131 
5132 void SelectionDAGBuilder::visitFence(const FenceInst &I) {
5133   SDLoc dl = getCurSDLoc();
5134   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5135   SDValue Ops[3];
5136   Ops[0] = getRoot();
5137   Ops[1] = DAG.getTargetConstant((unsigned)I.getOrdering(), dl,
5138                                  TLI.getFenceOperandTy(DAG.getDataLayout()));
5139   Ops[2] = DAG.getTargetConstant(I.getSyncScopeID(), dl,
5140                                  TLI.getFenceOperandTy(DAG.getDataLayout()));
5141   SDValue N = DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops);
5142   setValue(&I, N);
5143   DAG.setRoot(N);
5144 }
5145 
5146 void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &I) {
5147   SDLoc dl = getCurSDLoc();
5148   AtomicOrdering Order = I.getOrdering();
5149   SyncScope::ID SSID = I.getSyncScopeID();
5150 
5151   SDValue InChain = getRoot();
5152 
5153   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5154   EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
5155   EVT MemVT = TLI.getMemValueType(DAG.getDataLayout(), I.getType());
5156 
5157   if (!TLI.supportsUnalignedAtomics() &&
5158       I.getAlign().value() < MemVT.getSizeInBits() / 8)
5159     report_fatal_error("Cannot generate unaligned atomic load");
5160 
5161   auto Flags = TLI.getLoadMemOperandFlags(I, DAG.getDataLayout(), AC, LibInfo);
5162 
5163   MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
5164       MachinePointerInfo(I.getPointerOperand()), Flags,
5165       LocationSize::precise(MemVT.getStoreSize()), I.getAlign(), AAMDNodes(),
5166       nullptr, SSID, Order);
5167 
5168   InChain = TLI.prepareVolatileOrAtomicLoad(InChain, dl, DAG);
5169 
5170   SDValue Ptr = getValue(I.getPointerOperand());
5171   SDValue L = DAG.getAtomic(ISD::ATOMIC_LOAD, dl, MemVT, MemVT, InChain,
5172                             Ptr, MMO);
5173 
5174   SDValue OutChain = L.getValue(1);
5175   if (MemVT != VT)
5176     L = DAG.getPtrExtOrTrunc(L, dl, VT);
5177 
5178   setValue(&I, L);
5179   DAG.setRoot(OutChain);
5180 }
5181 
5182 void SelectionDAGBuilder::visitAtomicStore(const StoreInst &I) {
5183   SDLoc dl = getCurSDLoc();
5184 
5185   AtomicOrdering Ordering = I.getOrdering();
5186   SyncScope::ID SSID = I.getSyncScopeID();
5187 
5188   SDValue InChain = getRoot();
5189 
5190   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5191   EVT MemVT =
5192       TLI.getMemValueType(DAG.getDataLayout(), I.getValueOperand()->getType());
5193 
5194   if (!TLI.supportsUnalignedAtomics() &&
5195       I.getAlign().value() < MemVT.getSizeInBits() / 8)
5196     report_fatal_error("Cannot generate unaligned atomic store");
5197 
5198   auto Flags = TLI.getStoreMemOperandFlags(I, DAG.getDataLayout());
5199 
5200   MachineFunction &MF = DAG.getMachineFunction();
5201   MachineMemOperand *MMO = MF.getMachineMemOperand(
5202       MachinePointerInfo(I.getPointerOperand()), Flags,
5203       LocationSize::precise(MemVT.getStoreSize()), I.getAlign(), AAMDNodes(),
5204       nullptr, SSID, Ordering);
5205 
5206   SDValue Val = getValue(I.getValueOperand());
5207   if (Val.getValueType() != MemVT)
5208     Val = DAG.getPtrExtOrTrunc(Val, dl, MemVT);
5209   SDValue Ptr = getValue(I.getPointerOperand());
5210 
5211   SDValue OutChain =
5212       DAG.getAtomic(ISD::ATOMIC_STORE, dl, MemVT, InChain, Val, Ptr, MMO);
5213 
5214   setValue(&I, OutChain);
5215   DAG.setRoot(OutChain);
5216 }
5217 
5218 /// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
5219 /// node.
5220 void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
5221                                                unsigned Intrinsic) {
5222   // Ignore the callsite's attributes. A specific call site may be marked with
5223   // readnone, but the lowering code will expect the chain based on the
5224   // definition.
5225   const Function *F = I.getCalledFunction();
5226   bool HasChain = !F->doesNotAccessMemory();
5227   bool OnlyLoad = HasChain && F->onlyReadsMemory();
5228 
5229   // Build the operand list.
5230   SmallVector<SDValue, 8> Ops;
5231   if (HasChain) {  // If this intrinsic has side-effects, chainify it.
5232     if (OnlyLoad) {
5233       // We don't need to serialize loads against other loads.
5234       Ops.push_back(DAG.getRoot());
5235     } else {
5236       Ops.push_back(getRoot());
5237     }
5238   }
5239 
5240   // Info is set by getTgtMemIntrinsic
5241   TargetLowering::IntrinsicInfo Info;
5242   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5243   bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I,
5244                                                DAG.getMachineFunction(),
5245                                                Intrinsic);
5246 
5247   // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
5248   if (!IsTgtIntrinsic || Info.opc == ISD::INTRINSIC_VOID ||
5249       Info.opc == ISD::INTRINSIC_W_CHAIN)
5250     Ops.push_back(DAG.getTargetConstant(Intrinsic, getCurSDLoc(),
5251                                         TLI.getPointerTy(DAG.getDataLayout())));
5252 
5253   // Add all operands of the call to the operand list.
5254   for (unsigned i = 0, e = I.arg_size(); i != e; ++i) {
5255     const Value *Arg = I.getArgOperand(i);
5256     if (!I.paramHasAttr(i, Attribute::ImmArg)) {
5257       Ops.push_back(getValue(Arg));
5258       continue;
5259     }
5260 
5261     // Use TargetConstant instead of a regular constant for immarg.
5262     EVT VT = TLI.getValueType(DAG.getDataLayout(), Arg->getType(), true);
5263     if (const ConstantInt *CI = dyn_cast<ConstantInt>(Arg)) {
5264       assert(CI->getBitWidth() <= 64 &&
5265              "large intrinsic immediates not handled");
5266       Ops.push_back(DAG.getTargetConstant(*CI, SDLoc(), VT));
5267     } else {
5268       Ops.push_back(
5269           DAG.getTargetConstantFP(*cast<ConstantFP>(Arg), SDLoc(), VT));
5270     }
5271   }
5272 
5273   SmallVector<EVT, 4> ValueVTs;
5274   ComputeValueVTs(TLI, DAG.getDataLayout(), I.getType(), ValueVTs);
5275 
5276   if (HasChain)
5277     ValueVTs.push_back(MVT::Other);
5278 
5279   SDVTList VTs = DAG.getVTList(ValueVTs);
5280 
5281   // Propagate fast-math-flags from IR to node(s).
5282   SDNodeFlags Flags;
5283   if (auto *FPMO = dyn_cast<FPMathOperator>(&I))
5284     Flags.copyFMF(*FPMO);
5285   SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
5286 
5287   // Create the node.
5288   SDValue Result;
5289 
5290   if (auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl)) {
5291     auto *Token = Bundle->Inputs[0].get();
5292     SDValue ConvControlToken = getValue(Token);
5293     assert(Ops.back().getValueType() != MVT::Glue &&
5294            "Did not expected another glue node here.");
5295     ConvControlToken =
5296         DAG.getNode(ISD::CONVERGENCECTRL_GLUE, {}, MVT::Glue, ConvControlToken);
5297     Ops.push_back(ConvControlToken);
5298   }
5299 
5300   // In some cases, custom collection of operands from CallInst I may be needed.
5301   TLI.CollectTargetIntrinsicOperands(I, Ops, DAG);
5302   if (IsTgtIntrinsic) {
5303     // This is target intrinsic that touches memory
5304     //
5305     // TODO: We currently just fallback to address space 0 if getTgtMemIntrinsic
5306     //       didn't yield anything useful.
5307     MachinePointerInfo MPI;
5308     if (Info.ptrVal)
5309       MPI = MachinePointerInfo(Info.ptrVal, Info.offset);
5310     else if (Info.fallbackAddressSpace)
5311       MPI = MachinePointerInfo(*Info.fallbackAddressSpace);
5312     Result = DAG.getMemIntrinsicNode(Info.opc, getCurSDLoc(), VTs, Ops,
5313                                      Info.memVT, MPI, Info.align, Info.flags,
5314                                      Info.size, I.getAAMetadata());
5315   } else if (!HasChain) {
5316     Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurSDLoc(), VTs, Ops);
5317   } else if (!I.getType()->isVoidTy()) {
5318     Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurSDLoc(), VTs, Ops);
5319   } else {
5320     Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(), VTs, Ops);
5321   }
5322 
5323   if (HasChain) {
5324     SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
5325     if (OnlyLoad)
5326       PendingLoads.push_back(Chain);
5327     else
5328       DAG.setRoot(Chain);
5329   }
5330 
5331   if (!I.getType()->isVoidTy()) {
5332     if (!isa<VectorType>(I.getType()))
5333       Result = lowerRangeToAssertZExt(DAG, I, Result);
5334 
5335     MaybeAlign Alignment = I.getRetAlign();
5336 
5337     // Insert `assertalign` node if there's an alignment.
5338     if (InsertAssertAlign && Alignment) {
5339       Result =
5340           DAG.getAssertAlign(getCurSDLoc(), Result, Alignment.valueOrOne());
5341     }
5342   }
5343 
5344   setValue(&I, Result);
5345 }
5346 
5347 /// GetSignificand - Get the significand and build it into a floating-point
5348 /// number with exponent of 1:
5349 ///
5350 ///   Op = (Op & 0x007fffff) | 0x3f800000;
5351 ///
5352 /// where Op is the hexadecimal representation of floating point value.
5353 static SDValue GetSignificand(SelectionDAG &DAG, SDValue Op, const SDLoc &dl) {
5354   SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
5355                            DAG.getConstant(0x007fffff, dl, MVT::i32));
5356   SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
5357                            DAG.getConstant(0x3f800000, dl, MVT::i32));
5358   return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2);
5359 }
5360 
5361 /// GetExponent - Get the exponent:
5362 ///
5363 ///   (float)(int)(((Op & 0x7f800000) >> 23) - 127);
5364 ///
5365 /// where Op is the hexadecimal representation of floating point value.
5366 static SDValue GetExponent(SelectionDAG &DAG, SDValue Op,
5367                            const TargetLowering &TLI, const SDLoc &dl) {
5368   SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
5369                            DAG.getConstant(0x7f800000, dl, MVT::i32));
5370   SDValue t1 = DAG.getNode(
5371       ISD::SRL, dl, MVT::i32, t0,
5372       DAG.getConstant(23, dl,
5373                       TLI.getShiftAmountTy(MVT::i32, DAG.getDataLayout())));
5374   SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
5375                            DAG.getConstant(127, dl, MVT::i32));
5376   return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
5377 }
5378 
5379 /// getF32Constant - Get 32-bit floating point constant.
5380 static SDValue getF32Constant(SelectionDAG &DAG, unsigned Flt,
5381                               const SDLoc &dl) {
5382   return DAG.getConstantFP(APFloat(APFloat::IEEEsingle(), APInt(32, Flt)), dl,
5383                            MVT::f32);
5384 }
5385 
5386 static SDValue getLimitedPrecisionExp2(SDValue t0, const SDLoc &dl,
5387                                        SelectionDAG &DAG) {
5388   // TODO: What fast-math-flags should be set on the floating-point nodes?
5389 
5390   //   IntegerPartOfX = ((int32_t)(t0);
5391   SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
5392 
5393   //   FractionalPartOfX = t0 - (float)IntegerPartOfX;
5394   SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
5395   SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
5396 
5397   //   IntegerPartOfX <<= 23;
5398   IntegerPartOfX =
5399       DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
5400                   DAG.getConstant(23, dl,
5401                                   DAG.getTargetLoweringInfo().getShiftAmountTy(
5402                                       MVT::i32, DAG.getDataLayout())));
5403 
5404   SDValue TwoToFractionalPartOfX;
5405   if (LimitFloatPrecision <= 6) {
5406     // For floating-point precision of 6:
5407     //
5408     //   TwoToFractionalPartOfX =
5409     //     0.997535578f +
5410     //       (0.735607626f + 0.252464424f * x) * x;
5411     //
5412     // error 0.0144103317, which is 6 bits
5413     SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5414                              getF32Constant(DAG, 0x3e814304, dl));
5415     SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5416                              getF32Constant(DAG, 0x3f3c50c8, dl));
5417     SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5418     TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5419                                          getF32Constant(DAG, 0x3f7f5e7e, dl));
5420   } else if (LimitFloatPrecision <= 12) {
5421     // For floating-point precision of 12:
5422     //
5423     //   TwoToFractionalPartOfX =
5424     //     0.999892986f +
5425     //       (0.696457318f +
5426     //         (0.224338339f + 0.792043434e-1f * x) * x) * x;
5427     //
5428     // error 0.000107046256, which is 13 to 14 bits
5429     SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5430                              getF32Constant(DAG, 0x3da235e3, dl));
5431     SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5432                              getF32Constant(DAG, 0x3e65b8f3, dl));
5433     SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5434     SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5435                              getF32Constant(DAG, 0x3f324b07, dl));
5436     SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5437     TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5438                                          getF32Constant(DAG, 0x3f7ff8fd, dl));
5439   } else { // LimitFloatPrecision <= 18
5440     // For floating-point precision of 18:
5441     //
5442     //   TwoToFractionalPartOfX =
5443     //     0.999999982f +
5444     //       (0.693148872f +
5445     //         (0.240227044f +
5446     //           (0.554906021e-1f +
5447     //             (0.961591928e-2f +
5448     //               (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
5449     // error 2.47208000*10^(-7), which is better than 18 bits
5450     SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5451                              getF32Constant(DAG, 0x3924b03e, dl));
5452     SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5453                              getF32Constant(DAG, 0x3ab24b87, dl));
5454     SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5455     SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5456                              getF32Constant(DAG, 0x3c1d8c17, dl));
5457     SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5458     SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5459                              getF32Constant(DAG, 0x3d634a1d, dl));
5460     SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5461     SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5462                              getF32Constant(DAG, 0x3e75fe14, dl));
5463     SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5464     SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
5465                               getF32Constant(DAG, 0x3f317234, dl));
5466     SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
5467     TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
5468                                          getF32Constant(DAG, 0x3f800000, dl));
5469   }
5470 
5471   // Add the exponent into the result in integer domain.
5472   SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, TwoToFractionalPartOfX);
5473   return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
5474                      DAG.getNode(ISD::ADD, dl, MVT::i32, t13, IntegerPartOfX));
5475 }
5476 
5477 /// expandExp - Lower an exp intrinsic. Handles the special sequences for
5478 /// limited-precision mode.
5479 static SDValue expandExp(const SDLoc &dl, SDValue Op, SelectionDAG &DAG,
5480                          const TargetLowering &TLI, SDNodeFlags Flags) {
5481   if (Op.getValueType() == MVT::f32 &&
5482       LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
5483 
5484     // Put the exponent in the right bit position for later addition to the
5485     // final result:
5486     //
5487     // t0 = Op * log2(e)
5488 
5489     // TODO: What fast-math-flags should be set here?
5490     SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
5491                              DAG.getConstantFP(numbers::log2ef, dl, MVT::f32));
5492     return getLimitedPrecisionExp2(t0, dl, DAG);
5493   }
5494 
5495   // No special expansion.
5496   return DAG.getNode(ISD::FEXP, dl, Op.getValueType(), Op, Flags);
5497 }
5498 
5499 /// expandLog - Lower a log intrinsic. Handles the special sequences for
5500 /// limited-precision mode.
5501 static SDValue expandLog(const SDLoc &dl, SDValue Op, SelectionDAG &DAG,
5502                          const TargetLowering &TLI, SDNodeFlags Flags) {
5503   // TODO: What fast-math-flags should be set on the floating-point nodes?
5504 
5505   if (Op.getValueType() == MVT::f32 &&
5506       LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
5507     SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5508 
5509     // Scale the exponent by log(2).
5510     SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
5511     SDValue LogOfExponent =
5512         DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
5513                     DAG.getConstantFP(numbers::ln2f, dl, MVT::f32));
5514 
5515     // Get the significand and build it into a floating-point number with
5516     // exponent of 1.
5517     SDValue X = GetSignificand(DAG, Op1, dl);
5518 
5519     SDValue LogOfMantissa;
5520     if (LimitFloatPrecision <= 6) {
5521       // For floating-point precision of 6:
5522       //
5523       //   LogofMantissa =
5524       //     -1.1609546f +
5525       //       (1.4034025f - 0.23903021f * x) * x;
5526       //
5527       // error 0.0034276066, which is better than 8 bits
5528       SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5529                                getF32Constant(DAG, 0xbe74c456, dl));
5530       SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5531                                getF32Constant(DAG, 0x3fb3a2b1, dl));
5532       SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5533       LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5534                                   getF32Constant(DAG, 0x3f949a29, dl));
5535     } else if (LimitFloatPrecision <= 12) {
5536       // For floating-point precision of 12:
5537       //
5538       //   LogOfMantissa =
5539       //     -1.7417939f +
5540       //       (2.8212026f +
5541       //         (-1.4699568f +
5542       //           (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
5543       //
5544       // error 0.000061011436, which is 14 bits
5545       SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5546                                getF32Constant(DAG, 0xbd67b6d6, dl));
5547       SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5548                                getF32Constant(DAG, 0x3ee4f4b8, dl));
5549       SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5550       SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5551                                getF32Constant(DAG, 0x3fbc278b, dl));
5552       SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5553       SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5554                                getF32Constant(DAG, 0x40348e95, dl));
5555       SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5556       LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5557                                   getF32Constant(DAG, 0x3fdef31a, dl));
5558     } else { // LimitFloatPrecision <= 18
5559       // For floating-point precision of 18:
5560       //
5561       //   LogOfMantissa =
5562       //     -2.1072184f +
5563       //       (4.2372794f +
5564       //         (-3.7029485f +
5565       //           (2.2781945f +
5566       //             (-0.87823314f +
5567       //               (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
5568       //
5569       // error 0.0000023660568, which is better than 18 bits
5570       SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5571                                getF32Constant(DAG, 0xbc91e5ac, dl));
5572       SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5573                                getF32Constant(DAG, 0x3e4350aa, dl));
5574       SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5575       SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5576                                getF32Constant(DAG, 0x3f60d3e3, dl));
5577       SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5578       SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5579                                getF32Constant(DAG, 0x4011cdf0, dl));
5580       SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5581       SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5582                                getF32Constant(DAG, 0x406cfd1c, dl));
5583       SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5584       SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5585                                getF32Constant(DAG, 0x408797cb, dl));
5586       SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5587       LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
5588                                   getF32Constant(DAG, 0x4006dcab, dl));
5589     }
5590 
5591     return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, LogOfMantissa);
5592   }
5593 
5594   // No special expansion.
5595   return DAG.getNode(ISD::FLOG, dl, Op.getValueType(), Op, Flags);
5596 }
5597 
5598 /// expandLog2 - Lower a log2 intrinsic. Handles the special sequences for
5599 /// limited-precision mode.
5600 static SDValue expandLog2(const SDLoc &dl, SDValue Op, SelectionDAG &DAG,
5601                           const TargetLowering &TLI, SDNodeFlags Flags) {
5602   // TODO: What fast-math-flags should be set on the floating-point nodes?
5603 
5604   if (Op.getValueType() == MVT::f32 &&
5605       LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
5606     SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5607 
5608     // Get the exponent.
5609     SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
5610 
5611     // Get the significand and build it into a floating-point number with
5612     // exponent of 1.
5613     SDValue X = GetSignificand(DAG, Op1, dl);
5614 
5615     // Different possible minimax approximations of significand in
5616     // floating-point for various degrees of accuracy over [1,2].
5617     SDValue Log2ofMantissa;
5618     if (LimitFloatPrecision <= 6) {
5619       // For floating-point precision of 6:
5620       //
5621       //   Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
5622       //
5623       // error 0.0049451742, which is more than 7 bits
5624       SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5625                                getF32Constant(DAG, 0xbeb08fe0, dl));
5626       SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5627                                getF32Constant(DAG, 0x40019463, dl));
5628       SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5629       Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5630                                    getF32Constant(DAG, 0x3fd6633d, dl));
5631     } else if (LimitFloatPrecision <= 12) {
5632       // For floating-point precision of 12:
5633       //
5634       //   Log2ofMantissa =
5635       //     -2.51285454f +
5636       //       (4.07009056f +
5637       //         (-2.12067489f +
5638       //           (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
5639       //
5640       // error 0.0000876136000, which is better than 13 bits
5641       SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5642                                getF32Constant(DAG, 0xbda7262e, dl));
5643       SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5644                                getF32Constant(DAG, 0x3f25280b, dl));
5645       SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5646       SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5647                                getF32Constant(DAG, 0x4007b923, dl));
5648       SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5649       SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5650                                getF32Constant(DAG, 0x40823e2f, dl));
5651       SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5652       Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5653                                    getF32Constant(DAG, 0x4020d29c, dl));
5654     } else { // LimitFloatPrecision <= 18
5655       // For floating-point precision of 18:
5656       //
5657       //   Log2ofMantissa =
5658       //     -3.0400495f +
5659       //       (6.1129976f +
5660       //         (-5.3420409f +
5661       //           (3.2865683f +
5662       //             (-1.2669343f +
5663       //               (0.27515199f -
5664       //                 0.25691327e-1f * x) * x) * x) * x) * x) * x;
5665       //
5666       // error 0.0000018516, which is better than 18 bits
5667       SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5668                                getF32Constant(DAG, 0xbcd2769e, dl));
5669       SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5670                                getF32Constant(DAG, 0x3e8ce0b9, dl));
5671       SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5672       SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5673                                getF32Constant(DAG, 0x3fa22ae7, dl));
5674       SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5675       SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
5676                                getF32Constant(DAG, 0x40525723, dl));
5677       SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5678       SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
5679                                getF32Constant(DAG, 0x40aaf200, dl));
5680       SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5681       SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
5682                                getF32Constant(DAG, 0x40c39dad, dl));
5683       SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
5684       Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
5685                                    getF32Constant(DAG, 0x4042902c, dl));
5686     }
5687 
5688     return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log2ofMantissa);
5689   }
5690 
5691   // No special expansion.
5692   return DAG.getNode(ISD::FLOG2, dl, Op.getValueType(), Op, Flags);
5693 }
5694 
5695 /// expandLog10 - Lower a log10 intrinsic. Handles the special sequences for
5696 /// limited-precision mode.
5697 static SDValue expandLog10(const SDLoc &dl, SDValue Op, SelectionDAG &DAG,
5698                            const TargetLowering &TLI, SDNodeFlags Flags) {
5699   // TODO: What fast-math-flags should be set on the floating-point nodes?
5700 
5701   if (Op.getValueType() == MVT::f32 &&
5702       LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
5703     SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
5704 
5705     // Scale the exponent by log10(2) [0.30102999f].
5706     SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
5707     SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
5708                                         getF32Constant(DAG, 0x3e9a209a, dl));
5709 
5710     // Get the significand and build it into a floating-point number with
5711     // exponent of 1.
5712     SDValue X = GetSignificand(DAG, Op1, dl);
5713 
5714     SDValue Log10ofMantissa;
5715     if (LimitFloatPrecision <= 6) {
5716       // For floating-point precision of 6:
5717       //
5718       //   Log10ofMantissa =
5719       //     -0.50419619f +
5720       //       (0.60948995f - 0.10380950f * x) * x;
5721       //
5722       // error 0.0014886165, which is 6 bits
5723       SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5724                                getF32Constant(DAG, 0xbdd49a13, dl));
5725       SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
5726                                getF32Constant(DAG, 0x3f1c0789, dl));
5727       SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5728       Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
5729                                     getF32Constant(DAG, 0x3f011300, dl));
5730     } else if (LimitFloatPrecision <= 12) {
5731       // For floating-point precision of 12:
5732       //
5733       //   Log10ofMantissa =
5734       //     -0.64831180f +
5735       //       (0.91751397f +
5736       //         (-0.31664806f + 0.47637168e-1f * x) * x) * x;
5737       //
5738       // error 0.00019228036, which is better than 12 bits
5739       SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5740                                getF32Constant(DAG, 0x3d431f31, dl));
5741       SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
5742                                getF32Constant(DAG, 0x3ea21fb2, dl));
5743       SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5744       SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5745                                getF32Constant(DAG, 0x3f6ae232, dl));
5746       SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5747       Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
5748                                     getF32Constant(DAG, 0x3f25f7c3, dl));
5749     } else { // LimitFloatPrecision <= 18
5750       // For floating-point precision of 18:
5751       //
5752       //   Log10ofMantissa =
5753       //     -0.84299375f +
5754       //       (1.5327582f +
5755       //         (-1.0688956f +
5756       //           (0.49102474f +
5757       //             (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
5758       //
5759       // error 0.0000037995730, which is better than 18 bits
5760       SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
5761                                getF32Constant(DAG, 0x3c5d51ce, dl));
5762       SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
5763                                getF32Constant(DAG, 0x3e00685a, dl));
5764       SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
5765       SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
5766                                getF32Constant(DAG, 0x3efb6798, dl));
5767       SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
5768       SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
5769                                getF32Constant(DAG, 0x3f88d192, dl));
5770       SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
5771       SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
5772                                getF32Constant(DAG, 0x3fc4316c, dl));
5773       SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
5774       Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
5775                                     getF32Constant(DAG, 0x3f57ce70, dl));
5776     }
5777 
5778     return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log10ofMantissa);
5779   }
5780 
5781   // No special expansion.
5782   return DAG.getNode(ISD::FLOG10, dl, Op.getValueType(), Op, Flags);
5783 }
5784 
5785 /// expandExp2 - Lower an exp2 intrinsic. Handles the special sequences for
5786 /// limited-precision mode.
5787 static SDValue expandExp2(const SDLoc &dl, SDValue Op, SelectionDAG &DAG,
5788                           const TargetLowering &TLI, SDNodeFlags Flags) {
5789   if (Op.getValueType() == MVT::f32 &&
5790       LimitFloatPrecision > 0 && LimitFloatPrecision <= 18)
5791     return getLimitedPrecisionExp2(Op, dl, DAG);
5792 
5793   // No special expansion.
5794   return DAG.getNode(ISD::FEXP2, dl, Op.getValueType(), Op, Flags);
5795 }
5796 
5797 /// visitPow - Lower a pow intrinsic. Handles the special sequences for
5798 /// limited-precision mode with x == 10.0f.
5799 static SDValue expandPow(const SDLoc &dl, SDValue LHS, SDValue RHS,
5800                          SelectionDAG &DAG, const TargetLowering &TLI,
5801                          SDNodeFlags Flags) {
5802   bool IsExp10 = false;
5803   if (LHS.getValueType() == MVT::f32 && RHS.getValueType() == MVT::f32 &&
5804       LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
5805     if (ConstantFPSDNode *LHSC = dyn_cast<ConstantFPSDNode>(LHS)) {
5806       APFloat Ten(10.0f);
5807       IsExp10 = LHSC->isExactlyValue(Ten);
5808     }
5809   }
5810 
5811   // TODO: What fast-math-flags should be set on the FMUL node?
5812   if (IsExp10) {
5813     // Put the exponent in the right bit position for later addition to the
5814     // final result:
5815     //
5816     //   #define LOG2OF10 3.3219281f
5817     //   t0 = Op * LOG2OF10;
5818     SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, RHS,
5819                              getF32Constant(DAG, 0x40549a78, dl));
5820     return getLimitedPrecisionExp2(t0, dl, DAG);
5821   }
5822 
5823   // No special expansion.
5824   return DAG.getNode(ISD::FPOW, dl, LHS.getValueType(), LHS, RHS, Flags);
5825 }
5826 
5827 /// ExpandPowI - Expand a llvm.powi intrinsic.
5828 static SDValue ExpandPowI(const SDLoc &DL, SDValue LHS, SDValue RHS,
5829                           SelectionDAG &DAG) {
5830   // If RHS is a constant, we can expand this out to a multiplication tree if
5831   // it's beneficial on the target, otherwise we end up lowering to a call to
5832   // __powidf2 (for example).
5833   if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
5834     unsigned Val = RHSC->getSExtValue();
5835 
5836     // powi(x, 0) -> 1.0
5837     if (Val == 0)
5838       return DAG.getConstantFP(1.0, DL, LHS.getValueType());
5839 
5840     if (DAG.getTargetLoweringInfo().isBeneficialToExpandPowI(
5841             Val, DAG.shouldOptForSize())) {
5842       // Get the exponent as a positive value.
5843       if ((int)Val < 0)
5844         Val = -Val;
5845       // We use the simple binary decomposition method to generate the multiply
5846       // sequence.  There are more optimal ways to do this (for example,
5847       // powi(x,15) generates one more multiply than it should), but this has
5848       // the benefit of being both really simple and much better than a libcall.
5849       SDValue Res; // Logically starts equal to 1.0
5850       SDValue CurSquare = LHS;
5851       // TODO: Intrinsics should have fast-math-flags that propagate to these
5852       // nodes.
5853       while (Val) {
5854         if (Val & 1) {
5855           if (Res.getNode())
5856             Res =
5857                 DAG.getNode(ISD::FMUL, DL, Res.getValueType(), Res, CurSquare);
5858           else
5859             Res = CurSquare; // 1.0*CurSquare.
5860         }
5861 
5862         CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
5863                                 CurSquare, CurSquare);
5864         Val >>= 1;
5865       }
5866 
5867       // If the original was negative, invert the result, producing 1/(x*x*x).
5868       if (RHSC->getSExtValue() < 0)
5869         Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
5870                           DAG.getConstantFP(1.0, DL, LHS.getValueType()), Res);
5871       return Res;
5872     }
5873   }
5874 
5875   // Otherwise, expand to a libcall.
5876   return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
5877 }
5878 
5879 static SDValue expandDivFix(unsigned Opcode, const SDLoc &DL,
5880                             SDValue LHS, SDValue RHS, SDValue Scale,
5881                             SelectionDAG &DAG, const TargetLowering &TLI) {
5882   EVT VT = LHS.getValueType();
5883   bool Signed = Opcode == ISD::SDIVFIX || Opcode == ISD::SDIVFIXSAT;
5884   bool Saturating = Opcode == ISD::SDIVFIXSAT || Opcode == ISD::UDIVFIXSAT;
5885   LLVMContext &Ctx = *DAG.getContext();
5886 
5887   // If the type is legal but the operation isn't, this node might survive all
5888   // the way to operation legalization. If we end up there and we do not have
5889   // the ability to widen the type (if VT*2 is not legal), we cannot expand the
5890   // node.
5891 
5892   // Coax the legalizer into expanding the node during type legalization instead
5893   // by bumping the size by one bit. This will force it to Promote, enabling the
5894   // early expansion and avoiding the need to expand later.
5895 
5896   // We don't have to do this if Scale is 0; that can always be expanded, unless
5897   // it's a saturating signed operation. Those can experience true integer
5898   // division overflow, a case which we must avoid.
5899 
5900   // FIXME: We wouldn't have to do this (or any of the early
5901   // expansion/promotion) if it was possible to expand a libcall of an
5902   // illegal type during operation legalization. But it's not, so things
5903   // get a bit hacky.
5904   unsigned ScaleInt = Scale->getAsZExtVal();
5905   if ((ScaleInt > 0 || (Saturating && Signed)) &&
5906       (TLI.isTypeLegal(VT) ||
5907        (VT.isVector() && TLI.isTypeLegal(VT.getVectorElementType())))) {
5908     TargetLowering::LegalizeAction Action = TLI.getFixedPointOperationAction(
5909         Opcode, VT, ScaleInt);
5910     if (Action != TargetLowering::Legal && Action != TargetLowering::Custom) {
5911       EVT PromVT;
5912       if (VT.isScalarInteger())
5913         PromVT = EVT::getIntegerVT(Ctx, VT.getSizeInBits() + 1);
5914       else if (VT.isVector()) {
5915         PromVT = VT.getVectorElementType();
5916         PromVT = EVT::getIntegerVT(Ctx, PromVT.getSizeInBits() + 1);
5917         PromVT = EVT::getVectorVT(Ctx, PromVT, VT.getVectorElementCount());
5918       } else
5919         llvm_unreachable("Wrong VT for DIVFIX?");
5920       LHS = DAG.getExtOrTrunc(Signed, LHS, DL, PromVT);
5921       RHS = DAG.getExtOrTrunc(Signed, RHS, DL, PromVT);
5922       EVT ShiftTy = TLI.getShiftAmountTy(PromVT, DAG.getDataLayout());
5923       // For saturating operations, we need to shift up the LHS to get the
5924       // proper saturation width, and then shift down again afterwards.
5925       if (Saturating)
5926         LHS = DAG.getNode(ISD::SHL, DL, PromVT, LHS,
5927                           DAG.getConstant(1, DL, ShiftTy));
5928       SDValue Res = DAG.getNode(Opcode, DL, PromVT, LHS, RHS, Scale);
5929       if (Saturating)
5930         Res = DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, PromVT, Res,
5931                           DAG.getConstant(1, DL, ShiftTy));
5932       return DAG.getZExtOrTrunc(Res, DL, VT);
5933     }
5934   }
5935 
5936   return DAG.getNode(Opcode, DL, VT, LHS, RHS, Scale);
5937 }
5938 
5939 // getUnderlyingArgRegs - Find underlying registers used for a truncated,
5940 // bitcasted, or split argument. Returns a list of <Register, size in bits>
5941 static void
5942 getUnderlyingArgRegs(SmallVectorImpl<std::pair<unsigned, TypeSize>> &Regs,
5943                      const SDValue &N) {
5944   switch (N.getOpcode()) {
5945   case ISD::CopyFromReg: {
5946     SDValue Op = N.getOperand(1);
5947     Regs.emplace_back(cast<RegisterSDNode>(Op)->getReg(),
5948                       Op.getValueType().getSizeInBits());
5949     return;
5950   }
5951   case ISD::BITCAST:
5952   case ISD::AssertZext:
5953   case ISD::AssertSext:
5954   case ISD::TRUNCATE:
5955     getUnderlyingArgRegs(Regs, N.getOperand(0));
5956     return;
5957   case ISD::BUILD_PAIR:
5958   case ISD::BUILD_VECTOR:
5959   case ISD::CONCAT_VECTORS:
5960     for (SDValue Op : N->op_values())
5961       getUnderlyingArgRegs(Regs, Op);
5962     return;
5963   default:
5964     return;
5965   }
5966 }
5967 
5968 /// If the DbgValueInst is a dbg_value of a function argument, create the
5969 /// corresponding DBG_VALUE machine instruction for it now.  At the end of
5970 /// instruction selection, they will be inserted to the entry BB.
5971 /// We don't currently support this for variadic dbg_values, as they shouldn't
5972 /// appear for function arguments or in the prologue.
5973 bool SelectionDAGBuilder::EmitFuncArgumentDbgValue(
5974     const Value *V, DILocalVariable *Variable, DIExpression *Expr,
5975     DILocation *DL, FuncArgumentDbgValueKind Kind, const SDValue &N) {
5976   const Argument *Arg = dyn_cast<Argument>(V);
5977   if (!Arg)
5978     return false;
5979 
5980   MachineFunction &MF = DAG.getMachineFunction();
5981   const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo();
5982 
5983   // Helper to create DBG_INSTR_REFs or DBG_VALUEs, depending on what kind
5984   // we've been asked to pursue.
5985   auto MakeVRegDbgValue = [&](Register Reg, DIExpression *FragExpr,
5986                               bool Indirect) {
5987     if (Reg.isVirtual() && MF.useDebugInstrRef()) {
5988       // For VRegs, in instruction referencing mode, create a DBG_INSTR_REF
5989       // pointing at the VReg, which will be patched up later.
5990       auto &Inst = TII->get(TargetOpcode::DBG_INSTR_REF);
5991       SmallVector<MachineOperand, 1> MOs({MachineOperand::CreateReg(
5992           /* Reg */ Reg, /* isDef */ false, /* isImp */ false,
5993           /* isKill */ false, /* isDead */ false,
5994           /* isUndef */ false, /* isEarlyClobber */ false,
5995           /* SubReg */ 0, /* isDebug */ true)});
5996 
5997       auto *NewDIExpr = FragExpr;
5998       // We don't have an "Indirect" field in DBG_INSTR_REF, fold that into
5999       // the DIExpression.
6000       if (Indirect)
6001         NewDIExpr = DIExpression::prepend(FragExpr, DIExpression::DerefBefore);
6002       SmallVector<uint64_t, 2> Ops({dwarf::DW_OP_LLVM_arg, 0});
6003       NewDIExpr = DIExpression::prependOpcodes(NewDIExpr, Ops);
6004       return BuildMI(MF, DL, Inst, false, MOs, Variable, NewDIExpr);
6005     } else {
6006       // Create a completely standard DBG_VALUE.
6007       auto &Inst = TII->get(TargetOpcode::DBG_VALUE);
6008       return BuildMI(MF, DL, Inst, Indirect, Reg, Variable, FragExpr);
6009     }
6010   };
6011 
6012   if (Kind == FuncArgumentDbgValueKind::Value) {
6013     // ArgDbgValues are hoisted to the beginning of the entry block. So we
6014     // should only emit as ArgDbgValue if the dbg.value intrinsic is found in
6015     // the entry block.
6016     bool IsInEntryBlock = FuncInfo.MBB == &FuncInfo.MF->front();
6017     if (!IsInEntryBlock)
6018       return false;
6019 
6020     // ArgDbgValues are hoisted to the beginning of the entry block.  So we
6021     // should only emit as ArgDbgValue if the dbg.value intrinsic describes a
6022     // variable that also is a param.
6023     //
6024     // Although, if we are at the top of the entry block already, we can still
6025     // emit using ArgDbgValue. This might catch some situations when the
6026     // dbg.value refers to an argument that isn't used in the entry block, so
6027     // any CopyToReg node would be optimized out and the only way to express
6028     // this DBG_VALUE is by using the physical reg (or FI) as done in this
6029     // method.  ArgDbgValues are hoisted to the beginning of the entry block. So
6030     // we should only emit as ArgDbgValue if the Variable is an argument to the
6031     // current function, and the dbg.value intrinsic is found in the entry
6032     // block.
6033     bool VariableIsFunctionInputArg = Variable->isParameter() &&
6034         !DL->getInlinedAt();
6035     bool IsInPrologue = SDNodeOrder == LowestSDNodeOrder;
6036     if (!IsInPrologue && !VariableIsFunctionInputArg)
6037       return false;
6038 
6039     // Here we assume that a function argument on IR level only can be used to
6040     // describe one input parameter on source level. If we for example have
6041     // source code like this
6042     //
6043     //    struct A { long x, y; };
6044     //    void foo(struct A a, long b) {
6045     //      ...
6046     //      b = a.x;
6047     //      ...
6048     //    }
6049     //
6050     // and IR like this
6051     //
6052     //  define void @foo(i32 %a1, i32 %a2, i32 %b)  {
6053     //  entry:
6054     //    call void @llvm.dbg.value(metadata i32 %a1, "a", DW_OP_LLVM_fragment
6055     //    call void @llvm.dbg.value(metadata i32 %a2, "a", DW_OP_LLVM_fragment
6056     //    call void @llvm.dbg.value(metadata i32 %b, "b",
6057     //    ...
6058     //    call void @llvm.dbg.value(metadata i32 %a1, "b"
6059     //    ...
6060     //
6061     // then the last dbg.value is describing a parameter "b" using a value that
6062     // is an argument. But since we already has used %a1 to describe a parameter
6063     // we should not handle that last dbg.value here (that would result in an
6064     // incorrect hoisting of the DBG_VALUE to the function entry).
6065     // Notice that we allow one dbg.value per IR level argument, to accommodate
6066     // for the situation with fragments above.
6067     // If there is no node for the value being handled, we return true to skip
6068     // the normal generation of debug info, as it would kill existing debug
6069     // info for the parameter in case of duplicates.
6070     if (VariableIsFunctionInputArg) {
6071       unsigned ArgNo = Arg->getArgNo();
6072       if (ArgNo >= FuncInfo.DescribedArgs.size())
6073         FuncInfo.DescribedArgs.resize(ArgNo + 1, false);
6074       else if (!IsInPrologue && FuncInfo.DescribedArgs.test(ArgNo))
6075         return !NodeMap[V].getNode();
6076       FuncInfo.DescribedArgs.set(ArgNo);
6077     }
6078   }
6079 
6080   bool IsIndirect = false;
6081   std::optional<MachineOperand> Op;
6082   // Some arguments' frame index is recorded during argument lowering.
6083   int FI = FuncInfo.getArgumentFrameIndex(Arg);
6084   if (FI != std::numeric_limits<int>::max())
6085     Op = MachineOperand::CreateFI(FI);
6086 
6087   SmallVector<std::pair<unsigned, TypeSize>, 8> ArgRegsAndSizes;
6088   if (!Op && N.getNode()) {
6089     getUnderlyingArgRegs(ArgRegsAndSizes, N);
6090     Register Reg;
6091     if (ArgRegsAndSizes.size() == 1)
6092       Reg = ArgRegsAndSizes.front().first;
6093 
6094     if (Reg && Reg.isVirtual()) {
6095       MachineRegisterInfo &RegInfo = MF.getRegInfo();
6096       Register PR = RegInfo.getLiveInPhysReg(Reg);
6097       if (PR)
6098         Reg = PR;
6099     }
6100     if (Reg) {
6101       Op = MachineOperand::CreateReg(Reg, false);
6102       IsIndirect = Kind != FuncArgumentDbgValueKind::Value;
6103     }
6104   }
6105 
6106   if (!Op && N.getNode()) {
6107     // Check if frame index is available.
6108     SDValue LCandidate = peekThroughBitcasts(N);
6109     if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(LCandidate.getNode()))
6110       if (FrameIndexSDNode *FINode =
6111           dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
6112         Op = MachineOperand::CreateFI(FINode->getIndex());
6113   }
6114 
6115   if (!Op) {
6116     // Create a DBG_VALUE for each decomposed value in ArgRegs to cover Reg
6117     auto splitMultiRegDbgValue = [&](ArrayRef<std::pair<unsigned, TypeSize>>
6118                                          SplitRegs) {
6119       unsigned Offset = 0;
6120       for (const auto &RegAndSize : SplitRegs) {
6121         // If the expression is already a fragment, the current register
6122         // offset+size might extend beyond the fragment. In this case, only
6123         // the register bits that are inside the fragment are relevant.
6124         int RegFragmentSizeInBits = RegAndSize.second;
6125         if (auto ExprFragmentInfo = Expr->getFragmentInfo()) {
6126           uint64_t ExprFragmentSizeInBits = ExprFragmentInfo->SizeInBits;
6127           // The register is entirely outside the expression fragment,
6128           // so is irrelevant for debug info.
6129           if (Offset >= ExprFragmentSizeInBits)
6130             break;
6131           // The register is partially outside the expression fragment, only
6132           // the low bits within the fragment are relevant for debug info.
6133           if (Offset + RegFragmentSizeInBits > ExprFragmentSizeInBits) {
6134             RegFragmentSizeInBits = ExprFragmentSizeInBits - Offset;
6135           }
6136         }
6137 
6138         auto FragmentExpr = DIExpression::createFragmentExpression(
6139             Expr, Offset, RegFragmentSizeInBits);
6140         Offset += RegAndSize.second;
6141         // If a valid fragment expression cannot be created, the variable's
6142         // correct value cannot be determined and so it is set as Undef.
6143         if (!FragmentExpr) {
6144           SDDbgValue *SDV = DAG.getConstantDbgValue(
6145               Variable, Expr, UndefValue::get(V->getType()), DL, SDNodeOrder);
6146           DAG.AddDbgValue(SDV, false);
6147           continue;
6148         }
6149         MachineInstr *NewMI =
6150             MakeVRegDbgValue(RegAndSize.first, *FragmentExpr,
6151                              Kind != FuncArgumentDbgValueKind::Value);
6152         FuncInfo.ArgDbgValues.push_back(NewMI);
6153       }
6154     };
6155 
6156     // Check if ValueMap has reg number.
6157     DenseMap<const Value *, Register>::const_iterator
6158       VMI = FuncInfo.ValueMap.find(V);
6159     if (VMI != FuncInfo.ValueMap.end()) {
6160       const auto &TLI = DAG.getTargetLoweringInfo();
6161       RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), VMI->second,
6162                        V->getType(), std::nullopt);
6163       if (RFV.occupiesMultipleRegs()) {
6164         splitMultiRegDbgValue(RFV.getRegsAndSizes());
6165         return true;
6166       }
6167 
6168       Op = MachineOperand::CreateReg(VMI->second, false);
6169       IsIndirect = Kind != FuncArgumentDbgValueKind::Value;
6170     } else if (ArgRegsAndSizes.size() > 1) {
6171       // This was split due to the calling convention, and no virtual register
6172       // mapping exists for the value.
6173       splitMultiRegDbgValue(ArgRegsAndSizes);
6174       return true;
6175     }
6176   }
6177 
6178   if (!Op)
6179     return false;
6180 
6181   assert(Variable->isValidLocationForIntrinsic(DL) &&
6182          "Expected inlined-at fields to agree");
6183   MachineInstr *NewMI = nullptr;
6184 
6185   if (Op->isReg())
6186     NewMI = MakeVRegDbgValue(Op->getReg(), Expr, IsIndirect);
6187   else
6188     NewMI = BuildMI(MF, DL, TII->get(TargetOpcode::DBG_VALUE), true, *Op,
6189                     Variable, Expr);
6190 
6191   // Otherwise, use ArgDbgValues.
6192   FuncInfo.ArgDbgValues.push_back(NewMI);
6193   return true;
6194 }
6195 
6196 /// Return the appropriate SDDbgValue based on N.
6197 SDDbgValue *SelectionDAGBuilder::getDbgValue(SDValue N,
6198                                              DILocalVariable *Variable,
6199                                              DIExpression *Expr,
6200                                              const DebugLoc &dl,
6201                                              unsigned DbgSDNodeOrder) {
6202   if (auto *FISDN = dyn_cast<FrameIndexSDNode>(N.getNode())) {
6203     // Construct a FrameIndexDbgValue for FrameIndexSDNodes so we can describe
6204     // stack slot locations.
6205     //
6206     // Consider "int x = 0; int *px = &x;". There are two kinds of interesting
6207     // debug values here after optimization:
6208     //
6209     //   dbg.value(i32* %px, !"int *px", !DIExpression()), and
6210     //   dbg.value(i32* %px, !"int x", !DIExpression(DW_OP_deref))
6211     //
6212     // Both describe the direct values of their associated variables.
6213     return DAG.getFrameIndexDbgValue(Variable, Expr, FISDN->getIndex(),
6214                                      /*IsIndirect*/ false, dl, DbgSDNodeOrder);
6215   }
6216   return DAG.getDbgValue(Variable, Expr, N.getNode(), N.getResNo(),
6217                          /*IsIndirect*/ false, dl, DbgSDNodeOrder);
6218 }
6219 
6220 static unsigned FixedPointIntrinsicToOpcode(unsigned Intrinsic) {
6221   switch (Intrinsic) {
6222   case Intrinsic::smul_fix:
6223     return ISD::SMULFIX;
6224   case Intrinsic::umul_fix:
6225     return ISD::UMULFIX;
6226   case Intrinsic::smul_fix_sat:
6227     return ISD::SMULFIXSAT;
6228   case Intrinsic::umul_fix_sat:
6229     return ISD::UMULFIXSAT;
6230   case Intrinsic::sdiv_fix:
6231     return ISD::SDIVFIX;
6232   case Intrinsic::udiv_fix:
6233     return ISD::UDIVFIX;
6234   case Intrinsic::sdiv_fix_sat:
6235     return ISD::SDIVFIXSAT;
6236   case Intrinsic::udiv_fix_sat:
6237     return ISD::UDIVFIXSAT;
6238   default:
6239     llvm_unreachable("Unhandled fixed point intrinsic");
6240   }
6241 }
6242 
6243 void SelectionDAGBuilder::lowerCallToExternalSymbol(const CallInst &I,
6244                                            const char *FunctionName) {
6245   assert(FunctionName && "FunctionName must not be nullptr");
6246   SDValue Callee = DAG.getExternalSymbol(
6247       FunctionName,
6248       DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()));
6249   LowerCallTo(I, Callee, I.isTailCall(), I.isMustTailCall());
6250 }
6251 
6252 /// Given a @llvm.call.preallocated.setup, return the corresponding
6253 /// preallocated call.
6254 static const CallBase *FindPreallocatedCall(const Value *PreallocatedSetup) {
6255   assert(cast<CallBase>(PreallocatedSetup)
6256                  ->getCalledFunction()
6257                  ->getIntrinsicID() == Intrinsic::call_preallocated_setup &&
6258          "expected call_preallocated_setup Value");
6259   for (const auto *U : PreallocatedSetup->users()) {
6260     auto *UseCall = cast<CallBase>(U);
6261     const Function *Fn = UseCall->getCalledFunction();
6262     if (!Fn || Fn->getIntrinsicID() != Intrinsic::call_preallocated_arg) {
6263       return UseCall;
6264     }
6265   }
6266   llvm_unreachable("expected corresponding call to preallocated setup/arg");
6267 }
6268 
6269 /// If DI is a debug value with an EntryValue expression, lower it using the
6270 /// corresponding physical register of the associated Argument value
6271 /// (guaranteed to exist by the verifier).
6272 bool SelectionDAGBuilder::visitEntryValueDbgValue(
6273     ArrayRef<const Value *> Values, DILocalVariable *Variable,
6274     DIExpression *Expr, DebugLoc DbgLoc) {
6275   if (!Expr->isEntryValue() || !hasSingleElement(Values))
6276     return false;
6277 
6278   // These properties are guaranteed by the verifier.
6279   const Argument *Arg = cast<Argument>(Values[0]);
6280   assert(Arg->hasAttribute(Attribute::AttrKind::SwiftAsync));
6281 
6282   auto ArgIt = FuncInfo.ValueMap.find(Arg);
6283   if (ArgIt == FuncInfo.ValueMap.end()) {
6284     LLVM_DEBUG(
6285         dbgs() << "Dropping dbg.value: expression is entry_value but "
6286                   "couldn't find an associated register for the Argument\n");
6287     return true;
6288   }
6289   Register ArgVReg = ArgIt->getSecond();
6290 
6291   for (auto [PhysReg, VirtReg] : FuncInfo.RegInfo->liveins())
6292     if (ArgVReg == VirtReg || ArgVReg == PhysReg) {
6293       SDDbgValue *SDV = DAG.getVRegDbgValue(
6294           Variable, Expr, PhysReg, false /*IsIndidrect*/, DbgLoc, SDNodeOrder);
6295       DAG.AddDbgValue(SDV, false /*treat as dbg.declare byval parameter*/);
6296       return true;
6297     }
6298   LLVM_DEBUG(dbgs() << "Dropping dbg.value: expression is entry_value but "
6299                        "couldn't find a physical register\n");
6300   return true;
6301 }
6302 
6303 /// Lower the call to the specified intrinsic function.
6304 void SelectionDAGBuilder::visitConvergenceControl(const CallInst &I,
6305                                                   unsigned Intrinsic) {
6306   SDLoc sdl = getCurSDLoc();
6307   switch (Intrinsic) {
6308   case Intrinsic::experimental_convergence_anchor:
6309     setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_ANCHOR, sdl, MVT::Untyped));
6310     break;
6311   case Intrinsic::experimental_convergence_entry:
6312     setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_ENTRY, sdl, MVT::Untyped));
6313     break;
6314   case Intrinsic::experimental_convergence_loop: {
6315     auto Bundle = I.getOperandBundle(LLVMContext::OB_convergencectrl);
6316     auto *Token = Bundle->Inputs[0].get();
6317     setValue(&I, DAG.getNode(ISD::CONVERGENCECTRL_LOOP, sdl, MVT::Untyped,
6318                              getValue(Token)));
6319     break;
6320   }
6321   }
6322 }
6323 
6324 void SelectionDAGBuilder::visitVectorHistogram(const CallInst &I,
6325                                                unsigned IntrinsicID) {
6326   // For now, we're only lowering an 'add' histogram.
6327   // We can add others later, e.g. saturating adds, min/max.
6328   assert(IntrinsicID == Intrinsic::experimental_vector_histogram_add &&
6329          "Tried to lower unsupported histogram type");
6330   SDLoc sdl = getCurSDLoc();
6331   Value *Ptr = I.getOperand(0);
6332   SDValue Inc = getValue(I.getOperand(1));
6333   SDValue Mask = getValue(I.getOperand(2));
6334 
6335   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6336   DataLayout TargetDL = DAG.getDataLayout();
6337   EVT VT = Inc.getValueType();
6338   Align Alignment = DAG.getEVTAlign(VT);
6339 
6340   const MDNode *Ranges = getRangeMetadata(I);
6341 
6342   SDValue Root = DAG.getRoot();
6343   SDValue Base;
6344   SDValue Index;
6345   ISD::MemIndexType IndexType;
6346   SDValue Scale;
6347   bool UniformBase = getUniformBase(Ptr, Base, Index, IndexType, Scale, this,
6348                                     I.getParent(), VT.getScalarStoreSize());
6349 
6350   unsigned AS = Ptr->getType()->getScalarType()->getPointerAddressSpace();
6351 
6352   MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
6353       MachinePointerInfo(AS),
6354       MachineMemOperand::MOLoad | MachineMemOperand::MOStore,
6355       MemoryLocation::UnknownSize, Alignment, I.getAAMetadata(), Ranges);
6356 
6357   if (!UniformBase) {
6358     Base = DAG.getConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout()));
6359     Index = getValue(Ptr);
6360     IndexType = ISD::SIGNED_SCALED;
6361     Scale =
6362         DAG.getTargetConstant(1, sdl, TLI.getPointerTy(DAG.getDataLayout()));
6363   }
6364 
6365   EVT IdxVT = Index.getValueType();
6366   EVT EltTy = IdxVT.getVectorElementType();
6367   if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
6368     EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
6369     Index = DAG.getNode(ISD::SIGN_EXTEND, sdl, NewIdxVT, Index);
6370   }
6371 
6372   SDValue ID = DAG.getTargetConstant(IntrinsicID, sdl, MVT::i32);
6373 
6374   SDValue Ops[] = {Root, Inc, Mask, Base, Index, Scale, ID};
6375   SDValue Histogram = DAG.getMaskedHistogram(DAG.getVTList(MVT::Other), VT, sdl,
6376                                              Ops, MMO, IndexType);
6377 
6378   setValue(&I, Histogram);
6379   DAG.setRoot(Histogram);
6380 }
6381 
6382 /// Lower the call to the specified intrinsic function.
6383 void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
6384                                              unsigned Intrinsic) {
6385   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6386   SDLoc sdl = getCurSDLoc();
6387   DebugLoc dl = getCurDebugLoc();
6388   SDValue Res;
6389 
6390   SDNodeFlags Flags;
6391   if (auto *FPOp = dyn_cast<FPMathOperator>(&I))
6392     Flags.copyFMF(*FPOp);
6393 
6394   switch (Intrinsic) {
6395   default:
6396     // By default, turn this into a target intrinsic node.
6397     visitTargetIntrinsic(I, Intrinsic);
6398     return;
6399   case Intrinsic::vscale: {
6400     EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6401     setValue(&I, DAG.getVScale(sdl, VT, APInt(VT.getSizeInBits(), 1)));
6402     return;
6403   }
6404   case Intrinsic::vastart:  visitVAStart(I); return;
6405   case Intrinsic::vaend:    visitVAEnd(I); return;
6406   case Intrinsic::vacopy:   visitVACopy(I); return;
6407   case Intrinsic::returnaddress:
6408     setValue(&I, DAG.getNode(ISD::RETURNADDR, sdl,
6409                              TLI.getValueType(DAG.getDataLayout(), I.getType()),
6410                              getValue(I.getArgOperand(0))));
6411     return;
6412   case Intrinsic::addressofreturnaddress:
6413     setValue(&I,
6414              DAG.getNode(ISD::ADDROFRETURNADDR, sdl,
6415                          TLI.getValueType(DAG.getDataLayout(), I.getType())));
6416     return;
6417   case Intrinsic::sponentry:
6418     setValue(&I,
6419              DAG.getNode(ISD::SPONENTRY, sdl,
6420                          TLI.getValueType(DAG.getDataLayout(), I.getType())));
6421     return;
6422   case Intrinsic::frameaddress:
6423     setValue(&I, DAG.getNode(ISD::FRAMEADDR, sdl,
6424                              TLI.getFrameIndexTy(DAG.getDataLayout()),
6425                              getValue(I.getArgOperand(0))));
6426     return;
6427   case Intrinsic::read_volatile_register:
6428   case Intrinsic::read_register: {
6429     Value *Reg = I.getArgOperand(0);
6430     SDValue Chain = getRoot();
6431     SDValue RegName =
6432         DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
6433     EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6434     Res = DAG.getNode(ISD::READ_REGISTER, sdl,
6435       DAG.getVTList(VT, MVT::Other), Chain, RegName);
6436     setValue(&I, Res);
6437     DAG.setRoot(Res.getValue(1));
6438     return;
6439   }
6440   case Intrinsic::write_register: {
6441     Value *Reg = I.getArgOperand(0);
6442     Value *RegValue = I.getArgOperand(1);
6443     SDValue Chain = getRoot();
6444     SDValue RegName =
6445         DAG.getMDNode(cast<MDNode>(cast<MetadataAsValue>(Reg)->getMetadata()));
6446     DAG.setRoot(DAG.getNode(ISD::WRITE_REGISTER, sdl, MVT::Other, Chain,
6447                             RegName, getValue(RegValue)));
6448     return;
6449   }
6450   case Intrinsic::memcpy: {
6451     const auto &MCI = cast<MemCpyInst>(I);
6452     SDValue Op1 = getValue(I.getArgOperand(0));
6453     SDValue Op2 = getValue(I.getArgOperand(1));
6454     SDValue Op3 = getValue(I.getArgOperand(2));
6455     // @llvm.memcpy defines 0 and 1 to both mean no alignment.
6456     Align DstAlign = MCI.getDestAlign().valueOrOne();
6457     Align SrcAlign = MCI.getSourceAlign().valueOrOne();
6458     Align Alignment = std::min(DstAlign, SrcAlign);
6459     bool isVol = MCI.isVolatile();
6460     bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6461     // FIXME: Support passing different dest/src alignments to the memcpy DAG
6462     // node.
6463     SDValue Root = isVol ? getRoot() : getMemoryRoot();
6464     SDValue MC = DAG.getMemcpy(
6465         Root, sdl, Op1, Op2, Op3, Alignment, isVol,
6466         /* AlwaysInline */ false, isTC, MachinePointerInfo(I.getArgOperand(0)),
6467         MachinePointerInfo(I.getArgOperand(1)), I.getAAMetadata(), AA);
6468     updateDAGForMaybeTailCall(MC);
6469     return;
6470   }
6471   case Intrinsic::memcpy_inline: {
6472     const auto &MCI = cast<MemCpyInlineInst>(I);
6473     SDValue Dst = getValue(I.getArgOperand(0));
6474     SDValue Src = getValue(I.getArgOperand(1));
6475     SDValue Size = getValue(I.getArgOperand(2));
6476     assert(isa<ConstantSDNode>(Size) && "memcpy_inline needs constant size");
6477     // @llvm.memcpy.inline defines 0 and 1 to both mean no alignment.
6478     Align DstAlign = MCI.getDestAlign().valueOrOne();
6479     Align SrcAlign = MCI.getSourceAlign().valueOrOne();
6480     Align Alignment = std::min(DstAlign, SrcAlign);
6481     bool isVol = MCI.isVolatile();
6482     bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6483     // FIXME: Support passing different dest/src alignments to the memcpy DAG
6484     // node.
6485     SDValue MC = DAG.getMemcpy(
6486         getRoot(), sdl, Dst, Src, Size, Alignment, isVol,
6487         /* AlwaysInline */ true, isTC, MachinePointerInfo(I.getArgOperand(0)),
6488         MachinePointerInfo(I.getArgOperand(1)), I.getAAMetadata(), AA);
6489     updateDAGForMaybeTailCall(MC);
6490     return;
6491   }
6492   case Intrinsic::memset: {
6493     const auto &MSI = cast<MemSetInst>(I);
6494     SDValue Op1 = getValue(I.getArgOperand(0));
6495     SDValue Op2 = getValue(I.getArgOperand(1));
6496     SDValue Op3 = getValue(I.getArgOperand(2));
6497     // @llvm.memset defines 0 and 1 to both mean no alignment.
6498     Align Alignment = MSI.getDestAlign().valueOrOne();
6499     bool isVol = MSI.isVolatile();
6500     bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6501     SDValue Root = isVol ? getRoot() : getMemoryRoot();
6502     SDValue MS = DAG.getMemset(
6503         Root, sdl, Op1, Op2, Op3, Alignment, isVol, /* AlwaysInline */ false,
6504         isTC, MachinePointerInfo(I.getArgOperand(0)), I.getAAMetadata());
6505     updateDAGForMaybeTailCall(MS);
6506     return;
6507   }
6508   case Intrinsic::memset_inline: {
6509     const auto &MSII = cast<MemSetInlineInst>(I);
6510     SDValue Dst = getValue(I.getArgOperand(0));
6511     SDValue Value = getValue(I.getArgOperand(1));
6512     SDValue Size = getValue(I.getArgOperand(2));
6513     assert(isa<ConstantSDNode>(Size) && "memset_inline needs constant size");
6514     // @llvm.memset defines 0 and 1 to both mean no alignment.
6515     Align DstAlign = MSII.getDestAlign().valueOrOne();
6516     bool isVol = MSII.isVolatile();
6517     bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6518     SDValue Root = isVol ? getRoot() : getMemoryRoot();
6519     SDValue MC = DAG.getMemset(Root, sdl, Dst, Value, Size, DstAlign, isVol,
6520                                /* AlwaysInline */ true, isTC,
6521                                MachinePointerInfo(I.getArgOperand(0)),
6522                                I.getAAMetadata());
6523     updateDAGForMaybeTailCall(MC);
6524     return;
6525   }
6526   case Intrinsic::memmove: {
6527     const auto &MMI = cast<MemMoveInst>(I);
6528     SDValue Op1 = getValue(I.getArgOperand(0));
6529     SDValue Op2 = getValue(I.getArgOperand(1));
6530     SDValue Op3 = getValue(I.getArgOperand(2));
6531     // @llvm.memmove defines 0 and 1 to both mean no alignment.
6532     Align DstAlign = MMI.getDestAlign().valueOrOne();
6533     Align SrcAlign = MMI.getSourceAlign().valueOrOne();
6534     Align Alignment = std::min(DstAlign, SrcAlign);
6535     bool isVol = MMI.isVolatile();
6536     bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6537     // FIXME: Support passing different dest/src alignments to the memmove DAG
6538     // node.
6539     SDValue Root = isVol ? getRoot() : getMemoryRoot();
6540     SDValue MM = DAG.getMemmove(Root, sdl, Op1, Op2, Op3, Alignment, isVol,
6541                                 isTC, MachinePointerInfo(I.getArgOperand(0)),
6542                                 MachinePointerInfo(I.getArgOperand(1)),
6543                                 I.getAAMetadata(), AA);
6544     updateDAGForMaybeTailCall(MM);
6545     return;
6546   }
6547   case Intrinsic::memcpy_element_unordered_atomic: {
6548     const AtomicMemCpyInst &MI = cast<AtomicMemCpyInst>(I);
6549     SDValue Dst = getValue(MI.getRawDest());
6550     SDValue Src = getValue(MI.getRawSource());
6551     SDValue Length = getValue(MI.getLength());
6552 
6553     Type *LengthTy = MI.getLength()->getType();
6554     unsigned ElemSz = MI.getElementSizeInBytes();
6555     bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6556     SDValue MC =
6557         DAG.getAtomicMemcpy(getRoot(), sdl, Dst, Src, Length, LengthTy, ElemSz,
6558                             isTC, MachinePointerInfo(MI.getRawDest()),
6559                             MachinePointerInfo(MI.getRawSource()));
6560     updateDAGForMaybeTailCall(MC);
6561     return;
6562   }
6563   case Intrinsic::memmove_element_unordered_atomic: {
6564     auto &MI = cast<AtomicMemMoveInst>(I);
6565     SDValue Dst = getValue(MI.getRawDest());
6566     SDValue Src = getValue(MI.getRawSource());
6567     SDValue Length = getValue(MI.getLength());
6568 
6569     Type *LengthTy = MI.getLength()->getType();
6570     unsigned ElemSz = MI.getElementSizeInBytes();
6571     bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6572     SDValue MC =
6573         DAG.getAtomicMemmove(getRoot(), sdl, Dst, Src, Length, LengthTy, ElemSz,
6574                              isTC, MachinePointerInfo(MI.getRawDest()),
6575                              MachinePointerInfo(MI.getRawSource()));
6576     updateDAGForMaybeTailCall(MC);
6577     return;
6578   }
6579   case Intrinsic::memset_element_unordered_atomic: {
6580     auto &MI = cast<AtomicMemSetInst>(I);
6581     SDValue Dst = getValue(MI.getRawDest());
6582     SDValue Val = getValue(MI.getValue());
6583     SDValue Length = getValue(MI.getLength());
6584 
6585     Type *LengthTy = MI.getLength()->getType();
6586     unsigned ElemSz = MI.getElementSizeInBytes();
6587     bool isTC = I.isTailCall() && isInTailCallPosition(I, DAG.getTarget());
6588     SDValue MC =
6589         DAG.getAtomicMemset(getRoot(), sdl, Dst, Val, Length, LengthTy, ElemSz,
6590                             isTC, MachinePointerInfo(MI.getRawDest()));
6591     updateDAGForMaybeTailCall(MC);
6592     return;
6593   }
6594   case Intrinsic::call_preallocated_setup: {
6595     const CallBase *PreallocatedCall = FindPreallocatedCall(&I);
6596     SDValue SrcValue = DAG.getSrcValue(PreallocatedCall);
6597     SDValue Res = DAG.getNode(ISD::PREALLOCATED_SETUP, sdl, MVT::Other,
6598                               getRoot(), SrcValue);
6599     setValue(&I, Res);
6600     DAG.setRoot(Res);
6601     return;
6602   }
6603   case Intrinsic::call_preallocated_arg: {
6604     const CallBase *PreallocatedCall = FindPreallocatedCall(I.getOperand(0));
6605     SDValue SrcValue = DAG.getSrcValue(PreallocatedCall);
6606     SDValue Ops[3];
6607     Ops[0] = getRoot();
6608     Ops[1] = SrcValue;
6609     Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl,
6610                                    MVT::i32); // arg index
6611     SDValue Res = DAG.getNode(
6612         ISD::PREALLOCATED_ARG, sdl,
6613         DAG.getVTList(TLI.getPointerTy(DAG.getDataLayout()), MVT::Other), Ops);
6614     setValue(&I, Res);
6615     DAG.setRoot(Res.getValue(1));
6616     return;
6617   }
6618   case Intrinsic::dbg_declare: {
6619     const auto &DI = cast<DbgDeclareInst>(I);
6620     // Debug intrinsics are handled separately in assignment tracking mode.
6621     // Some intrinsics are handled right after Argument lowering.
6622     if (AssignmentTrackingEnabled ||
6623         FuncInfo.PreprocessedDbgDeclares.count(&DI))
6624       return;
6625     LLVM_DEBUG(dbgs() << "SelectionDAG visiting dbg_declare: " << DI << "\n");
6626     DILocalVariable *Variable = DI.getVariable();
6627     DIExpression *Expression = DI.getExpression();
6628     dropDanglingDebugInfo(Variable, Expression);
6629     // Assume dbg.declare can not currently use DIArgList, i.e.
6630     // it is non-variadic.
6631     assert(!DI.hasArgList() && "Only dbg.value should currently use DIArgList");
6632     handleDebugDeclare(DI.getVariableLocationOp(0), Variable, Expression,
6633                        DI.getDebugLoc());
6634     return;
6635   }
6636   case Intrinsic::dbg_label: {
6637     const DbgLabelInst &DI = cast<DbgLabelInst>(I);
6638     DILabel *Label = DI.getLabel();
6639     assert(Label && "Missing label");
6640 
6641     SDDbgLabel *SDV;
6642     SDV = DAG.getDbgLabel(Label, dl, SDNodeOrder);
6643     DAG.AddDbgLabel(SDV);
6644     return;
6645   }
6646   case Intrinsic::dbg_assign: {
6647     // Debug intrinsics are handled separately in assignment tracking mode.
6648     if (AssignmentTrackingEnabled)
6649       return;
6650     // If assignment tracking hasn't been enabled then fall through and treat
6651     // the dbg.assign as a dbg.value.
6652     [[fallthrough]];
6653   }
6654   case Intrinsic::dbg_value: {
6655     // Debug intrinsics are handled separately in assignment tracking mode.
6656     if (AssignmentTrackingEnabled)
6657       return;
6658     const DbgValueInst &DI = cast<DbgValueInst>(I);
6659     assert(DI.getVariable() && "Missing variable");
6660 
6661     DILocalVariable *Variable = DI.getVariable();
6662     DIExpression *Expression = DI.getExpression();
6663     dropDanglingDebugInfo(Variable, Expression);
6664 
6665     if (DI.isKillLocation()) {
6666       handleKillDebugValue(Variable, Expression, DI.getDebugLoc(), SDNodeOrder);
6667       return;
6668     }
6669 
6670     SmallVector<Value *, 4> Values(DI.getValues());
6671     if (Values.empty())
6672       return;
6673 
6674     bool IsVariadic = DI.hasArgList();
6675     if (!handleDebugValue(Values, Variable, Expression, DI.getDebugLoc(),
6676                           SDNodeOrder, IsVariadic))
6677       addDanglingDebugInfo(Values, Variable, Expression, IsVariadic,
6678                            DI.getDebugLoc(), SDNodeOrder);
6679     return;
6680   }
6681 
6682   case Intrinsic::eh_typeid_for: {
6683     // Find the type id for the given typeinfo.
6684     GlobalValue *GV = ExtractTypeInfo(I.getArgOperand(0));
6685     unsigned TypeID = DAG.getMachineFunction().getTypeIDFor(GV);
6686     Res = DAG.getConstant(TypeID, sdl, MVT::i32);
6687     setValue(&I, Res);
6688     return;
6689   }
6690 
6691   case Intrinsic::eh_return_i32:
6692   case Intrinsic::eh_return_i64:
6693     DAG.getMachineFunction().setCallsEHReturn(true);
6694     DAG.setRoot(DAG.getNode(ISD::EH_RETURN, sdl,
6695                             MVT::Other,
6696                             getControlRoot(),
6697                             getValue(I.getArgOperand(0)),
6698                             getValue(I.getArgOperand(1))));
6699     return;
6700   case Intrinsic::eh_unwind_init:
6701     DAG.getMachineFunction().setCallsUnwindInit(true);
6702     return;
6703   case Intrinsic::eh_dwarf_cfa:
6704     setValue(&I, DAG.getNode(ISD::EH_DWARF_CFA, sdl,
6705                              TLI.getPointerTy(DAG.getDataLayout()),
6706                              getValue(I.getArgOperand(0))));
6707     return;
6708   case Intrinsic::eh_sjlj_callsite: {
6709     MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
6710     ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(0));
6711     assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!");
6712 
6713     MMI.setCurrentCallSite(CI->getZExtValue());
6714     return;
6715   }
6716   case Intrinsic::eh_sjlj_functioncontext: {
6717     // Get and store the index of the function context.
6718     MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
6719     AllocaInst *FnCtx =
6720       cast<AllocaInst>(I.getArgOperand(0)->stripPointerCasts());
6721     int FI = FuncInfo.StaticAllocaMap[FnCtx];
6722     MFI.setFunctionContextIndex(FI);
6723     return;
6724   }
6725   case Intrinsic::eh_sjlj_setjmp: {
6726     SDValue Ops[2];
6727     Ops[0] = getRoot();
6728     Ops[1] = getValue(I.getArgOperand(0));
6729     SDValue Op = DAG.getNode(ISD::EH_SJLJ_SETJMP, sdl,
6730                              DAG.getVTList(MVT::i32, MVT::Other), Ops);
6731     setValue(&I, Op.getValue(0));
6732     DAG.setRoot(Op.getValue(1));
6733     return;
6734   }
6735   case Intrinsic::eh_sjlj_longjmp:
6736     DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, sdl, MVT::Other,
6737                             getRoot(), getValue(I.getArgOperand(0))));
6738     return;
6739   case Intrinsic::eh_sjlj_setup_dispatch:
6740     DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_SETUP_DISPATCH, sdl, MVT::Other,
6741                             getRoot()));
6742     return;
6743   case Intrinsic::masked_gather:
6744     visitMaskedGather(I);
6745     return;
6746   case Intrinsic::masked_load:
6747     visitMaskedLoad(I);
6748     return;
6749   case Intrinsic::masked_scatter:
6750     visitMaskedScatter(I);
6751     return;
6752   case Intrinsic::masked_store:
6753     visitMaskedStore(I);
6754     return;
6755   case Intrinsic::masked_expandload:
6756     visitMaskedLoad(I, true /* IsExpanding */);
6757     return;
6758   case Intrinsic::masked_compressstore:
6759     visitMaskedStore(I, true /* IsCompressing */);
6760     return;
6761   case Intrinsic::powi:
6762     setValue(&I, ExpandPowI(sdl, getValue(I.getArgOperand(0)),
6763                             getValue(I.getArgOperand(1)), DAG));
6764     return;
6765   case Intrinsic::log:
6766     setValue(&I, expandLog(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6767     return;
6768   case Intrinsic::log2:
6769     setValue(&I,
6770              expandLog2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6771     return;
6772   case Intrinsic::log10:
6773     setValue(&I,
6774              expandLog10(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6775     return;
6776   case Intrinsic::exp:
6777     setValue(&I, expandExp(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6778     return;
6779   case Intrinsic::exp2:
6780     setValue(&I,
6781              expandExp2(sdl, getValue(I.getArgOperand(0)), DAG, TLI, Flags));
6782     return;
6783   case Intrinsic::pow:
6784     setValue(&I, expandPow(sdl, getValue(I.getArgOperand(0)),
6785                            getValue(I.getArgOperand(1)), DAG, TLI, Flags));
6786     return;
6787   case Intrinsic::sqrt:
6788   case Intrinsic::fabs:
6789   case Intrinsic::sin:
6790   case Intrinsic::cos:
6791   case Intrinsic::tan:
6792   case Intrinsic::exp10:
6793   case Intrinsic::floor:
6794   case Intrinsic::ceil:
6795   case Intrinsic::trunc:
6796   case Intrinsic::rint:
6797   case Intrinsic::nearbyint:
6798   case Intrinsic::round:
6799   case Intrinsic::roundeven:
6800   case Intrinsic::canonicalize: {
6801     unsigned Opcode;
6802     // clang-format off
6803     switch (Intrinsic) {
6804     default: llvm_unreachable("Impossible intrinsic");  // Can't reach here.
6805     case Intrinsic::sqrt:         Opcode = ISD::FSQRT;         break;
6806     case Intrinsic::fabs:         Opcode = ISD::FABS;          break;
6807     case Intrinsic::sin:          Opcode = ISD::FSIN;          break;
6808     case Intrinsic::cos:          Opcode = ISD::FCOS;          break;
6809     case Intrinsic::tan:          Opcode = ISD::FTAN;          break;
6810     case Intrinsic::exp10:        Opcode = ISD::FEXP10;        break;
6811     case Intrinsic::floor:        Opcode = ISD::FFLOOR;        break;
6812     case Intrinsic::ceil:         Opcode = ISD::FCEIL;         break;
6813     case Intrinsic::trunc:        Opcode = ISD::FTRUNC;        break;
6814     case Intrinsic::rint:         Opcode = ISD::FRINT;         break;
6815     case Intrinsic::nearbyint:    Opcode = ISD::FNEARBYINT;    break;
6816     case Intrinsic::round:        Opcode = ISD::FROUND;        break;
6817     case Intrinsic::roundeven:    Opcode = ISD::FROUNDEVEN;    break;
6818     case Intrinsic::canonicalize: Opcode = ISD::FCANONICALIZE; break;
6819     }
6820     // clang-format on
6821 
6822     setValue(&I, DAG.getNode(Opcode, sdl,
6823                              getValue(I.getArgOperand(0)).getValueType(),
6824                              getValue(I.getArgOperand(0)), Flags));
6825     return;
6826   }
6827   case Intrinsic::lround:
6828   case Intrinsic::llround:
6829   case Intrinsic::lrint:
6830   case Intrinsic::llrint: {
6831     unsigned Opcode;
6832     // clang-format off
6833     switch (Intrinsic) {
6834     default: llvm_unreachable("Impossible intrinsic");  // Can't reach here.
6835     case Intrinsic::lround:  Opcode = ISD::LROUND;  break;
6836     case Intrinsic::llround: Opcode = ISD::LLROUND; break;
6837     case Intrinsic::lrint:   Opcode = ISD::LRINT;   break;
6838     case Intrinsic::llrint:  Opcode = ISD::LLRINT;  break;
6839     }
6840     // clang-format on
6841 
6842     EVT RetVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6843     setValue(&I, DAG.getNode(Opcode, sdl, RetVT,
6844                              getValue(I.getArgOperand(0))));
6845     return;
6846   }
6847   case Intrinsic::minnum:
6848     setValue(&I, DAG.getNode(ISD::FMINNUM, sdl,
6849                              getValue(I.getArgOperand(0)).getValueType(),
6850                              getValue(I.getArgOperand(0)),
6851                              getValue(I.getArgOperand(1)), Flags));
6852     return;
6853   case Intrinsic::maxnum:
6854     setValue(&I, DAG.getNode(ISD::FMAXNUM, sdl,
6855                              getValue(I.getArgOperand(0)).getValueType(),
6856                              getValue(I.getArgOperand(0)),
6857                              getValue(I.getArgOperand(1)), Flags));
6858     return;
6859   case Intrinsic::minimum:
6860     setValue(&I, DAG.getNode(ISD::FMINIMUM, sdl,
6861                              getValue(I.getArgOperand(0)).getValueType(),
6862                              getValue(I.getArgOperand(0)),
6863                              getValue(I.getArgOperand(1)), Flags));
6864     return;
6865   case Intrinsic::maximum:
6866     setValue(&I, DAG.getNode(ISD::FMAXIMUM, sdl,
6867                              getValue(I.getArgOperand(0)).getValueType(),
6868                              getValue(I.getArgOperand(0)),
6869                              getValue(I.getArgOperand(1)), Flags));
6870     return;
6871   case Intrinsic::copysign:
6872     setValue(&I, DAG.getNode(ISD::FCOPYSIGN, sdl,
6873                              getValue(I.getArgOperand(0)).getValueType(),
6874                              getValue(I.getArgOperand(0)),
6875                              getValue(I.getArgOperand(1)), Flags));
6876     return;
6877   case Intrinsic::ldexp:
6878     setValue(&I, DAG.getNode(ISD::FLDEXP, sdl,
6879                              getValue(I.getArgOperand(0)).getValueType(),
6880                              getValue(I.getArgOperand(0)),
6881                              getValue(I.getArgOperand(1)), Flags));
6882     return;
6883   case Intrinsic::frexp: {
6884     SmallVector<EVT, 2> ValueVTs;
6885     ComputeValueVTs(TLI, DAG.getDataLayout(), I.getType(), ValueVTs);
6886     SDVTList VTs = DAG.getVTList(ValueVTs);
6887     setValue(&I,
6888              DAG.getNode(ISD::FFREXP, sdl, VTs, getValue(I.getArgOperand(0))));
6889     return;
6890   }
6891   case Intrinsic::arithmetic_fence: {
6892     setValue(&I, DAG.getNode(ISD::ARITH_FENCE, sdl,
6893                              getValue(I.getArgOperand(0)).getValueType(),
6894                              getValue(I.getArgOperand(0)), Flags));
6895     return;
6896   }
6897   case Intrinsic::fma:
6898     setValue(&I, DAG.getNode(
6899                      ISD::FMA, sdl, getValue(I.getArgOperand(0)).getValueType(),
6900                      getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)),
6901                      getValue(I.getArgOperand(2)), Flags));
6902     return;
6903 #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC)                         \
6904   case Intrinsic::INTRINSIC:
6905 #include "llvm/IR/ConstrainedOps.def"
6906     visitConstrainedFPIntrinsic(cast<ConstrainedFPIntrinsic>(I));
6907     return;
6908 #define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
6909 #include "llvm/IR/VPIntrinsics.def"
6910     visitVectorPredicationIntrinsic(cast<VPIntrinsic>(I));
6911     return;
6912   case Intrinsic::fptrunc_round: {
6913     // Get the last argument, the metadata and convert it to an integer in the
6914     // call
6915     Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(1))->getMetadata();
6916     std::optional<RoundingMode> RoundMode =
6917         convertStrToRoundingMode(cast<MDString>(MD)->getString());
6918 
6919     EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6920 
6921     // Propagate fast-math-flags from IR to node(s).
6922     SDNodeFlags Flags;
6923     Flags.copyFMF(*cast<FPMathOperator>(&I));
6924     SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
6925 
6926     SDValue Result;
6927     Result = DAG.getNode(
6928         ISD::FPTRUNC_ROUND, sdl, VT, getValue(I.getArgOperand(0)),
6929         DAG.getTargetConstant((int)*RoundMode, sdl,
6930                               TLI.getPointerTy(DAG.getDataLayout())));
6931     setValue(&I, Result);
6932 
6933     return;
6934   }
6935   case Intrinsic::fmuladd: {
6936     EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6937     if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
6938         TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) {
6939       setValue(&I, DAG.getNode(ISD::FMA, sdl,
6940                                getValue(I.getArgOperand(0)).getValueType(),
6941                                getValue(I.getArgOperand(0)),
6942                                getValue(I.getArgOperand(1)),
6943                                getValue(I.getArgOperand(2)), Flags));
6944     } else {
6945       // TODO: Intrinsic calls should have fast-math-flags.
6946       SDValue Mul = DAG.getNode(
6947           ISD::FMUL, sdl, getValue(I.getArgOperand(0)).getValueType(),
6948           getValue(I.getArgOperand(0)), getValue(I.getArgOperand(1)), Flags);
6949       SDValue Add = DAG.getNode(ISD::FADD, sdl,
6950                                 getValue(I.getArgOperand(0)).getValueType(),
6951                                 Mul, getValue(I.getArgOperand(2)), Flags);
6952       setValue(&I, Add);
6953     }
6954     return;
6955   }
6956   case Intrinsic::convert_to_fp16:
6957     setValue(&I, DAG.getNode(ISD::BITCAST, sdl, MVT::i16,
6958                              DAG.getNode(ISD::FP_ROUND, sdl, MVT::f16,
6959                                          getValue(I.getArgOperand(0)),
6960                                          DAG.getTargetConstant(0, sdl,
6961                                                                MVT::i32))));
6962     return;
6963   case Intrinsic::convert_from_fp16:
6964     setValue(&I, DAG.getNode(ISD::FP_EXTEND, sdl,
6965                              TLI.getValueType(DAG.getDataLayout(), I.getType()),
6966                              DAG.getNode(ISD::BITCAST, sdl, MVT::f16,
6967                                          getValue(I.getArgOperand(0)))));
6968     return;
6969   case Intrinsic::fptosi_sat: {
6970     EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6971     setValue(&I, DAG.getNode(ISD::FP_TO_SINT_SAT, sdl, VT,
6972                              getValue(I.getArgOperand(0)),
6973                              DAG.getValueType(VT.getScalarType())));
6974     return;
6975   }
6976   case Intrinsic::fptoui_sat: {
6977     EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
6978     setValue(&I, DAG.getNode(ISD::FP_TO_UINT_SAT, sdl, VT,
6979                              getValue(I.getArgOperand(0)),
6980                              DAG.getValueType(VT.getScalarType())));
6981     return;
6982   }
6983   case Intrinsic::set_rounding:
6984     Res = DAG.getNode(ISD::SET_ROUNDING, sdl, MVT::Other,
6985                       {getRoot(), getValue(I.getArgOperand(0))});
6986     setValue(&I, Res);
6987     DAG.setRoot(Res.getValue(0));
6988     return;
6989   case Intrinsic::is_fpclass: {
6990     const DataLayout DLayout = DAG.getDataLayout();
6991     EVT DestVT = TLI.getValueType(DLayout, I.getType());
6992     EVT ArgVT = TLI.getValueType(DLayout, I.getArgOperand(0)->getType());
6993     FPClassTest Test = static_cast<FPClassTest>(
6994         cast<ConstantInt>(I.getArgOperand(1))->getZExtValue());
6995     MachineFunction &MF = DAG.getMachineFunction();
6996     const Function &F = MF.getFunction();
6997     SDValue Op = getValue(I.getArgOperand(0));
6998     SDNodeFlags Flags;
6999     Flags.setNoFPExcept(
7000         !F.getAttributes().hasFnAttr(llvm::Attribute::StrictFP));
7001     // If ISD::IS_FPCLASS should be expanded, do it right now, because the
7002     // expansion can use illegal types. Making expansion early allows
7003     // legalizing these types prior to selection.
7004     if (!TLI.isOperationLegalOrCustom(ISD::IS_FPCLASS, ArgVT)) {
7005       SDValue Result = TLI.expandIS_FPCLASS(DestVT, Op, Test, Flags, sdl, DAG);
7006       setValue(&I, Result);
7007       return;
7008     }
7009 
7010     SDValue Check = DAG.getTargetConstant(Test, sdl, MVT::i32);
7011     SDValue V = DAG.getNode(ISD::IS_FPCLASS, sdl, DestVT, {Op, Check}, Flags);
7012     setValue(&I, V);
7013     return;
7014   }
7015   case Intrinsic::get_fpenv: {
7016     const DataLayout DLayout = DAG.getDataLayout();
7017     EVT EnvVT = TLI.getValueType(DLayout, I.getType());
7018     Align TempAlign = DAG.getEVTAlign(EnvVT);
7019     SDValue Chain = getRoot();
7020     // Use GET_FPENV if it is legal or custom. Otherwise use memory-based node
7021     // and temporary storage in stack.
7022     if (TLI.isOperationLegalOrCustom(ISD::GET_FPENV, EnvVT)) {
7023       Res = DAG.getNode(
7024           ISD::GET_FPENV, sdl,
7025           DAG.getVTList(TLI.getValueType(DAG.getDataLayout(), I.getType()),
7026                         MVT::Other),
7027           Chain);
7028     } else {
7029       SDValue Temp = DAG.CreateStackTemporary(EnvVT, TempAlign.value());
7030       int SPFI = cast<FrameIndexSDNode>(Temp.getNode())->getIndex();
7031       auto MPI =
7032           MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
7033       MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7034           MPI, MachineMemOperand::MOStore, LocationSize::beforeOrAfterPointer(),
7035           TempAlign);
7036       Chain = DAG.getGetFPEnv(Chain, sdl, Temp, EnvVT, MMO);
7037       Res = DAG.getLoad(EnvVT, sdl, Chain, Temp, MPI);
7038     }
7039     setValue(&I, Res);
7040     DAG.setRoot(Res.getValue(1));
7041     return;
7042   }
7043   case Intrinsic::set_fpenv: {
7044     const DataLayout DLayout = DAG.getDataLayout();
7045     SDValue Env = getValue(I.getArgOperand(0));
7046     EVT EnvVT = Env.getValueType();
7047     Align TempAlign = DAG.getEVTAlign(EnvVT);
7048     SDValue Chain = getRoot();
7049     // If SET_FPENV is custom or legal, use it. Otherwise use loading
7050     // environment from memory.
7051     if (TLI.isOperationLegalOrCustom(ISD::SET_FPENV, EnvVT)) {
7052       Chain = DAG.getNode(ISD::SET_FPENV, sdl, MVT::Other, Chain, Env);
7053     } else {
7054       // Allocate space in stack, copy environment bits into it and use this
7055       // memory in SET_FPENV_MEM.
7056       SDValue Temp = DAG.CreateStackTemporary(EnvVT, TempAlign.value());
7057       int SPFI = cast<FrameIndexSDNode>(Temp.getNode())->getIndex();
7058       auto MPI =
7059           MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
7060       Chain = DAG.getStore(Chain, sdl, Env, Temp, MPI, TempAlign,
7061                            MachineMemOperand::MOStore);
7062       MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
7063           MPI, MachineMemOperand::MOLoad, LocationSize::beforeOrAfterPointer(),
7064           TempAlign);
7065       Chain = DAG.getSetFPEnv(Chain, sdl, Temp, EnvVT, MMO);
7066     }
7067     DAG.setRoot(Chain);
7068     return;
7069   }
7070   case Intrinsic::reset_fpenv:
7071     DAG.setRoot(DAG.getNode(ISD::RESET_FPENV, sdl, MVT::Other, getRoot()));
7072     return;
7073   case Intrinsic::get_fpmode:
7074     Res = DAG.getNode(
7075         ISD::GET_FPMODE, sdl,
7076         DAG.getVTList(TLI.getValueType(DAG.getDataLayout(), I.getType()),
7077                       MVT::Other),
7078         DAG.getRoot());
7079     setValue(&I, Res);
7080     DAG.setRoot(Res.getValue(1));
7081     return;
7082   case Intrinsic::set_fpmode:
7083     Res = DAG.getNode(ISD::SET_FPMODE, sdl, MVT::Other, {DAG.getRoot()},
7084                       getValue(I.getArgOperand(0)));
7085     DAG.setRoot(Res);
7086     return;
7087   case Intrinsic::reset_fpmode: {
7088     Res = DAG.getNode(ISD::RESET_FPMODE, sdl, MVT::Other, getRoot());
7089     DAG.setRoot(Res);
7090     return;
7091   }
7092   case Intrinsic::pcmarker: {
7093     SDValue Tmp = getValue(I.getArgOperand(0));
7094     DAG.setRoot(DAG.getNode(ISD::PCMARKER, sdl, MVT::Other, getRoot(), Tmp));
7095     return;
7096   }
7097   case Intrinsic::readcyclecounter: {
7098     SDValue Op = getRoot();
7099     Res = DAG.getNode(ISD::READCYCLECOUNTER, sdl,
7100                       DAG.getVTList(MVT::i64, MVT::Other), Op);
7101     setValue(&I, Res);
7102     DAG.setRoot(Res.getValue(1));
7103     return;
7104   }
7105   case Intrinsic::readsteadycounter: {
7106     SDValue Op = getRoot();
7107     Res = DAG.getNode(ISD::READSTEADYCOUNTER, sdl,
7108                       DAG.getVTList(MVT::i64, MVT::Other), Op);
7109     setValue(&I, Res);
7110     DAG.setRoot(Res.getValue(1));
7111     return;
7112   }
7113   case Intrinsic::bitreverse:
7114     setValue(&I, DAG.getNode(ISD::BITREVERSE, sdl,
7115                              getValue(I.getArgOperand(0)).getValueType(),
7116                              getValue(I.getArgOperand(0))));
7117     return;
7118   case Intrinsic::bswap:
7119     setValue(&I, DAG.getNode(ISD::BSWAP, sdl,
7120                              getValue(I.getArgOperand(0)).getValueType(),
7121                              getValue(I.getArgOperand(0))));
7122     return;
7123   case Intrinsic::cttz: {
7124     SDValue Arg = getValue(I.getArgOperand(0));
7125     ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
7126     EVT Ty = Arg.getValueType();
7127     setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTTZ : ISD::CTTZ_ZERO_UNDEF,
7128                              sdl, Ty, Arg));
7129     return;
7130   }
7131   case Intrinsic::ctlz: {
7132     SDValue Arg = getValue(I.getArgOperand(0));
7133     ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
7134     EVT Ty = Arg.getValueType();
7135     setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTLZ : ISD::CTLZ_ZERO_UNDEF,
7136                              sdl, Ty, Arg));
7137     return;
7138   }
7139   case Intrinsic::ctpop: {
7140     SDValue Arg = getValue(I.getArgOperand(0));
7141     EVT Ty = Arg.getValueType();
7142     setValue(&I, DAG.getNode(ISD::CTPOP, sdl, Ty, Arg));
7143     return;
7144   }
7145   case Intrinsic::fshl:
7146   case Intrinsic::fshr: {
7147     bool IsFSHL = Intrinsic == Intrinsic::fshl;
7148     SDValue X = getValue(I.getArgOperand(0));
7149     SDValue Y = getValue(I.getArgOperand(1));
7150     SDValue Z = getValue(I.getArgOperand(2));
7151     EVT VT = X.getValueType();
7152 
7153     if (X == Y) {
7154       auto RotateOpcode = IsFSHL ? ISD::ROTL : ISD::ROTR;
7155       setValue(&I, DAG.getNode(RotateOpcode, sdl, VT, X, Z));
7156     } else {
7157       auto FunnelOpcode = IsFSHL ? ISD::FSHL : ISD::FSHR;
7158       setValue(&I, DAG.getNode(FunnelOpcode, sdl, VT, X, Y, Z));
7159     }
7160     return;
7161   }
7162   case Intrinsic::sadd_sat: {
7163     SDValue Op1 = getValue(I.getArgOperand(0));
7164     SDValue Op2 = getValue(I.getArgOperand(1));
7165     setValue(&I, DAG.getNode(ISD::SADDSAT, sdl, Op1.getValueType(), Op1, Op2));
7166     return;
7167   }
7168   case Intrinsic::uadd_sat: {
7169     SDValue Op1 = getValue(I.getArgOperand(0));
7170     SDValue Op2 = getValue(I.getArgOperand(1));
7171     setValue(&I, DAG.getNode(ISD::UADDSAT, sdl, Op1.getValueType(), Op1, Op2));
7172     return;
7173   }
7174   case Intrinsic::ssub_sat: {
7175     SDValue Op1 = getValue(I.getArgOperand(0));
7176     SDValue Op2 = getValue(I.getArgOperand(1));
7177     setValue(&I, DAG.getNode(ISD::SSUBSAT, sdl, Op1.getValueType(), Op1, Op2));
7178     return;
7179   }
7180   case Intrinsic::usub_sat: {
7181     SDValue Op1 = getValue(I.getArgOperand(0));
7182     SDValue Op2 = getValue(I.getArgOperand(1));
7183     setValue(&I, DAG.getNode(ISD::USUBSAT, sdl, Op1.getValueType(), Op1, Op2));
7184     return;
7185   }
7186   case Intrinsic::sshl_sat: {
7187     SDValue Op1 = getValue(I.getArgOperand(0));
7188     SDValue Op2 = getValue(I.getArgOperand(1));
7189     setValue(&I, DAG.getNode(ISD::SSHLSAT, sdl, Op1.getValueType(), Op1, Op2));
7190     return;
7191   }
7192   case Intrinsic::ushl_sat: {
7193     SDValue Op1 = getValue(I.getArgOperand(0));
7194     SDValue Op2 = getValue(I.getArgOperand(1));
7195     setValue(&I, DAG.getNode(ISD::USHLSAT, sdl, Op1.getValueType(), Op1, Op2));
7196     return;
7197   }
7198   case Intrinsic::smul_fix:
7199   case Intrinsic::umul_fix:
7200   case Intrinsic::smul_fix_sat:
7201   case Intrinsic::umul_fix_sat: {
7202     SDValue Op1 = getValue(I.getArgOperand(0));
7203     SDValue Op2 = getValue(I.getArgOperand(1));
7204     SDValue Op3 = getValue(I.getArgOperand(2));
7205     setValue(&I, DAG.getNode(FixedPointIntrinsicToOpcode(Intrinsic), sdl,
7206                              Op1.getValueType(), Op1, Op2, Op3));
7207     return;
7208   }
7209   case Intrinsic::sdiv_fix:
7210   case Intrinsic::udiv_fix:
7211   case Intrinsic::sdiv_fix_sat:
7212   case Intrinsic::udiv_fix_sat: {
7213     SDValue Op1 = getValue(I.getArgOperand(0));
7214     SDValue Op2 = getValue(I.getArgOperand(1));
7215     SDValue Op3 = getValue(I.getArgOperand(2));
7216     setValue(&I, expandDivFix(FixedPointIntrinsicToOpcode(Intrinsic), sdl,
7217                               Op1, Op2, Op3, DAG, TLI));
7218     return;
7219   }
7220   case Intrinsic::smax: {
7221     SDValue Op1 = getValue(I.getArgOperand(0));
7222     SDValue Op2 = getValue(I.getArgOperand(1));
7223     setValue(&I, DAG.getNode(ISD::SMAX, sdl, Op1.getValueType(), Op1, Op2));
7224     return;
7225   }
7226   case Intrinsic::smin: {
7227     SDValue Op1 = getValue(I.getArgOperand(0));
7228     SDValue Op2 = getValue(I.getArgOperand(1));
7229     setValue(&I, DAG.getNode(ISD::SMIN, sdl, Op1.getValueType(), Op1, Op2));
7230     return;
7231   }
7232   case Intrinsic::umax: {
7233     SDValue Op1 = getValue(I.getArgOperand(0));
7234     SDValue Op2 = getValue(I.getArgOperand(1));
7235     setValue(&I, DAG.getNode(ISD::UMAX, sdl, Op1.getValueType(), Op1, Op2));
7236     return;
7237   }
7238   case Intrinsic::umin: {
7239     SDValue Op1 = getValue(I.getArgOperand(0));
7240     SDValue Op2 = getValue(I.getArgOperand(1));
7241     setValue(&I, DAG.getNode(ISD::UMIN, sdl, Op1.getValueType(), Op1, Op2));
7242     return;
7243   }
7244   case Intrinsic::abs: {
7245     // TODO: Preserve "int min is poison" arg in SDAG?
7246     SDValue Op1 = getValue(I.getArgOperand(0));
7247     setValue(&I, DAG.getNode(ISD::ABS, sdl, Op1.getValueType(), Op1));
7248     return;
7249   }
7250   case Intrinsic::scmp: {
7251     SDValue Op1 = getValue(I.getArgOperand(0));
7252     SDValue Op2 = getValue(I.getArgOperand(1));
7253     EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7254     setValue(&I, DAG.getNode(ISD::SCMP, sdl, DestVT, Op1, Op2));
7255     break;
7256   }
7257   case Intrinsic::ucmp: {
7258     SDValue Op1 = getValue(I.getArgOperand(0));
7259     SDValue Op2 = getValue(I.getArgOperand(1));
7260     EVT DestVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7261     setValue(&I, DAG.getNode(ISD::UCMP, sdl, DestVT, Op1, Op2));
7262     break;
7263   }
7264   case Intrinsic::stacksave: {
7265     SDValue Op = getRoot();
7266     EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7267     Res = DAG.getNode(ISD::STACKSAVE, sdl, DAG.getVTList(VT, MVT::Other), Op);
7268     setValue(&I, Res);
7269     DAG.setRoot(Res.getValue(1));
7270     return;
7271   }
7272   case Intrinsic::stackrestore:
7273     Res = getValue(I.getArgOperand(0));
7274     DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, sdl, MVT::Other, getRoot(), Res));
7275     return;
7276   case Intrinsic::get_dynamic_area_offset: {
7277     SDValue Op = getRoot();
7278     EVT PtrTy = TLI.getFrameIndexTy(DAG.getDataLayout());
7279     EVT ResTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
7280     // Result type for @llvm.get.dynamic.area.offset should match PtrTy for
7281     // target.
7282     if (PtrTy.getFixedSizeInBits() < ResTy.getFixedSizeInBits())
7283       report_fatal_error("Wrong result type for @llvm.get.dynamic.area.offset"
7284                          " intrinsic!");
7285     Res = DAG.getNode(ISD::GET_DYNAMIC_AREA_OFFSET, sdl, DAG.getVTList(ResTy),
7286                       Op);
7287     DAG.setRoot(Op);
7288     setValue(&I, Res);
7289     return;
7290   }
7291   case Intrinsic::stackguard: {
7292     MachineFunction &MF = DAG.getMachineFunction();
7293     const Module &M = *MF.getFunction().getParent();
7294     EVT PtrTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
7295     SDValue Chain = getRoot();
7296     if (TLI.useLoadStackGuardNode()) {
7297       Res = getLoadStackGuard(DAG, sdl, Chain);
7298       Res = DAG.getPtrExtOrTrunc(Res, sdl, PtrTy);
7299     } else {
7300       const Value *Global = TLI.getSDagStackGuard(M);
7301       Align Align = DAG.getDataLayout().getPrefTypeAlign(Global->getType());
7302       Res = DAG.getLoad(PtrTy, sdl, Chain, getValue(Global),
7303                         MachinePointerInfo(Global, 0), Align,
7304                         MachineMemOperand::MOVolatile);
7305     }
7306     if (TLI.useStackGuardXorFP())
7307       Res = TLI.emitStackGuardXorFP(DAG, Res, sdl);
7308     DAG.setRoot(Chain);
7309     setValue(&I, Res);
7310     return;
7311   }
7312   case Intrinsic::stackprotector: {
7313     // Emit code into the DAG to store the stack guard onto the stack.
7314     MachineFunction &MF = DAG.getMachineFunction();
7315     MachineFrameInfo &MFI = MF.getFrameInfo();
7316     SDValue Src, Chain = getRoot();
7317 
7318     if (TLI.useLoadStackGuardNode())
7319       Src = getLoadStackGuard(DAG, sdl, Chain);
7320     else
7321       Src = getValue(I.getArgOperand(0));   // The guard's value.
7322 
7323     AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
7324 
7325     int FI = FuncInfo.StaticAllocaMap[Slot];
7326     MFI.setStackProtectorIndex(FI);
7327     EVT PtrTy = TLI.getFrameIndexTy(DAG.getDataLayout());
7328 
7329     SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
7330 
7331     // Store the stack protector onto the stack.
7332     Res = DAG.getStore(
7333         Chain, sdl, Src, FIN,
7334         MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
7335         MaybeAlign(), MachineMemOperand::MOVolatile);
7336     setValue(&I, Res);
7337     DAG.setRoot(Res);
7338     return;
7339   }
7340   case Intrinsic::objectsize:
7341     llvm_unreachable("llvm.objectsize.* should have been lowered already");
7342 
7343   case Intrinsic::is_constant:
7344     llvm_unreachable("llvm.is.constant.* should have been lowered already");
7345 
7346   case Intrinsic::annotation:
7347   case Intrinsic::ptr_annotation:
7348   case Intrinsic::launder_invariant_group:
7349   case Intrinsic::strip_invariant_group:
7350     // Drop the intrinsic, but forward the value
7351     setValue(&I, getValue(I.getOperand(0)));
7352     return;
7353 
7354   case Intrinsic::assume:
7355   case Intrinsic::experimental_noalias_scope_decl:
7356   case Intrinsic::var_annotation:
7357   case Intrinsic::sideeffect:
7358     // Discard annotate attributes, noalias scope declarations, assumptions, and
7359     // artificial side-effects.
7360     return;
7361 
7362   case Intrinsic::codeview_annotation: {
7363     // Emit a label associated with this metadata.
7364     MachineFunction &MF = DAG.getMachineFunction();
7365     MCSymbol *Label =
7366         MF.getMMI().getContext().createTempSymbol("annotation", true);
7367     Metadata *MD = cast<MetadataAsValue>(I.getArgOperand(0))->getMetadata();
7368     MF.addCodeViewAnnotation(Label, cast<MDNode>(MD));
7369     Res = DAG.getLabelNode(ISD::ANNOTATION_LABEL, sdl, getRoot(), Label);
7370     DAG.setRoot(Res);
7371     return;
7372   }
7373 
7374   case Intrinsic::init_trampoline: {
7375     const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts());
7376 
7377     SDValue Ops[6];
7378     Ops[0] = getRoot();
7379     Ops[1] = getValue(I.getArgOperand(0));
7380     Ops[2] = getValue(I.getArgOperand(1));
7381     Ops[3] = getValue(I.getArgOperand(2));
7382     Ops[4] = DAG.getSrcValue(I.getArgOperand(0));
7383     Ops[5] = DAG.getSrcValue(F);
7384 
7385     Res = DAG.getNode(ISD::INIT_TRAMPOLINE, sdl, MVT::Other, Ops);
7386 
7387     DAG.setRoot(Res);
7388     return;
7389   }
7390   case Intrinsic::adjust_trampoline:
7391     setValue(&I, DAG.getNode(ISD::ADJUST_TRAMPOLINE, sdl,
7392                              TLI.getPointerTy(DAG.getDataLayout()),
7393                              getValue(I.getArgOperand(0))));
7394     return;
7395   case Intrinsic::gcroot: {
7396     assert(DAG.getMachineFunction().getFunction().hasGC() &&
7397            "only valid in functions with gc specified, enforced by Verifier");
7398     assert(GFI && "implied by previous");
7399     const Value *Alloca = I.getArgOperand(0)->stripPointerCasts();
7400     const Constant *TypeMap = cast<Constant>(I.getArgOperand(1));
7401 
7402     FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
7403     GFI->addStackRoot(FI->getIndex(), TypeMap);
7404     return;
7405   }
7406   case Intrinsic::gcread:
7407   case Intrinsic::gcwrite:
7408     llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
7409   case Intrinsic::get_rounding:
7410     Res = DAG.getNode(ISD::GET_ROUNDING, sdl, {MVT::i32, MVT::Other}, getRoot());
7411     setValue(&I, Res);
7412     DAG.setRoot(Res.getValue(1));
7413     return;
7414 
7415   case Intrinsic::expect:
7416     // Just replace __builtin_expect(exp, c) with EXP.
7417     setValue(&I, getValue(I.getArgOperand(0)));
7418     return;
7419 
7420   case Intrinsic::ubsantrap:
7421   case Intrinsic::debugtrap:
7422   case Intrinsic::trap: {
7423     StringRef TrapFuncName =
7424         I.getAttributes().getFnAttr("trap-func-name").getValueAsString();
7425     if (TrapFuncName.empty()) {
7426       switch (Intrinsic) {
7427       case Intrinsic::trap:
7428         DAG.setRoot(DAG.getNode(ISD::TRAP, sdl, MVT::Other, getRoot()));
7429         break;
7430       case Intrinsic::debugtrap:
7431         DAG.setRoot(DAG.getNode(ISD::DEBUGTRAP, sdl, MVT::Other, getRoot()));
7432         break;
7433       case Intrinsic::ubsantrap:
7434         DAG.setRoot(DAG.getNode(
7435             ISD::UBSANTRAP, sdl, MVT::Other, getRoot(),
7436             DAG.getTargetConstant(
7437                 cast<ConstantInt>(I.getArgOperand(0))->getZExtValue(), sdl,
7438                 MVT::i32)));
7439         break;
7440       default: llvm_unreachable("unknown trap intrinsic");
7441       }
7442       return;
7443     }
7444     TargetLowering::ArgListTy Args;
7445     if (Intrinsic == Intrinsic::ubsantrap) {
7446       Args.push_back(TargetLoweringBase::ArgListEntry());
7447       Args[0].Val = I.getArgOperand(0);
7448       Args[0].Node = getValue(Args[0].Val);
7449       Args[0].Ty = Args[0].Val->getType();
7450     }
7451 
7452     TargetLowering::CallLoweringInfo CLI(DAG);
7453     CLI.setDebugLoc(sdl).setChain(getRoot()).setLibCallee(
7454         CallingConv::C, I.getType(),
7455         DAG.getExternalSymbol(TrapFuncName.data(),
7456                               TLI.getPointerTy(DAG.getDataLayout())),
7457         std::move(Args));
7458 
7459     std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
7460     DAG.setRoot(Result.second);
7461     return;
7462   }
7463 
7464   case Intrinsic::allow_runtime_check:
7465   case Intrinsic::allow_ubsan_check:
7466     setValue(&I, getValue(ConstantInt::getTrue(I.getType())));
7467     return;
7468 
7469   case Intrinsic::uadd_with_overflow:
7470   case Intrinsic::sadd_with_overflow:
7471   case Intrinsic::usub_with_overflow:
7472   case Intrinsic::ssub_with_overflow:
7473   case Intrinsic::umul_with_overflow:
7474   case Intrinsic::smul_with_overflow: {
7475     ISD::NodeType Op;
7476     switch (Intrinsic) {
7477     default: llvm_unreachable("Impossible intrinsic");  // Can't reach here.
7478     case Intrinsic::uadd_with_overflow: Op = ISD::UADDO; break;
7479     case Intrinsic::sadd_with_overflow: Op = ISD::SADDO; break;
7480     case Intrinsic::usub_with_overflow: Op = ISD::USUBO; break;
7481     case Intrinsic::ssub_with_overflow: Op = ISD::SSUBO; break;
7482     case Intrinsic::umul_with_overflow: Op = ISD::UMULO; break;
7483     case Intrinsic::smul_with_overflow: Op = ISD::SMULO; break;
7484     }
7485     SDValue Op1 = getValue(I.getArgOperand(0));
7486     SDValue Op2 = getValue(I.getArgOperand(1));
7487 
7488     EVT ResultVT = Op1.getValueType();
7489     EVT OverflowVT = MVT::i1;
7490     if (ResultVT.isVector())
7491       OverflowVT = EVT::getVectorVT(
7492           *Context, OverflowVT, ResultVT.getVectorElementCount());
7493 
7494     SDVTList VTs = DAG.getVTList(ResultVT, OverflowVT);
7495     setValue(&I, DAG.getNode(Op, sdl, VTs, Op1, Op2));
7496     return;
7497   }
7498   case Intrinsic::prefetch: {
7499     SDValue Ops[5];
7500     unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
7501     auto Flags = rw == 0 ? MachineMemOperand::MOLoad :MachineMemOperand::MOStore;
7502     Ops[0] = DAG.getRoot();
7503     Ops[1] = getValue(I.getArgOperand(0));
7504     Ops[2] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(1)), sdl,
7505                                    MVT::i32);
7506     Ops[3] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(2)), sdl,
7507                                    MVT::i32);
7508     Ops[4] = DAG.getTargetConstant(*cast<ConstantInt>(I.getArgOperand(3)), sdl,
7509                                    MVT::i32);
7510     SDValue Result = DAG.getMemIntrinsicNode(
7511         ISD::PREFETCH, sdl, DAG.getVTList(MVT::Other), Ops,
7512         EVT::getIntegerVT(*Context, 8), MachinePointerInfo(I.getArgOperand(0)),
7513         /* align */ std::nullopt, Flags);
7514 
7515     // Chain the prefetch in parallel with any pending loads, to stay out of
7516     // the way of later optimizations.
7517     PendingLoads.push_back(Result);
7518     Result = getRoot();
7519     DAG.setRoot(Result);
7520     return;
7521   }
7522   case Intrinsic::lifetime_start:
7523   case Intrinsic::lifetime_end: {
7524     bool IsStart = (Intrinsic == Intrinsic::lifetime_start);
7525     // Stack coloring is not enabled in O0, discard region information.
7526     if (TM.getOptLevel() == CodeGenOptLevel::None)
7527       return;
7528 
7529     const int64_t ObjectSize =
7530         cast<ConstantInt>(I.getArgOperand(0))->getSExtValue();
7531     Value *const ObjectPtr = I.getArgOperand(1);
7532     SmallVector<const Value *, 4> Allocas;
7533     getUnderlyingObjects(ObjectPtr, Allocas);
7534 
7535     for (const Value *Alloca : Allocas) {
7536       const AllocaInst *LifetimeObject = dyn_cast_or_null<AllocaInst>(Alloca);
7537 
7538       // Could not find an Alloca.
7539       if (!LifetimeObject)
7540         continue;
7541 
7542       // First check that the Alloca is static, otherwise it won't have a
7543       // valid frame index.
7544       auto SI = FuncInfo.StaticAllocaMap.find(LifetimeObject);
7545       if (SI == FuncInfo.StaticAllocaMap.end())
7546         return;
7547 
7548       const int FrameIndex = SI->second;
7549       int64_t Offset;
7550       if (GetPointerBaseWithConstantOffset(
7551               ObjectPtr, Offset, DAG.getDataLayout()) != LifetimeObject)
7552         Offset = -1; // Cannot determine offset from alloca to lifetime object.
7553       Res = DAG.getLifetimeNode(IsStart, sdl, getRoot(), FrameIndex, ObjectSize,
7554                                 Offset);
7555       DAG.setRoot(Res);
7556     }
7557     return;
7558   }
7559   case Intrinsic::pseudoprobe: {
7560     auto Guid = cast<ConstantInt>(I.getArgOperand(0))->getZExtValue();
7561     auto Index = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
7562     auto Attr = cast<ConstantInt>(I.getArgOperand(2))->getZExtValue();
7563     Res = DAG.getPseudoProbeNode(sdl, getRoot(), Guid, Index, Attr);
7564     DAG.setRoot(Res);
7565     return;
7566   }
7567   case Intrinsic::invariant_start:
7568     // Discard region information.
7569     setValue(&I,
7570              DAG.getUNDEF(TLI.getValueType(DAG.getDataLayout(), I.getType())));
7571     return;
7572   case Intrinsic::invariant_end:
7573     // Discard region information.
7574     return;
7575   case Intrinsic::clear_cache: {
7576     SDValue InputChain = DAG.getRoot();
7577     SDValue StartVal = getValue(I.getArgOperand(0));
7578     SDValue EndVal = getValue(I.getArgOperand(1));
7579     Res = DAG.getNode(ISD::CLEAR_CACHE, sdl, DAG.getVTList(MVT::Other),
7580                       {InputChain, StartVal, EndVal});
7581     setValue(&I, Res);
7582     DAG.setRoot(Res);
7583     return;
7584   }
7585   case Intrinsic::donothing:
7586   case Intrinsic::seh_try_begin:
7587   case Intrinsic::seh_scope_begin:
7588   case Intrinsic::seh_try_end:
7589   case Intrinsic::seh_scope_end:
7590     // ignore
7591     return;
7592   case Intrinsic::experimental_stackmap:
7593     visitStackmap(I);
7594     return;
7595   case Intrinsic::experimental_patchpoint_void:
7596   case Intrinsic::experimental_patchpoint:
7597     visitPatchpoint(I);
7598     return;
7599   case Intrinsic::experimental_gc_statepoint:
7600     LowerStatepoint(cast<GCStatepointInst>(I));
7601     return;
7602   case Intrinsic::experimental_gc_result:
7603     visitGCResult(cast<GCResultInst>(I));
7604     return;
7605   case Intrinsic::experimental_gc_relocate:
7606     visitGCRelocate(cast<GCRelocateInst>(I));
7607     return;
7608   case Intrinsic::instrprof_cover:
7609     llvm_unreachable("instrprof failed to lower a cover");
7610   case Intrinsic::instrprof_increment:
7611     llvm_unreachable("instrprof failed to lower an increment");
7612   case Intrinsic::instrprof_timestamp:
7613     llvm_unreachable("instrprof failed to lower a timestamp");
7614   case Intrinsic::instrprof_value_profile:
7615     llvm_unreachable("instrprof failed to lower a value profiling call");
7616   case Intrinsic::instrprof_mcdc_parameters:
7617     llvm_unreachable("instrprof failed to lower mcdc parameters");
7618   case Intrinsic::instrprof_mcdc_tvbitmap_update:
7619     llvm_unreachable("instrprof failed to lower an mcdc tvbitmap update");
7620   case Intrinsic::localescape: {
7621     MachineFunction &MF = DAG.getMachineFunction();
7622     const TargetInstrInfo *TII = DAG.getSubtarget().getInstrInfo();
7623 
7624     // Directly emit some LOCAL_ESCAPE machine instrs. Label assignment emission
7625     // is the same on all targets.
7626     for (unsigned Idx = 0, E = I.arg_size(); Idx < E; ++Idx) {
7627       Value *Arg = I.getArgOperand(Idx)->stripPointerCasts();
7628       if (isa<ConstantPointerNull>(Arg))
7629         continue; // Skip null pointers. They represent a hole in index space.
7630       AllocaInst *Slot = cast<AllocaInst>(Arg);
7631       assert(FuncInfo.StaticAllocaMap.count(Slot) &&
7632              "can only escape static allocas");
7633       int FI = FuncInfo.StaticAllocaMap[Slot];
7634       MCSymbol *FrameAllocSym =
7635           MF.getMMI().getContext().getOrCreateFrameAllocSymbol(
7636               GlobalValue::dropLLVMManglingEscape(MF.getName()), Idx);
7637       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, dl,
7638               TII->get(TargetOpcode::LOCAL_ESCAPE))
7639           .addSym(FrameAllocSym)
7640           .addFrameIndex(FI);
7641     }
7642 
7643     return;
7644   }
7645 
7646   case Intrinsic::localrecover: {
7647     // i8* @llvm.localrecover(i8* %fn, i8* %fp, i32 %idx)
7648     MachineFunction &MF = DAG.getMachineFunction();
7649 
7650     // Get the symbol that defines the frame offset.
7651     auto *Fn = cast<Function>(I.getArgOperand(0)->stripPointerCasts());
7652     auto *Idx = cast<ConstantInt>(I.getArgOperand(2));
7653     unsigned IdxVal =
7654         unsigned(Idx->getLimitedValue(std::numeric_limits<int>::max()));
7655     MCSymbol *FrameAllocSym =
7656         MF.getMMI().getContext().getOrCreateFrameAllocSymbol(
7657             GlobalValue::dropLLVMManglingEscape(Fn->getName()), IdxVal);
7658 
7659     Value *FP = I.getArgOperand(1);
7660     SDValue FPVal = getValue(FP);
7661     EVT PtrVT = FPVal.getValueType();
7662 
7663     // Create a MCSymbol for the label to avoid any target lowering
7664     // that would make this PC relative.
7665     SDValue OffsetSym = DAG.getMCSymbol(FrameAllocSym, PtrVT);
7666     SDValue OffsetVal =
7667         DAG.getNode(ISD::LOCAL_RECOVER, sdl, PtrVT, OffsetSym);
7668 
7669     // Add the offset to the FP.
7670     SDValue Add = DAG.getMemBasePlusOffset(FPVal, OffsetVal, sdl);
7671     setValue(&I, Add);
7672 
7673     return;
7674   }
7675 
7676   case Intrinsic::eh_exceptionpointer:
7677   case Intrinsic::eh_exceptioncode: {
7678     // Get the exception pointer vreg, copy from it, and resize it to fit.
7679     const auto *CPI = cast<CatchPadInst>(I.getArgOperand(0));
7680     MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
7681     const TargetRegisterClass *PtrRC = TLI.getRegClassFor(PtrVT);
7682     unsigned VReg = FuncInfo.getCatchPadExceptionPointerVReg(CPI, PtrRC);
7683     SDValue N = DAG.getCopyFromReg(DAG.getEntryNode(), sdl, VReg, PtrVT);
7684     if (Intrinsic == Intrinsic::eh_exceptioncode)
7685       N = DAG.getZExtOrTrunc(N, sdl, MVT::i32);
7686     setValue(&I, N);
7687     return;
7688   }
7689   case Intrinsic::xray_customevent: {
7690     // Here we want to make sure that the intrinsic behaves as if it has a
7691     // specific calling convention.
7692     const auto &Triple = DAG.getTarget().getTargetTriple();
7693     if (!Triple.isAArch64(64) && Triple.getArch() != Triple::x86_64)
7694       return;
7695 
7696     SmallVector<SDValue, 8> Ops;
7697 
7698     // We want to say that we always want the arguments in registers.
7699     SDValue LogEntryVal = getValue(I.getArgOperand(0));
7700     SDValue StrSizeVal = getValue(I.getArgOperand(1));
7701     SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
7702     SDValue Chain = getRoot();
7703     Ops.push_back(LogEntryVal);
7704     Ops.push_back(StrSizeVal);
7705     Ops.push_back(Chain);
7706 
7707     // We need to enforce the calling convention for the callsite, so that
7708     // argument ordering is enforced correctly, and that register allocation can
7709     // see that some registers may be assumed clobbered and have to preserve
7710     // them across calls to the intrinsic.
7711     MachineSDNode *MN = DAG.getMachineNode(TargetOpcode::PATCHABLE_EVENT_CALL,
7712                                            sdl, NodeTys, Ops);
7713     SDValue patchableNode = SDValue(MN, 0);
7714     DAG.setRoot(patchableNode);
7715     setValue(&I, patchableNode);
7716     return;
7717   }
7718   case Intrinsic::xray_typedevent: {
7719     // Here we want to make sure that the intrinsic behaves as if it has a
7720     // specific calling convention.
7721     const auto &Triple = DAG.getTarget().getTargetTriple();
7722     if (!Triple.isAArch64(64) && Triple.getArch() != Triple::x86_64)
7723       return;
7724 
7725     SmallVector<SDValue, 8> Ops;
7726 
7727     // We want to say that we always want the arguments in registers.
7728     // It's unclear to me how manipulating the selection DAG here forces callers
7729     // to provide arguments in registers instead of on the stack.
7730     SDValue LogTypeId = getValue(I.getArgOperand(0));
7731     SDValue LogEntryVal = getValue(I.getArgOperand(1));
7732     SDValue StrSizeVal = getValue(I.getArgOperand(2));
7733     SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
7734     SDValue Chain = getRoot();
7735     Ops.push_back(LogTypeId);
7736     Ops.push_back(LogEntryVal);
7737     Ops.push_back(StrSizeVal);
7738     Ops.push_back(Chain);
7739 
7740     // We need to enforce the calling convention for the callsite, so that
7741     // argument ordering is enforced correctly, and that register allocation can
7742     // see that some registers may be assumed clobbered and have to preserve
7743     // them across calls to the intrinsic.
7744     MachineSDNode *MN = DAG.getMachineNode(
7745         TargetOpcode::PATCHABLE_TYPED_EVENT_CALL, sdl, NodeTys, Ops);
7746     SDValue patchableNode = SDValue(MN, 0);
7747     DAG.setRoot(patchableNode);
7748     setValue(&I, patchableNode);
7749     return;
7750   }
7751   case Intrinsic::experimental_deoptimize:
7752     LowerDeoptimizeCall(&I);
7753     return;
7754   case Intrinsic::experimental_stepvector:
7755     visitStepVector(I);
7756     return;
7757   case Intrinsic::vector_reduce_fadd:
7758   case Intrinsic::vector_reduce_fmul:
7759   case Intrinsic::vector_reduce_add:
7760   case Intrinsic::vector_reduce_mul:
7761   case Intrinsic::vector_reduce_and:
7762   case Intrinsic::vector_reduce_or:
7763   case Intrinsic::vector_reduce_xor:
7764   case Intrinsic::vector_reduce_smax:
7765   case Intrinsic::vector_reduce_smin:
7766   case Intrinsic::vector_reduce_umax:
7767   case Intrinsic::vector_reduce_umin:
7768   case Intrinsic::vector_reduce_fmax:
7769   case Intrinsic::vector_reduce_fmin:
7770   case Intrinsic::vector_reduce_fmaximum:
7771   case Intrinsic::vector_reduce_fminimum:
7772     visitVectorReduce(I, Intrinsic);
7773     return;
7774 
7775   case Intrinsic::icall_branch_funnel: {
7776     SmallVector<SDValue, 16> Ops;
7777     Ops.push_back(getValue(I.getArgOperand(0)));
7778 
7779     int64_t Offset;
7780     auto *Base = dyn_cast<GlobalObject>(GetPointerBaseWithConstantOffset(
7781         I.getArgOperand(1), Offset, DAG.getDataLayout()));
7782     if (!Base)
7783       report_fatal_error(
7784           "llvm.icall.branch.funnel operand must be a GlobalValue");
7785     Ops.push_back(DAG.getTargetGlobalAddress(Base, sdl, MVT::i64, 0));
7786 
7787     struct BranchFunnelTarget {
7788       int64_t Offset;
7789       SDValue Target;
7790     };
7791     SmallVector<BranchFunnelTarget, 8> Targets;
7792 
7793     for (unsigned Op = 1, N = I.arg_size(); Op != N; Op += 2) {
7794       auto *ElemBase = dyn_cast<GlobalObject>(GetPointerBaseWithConstantOffset(
7795           I.getArgOperand(Op), Offset, DAG.getDataLayout()));
7796       if (ElemBase != Base)
7797         report_fatal_error("all llvm.icall.branch.funnel operands must refer "
7798                            "to the same GlobalValue");
7799 
7800       SDValue Val = getValue(I.getArgOperand(Op + 1));
7801       auto *GA = dyn_cast<GlobalAddressSDNode>(Val);
7802       if (!GA)
7803         report_fatal_error(
7804             "llvm.icall.branch.funnel operand must be a GlobalValue");
7805       Targets.push_back({Offset, DAG.getTargetGlobalAddress(
7806                                      GA->getGlobal(), sdl, Val.getValueType(),
7807                                      GA->getOffset())});
7808     }
7809     llvm::sort(Targets,
7810                [](const BranchFunnelTarget &T1, const BranchFunnelTarget &T2) {
7811                  return T1.Offset < T2.Offset;
7812                });
7813 
7814     for (auto &T : Targets) {
7815       Ops.push_back(DAG.getTargetConstant(T.Offset, sdl, MVT::i32));
7816       Ops.push_back(T.Target);
7817     }
7818 
7819     Ops.push_back(DAG.getRoot()); // Chain
7820     SDValue N(DAG.getMachineNode(TargetOpcode::ICALL_BRANCH_FUNNEL, sdl,
7821                                  MVT::Other, Ops),
7822               0);
7823     DAG.setRoot(N);
7824     setValue(&I, N);
7825     HasTailCall = true;
7826     return;
7827   }
7828 
7829   case Intrinsic::wasm_landingpad_index:
7830     // Information this intrinsic contained has been transferred to
7831     // MachineFunction in SelectionDAGISel::PrepareEHLandingPad. We can safely
7832     // delete it now.
7833     return;
7834 
7835   case Intrinsic::aarch64_settag:
7836   case Intrinsic::aarch64_settag_zero: {
7837     const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
7838     bool ZeroMemory = Intrinsic == Intrinsic::aarch64_settag_zero;
7839     SDValue Val = TSI.EmitTargetCodeForSetTag(
7840         DAG, sdl, getRoot(), getValue(I.getArgOperand(0)),
7841         getValue(I.getArgOperand(1)), MachinePointerInfo(I.getArgOperand(0)),
7842         ZeroMemory);
7843     DAG.setRoot(Val);
7844     setValue(&I, Val);
7845     return;
7846   }
7847   case Intrinsic::amdgcn_cs_chain: {
7848     assert(I.arg_size() == 5 && "Additional args not supported yet");
7849     assert(cast<ConstantInt>(I.getOperand(4))->isZero() &&
7850            "Non-zero flags not supported yet");
7851 
7852     // At this point we don't care if it's amdgpu_cs_chain or
7853     // amdgpu_cs_chain_preserve.
7854     CallingConv::ID CC = CallingConv::AMDGPU_CS_Chain;
7855 
7856     Type *RetTy = I.getType();
7857     assert(RetTy->isVoidTy() && "Should not return");
7858 
7859     SDValue Callee = getValue(I.getOperand(0));
7860 
7861     // We only have 2 actual args: one for the SGPRs and one for the VGPRs.
7862     // We'll also tack the value of the EXEC mask at the end.
7863     TargetLowering::ArgListTy Args;
7864     Args.reserve(3);
7865 
7866     for (unsigned Idx : {2, 3, 1}) {
7867       TargetLowering::ArgListEntry Arg;
7868       Arg.Node = getValue(I.getOperand(Idx));
7869       Arg.Ty = I.getOperand(Idx)->getType();
7870       Arg.setAttributes(&I, Idx);
7871       Args.push_back(Arg);
7872     }
7873 
7874     assert(Args[0].IsInReg && "SGPR args should be marked inreg");
7875     assert(!Args[1].IsInReg && "VGPR args should not be marked inreg");
7876     Args[2].IsInReg = true; // EXEC should be inreg
7877 
7878     TargetLowering::CallLoweringInfo CLI(DAG);
7879     CLI.setDebugLoc(getCurSDLoc())
7880         .setChain(getRoot())
7881         .setCallee(CC, RetTy, Callee, std::move(Args))
7882         .setNoReturn(true)
7883         .setTailCall(true)
7884         .setConvergent(I.isConvergent());
7885     CLI.CB = &I;
7886     std::pair<SDValue, SDValue> Result =
7887         lowerInvokable(CLI, /*EHPadBB*/ nullptr);
7888     (void)Result;
7889     assert(!Result.first.getNode() && !Result.second.getNode() &&
7890            "Should've lowered as tail call");
7891 
7892     HasTailCall = true;
7893     return;
7894   }
7895   case Intrinsic::ptrmask: {
7896     SDValue Ptr = getValue(I.getOperand(0));
7897     SDValue Mask = getValue(I.getOperand(1));
7898 
7899     // On arm64_32, pointers are 32 bits when stored in memory, but
7900     // zero-extended to 64 bits when in registers.  Thus the mask is 32 bits to
7901     // match the index type, but the pointer is 64 bits, so the the mask must be
7902     // zero-extended up to 64 bits to match the pointer.
7903     EVT PtrVT =
7904         TLI.getValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
7905     EVT MemVT =
7906         TLI.getMemValueType(DAG.getDataLayout(), I.getOperand(0)->getType());
7907     assert(PtrVT == Ptr.getValueType());
7908     assert(MemVT == Mask.getValueType());
7909     if (MemVT != PtrVT)
7910       Mask = DAG.getPtrExtOrTrunc(Mask, sdl, PtrVT);
7911 
7912     setValue(&I, DAG.getNode(ISD::AND, sdl, PtrVT, Ptr, Mask));
7913     return;
7914   }
7915   case Intrinsic::threadlocal_address: {
7916     setValue(&I, getValue(I.getOperand(0)));
7917     return;
7918   }
7919   case Intrinsic::get_active_lane_mask: {
7920     EVT CCVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7921     SDValue Index = getValue(I.getOperand(0));
7922     EVT ElementVT = Index.getValueType();
7923 
7924     if (!TLI.shouldExpandGetActiveLaneMask(CCVT, ElementVT)) {
7925       visitTargetIntrinsic(I, Intrinsic);
7926       return;
7927     }
7928 
7929     SDValue TripCount = getValue(I.getOperand(1));
7930     EVT VecTy = EVT::getVectorVT(*DAG.getContext(), ElementVT,
7931                                  CCVT.getVectorElementCount());
7932 
7933     SDValue VectorIndex = DAG.getSplat(VecTy, sdl, Index);
7934     SDValue VectorTripCount = DAG.getSplat(VecTy, sdl, TripCount);
7935     SDValue VectorStep = DAG.getStepVector(sdl, VecTy);
7936     SDValue VectorInduction = DAG.getNode(
7937         ISD::UADDSAT, sdl, VecTy, VectorIndex, VectorStep);
7938     SDValue SetCC = DAG.getSetCC(sdl, CCVT, VectorInduction,
7939                                  VectorTripCount, ISD::CondCode::SETULT);
7940     setValue(&I, SetCC);
7941     return;
7942   }
7943   case Intrinsic::experimental_get_vector_length: {
7944     assert(cast<ConstantInt>(I.getOperand(1))->getSExtValue() > 0 &&
7945            "Expected positive VF");
7946     unsigned VF = cast<ConstantInt>(I.getOperand(1))->getZExtValue();
7947     bool IsScalable = cast<ConstantInt>(I.getOperand(2))->isOne();
7948 
7949     SDValue Count = getValue(I.getOperand(0));
7950     EVT CountVT = Count.getValueType();
7951 
7952     if (!TLI.shouldExpandGetVectorLength(CountVT, VF, IsScalable)) {
7953       visitTargetIntrinsic(I, Intrinsic);
7954       return;
7955     }
7956 
7957     // Expand to a umin between the trip count and the maximum elements the type
7958     // can hold.
7959     EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
7960 
7961     // Extend the trip count to at least the result VT.
7962     if (CountVT.bitsLT(VT)) {
7963       Count = DAG.getNode(ISD::ZERO_EXTEND, sdl, VT, Count);
7964       CountVT = VT;
7965     }
7966 
7967     SDValue MaxEVL = DAG.getElementCount(sdl, CountVT,
7968                                          ElementCount::get(VF, IsScalable));
7969 
7970     SDValue UMin = DAG.getNode(ISD::UMIN, sdl, CountVT, Count, MaxEVL);
7971     // Clip to the result type if needed.
7972     SDValue Trunc = DAG.getNode(ISD::TRUNCATE, sdl, VT, UMin);
7973 
7974     setValue(&I, Trunc);
7975     return;
7976   }
7977   case Intrinsic::experimental_cttz_elts: {
7978     auto DL = getCurSDLoc();
7979     SDValue Op = getValue(I.getOperand(0));
7980     EVT OpVT = Op.getValueType();
7981 
7982     if (!TLI.shouldExpandCttzElements(OpVT)) {
7983       visitTargetIntrinsic(I, Intrinsic);
7984       return;
7985     }
7986 
7987     if (OpVT.getScalarType() != MVT::i1) {
7988       // Compare the input vector elements to zero & use to count trailing zeros
7989       SDValue AllZero = DAG.getConstant(0, DL, OpVT);
7990       OpVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1,
7991                               OpVT.getVectorElementCount());
7992       Op = DAG.getSetCC(DL, OpVT, Op, AllZero, ISD::SETNE);
7993     }
7994 
7995     // If the zero-is-poison flag is set, we can assume the upper limit
7996     // of the result is VF-1.
7997     bool ZeroIsPoison =
7998         !cast<ConstantSDNode>(getValue(I.getOperand(1)))->isZero();
7999     ConstantRange VScaleRange(1, true); // Dummy value.
8000     if (isa<ScalableVectorType>(I.getOperand(0)->getType()))
8001       VScaleRange = getVScaleRange(I.getCaller(), 64);
8002     unsigned EltWidth = TLI.getBitWidthForCttzElements(
8003         I.getType(), OpVT.getVectorElementCount(), ZeroIsPoison, &VScaleRange);
8004 
8005     MVT NewEltTy = MVT::getIntegerVT(EltWidth);
8006 
8007     // Create the new vector type & get the vector length
8008     EVT NewVT = EVT::getVectorVT(*DAG.getContext(), NewEltTy,
8009                                  OpVT.getVectorElementCount());
8010 
8011     SDValue VL =
8012         DAG.getElementCount(DL, NewEltTy, OpVT.getVectorElementCount());
8013 
8014     SDValue StepVec = DAG.getStepVector(DL, NewVT);
8015     SDValue SplatVL = DAG.getSplat(NewVT, DL, VL);
8016     SDValue StepVL = DAG.getNode(ISD::SUB, DL, NewVT, SplatVL, StepVec);
8017     SDValue Ext = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, Op);
8018     SDValue And = DAG.getNode(ISD::AND, DL, NewVT, StepVL, Ext);
8019     SDValue Max = DAG.getNode(ISD::VECREDUCE_UMAX, DL, NewEltTy, And);
8020     SDValue Sub = DAG.getNode(ISD::SUB, DL, NewEltTy, VL, Max);
8021 
8022     EVT RetTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
8023     SDValue Ret = DAG.getZExtOrTrunc(Sub, DL, RetTy);
8024 
8025     setValue(&I, Ret);
8026     return;
8027   }
8028   case Intrinsic::vector_insert: {
8029     SDValue Vec = getValue(I.getOperand(0));
8030     SDValue SubVec = getValue(I.getOperand(1));
8031     SDValue Index = getValue(I.getOperand(2));
8032 
8033     // The intrinsic's index type is i64, but the SDNode requires an index type
8034     // suitable for the target. Convert the index as required.
8035     MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
8036     if (Index.getValueType() != VectorIdxTy)
8037       Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl);
8038 
8039     EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8040     setValue(&I, DAG.getNode(ISD::INSERT_SUBVECTOR, sdl, ResultVT, Vec, SubVec,
8041                              Index));
8042     return;
8043   }
8044   case Intrinsic::vector_extract: {
8045     SDValue Vec = getValue(I.getOperand(0));
8046     SDValue Index = getValue(I.getOperand(1));
8047     EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
8048 
8049     // The intrinsic's index type is i64, but the SDNode requires an index type
8050     // suitable for the target. Convert the index as required.
8051     MVT VectorIdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
8052     if (Index.getValueType() != VectorIdxTy)
8053       Index = DAG.getVectorIdxConstant(Index->getAsZExtVal(), sdl);
8054 
8055     setValue(&I,
8056              DAG.getNode(ISD::EXTRACT_SUBVECTOR, sdl, ResultVT, Vec, Index));
8057     return;
8058   }
8059   case Intrinsic::vector_reverse:
8060     visitVectorReverse(I);
8061     return;
8062   case Intrinsic::vector_splice:
8063     visitVectorSplice(I);
8064     return;
8065   case Intrinsic::callbr_landingpad:
8066     visitCallBrLandingPad(I);
8067     return;
8068   case Intrinsic::vector_interleave2:
8069     visitVectorInterleave(I);
8070     return;
8071   case Intrinsic::vector_deinterleave2:
8072     visitVectorDeinterleave(I);
8073     return;
8074   case Intrinsic::experimental_convergence_anchor:
8075   case Intrinsic::experimental_convergence_entry:
8076   case Intrinsic::experimental_convergence_loop:
8077     visitConvergenceControl(I, Intrinsic);
8078     return;
8079   case Intrinsic::experimental_vector_histogram_add: {
8080     visitVectorHistogram(I, Intrinsic);
8081     return;
8082   }
8083   }
8084 }
8085 
8086 void SelectionDAGBuilder::visitConstrainedFPIntrinsic(
8087     const ConstrainedFPIntrinsic &FPI) {
8088   SDLoc sdl = getCurSDLoc();
8089 
8090   // We do not need to serialize constrained FP intrinsics against
8091   // each other or against (nonvolatile) loads, so they can be
8092   // chained like loads.
8093   SDValue Chain = DAG.getRoot();
8094   SmallVector<SDValue, 4> Opers;
8095   Opers.push_back(Chain);
8096   for (unsigned I = 0, E = FPI.getNonMetadataArgCount(); I != E; ++I)
8097     Opers.push_back(getValue(FPI.getArgOperand(I)));
8098 
8099   auto pushOutChain = [this](SDValue Result, fp::ExceptionBehavior EB) {
8100     assert(Result.getNode()->getNumValues() == 2);
8101 
8102     // Push node to the appropriate list so that future instructions can be
8103     // chained up correctly.
8104     SDValue OutChain = Result.getValue(1);
8105     switch (EB) {
8106     case fp::ExceptionBehavior::ebIgnore:
8107       // The only reason why ebIgnore nodes still need to be chained is that
8108       // they might depend on the current rounding mode, and therefore must
8109       // not be moved across instruction that may change that mode.
8110       [[fallthrough]];
8111     case fp::ExceptionBehavior::ebMayTrap:
8112       // These must not be moved across calls or instructions that may change
8113       // floating-point exception masks.
8114       PendingConstrainedFP.push_back(OutChain);
8115       break;
8116     case fp::ExceptionBehavior::ebStrict:
8117       // These must not be moved across calls or instructions that may change
8118       // floating-point exception masks or read floating-point exception flags.
8119       // In addition, they cannot be optimized out even if unused.
8120       PendingConstrainedFPStrict.push_back(OutChain);
8121       break;
8122     }
8123   };
8124 
8125   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8126   EVT VT = TLI.getValueType(DAG.getDataLayout(), FPI.getType());
8127   SDVTList VTs = DAG.getVTList(VT, MVT::Other);
8128   fp::ExceptionBehavior EB = *FPI.getExceptionBehavior();
8129 
8130   SDNodeFlags Flags;
8131   if (EB == fp::ExceptionBehavior::ebIgnore)
8132     Flags.setNoFPExcept(true);
8133 
8134   if (auto *FPOp = dyn_cast<FPMathOperator>(&FPI))
8135     Flags.copyFMF(*FPOp);
8136 
8137   unsigned Opcode;
8138   switch (FPI.getIntrinsicID()) {
8139   default: llvm_unreachable("Impossible intrinsic");  // Can't reach here.
8140 #define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN)               \
8141   case Intrinsic::INTRINSIC:                                                   \
8142     Opcode = ISD::STRICT_##DAGN;                                               \
8143     break;
8144 #include "llvm/IR/ConstrainedOps.def"
8145   case Intrinsic::experimental_constrained_fmuladd: {
8146     Opcode = ISD::STRICT_FMA;
8147     // Break fmuladd into fmul and fadd.
8148     if (TM.Options.AllowFPOpFusion == FPOpFusion::Strict ||
8149         !TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), VT)) {
8150       Opers.pop_back();
8151       SDValue Mul = DAG.getNode(ISD::STRICT_FMUL, sdl, VTs, Opers, Flags);
8152       pushOutChain(Mul, EB);
8153       Opcode = ISD::STRICT_FADD;
8154       Opers.clear();
8155       Opers.push_back(Mul.getValue(1));
8156       Opers.push_back(Mul.getValue(0));
8157       Opers.push_back(getValue(FPI.getArgOperand(2)));
8158     }
8159     break;
8160   }
8161   }
8162 
8163   // A few strict DAG nodes carry additional operands that are not
8164   // set up by the default code above.
8165   switch (Opcode) {
8166   default: break;
8167   case ISD::STRICT_FP_ROUND:
8168     Opers.push_back(
8169         DAG.getTargetConstant(0, sdl, TLI.getPointerTy(DAG.getDataLayout())));
8170     break;
8171   case ISD::STRICT_FSETCC:
8172   case ISD::STRICT_FSETCCS: {
8173     auto *FPCmp = dyn_cast<ConstrainedFPCmpIntrinsic>(&FPI);
8174     ISD::CondCode Condition = getFCmpCondCode(FPCmp->getPredicate());
8175     if (TM.Options.NoNaNsFPMath)
8176       Condition = getFCmpCodeWithoutNaN(Condition);
8177     Opers.push_back(DAG.getCondCode(Condition));
8178     break;
8179   }
8180   }
8181 
8182   SDValue Result = DAG.getNode(Opcode, sdl, VTs, Opers, Flags);
8183   pushOutChain(Result, EB);
8184 
8185   SDValue FPResult = Result.getValue(0);
8186   setValue(&FPI, FPResult);
8187 }
8188 
8189 static unsigned getISDForVPIntrinsic(const VPIntrinsic &VPIntrin) {
8190   std::optional<unsigned> ResOPC;
8191   switch (VPIntrin.getIntrinsicID()) {
8192   case Intrinsic::vp_ctlz: {
8193     bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8194     ResOPC = IsZeroUndef ? ISD::VP_CTLZ_ZERO_UNDEF : ISD::VP_CTLZ;
8195     break;
8196   }
8197   case Intrinsic::vp_cttz: {
8198     bool IsZeroUndef = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8199     ResOPC = IsZeroUndef ? ISD::VP_CTTZ_ZERO_UNDEF : ISD::VP_CTTZ;
8200     break;
8201   }
8202   case Intrinsic::vp_cttz_elts: {
8203     bool IsZeroPoison = cast<ConstantInt>(VPIntrin.getArgOperand(1))->isOne();
8204     ResOPC = IsZeroPoison ? ISD::VP_CTTZ_ELTS_ZERO_UNDEF : ISD::VP_CTTZ_ELTS;
8205     break;
8206   }
8207 #define HELPER_MAP_VPID_TO_VPSD(VPID, VPSD)                                    \
8208   case Intrinsic::VPID:                                                        \
8209     ResOPC = ISD::VPSD;                                                        \
8210     break;
8211 #include "llvm/IR/VPIntrinsics.def"
8212   }
8213 
8214   if (!ResOPC)
8215     llvm_unreachable(
8216         "Inconsistency: no SDNode available for this VPIntrinsic!");
8217 
8218   if (*ResOPC == ISD::VP_REDUCE_SEQ_FADD ||
8219       *ResOPC == ISD::VP_REDUCE_SEQ_FMUL) {
8220     if (VPIntrin.getFastMathFlags().allowReassoc())
8221       return *ResOPC == ISD::VP_REDUCE_SEQ_FADD ? ISD::VP_REDUCE_FADD
8222                                                 : ISD::VP_REDUCE_FMUL;
8223   }
8224 
8225   return *ResOPC;
8226 }
8227 
8228 void SelectionDAGBuilder::visitVPLoad(
8229     const VPIntrinsic &VPIntrin, EVT VT,
8230     const SmallVectorImpl<SDValue> &OpValues) {
8231   SDLoc DL = getCurSDLoc();
8232   Value *PtrOperand = VPIntrin.getArgOperand(0);
8233   MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8234   AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8235   const MDNode *Ranges = getRangeMetadata(VPIntrin);
8236   SDValue LD;
8237   // Do not serialize variable-length loads of constant memory with
8238   // anything.
8239   if (!Alignment)
8240     Alignment = DAG.getEVTAlign(VT);
8241   MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8242   bool AddToChain = !AA || !AA->pointsToConstantMemory(ML);
8243   SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8244   MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8245       MachinePointerInfo(PtrOperand), MachineMemOperand::MOLoad,
8246       LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo, Ranges);
8247   LD = DAG.getLoadVP(VT, DL, InChain, OpValues[0], OpValues[1], OpValues[2],
8248                      MMO, false /*IsExpanding */);
8249   if (AddToChain)
8250     PendingLoads.push_back(LD.getValue(1));
8251   setValue(&VPIntrin, LD);
8252 }
8253 
8254 void SelectionDAGBuilder::visitVPGather(
8255     const VPIntrinsic &VPIntrin, EVT VT,
8256     const SmallVectorImpl<SDValue> &OpValues) {
8257   SDLoc DL = getCurSDLoc();
8258   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8259   Value *PtrOperand = VPIntrin.getArgOperand(0);
8260   MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8261   AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8262   const MDNode *Ranges = getRangeMetadata(VPIntrin);
8263   SDValue LD;
8264   if (!Alignment)
8265     Alignment = DAG.getEVTAlign(VT.getScalarType());
8266   unsigned AS =
8267     PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
8268   MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8269       MachinePointerInfo(AS), MachineMemOperand::MOLoad,
8270       LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo, Ranges);
8271   SDValue Base, Index, Scale;
8272   ISD::MemIndexType IndexType;
8273   bool UniformBase = getUniformBase(PtrOperand, Base, Index, IndexType, Scale,
8274                                     this, VPIntrin.getParent(),
8275                                     VT.getScalarStoreSize());
8276   if (!UniformBase) {
8277     Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout()));
8278     Index = getValue(PtrOperand);
8279     IndexType = ISD::SIGNED_SCALED;
8280     Scale = DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()));
8281   }
8282   EVT IdxVT = Index.getValueType();
8283   EVT EltTy = IdxVT.getVectorElementType();
8284   if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
8285     EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
8286     Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
8287   }
8288   LD = DAG.getGatherVP(
8289       DAG.getVTList(VT, MVT::Other), VT, DL,
8290       {DAG.getRoot(), Base, Index, Scale, OpValues[1], OpValues[2]}, MMO,
8291       IndexType);
8292   PendingLoads.push_back(LD.getValue(1));
8293   setValue(&VPIntrin, LD);
8294 }
8295 
8296 void SelectionDAGBuilder::visitVPStore(
8297     const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8298   SDLoc DL = getCurSDLoc();
8299   Value *PtrOperand = VPIntrin.getArgOperand(1);
8300   EVT VT = OpValues[0].getValueType();
8301   MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8302   AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8303   SDValue ST;
8304   if (!Alignment)
8305     Alignment = DAG.getEVTAlign(VT);
8306   SDValue Ptr = OpValues[1];
8307   SDValue Offset = DAG.getUNDEF(Ptr.getValueType());
8308   MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8309       MachinePointerInfo(PtrOperand), MachineMemOperand::MOStore,
8310       LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo);
8311   ST = DAG.getStoreVP(getMemoryRoot(), DL, OpValues[0], Ptr, Offset,
8312                       OpValues[2], OpValues[3], VT, MMO, ISD::UNINDEXED,
8313                       /* IsTruncating */ false, /*IsCompressing*/ false);
8314   DAG.setRoot(ST);
8315   setValue(&VPIntrin, ST);
8316 }
8317 
8318 void SelectionDAGBuilder::visitVPScatter(
8319     const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8320   SDLoc DL = getCurSDLoc();
8321   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8322   Value *PtrOperand = VPIntrin.getArgOperand(1);
8323   EVT VT = OpValues[0].getValueType();
8324   MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8325   AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8326   SDValue ST;
8327   if (!Alignment)
8328     Alignment = DAG.getEVTAlign(VT.getScalarType());
8329   unsigned AS =
8330       PtrOperand->getType()->getScalarType()->getPointerAddressSpace();
8331   MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8332       MachinePointerInfo(AS), MachineMemOperand::MOStore,
8333       LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo);
8334   SDValue Base, Index, Scale;
8335   ISD::MemIndexType IndexType;
8336   bool UniformBase = getUniformBase(PtrOperand, Base, Index, IndexType, Scale,
8337                                     this, VPIntrin.getParent(),
8338                                     VT.getScalarStoreSize());
8339   if (!UniformBase) {
8340     Base = DAG.getConstant(0, DL, TLI.getPointerTy(DAG.getDataLayout()));
8341     Index = getValue(PtrOperand);
8342     IndexType = ISD::SIGNED_SCALED;
8343     Scale =
8344       DAG.getTargetConstant(1, DL, TLI.getPointerTy(DAG.getDataLayout()));
8345   }
8346   EVT IdxVT = Index.getValueType();
8347   EVT EltTy = IdxVT.getVectorElementType();
8348   if (TLI.shouldExtendGSIndex(IdxVT, EltTy)) {
8349     EVT NewIdxVT = IdxVT.changeVectorElementType(EltTy);
8350     Index = DAG.getNode(ISD::SIGN_EXTEND, DL, NewIdxVT, Index);
8351   }
8352   ST = DAG.getScatterVP(DAG.getVTList(MVT::Other), VT, DL,
8353                         {getMemoryRoot(), OpValues[0], Base, Index, Scale,
8354                          OpValues[2], OpValues[3]},
8355                         MMO, IndexType);
8356   DAG.setRoot(ST);
8357   setValue(&VPIntrin, ST);
8358 }
8359 
8360 void SelectionDAGBuilder::visitVPStridedLoad(
8361     const VPIntrinsic &VPIntrin, EVT VT,
8362     const SmallVectorImpl<SDValue> &OpValues) {
8363   SDLoc DL = getCurSDLoc();
8364   Value *PtrOperand = VPIntrin.getArgOperand(0);
8365   MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8366   if (!Alignment)
8367     Alignment = DAG.getEVTAlign(VT.getScalarType());
8368   AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8369   const MDNode *Ranges = getRangeMetadata(VPIntrin);
8370   MemoryLocation ML = MemoryLocation::getAfter(PtrOperand, AAInfo);
8371   bool AddToChain = !AA || !AA->pointsToConstantMemory(ML);
8372   SDValue InChain = AddToChain ? DAG.getRoot() : DAG.getEntryNode();
8373   unsigned AS = PtrOperand->getType()->getPointerAddressSpace();
8374   MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8375       MachinePointerInfo(AS), MachineMemOperand::MOLoad,
8376       LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo, Ranges);
8377 
8378   SDValue LD = DAG.getStridedLoadVP(VT, DL, InChain, OpValues[0], OpValues[1],
8379                                     OpValues[2], OpValues[3], MMO,
8380                                     false /*IsExpanding*/);
8381 
8382   if (AddToChain)
8383     PendingLoads.push_back(LD.getValue(1));
8384   setValue(&VPIntrin, LD);
8385 }
8386 
8387 void SelectionDAGBuilder::visitVPStridedStore(
8388     const VPIntrinsic &VPIntrin, const SmallVectorImpl<SDValue> &OpValues) {
8389   SDLoc DL = getCurSDLoc();
8390   Value *PtrOperand = VPIntrin.getArgOperand(1);
8391   EVT VT = OpValues[0].getValueType();
8392   MaybeAlign Alignment = VPIntrin.getPointerAlignment();
8393   if (!Alignment)
8394     Alignment = DAG.getEVTAlign(VT.getScalarType());
8395   AAMDNodes AAInfo = VPIntrin.getAAMetadata();
8396   unsigned AS = PtrOperand->getType()->getPointerAddressSpace();
8397   MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
8398       MachinePointerInfo(AS), MachineMemOperand::MOStore,
8399       LocationSize::beforeOrAfterPointer(), *Alignment, AAInfo);
8400 
8401   SDValue ST = DAG.getStridedStoreVP(
8402       getMemoryRoot(), DL, OpValues[0], OpValues[1],
8403       DAG.getUNDEF(OpValues[1].getValueType()), OpValues[2], OpValues[3],
8404       OpValues[4], VT, MMO, ISD::UNINDEXED, /*IsTruncating*/ false,
8405       /*IsCompressing*/ false);
8406 
8407   DAG.setRoot(ST);
8408   setValue(&VPIntrin, ST);
8409 }
8410 
8411 void SelectionDAGBuilder::visitVPCmp(const VPCmpIntrinsic &VPIntrin) {
8412   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8413   SDLoc DL = getCurSDLoc();
8414 
8415   ISD::CondCode Condition;
8416   CmpInst::Predicate CondCode = VPIntrin.getPredicate();
8417   bool IsFP = VPIntrin.getOperand(0)->getType()->isFPOrFPVectorTy();
8418   if (IsFP) {
8419     // FIXME: Regular fcmps are FPMathOperators which may have fast-math (nnan)
8420     // flags, but calls that don't return floating-point types can't be
8421     // FPMathOperators, like vp.fcmp. This affects constrained fcmp too.
8422     Condition = getFCmpCondCode(CondCode);
8423     if (TM.Options.NoNaNsFPMath)
8424       Condition = getFCmpCodeWithoutNaN(Condition);
8425   } else {
8426     Condition = getICmpCondCode(CondCode);
8427   }
8428 
8429   SDValue Op1 = getValue(VPIntrin.getOperand(0));
8430   SDValue Op2 = getValue(VPIntrin.getOperand(1));
8431   // #2 is the condition code
8432   SDValue MaskOp = getValue(VPIntrin.getOperand(3));
8433   SDValue EVL = getValue(VPIntrin.getOperand(4));
8434   MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
8435   assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
8436          "Unexpected target EVL type");
8437   EVL = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, EVL);
8438 
8439   EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
8440                                                         VPIntrin.getType());
8441   setValue(&VPIntrin,
8442            DAG.getSetCCVP(DL, DestVT, Op1, Op2, Condition, MaskOp, EVL));
8443 }
8444 
8445 void SelectionDAGBuilder::visitVectorPredicationIntrinsic(
8446     const VPIntrinsic &VPIntrin) {
8447   SDLoc DL = getCurSDLoc();
8448   unsigned Opcode = getISDForVPIntrinsic(VPIntrin);
8449 
8450   auto IID = VPIntrin.getIntrinsicID();
8451 
8452   if (const auto *CmpI = dyn_cast<VPCmpIntrinsic>(&VPIntrin))
8453     return visitVPCmp(*CmpI);
8454 
8455   SmallVector<EVT, 4> ValueVTs;
8456   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8457   ComputeValueVTs(TLI, DAG.getDataLayout(), VPIntrin.getType(), ValueVTs);
8458   SDVTList VTs = DAG.getVTList(ValueVTs);
8459 
8460   auto EVLParamPos = VPIntrinsic::getVectorLengthParamPos(IID);
8461 
8462   MVT EVLParamVT = TLI.getVPExplicitVectorLengthTy();
8463   assert(EVLParamVT.isScalarInteger() && EVLParamVT.bitsGE(MVT::i32) &&
8464          "Unexpected target EVL type");
8465 
8466   // Request operands.
8467   SmallVector<SDValue, 7> OpValues;
8468   for (unsigned I = 0; I < VPIntrin.arg_size(); ++I) {
8469     auto Op = getValue(VPIntrin.getArgOperand(I));
8470     if (I == EVLParamPos)
8471       Op = DAG.getNode(ISD::ZERO_EXTEND, DL, EVLParamVT, Op);
8472     OpValues.push_back(Op);
8473   }
8474 
8475   switch (Opcode) {
8476   default: {
8477     SDNodeFlags SDFlags;
8478     if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin))
8479       SDFlags.copyFMF(*FPMO);
8480     SDValue Result = DAG.getNode(Opcode, DL, VTs, OpValues, SDFlags);
8481     setValue(&VPIntrin, Result);
8482     break;
8483   }
8484   case ISD::VP_LOAD:
8485     visitVPLoad(VPIntrin, ValueVTs[0], OpValues);
8486     break;
8487   case ISD::VP_GATHER:
8488     visitVPGather(VPIntrin, ValueVTs[0], OpValues);
8489     break;
8490   case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
8491     visitVPStridedLoad(VPIntrin, ValueVTs[0], OpValues);
8492     break;
8493   case ISD::VP_STORE:
8494     visitVPStore(VPIntrin, OpValues);
8495     break;
8496   case ISD::VP_SCATTER:
8497     visitVPScatter(VPIntrin, OpValues);
8498     break;
8499   case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
8500     visitVPStridedStore(VPIntrin, OpValues);
8501     break;
8502   case ISD::VP_FMULADD: {
8503     assert(OpValues.size() == 5 && "Unexpected number of operands");
8504     SDNodeFlags SDFlags;
8505     if (auto *FPMO = dyn_cast<FPMathOperator>(&VPIntrin))
8506       SDFlags.copyFMF(*FPMO);
8507     if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
8508         TLI.isFMAFasterThanFMulAndFAdd(DAG.getMachineFunction(), ValueVTs[0])) {
8509       setValue(&VPIntrin, DAG.getNode(ISD::VP_FMA, DL, VTs, OpValues, SDFlags));
8510     } else {
8511       SDValue Mul = DAG.getNode(
8512           ISD::VP_FMUL, DL, VTs,
8513           {OpValues[0], OpValues[1], OpValues[3], OpValues[4]}, SDFlags);
8514       SDValue Add =
8515           DAG.getNode(ISD::VP_FADD, DL, VTs,
8516                       {Mul, OpValues[2], OpValues[3], OpValues[4]}, SDFlags);
8517       setValue(&VPIntrin, Add);
8518     }
8519     break;
8520   }
8521   case ISD::VP_IS_FPCLASS: {
8522     const DataLayout DLayout = DAG.getDataLayout();
8523     EVT DestVT = TLI.getValueType(DLayout, VPIntrin.getType());
8524     auto Constant = OpValues[1]->getAsZExtVal();
8525     SDValue Check = DAG.getTargetConstant(Constant, DL, MVT::i32);
8526     SDValue V = DAG.getNode(ISD::VP_IS_FPCLASS, DL, DestVT,
8527                             {OpValues[0], Check, OpValues[2], OpValues[3]});
8528     setValue(&VPIntrin, V);
8529     return;
8530   }
8531   case ISD::VP_INTTOPTR: {
8532     SDValue N = OpValues[0];
8533     EVT DestVT = TLI.getValueType(DAG.getDataLayout(), VPIntrin.getType());
8534     EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(), VPIntrin.getType());
8535     N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1],
8536                                OpValues[2]);
8537     N = DAG.getVPZExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1],
8538                              OpValues[2]);
8539     setValue(&VPIntrin, N);
8540     break;
8541   }
8542   case ISD::VP_PTRTOINT: {
8543     SDValue N = OpValues[0];
8544     EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
8545                                                           VPIntrin.getType());
8546     EVT PtrMemVT = TLI.getMemValueType(DAG.getDataLayout(),
8547                                        VPIntrin.getOperand(0)->getType());
8548     N = DAG.getVPPtrExtOrTrunc(getCurSDLoc(), PtrMemVT, N, OpValues[1],
8549                                OpValues[2]);
8550     N = DAG.getVPZExtOrTrunc(getCurSDLoc(), DestVT, N, OpValues[1],
8551                              OpValues[2]);
8552     setValue(&VPIntrin, N);
8553     break;
8554   }
8555   case ISD::VP_ABS:
8556   case ISD::VP_CTLZ:
8557   case ISD::VP_CTLZ_ZERO_UNDEF:
8558   case ISD::VP_CTTZ:
8559   case ISD::VP_CTTZ_ZERO_UNDEF:
8560   case ISD::VP_CTTZ_ELTS_ZERO_UNDEF:
8561   case ISD::VP_CTTZ_ELTS: {
8562     SDValue Result =
8563         DAG.getNode(Opcode, DL, VTs, {OpValues[0], OpValues[2], OpValues[3]});
8564     setValue(&VPIntrin, Result);
8565     break;
8566   }
8567   }
8568 }
8569 
8570 SDValue SelectionDAGBuilder::lowerStartEH(SDValue Chain,
8571                                           const BasicBlock *EHPadBB,
8572                                           MCSymbol *&BeginLabel) {
8573   MachineFunction &MF = DAG.getMachineFunction();
8574   MachineModuleInfo &MMI = MF.getMMI();
8575 
8576   // Insert a label before the invoke call to mark the try range.  This can be
8577   // used to detect deletion of the invoke via the MachineModuleInfo.
8578   BeginLabel = MMI.getContext().createTempSymbol();
8579 
8580   // For SjLj, keep track of which landing pads go with which invokes
8581   // so as to maintain the ordering of pads in the LSDA.
8582   unsigned CallSiteIndex = MMI.getCurrentCallSite();
8583   if (CallSiteIndex) {
8584     MF.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
8585     LPadToCallSiteMap[FuncInfo.MBBMap[EHPadBB]].push_back(CallSiteIndex);
8586 
8587     // Now that the call site is handled, stop tracking it.
8588     MMI.setCurrentCallSite(0);
8589   }
8590 
8591   return DAG.getEHLabel(getCurSDLoc(), Chain, BeginLabel);
8592 }
8593 
8594 SDValue SelectionDAGBuilder::lowerEndEH(SDValue Chain, const InvokeInst *II,
8595                                         const BasicBlock *EHPadBB,
8596                                         MCSymbol *BeginLabel) {
8597   assert(BeginLabel && "BeginLabel should've been set");
8598 
8599   MachineFunction &MF = DAG.getMachineFunction();
8600   MachineModuleInfo &MMI = MF.getMMI();
8601 
8602   // Insert a label at the end of the invoke call to mark the try range.  This
8603   // can be used to detect deletion of the invoke via the MachineModuleInfo.
8604   MCSymbol *EndLabel = MMI.getContext().createTempSymbol();
8605   Chain = DAG.getEHLabel(getCurSDLoc(), Chain, EndLabel);
8606 
8607   // Inform MachineModuleInfo of range.
8608   auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
8609   // There is a platform (e.g. wasm) that uses funclet style IR but does not
8610   // actually use outlined funclets and their LSDA info style.
8611   if (MF.hasEHFunclets() && isFuncletEHPersonality(Pers)) {
8612     assert(II && "II should've been set");
8613     WinEHFuncInfo *EHInfo = MF.getWinEHFuncInfo();
8614     EHInfo->addIPToStateRange(II, BeginLabel, EndLabel);
8615   } else if (!isScopedEHPersonality(Pers)) {
8616     assert(EHPadBB);
8617     MF.addInvoke(FuncInfo.MBBMap[EHPadBB], BeginLabel, EndLabel);
8618   }
8619 
8620   return Chain;
8621 }
8622 
8623 std::pair<SDValue, SDValue>
8624 SelectionDAGBuilder::lowerInvokable(TargetLowering::CallLoweringInfo &CLI,
8625                                     const BasicBlock *EHPadBB) {
8626   MCSymbol *BeginLabel = nullptr;
8627 
8628   if (EHPadBB) {
8629     // Both PendingLoads and PendingExports must be flushed here;
8630     // this call might not return.
8631     (void)getRoot();
8632     DAG.setRoot(lowerStartEH(getControlRoot(), EHPadBB, BeginLabel));
8633     CLI.setChain(getRoot());
8634   }
8635 
8636   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8637   std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
8638 
8639   assert((CLI.IsTailCall || Result.second.getNode()) &&
8640          "Non-null chain expected with non-tail call!");
8641   assert((Result.second.getNode() || !Result.first.getNode()) &&
8642          "Null value expected with tail call!");
8643 
8644   if (!Result.second.getNode()) {
8645     // As a special case, a null chain means that a tail call has been emitted
8646     // and the DAG root is already updated.
8647     HasTailCall = true;
8648 
8649     // Since there's no actual continuation from this block, nothing can be
8650     // relying on us setting vregs for them.
8651     PendingExports.clear();
8652   } else {
8653     DAG.setRoot(Result.second);
8654   }
8655 
8656   if (EHPadBB) {
8657     DAG.setRoot(lowerEndEH(getRoot(), cast_or_null<InvokeInst>(CLI.CB), EHPadBB,
8658                            BeginLabel));
8659     Result.second = getRoot();
8660   }
8661 
8662   return Result;
8663 }
8664 
8665 void SelectionDAGBuilder::LowerCallTo(const CallBase &CB, SDValue Callee,
8666                                       bool isTailCall, bool isMustTailCall,
8667                                       const BasicBlock *EHPadBB,
8668                                       const TargetLowering::PtrAuthInfo *PAI) {
8669   auto &DL = DAG.getDataLayout();
8670   FunctionType *FTy = CB.getFunctionType();
8671   Type *RetTy = CB.getType();
8672 
8673   TargetLowering::ArgListTy Args;
8674   Args.reserve(CB.arg_size());
8675 
8676   const Value *SwiftErrorVal = nullptr;
8677   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8678 
8679   if (isTailCall) {
8680     // Avoid emitting tail calls in functions with the disable-tail-calls
8681     // attribute.
8682     auto *Caller = CB.getParent()->getParent();
8683     if (Caller->getFnAttribute("disable-tail-calls").getValueAsString() ==
8684         "true" && !isMustTailCall)
8685       isTailCall = false;
8686 
8687     // We can't tail call inside a function with a swifterror argument. Lowering
8688     // does not support this yet. It would have to move into the swifterror
8689     // register before the call.
8690     if (TLI.supportSwiftError() &&
8691         Caller->getAttributes().hasAttrSomewhere(Attribute::SwiftError))
8692       isTailCall = false;
8693   }
8694 
8695   for (auto I = CB.arg_begin(), E = CB.arg_end(); I != E; ++I) {
8696     TargetLowering::ArgListEntry Entry;
8697     const Value *V = *I;
8698 
8699     // Skip empty types
8700     if (V->getType()->isEmptyTy())
8701       continue;
8702 
8703     SDValue ArgNode = getValue(V);
8704     Entry.Node = ArgNode; Entry.Ty = V->getType();
8705 
8706     Entry.setAttributes(&CB, I - CB.arg_begin());
8707 
8708     // Use swifterror virtual register as input to the call.
8709     if (Entry.IsSwiftError && TLI.supportSwiftError()) {
8710       SwiftErrorVal = V;
8711       // We find the virtual register for the actual swifterror argument.
8712       // Instead of using the Value, we use the virtual register instead.
8713       Entry.Node =
8714           DAG.getRegister(SwiftError.getOrCreateVRegUseAt(&CB, FuncInfo.MBB, V),
8715                           EVT(TLI.getPointerTy(DL)));
8716     }
8717 
8718     Args.push_back(Entry);
8719 
8720     // If we have an explicit sret argument that is an Instruction, (i.e., it
8721     // might point to function-local memory), we can't meaningfully tail-call.
8722     if (Entry.IsSRet && isa<Instruction>(V))
8723       isTailCall = false;
8724   }
8725 
8726   // If call site has a cfguardtarget operand bundle, create and add an
8727   // additional ArgListEntry.
8728   if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_cfguardtarget)) {
8729     TargetLowering::ArgListEntry Entry;
8730     Value *V = Bundle->Inputs[0];
8731     SDValue ArgNode = getValue(V);
8732     Entry.Node = ArgNode;
8733     Entry.Ty = V->getType();
8734     Entry.IsCFGuardTarget = true;
8735     Args.push_back(Entry);
8736   }
8737 
8738   // Check if target-independent constraints permit a tail call here.
8739   // Target-dependent constraints are checked within TLI->LowerCallTo.
8740   if (isTailCall && !isInTailCallPosition(CB, DAG.getTarget()))
8741     isTailCall = false;
8742 
8743   // Disable tail calls if there is an swifterror argument. Targets have not
8744   // been updated to support tail calls.
8745   if (TLI.supportSwiftError() && SwiftErrorVal)
8746     isTailCall = false;
8747 
8748   ConstantInt *CFIType = nullptr;
8749   if (CB.isIndirectCall()) {
8750     if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_kcfi)) {
8751       if (!TLI.supportKCFIBundles())
8752         report_fatal_error(
8753             "Target doesn't support calls with kcfi operand bundles.");
8754       CFIType = cast<ConstantInt>(Bundle->Inputs[0]);
8755       assert(CFIType->getType()->isIntegerTy(32) && "Invalid CFI type");
8756     }
8757   }
8758 
8759   SDValue ConvControlToken;
8760   if (auto Bundle = CB.getOperandBundle(LLVMContext::OB_convergencectrl)) {
8761     auto *Token = Bundle->Inputs[0].get();
8762     ConvControlToken = getValue(Token);
8763   }
8764 
8765   TargetLowering::CallLoweringInfo CLI(DAG);
8766   CLI.setDebugLoc(getCurSDLoc())
8767       .setChain(getRoot())
8768       .setCallee(RetTy, FTy, Callee, std::move(Args), CB)
8769       .setTailCall(isTailCall)
8770       .setConvergent(CB.isConvergent())
8771       .setIsPreallocated(
8772           CB.countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0)
8773       .setCFIType(CFIType)
8774       .setConvergenceControlToken(ConvControlToken);
8775 
8776   // Set the pointer authentication info if we have it.
8777   if (PAI) {
8778     if (!TLI.supportPtrAuthBundles())
8779       report_fatal_error(
8780           "This target doesn't support calls with ptrauth operand bundles.");
8781     CLI.setPtrAuth(*PAI);
8782   }
8783 
8784   std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
8785 
8786   if (Result.first.getNode()) {
8787     Result.first = lowerRangeToAssertZExt(DAG, CB, Result.first);
8788     setValue(&CB, Result.first);
8789   }
8790 
8791   // The last element of CLI.InVals has the SDValue for swifterror return.
8792   // Here we copy it to a virtual register and update SwiftErrorMap for
8793   // book-keeping.
8794   if (SwiftErrorVal && TLI.supportSwiftError()) {
8795     // Get the last element of InVals.
8796     SDValue Src = CLI.InVals.back();
8797     Register VReg =
8798         SwiftError.getOrCreateVRegDefAt(&CB, FuncInfo.MBB, SwiftErrorVal);
8799     SDValue CopyNode = CLI.DAG.getCopyToReg(Result.second, CLI.DL, VReg, Src);
8800     DAG.setRoot(CopyNode);
8801   }
8802 }
8803 
8804 static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
8805                              SelectionDAGBuilder &Builder) {
8806   // Check to see if this load can be trivially constant folded, e.g. if the
8807   // input is from a string literal.
8808   if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
8809     // Cast pointer to the type we really want to load.
8810     Type *LoadTy =
8811         Type::getIntNTy(PtrVal->getContext(), LoadVT.getScalarSizeInBits());
8812     if (LoadVT.isVector())
8813       LoadTy = FixedVectorType::get(LoadTy, LoadVT.getVectorNumElements());
8814 
8815     LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput),
8816                                          PointerType::getUnqual(LoadTy));
8817 
8818     if (const Constant *LoadCst =
8819             ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
8820                                          LoadTy, Builder.DAG.getDataLayout()))
8821       return Builder.getValue(LoadCst);
8822   }
8823 
8824   // Otherwise, we have to emit the load.  If the pointer is to unfoldable but
8825   // still constant memory, the input chain can be the entry node.
8826   SDValue Root;
8827   bool ConstantMemory = false;
8828 
8829   // Do not serialize (non-volatile) loads of constant memory with anything.
8830   if (Builder.AA && Builder.AA->pointsToConstantMemory(PtrVal)) {
8831     Root = Builder.DAG.getEntryNode();
8832     ConstantMemory = true;
8833   } else {
8834     // Do not serialize non-volatile loads against each other.
8835     Root = Builder.DAG.getRoot();
8836   }
8837 
8838   SDValue Ptr = Builder.getValue(PtrVal);
8839   SDValue LoadVal =
8840       Builder.DAG.getLoad(LoadVT, Builder.getCurSDLoc(), Root, Ptr,
8841                           MachinePointerInfo(PtrVal), Align(1));
8842 
8843   if (!ConstantMemory)
8844     Builder.PendingLoads.push_back(LoadVal.getValue(1));
8845   return LoadVal;
8846 }
8847 
8848 /// Record the value for an instruction that produces an integer result,
8849 /// converting the type where necessary.
8850 void SelectionDAGBuilder::processIntegerCallValue(const Instruction &I,
8851                                                   SDValue Value,
8852                                                   bool IsSigned) {
8853   EVT VT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
8854                                                     I.getType(), true);
8855   Value = DAG.getExtOrTrunc(IsSigned, Value, getCurSDLoc(), VT);
8856   setValue(&I, Value);
8857 }
8858 
8859 /// See if we can lower a memcmp/bcmp call into an optimized form. If so, return
8860 /// true and lower it. Otherwise return false, and it will be lowered like a
8861 /// normal call.
8862 /// The caller already checked that \p I calls the appropriate LibFunc with a
8863 /// correct prototype.
8864 bool SelectionDAGBuilder::visitMemCmpBCmpCall(const CallInst &I) {
8865   const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
8866   const Value *Size = I.getArgOperand(2);
8867   const ConstantSDNode *CSize = dyn_cast<ConstantSDNode>(getValue(Size));
8868   if (CSize && CSize->getZExtValue() == 0) {
8869     EVT CallVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
8870                                                           I.getType(), true);
8871     setValue(&I, DAG.getConstant(0, getCurSDLoc(), CallVT));
8872     return true;
8873   }
8874 
8875   const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
8876   std::pair<SDValue, SDValue> Res = TSI.EmitTargetCodeForMemcmp(
8877       DAG, getCurSDLoc(), DAG.getRoot(), getValue(LHS), getValue(RHS),
8878       getValue(Size), MachinePointerInfo(LHS), MachinePointerInfo(RHS));
8879   if (Res.first.getNode()) {
8880     processIntegerCallValue(I, Res.first, true);
8881     PendingLoads.push_back(Res.second);
8882     return true;
8883   }
8884 
8885   // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS)  != 0
8886   // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS)  != 0
8887   if (!CSize || !isOnlyUsedInZeroEqualityComparison(&I))
8888     return false;
8889 
8890   // If the target has a fast compare for the given size, it will return a
8891   // preferred load type for that size. Require that the load VT is legal and
8892   // that the target supports unaligned loads of that type. Otherwise, return
8893   // INVALID.
8894   auto hasFastLoadsAndCompare = [&](unsigned NumBits) {
8895     const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8896     MVT LVT = TLI.hasFastEqualityCompare(NumBits);
8897     if (LVT != MVT::INVALID_SIMPLE_VALUE_TYPE) {
8898       // TODO: Handle 5 byte compare as 4-byte + 1 byte.
8899       // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
8900       // TODO: Check alignment of src and dest ptrs.
8901       unsigned DstAS = LHS->getType()->getPointerAddressSpace();
8902       unsigned SrcAS = RHS->getType()->getPointerAddressSpace();
8903       if (!TLI.isTypeLegal(LVT) ||
8904           !TLI.allowsMisalignedMemoryAccesses(LVT, SrcAS) ||
8905           !TLI.allowsMisalignedMemoryAccesses(LVT, DstAS))
8906         LVT = MVT::INVALID_SIMPLE_VALUE_TYPE;
8907     }
8908 
8909     return LVT;
8910   };
8911 
8912   // This turns into unaligned loads. We only do this if the target natively
8913   // supports the MVT we'll be loading or if it is small enough (<= 4) that
8914   // we'll only produce a small number of byte loads.
8915   MVT LoadVT;
8916   unsigned NumBitsToCompare = CSize->getZExtValue() * 8;
8917   switch (NumBitsToCompare) {
8918   default:
8919     return false;
8920   case 16:
8921     LoadVT = MVT::i16;
8922     break;
8923   case 32:
8924     LoadVT = MVT::i32;
8925     break;
8926   case 64:
8927   case 128:
8928   case 256:
8929     LoadVT = hasFastLoadsAndCompare(NumBitsToCompare);
8930     break;
8931   }
8932 
8933   if (LoadVT == MVT::INVALID_SIMPLE_VALUE_TYPE)
8934     return false;
8935 
8936   SDValue LoadL = getMemCmpLoad(LHS, LoadVT, *this);
8937   SDValue LoadR = getMemCmpLoad(RHS, LoadVT, *this);
8938 
8939   // Bitcast to a wide integer type if the loads are vectors.
8940   if (LoadVT.isVector()) {
8941     EVT CmpVT = EVT::getIntegerVT(LHS->getContext(), LoadVT.getSizeInBits());
8942     LoadL = DAG.getBitcast(CmpVT, LoadL);
8943     LoadR = DAG.getBitcast(CmpVT, LoadR);
8944   }
8945 
8946   SDValue Cmp = DAG.getSetCC(getCurSDLoc(), MVT::i1, LoadL, LoadR, ISD::SETNE);
8947   processIntegerCallValue(I, Cmp, false);
8948   return true;
8949 }
8950 
8951 /// See if we can lower a memchr call into an optimized form. If so, return
8952 /// true and lower it. Otherwise return false, and it will be lowered like a
8953 /// normal call.
8954 /// The caller already checked that \p I calls the appropriate LibFunc with a
8955 /// correct prototype.
8956 bool SelectionDAGBuilder::visitMemChrCall(const CallInst &I) {
8957   const Value *Src = I.getArgOperand(0);
8958   const Value *Char = I.getArgOperand(1);
8959   const Value *Length = I.getArgOperand(2);
8960 
8961   const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
8962   std::pair<SDValue, SDValue> Res =
8963     TSI.EmitTargetCodeForMemchr(DAG, getCurSDLoc(), DAG.getRoot(),
8964                                 getValue(Src), getValue(Char), getValue(Length),
8965                                 MachinePointerInfo(Src));
8966   if (Res.first.getNode()) {
8967     setValue(&I, Res.first);
8968     PendingLoads.push_back(Res.second);
8969     return true;
8970   }
8971 
8972   return false;
8973 }
8974 
8975 /// See if we can lower a mempcpy call into an optimized form. If so, return
8976 /// true and lower it. Otherwise return false, and it will be lowered like a
8977 /// normal call.
8978 /// The caller already checked that \p I calls the appropriate LibFunc with a
8979 /// correct prototype.
8980 bool SelectionDAGBuilder::visitMemPCpyCall(const CallInst &I) {
8981   SDValue Dst = getValue(I.getArgOperand(0));
8982   SDValue Src = getValue(I.getArgOperand(1));
8983   SDValue Size = getValue(I.getArgOperand(2));
8984 
8985   Align DstAlign = DAG.InferPtrAlign(Dst).valueOrOne();
8986   Align SrcAlign = DAG.InferPtrAlign(Src).valueOrOne();
8987   // DAG::getMemcpy needs Alignment to be defined.
8988   Align Alignment = std::min(DstAlign, SrcAlign);
8989 
8990   SDLoc sdl = getCurSDLoc();
8991 
8992   // In the mempcpy context we need to pass in a false value for isTailCall
8993   // because the return pointer needs to be adjusted by the size of
8994   // the copied memory.
8995   SDValue Root = getMemoryRoot();
8996   SDValue MC = DAG.getMemcpy(Root, sdl, Dst, Src, Size, Alignment, false, false,
8997                              /*isTailCall=*/false,
8998                              MachinePointerInfo(I.getArgOperand(0)),
8999                              MachinePointerInfo(I.getArgOperand(1)),
9000                              I.getAAMetadata());
9001   assert(MC.getNode() != nullptr &&
9002          "** memcpy should not be lowered as TailCall in mempcpy context **");
9003   DAG.setRoot(MC);
9004 
9005   // Check if Size needs to be truncated or extended.
9006   Size = DAG.getSExtOrTrunc(Size, sdl, Dst.getValueType());
9007 
9008   // Adjust return pointer to point just past the last dst byte.
9009   SDValue DstPlusSize = DAG.getNode(ISD::ADD, sdl, Dst.getValueType(),
9010                                     Dst, Size);
9011   setValue(&I, DstPlusSize);
9012   return true;
9013 }
9014 
9015 /// See if we can lower a strcpy call into an optimized form.  If so, return
9016 /// true and lower it, otherwise return false and it will be lowered like a
9017 /// normal call.
9018 /// The caller already checked that \p I calls the appropriate LibFunc with a
9019 /// correct prototype.
9020 bool SelectionDAGBuilder::visitStrCpyCall(const CallInst &I, bool isStpcpy) {
9021   const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9022 
9023   const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9024   std::pair<SDValue, SDValue> Res =
9025     TSI.EmitTargetCodeForStrcpy(DAG, getCurSDLoc(), getRoot(),
9026                                 getValue(Arg0), getValue(Arg1),
9027                                 MachinePointerInfo(Arg0),
9028                                 MachinePointerInfo(Arg1), isStpcpy);
9029   if (Res.first.getNode()) {
9030     setValue(&I, Res.first);
9031     DAG.setRoot(Res.second);
9032     return true;
9033   }
9034 
9035   return false;
9036 }
9037 
9038 /// See if we can lower a strcmp call into an optimized form.  If so, return
9039 /// true and lower it, otherwise return false and it will be lowered like a
9040 /// normal call.
9041 /// The caller already checked that \p I calls the appropriate LibFunc with a
9042 /// correct prototype.
9043 bool SelectionDAGBuilder::visitStrCmpCall(const CallInst &I) {
9044   const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9045 
9046   const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9047   std::pair<SDValue, SDValue> Res =
9048     TSI.EmitTargetCodeForStrcmp(DAG, getCurSDLoc(), DAG.getRoot(),
9049                                 getValue(Arg0), getValue(Arg1),
9050                                 MachinePointerInfo(Arg0),
9051                                 MachinePointerInfo(Arg1));
9052   if (Res.first.getNode()) {
9053     processIntegerCallValue(I, Res.first, true);
9054     PendingLoads.push_back(Res.second);
9055     return true;
9056   }
9057 
9058   return false;
9059 }
9060 
9061 /// See if we can lower a strlen call into an optimized form.  If so, return
9062 /// true and lower it, otherwise return false and it will be lowered like a
9063 /// normal call.
9064 /// The caller already checked that \p I calls the appropriate LibFunc with a
9065 /// correct prototype.
9066 bool SelectionDAGBuilder::visitStrLenCall(const CallInst &I) {
9067   const Value *Arg0 = I.getArgOperand(0);
9068 
9069   const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9070   std::pair<SDValue, SDValue> Res =
9071     TSI.EmitTargetCodeForStrlen(DAG, getCurSDLoc(), DAG.getRoot(),
9072                                 getValue(Arg0), MachinePointerInfo(Arg0));
9073   if (Res.first.getNode()) {
9074     processIntegerCallValue(I, Res.first, false);
9075     PendingLoads.push_back(Res.second);
9076     return true;
9077   }
9078 
9079   return false;
9080 }
9081 
9082 /// See if we can lower a strnlen call into an optimized form.  If so, return
9083 /// true and lower it, otherwise return false and it will be lowered like a
9084 /// normal call.
9085 /// The caller already checked that \p I calls the appropriate LibFunc with a
9086 /// correct prototype.
9087 bool SelectionDAGBuilder::visitStrNLenCall(const CallInst &I) {
9088   const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
9089 
9090   const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
9091   std::pair<SDValue, SDValue> Res =
9092     TSI.EmitTargetCodeForStrnlen(DAG, getCurSDLoc(), DAG.getRoot(),
9093                                  getValue(Arg0), getValue(Arg1),
9094                                  MachinePointerInfo(Arg0));
9095   if (Res.first.getNode()) {
9096     processIntegerCallValue(I, Res.first, false);
9097     PendingLoads.push_back(Res.second);
9098     return true;
9099   }
9100 
9101   return false;
9102 }
9103 
9104 /// See if we can lower a unary floating-point operation into an SDNode with
9105 /// the specified Opcode.  If so, return true and lower it, otherwise return
9106 /// false and it will be lowered like a normal call.
9107 /// The caller already checked that \p I calls the appropriate LibFunc with a
9108 /// correct prototype.
9109 bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I,
9110                                               unsigned Opcode) {
9111   // We already checked this call's prototype; verify it doesn't modify errno.
9112   if (!I.onlyReadsMemory())
9113     return false;
9114 
9115   SDNodeFlags Flags;
9116   Flags.copyFMF(cast<FPMathOperator>(I));
9117 
9118   SDValue Tmp = getValue(I.getArgOperand(0));
9119   setValue(&I,
9120            DAG.getNode(Opcode, getCurSDLoc(), Tmp.getValueType(), Tmp, Flags));
9121   return true;
9122 }
9123 
9124 /// See if we can lower a binary floating-point operation into an SDNode with
9125 /// the specified Opcode. If so, return true and lower it. Otherwise return
9126 /// false, and it will be lowered like a normal call.
9127 /// The caller already checked that \p I calls the appropriate LibFunc with a
9128 /// correct prototype.
9129 bool SelectionDAGBuilder::visitBinaryFloatCall(const CallInst &I,
9130                                                unsigned Opcode) {
9131   // We already checked this call's prototype; verify it doesn't modify errno.
9132   if (!I.onlyReadsMemory())
9133     return false;
9134 
9135   SDNodeFlags Flags;
9136   Flags.copyFMF(cast<FPMathOperator>(I));
9137 
9138   SDValue Tmp0 = getValue(I.getArgOperand(0));
9139   SDValue Tmp1 = getValue(I.getArgOperand(1));
9140   EVT VT = Tmp0.getValueType();
9141   setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), VT, Tmp0, Tmp1, Flags));
9142   return true;
9143 }
9144 
9145 void SelectionDAGBuilder::visitCall(const CallInst &I) {
9146   // Handle inline assembly differently.
9147   if (I.isInlineAsm()) {
9148     visitInlineAsm(I);
9149     return;
9150   }
9151 
9152   diagnoseDontCall(I);
9153 
9154   if (Function *F = I.getCalledFunction()) {
9155     if (F->isDeclaration()) {
9156       // Is this an LLVM intrinsic or a target-specific intrinsic?
9157       unsigned IID = F->getIntrinsicID();
9158       if (!IID)
9159         if (const TargetIntrinsicInfo *II = TM.getIntrinsicInfo())
9160           IID = II->getIntrinsicID(F);
9161 
9162       if (IID) {
9163         visitIntrinsicCall(I, IID);
9164         return;
9165       }
9166     }
9167 
9168     // Check for well-known libc/libm calls.  If the function is internal, it
9169     // can't be a library call.  Don't do the check if marked as nobuiltin for
9170     // some reason or the call site requires strict floating point semantics.
9171     LibFunc Func;
9172     if (!I.isNoBuiltin() && !I.isStrictFP() && !F->hasLocalLinkage() &&
9173         F->hasName() && LibInfo->getLibFunc(*F, Func) &&
9174         LibInfo->hasOptimizedCodeGen(Func)) {
9175       switch (Func) {
9176       default: break;
9177       case LibFunc_bcmp:
9178         if (visitMemCmpBCmpCall(I))
9179           return;
9180         break;
9181       case LibFunc_copysign:
9182       case LibFunc_copysignf:
9183       case LibFunc_copysignl:
9184         // We already checked this call's prototype; verify it doesn't modify
9185         // errno.
9186         if (I.onlyReadsMemory()) {
9187           SDValue LHS = getValue(I.getArgOperand(0));
9188           SDValue RHS = getValue(I.getArgOperand(1));
9189           setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurSDLoc(),
9190                                    LHS.getValueType(), LHS, RHS));
9191           return;
9192         }
9193         break;
9194       case LibFunc_fabs:
9195       case LibFunc_fabsf:
9196       case LibFunc_fabsl:
9197         if (visitUnaryFloatCall(I, ISD::FABS))
9198           return;
9199         break;
9200       case LibFunc_fmin:
9201       case LibFunc_fminf:
9202       case LibFunc_fminl:
9203         if (visitBinaryFloatCall(I, ISD::FMINNUM))
9204           return;
9205         break;
9206       case LibFunc_fmax:
9207       case LibFunc_fmaxf:
9208       case LibFunc_fmaxl:
9209         if (visitBinaryFloatCall(I, ISD::FMAXNUM))
9210           return;
9211         break;
9212       case LibFunc_sin:
9213       case LibFunc_sinf:
9214       case LibFunc_sinl:
9215         if (visitUnaryFloatCall(I, ISD::FSIN))
9216           return;
9217         break;
9218       case LibFunc_cos:
9219       case LibFunc_cosf:
9220       case LibFunc_cosl:
9221         if (visitUnaryFloatCall(I, ISD::FCOS))
9222           return;
9223         break;
9224       case LibFunc_tan:
9225       case LibFunc_tanf:
9226       case LibFunc_tanl:
9227         if (visitUnaryFloatCall(I, ISD::FTAN))
9228           return;
9229         break;
9230       case LibFunc_sqrt:
9231       case LibFunc_sqrtf:
9232       case LibFunc_sqrtl:
9233       case LibFunc_sqrt_finite:
9234       case LibFunc_sqrtf_finite:
9235       case LibFunc_sqrtl_finite:
9236         if (visitUnaryFloatCall(I, ISD::FSQRT))
9237           return;
9238         break;
9239       case LibFunc_floor:
9240       case LibFunc_floorf:
9241       case LibFunc_floorl:
9242         if (visitUnaryFloatCall(I, ISD::FFLOOR))
9243           return;
9244         break;
9245       case LibFunc_nearbyint:
9246       case LibFunc_nearbyintf:
9247       case LibFunc_nearbyintl:
9248         if (visitUnaryFloatCall(I, ISD::FNEARBYINT))
9249           return;
9250         break;
9251       case LibFunc_ceil:
9252       case LibFunc_ceilf:
9253       case LibFunc_ceill:
9254         if (visitUnaryFloatCall(I, ISD::FCEIL))
9255           return;
9256         break;
9257       case LibFunc_rint:
9258       case LibFunc_rintf:
9259       case LibFunc_rintl:
9260         if (visitUnaryFloatCall(I, ISD::FRINT))
9261           return;
9262         break;
9263       case LibFunc_round:
9264       case LibFunc_roundf:
9265       case LibFunc_roundl:
9266         if (visitUnaryFloatCall(I, ISD::FROUND))
9267           return;
9268         break;
9269       case LibFunc_trunc:
9270       case LibFunc_truncf:
9271       case LibFunc_truncl:
9272         if (visitUnaryFloatCall(I, ISD::FTRUNC))
9273           return;
9274         break;
9275       case LibFunc_log2:
9276       case LibFunc_log2f:
9277       case LibFunc_log2l:
9278         if (visitUnaryFloatCall(I, ISD::FLOG2))
9279           return;
9280         break;
9281       case LibFunc_exp2:
9282       case LibFunc_exp2f:
9283       case LibFunc_exp2l:
9284         if (visitUnaryFloatCall(I, ISD::FEXP2))
9285           return;
9286         break;
9287       case LibFunc_exp10:
9288       case LibFunc_exp10f:
9289       case LibFunc_exp10l:
9290         if (visitUnaryFloatCall(I, ISD::FEXP10))
9291           return;
9292         break;
9293       case LibFunc_ldexp:
9294       case LibFunc_ldexpf:
9295       case LibFunc_ldexpl:
9296         if (visitBinaryFloatCall(I, ISD::FLDEXP))
9297           return;
9298         break;
9299       case LibFunc_memcmp:
9300         if (visitMemCmpBCmpCall(I))
9301           return;
9302         break;
9303       case LibFunc_mempcpy:
9304         if (visitMemPCpyCall(I))
9305           return;
9306         break;
9307       case LibFunc_memchr:
9308         if (visitMemChrCall(I))
9309           return;
9310         break;
9311       case LibFunc_strcpy:
9312         if (visitStrCpyCall(I, false))
9313           return;
9314         break;
9315       case LibFunc_stpcpy:
9316         if (visitStrCpyCall(I, true))
9317           return;
9318         break;
9319       case LibFunc_strcmp:
9320         if (visitStrCmpCall(I))
9321           return;
9322         break;
9323       case LibFunc_strlen:
9324         if (visitStrLenCall(I))
9325           return;
9326         break;
9327       case LibFunc_strnlen:
9328         if (visitStrNLenCall(I))
9329           return;
9330         break;
9331       }
9332     }
9333   }
9334 
9335   if (I.countOperandBundlesOfType(LLVMContext::OB_ptrauth)) {
9336     LowerCallSiteWithPtrAuthBundle(cast<CallBase>(I), /*EHPadBB=*/nullptr);
9337     return;
9338   }
9339 
9340   // Deopt bundles are lowered in LowerCallSiteWithDeoptBundle, and we don't
9341   // have to do anything here to lower funclet bundles.
9342   // CFGuardTarget bundles are lowered in LowerCallTo.
9343   assert(!I.hasOperandBundlesOtherThan(
9344              {LLVMContext::OB_deopt, LLVMContext::OB_funclet,
9345               LLVMContext::OB_cfguardtarget, LLVMContext::OB_preallocated,
9346               LLVMContext::OB_clang_arc_attachedcall, LLVMContext::OB_kcfi,
9347               LLVMContext::OB_convergencectrl}) &&
9348          "Cannot lower calls with arbitrary operand bundles!");
9349 
9350   SDValue Callee = getValue(I.getCalledOperand());
9351 
9352   if (I.hasDeoptState())
9353     LowerCallSiteWithDeoptBundle(&I, Callee, nullptr);
9354   else
9355     // Check if we can potentially perform a tail call. More detailed checking
9356     // is be done within LowerCallTo, after more information about the call is
9357     // known.
9358     LowerCallTo(I, Callee, I.isTailCall(), I.isMustTailCall());
9359 }
9360 
9361 void SelectionDAGBuilder::LowerCallSiteWithPtrAuthBundle(
9362     const CallBase &CB, const BasicBlock *EHPadBB) {
9363   auto PAB = CB.getOperandBundle("ptrauth");
9364   const Value *CalleeV = CB.getCalledOperand();
9365 
9366   // Gather the call ptrauth data from the operand bundle:
9367   //   [ i32 <key>, i64 <discriminator> ]
9368   const auto *Key = cast<ConstantInt>(PAB->Inputs[0]);
9369   const Value *Discriminator = PAB->Inputs[1];
9370 
9371   assert(Key->getType()->isIntegerTy(32) && "Invalid ptrauth key");
9372   assert(Discriminator->getType()->isIntegerTy(64) &&
9373          "Invalid ptrauth discriminator");
9374 
9375   // Functions should never be ptrauth-called directly.
9376   assert(!isa<Function>(CalleeV) && "invalid direct ptrauth call");
9377 
9378   // Otherwise, do an authenticated indirect call.
9379   TargetLowering::PtrAuthInfo PAI = {Key->getZExtValue(),
9380                                      getValue(Discriminator)};
9381 
9382   LowerCallTo(CB, getValue(CalleeV), CB.isTailCall(), CB.isMustTailCall(),
9383               EHPadBB, &PAI);
9384 }
9385 
9386 namespace {
9387 
9388 /// AsmOperandInfo - This contains information for each constraint that we are
9389 /// lowering.
9390 class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo {
9391 public:
9392   /// CallOperand - If this is the result output operand or a clobber
9393   /// this is null, otherwise it is the incoming operand to the CallInst.
9394   /// This gets modified as the asm is processed.
9395   SDValue CallOperand;
9396 
9397   /// AssignedRegs - If this is a register or register class operand, this
9398   /// contains the set of register corresponding to the operand.
9399   RegsForValue AssignedRegs;
9400 
9401   explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info)
9402     : TargetLowering::AsmOperandInfo(info), CallOperand(nullptr, 0) {
9403   }
9404 
9405   /// Whether or not this operand accesses memory
9406   bool hasMemory(const TargetLowering &TLI) const {
9407     // Indirect operand accesses access memory.
9408     if (isIndirect)
9409       return true;
9410 
9411     for (const auto &Code : Codes)
9412       if (TLI.getConstraintType(Code) == TargetLowering::C_Memory)
9413         return true;
9414 
9415     return false;
9416   }
9417 };
9418 
9419 
9420 } // end anonymous namespace
9421 
9422 /// Make sure that the output operand \p OpInfo and its corresponding input
9423 /// operand \p MatchingOpInfo have compatible constraint types (otherwise error
9424 /// out).
9425 static void patchMatchingInput(const SDISelAsmOperandInfo &OpInfo,
9426                                SDISelAsmOperandInfo &MatchingOpInfo,
9427                                SelectionDAG &DAG) {
9428   if (OpInfo.ConstraintVT == MatchingOpInfo.ConstraintVT)
9429     return;
9430 
9431   const TargetRegisterInfo *TRI = DAG.getSubtarget().getRegisterInfo();
9432   const auto &TLI = DAG.getTargetLoweringInfo();
9433 
9434   std::pair<unsigned, const TargetRegisterClass *> MatchRC =
9435       TLI.getRegForInlineAsmConstraint(TRI, OpInfo.ConstraintCode,
9436                                        OpInfo.ConstraintVT);
9437   std::pair<unsigned, const TargetRegisterClass *> InputRC =
9438       TLI.getRegForInlineAsmConstraint(TRI, MatchingOpInfo.ConstraintCode,
9439                                        MatchingOpInfo.ConstraintVT);
9440   if ((OpInfo.ConstraintVT.isInteger() !=
9441        MatchingOpInfo.ConstraintVT.isInteger()) ||
9442       (MatchRC.second != InputRC.second)) {
9443     // FIXME: error out in a more elegant fashion
9444     report_fatal_error("Unsupported asm: input constraint"
9445                        " with a matching output constraint of"
9446                        " incompatible type!");
9447   }
9448   MatchingOpInfo.ConstraintVT = OpInfo.ConstraintVT;
9449 }
9450 
9451 /// Get a direct memory input to behave well as an indirect operand.
9452 /// This may introduce stores, hence the need for a \p Chain.
9453 /// \return The (possibly updated) chain.
9454 static SDValue getAddressForMemoryInput(SDValue Chain, const SDLoc &Location,
9455                                         SDISelAsmOperandInfo &OpInfo,
9456                                         SelectionDAG &DAG) {
9457   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9458 
9459   // If we don't have an indirect input, put it in the constpool if we can,
9460   // otherwise spill it to a stack slot.
9461   // TODO: This isn't quite right. We need to handle these according to
9462   // the addressing mode that the constraint wants. Also, this may take
9463   // an additional register for the computation and we don't want that
9464   // either.
9465 
9466   // If the operand is a float, integer, or vector constant, spill to a
9467   // constant pool entry to get its address.
9468   const Value *OpVal = OpInfo.CallOperandVal;
9469   if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
9470       isa<ConstantVector>(OpVal) || isa<ConstantDataVector>(OpVal)) {
9471     OpInfo.CallOperand = DAG.getConstantPool(
9472         cast<Constant>(OpVal), TLI.getPointerTy(DAG.getDataLayout()));
9473     return Chain;
9474   }
9475 
9476   // Otherwise, create a stack slot and emit a store to it before the asm.
9477   Type *Ty = OpVal->getType();
9478   auto &DL = DAG.getDataLayout();
9479   uint64_t TySize = DL.getTypeAllocSize(Ty);
9480   MachineFunction &MF = DAG.getMachineFunction();
9481   int SSFI = MF.getFrameInfo().CreateStackObject(
9482       TySize, DL.getPrefTypeAlign(Ty), false);
9483   SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getFrameIndexTy(DL));
9484   Chain = DAG.getTruncStore(Chain, Location, OpInfo.CallOperand, StackSlot,
9485                             MachinePointerInfo::getFixedStack(MF, SSFI),
9486                             TLI.getMemValueType(DL, Ty));
9487   OpInfo.CallOperand = StackSlot;
9488 
9489   return Chain;
9490 }
9491 
9492 /// GetRegistersForValue - Assign registers (virtual or physical) for the
9493 /// specified operand.  We prefer to assign virtual registers, to allow the
9494 /// register allocator to handle the assignment process.  However, if the asm
9495 /// uses features that we can't model on machineinstrs, we have SDISel do the
9496 /// allocation.  This produces generally horrible, but correct, code.
9497 ///
9498 ///   OpInfo describes the operand
9499 ///   RefOpInfo describes the matching operand if any, the operand otherwise
9500 static std::optional<unsigned>
9501 getRegistersForValue(SelectionDAG &DAG, const SDLoc &DL,
9502                      SDISelAsmOperandInfo &OpInfo,
9503                      SDISelAsmOperandInfo &RefOpInfo) {
9504   LLVMContext &Context = *DAG.getContext();
9505   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9506 
9507   MachineFunction &MF = DAG.getMachineFunction();
9508   SmallVector<unsigned, 4> Regs;
9509   const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
9510 
9511   // No work to do for memory/address operands.
9512   if (OpInfo.ConstraintType == TargetLowering::C_Memory ||
9513       OpInfo.ConstraintType == TargetLowering::C_Address)
9514     return std::nullopt;
9515 
9516   // If this is a constraint for a single physreg, or a constraint for a
9517   // register class, find it.
9518   unsigned AssignedReg;
9519   const TargetRegisterClass *RC;
9520   std::tie(AssignedReg, RC) = TLI.getRegForInlineAsmConstraint(
9521       &TRI, RefOpInfo.ConstraintCode, RefOpInfo.ConstraintVT);
9522   // RC is unset only on failure. Return immediately.
9523   if (!RC)
9524     return std::nullopt;
9525 
9526   // Get the actual register value type.  This is important, because the user
9527   // may have asked for (e.g.) the AX register in i32 type.  We need to
9528   // remember that AX is actually i16 to get the right extension.
9529   const MVT RegVT = *TRI.legalclasstypes_begin(*RC);
9530 
9531   if (OpInfo.ConstraintVT != MVT::Other && RegVT != MVT::Untyped) {
9532     // If this is an FP operand in an integer register (or visa versa), or more
9533     // generally if the operand value disagrees with the register class we plan
9534     // to stick it in, fix the operand type.
9535     //
9536     // If this is an input value, the bitcast to the new type is done now.
9537     // Bitcast for output value is done at the end of visitInlineAsm().
9538     if ((OpInfo.Type == InlineAsm::isOutput ||
9539          OpInfo.Type == InlineAsm::isInput) &&
9540         !TRI.isTypeLegalForClass(*RC, OpInfo.ConstraintVT)) {
9541       // Try to convert to the first EVT that the reg class contains.  If the
9542       // types are identical size, use a bitcast to convert (e.g. two differing
9543       // vector types).  Note: output bitcast is done at the end of
9544       // visitInlineAsm().
9545       if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
9546         // Exclude indirect inputs while they are unsupported because the code
9547         // to perform the load is missing and thus OpInfo.CallOperand still
9548         // refers to the input address rather than the pointed-to value.
9549         if (OpInfo.Type == InlineAsm::isInput && !OpInfo.isIndirect)
9550           OpInfo.CallOperand =
9551               DAG.getNode(ISD::BITCAST, DL, RegVT, OpInfo.CallOperand);
9552         OpInfo.ConstraintVT = RegVT;
9553         // If the operand is an FP value and we want it in integer registers,
9554         // use the corresponding integer type. This turns an f64 value into
9555         // i64, which can be passed with two i32 values on a 32-bit machine.
9556       } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
9557         MVT VT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits());
9558         if (OpInfo.Type == InlineAsm::isInput)
9559           OpInfo.CallOperand =
9560               DAG.getNode(ISD::BITCAST, DL, VT, OpInfo.CallOperand);
9561         OpInfo.ConstraintVT = VT;
9562       }
9563     }
9564   }
9565 
9566   // No need to allocate a matching input constraint since the constraint it's
9567   // matching to has already been allocated.
9568   if (OpInfo.isMatchingInputConstraint())
9569     return std::nullopt;
9570 
9571   EVT ValueVT = OpInfo.ConstraintVT;
9572   if (OpInfo.ConstraintVT == MVT::Other)
9573     ValueVT = RegVT;
9574 
9575   // Initialize NumRegs.
9576   unsigned NumRegs = 1;
9577   if (OpInfo.ConstraintVT != MVT::Other)
9578     NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT, RegVT);
9579 
9580   // If this is a constraint for a specific physical register, like {r17},
9581   // assign it now.
9582 
9583   // If this associated to a specific register, initialize iterator to correct
9584   // place. If virtual, make sure we have enough registers
9585 
9586   // Initialize iterator if necessary
9587   TargetRegisterClass::iterator I = RC->begin();
9588   MachineRegisterInfo &RegInfo = MF.getRegInfo();
9589 
9590   // Do not check for single registers.
9591   if (AssignedReg) {
9592     I = std::find(I, RC->end(), AssignedReg);
9593     if (I == RC->end()) {
9594       // RC does not contain the selected register, which indicates a
9595       // mismatch between the register and the required type/bitwidth.
9596       return {AssignedReg};
9597     }
9598   }
9599 
9600   for (; NumRegs; --NumRegs, ++I) {
9601     assert(I != RC->end() && "Ran out of registers to allocate!");
9602     Register R = AssignedReg ? Register(*I) : RegInfo.createVirtualRegister(RC);
9603     Regs.push_back(R);
9604   }
9605 
9606   OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
9607   return std::nullopt;
9608 }
9609 
9610 static unsigned
9611 findMatchingInlineAsmOperand(unsigned OperandNo,
9612                              const std::vector<SDValue> &AsmNodeOperands) {
9613   // Scan until we find the definition we already emitted of this operand.
9614   unsigned CurOp = InlineAsm::Op_FirstOperand;
9615   for (; OperandNo; --OperandNo) {
9616     // Advance to the next operand.
9617     unsigned OpFlag = AsmNodeOperands[CurOp]->getAsZExtVal();
9618     const InlineAsm::Flag F(OpFlag);
9619     assert(
9620         (F.isRegDefKind() || F.isRegDefEarlyClobberKind() || F.isMemKind()) &&
9621         "Skipped past definitions?");
9622     CurOp += F.getNumOperandRegisters() + 1;
9623   }
9624   return CurOp;
9625 }
9626 
9627 namespace {
9628 
9629 class ExtraFlags {
9630   unsigned Flags = 0;
9631 
9632 public:
9633   explicit ExtraFlags(const CallBase &Call) {
9634     const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
9635     if (IA->hasSideEffects())
9636       Flags |= InlineAsm::Extra_HasSideEffects;
9637     if (IA->isAlignStack())
9638       Flags |= InlineAsm::Extra_IsAlignStack;
9639     if (Call.isConvergent())
9640       Flags |= InlineAsm::Extra_IsConvergent;
9641     Flags |= IA->getDialect() * InlineAsm::Extra_AsmDialect;
9642   }
9643 
9644   void update(const TargetLowering::AsmOperandInfo &OpInfo) {
9645     // Ideally, we would only check against memory constraints.  However, the
9646     // meaning of an Other constraint can be target-specific and we can't easily
9647     // reason about it.  Therefore, be conservative and set MayLoad/MayStore
9648     // for Other constraints as well.
9649     if (OpInfo.ConstraintType == TargetLowering::C_Memory ||
9650         OpInfo.ConstraintType == TargetLowering::C_Other) {
9651       if (OpInfo.Type == InlineAsm::isInput)
9652         Flags |= InlineAsm::Extra_MayLoad;
9653       else if (OpInfo.Type == InlineAsm::isOutput)
9654         Flags |= InlineAsm::Extra_MayStore;
9655       else if (OpInfo.Type == InlineAsm::isClobber)
9656         Flags |= (InlineAsm::Extra_MayLoad | InlineAsm::Extra_MayStore);
9657     }
9658   }
9659 
9660   unsigned get() const { return Flags; }
9661 };
9662 
9663 } // end anonymous namespace
9664 
9665 static bool isFunction(SDValue Op) {
9666   if (Op && Op.getOpcode() == ISD::GlobalAddress) {
9667     if (auto *GA = dyn_cast<GlobalAddressSDNode>(Op)) {
9668       auto Fn = dyn_cast_or_null<Function>(GA->getGlobal());
9669 
9670       // In normal "call dllimport func" instruction (non-inlineasm) it force
9671       // indirect access by specifing call opcode. And usually specially print
9672       // asm with indirect symbol (i.g: "*") according to opcode. Inline asm can
9673       // not do in this way now. (In fact, this is similar with "Data Access"
9674       // action). So here we ignore dllimport function.
9675       if (Fn && !Fn->hasDLLImportStorageClass())
9676         return true;
9677     }
9678   }
9679   return false;
9680 }
9681 
9682 /// visitInlineAsm - Handle a call to an InlineAsm object.
9683 void SelectionDAGBuilder::visitInlineAsm(const CallBase &Call,
9684                                          const BasicBlock *EHPadBB) {
9685   const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
9686 
9687   /// ConstraintOperands - Information about all of the constraints.
9688   SmallVector<SDISelAsmOperandInfo, 16> ConstraintOperands;
9689 
9690   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9691   TargetLowering::AsmOperandInfoVector TargetConstraints = TLI.ParseConstraints(
9692       DAG.getDataLayout(), DAG.getSubtarget().getRegisterInfo(), Call);
9693 
9694   // First Pass: Calculate HasSideEffects and ExtraFlags (AlignStack,
9695   // AsmDialect, MayLoad, MayStore).
9696   bool HasSideEffect = IA->hasSideEffects();
9697   ExtraFlags ExtraInfo(Call);
9698 
9699   for (auto &T : TargetConstraints) {
9700     ConstraintOperands.push_back(SDISelAsmOperandInfo(T));
9701     SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
9702 
9703     if (OpInfo.CallOperandVal)
9704       OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
9705 
9706     if (!HasSideEffect)
9707       HasSideEffect = OpInfo.hasMemory(TLI);
9708 
9709     // Determine if this InlineAsm MayLoad or MayStore based on the constraints.
9710     // FIXME: Could we compute this on OpInfo rather than T?
9711 
9712     // Compute the constraint code and ConstraintType to use.
9713     TLI.ComputeConstraintToUse(T, SDValue());
9714 
9715     if (T.ConstraintType == TargetLowering::C_Immediate &&
9716         OpInfo.CallOperand && !isa<ConstantSDNode>(OpInfo.CallOperand))
9717       // We've delayed emitting a diagnostic like the "n" constraint because
9718       // inlining could cause an integer showing up.
9719       return emitInlineAsmError(Call, "constraint '" + Twine(T.ConstraintCode) +
9720                                           "' expects an integer constant "
9721                                           "expression");
9722 
9723     ExtraInfo.update(T);
9724   }
9725 
9726   // We won't need to flush pending loads if this asm doesn't touch
9727   // memory and is nonvolatile.
9728   SDValue Glue, Chain = (HasSideEffect) ? getRoot() : DAG.getRoot();
9729 
9730   bool EmitEHLabels = isa<InvokeInst>(Call);
9731   if (EmitEHLabels) {
9732     assert(EHPadBB && "InvokeInst must have an EHPadBB");
9733   }
9734   bool IsCallBr = isa<CallBrInst>(Call);
9735 
9736   if (IsCallBr || EmitEHLabels) {
9737     // If this is a callbr or invoke we need to flush pending exports since
9738     // inlineasm_br and invoke are terminators.
9739     // We need to do this before nodes are glued to the inlineasm_br node.
9740     Chain = getControlRoot();
9741   }
9742 
9743   MCSymbol *BeginLabel = nullptr;
9744   if (EmitEHLabels) {
9745     Chain = lowerStartEH(Chain, EHPadBB, BeginLabel);
9746   }
9747 
9748   int OpNo = -1;
9749   SmallVector<StringRef> AsmStrs;
9750   IA->collectAsmStrs(AsmStrs);
9751 
9752   // Second pass over the constraints: compute which constraint option to use.
9753   for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
9754     if (OpInfo.hasArg() || OpInfo.Type == InlineAsm::isOutput)
9755       OpNo++;
9756 
9757     // If this is an output operand with a matching input operand, look up the
9758     // matching input. If their types mismatch, e.g. one is an integer, the
9759     // other is floating point, or their sizes are different, flag it as an
9760     // error.
9761     if (OpInfo.hasMatchingInput()) {
9762       SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
9763       patchMatchingInput(OpInfo, Input, DAG);
9764     }
9765 
9766     // Compute the constraint code and ConstraintType to use.
9767     TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
9768 
9769     if ((OpInfo.ConstraintType == TargetLowering::C_Memory &&
9770          OpInfo.Type == InlineAsm::isClobber) ||
9771         OpInfo.ConstraintType == TargetLowering::C_Address)
9772       continue;
9773 
9774     // In Linux PIC model, there are 4 cases about value/label addressing:
9775     //
9776     // 1: Function call or Label jmp inside the module.
9777     // 2: Data access (such as global variable, static variable) inside module.
9778     // 3: Function call or Label jmp outside the module.
9779     // 4: Data access (such as global variable) outside the module.
9780     //
9781     // Due to current llvm inline asm architecture designed to not "recognize"
9782     // the asm code, there are quite troubles for us to treat mem addressing
9783     // differently for same value/adress used in different instuctions.
9784     // For example, in pic model, call a func may in plt way or direclty
9785     // pc-related, but lea/mov a function adress may use got.
9786     //
9787     // Here we try to "recognize" function call for the case 1 and case 3 in
9788     // inline asm. And try to adjust the constraint for them.
9789     //
9790     // TODO: Due to current inline asm didn't encourage to jmp to the outsider
9791     // label, so here we don't handle jmp function label now, but we need to
9792     // enhance it (especilly in PIC model) if we meet meaningful requirements.
9793     if (OpInfo.isIndirect && isFunction(OpInfo.CallOperand) &&
9794         TLI.isInlineAsmTargetBranch(AsmStrs, OpNo) &&
9795         TM.getCodeModel() != CodeModel::Large) {
9796       OpInfo.isIndirect = false;
9797       OpInfo.ConstraintType = TargetLowering::C_Address;
9798     }
9799 
9800     // If this is a memory input, and if the operand is not indirect, do what we
9801     // need to provide an address for the memory input.
9802     if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
9803         !OpInfo.isIndirect) {
9804       assert((OpInfo.isMultipleAlternative ||
9805               (OpInfo.Type == InlineAsm::isInput)) &&
9806              "Can only indirectify direct input operands!");
9807 
9808       // Memory operands really want the address of the value.
9809       Chain = getAddressForMemoryInput(Chain, getCurSDLoc(), OpInfo, DAG);
9810 
9811       // There is no longer a Value* corresponding to this operand.
9812       OpInfo.CallOperandVal = nullptr;
9813 
9814       // It is now an indirect operand.
9815       OpInfo.isIndirect = true;
9816     }
9817 
9818   }
9819 
9820   // AsmNodeOperands - The operands for the ISD::INLINEASM node.
9821   std::vector<SDValue> AsmNodeOperands;
9822   AsmNodeOperands.push_back(SDValue());  // reserve space for input chain
9823   AsmNodeOperands.push_back(DAG.getTargetExternalSymbol(
9824       IA->getAsmString().c_str(), TLI.getProgramPointerTy(DAG.getDataLayout())));
9825 
9826   // If we have a !srcloc metadata node associated with it, we want to attach
9827   // this to the ultimately generated inline asm machineinstr.  To do this, we
9828   // pass in the third operand as this (potentially null) inline asm MDNode.
9829   const MDNode *SrcLoc = Call.getMetadata("srcloc");
9830   AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
9831 
9832   // Remember the HasSideEffect, AlignStack, AsmDialect, MayLoad and MayStore
9833   // bits as operand 3.
9834   AsmNodeOperands.push_back(DAG.getTargetConstant(
9835       ExtraInfo.get(), getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
9836 
9837   // Third pass: Loop over operands to prepare DAG-level operands.. As part of
9838   // this, assign virtual and physical registers for inputs and otput.
9839   for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
9840     // Assign Registers.
9841     SDISelAsmOperandInfo &RefOpInfo =
9842         OpInfo.isMatchingInputConstraint()
9843             ? ConstraintOperands[OpInfo.getMatchedOperand()]
9844             : OpInfo;
9845     const auto RegError =
9846         getRegistersForValue(DAG, getCurSDLoc(), OpInfo, RefOpInfo);
9847     if (RegError) {
9848       const MachineFunction &MF = DAG.getMachineFunction();
9849       const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
9850       const char *RegName = TRI.getName(*RegError);
9851       emitInlineAsmError(Call, "register '" + Twine(RegName) +
9852                                    "' allocated for constraint '" +
9853                                    Twine(OpInfo.ConstraintCode) +
9854                                    "' does not match required type");
9855       return;
9856     }
9857 
9858     auto DetectWriteToReservedRegister = [&]() {
9859       const MachineFunction &MF = DAG.getMachineFunction();
9860       const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
9861       for (unsigned Reg : OpInfo.AssignedRegs.Regs) {
9862         if (Register::isPhysicalRegister(Reg) &&
9863             TRI.isInlineAsmReadOnlyReg(MF, Reg)) {
9864           const char *RegName = TRI.getName(Reg);
9865           emitInlineAsmError(Call, "write to reserved register '" +
9866                                        Twine(RegName) + "'");
9867           return true;
9868         }
9869       }
9870       return false;
9871     };
9872     assert((OpInfo.ConstraintType != TargetLowering::C_Address ||
9873             (OpInfo.Type == InlineAsm::isInput &&
9874              !OpInfo.isMatchingInputConstraint())) &&
9875            "Only address as input operand is allowed.");
9876 
9877     switch (OpInfo.Type) {
9878     case InlineAsm::isOutput:
9879       if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
9880         const InlineAsm::ConstraintCode ConstraintID =
9881             TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
9882         assert(ConstraintID != InlineAsm::ConstraintCode::Unknown &&
9883                "Failed to convert memory constraint code to constraint id.");
9884 
9885         // Add information to the INLINEASM node to know about this output.
9886         InlineAsm::Flag OpFlags(InlineAsm::Kind::Mem, 1);
9887         OpFlags.setMemConstraint(ConstraintID);
9888         AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags, getCurSDLoc(),
9889                                                         MVT::i32));
9890         AsmNodeOperands.push_back(OpInfo.CallOperand);
9891       } else {
9892         // Otherwise, this outputs to a register (directly for C_Register /
9893         // C_RegisterClass, and a target-defined fashion for
9894         // C_Immediate/C_Other). Find a register that we can use.
9895         if (OpInfo.AssignedRegs.Regs.empty()) {
9896           emitInlineAsmError(
9897               Call, "couldn't allocate output register for constraint '" +
9898                         Twine(OpInfo.ConstraintCode) + "'");
9899           return;
9900         }
9901 
9902         if (DetectWriteToReservedRegister())
9903           return;
9904 
9905         // Add information to the INLINEASM node to know that this register is
9906         // set.
9907         OpInfo.AssignedRegs.AddInlineAsmOperands(
9908             OpInfo.isEarlyClobber ? InlineAsm::Kind::RegDefEarlyClobber
9909                                   : InlineAsm::Kind::RegDef,
9910             false, 0, getCurSDLoc(), DAG, AsmNodeOperands);
9911       }
9912       break;
9913 
9914     case InlineAsm::isInput:
9915     case InlineAsm::isLabel: {
9916       SDValue InOperandVal = OpInfo.CallOperand;
9917 
9918       if (OpInfo.isMatchingInputConstraint()) {
9919         // If this is required to match an output register we have already set,
9920         // just use its register.
9921         auto CurOp = findMatchingInlineAsmOperand(OpInfo.getMatchedOperand(),
9922                                                   AsmNodeOperands);
9923         InlineAsm::Flag Flag(AsmNodeOperands[CurOp]->getAsZExtVal());
9924         if (Flag.isRegDefKind() || Flag.isRegDefEarlyClobberKind()) {
9925           if (OpInfo.isIndirect) {
9926             // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
9927             emitInlineAsmError(Call, "inline asm not supported yet: "
9928                                      "don't know how to handle tied "
9929                                      "indirect register inputs");
9930             return;
9931           }
9932 
9933           SmallVector<unsigned, 4> Regs;
9934           MachineFunction &MF = DAG.getMachineFunction();
9935           MachineRegisterInfo &MRI = MF.getRegInfo();
9936           const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
9937           auto *R = cast<RegisterSDNode>(AsmNodeOperands[CurOp+1]);
9938           Register TiedReg = R->getReg();
9939           MVT RegVT = R->getSimpleValueType(0);
9940           const TargetRegisterClass *RC =
9941               TiedReg.isVirtual()     ? MRI.getRegClass(TiedReg)
9942               : RegVT != MVT::Untyped ? TLI.getRegClassFor(RegVT)
9943                                       : TRI.getMinimalPhysRegClass(TiedReg);
9944           for (unsigned i = 0, e = Flag.getNumOperandRegisters(); i != e; ++i)
9945             Regs.push_back(MRI.createVirtualRegister(RC));
9946 
9947           RegsForValue MatchedRegs(Regs, RegVT, InOperandVal.getValueType());
9948 
9949           SDLoc dl = getCurSDLoc();
9950           // Use the produced MatchedRegs object to
9951           MatchedRegs.getCopyToRegs(InOperandVal, DAG, dl, Chain, &Glue, &Call);
9952           MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind::RegUse, true,
9953                                            OpInfo.getMatchedOperand(), dl, DAG,
9954                                            AsmNodeOperands);
9955           break;
9956         }
9957 
9958         assert(Flag.isMemKind() && "Unknown matching constraint!");
9959         assert(Flag.getNumOperandRegisters() == 1 &&
9960                "Unexpected number of operands");
9961         // Add information to the INLINEASM node to know about this input.
9962         // See InlineAsm.h isUseOperandTiedToDef.
9963         Flag.clearMemConstraint();
9964         Flag.setMatchingOp(OpInfo.getMatchedOperand());
9965         AsmNodeOperands.push_back(DAG.getTargetConstant(
9966             Flag, getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
9967         AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
9968         break;
9969       }
9970 
9971       // Treat indirect 'X' constraint as memory.
9972       if (OpInfo.ConstraintType == TargetLowering::C_Other &&
9973           OpInfo.isIndirect)
9974         OpInfo.ConstraintType = TargetLowering::C_Memory;
9975 
9976       if (OpInfo.ConstraintType == TargetLowering::C_Immediate ||
9977           OpInfo.ConstraintType == TargetLowering::C_Other) {
9978         std::vector<SDValue> Ops;
9979         TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode,
9980                                           Ops, DAG);
9981         if (Ops.empty()) {
9982           if (OpInfo.ConstraintType == TargetLowering::C_Immediate)
9983             if (isa<ConstantSDNode>(InOperandVal)) {
9984               emitInlineAsmError(Call, "value out of range for constraint '" +
9985                                            Twine(OpInfo.ConstraintCode) + "'");
9986               return;
9987             }
9988 
9989           emitInlineAsmError(Call,
9990                              "invalid operand for inline asm constraint '" +
9991                                  Twine(OpInfo.ConstraintCode) + "'");
9992           return;
9993         }
9994 
9995         // Add information to the INLINEASM node to know about this input.
9996         InlineAsm::Flag ResOpType(InlineAsm::Kind::Imm, Ops.size());
9997         AsmNodeOperands.push_back(DAG.getTargetConstant(
9998             ResOpType, getCurSDLoc(), TLI.getPointerTy(DAG.getDataLayout())));
9999         llvm::append_range(AsmNodeOperands, Ops);
10000         break;
10001       }
10002 
10003       if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
10004         assert((OpInfo.isIndirect ||
10005                 OpInfo.ConstraintType != TargetLowering::C_Memory) &&
10006                "Operand must be indirect to be a mem!");
10007         assert(InOperandVal.getValueType() ==
10008                    TLI.getPointerTy(DAG.getDataLayout()) &&
10009                "Memory operands expect pointer values");
10010 
10011         const InlineAsm::ConstraintCode ConstraintID =
10012             TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10013         assert(ConstraintID != InlineAsm::ConstraintCode::Unknown &&
10014                "Failed to convert memory constraint code to constraint id.");
10015 
10016         // Add information to the INLINEASM node to know about this input.
10017         InlineAsm::Flag ResOpType(InlineAsm::Kind::Mem, 1);
10018         ResOpType.setMemConstraint(ConstraintID);
10019         AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
10020                                                         getCurSDLoc(),
10021                                                         MVT::i32));
10022         AsmNodeOperands.push_back(InOperandVal);
10023         break;
10024       }
10025 
10026       if (OpInfo.ConstraintType == TargetLowering::C_Address) {
10027         const InlineAsm::ConstraintCode ConstraintID =
10028             TLI.getInlineAsmMemConstraint(OpInfo.ConstraintCode);
10029         assert(ConstraintID != InlineAsm::ConstraintCode::Unknown &&
10030                "Failed to convert memory constraint code to constraint id.");
10031 
10032         InlineAsm::Flag ResOpType(InlineAsm::Kind::Mem, 1);
10033 
10034         SDValue AsmOp = InOperandVal;
10035         if (isFunction(InOperandVal)) {
10036           auto *GA = cast<GlobalAddressSDNode>(InOperandVal);
10037           ResOpType = InlineAsm::Flag(InlineAsm::Kind::Func, 1);
10038           AsmOp = DAG.getTargetGlobalAddress(GA->getGlobal(), getCurSDLoc(),
10039                                              InOperandVal.getValueType(),
10040                                              GA->getOffset());
10041         }
10042 
10043         // Add information to the INLINEASM node to know about this input.
10044         ResOpType.setMemConstraint(ConstraintID);
10045 
10046         AsmNodeOperands.push_back(
10047             DAG.getTargetConstant(ResOpType, getCurSDLoc(), MVT::i32));
10048 
10049         AsmNodeOperands.push_back(AsmOp);
10050         break;
10051       }
10052 
10053       if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
10054           OpInfo.ConstraintType != TargetLowering::C_Register) {
10055         emitInlineAsmError(Call, "unknown asm constraint '" +
10056                                      Twine(OpInfo.ConstraintCode) + "'");
10057         return;
10058       }
10059 
10060       // TODO: Support this.
10061       if (OpInfo.isIndirect) {
10062         emitInlineAsmError(
10063             Call, "Don't know how to handle indirect register inputs yet "
10064                   "for constraint '" +
10065                       Twine(OpInfo.ConstraintCode) + "'");
10066         return;
10067       }
10068 
10069       // Copy the input into the appropriate registers.
10070       if (OpInfo.AssignedRegs.Regs.empty()) {
10071         emitInlineAsmError(Call,
10072                            "couldn't allocate input reg for constraint '" +
10073                                Twine(OpInfo.ConstraintCode) + "'");
10074         return;
10075       }
10076 
10077       if (DetectWriteToReservedRegister())
10078         return;
10079 
10080       SDLoc dl = getCurSDLoc();
10081 
10082       OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, dl, Chain, &Glue,
10083                                         &Call);
10084 
10085       OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind::RegUse, false,
10086                                                0, dl, DAG, AsmNodeOperands);
10087       break;
10088     }
10089     case InlineAsm::isClobber:
10090       // Add the clobbered value to the operand list, so that the register
10091       // allocator is aware that the physreg got clobbered.
10092       if (!OpInfo.AssignedRegs.Regs.empty())
10093         OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind::Clobber,
10094                                                  false, 0, getCurSDLoc(), DAG,
10095                                                  AsmNodeOperands);
10096       break;
10097     }
10098   }
10099 
10100   // Finish up input operands.  Set the input chain and add the flag last.
10101   AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
10102   if (Glue.getNode()) AsmNodeOperands.push_back(Glue);
10103 
10104   unsigned ISDOpc = IsCallBr ? ISD::INLINEASM_BR : ISD::INLINEASM;
10105   Chain = DAG.getNode(ISDOpc, getCurSDLoc(),
10106                       DAG.getVTList(MVT::Other, MVT::Glue), AsmNodeOperands);
10107   Glue = Chain.getValue(1);
10108 
10109   // Do additional work to generate outputs.
10110 
10111   SmallVector<EVT, 1> ResultVTs;
10112   SmallVector<SDValue, 1> ResultValues;
10113   SmallVector<SDValue, 8> OutChains;
10114 
10115   llvm::Type *CallResultType = Call.getType();
10116   ArrayRef<Type *> ResultTypes;
10117   if (StructType *StructResult = dyn_cast<StructType>(CallResultType))
10118     ResultTypes = StructResult->elements();
10119   else if (!CallResultType->isVoidTy())
10120     ResultTypes = ArrayRef(CallResultType);
10121 
10122   auto CurResultType = ResultTypes.begin();
10123   auto handleRegAssign = [&](SDValue V) {
10124     assert(CurResultType != ResultTypes.end() && "Unexpected value");
10125     assert((*CurResultType)->isSized() && "Unexpected unsized type");
10126     EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), *CurResultType);
10127     ++CurResultType;
10128     // If the type of the inline asm call site return value is different but has
10129     // same size as the type of the asm output bitcast it.  One example of this
10130     // is for vectors with different width / number of elements.  This can
10131     // happen for register classes that can contain multiple different value
10132     // types.  The preg or vreg allocated may not have the same VT as was
10133     // expected.
10134     //
10135     // This can also happen for a return value that disagrees with the register
10136     // class it is put in, eg. a double in a general-purpose register on a
10137     // 32-bit machine.
10138     if (ResultVT != V.getValueType() &&
10139         ResultVT.getSizeInBits() == V.getValueSizeInBits())
10140       V = DAG.getNode(ISD::BITCAST, getCurSDLoc(), ResultVT, V);
10141     else if (ResultVT != V.getValueType() && ResultVT.isInteger() &&
10142              V.getValueType().isInteger()) {
10143       // If a result value was tied to an input value, the computed result
10144       // may have a wider width than the expected result.  Extract the
10145       // relevant portion.
10146       V = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), ResultVT, V);
10147     }
10148     assert(ResultVT == V.getValueType() && "Asm result value mismatch!");
10149     ResultVTs.push_back(ResultVT);
10150     ResultValues.push_back(V);
10151   };
10152 
10153   // Deal with output operands.
10154   for (SDISelAsmOperandInfo &OpInfo : ConstraintOperands) {
10155     if (OpInfo.Type == InlineAsm::isOutput) {
10156       SDValue Val;
10157       // Skip trivial output operands.
10158       if (OpInfo.AssignedRegs.Regs.empty())
10159         continue;
10160 
10161       switch (OpInfo.ConstraintType) {
10162       case TargetLowering::C_Register:
10163       case TargetLowering::C_RegisterClass:
10164         Val = OpInfo.AssignedRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
10165                                                   Chain, &Glue, &Call);
10166         break;
10167       case TargetLowering::C_Immediate:
10168       case TargetLowering::C_Other:
10169         Val = TLI.LowerAsmOutputForConstraint(Chain, Glue, getCurSDLoc(),
10170                                               OpInfo, DAG);
10171         break;
10172       case TargetLowering::C_Memory:
10173         break; // Already handled.
10174       case TargetLowering::C_Address:
10175         break; // Silence warning.
10176       case TargetLowering::C_Unknown:
10177         assert(false && "Unexpected unknown constraint");
10178       }
10179 
10180       // Indirect output manifest as stores. Record output chains.
10181       if (OpInfo.isIndirect) {
10182         const Value *Ptr = OpInfo.CallOperandVal;
10183         assert(Ptr && "Expected value CallOperandVal for indirect asm operand");
10184         SDValue Store = DAG.getStore(Chain, getCurSDLoc(), Val, getValue(Ptr),
10185                                      MachinePointerInfo(Ptr));
10186         OutChains.push_back(Store);
10187       } else {
10188         // generate CopyFromRegs to associated registers.
10189         assert(!Call.getType()->isVoidTy() && "Bad inline asm!");
10190         if (Val.getOpcode() == ISD::MERGE_VALUES) {
10191           for (const SDValue &V : Val->op_values())
10192             handleRegAssign(V);
10193         } else
10194           handleRegAssign(Val);
10195       }
10196     }
10197   }
10198 
10199   // Set results.
10200   if (!ResultValues.empty()) {
10201     assert(CurResultType == ResultTypes.end() &&
10202            "Mismatch in number of ResultTypes");
10203     assert(ResultValues.size() == ResultTypes.size() &&
10204            "Mismatch in number of output operands in asm result");
10205 
10206     SDValue V = DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
10207                             DAG.getVTList(ResultVTs), ResultValues);
10208     setValue(&Call, V);
10209   }
10210 
10211   // Collect store chains.
10212   if (!OutChains.empty())
10213     Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other, OutChains);
10214 
10215   if (EmitEHLabels) {
10216     Chain = lowerEndEH(Chain, cast<InvokeInst>(&Call), EHPadBB, BeginLabel);
10217   }
10218 
10219   // Only Update Root if inline assembly has a memory effect.
10220   if (ResultValues.empty() || HasSideEffect || !OutChains.empty() || IsCallBr ||
10221       EmitEHLabels)
10222     DAG.setRoot(Chain);
10223 }
10224 
10225 void SelectionDAGBuilder::emitInlineAsmError(const CallBase &Call,
10226                                              const Twine &Message) {
10227   LLVMContext &Ctx = *DAG.getContext();
10228   Ctx.emitError(&Call, Message);
10229 
10230   // Make sure we leave the DAG in a valid state
10231   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10232   SmallVector<EVT, 1> ValueVTs;
10233   ComputeValueVTs(TLI, DAG.getDataLayout(), Call.getType(), ValueVTs);
10234 
10235   if (ValueVTs.empty())
10236     return;
10237 
10238   SmallVector<SDValue, 1> Ops;
10239   for (unsigned i = 0, e = ValueVTs.size(); i != e; ++i)
10240     Ops.push_back(DAG.getUNDEF(ValueVTs[i]));
10241 
10242   setValue(&Call, DAG.getMergeValues(Ops, getCurSDLoc()));
10243 }
10244 
10245 void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
10246   DAG.setRoot(DAG.getNode(ISD::VASTART, getCurSDLoc(),
10247                           MVT::Other, getRoot(),
10248                           getValue(I.getArgOperand(0)),
10249                           DAG.getSrcValue(I.getArgOperand(0))));
10250 }
10251 
10252 void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
10253   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10254   const DataLayout &DL = DAG.getDataLayout();
10255   SDValue V = DAG.getVAArg(
10256       TLI.getMemValueType(DAG.getDataLayout(), I.getType()), getCurSDLoc(),
10257       getRoot(), getValue(I.getOperand(0)), DAG.getSrcValue(I.getOperand(0)),
10258       DL.getABITypeAlign(I.getType()).value());
10259   DAG.setRoot(V.getValue(1));
10260 
10261   if (I.getType()->isPointerTy())
10262     V = DAG.getPtrExtOrTrunc(
10263         V, getCurSDLoc(), TLI.getValueType(DAG.getDataLayout(), I.getType()));
10264   setValue(&I, V);
10265 }
10266 
10267 void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
10268   DAG.setRoot(DAG.getNode(ISD::VAEND, getCurSDLoc(),
10269                           MVT::Other, getRoot(),
10270                           getValue(I.getArgOperand(0)),
10271                           DAG.getSrcValue(I.getArgOperand(0))));
10272 }
10273 
10274 void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
10275   DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurSDLoc(),
10276                           MVT::Other, getRoot(),
10277                           getValue(I.getArgOperand(0)),
10278                           getValue(I.getArgOperand(1)),
10279                           DAG.getSrcValue(I.getArgOperand(0)),
10280                           DAG.getSrcValue(I.getArgOperand(1))));
10281 }
10282 
10283 SDValue SelectionDAGBuilder::lowerRangeToAssertZExt(SelectionDAG &DAG,
10284                                                     const Instruction &I,
10285                                                     SDValue Op) {
10286   std::optional<ConstantRange> CR = getRange(I);
10287 
10288   if (!CR || CR->isFullSet() || CR->isEmptySet() || CR->isUpperWrapped())
10289     return Op;
10290 
10291   APInt Lo = CR->getUnsignedMin();
10292   if (!Lo.isMinValue())
10293     return Op;
10294 
10295   APInt Hi = CR->getUnsignedMax();
10296   unsigned Bits = std::max(Hi.getActiveBits(),
10297                            static_cast<unsigned>(IntegerType::MIN_INT_BITS));
10298 
10299   EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), Bits);
10300 
10301   SDLoc SL = getCurSDLoc();
10302 
10303   SDValue ZExt = DAG.getNode(ISD::AssertZext, SL, Op.getValueType(), Op,
10304                              DAG.getValueType(SmallVT));
10305   unsigned NumVals = Op.getNode()->getNumValues();
10306   if (NumVals == 1)
10307     return ZExt;
10308 
10309   SmallVector<SDValue, 4> Ops;
10310 
10311   Ops.push_back(ZExt);
10312   for (unsigned I = 1; I != NumVals; ++I)
10313     Ops.push_back(Op.getValue(I));
10314 
10315   return DAG.getMergeValues(Ops, SL);
10316 }
10317 
10318 /// Populate a CallLowerinInfo (into \p CLI) based on the properties of
10319 /// the call being lowered.
10320 ///
10321 /// This is a helper for lowering intrinsics that follow a target calling
10322 /// convention or require stack pointer adjustment. Only a subset of the
10323 /// intrinsic's operands need to participate in the calling convention.
10324 void SelectionDAGBuilder::populateCallLoweringInfo(
10325     TargetLowering::CallLoweringInfo &CLI, const CallBase *Call,
10326     unsigned ArgIdx, unsigned NumArgs, SDValue Callee, Type *ReturnTy,
10327     AttributeSet RetAttrs, bool IsPatchPoint) {
10328   TargetLowering::ArgListTy Args;
10329   Args.reserve(NumArgs);
10330 
10331   // Populate the argument list.
10332   // Attributes for args start at offset 1, after the return attribute.
10333   for (unsigned ArgI = ArgIdx, ArgE = ArgIdx + NumArgs;
10334        ArgI != ArgE; ++ArgI) {
10335     const Value *V = Call->getOperand(ArgI);
10336 
10337     assert(!V->getType()->isEmptyTy() && "Empty type passed to intrinsic.");
10338 
10339     TargetLowering::ArgListEntry Entry;
10340     Entry.Node = getValue(V);
10341     Entry.Ty = V->getType();
10342     Entry.setAttributes(Call, ArgI);
10343     Args.push_back(Entry);
10344   }
10345 
10346   CLI.setDebugLoc(getCurSDLoc())
10347       .setChain(getRoot())
10348       .setCallee(Call->getCallingConv(), ReturnTy, Callee, std::move(Args),
10349                  RetAttrs)
10350       .setDiscardResult(Call->use_empty())
10351       .setIsPatchPoint(IsPatchPoint)
10352       .setIsPreallocated(
10353           Call->countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0);
10354 }
10355 
10356 /// Add a stack map intrinsic call's live variable operands to a stackmap
10357 /// or patchpoint target node's operand list.
10358 ///
10359 /// Constants are converted to TargetConstants purely as an optimization to
10360 /// avoid constant materialization and register allocation.
10361 ///
10362 /// FrameIndex operands are converted to TargetFrameIndex so that ISEL does not
10363 /// generate addess computation nodes, and so FinalizeISel can convert the
10364 /// TargetFrameIndex into a DirectMemRefOp StackMap location. This avoids
10365 /// address materialization and register allocation, but may also be required
10366 /// for correctness. If a StackMap (or PatchPoint) intrinsic directly uses an
10367 /// alloca in the entry block, then the runtime may assume that the alloca's
10368 /// StackMap location can be read immediately after compilation and that the
10369 /// location is valid at any point during execution (this is similar to the
10370 /// assumption made by the llvm.gcroot intrinsic). If the alloca's location were
10371 /// only available in a register, then the runtime would need to trap when
10372 /// execution reaches the StackMap in order to read the alloca's location.
10373 static void addStackMapLiveVars(const CallBase &Call, unsigned StartIdx,
10374                                 const SDLoc &DL, SmallVectorImpl<SDValue> &Ops,
10375                                 SelectionDAGBuilder &Builder) {
10376   SelectionDAG &DAG = Builder.DAG;
10377   for (unsigned I = StartIdx; I < Call.arg_size(); I++) {
10378     SDValue Op = Builder.getValue(Call.getArgOperand(I));
10379 
10380     // Things on the stack are pointer-typed, meaning that they are already
10381     // legal and can be emitted directly to target nodes.
10382     if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op)) {
10383       Ops.push_back(DAG.getTargetFrameIndex(FI->getIndex(), Op.getValueType()));
10384     } else {
10385       // Otherwise emit a target independent node to be legalised.
10386       Ops.push_back(Builder.getValue(Call.getArgOperand(I)));
10387     }
10388   }
10389 }
10390 
10391 /// Lower llvm.experimental.stackmap.
10392 void SelectionDAGBuilder::visitStackmap(const CallInst &CI) {
10393   // void @llvm.experimental.stackmap(i64 <id>, i32 <numShadowBytes>,
10394   //                                  [live variables...])
10395 
10396   assert(CI.getType()->isVoidTy() && "Stackmap cannot return a value.");
10397 
10398   SDValue Chain, InGlue, Callee;
10399   SmallVector<SDValue, 32> Ops;
10400 
10401   SDLoc DL = getCurSDLoc();
10402   Callee = getValue(CI.getCalledOperand());
10403 
10404   // The stackmap intrinsic only records the live variables (the arguments
10405   // passed to it) and emits NOPS (if requested). Unlike the patchpoint
10406   // intrinsic, this won't be lowered to a function call. This means we don't
10407   // have to worry about calling conventions and target specific lowering code.
10408   // Instead we perform the call lowering right here.
10409   //
10410   // chain, flag = CALLSEQ_START(chain, 0, 0)
10411   // chain, flag = STACKMAP(id, nbytes, ..., chain, flag)
10412   // chain, flag = CALLSEQ_END(chain, 0, 0, flag)
10413   //
10414   Chain = DAG.getCALLSEQ_START(getRoot(), 0, 0, DL);
10415   InGlue = Chain.getValue(1);
10416 
10417   // Add the STACKMAP operands, starting with DAG house-keeping.
10418   Ops.push_back(Chain);
10419   Ops.push_back(InGlue);
10420 
10421   // Add the <id>, <numShadowBytes> operands.
10422   //
10423   // These do not require legalisation, and can be emitted directly to target
10424   // constant nodes.
10425   SDValue ID = getValue(CI.getArgOperand(0));
10426   assert(ID.getValueType() == MVT::i64);
10427   SDValue IDConst =
10428       DAG.getTargetConstant(ID->getAsZExtVal(), DL, ID.getValueType());
10429   Ops.push_back(IDConst);
10430 
10431   SDValue Shad = getValue(CI.getArgOperand(1));
10432   assert(Shad.getValueType() == MVT::i32);
10433   SDValue ShadConst =
10434       DAG.getTargetConstant(Shad->getAsZExtVal(), DL, Shad.getValueType());
10435   Ops.push_back(ShadConst);
10436 
10437   // Add the live variables.
10438   addStackMapLiveVars(CI, 2, DL, Ops, *this);
10439 
10440   // Create the STACKMAP node.
10441   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
10442   Chain = DAG.getNode(ISD::STACKMAP, DL, NodeTys, Ops);
10443   InGlue = Chain.getValue(1);
10444 
10445   Chain = DAG.getCALLSEQ_END(Chain, 0, 0, InGlue, DL);
10446 
10447   // Stackmaps don't generate values, so nothing goes into the NodeMap.
10448 
10449   // Set the root to the target-lowered call chain.
10450   DAG.setRoot(Chain);
10451 
10452   // Inform the Frame Information that we have a stackmap in this function.
10453   FuncInfo.MF->getFrameInfo().setHasStackMap();
10454 }
10455 
10456 /// Lower llvm.experimental.patchpoint directly to its target opcode.
10457 void SelectionDAGBuilder::visitPatchpoint(const CallBase &CB,
10458                                           const BasicBlock *EHPadBB) {
10459   // <ty> @llvm.experimental.patchpoint.<ty>(i64 <id>,
10460   //                                         i32 <numBytes>,
10461   //                                         i8* <target>,
10462   //                                         i32 <numArgs>,
10463   //                                         [Args...],
10464   //                                         [live variables...])
10465 
10466   CallingConv::ID CC = CB.getCallingConv();
10467   bool IsAnyRegCC = CC == CallingConv::AnyReg;
10468   bool HasDef = !CB.getType()->isVoidTy();
10469   SDLoc dl = getCurSDLoc();
10470   SDValue Callee = getValue(CB.getArgOperand(PatchPointOpers::TargetPos));
10471 
10472   // Handle immediate and symbolic callees.
10473   if (auto* ConstCallee = dyn_cast<ConstantSDNode>(Callee))
10474     Callee = DAG.getIntPtrConstant(ConstCallee->getZExtValue(), dl,
10475                                    /*isTarget=*/true);
10476   else if (auto* SymbolicCallee = dyn_cast<GlobalAddressSDNode>(Callee))
10477     Callee =  DAG.getTargetGlobalAddress(SymbolicCallee->getGlobal(),
10478                                          SDLoc(SymbolicCallee),
10479                                          SymbolicCallee->getValueType(0));
10480 
10481   // Get the real number of arguments participating in the call <numArgs>
10482   SDValue NArgVal = getValue(CB.getArgOperand(PatchPointOpers::NArgPos));
10483   unsigned NumArgs = NArgVal->getAsZExtVal();
10484 
10485   // Skip the four meta args: <id>, <numNopBytes>, <target>, <numArgs>
10486   // Intrinsics include all meta-operands up to but not including CC.
10487   unsigned NumMetaOpers = PatchPointOpers::CCPos;
10488   assert(CB.arg_size() >= NumMetaOpers + NumArgs &&
10489          "Not enough arguments provided to the patchpoint intrinsic");
10490 
10491   // For AnyRegCC the arguments are lowered later on manually.
10492   unsigned NumCallArgs = IsAnyRegCC ? 0 : NumArgs;
10493   Type *ReturnTy =
10494       IsAnyRegCC ? Type::getVoidTy(*DAG.getContext()) : CB.getType();
10495 
10496   TargetLowering::CallLoweringInfo CLI(DAG);
10497   populateCallLoweringInfo(CLI, &CB, NumMetaOpers, NumCallArgs, Callee,
10498                            ReturnTy, CB.getAttributes().getRetAttrs(), true);
10499   std::pair<SDValue, SDValue> Result = lowerInvokable(CLI, EHPadBB);
10500 
10501   SDNode *CallEnd = Result.second.getNode();
10502   if (CallEnd->getOpcode() == ISD::EH_LABEL)
10503     CallEnd = CallEnd->getOperand(0).getNode();
10504   if (HasDef && (CallEnd->getOpcode() == ISD::CopyFromReg))
10505     CallEnd = CallEnd->getOperand(0).getNode();
10506 
10507   /// Get a call instruction from the call sequence chain.
10508   /// Tail calls are not allowed.
10509   assert(CallEnd->getOpcode() == ISD::CALLSEQ_END &&
10510          "Expected a callseq node.");
10511   SDNode *Call = CallEnd->getOperand(0).getNode();
10512   bool HasGlue = Call->getGluedNode();
10513 
10514   // Replace the target specific call node with the patchable intrinsic.
10515   SmallVector<SDValue, 8> Ops;
10516 
10517   // Push the chain.
10518   Ops.push_back(*(Call->op_begin()));
10519 
10520   // Optionally, push the glue (if any).
10521   if (HasGlue)
10522     Ops.push_back(*(Call->op_end() - 1));
10523 
10524   // Push the register mask info.
10525   if (HasGlue)
10526     Ops.push_back(*(Call->op_end() - 2));
10527   else
10528     Ops.push_back(*(Call->op_end() - 1));
10529 
10530   // Add the <id> and <numBytes> constants.
10531   SDValue IDVal = getValue(CB.getArgOperand(PatchPointOpers::IDPos));
10532   Ops.push_back(DAG.getTargetConstant(IDVal->getAsZExtVal(), dl, MVT::i64));
10533   SDValue NBytesVal = getValue(CB.getArgOperand(PatchPointOpers::NBytesPos));
10534   Ops.push_back(DAG.getTargetConstant(NBytesVal->getAsZExtVal(), dl, MVT::i32));
10535 
10536   // Add the callee.
10537   Ops.push_back(Callee);
10538 
10539   // Adjust <numArgs> to account for any arguments that have been passed on the
10540   // stack instead.
10541   // Call Node: Chain, Target, {Args}, RegMask, [Glue]
10542   unsigned NumCallRegArgs = Call->getNumOperands() - (HasGlue ? 4 : 3);
10543   NumCallRegArgs = IsAnyRegCC ? NumArgs : NumCallRegArgs;
10544   Ops.push_back(DAG.getTargetConstant(NumCallRegArgs, dl, MVT::i32));
10545 
10546   // Add the calling convention
10547   Ops.push_back(DAG.getTargetConstant((unsigned)CC, dl, MVT::i32));
10548 
10549   // Add the arguments we omitted previously. The register allocator should
10550   // place these in any free register.
10551   if (IsAnyRegCC)
10552     for (unsigned i = NumMetaOpers, e = NumMetaOpers + NumArgs; i != e; ++i)
10553       Ops.push_back(getValue(CB.getArgOperand(i)));
10554 
10555   // Push the arguments from the call instruction.
10556   SDNode::op_iterator e = HasGlue ? Call->op_end()-2 : Call->op_end()-1;
10557   Ops.append(Call->op_begin() + 2, e);
10558 
10559   // Push live variables for the stack map.
10560   addStackMapLiveVars(CB, NumMetaOpers + NumArgs, dl, Ops, *this);
10561 
10562   SDVTList NodeTys;
10563   if (IsAnyRegCC && HasDef) {
10564     // Create the return types based on the intrinsic definition
10565     const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10566     SmallVector<EVT, 3> ValueVTs;
10567     ComputeValueVTs(TLI, DAG.getDataLayout(), CB.getType(), ValueVTs);
10568     assert(ValueVTs.size() == 1 && "Expected only one return value type.");
10569 
10570     // There is always a chain and a glue type at the end
10571     ValueVTs.push_back(MVT::Other);
10572     ValueVTs.push_back(MVT::Glue);
10573     NodeTys = DAG.getVTList(ValueVTs);
10574   } else
10575     NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
10576 
10577   // Replace the target specific call node with a PATCHPOINT node.
10578   SDValue PPV = DAG.getNode(ISD::PATCHPOINT, dl, NodeTys, Ops);
10579 
10580   // Update the NodeMap.
10581   if (HasDef) {
10582     if (IsAnyRegCC)
10583       setValue(&CB, SDValue(PPV.getNode(), 0));
10584     else
10585       setValue(&CB, Result.first);
10586   }
10587 
10588   // Fixup the consumers of the intrinsic. The chain and glue may be used in the
10589   // call sequence. Furthermore the location of the chain and glue can change
10590   // when the AnyReg calling convention is used and the intrinsic returns a
10591   // value.
10592   if (IsAnyRegCC && HasDef) {
10593     SDValue From[] = {SDValue(Call, 0), SDValue(Call, 1)};
10594     SDValue To[] = {PPV.getValue(1), PPV.getValue(2)};
10595     DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
10596   } else
10597     DAG.ReplaceAllUsesWith(Call, PPV.getNode());
10598   DAG.DeleteNode(Call);
10599 
10600   // Inform the Frame Information that we have a patchpoint in this function.
10601   FuncInfo.MF->getFrameInfo().setHasPatchPoint();
10602 }
10603 
10604 void SelectionDAGBuilder::visitVectorReduce(const CallInst &I,
10605                                             unsigned Intrinsic) {
10606   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10607   SDValue Op1 = getValue(I.getArgOperand(0));
10608   SDValue Op2;
10609   if (I.arg_size() > 1)
10610     Op2 = getValue(I.getArgOperand(1));
10611   SDLoc dl = getCurSDLoc();
10612   EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
10613   SDValue Res;
10614   SDNodeFlags SDFlags;
10615   if (auto *FPMO = dyn_cast<FPMathOperator>(&I))
10616     SDFlags.copyFMF(*FPMO);
10617 
10618   switch (Intrinsic) {
10619   case Intrinsic::vector_reduce_fadd:
10620     if (SDFlags.hasAllowReassociation())
10621       Res = DAG.getNode(ISD::FADD, dl, VT, Op1,
10622                         DAG.getNode(ISD::VECREDUCE_FADD, dl, VT, Op2, SDFlags),
10623                         SDFlags);
10624     else
10625       Res = DAG.getNode(ISD::VECREDUCE_SEQ_FADD, dl, VT, Op1, Op2, SDFlags);
10626     break;
10627   case Intrinsic::vector_reduce_fmul:
10628     if (SDFlags.hasAllowReassociation())
10629       Res = DAG.getNode(ISD::FMUL, dl, VT, Op1,
10630                         DAG.getNode(ISD::VECREDUCE_FMUL, dl, VT, Op2, SDFlags),
10631                         SDFlags);
10632     else
10633       Res = DAG.getNode(ISD::VECREDUCE_SEQ_FMUL, dl, VT, Op1, Op2, SDFlags);
10634     break;
10635   case Intrinsic::vector_reduce_add:
10636     Res = DAG.getNode(ISD::VECREDUCE_ADD, dl, VT, Op1);
10637     break;
10638   case Intrinsic::vector_reduce_mul:
10639     Res = DAG.getNode(ISD::VECREDUCE_MUL, dl, VT, Op1);
10640     break;
10641   case Intrinsic::vector_reduce_and:
10642     Res = DAG.getNode(ISD::VECREDUCE_AND, dl, VT, Op1);
10643     break;
10644   case Intrinsic::vector_reduce_or:
10645     Res = DAG.getNode(ISD::VECREDUCE_OR, dl, VT, Op1);
10646     break;
10647   case Intrinsic::vector_reduce_xor:
10648     Res = DAG.getNode(ISD::VECREDUCE_XOR, dl, VT, Op1);
10649     break;
10650   case Intrinsic::vector_reduce_smax:
10651     Res = DAG.getNode(ISD::VECREDUCE_SMAX, dl, VT, Op1);
10652     break;
10653   case Intrinsic::vector_reduce_smin:
10654     Res = DAG.getNode(ISD::VECREDUCE_SMIN, dl, VT, Op1);
10655     break;
10656   case Intrinsic::vector_reduce_umax:
10657     Res = DAG.getNode(ISD::VECREDUCE_UMAX, dl, VT, Op1);
10658     break;
10659   case Intrinsic::vector_reduce_umin:
10660     Res = DAG.getNode(ISD::VECREDUCE_UMIN, dl, VT, Op1);
10661     break;
10662   case Intrinsic::vector_reduce_fmax:
10663     Res = DAG.getNode(ISD::VECREDUCE_FMAX, dl, VT, Op1, SDFlags);
10664     break;
10665   case Intrinsic::vector_reduce_fmin:
10666     Res = DAG.getNode(ISD::VECREDUCE_FMIN, dl, VT, Op1, SDFlags);
10667     break;
10668   case Intrinsic::vector_reduce_fmaximum:
10669     Res = DAG.getNode(ISD::VECREDUCE_FMAXIMUM, dl, VT, Op1, SDFlags);
10670     break;
10671   case Intrinsic::vector_reduce_fminimum:
10672     Res = DAG.getNode(ISD::VECREDUCE_FMINIMUM, dl, VT, Op1, SDFlags);
10673     break;
10674   default:
10675     llvm_unreachable("Unhandled vector reduce intrinsic");
10676   }
10677   setValue(&I, Res);
10678 }
10679 
10680 /// Returns an AttributeList representing the attributes applied to the return
10681 /// value of the given call.
10682 static AttributeList getReturnAttrs(TargetLowering::CallLoweringInfo &CLI) {
10683   SmallVector<Attribute::AttrKind, 2> Attrs;
10684   if (CLI.RetSExt)
10685     Attrs.push_back(Attribute::SExt);
10686   if (CLI.RetZExt)
10687     Attrs.push_back(Attribute::ZExt);
10688   if (CLI.IsInReg)
10689     Attrs.push_back(Attribute::InReg);
10690 
10691   return AttributeList::get(CLI.RetTy->getContext(), AttributeList::ReturnIndex,
10692                             Attrs);
10693 }
10694 
10695 /// TargetLowering::LowerCallTo - This is the default LowerCallTo
10696 /// implementation, which just calls LowerCall.
10697 /// FIXME: When all targets are
10698 /// migrated to using LowerCall, this hook should be integrated into SDISel.
10699 std::pair<SDValue, SDValue>
10700 TargetLowering::LowerCallTo(TargetLowering::CallLoweringInfo &CLI) const {
10701   // Handle the incoming return values from the call.
10702   CLI.Ins.clear();
10703   Type *OrigRetTy = CLI.RetTy;
10704   SmallVector<EVT, 4> RetTys;
10705   SmallVector<TypeSize, 4> Offsets;
10706   auto &DL = CLI.DAG.getDataLayout();
10707   ComputeValueVTs(*this, DL, CLI.RetTy, RetTys, &Offsets);
10708 
10709   if (CLI.IsPostTypeLegalization) {
10710     // If we are lowering a libcall after legalization, split the return type.
10711     SmallVector<EVT, 4> OldRetTys;
10712     SmallVector<TypeSize, 4> OldOffsets;
10713     RetTys.swap(OldRetTys);
10714     Offsets.swap(OldOffsets);
10715 
10716     for (size_t i = 0, e = OldRetTys.size(); i != e; ++i) {
10717       EVT RetVT = OldRetTys[i];
10718       uint64_t Offset = OldOffsets[i];
10719       MVT RegisterVT = getRegisterType(CLI.RetTy->getContext(), RetVT);
10720       unsigned NumRegs = getNumRegisters(CLI.RetTy->getContext(), RetVT);
10721       unsigned RegisterVTByteSZ = RegisterVT.getSizeInBits() / 8;
10722       RetTys.append(NumRegs, RegisterVT);
10723       for (unsigned j = 0; j != NumRegs; ++j)
10724         Offsets.push_back(TypeSize::getFixed(Offset + j * RegisterVTByteSZ));
10725     }
10726   }
10727 
10728   SmallVector<ISD::OutputArg, 4> Outs;
10729   GetReturnInfo(CLI.CallConv, CLI.RetTy, getReturnAttrs(CLI), Outs, *this, DL);
10730 
10731   bool CanLowerReturn =
10732       this->CanLowerReturn(CLI.CallConv, CLI.DAG.getMachineFunction(),
10733                            CLI.IsVarArg, Outs, CLI.RetTy->getContext());
10734 
10735   SDValue DemoteStackSlot;
10736   int DemoteStackIdx = -100;
10737   if (!CanLowerReturn) {
10738     // FIXME: equivalent assert?
10739     // assert(!CS.hasInAllocaArgument() &&
10740     //        "sret demotion is incompatible with inalloca");
10741     uint64_t TySize = DL.getTypeAllocSize(CLI.RetTy);
10742     Align Alignment = DL.getPrefTypeAlign(CLI.RetTy);
10743     MachineFunction &MF = CLI.DAG.getMachineFunction();
10744     DemoteStackIdx =
10745         MF.getFrameInfo().CreateStackObject(TySize, Alignment, false);
10746     Type *StackSlotPtrType = PointerType::get(CLI.RetTy,
10747                                               DL.getAllocaAddrSpace());
10748 
10749     DemoteStackSlot = CLI.DAG.getFrameIndex(DemoteStackIdx, getFrameIndexTy(DL));
10750     ArgListEntry Entry;
10751     Entry.Node = DemoteStackSlot;
10752     Entry.Ty = StackSlotPtrType;
10753     Entry.IsSExt = false;
10754     Entry.IsZExt = false;
10755     Entry.IsInReg = false;
10756     Entry.IsSRet = true;
10757     Entry.IsNest = false;
10758     Entry.IsByVal = false;
10759     Entry.IsByRef = false;
10760     Entry.IsReturned = false;
10761     Entry.IsSwiftSelf = false;
10762     Entry.IsSwiftAsync = false;
10763     Entry.IsSwiftError = false;
10764     Entry.IsCFGuardTarget = false;
10765     Entry.Alignment = Alignment;
10766     CLI.getArgs().insert(CLI.getArgs().begin(), Entry);
10767     CLI.NumFixedArgs += 1;
10768     CLI.getArgs()[0].IndirectType = CLI.RetTy;
10769     CLI.RetTy = Type::getVoidTy(CLI.RetTy->getContext());
10770 
10771     // sret demotion isn't compatible with tail-calls, since the sret argument
10772     // points into the callers stack frame.
10773     CLI.IsTailCall = false;
10774   } else {
10775     bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
10776         CLI.RetTy, CLI.CallConv, CLI.IsVarArg, DL);
10777     for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
10778       ISD::ArgFlagsTy Flags;
10779       if (NeedsRegBlock) {
10780         Flags.setInConsecutiveRegs();
10781         if (I == RetTys.size() - 1)
10782           Flags.setInConsecutiveRegsLast();
10783       }
10784       EVT VT = RetTys[I];
10785       MVT RegisterVT = getRegisterTypeForCallingConv(CLI.RetTy->getContext(),
10786                                                      CLI.CallConv, VT);
10787       unsigned NumRegs = getNumRegistersForCallingConv(CLI.RetTy->getContext(),
10788                                                        CLI.CallConv, VT);
10789       for (unsigned i = 0; i != NumRegs; ++i) {
10790         ISD::InputArg MyFlags;
10791         MyFlags.Flags = Flags;
10792         MyFlags.VT = RegisterVT;
10793         MyFlags.ArgVT = VT;
10794         MyFlags.Used = CLI.IsReturnValueUsed;
10795         if (CLI.RetTy->isPointerTy()) {
10796           MyFlags.Flags.setPointer();
10797           MyFlags.Flags.setPointerAddrSpace(
10798               cast<PointerType>(CLI.RetTy)->getAddressSpace());
10799         }
10800         if (CLI.RetSExt)
10801           MyFlags.Flags.setSExt();
10802         if (CLI.RetZExt)
10803           MyFlags.Flags.setZExt();
10804         if (CLI.IsInReg)
10805           MyFlags.Flags.setInReg();
10806         CLI.Ins.push_back(MyFlags);
10807       }
10808     }
10809   }
10810 
10811   // We push in swifterror return as the last element of CLI.Ins.
10812   ArgListTy &Args = CLI.getArgs();
10813   if (supportSwiftError()) {
10814     for (const ArgListEntry &Arg : Args) {
10815       if (Arg.IsSwiftError) {
10816         ISD::InputArg MyFlags;
10817         MyFlags.VT = getPointerTy(DL);
10818         MyFlags.ArgVT = EVT(getPointerTy(DL));
10819         MyFlags.Flags.setSwiftError();
10820         CLI.Ins.push_back(MyFlags);
10821       }
10822     }
10823   }
10824 
10825   // Handle all of the outgoing arguments.
10826   CLI.Outs.clear();
10827   CLI.OutVals.clear();
10828   for (unsigned i = 0, e = Args.size(); i != e; ++i) {
10829     SmallVector<EVT, 4> ValueVTs;
10830     ComputeValueVTs(*this, DL, Args[i].Ty, ValueVTs);
10831     // FIXME: Split arguments if CLI.IsPostTypeLegalization
10832     Type *FinalType = Args[i].Ty;
10833     if (Args[i].IsByVal)
10834       FinalType = Args[i].IndirectType;
10835     bool NeedsRegBlock = functionArgumentNeedsConsecutiveRegisters(
10836         FinalType, CLI.CallConv, CLI.IsVarArg, DL);
10837     for (unsigned Value = 0, NumValues = ValueVTs.size(); Value != NumValues;
10838          ++Value) {
10839       EVT VT = ValueVTs[Value];
10840       Type *ArgTy = VT.getTypeForEVT(CLI.RetTy->getContext());
10841       SDValue Op = SDValue(Args[i].Node.getNode(),
10842                            Args[i].Node.getResNo() + Value);
10843       ISD::ArgFlagsTy Flags;
10844 
10845       // Certain targets (such as MIPS), may have a different ABI alignment
10846       // for a type depending on the context. Give the target a chance to
10847       // specify the alignment it wants.
10848       const Align OriginalAlignment(getABIAlignmentForCallingConv(ArgTy, DL));
10849       Flags.setOrigAlign(OriginalAlignment);
10850 
10851       if (Args[i].Ty->isPointerTy()) {
10852         Flags.setPointer();
10853         Flags.setPointerAddrSpace(
10854             cast<PointerType>(Args[i].Ty)->getAddressSpace());
10855       }
10856       if (Args[i].IsZExt)
10857         Flags.setZExt();
10858       if (Args[i].IsSExt)
10859         Flags.setSExt();
10860       if (Args[i].IsInReg) {
10861         // If we are using vectorcall calling convention, a structure that is
10862         // passed InReg - is surely an HVA
10863         if (CLI.CallConv == CallingConv::X86_VectorCall &&
10864             isa<StructType>(FinalType)) {
10865           // The first value of a structure is marked
10866           if (0 == Value)
10867             Flags.setHvaStart();
10868           Flags.setHva();
10869         }
10870         // Set InReg Flag
10871         Flags.setInReg();
10872       }
10873       if (Args[i].IsSRet)
10874         Flags.setSRet();
10875       if (Args[i].IsSwiftSelf)
10876         Flags.setSwiftSelf();
10877       if (Args[i].IsSwiftAsync)
10878         Flags.setSwiftAsync();
10879       if (Args[i].IsSwiftError)
10880         Flags.setSwiftError();
10881       if (Args[i].IsCFGuardTarget)
10882         Flags.setCFGuardTarget();
10883       if (Args[i].IsByVal)
10884         Flags.setByVal();
10885       if (Args[i].IsByRef)
10886         Flags.setByRef();
10887       if (Args[i].IsPreallocated) {
10888         Flags.setPreallocated();
10889         // Set the byval flag for CCAssignFn callbacks that don't know about
10890         // preallocated.  This way we can know how many bytes we should've
10891         // allocated and how many bytes a callee cleanup function will pop.  If
10892         // we port preallocated to more targets, we'll have to add custom
10893         // preallocated handling in the various CC lowering callbacks.
10894         Flags.setByVal();
10895       }
10896       if (Args[i].IsInAlloca) {
10897         Flags.setInAlloca();
10898         // Set the byval flag for CCAssignFn callbacks that don't know about
10899         // inalloca.  This way we can know how many bytes we should've allocated
10900         // and how many bytes a callee cleanup function will pop.  If we port
10901         // inalloca to more targets, we'll have to add custom inalloca handling
10902         // in the various CC lowering callbacks.
10903         Flags.setByVal();
10904       }
10905       Align MemAlign;
10906       if (Args[i].IsByVal || Args[i].IsInAlloca || Args[i].IsPreallocated) {
10907         unsigned FrameSize = DL.getTypeAllocSize(Args[i].IndirectType);
10908         Flags.setByValSize(FrameSize);
10909 
10910         // info is not there but there are cases it cannot get right.
10911         if (auto MA = Args[i].Alignment)
10912           MemAlign = *MA;
10913         else
10914           MemAlign = Align(getByValTypeAlignment(Args[i].IndirectType, DL));
10915       } else if (auto MA = Args[i].Alignment) {
10916         MemAlign = *MA;
10917       } else {
10918         MemAlign = OriginalAlignment;
10919       }
10920       Flags.setMemAlign(MemAlign);
10921       if (Args[i].IsNest)
10922         Flags.setNest();
10923       if (NeedsRegBlock)
10924         Flags.setInConsecutiveRegs();
10925 
10926       MVT PartVT = getRegisterTypeForCallingConv(CLI.RetTy->getContext(),
10927                                                  CLI.CallConv, VT);
10928       unsigned NumParts = getNumRegistersForCallingConv(CLI.RetTy->getContext(),
10929                                                         CLI.CallConv, VT);
10930       SmallVector<SDValue, 4> Parts(NumParts);
10931       ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
10932 
10933       if (Args[i].IsSExt)
10934         ExtendKind = ISD::SIGN_EXTEND;
10935       else if (Args[i].IsZExt)
10936         ExtendKind = ISD::ZERO_EXTEND;
10937 
10938       // Conservatively only handle 'returned' on non-vectors that can be lowered,
10939       // for now.
10940       if (Args[i].IsReturned && !Op.getValueType().isVector() &&
10941           CanLowerReturn) {
10942         assert((CLI.RetTy == Args[i].Ty ||
10943                 (CLI.RetTy->isPointerTy() && Args[i].Ty->isPointerTy() &&
10944                  CLI.RetTy->getPointerAddressSpace() ==
10945                      Args[i].Ty->getPointerAddressSpace())) &&
10946                RetTys.size() == NumValues && "unexpected use of 'returned'");
10947         // Before passing 'returned' to the target lowering code, ensure that
10948         // either the register MVT and the actual EVT are the same size or that
10949         // the return value and argument are extended in the same way; in these
10950         // cases it's safe to pass the argument register value unchanged as the
10951         // return register value (although it's at the target's option whether
10952         // to do so)
10953         // TODO: allow code generation to take advantage of partially preserved
10954         // registers rather than clobbering the entire register when the
10955         // parameter extension method is not compatible with the return
10956         // extension method
10957         if ((NumParts * PartVT.getSizeInBits() == VT.getSizeInBits()) ||
10958             (ExtendKind != ISD::ANY_EXTEND && CLI.RetSExt == Args[i].IsSExt &&
10959              CLI.RetZExt == Args[i].IsZExt))
10960           Flags.setReturned();
10961       }
10962 
10963       getCopyToParts(CLI.DAG, CLI.DL, Op, &Parts[0], NumParts, PartVT, CLI.CB,
10964                      CLI.CallConv, ExtendKind);
10965 
10966       for (unsigned j = 0; j != NumParts; ++j) {
10967         // if it isn't first piece, alignment must be 1
10968         // For scalable vectors the scalable part is currently handled
10969         // by individual targets, so we just use the known minimum size here.
10970         ISD::OutputArg MyFlags(
10971             Flags, Parts[j].getValueType().getSimpleVT(), VT,
10972             i < CLI.NumFixedArgs, i,
10973             j * Parts[j].getValueType().getStoreSize().getKnownMinValue());
10974         if (NumParts > 1 && j == 0)
10975           MyFlags.Flags.setSplit();
10976         else if (j != 0) {
10977           MyFlags.Flags.setOrigAlign(Align(1));
10978           if (j == NumParts - 1)
10979             MyFlags.Flags.setSplitEnd();
10980         }
10981 
10982         CLI.Outs.push_back(MyFlags);
10983         CLI.OutVals.push_back(Parts[j]);
10984       }
10985 
10986       if (NeedsRegBlock && Value == NumValues - 1)
10987         CLI.Outs[CLI.Outs.size() - 1].Flags.setInConsecutiveRegsLast();
10988     }
10989   }
10990 
10991   SmallVector<SDValue, 4> InVals;
10992   CLI.Chain = LowerCall(CLI, InVals);
10993 
10994   // Update CLI.InVals to use outside of this function.
10995   CLI.InVals = InVals;
10996 
10997   // Verify that the target's LowerCall behaved as expected.
10998   assert(CLI.Chain.getNode() && CLI.Chain.getValueType() == MVT::Other &&
10999          "LowerCall didn't return a valid chain!");
11000   assert((!CLI.IsTailCall || InVals.empty()) &&
11001          "LowerCall emitted a return value for a tail call!");
11002   assert((CLI.IsTailCall || InVals.size() == CLI.Ins.size()) &&
11003          "LowerCall didn't emit the correct number of values!");
11004 
11005   // For a tail call, the return value is merely live-out and there aren't
11006   // any nodes in the DAG representing it. Return a special value to
11007   // indicate that a tail call has been emitted and no more Instructions
11008   // should be processed in the current block.
11009   if (CLI.IsTailCall) {
11010     CLI.DAG.setRoot(CLI.Chain);
11011     return std::make_pair(SDValue(), SDValue());
11012   }
11013 
11014 #ifndef NDEBUG
11015   for (unsigned i = 0, e = CLI.Ins.size(); i != e; ++i) {
11016     assert(InVals[i].getNode() && "LowerCall emitted a null value!");
11017     assert(EVT(CLI.Ins[i].VT) == InVals[i].getValueType() &&
11018            "LowerCall emitted a value with the wrong type!");
11019   }
11020 #endif
11021 
11022   SmallVector<SDValue, 4> ReturnValues;
11023   if (!CanLowerReturn) {
11024     // The instruction result is the result of loading from the
11025     // hidden sret parameter.
11026     SmallVector<EVT, 1> PVTs;
11027     Type *PtrRetTy =
11028         PointerType::get(OrigRetTy->getContext(), DL.getAllocaAddrSpace());
11029 
11030     ComputeValueVTs(*this, DL, PtrRetTy, PVTs);
11031     assert(PVTs.size() == 1 && "Pointers should fit in one register");
11032     EVT PtrVT = PVTs[0];
11033 
11034     unsigned NumValues = RetTys.size();
11035     ReturnValues.resize(NumValues);
11036     SmallVector<SDValue, 4> Chains(NumValues);
11037 
11038     // An aggregate return value cannot wrap around the address space, so
11039     // offsets to its parts don't wrap either.
11040     SDNodeFlags Flags;
11041     Flags.setNoUnsignedWrap(true);
11042 
11043     MachineFunction &MF = CLI.DAG.getMachineFunction();
11044     Align HiddenSRetAlign = MF.getFrameInfo().getObjectAlign(DemoteStackIdx);
11045     for (unsigned i = 0; i < NumValues; ++i) {
11046       SDValue Add = CLI.DAG.getNode(ISD::ADD, CLI.DL, PtrVT, DemoteStackSlot,
11047                                     CLI.DAG.getConstant(Offsets[i], CLI.DL,
11048                                                         PtrVT), Flags);
11049       SDValue L = CLI.DAG.getLoad(
11050           RetTys[i], CLI.DL, CLI.Chain, Add,
11051           MachinePointerInfo::getFixedStack(CLI.DAG.getMachineFunction(),
11052                                             DemoteStackIdx, Offsets[i]),
11053           HiddenSRetAlign);
11054       ReturnValues[i] = L;
11055       Chains[i] = L.getValue(1);
11056     }
11057 
11058     CLI.Chain = CLI.DAG.getNode(ISD::TokenFactor, CLI.DL, MVT::Other, Chains);
11059   } else {
11060     // Collect the legal value parts into potentially illegal values
11061     // that correspond to the original function's return values.
11062     std::optional<ISD::NodeType> AssertOp;
11063     if (CLI.RetSExt)
11064       AssertOp = ISD::AssertSext;
11065     else if (CLI.RetZExt)
11066       AssertOp = ISD::AssertZext;
11067     unsigned CurReg = 0;
11068     for (EVT VT : RetTys) {
11069       MVT RegisterVT = getRegisterTypeForCallingConv(CLI.RetTy->getContext(),
11070                                                      CLI.CallConv, VT);
11071       unsigned NumRegs = getNumRegistersForCallingConv(CLI.RetTy->getContext(),
11072                                                        CLI.CallConv, VT);
11073 
11074       ReturnValues.push_back(getCopyFromParts(
11075           CLI.DAG, CLI.DL, &InVals[CurReg], NumRegs, RegisterVT, VT, nullptr,
11076           CLI.Chain, CLI.CallConv, AssertOp));
11077       CurReg += NumRegs;
11078     }
11079 
11080     // For a function returning void, there is no return value. We can't create
11081     // such a node, so we just return a null return value in that case. In
11082     // that case, nothing will actually look at the value.
11083     if (ReturnValues.empty())
11084       return std::make_pair(SDValue(), CLI.Chain);
11085   }
11086 
11087   SDValue Res = CLI.DAG.getNode(ISD::MERGE_VALUES, CLI.DL,
11088                                 CLI.DAG.getVTList(RetTys), ReturnValues);
11089   return std::make_pair(Res, CLI.Chain);
11090 }
11091 
11092 /// Places new result values for the node in Results (their number
11093 /// and types must exactly match those of the original return values of
11094 /// the node), or leaves Results empty, which indicates that the node is not
11095 /// to be custom lowered after all.
11096 void TargetLowering::LowerOperationWrapper(SDNode *N,
11097                                            SmallVectorImpl<SDValue> &Results,
11098                                            SelectionDAG &DAG) const {
11099   SDValue Res = LowerOperation(SDValue(N, 0), DAG);
11100 
11101   if (!Res.getNode())
11102     return;
11103 
11104   // If the original node has one result, take the return value from
11105   // LowerOperation as is. It might not be result number 0.
11106   if (N->getNumValues() == 1) {
11107     Results.push_back(Res);
11108     return;
11109   }
11110 
11111   // If the original node has multiple results, then the return node should
11112   // have the same number of results.
11113   assert((N->getNumValues() == Res->getNumValues()) &&
11114       "Lowering returned the wrong number of results!");
11115 
11116   // Places new result values base on N result number.
11117   for (unsigned I = 0, E = N->getNumValues(); I != E; ++I)
11118     Results.push_back(Res.getValue(I));
11119 }
11120 
11121 SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
11122   llvm_unreachable("LowerOperation not implemented for this target!");
11123 }
11124 
11125 void SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V,
11126                                                      unsigned Reg,
11127                                                      ISD::NodeType ExtendType) {
11128   SDValue Op = getNonRegisterValue(V);
11129   assert((Op.getOpcode() != ISD::CopyFromReg ||
11130           cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
11131          "Copy from a reg to the same reg!");
11132   assert(!Register::isPhysicalRegister(Reg) && "Is a physreg");
11133 
11134   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11135   // If this is an InlineAsm we have to match the registers required, not the
11136   // notional registers required by the type.
11137 
11138   RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg, V->getType(),
11139                    std::nullopt); // This is not an ABI copy.
11140   SDValue Chain = DAG.getEntryNode();
11141 
11142   if (ExtendType == ISD::ANY_EXTEND) {
11143     auto PreferredExtendIt = FuncInfo.PreferredExtendType.find(V);
11144     if (PreferredExtendIt != FuncInfo.PreferredExtendType.end())
11145       ExtendType = PreferredExtendIt->second;
11146   }
11147   RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, nullptr, V, ExtendType);
11148   PendingExports.push_back(Chain);
11149 }
11150 
11151 #include "llvm/CodeGen/SelectionDAGISel.h"
11152 
11153 /// isOnlyUsedInEntryBlock - If the specified argument is only used in the
11154 /// entry block, return true.  This includes arguments used by switches, since
11155 /// the switch may expand into multiple basic blocks.
11156 static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel) {
11157   // With FastISel active, we may be splitting blocks, so force creation
11158   // of virtual registers for all non-dead arguments.
11159   if (FastISel)
11160     return A->use_empty();
11161 
11162   const BasicBlock &Entry = A->getParent()->front();
11163   for (const User *U : A->users())
11164     if (cast<Instruction>(U)->getParent() != &Entry || isa<SwitchInst>(U))
11165       return false;  // Use not in entry block.
11166 
11167   return true;
11168 }
11169 
11170 using ArgCopyElisionMapTy =
11171     DenseMap<const Argument *,
11172              std::pair<const AllocaInst *, const StoreInst *>>;
11173 
11174 /// Scan the entry block of the function in FuncInfo for arguments that look
11175 /// like copies into a local alloca. Record any copied arguments in
11176 /// ArgCopyElisionCandidates.
11177 static void
11178 findArgumentCopyElisionCandidates(const DataLayout &DL,
11179                                   FunctionLoweringInfo *FuncInfo,
11180                                   ArgCopyElisionMapTy &ArgCopyElisionCandidates) {
11181   // Record the state of every static alloca used in the entry block. Argument
11182   // allocas are all used in the entry block, so we need approximately as many
11183   // entries as we have arguments.
11184   enum StaticAllocaInfo { Unknown, Clobbered, Elidable };
11185   SmallDenseMap<const AllocaInst *, StaticAllocaInfo, 8> StaticAllocas;
11186   unsigned NumArgs = FuncInfo->Fn->arg_size();
11187   StaticAllocas.reserve(NumArgs * 2);
11188 
11189   auto GetInfoIfStaticAlloca = [&](const Value *V) -> StaticAllocaInfo * {
11190     if (!V)
11191       return nullptr;
11192     V = V->stripPointerCasts();
11193     const auto *AI = dyn_cast<AllocaInst>(V);
11194     if (!AI || !AI->isStaticAlloca() || !FuncInfo->StaticAllocaMap.count(AI))
11195       return nullptr;
11196     auto Iter = StaticAllocas.insert({AI, Unknown});
11197     return &Iter.first->second;
11198   };
11199 
11200   // Look for stores of arguments to static allocas. Look through bitcasts and
11201   // GEPs to handle type coercions, as long as the alloca is fully initialized
11202   // by the store. Any non-store use of an alloca escapes it and any subsequent
11203   // unanalyzed store might write it.
11204   // FIXME: Handle structs initialized with multiple stores.
11205   for (const Instruction &I : FuncInfo->Fn->getEntryBlock()) {
11206     // Look for stores, and handle non-store uses conservatively.
11207     const auto *SI = dyn_cast<StoreInst>(&I);
11208     if (!SI) {
11209       // We will look through cast uses, so ignore them completely.
11210       if (I.isCast())
11211         continue;
11212       // Ignore debug info and pseudo op intrinsics, they don't escape or store
11213       // to allocas.
11214       if (I.isDebugOrPseudoInst())
11215         continue;
11216       // This is an unknown instruction. Assume it escapes or writes to all
11217       // static alloca operands.
11218       for (const Use &U : I.operands()) {
11219         if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(U))
11220           *Info = StaticAllocaInfo::Clobbered;
11221       }
11222       continue;
11223     }
11224 
11225     // If the stored value is a static alloca, mark it as escaped.
11226     if (StaticAllocaInfo *Info = GetInfoIfStaticAlloca(SI->getValueOperand()))
11227       *Info = StaticAllocaInfo::Clobbered;
11228 
11229     // Check if the destination is a static alloca.
11230     const Value *Dst = SI->getPointerOperand()->stripPointerCasts();
11231     StaticAllocaInfo *Info = GetInfoIfStaticAlloca(Dst);
11232     if (!Info)
11233       continue;
11234     const AllocaInst *AI = cast<AllocaInst>(Dst);
11235 
11236     // Skip allocas that have been initialized or clobbered.
11237     if (*Info != StaticAllocaInfo::Unknown)
11238       continue;
11239 
11240     // Check if the stored value is an argument, and that this store fully
11241     // initializes the alloca.
11242     // If the argument type has padding bits we can't directly forward a pointer
11243     // as the upper bits may contain garbage.
11244     // Don't elide copies from the same argument twice.
11245     const Value *Val = SI->getValueOperand()->stripPointerCasts();
11246     const auto *Arg = dyn_cast<Argument>(Val);
11247     if (!Arg || Arg->hasPassPointeeByValueCopyAttr() ||
11248         Arg->getType()->isEmptyTy() ||
11249         DL.getTypeStoreSize(Arg->getType()) !=
11250             DL.getTypeAllocSize(AI->getAllocatedType()) ||
11251         !DL.typeSizeEqualsStoreSize(Arg->getType()) ||
11252         ArgCopyElisionCandidates.count(Arg)) {
11253       *Info = StaticAllocaInfo::Clobbered;
11254       continue;
11255     }
11256 
11257     LLVM_DEBUG(dbgs() << "Found argument copy elision candidate: " << *AI
11258                       << '\n');
11259 
11260     // Mark this alloca and store for argument copy elision.
11261     *Info = StaticAllocaInfo::Elidable;
11262     ArgCopyElisionCandidates.insert({Arg, {AI, SI}});
11263 
11264     // Stop scanning if we've seen all arguments. This will happen early in -O0
11265     // builds, which is useful, because -O0 builds have large entry blocks and
11266     // many allocas.
11267     if (ArgCopyElisionCandidates.size() == NumArgs)
11268       break;
11269   }
11270 }
11271 
11272 /// Try to elide argument copies from memory into a local alloca. Succeeds if
11273 /// ArgVal is a load from a suitable fixed stack object.
11274 static void tryToElideArgumentCopy(
11275     FunctionLoweringInfo &FuncInfo, SmallVectorImpl<SDValue> &Chains,
11276     DenseMap<int, int> &ArgCopyElisionFrameIndexMap,
11277     SmallPtrSetImpl<const Instruction *> &ElidedArgCopyInstrs,
11278     ArgCopyElisionMapTy &ArgCopyElisionCandidates, const Argument &Arg,
11279     ArrayRef<SDValue> ArgVals, bool &ArgHasUses) {
11280   // Check if this is a load from a fixed stack object.
11281   auto *LNode = dyn_cast<LoadSDNode>(ArgVals[0]);
11282   if (!LNode)
11283     return;
11284   auto *FINode = dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode());
11285   if (!FINode)
11286     return;
11287 
11288   // Check that the fixed stack object is the right size and alignment.
11289   // Look at the alignment that the user wrote on the alloca instead of looking
11290   // at the stack object.
11291   auto ArgCopyIter = ArgCopyElisionCandidates.find(&Arg);
11292   assert(ArgCopyIter != ArgCopyElisionCandidates.end());
11293   const AllocaInst *AI = ArgCopyIter->second.first;
11294   int FixedIndex = FINode->getIndex();
11295   int &AllocaIndex = FuncInfo.StaticAllocaMap[AI];
11296   int OldIndex = AllocaIndex;
11297   MachineFrameInfo &MFI = FuncInfo.MF->getFrameInfo();
11298   if (MFI.getObjectSize(FixedIndex) != MFI.getObjectSize(OldIndex)) {
11299     LLVM_DEBUG(
11300         dbgs() << "  argument copy elision failed due to bad fixed stack "
11301                   "object size\n");
11302     return;
11303   }
11304   Align RequiredAlignment = AI->getAlign();
11305   if (MFI.getObjectAlign(FixedIndex) < RequiredAlignment) {
11306     LLVM_DEBUG(dbgs() << "  argument copy elision failed: alignment of alloca "
11307                          "greater than stack argument alignment ("
11308                       << DebugStr(RequiredAlignment) << " vs "
11309                       << DebugStr(MFI.getObjectAlign(FixedIndex)) << ")\n");
11310     return;
11311   }
11312 
11313   // Perform the elision. Delete the old stack object and replace its only use
11314   // in the variable info map. Mark the stack object as mutable and aliased.
11315   LLVM_DEBUG({
11316     dbgs() << "Eliding argument copy from " << Arg << " to " << *AI << '\n'
11317            << "  Replacing frame index " << OldIndex << " with " << FixedIndex
11318            << '\n';
11319   });
11320   MFI.RemoveStackObject(OldIndex);
11321   MFI.setIsImmutableObjectIndex(FixedIndex, false);
11322   MFI.setIsAliasedObjectIndex(FixedIndex, true);
11323   AllocaIndex = FixedIndex;
11324   ArgCopyElisionFrameIndexMap.insert({OldIndex, FixedIndex});
11325   for (SDValue ArgVal : ArgVals)
11326     Chains.push_back(ArgVal.getValue(1));
11327 
11328   // Avoid emitting code for the store implementing the copy.
11329   const StoreInst *SI = ArgCopyIter->second.second;
11330   ElidedArgCopyInstrs.insert(SI);
11331 
11332   // Check for uses of the argument again so that we can avoid exporting ArgVal
11333   // if it is't used by anything other than the store.
11334   for (const Value *U : Arg.users()) {
11335     if (U != SI) {
11336       ArgHasUses = true;
11337       break;
11338     }
11339   }
11340 }
11341 
11342 void SelectionDAGISel::LowerArguments(const Function &F) {
11343   SelectionDAG &DAG = SDB->DAG;
11344   SDLoc dl = SDB->getCurSDLoc();
11345   const DataLayout &DL = DAG.getDataLayout();
11346   SmallVector<ISD::InputArg, 16> Ins;
11347 
11348   // In Naked functions we aren't going to save any registers.
11349   if (F.hasFnAttribute(Attribute::Naked))
11350     return;
11351 
11352   if (!FuncInfo->CanLowerReturn) {
11353     // Put in an sret pointer parameter before all the other parameters.
11354     SmallVector<EVT, 1> ValueVTs;
11355     ComputeValueVTs(*TLI, DAG.getDataLayout(),
11356                     PointerType::get(F.getContext(),
11357                                      DAG.getDataLayout().getAllocaAddrSpace()),
11358                     ValueVTs);
11359 
11360     // NOTE: Assuming that a pointer will never break down to more than one VT
11361     // or one register.
11362     ISD::ArgFlagsTy Flags;
11363     Flags.setSRet();
11364     MVT RegisterVT = TLI->getRegisterType(*DAG.getContext(), ValueVTs[0]);
11365     ISD::InputArg RetArg(Flags, RegisterVT, ValueVTs[0], true,
11366                          ISD::InputArg::NoArgIndex, 0);
11367     Ins.push_back(RetArg);
11368   }
11369 
11370   // Look for stores of arguments to static allocas. Mark such arguments with a
11371   // flag to ask the target to give us the memory location of that argument if
11372   // available.
11373   ArgCopyElisionMapTy ArgCopyElisionCandidates;
11374   findArgumentCopyElisionCandidates(DL, FuncInfo.get(),
11375                                     ArgCopyElisionCandidates);
11376 
11377   // Set up the incoming argument description vector.
11378   for (const Argument &Arg : F.args()) {
11379     unsigned ArgNo = Arg.getArgNo();
11380     SmallVector<EVT, 4> ValueVTs;
11381     ComputeValueVTs(*TLI, DAG.getDataLayout(), Arg.getType(), ValueVTs);
11382     bool isArgValueUsed = !Arg.use_empty();
11383     unsigned PartBase = 0;
11384     Type *FinalType = Arg.getType();
11385     if (Arg.hasAttribute(Attribute::ByVal))
11386       FinalType = Arg.getParamByValType();
11387     bool NeedsRegBlock = TLI->functionArgumentNeedsConsecutiveRegisters(
11388         FinalType, F.getCallingConv(), F.isVarArg(), DL);
11389     for (unsigned Value = 0, NumValues = ValueVTs.size();
11390          Value != NumValues; ++Value) {
11391       EVT VT = ValueVTs[Value];
11392       Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
11393       ISD::ArgFlagsTy Flags;
11394 
11395 
11396       if (Arg.getType()->isPointerTy()) {
11397         Flags.setPointer();
11398         Flags.setPointerAddrSpace(
11399             cast<PointerType>(Arg.getType())->getAddressSpace());
11400       }
11401       if (Arg.hasAttribute(Attribute::ZExt))
11402         Flags.setZExt();
11403       if (Arg.hasAttribute(Attribute::SExt))
11404         Flags.setSExt();
11405       if (Arg.hasAttribute(Attribute::InReg)) {
11406         // If we are using vectorcall calling convention, a structure that is
11407         // passed InReg - is surely an HVA
11408         if (F.getCallingConv() == CallingConv::X86_VectorCall &&
11409             isa<StructType>(Arg.getType())) {
11410           // The first value of a structure is marked
11411           if (0 == Value)
11412             Flags.setHvaStart();
11413           Flags.setHva();
11414         }
11415         // Set InReg Flag
11416         Flags.setInReg();
11417       }
11418       if (Arg.hasAttribute(Attribute::StructRet))
11419         Flags.setSRet();
11420       if (Arg.hasAttribute(Attribute::SwiftSelf))
11421         Flags.setSwiftSelf();
11422       if (Arg.hasAttribute(Attribute::SwiftAsync))
11423         Flags.setSwiftAsync();
11424       if (Arg.hasAttribute(Attribute::SwiftError))
11425         Flags.setSwiftError();
11426       if (Arg.hasAttribute(Attribute::ByVal))
11427         Flags.setByVal();
11428       if (Arg.hasAttribute(Attribute::ByRef))
11429         Flags.setByRef();
11430       if (Arg.hasAttribute(Attribute::InAlloca)) {
11431         Flags.setInAlloca();
11432         // Set the byval flag for CCAssignFn callbacks that don't know about
11433         // inalloca.  This way we can know how many bytes we should've allocated
11434         // and how many bytes a callee cleanup function will pop.  If we port
11435         // inalloca to more targets, we'll have to add custom inalloca handling
11436         // in the various CC lowering callbacks.
11437         Flags.setByVal();
11438       }
11439       if (Arg.hasAttribute(Attribute::Preallocated)) {
11440         Flags.setPreallocated();
11441         // Set the byval flag for CCAssignFn callbacks that don't know about
11442         // preallocated.  This way we can know how many bytes we should've
11443         // allocated and how many bytes a callee cleanup function will pop.  If
11444         // we port preallocated to more targets, we'll have to add custom
11445         // preallocated handling in the various CC lowering callbacks.
11446         Flags.setByVal();
11447       }
11448 
11449       // Certain targets (such as MIPS), may have a different ABI alignment
11450       // for a type depending on the context. Give the target a chance to
11451       // specify the alignment it wants.
11452       const Align OriginalAlignment(
11453           TLI->getABIAlignmentForCallingConv(ArgTy, DL));
11454       Flags.setOrigAlign(OriginalAlignment);
11455 
11456       Align MemAlign;
11457       Type *ArgMemTy = nullptr;
11458       if (Flags.isByVal() || Flags.isInAlloca() || Flags.isPreallocated() ||
11459           Flags.isByRef()) {
11460         if (!ArgMemTy)
11461           ArgMemTy = Arg.getPointeeInMemoryValueType();
11462 
11463         uint64_t MemSize = DL.getTypeAllocSize(ArgMemTy);
11464 
11465         // For in-memory arguments, size and alignment should be passed from FE.
11466         // BE will guess if this info is not there but there are cases it cannot
11467         // get right.
11468         if (auto ParamAlign = Arg.getParamStackAlign())
11469           MemAlign = *ParamAlign;
11470         else if ((ParamAlign = Arg.getParamAlign()))
11471           MemAlign = *ParamAlign;
11472         else
11473           MemAlign = Align(TLI->getByValTypeAlignment(ArgMemTy, DL));
11474         if (Flags.isByRef())
11475           Flags.setByRefSize(MemSize);
11476         else
11477           Flags.setByValSize(MemSize);
11478       } else if (auto ParamAlign = Arg.getParamStackAlign()) {
11479         MemAlign = *ParamAlign;
11480       } else {
11481         MemAlign = OriginalAlignment;
11482       }
11483       Flags.setMemAlign(MemAlign);
11484 
11485       if (Arg.hasAttribute(Attribute::Nest))
11486         Flags.setNest();
11487       if (NeedsRegBlock)
11488         Flags.setInConsecutiveRegs();
11489       if (ArgCopyElisionCandidates.count(&Arg))
11490         Flags.setCopyElisionCandidate();
11491       if (Arg.hasAttribute(Attribute::Returned))
11492         Flags.setReturned();
11493 
11494       MVT RegisterVT = TLI->getRegisterTypeForCallingConv(
11495           *CurDAG->getContext(), F.getCallingConv(), VT);
11496       unsigned NumRegs = TLI->getNumRegistersForCallingConv(
11497           *CurDAG->getContext(), F.getCallingConv(), VT);
11498       for (unsigned i = 0; i != NumRegs; ++i) {
11499         // For scalable vectors, use the minimum size; individual targets
11500         // are responsible for handling scalable vector arguments and
11501         // return values.
11502         ISD::InputArg MyFlags(
11503             Flags, RegisterVT, VT, isArgValueUsed, ArgNo,
11504             PartBase + i * RegisterVT.getStoreSize().getKnownMinValue());
11505         if (NumRegs > 1 && i == 0)
11506           MyFlags.Flags.setSplit();
11507         // if it isn't first piece, alignment must be 1
11508         else if (i > 0) {
11509           MyFlags.Flags.setOrigAlign(Align(1));
11510           if (i == NumRegs - 1)
11511             MyFlags.Flags.setSplitEnd();
11512         }
11513         Ins.push_back(MyFlags);
11514       }
11515       if (NeedsRegBlock && Value == NumValues - 1)
11516         Ins[Ins.size() - 1].Flags.setInConsecutiveRegsLast();
11517       PartBase += VT.getStoreSize().getKnownMinValue();
11518     }
11519   }
11520 
11521   // Call the target to set up the argument values.
11522   SmallVector<SDValue, 8> InVals;
11523   SDValue NewRoot = TLI->LowerFormalArguments(
11524       DAG.getRoot(), F.getCallingConv(), F.isVarArg(), Ins, dl, DAG, InVals);
11525 
11526   // Verify that the target's LowerFormalArguments behaved as expected.
11527   assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
11528          "LowerFormalArguments didn't return a valid chain!");
11529   assert(InVals.size() == Ins.size() &&
11530          "LowerFormalArguments didn't emit the correct number of values!");
11531   LLVM_DEBUG({
11532     for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
11533       assert(InVals[i].getNode() &&
11534              "LowerFormalArguments emitted a null value!");
11535       assert(EVT(Ins[i].VT) == InVals[i].getValueType() &&
11536              "LowerFormalArguments emitted a value with the wrong type!");
11537     }
11538   });
11539 
11540   // Update the DAG with the new chain value resulting from argument lowering.
11541   DAG.setRoot(NewRoot);
11542 
11543   // Set up the argument values.
11544   unsigned i = 0;
11545   if (!FuncInfo->CanLowerReturn) {
11546     // Create a virtual register for the sret pointer, and put in a copy
11547     // from the sret argument into it.
11548     SmallVector<EVT, 1> ValueVTs;
11549     ComputeValueVTs(*TLI, DAG.getDataLayout(),
11550                     PointerType::get(F.getContext(),
11551                                      DAG.getDataLayout().getAllocaAddrSpace()),
11552                     ValueVTs);
11553     MVT VT = ValueVTs[0].getSimpleVT();
11554     MVT RegVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
11555     std::optional<ISD::NodeType> AssertOp;
11556     SDValue ArgValue =
11557         getCopyFromParts(DAG, dl, &InVals[0], 1, RegVT, VT, nullptr, NewRoot,
11558                          F.getCallingConv(), AssertOp);
11559 
11560     MachineFunction& MF = SDB->DAG.getMachineFunction();
11561     MachineRegisterInfo& RegInfo = MF.getRegInfo();
11562     Register SRetReg =
11563         RegInfo.createVirtualRegister(TLI->getRegClassFor(RegVT));
11564     FuncInfo->DemoteRegister = SRetReg;
11565     NewRoot =
11566         SDB->DAG.getCopyToReg(NewRoot, SDB->getCurSDLoc(), SRetReg, ArgValue);
11567     DAG.setRoot(NewRoot);
11568 
11569     // i indexes lowered arguments.  Bump it past the hidden sret argument.
11570     ++i;
11571   }
11572 
11573   SmallVector<SDValue, 4> Chains;
11574   DenseMap<int, int> ArgCopyElisionFrameIndexMap;
11575   for (const Argument &Arg : F.args()) {
11576     SmallVector<SDValue, 4> ArgValues;
11577     SmallVector<EVT, 4> ValueVTs;
11578     ComputeValueVTs(*TLI, DAG.getDataLayout(), Arg.getType(), ValueVTs);
11579     unsigned NumValues = ValueVTs.size();
11580     if (NumValues == 0)
11581       continue;
11582 
11583     bool ArgHasUses = !Arg.use_empty();
11584 
11585     // Elide the copying store if the target loaded this argument from a
11586     // suitable fixed stack object.
11587     if (Ins[i].Flags.isCopyElisionCandidate()) {
11588       unsigned NumParts = 0;
11589       for (EVT VT : ValueVTs)
11590         NumParts += TLI->getNumRegistersForCallingConv(*CurDAG->getContext(),
11591                                                        F.getCallingConv(), VT);
11592 
11593       tryToElideArgumentCopy(*FuncInfo, Chains, ArgCopyElisionFrameIndexMap,
11594                              ElidedArgCopyInstrs, ArgCopyElisionCandidates, Arg,
11595                              ArrayRef(&InVals[i], NumParts), ArgHasUses);
11596     }
11597 
11598     // If this argument is unused then remember its value. It is used to generate
11599     // debugging information.
11600     bool isSwiftErrorArg =
11601         TLI->supportSwiftError() &&
11602         Arg.hasAttribute(Attribute::SwiftError);
11603     if (!ArgHasUses && !isSwiftErrorArg) {
11604       SDB->setUnusedArgValue(&Arg, InVals[i]);
11605 
11606       // Also remember any frame index for use in FastISel.
11607       if (FrameIndexSDNode *FI =
11608           dyn_cast<FrameIndexSDNode>(InVals[i].getNode()))
11609         FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
11610     }
11611 
11612     for (unsigned Val = 0; Val != NumValues; ++Val) {
11613       EVT VT = ValueVTs[Val];
11614       MVT PartVT = TLI->getRegisterTypeForCallingConv(*CurDAG->getContext(),
11615                                                       F.getCallingConv(), VT);
11616       unsigned NumParts = TLI->getNumRegistersForCallingConv(
11617           *CurDAG->getContext(), F.getCallingConv(), VT);
11618 
11619       // Even an apparent 'unused' swifterror argument needs to be returned. So
11620       // we do generate a copy for it that can be used on return from the
11621       // function.
11622       if (ArgHasUses || isSwiftErrorArg) {
11623         std::optional<ISD::NodeType> AssertOp;
11624         if (Arg.hasAttribute(Attribute::SExt))
11625           AssertOp = ISD::AssertSext;
11626         else if (Arg.hasAttribute(Attribute::ZExt))
11627           AssertOp = ISD::AssertZext;
11628 
11629         ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i], NumParts,
11630                                              PartVT, VT, nullptr, NewRoot,
11631                                              F.getCallingConv(), AssertOp));
11632       }
11633 
11634       i += NumParts;
11635     }
11636 
11637     // We don't need to do anything else for unused arguments.
11638     if (ArgValues.empty())
11639       continue;
11640 
11641     // Note down frame index.
11642     if (FrameIndexSDNode *FI =
11643         dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode()))
11644       FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
11645 
11646     SDValue Res = DAG.getMergeValues(ArrayRef(ArgValues.data(), NumValues),
11647                                      SDB->getCurSDLoc());
11648 
11649     SDB->setValue(&Arg, Res);
11650     if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::BUILD_PAIR) {
11651       // We want to associate the argument with the frame index, among
11652       // involved operands, that correspond to the lowest address. The
11653       // getCopyFromParts function, called earlier, is swapping the order of
11654       // the operands to BUILD_PAIR depending on endianness. The result of
11655       // that swapping is that the least significant bits of the argument will
11656       // be in the first operand of the BUILD_PAIR node, and the most
11657       // significant bits will be in the second operand.
11658       unsigned LowAddressOp = DAG.getDataLayout().isBigEndian() ? 1 : 0;
11659       if (LoadSDNode *LNode =
11660           dyn_cast<LoadSDNode>(Res.getOperand(LowAddressOp).getNode()))
11661         if (FrameIndexSDNode *FI =
11662             dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
11663           FuncInfo->setArgumentFrameIndex(&Arg, FI->getIndex());
11664     }
11665 
11666     // Analyses past this point are naive and don't expect an assertion.
11667     if (Res.getOpcode() == ISD::AssertZext)
11668       Res = Res.getOperand(0);
11669 
11670     // Update the SwiftErrorVRegDefMap.
11671     if (Res.getOpcode() == ISD::CopyFromReg && isSwiftErrorArg) {
11672       unsigned Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
11673       if (Register::isVirtualRegister(Reg))
11674         SwiftError->setCurrentVReg(FuncInfo->MBB, SwiftError->getFunctionArg(),
11675                                    Reg);
11676     }
11677 
11678     // If this argument is live outside of the entry block, insert a copy from
11679     // wherever we got it to the vreg that other BB's will reference it as.
11680     if (Res.getOpcode() == ISD::CopyFromReg) {
11681       // If we can, though, try to skip creating an unnecessary vreg.
11682       // FIXME: This isn't very clean... it would be nice to make this more
11683       // general.
11684       unsigned Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
11685       if (Register::isVirtualRegister(Reg)) {
11686         FuncInfo->ValueMap[&Arg] = Reg;
11687         continue;
11688       }
11689     }
11690     if (!isOnlyUsedInEntryBlock(&Arg, TM.Options.EnableFastISel)) {
11691       FuncInfo->InitializeRegForValue(&Arg);
11692       SDB->CopyToExportRegsIfNeeded(&Arg);
11693     }
11694   }
11695 
11696   if (!Chains.empty()) {
11697     Chains.push_back(NewRoot);
11698     NewRoot = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
11699   }
11700 
11701   DAG.setRoot(NewRoot);
11702 
11703   assert(i == InVals.size() && "Argument register count mismatch!");
11704 
11705   // If any argument copy elisions occurred and we have debug info, update the
11706   // stale frame indices used in the dbg.declare variable info table.
11707   if (!ArgCopyElisionFrameIndexMap.empty()) {
11708     for (MachineFunction::VariableDbgInfo &VI :
11709          MF->getInStackSlotVariableDbgInfo()) {
11710       auto I = ArgCopyElisionFrameIndexMap.find(VI.getStackSlot());
11711       if (I != ArgCopyElisionFrameIndexMap.end())
11712         VI.updateStackSlot(I->second);
11713     }
11714   }
11715 
11716   // Finally, if the target has anything special to do, allow it to do so.
11717   emitFunctionEntryCode();
11718 }
11719 
11720 /// Handle PHI nodes in successor blocks.  Emit code into the SelectionDAG to
11721 /// ensure constants are generated when needed.  Remember the virtual registers
11722 /// that need to be added to the Machine PHI nodes as input.  We cannot just
11723 /// directly add them, because expansion might result in multiple MBB's for one
11724 /// BB.  As such, the start of the BB might correspond to a different MBB than
11725 /// the end.
11726 void
11727 SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
11728   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11729 
11730   SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
11731 
11732   // Check PHI nodes in successors that expect a value to be available from this
11733   // block.
11734   for (const BasicBlock *SuccBB : successors(LLVMBB->getTerminator())) {
11735     if (!isa<PHINode>(SuccBB->begin())) continue;
11736     MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
11737 
11738     // If this terminator has multiple identical successors (common for
11739     // switches), only handle each succ once.
11740     if (!SuccsHandled.insert(SuccMBB).second)
11741       continue;
11742 
11743     MachineBasicBlock::iterator MBBI = SuccMBB->begin();
11744 
11745     // At this point we know that there is a 1-1 correspondence between LLVM PHI
11746     // nodes and Machine PHI nodes, but the incoming operands have not been
11747     // emitted yet.
11748     for (const PHINode &PN : SuccBB->phis()) {
11749       // Ignore dead phi's.
11750       if (PN.use_empty())
11751         continue;
11752 
11753       // Skip empty types
11754       if (PN.getType()->isEmptyTy())
11755         continue;
11756 
11757       unsigned Reg;
11758       const Value *PHIOp = PN.getIncomingValueForBlock(LLVMBB);
11759 
11760       if (const auto *C = dyn_cast<Constant>(PHIOp)) {
11761         unsigned &RegOut = ConstantsOut[C];
11762         if (RegOut == 0) {
11763           RegOut = FuncInfo.CreateRegs(C);
11764           // We need to zero/sign extend ConstantInt phi operands to match
11765           // assumptions in FunctionLoweringInfo::ComputePHILiveOutRegInfo.
11766           ISD::NodeType ExtendType = ISD::ANY_EXTEND;
11767           if (auto *CI = dyn_cast<ConstantInt>(C))
11768             ExtendType = TLI.signExtendConstant(CI) ? ISD::SIGN_EXTEND
11769                                                     : ISD::ZERO_EXTEND;
11770           CopyValueToVirtualRegister(C, RegOut, ExtendType);
11771         }
11772         Reg = RegOut;
11773       } else {
11774         DenseMap<const Value *, Register>::iterator I =
11775           FuncInfo.ValueMap.find(PHIOp);
11776         if (I != FuncInfo.ValueMap.end())
11777           Reg = I->second;
11778         else {
11779           assert(isa<AllocaInst>(PHIOp) &&
11780                  FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
11781                  "Didn't codegen value into a register!??");
11782           Reg = FuncInfo.CreateRegs(PHIOp);
11783           CopyValueToVirtualRegister(PHIOp, Reg);
11784         }
11785       }
11786 
11787       // Remember that this register needs to added to the machine PHI node as
11788       // the input for this MBB.
11789       SmallVector<EVT, 4> ValueVTs;
11790       ComputeValueVTs(TLI, DAG.getDataLayout(), PN.getType(), ValueVTs);
11791       for (EVT VT : ValueVTs) {
11792         const unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
11793         for (unsigned i = 0; i != NumRegisters; ++i)
11794           FuncInfo.PHINodesToUpdate.push_back(
11795               std::make_pair(&*MBBI++, Reg + i));
11796         Reg += NumRegisters;
11797       }
11798     }
11799   }
11800 
11801   ConstantsOut.clear();
11802 }
11803 
11804 MachineBasicBlock *SelectionDAGBuilder::NextBlock(MachineBasicBlock *MBB) {
11805   MachineFunction::iterator I(MBB);
11806   if (++I == FuncInfo.MF->end())
11807     return nullptr;
11808   return &*I;
11809 }
11810 
11811 /// During lowering new call nodes can be created (such as memset, etc.).
11812 /// Those will become new roots of the current DAG, but complications arise
11813 /// when they are tail calls. In such cases, the call lowering will update
11814 /// the root, but the builder still needs to know that a tail call has been
11815 /// lowered in order to avoid generating an additional return.
11816 void SelectionDAGBuilder::updateDAGForMaybeTailCall(SDValue MaybeTC) {
11817   // If the node is null, we do have a tail call.
11818   if (MaybeTC.getNode() != nullptr)
11819     DAG.setRoot(MaybeTC);
11820   else
11821     HasTailCall = true;
11822 }
11823 
11824 void SelectionDAGBuilder::lowerWorkItem(SwitchWorkListItem W, Value *Cond,
11825                                         MachineBasicBlock *SwitchMBB,
11826                                         MachineBasicBlock *DefaultMBB) {
11827   MachineFunction *CurMF = FuncInfo.MF;
11828   MachineBasicBlock *NextMBB = nullptr;
11829   MachineFunction::iterator BBI(W.MBB);
11830   if (++BBI != FuncInfo.MF->end())
11831     NextMBB = &*BBI;
11832 
11833   unsigned Size = W.LastCluster - W.FirstCluster + 1;
11834 
11835   BranchProbabilityInfo *BPI = FuncInfo.BPI;
11836 
11837   if (Size == 2 && W.MBB == SwitchMBB) {
11838     // If any two of the cases has the same destination, and if one value
11839     // is the same as the other, but has one bit unset that the other has set,
11840     // use bit manipulation to do two compares at once.  For example:
11841     // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
11842     // TODO: This could be extended to merge any 2 cases in switches with 3
11843     // cases.
11844     // TODO: Handle cases where W.CaseBB != SwitchBB.
11845     CaseCluster &Small = *W.FirstCluster;
11846     CaseCluster &Big = *W.LastCluster;
11847 
11848     if (Small.Low == Small.High && Big.Low == Big.High &&
11849         Small.MBB == Big.MBB) {
11850       const APInt &SmallValue = Small.Low->getValue();
11851       const APInt &BigValue = Big.Low->getValue();
11852 
11853       // Check that there is only one bit different.
11854       APInt CommonBit = BigValue ^ SmallValue;
11855       if (CommonBit.isPowerOf2()) {
11856         SDValue CondLHS = getValue(Cond);
11857         EVT VT = CondLHS.getValueType();
11858         SDLoc DL = getCurSDLoc();
11859 
11860         SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS,
11861                                  DAG.getConstant(CommonBit, DL, VT));
11862         SDValue Cond = DAG.getSetCC(
11863             DL, MVT::i1, Or, DAG.getConstant(BigValue | SmallValue, DL, VT),
11864             ISD::SETEQ);
11865 
11866         // Update successor info.
11867         // Both Small and Big will jump to Small.BB, so we sum up the
11868         // probabilities.
11869         addSuccessorWithProb(SwitchMBB, Small.MBB, Small.Prob + Big.Prob);
11870         if (BPI)
11871           addSuccessorWithProb(
11872               SwitchMBB, DefaultMBB,
11873               // The default destination is the first successor in IR.
11874               BPI->getEdgeProbability(SwitchMBB->getBasicBlock(), (unsigned)0));
11875         else
11876           addSuccessorWithProb(SwitchMBB, DefaultMBB);
11877 
11878         // Insert the true branch.
11879         SDValue BrCond =
11880             DAG.getNode(ISD::BRCOND, DL, MVT::Other, getControlRoot(), Cond,
11881                         DAG.getBasicBlock(Small.MBB));
11882         // Insert the false branch.
11883         BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond,
11884                              DAG.getBasicBlock(DefaultMBB));
11885 
11886         DAG.setRoot(BrCond);
11887         return;
11888       }
11889     }
11890   }
11891 
11892   if (TM.getOptLevel() != CodeGenOptLevel::None) {
11893     // Here, we order cases by probability so the most likely case will be
11894     // checked first. However, two clusters can have the same probability in
11895     // which case their relative ordering is non-deterministic. So we use Low
11896     // as a tie-breaker as clusters are guaranteed to never overlap.
11897     llvm::sort(W.FirstCluster, W.LastCluster + 1,
11898                [](const CaseCluster &a, const CaseCluster &b) {
11899       return a.Prob != b.Prob ?
11900              a.Prob > b.Prob :
11901              a.Low->getValue().slt(b.Low->getValue());
11902     });
11903 
11904     // Rearrange the case blocks so that the last one falls through if possible
11905     // without changing the order of probabilities.
11906     for (CaseClusterIt I = W.LastCluster; I > W.FirstCluster; ) {
11907       --I;
11908       if (I->Prob > W.LastCluster->Prob)
11909         break;
11910       if (I->Kind == CC_Range && I->MBB == NextMBB) {
11911         std::swap(*I, *W.LastCluster);
11912         break;
11913       }
11914     }
11915   }
11916 
11917   // Compute total probability.
11918   BranchProbability DefaultProb = W.DefaultProb;
11919   BranchProbability UnhandledProbs = DefaultProb;
11920   for (CaseClusterIt I = W.FirstCluster; I <= W.LastCluster; ++I)
11921     UnhandledProbs += I->Prob;
11922 
11923   MachineBasicBlock *CurMBB = W.MBB;
11924   for (CaseClusterIt I = W.FirstCluster, E = W.LastCluster; I <= E; ++I) {
11925     bool FallthroughUnreachable = false;
11926     MachineBasicBlock *Fallthrough;
11927     if (I == W.LastCluster) {
11928       // For the last cluster, fall through to the default destination.
11929       Fallthrough = DefaultMBB;
11930       FallthroughUnreachable = isa<UnreachableInst>(
11931           DefaultMBB->getBasicBlock()->getFirstNonPHIOrDbg());
11932     } else {
11933       Fallthrough = CurMF->CreateMachineBasicBlock(CurMBB->getBasicBlock());
11934       CurMF->insert(BBI, Fallthrough);
11935       // Put Cond in a virtual register to make it available from the new blocks.
11936       ExportFromCurrentBlock(Cond);
11937     }
11938     UnhandledProbs -= I->Prob;
11939 
11940     switch (I->Kind) {
11941       case CC_JumpTable: {
11942         // FIXME: Optimize away range check based on pivot comparisons.
11943         JumpTableHeader *JTH = &SL->JTCases[I->JTCasesIndex].first;
11944         SwitchCG::JumpTable *JT = &SL->JTCases[I->JTCasesIndex].second;
11945 
11946         // The jump block hasn't been inserted yet; insert it here.
11947         MachineBasicBlock *JumpMBB = JT->MBB;
11948         CurMF->insert(BBI, JumpMBB);
11949 
11950         auto JumpProb = I->Prob;
11951         auto FallthroughProb = UnhandledProbs;
11952 
11953         // If the default statement is a target of the jump table, we evenly
11954         // distribute the default probability to successors of CurMBB. Also
11955         // update the probability on the edge from JumpMBB to Fallthrough.
11956         for (MachineBasicBlock::succ_iterator SI = JumpMBB->succ_begin(),
11957                                               SE = JumpMBB->succ_end();
11958              SI != SE; ++SI) {
11959           if (*SI == DefaultMBB) {
11960             JumpProb += DefaultProb / 2;
11961             FallthroughProb -= DefaultProb / 2;
11962             JumpMBB->setSuccProbability(SI, DefaultProb / 2);
11963             JumpMBB->normalizeSuccProbs();
11964             break;
11965           }
11966         }
11967 
11968         // If the default clause is unreachable, propagate that knowledge into
11969         // JTH->FallthroughUnreachable which will use it to suppress the range
11970         // check.
11971         //
11972         // However, don't do this if we're doing branch target enforcement,
11973         // because a table branch _without_ a range check can be a tempting JOP
11974         // gadget - out-of-bounds inputs that are impossible in correct
11975         // execution become possible again if an attacker can influence the
11976         // control flow. So if an attacker doesn't already have a BTI bypass
11977         // available, we don't want them to be able to get one out of this
11978         // table branch.
11979         if (FallthroughUnreachable) {
11980           Function &CurFunc = CurMF->getFunction();
11981           bool HasBranchTargetEnforcement = false;
11982           if (CurFunc.hasFnAttribute("branch-target-enforcement")) {
11983             HasBranchTargetEnforcement =
11984                 CurFunc.getFnAttribute("branch-target-enforcement")
11985                     .getValueAsBool();
11986           } else {
11987             HasBranchTargetEnforcement =
11988                 CurMF->getMMI().getModule()->getModuleFlag(
11989                     "branch-target-enforcement");
11990           }
11991           if (!HasBranchTargetEnforcement)
11992             JTH->FallthroughUnreachable = true;
11993         }
11994 
11995         if (!JTH->FallthroughUnreachable)
11996           addSuccessorWithProb(CurMBB, Fallthrough, FallthroughProb);
11997         addSuccessorWithProb(CurMBB, JumpMBB, JumpProb);
11998         CurMBB->normalizeSuccProbs();
11999 
12000         // The jump table header will be inserted in our current block, do the
12001         // range check, and fall through to our fallthrough block.
12002         JTH->HeaderBB = CurMBB;
12003         JT->Default = Fallthrough; // FIXME: Move Default to JumpTableHeader.
12004 
12005         // If we're in the right place, emit the jump table header right now.
12006         if (CurMBB == SwitchMBB) {
12007           visitJumpTableHeader(*JT, *JTH, SwitchMBB);
12008           JTH->Emitted = true;
12009         }
12010         break;
12011       }
12012       case CC_BitTests: {
12013         // FIXME: Optimize away range check based on pivot comparisons.
12014         BitTestBlock *BTB = &SL->BitTestCases[I->BTCasesIndex];
12015 
12016         // The bit test blocks haven't been inserted yet; insert them here.
12017         for (BitTestCase &BTC : BTB->Cases)
12018           CurMF->insert(BBI, BTC.ThisBB);
12019 
12020         // Fill in fields of the BitTestBlock.
12021         BTB->Parent = CurMBB;
12022         BTB->Default = Fallthrough;
12023 
12024         BTB->DefaultProb = UnhandledProbs;
12025         // If the cases in bit test don't form a contiguous range, we evenly
12026         // distribute the probability on the edge to Fallthrough to two
12027         // successors of CurMBB.
12028         if (!BTB->ContiguousRange) {
12029           BTB->Prob += DefaultProb / 2;
12030           BTB->DefaultProb -= DefaultProb / 2;
12031         }
12032 
12033         if (FallthroughUnreachable)
12034           BTB->FallthroughUnreachable = true;
12035 
12036         // If we're in the right place, emit the bit test header right now.
12037         if (CurMBB == SwitchMBB) {
12038           visitBitTestHeader(*BTB, SwitchMBB);
12039           BTB->Emitted = true;
12040         }
12041         break;
12042       }
12043       case CC_Range: {
12044         const Value *RHS, *LHS, *MHS;
12045         ISD::CondCode CC;
12046         if (I->Low == I->High) {
12047           // Check Cond == I->Low.
12048           CC = ISD::SETEQ;
12049           LHS = Cond;
12050           RHS=I->Low;
12051           MHS = nullptr;
12052         } else {
12053           // Check I->Low <= Cond <= I->High.
12054           CC = ISD::SETLE;
12055           LHS = I->Low;
12056           MHS = Cond;
12057           RHS = I->High;
12058         }
12059 
12060         // If Fallthrough is unreachable, fold away the comparison.
12061         if (FallthroughUnreachable)
12062           CC = ISD::SETTRUE;
12063 
12064         // The false probability is the sum of all unhandled cases.
12065         CaseBlock CB(CC, LHS, RHS, MHS, I->MBB, Fallthrough, CurMBB,
12066                      getCurSDLoc(), I->Prob, UnhandledProbs);
12067 
12068         if (CurMBB == SwitchMBB)
12069           visitSwitchCase(CB, SwitchMBB);
12070         else
12071           SL->SwitchCases.push_back(CB);
12072 
12073         break;
12074       }
12075     }
12076     CurMBB = Fallthrough;
12077   }
12078 }
12079 
12080 void SelectionDAGBuilder::splitWorkItem(SwitchWorkList &WorkList,
12081                                         const SwitchWorkListItem &W,
12082                                         Value *Cond,
12083                                         MachineBasicBlock *SwitchMBB) {
12084   assert(W.FirstCluster->Low->getValue().slt(W.LastCluster->Low->getValue()) &&
12085          "Clusters not sorted?");
12086   assert(W.LastCluster - W.FirstCluster + 1 >= 2 && "Too small to split!");
12087 
12088   auto [LastLeft, FirstRight, LeftProb, RightProb] =
12089       SL->computeSplitWorkItemInfo(W);
12090 
12091   // Use the first element on the right as pivot since we will make less-than
12092   // comparisons against it.
12093   CaseClusterIt PivotCluster = FirstRight;
12094   assert(PivotCluster > W.FirstCluster);
12095   assert(PivotCluster <= W.LastCluster);
12096 
12097   CaseClusterIt FirstLeft = W.FirstCluster;
12098   CaseClusterIt LastRight = W.LastCluster;
12099 
12100   const ConstantInt *Pivot = PivotCluster->Low;
12101 
12102   // New blocks will be inserted immediately after the current one.
12103   MachineFunction::iterator BBI(W.MBB);
12104   ++BBI;
12105 
12106   // We will branch to the LHS if Value < Pivot. If LHS is a single cluster,
12107   // we can branch to its destination directly if it's squeezed exactly in
12108   // between the known lower bound and Pivot - 1.
12109   MachineBasicBlock *LeftMBB;
12110   if (FirstLeft == LastLeft && FirstLeft->Kind == CC_Range &&
12111       FirstLeft->Low == W.GE &&
12112       (FirstLeft->High->getValue() + 1LL) == Pivot->getValue()) {
12113     LeftMBB = FirstLeft->MBB;
12114   } else {
12115     LeftMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
12116     FuncInfo.MF->insert(BBI, LeftMBB);
12117     WorkList.push_back(
12118         {LeftMBB, FirstLeft, LastLeft, W.GE, Pivot, W.DefaultProb / 2});
12119     // Put Cond in a virtual register to make it available from the new blocks.
12120     ExportFromCurrentBlock(Cond);
12121   }
12122 
12123   // Similarly, we will branch to the RHS if Value >= Pivot. If RHS is a
12124   // single cluster, RHS.Low == Pivot, and we can branch to its destination
12125   // directly if RHS.High equals the current upper bound.
12126   MachineBasicBlock *RightMBB;
12127   if (FirstRight == LastRight && FirstRight->Kind == CC_Range &&
12128       W.LT && (FirstRight->High->getValue() + 1ULL) == W.LT->getValue()) {
12129     RightMBB = FirstRight->MBB;
12130   } else {
12131     RightMBB = FuncInfo.MF->CreateMachineBasicBlock(W.MBB->getBasicBlock());
12132     FuncInfo.MF->insert(BBI, RightMBB);
12133     WorkList.push_back(
12134         {RightMBB, FirstRight, LastRight, Pivot, W.LT, W.DefaultProb / 2});
12135     // Put Cond in a virtual register to make it available from the new blocks.
12136     ExportFromCurrentBlock(Cond);
12137   }
12138 
12139   // Create the CaseBlock record that will be used to lower the branch.
12140   CaseBlock CB(ISD::SETLT, Cond, Pivot, nullptr, LeftMBB, RightMBB, W.MBB,
12141                getCurSDLoc(), LeftProb, RightProb);
12142 
12143   if (W.MBB == SwitchMBB)
12144     visitSwitchCase(CB, SwitchMBB);
12145   else
12146     SL->SwitchCases.push_back(CB);
12147 }
12148 
12149 // Scale CaseProb after peeling a case with the probablity of PeeledCaseProb
12150 // from the swith statement.
12151 static BranchProbability scaleCaseProbality(BranchProbability CaseProb,
12152                                             BranchProbability PeeledCaseProb) {
12153   if (PeeledCaseProb == BranchProbability::getOne())
12154     return BranchProbability::getZero();
12155   BranchProbability SwitchProb = PeeledCaseProb.getCompl();
12156 
12157   uint32_t Numerator = CaseProb.getNumerator();
12158   uint32_t Denominator = SwitchProb.scale(CaseProb.getDenominator());
12159   return BranchProbability(Numerator, std::max(Numerator, Denominator));
12160 }
12161 
12162 // Try to peel the top probability case if it exceeds the threshold.
12163 // Return current MachineBasicBlock for the switch statement if the peeling
12164 // does not occur.
12165 // If the peeling is performed, return the newly created MachineBasicBlock
12166 // for the peeled switch statement. Also update Clusters to remove the peeled
12167 // case. PeeledCaseProb is the BranchProbability for the peeled case.
12168 MachineBasicBlock *SelectionDAGBuilder::peelDominantCaseCluster(
12169     const SwitchInst &SI, CaseClusterVector &Clusters,
12170     BranchProbability &PeeledCaseProb) {
12171   MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
12172   // Don't perform if there is only one cluster or optimizing for size.
12173   if (SwitchPeelThreshold > 100 || !FuncInfo.BPI || Clusters.size() < 2 ||
12174       TM.getOptLevel() == CodeGenOptLevel::None ||
12175       SwitchMBB->getParent()->getFunction().hasMinSize())
12176     return SwitchMBB;
12177 
12178   BranchProbability TopCaseProb = BranchProbability(SwitchPeelThreshold, 100);
12179   unsigned PeeledCaseIndex = 0;
12180   bool SwitchPeeled = false;
12181   for (unsigned Index = 0; Index < Clusters.size(); ++Index) {
12182     CaseCluster &CC = Clusters[Index];
12183     if (CC.Prob < TopCaseProb)
12184       continue;
12185     TopCaseProb = CC.Prob;
12186     PeeledCaseIndex = Index;
12187     SwitchPeeled = true;
12188   }
12189   if (!SwitchPeeled)
12190     return SwitchMBB;
12191 
12192   LLVM_DEBUG(dbgs() << "Peeled one top case in switch stmt, prob: "
12193                     << TopCaseProb << "\n");
12194 
12195   // Record the MBB for the peeled switch statement.
12196   MachineFunction::iterator BBI(SwitchMBB);
12197   ++BBI;
12198   MachineBasicBlock *PeeledSwitchMBB =
12199       FuncInfo.MF->CreateMachineBasicBlock(SwitchMBB->getBasicBlock());
12200   FuncInfo.MF->insert(BBI, PeeledSwitchMBB);
12201 
12202   ExportFromCurrentBlock(SI.getCondition());
12203   auto PeeledCaseIt = Clusters.begin() + PeeledCaseIndex;
12204   SwitchWorkListItem W = {SwitchMBB, PeeledCaseIt, PeeledCaseIt,
12205                           nullptr,   nullptr,      TopCaseProb.getCompl()};
12206   lowerWorkItem(W, SI.getCondition(), SwitchMBB, PeeledSwitchMBB);
12207 
12208   Clusters.erase(PeeledCaseIt);
12209   for (CaseCluster &CC : Clusters) {
12210     LLVM_DEBUG(
12211         dbgs() << "Scale the probablity for one cluster, before scaling: "
12212                << CC.Prob << "\n");
12213     CC.Prob = scaleCaseProbality(CC.Prob, TopCaseProb);
12214     LLVM_DEBUG(dbgs() << "After scaling: " << CC.Prob << "\n");
12215   }
12216   PeeledCaseProb = TopCaseProb;
12217   return PeeledSwitchMBB;
12218 }
12219 
12220 void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
12221   // Extract cases from the switch.
12222   BranchProbabilityInfo *BPI = FuncInfo.BPI;
12223   CaseClusterVector Clusters;
12224   Clusters.reserve(SI.getNumCases());
12225   for (auto I : SI.cases()) {
12226     MachineBasicBlock *Succ = FuncInfo.MBBMap[I.getCaseSuccessor()];
12227     const ConstantInt *CaseVal = I.getCaseValue();
12228     BranchProbability Prob =
12229         BPI ? BPI->getEdgeProbability(SI.getParent(), I.getSuccessorIndex())
12230             : BranchProbability(1, SI.getNumCases() + 1);
12231     Clusters.push_back(CaseCluster::range(CaseVal, CaseVal, Succ, Prob));
12232   }
12233 
12234   MachineBasicBlock *DefaultMBB = FuncInfo.MBBMap[SI.getDefaultDest()];
12235 
12236   // Cluster adjacent cases with the same destination. We do this at all
12237   // optimization levels because it's cheap to do and will make codegen faster
12238   // if there are many clusters.
12239   sortAndRangeify(Clusters);
12240 
12241   // The branch probablity of the peeled case.
12242   BranchProbability PeeledCaseProb = BranchProbability::getZero();
12243   MachineBasicBlock *PeeledSwitchMBB =
12244       peelDominantCaseCluster(SI, Clusters, PeeledCaseProb);
12245 
12246   // If there is only the default destination, jump there directly.
12247   MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
12248   if (Clusters.empty()) {
12249     assert(PeeledSwitchMBB == SwitchMBB);
12250     SwitchMBB->addSuccessor(DefaultMBB);
12251     if (DefaultMBB != NextBlock(SwitchMBB)) {
12252       DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other,
12253                               getControlRoot(), DAG.getBasicBlock(DefaultMBB)));
12254     }
12255     return;
12256   }
12257 
12258   SL->findJumpTables(Clusters, &SI, getCurSDLoc(), DefaultMBB, DAG.getPSI(),
12259                      DAG.getBFI());
12260   SL->findBitTestClusters(Clusters, &SI);
12261 
12262   LLVM_DEBUG({
12263     dbgs() << "Case clusters: ";
12264     for (const CaseCluster &C : Clusters) {
12265       if (C.Kind == CC_JumpTable)
12266         dbgs() << "JT:";
12267       if (C.Kind == CC_BitTests)
12268         dbgs() << "BT:";
12269 
12270       C.Low->getValue().print(dbgs(), true);
12271       if (C.Low != C.High) {
12272         dbgs() << '-';
12273         C.High->getValue().print(dbgs(), true);
12274       }
12275       dbgs() << ' ';
12276     }
12277     dbgs() << '\n';
12278   });
12279 
12280   assert(!Clusters.empty());
12281   SwitchWorkList WorkList;
12282   CaseClusterIt First = Clusters.begin();
12283   CaseClusterIt Last = Clusters.end() - 1;
12284   auto DefaultProb = getEdgeProbability(PeeledSwitchMBB, DefaultMBB);
12285   // Scale the branchprobability for DefaultMBB if the peel occurs and
12286   // DefaultMBB is not replaced.
12287   if (PeeledCaseProb != BranchProbability::getZero() &&
12288       DefaultMBB == FuncInfo.MBBMap[SI.getDefaultDest()])
12289     DefaultProb = scaleCaseProbality(DefaultProb, PeeledCaseProb);
12290   WorkList.push_back(
12291       {PeeledSwitchMBB, First, Last, nullptr, nullptr, DefaultProb});
12292 
12293   while (!WorkList.empty()) {
12294     SwitchWorkListItem W = WorkList.pop_back_val();
12295     unsigned NumClusters = W.LastCluster - W.FirstCluster + 1;
12296 
12297     if (NumClusters > 3 && TM.getOptLevel() != CodeGenOptLevel::None &&
12298         !DefaultMBB->getParent()->getFunction().hasMinSize()) {
12299       // For optimized builds, lower large range as a balanced binary tree.
12300       splitWorkItem(WorkList, W, SI.getCondition(), SwitchMBB);
12301       continue;
12302     }
12303 
12304     lowerWorkItem(W, SI.getCondition(), SwitchMBB, DefaultMBB);
12305   }
12306 }
12307 
12308 void SelectionDAGBuilder::visitStepVector(const CallInst &I) {
12309   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12310   auto DL = getCurSDLoc();
12311   EVT ResultVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12312   setValue(&I, DAG.getStepVector(DL, ResultVT));
12313 }
12314 
12315 void SelectionDAGBuilder::visitVectorReverse(const CallInst &I) {
12316   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12317   EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12318 
12319   SDLoc DL = getCurSDLoc();
12320   SDValue V = getValue(I.getOperand(0));
12321   assert(VT == V.getValueType() && "Malformed vector.reverse!");
12322 
12323   if (VT.isScalableVector()) {
12324     setValue(&I, DAG.getNode(ISD::VECTOR_REVERSE, DL, VT, V));
12325     return;
12326   }
12327 
12328   // Use VECTOR_SHUFFLE for the fixed-length vector
12329   // to maintain existing behavior.
12330   SmallVector<int, 8> Mask;
12331   unsigned NumElts = VT.getVectorMinNumElements();
12332   for (unsigned i = 0; i != NumElts; ++i)
12333     Mask.push_back(NumElts - 1 - i);
12334 
12335   setValue(&I, DAG.getVectorShuffle(VT, DL, V, DAG.getUNDEF(VT), Mask));
12336 }
12337 
12338 void SelectionDAGBuilder::visitVectorDeinterleave(const CallInst &I) {
12339   auto DL = getCurSDLoc();
12340   SDValue InVec = getValue(I.getOperand(0));
12341   EVT OutVT =
12342       InVec.getValueType().getHalfNumVectorElementsVT(*DAG.getContext());
12343 
12344   unsigned OutNumElts = OutVT.getVectorMinNumElements();
12345 
12346   // ISD Node needs the input vectors split into two equal parts
12347   SDValue Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, OutVT, InVec,
12348                            DAG.getVectorIdxConstant(0, DL));
12349   SDValue Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, OutVT, InVec,
12350                            DAG.getVectorIdxConstant(OutNumElts, DL));
12351 
12352   // Use VECTOR_SHUFFLE for fixed-length vectors to benefit from existing
12353   // legalisation and combines.
12354   if (OutVT.isFixedLengthVector()) {
12355     SDValue Even = DAG.getVectorShuffle(OutVT, DL, Lo, Hi,
12356                                         createStrideMask(0, 2, OutNumElts));
12357     SDValue Odd = DAG.getVectorShuffle(OutVT, DL, Lo, Hi,
12358                                        createStrideMask(1, 2, OutNumElts));
12359     SDValue Res = DAG.getMergeValues({Even, Odd}, getCurSDLoc());
12360     setValue(&I, Res);
12361     return;
12362   }
12363 
12364   SDValue Res = DAG.getNode(ISD::VECTOR_DEINTERLEAVE, DL,
12365                             DAG.getVTList(OutVT, OutVT), Lo, Hi);
12366   setValue(&I, Res);
12367 }
12368 
12369 void SelectionDAGBuilder::visitVectorInterleave(const CallInst &I) {
12370   auto DL = getCurSDLoc();
12371   EVT InVT = getValue(I.getOperand(0)).getValueType();
12372   SDValue InVec0 = getValue(I.getOperand(0));
12373   SDValue InVec1 = getValue(I.getOperand(1));
12374   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12375   EVT OutVT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12376 
12377   // Use VECTOR_SHUFFLE for fixed-length vectors to benefit from existing
12378   // legalisation and combines.
12379   if (OutVT.isFixedLengthVector()) {
12380     unsigned NumElts = InVT.getVectorMinNumElements();
12381     SDValue V = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, InVec0, InVec1);
12382     setValue(&I, DAG.getVectorShuffle(OutVT, DL, V, DAG.getUNDEF(OutVT),
12383                                       createInterleaveMask(NumElts, 2)));
12384     return;
12385   }
12386 
12387   SDValue Res = DAG.getNode(ISD::VECTOR_INTERLEAVE, DL,
12388                             DAG.getVTList(InVT, InVT), InVec0, InVec1);
12389   Res = DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, Res.getValue(0),
12390                     Res.getValue(1));
12391   setValue(&I, Res);
12392 }
12393 
12394 void SelectionDAGBuilder::visitFreeze(const FreezeInst &I) {
12395   SmallVector<EVT, 4> ValueVTs;
12396   ComputeValueVTs(DAG.getTargetLoweringInfo(), DAG.getDataLayout(), I.getType(),
12397                   ValueVTs);
12398   unsigned NumValues = ValueVTs.size();
12399   if (NumValues == 0) return;
12400 
12401   SmallVector<SDValue, 4> Values(NumValues);
12402   SDValue Op = getValue(I.getOperand(0));
12403 
12404   for (unsigned i = 0; i != NumValues; ++i)
12405     Values[i] = DAG.getNode(ISD::FREEZE, getCurSDLoc(), ValueVTs[i],
12406                             SDValue(Op.getNode(), Op.getResNo() + i));
12407 
12408   setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
12409                            DAG.getVTList(ValueVTs), Values));
12410 }
12411 
12412 void SelectionDAGBuilder::visitVectorSplice(const CallInst &I) {
12413   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12414   EVT VT = TLI.getValueType(DAG.getDataLayout(), I.getType());
12415 
12416   SDLoc DL = getCurSDLoc();
12417   SDValue V1 = getValue(I.getOperand(0));
12418   SDValue V2 = getValue(I.getOperand(1));
12419   int64_t Imm = cast<ConstantInt>(I.getOperand(2))->getSExtValue();
12420 
12421   // VECTOR_SHUFFLE doesn't support a scalable mask so use a dedicated node.
12422   if (VT.isScalableVector()) {
12423     setValue(&I, DAG.getNode(ISD::VECTOR_SPLICE, DL, VT, V1, V2,
12424                              DAG.getVectorIdxConstant(Imm, DL)));
12425     return;
12426   }
12427 
12428   unsigned NumElts = VT.getVectorNumElements();
12429 
12430   uint64_t Idx = (NumElts + Imm) % NumElts;
12431 
12432   // Use VECTOR_SHUFFLE to maintain original behaviour for fixed-length vectors.
12433   SmallVector<int, 8> Mask;
12434   for (unsigned i = 0; i < NumElts; ++i)
12435     Mask.push_back(Idx + i);
12436   setValue(&I, DAG.getVectorShuffle(VT, DL, V1, V2, Mask));
12437 }
12438 
12439 // Consider the following MIR after SelectionDAG, which produces output in
12440 // phyregs in the first case or virtregs in the second case.
12441 //
12442 // INLINEASM_BR ..., implicit-def $ebx, ..., implicit-def $edx
12443 // %5:gr32 = COPY $ebx
12444 // %6:gr32 = COPY $edx
12445 // %1:gr32 = COPY %6:gr32
12446 // %0:gr32 = COPY %5:gr32
12447 //
12448 // INLINEASM_BR ..., def %5:gr32, ..., def %6:gr32
12449 // %1:gr32 = COPY %6:gr32
12450 // %0:gr32 = COPY %5:gr32
12451 //
12452 // Given %0, we'd like to return $ebx in the first case and %5 in the second.
12453 // Given %1, we'd like to return $edx in the first case and %6 in the second.
12454 //
12455 // If a callbr has outputs, it will have a single mapping in FuncInfo.ValueMap
12456 // to a single virtreg (such as %0). The remaining outputs monotonically
12457 // increase in virtreg number from there. If a callbr has no outputs, then it
12458 // should not have a corresponding callbr landingpad; in fact, the callbr
12459 // landingpad would not even be able to refer to such a callbr.
12460 static Register FollowCopyChain(MachineRegisterInfo &MRI, Register Reg) {
12461   MachineInstr *MI = MRI.def_begin(Reg)->getParent();
12462   // There is definitely at least one copy.
12463   assert(MI->getOpcode() == TargetOpcode::COPY &&
12464          "start of copy chain MUST be COPY");
12465   Reg = MI->getOperand(1).getReg();
12466   MI = MRI.def_begin(Reg)->getParent();
12467   // There may be an optional second copy.
12468   if (MI->getOpcode() == TargetOpcode::COPY) {
12469     assert(Reg.isVirtual() && "expected COPY of virtual register");
12470     Reg = MI->getOperand(1).getReg();
12471     assert(Reg.isPhysical() && "expected COPY of physical register");
12472     MI = MRI.def_begin(Reg)->getParent();
12473   }
12474   // The start of the chain must be an INLINEASM_BR.
12475   assert(MI->getOpcode() == TargetOpcode::INLINEASM_BR &&
12476          "end of copy chain MUST be INLINEASM_BR");
12477   return Reg;
12478 }
12479 
12480 // We must do this walk rather than the simpler
12481 //   setValue(&I, getCopyFromRegs(CBR, CBR->getType()));
12482 // otherwise we will end up with copies of virtregs only valid along direct
12483 // edges.
12484 void SelectionDAGBuilder::visitCallBrLandingPad(const CallInst &I) {
12485   SmallVector<EVT, 8> ResultVTs;
12486   SmallVector<SDValue, 8> ResultValues;
12487   const auto *CBR =
12488       cast<CallBrInst>(I.getParent()->getUniquePredecessor()->getTerminator());
12489 
12490   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
12491   const TargetRegisterInfo *TRI = DAG.getSubtarget().getRegisterInfo();
12492   MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
12493 
12494   unsigned InitialDef = FuncInfo.ValueMap[CBR];
12495   SDValue Chain = DAG.getRoot();
12496 
12497   // Re-parse the asm constraints string.
12498   TargetLowering::AsmOperandInfoVector TargetConstraints =
12499       TLI.ParseConstraints(DAG.getDataLayout(), TRI, *CBR);
12500   for (auto &T : TargetConstraints) {
12501     SDISelAsmOperandInfo OpInfo(T);
12502     if (OpInfo.Type != InlineAsm::isOutput)
12503       continue;
12504 
12505     // Pencil in OpInfo.ConstraintType and OpInfo.ConstraintVT based on the
12506     // individual constraint.
12507     TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
12508 
12509     switch (OpInfo.ConstraintType) {
12510     case TargetLowering::C_Register:
12511     case TargetLowering::C_RegisterClass: {
12512       // Fill in OpInfo.AssignedRegs.Regs.
12513       getRegistersForValue(DAG, getCurSDLoc(), OpInfo, OpInfo);
12514 
12515       // getRegistersForValue may produce 1 to many registers based on whether
12516       // the OpInfo.ConstraintVT is legal on the target or not.
12517       for (size_t i = 0, e = OpInfo.AssignedRegs.Regs.size(); i != e; ++i) {
12518         Register OriginalDef = FollowCopyChain(MRI, InitialDef++);
12519         if (Register::isPhysicalRegister(OriginalDef))
12520           FuncInfo.MBB->addLiveIn(OriginalDef);
12521         // Update the assigned registers to use the original defs.
12522         OpInfo.AssignedRegs.Regs[i] = OriginalDef;
12523       }
12524 
12525       SDValue V = OpInfo.AssignedRegs.getCopyFromRegs(
12526           DAG, FuncInfo, getCurSDLoc(), Chain, nullptr, CBR);
12527       ResultValues.push_back(V);
12528       ResultVTs.push_back(OpInfo.ConstraintVT);
12529       break;
12530     }
12531     case TargetLowering::C_Other: {
12532       SDValue Flag;
12533       SDValue V = TLI.LowerAsmOutputForConstraint(Chain, Flag, getCurSDLoc(),
12534                                                   OpInfo, DAG);
12535       ++InitialDef;
12536       ResultValues.push_back(V);
12537       ResultVTs.push_back(OpInfo.ConstraintVT);
12538       break;
12539     }
12540     default:
12541       break;
12542     }
12543   }
12544   SDValue V = DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
12545                           DAG.getVTList(ResultVTs), ResultValues);
12546   setValue(&I, V);
12547 }
12548