Disable support for x86_f80 in X86FastISel. Supporting it would
[oota-llvm.git] / lib / Target / X86 / X86FastISel.cpp
1 //===-- X86FastISel.cpp - X86 FastISel implementation ---------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the X86-specific support for the FastISel class. Much
11 // of the target-specific code is generated by tablegen in the file
12 // X86GenFastISel.inc, which is #included here.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "X86.h"
17 #include "X86InstrBuilder.h"
18 #include "X86ISelLowering.h"
19 #include "X86RegisterInfo.h"
20 #include "X86Subtarget.h"
21 #include "X86TargetMachine.h"
22 #include "llvm/CallingConv.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Instructions.h"
25 #include "llvm/CodeGen/FastISel.h"
26 #include "llvm/CodeGen/MachineConstantPool.h"
27 #include "llvm/CodeGen/MachineFrameInfo.h"
28 #include "llvm/CodeGen/MachineRegisterInfo.h"
29 #include "llvm/Support/CallSite.h"
30 #include "llvm/Support/GetElementPtrTypeIterator.h"
31
32 using namespace llvm;
33
34 class X86FastISel : public FastISel {
35   /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
36   /// make the right decision when generating code for different targets.
37   const X86Subtarget *Subtarget;
38
39   /// StackPtr - Register used as the stack pointer.
40   ///
41   unsigned StackPtr;
42
43   /// GlobalBaseReg - keeps track of the virtual register mapped onto global
44   /// base register.
45   unsigned GlobalBaseReg;
46
47   /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87 
48   /// floating point ops.
49   /// When SSE is available, use it for f32 operations.
50   /// When SSE2 is available, use it for f64 operations.
51   bool X86ScalarSSEf64;
52   bool X86ScalarSSEf32;
53
54 public:
55   explicit X86FastISel(MachineFunction &mf,
56                        MachineModuleInfo *mmi,
57                        DenseMap<const Value *, unsigned> &vm,
58                        DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
59                        DenseMap<const AllocaInst *, int> &am)
60     : FastISel(mf, mmi, vm, bm, am) {
61     Subtarget = &TM.getSubtarget<X86Subtarget>();
62     StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
63     GlobalBaseReg = 0;
64     X86ScalarSSEf64 = Subtarget->hasSSE2();
65     X86ScalarSSEf32 = Subtarget->hasSSE1();
66   }
67
68   virtual bool TargetSelectInstruction(Instruction *I);
69
70 #include "X86GenFastISel.inc"
71
72 private:
73   bool X86FastEmitLoad(MVT VT, const X86AddressMode &AM, unsigned &RR);
74
75   bool X86FastEmitStore(MVT VT, unsigned Val,
76                         const X86AddressMode &AM);
77
78   bool X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT, unsigned Src, MVT SrcVT,
79                          unsigned &ResultReg);
80   
81   bool X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall);
82
83   bool X86SelectLoad(Instruction *I);
84   
85   bool X86SelectStore(Instruction *I);
86
87   bool X86SelectCmp(Instruction *I);
88
89   bool X86SelectZExt(Instruction *I);
90
91   bool X86SelectBranch(Instruction *I);
92
93   bool X86SelectShift(Instruction *I);
94
95   bool X86SelectSelect(Instruction *I);
96
97   bool X86SelectTrunc(Instruction *I);
98
99   bool X86SelectFPExt(Instruction *I);
100   bool X86SelectFPTrunc(Instruction *I);
101
102   bool X86SelectCall(Instruction *I);
103
104   CCAssignFn *CCAssignFnForCall(unsigned CC, bool isTailCall = false);
105
106   unsigned getGlobalBaseReg();
107
108   const X86InstrInfo *getInstrInfo() const {
109     return static_cast<const X86InstrInfo *>(TM.getInstrInfo());
110   }
111
112   unsigned TargetMaterializeConstant(Constant *C);
113
114   unsigned TargetMaterializeAlloca(AllocaInst *C);
115
116   /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
117   /// computed in an SSE register, not on the X87 floating point stack.
118   bool isScalarFPTypeInSSEReg(MVT VT) const {
119     return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
120       (VT == MVT::f32 && X86ScalarSSEf32);   // f32 is when SSE1
121   }
122
123 };
124
125 static bool isTypeLegal(const Type *Ty, const TargetLowering &TLI, MVT &VT,
126                         bool AllowI1 = false) {
127   VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
128   if (VT == MVT::Other || !VT.isSimple())
129     // Unhandled type. Halt "fast" selection and bail.
130     return false;
131   if (VT == MVT::iPTR)
132     // Use pointer type.
133     VT = TLI.getPointerTy();
134   // We only handle legal types. For example, on x86-32 the instruction
135   // selector contains all of the 64-bit instructions from x86-64,
136   // under the assumption that i64 won't be used if the target doesn't
137   // support it.
138   return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
139 }
140
141 /// getGlobalBaseReg - Return the the global base register. Output
142 /// instructions required to initialize the global base register, if necessary.
143 ///
144 unsigned X86FastISel::getGlobalBaseReg() {
145   assert(!Subtarget->is64Bit() && "X86-64 PIC uses RIP relative addressing");
146   if (!GlobalBaseReg)
147     GlobalBaseReg = getInstrInfo()->initializeGlobalBaseReg(MBB->getParent());
148   return GlobalBaseReg;
149 }
150
151 #include "X86GenCallingConv.inc"
152
153 /// CCAssignFnForCall - Selects the correct CCAssignFn for a given calling
154 /// convention.
155 CCAssignFn *X86FastISel::CCAssignFnForCall(unsigned CC, bool isTaillCall) {
156   if (Subtarget->is64Bit()) {
157     if (Subtarget->isTargetWin64())
158       return CC_X86_Win64_C;
159     else if (CC == CallingConv::Fast && isTaillCall)
160       return CC_X86_64_TailCall;
161     else
162       return CC_X86_64_C;
163   }
164
165   if (CC == CallingConv::X86_FastCall)
166     return CC_X86_32_FastCall;
167   else if (CC == CallingConv::Fast)
168     return CC_X86_32_FastCC;
169   else
170     return CC_X86_32_C;
171 }
172
173 /// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
174 /// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
175 /// Return true and the result register by reference if it is possible.
176 bool X86FastISel::X86FastEmitLoad(MVT VT, const X86AddressMode &AM,
177                                   unsigned &ResultReg) {
178   // Get opcode and regclass of the output for the given load instruction.
179   unsigned Opc = 0;
180   const TargetRegisterClass *RC = NULL;
181   switch (VT.getSimpleVT()) {
182   default: return false;
183   case MVT::i8:
184     Opc = X86::MOV8rm;
185     RC  = X86::GR8RegisterClass;
186     break;
187   case MVT::i16:
188     Opc = X86::MOV16rm;
189     RC  = X86::GR16RegisterClass;
190     break;
191   case MVT::i32:
192     Opc = X86::MOV32rm;
193     RC  = X86::GR32RegisterClass;
194     break;
195   case MVT::i64:
196     // Must be in x86-64 mode.
197     Opc = X86::MOV64rm;
198     RC  = X86::GR64RegisterClass;
199     break;
200   case MVT::f32:
201     if (Subtarget->hasSSE1()) {
202       Opc = X86::MOVSSrm;
203       RC  = X86::FR32RegisterClass;
204     } else {
205       Opc = X86::LD_Fp32m;
206       RC  = X86::RFP32RegisterClass;
207     }
208     break;
209   case MVT::f64:
210     if (Subtarget->hasSSE2()) {
211       Opc = X86::MOVSDrm;
212       RC  = X86::FR64RegisterClass;
213     } else {
214       Opc = X86::LD_Fp64m;
215       RC  = X86::RFP64RegisterClass;
216     }
217     break;
218   case MVT::f80:
219     // No f80 support yet.
220     return false;
221   }
222
223   ResultReg = createResultReg(RC);
224   addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
225   return true;
226 }
227
228 /// X86FastEmitStore - Emit a machine instruction to store a value Val of
229 /// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
230 /// and a displacement offset, or a GlobalAddress,
231 /// i.e. V. Return true if it is possible.
232 bool
233 X86FastISel::X86FastEmitStore(MVT VT, unsigned Val,
234                               const X86AddressMode &AM) {
235   // Get opcode and regclass of the output for the given store instruction.
236   unsigned Opc = 0;
237   const TargetRegisterClass *RC = NULL;
238   switch (VT.getSimpleVT()) {
239   default: return false;
240   case MVT::i8:
241     Opc = X86::MOV8mr;
242     RC  = X86::GR8RegisterClass;
243     break;
244   case MVT::i16:
245     Opc = X86::MOV16mr;
246     RC  = X86::GR16RegisterClass;
247     break;
248   case MVT::i32:
249     Opc = X86::MOV32mr;
250     RC  = X86::GR32RegisterClass;
251     break;
252   case MVT::i64:
253     // Must be in x86-64 mode.
254     Opc = X86::MOV64mr;
255     RC  = X86::GR64RegisterClass;
256     break;
257   case MVT::f32:
258     if (Subtarget->hasSSE1()) {
259       Opc = X86::MOVSSmr;
260       RC  = X86::FR32RegisterClass;
261     } else {
262       Opc = X86::ST_Fp32m;
263       RC  = X86::RFP32RegisterClass;
264     }
265     break;
266   case MVT::f64:
267     if (Subtarget->hasSSE2()) {
268       Opc = X86::MOVSDmr;
269       RC  = X86::FR64RegisterClass;
270     } else {
271       Opc = X86::ST_Fp64m;
272       RC  = X86::RFP64RegisterClass;
273     }
274     break;
275   case MVT::f80:
276     // No f80 support yet.
277     return false;
278   }
279
280   addFullAddress(BuildMI(MBB, TII.get(Opc)), AM).addReg(Val);
281   return true;
282 }
283
284 /// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
285 /// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
286 /// ISD::SIGN_EXTEND).
287 bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, MVT DstVT,
288                                     unsigned Src, MVT SrcVT,
289                                     unsigned &ResultReg) {
290   unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc, Src);
291   
292   if (RR != 0) {
293     ResultReg = RR;
294     return true;
295   } else
296     return false;
297 }
298
299 /// X86SelectAddress - Attempt to fill in an address from the given value.
300 ///
301 bool X86FastISel::X86SelectAddress(Value *V, X86AddressMode &AM, bool isCall) {
302   User *U;
303   unsigned Opcode = Instruction::UserOp1;
304   if (Instruction *I = dyn_cast<Instruction>(V)) {
305     Opcode = I->getOpcode();
306     U = I;
307   } else if (ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
308     Opcode = C->getOpcode();
309     U = C;
310   }
311
312   switch (Opcode) {
313   default: break;
314   case Instruction::BitCast:
315     // Look past bitcasts.
316     return X86SelectAddress(U->getOperand(0), AM, isCall);
317
318   case Instruction::IntToPtr:
319     // Look past no-op inttoptrs.
320     if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
321       return X86SelectAddress(U->getOperand(0), AM, isCall);
322
323   case Instruction::PtrToInt:
324     // Look past no-op ptrtoints.
325     if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
326       return X86SelectAddress(U->getOperand(0), AM, isCall);
327
328   case Instruction::Alloca: {
329     if (isCall) break;
330     // Do static allocas.
331     const AllocaInst *A = cast<AllocaInst>(V);
332     DenseMap<const AllocaInst*, int>::iterator SI = StaticAllocaMap.find(A);
333     if (SI == StaticAllocaMap.end())
334       return false;
335     AM.BaseType = X86AddressMode::FrameIndexBase;
336     AM.Base.FrameIndex = SI->second;
337     return true;
338   }
339
340   case Instruction::Add: {
341     if (isCall) break;
342     // Adds of constants are common and easy enough.
343     if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
344       AM.Disp += CI->getZExtValue();
345       return X86SelectAddress(U->getOperand(0), AM, isCall);
346     }
347     break;
348   }
349
350   case Instruction::GetElementPtr: {
351     if (isCall) break;
352     // Pattern-match simple GEPs.
353     uint64_t Disp = AM.Disp;
354     unsigned IndexReg = AM.IndexReg;
355     unsigned Scale = AM.Scale;
356     gep_type_iterator GTI = gep_type_begin(U);
357     // Look at all but the last index. Constants can be folded,
358     // and one dynamic index can be handled, if the scale is supported.
359     for (User::op_iterator i = U->op_begin() + 1, e = U->op_end();
360          i != e; ++i, ++GTI) {
361       Value *Op = *i;
362       if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
363         const StructLayout *SL = TD.getStructLayout(STy);
364         unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
365         Disp += SL->getElementOffset(Idx);
366       } else {
367         uint64_t S = TD.getABITypeSize(GTI.getIndexedType());
368         if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
369           // Constant-offset addressing.
370           Disp += CI->getZExtValue() * S;
371         } else if (IndexReg == 0 &&
372                    (S == 1 || S == 2 || S == 4 || S == 8)) {
373           // Scaled-index addressing.
374           Scale = S;
375           IndexReg = getRegForValue(Op);
376           if (IndexReg == 0)
377             return false;
378         } else
379           // Unsupported.
380           goto unsupported_gep;
381       }
382     }
383     // Ok, the GEP indices were covered by constant-offset and scaled-index
384     // addressing. Update the address state and move on to examining the base.
385     AM.IndexReg = IndexReg;
386     AM.Scale = Scale;
387     AM.Disp = Disp;
388     return X86SelectAddress(U->getOperand(0), AM, isCall);
389   unsupported_gep:
390     // Ok, the GEP indices weren't all covered.
391     break;
392   }
393   }
394
395   // Handle constant address.
396   if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
397     // Can't handle alternate code models yet.
398     if (TM.getCodeModel() != CodeModel::Default &&
399         TM.getCodeModel() != CodeModel::Small)
400       return false;
401
402     // Set up the basic address.
403     AM.GV = GV;
404     if (!isCall &&
405         TM.getRelocationModel() == Reloc::PIC_ &&
406         !Subtarget->is64Bit())
407       AM.Base.Reg = getGlobalBaseReg();
408
409     // Emit an extra load if the ABI requires it.
410     if (Subtarget->GVRequiresExtraLoad(GV, TM, isCall)) {
411       // Check to see if we've already materialized this
412       // value in a register in this block.
413       if (unsigned Reg = LocalValueMap[V]) {
414         AM.Base.Reg = Reg;
415         AM.GV = 0;
416         return true;
417       }
418       // Issue load from stub if necessary.
419       unsigned Opc = 0;
420       const TargetRegisterClass *RC = NULL;
421       if (TLI.getPointerTy() == MVT::i32) {
422         Opc = X86::MOV32rm;
423         RC  = X86::GR32RegisterClass;
424       } else {
425         Opc = X86::MOV64rm;
426         RC  = X86::GR64RegisterClass;
427       }
428
429       X86AddressMode StubAM;
430       StubAM.Base.Reg = AM.Base.Reg;
431       StubAM.GV = AM.GV;
432       unsigned ResultReg = createResultReg(RC);
433       addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), StubAM);
434
435       // Now construct the final address. Note that the Disp, Scale,
436       // and Index values may already be set here.
437       AM.Base.Reg = ResultReg;
438       AM.GV = 0;
439
440       // Prevent loading GV stub multiple times in same MBB.
441       LocalValueMap[V] = AM.Base.Reg;
442     }
443     return true;
444   }
445
446   // If all else fails, just materialize the value in a register.
447   AM.Base.Reg = getRegForValue(V);
448   return AM.Base.Reg != 0;
449 }
450
451 /// X86SelectStore - Select and emit code to implement store instructions.
452 bool X86FastISel::X86SelectStore(Instruction* I) {
453   MVT VT;
454   if (!isTypeLegal(I->getOperand(0)->getType(), TLI, VT))
455     return false;
456   unsigned Val = getRegForValue(I->getOperand(0));
457   if (Val == 0)
458     // Unhandled operand. Halt "fast" selection and bail.
459     return false;    
460
461   X86AddressMode AM;
462   if (!X86SelectAddress(I->getOperand(1), AM, false))
463     return false;
464
465   return X86FastEmitStore(VT, Val, AM);
466 }
467
468 /// X86SelectLoad - Select and emit code to implement load instructions.
469 ///
470 bool X86FastISel::X86SelectLoad(Instruction *I)  {
471   MVT VT;
472   if (!isTypeLegal(I->getType(), TLI, VT))
473     return false;
474
475   X86AddressMode AM;
476   if (!X86SelectAddress(I->getOperand(0), AM, false))
477     return false;
478
479   unsigned ResultReg = 0;
480   if (X86FastEmitLoad(VT, AM, ResultReg)) {
481     UpdateValueMap(I, ResultReg);
482     return true;
483   }
484   return false;
485 }
486
487 bool X86FastISel::X86SelectCmp(Instruction *I) {
488   CmpInst *CI = cast<CmpInst>(I);
489
490   MVT VT = TLI.getValueType(I->getOperand(0)->getType());
491   if (!TLI.isTypeLegal(VT))
492     return false;
493
494   unsigned Op0Reg = getRegForValue(CI->getOperand(0));
495   if (Op0Reg == 0) return false;
496   unsigned Op1Reg = getRegForValue(CI->getOperand(1));
497   if (Op1Reg == 0) return false;
498
499   unsigned Opc;
500   switch (VT.getSimpleVT()) {
501   case MVT::i8: Opc = X86::CMP8rr; break;
502   case MVT::i16: Opc = X86::CMP16rr; break;
503   case MVT::i32: Opc = X86::CMP32rr; break;
504   case MVT::i64: Opc = X86::CMP64rr; break;
505   case MVT::f32: Opc = X86::UCOMISSrr; break;
506   case MVT::f64: Opc = X86::UCOMISDrr; break;
507   default: return false;
508   }
509
510   unsigned ResultReg = createResultReg(&X86::GR8RegClass);
511   switch (CI->getPredicate()) {
512   case CmpInst::FCMP_OEQ: {
513     unsigned EReg = createResultReg(&X86::GR8RegClass);
514     unsigned NPReg = createResultReg(&X86::GR8RegClass);
515     BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
516     BuildMI(MBB, TII.get(X86::SETEr), EReg);
517     BuildMI(MBB, TII.get(X86::SETNPr), NPReg);
518     BuildMI(MBB, TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
519     break;
520   }
521   case CmpInst::FCMP_UNE: {
522     unsigned NEReg = createResultReg(&X86::GR8RegClass);
523     unsigned PReg = createResultReg(&X86::GR8RegClass);
524     BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
525     BuildMI(MBB, TII.get(X86::SETNEr), NEReg);
526     BuildMI(MBB, TII.get(X86::SETPr), PReg);
527     BuildMI(MBB, TII.get(X86::OR8rr), ResultReg).addReg(PReg).addReg(NEReg);
528     break;
529   }
530   case CmpInst::FCMP_OGT:
531     BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
532     BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
533     break;
534   case CmpInst::FCMP_OGE:
535     BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
536     BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
537     break;
538   case CmpInst::FCMP_OLT:
539     BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
540     BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
541     break;
542   case CmpInst::FCMP_OLE:
543     BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
544     BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
545     break;
546   case CmpInst::FCMP_ONE:
547     BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
548     BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
549     break;
550   case CmpInst::FCMP_ORD:
551     BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
552     BuildMI(MBB, TII.get(X86::SETNPr), ResultReg);
553     break;
554   case CmpInst::FCMP_UNO:
555     BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
556     BuildMI(MBB, TII.get(X86::SETPr), ResultReg);
557     break;
558   case CmpInst::FCMP_UEQ:
559     BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
560     BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
561     break;
562   case CmpInst::FCMP_UGT:
563     BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
564     BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
565     break;
566   case CmpInst::FCMP_UGE:
567     BuildMI(MBB, TII.get(Opc)).addReg(Op1Reg).addReg(Op0Reg);
568     BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
569     break;
570   case CmpInst::FCMP_ULT:
571     BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
572     BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
573     break;
574   case CmpInst::FCMP_ULE:
575     BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
576     BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
577     break;
578   case CmpInst::ICMP_EQ:
579     BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
580     BuildMI(MBB, TII.get(X86::SETEr), ResultReg);
581     break;
582   case CmpInst::ICMP_NE:
583     BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
584     BuildMI(MBB, TII.get(X86::SETNEr), ResultReg);
585     break;
586   case CmpInst::ICMP_UGT:
587     BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
588     BuildMI(MBB, TII.get(X86::SETAr), ResultReg);
589     break;
590   case CmpInst::ICMP_UGE:
591     BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
592     BuildMI(MBB, TII.get(X86::SETAEr), ResultReg);
593     break;
594   case CmpInst::ICMP_ULT:
595     BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
596     BuildMI(MBB, TII.get(X86::SETBr), ResultReg);
597     break;
598   case CmpInst::ICMP_ULE:
599     BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
600     BuildMI(MBB, TII.get(X86::SETBEr), ResultReg);
601     break;
602   case CmpInst::ICMP_SGT:
603     BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
604     BuildMI(MBB, TII.get(X86::SETGr), ResultReg);
605     break;
606   case CmpInst::ICMP_SGE:
607     BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
608     BuildMI(MBB, TII.get(X86::SETGEr), ResultReg);
609     break;
610   case CmpInst::ICMP_SLT:
611     BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
612     BuildMI(MBB, TII.get(X86::SETLr), ResultReg);
613     break;
614   case CmpInst::ICMP_SLE:
615     BuildMI(MBB, TII.get(Opc)).addReg(Op0Reg).addReg(Op1Reg);
616     BuildMI(MBB, TII.get(X86::SETLEr), ResultReg);
617     break;
618   default:
619     return false;
620   }
621
622   UpdateValueMap(I, ResultReg);
623   return true;
624 }
625
626 bool X86FastISel::X86SelectZExt(Instruction *I) {
627   // Special-case hack: The only i1 values we know how to produce currently
628   // set the upper bits of an i8 value to zero.
629   if (I->getType() == Type::Int8Ty &&
630       I->getOperand(0)->getType() == Type::Int1Ty) {
631     unsigned ResultReg = getRegForValue(I->getOperand(0));
632     if (ResultReg == 0) return false;
633     UpdateValueMap(I, ResultReg);
634     return true;
635   }
636
637   return false;
638 }
639
640 bool X86FastISel::X86SelectBranch(Instruction *I) {
641   BranchInst *BI = cast<BranchInst>(I);
642   // Unconditional branches are selected by tablegen-generated code.
643   unsigned OpReg = getRegForValue(BI->getCondition());
644   if (OpReg == 0) return false;
645   MachineBasicBlock *TrueMBB = MBBMap[BI->getSuccessor(0)];
646   MachineBasicBlock *FalseMBB = MBBMap[BI->getSuccessor(1)];
647
648   BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(OpReg).addReg(OpReg);
649   BuildMI(MBB, TII.get(X86::JNE)).addMBB(TrueMBB);
650   BuildMI(MBB, TII.get(X86::JMP)).addMBB(FalseMBB);
651
652   MBB->addSuccessor(TrueMBB);
653   MBB->addSuccessor(FalseMBB);
654
655   return true;
656 }
657
658 bool X86FastISel::X86SelectShift(Instruction *I) {
659   unsigned CReg = 0, OpReg = 0, OpImm = 0;
660   const TargetRegisterClass *RC = NULL;
661   if (I->getType() == Type::Int8Ty) {
662     CReg = X86::CL;
663     RC = &X86::GR8RegClass;
664     switch (I->getOpcode()) {
665     case Instruction::LShr: OpReg = X86::SHR8rCL; OpImm = X86::SHR8ri; break;
666     case Instruction::AShr: OpReg = X86::SAR8rCL; OpImm = X86::SAR8ri; break;
667     case Instruction::Shl:  OpReg = X86::SHL8rCL; OpImm = X86::SHL8ri; break;
668     default: return false;
669     }
670   } else if (I->getType() == Type::Int16Ty) {
671     CReg = X86::CX;
672     RC = &X86::GR16RegClass;
673     switch (I->getOpcode()) {
674     case Instruction::LShr: OpReg = X86::SHR16rCL; OpImm = X86::SHR16ri; break;
675     case Instruction::AShr: OpReg = X86::SAR16rCL; OpImm = X86::SAR16ri; break;
676     case Instruction::Shl:  OpReg = X86::SHL16rCL; OpImm = X86::SHL16ri; break;
677     default: return false;
678     }
679   } else if (I->getType() == Type::Int32Ty) {
680     CReg = X86::ECX;
681     RC = &X86::GR32RegClass;
682     switch (I->getOpcode()) {
683     case Instruction::LShr: OpReg = X86::SHR32rCL; OpImm = X86::SHR32ri; break;
684     case Instruction::AShr: OpReg = X86::SAR32rCL; OpImm = X86::SAR32ri; break;
685     case Instruction::Shl:  OpReg = X86::SHL32rCL; OpImm = X86::SHL32ri; break;
686     default: return false;
687     }
688   } else if (I->getType() == Type::Int64Ty) {
689     CReg = X86::RCX;
690     RC = &X86::GR64RegClass;
691     switch (I->getOpcode()) {
692     case Instruction::LShr: OpReg = X86::SHR64rCL; OpImm = X86::SHR64ri; break;
693     case Instruction::AShr: OpReg = X86::SAR64rCL; OpImm = X86::SAR64ri; break;
694     case Instruction::Shl:  OpReg = X86::SHL64rCL; OpImm = X86::SHL64ri; break;
695     default: return false;
696     }
697   } else {
698     return false;
699   }
700
701   MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
702   if (VT == MVT::Other || !TLI.isTypeLegal(VT))
703     return false;
704
705   unsigned Op0Reg = getRegForValue(I->getOperand(0));
706   if (Op0Reg == 0) return false;
707   
708   // Fold immediate in shl(x,3).
709   if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
710     unsigned ResultReg = createResultReg(RC);
711     BuildMI(MBB, TII.get(OpImm), 
712             ResultReg).addReg(Op0Reg).addImm(CI->getZExtValue());
713     UpdateValueMap(I, ResultReg);
714     return true;
715   }
716   
717   unsigned Op1Reg = getRegForValue(I->getOperand(1));
718   if (Op1Reg == 0) return false;
719   TII.copyRegToReg(*MBB, MBB->end(), CReg, Op1Reg, RC, RC);
720   unsigned ResultReg = createResultReg(RC);
721   BuildMI(MBB, TII.get(OpReg), ResultReg).addReg(Op0Reg);
722   UpdateValueMap(I, ResultReg);
723   return true;
724 }
725
726 bool X86FastISel::X86SelectSelect(Instruction *I) {
727   const Type *Ty = I->getType();
728   if (isa<PointerType>(Ty))
729     Ty = TD.getIntPtrType();
730
731   unsigned Opc = 0;
732   const TargetRegisterClass *RC = NULL;
733   if (Ty == Type::Int16Ty) {
734     Opc = X86::CMOVE16rr;
735     RC = &X86::GR16RegClass;
736   } else if (Ty == Type::Int32Ty) {
737     Opc = X86::CMOVE32rr;
738     RC = &X86::GR32RegClass;
739   } else if (Ty == Type::Int64Ty) {
740     Opc = X86::CMOVE64rr;
741     RC = &X86::GR64RegClass;
742   } else {
743     return false; 
744   }
745
746   MVT VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
747   if (VT == MVT::Other || !TLI.isTypeLegal(VT))
748     return false;
749
750   unsigned Op0Reg = getRegForValue(I->getOperand(0));
751   if (Op0Reg == 0) return false;
752   unsigned Op1Reg = getRegForValue(I->getOperand(1));
753   if (Op1Reg == 0) return false;
754   unsigned Op2Reg = getRegForValue(I->getOperand(2));
755   if (Op2Reg == 0) return false;
756
757   BuildMI(MBB, TII.get(X86::TEST8rr)).addReg(Op0Reg).addReg(Op0Reg);
758   unsigned ResultReg = createResultReg(RC);
759   BuildMI(MBB, TII.get(Opc), ResultReg).addReg(Op1Reg).addReg(Op2Reg);
760   UpdateValueMap(I, ResultReg);
761   return true;
762 }
763
764 bool X86FastISel::X86SelectFPExt(Instruction *I) {
765   if (Subtarget->hasSSE2()) {
766     if (I->getType() == Type::DoubleTy) {
767       Value *V = I->getOperand(0);
768       if (V->getType() == Type::FloatTy) {
769         unsigned OpReg = getRegForValue(V);
770         if (OpReg == 0) return false;
771         unsigned ResultReg = createResultReg(X86::FR64RegisterClass);
772         BuildMI(MBB, TII.get(X86::CVTSS2SDrr), ResultReg).addReg(OpReg);
773         UpdateValueMap(I, ResultReg);
774         return true;
775       }
776     }
777   }
778
779   return false;
780 }
781
782 bool X86FastISel::X86SelectFPTrunc(Instruction *I) {
783   if (Subtarget->hasSSE2()) {
784     if (I->getType() == Type::FloatTy) {
785       Value *V = I->getOperand(0);
786       if (V->getType() == Type::DoubleTy) {
787         unsigned OpReg = getRegForValue(V);
788         if (OpReg == 0) return false;
789         unsigned ResultReg = createResultReg(X86::FR32RegisterClass);
790         BuildMI(MBB, TII.get(X86::CVTSD2SSrr), ResultReg).addReg(OpReg);
791         UpdateValueMap(I, ResultReg);
792         return true;
793       }
794     }
795   }
796
797   return false;
798 }
799
800 bool X86FastISel::X86SelectTrunc(Instruction *I) {
801   if (Subtarget->is64Bit())
802     // All other cases should be handled by the tblgen generated code.
803     return false;
804   MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
805   MVT DstVT = TLI.getValueType(I->getType());
806   if (DstVT != MVT::i8)
807     // All other cases should be handled by the tblgen generated code.
808     return false;
809   if (SrcVT != MVT::i16 && SrcVT != MVT::i32)
810     // All other cases should be handled by the tblgen generated code.
811     return false;
812
813   unsigned InputReg = getRegForValue(I->getOperand(0));
814   if (!InputReg)
815     // Unhandled operand.  Halt "fast" selection and bail.
816     return false;
817
818   // First issue a copy to GR16_ or GR32_.
819   unsigned CopyOpc = (SrcVT == MVT::i16) ? X86::MOV16to16_ : X86::MOV32to32_;
820   const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16)
821     ? X86::GR16_RegisterClass : X86::GR32_RegisterClass;
822   unsigned CopyReg = createResultReg(CopyRC);
823   BuildMI(MBB, TII.get(CopyOpc), CopyReg).addReg(InputReg);
824
825   // Then issue an extract_subreg.
826   unsigned ResultReg = FastEmitInst_extractsubreg(CopyReg,1); // x86_subreg_8bit
827   if (!ResultReg)
828     return false;
829
830   UpdateValueMap(I, ResultReg);
831   return true;
832 }
833
834 bool X86FastISel::X86SelectCall(Instruction *I) {
835   CallInst *CI = cast<CallInst>(I);
836   Value *Callee = I->getOperand(0);
837
838   // Can't handle inline asm yet.
839   if (isa<InlineAsm>(Callee))
840     return false;
841
842   // FIXME: Handle some intrinsics.
843   if (Function *F = CI->getCalledFunction()) {
844     if (F->isDeclaration() &&F->getIntrinsicID())
845       return false;
846   }
847
848   // Handle only C and fastcc calling conventions for now.
849   CallSite CS(CI);
850   unsigned CC = CS.getCallingConv();
851   if (CC != CallingConv::C &&
852       CC != CallingConv::Fast &&
853       CC != CallingConv::X86_FastCall)
854     return false;
855
856   // Let SDISel handle vararg functions.
857   const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
858   const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
859   if (FTy->isVarArg())
860     return false;
861
862   // Handle *simple* calls for now.
863   const Type *RetTy = CS.getType();
864   MVT RetVT;
865   if (RetTy == Type::VoidTy)
866     RetVT = MVT::isVoid;
867   else if (!isTypeLegal(RetTy, TLI, RetVT, true))
868     return false;
869
870   // Materialize callee address in a register. FIXME: GV address can be
871   // handled with a CALLpcrel32 instead.
872   X86AddressMode CalleeAM;
873   if (!X86SelectAddress(Callee, CalleeAM, true))
874     return false;
875   unsigned CalleeOp = 0;
876   GlobalValue *GV = 0;
877   if (CalleeAM.Base.Reg != 0) {
878     assert(CalleeAM.GV == 0);
879     CalleeOp = CalleeAM.Base.Reg;
880   } else if (CalleeAM.GV != 0) {
881     assert(CalleeAM.GV != 0);
882     GV = CalleeAM.GV;
883   } else
884     return false;
885
886   // Allow calls which produce i1 results.
887   bool AndToI1 = false;
888   if (RetVT == MVT::i1) {
889     RetVT = MVT::i8;
890     AndToI1 = true;
891   }
892
893   // Deal with call operands first.
894   SmallVector<unsigned, 4> Args;
895   SmallVector<MVT, 4> ArgVTs;
896   SmallVector<ISD::ArgFlagsTy, 4> ArgFlags;
897   Args.reserve(CS.arg_size());
898   ArgVTs.reserve(CS.arg_size());
899   ArgFlags.reserve(CS.arg_size());
900   for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
901        i != e; ++i) {
902     unsigned Arg = getRegForValue(*i);
903     if (Arg == 0)
904       return false;
905     ISD::ArgFlagsTy Flags;
906     unsigned AttrInd = i - CS.arg_begin() + 1;
907     if (CS.paramHasAttr(AttrInd, Attribute::SExt))
908       Flags.setSExt();
909     if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
910       Flags.setZExt();
911
912     // FIXME: Only handle *easy* calls for now.
913     if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
914         CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
915         CS.paramHasAttr(AttrInd, Attribute::Nest) ||
916         CS.paramHasAttr(AttrInd, Attribute::ByVal))
917       return false;
918
919     const Type *ArgTy = (*i)->getType();
920     MVT ArgVT;
921     if (!isTypeLegal(ArgTy, TLI, ArgVT))
922       return false;
923     unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
924     Flags.setOrigAlign(OriginalAlignment);
925
926     Args.push_back(Arg);
927     ArgVTs.push_back(ArgVT);
928     ArgFlags.push_back(Flags);
929   }
930
931   // Analyze operands of the call, assigning locations to each operand.
932   SmallVector<CCValAssign, 16> ArgLocs;
933   CCState CCInfo(CC, false, TM, ArgLocs);
934   CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
935
936   // Get a count of how many bytes are to be pushed on the stack.
937   unsigned NumBytes = CCInfo.getNextStackOffset();
938
939   // Issue CALLSEQ_START
940   BuildMI(MBB, TII.get(X86::ADJCALLSTACKDOWN)).addImm(NumBytes);
941
942   // Process argumenet: walk the register/memloc assignments, inserting
943   // copies / loads.
944   SmallVector<unsigned, 4> RegArgs;
945   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
946     CCValAssign &VA = ArgLocs[i];
947     unsigned Arg = Args[VA.getValNo()];
948     MVT ArgVT = ArgVTs[VA.getValNo()];
949   
950     // Promote the value if needed.
951     switch (VA.getLocInfo()) {
952     default: assert(0 && "Unknown loc info!");
953     case CCValAssign::Full: break;
954     case CCValAssign::SExt: {
955       bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
956                                        Arg, ArgVT, Arg);
957       assert(Emitted && "Failed to emit a sext!");
958       ArgVT = VA.getLocVT();
959       break;
960     }
961     case CCValAssign::ZExt: {
962       bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
963                                        Arg, ArgVT, Arg);
964       assert(Emitted && "Failed to emit a zext!");
965       ArgVT = VA.getLocVT();
966       break;
967     }
968     case CCValAssign::AExt: {
969       bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
970                                        Arg, ArgVT, Arg);
971       if (!Emitted)
972         Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
973                                          Arg, ArgVT, Arg);
974       if (!Emitted)
975         Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
976                                     Arg, ArgVT, Arg);
977       
978       assert(Emitted && "Failed to emit a aext!");
979       ArgVT = VA.getLocVT();
980       break;
981     }
982     }
983     
984     if (VA.isRegLoc()) {
985       TargetRegisterClass* RC = TLI.getRegClassFor(ArgVT);
986       bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), VA.getLocReg(),
987                                       Arg, RC, RC);
988       assert(Emitted && "Failed to emit a copy instruction!");
989       RegArgs.push_back(VA.getLocReg());
990     } else {
991       unsigned LocMemOffset = VA.getLocMemOffset();
992       X86AddressMode AM;
993       AM.Base.Reg = StackPtr;
994       AM.Disp = LocMemOffset;
995       X86FastEmitStore(ArgVT, Arg, AM);
996     }
997   }
998
999   // ELF / PIC requires GOT in the EBX register before function calls via PLT
1000   // GOT pointer.  
1001   if (!Subtarget->is64Bit() &&
1002       TM.getRelocationModel() == Reloc::PIC_ &&
1003       Subtarget->isPICStyleGOT()) {
1004     TargetRegisterClass *RC = X86::GR32RegisterClass;
1005     unsigned Base = getGlobalBaseReg();
1006     bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), X86::EBX, Base, RC, RC);
1007     assert(Emitted && "Failed to emit a copy instruction!");
1008   }
1009
1010   // Issue the call.
1011   unsigned CallOpc = CalleeOp
1012     ? (Subtarget->is64Bit() ? X86::CALL64r       : X86::CALL32r)
1013     : (Subtarget->is64Bit() ? X86::CALL64pcrel32 : X86::CALLpcrel32);
1014   MachineInstrBuilder MIB = CalleeOp
1015     ? BuildMI(MBB, TII.get(CallOpc)).addReg(CalleeOp)
1016     : BuildMI(MBB, TII.get(CallOpc)).addGlobalAddress(GV);
1017
1018   // Add an implicit use GOT pointer in EBX.
1019   if (!Subtarget->is64Bit() &&
1020       TM.getRelocationModel() == Reloc::PIC_ &&
1021       Subtarget->isPICStyleGOT())
1022     MIB.addReg(X86::EBX);
1023
1024   // Add implicit physical register uses to the call.
1025   while (!RegArgs.empty()) {
1026     MIB.addReg(RegArgs.back());
1027     RegArgs.pop_back();
1028   }
1029
1030   // Issue CALLSEQ_END
1031   BuildMI(MBB, TII.get(X86::ADJCALLSTACKUP)).addImm(NumBytes).addImm(0);
1032
1033   // Now handle call return value (if any).
1034   if (RetVT.getSimpleVT() != MVT::isVoid) {
1035     SmallVector<CCValAssign, 16> RVLocs;
1036     CCState CCInfo(CC, false, TM, RVLocs);
1037     CCInfo.AnalyzeCallResult(RetVT, RetCC_X86);
1038
1039     // Copy all of the result registers out of their specified physreg.
1040     assert(RVLocs.size() == 1 && "Can't handle multi-value calls!");
1041     MVT CopyVT = RVLocs[0].getValVT();
1042     TargetRegisterClass* DstRC = TLI.getRegClassFor(CopyVT);
1043     TargetRegisterClass *SrcRC = DstRC;
1044     
1045     // If this is a call to a function that returns an fp value on the x87 fp
1046     // stack, but where we prefer to use the value in xmm registers, copy it
1047     // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1048     if ((RVLocs[0].getLocReg() == X86::ST0 ||
1049          RVLocs[0].getLocReg() == X86::ST1) &&
1050         isScalarFPTypeInSSEReg(RVLocs[0].getValVT())) {
1051       CopyVT = MVT::f80;
1052       SrcRC = X86::RSTRegisterClass;
1053       DstRC = X86::RFP80RegisterClass;
1054     }
1055
1056     unsigned ResultReg = createResultReg(DstRC);
1057     bool Emitted = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
1058                                     RVLocs[0].getLocReg(), DstRC, SrcRC);
1059     assert(Emitted && "Failed to emit a copy instruction!");
1060     if (CopyVT != RVLocs[0].getValVT()) {
1061       // Round the F80 the right size, which also moves to the appropriate xmm
1062       // register. This is accomplished by storing the F80 value in memory and
1063       // then loading it back. Ewww...
1064       MVT ResVT = RVLocs[0].getValVT();
1065       unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
1066       unsigned MemSize = ResVT.getSizeInBits()/8;
1067       int FI = MFI.CreateStackObject(MemSize, MemSize);
1068       addFrameReference(BuildMI(MBB, TII.get(Opc)), FI).addReg(ResultReg);
1069       DstRC = ResVT == MVT::f32
1070         ? X86::FR32RegisterClass : X86::FR64RegisterClass;
1071       Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
1072       ResultReg = createResultReg(DstRC);
1073       addFrameReference(BuildMI(MBB, TII.get(Opc), ResultReg), FI);
1074     }
1075
1076     if (AndToI1) {
1077       // Mask out all but lowest bit for some call which produces an i1.
1078       unsigned AndResult = createResultReg(X86::GR8RegisterClass);
1079       BuildMI(MBB, TII.get(X86::AND8ri), AndResult).addReg(ResultReg).addImm(1);
1080       ResultReg = AndResult;
1081     }
1082
1083     UpdateValueMap(I, ResultReg);
1084   }
1085
1086   return true;
1087 }
1088
1089
1090 bool
1091 X86FastISel::TargetSelectInstruction(Instruction *I)  {
1092   switch (I->getOpcode()) {
1093   default: break;
1094   case Instruction::Load:
1095     return X86SelectLoad(I);
1096   case Instruction::Store:
1097     return X86SelectStore(I);
1098   case Instruction::ICmp:
1099   case Instruction::FCmp:
1100     return X86SelectCmp(I);
1101   case Instruction::ZExt:
1102     return X86SelectZExt(I);
1103   case Instruction::Br:
1104     return X86SelectBranch(I);
1105   case Instruction::Call:
1106     return X86SelectCall(I);
1107   case Instruction::LShr:
1108   case Instruction::AShr:
1109   case Instruction::Shl:
1110     return X86SelectShift(I);
1111   case Instruction::Select:
1112     return X86SelectSelect(I);
1113   case Instruction::Trunc:
1114     return X86SelectTrunc(I);
1115   case Instruction::FPExt:
1116     return X86SelectFPExt(I);
1117   case Instruction::FPTrunc:
1118     return X86SelectFPTrunc(I);
1119   }
1120
1121   return false;
1122 }
1123
1124 unsigned X86FastISel::TargetMaterializeConstant(Constant *C) {
1125   MVT VT;
1126   if (!isTypeLegal(C->getType(), TLI, VT))
1127     return false;
1128   
1129   // Get opcode and regclass of the output for the given load instruction.
1130   unsigned Opc = 0;
1131   const TargetRegisterClass *RC = NULL;
1132   switch (VT.getSimpleVT()) {
1133   default: return false;
1134   case MVT::i8:
1135     Opc = X86::MOV8rm;
1136     RC  = X86::GR8RegisterClass;
1137     break;
1138   case MVT::i16:
1139     Opc = X86::MOV16rm;
1140     RC  = X86::GR16RegisterClass;
1141     break;
1142   case MVT::i32:
1143     Opc = X86::MOV32rm;
1144     RC  = X86::GR32RegisterClass;
1145     break;
1146   case MVT::i64:
1147     // Must be in x86-64 mode.
1148     Opc = X86::MOV64rm;
1149     RC  = X86::GR64RegisterClass;
1150     break;
1151   case MVT::f32:
1152     if (Subtarget->hasSSE1()) {
1153       Opc = X86::MOVSSrm;
1154       RC  = X86::FR32RegisterClass;
1155     } else {
1156       Opc = X86::LD_Fp32m;
1157       RC  = X86::RFP32RegisterClass;
1158     }
1159     break;
1160   case MVT::f64:
1161     if (Subtarget->hasSSE2()) {
1162       Opc = X86::MOVSDrm;
1163       RC  = X86::FR64RegisterClass;
1164     } else {
1165       Opc = X86::LD_Fp64m;
1166       RC  = X86::RFP64RegisterClass;
1167     }
1168     break;
1169   case MVT::f80:
1170     // No f80 support yet.
1171     return false;
1172   }
1173   
1174   // Materialize addresses with LEA instructions.
1175   if (isa<GlobalValue>(C)) {
1176     X86AddressMode AM;
1177     if (X86SelectAddress(C, AM, false)) {
1178       if (TLI.getPointerTy() == MVT::i32)
1179         Opc = X86::LEA32r;
1180       else
1181         Opc = X86::LEA64r;
1182       unsigned ResultReg = createResultReg(RC);
1183       addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1184       return ResultReg;
1185     }
1186     return 0;
1187   }
1188   
1189   // MachineConstantPool wants an explicit alignment.
1190   unsigned Align = TD.getPreferredTypeAlignmentShift(C->getType());
1191   if (Align == 0) {
1192     // Alignment of vector types.  FIXME!
1193     Align = TD.getABITypeSize(C->getType());
1194     Align = Log2_64(Align);
1195   }
1196   
1197   unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
1198   unsigned ResultReg = createResultReg(RC);
1199   addConstantPoolReference(BuildMI(MBB, TII.get(Opc), ResultReg), MCPOffset);
1200   return ResultReg;
1201 }
1202
1203 unsigned X86FastISel::TargetMaterializeAlloca(AllocaInst *C) {
1204   X86AddressMode AM;
1205   if (!X86SelectAddress(C, AM, false))
1206     return 0;
1207   unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
1208   TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
1209   unsigned ResultReg = createResultReg(RC);
1210   addFullAddress(BuildMI(MBB, TII.get(Opc), ResultReg), AM);
1211   return ResultReg;
1212 }
1213
1214 namespace llvm {
1215   llvm::FastISel *X86::createFastISel(MachineFunction &mf,
1216                         MachineModuleInfo *mmi,
1217                         DenseMap<const Value *, unsigned> &vm,
1218                         DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
1219                         DenseMap<const AllocaInst *, int> &am) {
1220     return new X86FastISel(mf, mmi, vm, bm, am);
1221   }
1222 }