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