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