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