1 //===- llvm/CodeGen/GlobalISel/IRTranslator.cpp - IRTranslator ---*- C++ -*-==//
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 /// \file
9 /// This file implements the IRTranslator class.
10 //===----------------------------------------------------------------------===//
11 
12 #include "llvm/CodeGen/GlobalISel/IRTranslator.h"
13 #include "llvm/ADT/PostOrderIterator.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/ScopeExit.h"
16 #include "llvm/ADT/SmallSet.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/Analysis/BranchProbabilityInfo.h"
19 #include "llvm/Analysis/Loads.h"
20 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
21 #include "llvm/Analysis/ValueTracking.h"
22 #include "llvm/CodeGen/Analysis.h"
23 #include "llvm/CodeGen/GlobalISel/CallLowering.h"
24 #include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
25 #include "llvm/CodeGen/GlobalISel/InlineAsmLowering.h"
26 #include "llvm/CodeGen/LowLevelType.h"
27 #include "llvm/CodeGen/MachineBasicBlock.h"
28 #include "llvm/CodeGen/MachineFrameInfo.h"
29 #include "llvm/CodeGen/MachineFunction.h"
30 #include "llvm/CodeGen/MachineInstrBuilder.h"
31 #include "llvm/CodeGen/MachineMemOperand.h"
32 #include "llvm/CodeGen/MachineModuleInfo.h"
33 #include "llvm/CodeGen/MachineOperand.h"
34 #include "llvm/CodeGen/MachineRegisterInfo.h"
35 #include "llvm/CodeGen/StackProtector.h"
36 #include "llvm/CodeGen/SwitchLoweringUtils.h"
37 #include "llvm/CodeGen/TargetFrameLowering.h"
38 #include "llvm/CodeGen/TargetInstrInfo.h"
39 #include "llvm/CodeGen/TargetLowering.h"
40 #include "llvm/CodeGen/TargetPassConfig.h"
41 #include "llvm/CodeGen/TargetRegisterInfo.h"
42 #include "llvm/CodeGen/TargetSubtargetInfo.h"
43 #include "llvm/IR/BasicBlock.h"
44 #include "llvm/IR/CFG.h"
45 #include "llvm/IR/Constant.h"
46 #include "llvm/IR/Constants.h"
47 #include "llvm/IR/DataLayout.h"
48 #include "llvm/IR/DebugInfo.h"
49 #include "llvm/IR/DerivedTypes.h"
50 #include "llvm/IR/Function.h"
51 #include "llvm/IR/GetElementPtrTypeIterator.h"
52 #include "llvm/IR/InlineAsm.h"
53 #include "llvm/IR/InstrTypes.h"
54 #include "llvm/IR/Instructions.h"
55 #include "llvm/IR/IntrinsicInst.h"
56 #include "llvm/IR/Intrinsics.h"
57 #include "llvm/IR/LLVMContext.h"
58 #include "llvm/IR/Metadata.h"
59 #include "llvm/IR/PatternMatch.h"
60 #include "llvm/IR/Type.h"
61 #include "llvm/IR/User.h"
62 #include "llvm/IR/Value.h"
63 #include "llvm/InitializePasses.h"
64 #include "llvm/MC/MCContext.h"
65 #include "llvm/Pass.h"
66 #include "llvm/Support/Casting.h"
67 #include "llvm/Support/CodeGen.h"
68 #include "llvm/Support/Debug.h"
69 #include "llvm/Support/ErrorHandling.h"
70 #include "llvm/Support/LowLevelTypeImpl.h"
71 #include "llvm/Support/MathExtras.h"
72 #include "llvm/Support/raw_ostream.h"
73 #include "llvm/Target/TargetIntrinsicInfo.h"
74 #include "llvm/Target/TargetMachine.h"
75 #include <algorithm>
76 #include <cassert>
77 #include <cstddef>
78 #include <cstdint>
79 #include <iterator>
80 #include <string>
81 #include <utility>
82 #include <vector>
83 
84 #define DEBUG_TYPE "irtranslator"
85 
86 using namespace llvm;
87 
88 static cl::opt<bool>
89     EnableCSEInIRTranslator("enable-cse-in-irtranslator",
90                             cl::desc("Should enable CSE in irtranslator"),
91                             cl::Optional, cl::init(false));
92 char IRTranslator::ID = 0;
93 
94 INITIALIZE_PASS_BEGIN(IRTranslator, DEBUG_TYPE, "IRTranslator LLVM IR -> MI",
95                 false, false)
96 INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
97 INITIALIZE_PASS_DEPENDENCY(GISelCSEAnalysisWrapperPass)
98 INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
99 INITIALIZE_PASS_DEPENDENCY(StackProtector)
100 INITIALIZE_PASS_END(IRTranslator, DEBUG_TYPE, "IRTranslator LLVM IR -> MI",
101                 false, false)
102 
103 static void reportTranslationError(MachineFunction &MF,
104                                    const TargetPassConfig &TPC,
105                                    OptimizationRemarkEmitter &ORE,
106                                    OptimizationRemarkMissed &R) {
107   MF.getProperties().set(MachineFunctionProperties::Property::FailedISel);
108 
109   // Print the function name explicitly if we don't have a debug location (which
110   // makes the diagnostic less useful) or if we're going to emit a raw error.
111   if (!R.getLocation().isValid() || TPC.isGlobalISelAbortEnabled())
112     R << (" (in function: " + MF.getName() + ")").str();
113 
114   if (TPC.isGlobalISelAbortEnabled())
115     report_fatal_error(R.getMsg());
116   else
117     ORE.emit(R);
118 }
119 
120 IRTranslator::IRTranslator(CodeGenOpt::Level optlevel)
121     : MachineFunctionPass(ID), OptLevel(optlevel) {}
122 
123 #ifndef NDEBUG
124 namespace {
125 /// Verify that every instruction created has the same DILocation as the
126 /// instruction being translated.
127 class DILocationVerifier : public GISelChangeObserver {
128   const Instruction *CurrInst = nullptr;
129 
130 public:
131   DILocationVerifier() = default;
132   ~DILocationVerifier() = default;
133 
134   const Instruction *getCurrentInst() const { return CurrInst; }
135   void setCurrentInst(const Instruction *Inst) { CurrInst = Inst; }
136 
137   void erasingInstr(MachineInstr &MI) override {}
138   void changingInstr(MachineInstr &MI) override {}
139   void changedInstr(MachineInstr &MI) override {}
140 
141   void createdInstr(MachineInstr &MI) override {
142     assert(getCurrentInst() && "Inserted instruction without a current MI");
143 
144     // Only print the check message if we're actually checking it.
145 #ifndef NDEBUG
146     LLVM_DEBUG(dbgs() << "Checking DILocation from " << *CurrInst
147                       << " was copied to " << MI);
148 #endif
149     // We allow insts in the entry block to have a debug loc line of 0 because
150     // they could have originated from constants, and we don't want a jumpy
151     // debug experience.
152     assert((CurrInst->getDebugLoc() == MI.getDebugLoc() ||
153             MI.getDebugLoc().getLine() == 0) &&
154            "Line info was not transferred to all instructions");
155   }
156 };
157 } // namespace
158 #endif // ifndef NDEBUG
159 
160 
161 void IRTranslator::getAnalysisUsage(AnalysisUsage &AU) const {
162   AU.addRequired<StackProtector>();
163   AU.addRequired<TargetPassConfig>();
164   AU.addRequired<GISelCSEAnalysisWrapperPass>();
165   if (OptLevel != CodeGenOpt::None)
166     AU.addRequired<BranchProbabilityInfoWrapperPass>();
167   getSelectionDAGFallbackAnalysisUsage(AU);
168   MachineFunctionPass::getAnalysisUsage(AU);
169 }
170 
171 IRTranslator::ValueToVRegInfo::VRegListT &
172 IRTranslator::allocateVRegs(const Value &Val) {
173   assert(!VMap.contains(Val) && "Value already allocated in VMap");
174   auto *Regs = VMap.getVRegs(Val);
175   auto *Offsets = VMap.getOffsets(Val);
176   SmallVector<LLT, 4> SplitTys;
177   computeValueLLTs(*DL, *Val.getType(), SplitTys,
178                    Offsets->empty() ? Offsets : nullptr);
179   for (unsigned i = 0; i < SplitTys.size(); ++i)
180     Regs->push_back(0);
181   return *Regs;
182 }
183 
184 ArrayRef<Register> IRTranslator::getOrCreateVRegs(const Value &Val) {
185   auto VRegsIt = VMap.findVRegs(Val);
186   if (VRegsIt != VMap.vregs_end())
187     return *VRegsIt->second;
188 
189   if (Val.getType()->isVoidTy())
190     return *VMap.getVRegs(Val);
191 
192   // Create entry for this type.
193   auto *VRegs = VMap.getVRegs(Val);
194   auto *Offsets = VMap.getOffsets(Val);
195 
196   assert(Val.getType()->isSized() &&
197          "Don't know how to create an empty vreg");
198 
199   SmallVector<LLT, 4> SplitTys;
200   computeValueLLTs(*DL, *Val.getType(), SplitTys,
201                    Offsets->empty() ? Offsets : nullptr);
202 
203   if (!isa<Constant>(Val)) {
204     for (auto Ty : SplitTys)
205       VRegs->push_back(MRI->createGenericVirtualRegister(Ty));
206     return *VRegs;
207   }
208 
209   if (Val.getType()->isAggregateType()) {
210     // UndefValue, ConstantAggregateZero
211     auto &C = cast<Constant>(Val);
212     unsigned Idx = 0;
213     while (auto Elt = C.getAggregateElement(Idx++)) {
214       auto EltRegs = getOrCreateVRegs(*Elt);
215       llvm::copy(EltRegs, std::back_inserter(*VRegs));
216     }
217   } else {
218     assert(SplitTys.size() == 1 && "unexpectedly split LLT");
219     VRegs->push_back(MRI->createGenericVirtualRegister(SplitTys[0]));
220     bool Success = translate(cast<Constant>(Val), VRegs->front());
221     if (!Success) {
222       OptimizationRemarkMissed R("gisel-irtranslator", "GISelFailure",
223                                  MF->getFunction().getSubprogram(),
224                                  &MF->getFunction().getEntryBlock());
225       R << "unable to translate constant: " << ore::NV("Type", Val.getType());
226       reportTranslationError(*MF, *TPC, *ORE, R);
227       return *VRegs;
228     }
229   }
230 
231   return *VRegs;
232 }
233 
234 int IRTranslator::getOrCreateFrameIndex(const AllocaInst &AI) {
235   auto MapEntry = FrameIndices.find(&AI);
236   if (MapEntry != FrameIndices.end())
237     return MapEntry->second;
238 
239   uint64_t ElementSize = DL->getTypeAllocSize(AI.getAllocatedType());
240   uint64_t Size =
241       ElementSize * cast<ConstantInt>(AI.getArraySize())->getZExtValue();
242 
243   // Always allocate at least one byte.
244   Size = std::max<uint64_t>(Size, 1u);
245 
246   int &FI = FrameIndices[&AI];
247   FI = MF->getFrameInfo().CreateStackObject(Size, AI.getAlign(), false, &AI);
248   return FI;
249 }
250 
251 Align IRTranslator::getMemOpAlign(const Instruction &I) {
252   if (const StoreInst *SI = dyn_cast<StoreInst>(&I))
253     return SI->getAlign();
254   if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
255     return LI->getAlign();
256   }
257   if (const AtomicCmpXchgInst *AI = dyn_cast<AtomicCmpXchgInst>(&I)) {
258     // TODO(PR27168): This instruction has no alignment attribute, but unlike
259     // the default alignment for load/store, the default here is to assume
260     // it has NATURAL alignment, not DataLayout-specified alignment.
261     const DataLayout &DL = AI->getModule()->getDataLayout();
262     return Align(DL.getTypeStoreSize(AI->getCompareOperand()->getType()));
263   }
264   if (const AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(&I)) {
265     // TODO(PR27168): This instruction has no alignment attribute, but unlike
266     // the default alignment for load/store, the default here is to assume
267     // it has NATURAL alignment, not DataLayout-specified alignment.
268     const DataLayout &DL = AI->getModule()->getDataLayout();
269     return Align(DL.getTypeStoreSize(AI->getValOperand()->getType()));
270   }
271   OptimizationRemarkMissed R("gisel-irtranslator", "", &I);
272   R << "unable to translate memop: " << ore::NV("Opcode", &I);
273   reportTranslationError(*MF, *TPC, *ORE, R);
274   return Align(1);
275 }
276 
277 MachineBasicBlock &IRTranslator::getMBB(const BasicBlock &BB) {
278   MachineBasicBlock *&MBB = BBToMBB[&BB];
279   assert(MBB && "BasicBlock was not encountered before");
280   return *MBB;
281 }
282 
283 void IRTranslator::addMachineCFGPred(CFGEdge Edge, MachineBasicBlock *NewPred) {
284   assert(NewPred && "new predecessor must be a real MachineBasicBlock");
285   MachinePreds[Edge].push_back(NewPred);
286 }
287 
288 bool IRTranslator::translateBinaryOp(unsigned Opcode, const User &U,
289                                      MachineIRBuilder &MIRBuilder) {
290   // Get or create a virtual register for each value.
291   // Unless the value is a Constant => loadimm cst?
292   // or inline constant each time?
293   // Creation of a virtual register needs to have a size.
294   Register Op0 = getOrCreateVReg(*U.getOperand(0));
295   Register Op1 = getOrCreateVReg(*U.getOperand(1));
296   Register Res = getOrCreateVReg(U);
297   uint16_t Flags = 0;
298   if (isa<Instruction>(U)) {
299     const Instruction &I = cast<Instruction>(U);
300     Flags = MachineInstr::copyFlagsFromInstruction(I);
301   }
302 
303   MIRBuilder.buildInstr(Opcode, {Res}, {Op0, Op1}, Flags);
304   return true;
305 }
306 
307 bool IRTranslator::translateUnaryOp(unsigned Opcode, const User &U,
308                                     MachineIRBuilder &MIRBuilder) {
309   Register Op0 = getOrCreateVReg(*U.getOperand(0));
310   Register Res = getOrCreateVReg(U);
311   uint16_t Flags = 0;
312   if (isa<Instruction>(U)) {
313     const Instruction &I = cast<Instruction>(U);
314     Flags = MachineInstr::copyFlagsFromInstruction(I);
315   }
316   MIRBuilder.buildInstr(Opcode, {Res}, {Op0}, Flags);
317   return true;
318 }
319 
320 bool IRTranslator::translateFNeg(const User &U, MachineIRBuilder &MIRBuilder) {
321   return translateUnaryOp(TargetOpcode::G_FNEG, U, MIRBuilder);
322 }
323 
324 bool IRTranslator::translateCompare(const User &U,
325                                     MachineIRBuilder &MIRBuilder) {
326   auto *CI = dyn_cast<CmpInst>(&U);
327   Register Op0 = getOrCreateVReg(*U.getOperand(0));
328   Register Op1 = getOrCreateVReg(*U.getOperand(1));
329   Register Res = getOrCreateVReg(U);
330   CmpInst::Predicate Pred =
331       CI ? CI->getPredicate() : static_cast<CmpInst::Predicate>(
332                                     cast<ConstantExpr>(U).getPredicate());
333   if (CmpInst::isIntPredicate(Pred))
334     MIRBuilder.buildICmp(Pred, Res, Op0, Op1);
335   else if (Pred == CmpInst::FCMP_FALSE)
336     MIRBuilder.buildCopy(
337         Res, getOrCreateVReg(*Constant::getNullValue(U.getType())));
338   else if (Pred == CmpInst::FCMP_TRUE)
339     MIRBuilder.buildCopy(
340         Res, getOrCreateVReg(*Constant::getAllOnesValue(U.getType())));
341   else {
342     assert(CI && "Instruction should be CmpInst");
343     MIRBuilder.buildFCmp(Pred, Res, Op0, Op1,
344                          MachineInstr::copyFlagsFromInstruction(*CI));
345   }
346 
347   return true;
348 }
349 
350 bool IRTranslator::translateRet(const User &U, MachineIRBuilder &MIRBuilder) {
351   const ReturnInst &RI = cast<ReturnInst>(U);
352   const Value *Ret = RI.getReturnValue();
353   if (Ret && DL->getTypeStoreSize(Ret->getType()) == 0)
354     Ret = nullptr;
355 
356   ArrayRef<Register> VRegs;
357   if (Ret)
358     VRegs = getOrCreateVRegs(*Ret);
359 
360   Register SwiftErrorVReg = 0;
361   if (CLI->supportSwiftError() && SwiftError.getFunctionArg()) {
362     SwiftErrorVReg = SwiftError.getOrCreateVRegUseAt(
363         &RI, &MIRBuilder.getMBB(), SwiftError.getFunctionArg());
364   }
365 
366   // The target may mess up with the insertion point, but
367   // this is not important as a return is the last instruction
368   // of the block anyway.
369   return CLI->lowerReturn(MIRBuilder, Ret, VRegs, SwiftErrorVReg);
370 }
371 
372 void IRTranslator::emitBranchForMergedCondition(
373     const Value *Cond, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
374     MachineBasicBlock *CurBB, MachineBasicBlock *SwitchBB,
375     BranchProbability TProb, BranchProbability FProb, bool InvertCond) {
376   // If the leaf of the tree is a comparison, merge the condition into
377   // the caseblock.
378   if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
379     CmpInst::Predicate Condition;
380     if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
381       Condition = InvertCond ? IC->getInversePredicate() : IC->getPredicate();
382     } else {
383       const FCmpInst *FC = cast<FCmpInst>(Cond);
384       Condition = InvertCond ? FC->getInversePredicate() : FC->getPredicate();
385     }
386 
387     SwitchCG::CaseBlock CB(Condition, false, BOp->getOperand(0),
388                            BOp->getOperand(1), nullptr, TBB, FBB, CurBB,
389                            CurBuilder->getDebugLoc(), TProb, FProb);
390     SL->SwitchCases.push_back(CB);
391     return;
392   }
393 
394   // Create a CaseBlock record representing this branch.
395   CmpInst::Predicate Pred = InvertCond ? CmpInst::ICMP_NE : CmpInst::ICMP_EQ;
396   SwitchCG::CaseBlock CB(
397       Pred, false, Cond, ConstantInt::getTrue(MF->getFunction().getContext()),
398       nullptr, TBB, FBB, CurBB, CurBuilder->getDebugLoc(), TProb, FProb);
399   SL->SwitchCases.push_back(CB);
400 }
401 
402 static bool isValInBlock(const Value *V, const BasicBlock *BB) {
403   if (const Instruction *I = dyn_cast<Instruction>(V))
404     return I->getParent() == BB;
405   return true;
406 }
407 
408 void IRTranslator::findMergedConditions(
409     const Value *Cond, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
410     MachineBasicBlock *CurBB, MachineBasicBlock *SwitchBB,
411     Instruction::BinaryOps Opc, BranchProbability TProb,
412     BranchProbability FProb, bool InvertCond) {
413   using namespace PatternMatch;
414   assert((Opc == Instruction::And || Opc == Instruction::Or) &&
415          "Expected Opc to be AND/OR");
416   // Skip over not part of the tree and remember to invert op and operands at
417   // next level.
418   Value *NotCond;
419   if (match(Cond, m_OneUse(m_Not(m_Value(NotCond)))) &&
420       isValInBlock(NotCond, CurBB->getBasicBlock())) {
421     findMergedConditions(NotCond, TBB, FBB, CurBB, SwitchBB, Opc, TProb, FProb,
422                          !InvertCond);
423     return;
424   }
425 
426   const Instruction *BOp = dyn_cast<Instruction>(Cond);
427   // Compute the effective opcode for Cond, taking into account whether it needs
428   // to be inverted, e.g.
429   //   and (not (or A, B)), C
430   // gets lowered as
431   //   and (and (not A, not B), C)
432   unsigned BOpc = 0;
433   if (BOp) {
434     BOpc = BOp->getOpcode();
435     if (InvertCond) {
436       if (BOpc == Instruction::And)
437         BOpc = Instruction::Or;
438       else if (BOpc == Instruction::Or)
439         BOpc = Instruction::And;
440     }
441   }
442 
443   // If this node is not part of the or/and tree, emit it as a branch.
444   if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
445       BOpc != static_cast<unsigned>(Opc) || !BOp->hasOneUse() ||
446       BOp->getParent() != CurBB->getBasicBlock() ||
447       !isValInBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
448       !isValInBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
449     emitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB, TProb, FProb,
450                                  InvertCond);
451     return;
452   }
453 
454   //  Create TmpBB after CurBB.
455   MachineFunction::iterator BBI(CurBB);
456   MachineBasicBlock *TmpBB =
457       MF->CreateMachineBasicBlock(CurBB->getBasicBlock());
458   CurBB->getParent()->insert(++BBI, TmpBB);
459 
460   if (Opc == Instruction::Or) {
461     // Codegen X | Y as:
462     // BB1:
463     //   jmp_if_X TBB
464     //   jmp TmpBB
465     // TmpBB:
466     //   jmp_if_Y TBB
467     //   jmp FBB
468     //
469 
470     // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
471     // The requirement is that
472     //   TrueProb for BB1 + (FalseProb for BB1 * TrueProb for TmpBB)
473     //     = TrueProb for original BB.
474     // Assuming the original probabilities are A and B, one choice is to set
475     // BB1's probabilities to A/2 and A/2+B, and set TmpBB's probabilities to
476     // A/(1+B) and 2B/(1+B). This choice assumes that
477     //   TrueProb for BB1 == FalseProb for BB1 * TrueProb for TmpBB.
478     // Another choice is to assume TrueProb for BB1 equals to TrueProb for
479     // TmpBB, but the math is more complicated.
480 
481     auto NewTrueProb = TProb / 2;
482     auto NewFalseProb = TProb / 2 + FProb;
483     // Emit the LHS condition.
484     findMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc,
485                          NewTrueProb, NewFalseProb, InvertCond);
486 
487     // Normalize A/2 and B to get A/(1+B) and 2B/(1+B).
488     SmallVector<BranchProbability, 2> Probs{TProb / 2, FProb};
489     BranchProbability::normalizeProbabilities(Probs.begin(), Probs.end());
490     // Emit the RHS condition into TmpBB.
491     findMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc,
492                          Probs[0], Probs[1], InvertCond);
493   } else {
494     assert(Opc == Instruction::And && "Unknown merge op!");
495     // Codegen X & Y as:
496     // BB1:
497     //   jmp_if_X TmpBB
498     //   jmp FBB
499     // TmpBB:
500     //   jmp_if_Y TBB
501     //   jmp FBB
502     //
503     //  This requires creation of TmpBB after CurBB.
504 
505     // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
506     // The requirement is that
507     //   FalseProb for BB1 + (TrueProb for BB1 * FalseProb for TmpBB)
508     //     = FalseProb for original BB.
509     // Assuming the original probabilities are A and B, one choice is to set
510     // BB1's probabilities to A+B/2 and B/2, and set TmpBB's probabilities to
511     // 2A/(1+A) and B/(1+A). This choice assumes that FalseProb for BB1 ==
512     // TrueProb for BB1 * FalseProb for TmpBB.
513 
514     auto NewTrueProb = TProb + FProb / 2;
515     auto NewFalseProb = FProb / 2;
516     // Emit the LHS condition.
517     findMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc,
518                          NewTrueProb, NewFalseProb, InvertCond);
519 
520     // Normalize A and B/2 to get 2A/(1+A) and B/(1+A).
521     SmallVector<BranchProbability, 2> Probs{TProb, FProb / 2};
522     BranchProbability::normalizeProbabilities(Probs.begin(), Probs.end());
523     // Emit the RHS condition into TmpBB.
524     findMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc,
525                          Probs[0], Probs[1], InvertCond);
526   }
527 }
528 
529 bool IRTranslator::shouldEmitAsBranches(
530     const std::vector<SwitchCG::CaseBlock> &Cases) {
531   // For multiple cases, it's better to emit as branches.
532   if (Cases.size() != 2)
533     return true;
534 
535   // If this is two comparisons of the same values or'd or and'd together, they
536   // will get folded into a single comparison, so don't emit two blocks.
537   if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
538        Cases[0].CmpRHS == Cases[1].CmpRHS) ||
539       (Cases[0].CmpRHS == Cases[1].CmpLHS &&
540        Cases[0].CmpLHS == Cases[1].CmpRHS)) {
541     return false;
542   }
543 
544   // Handle: (X != null) | (Y != null) --> (X|Y) != 0
545   // Handle: (X == null) & (Y == null) --> (X|Y) == 0
546   if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
547       Cases[0].PredInfo.Pred == Cases[1].PredInfo.Pred &&
548       isa<Constant>(Cases[0].CmpRHS) &&
549       cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
550     if (Cases[0].PredInfo.Pred == CmpInst::ICMP_EQ &&
551         Cases[0].TrueBB == Cases[1].ThisBB)
552       return false;
553     if (Cases[0].PredInfo.Pred == CmpInst::ICMP_NE &&
554         Cases[0].FalseBB == Cases[1].ThisBB)
555       return false;
556   }
557 
558   return true;
559 }
560 
561 bool IRTranslator::translateBr(const User &U, MachineIRBuilder &MIRBuilder) {
562   const BranchInst &BrInst = cast<BranchInst>(U);
563   auto &CurMBB = MIRBuilder.getMBB();
564   auto *Succ0MBB = &getMBB(*BrInst.getSuccessor(0));
565 
566   if (BrInst.isUnconditional()) {
567     // If the unconditional target is the layout successor, fallthrough.
568     if (!CurMBB.isLayoutSuccessor(Succ0MBB))
569       MIRBuilder.buildBr(*Succ0MBB);
570 
571     // Link successors.
572     for (const BasicBlock *Succ : successors(&BrInst))
573       CurMBB.addSuccessor(&getMBB(*Succ));
574     return true;
575   }
576 
577   // If this condition is one of the special cases we handle, do special stuff
578   // now.
579   const Value *CondVal = BrInst.getCondition();
580   MachineBasicBlock *Succ1MBB = &getMBB(*BrInst.getSuccessor(1));
581 
582   const auto &TLI = *MF->getSubtarget().getTargetLowering();
583 
584   // If this is a series of conditions that are or'd or and'd together, emit
585   // this as a sequence of branches instead of setcc's with and/or operations.
586   // As long as jumps are not expensive (exceptions for multi-use logic ops,
587   // unpredictable branches, and vector extracts because those jumps are likely
588   // expensive for any target), this should improve performance.
589   // For example, instead of something like:
590   //     cmp A, B
591   //     C = seteq
592   //     cmp D, E
593   //     F = setle
594   //     or C, F
595   //     jnz foo
596   // Emit:
597   //     cmp A, B
598   //     je foo
599   //     cmp D, E
600   //     jle foo
601   using namespace PatternMatch;
602   if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
603     Instruction::BinaryOps Opcode = BOp->getOpcode();
604     Value *Vec, *BOp0 = BOp->getOperand(0), *BOp1 = BOp->getOperand(1);
605     if (!TLI.isJumpExpensive() && BOp->hasOneUse() &&
606         !BrInst.hasMetadata(LLVMContext::MD_unpredictable) &&
607         (Opcode == Instruction::And || Opcode == Instruction::Or) &&
608         !(match(BOp0, m_ExtractElt(m_Value(Vec), m_Value())) &&
609           match(BOp1, m_ExtractElt(m_Specific(Vec), m_Value())))) {
610       findMergedConditions(BOp, Succ0MBB, Succ1MBB, &CurMBB, &CurMBB, Opcode,
611                            getEdgeProbability(&CurMBB, Succ0MBB),
612                            getEdgeProbability(&CurMBB, Succ1MBB),
613                            /*InvertCond=*/false);
614       assert(SL->SwitchCases[0].ThisBB == &CurMBB && "Unexpected lowering!");
615 
616       // Allow some cases to be rejected.
617       if (shouldEmitAsBranches(SL->SwitchCases)) {
618         // Emit the branch for this block.
619         emitSwitchCase(SL->SwitchCases[0], &CurMBB, *CurBuilder);
620         SL->SwitchCases.erase(SL->SwitchCases.begin());
621         return true;
622       }
623 
624       // Okay, we decided not to do this, remove any inserted MBB's and clear
625       // SwitchCases.
626       for (unsigned I = 1, E = SL->SwitchCases.size(); I != E; ++I)
627         MF->erase(SL->SwitchCases[I].ThisBB);
628 
629       SL->SwitchCases.clear();
630     }
631   }
632 
633   // Create a CaseBlock record representing this branch.
634   SwitchCG::CaseBlock CB(CmpInst::ICMP_EQ, false, CondVal,
635                          ConstantInt::getTrue(MF->getFunction().getContext()),
636                          nullptr, Succ0MBB, Succ1MBB, &CurMBB,
637                          CurBuilder->getDebugLoc());
638 
639   // Use emitSwitchCase to actually insert the fast branch sequence for this
640   // cond branch.
641   emitSwitchCase(CB, &CurMBB, *CurBuilder);
642   return true;
643 }
644 
645 void IRTranslator::addSuccessorWithProb(MachineBasicBlock *Src,
646                                         MachineBasicBlock *Dst,
647                                         BranchProbability Prob) {
648   if (!FuncInfo.BPI) {
649     Src->addSuccessorWithoutProb(Dst);
650     return;
651   }
652   if (Prob.isUnknown())
653     Prob = getEdgeProbability(Src, Dst);
654   Src->addSuccessor(Dst, Prob);
655 }
656 
657 BranchProbability
658 IRTranslator::getEdgeProbability(const MachineBasicBlock *Src,
659                                  const MachineBasicBlock *Dst) const {
660   const BasicBlock *SrcBB = Src->getBasicBlock();
661   const BasicBlock *DstBB = Dst->getBasicBlock();
662   if (!FuncInfo.BPI) {
663     // If BPI is not available, set the default probability as 1 / N, where N is
664     // the number of successors.
665     auto SuccSize = std::max<uint32_t>(succ_size(SrcBB), 1);
666     return BranchProbability(1, SuccSize);
667   }
668   return FuncInfo.BPI->getEdgeProbability(SrcBB, DstBB);
669 }
670 
671 bool IRTranslator::translateSwitch(const User &U, MachineIRBuilder &MIB) {
672   using namespace SwitchCG;
673   // Extract cases from the switch.
674   const SwitchInst &SI = cast<SwitchInst>(U);
675   BranchProbabilityInfo *BPI = FuncInfo.BPI;
676   CaseClusterVector Clusters;
677   Clusters.reserve(SI.getNumCases());
678   for (auto &I : SI.cases()) {
679     MachineBasicBlock *Succ = &getMBB(*I.getCaseSuccessor());
680     assert(Succ && "Could not find successor mbb in mapping");
681     const ConstantInt *CaseVal = I.getCaseValue();
682     BranchProbability Prob =
683         BPI ? BPI->getEdgeProbability(SI.getParent(), I.getSuccessorIndex())
684             : BranchProbability(1, SI.getNumCases() + 1);
685     Clusters.push_back(CaseCluster::range(CaseVal, CaseVal, Succ, Prob));
686   }
687 
688   MachineBasicBlock *DefaultMBB = &getMBB(*SI.getDefaultDest());
689 
690   // Cluster adjacent cases with the same destination. We do this at all
691   // optimization levels because it's cheap to do and will make codegen faster
692   // if there are many clusters.
693   sortAndRangeify(Clusters);
694 
695   MachineBasicBlock *SwitchMBB = &getMBB(*SI.getParent());
696 
697   // If there is only the default destination, jump there directly.
698   if (Clusters.empty()) {
699     SwitchMBB->addSuccessor(DefaultMBB);
700     if (DefaultMBB != SwitchMBB->getNextNode())
701       MIB.buildBr(*DefaultMBB);
702     return true;
703   }
704 
705   SL->findJumpTables(Clusters, &SI, DefaultMBB, nullptr, nullptr);
706   SL->findBitTestClusters(Clusters, &SI);
707 
708   LLVM_DEBUG({
709     dbgs() << "Case clusters: ";
710     for (const CaseCluster &C : Clusters) {
711       if (C.Kind == CC_JumpTable)
712         dbgs() << "JT:";
713       if (C.Kind == CC_BitTests)
714         dbgs() << "BT:";
715 
716       C.Low->getValue().print(dbgs(), true);
717       if (C.Low != C.High) {
718         dbgs() << '-';
719         C.High->getValue().print(dbgs(), true);
720       }
721       dbgs() << ' ';
722     }
723     dbgs() << '\n';
724   });
725 
726   assert(!Clusters.empty());
727   SwitchWorkList WorkList;
728   CaseClusterIt First = Clusters.begin();
729   CaseClusterIt Last = Clusters.end() - 1;
730   auto DefaultProb = getEdgeProbability(SwitchMBB, DefaultMBB);
731   WorkList.push_back({SwitchMBB, First, Last, nullptr, nullptr, DefaultProb});
732 
733   // FIXME: At the moment we don't do any splitting optimizations here like
734   // SelectionDAG does, so this worklist only has one entry.
735   while (!WorkList.empty()) {
736     SwitchWorkListItem W = WorkList.back();
737     WorkList.pop_back();
738     if (!lowerSwitchWorkItem(W, SI.getCondition(), SwitchMBB, DefaultMBB, MIB))
739       return false;
740   }
741   return true;
742 }
743 
744 void IRTranslator::emitJumpTable(SwitchCG::JumpTable &JT,
745                                  MachineBasicBlock *MBB) {
746   // Emit the code for the jump table
747   assert(JT.Reg != -1U && "Should lower JT Header first!");
748   MachineIRBuilder MIB(*MBB->getParent());
749   MIB.setMBB(*MBB);
750   MIB.setDebugLoc(CurBuilder->getDebugLoc());
751 
752   Type *PtrIRTy = Type::getInt8PtrTy(MF->getFunction().getContext());
753   const LLT PtrTy = getLLTForType(*PtrIRTy, *DL);
754 
755   auto Table = MIB.buildJumpTable(PtrTy, JT.JTI);
756   MIB.buildBrJT(Table.getReg(0), JT.JTI, JT.Reg);
757 }
758 
759 bool IRTranslator::emitJumpTableHeader(SwitchCG::JumpTable &JT,
760                                        SwitchCG::JumpTableHeader &JTH,
761                                        MachineBasicBlock *HeaderBB) {
762   MachineIRBuilder MIB(*HeaderBB->getParent());
763   MIB.setMBB(*HeaderBB);
764   MIB.setDebugLoc(CurBuilder->getDebugLoc());
765 
766   const Value &SValue = *JTH.SValue;
767   // Subtract the lowest switch case value from the value being switched on.
768   const LLT SwitchTy = getLLTForType(*SValue.getType(), *DL);
769   Register SwitchOpReg = getOrCreateVReg(SValue);
770   auto FirstCst = MIB.buildConstant(SwitchTy, JTH.First);
771   auto Sub = MIB.buildSub({SwitchTy}, SwitchOpReg, FirstCst);
772 
773   // This value may be smaller or larger than the target's pointer type, and
774   // therefore require extension or truncating.
775   Type *PtrIRTy = SValue.getType()->getPointerTo();
776   const LLT PtrScalarTy = LLT::scalar(DL->getTypeSizeInBits(PtrIRTy));
777   Sub = MIB.buildZExtOrTrunc(PtrScalarTy, Sub);
778 
779   JT.Reg = Sub.getReg(0);
780 
781   if (JTH.OmitRangeCheck) {
782     if (JT.MBB != HeaderBB->getNextNode())
783       MIB.buildBr(*JT.MBB);
784     return true;
785   }
786 
787   // Emit the range check for the jump table, and branch to the default block
788   // for the switch statement if the value being switched on exceeds the
789   // largest case in the switch.
790   auto Cst = getOrCreateVReg(
791       *ConstantInt::get(SValue.getType(), JTH.Last - JTH.First));
792   Cst = MIB.buildZExtOrTrunc(PtrScalarTy, Cst).getReg(0);
793   auto Cmp = MIB.buildICmp(CmpInst::ICMP_UGT, LLT::scalar(1), Sub, Cst);
794 
795   auto BrCond = MIB.buildBrCond(Cmp.getReg(0), *JT.Default);
796 
797   // Avoid emitting unnecessary branches to the next block.
798   if (JT.MBB != HeaderBB->getNextNode())
799     BrCond = MIB.buildBr(*JT.MBB);
800   return true;
801 }
802 
803 void IRTranslator::emitSwitchCase(SwitchCG::CaseBlock &CB,
804                                   MachineBasicBlock *SwitchBB,
805                                   MachineIRBuilder &MIB) {
806   Register CondLHS = getOrCreateVReg(*CB.CmpLHS);
807   Register Cond;
808   DebugLoc OldDbgLoc = MIB.getDebugLoc();
809   MIB.setDebugLoc(CB.DbgLoc);
810   MIB.setMBB(*CB.ThisBB);
811 
812   if (CB.PredInfo.NoCmp) {
813     // Branch or fall through to TrueBB.
814     addSuccessorWithProb(CB.ThisBB, CB.TrueBB, CB.TrueProb);
815     addMachineCFGPred({SwitchBB->getBasicBlock(), CB.TrueBB->getBasicBlock()},
816                       CB.ThisBB);
817     CB.ThisBB->normalizeSuccProbs();
818     if (CB.TrueBB != CB.ThisBB->getNextNode())
819       MIB.buildBr(*CB.TrueBB);
820     MIB.setDebugLoc(OldDbgLoc);
821     return;
822   }
823 
824   const LLT i1Ty = LLT::scalar(1);
825   // Build the compare.
826   if (!CB.CmpMHS) {
827     const auto *CI = dyn_cast<ConstantInt>(CB.CmpRHS);
828     // For conditional branch lowering, we might try to do something silly like
829     // emit an G_ICMP to compare an existing G_ICMP i1 result with true. If so,
830     // just re-use the existing condition vreg.
831     if (CI && CI->getZExtValue() == 1 &&
832         MRI->getType(CondLHS).getSizeInBits() == 1 &&
833         CB.PredInfo.Pred == CmpInst::ICMP_EQ) {
834       Cond = CondLHS;
835     } else {
836       Register CondRHS = getOrCreateVReg(*CB.CmpRHS);
837       if (CmpInst::isFPPredicate(CB.PredInfo.Pred))
838         Cond =
839             MIB.buildFCmp(CB.PredInfo.Pred, i1Ty, CondLHS, CondRHS).getReg(0);
840       else
841         Cond =
842             MIB.buildICmp(CB.PredInfo.Pred, i1Ty, CondLHS, CondRHS).getReg(0);
843     }
844   } else {
845     assert(CB.PredInfo.Pred == CmpInst::ICMP_SLE &&
846            "Can only handle SLE ranges");
847 
848     const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
849     const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
850 
851     Register CmpOpReg = getOrCreateVReg(*CB.CmpMHS);
852     if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
853       Register CondRHS = getOrCreateVReg(*CB.CmpRHS);
854       Cond =
855           MIB.buildICmp(CmpInst::ICMP_SLE, i1Ty, CmpOpReg, CondRHS).getReg(0);
856     } else {
857       const LLT CmpTy = MRI->getType(CmpOpReg);
858       auto Sub = MIB.buildSub({CmpTy}, CmpOpReg, CondLHS);
859       auto Diff = MIB.buildConstant(CmpTy, High - Low);
860       Cond = MIB.buildICmp(CmpInst::ICMP_ULE, i1Ty, Sub, Diff).getReg(0);
861     }
862   }
863 
864   // Update successor info
865   addSuccessorWithProb(CB.ThisBB, CB.TrueBB, CB.TrueProb);
866 
867   addMachineCFGPred({SwitchBB->getBasicBlock(), CB.TrueBB->getBasicBlock()},
868                     CB.ThisBB);
869 
870   // TrueBB and FalseBB are always different unless the incoming IR is
871   // degenerate. This only happens when running llc on weird IR.
872   if (CB.TrueBB != CB.FalseBB)
873     addSuccessorWithProb(CB.ThisBB, CB.FalseBB, CB.FalseProb);
874   CB.ThisBB->normalizeSuccProbs();
875 
876   addMachineCFGPred({SwitchBB->getBasicBlock(), CB.FalseBB->getBasicBlock()},
877                     CB.ThisBB);
878 
879   MIB.buildBrCond(Cond, *CB.TrueBB);
880   MIB.buildBr(*CB.FalseBB);
881   MIB.setDebugLoc(OldDbgLoc);
882 }
883 
884 bool IRTranslator::lowerJumpTableWorkItem(SwitchCG::SwitchWorkListItem W,
885                                           MachineBasicBlock *SwitchMBB,
886                                           MachineBasicBlock *CurMBB,
887                                           MachineBasicBlock *DefaultMBB,
888                                           MachineIRBuilder &MIB,
889                                           MachineFunction::iterator BBI,
890                                           BranchProbability UnhandledProbs,
891                                           SwitchCG::CaseClusterIt I,
892                                           MachineBasicBlock *Fallthrough,
893                                           bool FallthroughUnreachable) {
894   using namespace SwitchCG;
895   MachineFunction *CurMF = SwitchMBB->getParent();
896   // FIXME: Optimize away range check based on pivot comparisons.
897   JumpTableHeader *JTH = &SL->JTCases[I->JTCasesIndex].first;
898   SwitchCG::JumpTable *JT = &SL->JTCases[I->JTCasesIndex].second;
899   BranchProbability DefaultProb = W.DefaultProb;
900 
901   // The jump block hasn't been inserted yet; insert it here.
902   MachineBasicBlock *JumpMBB = JT->MBB;
903   CurMF->insert(BBI, JumpMBB);
904 
905   // Since the jump table block is separate from the switch block, we need
906   // to keep track of it as a machine predecessor to the default block,
907   // otherwise we lose the phi edges.
908   addMachineCFGPred({SwitchMBB->getBasicBlock(), DefaultMBB->getBasicBlock()},
909                     CurMBB);
910   addMachineCFGPred({SwitchMBB->getBasicBlock(), DefaultMBB->getBasicBlock()},
911                     JumpMBB);
912 
913   auto JumpProb = I->Prob;
914   auto FallthroughProb = UnhandledProbs;
915 
916   // If the default statement is a target of the jump table, we evenly
917   // distribute the default probability to successors of CurMBB. Also
918   // update the probability on the edge from JumpMBB to Fallthrough.
919   for (MachineBasicBlock::succ_iterator SI = JumpMBB->succ_begin(),
920                                         SE = JumpMBB->succ_end();
921        SI != SE; ++SI) {
922     if (*SI == DefaultMBB) {
923       JumpProb += DefaultProb / 2;
924       FallthroughProb -= DefaultProb / 2;
925       JumpMBB->setSuccProbability(SI, DefaultProb / 2);
926       JumpMBB->normalizeSuccProbs();
927     } else {
928       // Also record edges from the jump table block to it's successors.
929       addMachineCFGPred({SwitchMBB->getBasicBlock(), (*SI)->getBasicBlock()},
930                         JumpMBB);
931     }
932   }
933 
934   // Skip the range check if the fallthrough block is unreachable.
935   if (FallthroughUnreachable)
936     JTH->OmitRangeCheck = true;
937 
938   if (!JTH->OmitRangeCheck)
939     addSuccessorWithProb(CurMBB, Fallthrough, FallthroughProb);
940   addSuccessorWithProb(CurMBB, JumpMBB, JumpProb);
941   CurMBB->normalizeSuccProbs();
942 
943   // The jump table header will be inserted in our current block, do the
944   // range check, and fall through to our fallthrough block.
945   JTH->HeaderBB = CurMBB;
946   JT->Default = Fallthrough; // FIXME: Move Default to JumpTableHeader.
947 
948   // If we're in the right place, emit the jump table header right now.
949   if (CurMBB == SwitchMBB) {
950     if (!emitJumpTableHeader(*JT, *JTH, CurMBB))
951       return false;
952     JTH->Emitted = true;
953   }
954   return true;
955 }
956 bool IRTranslator::lowerSwitchRangeWorkItem(SwitchCG::CaseClusterIt I,
957                                             Value *Cond,
958                                             MachineBasicBlock *Fallthrough,
959                                             bool FallthroughUnreachable,
960                                             BranchProbability UnhandledProbs,
961                                             MachineBasicBlock *CurMBB,
962                                             MachineIRBuilder &MIB,
963                                             MachineBasicBlock *SwitchMBB) {
964   using namespace SwitchCG;
965   const Value *RHS, *LHS, *MHS;
966   CmpInst::Predicate Pred;
967   if (I->Low == I->High) {
968     // Check Cond == I->Low.
969     Pred = CmpInst::ICMP_EQ;
970     LHS = Cond;
971     RHS = I->Low;
972     MHS = nullptr;
973   } else {
974     // Check I->Low <= Cond <= I->High.
975     Pred = CmpInst::ICMP_SLE;
976     LHS = I->Low;
977     MHS = Cond;
978     RHS = I->High;
979   }
980 
981   // If Fallthrough is unreachable, fold away the comparison.
982   // The false probability is the sum of all unhandled cases.
983   CaseBlock CB(Pred, FallthroughUnreachable, LHS, RHS, MHS, I->MBB, Fallthrough,
984                CurMBB, MIB.getDebugLoc(), I->Prob, UnhandledProbs);
985 
986   emitSwitchCase(CB, SwitchMBB, MIB);
987   return true;
988 }
989 
990 void IRTranslator::emitBitTestHeader(SwitchCG::BitTestBlock &B,
991                                      MachineBasicBlock *SwitchBB) {
992   MachineIRBuilder &MIB = *CurBuilder;
993   MIB.setMBB(*SwitchBB);
994 
995   // Subtract the minimum value.
996   Register SwitchOpReg = getOrCreateVReg(*B.SValue);
997 
998   LLT SwitchOpTy = MRI->getType(SwitchOpReg);
999   Register MinValReg = MIB.buildConstant(SwitchOpTy, B.First).getReg(0);
1000   auto RangeSub = MIB.buildSub(SwitchOpTy, SwitchOpReg, MinValReg);
1001 
1002   // Ensure that the type will fit the mask value.
1003   LLT MaskTy = SwitchOpTy;
1004   for (unsigned I = 0, E = B.Cases.size(); I != E; ++I) {
1005     if (!isUIntN(SwitchOpTy.getSizeInBits(), B.Cases[I].Mask)) {
1006       // Switch table case range are encoded into series of masks.
1007       // Just use pointer type, it's guaranteed to fit.
1008       MaskTy = LLT::scalar(64);
1009       break;
1010     }
1011   }
1012   Register SubReg = RangeSub.getReg(0);
1013   if (SwitchOpTy != MaskTy)
1014     SubReg = MIB.buildZExtOrTrunc(MaskTy, SubReg).getReg(0);
1015 
1016   B.RegVT = getMVTForLLT(MaskTy);
1017   B.Reg = SubReg;
1018 
1019   MachineBasicBlock *MBB = B.Cases[0].ThisBB;
1020 
1021   if (!B.OmitRangeCheck)
1022     addSuccessorWithProb(SwitchBB, B.Default, B.DefaultProb);
1023   addSuccessorWithProb(SwitchBB, MBB, B.Prob);
1024 
1025   SwitchBB->normalizeSuccProbs();
1026 
1027   if (!B.OmitRangeCheck) {
1028     // Conditional branch to the default block.
1029     auto RangeCst = MIB.buildConstant(SwitchOpTy, B.Range);
1030     auto RangeCmp = MIB.buildICmp(CmpInst::Predicate::ICMP_UGT, LLT::scalar(1),
1031                                   RangeSub, RangeCst);
1032     MIB.buildBrCond(RangeCmp, *B.Default);
1033   }
1034 
1035   // Avoid emitting unnecessary branches to the next block.
1036   if (MBB != SwitchBB->getNextNode())
1037     MIB.buildBr(*MBB);
1038 }
1039 
1040 void IRTranslator::emitBitTestCase(SwitchCG::BitTestBlock &BB,
1041                                    MachineBasicBlock *NextMBB,
1042                                    BranchProbability BranchProbToNext,
1043                                    Register Reg, SwitchCG::BitTestCase &B,
1044                                    MachineBasicBlock *SwitchBB) {
1045   MachineIRBuilder &MIB = *CurBuilder;
1046   MIB.setMBB(*SwitchBB);
1047 
1048   LLT SwitchTy = getLLTForMVT(BB.RegVT);
1049   Register Cmp;
1050   unsigned PopCount = countPopulation(B.Mask);
1051   if (PopCount == 1) {
1052     // Testing for a single bit; just compare the shift count with what it
1053     // would need to be to shift a 1 bit in that position.
1054     auto MaskTrailingZeros =
1055         MIB.buildConstant(SwitchTy, countTrailingZeros(B.Mask));
1056     Cmp =
1057         MIB.buildICmp(ICmpInst::ICMP_EQ, LLT::scalar(1), Reg, MaskTrailingZeros)
1058             .getReg(0);
1059   } else if (PopCount == BB.Range) {
1060     // There is only one zero bit in the range, test for it directly.
1061     auto MaskTrailingOnes =
1062         MIB.buildConstant(SwitchTy, countTrailingOnes(B.Mask));
1063     Cmp = MIB.buildICmp(CmpInst::ICMP_NE, LLT::scalar(1), Reg, MaskTrailingOnes)
1064               .getReg(0);
1065   } else {
1066     // Make desired shift.
1067     auto CstOne = MIB.buildConstant(SwitchTy, 1);
1068     auto SwitchVal = MIB.buildShl(SwitchTy, CstOne, Reg);
1069 
1070     // Emit bit tests and jumps.
1071     auto CstMask = MIB.buildConstant(SwitchTy, B.Mask);
1072     auto AndOp = MIB.buildAnd(SwitchTy, SwitchVal, CstMask);
1073     auto CstZero = MIB.buildConstant(SwitchTy, 0);
1074     Cmp = MIB.buildICmp(CmpInst::ICMP_NE, LLT::scalar(1), AndOp, CstZero)
1075               .getReg(0);
1076   }
1077 
1078   // The branch probability from SwitchBB to B.TargetBB is B.ExtraProb.
1079   addSuccessorWithProb(SwitchBB, B.TargetBB, B.ExtraProb);
1080   // The branch probability from SwitchBB to NextMBB is BranchProbToNext.
1081   addSuccessorWithProb(SwitchBB, NextMBB, BranchProbToNext);
1082   // It is not guaranteed that the sum of B.ExtraProb and BranchProbToNext is
1083   // one as they are relative probabilities (and thus work more like weights),
1084   // and hence we need to normalize them to let the sum of them become one.
1085   SwitchBB->normalizeSuccProbs();
1086 
1087   // Record the fact that the IR edge from the header to the bit test target
1088   // will go through our new block. Neeeded for PHIs to have nodes added.
1089   addMachineCFGPred({BB.Parent->getBasicBlock(), B.TargetBB->getBasicBlock()},
1090                     SwitchBB);
1091 
1092   MIB.buildBrCond(Cmp, *B.TargetBB);
1093 
1094   // Avoid emitting unnecessary branches to the next block.
1095   if (NextMBB != SwitchBB->getNextNode())
1096     MIB.buildBr(*NextMBB);
1097 }
1098 
1099 bool IRTranslator::lowerBitTestWorkItem(
1100     SwitchCG::SwitchWorkListItem W, MachineBasicBlock *SwitchMBB,
1101     MachineBasicBlock *CurMBB, MachineBasicBlock *DefaultMBB,
1102     MachineIRBuilder &MIB, MachineFunction::iterator BBI,
1103     BranchProbability DefaultProb, BranchProbability UnhandledProbs,
1104     SwitchCG::CaseClusterIt I, MachineBasicBlock *Fallthrough,
1105     bool FallthroughUnreachable) {
1106   using namespace SwitchCG;
1107   MachineFunction *CurMF = SwitchMBB->getParent();
1108   // FIXME: Optimize away range check based on pivot comparisons.
1109   BitTestBlock *BTB = &SL->BitTestCases[I->BTCasesIndex];
1110   // The bit test blocks haven't been inserted yet; insert them here.
1111   for (BitTestCase &BTC : BTB->Cases)
1112     CurMF->insert(BBI, BTC.ThisBB);
1113 
1114   // Fill in fields of the BitTestBlock.
1115   BTB->Parent = CurMBB;
1116   BTB->Default = Fallthrough;
1117 
1118   BTB->DefaultProb = UnhandledProbs;
1119   // If the cases in bit test don't form a contiguous range, we evenly
1120   // distribute the probability on the edge to Fallthrough to two
1121   // successors of CurMBB.
1122   if (!BTB->ContiguousRange) {
1123     BTB->Prob += DefaultProb / 2;
1124     BTB->DefaultProb -= DefaultProb / 2;
1125   }
1126 
1127   if (FallthroughUnreachable) {
1128     // Skip the range check if the fallthrough block is unreachable.
1129     BTB->OmitRangeCheck = true;
1130   }
1131 
1132   // If we're in the right place, emit the bit test header right now.
1133   if (CurMBB == SwitchMBB) {
1134     emitBitTestHeader(*BTB, SwitchMBB);
1135     BTB->Emitted = true;
1136   }
1137   return true;
1138 }
1139 
1140 bool IRTranslator::lowerSwitchWorkItem(SwitchCG::SwitchWorkListItem W,
1141                                        Value *Cond,
1142                                        MachineBasicBlock *SwitchMBB,
1143                                        MachineBasicBlock *DefaultMBB,
1144                                        MachineIRBuilder &MIB) {
1145   using namespace SwitchCG;
1146   MachineFunction *CurMF = FuncInfo.MF;
1147   MachineBasicBlock *NextMBB = nullptr;
1148   MachineFunction::iterator BBI(W.MBB);
1149   if (++BBI != FuncInfo.MF->end())
1150     NextMBB = &*BBI;
1151 
1152   if (EnableOpts) {
1153     // Here, we order cases by probability so the most likely case will be
1154     // checked first. However, two clusters can have the same probability in
1155     // which case their relative ordering is non-deterministic. So we use Low
1156     // as a tie-breaker as clusters are guaranteed to never overlap.
1157     llvm::sort(W.FirstCluster, W.LastCluster + 1,
1158                [](const CaseCluster &a, const CaseCluster &b) {
1159                  return a.Prob != b.Prob
1160                             ? a.Prob > b.Prob
1161                             : a.Low->getValue().slt(b.Low->getValue());
1162                });
1163 
1164     // Rearrange the case blocks so that the last one falls through if possible
1165     // without changing the order of probabilities.
1166     for (CaseClusterIt I = W.LastCluster; I > W.FirstCluster;) {
1167       --I;
1168       if (I->Prob > W.LastCluster->Prob)
1169         break;
1170       if (I->Kind == CC_Range && I->MBB == NextMBB) {
1171         std::swap(*I, *W.LastCluster);
1172         break;
1173       }
1174     }
1175   }
1176 
1177   // Compute total probability.
1178   BranchProbability DefaultProb = W.DefaultProb;
1179   BranchProbability UnhandledProbs = DefaultProb;
1180   for (CaseClusterIt I = W.FirstCluster; I <= W.LastCluster; ++I)
1181     UnhandledProbs += I->Prob;
1182 
1183   MachineBasicBlock *CurMBB = W.MBB;
1184   for (CaseClusterIt I = W.FirstCluster, E = W.LastCluster; I <= E; ++I) {
1185     bool FallthroughUnreachable = false;
1186     MachineBasicBlock *Fallthrough;
1187     if (I == W.LastCluster) {
1188       // For the last cluster, fall through to the default destination.
1189       Fallthrough = DefaultMBB;
1190       FallthroughUnreachable = isa<UnreachableInst>(
1191           DefaultMBB->getBasicBlock()->getFirstNonPHIOrDbg());
1192     } else {
1193       Fallthrough = CurMF->CreateMachineBasicBlock(CurMBB->getBasicBlock());
1194       CurMF->insert(BBI, Fallthrough);
1195     }
1196     UnhandledProbs -= I->Prob;
1197 
1198     switch (I->Kind) {
1199     case CC_BitTests: {
1200       if (!lowerBitTestWorkItem(W, SwitchMBB, CurMBB, DefaultMBB, MIB, BBI,
1201                                 DefaultProb, UnhandledProbs, I, Fallthrough,
1202                                 FallthroughUnreachable)) {
1203         LLVM_DEBUG(dbgs() << "Failed to lower bit test for switch");
1204         return false;
1205       }
1206       break;
1207     }
1208 
1209     case CC_JumpTable: {
1210       if (!lowerJumpTableWorkItem(W, SwitchMBB, CurMBB, DefaultMBB, MIB, BBI,
1211                                   UnhandledProbs, I, Fallthrough,
1212                                   FallthroughUnreachable)) {
1213         LLVM_DEBUG(dbgs() << "Failed to lower jump table");
1214         return false;
1215       }
1216       break;
1217     }
1218     case CC_Range: {
1219       if (!lowerSwitchRangeWorkItem(I, Cond, Fallthrough,
1220                                     FallthroughUnreachable, UnhandledProbs,
1221                                     CurMBB, MIB, SwitchMBB)) {
1222         LLVM_DEBUG(dbgs() << "Failed to lower switch range");
1223         return false;
1224       }
1225       break;
1226     }
1227     }
1228     CurMBB = Fallthrough;
1229   }
1230 
1231   return true;
1232 }
1233 
1234 bool IRTranslator::translateIndirectBr(const User &U,
1235                                        MachineIRBuilder &MIRBuilder) {
1236   const IndirectBrInst &BrInst = cast<IndirectBrInst>(U);
1237 
1238   const Register Tgt = getOrCreateVReg(*BrInst.getAddress());
1239   MIRBuilder.buildBrIndirect(Tgt);
1240 
1241   // Link successors.
1242   SmallPtrSet<const BasicBlock *, 32> AddedSuccessors;
1243   MachineBasicBlock &CurBB = MIRBuilder.getMBB();
1244   for (const BasicBlock *Succ : successors(&BrInst)) {
1245     // It's legal for indirectbr instructions to have duplicate blocks in the
1246     // destination list. We don't allow this in MIR. Skip anything that's
1247     // already a successor.
1248     if (!AddedSuccessors.insert(Succ).second)
1249       continue;
1250     CurBB.addSuccessor(&getMBB(*Succ));
1251   }
1252 
1253   return true;
1254 }
1255 
1256 static bool isSwiftError(const Value *V) {
1257   if (auto Arg = dyn_cast<Argument>(V))
1258     return Arg->hasSwiftErrorAttr();
1259   if (auto AI = dyn_cast<AllocaInst>(V))
1260     return AI->isSwiftError();
1261   return false;
1262 }
1263 
1264 bool IRTranslator::translateLoad(const User &U, MachineIRBuilder &MIRBuilder) {
1265   const LoadInst &LI = cast<LoadInst>(U);
1266   if (DL->getTypeStoreSize(LI.getType()) == 0)
1267     return true;
1268 
1269   ArrayRef<Register> Regs = getOrCreateVRegs(LI);
1270   ArrayRef<uint64_t> Offsets = *VMap.getOffsets(LI);
1271   Register Base = getOrCreateVReg(*LI.getPointerOperand());
1272 
1273   Type *OffsetIRTy = DL->getIntPtrType(LI.getPointerOperandType());
1274   LLT OffsetTy = getLLTForType(*OffsetIRTy, *DL);
1275 
1276   if (CLI->supportSwiftError() && isSwiftError(LI.getPointerOperand())) {
1277     assert(Regs.size() == 1 && "swifterror should be single pointer");
1278     Register VReg = SwiftError.getOrCreateVRegUseAt(&LI, &MIRBuilder.getMBB(),
1279                                                     LI.getPointerOperand());
1280     MIRBuilder.buildCopy(Regs[0], VReg);
1281     return true;
1282   }
1283 
1284   auto &TLI = *MF->getSubtarget().getTargetLowering();
1285   MachineMemOperand::Flags Flags = TLI.getLoadMemOperandFlags(LI, *DL);
1286 
1287   const MDNode *Ranges =
1288       Regs.size() == 1 ? LI.getMetadata(LLVMContext::MD_range) : nullptr;
1289   for (unsigned i = 0; i < Regs.size(); ++i) {
1290     Register Addr;
1291     MIRBuilder.materializePtrAdd(Addr, Base, OffsetTy, Offsets[i] / 8);
1292 
1293     MachinePointerInfo Ptr(LI.getPointerOperand(), Offsets[i] / 8);
1294     Align BaseAlign = getMemOpAlign(LI);
1295     AAMDNodes AAMetadata;
1296     LI.getAAMetadata(AAMetadata);
1297     auto MMO = MF->getMachineMemOperand(
1298         Ptr, Flags, MRI->getType(Regs[i]).getSizeInBytes(),
1299         commonAlignment(BaseAlign, Offsets[i] / 8), AAMetadata, Ranges,
1300         LI.getSyncScopeID(), LI.getOrdering());
1301     MIRBuilder.buildLoad(Regs[i], Addr, *MMO);
1302   }
1303 
1304   return true;
1305 }
1306 
1307 bool IRTranslator::translateStore(const User &U, MachineIRBuilder &MIRBuilder) {
1308   const StoreInst &SI = cast<StoreInst>(U);
1309   if (DL->getTypeStoreSize(SI.getValueOperand()->getType()) == 0)
1310     return true;
1311 
1312   ArrayRef<Register> Vals = getOrCreateVRegs(*SI.getValueOperand());
1313   ArrayRef<uint64_t> Offsets = *VMap.getOffsets(*SI.getValueOperand());
1314   Register Base = getOrCreateVReg(*SI.getPointerOperand());
1315 
1316   Type *OffsetIRTy = DL->getIntPtrType(SI.getPointerOperandType());
1317   LLT OffsetTy = getLLTForType(*OffsetIRTy, *DL);
1318 
1319   if (CLI->supportSwiftError() && isSwiftError(SI.getPointerOperand())) {
1320     assert(Vals.size() == 1 && "swifterror should be single pointer");
1321 
1322     Register VReg = SwiftError.getOrCreateVRegDefAt(&SI, &MIRBuilder.getMBB(),
1323                                                     SI.getPointerOperand());
1324     MIRBuilder.buildCopy(VReg, Vals[0]);
1325     return true;
1326   }
1327 
1328   auto &TLI = *MF->getSubtarget().getTargetLowering();
1329   MachineMemOperand::Flags Flags = TLI.getStoreMemOperandFlags(SI, *DL);
1330 
1331   for (unsigned i = 0; i < Vals.size(); ++i) {
1332     Register Addr;
1333     MIRBuilder.materializePtrAdd(Addr, Base, OffsetTy, Offsets[i] / 8);
1334 
1335     MachinePointerInfo Ptr(SI.getPointerOperand(), Offsets[i] / 8);
1336     Align BaseAlign = getMemOpAlign(SI);
1337     AAMDNodes AAMetadata;
1338     SI.getAAMetadata(AAMetadata);
1339     auto MMO = MF->getMachineMemOperand(
1340         Ptr, Flags, MRI->getType(Vals[i]).getSizeInBytes(),
1341         commonAlignment(BaseAlign, Offsets[i] / 8), AAMetadata, nullptr,
1342         SI.getSyncScopeID(), SI.getOrdering());
1343     MIRBuilder.buildStore(Vals[i], Addr, *MMO);
1344   }
1345   return true;
1346 }
1347 
1348 static uint64_t getOffsetFromIndices(const User &U, const DataLayout &DL) {
1349   const Value *Src = U.getOperand(0);
1350   Type *Int32Ty = Type::getInt32Ty(U.getContext());
1351 
1352   // getIndexedOffsetInType is designed for GEPs, so the first index is the
1353   // usual array element rather than looking into the actual aggregate.
1354   SmallVector<Value *, 1> Indices;
1355   Indices.push_back(ConstantInt::get(Int32Ty, 0));
1356 
1357   if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&U)) {
1358     for (auto Idx : EVI->indices())
1359       Indices.push_back(ConstantInt::get(Int32Ty, Idx));
1360   } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&U)) {
1361     for (auto Idx : IVI->indices())
1362       Indices.push_back(ConstantInt::get(Int32Ty, Idx));
1363   } else {
1364     for (unsigned i = 1; i < U.getNumOperands(); ++i)
1365       Indices.push_back(U.getOperand(i));
1366   }
1367 
1368   return 8 * static_cast<uint64_t>(
1369                  DL.getIndexedOffsetInType(Src->getType(), Indices));
1370 }
1371 
1372 bool IRTranslator::translateExtractValue(const User &U,
1373                                          MachineIRBuilder &MIRBuilder) {
1374   const Value *Src = U.getOperand(0);
1375   uint64_t Offset = getOffsetFromIndices(U, *DL);
1376   ArrayRef<Register> SrcRegs = getOrCreateVRegs(*Src);
1377   ArrayRef<uint64_t> Offsets = *VMap.getOffsets(*Src);
1378   unsigned Idx = llvm::lower_bound(Offsets, Offset) - Offsets.begin();
1379   auto &DstRegs = allocateVRegs(U);
1380 
1381   for (unsigned i = 0; i < DstRegs.size(); ++i)
1382     DstRegs[i] = SrcRegs[Idx++];
1383 
1384   return true;
1385 }
1386 
1387 bool IRTranslator::translateInsertValue(const User &U,
1388                                         MachineIRBuilder &MIRBuilder) {
1389   const Value *Src = U.getOperand(0);
1390   uint64_t Offset = getOffsetFromIndices(U, *DL);
1391   auto &DstRegs = allocateVRegs(U);
1392   ArrayRef<uint64_t> DstOffsets = *VMap.getOffsets(U);
1393   ArrayRef<Register> SrcRegs = getOrCreateVRegs(*Src);
1394   ArrayRef<Register> InsertedRegs = getOrCreateVRegs(*U.getOperand(1));
1395   auto InsertedIt = InsertedRegs.begin();
1396 
1397   for (unsigned i = 0; i < DstRegs.size(); ++i) {
1398     if (DstOffsets[i] >= Offset && InsertedIt != InsertedRegs.end())
1399       DstRegs[i] = *InsertedIt++;
1400     else
1401       DstRegs[i] = SrcRegs[i];
1402   }
1403 
1404   return true;
1405 }
1406 
1407 bool IRTranslator::translateSelect(const User &U,
1408                                    MachineIRBuilder &MIRBuilder) {
1409   Register Tst = getOrCreateVReg(*U.getOperand(0));
1410   ArrayRef<Register> ResRegs = getOrCreateVRegs(U);
1411   ArrayRef<Register> Op0Regs = getOrCreateVRegs(*U.getOperand(1));
1412   ArrayRef<Register> Op1Regs = getOrCreateVRegs(*U.getOperand(2));
1413 
1414   uint16_t Flags = 0;
1415   if (const SelectInst *SI = dyn_cast<SelectInst>(&U))
1416     Flags = MachineInstr::copyFlagsFromInstruction(*SI);
1417 
1418   for (unsigned i = 0; i < ResRegs.size(); ++i) {
1419     MIRBuilder.buildSelect(ResRegs[i], Tst, Op0Regs[i], Op1Regs[i], Flags);
1420   }
1421 
1422   return true;
1423 }
1424 
1425 bool IRTranslator::translateCopy(const User &U, const Value &V,
1426                                  MachineIRBuilder &MIRBuilder) {
1427   Register Src = getOrCreateVReg(V);
1428   auto &Regs = *VMap.getVRegs(U);
1429   if (Regs.empty()) {
1430     Regs.push_back(Src);
1431     VMap.getOffsets(U)->push_back(0);
1432   } else {
1433     // If we already assigned a vreg for this instruction, we can't change that.
1434     // Emit a copy to satisfy the users we already emitted.
1435     MIRBuilder.buildCopy(Regs[0], Src);
1436   }
1437   return true;
1438 }
1439 
1440 bool IRTranslator::translateBitCast(const User &U,
1441                                     MachineIRBuilder &MIRBuilder) {
1442   // If we're bitcasting to the source type, we can reuse the source vreg.
1443   if (getLLTForType(*U.getOperand(0)->getType(), *DL) ==
1444       getLLTForType(*U.getType(), *DL))
1445     return translateCopy(U, *U.getOperand(0), MIRBuilder);
1446 
1447   return translateCast(TargetOpcode::G_BITCAST, U, MIRBuilder);
1448 }
1449 
1450 bool IRTranslator::translateCast(unsigned Opcode, const User &U,
1451                                  MachineIRBuilder &MIRBuilder) {
1452   Register Op = getOrCreateVReg(*U.getOperand(0));
1453   Register Res = getOrCreateVReg(U);
1454   MIRBuilder.buildInstr(Opcode, {Res}, {Op});
1455   return true;
1456 }
1457 
1458 bool IRTranslator::translateGetElementPtr(const User &U,
1459                                           MachineIRBuilder &MIRBuilder) {
1460   Value &Op0 = *U.getOperand(0);
1461   Register BaseReg = getOrCreateVReg(Op0);
1462   Type *PtrIRTy = Op0.getType();
1463   LLT PtrTy = getLLTForType(*PtrIRTy, *DL);
1464   Type *OffsetIRTy = DL->getIntPtrType(PtrIRTy);
1465   LLT OffsetTy = getLLTForType(*OffsetIRTy, *DL);
1466 
1467   // Normalize Vector GEP - all scalar operands should be converted to the
1468   // splat vector.
1469   unsigned VectorWidth = 0;
1470   if (auto *VT = dyn_cast<VectorType>(U.getType()))
1471     VectorWidth = cast<FixedVectorType>(VT)->getNumElements();
1472 
1473   // We might need to splat the base pointer into a vector if the offsets
1474   // are vectors.
1475   if (VectorWidth && !PtrTy.isVector()) {
1476     BaseReg =
1477         MIRBuilder.buildSplatVector(LLT::vector(VectorWidth, PtrTy), BaseReg)
1478             .getReg(0);
1479     PtrIRTy = FixedVectorType::get(PtrIRTy, VectorWidth);
1480     PtrTy = getLLTForType(*PtrIRTy, *DL);
1481     OffsetIRTy = DL->getIntPtrType(PtrIRTy);
1482     OffsetTy = getLLTForType(*OffsetIRTy, *DL);
1483   }
1484 
1485   int64_t Offset = 0;
1486   for (gep_type_iterator GTI = gep_type_begin(&U), E = gep_type_end(&U);
1487        GTI != E; ++GTI) {
1488     const Value *Idx = GTI.getOperand();
1489     if (StructType *StTy = GTI.getStructTypeOrNull()) {
1490       unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue();
1491       Offset += DL->getStructLayout(StTy)->getElementOffset(Field);
1492       continue;
1493     } else {
1494       uint64_t ElementSize = DL->getTypeAllocSize(GTI.getIndexedType());
1495 
1496       // If this is a scalar constant or a splat vector of constants,
1497       // handle it quickly.
1498       if (const auto *CI = dyn_cast<ConstantInt>(Idx)) {
1499         Offset += ElementSize * CI->getSExtValue();
1500         continue;
1501       }
1502 
1503       if (Offset != 0) {
1504         auto OffsetMIB = MIRBuilder.buildConstant({OffsetTy}, Offset);
1505         BaseReg = MIRBuilder.buildPtrAdd(PtrTy, BaseReg, OffsetMIB.getReg(0))
1506                       .getReg(0);
1507         Offset = 0;
1508       }
1509 
1510       Register IdxReg = getOrCreateVReg(*Idx);
1511       LLT IdxTy = MRI->getType(IdxReg);
1512       if (IdxTy != OffsetTy) {
1513         if (!IdxTy.isVector() && VectorWidth) {
1514           IdxReg = MIRBuilder.buildSplatVector(
1515             OffsetTy.changeElementType(IdxTy), IdxReg).getReg(0);
1516         }
1517 
1518         IdxReg = MIRBuilder.buildSExtOrTrunc(OffsetTy, IdxReg).getReg(0);
1519       }
1520 
1521       // N = N + Idx * ElementSize;
1522       // Avoid doing it for ElementSize of 1.
1523       Register GepOffsetReg;
1524       if (ElementSize != 1) {
1525         auto ElementSizeMIB = MIRBuilder.buildConstant(
1526             getLLTForType(*OffsetIRTy, *DL), ElementSize);
1527         GepOffsetReg =
1528             MIRBuilder.buildMul(OffsetTy, IdxReg, ElementSizeMIB).getReg(0);
1529       } else
1530         GepOffsetReg = IdxReg;
1531 
1532       BaseReg = MIRBuilder.buildPtrAdd(PtrTy, BaseReg, GepOffsetReg).getReg(0);
1533     }
1534   }
1535 
1536   if (Offset != 0) {
1537     auto OffsetMIB =
1538         MIRBuilder.buildConstant(OffsetTy, Offset);
1539     MIRBuilder.buildPtrAdd(getOrCreateVReg(U), BaseReg, OffsetMIB.getReg(0));
1540     return true;
1541   }
1542 
1543   MIRBuilder.buildCopy(getOrCreateVReg(U), BaseReg);
1544   return true;
1545 }
1546 
1547 bool IRTranslator::translateMemFunc(const CallInst &CI,
1548                                     MachineIRBuilder &MIRBuilder,
1549                                     unsigned Opcode) {
1550 
1551   // If the source is undef, then just emit a nop.
1552   if (isa<UndefValue>(CI.getArgOperand(1)))
1553     return true;
1554 
1555   SmallVector<Register, 3> SrcRegs;
1556 
1557   unsigned MinPtrSize = UINT_MAX;
1558   for (auto AI = CI.arg_begin(), AE = CI.arg_end(); std::next(AI) != AE; ++AI) {
1559     Register SrcReg = getOrCreateVReg(**AI);
1560     LLT SrcTy = MRI->getType(SrcReg);
1561     if (SrcTy.isPointer())
1562       MinPtrSize = std::min(SrcTy.getSizeInBits(), MinPtrSize);
1563     SrcRegs.push_back(SrcReg);
1564   }
1565 
1566   LLT SizeTy = LLT::scalar(MinPtrSize);
1567 
1568   // The size operand should be the minimum of the pointer sizes.
1569   Register &SizeOpReg = SrcRegs[SrcRegs.size() - 1];
1570   if (MRI->getType(SizeOpReg) != SizeTy)
1571     SizeOpReg = MIRBuilder.buildZExtOrTrunc(SizeTy, SizeOpReg).getReg(0);
1572 
1573   auto ICall = MIRBuilder.buildInstr(Opcode);
1574   for (Register SrcReg : SrcRegs)
1575     ICall.addUse(SrcReg);
1576 
1577   Align DstAlign;
1578   Align SrcAlign;
1579   unsigned IsVol =
1580       cast<ConstantInt>(CI.getArgOperand(CI.getNumArgOperands() - 1))
1581           ->getZExtValue();
1582 
1583   if (auto *MCI = dyn_cast<MemCpyInst>(&CI)) {
1584     DstAlign = MCI->getDestAlign().valueOrOne();
1585     SrcAlign = MCI->getSourceAlign().valueOrOne();
1586   } else if (auto *MMI = dyn_cast<MemMoveInst>(&CI)) {
1587     DstAlign = MMI->getDestAlign().valueOrOne();
1588     SrcAlign = MMI->getSourceAlign().valueOrOne();
1589   } else {
1590     auto *MSI = cast<MemSetInst>(&CI);
1591     DstAlign = MSI->getDestAlign().valueOrOne();
1592   }
1593 
1594   // We need to propagate the tail call flag from the IR inst as an argument.
1595   // Otherwise, we have to pessimize and assume later that we cannot tail call
1596   // any memory intrinsics.
1597   ICall.addImm(CI.isTailCall() ? 1 : 0);
1598 
1599   // Create mem operands to store the alignment and volatile info.
1600   auto VolFlag = IsVol ? MachineMemOperand::MOVolatile : MachineMemOperand::MONone;
1601   ICall.addMemOperand(MF->getMachineMemOperand(
1602       MachinePointerInfo(CI.getArgOperand(0)),
1603       MachineMemOperand::MOStore | VolFlag, 1, DstAlign));
1604   if (Opcode != TargetOpcode::G_MEMSET)
1605     ICall.addMemOperand(MF->getMachineMemOperand(
1606         MachinePointerInfo(CI.getArgOperand(1)),
1607         MachineMemOperand::MOLoad | VolFlag, 1, SrcAlign));
1608 
1609   return true;
1610 }
1611 
1612 void IRTranslator::getStackGuard(Register DstReg,
1613                                  MachineIRBuilder &MIRBuilder) {
1614   const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
1615   MRI->setRegClass(DstReg, TRI->getPointerRegClass(*MF));
1616   auto MIB =
1617       MIRBuilder.buildInstr(TargetOpcode::LOAD_STACK_GUARD, {DstReg}, {});
1618 
1619   auto &TLI = *MF->getSubtarget().getTargetLowering();
1620   Value *Global = TLI.getSDagStackGuard(*MF->getFunction().getParent());
1621   if (!Global)
1622     return;
1623 
1624   MachinePointerInfo MPInfo(Global);
1625   auto Flags = MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant |
1626                MachineMemOperand::MODereferenceable;
1627   MachineMemOperand *MemRef =
1628       MF->getMachineMemOperand(MPInfo, Flags, DL->getPointerSizeInBits() / 8,
1629                                DL->getPointerABIAlignment(0));
1630   MIB.setMemRefs({MemRef});
1631 }
1632 
1633 bool IRTranslator::translateOverflowIntrinsic(const CallInst &CI, unsigned Op,
1634                                               MachineIRBuilder &MIRBuilder) {
1635   ArrayRef<Register> ResRegs = getOrCreateVRegs(CI);
1636   MIRBuilder.buildInstr(
1637       Op, {ResRegs[0], ResRegs[1]},
1638       {getOrCreateVReg(*CI.getOperand(0)), getOrCreateVReg(*CI.getOperand(1))});
1639 
1640   return true;
1641 }
1642 
1643 bool IRTranslator::translateFixedPointIntrinsic(unsigned Op, const CallInst &CI,
1644                                                 MachineIRBuilder &MIRBuilder) {
1645   Register Dst = getOrCreateVReg(CI);
1646   Register Src0 = getOrCreateVReg(*CI.getOperand(0));
1647   Register Src1 = getOrCreateVReg(*CI.getOperand(1));
1648   uint64_t Scale = cast<ConstantInt>(CI.getOperand(2))->getZExtValue();
1649   MIRBuilder.buildInstr(Op, {Dst}, { Src0, Src1, Scale });
1650   return true;
1651 }
1652 
1653 unsigned IRTranslator::getSimpleIntrinsicOpcode(Intrinsic::ID ID) {
1654   switch (ID) {
1655     default:
1656       break;
1657     case Intrinsic::bswap:
1658       return TargetOpcode::G_BSWAP;
1659     case Intrinsic::bitreverse:
1660       return TargetOpcode::G_BITREVERSE;
1661     case Intrinsic::fshl:
1662       return TargetOpcode::G_FSHL;
1663     case Intrinsic::fshr:
1664       return TargetOpcode::G_FSHR;
1665     case Intrinsic::ceil:
1666       return TargetOpcode::G_FCEIL;
1667     case Intrinsic::cos:
1668       return TargetOpcode::G_FCOS;
1669     case Intrinsic::ctpop:
1670       return TargetOpcode::G_CTPOP;
1671     case Intrinsic::exp:
1672       return TargetOpcode::G_FEXP;
1673     case Intrinsic::exp2:
1674       return TargetOpcode::G_FEXP2;
1675     case Intrinsic::fabs:
1676       return TargetOpcode::G_FABS;
1677     case Intrinsic::copysign:
1678       return TargetOpcode::G_FCOPYSIGN;
1679     case Intrinsic::minnum:
1680       return TargetOpcode::G_FMINNUM;
1681     case Intrinsic::maxnum:
1682       return TargetOpcode::G_FMAXNUM;
1683     case Intrinsic::minimum:
1684       return TargetOpcode::G_FMINIMUM;
1685     case Intrinsic::maximum:
1686       return TargetOpcode::G_FMAXIMUM;
1687     case Intrinsic::canonicalize:
1688       return TargetOpcode::G_FCANONICALIZE;
1689     case Intrinsic::floor:
1690       return TargetOpcode::G_FFLOOR;
1691     case Intrinsic::fma:
1692       return TargetOpcode::G_FMA;
1693     case Intrinsic::log:
1694       return TargetOpcode::G_FLOG;
1695     case Intrinsic::log2:
1696       return TargetOpcode::G_FLOG2;
1697     case Intrinsic::log10:
1698       return TargetOpcode::G_FLOG10;
1699     case Intrinsic::nearbyint:
1700       return TargetOpcode::G_FNEARBYINT;
1701     case Intrinsic::pow:
1702       return TargetOpcode::G_FPOW;
1703     case Intrinsic::powi:
1704       return TargetOpcode::G_FPOWI;
1705     case Intrinsic::rint:
1706       return TargetOpcode::G_FRINT;
1707     case Intrinsic::round:
1708       return TargetOpcode::G_INTRINSIC_ROUND;
1709     case Intrinsic::roundeven:
1710       return TargetOpcode::G_INTRINSIC_ROUNDEVEN;
1711     case Intrinsic::sin:
1712       return TargetOpcode::G_FSIN;
1713     case Intrinsic::sqrt:
1714       return TargetOpcode::G_FSQRT;
1715     case Intrinsic::trunc:
1716       return TargetOpcode::G_INTRINSIC_TRUNC;
1717     case Intrinsic::readcyclecounter:
1718       return TargetOpcode::G_READCYCLECOUNTER;
1719     case Intrinsic::ptrmask:
1720       return TargetOpcode::G_PTRMASK;
1721     case Intrinsic::lrint:
1722       return TargetOpcode::G_INTRINSIC_LRINT;
1723     // FADD/FMUL require checking the FMF, so are handled elsewhere.
1724     case Intrinsic::vector_reduce_fmin:
1725       return TargetOpcode::G_VECREDUCE_FMIN;
1726     case Intrinsic::vector_reduce_fmax:
1727       return TargetOpcode::G_VECREDUCE_FMAX;
1728     case Intrinsic::vector_reduce_add:
1729       return TargetOpcode::G_VECREDUCE_ADD;
1730     case Intrinsic::vector_reduce_mul:
1731       return TargetOpcode::G_VECREDUCE_MUL;
1732     case Intrinsic::vector_reduce_and:
1733       return TargetOpcode::G_VECREDUCE_AND;
1734     case Intrinsic::vector_reduce_or:
1735       return TargetOpcode::G_VECREDUCE_OR;
1736     case Intrinsic::vector_reduce_xor:
1737       return TargetOpcode::G_VECREDUCE_XOR;
1738     case Intrinsic::vector_reduce_smax:
1739       return TargetOpcode::G_VECREDUCE_SMAX;
1740     case Intrinsic::vector_reduce_smin:
1741       return TargetOpcode::G_VECREDUCE_SMIN;
1742     case Intrinsic::vector_reduce_umax:
1743       return TargetOpcode::G_VECREDUCE_UMAX;
1744     case Intrinsic::vector_reduce_umin:
1745       return TargetOpcode::G_VECREDUCE_UMIN;
1746   }
1747   return Intrinsic::not_intrinsic;
1748 }
1749 
1750 bool IRTranslator::translateSimpleIntrinsic(const CallInst &CI,
1751                                             Intrinsic::ID ID,
1752                                             MachineIRBuilder &MIRBuilder) {
1753 
1754   unsigned Op = getSimpleIntrinsicOpcode(ID);
1755 
1756   // Is this a simple intrinsic?
1757   if (Op == Intrinsic::not_intrinsic)
1758     return false;
1759 
1760   // Yes. Let's translate it.
1761   SmallVector<llvm::SrcOp, 4> VRegs;
1762   for (auto &Arg : CI.arg_operands())
1763     VRegs.push_back(getOrCreateVReg(*Arg));
1764 
1765   MIRBuilder.buildInstr(Op, {getOrCreateVReg(CI)}, VRegs,
1766                         MachineInstr::copyFlagsFromInstruction(CI));
1767   return true;
1768 }
1769 
1770 // TODO: Include ConstainedOps.def when all strict instructions are defined.
1771 static unsigned getConstrainedOpcode(Intrinsic::ID ID) {
1772   switch (ID) {
1773   case Intrinsic::experimental_constrained_fadd:
1774     return TargetOpcode::G_STRICT_FADD;
1775   case Intrinsic::experimental_constrained_fsub:
1776     return TargetOpcode::G_STRICT_FSUB;
1777   case Intrinsic::experimental_constrained_fmul:
1778     return TargetOpcode::G_STRICT_FMUL;
1779   case Intrinsic::experimental_constrained_fdiv:
1780     return TargetOpcode::G_STRICT_FDIV;
1781   case Intrinsic::experimental_constrained_frem:
1782     return TargetOpcode::G_STRICT_FREM;
1783   case Intrinsic::experimental_constrained_fma:
1784     return TargetOpcode::G_STRICT_FMA;
1785   case Intrinsic::experimental_constrained_sqrt:
1786     return TargetOpcode::G_STRICT_FSQRT;
1787   default:
1788     return 0;
1789   }
1790 }
1791 
1792 bool IRTranslator::translateConstrainedFPIntrinsic(
1793   const ConstrainedFPIntrinsic &FPI, MachineIRBuilder &MIRBuilder) {
1794   fp::ExceptionBehavior EB = FPI.getExceptionBehavior().getValue();
1795 
1796   unsigned Opcode = getConstrainedOpcode(FPI.getIntrinsicID());
1797   if (!Opcode)
1798     return false;
1799 
1800   unsigned Flags = MachineInstr::copyFlagsFromInstruction(FPI);
1801   if (EB == fp::ExceptionBehavior::ebIgnore)
1802     Flags |= MachineInstr::NoFPExcept;
1803 
1804   SmallVector<llvm::SrcOp, 4> VRegs;
1805   VRegs.push_back(getOrCreateVReg(*FPI.getArgOperand(0)));
1806   if (!FPI.isUnaryOp())
1807     VRegs.push_back(getOrCreateVReg(*FPI.getArgOperand(1)));
1808   if (FPI.isTernaryOp())
1809     VRegs.push_back(getOrCreateVReg(*FPI.getArgOperand(2)));
1810 
1811   MIRBuilder.buildInstr(Opcode, {getOrCreateVReg(FPI)}, VRegs, Flags);
1812   return true;
1813 }
1814 
1815 bool IRTranslator::translateKnownIntrinsic(const CallInst &CI, Intrinsic::ID ID,
1816                                            MachineIRBuilder &MIRBuilder) {
1817 
1818   // If this is a simple intrinsic (that is, we just need to add a def of
1819   // a vreg, and uses for each arg operand, then translate it.
1820   if (translateSimpleIntrinsic(CI, ID, MIRBuilder))
1821     return true;
1822 
1823   switch (ID) {
1824   default:
1825     break;
1826   case Intrinsic::lifetime_start:
1827   case Intrinsic::lifetime_end: {
1828     // No stack colouring in O0, discard region information.
1829     if (MF->getTarget().getOptLevel() == CodeGenOpt::None)
1830       return true;
1831 
1832     unsigned Op = ID == Intrinsic::lifetime_start ? TargetOpcode::LIFETIME_START
1833                                                   : TargetOpcode::LIFETIME_END;
1834 
1835     // Get the underlying objects for the location passed on the lifetime
1836     // marker.
1837     SmallVector<const Value *, 4> Allocas;
1838     getUnderlyingObjects(CI.getArgOperand(1), Allocas);
1839 
1840     // Iterate over each underlying object, creating lifetime markers for each
1841     // static alloca. Quit if we find a non-static alloca.
1842     for (const Value *V : Allocas) {
1843       const AllocaInst *AI = dyn_cast<AllocaInst>(V);
1844       if (!AI)
1845         continue;
1846 
1847       if (!AI->isStaticAlloca())
1848         return true;
1849 
1850       MIRBuilder.buildInstr(Op).addFrameIndex(getOrCreateFrameIndex(*AI));
1851     }
1852     return true;
1853   }
1854   case Intrinsic::dbg_declare: {
1855     const DbgDeclareInst &DI = cast<DbgDeclareInst>(CI);
1856     assert(DI.getVariable() && "Missing variable");
1857 
1858     const Value *Address = DI.getAddress();
1859     if (!Address || isa<UndefValue>(Address)) {
1860       LLVM_DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
1861       return true;
1862     }
1863 
1864     assert(DI.getVariable()->isValidLocationForIntrinsic(
1865                MIRBuilder.getDebugLoc()) &&
1866            "Expected inlined-at fields to agree");
1867     auto AI = dyn_cast<AllocaInst>(Address);
1868     if (AI && AI->isStaticAlloca()) {
1869       // Static allocas are tracked at the MF level, no need for DBG_VALUE
1870       // instructions (in fact, they get ignored if they *do* exist).
1871       MF->setVariableDbgInfo(DI.getVariable(), DI.getExpression(),
1872                              getOrCreateFrameIndex(*AI), DI.getDebugLoc());
1873     } else {
1874       // A dbg.declare describes the address of a source variable, so lower it
1875       // into an indirect DBG_VALUE.
1876       MIRBuilder.buildIndirectDbgValue(getOrCreateVReg(*Address),
1877                                        DI.getVariable(), DI.getExpression());
1878     }
1879     return true;
1880   }
1881   case Intrinsic::dbg_label: {
1882     const DbgLabelInst &DI = cast<DbgLabelInst>(CI);
1883     assert(DI.getLabel() && "Missing label");
1884 
1885     assert(DI.getLabel()->isValidLocationForIntrinsic(
1886                MIRBuilder.getDebugLoc()) &&
1887            "Expected inlined-at fields to agree");
1888 
1889     MIRBuilder.buildDbgLabel(DI.getLabel());
1890     return true;
1891   }
1892   case Intrinsic::vaend:
1893     // No target I know of cares about va_end. Certainly no in-tree target
1894     // does. Simplest intrinsic ever!
1895     return true;
1896   case Intrinsic::vastart: {
1897     auto &TLI = *MF->getSubtarget().getTargetLowering();
1898     Value *Ptr = CI.getArgOperand(0);
1899     unsigned ListSize = TLI.getVaListSizeInBits(*DL) / 8;
1900 
1901     // FIXME: Get alignment
1902     MIRBuilder.buildInstr(TargetOpcode::G_VASTART, {}, {getOrCreateVReg(*Ptr)})
1903         .addMemOperand(MF->getMachineMemOperand(MachinePointerInfo(Ptr),
1904                                                 MachineMemOperand::MOStore,
1905                                                 ListSize, Align(1)));
1906     return true;
1907   }
1908   case Intrinsic::dbg_value: {
1909     // This form of DBG_VALUE is target-independent.
1910     const DbgValueInst &DI = cast<DbgValueInst>(CI);
1911     const Value *V = DI.getValue();
1912     assert(DI.getVariable()->isValidLocationForIntrinsic(
1913                MIRBuilder.getDebugLoc()) &&
1914            "Expected inlined-at fields to agree");
1915     if (!V) {
1916       // Currently the optimizer can produce this; insert an undef to
1917       // help debugging.  Probably the optimizer should not do this.
1918       MIRBuilder.buildIndirectDbgValue(0, DI.getVariable(), DI.getExpression());
1919     } else if (const auto *CI = dyn_cast<Constant>(V)) {
1920       MIRBuilder.buildConstDbgValue(*CI, DI.getVariable(), DI.getExpression());
1921     } else {
1922       for (Register Reg : getOrCreateVRegs(*V)) {
1923         // FIXME: This does not handle register-indirect values at offset 0. The
1924         // direct/indirect thing shouldn't really be handled by something as
1925         // implicit as reg+noreg vs reg+imm in the first place, but it seems
1926         // pretty baked in right now.
1927         MIRBuilder.buildDirectDbgValue(Reg, DI.getVariable(), DI.getExpression());
1928       }
1929     }
1930     return true;
1931   }
1932   case Intrinsic::uadd_with_overflow:
1933     return translateOverflowIntrinsic(CI, TargetOpcode::G_UADDO, MIRBuilder);
1934   case Intrinsic::sadd_with_overflow:
1935     return translateOverflowIntrinsic(CI, TargetOpcode::G_SADDO, MIRBuilder);
1936   case Intrinsic::usub_with_overflow:
1937     return translateOverflowIntrinsic(CI, TargetOpcode::G_USUBO, MIRBuilder);
1938   case Intrinsic::ssub_with_overflow:
1939     return translateOverflowIntrinsic(CI, TargetOpcode::G_SSUBO, MIRBuilder);
1940   case Intrinsic::umul_with_overflow:
1941     return translateOverflowIntrinsic(CI, TargetOpcode::G_UMULO, MIRBuilder);
1942   case Intrinsic::smul_with_overflow:
1943     return translateOverflowIntrinsic(CI, TargetOpcode::G_SMULO, MIRBuilder);
1944   case Intrinsic::uadd_sat:
1945     return translateBinaryOp(TargetOpcode::G_UADDSAT, CI, MIRBuilder);
1946   case Intrinsic::sadd_sat:
1947     return translateBinaryOp(TargetOpcode::G_SADDSAT, CI, MIRBuilder);
1948   case Intrinsic::usub_sat:
1949     return translateBinaryOp(TargetOpcode::G_USUBSAT, CI, MIRBuilder);
1950   case Intrinsic::ssub_sat:
1951     return translateBinaryOp(TargetOpcode::G_SSUBSAT, CI, MIRBuilder);
1952   case Intrinsic::ushl_sat:
1953     return translateBinaryOp(TargetOpcode::G_USHLSAT, CI, MIRBuilder);
1954   case Intrinsic::sshl_sat:
1955     return translateBinaryOp(TargetOpcode::G_SSHLSAT, CI, MIRBuilder);
1956   case Intrinsic::umin:
1957     return translateBinaryOp(TargetOpcode::G_UMIN, CI, MIRBuilder);
1958   case Intrinsic::umax:
1959     return translateBinaryOp(TargetOpcode::G_UMAX, CI, MIRBuilder);
1960   case Intrinsic::smin:
1961     return translateBinaryOp(TargetOpcode::G_SMIN, CI, MIRBuilder);
1962   case Intrinsic::smax:
1963     return translateBinaryOp(TargetOpcode::G_SMAX, CI, MIRBuilder);
1964   case Intrinsic::abs:
1965     // TODO: Preserve "int min is poison" arg in GMIR?
1966     return translateUnaryOp(TargetOpcode::G_ABS, CI, MIRBuilder);
1967   case Intrinsic::smul_fix:
1968     return translateFixedPointIntrinsic(TargetOpcode::G_SMULFIX, CI, MIRBuilder);
1969   case Intrinsic::umul_fix:
1970     return translateFixedPointIntrinsic(TargetOpcode::G_UMULFIX, CI, MIRBuilder);
1971   case Intrinsic::smul_fix_sat:
1972     return translateFixedPointIntrinsic(TargetOpcode::G_SMULFIXSAT, CI, MIRBuilder);
1973   case Intrinsic::umul_fix_sat:
1974     return translateFixedPointIntrinsic(TargetOpcode::G_UMULFIXSAT, CI, MIRBuilder);
1975   case Intrinsic::sdiv_fix:
1976     return translateFixedPointIntrinsic(TargetOpcode::G_SDIVFIX, CI, MIRBuilder);
1977   case Intrinsic::udiv_fix:
1978     return translateFixedPointIntrinsic(TargetOpcode::G_UDIVFIX, CI, MIRBuilder);
1979   case Intrinsic::sdiv_fix_sat:
1980     return translateFixedPointIntrinsic(TargetOpcode::G_SDIVFIXSAT, CI, MIRBuilder);
1981   case Intrinsic::udiv_fix_sat:
1982     return translateFixedPointIntrinsic(TargetOpcode::G_UDIVFIXSAT, CI, MIRBuilder);
1983   case Intrinsic::fmuladd: {
1984     const TargetMachine &TM = MF->getTarget();
1985     const TargetLowering &TLI = *MF->getSubtarget().getTargetLowering();
1986     Register Dst = getOrCreateVReg(CI);
1987     Register Op0 = getOrCreateVReg(*CI.getArgOperand(0));
1988     Register Op1 = getOrCreateVReg(*CI.getArgOperand(1));
1989     Register Op2 = getOrCreateVReg(*CI.getArgOperand(2));
1990     if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
1991         TLI.isFMAFasterThanFMulAndFAdd(*MF,
1992                                        TLI.getValueType(*DL, CI.getType()))) {
1993       // TODO: Revisit this to see if we should move this part of the
1994       // lowering to the combiner.
1995       MIRBuilder.buildFMA(Dst, Op0, Op1, Op2,
1996                           MachineInstr::copyFlagsFromInstruction(CI));
1997     } else {
1998       LLT Ty = getLLTForType(*CI.getType(), *DL);
1999       auto FMul = MIRBuilder.buildFMul(
2000           Ty, Op0, Op1, MachineInstr::copyFlagsFromInstruction(CI));
2001       MIRBuilder.buildFAdd(Dst, FMul, Op2,
2002                            MachineInstr::copyFlagsFromInstruction(CI));
2003     }
2004     return true;
2005   }
2006   case Intrinsic::convert_from_fp16:
2007     // FIXME: This intrinsic should probably be removed from the IR.
2008     MIRBuilder.buildFPExt(getOrCreateVReg(CI),
2009                           getOrCreateVReg(*CI.getArgOperand(0)),
2010                           MachineInstr::copyFlagsFromInstruction(CI));
2011     return true;
2012   case Intrinsic::convert_to_fp16:
2013     // FIXME: This intrinsic should probably be removed from the IR.
2014     MIRBuilder.buildFPTrunc(getOrCreateVReg(CI),
2015                             getOrCreateVReg(*CI.getArgOperand(0)),
2016                             MachineInstr::copyFlagsFromInstruction(CI));
2017     return true;
2018   case Intrinsic::memcpy:
2019     return translateMemFunc(CI, MIRBuilder, TargetOpcode::G_MEMCPY);
2020   case Intrinsic::memmove:
2021     return translateMemFunc(CI, MIRBuilder, TargetOpcode::G_MEMMOVE);
2022   case Intrinsic::memset:
2023     return translateMemFunc(CI, MIRBuilder, TargetOpcode::G_MEMSET);
2024   case Intrinsic::eh_typeid_for: {
2025     GlobalValue *GV = ExtractTypeInfo(CI.getArgOperand(0));
2026     Register Reg = getOrCreateVReg(CI);
2027     unsigned TypeID = MF->getTypeIDFor(GV);
2028     MIRBuilder.buildConstant(Reg, TypeID);
2029     return true;
2030   }
2031   case Intrinsic::objectsize:
2032     llvm_unreachable("llvm.objectsize.* should have been lowered already");
2033 
2034   case Intrinsic::is_constant:
2035     llvm_unreachable("llvm.is.constant.* should have been lowered already");
2036 
2037   case Intrinsic::stackguard:
2038     getStackGuard(getOrCreateVReg(CI), MIRBuilder);
2039     return true;
2040   case Intrinsic::stackprotector: {
2041     LLT PtrTy = getLLTForType(*CI.getArgOperand(0)->getType(), *DL);
2042     Register GuardVal = MRI->createGenericVirtualRegister(PtrTy);
2043     getStackGuard(GuardVal, MIRBuilder);
2044 
2045     AllocaInst *Slot = cast<AllocaInst>(CI.getArgOperand(1));
2046     int FI = getOrCreateFrameIndex(*Slot);
2047     MF->getFrameInfo().setStackProtectorIndex(FI);
2048 
2049     MIRBuilder.buildStore(
2050         GuardVal, getOrCreateVReg(*Slot),
2051         *MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(*MF, FI),
2052                                   MachineMemOperand::MOStore |
2053                                       MachineMemOperand::MOVolatile,
2054                                   PtrTy.getSizeInBits() / 8, Align(8)));
2055     return true;
2056   }
2057   case Intrinsic::stacksave: {
2058     // Save the stack pointer to the location provided by the intrinsic.
2059     Register Reg = getOrCreateVReg(CI);
2060     Register StackPtr = MF->getSubtarget()
2061                             .getTargetLowering()
2062                             ->getStackPointerRegisterToSaveRestore();
2063 
2064     // If the target doesn't specify a stack pointer, then fall back.
2065     if (!StackPtr)
2066       return false;
2067 
2068     MIRBuilder.buildCopy(Reg, StackPtr);
2069     return true;
2070   }
2071   case Intrinsic::stackrestore: {
2072     // Restore the stack pointer from the location provided by the intrinsic.
2073     Register Reg = getOrCreateVReg(*CI.getArgOperand(0));
2074     Register StackPtr = MF->getSubtarget()
2075                             .getTargetLowering()
2076                             ->getStackPointerRegisterToSaveRestore();
2077 
2078     // If the target doesn't specify a stack pointer, then fall back.
2079     if (!StackPtr)
2080       return false;
2081 
2082     MIRBuilder.buildCopy(StackPtr, Reg);
2083     return true;
2084   }
2085   case Intrinsic::cttz:
2086   case Intrinsic::ctlz: {
2087     ConstantInt *Cst = cast<ConstantInt>(CI.getArgOperand(1));
2088     bool isTrailing = ID == Intrinsic::cttz;
2089     unsigned Opcode = isTrailing
2090                           ? Cst->isZero() ? TargetOpcode::G_CTTZ
2091                                           : TargetOpcode::G_CTTZ_ZERO_UNDEF
2092                           : Cst->isZero() ? TargetOpcode::G_CTLZ
2093                                           : TargetOpcode::G_CTLZ_ZERO_UNDEF;
2094     MIRBuilder.buildInstr(Opcode, {getOrCreateVReg(CI)},
2095                           {getOrCreateVReg(*CI.getArgOperand(0))});
2096     return true;
2097   }
2098   case Intrinsic::invariant_start: {
2099     LLT PtrTy = getLLTForType(*CI.getArgOperand(0)->getType(), *DL);
2100     Register Undef = MRI->createGenericVirtualRegister(PtrTy);
2101     MIRBuilder.buildUndef(Undef);
2102     return true;
2103   }
2104   case Intrinsic::invariant_end:
2105     return true;
2106   case Intrinsic::expect:
2107   case Intrinsic::annotation:
2108   case Intrinsic::ptr_annotation:
2109   case Intrinsic::launder_invariant_group:
2110   case Intrinsic::strip_invariant_group: {
2111     // Drop the intrinsic, but forward the value.
2112     MIRBuilder.buildCopy(getOrCreateVReg(CI),
2113                          getOrCreateVReg(*CI.getArgOperand(0)));
2114     return true;
2115   }
2116   case Intrinsic::assume:
2117   case Intrinsic::var_annotation:
2118   case Intrinsic::sideeffect:
2119     // Discard annotate attributes, assumptions, and artificial side-effects.
2120     return true;
2121   case Intrinsic::read_volatile_register:
2122   case Intrinsic::read_register: {
2123     Value *Arg = CI.getArgOperand(0);
2124     MIRBuilder
2125         .buildInstr(TargetOpcode::G_READ_REGISTER, {getOrCreateVReg(CI)}, {})
2126         .addMetadata(cast<MDNode>(cast<MetadataAsValue>(Arg)->getMetadata()));
2127     return true;
2128   }
2129   case Intrinsic::write_register: {
2130     Value *Arg = CI.getArgOperand(0);
2131     MIRBuilder.buildInstr(TargetOpcode::G_WRITE_REGISTER)
2132       .addMetadata(cast<MDNode>(cast<MetadataAsValue>(Arg)->getMetadata()))
2133       .addUse(getOrCreateVReg(*CI.getArgOperand(1)));
2134     return true;
2135   }
2136   case Intrinsic::localescape: {
2137     MachineBasicBlock &EntryMBB = MF->front();
2138     StringRef EscapedName = GlobalValue::dropLLVMManglingEscape(MF->getName());
2139 
2140     // Directly emit some LOCAL_ESCAPE machine instrs. Label assignment emission
2141     // is the same on all targets.
2142     for (unsigned Idx = 0, E = CI.getNumArgOperands(); Idx < E; ++Idx) {
2143       Value *Arg = CI.getArgOperand(Idx)->stripPointerCasts();
2144       if (isa<ConstantPointerNull>(Arg))
2145         continue; // Skip null pointers. They represent a hole in index space.
2146 
2147       int FI = getOrCreateFrameIndex(*cast<AllocaInst>(Arg));
2148       MCSymbol *FrameAllocSym =
2149           MF->getMMI().getContext().getOrCreateFrameAllocSymbol(EscapedName,
2150                                                                 Idx);
2151 
2152       // This should be inserted at the start of the entry block.
2153       auto LocalEscape =
2154           MIRBuilder.buildInstrNoInsert(TargetOpcode::LOCAL_ESCAPE)
2155               .addSym(FrameAllocSym)
2156               .addFrameIndex(FI);
2157 
2158       EntryMBB.insert(EntryMBB.begin(), LocalEscape);
2159     }
2160 
2161     return true;
2162   }
2163   case Intrinsic::vector_reduce_fadd:
2164   case Intrinsic::vector_reduce_fmul: {
2165     // Need to check for the reassoc flag to decide whether we want a
2166     // sequential reduction opcode or not.
2167     Register Dst = getOrCreateVReg(CI);
2168     Register ScalarSrc = getOrCreateVReg(*CI.getArgOperand(0));
2169     Register VecSrc = getOrCreateVReg(*CI.getArgOperand(1));
2170     unsigned Opc = 0;
2171     if (!CI.hasAllowReassoc()) {
2172       // The sequential ordering case.
2173       Opc = ID == Intrinsic::vector_reduce_fadd
2174                 ? TargetOpcode::G_VECREDUCE_SEQ_FADD
2175                 : TargetOpcode::G_VECREDUCE_SEQ_FMUL;
2176       MIRBuilder.buildInstr(Opc, {Dst}, {ScalarSrc, VecSrc},
2177                             MachineInstr::copyFlagsFromInstruction(CI));
2178       return true;
2179     }
2180     // We split the operation into a separate G_FADD/G_FMUL + the reduce,
2181     // since the associativity doesn't matter.
2182     unsigned ScalarOpc;
2183     if (ID == Intrinsic::vector_reduce_fadd) {
2184       Opc = TargetOpcode::G_VECREDUCE_FADD;
2185       ScalarOpc = TargetOpcode::G_FADD;
2186     } else {
2187       Opc = TargetOpcode::G_VECREDUCE_FMUL;
2188       ScalarOpc = TargetOpcode::G_FMUL;
2189     }
2190     LLT DstTy = MRI->getType(Dst);
2191     auto Rdx = MIRBuilder.buildInstr(
2192         Opc, {DstTy}, {VecSrc}, MachineInstr::copyFlagsFromInstruction(CI));
2193     MIRBuilder.buildInstr(ScalarOpc, {Dst}, {ScalarSrc, Rdx},
2194                           MachineInstr::copyFlagsFromInstruction(CI));
2195 
2196     return true;
2197   }
2198 #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC)  \
2199   case Intrinsic::INTRINSIC:
2200 #include "llvm/IR/ConstrainedOps.def"
2201     return translateConstrainedFPIntrinsic(cast<ConstrainedFPIntrinsic>(CI),
2202                                            MIRBuilder);
2203 
2204   }
2205   return false;
2206 }
2207 
2208 bool IRTranslator::translateInlineAsm(const CallBase &CB,
2209                                       MachineIRBuilder &MIRBuilder) {
2210 
2211   const InlineAsmLowering *ALI = MF->getSubtarget().getInlineAsmLowering();
2212 
2213   if (!ALI) {
2214     LLVM_DEBUG(
2215         dbgs() << "Inline asm lowering is not supported for this target yet\n");
2216     return false;
2217   }
2218 
2219   return ALI->lowerInlineAsm(
2220       MIRBuilder, CB, [&](const Value &Val) { return getOrCreateVRegs(Val); });
2221 }
2222 
2223 bool IRTranslator::translateCallBase(const CallBase &CB,
2224                                      MachineIRBuilder &MIRBuilder) {
2225   ArrayRef<Register> Res = getOrCreateVRegs(CB);
2226 
2227   SmallVector<ArrayRef<Register>, 8> Args;
2228   Register SwiftInVReg = 0;
2229   Register SwiftErrorVReg = 0;
2230   for (auto &Arg : CB.args()) {
2231     if (CLI->supportSwiftError() && isSwiftError(Arg)) {
2232       assert(SwiftInVReg == 0 && "Expected only one swift error argument");
2233       LLT Ty = getLLTForType(*Arg->getType(), *DL);
2234       SwiftInVReg = MRI->createGenericVirtualRegister(Ty);
2235       MIRBuilder.buildCopy(SwiftInVReg, SwiftError.getOrCreateVRegUseAt(
2236                                             &CB, &MIRBuilder.getMBB(), Arg));
2237       Args.emplace_back(makeArrayRef(SwiftInVReg));
2238       SwiftErrorVReg =
2239           SwiftError.getOrCreateVRegDefAt(&CB, &MIRBuilder.getMBB(), Arg);
2240       continue;
2241     }
2242     Args.push_back(getOrCreateVRegs(*Arg));
2243   }
2244 
2245   // We don't set HasCalls on MFI here yet because call lowering may decide to
2246   // optimize into tail calls. Instead, we defer that to selection where a final
2247   // scan is done to check if any instructions are calls.
2248   bool Success =
2249       CLI->lowerCall(MIRBuilder, CB, Res, Args, SwiftErrorVReg,
2250                      [&]() { return getOrCreateVReg(*CB.getCalledOperand()); });
2251 
2252   // Check if we just inserted a tail call.
2253   if (Success) {
2254     assert(!HasTailCall && "Can't tail call return twice from block?");
2255     const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
2256     HasTailCall = TII->isTailCall(*std::prev(MIRBuilder.getInsertPt()));
2257   }
2258 
2259   return Success;
2260 }
2261 
2262 bool IRTranslator::translateCall(const User &U, MachineIRBuilder &MIRBuilder) {
2263   const CallInst &CI = cast<CallInst>(U);
2264   auto TII = MF->getTarget().getIntrinsicInfo();
2265   const Function *F = CI.getCalledFunction();
2266 
2267   // FIXME: support Windows dllimport function calls.
2268   if (F && (F->hasDLLImportStorageClass() ||
2269             (MF->getTarget().getTargetTriple().isOSWindows() &&
2270              F->hasExternalWeakLinkage())))
2271     return false;
2272 
2273   // FIXME: support control flow guard targets.
2274   if (CI.countOperandBundlesOfType(LLVMContext::OB_cfguardtarget))
2275     return false;
2276 
2277   if (CI.isInlineAsm())
2278     return translateInlineAsm(CI, MIRBuilder);
2279 
2280   Intrinsic::ID ID = Intrinsic::not_intrinsic;
2281   if (F && F->isIntrinsic()) {
2282     ID = F->getIntrinsicID();
2283     if (TII && ID == Intrinsic::not_intrinsic)
2284       ID = static_cast<Intrinsic::ID>(TII->getIntrinsicID(F));
2285   }
2286 
2287   if (!F || !F->isIntrinsic() || ID == Intrinsic::not_intrinsic)
2288     return translateCallBase(CI, MIRBuilder);
2289 
2290   assert(ID != Intrinsic::not_intrinsic && "unknown intrinsic");
2291 
2292   if (translateKnownIntrinsic(CI, ID, MIRBuilder))
2293     return true;
2294 
2295   ArrayRef<Register> ResultRegs;
2296   if (!CI.getType()->isVoidTy())
2297     ResultRegs = getOrCreateVRegs(CI);
2298 
2299   // Ignore the callsite attributes. Backend code is most likely not expecting
2300   // an intrinsic to sometimes have side effects and sometimes not.
2301   MachineInstrBuilder MIB =
2302       MIRBuilder.buildIntrinsic(ID, ResultRegs, !F->doesNotAccessMemory());
2303   if (isa<FPMathOperator>(CI))
2304     MIB->copyIRFlags(CI);
2305 
2306   for (auto &Arg : enumerate(CI.arg_operands())) {
2307     // If this is required to be an immediate, don't materialize it in a
2308     // register.
2309     if (CI.paramHasAttr(Arg.index(), Attribute::ImmArg)) {
2310       if (ConstantInt *CI = dyn_cast<ConstantInt>(Arg.value())) {
2311         // imm arguments are more convenient than cimm (and realistically
2312         // probably sufficient), so use them.
2313         assert(CI->getBitWidth() <= 64 &&
2314                "large intrinsic immediates not handled");
2315         MIB.addImm(CI->getSExtValue());
2316       } else {
2317         MIB.addFPImm(cast<ConstantFP>(Arg.value()));
2318       }
2319     } else if (auto MD = dyn_cast<MetadataAsValue>(Arg.value())) {
2320       auto *MDN = dyn_cast<MDNode>(MD->getMetadata());
2321       if (!MDN) // This was probably an MDString.
2322         return false;
2323       MIB.addMetadata(MDN);
2324     } else {
2325       ArrayRef<Register> VRegs = getOrCreateVRegs(*Arg.value());
2326       if (VRegs.size() > 1)
2327         return false;
2328       MIB.addUse(VRegs[0]);
2329     }
2330   }
2331 
2332   // Add a MachineMemOperand if it is a target mem intrinsic.
2333   const TargetLowering &TLI = *MF->getSubtarget().getTargetLowering();
2334   TargetLowering::IntrinsicInfo Info;
2335   // TODO: Add a GlobalISel version of getTgtMemIntrinsic.
2336   if (TLI.getTgtMemIntrinsic(Info, CI, *MF, ID)) {
2337     Align Alignment = Info.align.getValueOr(
2338         DL->getABITypeAlign(Info.memVT.getTypeForEVT(F->getContext())));
2339 
2340     uint64_t Size = Info.memVT.getStoreSize();
2341     MIB.addMemOperand(MF->getMachineMemOperand(MachinePointerInfo(Info.ptrVal),
2342                                                Info.flags, Size, Alignment));
2343   }
2344 
2345   return true;
2346 }
2347 
2348 bool IRTranslator::translateInvoke(const User &U,
2349                                    MachineIRBuilder &MIRBuilder) {
2350   const InvokeInst &I = cast<InvokeInst>(U);
2351   MCContext &Context = MF->getContext();
2352 
2353   const BasicBlock *ReturnBB = I.getSuccessor(0);
2354   const BasicBlock *EHPadBB = I.getSuccessor(1);
2355 
2356   const Function *Fn = I.getCalledFunction();
2357   if (I.isInlineAsm())
2358     return false;
2359 
2360   // FIXME: support invoking patchpoint and statepoint intrinsics.
2361   if (Fn && Fn->isIntrinsic())
2362     return false;
2363 
2364   // FIXME: support whatever these are.
2365   if (I.countOperandBundlesOfType(LLVMContext::OB_deopt))
2366     return false;
2367 
2368   // FIXME: support control flow guard targets.
2369   if (I.countOperandBundlesOfType(LLVMContext::OB_cfguardtarget))
2370     return false;
2371 
2372   // FIXME: support Windows exception handling.
2373   if (!isa<LandingPadInst>(EHPadBB->getFirstNonPHI()))
2374     return false;
2375 
2376   // Emit the actual call, bracketed by EH_LABELs so that the MF knows about
2377   // the region covered by the try.
2378   MCSymbol *BeginSymbol = Context.createTempSymbol();
2379   MIRBuilder.buildInstr(TargetOpcode::EH_LABEL).addSym(BeginSymbol);
2380 
2381   if (!translateCallBase(I, MIRBuilder))
2382     return false;
2383 
2384   MCSymbol *EndSymbol = Context.createTempSymbol();
2385   MIRBuilder.buildInstr(TargetOpcode::EH_LABEL).addSym(EndSymbol);
2386 
2387   // FIXME: track probabilities.
2388   MachineBasicBlock &EHPadMBB = getMBB(*EHPadBB),
2389                     &ReturnMBB = getMBB(*ReturnBB);
2390   MF->addInvoke(&EHPadMBB, BeginSymbol, EndSymbol);
2391   MIRBuilder.getMBB().addSuccessor(&ReturnMBB);
2392   MIRBuilder.getMBB().addSuccessor(&EHPadMBB);
2393   MIRBuilder.buildBr(ReturnMBB);
2394 
2395   return true;
2396 }
2397 
2398 bool IRTranslator::translateCallBr(const User &U,
2399                                    MachineIRBuilder &MIRBuilder) {
2400   // FIXME: Implement this.
2401   return false;
2402 }
2403 
2404 bool IRTranslator::translateLandingPad(const User &U,
2405                                        MachineIRBuilder &MIRBuilder) {
2406   const LandingPadInst &LP = cast<LandingPadInst>(U);
2407 
2408   MachineBasicBlock &MBB = MIRBuilder.getMBB();
2409 
2410   MBB.setIsEHPad();
2411 
2412   // If there aren't registers to copy the values into (e.g., during SjLj
2413   // exceptions), then don't bother.
2414   auto &TLI = *MF->getSubtarget().getTargetLowering();
2415   const Constant *PersonalityFn = MF->getFunction().getPersonalityFn();
2416   if (TLI.getExceptionPointerRegister(PersonalityFn) == 0 &&
2417       TLI.getExceptionSelectorRegister(PersonalityFn) == 0)
2418     return true;
2419 
2420   // If landingpad's return type is token type, we don't create DAG nodes
2421   // for its exception pointer and selector value. The extraction of exception
2422   // pointer or selector value from token type landingpads is not currently
2423   // supported.
2424   if (LP.getType()->isTokenTy())
2425     return true;
2426 
2427   // Add a label to mark the beginning of the landing pad.  Deletion of the
2428   // landing pad can thus be detected via the MachineModuleInfo.
2429   MIRBuilder.buildInstr(TargetOpcode::EH_LABEL)
2430     .addSym(MF->addLandingPad(&MBB));
2431 
2432   // If the unwinder does not preserve all registers, ensure that the
2433   // function marks the clobbered registers as used.
2434   const TargetRegisterInfo &TRI = *MF->getSubtarget().getRegisterInfo();
2435   if (auto *RegMask = TRI.getCustomEHPadPreservedMask(*MF))
2436     MF->getRegInfo().addPhysRegsUsedFromRegMask(RegMask);
2437 
2438   LLT Ty = getLLTForType(*LP.getType(), *DL);
2439   Register Undef = MRI->createGenericVirtualRegister(Ty);
2440   MIRBuilder.buildUndef(Undef);
2441 
2442   SmallVector<LLT, 2> Tys;
2443   for (Type *Ty : cast<StructType>(LP.getType())->elements())
2444     Tys.push_back(getLLTForType(*Ty, *DL));
2445   assert(Tys.size() == 2 && "Only two-valued landingpads are supported");
2446 
2447   // Mark exception register as live in.
2448   Register ExceptionReg = TLI.getExceptionPointerRegister(PersonalityFn);
2449   if (!ExceptionReg)
2450     return false;
2451 
2452   MBB.addLiveIn(ExceptionReg);
2453   ArrayRef<Register> ResRegs = getOrCreateVRegs(LP);
2454   MIRBuilder.buildCopy(ResRegs[0], ExceptionReg);
2455 
2456   Register SelectorReg = TLI.getExceptionSelectorRegister(PersonalityFn);
2457   if (!SelectorReg)
2458     return false;
2459 
2460   MBB.addLiveIn(SelectorReg);
2461   Register PtrVReg = MRI->createGenericVirtualRegister(Tys[0]);
2462   MIRBuilder.buildCopy(PtrVReg, SelectorReg);
2463   MIRBuilder.buildCast(ResRegs[1], PtrVReg);
2464 
2465   return true;
2466 }
2467 
2468 bool IRTranslator::translateAlloca(const User &U,
2469                                    MachineIRBuilder &MIRBuilder) {
2470   auto &AI = cast<AllocaInst>(U);
2471 
2472   if (AI.isSwiftError())
2473     return true;
2474 
2475   if (AI.isStaticAlloca()) {
2476     Register Res = getOrCreateVReg(AI);
2477     int FI = getOrCreateFrameIndex(AI);
2478     MIRBuilder.buildFrameIndex(Res, FI);
2479     return true;
2480   }
2481 
2482   // FIXME: support stack probing for Windows.
2483   if (MF->getTarget().getTargetTriple().isOSWindows())
2484     return false;
2485 
2486   // Now we're in the harder dynamic case.
2487   Register NumElts = getOrCreateVReg(*AI.getArraySize());
2488   Type *IntPtrIRTy = DL->getIntPtrType(AI.getType());
2489   LLT IntPtrTy = getLLTForType(*IntPtrIRTy, *DL);
2490   if (MRI->getType(NumElts) != IntPtrTy) {
2491     Register ExtElts = MRI->createGenericVirtualRegister(IntPtrTy);
2492     MIRBuilder.buildZExtOrTrunc(ExtElts, NumElts);
2493     NumElts = ExtElts;
2494   }
2495 
2496   Type *Ty = AI.getAllocatedType();
2497 
2498   Register AllocSize = MRI->createGenericVirtualRegister(IntPtrTy);
2499   Register TySize =
2500       getOrCreateVReg(*ConstantInt::get(IntPtrIRTy, DL->getTypeAllocSize(Ty)));
2501   MIRBuilder.buildMul(AllocSize, NumElts, TySize);
2502 
2503   // Round the size of the allocation up to the stack alignment size
2504   // by add SA-1 to the size. This doesn't overflow because we're computing
2505   // an address inside an alloca.
2506   Align StackAlign = MF->getSubtarget().getFrameLowering()->getStackAlign();
2507   auto SAMinusOne = MIRBuilder.buildConstant(IntPtrTy, StackAlign.value() - 1);
2508   auto AllocAdd = MIRBuilder.buildAdd(IntPtrTy, AllocSize, SAMinusOne,
2509                                       MachineInstr::NoUWrap);
2510   auto AlignCst =
2511       MIRBuilder.buildConstant(IntPtrTy, ~(uint64_t)(StackAlign.value() - 1));
2512   auto AlignedAlloc = MIRBuilder.buildAnd(IntPtrTy, AllocAdd, AlignCst);
2513 
2514   Align Alignment = std::max(AI.getAlign(), DL->getPrefTypeAlign(Ty));
2515   if (Alignment <= StackAlign)
2516     Alignment = Align(1);
2517   MIRBuilder.buildDynStackAlloc(getOrCreateVReg(AI), AlignedAlloc, Alignment);
2518 
2519   MF->getFrameInfo().CreateVariableSizedObject(Alignment, &AI);
2520   assert(MF->getFrameInfo().hasVarSizedObjects());
2521   return true;
2522 }
2523 
2524 bool IRTranslator::translateVAArg(const User &U, MachineIRBuilder &MIRBuilder) {
2525   // FIXME: We may need more info about the type. Because of how LLT works,
2526   // we're completely discarding the i64/double distinction here (amongst
2527   // others). Fortunately the ABIs I know of where that matters don't use va_arg
2528   // anyway but that's not guaranteed.
2529   MIRBuilder.buildInstr(TargetOpcode::G_VAARG, {getOrCreateVReg(U)},
2530                         {getOrCreateVReg(*U.getOperand(0)),
2531                          DL->getABITypeAlign(U.getType()).value()});
2532   return true;
2533 }
2534 
2535 bool IRTranslator::translateInsertElement(const User &U,
2536                                           MachineIRBuilder &MIRBuilder) {
2537   // If it is a <1 x Ty> vector, use the scalar as it is
2538   // not a legal vector type in LLT.
2539   if (cast<FixedVectorType>(U.getType())->getNumElements() == 1)
2540     return translateCopy(U, *U.getOperand(1), MIRBuilder);
2541 
2542   Register Res = getOrCreateVReg(U);
2543   Register Val = getOrCreateVReg(*U.getOperand(0));
2544   Register Elt = getOrCreateVReg(*U.getOperand(1));
2545   Register Idx = getOrCreateVReg(*U.getOperand(2));
2546   MIRBuilder.buildInsertVectorElement(Res, Val, Elt, Idx);
2547   return true;
2548 }
2549 
2550 bool IRTranslator::translateExtractElement(const User &U,
2551                                            MachineIRBuilder &MIRBuilder) {
2552   // If it is a <1 x Ty> vector, use the scalar as it is
2553   // not a legal vector type in LLT.
2554   if (cast<FixedVectorType>(U.getOperand(0)->getType())->getNumElements() == 1)
2555     return translateCopy(U, *U.getOperand(0), MIRBuilder);
2556 
2557   Register Res = getOrCreateVReg(U);
2558   Register Val = getOrCreateVReg(*U.getOperand(0));
2559   const auto &TLI = *MF->getSubtarget().getTargetLowering();
2560   unsigned PreferredVecIdxWidth = TLI.getVectorIdxTy(*DL).getSizeInBits();
2561   Register Idx;
2562   if (auto *CI = dyn_cast<ConstantInt>(U.getOperand(1))) {
2563     if (CI->getBitWidth() != PreferredVecIdxWidth) {
2564       APInt NewIdx = CI->getValue().sextOrTrunc(PreferredVecIdxWidth);
2565       auto *NewIdxCI = ConstantInt::get(CI->getContext(), NewIdx);
2566       Idx = getOrCreateVReg(*NewIdxCI);
2567     }
2568   }
2569   if (!Idx)
2570     Idx = getOrCreateVReg(*U.getOperand(1));
2571   if (MRI->getType(Idx).getSizeInBits() != PreferredVecIdxWidth) {
2572     const LLT VecIdxTy = LLT::scalar(PreferredVecIdxWidth);
2573     Idx = MIRBuilder.buildSExtOrTrunc(VecIdxTy, Idx).getReg(0);
2574   }
2575   MIRBuilder.buildExtractVectorElement(Res, Val, Idx);
2576   return true;
2577 }
2578 
2579 bool IRTranslator::translateShuffleVector(const User &U,
2580                                           MachineIRBuilder &MIRBuilder) {
2581   ArrayRef<int> Mask;
2582   if (auto *SVI = dyn_cast<ShuffleVectorInst>(&U))
2583     Mask = SVI->getShuffleMask();
2584   else
2585     Mask = cast<ConstantExpr>(U).getShuffleMask();
2586   ArrayRef<int> MaskAlloc = MF->allocateShuffleMask(Mask);
2587   MIRBuilder
2588       .buildInstr(TargetOpcode::G_SHUFFLE_VECTOR, {getOrCreateVReg(U)},
2589                   {getOrCreateVReg(*U.getOperand(0)),
2590                    getOrCreateVReg(*U.getOperand(1))})
2591       .addShuffleMask(MaskAlloc);
2592   return true;
2593 }
2594 
2595 bool IRTranslator::translatePHI(const User &U, MachineIRBuilder &MIRBuilder) {
2596   const PHINode &PI = cast<PHINode>(U);
2597 
2598   SmallVector<MachineInstr *, 4> Insts;
2599   for (auto Reg : getOrCreateVRegs(PI)) {
2600     auto MIB = MIRBuilder.buildInstr(TargetOpcode::G_PHI, {Reg}, {});
2601     Insts.push_back(MIB.getInstr());
2602   }
2603 
2604   PendingPHIs.emplace_back(&PI, std::move(Insts));
2605   return true;
2606 }
2607 
2608 bool IRTranslator::translateAtomicCmpXchg(const User &U,
2609                                           MachineIRBuilder &MIRBuilder) {
2610   const AtomicCmpXchgInst &I = cast<AtomicCmpXchgInst>(U);
2611 
2612   auto &TLI = *MF->getSubtarget().getTargetLowering();
2613   auto Flags = TLI.getAtomicMemOperandFlags(I, *DL);
2614 
2615   Type *ResType = I.getType();
2616   Type *ValType = ResType->Type::getStructElementType(0);
2617 
2618   auto Res = getOrCreateVRegs(I);
2619   Register OldValRes = Res[0];
2620   Register SuccessRes = Res[1];
2621   Register Addr = getOrCreateVReg(*I.getPointerOperand());
2622   Register Cmp = getOrCreateVReg(*I.getCompareOperand());
2623   Register NewVal = getOrCreateVReg(*I.getNewValOperand());
2624 
2625   AAMDNodes AAMetadata;
2626   I.getAAMetadata(AAMetadata);
2627 
2628   MIRBuilder.buildAtomicCmpXchgWithSuccess(
2629       OldValRes, SuccessRes, Addr, Cmp, NewVal,
2630       *MF->getMachineMemOperand(
2631           MachinePointerInfo(I.getPointerOperand()), Flags,
2632           DL->getTypeStoreSize(ValType), getMemOpAlign(I), AAMetadata, nullptr,
2633           I.getSyncScopeID(), I.getSuccessOrdering(), I.getFailureOrdering()));
2634   return true;
2635 }
2636 
2637 bool IRTranslator::translateAtomicRMW(const User &U,
2638                                       MachineIRBuilder &MIRBuilder) {
2639   const AtomicRMWInst &I = cast<AtomicRMWInst>(U);
2640   auto &TLI = *MF->getSubtarget().getTargetLowering();
2641   auto Flags = TLI.getAtomicMemOperandFlags(I, *DL);
2642 
2643   Type *ResType = I.getType();
2644 
2645   Register Res = getOrCreateVReg(I);
2646   Register Addr = getOrCreateVReg(*I.getPointerOperand());
2647   Register Val = getOrCreateVReg(*I.getValOperand());
2648 
2649   unsigned Opcode = 0;
2650   switch (I.getOperation()) {
2651   default:
2652     return false;
2653   case AtomicRMWInst::Xchg:
2654     Opcode = TargetOpcode::G_ATOMICRMW_XCHG;
2655     break;
2656   case AtomicRMWInst::Add:
2657     Opcode = TargetOpcode::G_ATOMICRMW_ADD;
2658     break;
2659   case AtomicRMWInst::Sub:
2660     Opcode = TargetOpcode::G_ATOMICRMW_SUB;
2661     break;
2662   case AtomicRMWInst::And:
2663     Opcode = TargetOpcode::G_ATOMICRMW_AND;
2664     break;
2665   case AtomicRMWInst::Nand:
2666     Opcode = TargetOpcode::G_ATOMICRMW_NAND;
2667     break;
2668   case AtomicRMWInst::Or:
2669     Opcode = TargetOpcode::G_ATOMICRMW_OR;
2670     break;
2671   case AtomicRMWInst::Xor:
2672     Opcode = TargetOpcode::G_ATOMICRMW_XOR;
2673     break;
2674   case AtomicRMWInst::Max:
2675     Opcode = TargetOpcode::G_ATOMICRMW_MAX;
2676     break;
2677   case AtomicRMWInst::Min:
2678     Opcode = TargetOpcode::G_ATOMICRMW_MIN;
2679     break;
2680   case AtomicRMWInst::UMax:
2681     Opcode = TargetOpcode::G_ATOMICRMW_UMAX;
2682     break;
2683   case AtomicRMWInst::UMin:
2684     Opcode = TargetOpcode::G_ATOMICRMW_UMIN;
2685     break;
2686   case AtomicRMWInst::FAdd:
2687     Opcode = TargetOpcode::G_ATOMICRMW_FADD;
2688     break;
2689   case AtomicRMWInst::FSub:
2690     Opcode = TargetOpcode::G_ATOMICRMW_FSUB;
2691     break;
2692   }
2693 
2694   AAMDNodes AAMetadata;
2695   I.getAAMetadata(AAMetadata);
2696 
2697   MIRBuilder.buildAtomicRMW(
2698       Opcode, Res, Addr, Val,
2699       *MF->getMachineMemOperand(MachinePointerInfo(I.getPointerOperand()),
2700                                 Flags, DL->getTypeStoreSize(ResType),
2701                                 getMemOpAlign(I), AAMetadata, nullptr,
2702                                 I.getSyncScopeID(), I.getOrdering()));
2703   return true;
2704 }
2705 
2706 bool IRTranslator::translateFence(const User &U,
2707                                   MachineIRBuilder &MIRBuilder) {
2708   const FenceInst &Fence = cast<FenceInst>(U);
2709   MIRBuilder.buildFence(static_cast<unsigned>(Fence.getOrdering()),
2710                         Fence.getSyncScopeID());
2711   return true;
2712 }
2713 
2714 bool IRTranslator::translateFreeze(const User &U,
2715                                    MachineIRBuilder &MIRBuilder) {
2716   const ArrayRef<Register> DstRegs = getOrCreateVRegs(U);
2717   const ArrayRef<Register> SrcRegs = getOrCreateVRegs(*U.getOperand(0));
2718 
2719   assert(DstRegs.size() == SrcRegs.size() &&
2720          "Freeze with different source and destination type?");
2721 
2722   for (unsigned I = 0; I < DstRegs.size(); ++I) {
2723     MIRBuilder.buildFreeze(DstRegs[I], SrcRegs[I]);
2724   }
2725 
2726   return true;
2727 }
2728 
2729 void IRTranslator::finishPendingPhis() {
2730 #ifndef NDEBUG
2731   DILocationVerifier Verifier;
2732   GISelObserverWrapper WrapperObserver(&Verifier);
2733   RAIIDelegateInstaller DelInstall(*MF, &WrapperObserver);
2734 #endif // ifndef NDEBUG
2735   for (auto &Phi : PendingPHIs) {
2736     const PHINode *PI = Phi.first;
2737     ArrayRef<MachineInstr *> ComponentPHIs = Phi.second;
2738     MachineBasicBlock *PhiMBB = ComponentPHIs[0]->getParent();
2739     EntryBuilder->setDebugLoc(PI->getDebugLoc());
2740 #ifndef NDEBUG
2741     Verifier.setCurrentInst(PI);
2742 #endif // ifndef NDEBUG
2743 
2744     SmallSet<const MachineBasicBlock *, 16> SeenPreds;
2745     for (unsigned i = 0; i < PI->getNumIncomingValues(); ++i) {
2746       auto IRPred = PI->getIncomingBlock(i);
2747       ArrayRef<Register> ValRegs = getOrCreateVRegs(*PI->getIncomingValue(i));
2748       for (auto Pred : getMachinePredBBs({IRPred, PI->getParent()})) {
2749         if (SeenPreds.count(Pred) || !PhiMBB->isPredecessor(Pred))
2750           continue;
2751         SeenPreds.insert(Pred);
2752         for (unsigned j = 0; j < ValRegs.size(); ++j) {
2753           MachineInstrBuilder MIB(*MF, ComponentPHIs[j]);
2754           MIB.addUse(ValRegs[j]);
2755           MIB.addMBB(Pred);
2756         }
2757       }
2758     }
2759   }
2760 }
2761 
2762 bool IRTranslator::valueIsSplit(const Value &V,
2763                                 SmallVectorImpl<uint64_t> *Offsets) {
2764   SmallVector<LLT, 4> SplitTys;
2765   if (Offsets && !Offsets->empty())
2766     Offsets->clear();
2767   computeValueLLTs(*DL, *V.getType(), SplitTys, Offsets);
2768   return SplitTys.size() > 1;
2769 }
2770 
2771 bool IRTranslator::translate(const Instruction &Inst) {
2772   CurBuilder->setDebugLoc(Inst.getDebugLoc());
2773   // We only emit constants into the entry block from here. To prevent jumpy
2774   // debug behaviour set the line to 0.
2775   if (const DebugLoc &DL = Inst.getDebugLoc())
2776     EntryBuilder->setDebugLoc(
2777         DebugLoc::get(0, 0, DL.getScope(), DL.getInlinedAt()));
2778   else
2779     EntryBuilder->setDebugLoc(DebugLoc());
2780 
2781   auto &TLI = *MF->getSubtarget().getTargetLowering();
2782   if (TLI.fallBackToDAGISel(Inst))
2783     return false;
2784 
2785   switch (Inst.getOpcode()) {
2786 #define HANDLE_INST(NUM, OPCODE, CLASS)                                        \
2787   case Instruction::OPCODE:                                                    \
2788     return translate##OPCODE(Inst, *CurBuilder.get());
2789 #include "llvm/IR/Instruction.def"
2790   default:
2791     return false;
2792   }
2793 }
2794 
2795 bool IRTranslator::translate(const Constant &C, Register Reg) {
2796   if (auto CI = dyn_cast<ConstantInt>(&C))
2797     EntryBuilder->buildConstant(Reg, *CI);
2798   else if (auto CF = dyn_cast<ConstantFP>(&C))
2799     EntryBuilder->buildFConstant(Reg, *CF);
2800   else if (isa<UndefValue>(C))
2801     EntryBuilder->buildUndef(Reg);
2802   else if (isa<ConstantPointerNull>(C))
2803     EntryBuilder->buildConstant(Reg, 0);
2804   else if (auto GV = dyn_cast<GlobalValue>(&C))
2805     EntryBuilder->buildGlobalValue(Reg, GV);
2806   else if (auto CAZ = dyn_cast<ConstantAggregateZero>(&C)) {
2807     if (!CAZ->getType()->isVectorTy())
2808       return false;
2809     // Return the scalar if it is a <1 x Ty> vector.
2810     if (CAZ->getNumElements() == 1)
2811       return translateCopy(C, *CAZ->getElementValue(0u), *EntryBuilder.get());
2812     SmallVector<Register, 4> Ops;
2813     for (unsigned i = 0; i < CAZ->getNumElements(); ++i) {
2814       Constant &Elt = *CAZ->getElementValue(i);
2815       Ops.push_back(getOrCreateVReg(Elt));
2816     }
2817     EntryBuilder->buildBuildVector(Reg, Ops);
2818   } else if (auto CV = dyn_cast<ConstantDataVector>(&C)) {
2819     // Return the scalar if it is a <1 x Ty> vector.
2820     if (CV->getNumElements() == 1)
2821       return translateCopy(C, *CV->getElementAsConstant(0),
2822                            *EntryBuilder.get());
2823     SmallVector<Register, 4> Ops;
2824     for (unsigned i = 0; i < CV->getNumElements(); ++i) {
2825       Constant &Elt = *CV->getElementAsConstant(i);
2826       Ops.push_back(getOrCreateVReg(Elt));
2827     }
2828     EntryBuilder->buildBuildVector(Reg, Ops);
2829   } else if (auto CE = dyn_cast<ConstantExpr>(&C)) {
2830     switch(CE->getOpcode()) {
2831 #define HANDLE_INST(NUM, OPCODE, CLASS)                                        \
2832   case Instruction::OPCODE:                                                    \
2833     return translate##OPCODE(*CE, *EntryBuilder.get());
2834 #include "llvm/IR/Instruction.def"
2835     default:
2836       return false;
2837     }
2838   } else if (auto CV = dyn_cast<ConstantVector>(&C)) {
2839     if (CV->getNumOperands() == 1)
2840       return translateCopy(C, *CV->getOperand(0), *EntryBuilder.get());
2841     SmallVector<Register, 4> Ops;
2842     for (unsigned i = 0; i < CV->getNumOperands(); ++i) {
2843       Ops.push_back(getOrCreateVReg(*CV->getOperand(i)));
2844     }
2845     EntryBuilder->buildBuildVector(Reg, Ops);
2846   } else if (auto *BA = dyn_cast<BlockAddress>(&C)) {
2847     EntryBuilder->buildBlockAddress(Reg, BA);
2848   } else
2849     return false;
2850 
2851   return true;
2852 }
2853 
2854 void IRTranslator::finalizeBasicBlock() {
2855   for (auto &BTB : SL->BitTestCases) {
2856     // Emit header first, if it wasn't already emitted.
2857     if (!BTB.Emitted)
2858       emitBitTestHeader(BTB, BTB.Parent);
2859 
2860     BranchProbability UnhandledProb = BTB.Prob;
2861     for (unsigned j = 0, ej = BTB.Cases.size(); j != ej; ++j) {
2862       UnhandledProb -= BTB.Cases[j].ExtraProb;
2863       // Set the current basic block to the mbb we wish to insert the code into
2864       MachineBasicBlock *MBB = BTB.Cases[j].ThisBB;
2865       // If all cases cover a contiguous range, it is not necessary to jump to
2866       // the default block after the last bit test fails. This is because the
2867       // range check during bit test header creation has guaranteed that every
2868       // case here doesn't go outside the range. In this case, there is no need
2869       // to perform the last bit test, as it will always be true. Instead, make
2870       // the second-to-last bit-test fall through to the target of the last bit
2871       // test, and delete the last bit test.
2872 
2873       MachineBasicBlock *NextMBB;
2874       if (BTB.ContiguousRange && j + 2 == ej) {
2875         // Second-to-last bit-test with contiguous range: fall through to the
2876         // target of the final bit test.
2877         NextMBB = BTB.Cases[j + 1].TargetBB;
2878       } else if (j + 1 == ej) {
2879         // For the last bit test, fall through to Default.
2880         NextMBB = BTB.Default;
2881       } else {
2882         // Otherwise, fall through to the next bit test.
2883         NextMBB = BTB.Cases[j + 1].ThisBB;
2884       }
2885 
2886       emitBitTestCase(BTB, NextMBB, UnhandledProb, BTB.Reg, BTB.Cases[j], MBB);
2887 
2888       // FIXME delete this block below?
2889       if (BTB.ContiguousRange && j + 2 == ej) {
2890         // Since we're not going to use the final bit test, remove it.
2891         BTB.Cases.pop_back();
2892         break;
2893       }
2894     }
2895     // This is "default" BB. We have two jumps to it. From "header" BB and from
2896     // last "case" BB, unless the latter was skipped.
2897     CFGEdge HeaderToDefaultEdge = {BTB.Parent->getBasicBlock(),
2898                                    BTB.Default->getBasicBlock()};
2899     addMachineCFGPred(HeaderToDefaultEdge, BTB.Parent);
2900     if (!BTB.ContiguousRange) {
2901       addMachineCFGPred(HeaderToDefaultEdge, BTB.Cases.back().ThisBB);
2902     }
2903   }
2904   SL->BitTestCases.clear();
2905 
2906   for (auto &JTCase : SL->JTCases) {
2907     // Emit header first, if it wasn't already emitted.
2908     if (!JTCase.first.Emitted)
2909       emitJumpTableHeader(JTCase.second, JTCase.first, JTCase.first.HeaderBB);
2910 
2911     emitJumpTable(JTCase.second, JTCase.second.MBB);
2912   }
2913   SL->JTCases.clear();
2914 
2915   for (auto &SwCase : SL->SwitchCases)
2916     emitSwitchCase(SwCase, &CurBuilder->getMBB(), *CurBuilder);
2917   SL->SwitchCases.clear();
2918 }
2919 
2920 void IRTranslator::finalizeFunction() {
2921   // Release the memory used by the different maps we
2922   // needed during the translation.
2923   PendingPHIs.clear();
2924   VMap.reset();
2925   FrameIndices.clear();
2926   MachinePreds.clear();
2927   // MachineIRBuilder::DebugLoc can outlive the DILocation it holds. Clear it
2928   // to avoid accessing free’d memory (in runOnMachineFunction) and to avoid
2929   // destroying it twice (in ~IRTranslator() and ~LLVMContext())
2930   EntryBuilder.reset();
2931   CurBuilder.reset();
2932   FuncInfo.clear();
2933 }
2934 
2935 /// Returns true if a BasicBlock \p BB within a variadic function contains a
2936 /// variadic musttail call.
2937 static bool checkForMustTailInVarArgFn(bool IsVarArg, const BasicBlock &BB) {
2938   if (!IsVarArg)
2939     return false;
2940 
2941   // Walk the block backwards, because tail calls usually only appear at the end
2942   // of a block.
2943   return std::any_of(BB.rbegin(), BB.rend(), [](const Instruction &I) {
2944     const auto *CI = dyn_cast<CallInst>(&I);
2945     return CI && CI->isMustTailCall();
2946   });
2947 }
2948 
2949 bool IRTranslator::runOnMachineFunction(MachineFunction &CurMF) {
2950   MF = &CurMF;
2951   const Function &F = MF->getFunction();
2952   if (F.empty())
2953     return false;
2954   GISelCSEAnalysisWrapper &Wrapper =
2955       getAnalysis<GISelCSEAnalysisWrapperPass>().getCSEWrapper();
2956   // Set the CSEConfig and run the analysis.
2957   GISelCSEInfo *CSEInfo = nullptr;
2958   TPC = &getAnalysis<TargetPassConfig>();
2959   bool EnableCSE = EnableCSEInIRTranslator.getNumOccurrences()
2960                        ? EnableCSEInIRTranslator
2961                        : TPC->isGISelCSEEnabled();
2962 
2963   if (EnableCSE) {
2964     EntryBuilder = std::make_unique<CSEMIRBuilder>(CurMF);
2965     CSEInfo = &Wrapper.get(TPC->getCSEConfig());
2966     EntryBuilder->setCSEInfo(CSEInfo);
2967     CurBuilder = std::make_unique<CSEMIRBuilder>(CurMF);
2968     CurBuilder->setCSEInfo(CSEInfo);
2969   } else {
2970     EntryBuilder = std::make_unique<MachineIRBuilder>();
2971     CurBuilder = std::make_unique<MachineIRBuilder>();
2972   }
2973   CLI = MF->getSubtarget().getCallLowering();
2974   CurBuilder->setMF(*MF);
2975   EntryBuilder->setMF(*MF);
2976   MRI = &MF->getRegInfo();
2977   DL = &F.getParent()->getDataLayout();
2978   ORE = std::make_unique<OptimizationRemarkEmitter>(&F);
2979   const TargetMachine &TM = MF->getTarget();
2980   TM.resetTargetOptions(F);
2981   EnableOpts = OptLevel != CodeGenOpt::None && !skipFunction(F);
2982   FuncInfo.MF = MF;
2983   if (EnableOpts)
2984     FuncInfo.BPI = &getAnalysis<BranchProbabilityInfoWrapperPass>().getBPI();
2985   else
2986     FuncInfo.BPI = nullptr;
2987 
2988   const auto &TLI = *MF->getSubtarget().getTargetLowering();
2989 
2990   SL = std::make_unique<GISelSwitchLowering>(this, FuncInfo);
2991   SL->init(TLI, TM, *DL);
2992 
2993 
2994 
2995   assert(PendingPHIs.empty() && "stale PHIs");
2996 
2997   if (!DL->isLittleEndian()) {
2998     // Currently we don't properly handle big endian code.
2999     OptimizationRemarkMissed R("gisel-irtranslator", "GISelFailure",
3000                                F.getSubprogram(), &F.getEntryBlock());
3001     R << "unable to translate in big endian mode";
3002     reportTranslationError(*MF, *TPC, *ORE, R);
3003   }
3004 
3005   // Release the per-function state when we return, whether we succeeded or not.
3006   auto FinalizeOnReturn = make_scope_exit([this]() { finalizeFunction(); });
3007 
3008   // Setup a separate basic-block for the arguments and constants
3009   MachineBasicBlock *EntryBB = MF->CreateMachineBasicBlock();
3010   MF->push_back(EntryBB);
3011   EntryBuilder->setMBB(*EntryBB);
3012 
3013   DebugLoc DbgLoc = F.getEntryBlock().getFirstNonPHI()->getDebugLoc();
3014   SwiftError.setFunction(CurMF);
3015   SwiftError.createEntriesInEntryBlock(DbgLoc);
3016 
3017   bool IsVarArg = F.isVarArg();
3018   bool HasMustTailInVarArgFn = false;
3019 
3020   // Create all blocks, in IR order, to preserve the layout.
3021   for (const BasicBlock &BB: F) {
3022     auto *&MBB = BBToMBB[&BB];
3023 
3024     MBB = MF->CreateMachineBasicBlock(&BB);
3025     MF->push_back(MBB);
3026 
3027     if (BB.hasAddressTaken())
3028       MBB->setHasAddressTaken();
3029 
3030     if (!HasMustTailInVarArgFn)
3031       HasMustTailInVarArgFn = checkForMustTailInVarArgFn(IsVarArg, BB);
3032   }
3033 
3034   MF->getFrameInfo().setHasMustTailInVarArgFunc(HasMustTailInVarArgFn);
3035 
3036   // Make our arguments/constants entry block fallthrough to the IR entry block.
3037   EntryBB->addSuccessor(&getMBB(F.front()));
3038 
3039   if (CLI->fallBackToDAGISel(F)) {
3040     OptimizationRemarkMissed R("gisel-irtranslator", "GISelFailure",
3041                                F.getSubprogram(), &F.getEntryBlock());
3042     R << "unable to lower function: " << ore::NV("Prototype", F.getType());
3043     reportTranslationError(*MF, *TPC, *ORE, R);
3044     return false;
3045   }
3046 
3047   // Lower the actual args into this basic block.
3048   SmallVector<ArrayRef<Register>, 8> VRegArgs;
3049   for (const Argument &Arg: F.args()) {
3050     if (DL->getTypeStoreSize(Arg.getType()).isZero())
3051       continue; // Don't handle zero sized types.
3052     ArrayRef<Register> VRegs = getOrCreateVRegs(Arg);
3053     VRegArgs.push_back(VRegs);
3054 
3055     if (Arg.hasSwiftErrorAttr()) {
3056       assert(VRegs.size() == 1 && "Too many vregs for Swift error");
3057       SwiftError.setCurrentVReg(EntryBB, SwiftError.getFunctionArg(), VRegs[0]);
3058     }
3059   }
3060 
3061   if (!CLI->lowerFormalArguments(*EntryBuilder.get(), F, VRegArgs)) {
3062     OptimizationRemarkMissed R("gisel-irtranslator", "GISelFailure",
3063                                F.getSubprogram(), &F.getEntryBlock());
3064     R << "unable to lower arguments: " << ore::NV("Prototype", F.getType());
3065     reportTranslationError(*MF, *TPC, *ORE, R);
3066     return false;
3067   }
3068 
3069   // Need to visit defs before uses when translating instructions.
3070   GISelObserverWrapper WrapperObserver;
3071   if (EnableCSE && CSEInfo)
3072     WrapperObserver.addObserver(CSEInfo);
3073   {
3074     ReversePostOrderTraversal<const Function *> RPOT(&F);
3075 #ifndef NDEBUG
3076     DILocationVerifier Verifier;
3077     WrapperObserver.addObserver(&Verifier);
3078 #endif // ifndef NDEBUG
3079     RAIIDelegateInstaller DelInstall(*MF, &WrapperObserver);
3080     RAIIMFObserverInstaller ObsInstall(*MF, WrapperObserver);
3081     for (const BasicBlock *BB : RPOT) {
3082       MachineBasicBlock &MBB = getMBB(*BB);
3083       // Set the insertion point of all the following translations to
3084       // the end of this basic block.
3085       CurBuilder->setMBB(MBB);
3086       HasTailCall = false;
3087       for (const Instruction &Inst : *BB) {
3088         // If we translated a tail call in the last step, then we know
3089         // everything after the call is either a return, or something that is
3090         // handled by the call itself. (E.g. a lifetime marker or assume
3091         // intrinsic.) In this case, we should stop translating the block and
3092         // move on.
3093         if (HasTailCall)
3094           break;
3095 #ifndef NDEBUG
3096         Verifier.setCurrentInst(&Inst);
3097 #endif // ifndef NDEBUG
3098         if (translate(Inst))
3099           continue;
3100 
3101         OptimizationRemarkMissed R("gisel-irtranslator", "GISelFailure",
3102                                    Inst.getDebugLoc(), BB);
3103         R << "unable to translate instruction: " << ore::NV("Opcode", &Inst);
3104 
3105         if (ORE->allowExtraAnalysis("gisel-irtranslator")) {
3106           std::string InstStrStorage;
3107           raw_string_ostream InstStr(InstStrStorage);
3108           InstStr << Inst;
3109 
3110           R << ": '" << InstStr.str() << "'";
3111         }
3112 
3113         reportTranslationError(*MF, *TPC, *ORE, R);
3114         return false;
3115       }
3116 
3117       finalizeBasicBlock();
3118     }
3119 #ifndef NDEBUG
3120     WrapperObserver.removeObserver(&Verifier);
3121 #endif
3122   }
3123 
3124   finishPendingPhis();
3125 
3126   SwiftError.propagateVRegs();
3127 
3128   // Merge the argument lowering and constants block with its single
3129   // successor, the LLVM-IR entry block.  We want the basic block to
3130   // be maximal.
3131   assert(EntryBB->succ_size() == 1 &&
3132          "Custom BB used for lowering should have only one successor");
3133   // Get the successor of the current entry block.
3134   MachineBasicBlock &NewEntryBB = **EntryBB->succ_begin();
3135   assert(NewEntryBB.pred_size() == 1 &&
3136          "LLVM-IR entry block has a predecessor!?");
3137   // Move all the instruction from the current entry block to the
3138   // new entry block.
3139   NewEntryBB.splice(NewEntryBB.begin(), EntryBB, EntryBB->begin(),
3140                     EntryBB->end());
3141 
3142   // Update the live-in information for the new entry block.
3143   for (const MachineBasicBlock::RegisterMaskPair &LiveIn : EntryBB->liveins())
3144     NewEntryBB.addLiveIn(LiveIn);
3145   NewEntryBB.sortUniqueLiveIns();
3146 
3147   // Get rid of the now empty basic block.
3148   EntryBB->removeSuccessor(&NewEntryBB);
3149   MF->remove(EntryBB);
3150   MF->DeleteMachineBasicBlock(EntryBB);
3151 
3152   assert(&MF->front() == &NewEntryBB &&
3153          "New entry wasn't next in the list of basic block!");
3154 
3155   // Initialize stack protector information.
3156   StackProtector &SP = getAnalysis<StackProtector>();
3157   SP.copyToMachineFrameInfo(MF->getFrameInfo());
3158 
3159   return false;
3160 }
3161