LLVM API Documentation

FastISel.cpp
Go to the documentation of this file.
00001 //===-- FastISel.cpp - Implementation of the FastISel class ---------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the implementation of the FastISel class.
00011 //
00012 // "Fast" instruction selection is designed to emit very poor code quickly.
00013 // Also, it is not designed to be able to do much lowering, so most illegal
00014 // types (e.g. i64 on 32-bit targets) and operations are not supported.  It is
00015 // also not intended to be able to do much optimization, except in a few cases
00016 // where doing optimizations reduces overall compile time.  For example, folding
00017 // constants into immediate fields is often done, because it's cheap and it
00018 // reduces the number of instructions later phases have to examine.
00019 //
00020 // "Fast" instruction selection is able to fail gracefully and transfer
00021 // control to the SelectionDAG selector for operations that it doesn't
00022 // support.  In many cases, this allows us to avoid duplicating a lot of
00023 // the complicated lowering logic that SelectionDAG currently has.
00024 //
00025 // The intended use for "fast" instruction selection is "-O0" mode
00026 // compilation, where the quality of the generated code is irrelevant when
00027 // weighed against the speed at which the code can be generated.  Also,
00028 // at -O0, the LLVM optimizers are not running, and this makes the
00029 // compile time of codegen a much higher portion of the overall compile
00030 // time.  Despite its limitations, "fast" instruction selection is able to
00031 // handle enough code on its own to provide noticeable overall speedups
00032 // in -O0 compiles.
00033 //
00034 // Basic operations are supported in a target-independent way, by reading
00035 // the same instruction descriptions that the SelectionDAG selector reads,
00036 // and identifying simple arithmetic operations that can be directly selected
00037 // from simple operators.  More complicated operations currently require
00038 // target-specific code.
00039 //
00040 //===----------------------------------------------------------------------===//
00041 
00042 #include "llvm/CodeGen/Analysis.h"
00043 #include "llvm/CodeGen/FastISel.h"
00044 #include "llvm/ADT/Optional.h"
00045 #include "llvm/ADT/Statistic.h"
00046 #include "llvm/Analysis/BranchProbabilityInfo.h"
00047 #include "llvm/Analysis/Loads.h"
00048 #include "llvm/CodeGen/Analysis.h"
00049 #include "llvm/CodeGen/FunctionLoweringInfo.h"
00050 #include "llvm/CodeGen/MachineFrameInfo.h"
00051 #include "llvm/CodeGen/MachineInstrBuilder.h"
00052 #include "llvm/CodeGen/MachineModuleInfo.h"
00053 #include "llvm/CodeGen/MachineRegisterInfo.h"
00054 #include "llvm/CodeGen/StackMaps.h"
00055 #include "llvm/IR/DataLayout.h"
00056 #include "llvm/IR/DebugInfo.h"
00057 #include "llvm/IR/Function.h"
00058 #include "llvm/IR/GlobalVariable.h"
00059 #include "llvm/IR/Instructions.h"
00060 #include "llvm/IR/IntrinsicInst.h"
00061 #include "llvm/IR/Operator.h"
00062 #include "llvm/Support/Debug.h"
00063 #include "llvm/Support/ErrorHandling.h"
00064 #include "llvm/Target/TargetInstrInfo.h"
00065 #include "llvm/Target/TargetLibraryInfo.h"
00066 #include "llvm/Target/TargetLowering.h"
00067 #include "llvm/Target/TargetMachine.h"
00068 #include "llvm/Target/TargetSubtargetInfo.h"
00069 using namespace llvm;
00070 
00071 #define DEBUG_TYPE "isel"
00072 
00073 STATISTIC(NumFastIselSuccessIndependent, "Number of insts selected by "
00074                                          "target-independent selector");
00075 STATISTIC(NumFastIselSuccessTarget, "Number of insts selected by "
00076                                     "target-specific selector");
00077 STATISTIC(NumFastIselDead, "Number of dead insts removed on failure");
00078 
00079 void FastISel::ArgListEntry::setAttributes(ImmutableCallSite *CS,
00080                                            unsigned AttrIdx) {
00081   IsSExt = CS->paramHasAttr(AttrIdx, Attribute::SExt);
00082   IsZExt = CS->paramHasAttr(AttrIdx, Attribute::ZExt);
00083   IsInReg = CS->paramHasAttr(AttrIdx, Attribute::InReg);
00084   IsSRet = CS->paramHasAttr(AttrIdx, Attribute::StructRet);
00085   IsNest = CS->paramHasAttr(AttrIdx, Attribute::Nest);
00086   IsByVal = CS->paramHasAttr(AttrIdx, Attribute::ByVal);
00087   IsInAlloca = CS->paramHasAttr(AttrIdx, Attribute::InAlloca);
00088   IsReturned = CS->paramHasAttr(AttrIdx, Attribute::Returned);
00089   Alignment = CS->getParamAlignment(AttrIdx);
00090 }
00091 
00092 /// Set the current block to which generated machine instructions will be
00093 /// appended, and clear the local CSE map.
00094 void FastISel::startNewBlock() {
00095   LocalValueMap.clear();
00096 
00097   // Instructions are appended to FuncInfo.MBB. If the basic block already
00098   // contains labels or copies, use the last instruction as the last local
00099   // value.
00100   EmitStartPt = nullptr;
00101   if (!FuncInfo.MBB->empty())
00102     EmitStartPt = &FuncInfo.MBB->back();
00103   LastLocalValue = EmitStartPt;
00104 }
00105 
00106 bool FastISel::lowerArguments() {
00107   if (!FuncInfo.CanLowerReturn)
00108     // Fallback to SDISel argument lowering code to deal with sret pointer
00109     // parameter.
00110     return false;
00111 
00112   if (!fastLowerArguments())
00113     return false;
00114 
00115   // Enter arguments into ValueMap for uses in non-entry BBs.
00116   for (Function::const_arg_iterator I = FuncInfo.Fn->arg_begin(),
00117                                     E = FuncInfo.Fn->arg_end();
00118        I != E; ++I) {
00119     DenseMap<const Value *, unsigned>::iterator VI = LocalValueMap.find(I);
00120     assert(VI != LocalValueMap.end() && "Missed an argument?");
00121     FuncInfo.ValueMap[I] = VI->second;
00122   }
00123   return true;
00124 }
00125 
00126 void FastISel::flushLocalValueMap() {
00127   LocalValueMap.clear();
00128   LastLocalValue = EmitStartPt;
00129   recomputeInsertPt();
00130   SavedInsertPt = FuncInfo.InsertPt;
00131 }
00132 
00133 bool FastISel::hasTrivialKill(const Value *V) {
00134   // Don't consider constants or arguments to have trivial kills.
00135   const Instruction *I = dyn_cast<Instruction>(V);
00136   if (!I)
00137     return false;
00138 
00139   // No-op casts are trivially coalesced by fast-isel.
00140   if (const auto *Cast = dyn_cast<CastInst>(I))
00141     if (Cast->isNoopCast(DL.getIntPtrType(Cast->getContext())) &&
00142         !hasTrivialKill(Cast->getOperand(0)))
00143       return false;
00144 
00145   // Even the value might have only one use in the LLVM IR, it is possible that
00146   // FastISel might fold the use into another instruction and now there is more
00147   // than one use at the Machine Instruction level.
00148   unsigned Reg = lookUpRegForValue(V);
00149   if (Reg && !MRI.use_empty(Reg))
00150     return false;
00151 
00152   // GEPs with all zero indices are trivially coalesced by fast-isel.
00153   if (const auto *GEP = dyn_cast<GetElementPtrInst>(I))
00154     if (GEP->hasAllZeroIndices() && !hasTrivialKill(GEP->getOperand(0)))
00155       return false;
00156 
00157   // Only instructions with a single use in the same basic block are considered
00158   // to have trivial kills.
00159   return I->hasOneUse() &&
00160          !(I->getOpcode() == Instruction::BitCast ||
00161            I->getOpcode() == Instruction::PtrToInt ||
00162            I->getOpcode() == Instruction::IntToPtr) &&
00163          cast<Instruction>(*I->user_begin())->getParent() == I->getParent();
00164 }
00165 
00166 unsigned FastISel::getRegForValue(const Value *V) {
00167   EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
00168   // Don't handle non-simple values in FastISel.
00169   if (!RealVT.isSimple())
00170     return 0;
00171 
00172   // Ignore illegal types. We must do this before looking up the value
00173   // in ValueMap because Arguments are given virtual registers regardless
00174   // of whether FastISel can handle them.
00175   MVT VT = RealVT.getSimpleVT();
00176   if (!TLI.isTypeLegal(VT)) {
00177     // Handle integer promotions, though, because they're common and easy.
00178     if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
00179       VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
00180     else
00181       return 0;
00182   }
00183 
00184   // Look up the value to see if we already have a register for it.
00185   unsigned Reg = lookUpRegForValue(V);
00186   if (Reg)
00187     return Reg;
00188 
00189   // In bottom-up mode, just create the virtual register which will be used
00190   // to hold the value. It will be materialized later.
00191   if (isa<Instruction>(V) &&
00192       (!isa<AllocaInst>(V) ||
00193        !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(V))))
00194     return FuncInfo.InitializeRegForValue(V);
00195 
00196   SavePoint SaveInsertPt = enterLocalValueArea();
00197 
00198   // Materialize the value in a register. Emit any instructions in the
00199   // local value area.
00200   Reg = materializeRegForValue(V, VT);
00201 
00202   leaveLocalValueArea(SaveInsertPt);
00203 
00204   return Reg;
00205 }
00206 
00207 unsigned FastISel::materializeConstant(const Value *V, MVT VT) {
00208   unsigned Reg = 0;
00209   if (const auto *CI = dyn_cast<ConstantInt>(V)) {
00210     if (CI->getValue().getActiveBits() <= 64)
00211       Reg = fastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
00212   } else if (isa<AllocaInst>(V))
00213     Reg = fastMaterializeAlloca(cast<AllocaInst>(V));
00214   else if (isa<ConstantPointerNull>(V))
00215     // Translate this as an integer zero so that it can be
00216     // local-CSE'd with actual integer zeros.
00217     Reg = getRegForValue(
00218         Constant::getNullValue(DL.getIntPtrType(V->getContext())));
00219   else if (const auto *CF = dyn_cast<ConstantFP>(V)) {
00220     if (CF->isNullValue())
00221       Reg = fastMaterializeFloatZero(CF);
00222     else
00223       // Try to emit the constant directly.
00224       Reg = fastEmit_f(VT, VT, ISD::ConstantFP, CF);
00225 
00226     if (!Reg) {
00227       // Try to emit the constant by using an integer constant with a cast.
00228       const APFloat &Flt = CF->getValueAPF();
00229       EVT IntVT = TLI.getPointerTy();
00230 
00231       uint64_t x[2];
00232       uint32_t IntBitWidth = IntVT.getSizeInBits();
00233       bool isExact;
00234       (void)Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
00235                                  APFloat::rmTowardZero, &isExact);
00236       if (isExact) {
00237         APInt IntVal(IntBitWidth, x);
00238 
00239         unsigned IntegerReg =
00240             getRegForValue(ConstantInt::get(V->getContext(), IntVal));
00241         if (IntegerReg != 0)
00242           Reg = fastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg,
00243                            /*Kill=*/false);
00244       }
00245     }
00246   } else if (const auto *Op = dyn_cast<Operator>(V)) {
00247     if (!selectOperator(Op, Op->getOpcode()))
00248       if (!isa<Instruction>(Op) ||
00249           !fastSelectInstruction(cast<Instruction>(Op)))
00250         return 0;
00251     Reg = lookUpRegForValue(Op);
00252   } else if (isa<UndefValue>(V)) {
00253     Reg = createResultReg(TLI.getRegClassFor(VT));
00254     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
00255             TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
00256   }
00257   return Reg;
00258 }
00259 
00260 /// Helper for getRegForValue. This function is called when the value isn't
00261 /// already available in a register and must be materialized with new
00262 /// instructions.
00263 unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) {
00264   unsigned Reg = 0;
00265   // Give the target-specific code a try first.
00266   if (isa<Constant>(V))
00267     Reg = fastMaterializeConstant(cast<Constant>(V));
00268 
00269   // If target-specific code couldn't or didn't want to handle the value, then
00270   // give target-independent code a try.
00271   if (!Reg)
00272     Reg = materializeConstant(V, VT);
00273 
00274   // Don't cache constant materializations in the general ValueMap.
00275   // To do so would require tracking what uses they dominate.
00276   if (Reg) {
00277     LocalValueMap[V] = Reg;
00278     LastLocalValue = MRI.getVRegDef(Reg);
00279   }
00280   return Reg;
00281 }
00282 
00283 unsigned FastISel::lookUpRegForValue(const Value *V) {
00284   // Look up the value to see if we already have a register for it. We
00285   // cache values defined by Instructions across blocks, and other values
00286   // only locally. This is because Instructions already have the SSA
00287   // def-dominates-use requirement enforced.
00288   DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
00289   if (I != FuncInfo.ValueMap.end())
00290     return I->second;
00291   return LocalValueMap[V];
00292 }
00293 
00294 void FastISel::updateValueMap(const Value *I, unsigned Reg, unsigned NumRegs) {
00295   if (!isa<Instruction>(I)) {
00296     LocalValueMap[I] = Reg;
00297     return;
00298   }
00299 
00300   unsigned &AssignedReg = FuncInfo.ValueMap[I];
00301   if (AssignedReg == 0)
00302     // Use the new register.
00303     AssignedReg = Reg;
00304   else if (Reg != AssignedReg) {
00305     // Arrange for uses of AssignedReg to be replaced by uses of Reg.
00306     for (unsigned i = 0; i < NumRegs; i++)
00307       FuncInfo.RegFixups[AssignedReg + i] = Reg + i;
00308 
00309     AssignedReg = Reg;
00310   }
00311 }
00312 
00313 std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
00314   unsigned IdxN = getRegForValue(Idx);
00315   if (IdxN == 0)
00316     // Unhandled operand. Halt "fast" selection and bail.
00317     return std::pair<unsigned, bool>(0, false);
00318 
00319   bool IdxNIsKill = hasTrivialKill(Idx);
00320 
00321   // If the index is smaller or larger than intptr_t, truncate or extend it.
00322   MVT PtrVT = TLI.getPointerTy();
00323   EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
00324   if (IdxVT.bitsLT(PtrVT)) {
00325     IdxN = fastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND, IdxN,
00326                       IdxNIsKill);
00327     IdxNIsKill = true;
00328   } else if (IdxVT.bitsGT(PtrVT)) {
00329     IdxN =
00330         fastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE, IdxN, IdxNIsKill);
00331     IdxNIsKill = true;
00332   }
00333   return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
00334 }
00335 
00336 void FastISel::recomputeInsertPt() {
00337   if (getLastLocalValue()) {
00338     FuncInfo.InsertPt = getLastLocalValue();
00339     FuncInfo.MBB = FuncInfo.InsertPt->getParent();
00340     ++FuncInfo.InsertPt;
00341   } else
00342     FuncInfo.InsertPt = FuncInfo.MBB->getFirstNonPHI();
00343 
00344   // Now skip past any EH_LABELs, which must remain at the beginning.
00345   while (FuncInfo.InsertPt != FuncInfo.MBB->end() &&
00346          FuncInfo.InsertPt->getOpcode() == TargetOpcode::EH_LABEL)
00347     ++FuncInfo.InsertPt;
00348 }
00349 
00350 void FastISel::removeDeadCode(MachineBasicBlock::iterator I,
00351                               MachineBasicBlock::iterator E) {
00352   assert(I && E && std::distance(I, E) > 0 && "Invalid iterator!");
00353   while (I != E) {
00354     MachineInstr *Dead = &*I;
00355     ++I;
00356     Dead->eraseFromParent();
00357     ++NumFastIselDead;
00358   }
00359   recomputeInsertPt();
00360 }
00361 
00362 FastISel::SavePoint FastISel::enterLocalValueArea() {
00363   MachineBasicBlock::iterator OldInsertPt = FuncInfo.InsertPt;
00364   DebugLoc OldDL = DbgLoc;
00365   recomputeInsertPt();
00366   DbgLoc = DebugLoc();
00367   SavePoint SP = {OldInsertPt, OldDL};
00368   return SP;
00369 }
00370 
00371 void FastISel::leaveLocalValueArea(SavePoint OldInsertPt) {
00372   if (FuncInfo.InsertPt != FuncInfo.MBB->begin())
00373     LastLocalValue = std::prev(FuncInfo.InsertPt);
00374 
00375   // Restore the previous insert position.
00376   FuncInfo.InsertPt = OldInsertPt.InsertPt;
00377   DbgLoc = OldInsertPt.DL;
00378 }
00379 
00380 bool FastISel::selectBinaryOp(const User *I, unsigned ISDOpcode) {
00381   EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
00382   if (VT == MVT::Other || !VT.isSimple())
00383     // Unhandled type. Halt "fast" selection and bail.
00384     return false;
00385 
00386   // We only handle legal types. For example, on x86-32 the instruction
00387   // selector contains all of the 64-bit instructions from x86-64,
00388   // under the assumption that i64 won't be used if the target doesn't
00389   // support it.
00390   if (!TLI.isTypeLegal(VT)) {
00391     // MVT::i1 is special. Allow AND, OR, or XOR because they
00392     // don't require additional zeroing, which makes them easy.
00393     if (VT == MVT::i1 && (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
00394                           ISDOpcode == ISD::XOR))
00395       VT = TLI.getTypeToTransformTo(I->getContext(), VT);
00396     else
00397       return false;
00398   }
00399 
00400   // Check if the first operand is a constant, and handle it as "ri".  At -O0,
00401   // we don't have anything that canonicalizes operand order.
00402   if (const auto *CI = dyn_cast<ConstantInt>(I->getOperand(0)))
00403     if (isa<Instruction>(I) && cast<Instruction>(I)->isCommutative()) {
00404       unsigned Op1 = getRegForValue(I->getOperand(1));
00405       if (!Op1)
00406         return false;
00407       bool Op1IsKill = hasTrivialKill(I->getOperand(1));
00408 
00409       unsigned ResultReg =
00410           fastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op1, Op1IsKill,
00411                        CI->getZExtValue(), VT.getSimpleVT());
00412       if (!ResultReg)
00413         return false;
00414 
00415       // We successfully emitted code for the given LLVM Instruction.
00416       updateValueMap(I, ResultReg);
00417       return true;
00418     }
00419 
00420   unsigned Op0 = getRegForValue(I->getOperand(0));
00421   if (!Op0) // Unhandled operand. Halt "fast" selection and bail.
00422     return false;
00423   bool Op0IsKill = hasTrivialKill(I->getOperand(0));
00424 
00425   // Check if the second operand is a constant and handle it appropriately.
00426   if (const auto *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
00427     uint64_t Imm = CI->getZExtValue();
00428 
00429     // Transform "sdiv exact X, 8" -> "sra X, 3".
00430     if (ISDOpcode == ISD::SDIV && isa<BinaryOperator>(I) &&
00431         cast<BinaryOperator>(I)->isExact() && isPowerOf2_64(Imm)) {
00432       Imm = Log2_64(Imm);
00433       ISDOpcode = ISD::SRA;
00434     }
00435 
00436     // Transform "urem x, pow2" -> "and x, pow2-1".
00437     if (ISDOpcode == ISD::UREM && isa<BinaryOperator>(I) &&
00438         isPowerOf2_64(Imm)) {
00439       --Imm;
00440       ISDOpcode = ISD::AND;
00441     }
00442 
00443     unsigned ResultReg = fastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0,
00444                                       Op0IsKill, Imm, VT.getSimpleVT());
00445     if (!ResultReg)
00446       return false;
00447 
00448     // We successfully emitted code for the given LLVM Instruction.
00449     updateValueMap(I, ResultReg);
00450     return true;
00451   }
00452 
00453   // Check if the second operand is a constant float.
00454   if (const auto *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
00455     unsigned ResultReg = fastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
00456                                      ISDOpcode, Op0, Op0IsKill, CF);
00457     if (ResultReg) {
00458       // We successfully emitted code for the given LLVM Instruction.
00459       updateValueMap(I, ResultReg);
00460       return true;
00461     }
00462   }
00463 
00464   unsigned Op1 = getRegForValue(I->getOperand(1));
00465   if (!Op1) // Unhandled operand. Halt "fast" selection and bail.
00466     return false;
00467   bool Op1IsKill = hasTrivialKill(I->getOperand(1));
00468 
00469   // Now we have both operands in registers. Emit the instruction.
00470   unsigned ResultReg = fastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
00471                                    ISDOpcode, Op0, Op0IsKill, Op1, Op1IsKill);
00472   if (!ResultReg)
00473     // Target-specific code wasn't able to find a machine opcode for
00474     // the given ISD opcode and type. Halt "fast" selection and bail.
00475     return false;
00476 
00477   // We successfully emitted code for the given LLVM Instruction.
00478   updateValueMap(I, ResultReg);
00479   return true;
00480 }
00481 
00482 bool FastISel::selectGetElementPtr(const User *I) {
00483   unsigned N = getRegForValue(I->getOperand(0));
00484   if (!N) // Unhandled operand. Halt "fast" selection and bail.
00485     return false;
00486   bool NIsKill = hasTrivialKill(I->getOperand(0));
00487 
00488   // Keep a running tab of the total offset to coalesce multiple N = N + Offset
00489   // into a single N = N + TotalOffset.
00490   uint64_t TotalOffs = 0;
00491   // FIXME: What's a good SWAG number for MaxOffs?
00492   uint64_t MaxOffs = 2048;
00493   Type *Ty = I->getOperand(0)->getType();
00494   MVT VT = TLI.getPointerTy();
00495   for (GetElementPtrInst::const_op_iterator OI = I->op_begin() + 1,
00496                                             E = I->op_end();
00497        OI != E; ++OI) {
00498     const Value *Idx = *OI;
00499     if (auto *StTy = dyn_cast<StructType>(Ty)) {
00500       unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
00501       if (Field) {
00502         // N = N + Offset
00503         TotalOffs += DL.getStructLayout(StTy)->getElementOffset(Field);
00504         if (TotalOffs >= MaxOffs) {
00505           N = fastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
00506           if (!N) // Unhandled operand. Halt "fast" selection and bail.
00507             return false;
00508           NIsKill = true;
00509           TotalOffs = 0;
00510         }
00511       }
00512       Ty = StTy->getElementType(Field);
00513     } else {
00514       Ty = cast<SequentialType>(Ty)->getElementType();
00515 
00516       // If this is a constant subscript, handle it quickly.
00517       if (const auto *CI = dyn_cast<ConstantInt>(Idx)) {
00518         if (CI->isZero())
00519           continue;
00520         // N = N + Offset
00521         TotalOffs +=
00522             DL.getTypeAllocSize(Ty) * cast<ConstantInt>(CI)->getSExtValue();
00523         if (TotalOffs >= MaxOffs) {
00524           N = fastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
00525           if (!N) // Unhandled operand. Halt "fast" selection and bail.
00526             return false;
00527           NIsKill = true;
00528           TotalOffs = 0;
00529         }
00530         continue;
00531       }
00532       if (TotalOffs) {
00533         N = fastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
00534         if (!N) // Unhandled operand. Halt "fast" selection and bail.
00535           return false;
00536         NIsKill = true;
00537         TotalOffs = 0;
00538       }
00539 
00540       // N = N + Idx * ElementSize;
00541       uint64_t ElementSize = DL.getTypeAllocSize(Ty);
00542       std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
00543       unsigned IdxN = Pair.first;
00544       bool IdxNIsKill = Pair.second;
00545       if (!IdxN) // Unhandled operand. Halt "fast" selection and bail.
00546         return false;
00547 
00548       if (ElementSize != 1) {
00549         IdxN = fastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
00550         if (!IdxN) // Unhandled operand. Halt "fast" selection and bail.
00551           return false;
00552         IdxNIsKill = true;
00553       }
00554       N = fastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
00555       if (!N) // Unhandled operand. Halt "fast" selection and bail.
00556         return false;
00557     }
00558   }
00559   if (TotalOffs) {
00560     N = fastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
00561     if (!N) // Unhandled operand. Halt "fast" selection and bail.
00562       return false;
00563   }
00564 
00565   // We successfully emitted code for the given LLVM Instruction.
00566   updateValueMap(I, N);
00567   return true;
00568 }
00569 
00570 bool FastISel::addStackMapLiveVars(SmallVectorImpl<MachineOperand> &Ops,
00571                                    const CallInst *CI, unsigned StartIdx) {
00572   for (unsigned i = StartIdx, e = CI->getNumArgOperands(); i != e; ++i) {
00573     Value *Val = CI->getArgOperand(i);
00574     // Check for constants and encode them with a StackMaps::ConstantOp prefix.
00575     if (const auto *C = dyn_cast<ConstantInt>(Val)) {
00576       Ops.push_back(MachineOperand::CreateImm(StackMaps::ConstantOp));
00577       Ops.push_back(MachineOperand::CreateImm(C->getSExtValue()));
00578     } else if (isa<ConstantPointerNull>(Val)) {
00579       Ops.push_back(MachineOperand::CreateImm(StackMaps::ConstantOp));
00580       Ops.push_back(MachineOperand::CreateImm(0));
00581     } else if (auto *AI = dyn_cast<AllocaInst>(Val)) {
00582       // Values coming from a stack location also require a sepcial encoding,
00583       // but that is added later on by the target specific frame index
00584       // elimination implementation.
00585       auto SI = FuncInfo.StaticAllocaMap.find(AI);
00586       if (SI != FuncInfo.StaticAllocaMap.end())
00587         Ops.push_back(MachineOperand::CreateFI(SI->second));
00588       else
00589         return false;
00590     } else {
00591       unsigned Reg = getRegForValue(Val);
00592       if (!Reg)
00593         return false;
00594       Ops.push_back(MachineOperand::CreateReg(Reg, /*IsDef=*/false));
00595     }
00596   }
00597   return true;
00598 }
00599 
00600 bool FastISel::selectStackmap(const CallInst *I) {
00601   // void @llvm.experimental.stackmap(i64 <id>, i32 <numShadowBytes>,
00602   //                                  [live variables...])
00603   assert(I->getCalledFunction()->getReturnType()->isVoidTy() &&
00604          "Stackmap cannot return a value.");
00605 
00606   // The stackmap intrinsic only records the live variables (the arguments
00607   // passed to it) and emits NOPS (if requested). Unlike the patchpoint
00608   // intrinsic, this won't be lowered to a function call. This means we don't
00609   // have to worry about calling conventions and target-specific lowering code.
00610   // Instead we perform the call lowering right here.
00611   //
00612   // CALLSEQ_START(0)
00613   // STACKMAP(id, nbytes, ...)
00614   // CALLSEQ_END(0, 0)
00615   //
00616   SmallVector<MachineOperand, 32> Ops;
00617 
00618   // Add the <id> and <numBytes> constants.
00619   assert(isa<ConstantInt>(I->getOperand(PatchPointOpers::IDPos)) &&
00620          "Expected a constant integer.");
00621   const auto *ID = cast<ConstantInt>(I->getOperand(PatchPointOpers::IDPos));
00622   Ops.push_back(MachineOperand::CreateImm(ID->getZExtValue()));
00623 
00624   assert(isa<ConstantInt>(I->getOperand(PatchPointOpers::NBytesPos)) &&
00625          "Expected a constant integer.");
00626   const auto *NumBytes =
00627       cast<ConstantInt>(I->getOperand(PatchPointOpers::NBytesPos));
00628   Ops.push_back(MachineOperand::CreateImm(NumBytes->getZExtValue()));
00629 
00630   // Push live variables for the stack map (skipping the first two arguments
00631   // <id> and <numBytes>).
00632   if (!addStackMapLiveVars(Ops, I, 2))
00633     return false;
00634 
00635   // We are not adding any register mask info here, because the stackmap doesn't
00636   // clobber anything.
00637 
00638   // Add scratch registers as implicit def and early clobber.
00639   CallingConv::ID CC = I->getCallingConv();
00640   const MCPhysReg *ScratchRegs = TLI.getScratchRegisters(CC);
00641   for (unsigned i = 0; ScratchRegs[i]; ++i)
00642     Ops.push_back(MachineOperand::CreateReg(
00643         ScratchRegs[i], /*IsDef=*/true, /*IsImp=*/true, /*IsKill=*/false,
00644         /*IsDead=*/false, /*IsUndef=*/false, /*IsEarlyClobber=*/true));
00645 
00646   // Issue CALLSEQ_START
00647   unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
00648   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
00649       .addImm(0);
00650 
00651   // Issue STACKMAP.
00652   MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
00653                                     TII.get(TargetOpcode::STACKMAP));
00654   for (auto const &MO : Ops)
00655     MIB.addOperand(MO);
00656 
00657   // Issue CALLSEQ_END
00658   unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
00659   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
00660       .addImm(0)
00661       .addImm(0);
00662 
00663   // Inform the Frame Information that we have a stackmap in this function.
00664   FuncInfo.MF->getFrameInfo()->setHasStackMap();
00665 
00666   return true;
00667 }
00668 
00669 /// \brief Lower an argument list according to the target calling convention.
00670 ///
00671 /// This is a helper for lowering intrinsics that follow a target calling
00672 /// convention or require stack pointer adjustment. Only a subset of the
00673 /// intrinsic's operands need to participate in the calling convention.
00674 bool FastISel::lowerCallOperands(const CallInst *CI, unsigned ArgIdx,
00675                                  unsigned NumArgs, const Value *Callee,
00676                                  bool ForceRetVoidTy, CallLoweringInfo &CLI) {
00677   ArgListTy Args;
00678   Args.reserve(NumArgs);
00679 
00680   // Populate the argument list.
00681   // Attributes for args start at offset 1, after the return attribute.
00682   ImmutableCallSite CS(CI);
00683   for (unsigned ArgI = ArgIdx, ArgE = ArgIdx + NumArgs, AttrI = ArgIdx + 1;
00684        ArgI != ArgE; ++ArgI) {
00685     Value *V = CI->getOperand(ArgI);
00686 
00687     assert(!V->getType()->isEmptyTy() && "Empty type passed to intrinsic.");
00688 
00689     ArgListEntry Entry;
00690     Entry.Val = V;
00691     Entry.Ty = V->getType();
00692     Entry.setAttributes(&CS, AttrI);
00693     Args.push_back(Entry);
00694   }
00695 
00696   Type *RetTy = ForceRetVoidTy ? Type::getVoidTy(CI->getType()->getContext())
00697                                : CI->getType();
00698   CLI.setCallee(CI->getCallingConv(), RetTy, Callee, std::move(Args), NumArgs);
00699 
00700   return lowerCallTo(CLI);
00701 }
00702 
00703 bool FastISel::selectPatchpoint(const CallInst *I) {
00704   // void|i64 @llvm.experimental.patchpoint.void|i64(i64 <id>,
00705   //                                                 i32 <numBytes>,
00706   //                                                 i8* <target>,
00707   //                                                 i32 <numArgs>,
00708   //                                                 [Args...],
00709   //                                                 [live variables...])
00710   CallingConv::ID CC = I->getCallingConv();
00711   bool IsAnyRegCC = CC == CallingConv::AnyReg;
00712   bool HasDef = !I->getType()->isVoidTy();
00713   Value *Callee = I->getOperand(PatchPointOpers::TargetPos);
00714 
00715   // Get the real number of arguments participating in the call <numArgs>
00716   assert(isa<ConstantInt>(I->getOperand(PatchPointOpers::NArgPos)) &&
00717          "Expected a constant integer.");
00718   const auto *NumArgsVal =
00719       cast<ConstantInt>(I->getOperand(PatchPointOpers::NArgPos));
00720   unsigned NumArgs = NumArgsVal->getZExtValue();
00721 
00722   // Skip the four meta args: <id>, <numNopBytes>, <target>, <numArgs>
00723   // This includes all meta-operands up to but not including CC.
00724   unsigned NumMetaOpers = PatchPointOpers::CCPos;
00725   assert(I->getNumArgOperands() >= NumMetaOpers + NumArgs &&
00726          "Not enough arguments provided to the patchpoint intrinsic");
00727 
00728   // For AnyRegCC the arguments are lowered later on manually.
00729   unsigned NumCallArgs = IsAnyRegCC ? 0 : NumArgs;
00730   CallLoweringInfo CLI;
00731   if (!lowerCallOperands(I, NumMetaOpers, NumCallArgs, Callee, IsAnyRegCC, CLI))
00732     return false;
00733 
00734   assert(CLI.Call && "No call instruction specified.");
00735 
00736   SmallVector<MachineOperand, 32> Ops;
00737 
00738   // Add an explicit result reg if we use the anyreg calling convention.
00739   if (IsAnyRegCC && HasDef) {
00740     assert(CLI.NumResultRegs == 0 && "Unexpected result register.");
00741     CLI.ResultReg = createResultReg(TLI.getRegClassFor(MVT::i64));
00742     CLI.NumResultRegs = 1;
00743     Ops.push_back(MachineOperand::CreateReg(CLI.ResultReg, /*IsDef=*/true));
00744   }
00745 
00746   // Add the <id> and <numBytes> constants.
00747   assert(isa<ConstantInt>(I->getOperand(PatchPointOpers::IDPos)) &&
00748          "Expected a constant integer.");
00749   const auto *ID = cast<ConstantInt>(I->getOperand(PatchPointOpers::IDPos));
00750   Ops.push_back(MachineOperand::CreateImm(ID->getZExtValue()));
00751 
00752   assert(isa<ConstantInt>(I->getOperand(PatchPointOpers::NBytesPos)) &&
00753          "Expected a constant integer.");
00754   const auto *NumBytes =
00755       cast<ConstantInt>(I->getOperand(PatchPointOpers::NBytesPos));
00756   Ops.push_back(MachineOperand::CreateImm(NumBytes->getZExtValue()));
00757 
00758   // Assume that the callee is a constant address or null pointer.
00759   // FIXME: handle function symbols in the future.
00760   uint64_t CalleeAddr;
00761   if (const auto *C = dyn_cast<IntToPtrInst>(Callee))
00762     CalleeAddr = cast<ConstantInt>(C->getOperand(0))->getZExtValue();
00763   else if (const auto *C = dyn_cast<ConstantExpr>(Callee)) {
00764     if (C->getOpcode() == Instruction::IntToPtr)
00765       CalleeAddr = cast<ConstantInt>(C->getOperand(0))->getZExtValue();
00766     else
00767       llvm_unreachable("Unsupported ConstantExpr.");
00768   } else if (isa<ConstantPointerNull>(Callee))
00769     CalleeAddr = 0;
00770   else
00771     llvm_unreachable("Unsupported callee address.");
00772 
00773   Ops.push_back(MachineOperand::CreateImm(CalleeAddr));
00774 
00775   // Adjust <numArgs> to account for any arguments that have been passed on
00776   // the stack instead.
00777   unsigned NumCallRegArgs = IsAnyRegCC ? NumArgs : CLI.OutRegs.size();
00778   Ops.push_back(MachineOperand::CreateImm(NumCallRegArgs));
00779 
00780   // Add the calling convention
00781   Ops.push_back(MachineOperand::CreateImm((unsigned)CC));
00782 
00783   // Add the arguments we omitted previously. The register allocator should
00784   // place these in any free register.
00785   if (IsAnyRegCC) {
00786     for (unsigned i = NumMetaOpers, e = NumMetaOpers + NumArgs; i != e; ++i) {
00787       unsigned Reg = getRegForValue(I->getArgOperand(i));
00788       if (!Reg)
00789         return false;
00790       Ops.push_back(MachineOperand::CreateReg(Reg, /*IsDef=*/false));
00791     }
00792   }
00793 
00794   // Push the arguments from the call instruction.
00795   for (auto Reg : CLI.OutRegs)
00796     Ops.push_back(MachineOperand::CreateReg(Reg, /*IsDef=*/false));
00797 
00798   // Push live variables for the stack map.
00799   if (!addStackMapLiveVars(Ops, I, NumMetaOpers + NumArgs))
00800     return false;
00801 
00802   // Push the register mask info.
00803   Ops.push_back(MachineOperand::CreateRegMask(TRI.getCallPreservedMask(CC)));
00804 
00805   // Add scratch registers as implicit def and early clobber.
00806   const MCPhysReg *ScratchRegs = TLI.getScratchRegisters(CC);
00807   for (unsigned i = 0; ScratchRegs[i]; ++i)
00808     Ops.push_back(MachineOperand::CreateReg(
00809         ScratchRegs[i], /*IsDef=*/true, /*IsImp=*/true, /*IsKill=*/false,
00810         /*IsDead=*/false, /*IsUndef=*/false, /*IsEarlyClobber=*/true));
00811 
00812   // Add implicit defs (return values).
00813   for (auto Reg : CLI.InRegs)
00814     Ops.push_back(MachineOperand::CreateReg(Reg, /*IsDef=*/true,
00815                                             /*IsImpl=*/true));
00816 
00817   // Insert the patchpoint instruction before the call generated by the target.
00818   MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, CLI.Call, DbgLoc,
00819                                     TII.get(TargetOpcode::PATCHPOINT));
00820 
00821   for (auto &MO : Ops)
00822     MIB.addOperand(MO);
00823 
00824   MIB->setPhysRegsDeadExcept(CLI.InRegs, TRI);
00825 
00826   // Delete the original call instruction.
00827   CLI.Call->eraseFromParent();
00828 
00829   // Inform the Frame Information that we have a patchpoint in this function.
00830   FuncInfo.MF->getFrameInfo()->setHasPatchPoint();
00831 
00832   if (CLI.NumResultRegs)
00833     updateValueMap(I, CLI.ResultReg, CLI.NumResultRegs);
00834   return true;
00835 }
00836 
00837 /// Returns an AttributeSet representing the attributes applied to the return
00838 /// value of the given call.
00839 static AttributeSet getReturnAttrs(FastISel::CallLoweringInfo &CLI) {
00840   SmallVector<Attribute::AttrKind, 2> Attrs;
00841   if (CLI.RetSExt)
00842     Attrs.push_back(Attribute::SExt);
00843   if (CLI.RetZExt)
00844     Attrs.push_back(Attribute::ZExt);
00845   if (CLI.IsInReg)
00846     Attrs.push_back(Attribute::InReg);
00847 
00848   return AttributeSet::get(CLI.RetTy->getContext(), AttributeSet::ReturnIndex,
00849                            Attrs);
00850 }
00851 
00852 bool FastISel::lowerCallTo(const CallInst *CI, const char *SymName,
00853                            unsigned NumArgs) {
00854   ImmutableCallSite CS(CI);
00855 
00856   PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
00857   FunctionType *FTy = cast<FunctionType>(PT->getElementType());
00858   Type *RetTy = FTy->getReturnType();
00859 
00860   ArgListTy Args;
00861   Args.reserve(NumArgs);
00862 
00863   // Populate the argument list.
00864   // Attributes for args start at offset 1, after the return attribute.
00865   for (unsigned ArgI = 0; ArgI != NumArgs; ++ArgI) {
00866     Value *V = CI->getOperand(ArgI);
00867 
00868     assert(!V->getType()->isEmptyTy() && "Empty type passed to intrinsic.");
00869 
00870     ArgListEntry Entry;
00871     Entry.Val = V;
00872     Entry.Ty = V->getType();
00873     Entry.setAttributes(&CS, ArgI + 1);
00874     Args.push_back(Entry);
00875   }
00876 
00877   CallLoweringInfo CLI;
00878   CLI.setCallee(RetTy, FTy, SymName, std::move(Args), CS, NumArgs);
00879 
00880   return lowerCallTo(CLI);
00881 }
00882 
00883 bool FastISel::lowerCallTo(CallLoweringInfo &CLI) {
00884   // Handle the incoming return values from the call.
00885   CLI.clearIns();
00886   SmallVector<EVT, 4> RetTys;
00887   ComputeValueVTs(TLI, CLI.RetTy, RetTys);
00888 
00889   SmallVector<ISD::OutputArg, 4> Outs;
00890   GetReturnInfo(CLI.RetTy, getReturnAttrs(CLI), Outs, TLI);
00891 
00892   bool CanLowerReturn = TLI.CanLowerReturn(
00893       CLI.CallConv, *FuncInfo.MF, CLI.IsVarArg, Outs, CLI.RetTy->getContext());
00894 
00895   // FIXME: sret demotion isn't supported yet - bail out.
00896   if (!CanLowerReturn)
00897     return false;
00898 
00899   for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
00900     EVT VT = RetTys[I];
00901     MVT RegisterVT = TLI.getRegisterType(CLI.RetTy->getContext(), VT);
00902     unsigned NumRegs = TLI.getNumRegisters(CLI.RetTy->getContext(), VT);
00903     for (unsigned i = 0; i != NumRegs; ++i) {
00904       ISD::InputArg MyFlags;
00905       MyFlags.VT = RegisterVT;
00906       MyFlags.ArgVT = VT;
00907       MyFlags.Used = CLI.IsReturnValueUsed;
00908       if (CLI.RetSExt)
00909         MyFlags.Flags.setSExt();
00910       if (CLI.RetZExt)
00911         MyFlags.Flags.setZExt();
00912       if (CLI.IsInReg)
00913         MyFlags.Flags.setInReg();
00914       CLI.Ins.push_back(MyFlags);
00915     }
00916   }
00917 
00918   // Handle all of the outgoing arguments.
00919   CLI.clearOuts();
00920   for (auto &Arg : CLI.getArgs()) {
00921     Type *FinalType = Arg.Ty;
00922     if (Arg.IsByVal)
00923       FinalType = cast<PointerType>(Arg.Ty)->getElementType();
00924     bool NeedsRegBlock = TLI.functionArgumentNeedsConsecutiveRegisters(
00925         FinalType, CLI.CallConv, CLI.IsVarArg);
00926 
00927     ISD::ArgFlagsTy Flags;
00928     if (Arg.IsZExt)
00929       Flags.setZExt();
00930     if (Arg.IsSExt)
00931       Flags.setSExt();
00932     if (Arg.IsInReg)
00933       Flags.setInReg();
00934     if (Arg.IsSRet)
00935       Flags.setSRet();
00936     if (Arg.IsByVal)
00937       Flags.setByVal();
00938     if (Arg.IsInAlloca) {
00939       Flags.setInAlloca();
00940       // Set the byval flag for CCAssignFn callbacks that don't know about
00941       // inalloca. This way we can know how many bytes we should've allocated
00942       // and how many bytes a callee cleanup function will pop.  If we port
00943       // inalloca to more targets, we'll have to add custom inalloca handling in
00944       // the various CC lowering callbacks.
00945       Flags.setByVal();
00946     }
00947     if (Arg.IsByVal || Arg.IsInAlloca) {
00948       PointerType *Ty = cast<PointerType>(Arg.Ty);
00949       Type *ElementTy = Ty->getElementType();
00950       unsigned FrameSize = DL.getTypeAllocSize(ElementTy);
00951       // For ByVal, alignment should come from FE. BE will guess if this info is
00952       // not there, but there are cases it cannot get right.
00953       unsigned FrameAlign = Arg.Alignment;
00954       if (!FrameAlign)
00955         FrameAlign = TLI.getByValTypeAlignment(ElementTy);
00956       Flags.setByValSize(FrameSize);
00957       Flags.setByValAlign(FrameAlign);
00958     }
00959     if (Arg.IsNest)
00960       Flags.setNest();
00961     if (NeedsRegBlock)
00962       Flags.setInConsecutiveRegs();
00963     unsigned OriginalAlignment = DL.getABITypeAlignment(Arg.Ty);
00964     Flags.setOrigAlign(OriginalAlignment);
00965 
00966     CLI.OutVals.push_back(Arg.Val);
00967     CLI.OutFlags.push_back(Flags);
00968   }
00969 
00970   if (!fastLowerCall(CLI))
00971     return false;
00972 
00973   // Set all unused physreg defs as dead.
00974   assert(CLI.Call && "No call instruction specified.");
00975   CLI.Call->setPhysRegsDeadExcept(CLI.InRegs, TRI);
00976 
00977   if (CLI.NumResultRegs && CLI.CS)
00978     updateValueMap(CLI.CS->getInstruction(), CLI.ResultReg, CLI.NumResultRegs);
00979 
00980   return true;
00981 }
00982 
00983 bool FastISel::lowerCall(const CallInst *CI) {
00984   ImmutableCallSite CS(CI);
00985 
00986   PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
00987   FunctionType *FuncTy = cast<FunctionType>(PT->getElementType());
00988   Type *RetTy = FuncTy->getReturnType();
00989 
00990   ArgListTy Args;
00991   ArgListEntry Entry;
00992   Args.reserve(CS.arg_size());
00993 
00994   for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
00995        i != e; ++i) {
00996     Value *V = *i;
00997 
00998     // Skip empty types
00999     if (V->getType()->isEmptyTy())
01000       continue;
01001 
01002     Entry.Val = V;
01003     Entry.Ty = V->getType();
01004 
01005     // Skip the first return-type Attribute to get to params.
01006     Entry.setAttributes(&CS, i - CS.arg_begin() + 1);
01007     Args.push_back(Entry);
01008   }
01009 
01010   // Check if target-independent constraints permit a tail call here.
01011   // Target-dependent constraints are checked within fastLowerCall.
01012   bool IsTailCall = CI->isTailCall();
01013   if (IsTailCall && !isInTailCallPosition(CS, TM))
01014     IsTailCall = false;
01015 
01016   CallLoweringInfo CLI;
01017   CLI.setCallee(RetTy, FuncTy, CI->getCalledValue(), std::move(Args), CS)
01018       .setTailCall(IsTailCall);
01019 
01020   return lowerCallTo(CLI);
01021 }
01022 
01023 bool FastISel::selectCall(const User *I) {
01024   const CallInst *Call = cast<CallInst>(I);
01025 
01026   // Handle simple inline asms.
01027   if (const InlineAsm *IA = dyn_cast<InlineAsm>(Call->getCalledValue())) {
01028     // If the inline asm has side effects, then make sure that no local value
01029     // lives across by flushing the local value map.
01030     if (IA->hasSideEffects())
01031       flushLocalValueMap();
01032 
01033     // Don't attempt to handle constraints.
01034     if (!IA->getConstraintString().empty())
01035       return false;
01036 
01037     unsigned ExtraInfo = 0;
01038     if (IA->hasSideEffects())
01039       ExtraInfo |= InlineAsm::Extra_HasSideEffects;
01040     if (IA->isAlignStack())
01041       ExtraInfo |= InlineAsm::Extra_IsAlignStack;
01042 
01043     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01044             TII.get(TargetOpcode::INLINEASM))
01045         .addExternalSymbol(IA->getAsmString().c_str())
01046         .addImm(ExtraInfo);
01047     return true;
01048   }
01049 
01050   MachineModuleInfo &MMI = FuncInfo.MF->getMMI();
01051   ComputeUsesVAFloatArgument(*Call, &MMI);
01052 
01053   // Handle intrinsic function calls.
01054   if (const auto *II = dyn_cast<IntrinsicInst>(Call))
01055     return selectIntrinsicCall(II);
01056 
01057   // Usually, it does not make sense to initialize a value,
01058   // make an unrelated function call and use the value, because
01059   // it tends to be spilled on the stack. So, we move the pointer
01060   // to the last local value to the beginning of the block, so that
01061   // all the values which have already been materialized,
01062   // appear after the call. It also makes sense to skip intrinsics
01063   // since they tend to be inlined.
01064   flushLocalValueMap();
01065 
01066   return lowerCall(Call);
01067 }
01068 
01069 bool FastISel::selectIntrinsicCall(const IntrinsicInst *II) {
01070   switch (II->getIntrinsicID()) {
01071   default:
01072     break;
01073   // At -O0 we don't care about the lifetime intrinsics.
01074   case Intrinsic::lifetime_start:
01075   case Intrinsic::lifetime_end:
01076   // The donothing intrinsic does, well, nothing.
01077   case Intrinsic::donothing:
01078     return true;
01079   case Intrinsic::dbg_declare: {
01080     const DbgDeclareInst *DI = cast<DbgDeclareInst>(II);
01081     DIVariable DIVar(DI->getVariable());
01082     assert((!DIVar || DIVar.isVariable()) &&
01083            "Variable in DbgDeclareInst should be either null or a DIVariable.");
01084     if (!DIVar || !FuncInfo.MF->getMMI().hasDebugInfo()) {
01085       DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
01086       return true;
01087     }
01088 
01089     const Value *Address = DI->getAddress();
01090     if (!Address || isa<UndefValue>(Address)) {
01091       DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
01092       return true;
01093     }
01094 
01095     unsigned Offset = 0;
01096     Optional<MachineOperand> Op;
01097     if (const auto *Arg = dyn_cast<Argument>(Address))
01098       // Some arguments' frame index is recorded during argument lowering.
01099       Offset = FuncInfo.getArgumentFrameIndex(Arg);
01100     if (Offset)
01101       Op = MachineOperand::CreateFI(Offset);
01102     if (!Op)
01103       if (unsigned Reg = lookUpRegForValue(Address))
01104         Op = MachineOperand::CreateReg(Reg, false);
01105 
01106     // If we have a VLA that has a "use" in a metadata node that's then used
01107     // here but it has no other uses, then we have a problem. E.g.,
01108     //
01109     //   int foo (const int *x) {
01110     //     char a[*x];
01111     //     return 0;
01112     //   }
01113     //
01114     // If we assign 'a' a vreg and fast isel later on has to use the selection
01115     // DAG isel, it will want to copy the value to the vreg. However, there are
01116     // no uses, which goes counter to what selection DAG isel expects.
01117     if (!Op && !Address->use_empty() && isa<Instruction>(Address) &&
01118         (!isa<AllocaInst>(Address) ||
01119          !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(Address))))
01120       Op = MachineOperand::CreateReg(FuncInfo.InitializeRegForValue(Address),
01121                                      false);
01122 
01123     if (Op) {
01124       if (Op->isReg()) {
01125         Op->setIsDebug(true);
01126         BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01127                 TII.get(TargetOpcode::DBG_VALUE), false, Op->getReg(), 0,
01128                 DI->getVariable());
01129       } else
01130         BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01131                 TII.get(TargetOpcode::DBG_VALUE))
01132             .addOperand(*Op)
01133             .addImm(0)
01134             .addMetadata(DI->getVariable());
01135     } else {
01136       // We can't yet handle anything else here because it would require
01137       // generating code, thus altering codegen because of debug info.
01138       DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
01139     }
01140     return true;
01141   }
01142   case Intrinsic::dbg_value: {
01143     // This form of DBG_VALUE is target-independent.
01144     const DbgValueInst *DI = cast<DbgValueInst>(II);
01145     const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
01146     const Value *V = DI->getValue();
01147     if (!V) {
01148       // Currently the optimizer can produce this; insert an undef to
01149       // help debugging.  Probably the optimizer should not do this.
01150       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
01151           .addReg(0U)
01152           .addImm(DI->getOffset())
01153           .addMetadata(DI->getVariable());
01154     } else if (const auto *CI = dyn_cast<ConstantInt>(V)) {
01155       if (CI->getBitWidth() > 64)
01156         BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
01157             .addCImm(CI)
01158             .addImm(DI->getOffset())
01159             .addMetadata(DI->getVariable());
01160       else
01161         BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
01162             .addImm(CI->getZExtValue())
01163             .addImm(DI->getOffset())
01164             .addMetadata(DI->getVariable());
01165     } else if (const auto *CF = dyn_cast<ConstantFP>(V)) {
01166       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
01167           .addFPImm(CF)
01168           .addImm(DI->getOffset())
01169           .addMetadata(DI->getVariable());
01170     } else if (unsigned Reg = lookUpRegForValue(V)) {
01171       // FIXME: This does not handle register-indirect values at offset 0.
01172       bool IsIndirect = DI->getOffset() != 0;
01173       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, IsIndirect, Reg,
01174               DI->getOffset(), DI->getVariable());
01175     } else {
01176       // We can't yet handle anything else here because it would require
01177       // generating code, thus altering codegen because of debug info.
01178       DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
01179     }
01180     return true;
01181   }
01182   case Intrinsic::objectsize: {
01183     ConstantInt *CI = cast<ConstantInt>(II->getArgOperand(1));
01184     unsigned long long Res = CI->isZero() ? -1ULL : 0;
01185     Constant *ResCI = ConstantInt::get(II->getType(), Res);
01186     unsigned ResultReg = getRegForValue(ResCI);
01187     if (!ResultReg)
01188       return false;
01189     updateValueMap(II, ResultReg);
01190     return true;
01191   }
01192   case Intrinsic::expect: {
01193     unsigned ResultReg = getRegForValue(II->getArgOperand(0));
01194     if (!ResultReg)
01195       return false;
01196     updateValueMap(II, ResultReg);
01197     return true;
01198   }
01199   case Intrinsic::experimental_stackmap:
01200     return selectStackmap(II);
01201   case Intrinsic::experimental_patchpoint_void:
01202   case Intrinsic::experimental_patchpoint_i64:
01203     return selectPatchpoint(II);
01204   }
01205 
01206   return fastLowerIntrinsicCall(II);
01207 }
01208 
01209 bool FastISel::selectCast(const User *I, unsigned Opcode) {
01210   EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
01211   EVT DstVT = TLI.getValueType(I->getType());
01212 
01213   if (SrcVT == MVT::Other || !SrcVT.isSimple() || DstVT == MVT::Other ||
01214       !DstVT.isSimple())
01215     // Unhandled type. Halt "fast" selection and bail.
01216     return false;
01217 
01218   // Check if the destination type is legal.
01219   if (!TLI.isTypeLegal(DstVT))
01220     return false;
01221 
01222   // Check if the source operand is legal.
01223   if (!TLI.isTypeLegal(SrcVT))
01224     return false;
01225 
01226   unsigned InputReg = getRegForValue(I->getOperand(0));
01227   if (!InputReg)
01228     // Unhandled operand.  Halt "fast" selection and bail.
01229     return false;
01230 
01231   bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
01232 
01233   unsigned ResultReg = fastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
01234                                   Opcode, InputReg, InputRegIsKill);
01235   if (!ResultReg)
01236     return false;
01237 
01238   updateValueMap(I, ResultReg);
01239   return true;
01240 }
01241 
01242 bool FastISel::selectBitCast(const User *I) {
01243   // If the bitcast doesn't change the type, just use the operand value.
01244   if (I->getType() == I->getOperand(0)->getType()) {
01245     unsigned Reg = getRegForValue(I->getOperand(0));
01246     if (!Reg)
01247       return false;
01248     updateValueMap(I, Reg);
01249     return true;
01250   }
01251 
01252   // Bitcasts of other values become reg-reg copies or BITCAST operators.
01253   EVT SrcEVT = TLI.getValueType(I->getOperand(0)->getType());
01254   EVT DstEVT = TLI.getValueType(I->getType());
01255   if (SrcEVT == MVT::Other || DstEVT == MVT::Other ||
01256       !TLI.isTypeLegal(SrcEVT) || !TLI.isTypeLegal(DstEVT))
01257     // Unhandled type. Halt "fast" selection and bail.
01258     return false;
01259 
01260   MVT SrcVT = SrcEVT.getSimpleVT();
01261   MVT DstVT = DstEVT.getSimpleVT();
01262   unsigned Op0 = getRegForValue(I->getOperand(0));
01263   if (!Op0) // Unhandled operand. Halt "fast" selection and bail.
01264     return false;
01265   bool Op0IsKill = hasTrivialKill(I->getOperand(0));
01266 
01267   // First, try to perform the bitcast by inserting a reg-reg copy.
01268   unsigned ResultReg = 0;
01269   if (SrcVT == DstVT) {
01270     const TargetRegisterClass *SrcClass = TLI.getRegClassFor(SrcVT);
01271     const TargetRegisterClass *DstClass = TLI.getRegClassFor(DstVT);
01272     // Don't attempt a cross-class copy. It will likely fail.
01273     if (SrcClass == DstClass) {
01274       ResultReg = createResultReg(DstClass);
01275       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01276               TII.get(TargetOpcode::COPY), ResultReg).addReg(Op0);
01277     }
01278   }
01279 
01280   // If the reg-reg copy failed, select a BITCAST opcode.
01281   if (!ResultReg)
01282     ResultReg = fastEmit_r(SrcVT, DstVT, ISD::BITCAST, Op0, Op0IsKill);
01283 
01284   if (!ResultReg)
01285     return false;
01286 
01287   updateValueMap(I, ResultReg);
01288   return true;
01289 }
01290 
01291 bool FastISel::selectInstruction(const Instruction *I) {
01292   // Just before the terminator instruction, insert instructions to
01293   // feed PHI nodes in successor blocks.
01294   if (isa<TerminatorInst>(I))
01295     if (!handlePHINodesInSuccessorBlocks(I->getParent()))
01296       return false;
01297 
01298   DbgLoc = I->getDebugLoc();
01299 
01300   SavedInsertPt = FuncInfo.InsertPt;
01301 
01302   if (const auto *Call = dyn_cast<CallInst>(I)) {
01303     const Function *F = Call->getCalledFunction();
01304     LibFunc::Func Func;
01305 
01306     // As a special case, don't handle calls to builtin library functions that
01307     // may be translated directly to target instructions.
01308     if (F && !F->hasLocalLinkage() && F->hasName() &&
01309         LibInfo->getLibFunc(F->getName(), Func) &&
01310         LibInfo->hasOptimizedCodeGen(Func))
01311       return false;
01312 
01313     // Don't handle Intrinsic::trap if a trap funciton is specified.
01314     if (F && F->getIntrinsicID() == Intrinsic::trap &&
01315         !TM.Options.getTrapFunctionName().empty())
01316       return false;
01317   }
01318 
01319   // First, try doing target-independent selection.
01320   if (!SkipTargetIndependentISel) {
01321     if (selectOperator(I, I->getOpcode())) {
01322       ++NumFastIselSuccessIndependent;
01323       DbgLoc = DebugLoc();
01324       return true;
01325     }
01326     // Remove dead code.
01327     recomputeInsertPt();
01328     if (SavedInsertPt != FuncInfo.InsertPt)
01329       removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
01330     SavedInsertPt = FuncInfo.InsertPt;
01331   }
01332   // Next, try calling the target to attempt to handle the instruction.
01333   if (fastSelectInstruction(I)) {
01334     ++NumFastIselSuccessTarget;
01335     DbgLoc = DebugLoc();
01336     return true;
01337   }
01338   // Remove dead code.
01339   recomputeInsertPt();
01340   if (SavedInsertPt != FuncInfo.InsertPt)
01341     removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
01342 
01343   DbgLoc = DebugLoc();
01344   // Undo phi node updates, because they will be added again by SelectionDAG.
01345   if (isa<TerminatorInst>(I))
01346     FuncInfo.PHINodesToUpdate.resize(FuncInfo.OrigNumPHINodesToUpdate);
01347   return false;
01348 }
01349 
01350 /// Emit an unconditional branch to the given block, unless it is the immediate
01351 /// (fall-through) successor, and update the CFG.
01352 void FastISel::fastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DbgLoc) {
01353   if (FuncInfo.MBB->getBasicBlock()->size() > 1 &&
01354       FuncInfo.MBB->isLayoutSuccessor(MSucc)) {
01355     // For more accurate line information if this is the only instruction
01356     // in the block then emit it, otherwise we have the unconditional
01357     // fall-through case, which needs no instructions.
01358   } else {
01359     // The unconditional branch case.
01360     TII.InsertBranch(*FuncInfo.MBB, MSucc, nullptr,
01361                      SmallVector<MachineOperand, 0>(), DbgLoc);
01362   }
01363   uint32_t BranchWeight = 0;
01364   if (FuncInfo.BPI)
01365     BranchWeight = FuncInfo.BPI->getEdgeWeight(FuncInfo.MBB->getBasicBlock(),
01366                                                MSucc->getBasicBlock());
01367   FuncInfo.MBB->addSuccessor(MSucc, BranchWeight);
01368 }
01369 
01370 /// Emit an FNeg operation.
01371 bool FastISel::selectFNeg(const User *I) {
01372   unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
01373   if (!OpReg)
01374     return false;
01375   bool OpRegIsKill = hasTrivialKill(I);
01376 
01377   // If the target has ISD::FNEG, use it.
01378   EVT VT = TLI.getValueType(I->getType());
01379   unsigned ResultReg = fastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(), ISD::FNEG,
01380                                   OpReg, OpRegIsKill);
01381   if (ResultReg) {
01382     updateValueMap(I, ResultReg);
01383     return true;
01384   }
01385 
01386   // Bitcast the value to integer, twiddle the sign bit with xor,
01387   // and then bitcast it back to floating-point.
01388   if (VT.getSizeInBits() > 64)
01389     return false;
01390   EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
01391   if (!TLI.isTypeLegal(IntVT))
01392     return false;
01393 
01394   unsigned IntReg = fastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
01395                                ISD::BITCAST, OpReg, OpRegIsKill);
01396   if (!IntReg)
01397     return false;
01398 
01399   unsigned IntResultReg = fastEmit_ri_(
01400       IntVT.getSimpleVT(), ISD::XOR, IntReg, /*IsKill=*/true,
01401       UINT64_C(1) << (VT.getSizeInBits() - 1), IntVT.getSimpleVT());
01402   if (!IntResultReg)
01403     return false;
01404 
01405   ResultReg = fastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(), ISD::BITCAST,
01406                          IntResultReg, /*IsKill=*/true);
01407   if (!ResultReg)
01408     return false;
01409 
01410   updateValueMap(I, ResultReg);
01411   return true;
01412 }
01413 
01414 bool FastISel::selectExtractValue(const User *U) {
01415   const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(U);
01416   if (!EVI)
01417     return false;
01418 
01419   // Make sure we only try to handle extracts with a legal result.  But also
01420   // allow i1 because it's easy.
01421   EVT RealVT = TLI.getValueType(EVI->getType(), /*AllowUnknown=*/true);
01422   if (!RealVT.isSimple())
01423     return false;
01424   MVT VT = RealVT.getSimpleVT();
01425   if (!TLI.isTypeLegal(VT) && VT != MVT::i1)
01426     return false;
01427 
01428   const Value *Op0 = EVI->getOperand(0);
01429   Type *AggTy = Op0->getType();
01430 
01431   // Get the base result register.
01432   unsigned ResultReg;
01433   DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(Op0);
01434   if (I != FuncInfo.ValueMap.end())
01435     ResultReg = I->second;
01436   else if (isa<Instruction>(Op0))
01437     ResultReg = FuncInfo.InitializeRegForValue(Op0);
01438   else
01439     return false; // fast-isel can't handle aggregate constants at the moment
01440 
01441   // Get the actual result register, which is an offset from the base register.
01442   unsigned VTIndex = ComputeLinearIndex(AggTy, EVI->getIndices());
01443 
01444   SmallVector<EVT, 4> AggValueVTs;
01445   ComputeValueVTs(TLI, AggTy, AggValueVTs);
01446 
01447   for (unsigned i = 0; i < VTIndex; i++)
01448     ResultReg += TLI.getNumRegisters(FuncInfo.Fn->getContext(), AggValueVTs[i]);
01449 
01450   updateValueMap(EVI, ResultReg);
01451   return true;
01452 }
01453 
01454 bool FastISel::selectOperator(const User *I, unsigned Opcode) {
01455   switch (Opcode) {
01456   case Instruction::Add:
01457     return selectBinaryOp(I, ISD::ADD);
01458   case Instruction::FAdd:
01459     return selectBinaryOp(I, ISD::FADD);
01460   case Instruction::Sub:
01461     return selectBinaryOp(I, ISD::SUB);
01462   case Instruction::FSub:
01463     // FNeg is currently represented in LLVM IR as a special case of FSub.
01464     if (BinaryOperator::isFNeg(I))
01465       return selectFNeg(I);
01466     return selectBinaryOp(I, ISD::FSUB);
01467   case Instruction::Mul:
01468     return selectBinaryOp(I, ISD::MUL);
01469   case Instruction::FMul:
01470     return selectBinaryOp(I, ISD::FMUL);
01471   case Instruction::SDiv:
01472     return selectBinaryOp(I, ISD::SDIV);
01473   case Instruction::UDiv:
01474     return selectBinaryOp(I, ISD::UDIV);
01475   case Instruction::FDiv:
01476     return selectBinaryOp(I, ISD::FDIV);
01477   case Instruction::SRem:
01478     return selectBinaryOp(I, ISD::SREM);
01479   case Instruction::URem:
01480     return selectBinaryOp(I, ISD::UREM);
01481   case Instruction::FRem:
01482     return selectBinaryOp(I, ISD::FREM);
01483   case Instruction::Shl:
01484     return selectBinaryOp(I, ISD::SHL);
01485   case Instruction::LShr:
01486     return selectBinaryOp(I, ISD::SRL);
01487   case Instruction::AShr:
01488     return selectBinaryOp(I, ISD::SRA);
01489   case Instruction::And:
01490     return selectBinaryOp(I, ISD::AND);
01491   case Instruction::Or:
01492     return selectBinaryOp(I, ISD::OR);
01493   case Instruction::Xor:
01494     return selectBinaryOp(I, ISD::XOR);
01495 
01496   case Instruction::GetElementPtr:
01497     return selectGetElementPtr(I);
01498 
01499   case Instruction::Br: {
01500     const BranchInst *BI = cast<BranchInst>(I);
01501 
01502     if (BI->isUnconditional()) {
01503       const BasicBlock *LLVMSucc = BI->getSuccessor(0);
01504       MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc];
01505       fastEmitBranch(MSucc, BI->getDebugLoc());
01506       return true;
01507     }
01508 
01509     // Conditional branches are not handed yet.
01510     // Halt "fast" selection and bail.
01511     return false;
01512   }
01513 
01514   case Instruction::Unreachable:
01515     if (TM.Options.TrapUnreachable)
01516       return fastEmit_(MVT::Other, MVT::Other, ISD::TRAP) != 0;
01517     else
01518       return true;
01519 
01520   case Instruction::Alloca:
01521     // FunctionLowering has the static-sized case covered.
01522     if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I)))
01523       return true;
01524 
01525     // Dynamic-sized alloca is not handled yet.
01526     return false;
01527 
01528   case Instruction::Call:
01529     return selectCall(I);
01530 
01531   case Instruction::BitCast:
01532     return selectBitCast(I);
01533 
01534   case Instruction::FPToSI:
01535     return selectCast(I, ISD::FP_TO_SINT);
01536   case Instruction::ZExt:
01537     return selectCast(I, ISD::ZERO_EXTEND);
01538   case Instruction::SExt:
01539     return selectCast(I, ISD::SIGN_EXTEND);
01540   case Instruction::Trunc:
01541     return selectCast(I, ISD::TRUNCATE);
01542   case Instruction::SIToFP:
01543     return selectCast(I, ISD::SINT_TO_FP);
01544 
01545   case Instruction::IntToPtr: // Deliberate fall-through.
01546   case Instruction::PtrToInt: {
01547     EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
01548     EVT DstVT = TLI.getValueType(I->getType());
01549     if (DstVT.bitsGT(SrcVT))
01550       return selectCast(I, ISD::ZERO_EXTEND);
01551     if (DstVT.bitsLT(SrcVT))
01552       return selectCast(I, ISD::TRUNCATE);
01553     unsigned Reg = getRegForValue(I->getOperand(0));
01554     if (!Reg)
01555       return false;
01556     updateValueMap(I, Reg);
01557     return true;
01558   }
01559 
01560   case Instruction::ExtractValue:
01561     return selectExtractValue(I);
01562 
01563   case Instruction::PHI:
01564     llvm_unreachable("FastISel shouldn't visit PHI nodes!");
01565 
01566   default:
01567     // Unhandled instruction. Halt "fast" selection and bail.
01568     return false;
01569   }
01570 }
01571 
01572 FastISel::FastISel(FunctionLoweringInfo &FuncInfo,
01573                    const TargetLibraryInfo *LibInfo,
01574                    bool SkipTargetIndependentISel)
01575     : FuncInfo(FuncInfo), MF(FuncInfo.MF), MRI(FuncInfo.MF->getRegInfo()),
01576       MFI(*FuncInfo.MF->getFrameInfo()), MCP(*FuncInfo.MF->getConstantPool()),
01577       TM(FuncInfo.MF->getTarget()), DL(*TM.getSubtargetImpl()->getDataLayout()),
01578       TII(*TM.getSubtargetImpl()->getInstrInfo()),
01579       TLI(*TM.getSubtargetImpl()->getTargetLowering()),
01580       TRI(*TM.getSubtargetImpl()->getRegisterInfo()), LibInfo(LibInfo),
01581       SkipTargetIndependentISel(SkipTargetIndependentISel) {}
01582 
01583 FastISel::~FastISel() {}
01584 
01585 bool FastISel::fastLowerArguments() { return false; }
01586 
01587 bool FastISel::fastLowerCall(CallLoweringInfo & /*CLI*/) { return false; }
01588 
01589 bool FastISel::fastLowerIntrinsicCall(const IntrinsicInst * /*II*/) {
01590   return false;
01591 }
01592 
01593 unsigned FastISel::fastEmit_(MVT, MVT, unsigned) { return 0; }
01594 
01595 unsigned FastISel::fastEmit_r(MVT, MVT, unsigned, unsigned /*Op0*/,
01596                               bool /*Op0IsKill*/) {
01597   return 0;
01598 }
01599 
01600 unsigned FastISel::fastEmit_rr(MVT, MVT, unsigned, unsigned /*Op0*/,
01601                                bool /*Op0IsKill*/, unsigned /*Op1*/,
01602                                bool /*Op1IsKill*/) {
01603   return 0;
01604 }
01605 
01606 unsigned FastISel::fastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
01607   return 0;
01608 }
01609 
01610 unsigned FastISel::fastEmit_f(MVT, MVT, unsigned,
01611                               const ConstantFP * /*FPImm*/) {
01612   return 0;
01613 }
01614 
01615 unsigned FastISel::fastEmit_ri(MVT, MVT, unsigned, unsigned /*Op0*/,
01616                                bool /*Op0IsKill*/, uint64_t /*Imm*/) {
01617   return 0;
01618 }
01619 
01620 unsigned FastISel::fastEmit_rf(MVT, MVT, unsigned, unsigned /*Op0*/,
01621                                bool /*Op0IsKill*/,
01622                                const ConstantFP * /*FPImm*/) {
01623   return 0;
01624 }
01625 
01626 unsigned FastISel::fastEmit_rri(MVT, MVT, unsigned, unsigned /*Op0*/,
01627                                 bool /*Op0IsKill*/, unsigned /*Op1*/,
01628                                 bool /*Op1IsKill*/, uint64_t /*Imm*/) {
01629   return 0;
01630 }
01631 
01632 /// This method is a wrapper of fastEmit_ri. It first tries to emit an
01633 /// instruction with an immediate operand using fastEmit_ri.
01634 /// If that fails, it materializes the immediate into a register and try
01635 /// fastEmit_rr instead.
01636 unsigned FastISel::fastEmit_ri_(MVT VT, unsigned Opcode, unsigned Op0,
01637                                 bool Op0IsKill, uint64_t Imm, MVT ImmType) {
01638   // If this is a multiply by a power of two, emit this as a shift left.
01639   if (Opcode == ISD::MUL && isPowerOf2_64(Imm)) {
01640     Opcode = ISD::SHL;
01641     Imm = Log2_64(Imm);
01642   } else if (Opcode == ISD::UDIV && isPowerOf2_64(Imm)) {
01643     // div x, 8 -> srl x, 3
01644     Opcode = ISD::SRL;
01645     Imm = Log2_64(Imm);
01646   }
01647 
01648   // Horrible hack (to be removed), check to make sure shift amounts are
01649   // in-range.
01650   if ((Opcode == ISD::SHL || Opcode == ISD::SRA || Opcode == ISD::SRL) &&
01651       Imm >= VT.getSizeInBits())
01652     return 0;
01653 
01654   // First check if immediate type is legal. If not, we can't use the ri form.
01655   unsigned ResultReg = fastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
01656   if (ResultReg)
01657     return ResultReg;
01658   unsigned MaterialReg = fastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
01659   if (!MaterialReg) {
01660     // This is a bit ugly/slow, but failing here means falling out of
01661     // fast-isel, which would be very slow.
01662     IntegerType *ITy =
01663         IntegerType::get(FuncInfo.Fn->getContext(), VT.getSizeInBits());
01664     MaterialReg = getRegForValue(ConstantInt::get(ITy, Imm));
01665     if (!MaterialReg)
01666       return 0;
01667   }
01668   return fastEmit_rr(VT, VT, Opcode, Op0, Op0IsKill, MaterialReg,
01669                      /*IsKill=*/true);
01670 }
01671 
01672 unsigned FastISel::createResultReg(const TargetRegisterClass *RC) {
01673   return MRI.createVirtualRegister(RC);
01674 }
01675 
01676 unsigned FastISel::constrainOperandRegClass(const MCInstrDesc &II, unsigned Op,
01677                                             unsigned OpNum) {
01678   if (TargetRegisterInfo::isVirtualRegister(Op)) {
01679     const TargetRegisterClass *RegClass =
01680         TII.getRegClass(II, OpNum, &TRI, *FuncInfo.MF);
01681     if (!MRI.constrainRegClass(Op, RegClass)) {
01682       // If it's not legal to COPY between the register classes, something
01683       // has gone very wrong before we got here.
01684       unsigned NewOp = createResultReg(RegClass);
01685       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01686               TII.get(TargetOpcode::COPY), NewOp).addReg(Op);
01687       return NewOp;
01688     }
01689   }
01690   return Op;
01691 }
01692 
01693 unsigned FastISel::fastEmitInst_(unsigned MachineInstOpcode,
01694                                  const TargetRegisterClass *RC) {
01695   unsigned ResultReg = createResultReg(RC);
01696   const MCInstrDesc &II = TII.get(MachineInstOpcode);
01697 
01698   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg);
01699   return ResultReg;
01700 }
01701 
01702 unsigned FastISel::fastEmitInst_r(unsigned MachineInstOpcode,
01703                                   const TargetRegisterClass *RC, unsigned Op0,
01704                                   bool Op0IsKill) {
01705   const MCInstrDesc &II = TII.get(MachineInstOpcode);
01706 
01707   unsigned ResultReg = createResultReg(RC);
01708   Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
01709 
01710   if (II.getNumDefs() >= 1)
01711     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
01712         .addReg(Op0, getKillRegState(Op0IsKill));
01713   else {
01714     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
01715         .addReg(Op0, getKillRegState(Op0IsKill));
01716     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01717             TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
01718   }
01719 
01720   return ResultReg;
01721 }
01722 
01723 unsigned FastISel::fastEmitInst_rr(unsigned MachineInstOpcode,
01724                                    const TargetRegisterClass *RC, unsigned Op0,
01725                                    bool Op0IsKill, unsigned Op1,
01726                                    bool Op1IsKill) {
01727   const MCInstrDesc &II = TII.get(MachineInstOpcode);
01728 
01729   unsigned ResultReg = createResultReg(RC);
01730   Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
01731   Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
01732 
01733   if (II.getNumDefs() >= 1)
01734     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
01735         .addReg(Op0, getKillRegState(Op0IsKill))
01736         .addReg(Op1, getKillRegState(Op1IsKill));
01737   else {
01738     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
01739         .addReg(Op0, getKillRegState(Op0IsKill))
01740         .addReg(Op1, getKillRegState(Op1IsKill));
01741     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01742             TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
01743   }
01744   return ResultReg;
01745 }
01746 
01747 unsigned FastISel::fastEmitInst_rrr(unsigned MachineInstOpcode,
01748                                     const TargetRegisterClass *RC, unsigned Op0,
01749                                     bool Op0IsKill, unsigned Op1,
01750                                     bool Op1IsKill, unsigned Op2,
01751                                     bool Op2IsKill) {
01752   const MCInstrDesc &II = TII.get(MachineInstOpcode);
01753 
01754   unsigned ResultReg = createResultReg(RC);
01755   Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
01756   Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
01757   Op2 = constrainOperandRegClass(II, Op2, II.getNumDefs() + 2);
01758 
01759   if (II.getNumDefs() >= 1)
01760     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
01761         .addReg(Op0, getKillRegState(Op0IsKill))
01762         .addReg(Op1, getKillRegState(Op1IsKill))
01763         .addReg(Op2, getKillRegState(Op2IsKill));
01764   else {
01765     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
01766         .addReg(Op0, getKillRegState(Op0IsKill))
01767         .addReg(Op1, getKillRegState(Op1IsKill))
01768         .addReg(Op2, getKillRegState(Op2IsKill));
01769     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01770             TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
01771   }
01772   return ResultReg;
01773 }
01774 
01775 unsigned FastISel::fastEmitInst_ri(unsigned MachineInstOpcode,
01776                                    const TargetRegisterClass *RC, unsigned Op0,
01777                                    bool Op0IsKill, uint64_t Imm) {
01778   const MCInstrDesc &II = TII.get(MachineInstOpcode);
01779 
01780   unsigned ResultReg = createResultReg(RC);
01781   Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
01782 
01783   if (II.getNumDefs() >= 1)
01784     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
01785         .addReg(Op0, getKillRegState(Op0IsKill))
01786         .addImm(Imm);
01787   else {
01788     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
01789         .addReg(Op0, getKillRegState(Op0IsKill))
01790         .addImm(Imm);
01791     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01792             TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
01793   }
01794   return ResultReg;
01795 }
01796 
01797 unsigned FastISel::fastEmitInst_rii(unsigned MachineInstOpcode,
01798                                     const TargetRegisterClass *RC, unsigned Op0,
01799                                     bool Op0IsKill, uint64_t Imm1,
01800                                     uint64_t Imm2) {
01801   const MCInstrDesc &II = TII.get(MachineInstOpcode);
01802 
01803   unsigned ResultReg = createResultReg(RC);
01804   Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
01805 
01806   if (II.getNumDefs() >= 1)
01807     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
01808         .addReg(Op0, getKillRegState(Op0IsKill))
01809         .addImm(Imm1)
01810         .addImm(Imm2);
01811   else {
01812     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
01813         .addReg(Op0, getKillRegState(Op0IsKill))
01814         .addImm(Imm1)
01815         .addImm(Imm2);
01816     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01817             TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
01818   }
01819   return ResultReg;
01820 }
01821 
01822 unsigned FastISel::fastEmitInst_rf(unsigned MachineInstOpcode,
01823                                    const TargetRegisterClass *RC, unsigned Op0,
01824                                    bool Op0IsKill, const ConstantFP *FPImm) {
01825   const MCInstrDesc &II = TII.get(MachineInstOpcode);
01826 
01827   unsigned ResultReg = createResultReg(RC);
01828   Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
01829 
01830   if (II.getNumDefs() >= 1)
01831     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
01832         .addReg(Op0, getKillRegState(Op0IsKill))
01833         .addFPImm(FPImm);
01834   else {
01835     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
01836         .addReg(Op0, getKillRegState(Op0IsKill))
01837         .addFPImm(FPImm);
01838     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01839             TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
01840   }
01841   return ResultReg;
01842 }
01843 
01844 unsigned FastISel::fastEmitInst_rri(unsigned MachineInstOpcode,
01845                                     const TargetRegisterClass *RC, unsigned Op0,
01846                                     bool Op0IsKill, unsigned Op1,
01847                                     bool Op1IsKill, uint64_t Imm) {
01848   const MCInstrDesc &II = TII.get(MachineInstOpcode);
01849 
01850   unsigned ResultReg = createResultReg(RC);
01851   Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
01852   Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
01853 
01854   if (II.getNumDefs() >= 1)
01855     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
01856         .addReg(Op0, getKillRegState(Op0IsKill))
01857         .addReg(Op1, getKillRegState(Op1IsKill))
01858         .addImm(Imm);
01859   else {
01860     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
01861         .addReg(Op0, getKillRegState(Op0IsKill))
01862         .addReg(Op1, getKillRegState(Op1IsKill))
01863         .addImm(Imm);
01864     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01865             TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
01866   }
01867   return ResultReg;
01868 }
01869 
01870 unsigned FastISel::fastEmitInst_rrii(unsigned MachineInstOpcode,
01871                                      const TargetRegisterClass *RC,
01872                                      unsigned Op0, bool Op0IsKill, unsigned Op1,
01873                                      bool Op1IsKill, uint64_t Imm1,
01874                                      uint64_t Imm2) {
01875   const MCInstrDesc &II = TII.get(MachineInstOpcode);
01876 
01877   unsigned ResultReg = createResultReg(RC);
01878   Op0 = constrainOperandRegClass(II, Op0, II.getNumDefs());
01879   Op1 = constrainOperandRegClass(II, Op1, II.getNumDefs() + 1);
01880 
01881   if (II.getNumDefs() >= 1)
01882     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
01883         .addReg(Op0, getKillRegState(Op0IsKill))
01884         .addReg(Op1, getKillRegState(Op1IsKill))
01885         .addImm(Imm1)
01886         .addImm(Imm2);
01887   else {
01888     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
01889         .addReg(Op0, getKillRegState(Op0IsKill))
01890         .addReg(Op1, getKillRegState(Op1IsKill))
01891         .addImm(Imm1)
01892         .addImm(Imm2);
01893     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01894             TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
01895   }
01896   return ResultReg;
01897 }
01898 
01899 unsigned FastISel::fastEmitInst_i(unsigned MachineInstOpcode,
01900                                   const TargetRegisterClass *RC, uint64_t Imm) {
01901   unsigned ResultReg = createResultReg(RC);
01902   const MCInstrDesc &II = TII.get(MachineInstOpcode);
01903 
01904   if (II.getNumDefs() >= 1)
01905     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
01906         .addImm(Imm);
01907   else {
01908     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addImm(Imm);
01909     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01910             TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
01911   }
01912   return ResultReg;
01913 }
01914 
01915 unsigned FastISel::fastEmitInst_ii(unsigned MachineInstOpcode,
01916                                    const TargetRegisterClass *RC, uint64_t Imm1,
01917                                    uint64_t Imm2) {
01918   unsigned ResultReg = createResultReg(RC);
01919   const MCInstrDesc &II = TII.get(MachineInstOpcode);
01920 
01921   if (II.getNumDefs() >= 1)
01922     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
01923         .addImm(Imm1)
01924         .addImm(Imm2);
01925   else {
01926     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addImm(Imm1)
01927         .addImm(Imm2);
01928     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
01929             TII.get(TargetOpcode::COPY), ResultReg).addReg(II.ImplicitDefs[0]);
01930   }
01931   return ResultReg;
01932 }
01933 
01934 unsigned FastISel::fastEmitInst_extractsubreg(MVT RetVT, unsigned Op0,
01935                                               bool Op0IsKill, uint32_t Idx) {
01936   unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
01937   assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
01938          "Cannot yet extract from physregs");
01939   const TargetRegisterClass *RC = MRI.getRegClass(Op0);
01940   MRI.constrainRegClass(Op0, TRI.getSubClassWithSubReg(RC, Idx));
01941   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
01942           ResultReg).addReg(Op0, getKillRegState(Op0IsKill), Idx);
01943   return ResultReg;
01944 }
01945 
01946 /// Emit MachineInstrs to compute the value of Op with all but the least
01947 /// significant bit set to zero.
01948 unsigned FastISel::fastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
01949   return fastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
01950 }
01951 
01952 /// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
01953 /// Emit code to ensure constants are copied into registers when needed.
01954 /// Remember the virtual registers that need to be added to the Machine PHI
01955 /// nodes as input.  We cannot just directly add them, because expansion
01956 /// might result in multiple MBB's for one BB.  As such, the start of the
01957 /// BB might correspond to a different MBB than the end.
01958 bool FastISel::handlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
01959   const TerminatorInst *TI = LLVMBB->getTerminator();
01960 
01961   SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
01962   FuncInfo.OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size();
01963 
01964   // Check successor nodes' PHI nodes that expect a constant to be available
01965   // from this block.
01966   for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
01967     const BasicBlock *SuccBB = TI->getSuccessor(succ);
01968     if (!isa<PHINode>(SuccBB->begin()))
01969       continue;
01970     MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
01971 
01972     // If this terminator has multiple identical successors (common for
01973     // switches), only handle each succ once.
01974     if (!SuccsHandled.insert(SuccMBB))
01975       continue;
01976 
01977     MachineBasicBlock::iterator MBBI = SuccMBB->begin();
01978 
01979     // At this point we know that there is a 1-1 correspondence between LLVM PHI
01980     // nodes and Machine PHI nodes, but the incoming operands have not been
01981     // emitted yet.
01982     for (BasicBlock::const_iterator I = SuccBB->begin();
01983          const auto *PN = dyn_cast<PHINode>(I); ++I) {
01984 
01985       // Ignore dead phi's.
01986       if (PN->use_empty())
01987         continue;
01988 
01989       // Only handle legal types. Two interesting things to note here. First,
01990       // by bailing out early, we may leave behind some dead instructions,
01991       // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
01992       // own moves. Second, this check is necessary because FastISel doesn't
01993       // use CreateRegs to create registers, so it always creates
01994       // exactly one register for each non-void instruction.
01995       EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
01996       if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
01997         // Handle integer promotions, though, because they're common and easy.
01998         if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
01999           VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
02000         else {
02001           FuncInfo.PHINodesToUpdate.resize(FuncInfo.OrigNumPHINodesToUpdate);
02002           return false;
02003         }
02004       }
02005 
02006       const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
02007 
02008       // Set the DebugLoc for the copy. Prefer the location of the operand
02009       // if there is one; use the location of the PHI otherwise.
02010       DbgLoc = PN->getDebugLoc();
02011       if (const auto *Inst = dyn_cast<Instruction>(PHIOp))
02012         DbgLoc = Inst->getDebugLoc();
02013 
02014       unsigned Reg = getRegForValue(PHIOp);
02015       if (!Reg) {
02016         FuncInfo.PHINodesToUpdate.resize(FuncInfo.OrigNumPHINodesToUpdate);
02017         return false;
02018       }
02019       FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
02020       DbgLoc = DebugLoc();
02021     }
02022   }
02023 
02024   return true;
02025 }
02026 
02027 bool FastISel::tryToFoldLoad(const LoadInst *LI, const Instruction *FoldInst) {
02028   assert(LI->hasOneUse() &&
02029          "tryToFoldLoad expected a LoadInst with a single use");
02030   // We know that the load has a single use, but don't know what it is.  If it
02031   // isn't one of the folded instructions, then we can't succeed here.  Handle
02032   // this by scanning the single-use users of the load until we get to FoldInst.
02033   unsigned MaxUsers = 6; // Don't scan down huge single-use chains of instrs.
02034 
02035   const Instruction *TheUser = LI->user_back();
02036   while (TheUser != FoldInst && // Scan up until we find FoldInst.
02037          // Stay in the right block.
02038          TheUser->getParent() == FoldInst->getParent() &&
02039          --MaxUsers) { // Don't scan too far.
02040     // If there are multiple or no uses of this instruction, then bail out.
02041     if (!TheUser->hasOneUse())
02042       return false;
02043 
02044     TheUser = TheUser->user_back();
02045   }
02046 
02047   // If we didn't find the fold instruction, then we failed to collapse the
02048   // sequence.
02049   if (TheUser != FoldInst)
02050     return false;
02051 
02052   // Don't try to fold volatile loads.  Target has to deal with alignment
02053   // constraints.
02054   if (LI->isVolatile())
02055     return false;
02056 
02057   // Figure out which vreg this is going into.  If there is no assigned vreg yet
02058   // then there actually was no reference to it.  Perhaps the load is referenced
02059   // by a dead instruction.
02060   unsigned LoadReg = getRegForValue(LI);
02061   if (!LoadReg)
02062     return false;
02063 
02064   // We can't fold if this vreg has no uses or more than one use.  Multiple uses
02065   // may mean that the instruction got lowered to multiple MIs, or the use of
02066   // the loaded value ended up being multiple operands of the result.
02067   if (!MRI.hasOneUse(LoadReg))
02068     return false;
02069 
02070   MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LoadReg);
02071   MachineInstr *User = RI->getParent();
02072 
02073   // Set the insertion point properly.  Folding the load can cause generation of
02074   // other random instructions (like sign extends) for addressing modes; make
02075   // sure they get inserted in a logical place before the new instruction.
02076   FuncInfo.InsertPt = User;
02077   FuncInfo.MBB = User->getParent();
02078 
02079   // Ask the target to try folding the load.
02080   return tryToFoldLoadIntoMI(User, RI.getOperandNo(), LI);
02081 }
02082 
02083 bool FastISel::canFoldAddIntoGEP(const User *GEP, const Value *Add) {
02084   // Must be an add.
02085   if (!isa<AddOperator>(Add))
02086     return false;
02087   // Type size needs to match.
02088   if (DL.getTypeSizeInBits(GEP->getType()) !=
02089       DL.getTypeSizeInBits(Add->getType()))
02090     return false;
02091   // Must be in the same basic block.
02092   if (isa<Instruction>(Add) &&
02093       FuncInfo.MBBMap[cast<Instruction>(Add)->getParent()] != FuncInfo.MBB)
02094     return false;
02095   // Must have a constant operand.
02096   return isa<ConstantInt>(cast<AddOperator>(Add)->getOperand(1));
02097 }
02098 
02099 MachineMemOperand *
02100 FastISel::createMachineMemOperandFor(const Instruction *I) const {
02101   const Value *Ptr;
02102   Type *ValTy;
02103   unsigned Alignment;
02104   unsigned Flags;
02105   bool IsVolatile;
02106 
02107   if (const auto *LI = dyn_cast<LoadInst>(I)) {
02108     Alignment = LI->getAlignment();
02109     IsVolatile = LI->isVolatile();
02110     Flags = MachineMemOperand::MOLoad;
02111     Ptr = LI->getPointerOperand();
02112     ValTy = LI->getType();
02113   } else if (const auto *SI = dyn_cast<StoreInst>(I)) {
02114     Alignment = SI->getAlignment();
02115     IsVolatile = SI->isVolatile();
02116     Flags = MachineMemOperand::MOStore;
02117     Ptr = SI->getPointerOperand();
02118     ValTy = SI->getValueOperand()->getType();
02119   } else
02120     return nullptr;
02121 
02122   bool IsNonTemporal = I->getMetadata("nontemporal") != nullptr;
02123   bool IsInvariant = I->getMetadata("invariant.load") != nullptr;
02124   const MDNode *Ranges = I->getMetadata(LLVMContext::MD_range);
02125 
02126   AAMDNodes AAInfo;
02127   I->getAAMetadata(AAInfo);
02128 
02129   if (Alignment == 0) // Ensure that codegen never sees alignment 0.
02130     Alignment = DL.getABITypeAlignment(ValTy);
02131 
02132   unsigned Size =
02133       TM.getSubtargetImpl()->getDataLayout()->getTypeStoreSize(ValTy);
02134 
02135   if (IsVolatile)
02136     Flags |= MachineMemOperand::MOVolatile;
02137   if (IsNonTemporal)
02138     Flags |= MachineMemOperand::MONonTemporal;
02139   if (IsInvariant)
02140     Flags |= MachineMemOperand::MOInvariant;
02141 
02142   return FuncInfo.MF->getMachineMemOperand(MachinePointerInfo(Ptr), Flags, Size,
02143                                            Alignment, AAInfo, Ranges);
02144 }
02145 
02146 CmpInst::Predicate FastISel::optimizeCmpPredicate(const CmpInst *CI) const {
02147   // If both operands are the same, then try to optimize or fold the cmp.
02148   CmpInst::Predicate Predicate = CI->getPredicate();
02149   if (CI->getOperand(0) != CI->getOperand(1))
02150     return Predicate;
02151 
02152   switch (Predicate) {
02153   default: llvm_unreachable("Invalid predicate!");
02154   case CmpInst::FCMP_FALSE: Predicate = CmpInst::FCMP_FALSE; break;
02155   case CmpInst::FCMP_OEQ:   Predicate = CmpInst::FCMP_ORD;   break;
02156   case CmpInst::FCMP_OGT:   Predicate = CmpInst::FCMP_FALSE; break;
02157   case CmpInst::FCMP_OGE:   Predicate = CmpInst::FCMP_ORD;   break;
02158   case CmpInst::FCMP_OLT:   Predicate = CmpInst::FCMP_FALSE; break;
02159   case CmpInst::FCMP_OLE:   Predicate = CmpInst::FCMP_ORD;   break;
02160   case CmpInst::FCMP_ONE:   Predicate = CmpInst::FCMP_FALSE; break;
02161   case CmpInst::FCMP_ORD:   Predicate = CmpInst::FCMP_ORD;   break;
02162   case CmpInst::FCMP_UNO:   Predicate = CmpInst::FCMP_UNO;   break;
02163   case CmpInst::FCMP_UEQ:   Predicate = CmpInst::FCMP_TRUE;  break;
02164   case CmpInst::FCMP_UGT:   Predicate = CmpInst::FCMP_UNO;   break;
02165   case CmpInst::FCMP_UGE:   Predicate = CmpInst::FCMP_TRUE;  break;
02166   case CmpInst::FCMP_ULT:   Predicate = CmpInst::FCMP_UNO;   break;
02167   case CmpInst::FCMP_ULE:   Predicate = CmpInst::FCMP_TRUE;  break;
02168   case CmpInst::FCMP_UNE:   Predicate = CmpInst::FCMP_UNO;   break;
02169   case CmpInst::FCMP_TRUE:  Predicate = CmpInst::FCMP_TRUE;  break;
02170 
02171   case CmpInst::ICMP_EQ:    Predicate = CmpInst::FCMP_TRUE;  break;
02172   case CmpInst::ICMP_NE:    Predicate = CmpInst::FCMP_FALSE; break;
02173   case CmpInst::ICMP_UGT:   Predicate = CmpInst::FCMP_FALSE; break;
02174   case CmpInst::ICMP_UGE:   Predicate = CmpInst::FCMP_TRUE;  break;
02175   case CmpInst::ICMP_ULT:   Predicate = CmpInst::FCMP_FALSE; break;
02176   case CmpInst::ICMP_ULE:   Predicate = CmpInst::FCMP_TRUE;  break;
02177   case CmpInst::ICMP_SGT:   Predicate = CmpInst::FCMP_FALSE; break;
02178   case CmpInst::ICMP_SGE:   Predicate = CmpInst::FCMP_TRUE;  break;
02179   case CmpInst::ICMP_SLT:   Predicate = CmpInst::FCMP_FALSE; break;
02180   case CmpInst::ICMP_SLE:   Predicate = CmpInst::FCMP_TRUE;  break;
02181   }
02182 
02183   return Predicate;
02184 }