LLVM API Documentation

X86FastISel.cpp

Go to the documentation of this file.
00001 //===-- X86FastISel.cpp - X86 FastISel implementation ---------------------===//
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 defines the X86-specific support for the FastISel class. Much
00011 // of the target-specific code is generated by tablegen in the file
00012 // X86GenFastISel.inc, which is #included here.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #include "X86.h"
00017 #include "X86InstrBuilder.h"
00018 #include "X86ISelLowering.h"
00019 #include "X86RegisterInfo.h"
00020 #include "X86Subtarget.h"
00021 #include "X86TargetMachine.h"
00022 #include "llvm/CallingConv.h"
00023 #include "llvm/DerivedTypes.h"
00024 #include "llvm/GlobalVariable.h"
00025 #include "llvm/Instructions.h"
00026 #include "llvm/IntrinsicInst.h"
00027 #include "llvm/CodeGen/FastISel.h"
00028 #include "llvm/CodeGen/MachineConstantPool.h"
00029 #include "llvm/CodeGen/MachineFrameInfo.h"
00030 #include "llvm/CodeGen/MachineRegisterInfo.h"
00031 #include "llvm/Support/CallSite.h"
00032 #include "llvm/Support/ErrorHandling.h"
00033 #include "llvm/Support/GetElementPtrTypeIterator.h"
00034 #include "llvm/Target/TargetOptions.h"
00035 using namespace llvm;
00036 
00037 namespace {
00038   
00039 class X86FastISel : public FastISel {
00040   /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
00041   /// make the right decision when generating code for different targets.
00042   const X86Subtarget *Subtarget;
00043 
00044   /// StackPtr - Register used as the stack pointer.
00045   ///
00046   unsigned StackPtr;
00047 
00048   /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87 
00049   /// floating point ops.
00050   /// When SSE is available, use it for f32 operations.
00051   /// When SSE2 is available, use it for f64 operations.
00052   bool X86ScalarSSEf64;
00053   bool X86ScalarSSEf32;
00054 
00055 public:
00056   explicit X86FastISel(MachineFunction &mf,
00057                        MachineModuleInfo *mmi,
00058                        DwarfWriter *dw,
00059                        DenseMap<const Value *, unsigned> &vm,
00060                        DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
00061                        DenseMap<const AllocaInst *, int> &am
00062 #ifndef NDEBUG
00063                        , SmallSet<Instruction*, 8> &cil
00064 #endif
00065                        )
00066     : FastISel(mf, mmi, dw, vm, bm, am
00067 #ifndef NDEBUG
00068                , cil
00069 #endif
00070                ) {
00071     Subtarget = &TM.getSubtarget<X86Subtarget>();
00072     StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
00073     X86ScalarSSEf64 = Subtarget->hasSSE2();
00074     X86ScalarSSEf32 = Subtarget->hasSSE1();
00075   }
00076 
00077   virtual bool TargetSelectInstruction(Instruction *I);
00078 
00079 #include "X86GenFastISel.inc"
00080 
00081 private:
00082   bool X86FastEmitCompare(Value *LHS, Value *RHS, EVT VT);
00083   
00084   bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
00085 
00086   bool X86FastEmitStore(EVT VT, Value *Val,
00087                         const X86AddressMode &AM);
00088   bool X86FastEmitStore(EVT VT, unsigned Val,
00089                         const X86AddressMode &AM);
00090 
00091   bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
00092                          unsigned &ResultReg);
00093   
00094   bool X86SelectAddress(Value *V, X86AddressMode &AM);
00095   bool X86SelectCallAddress(Value *V, X86AddressMode &AM);
00096 
00097   bool X86SelectLoad(Instruction *I);
00098   
00099   bool X86SelectStore(Instruction *I);
00100 
00101   bool X86SelectCmp(Instruction *I);
00102 
00103   bool X86SelectZExt(Instruction *I);
00104 
00105   bool X86SelectBranch(Instruction *I);
00106 
00107   bool X86SelectShift(Instruction *I);
00108 
00109   bool X86SelectSelect(Instruction *I);
00110 
00111   bool X86SelectTrunc(Instruction *I);
00112  
00113   bool X86SelectFPExt(Instruction *I);
00114   bool X86SelectFPTrunc(Instruction *I);
00115 
00116   bool X86SelectExtractValue(Instruction *I);
00117 
00118   bool X86VisitIntrinsicCall(IntrinsicInst &I);
00119   bool X86SelectCall(Instruction *I);
00120 
00121   CCAssignFn *CCAssignFnForCall(unsigned CC, bool isTailCall = false);
00122 
00123   const X86InstrInfo *getInstrInfo() const {
00124     return getTargetMachine()->getInstrInfo();
00125   }
00126   const X86TargetMachine *getTargetMachine() const {
00127     return static_cast<const X86TargetMachine *>(&TM);
00128   }
00129 
00130   unsigned TargetMaterializeConstant(Constant *C);
00131 
00132   unsigned TargetMaterializeAlloca(AllocaInst *C);
00133 
00134   /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
00135   /// computed in an SSE register, not on the X87 floating point stack.
00136   bool isScalarFPTypeInSSEReg(EVT VT) const {
00137     return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
00138       (VT == MVT::f32 && X86ScalarSSEf32);   // f32 is when SSE1
00139   }
00140 
00141   bool isTypeLegal(const Type *Ty, EVT &VT, bool AllowI1 = false);
00142 };
00143   
00144 } // end anonymous namespace.
00145 
00146 bool X86FastISel::isTypeLegal(const Type *Ty, EVT &VT, bool AllowI1) {
00147   VT = TLI.getValueType(Ty, /*HandleUnknown=*/true);
00148   if (VT == MVT::Other || !VT.isSimple())
00149     // Unhandled type. Halt "fast" selection and bail.
00150     return false;
00151   
00152   // For now, require SSE/SSE2 for performing floating-point operations,
00153   // since x87 requires additional work.
00154   if (VT == MVT::f64 && !X86ScalarSSEf64)
00155      return false;
00156   if (VT == MVT::f32 && !X86ScalarSSEf32)
00157      return false;
00158   // Similarly, no f80 support yet.
00159   if (VT == MVT::f80)
00160     return false;
00161   // We only handle legal types. For example, on x86-32 the instruction
00162   // selector contains all of the 64-bit instructions from x86-64,
00163   // under the assumption that i64 won't be used if the target doesn't
00164   // support it.
00165   return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
00166 }
00167 
00168 #include "X86GenCallingConv.inc"
00169 
00170 /// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
00171 /// convention.
00172 CCAssignFn *X86FastISel::CCAssignFnForCall(unsigned CC, bool isTaillCall) {
00173   if (Subtarget->is64Bit()) {
00174     if (Subtarget->isTargetWin64())
00175       return CC_X86_Win64_C;
00176     else
00177       return CC_X86_64_C;
00178   }
00179 
00180   if (CC == CallingConv::X86_FastCall)
00181     return CC_X86_32_FastCall;
00182   else if (CC == CallingConv::Fast)
00183     return CC_X86_32_FastCC;
00184   else
00185     return CC_X86_32_C;
00186 }
00187 
00188 /// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
00189 /// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
00190 /// Return true and the result register by reference if it is possible.
00191 bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
00192                                   unsigned &ResultReg) {
00193   // Get opcode and regclass of the output for the given load instruction.
00194   unsigned Opc = 0;
00195   const TargetRegisterClass *RC = NULL;
00196   switch (VT.getSimpleVT().SimpleTy) {
00197   default: return false;
00198   case MVT::i8:
00199     Opc = X86::MOV8rm;
00200     RC  = X86::GR8RegisterClass;
00201     break;
00202   case MVT::i16:
00203     Opc = X86::MOV16rm;
00204     RC  = X86::GR16RegisterClass;
00205     break;
00206   case MVT::i32:
00207     Opc = X86::MOV32rm;
00208     RC  = X86::GR32RegisterClass;
00209     break;
00210   case MVT::i64:
00211     // Must be in x86-64 mode.
00212     Opc = X86::MOV64rm;
00213     RC  = X86::GR64RegisterClass;
00214     break;
00215   case MVT::f32:
00216     if (Subtarget->hasSSE1()) {
00217       Opc = X86::MOVSSrm;
00218       RC  = X86::FR32RegisterClass;
00219     } else {
00220       Opc = X86::LD_Fp32m;
00221       RC  = X86::RFP32RegisterClass;
00222     }
00223     break;
00224   case MVT::f64:
00225     if (Subtarget->hasSSE2()) {
00226       Opc = X86::MOVSDrm;
00227       RC  = X86::FR64RegisterClass;
00228     } else {
00229       Opc = X86::LD_Fp64m;
00230       RC  = X86::RFP64RegisterClass;
00231     }
00232     break;
00233   case MVT::f80:
00234     // No f80 support yet.
00235     return false;
00236   }
00237 
00238   ResultReg = createResultReg(RC);
00239   addFullAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
00240   return true;
00241 }
00242 
00243 /// X86FastEmitStore - Emit a machine instruction to store a value Val of
00244 /// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
00245 /// and a displacement offset, or a GlobalAddress,
00246 /// i.e. V. Return true if it is possible.
00247 bool
00248 X86FastISel::X86FastEmitStore(EVT VT, unsigned Val,
00249                               const X86AddressMode &AM) {
00250   // Get opcode and regclass of the output for the given store instruction.
00251   unsigned Opc = 0;
00252   switch (VT.getSimpleVT().SimpleTy) {
00253   case MVT::f80: // No f80 support yet.
00254   default: return false;
00255   case MVT::i8:  Opc = X86::MOV8mr;  break;
00256   case MVT::i16: Opc = X86::MOV16mr; break;
00257   case MVT::i32: Opc = X86::MOV32mr; break;
00258   case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
00259   case MVT::f32:
00260     Opc = Subtarget->hasSSE1() ? X86::MOVSSmr : X86::ST_Fp32m;
00261     break;
00262   case MVT::f64:
00263     Opc = Subtarget->hasSSE2() ? X86::MOVSDmr : X86::ST_Fp64m;
00264     break;
00265   }
00266   
00267   addFullAddress(BuildMI(MBB, DL, TII.get(Opc)), AM).addReg(Val);
00268   return true;
00269 }
00270 
00271 bool X86FastISel::X86FastEmitStore(EVT VT, Value *Val,
00272                                    const X86AddressMode &AM) {
00273   // Handle 'null' like i32/i64 0.
00274   if (isa<ConstantPointerNull>(Val))
00275     Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
00276   
00277   // If this is a store of a simple constant, fold the constant into the store.
00278   if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
00279     unsigned Opc = 0;
00280     switch (VT.getSimpleVT().SimpleTy) {
00281     default: break;
00282     case MVT::i8:  Opc = X86::MOV8mi;  break;
00283     case MVT::i16: Opc = X86::MOV16mi; break;
00284     case MVT::i32: Opc = X86::MOV32mi; break;
00285     case MVT::i64:
00286       // Must be a 32-bit sign extended value.
00287       if ((int)CI->getSExtValue() == CI->getSExtValue())
00288         Opc = X86::MOV64mi32;
00289       break;
00290     }
00291     
00292     if (Opc) {
00293       addFullAddress(BuildMI(MBB, DL, TII.get(Opc)), AM)
00294                              .addImm(CI->getSExtValue());
00295       return true;
00296     }
00297   }
00298   
00299   unsigned ValReg = getRegForValue(Val);
00300   if (ValReg == 0)
00301     return false;    
00302  
00303   return X86FastEmitStore(VT, ValReg, AM);
00304 }
00305 
00306 /// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
00307 /// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
00308 /// ISD::SIGN_EXTEND).
00309 bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
00310                                     unsigned Src, EVT SrcVT,
00311                                     unsigned &ResultReg) {
00312   unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
00313   
00314   if (RR != 0) {
00315     ResultReg = RR;
00316     return true;
00317   } else
00318     return false;
00319 }
00320 
00321 /// X86SelectAddress - Attempt to fill in an address from the given value.
00322 ///
00323 bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM) {
00324   User *U = NULL;
00325   unsigned Opcode = Instruction::UserOp1;
00326   if (Instruction *I = dyn_cast<Instruction>(V)) {
00327     Opcode = I->getOpcode();
00328     U = I;
00329   } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
00330     Opcode = C->getOpcode();
00331     U = C;
00332   }
00333 
00334   switch (Opcode) {
00335   default: break;
00336   case Instruction::BitCast:
00337     // Look past bitcasts.
00338     return X86SelectAddress(U->getOperand(0), AM);
00339 
00340   case Instruction::IntToPtr:
00341     // Look past no-op inttoptrs.
00342     if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
00343       return X86SelectAddress(U->getOperand(0), AM);
00344     break;
00345 
00346   case Instruction::PtrToInt:
00347     // Look past no-op ptrtoints.
00348     if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
00349       return X86SelectAddress(U->getOperand(0), AM);
00350     break;
00351 
00352   case Instruction::Alloca: {
00353     // Do static allocas.
00354     const AllocaInst *A = cast<AllocaInst>(V);
00355     DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
00356     if (SI != StaticAllocaMap.end()) {
00357       AM.BaseType = X86AddressMode::FrameIndexBase;
00358       AM.Base.FrameIndex = SI->second;
00359       return true;
00360     }
00361     break;
00362   }
00363 
00364   case Instruction::Add: {
00365     // Adds of constants are common and easy enough.
00366     if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
00367       uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
00368       // They have to fit in the 32-bit signed displacement field though.
00369       if (isInt32(Disp)) {
00370         AM.Disp = (uint32_t)Disp;
00371         return X86SelectAddress(U->getOperand(0), AM);
00372       }
00373     }
00374     break;
00375   }
00376 
00377   case Instruction::GetElementPtr: {
00378     // Pattern-match simple GEPs.
00379     uint64_t Disp = (int32_t)AM.Disp;
00380     unsigned IndexReg = AM.IndexReg;
00381     unsigned Scale = AM.Scale;
00382     gep_type_iterator GTI = gep_type_begin(U);
00383     // Iterate through the indices, folding what we can. Constants can be
00384     // folded, and one dynamic index can be handled, if the scale is supported.
00385     for (User::op_iterator i = U->op_begin() + 1, e = U->op_end();
00386          i != e; ++i, ++GTI) {
00387       Value *Op = *i;
00388       if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
00389         const StructLayout *SL = TD.getStructLayout(STy);
00390         unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
00391         Disp += SL->getElementOffset(Idx);
00392       } else {
00393         uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
00394         if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
00395           // Constant-offset addressing.
00396           Disp += CI->getSExtValue() * S;
00397         } else if (IndexReg == 0 &&
00398                    (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
00399                    (S == 1 || S == 2 || S == 4 || S == 8)) {
00400           // Scaled-index addressing.
00401           Scale = S;
00402           IndexReg = getRegForGEPIndex(Op);
00403           if (IndexReg == 0)
00404             return false;
00405         } else
00406           // Unsupported.
00407           goto unsupported_gep;
00408       }
00409     }
00410     // Check for displacement overflow.
00411     if (!isInt32(Disp))
00412       break;
00413     // Ok, the GEP indices were covered by constant-offset and scaled-index
00414     // addressing. Update the address state and move on to examining the base.
00415     AM.IndexReg = IndexReg;
00416     AM.Scale = Scale;
00417     AM.Disp = (uint32_t)Disp;
00418     return X86SelectAddress(U->getOperand(0), AM);
00419   unsupported_gep:
00420     // Ok, the GEP indices weren't all covered.
00421     break;
00422   }
00423   }
00424 
00425   // Handle constant address.
00426   if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
00427     // Can't handle alternate code models yet.
00428     if (TM.getCodeModel() != CodeModel::Small)
00429       return false;
00430 
00431     // RIP-relative addresses can't have additional register operands.
00432     if (Subtarget->isPICStyleRIPRel() &&
00433         (AM.Base.Reg != 0 || AM.IndexReg != 0))
00434       return false;
00435 
00436     // Can't handle TLS yet.
00437     if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
00438       if (GVar->isThreadLocal())
00439         return false;
00440 
00441     // Okay, we've committed to selecting this global. Set up the basic address.
00442     AM.GV = GV;
00443     
00444     // Allow the subtarget to classify the global.
00445     unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
00446 
00447     // If this reference is relative to the pic base, set it now.
00448     if (isGlobalRelativeToPICBase(GVFlags)) {
00449       // FIXME: How do we know Base.Reg is free??
00450       AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(&MF);
00451     }
00452     
00453     // Unless the ABI requires an extra load, return a direct reference to
00454     // the global.
00455     if (!isGlobalStubReference(GVFlags)) {
00456       if (Subtarget->isPICStyleRIPRel()) {
00457         // Use rip-relative addressing if we can.  Above we verified that the
00458         // base and index registers are unused.
00459         assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
00460         AM.Base.Reg = X86::RIP;
00461       }
00462       AM.GVOpFlags = GVFlags;
00463       return true;
00464     }
00465     
00466     // Ok, we need to do a load from a stub.  If we've already loaded from this
00467     // stub, reuse the loaded pointer, otherwise emit the load now.
00468     DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
00469     unsigned LoadReg;
00470     if (I != LocalValueMap.end() && I->second != 0) {
00471       LoadReg = I->second;
00472     } else {
00473       // Issue load from stub.
00474       unsigned Opc = 0;
00475       const TargetRegisterClass *RC = NULL;
00476       X86AddressMode StubAM;
00477       StubAM.Base.Reg = AM.Base.Reg;
00478       StubAM.GV = GV;
00479       StubAM.GVOpFlags = GVFlags;
00480 
00481       if (TLI.getPointerTy() == MVT::i64) {
00482         Opc = X86::MOV64rm;
00483         RC  = X86::GR64RegisterClass;
00484         
00485         if (Subtarget->isPICStyleRIPRel())
00486           StubAM.Base.Reg = X86::RIP;
00487       } else {
00488         Opc = X86::MOV32rm;
00489         RC  = X86::GR32RegisterClass;
00490       }
00491       
00492       LoadReg = createResultReg(RC);
00493       addFullAddress(BuildMI(MBB, DL, TII.get(Opc), LoadReg), StubAM);
00494       
00495       // Prevent loading GV stub multiple times in same MBB.
00496       LocalValueMap[V] = LoadReg;
00497     }
00498     
00499     // Now construct the final address. Note that the Disp, Scale,
00500     // and Index values may already be set here.
00501     AM.Base.Reg = LoadReg;
00502     AM.GV = 0;
00503     return true;
00504   }
00505 
00506   // If all else fails, try to materialize the value in a register.
00507   if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
00508     if (AM.Base.Reg == 0) {
00509       AM.Base.Reg = getRegForValue(V);
00510       return AM.Base.Reg != 0;
00511     }
00512     if (AM.IndexReg == 0) {
00513       assert(AM.Scale == 1 && "Scale with no index!");
00514       AM.IndexReg = getRegForValue(V);
00515       return AM.IndexReg != 0;
00516     }
00517   }
00518 
00519   return false;
00520 }
00521 
00522 /// X86SelectCallAddress - Attempt to fill in an address from the given value.
00523 ///
00524 bool X86FastISel::X86SelectCallAddress(Value *V, X86AddressMode &AM) {
00525   User *U = NULL;
00526   unsigned Opcode = Instruction::UserOp1;
00527   if (Instruction *I = dyn_cast<Instruction>(V)) {
00528     Opcode = I->getOpcode();
00529     U = I;
00530   } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
00531     Opcode = C->getOpcode();
00532     U = C;
00533   }
00534 
00535   switch (Opcode) {
00536   default: break;
00537   case Instruction::BitCast:
00538     // Look past bitcasts.
00539     return X86SelectCallAddress(U->getOperand(0), AM);
00540 
00541   case Instruction::IntToPtr:
00542     // Look past no-op inttoptrs.
00543     if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
00544       return X86SelectCallAddress(U->getOperand(0), AM);
00545     break;
00546 
00547   case Instruction::PtrToInt:
00548     // Look past no-op ptrtoints.
00549     if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
00550       return X86SelectCallAddress(U->getOperand(0), AM);
00551     break;
00552   }
00553 
00554   // Handle constant address.
00555   if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
00556     // Can't handle alternate code models yet.
00557     if (TM.getCodeModel() != CodeModel::Small)
00558       return false;
00559 
00560     // RIP-relative addresses can't have additional register operands.
00561     if (Subtarget->isPICStyleRIPRel() &&
00562         (AM.Base.Reg != 0 || AM.IndexReg != 0))
00563       return false;
00564 
00565     // Can't handle TLS or DLLImport.
00566     if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
00567       if (GVar->isThreadLocal() || GVar->hasDLLImportLinkage())
00568         return false;
00569 
00570     // Okay, we've committed to selecting this global. Set up the basic address.
00571     AM.GV = GV;
00572     
00573     // No ABI requires an extra load for anything other than DLLImport, which
00574     // we rejected above. Return a direct reference to the global.
00575     if (Subtarget->isPICStyleRIPRel()) {
00576       // Use rip-relative addressing if we can.  Above we verified that the
00577       // base and index registers are unused.
00578       assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
00579       AM.Base.Reg = X86::RIP;
00580     } else if (Subtarget->isPICStyleStubPIC()) {
00581       AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
00582     } else if (Subtarget->isPICStyleGOT()) {
00583       AM.GVOpFlags = X86II::MO_GOTOFF;
00584     }
00585     
00586     return true;
00587   }
00588 
00589   // If all else fails, try to materialize the value in a register.
00590   if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
00591     if (AM.Base.Reg == 0) {
00592       AM.Base.Reg = getRegForValue(V);
00593       return AM.Base.Reg != 0;
00594     }
00595     if (AM.IndexReg == 0) {
00596       assert(AM.Scale == 1 && "Scale with no index!");
00597       AM.IndexReg = getRegForValue(V);
00598       return AM.IndexReg != 0;
00599     }
00600   }
00601 
00602   return false;
00603 }
00604 
00605 
00606 /// X86SelectStore - Select and emit code to implement store instructions.
00607 bool X86FastISel::X86SelectStore(Instruction* I) {
00608   EVT VT;
00609   if (!isTypeLegal(I->getOperand(0)->getType(), VT))
00610     return false;
00611 
00612   X86AddressMode AM;
00613   if (!X86SelectAddress(I->getOperand(1), AM))
00614     return false;
00615 
00616   return X86FastEmitStore(VT, I->getOperand(0), AM);
00617 }
00618 
00619 /// X86SelectLoad - Select and emit code to implement load instructions.
00620 ///
00621 bool X86FastISel::X86SelectLoad(Instruction *I)  {
00622   EVT VT;
00623   if (!isTypeLegal(I->getType(), VT))
00624     return false;
00625 
00626   X86AddressMode AM;
00627   if (!X86SelectAddress(I->getOperand(0), AM))
00628     return false;
00629 
00630   unsigned ResultReg = 0;
00631   if (X86FastEmitLoad(VT, AM, ResultReg)) {
00632     UpdateValueMap(I, ResultReg);
00633     return true;
00634   }
00635   return false;
00636 }
00637 
00638 static unsigned X86ChooseCmpOpcode(EVT VT) {
00639   switch (VT.getSimpleVT().SimpleTy) {
00640   default:       return 0;
00641   case MVT::i8:  return X86::CMP8rr;
00642   case MVT::i16: return X86::CMP16rr;
00643   case MVT::i32: return X86::CMP32rr;
00644   case MVT::i64: return X86::CMP64rr;
00645   case MVT::f32: return X86::UCOMISSrr;
00646   case MVT::f64: return X86::UCOMISDrr;
00647   }
00648 }
00649 
00650 /// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
00651 /// of the comparison, return an opcode that works for the compare (e.g.
00652 /// CMP32ri) otherwise return 0.
00653 static unsigned X86ChooseCmpImmediateOpcode(EVT VT, ConstantInt *RHSC) {
00654   switch (VT.getSimpleVT().SimpleTy) {
00655   // Otherwise, we can't fold the immediate into this comparison.
00656   default: return 0;
00657   case MVT::i8: return X86::CMP8ri;
00658   case MVT::i16: return X86::CMP16ri;
00659   case MVT::i32: return X86::CMP32ri;
00660   case MVT::i64:
00661     // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
00662     // field.
00663     if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
00664       return X86::CMP64ri32;
00665     return 0;
00666   }
00667 }
00668 
00669 bool X86FastISel::X86FastEmitCompare(Value *Op0, Value *Op1, EVT VT) {
00670   unsigned Op0Reg = getRegForValue(Op0);
00671   if (Op0Reg == 0) return false;
00672   
00673   // Handle 'null' like i32/i64 0.
00674   if (isa<ConstantPointerNull>(Op1))
00675     Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
00676   
00677   // We have two options: compare with register or immediate.  If the RHS of
00678   // the compare is an immediate that we can fold into this compare, use
00679   // CMPri, otherwise use CMPrr.
00680   if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
00681     if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
00682       BuildMI(MBB, DL, TII.get(CompareImmOpc)).addReg(Op0Reg)
00683                                           .addImm(Op1C->getSExtValue());
00684       return true;
00685     }
00686   }
00687   
00688   unsigned CompareOpc = X86ChooseCmpOpcode(VT);
00689   if (CompareOpc == 0) return false;
00690     
00691   unsigned Op1Reg = getRegForValue(Op1);
00692   if (Op1Reg == 0) return false;
00693   BuildMI(MBB, DL, TII.get(CompareOpc)).addReg(Op0Reg).addReg(Op1Reg);
00694   
00695   return true;
00696 }
00697 
00698 bool X86FastISel::X86SelectCmp(Instruction *I) {
00699   CmpInst *CI = cast<CmpInst>(I);
00700 
00701   EVT VT;
00702   if (!isTypeLegal(I->getOperand(0)->getType(), VT))
00703     return false;
00704 
00705   unsigned ResultReg = createResultReg(&X86::GR8RegClass);
00706   unsigned SetCCOpc;
00707   bool SwapArgs;  // false -> compare Op0, Op1.  true -> compare Op1, Op0.
00708   switch (CI->getPredicate()) {
00709   case CmpInst::FCMP_OEQ: {
00710     if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
00711       return false;
00712     
00713     unsigned EReg = createResultReg(&X86::GR8RegClass);
00714     unsigned NPReg = createResultReg(&X86::GR8RegClass);
00715     BuildMI(MBB, DL, TII.get(X86::SETEr), EReg);
00716     BuildMI(MBB, DL, TII.get(X86::SETNPr), NPReg);
00717     BuildMI(MBB, DL, 
00718             TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
00719     UpdateValueMap(I, ResultReg);
00720     return true;
00721   }
00722   case CmpInst::FCMP_UNE: {
00723     if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
00724       return false;
00725 
00726     unsigned NEReg = createResultReg(&X86::GR8RegClass);
00727     unsigned PReg = createResultReg(&X86::GR8RegClass);
00728     BuildMI(MBB, DL, TII.get(X86::SETNEr), NEReg);
00729     BuildMI(MBB, DL, TII.get(X86::SETPr), PReg);
00730     BuildMI(MBB, DL, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
00731     UpdateValueMap(I, ResultReg);
00732     return true;
00733   }
00734   case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr;  break;
00735   case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
00736   case CmpInst::FCMP_OLT: SwapArgs = true;  SetCCOpc = X86::SETAr;  break;
00737   case CmpInst::FCMP_OLE: SwapArgs = true;  SetCCOpc = X86::SETAEr; break;
00738   case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
00739   case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
00740   case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr;  break;
00741   case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr;  break;
00742   case CmpInst::FCMP_UGT: SwapArgs = true;  SetCCOpc = X86::SETBr;  break;
00743   case CmpInst::FCMP_UGE: SwapArgs = true;  SetCCOpc = X86::SETBEr; break;
00744   case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr;  break;
00745   case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
00746   
00747   case CmpInst::ICMP_EQ:  SwapArgs = false; SetCCOpc = X86::SETEr;  break;
00748   case CmpInst::ICMP_NE:  SwapArgs = false; SetCCOpc = X86::SETNEr; break;
00749   case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr;  break;
00750   case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
00751   case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr;  break;
00752   case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
00753   case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr;  break;
00754   case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
00755   case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr;  break;
00756   case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
00757   default:
00758     return false;
00759   }
00760 
00761   Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
00762   if (SwapArgs)
00763     std::swap(Op0, Op1);
00764 
00765   // Emit a compare of Op0/Op1.
00766   if (!X86FastEmitCompare(Op0, Op1, VT))
00767     return false;
00768   
00769   BuildMI(MBB, DL, TII.get(SetCCOpc), ResultReg);
00770   UpdateValueMap(I, ResultReg);
00771   return true;
00772 }
00773 
00774 bool X86FastISel::X86SelectZExt(Instruction *I) {
00775   // Handle zero-extension from i1 to i8, which is common.
00776   if (I->getType() == Type::getInt8Ty(I->getContext()) &&
00777       I->getOperand(0)->getType() == Type::getInt1Ty(I->getContext())) {
00778     unsigned ResultReg = getRegForValue(I->getOperand(0));
00779     if (ResultReg == 0) return false;
00780     // Set the high bits to zero.
00781     ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg);
00782     if (ResultReg == 0) return false;
00783     UpdateValueMap(I, ResultReg);
00784     return true;
00785   }
00786 
00787   return false;
00788 }
00789 
00790 
00791 bool X86FastISel::X86SelectBranch(Instruction *I) {
00792   // Unconditional branches are selected by tablegen-generated code.
00793   // Handle a conditional branch.
00794   BranchInst *BI = cast<BranchInst>(I);
00795   MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
00796   MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
00797 
00798   // Fold the common case of a conditional branch with a comparison.
00799   if (CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
00800     if (CI->hasOneUse()) {
00801       EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
00802 
00803       // Try to take advantage of fallthrough opportunities.
00804       CmpInst::Predicate Predicate = CI->getPredicate();
00805       if (MBB->isLayoutSuccessor(TrueMBB)) {
00806         std::swap(TrueMBB, FalseMBB);
00807         Predicate = CmpInst::getInversePredicate(Predicate);
00808       }
00809 
00810       bool SwapArgs;  // false -> compare Op0, Op1.  true -> compare Op1, Op0.
00811       unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
00812 
00813       switch (Predicate) {
00814       case CmpInst::FCMP_OEQ:
00815         std::swap(TrueMBB, FalseMBB);
00816         Predicate = CmpInst::FCMP_UNE;
00817         // FALL THROUGH
00818       case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE; break;
00819       case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA;  break;
00820       case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE; break;
00821       case CmpInst::FCMP_OLT: SwapArgs = true;  BranchOpc = X86::JA;  break;
00822       case CmpInst::FCMP_OLE: SwapArgs = true;  BranchOpc = X86::JAE; break;
00823       case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE; break;
00824       case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP; break;
00825       case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP;  break;
00826       case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE;  break;
00827       case CmpInst::FCMP_UGT: SwapArgs = true;  BranchOpc = X86::JB;  break;
00828       case CmpInst::FCMP_UGE: SwapArgs = true;  BranchOpc = X86::JBE; break;
00829       case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB;  break;
00830       case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE; break;
00831           
00832       case CmpInst::ICMP_EQ:  SwapArgs = false; BranchOpc = X86::JE;  break;
00833       case CmpInst::ICMP_NE:  SwapArgs = false; BranchOpc = X86::JNE; break;
00834       case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA;  break;
00835       case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE; break;
00836       case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB;  break;
00837       case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE; break;
00838       case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG;  break;
00839       case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE; break;
00840       case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL;  break;
00841       case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE; break;
00842       default:
00843         return false;
00844       }
00845       
00846       Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
00847       if (SwapArgs)
00848         std::swap(Op0, Op1);
00849 
00850       // Emit a compare of the LHS and RHS, setting the flags.
00851       if (!X86FastEmitCompare(Op0, Op1, VT))
00852         return false;
00853       
00854       BuildMI(MBB, DL, TII.get(BranchOpc)).addMBB(TrueMBB);
00855 
00856       if (Predicate == CmpInst::FCMP_UNE) {
00857         // X86 requires a second branch to handle UNE (and OEQ,
00858         // which is mapped to UNE above).
00859         BuildMI(MBB, DL, TII.get(X86::JP)).addMBB(TrueMBB);
00860       }
00861 
00862       FastEmitBranch(FalseMBB);
00863       MBB->addSuccessor(TrueMBB);
00864       return true;
00865     }
00866   } else if (ExtractValueInst *EI =
00867              dyn_cast<ExtractValueInst>(BI->getCondition())) {
00868     // Check to see if the branch instruction is from an "arithmetic with
00869     // overflow" intrinsic. The main way these intrinsics are used is:
00870     //
00871     //   %t = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %v1, i32 %v2)
00872     //   %sum = extractvalue { i32, i1 } %t, 0
00873     //   %obit = extractvalue { i32, i1 } %t, 1
00874     //   br i1 %obit, label %overflow, label %normal
00875     //
00876     // The %sum and %obit are converted in an ADD and a SETO/SETB before
00877     // reaching the branch. Therefore, we search backwards through the MBB
00878     // looking for the SETO/SETB instruction. If an instruction modifies the
00879     // EFLAGS register before we reach the SETO/SETB instruction, then we can't
00880     // convert the branch into a JO/JB instruction.
00881     if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(EI->getAggregateOperand())){
00882       if (CI->getIntrinsicID() == Intrinsic::sadd_with_overflow ||
00883           CI->getIntrinsicID() == Intrinsic::uadd_with_overflow) {
00884         const MachineInstr *SetMI = 0;
00885         unsigned Reg = lookUpRegForValue(EI);
00886 
00887         for (MachineBasicBlock::const_reverse_iterator
00888                RI = MBB->rbegin(), RE = MBB->rend(); RI != RE; ++RI) {
00889           const MachineInstr &MI = *RI;
00890 
00891           if (MI.modifiesRegister(Reg)) {
00892             unsigned Src, Dst, SrcSR, DstSR;
00893 
00894             if (getInstrInfo()->isMoveInstr(MI, Src, Dst, SrcSR, DstSR)) {
00895               Reg = Src;
00896               continue;
00897             }
00898 
00899             SetMI = &MI;
00900             break;
00901           }
00902 
00903           const TargetInstrDesc &TID = MI.getDesc();
00904           if (TID.hasUnmodeledSideEffects() ||
00905               TID.hasImplicitDefOfPhysReg(X86::EFLAGS))
00906             break;
00907         }
00908 
00909         if (SetMI) {
00910           unsigned OpCode = SetMI->getOpcode();
00911 
00912           if (OpCode == X86::SETOr || OpCode == X86::SETBr) {
00913             BuildMI(MBB, DL, TII.get(OpCode == X86::SETOr ? X86::JO : X86::JB))
00914               .addMBB(TrueMBB);
00915             FastEmitBranch(FalseMBB);
00916             MBB->addSuccessor(TrueMBB);
00917             return true;
00918           }
00919         }
00920       }
00921     }
00922   }
00923 
00924   // Otherwise do a clumsy setcc and re-test it.
00925   unsigned OpReg = getRegForValue(BI->getCondition());
00926   if (OpReg == 0) return false;
00927 
00928   BuildMI(MBB, DL, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
00929   BuildMI(MBB, DL, TII.get(X86::JNE)).addMBB(TrueMBB);
00930   FastEmitBranch(FalseMBB);
00931   MBB->addSuccessor(TrueMBB);
00932   return true;
00933 }
00934 
00935 bool X86FastISel::X86SelectShift(Instruction *I) {
00936   unsigned CReg = 0, OpReg = 0, OpImm = 0;
00937   const TargetRegisterClass *RC = NULL;
00938   if (I->getType() == Type::getInt8Ty(I->getContext())) {
00939     CReg = X86::CL;
00940     RC = &X86::GR8RegClass;
00941     switch (I->getOpcode()) {
00942     case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
00943     case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
00944     case Instruction::Shl:  OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
00945     default: return false;
00946     }
00947   } else if (I->getType() == Type::getInt16Ty(I->getContext())) {
00948     CReg = X86::CX;
00949     RC = &X86::GR16RegClass;
00950     switch (I->getOpcode()) {
00951     case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
00952     case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
00953     case Instruction::Shl:  OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
00954     default: return false;
00955     }
00956   } else if (I->getType() == Type::getInt32Ty(I->getContext())) {
00957     CReg = X86::ECX;
00958     RC = &X86::GR32RegClass;
00959     switch (I->getOpcode()) {
00960     case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
00961     case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
00962     case Instruction::Shl:  OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
00963     default: return false;
00964     }
00965   } else if (I->getType() == Type::getInt64Ty(I->getContext())) {
00966     CReg = X86::RCX;
00967     RC = &X86::GR64RegClass;
00968     switch (I->getOpcode()) {
00969     case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
00970     case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
00971     case Instruction::Shl:  OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
00972     default: return false;
00973     }
00974   } else {
00975     return false;
00976   }
00977 
00978   EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
00979   if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
00980     return false;
00981 
00982   unsigned Op0Reg = getRegForValue(I->getOperand(0));
00983   if (Op0Reg == 0) return false;
00984   
00985   // Fold immediate in shl(x,3).
00986   if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
00987     unsigned ResultReg = createResultReg(RC);
00988     BuildMI(MBB, DL, TII.get(OpImm), 
00989             ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue() & 0xff);
00990     UpdateValueMap(I, ResultReg);
00991     return true;
00992   }
00993   
00994   unsigned Op1Reg = getRegForValue(I->getOperand(1));
00995   if (Op1Reg == 0) return false;
00996   TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
00997 
00998   // The shift instruction uses X86::CL. If we defined a super-register
00999   // of X86::CL, emit an EXTRACT_SUBREG to precisely describe what
01000   // we're doing here.
01001   if (CReg != X86::CL)
01002     BuildMI(MBB, DL, TII.get(TargetInstrInfo::EXTRACT_SUBREG), X86::CL)
01003       .addReg(CReg).addImm(X86::SUBREG_8BIT);
01004 
01005   unsigned ResultReg = createResultReg(RC);
01006   BuildMI(MBB, DL, TII.get(OpReg), ResultReg).addReg(Op0Reg);
01007   UpdateValueMap(I, ResultReg);
01008   return true;
01009 }
01010 
01011 bool X86FastISel::X86SelectSelect(Instruction *I) {
01012   EVT VT = TLI.getValueType(I->getType(), /*HandleUnknown=*/true);
01013   if (VT == MVT::Other || !isTypeLegal(I->getType(), VT))
01014     return false;
01015   
01016   unsigned Opc = 0;
01017   const TargetRegisterClass *RC = NULL;
01018   if (VT.getSimpleVT() == MVT::i16) {
01019     Opc = X86::CMOVE16rr;
01020     RC = &X86::GR16RegClass;
01021   } else if (VT.getSimpleVT() == MVT::i32) {
01022     Opc = X86::CMOVE32rr;
01023     RC = &X86::GR32RegClass;
01024   } else if (VT.getSimpleVT() == MVT::i64) {
01025     Opc = X86::CMOVE64rr;
01026     RC = &X86::GR64RegClass;
01027   } else {
01028     return false; 
01029   }
01030 
01031   unsigned Op0Reg = getRegForValue(I->getOperand(0));
01032   if (Op0Reg == 0) return false;
01033   unsigned Op1Reg = getRegForValue(I->getOperand(1));
01034   if (Op1Reg == 0) return false;
01035   unsigned Op2Reg = getRegForValue(I->getOperand(2));
01036   if (Op2Reg == 0) return false;
01037 
01038   BuildMI(MBB, DL, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
01039   unsigned ResultReg = createResultReg(RC);
01040   BuildMI(MBB, DL, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
01041   UpdateValueMap(I, ResultReg);
01042   return true;
01043 }
01044 
01045 bool X86FastISel::X86SelectFPExt(Instruction *I) {
01046   // fpext from float to double.
01047   if (Subtarget->hasSSE2() &&
01048       I->getType() == Type::getDoubleTy(I->getContext())) {
01049     Value *V = I->getOperand(0);
01050     if (V->getType() == Type::getFloatTy(I->getContext())) {
01051       unsigned OpReg = getRegForValue(V);
01052       if (OpReg == 0) return false;
01053       unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
01054       BuildMI(MBB, DL, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
01055       UpdateValueMap(I, ResultReg);
01056       return true;
01057     }
01058   }
01059 
01060   return false;
01061 }
01062 
01063 bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
01064   if (Subtarget->hasSSE2()) {
01065     if (I->getType() == Type::getFloatTy(I->getContext())) {
01066       Value *V = I->getOperand(0);
01067       if (V->getType() == Type::getDoubleTy(I->getContext())) {
01068         unsigned OpReg = getRegForValue(V);
01069         if (OpReg == 0) return false;
01070         unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
01071         BuildMI(MBB, DL, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
01072         UpdateValueMap(I, ResultReg);
01073         return true;
01074       }
01075     }
01076   }
01077 
01078   return false;
01079 }
01080 
01081 bool X86FastISel::X86SelectTrunc(Instruction *I) {
01082   if (Subtarget->is64Bit())
01083     // All other cases should be handled by the tblgen generated code.
01084     return false;
01085   EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
01086   EVT DstVT = TLI.getValueType(I->getType());
01087   
01088   // This code only handles truncation to byte right now.
01089   if (DstVT != MVT::i8 && DstVT != MVT::i1)
01090     // All other cases should be handled by the tblgen generated code.
01091     return false;
01092   if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
01093     // All other cases should be handled by the tblgen generated code.
01094     return false;
01095 
01096   unsigned InputReg = getRegForValue(I->getOperand(0));
01097   if (!InputReg)
01098     // Unhandled operand.  Halt "fast" selection and bail.
01099     return false;
01100 
01101   // First issue a copy to GR16_ABCD or GR32_ABCD.
01102   unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16rr : X86::MOV32rr;
01103   const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
01104     ? X86::GR16_ABCDRegisterClass : X86::GR32_ABCDRegisterClass;
01105   unsigned CopyReg = createResultReg(CopyRC);
01106   BuildMI(MBB, DL, TII.get(CopyOpc), CopyReg).addReg(InputReg);
01107 
01108   // Then issue an extract_subreg.
01109   unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
01110                                                   CopyReg, X86::SUBREG_8BIT);
01111   if (!ResultReg)
01112     return false;
01113 
01114   UpdateValueMap(I, ResultReg);
01115   return true;
01116 }
01117 
01118 bool X86FastISel::X86SelectExtractValue(Instruction *I) {
01119   ExtractValueInst *EI = cast<ExtractValueInst>(I);
01120   Value *Agg = EI->getAggregateOperand();
01121 
01122   if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Agg)) {
01123     switch (CI->getIntrinsicID()) {
01124     default: break;
01125     case Intrinsic::sadd_with_overflow:
01126     case Intrinsic::uadd_with_overflow:
01127       // Cheat a little. We know that the registers for "add" and "seto" are
01128       // allocated sequentially. However, we only keep track of the register
01129       // for "add" in the value map. Use extractvalue's index to get the
01130       // correct register for "seto".
01131       UpdateValueMap(I, lookUpRegForValue(Agg) + *EI->idx_begin());
01132       return true;
01133     }
01134   }
01135 
01136   return false;
01137 }
01138 
01139 bool X86FastISel::X86VisitIntrinsicCall(IntrinsicInst &I) {
01140   // FIXME: Handle more intrinsics.
01141   switch (I.getIntrinsicID()) {
01142   default: return false;
01143   case Intrinsic::sadd_with_overflow:
01144   case Intrinsic::uadd_with_overflow: {
01145     // Replace "add with overflow" intrinsics with an "add" instruction followed
01146     // by a seto/setc instruction. Later on, when the "extractvalue"
01147     // instructions are encountered, we use the fact that two registers were
01148     // created sequentially to get the correct registers for the "sum" and the
01149     // "overflow bit".
01150     const Function *Callee = I.getCalledFunction();
01151     const Type *RetTy =
01152       cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
01153 
01154     EVT VT;
01155     if (!isTypeLegal(RetTy, VT))
01156       return false;
01157 
01158     Value *Op1 = I.getOperand(1);
01159     Value *Op2 = I.getOperand(2);
01160     unsigned Reg1 = getRegForValue(Op1);
01161     unsigned Reg2 = getRegForValue(Op2);
01162 
01163     if (Reg1 == 0 || Reg2 == 0)
01164       // FIXME: Handle values *not* in registers.
01165       return false;
01166 
01167     unsigned OpC = 0;
01168     if (VT == MVT::i32)
01169       OpC = X86::ADD32rr;
01170     else if (VT == MVT::i64)
01171       OpC = X86::ADD64rr;
01172     else
01173       return false;
01174 
01175     unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
01176     BuildMI(MBB, DL, TII.get(OpC), ResultReg).addReg(Reg1).addReg(Reg2);
01177     unsigned DestReg1 = UpdateValueMap(&I, ResultReg);
01178 
01179     // If the add with overflow is an intra-block value then we just want to
01180     // create temporaries for it like normal.  If it is a cross-block value then
01181     // UpdateValueMap will return the cross-block register used.  Since we
01182     // *really* want the value to be live in the register pair known by
01183     // UpdateValueMap, we have to use DestReg1+1 as the destination register in
01184     // the cross block case.  In the non-cross-block case, we should just make
01185     // another register for the value.
01186     if (DestReg1 != ResultReg)
01187       ResultReg = DestReg1+1;
01188     else
01189       ResultReg = createResultReg(TLI.getRegClassFor(MVT::i8));
01190     
01191     unsigned Opc = X86::SETBr;
01192     if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
01193       Opc = X86::SETOr;
01194     BuildMI(MBB, DL, TII.get(Opc), ResultReg);
01195     return true;
01196   }
01197   }
01198 }
01199 
01200 bool X86FastISel::X86SelectCall(Instruction *I) {
01201   CallInst *CI = cast<CallInst>(I);
01202   Value *Callee = I->getOperand(0);
01203 
01204   // Can't handle inline asm yet.
01205   if (isa<InlineAsm>(Callee))
01206     return false;
01207 
01208   // Handle intrinsic calls.
01209   if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
01210     return X86VisitIntrinsicCall(*II);
01211 
01212   // Handle only C and fastcc calling conventions for now.
01213   CallSite CS(CI);
01214   unsigned CC = CS.getCallingConv();
01215   if (CC != CallingConv::C &&
01216       CC != CallingConv::Fast &&
01217       CC != CallingConv::X86_FastCall)
01218     return false;
01219 
01220   // On X86, -tailcallopt changes the fastcc ABI. FastISel doesn't
01221   // handle this for now.
01222   if (CC == CallingConv::Fast && PerformTailCallOpt)
01223     return false;
01224 
01225   // Let SDISel handle vararg functions.
01226   const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
01227   const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
01228   if (FTy->isVarArg())
01229     return false;
01230 
01231   // Handle *simple* calls for now.
01232   const Type *RetTy = CS.getType();
01233   EVT RetVT;
01234   if (RetTy == Type::getVoidTy(I->getContext()))
01235     RetVT = MVT::isVoid;
01236   else if (!isTypeLegal(RetTy, RetVT, true))
01237     return false;
01238 
01239   // Materialize callee address in a register. FIXME: GV address can be
01240   // handled with a CALLpcrel32 instead.
01241   X86AddressMode CalleeAM;
01242   if (!X86SelectCallAddress(Callee, CalleeAM))
01243     return false;
01244   unsigned CalleeOp = 0;
01245   GlobalValue *GV = 0;
01246   if (CalleeAM.GV != 0) {
01247     GV = CalleeAM.GV;
01248   } else if (CalleeAM.Base.Reg != 0) {
01249     CalleeOp = CalleeAM.Base.Reg;
01250   } else
01251     return false;
01252 
01253   // Allow calls which produce i1 results.
01254   bool AndToI1 = false;
01255   if (RetVT == MVT::i1) {
01256     RetVT = MVT::i8;
01257     AndToI1 = true;
01258   }
01259 
01260   // Deal with call operands first.
01261   SmallVector<Value*, 8> ArgVals;
01262   SmallVector<unsigned, 8> Args;
01263   SmallVector<EVT, 8> ArgVTs;
01264   SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
01265   Args.reserve(CS.arg_size());
01266   ArgVals.reserve(CS.arg_size());
01267   ArgVTs.reserve(CS.arg_size());
01268   ArgFlags.reserve(CS.arg_size());
01269   for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
01270        i != e; ++i) {
01271     unsigned Arg = getRegForValue(*i);
01272     if (Arg == 0)
01273       return false;
01274     ISD::ArgFlagsTy Flags;
01275     unsigned AttrInd = i - CS.arg_begin() + 1;
01276     if (CS.paramHasAttr(AttrInd, Attribute::SExt))
01277       Flags.setSExt();
01278     if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
01279       Flags.setZExt();
01280 
01281     // FIXME: Only handle *easy* calls for now.
01282     if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
01283         CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
01284         CS.paramHasAttr(AttrInd, Attribute::Nest) ||
01285         CS.paramHasAttr(AttrInd, Attribute::ByVal))
01286       return false;
01287 
01288     const Type *ArgTy = (*i)->getType();
01289     EVT ArgVT;
01290     if (!isTypeLegal(ArgTy, ArgVT))
01291       return false;
01292     unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
01293     Flags.setOrigAlign(OriginalAlignment);
01294 
01295     Args.push_back(Arg);
01296     ArgVals.push_back(*i);
01297     ArgVTs.push_back(ArgVT);
01298     ArgFlags.push_back(Flags);
01299   }
01300 
01301   // Analyze operands of the call, assigning locations to each operand.
01302   SmallVector<CCValAssign, 16> ArgLocs;
01303   CCState CCInfo(CC, false, TM, ArgLocs, I->getParent()->getContext());
01304   CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
01305 
01306   // Get a count of how many bytes are to be pushed on the stack.
01307   unsigned NumBytes = CCInfo.getNextStackOffset();
01308 
01309   // Issue CALLSEQ_START
01310   unsigned AdjStackDown = TM.getRegisterInfo()->getCallFrameSetupOpcode();
01311   BuildMI(MBB, DL, TII.get(AdjStackDown)).addImm(NumBytes);
01312 
01313   // Process argument: walk the register/memloc assignments, inserting
01314   // copies / loads.
01315   SmallVector<unsigned, 4> RegArgs;
01316   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
01317     CCValAssign &VA = ArgLocs[i];
01318     unsigned Arg = Args[VA.getValNo()];
01319     EVT ArgVT = ArgVTs[VA.getValNo()];
01320   
01321     // Promote the value if needed.
01322     switch (VA.getLocInfo()) {
01323     default: llvm_unreachable("Unknown loc info!");
01324     case CCValAssign::Full: break;
01325     case CCValAssign::SExt: {
01326       bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
01327                                        Arg, ArgVT, Arg);
01328       assert(Emitted && "Failed to emit a sext!"); Emitted=Emitted;
01329       Emitted = true;
01330       ArgVT = VA.getLocVT();
01331       break;
01332     }
01333     case CCValAssign::ZExt: {
01334       bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
01335                                        Arg, ArgVT, Arg);
01336       assert(Emitted && "Failed to emit a zext!"); Emitted=Emitted;
01337       Emitted = true;
01338       ArgVT = VA.getLocVT();
01339       break;
01340     }
01341     case CCValAssign::AExt: {
01342       bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
01343                                        Arg, ArgVT, Arg);
01344       if (!Emitted)
01345         Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
01346                                     Arg, ArgVT, Arg);
01347       if (!Emitted)
01348         Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
01349                                     Arg, ArgVT, Arg);
01350       
01351       assert(Emitted && "Failed to emit a aext!"); Emitted=Emitted;
01352       ArgVT = VA.getLocVT();
01353       break;
01354     }
01355     case CCValAssign::BCvt: {
01356       unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT().getSimpleVT(),
01357                                ISD::BIT_CONVERT, Arg);
01358       assert(BC != 0 && "Failed to emit a bitcast!");
01359       Arg = BC;
01360       ArgVT = VA.getLocVT();
01361       break;
01362     }
01363     }
01364     
01365     if (VA.isRegLoc()) {
01366       TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
01367       bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
01368                                       Arg, RC, RC);
01369       assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
01370       Emitted = true;
01371       RegArgs.push_back(VA.getLocReg());
01372     } else {
01373       unsigned LocMemOffset = VA.getLocMemOffset();
01374       X86AddressMode AM;
01375       AM.Base.Reg = StackPtr;
01376       AM.Disp = LocMemOffset;
01377       Value *ArgVal = ArgVals[VA.getValNo()];
01378       
01379       // If this is a really simple value, emit this with the Value* version of
01380       // X86FastEmitStore.  If it isn't simple, we don't want to do this, as it
01381       // can cause us to reevaluate the argument.
01382       if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal))
01383         X86FastEmitStore(ArgVT, ArgVal, AM);
01384       else
01385         X86FastEmitStore(ArgVT, Arg, AM);
01386     }
01387   }
01388 
01389   // ELF / PIC requires GOT in the EBX register before function calls via PLT
01390   // GOT pointer.  
01391   if (Subtarget->isPICStyleGOT()) {
01392     TargetRegisterClass *RC = X86::GR32RegisterClass;
01393     unsigned Base = getInstrInfo()->getGlobalBaseReg(&MF);
01394     bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC);
01395     assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
01396     Emitted = true;
01397   }
01398   
01399   // Issue the call.
01400   MachineInstrBuilder MIB;
01401   if (CalleeOp) {
01402     // Register-indirect call.
01403     unsigned CallOpc = Subtarget->is64Bit() ? X86::CALL64r : X86::CALL32r;
01404     MIB = BuildMI(MBB, DL, TII.get(CallOpc)).addReg(CalleeOp);
01405     
01406   } else {
01407     // Direct call.
01408     assert(GV && "Not a direct call");
01409     unsigned CallOpc =
01410       Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32;
01411     
01412     // See if we need any target-specific flags on the GV operand.
01413     unsigned char OpFlags = 0;
01414     
01415     // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
01416     // external symbols most go through the PLT in PIC mode.  If the symbol
01417     // has hidden or protected visibility, or if it is static or local, then
01418     // we don't need to use the PLT - we can directly call it.
01419     if (Subtarget->isTargetELF() &&
01420         TM.getRelocationModel() == Reloc::PIC_ &&
01421         GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
01422       OpFlags = X86II::MO_PLT;
01423     } else if (Subtarget->isPICStyleStubAny() &&
01424                (GV->isDeclaration() || GV->isWeakForLinker()) &&
01425                Subtarget->getDarwinVers() < 9) {
01426       // PC-relative references to external symbols should go through $stub,
01427       // unless we're building with the leopard linker or later, which
01428       // automatically synthesizes these stubs.
01429       OpFlags = X86II::MO_DARWIN_STUB;
01430     }
01431     
01432     
01433     MIB = BuildMI(MBB, DL, TII.get(CallOpc)).addGlobalAddress(GV, 0, OpFlags);
01434   }
01435 
01436   // Add an implicit use GOT pointer in EBX.
01437   if (Subtarget->isPICStyleGOT())
01438     MIB.addReg(X86::EBX);
01439 
01440   // Add implicit physical register uses to the call.
01441   for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
01442     MIB.addReg(RegArgs[i]);
01443 
01444   // Issue CALLSEQ_END
01445   unsigned AdjStackUp = TM.getRegisterInfo()->getCallFrameDestroyOpcode();
01446   BuildMI(MBB, DL, TII.get(AdjStackUp)).addImm(NumBytes).addImm(0);
01447 
01448   // Now handle call return value (if any).
01449   if (RetVT.getSimpleVT().SimpleTy != MVT::isVoid) {
01450     SmallVector<CCValAssign, 16> RVLocs;
01451     CCState CCInfo(CC, false, TM, RVLocs, I->getParent()->getContext());
01452     CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
01453 
01454     // Copy all of the result registers out of their specified physreg.
01455     assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
01456     EVT CopyVT = RVLocs[0].getValVT();
01457     TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
01458     TargetRegisterClass *SrcRC = DstRC;
01459     
01460     // If this is a call to a function that returns an fp value on the x87 fp
01461     // stack, but where we prefer to use the value in xmm registers, copy it
01462     // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
01463     if ((RVLocs[0].getLocReg() == X86::ST0 ||
01464          RVLocs[0].getLocReg() == X86::ST1) &&
01465         isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
01466       CopyVT = MVT::f80;
01467       SrcRC = X86::RSTRegisterClass;
01468       DstRC = X86::RFP80RegisterClass;
01469     }
01470 
01471     unsigned ResultReg = createResultReg(DstRC);
01472     bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
01473                                     RVLocs[0].getLocReg(), DstRC, SrcRC);
01474     assert(Emitted && "Failed to emit a copy instruction!"); Emitted=Emitted;
01475     Emitted = true;
01476     if (CopyVT != RVLocs[0].getValVT()) {
01477       // Round the F80 the right size, which also moves to the appropriate xmm
01478       // register. This is accomplished by storing the F80 value in memory and
01479       // then loading it back. Ewww...
01480       EVT ResVT = RVLocs[0].getValVT();
01481       unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
01482       unsigned MemSize = ResVT.getSizeInBits()/8;
01483       int FI = MFI.CreateStackObject(MemSize, MemSize);
01484       addFrameReference(BuildMI(MBB, DL, TII.get(Opc)), FI).addReg(ResultReg);
01485       DstRC = ResVT == MVT::f32
01486         ? X86::FR32RegisterClass : X86::FR64RegisterClass;
01487       Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
01488       ResultReg = createResultReg(DstRC);
01489       addFrameReference(BuildMI(MBB, DL, TII.get(Opc), ResultReg), FI);
01490     }
01491 
01492     if (AndToI1) {
01493       // Mask out all but lowest bit for some call which produces an i1.
01494       unsigned AndResult = createResultReg(X86::GR8RegisterClass);
01495       BuildMI(MBB, DL, 
01496               TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
01497       ResultReg = AndResult;
01498     }
01499 
01500     UpdateValueMap(I, ResultReg);
01501   }
01502 
01503   return true;
01504 }
01505 
01506 
01507 bool
01508 X86FastISel::TargetSelectInstruction(Instruction *I)  {
01509   switch (I->getOpcode()) {
01510   default: break;
01511   case Instruction::Load:
01512     return X86SelectLoad(I);
01513   case Instruction::Store:
01514     return X86SelectStore(I);
01515   case Instruction::ICmp:
01516   case Instruction::FCmp:
01517     return X86SelectCmp(I);
01518   case Instruction::ZExt:
01519     return X86SelectZExt(I);
01520   case Instruction::Br:
01521     return X86SelectBranch(I);
01522   case Instruction::Call:
01523     return X86SelectCall(I);
01524   case Instruction::LShr:
01525   case Instruction::AShr:
01526   case Instruction::Shl:
01527     return X86SelectShift(I);
01528   case Instruction::Select:
01529     return X86SelectSelect(I);
01530   case Instruction::Trunc:
01531     return X86SelectTrunc(I);
01532   case Instruction::FPExt:
01533     return X86SelectFPExt(I);
01534   case Instruction::FPTrunc:
01535     return X86SelectFPTrunc(I);
01536   case Instruction::ExtractValue:
01537     return X86SelectExtractValue(I);
01538   case Instruction::IntToPtr: // Deliberate fall-through.
01539   case Instruction::PtrToInt: {
01540     EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
01541     EVT DstVT = TLI.getValueType(I->getType());
01542     if (DstVT.bitsGT(SrcVT))
01543       return X86SelectZExt(I);
01544     if (DstVT.bitsLT(SrcVT))
01545       return X86SelectTrunc(I);
01546     unsigned Reg = getRegForValue(I->getOperand(0));
01547     if (Reg == 0) return false;
01548     UpdateValueMap(I, Reg);
01549     return true;
01550   }
01551   }
01552 
01553   return false;
01554 }
01555 
01556 unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
01557   EVT VT;
01558   if (!isTypeLegal(C->getType(), VT))
01559     return false;
01560   
01561   // Get opcode and regclass of the output for the given load instruction.
01562   unsigned Opc = 0;
01563   const TargetRegisterClass *RC = NULL;
01564   switch (VT.getSimpleVT().SimpleTy) {
01565   default: return false;
01566   case MVT::i8:
01567     Opc = X86::MOV8rm;
01568     RC  = X86::GR8RegisterClass;
01569     break;
01570   case MVT::i16:
01571     Opc = X86::MOV16rm;
01572     RC  = X86::GR16RegisterClass;
01573     break;
01574   case MVT::i32:
01575     Opc = X86::MOV32rm;
01576     RC  = X86::GR32RegisterClass;
01577     break;
01578   case MVT::i64:
01579     // Must be in x86-64 mode.
01580     Opc = X86::MOV64rm;
01581     RC  = X86::GR64RegisterClass;
01582     break;
01583   case MVT::f32:
01584     if (Subtarget->hasSSE1()) {
01585       Opc = X86::MOVSSrm;
01586       RC  = X86::FR32RegisterClass;
01587     } else {
01588       Opc = X86::LD_Fp32m;
01589       RC  = X86::RFP32RegisterClass;
01590     }
01591     break;
01592   case MVT::f64:
01593     if (Subtarget->hasSSE2()) {
01594       Opc = X86::MOVSDrm;
01595       RC  = X86::FR64RegisterClass;
01596     } else {
01597       Opc = X86::LD_Fp64m;
01598       RC  = X86::RFP64RegisterClass;
01599     }
01600     break;
01601   case MVT::f80:
01602     // No f80 support yet.
01603     return false;
01604   }
01605   
01606   // Materialize addresses with LEA instructions.
01607   if (isa<GlobalValue>(C)) {
01608     X86AddressMode AM;
01609     if (X86SelectAddress(C, AM)) {
01610       if (TLI.getPointerTy() == MVT::i32)
01611         Opc = X86::LEA32r;
01612       else
01613         Opc = X86::LEA64r;
01614       unsigned ResultReg = createResultReg(RC);
01615       addLeaAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
01616       return ResultReg;
01617     }
01618     return 0;
01619   }
01620   
01621   // MachineConstantPool wants an explicit alignment.
01622   unsigned Align = TD.getPrefTypeAlignment(C->getType());
01623   if (Align == 0) {
01624     // Alignment of vector types.  FIXME!
01625     Align = TD.getTypeAllocSize(C->getType());
01626   }
01627   
01628   // x86-32 PIC requires a PIC base register for constant pools.
01629   unsigned PICBase = 0;
01630   unsigned char OpFlag = 0;
01631   if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
01632     OpFlag = X86II::MO_PIC_BASE_OFFSET;
01633     PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
01634   } else if (Subtarget->isPICStyleGOT()) {
01635     OpFlag = X86II::MO_GOTOFF;
01636     PICBase = getInstrInfo()->getGlobalBaseReg(&MF);
01637   } else if (Subtarget->isPICStyleRIPRel() &&
01638              TM.getCodeModel() == CodeModel::Small) {
01639     PICBase = X86::RIP;
01640   }
01641 
01642   // Create the load from the constant pool.
01643   unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
01644   unsigned ResultReg = createResultReg(RC);
01645   addConstantPoolReference(BuildMI(MBB, DL, TII.get(Opc), ResultReg),
01646                            MCPOffset, PICBase, OpFlag);
01647 
01648   return ResultReg;
01649 }
01650 
01651 unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
01652   // Fail on dynamic allocas. At this point, getRegForValue has already
01653   // checked its CSE maps, so if we're here trying to handle a dynamic
01654   // alloca, we're not going to succeed. X86SelectAddress has a
01655   // check for dynamic allocas, because it's called directly from
01656   // various places, but TargetMaterializeAlloca also needs a check
01657   // in order to avoid recursion between getRegForValue,
01658   // X86SelectAddrss, and TargetMaterializeAlloca.
01659   if (!StaticAllocaMap.count(C))
01660     return 0;
01661 
01662   X86AddressMode AM;
01663   if (!X86SelectAddress(C, AM))
01664     return 0;
01665   unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
01666   TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
01667   unsigned ResultReg = createResultReg(RC);
01668   addLeaAddress(BuildMI(MBB, DL, TII.get(Opc), ResultReg), AM);
01669   return ResultReg;
01670 }
01671 
01672 namespace llvm {
01673   llvm::FastISel *X86::createFastISel(MachineFunction &mf,
01674                         MachineModuleInfo *mmi,
01675                         DwarfWriter *dw,
01676                         DenseMap<const Value *, unsigned> &vm,
01677                         DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
01678                         DenseMap<const AllocaInst *, int> &am
01679 #ifndef NDEBUG
01680                         , SmallSet<Instruction*, 8> &cil
01681 #endif
01682                         ) {
01683     return new X86FastISel(mf, mmi, dw, vm, bm, am
01684 #ifndef NDEBUG
01685                            , cil
01686 #endif
01687                            );
01688   }
01689 }