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 #define DEBUG_TYPE "isel"
00043 #include "llvm/CodeGen/FastISel.h"
00044 #include "llvm/ADT/Statistic.h"
00045 #include "llvm/Analysis/Loads.h"
00046 #include "llvm/CodeGen/Analysis.h"
00047 #include "llvm/CodeGen/FunctionLoweringInfo.h"
00048 #include "llvm/CodeGen/MachineInstrBuilder.h"
00049 #include "llvm/CodeGen/MachineModuleInfo.h"
00050 #include "llvm/CodeGen/MachineRegisterInfo.h"
00051 #include "llvm/DebugInfo.h"
00052 #include "llvm/IR/DataLayout.h"
00053 #include "llvm/IR/Function.h"
00054 #include "llvm/IR/GlobalVariable.h"
00055 #include "llvm/IR/Instructions.h"
00056 #include "llvm/IR/IntrinsicInst.h"
00057 #include "llvm/IR/Operator.h"
00058 #include "llvm/Support/Debug.h"
00059 #include "llvm/Support/ErrorHandling.h"
00060 #include "llvm/Target/TargetInstrInfo.h"
00061 #include "llvm/Target/TargetLibraryInfo.h"
00062 #include "llvm/Target/TargetLowering.h"
00063 #include "llvm/Target/TargetMachine.h"
00064 using namespace llvm;
00065 
00066 STATISTIC(NumFastIselSuccessIndependent, "Number of insts selected by "
00067           "target-independent selector");
00068 STATISTIC(NumFastIselSuccessTarget, "Number of insts selected by "
00069           "target-specific selector");
00070 STATISTIC(NumFastIselDead, "Number of dead insts removed on failure");
00071 
00072 /// startNewBlock - Set the current block to which generated machine
00073 /// instructions will be appended, and clear the local CSE map.
00074 ///
00075 void FastISel::startNewBlock() {
00076   LocalValueMap.clear();
00077 
00078   EmitStartPt = 0;
00079 
00080   // Advance the emit start point past any EH_LABEL instructions.
00081   MachineBasicBlock::iterator
00082     I = FuncInfo.MBB->begin(), E = FuncInfo.MBB->end();
00083   while (I != E && I->getOpcode() == TargetOpcode::EH_LABEL) {
00084     EmitStartPt = I;
00085     ++I;
00086   }
00087   LastLocalValue = EmitStartPt;
00088 }
00089 
00090 bool FastISel::LowerArguments() {
00091   if (!FuncInfo.CanLowerReturn)
00092     // Fallback to SDISel argument lowering code to deal with sret pointer
00093     // parameter.
00094     return false;
00095   
00096   if (!FastLowerArguments())
00097     return false;
00098 
00099   // Enter non-dead arguments into ValueMap for uses in non-entry BBs.
00100   for (Function::const_arg_iterator I = FuncInfo.Fn->arg_begin(),
00101          E = FuncInfo.Fn->arg_end(); I != E; ++I) {
00102     if (!I->use_empty()) {
00103       DenseMap<const Value *, unsigned>::iterator VI = LocalValueMap.find(I);
00104       assert(VI != LocalValueMap.end() && "Missed an argument?");
00105       FuncInfo.ValueMap[I] = VI->second;
00106     }
00107   }
00108   return true;
00109 }
00110 
00111 void FastISel::flushLocalValueMap() {
00112   LocalValueMap.clear();
00113   LastLocalValue = EmitStartPt;
00114   recomputeInsertPt();
00115 }
00116 
00117 bool FastISel::hasTrivialKill(const Value *V) const {
00118   // Don't consider constants or arguments to have trivial kills.
00119   const Instruction *I = dyn_cast<Instruction>(V);
00120   if (!I)
00121     return false;
00122 
00123   // No-op casts are trivially coalesced by fast-isel.
00124   if (const CastInst *Cast = dyn_cast<CastInst>(I))
00125     if (Cast->isNoopCast(TD.getIntPtrType(Cast->getContext())) &&
00126         !hasTrivialKill(Cast->getOperand(0)))
00127       return false;
00128 
00129   // GEPs with all zero indices are trivially coalesced by fast-isel.
00130   if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I))
00131     if (GEP->hasAllZeroIndices() && !hasTrivialKill(GEP->getOperand(0)))
00132       return false;
00133 
00134   // Only instructions with a single use in the same basic block are considered
00135   // to have trivial kills.
00136   return I->hasOneUse() &&
00137          !(I->getOpcode() == Instruction::BitCast ||
00138            I->getOpcode() == Instruction::PtrToInt ||
00139            I->getOpcode() == Instruction::IntToPtr) &&
00140          cast<Instruction>(*I->use_begin())->getParent() == I->getParent();
00141 }
00142 
00143 unsigned FastISel::getRegForValue(const Value *V) {
00144   EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
00145   // Don't handle non-simple values in FastISel.
00146   if (!RealVT.isSimple())
00147     return 0;
00148 
00149   // Ignore illegal types. We must do this before looking up the value
00150   // in ValueMap because Arguments are given virtual registers regardless
00151   // of whether FastISel can handle them.
00152   MVT VT = RealVT.getSimpleVT();
00153   if (!TLI.isTypeLegal(VT)) {
00154     // Handle integer promotions, though, because they're common and easy.
00155     if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
00156       VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
00157     else
00158       return 0;
00159   }
00160 
00161   // Look up the value to see if we already have a register for it.
00162   unsigned Reg = lookUpRegForValue(V);
00163   if (Reg != 0)
00164     return Reg;
00165 
00166   // In bottom-up mode, just create the virtual register which will be used
00167   // to hold the value. It will be materialized later.
00168   if (isa<Instruction>(V) &&
00169       (!isa<AllocaInst>(V) ||
00170        !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(V))))
00171     return FuncInfo.InitializeRegForValue(V);
00172 
00173   SavePoint SaveInsertPt = enterLocalValueArea();
00174 
00175   // Materialize the value in a register. Emit any instructions in the
00176   // local value area.
00177   Reg = materializeRegForValue(V, VT);
00178 
00179   leaveLocalValueArea(SaveInsertPt);
00180 
00181   return Reg;
00182 }
00183 
00184 /// materializeRegForValue - Helper for getRegForValue. This function is
00185 /// called when the value isn't already available in a register and must
00186 /// be materialized with new instructions.
00187 unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) {
00188   unsigned Reg = 0;
00189 
00190   if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
00191     if (CI->getValue().getActiveBits() <= 64)
00192       Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
00193   } else if (isa<AllocaInst>(V)) {
00194     Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
00195   } else if (isa<ConstantPointerNull>(V)) {
00196     // Translate this as an integer zero so that it can be
00197     // local-CSE'd with actual integer zeros.
00198     Reg =
00199       getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext())));
00200   } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
00201     if (CF->isNullValue()) {
00202       Reg = TargetMaterializeFloatZero(CF);
00203     } else {
00204       // Try to emit the constant directly.
00205       Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
00206     }
00207 
00208     if (!Reg) {
00209       // Try to emit the constant by using an integer constant with a cast.
00210       const APFloat &Flt = CF->getValueAPF();
00211       EVT IntVT = TLI.getPointerTy();
00212 
00213       uint64_t x[2];
00214       uint32_t IntBitWidth = IntVT.getSizeInBits();
00215       bool isExact;
00216       (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
00217                                   APFloat::rmTowardZero, &isExact);
00218       if (isExact) {
00219         APInt IntVal(IntBitWidth, x);
00220 
00221         unsigned IntegerReg =
00222           getRegForValue(ConstantInt::get(V->getContext(), IntVal));
00223         if (IntegerReg != 0)
00224           Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP,
00225                            IntegerReg, /*Kill=*/false);
00226       }
00227     }
00228   } else if (const Operator *Op = dyn_cast<Operator>(V)) {
00229     if (!SelectOperator(Op, Op->getOpcode()))
00230       if (!isa<Instruction>(Op) ||
00231           !TargetSelectInstruction(cast<Instruction>(Op)))
00232         return 0;
00233     Reg = lookUpRegForValue(Op);
00234   } else if (isa<UndefValue>(V)) {
00235     Reg = createResultReg(TLI.getRegClassFor(VT));
00236     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
00237             TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
00238   }
00239 
00240   // If target-independent code couldn't handle the value, give target-specific
00241   // code a try.
00242   if (!Reg && isa<Constant>(V))
00243     Reg = TargetMaterializeConstant(cast<Constant>(V));
00244 
00245   // Don't cache constant materializations in the general ValueMap.
00246   // To do so would require tracking what uses they dominate.
00247   if (Reg != 0) {
00248     LocalValueMap[V] = Reg;
00249     LastLocalValue = MRI.getVRegDef(Reg);
00250   }
00251   return Reg;
00252 }
00253 
00254 unsigned FastISel::lookUpRegForValue(const Value *V) {
00255   // Look up the value to see if we already have a register for it. We
00256   // cache values defined by Instructions across blocks, and other values
00257   // only locally. This is because Instructions already have the SSA
00258   // def-dominates-use requirement enforced.
00259   DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
00260   if (I != FuncInfo.ValueMap.end())
00261     return I->second;
00262   return LocalValueMap[V];
00263 }
00264 
00265 /// UpdateValueMap - Update the value map to include the new mapping for this
00266 /// instruction, or insert an extra copy to get the result in a previous
00267 /// determined register.
00268 /// NOTE: This is only necessary because we might select a block that uses
00269 /// a value before we select the block that defines the value.  It might be
00270 /// possible to fix this by selecting blocks in reverse postorder.
00271 void FastISel::UpdateValueMap(const Value *I, unsigned Reg, unsigned NumRegs) {
00272   if (!isa<Instruction>(I)) {
00273     LocalValueMap[I] = Reg;
00274     return;
00275   }
00276 
00277   unsigned &AssignedReg = FuncInfo.ValueMap[I];
00278   if (AssignedReg == 0)
00279     // Use the new register.
00280     AssignedReg = Reg;
00281   else if (Reg != AssignedReg) {
00282     // Arrange for uses of AssignedReg to be replaced by uses of Reg.
00283     for (unsigned i = 0; i < NumRegs; i++)
00284       FuncInfo.RegFixups[AssignedReg+i] = Reg+i;
00285 
00286     AssignedReg = Reg;
00287   }
00288 }
00289 
00290 std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
00291   unsigned IdxN = getRegForValue(Idx);
00292   if (IdxN == 0)
00293     // Unhandled operand. Halt "fast" selection and bail.
00294     return std::pair<unsigned, bool>(0, false);
00295 
00296   bool IdxNIsKill = hasTrivialKill(Idx);
00297 
00298   // If the index is smaller or larger than intptr_t, truncate or extend it.
00299   MVT PtrVT = TLI.getPointerTy();
00300   EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
00301   if (IdxVT.bitsLT(PtrVT)) {
00302     IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND,
00303                       IdxN, IdxNIsKill);
00304     IdxNIsKill = true;
00305   }
00306   else if (IdxVT.bitsGT(PtrVT)) {
00307     IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE,
00308                       IdxN, IdxNIsKill);
00309     IdxNIsKill = true;
00310   }
00311   return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
00312 }
00313 
00314 void FastISel::recomputeInsertPt() {
00315   if (getLastLocalValue()) {
00316     FuncInfo.InsertPt = getLastLocalValue();
00317     FuncInfo.MBB = FuncInfo.InsertPt->getParent();
00318     ++FuncInfo.InsertPt;
00319   } else
00320     FuncInfo.InsertPt = FuncInfo.MBB->getFirstNonPHI();
00321 
00322   // Now skip past any EH_LABELs, which must remain at the beginning.
00323   while (FuncInfo.InsertPt != FuncInfo.MBB->end() &&
00324          FuncInfo.InsertPt->getOpcode() == TargetOpcode::EH_LABEL)
00325     ++FuncInfo.InsertPt;
00326 }
00327 
00328 void FastISel::removeDeadCode(MachineBasicBlock::iterator I,
00329                               MachineBasicBlock::iterator E) {
00330   assert (I && E && std::distance(I, E) > 0 && "Invalid iterator!");
00331   while (I != E) {
00332     MachineInstr *Dead = &*I;
00333     ++I;
00334     Dead->eraseFromParent();
00335     ++NumFastIselDead;
00336   }
00337   recomputeInsertPt();
00338 }
00339 
00340 FastISel::SavePoint FastISel::enterLocalValueArea() {
00341   MachineBasicBlock::iterator OldInsertPt = FuncInfo.InsertPt;
00342   DebugLoc OldDL = DL;
00343   recomputeInsertPt();
00344   DL = DebugLoc();
00345   SavePoint SP = { OldInsertPt, OldDL };
00346   return SP;
00347 }
00348 
00349 void FastISel::leaveLocalValueArea(SavePoint OldInsertPt) {
00350   if (FuncInfo.InsertPt != FuncInfo.MBB->begin())
00351     LastLocalValue = llvm::prior(FuncInfo.InsertPt);
00352 
00353   // Restore the previous insert position.
00354   FuncInfo.InsertPt = OldInsertPt.InsertPt;
00355   DL = OldInsertPt.DL;
00356 }
00357 
00358 /// SelectBinaryOp - Select and emit code for a binary operator instruction,
00359 /// which has an opcode which directly corresponds to the given ISD opcode.
00360 ///
00361 bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
00362   EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
00363   if (VT == MVT::Other || !VT.isSimple())
00364     // Unhandled type. Halt "fast" selection and bail.
00365     return false;
00366 
00367   // We only handle legal types. For example, on x86-32 the instruction
00368   // selector contains all of the 64-bit instructions from x86-64,
00369   // under the assumption that i64 won't be used if the target doesn't
00370   // support it.
00371   if (!TLI.isTypeLegal(VT)) {
00372     // MVT::i1 is special. Allow AND, OR, or XOR because they
00373     // don't require additional zeroing, which makes them easy.
00374     if (VT == MVT::i1 &&
00375         (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
00376          ISDOpcode == ISD::XOR))
00377       VT = TLI.getTypeToTransformTo(I->getContext(), VT);
00378     else
00379       return false;
00380   }
00381 
00382   // Check if the first operand is a constant, and handle it as "ri".  At -O0,
00383   // we don't have anything that canonicalizes operand order.
00384   if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(0)))
00385     if (isa<Instruction>(I) && cast<Instruction>(I)->isCommutative()) {
00386       unsigned Op1 = getRegForValue(I->getOperand(1));
00387       if (Op1 == 0) return false;
00388 
00389       bool Op1IsKill = hasTrivialKill(I->getOperand(1));
00390 
00391       unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op1,
00392                                         Op1IsKill, CI->getZExtValue(),
00393                                         VT.getSimpleVT());
00394       if (ResultReg == 0) return false;
00395 
00396       // We successfully emitted code for the given LLVM Instruction.
00397       UpdateValueMap(I, ResultReg);
00398       return true;
00399     }
00400 
00401 
00402   unsigned Op0 = getRegForValue(I->getOperand(0));
00403   if (Op0 == 0)   // Unhandled operand. Halt "fast" selection and bail.
00404     return false;
00405 
00406   bool Op0IsKill = hasTrivialKill(I->getOperand(0));
00407 
00408   // Check if the second operand is a constant and handle it appropriately.
00409   if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
00410     uint64_t Imm = CI->getZExtValue();
00411 
00412     // Transform "sdiv exact X, 8" -> "sra X, 3".
00413     if (ISDOpcode == ISD::SDIV && isa<BinaryOperator>(I) &&
00414         cast<BinaryOperator>(I)->isExact() &&
00415         isPowerOf2_64(Imm)) {
00416       Imm = Log2_64(Imm);
00417       ISDOpcode = ISD::SRA;
00418     }
00419 
00420     // Transform "urem x, pow2" -> "and x, pow2-1".
00421     if (ISDOpcode == ISD::UREM && isa<BinaryOperator>(I) &&
00422         isPowerOf2_64(Imm)) {
00423       --Imm;
00424       ISDOpcode = ISD::AND;
00425     }
00426 
00427     unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0,
00428                                       Op0IsKill, Imm, VT.getSimpleVT());
00429     if (ResultReg == 0) return false;
00430 
00431     // We successfully emitted code for the given LLVM Instruction.
00432     UpdateValueMap(I, ResultReg);
00433     return true;
00434   }
00435 
00436   // Check if the second operand is a constant float.
00437   if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
00438     unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
00439                                      ISDOpcode, Op0, Op0IsKill, CF);
00440     if (ResultReg != 0) {
00441       // We successfully emitted code for the given LLVM Instruction.
00442       UpdateValueMap(I, ResultReg);
00443       return true;
00444     }
00445   }
00446 
00447   unsigned Op1 = getRegForValue(I->getOperand(1));
00448   if (Op1 == 0)
00449     // Unhandled operand. Halt "fast" selection and bail.
00450     return false;
00451 
00452   bool Op1IsKill = hasTrivialKill(I->getOperand(1));
00453 
00454   // Now we have both operands in registers. Emit the instruction.
00455   unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
00456                                    ISDOpcode,
00457                                    Op0, Op0IsKill,
00458                                    Op1, Op1IsKill);
00459   if (ResultReg == 0)
00460     // Target-specific code wasn't able to find a machine opcode for
00461     // the given ISD opcode and type. Halt "fast" selection and bail.
00462     return false;
00463 
00464   // We successfully emitted code for the given LLVM Instruction.
00465   UpdateValueMap(I, ResultReg);
00466   return true;
00467 }
00468 
00469 bool FastISel::SelectGetElementPtr(const User *I) {
00470   unsigned N = getRegForValue(I->getOperand(0));
00471   if (N == 0)
00472     // Unhandled operand. Halt "fast" selection and bail.
00473     return false;
00474 
00475   bool NIsKill = hasTrivialKill(I->getOperand(0));
00476 
00477   // Keep a running tab of the total offset to coalesce multiple N = N + Offset
00478   // into a single N = N + TotalOffset.
00479   uint64_t TotalOffs = 0;
00480   // FIXME: What's a good SWAG number for MaxOffs?
00481   uint64_t MaxOffs = 2048;
00482   Type *Ty = I->getOperand(0)->getType();
00483   MVT VT = TLI.getPointerTy();
00484   for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1,
00485        E = I->op_end(); OI != E; ++OI) {
00486     const Value *Idx = *OI;
00487     if (StructType *StTy = dyn_cast<StructType>(Ty)) {
00488       unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
00489       if (Field) {
00490         // N = N + Offset
00491         TotalOffs += TD.getStructLayout(StTy)->getElementOffset(Field);
00492         if (TotalOffs >= MaxOffs) {
00493           N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
00494           if (N == 0)
00495             // Unhandled operand. Halt "fast" selection and bail.
00496             return false;
00497           NIsKill = true;
00498           TotalOffs = 0;
00499         }
00500       }
00501       Ty = StTy->getElementType(Field);
00502     } else {
00503       Ty = cast<SequentialType>(Ty)->getElementType();
00504 
00505       // If this is a constant subscript, handle it quickly.
00506       if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
00507         if (CI->isZero()) continue;
00508         // N = N + Offset
00509         TotalOffs +=
00510           TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
00511         if (TotalOffs >= MaxOffs) {
00512           N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
00513           if (N == 0)
00514             // Unhandled operand. Halt "fast" selection and bail.
00515             return false;
00516           NIsKill = true;
00517           TotalOffs = 0;
00518         }
00519         continue;
00520       }
00521       if (TotalOffs) {
00522         N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
00523         if (N == 0)
00524           // Unhandled operand. Halt "fast" selection and bail.
00525           return false;
00526         NIsKill = true;
00527         TotalOffs = 0;
00528       }
00529 
00530       // N = N + Idx * ElementSize;
00531       uint64_t ElementSize = TD.getTypeAllocSize(Ty);
00532       std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
00533       unsigned IdxN = Pair.first;
00534       bool IdxNIsKill = Pair.second;
00535       if (IdxN == 0)
00536         // Unhandled operand. Halt "fast" selection and bail.
00537         return false;
00538 
00539       if (ElementSize != 1) {
00540         IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
00541         if (IdxN == 0)
00542           // Unhandled operand. Halt "fast" selection and bail.
00543           return false;
00544         IdxNIsKill = true;
00545       }
00546       N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
00547       if (N == 0)
00548         // Unhandled operand. Halt "fast" selection and bail.
00549         return false;
00550     }
00551   }
00552   if (TotalOffs) {
00553     N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, TotalOffs, VT);
00554     if (N == 0)
00555       // Unhandled operand. Halt "fast" selection and bail.
00556       return false;
00557   }
00558 
00559   // We successfully emitted code for the given LLVM Instruction.
00560   UpdateValueMap(I, N);
00561   return true;
00562 }
00563 
00564 bool FastISel::SelectCall(const User *I) {
00565   const CallInst *Call = cast<CallInst>(I);
00566 
00567   // Handle simple inline asms.
00568   if (const InlineAsm *IA = dyn_cast<InlineAsm>(Call->getCalledValue())) {
00569     // Don't attempt to handle constraints.
00570     if (!IA->getConstraintString().empty())
00571       return false;
00572 
00573     unsigned ExtraInfo = 0;
00574     if (IA->hasSideEffects())
00575       ExtraInfo |= InlineAsm::Extra_HasSideEffects;
00576     if (IA->isAlignStack())
00577       ExtraInfo |= InlineAsm::Extra_IsAlignStack;
00578 
00579     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
00580             TII.get(TargetOpcode::INLINEASM))
00581       .addExternalSymbol(IA->getAsmString().c_str())
00582       .addImm(ExtraInfo);
00583     return true;
00584   }
00585 
00586   MachineModuleInfo &MMI = FuncInfo.MF->getMMI();
00587   ComputeUsesVAFloatArgument(*Call, &MMI);
00588 
00589   const Function *F = Call->getCalledFunction();
00590   if (!F) return false;
00591 
00592   // Handle selected intrinsic function calls.
00593   switch (F->getIntrinsicID()) {
00594   default: break;
00595     // At -O0 we don't care about the lifetime intrinsics.
00596   case Intrinsic::lifetime_start:
00597   case Intrinsic::lifetime_end:
00598     // The donothing intrinsic does, well, nothing.
00599   case Intrinsic::donothing:
00600     return true;
00601 
00602   case Intrinsic::dbg_declare: {
00603     const DbgDeclareInst *DI = cast<DbgDeclareInst>(Call);
00604     if (!DIVariable(DI->getVariable()).Verify() ||
00605         !FuncInfo.MF->getMMI().hasDebugInfo()) {
00606       DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
00607       return true;
00608     }
00609 
00610     const Value *Address = DI->getAddress();
00611     if (!Address || isa<UndefValue>(Address)) {
00612       DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
00613       return true;
00614     }
00615 
00616     unsigned Reg = 0;
00617     unsigned Offset = 0;
00618     if (const Argument *Arg = dyn_cast<Argument>(Address)) {
00619       // Some arguments' frame index is recorded during argument lowering.
00620       Offset = FuncInfo.getArgumentFrameIndex(Arg);
00621       if (Offset)
00622         Reg = TRI.getFrameRegister(*FuncInfo.MF);
00623     }
00624     if (!Reg)
00625       Reg = lookUpRegForValue(Address);
00626 
00627     // If we have a VLA that has a "use" in a metadata node that's then used
00628     // here but it has no other uses, then we have a problem. E.g.,
00629     //
00630     //   int foo (const int *x) {
00631     //     char a[*x];
00632     //     return 0;
00633     //   }
00634     //
00635     // If we assign 'a' a vreg and fast isel later on has to use the selection
00636     // DAG isel, it will want to copy the value to the vreg. However, there are
00637     // no uses, which goes counter to what selection DAG isel expects.
00638     if (!Reg && !Address->use_empty() && isa<Instruction>(Address) &&
00639         (!isa<AllocaInst>(Address) ||
00640          !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(Address))))
00641       Reg = FuncInfo.InitializeRegForValue(Address);
00642 
00643     if (Reg)
00644       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
00645               TII.get(TargetOpcode::DBG_VALUE))
00646         .addReg(Reg, RegState::Debug).addImm(Offset)
00647         .addMetadata(DI->getVariable());
00648     else
00649       // We can't yet handle anything else here because it would require
00650       // generating code, thus altering codegen because of debug info.
00651       DEBUG(dbgs() << "Dropping debug info for " << DI);
00652     return true;
00653   }
00654   case Intrinsic::dbg_value: {
00655     // This form of DBG_VALUE is target-independent.
00656     const DbgValueInst *DI = cast<DbgValueInst>(Call);
00657     const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
00658     const Value *V = DI->getValue();
00659     if (!V) {
00660       // Currently the optimizer can produce this; insert an undef to
00661       // help debugging.  Probably the optimizer should not do this.
00662       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
00663         .addReg(0U).addImm(DI->getOffset())
00664         .addMetadata(DI->getVariable());
00665     } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
00666       if (CI->getBitWidth() > 64)
00667         BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
00668           .addCImm(CI).addImm(DI->getOffset())
00669           .addMetadata(DI->getVariable());
00670       else
00671         BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
00672           .addImm(CI->getZExtValue()).addImm(DI->getOffset())
00673           .addMetadata(DI->getVariable());
00674     } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
00675       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
00676         .addFPImm(CF).addImm(DI->getOffset())
00677         .addMetadata(DI->getVariable());
00678     } else if (unsigned Reg = lookUpRegForValue(V)) {
00679       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
00680         .addReg(Reg, RegState::Debug).addImm(DI->getOffset())
00681         .addMetadata(DI->getVariable());
00682     } else {
00683       // We can't yet handle anything else here because it would require
00684       // generating code, thus altering codegen because of debug info.
00685       DEBUG(dbgs() << "Dropping debug info for " << DI);
00686     }
00687     return true;
00688   }
00689   case Intrinsic::objectsize: {
00690     ConstantInt *CI = cast<ConstantInt>(Call->getArgOperand(1));
00691     unsigned long long Res = CI->isZero() ? -1ULL : 0;
00692     Constant *ResCI = ConstantInt::get(Call->getType(), Res);
00693     unsigned ResultReg = getRegForValue(ResCI);
00694     if (ResultReg == 0)
00695       return false;
00696     UpdateValueMap(Call, ResultReg);
00697     return true;
00698   }
00699   case Intrinsic::expect: {
00700     unsigned ResultReg = getRegForValue(Call->getArgOperand(0));
00701     if (ResultReg == 0)
00702       return false;
00703     UpdateValueMap(Call, ResultReg);
00704     return true;
00705   }
00706   }
00707 
00708   // Usually, it does not make sense to initialize a value,
00709   // make an unrelated function call and use the value, because
00710   // it tends to be spilled on the stack. So, we move the pointer
00711   // to the last local value to the beginning of the block, so that
00712   // all the values which have already been materialized,
00713   // appear after the call. It also makes sense to skip intrinsics
00714   // since they tend to be inlined.
00715   if (!isa<IntrinsicInst>(Call))
00716     flushLocalValueMap();
00717 
00718   // An arbitrary call. Bail.
00719   return false;
00720 }
00721 
00722 bool FastISel::SelectCast(const User *I, unsigned Opcode) {
00723   EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
00724   EVT DstVT = TLI.getValueType(I->getType());
00725 
00726   if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
00727       DstVT == MVT::Other || !DstVT.isSimple())
00728     // Unhandled type. Halt "fast" selection and bail.
00729     return false;
00730 
00731   // Check if the destination type is legal.
00732   if (!TLI.isTypeLegal(DstVT))
00733     return false;
00734 
00735   // Check if the source operand is legal.
00736   if (!TLI.isTypeLegal(SrcVT))
00737     return false;
00738 
00739   unsigned InputReg = getRegForValue(I->getOperand(0));
00740   if (!InputReg)
00741     // Unhandled operand.  Halt "fast" selection and bail.
00742     return false;
00743 
00744   bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
00745 
00746   unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
00747                                   DstVT.getSimpleVT(),
00748                                   Opcode,
00749                                   InputReg, InputRegIsKill);
00750   if (!ResultReg)
00751     return false;
00752 
00753   UpdateValueMap(I, ResultReg);
00754   return true;
00755 }
00756 
00757 bool FastISel::SelectBitCast(const User *I) {
00758   // If the bitcast doesn't change the type, just use the operand value.
00759   if (I->getType() == I->getOperand(0)->getType()) {
00760     unsigned Reg = getRegForValue(I->getOperand(0));
00761     if (Reg == 0)
00762       return false;
00763     UpdateValueMap(I, Reg);
00764     return true;
00765   }
00766 
00767   // Bitcasts of other values become reg-reg copies or BITCAST operators.
00768   EVT SrcEVT = TLI.getValueType(I->getOperand(0)->getType());
00769   EVT DstEVT = TLI.getValueType(I->getType());
00770   if (SrcEVT == MVT::Other || DstEVT == MVT::Other ||
00771       !TLI.isTypeLegal(SrcEVT) || !TLI.isTypeLegal(DstEVT))
00772     // Unhandled type. Halt "fast" selection and bail.
00773     return false;
00774 
00775   MVT SrcVT = SrcEVT.getSimpleVT();
00776   MVT DstVT = DstEVT.getSimpleVT();
00777   unsigned Op0 = getRegForValue(I->getOperand(0));
00778   if (Op0 == 0)
00779     // Unhandled operand. Halt "fast" selection and bail.
00780     return false;
00781 
00782   bool Op0IsKill = hasTrivialKill(I->getOperand(0));
00783 
00784   // First, try to perform the bitcast by inserting a reg-reg copy.
00785   unsigned ResultReg = 0;
00786   if (SrcVT == DstVT) {
00787     const TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
00788     const TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
00789     // Don't attempt a cross-class copy. It will likely fail.
00790     if (SrcClass == DstClass) {
00791       ResultReg = createResultReg(DstClass);
00792       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
00793               ResultReg).addReg(Op0);
00794     }
00795   }
00796 
00797   // If the reg-reg copy failed, select a BITCAST opcode.
00798   if (!ResultReg)
00799     ResultReg = FastEmit_r(SrcVT, DstVT, ISD::BITCAST, Op0, Op0IsKill);
00800 
00801   if (!ResultReg)
00802     return false;
00803 
00804   UpdateValueMap(I, ResultReg);
00805   return true;
00806 }
00807 
00808 bool
00809 FastISel::SelectInstruction(const Instruction *I) {
00810   // Just before the terminator instruction, insert instructions to
00811   // feed PHI nodes in successor blocks.
00812   if (isa<TerminatorInst>(I))
00813     if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
00814       return false;
00815 
00816   DL = I->getDebugLoc();
00817 
00818   MachineBasicBlock::iterator SavedInsertPt = FuncInfo.InsertPt;
00819 
00820   // As a special case, don't handle calls to builtin library functions that
00821   // may be translated directly to target instructions.
00822   if (const CallInst *Call = dyn_cast<CallInst>(I)) {
00823     const Function *F = Call->getCalledFunction();
00824     LibFunc::Func Func;
00825     if (F && !F->hasLocalLinkage() && F->hasName() &&
00826         LibInfo->getLibFunc(F->getName(), Func) &&
00827         LibInfo->hasOptimizedCodeGen(Func))
00828       return false;
00829   }
00830 
00831   // First, try doing target-independent selection.
00832   if (SelectOperator(I, I->getOpcode())) {
00833     ++NumFastIselSuccessIndependent;
00834     DL = DebugLoc();
00835     return true;
00836   }
00837   // Remove dead code.  However, ignore call instructions since we've flushed
00838   // the local value map and recomputed the insert point.
00839   if (!isa<CallInst>(I)) {
00840     recomputeInsertPt();
00841     if (SavedInsertPt != FuncInfo.InsertPt)
00842       removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
00843   }
00844 
00845   // Next, try calling the target to attempt to handle the instruction.
00846   SavedInsertPt = FuncInfo.InsertPt;
00847   if (TargetSelectInstruction(I)) {
00848     ++NumFastIselSuccessTarget;
00849     DL = DebugLoc();
00850     return true;
00851   }
00852   // Check for dead code and remove as necessary.
00853   recomputeInsertPt();
00854   if (SavedInsertPt != FuncInfo.InsertPt)
00855     removeDeadCode(FuncInfo.InsertPt, SavedInsertPt);
00856 
00857   DL = DebugLoc();
00858   return false;
00859 }
00860 
00861 /// FastEmitBranch - Emit an unconditional branch to the given block,
00862 /// unless it is the immediate (fall-through) successor, and update
00863 /// the CFG.
00864 void
00865 FastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DL) {
00866 
00867   if (FuncInfo.MBB->getBasicBlock()->size() > 1 &&
00868       FuncInfo.MBB->isLayoutSuccessor(MSucc)) {
00869     // For more accurate line information if this is the only instruction
00870     // in the block then emit it, otherwise we have the unconditional
00871     // fall-through case, which needs no instructions.
00872   } else {
00873     // The unconditional branch case.
00874     TII.InsertBranch(*FuncInfo.MBB, MSucc, NULL,
00875                      SmallVector<MachineOperand, 0>(), DL);
00876   }
00877   FuncInfo.MBB->addSuccessor(MSucc);
00878 }
00879 
00880 /// SelectFNeg - Emit an FNeg operation.
00881 ///
00882 bool
00883 FastISel::SelectFNeg(const User *I) {
00884   unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
00885   if (OpReg == 0) return false;
00886 
00887   bool OpRegIsKill = hasTrivialKill(I);
00888 
00889   // If the target has ISD::FNEG, use it.
00890   EVT VT = TLI.getValueType(I->getType());
00891   unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
00892                                   ISD::FNEG, OpReg, OpRegIsKill);
00893   if (ResultReg != 0) {
00894     UpdateValueMap(I, ResultReg);
00895     return true;
00896   }
00897 
00898   // Bitcast the value to integer, twiddle the sign bit with xor,
00899   // and then bitcast it back to floating-point.
00900   if (VT.getSizeInBits() > 64) return false;
00901   EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
00902   if (!TLI.isTypeLegal(IntVT))
00903     return false;
00904 
00905   unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
00906                                ISD::BITCAST, OpReg, OpRegIsKill);
00907   if (IntReg == 0)
00908     return false;
00909 
00910   unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR,
00911                                        IntReg, /*Kill=*/true,
00912                                        UINT64_C(1) << (VT.getSizeInBits()-1),
00913                                        IntVT.getSimpleVT());
00914   if (IntResultReg == 0)
00915     return false;
00916 
00917   ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
00918                          ISD::BITCAST, IntResultReg, /*Kill=*/true);
00919   if (ResultReg == 0)
00920     return false;
00921 
00922   UpdateValueMap(I, ResultReg);
00923   return true;
00924 }
00925 
00926 bool
00927 FastISel::SelectExtractValue(const User *U) {
00928   const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(U);
00929   if (!EVI)
00930     return false;
00931 
00932   // Make sure we only try to handle extracts with a legal result.  But also
00933   // allow i1 because it's easy.
00934   EVT RealVT = TLI.getValueType(EVI->getType(), /*AllowUnknown=*/true);
00935   if (!RealVT.isSimple())
00936     return false;
00937   MVT VT = RealVT.getSimpleVT();
00938   if (!TLI.isTypeLegal(VT) && VT != MVT::i1)
00939     return false;
00940 
00941   const Value *Op0 = EVI->getOperand(0);
00942   Type *AggTy = Op0->getType();
00943 
00944   // Get the base result register.
00945   unsigned ResultReg;
00946   DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(Op0);
00947   if (I != FuncInfo.ValueMap.end())
00948     ResultReg = I->second;
00949   else if (isa<Instruction>(Op0))
00950     ResultReg = FuncInfo.InitializeRegForValue(Op0);
00951   else
00952     return false; // fast-isel can't handle aggregate constants at the moment
00953 
00954   // Get the actual result register, which is an offset from the base register.
00955   unsigned VTIndex = ComputeLinearIndex(AggTy, EVI->getIndices());
00956 
00957   SmallVector<EVT, 4> AggValueVTs;
00958   ComputeValueVTs(TLI, AggTy, AggValueVTs);
00959 
00960   for (unsigned i = 0; i < VTIndex; i++)
00961     ResultReg += TLI.getNumRegisters(FuncInfo.Fn->getContext(), AggValueVTs[i]);
00962 
00963   UpdateValueMap(EVI, ResultReg);
00964   return true;
00965 }
00966 
00967 bool
00968 FastISel::SelectOperator(const User *I, unsigned Opcode) {
00969   switch (Opcode) {
00970   case Instruction::Add:
00971     return SelectBinaryOp(I, ISD::ADD);
00972   case Instruction::FAdd:
00973     return SelectBinaryOp(I, ISD::FADD);
00974   case Instruction::Sub:
00975     return SelectBinaryOp(I, ISD::SUB);
00976   case Instruction::FSub:
00977     // FNeg is currently represented in LLVM IR as a special case of FSub.
00978     if (BinaryOperator::isFNeg(I))
00979       return SelectFNeg(I);
00980     return SelectBinaryOp(I, ISD::FSUB);
00981   case Instruction::Mul:
00982     return SelectBinaryOp(I, ISD::MUL);
00983   case Instruction::FMul:
00984     return SelectBinaryOp(I, ISD::FMUL);
00985   case Instruction::SDiv:
00986     return SelectBinaryOp(I, ISD::SDIV);
00987   case Instruction::UDiv:
00988     return SelectBinaryOp(I, ISD::UDIV);
00989   case Instruction::FDiv:
00990     return SelectBinaryOp(I, ISD::FDIV);
00991   case Instruction::SRem:
00992     return SelectBinaryOp(I, ISD::SREM);
00993   case Instruction::URem:
00994     return SelectBinaryOp(I, ISD::UREM);
00995   case Instruction::FRem:
00996     return SelectBinaryOp(I, ISD::FREM);
00997   case Instruction::Shl:
00998     return SelectBinaryOp(I, ISD::SHL);
00999   case Instruction::LShr:
01000     return SelectBinaryOp(I, ISD::SRL);
01001   case Instruction::AShr:
01002     return SelectBinaryOp(I, ISD::SRA);
01003   case Instruction::And:
01004     return SelectBinaryOp(I, ISD::AND);
01005   case Instruction::Or:
01006     return SelectBinaryOp(I, ISD::OR);
01007   case Instruction::Xor:
01008     return SelectBinaryOp(I, ISD::XOR);
01009 
01010   case Instruction::GetElementPtr:
01011     return SelectGetElementPtr(I);
01012 
01013   case Instruction::Br: {
01014     const BranchInst *BI = cast<BranchInst>(I);
01015 
01016     if (BI->isUnconditional()) {
01017       const BasicBlock *LLVMSucc = BI->getSuccessor(0);
01018       MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc];
01019       FastEmitBranch(MSucc, BI->getDebugLoc());
01020       return true;
01021     }
01022 
01023     // Conditional branches are not handed yet.
01024     // Halt "fast" selection and bail.
01025     return false;
01026   }
01027 
01028   case Instruction::Unreachable:
01029     // Nothing to emit.
01030     return true;
01031 
01032   case Instruction::Alloca:
01033     // FunctionLowering has the static-sized case covered.
01034     if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I)))
01035       return true;
01036 
01037     // Dynamic-sized alloca is not handled yet.
01038     return false;
01039 
01040   case Instruction::Call:
01041     return SelectCall(I);
01042 
01043   case Instruction::BitCast:
01044     return SelectBitCast(I);
01045 
01046   case Instruction::FPToSI:
01047     return SelectCast(I, ISD::FP_TO_SINT);
01048   case Instruction::ZExt:
01049     return SelectCast(I, ISD::ZERO_EXTEND);
01050   case Instruction::SExt:
01051     return SelectCast(I, ISD::SIGN_EXTEND);
01052   case Instruction::Trunc:
01053     return SelectCast(I, ISD::TRUNCATE);
01054   case Instruction::SIToFP:
01055     return SelectCast(I, ISD::SINT_TO_FP);
01056 
01057   case Instruction::IntToPtr: // Deliberate fall-through.
01058   case Instruction::PtrToInt: {
01059     EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
01060     EVT DstVT = TLI.getValueType(I->getType());
01061     if (DstVT.bitsGT(SrcVT))
01062       return SelectCast(I, ISD::ZERO_EXTEND);
01063     if (DstVT.bitsLT(SrcVT))
01064       return SelectCast(I, ISD::TRUNCATE);
01065     unsigned Reg = getRegForValue(I->getOperand(0));
01066     if (Reg == 0) return false;
01067     UpdateValueMap(I, Reg);
01068     return true;
01069   }
01070 
01071   case Instruction::ExtractValue:
01072     return SelectExtractValue(I);
01073 
01074   case Instruction::PHI:
01075     llvm_unreachable("FastISel shouldn't visit PHI nodes!");
01076 
01077   default:
01078     // Unhandled instruction. Halt "fast" selection and bail.
01079     return false;
01080   }
01081 }
01082 
01083 FastISel::FastISel(FunctionLoweringInfo &funcInfo,
01084                    const TargetLibraryInfo *libInfo)
01085   : FuncInfo(funcInfo),
01086     MRI(FuncInfo.MF->getRegInfo()),
01087     MFI(*FuncInfo.MF->getFrameInfo()),
01088     MCP(*FuncInfo.MF->getConstantPool()),
01089     TM(FuncInfo.MF->getTarget()),
01090     TD(*TM.getDataLayout()),
01091     TII(*TM.getInstrInfo()),
01092     TLI(*TM.getTargetLowering()),
01093     TRI(*TM.getRegisterInfo()),
01094     LibInfo(libInfo) {
01095 }
01096 
01097 FastISel::~FastISel() {}
01098 
01099 bool FastISel::FastLowerArguments() {
01100   return false;
01101 }
01102 
01103 unsigned FastISel::FastEmit_(MVT, MVT,
01104                              unsigned) {
01105   return 0;
01106 }
01107 
01108 unsigned FastISel::FastEmit_r(MVT, MVT,
01109                               unsigned,
01110                               unsigned /*Op0*/, bool /*Op0IsKill*/) {
01111   return 0;
01112 }
01113 
01114 unsigned FastISel::FastEmit_rr(MVT, MVT,
01115                                unsigned,
01116                                unsigned /*Op0*/, bool /*Op0IsKill*/,
01117                                unsigned /*Op1*/, bool /*Op1IsKill*/) {
01118   return 0;
01119 }
01120 
01121 unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
01122   return 0;
01123 }
01124 
01125 unsigned FastISel::FastEmit_f(MVT, MVT,
01126                               unsigned, const ConstantFP * /*FPImm*/) {
01127   return 0;
01128 }
01129 
01130 unsigned FastISel::FastEmit_ri(MVT, MVT,
01131                                unsigned,
01132                                unsigned /*Op0*/, bool /*Op0IsKill*/,
01133                                uint64_t /*Imm*/) {
01134   return 0;
01135 }
01136 
01137 unsigned FastISel::FastEmit_rf(MVT, MVT,
01138                                unsigned,
01139                                unsigned /*Op0*/, bool /*Op0IsKill*/,
01140                                const ConstantFP * /*FPImm*/) {
01141   return 0;
01142 }
01143 
01144 unsigned FastISel::FastEmit_rri(MVT, MVT,
01145                                 unsigned,
01146                                 unsigned /*Op0*/, bool /*Op0IsKill*/,
01147                                 unsigned /*Op1*/, bool /*Op1IsKill*/,
01148                                 uint64_t /*Imm*/) {
01149   return 0;
01150 }
01151 
01152 /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
01153 /// to emit an instruction with an immediate operand using FastEmit_ri.
01154 /// If that fails, it materializes the immediate into a register and try
01155 /// FastEmit_rr instead.
01156 unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
01157                                 unsigned Op0, bool Op0IsKill,
01158                                 uint64_t Imm, MVT ImmType) {
01159   // If this is a multiply by a power of two, emit this as a shift left.
01160   if (Opcode == ISD::MUL && isPowerOf2_64(Imm)) {
01161     Opcode = ISD::SHL;
01162     Imm = Log2_64(Imm);
01163   } else if (Opcode == ISD::UDIV && isPowerOf2_64(Imm)) {
01164     // div x, 8 -> srl x, 3
01165     Opcode = ISD::SRL;
01166     Imm = Log2_64(Imm);
01167   }
01168 
01169   // Horrible hack (to be removed), check to make sure shift amounts are
01170   // in-range.
01171   if ((Opcode == ISD::SHL || Opcode == ISD::SRA || Opcode == ISD::SRL) &&
01172       Imm >= VT.getSizeInBits())
01173     return 0;
01174 
01175   // First check if immediate type is legal. If not, we can't use the ri form.
01176   unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
01177   if (ResultReg != 0)
01178     return ResultReg;
01179   unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
01180   if (MaterialReg == 0) {
01181     // This is a bit ugly/slow, but failing here means falling out of
01182     // fast-isel, which would be very slow.
01183     IntegerType *ITy = IntegerType::get(FuncInfo.Fn->getContext(),
01184                                               VT.getSizeInBits());
01185     MaterialReg = getRegForValue(ConstantInt::get(ITy, Imm));
01186     assert (MaterialReg != 0 && "Unable to materialize imm.");
01187     if (MaterialReg == 0) return 0;
01188   }
01189   return FastEmit_rr(VT, VT, Opcode,
01190                      Op0, Op0IsKill,
01191                      MaterialReg, /*Kill=*/true);
01192 }
01193 
01194 unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
01195   return MRI.createVirtualRegister(RC);
01196 }
01197 
01198 unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
01199                                  const TargetRegisterClass* RC) {
01200   unsigned ResultReg = createResultReg(RC);
01201   const MCInstrDesc &II = TII.get(MachineInstOpcode);
01202 
01203   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg);
01204   return ResultReg;
01205 }
01206 
01207 unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
01208                                   const TargetRegisterClass *RC,
01209                                   unsigned Op0, bool Op0IsKill) {
01210   unsigned ResultReg = createResultReg(RC);
01211   const MCInstrDesc &II = TII.get(MachineInstOpcode);
01212 
01213   if (II.getNumDefs() >= 1)
01214     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
01215       .addReg(Op0, Op0IsKill * RegState::Kill);
01216   else {
01217     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
01218       .addReg(Op0, Op0IsKill * RegState::Kill);
01219     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
01220             ResultReg).addReg(II.ImplicitDefs[0]);
01221   }
01222 
01223   return ResultReg;
01224 }
01225 
01226 unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
01227                                    const TargetRegisterClass *RC,
01228                                    unsigned Op0, bool Op0IsKill,
01229                                    unsigned Op1, bool Op1IsKill) {
01230   unsigned ResultReg = createResultReg(RC);
01231   const MCInstrDesc &II = TII.get(MachineInstOpcode);
01232 
01233   if (II.getNumDefs() >= 1)
01234     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
01235       .addReg(Op0, Op0IsKill * RegState::Kill)
01236       .addReg(Op1, Op1IsKill * RegState::Kill);
01237   else {
01238     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
01239       .addReg(Op0, Op0IsKill * RegState::Kill)
01240       .addReg(Op1, Op1IsKill * RegState::Kill);
01241     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
01242             ResultReg).addReg(II.ImplicitDefs[0]);
01243   }
01244   return ResultReg;
01245 }
01246 
01247 unsigned FastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
01248                                    const TargetRegisterClass *RC,
01249                                    unsigned Op0, bool Op0IsKill,
01250                                    unsigned Op1, bool Op1IsKill,
01251                                    unsigned Op2, bool Op2IsKill) {
01252   unsigned ResultReg = createResultReg(RC);
01253   const MCInstrDesc &II = TII.get(MachineInstOpcode);
01254 
01255   if (II.getNumDefs() >= 1)
01256     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
01257       .addReg(Op0, Op0IsKill * RegState::Kill)
01258       .addReg(Op1, Op1IsKill * RegState::Kill)
01259       .addReg(Op2, Op2IsKill * RegState::Kill);
01260   else {
01261     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
01262       .addReg(Op0, Op0IsKill * RegState::Kill)
01263       .addReg(Op1, Op1IsKill * RegState::Kill)
01264       .addReg(Op2, Op2IsKill * RegState::Kill);
01265     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
01266             ResultReg).addReg(II.ImplicitDefs[0]);
01267   }
01268   return ResultReg;
01269 }
01270 
01271 unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
01272                                    const TargetRegisterClass *RC,
01273                                    unsigned Op0, bool Op0IsKill,
01274                                    uint64_t Imm) {
01275   unsigned ResultReg = createResultReg(RC);
01276   const MCInstrDesc &II = TII.get(MachineInstOpcode);
01277 
01278   if (II.getNumDefs() >= 1)
01279     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
01280       .addReg(Op0, Op0IsKill * RegState::Kill)
01281       .addImm(Imm);
01282   else {
01283     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
01284       .addReg(Op0, Op0IsKill * RegState::Kill)
01285       .addImm(Imm);
01286     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
01287             ResultReg).addReg(II.ImplicitDefs[0]);
01288   }
01289   return ResultReg;
01290 }
01291 
01292 unsigned FastISel::FastEmitInst_rii(unsigned MachineInstOpcode,
01293                                    const TargetRegisterClass *RC,
01294                                    unsigned Op0, bool Op0IsKill,
01295                                    uint64_t Imm1, uint64_t Imm2) {
01296   unsigned ResultReg = createResultReg(RC);
01297   const MCInstrDesc &II = TII.get(MachineInstOpcode);
01298 
01299   if (II.getNumDefs() >= 1)
01300     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
01301       .addReg(Op0, Op0IsKill * RegState::Kill)
01302       .addImm(Imm1)
01303       .addImm(Imm2);
01304   else {
01305     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
01306       .addReg(Op0, Op0IsKill * RegState::Kill)
01307       .addImm(Imm1)
01308       .addImm(Imm2);
01309     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
01310             ResultReg).addReg(II.ImplicitDefs[0]);
01311   }
01312   return ResultReg;
01313 }
01314 
01315 unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
01316                                    const TargetRegisterClass *RC,
01317                                    unsigned Op0, bool Op0IsKill,
01318                                    const ConstantFP *FPImm) {
01319   unsigned ResultReg = createResultReg(RC);
01320   const MCInstrDesc &II = TII.get(MachineInstOpcode);
01321 
01322   if (II.getNumDefs() >= 1)
01323     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
01324       .addReg(Op0, Op0IsKill * RegState::Kill)
01325       .addFPImm(FPImm);
01326   else {
01327     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
01328       .addReg(Op0, Op0IsKill * RegState::Kill)
01329       .addFPImm(FPImm);
01330     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
01331             ResultReg).addReg(II.ImplicitDefs[0]);
01332   }
01333   return ResultReg;
01334 }
01335 
01336 unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
01337                                     const TargetRegisterClass *RC,
01338                                     unsigned Op0, bool Op0IsKill,
01339                                     unsigned Op1, bool Op1IsKill,
01340                                     uint64_t Imm) {
01341   unsigned ResultReg = createResultReg(RC);
01342   const MCInstrDesc &II = TII.get(MachineInstOpcode);
01343 
01344   if (II.getNumDefs() >= 1)
01345     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
01346       .addReg(Op0, Op0IsKill * RegState::Kill)
01347       .addReg(Op1, Op1IsKill * RegState::Kill)
01348       .addImm(Imm);
01349   else {
01350     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
01351       .addReg(Op0, Op0IsKill * RegState::Kill)
01352       .addReg(Op1, Op1IsKill * RegState::Kill)
01353       .addImm(Imm);
01354     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
01355             ResultReg).addReg(II.ImplicitDefs[0]);
01356   }
01357   return ResultReg;
01358 }
01359 
01360 unsigned FastISel::FastEmitInst_rrii(unsigned MachineInstOpcode,
01361                                      const TargetRegisterClass *RC,
01362                                      unsigned Op0, bool Op0IsKill,
01363                                      unsigned Op1, bool Op1IsKill,
01364                                      uint64_t Imm1, uint64_t Imm2) {
01365   unsigned ResultReg = createResultReg(RC);
01366   const MCInstrDesc &II = TII.get(MachineInstOpcode);
01367 
01368   if (II.getNumDefs() >= 1)
01369     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
01370       .addReg(Op0, Op0IsKill * RegState::Kill)
01371       .addReg(Op1, Op1IsKill * RegState::Kill)
01372       .addImm(Imm1).addImm(Imm2);
01373   else {
01374     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
01375       .addReg(Op0, Op0IsKill * RegState::Kill)
01376       .addReg(Op1, Op1IsKill * RegState::Kill)
01377       .addImm(Imm1).addImm(Imm2);
01378     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
01379             ResultReg).addReg(II.ImplicitDefs[0]);
01380   }
01381   return ResultReg;
01382 }
01383 
01384 unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
01385                                   const TargetRegisterClass *RC,
01386                                   uint64_t Imm) {
01387   unsigned ResultReg = createResultReg(RC);
01388   const MCInstrDesc &II = TII.get(MachineInstOpcode);
01389 
01390   if (II.getNumDefs() >= 1)
01391     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg).addImm(Imm);
01392   else {
01393     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm);
01394     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
01395             ResultReg).addReg(II.ImplicitDefs[0]);
01396   }
01397   return ResultReg;
01398 }
01399 
01400 unsigned FastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
01401                                   const TargetRegisterClass *RC,
01402                                   uint64_t Imm1, uint64_t Imm2) {
01403   unsigned ResultReg = createResultReg(RC);
01404   const MCInstrDesc &II = TII.get(MachineInstOpcode);
01405 
01406   if (II.getNumDefs() >= 1)
01407     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
01408       .addImm(Imm1).addImm(Imm2);
01409   else {
01410     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm1).addImm(Imm2);
01411     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
01412             ResultReg).addReg(II.ImplicitDefs[0]);
01413   }
01414   return ResultReg;
01415 }
01416 
01417 unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
01418                                               unsigned Op0, bool Op0IsKill,
01419                                               uint32_t Idx) {
01420   unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
01421   assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
01422          "Cannot yet extract from physregs");
01423   const TargetRegisterClass *RC = MRI.getRegClass(Op0);
01424   MRI.constrainRegClass(Op0, TRI.getSubClassWithSubReg(RC, Idx));
01425   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
01426           DL, TII.get(TargetOpcode::COPY), ResultReg)
01427     .addReg(Op0, getKillRegState(Op0IsKill), Idx);
01428   return ResultReg;
01429 }
01430 
01431 /// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
01432 /// with all but the least significant bit set to zero.
01433 unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
01434   return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
01435 }
01436 
01437 /// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
01438 /// Emit code to ensure constants are copied into registers when needed.
01439 /// Remember the virtual registers that need to be added to the Machine PHI
01440 /// nodes as input.  We cannot just directly add them, because expansion
01441 /// might result in multiple MBB's for one BB.  As such, the start of the
01442 /// BB might correspond to a different MBB than the end.
01443 bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
01444   const TerminatorInst *TI = LLVMBB->getTerminator();
01445 
01446   SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
01447   unsigned OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size();
01448 
01449   // Check successor nodes' PHI nodes that expect a constant to be available
01450   // from this block.
01451   for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
01452     const BasicBlock *SuccBB = TI->getSuccessor(succ);
01453     if (!isa<PHINode>(SuccBB->begin())) continue;
01454     MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
01455 
01456     // If this terminator has multiple identical successors (common for
01457     // switches), only handle each succ once.
01458     if (!SuccsHandled.insert(SuccMBB)) continue;
01459 
01460     MachineBasicBlock::iterator MBBI = SuccMBB->begin();
01461 
01462     // At this point we know that there is a 1-1 correspondence between LLVM PHI
01463     // nodes and Machine PHI nodes, but the incoming operands have not been
01464     // emitted yet.
01465     for (BasicBlock::const_iterator I = SuccBB->begin();
01466          const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
01467 
01468       // Ignore dead phi's.
01469       if (PN->use_empty()) continue;
01470 
01471       // Only handle legal types. Two interesting things to note here. First,
01472       // by bailing out early, we may leave behind some dead instructions,
01473       // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
01474       // own moves. Second, this check is necessary because FastISel doesn't
01475       // use CreateRegs to create registers, so it always creates
01476       // exactly one register for each non-void instruction.
01477       EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
01478       if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
01479         // Handle integer promotions, though, because they're common and easy.
01480         if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
01481           VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
01482         else {
01483           FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
01484           return false;
01485         }
01486       }
01487 
01488       const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
01489 
01490       // Set the DebugLoc for the copy. Prefer the location of the operand
01491       // if there is one; use the location of the PHI otherwise.
01492       DL = PN->getDebugLoc();
01493       if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
01494         DL = Inst->getDebugLoc();
01495 
01496       unsigned Reg = getRegForValue(PHIOp);
01497       if (Reg == 0) {
01498         FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
01499         return false;
01500       }
01501       FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
01502       DL = DebugLoc();
01503     }
01504   }
01505 
01506   return true;
01507 }
01508 
01509 bool FastISel::tryToFoldLoad(const LoadInst *LI, const Instruction *FoldInst) {
01510   assert(LI->hasOneUse() &&
01511       "tryToFoldLoad expected a LoadInst with a single use");
01512   // We know that the load has a single use, but don't know what it is.  If it
01513   // isn't one of the folded instructions, then we can't succeed here.  Handle
01514   // this by scanning the single-use users of the load until we get to FoldInst.
01515   unsigned MaxUsers = 6;  // Don't scan down huge single-use chains of instrs.
01516 
01517   const Instruction *TheUser = LI->use_back();
01518   while (TheUser != FoldInst &&   // Scan up until we find FoldInst.
01519          // Stay in the right block.
01520          TheUser->getParent() == FoldInst->getParent() &&
01521          --MaxUsers) {  // Don't scan too far.
01522     // If there are multiple or no uses of this instruction, then bail out.
01523     if (!TheUser->hasOneUse())
01524       return false;
01525 
01526     TheUser = TheUser->use_back();
01527   }
01528 
01529   // If we didn't find the fold instruction, then we failed to collapse the
01530   // sequence.
01531   if (TheUser != FoldInst)
01532     return false;
01533 
01534   // Don't try to fold volatile loads.  Target has to deal with alignment
01535   // constraints.
01536   if (LI->isVolatile())
01537     return false;
01538 
01539   // Figure out which vreg this is going into.  If there is no assigned vreg yet
01540   // then there actually was no reference to it.  Perhaps the load is referenced
01541   // by a dead instruction.
01542   unsigned LoadReg = getRegForValue(LI);
01543   if (LoadReg == 0)
01544     return false;
01545 
01546   // We can't fold if this vreg has no uses or more than one use.  Multiple uses
01547   // may mean that the instruction got lowered to multiple MIs, or the use of
01548   // the loaded value ended up being multiple operands of the result.
01549   if (!MRI.hasOneUse(LoadReg))
01550     return false;
01551 
01552   MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LoadReg);
01553   MachineInstr *User = &*RI;
01554 
01555   // Set the insertion point properly.  Folding the load can cause generation of
01556   // other random instructions (like sign extends) for addressing modes; make
01557   // sure they get inserted in a logical place before the new instruction.
01558   FuncInfo.InsertPt = User;
01559   FuncInfo.MBB = User->getParent();
01560 
01561   // Ask the target to try folding the load.
01562   return tryToFoldLoadIntoMI(User, RI.getOperandNo(), LI);
01563 }
01564 
01565