Convert some uses of XXXRegisterClass to &XXXRegClass. No functional change since...
[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/GlobalVariable.h"
25 #include "llvm/GlobalAlias.h"
26 #include "llvm/Instructions.h"
27 #include "llvm/IntrinsicInst.h"
28 #include "llvm/Operator.h"
29 #include "llvm/CodeGen/Analysis.h"
30 #include "llvm/CodeGen/FastISel.h"
31 #include "llvm/CodeGen/FunctionLoweringInfo.h"
32 #include "llvm/CodeGen/MachineConstantPool.h"
33 #include "llvm/CodeGen/MachineFrameInfo.h"
34 #include "llvm/CodeGen/MachineRegisterInfo.h"
35 #include "llvm/Support/CallSite.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/GetElementPtrTypeIterator.h"
38 #include "llvm/Target/TargetOptions.h"
39 using namespace llvm;
40
41 namespace {
42
43 class X86FastISel : public FastISel {
44   /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
45   /// make the right decision when generating code for different targets.
46   const X86Subtarget *Subtarget;
47
48   /// StackPtr - Register used as the stack pointer.
49   ///
50   unsigned StackPtr;
51
52   /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
53   /// floating point ops.
54   /// When SSE is available, use it for f32 operations.
55   /// When SSE2 is available, use it for f64 operations.
56   bool X86ScalarSSEf64;
57   bool X86ScalarSSEf32;
58
59 public:
60   explicit X86FastISel(FunctionLoweringInfo &funcInfo) : FastISel(funcInfo) {
61     Subtarget = &TM.getSubtarget<X86Subtarget>();
62     StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
63     X86ScalarSSEf64 = Subtarget->hasSSE2();
64     X86ScalarSSEf32 = Subtarget->hasSSE1();
65   }
66
67   virtual bool TargetSelectInstruction(const Instruction *I);
68
69   /// TryToFoldLoad - The specified machine instr operand is a vreg, and that
70   /// vreg is being provided by the specified load instruction.  If possible,
71   /// try to fold the load as an operand to the instruction, returning true if
72   /// possible.
73   virtual bool TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
74                              const LoadInst *LI);
75
76 #include "X86GenFastISel.inc"
77
78 private:
79   bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
80
81   bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
82
83   bool X86FastEmitStore(EVT VT, const Value *Val, const X86AddressMode &AM);
84   bool X86FastEmitStore(EVT VT, unsigned Val, const X86AddressMode &AM);
85
86   bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
87                          unsigned &ResultReg);
88
89   bool X86SelectAddress(const Value *V, X86AddressMode &AM);
90   bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
91
92   bool X86SelectLoad(const Instruction *I);
93
94   bool X86SelectStore(const Instruction *I);
95
96   bool X86SelectRet(const Instruction *I);
97
98   bool X86SelectCmp(const Instruction *I);
99
100   bool X86SelectZExt(const Instruction *I);
101
102   bool X86SelectBranch(const Instruction *I);
103
104   bool X86SelectShift(const Instruction *I);
105
106   bool X86SelectSelect(const Instruction *I);
107
108   bool X86SelectTrunc(const Instruction *I);
109
110   bool X86SelectFPExt(const Instruction *I);
111   bool X86SelectFPTrunc(const Instruction *I);
112
113   bool X86VisitIntrinsicCall(const IntrinsicInst &I);
114   bool X86SelectCall(const Instruction *I);
115
116   bool DoSelectCall(const Instruction *I, const char *MemIntName);
117
118   const X86InstrInfo *getInstrInfo() const {
119     return getTargetMachine()->getInstrInfo();
120   }
121   const X86TargetMachine *getTargetMachine() const {
122     return static_cast<const X86TargetMachine *>(&TM);
123   }
124
125   unsigned TargetMaterializeConstant(const Constant *C);
126
127   unsigned TargetMaterializeAlloca(const AllocaInst *C);
128
129   unsigned TargetMaterializeFloatZero(const ConstantFP *CF);
130
131   /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
132   /// computed in an SSE register, not on the X87 floating point stack.
133   bool isScalarFPTypeInSSEReg(EVT VT) const {
134     return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
135       (VT == MVT::f32 && X86ScalarSSEf32);   // f32 is when SSE1
136   }
137
138   bool isTypeLegal(Type *Ty, MVT &VT, bool AllowI1 = false);
139
140   bool IsMemcpySmall(uint64_t Len);
141
142   bool TryEmitSmallMemcpy(X86AddressMode DestAM,
143                           X86AddressMode SrcAM, uint64_t Len);
144 };
145
146 } // end anonymous namespace.
147
148 bool X86FastISel::isTypeLegal(Type *Ty, MVT &VT, bool AllowI1) {
149   EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
150   if (evt == MVT::Other || !evt.isSimple())
151     // Unhandled type. Halt "fast" selection and bail.
152     return false;
153
154   VT = evt.getSimpleVT();
155   // For now, require SSE/SSE2 for performing floating-point operations,
156   // since x87 requires additional work.
157   if (VT == MVT::f64 && !X86ScalarSSEf64)
158      return false;
159   if (VT == MVT::f32 && !X86ScalarSSEf32)
160      return false;
161   // Similarly, no f80 support yet.
162   if (VT == MVT::f80)
163     return false;
164   // We only handle legal types. For example, on x86-32 the instruction
165   // selector contains all of the 64-bit instructions from x86-64,
166   // under the assumption that i64 won't be used if the target doesn't
167   // support it.
168   return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
169 }
170
171 #include "X86GenCallingConv.inc"
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(EVT 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().SimpleTy) {
182   default: return false;
183   case MVT::i1:
184   case MVT::i8:
185     Opc = X86::MOV8rm;
186     RC  = &X86::GR8RegClass;
187     break;
188   case MVT::i16:
189     Opc = X86::MOV16rm;
190     RC  = &X86::GR16RegClass;
191     break;
192   case MVT::i32:
193     Opc = X86::MOV32rm;
194     RC  = &X86::GR32RegClass;
195     break;
196   case MVT::i64:
197     // Must be in x86-64 mode.
198     Opc = X86::MOV64rm;
199     RC  = &X86::GR64RegClass;
200     break;
201   case MVT::f32:
202     if (X86ScalarSSEf32) {
203       Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
204       RC  = &X86::FR32RegClass;
205     } else {
206       Opc = X86::LD_Fp32m;
207       RC  = &X86::RFP32RegClass;
208     }
209     break;
210   case MVT::f64:
211     if (X86ScalarSSEf64) {
212       Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
213       RC  = &X86::FR64RegClass;
214     } else {
215       Opc = X86::LD_Fp64m;
216       RC  = &X86::RFP64RegClass;
217     }
218     break;
219   case MVT::f80:
220     // No f80 support yet.
221     return false;
222   }
223
224   ResultReg = createResultReg(RC);
225   addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
226                          DL, TII.get(Opc), ResultReg), AM);
227   return true;
228 }
229
230 /// X86FastEmitStore - Emit a machine instruction to store a value Val of
231 /// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
232 /// and a displacement offset, or a GlobalAddress,
233 /// i.e. V. Return true if it is possible.
234 bool
235 X86FastISel::X86FastEmitStore(EVT VT, unsigned Val, const X86AddressMode &AM) {
236   // Get opcode and regclass of the output for the given store instruction.
237   unsigned Opc = 0;
238   switch (VT.getSimpleVT().SimpleTy) {
239   case MVT::f80: // No f80 support yet.
240   default: return false;
241   case MVT::i1: {
242     // Mask out all but lowest bit.
243     unsigned AndResult = createResultReg(&X86::GR8RegClass);
244     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
245             TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
246     Val = AndResult;
247   }
248   // FALLTHROUGH, handling i1 as i8.
249   case MVT::i8:  Opc = X86::MOV8mr;  break;
250   case MVT::i16: Opc = X86::MOV16mr; break;
251   case MVT::i32: Opc = X86::MOV32mr; break;
252   case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
253   case MVT::f32:
254     Opc = X86ScalarSSEf32 ?
255           (Subtarget->hasAVX() ? X86::VMOVSSmr : X86::MOVSSmr) : X86::ST_Fp32m;
256     break;
257   case MVT::f64:
258     Opc = X86ScalarSSEf64 ?
259           (Subtarget->hasAVX() ? X86::VMOVSDmr : X86::MOVSDmr) : X86::ST_Fp64m;
260     break;
261   case MVT::v4f32:
262     Opc = X86::MOVAPSmr;
263     break;
264   case MVT::v2f64:
265     Opc = X86::MOVAPDmr;
266     break;
267   case MVT::v4i32:
268   case MVT::v2i64:
269   case MVT::v8i16:
270   case MVT::v16i8:
271     Opc = X86::MOVDQAmr;
272     break;
273   }
274
275   addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
276                          DL, TII.get(Opc)), AM).addReg(Val);
277   return true;
278 }
279
280 bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
281                                    const X86AddressMode &AM) {
282   // Handle 'null' like i32/i64 0.
283   if (isa<ConstantPointerNull>(Val))
284     Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
285
286   // If this is a store of a simple constant, fold the constant into the store.
287   if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
288     unsigned Opc = 0;
289     bool Signed = true;
290     switch (VT.getSimpleVT().SimpleTy) {
291     default: break;
292     case MVT::i1:  Signed = false;     // FALLTHROUGH to handle as i8.
293     case MVT::i8:  Opc = X86::MOV8mi;  break;
294     case MVT::i16: Opc = X86::MOV16mi; break;
295     case MVT::i32: Opc = X86::MOV32mi; break;
296     case MVT::i64:
297       // Must be a 32-bit sign extended value.
298       if ((int)CI->getSExtValue() == CI->getSExtValue())
299         Opc = X86::MOV64mi32;
300       break;
301     }
302
303     if (Opc) {
304       addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
305                              DL, TII.get(Opc)), AM)
306                              .addImm(Signed ? (uint64_t) CI->getSExtValue() :
307                                               CI->getZExtValue());
308       return true;
309     }
310   }
311
312   unsigned ValReg = getRegForValue(Val);
313   if (ValReg == 0)
314     return false;
315
316   return X86FastEmitStore(VT, ValReg, AM);
317 }
318
319 /// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
320 /// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
321 /// ISD::SIGN_EXTEND).
322 bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
323                                     unsigned Src, EVT SrcVT,
324                                     unsigned &ResultReg) {
325   unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
326                            Src, /*TODO: Kill=*/false);
327
328   if (RR != 0) {
329     ResultReg = RR;
330     return true;
331   } else
332     return false;
333 }
334
335 /// X86SelectAddress - Attempt to fill in an address from the given value.
336 ///
337 bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
338   const User *U = NULL;
339   unsigned Opcode = Instruction::UserOp1;
340   if (const Instruction *I = dyn_cast<Instruction>(V)) {
341     // Don't walk into other basic blocks; it's possible we haven't
342     // visited them yet, so the instructions may not yet be assigned
343     // virtual registers.
344     if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
345         FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
346       Opcode = I->getOpcode();
347       U = I;
348     }
349   } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
350     Opcode = C->getOpcode();
351     U = C;
352   }
353
354   if (PointerType *Ty = dyn_cast<PointerType>(V->getType()))
355     if (Ty->getAddressSpace() > 255)
356       // Fast instruction selection doesn't support the special
357       // address spaces.
358       return false;
359
360   switch (Opcode) {
361   default: break;
362   case Instruction::BitCast:
363     // Look past bitcasts.
364     return X86SelectAddress(U->getOperand(0), AM);
365
366   case Instruction::IntToPtr:
367     // Look past no-op inttoptrs.
368     if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
369       return X86SelectAddress(U->getOperand(0), AM);
370     break;
371
372   case Instruction::PtrToInt:
373     // Look past no-op ptrtoints.
374     if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
375       return X86SelectAddress(U->getOperand(0), AM);
376     break;
377
378   case Instruction::Alloca: {
379     // Do static allocas.
380     const AllocaInst *A = cast<AllocaInst>(V);
381     DenseMap<const AllocaInst*, int>::iterator SI =
382       FuncInfo.StaticAllocaMap.find(A);
383     if (SI != FuncInfo.StaticAllocaMap.end()) {
384       AM.BaseType = X86AddressMode::FrameIndexBase;
385       AM.Base.FrameIndex = SI->second;
386       return true;
387     }
388     break;
389   }
390
391   case Instruction::Add: {
392     // Adds of constants are common and easy enough.
393     if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
394       uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
395       // They have to fit in the 32-bit signed displacement field though.
396       if (isInt<32>(Disp)) {
397         AM.Disp = (uint32_t)Disp;
398         return X86SelectAddress(U->getOperand(0), AM);
399       }
400     }
401     break;
402   }
403
404   case Instruction::GetElementPtr: {
405     X86AddressMode SavedAM = AM;
406
407     // Pattern-match simple GEPs.
408     uint64_t Disp = (int32_t)AM.Disp;
409     unsigned IndexReg = AM.IndexReg;
410     unsigned Scale = AM.Scale;
411     gep_type_iterator GTI = gep_type_begin(U);
412     // Iterate through the indices, folding what we can. Constants can be
413     // folded, and one dynamic index can be handled, if the scale is supported.
414     for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
415          i != e; ++i, ++GTI) {
416       const Value *Op = *i;
417       if (StructType *STy = dyn_cast<StructType>(*GTI)) {
418         const StructLayout *SL = TD.getStructLayout(STy);
419         Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
420         continue;
421       }
422
423       // A array/variable index is always of the form i*S where S is the
424       // constant scale size.  See if we can push the scale into immediates.
425       uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
426       for (;;) {
427         if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
428           // Constant-offset addressing.
429           Disp += CI->getSExtValue() * S;
430           break;
431         }
432         if (isa<AddOperator>(Op) &&
433             (!isa<Instruction>(Op) ||
434              FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
435                == FuncInfo.MBB) &&
436             isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
437           // An add (in the same block) with a constant operand. Fold the
438           // constant.
439           ConstantInt *CI =
440             cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
441           Disp += CI->getSExtValue() * S;
442           // Iterate on the other operand.
443           Op = cast<AddOperator>(Op)->getOperand(0);
444           continue;
445         }
446         if (IndexReg == 0 &&
447             (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
448             (S == 1 || S == 2 || S == 4 || S == 8)) {
449           // Scaled-index addressing.
450           Scale = S;
451           IndexReg = getRegForGEPIndex(Op).first;
452           if (IndexReg == 0)
453             return false;
454           break;
455         }
456         // Unsupported.
457         goto unsupported_gep;
458       }
459     }
460     // Check for displacement overflow.
461     if (!isInt<32>(Disp))
462       break;
463     // Ok, the GEP indices were covered by constant-offset and scaled-index
464     // addressing. Update the address state and move on to examining the base.
465     AM.IndexReg = IndexReg;
466     AM.Scale = Scale;
467     AM.Disp = (uint32_t)Disp;
468     if (X86SelectAddress(U->getOperand(0), AM))
469       return true;
470
471     // If we couldn't merge the gep value into this addr mode, revert back to
472     // our address and just match the value instead of completely failing.
473     AM = SavedAM;
474     break;
475   unsupported_gep:
476     // Ok, the GEP indices weren't all covered.
477     break;
478   }
479   }
480
481   // Handle constant address.
482   if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
483     // Can't handle alternate code models yet.
484     if (TM.getCodeModel() != CodeModel::Small)
485       return false;
486
487     // Can't handle TLS yet.
488     if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
489       if (GVar->isThreadLocal())
490         return false;
491
492     // Can't handle TLS yet, part 2 (this is slightly crazy, but this is how
493     // it works...).
494     if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
495       if (const GlobalVariable *GVar =
496             dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false)))
497         if (GVar->isThreadLocal())
498           return false;
499
500     // RIP-relative addresses can't have additional register operands, so if
501     // we've already folded stuff into the addressing mode, just force the
502     // global value into its own register, which we can use as the basereg.
503     if (!Subtarget->isPICStyleRIPRel() ||
504         (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
505       // Okay, we've committed to selecting this global. Set up the address.
506       AM.GV = GV;
507
508       // Allow the subtarget to classify the global.
509       unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
510
511       // If this reference is relative to the pic base, set it now.
512       if (isGlobalRelativeToPICBase(GVFlags)) {
513         // FIXME: How do we know Base.Reg is free??
514         AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
515       }
516
517       // Unless the ABI requires an extra load, return a direct reference to
518       // the global.
519       if (!isGlobalStubReference(GVFlags)) {
520         if (Subtarget->isPICStyleRIPRel()) {
521           // Use rip-relative addressing if we can.  Above we verified that the
522           // base and index registers are unused.
523           assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
524           AM.Base.Reg = X86::RIP;
525         }
526         AM.GVOpFlags = GVFlags;
527         return true;
528       }
529
530       // Ok, we need to do a load from a stub.  If we've already loaded from
531       // this stub, reuse the loaded pointer, otherwise emit the load now.
532       DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
533       unsigned LoadReg;
534       if (I != LocalValueMap.end() && I->second != 0) {
535         LoadReg = I->second;
536       } else {
537         // Issue load from stub.
538         unsigned Opc = 0;
539         const TargetRegisterClass *RC = NULL;
540         X86AddressMode StubAM;
541         StubAM.Base.Reg = AM.Base.Reg;
542         StubAM.GV = GV;
543         StubAM.GVOpFlags = GVFlags;
544
545         // Prepare for inserting code in the local-value area.
546         SavePoint SaveInsertPt = enterLocalValueArea();
547
548         if (TLI.getPointerTy() == MVT::i64) {
549           Opc = X86::MOV64rm;
550           RC  = &X86::GR64RegClass;
551
552           if (Subtarget->isPICStyleRIPRel())
553             StubAM.Base.Reg = X86::RIP;
554         } else {
555           Opc = X86::MOV32rm;
556           RC  = &X86::GR32RegClass;
557         }
558
559         LoadReg = createResultReg(RC);
560         MachineInstrBuilder LoadMI =
561           BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), LoadReg);
562         addFullAddress(LoadMI, StubAM);
563
564         // Ok, back to normal mode.
565         leaveLocalValueArea(SaveInsertPt);
566
567         // Prevent loading GV stub multiple times in same MBB.
568         LocalValueMap[V] = LoadReg;
569       }
570
571       // Now construct the final address. Note that the Disp, Scale,
572       // and Index values may already be set here.
573       AM.Base.Reg = LoadReg;
574       AM.GV = 0;
575       return true;
576     }
577   }
578
579   // If all else fails, try to materialize the value in a register.
580   if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
581     if (AM.Base.Reg == 0) {
582       AM.Base.Reg = getRegForValue(V);
583       return AM.Base.Reg != 0;
584     }
585     if (AM.IndexReg == 0) {
586       assert(AM.Scale == 1 && "Scale with no index!");
587       AM.IndexReg = getRegForValue(V);
588       return AM.IndexReg != 0;
589     }
590   }
591
592   return false;
593 }
594
595 /// X86SelectCallAddress - Attempt to fill in an address from the given value.
596 ///
597 bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
598   const User *U = NULL;
599   unsigned Opcode = Instruction::UserOp1;
600   if (const Instruction *I = dyn_cast<Instruction>(V)) {
601     Opcode = I->getOpcode();
602     U = I;
603   } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
604     Opcode = C->getOpcode();
605     U = C;
606   }
607
608   switch (Opcode) {
609   default: break;
610   case Instruction::BitCast:
611     // Look past bitcasts.
612     return X86SelectCallAddress(U->getOperand(0), AM);
613
614   case Instruction::IntToPtr:
615     // Look past no-op inttoptrs.
616     if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
617       return X86SelectCallAddress(U->getOperand(0), AM);
618     break;
619
620   case Instruction::PtrToInt:
621     // Look past no-op ptrtoints.
622     if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
623       return X86SelectCallAddress(U->getOperand(0), AM);
624     break;
625   }
626
627   // Handle constant address.
628   if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
629     // Can't handle alternate code models yet.
630     if (TM.getCodeModel() != CodeModel::Small)
631       return false;
632
633     // RIP-relative addresses can't have additional register operands.
634     if (Subtarget->isPICStyleRIPRel() &&
635         (AM.Base.Reg != 0 || AM.IndexReg != 0))
636       return false;
637
638     // Can't handle DLLImport.
639     if (GV->hasDLLImportLinkage())
640       return false;
641
642     // Can't handle TLS.
643     if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
644       if (GVar->isThreadLocal())
645         return false;
646
647     // Okay, we've committed to selecting this global. Set up the basic address.
648     AM.GV = GV;
649
650     // No ABI requires an extra load for anything other than DLLImport, which
651     // we rejected above. Return a direct reference to the global.
652     if (Subtarget->isPICStyleRIPRel()) {
653       // Use rip-relative addressing if we can.  Above we verified that the
654       // base and index registers are unused.
655       assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
656       AM.Base.Reg = X86::RIP;
657     } else if (Subtarget->isPICStyleStubPIC()) {
658       AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
659     } else if (Subtarget->isPICStyleGOT()) {
660       AM.GVOpFlags = X86II::MO_GOTOFF;
661     }
662
663     return true;
664   }
665
666   // If all else fails, try to materialize the value in a register.
667   if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
668     if (AM.Base.Reg == 0) {
669       AM.Base.Reg = getRegForValue(V);
670       return AM.Base.Reg != 0;
671     }
672     if (AM.IndexReg == 0) {
673       assert(AM.Scale == 1 && "Scale with no index!");
674       AM.IndexReg = getRegForValue(V);
675       return AM.IndexReg != 0;
676     }
677   }
678
679   return false;
680 }
681
682
683 /// X86SelectStore - Select and emit code to implement store instructions.
684 bool X86FastISel::X86SelectStore(const Instruction *I) {
685   // Atomic stores need special handling.
686   const StoreInst *S = cast<StoreInst>(I);
687
688   if (S->isAtomic())
689     return false;
690
691   unsigned SABIAlignment =
692     TD.getABITypeAlignment(S->getValueOperand()->getType());
693   if (S->getAlignment() != 0 && S->getAlignment() < SABIAlignment)
694     return false;
695
696   MVT VT;
697   if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
698     return false;
699
700   X86AddressMode AM;
701   if (!X86SelectAddress(I->getOperand(1), AM))
702     return false;
703
704   return X86FastEmitStore(VT, I->getOperand(0), AM);
705 }
706
707 /// X86SelectRet - Select and emit code to implement ret instructions.
708 bool X86FastISel::X86SelectRet(const Instruction *I) {
709   const ReturnInst *Ret = cast<ReturnInst>(I);
710   const Function &F = *I->getParent()->getParent();
711
712   if (!FuncInfo.CanLowerReturn)
713     return false;
714
715   CallingConv::ID CC = F.getCallingConv();
716   if (CC != CallingConv::C &&
717       CC != CallingConv::Fast &&
718       CC != CallingConv::X86_FastCall)
719     return false;
720
721   if (Subtarget->isTargetWin64())
722     return false;
723
724   // Don't handle popping bytes on return for now.
725   if (FuncInfo.MF->getInfo<X86MachineFunctionInfo>()
726         ->getBytesToPopOnReturn() != 0)
727     return 0;
728
729   // fastcc with -tailcallopt is intended to provide a guaranteed
730   // tail call optimization. Fastisel doesn't know how to do that.
731   if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
732     return false;
733
734   // Let SDISel handle vararg functions.
735   if (F.isVarArg())
736     return false;
737
738   if (Ret->getNumOperands() > 0) {
739     SmallVector<ISD::OutputArg, 4> Outs;
740     GetReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
741                   Outs, TLI);
742
743     // Analyze operands of the call, assigning locations to each operand.
744     SmallVector<CCValAssign, 16> ValLocs;
745     CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
746                    I->getContext());
747     CCInfo.AnalyzeReturn(Outs, RetCC_X86);
748
749     const Value *RV = Ret->getOperand(0);
750     unsigned Reg = getRegForValue(RV);
751     if (Reg == 0)
752       return false;
753
754     // Only handle a single return value for now.
755     if (ValLocs.size() != 1)
756       return false;
757
758     CCValAssign &VA = ValLocs[0];
759
760     // Don't bother handling odd stuff for now.
761     if (VA.getLocInfo() != CCValAssign::Full)
762       return false;
763     // Only handle register returns for now.
764     if (!VA.isRegLoc())
765       return false;
766
767     // The calling-convention tables for x87 returns don't tell
768     // the whole story.
769     if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
770       return false;
771
772     unsigned SrcReg = Reg + VA.getValNo();
773     EVT SrcVT = TLI.getValueType(RV->getType());
774     EVT DstVT = VA.getValVT();
775     // Special handling for extended integers.
776     if (SrcVT != DstVT) {
777       if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
778         return false;
779
780       if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
781         return false;
782
783       assert(DstVT == MVT::i32 && "X86 should always ext to i32");
784
785       if (SrcVT == MVT::i1) {
786         if (Outs[0].Flags.isSExt())
787           return false;
788         SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
789         SrcVT = MVT::i8;
790       }
791       unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
792                                              ISD::SIGN_EXTEND;
793       SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
794                           SrcReg, /*TODO: Kill=*/false);
795     }
796
797     // Make the copy.
798     unsigned DstReg = VA.getLocReg();
799     const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
800     // Avoid a cross-class copy. This is very unlikely.
801     if (!SrcRC->contains(DstReg))
802       return false;
803     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
804             DstReg).addReg(SrcReg);
805
806     // Mark the register as live out of the function.
807     MRI.addLiveOut(VA.getLocReg());
808   }
809
810   // Now emit the RET.
811   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
812   return true;
813 }
814
815 /// X86SelectLoad - Select and emit code to implement load instructions.
816 ///
817 bool X86FastISel::X86SelectLoad(const Instruction *I)  {
818   // Atomic loads need special handling.
819   if (cast<LoadInst>(I)->isAtomic())
820     return false;
821
822   MVT VT;
823   if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
824     return false;
825
826   X86AddressMode AM;
827   if (!X86SelectAddress(I->getOperand(0), AM))
828     return false;
829
830   unsigned ResultReg = 0;
831   if (X86FastEmitLoad(VT, AM, ResultReg)) {
832     UpdateValueMap(I, ResultReg);
833     return true;
834   }
835   return false;
836 }
837
838 static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
839   bool HasAVX = Subtarget->hasAVX();
840   bool X86ScalarSSEf32 = Subtarget->hasSSE1();
841   bool X86ScalarSSEf64 = Subtarget->hasSSE2();
842
843   switch (VT.getSimpleVT().SimpleTy) {
844   default:       return 0;
845   case MVT::i8:  return X86::CMP8rr;
846   case MVT::i16: return X86::CMP16rr;
847   case MVT::i32: return X86::CMP32rr;
848   case MVT::i64: return X86::CMP64rr;
849   case MVT::f32:
850     return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0;
851   case MVT::f64:
852     return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0;
853   }
854 }
855
856 /// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
857 /// of the comparison, return an opcode that works for the compare (e.g.
858 /// CMP32ri) otherwise return 0.
859 static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
860   switch (VT.getSimpleVT().SimpleTy) {
861   // Otherwise, we can't fold the immediate into this comparison.
862   default: return 0;
863   case MVT::i8: return X86::CMP8ri;
864   case MVT::i16: return X86::CMP16ri;
865   case MVT::i32: return X86::CMP32ri;
866   case MVT::i64:
867     // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
868     // field.
869     if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
870       return X86::CMP64ri32;
871     return 0;
872   }
873 }
874
875 bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
876                                      EVT VT) {
877   unsigned Op0Reg = getRegForValue(Op0);
878   if (Op0Reg == 0) return false;
879
880   // Handle 'null' like i32/i64 0.
881   if (isa<ConstantPointerNull>(Op1))
882     Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
883
884   // We have two options: compare with register or immediate.  If the RHS of
885   // the compare is an immediate that we can fold into this compare, use
886   // CMPri, otherwise use CMPrr.
887   if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
888     if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
889       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
890         .addReg(Op0Reg)
891         .addImm(Op1C->getSExtValue());
892       return true;
893     }
894   }
895
896   unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
897   if (CompareOpc == 0) return false;
898
899   unsigned Op1Reg = getRegForValue(Op1);
900   if (Op1Reg == 0) return false;
901   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
902     .addReg(Op0Reg)
903     .addReg(Op1Reg);
904
905   return true;
906 }
907
908 bool X86FastISel::X86SelectCmp(const Instruction *I) {
909   const CmpInst *CI = cast<CmpInst>(I);
910
911   MVT VT;
912   if (!isTypeLegal(I->getOperand(0)->getType(), VT))
913     return false;
914
915   unsigned ResultReg = createResultReg(&X86::GR8RegClass);
916   unsigned SetCCOpc;
917   bool SwapArgs;  // false -> compare Op0, Op1.  true -> compare Op1, Op0.
918   switch (CI->getPredicate()) {
919   case CmpInst::FCMP_OEQ: {
920     if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
921       return false;
922
923     unsigned EReg = createResultReg(&X86::GR8RegClass);
924     unsigned NPReg = createResultReg(&X86::GR8RegClass);
925     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
926     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
927             TII.get(X86::SETNPr), NPReg);
928     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
929             TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
930     UpdateValueMap(I, ResultReg);
931     return true;
932   }
933   case CmpInst::FCMP_UNE: {
934     if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
935       return false;
936
937     unsigned NEReg = createResultReg(&X86::GR8RegClass);
938     unsigned PReg = createResultReg(&X86::GR8RegClass);
939     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETNEr), NEReg);
940     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETPr), PReg);
941     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::OR8rr),ResultReg)
942       .addReg(PReg).addReg(NEReg);
943     UpdateValueMap(I, ResultReg);
944     return true;
945   }
946   case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr;  break;
947   case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
948   case CmpInst::FCMP_OLT: SwapArgs = true;  SetCCOpc = X86::SETAr;  break;
949   case CmpInst::FCMP_OLE: SwapArgs = true;  SetCCOpc = X86::SETAEr; break;
950   case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
951   case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
952   case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr;  break;
953   case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr;  break;
954   case CmpInst::FCMP_UGT: SwapArgs = true;  SetCCOpc = X86::SETBr;  break;
955   case CmpInst::FCMP_UGE: SwapArgs = true;  SetCCOpc = X86::SETBEr; break;
956   case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr;  break;
957   case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
958
959   case CmpInst::ICMP_EQ:  SwapArgs = false; SetCCOpc = X86::SETEr;  break;
960   case CmpInst::ICMP_NE:  SwapArgs = false; SetCCOpc = X86::SETNEr; break;
961   case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr;  break;
962   case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
963   case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr;  break;
964   case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
965   case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr;  break;
966   case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
967   case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr;  break;
968   case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
969   default:
970     return false;
971   }
972
973   const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
974   if (SwapArgs)
975     std::swap(Op0, Op1);
976
977   // Emit a compare of Op0/Op1.
978   if (!X86FastEmitCompare(Op0, Op1, VT))
979     return false;
980
981   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
982   UpdateValueMap(I, ResultReg);
983   return true;
984 }
985
986 bool X86FastISel::X86SelectZExt(const Instruction *I) {
987   // Handle zero-extension from i1 to i8, which is common.
988   if (!I->getOperand(0)->getType()->isIntegerTy(1))
989     return false;
990
991   EVT DstVT = TLI.getValueType(I->getType());
992   if (!TLI.isTypeLegal(DstVT))
993     return false;
994
995   unsigned ResultReg = getRegForValue(I->getOperand(0));
996   if (ResultReg == 0)
997     return false;
998
999   // Set the high bits to zero.
1000   ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
1001   if (ResultReg == 0)
1002     return false;
1003
1004   if (DstVT != MVT::i8) {
1005     ResultReg = FastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
1006                            ResultReg, /*Kill=*/true);
1007     if (ResultReg == 0)
1008       return false;
1009   }
1010
1011   UpdateValueMap(I, ResultReg);
1012   return true;
1013 }
1014
1015
1016 bool X86FastISel::X86SelectBranch(const Instruction *I) {
1017   // Unconditional branches are selected by tablegen-generated code.
1018   // Handle a conditional branch.
1019   const BranchInst *BI = cast<BranchInst>(I);
1020   MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1021   MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
1022
1023   // Fold the common case of a conditional branch with a comparison
1024   // in the same block (values defined on other blocks may not have
1025   // initialized registers).
1026   if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
1027     if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
1028       EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
1029
1030       // Try to take advantage of fallthrough opportunities.
1031       CmpInst::Predicate Predicate = CI->getPredicate();
1032       if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1033         std::swap(TrueMBB, FalseMBB);
1034         Predicate = CmpInst::getInversePredicate(Predicate);
1035       }
1036
1037       bool SwapArgs;  // false -> compare Op0, Op1.  true -> compare Op1, Op0.
1038       unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
1039
1040       switch (Predicate) {
1041       case CmpInst::FCMP_OEQ:
1042         std::swap(TrueMBB, FalseMBB);
1043         Predicate = CmpInst::FCMP_UNE;
1044         // FALL THROUGH
1045       case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1046       case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4;  break;
1047       case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1048       case CmpInst::FCMP_OLT: SwapArgs = true;  BranchOpc = X86::JA_4;  break;
1049       case CmpInst::FCMP_OLE: SwapArgs = true;  BranchOpc = X86::JAE_4; break;
1050       case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1051       case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
1052       case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4;  break;
1053       case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4;  break;
1054       case CmpInst::FCMP_UGT: SwapArgs = true;  BranchOpc = X86::JB_4;  break;
1055       case CmpInst::FCMP_UGE: SwapArgs = true;  BranchOpc = X86::JBE_4; break;
1056       case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4;  break;
1057       case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
1058
1059       case CmpInst::ICMP_EQ:  SwapArgs = false; BranchOpc = X86::JE_4;  break;
1060       case CmpInst::ICMP_NE:  SwapArgs = false; BranchOpc = X86::JNE_4; break;
1061       case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4;  break;
1062       case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1063       case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4;  break;
1064       case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
1065       case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4;  break;
1066       case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
1067       case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4;  break;
1068       case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
1069       default:
1070         return false;
1071       }
1072
1073       const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
1074       if (SwapArgs)
1075         std::swap(Op0, Op1);
1076
1077       // Emit a compare of the LHS and RHS, setting the flags.
1078       if (!X86FastEmitCompare(Op0, Op1, VT))
1079         return false;
1080
1081       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
1082         .addMBB(TrueMBB);
1083
1084       if (Predicate == CmpInst::FCMP_UNE) {
1085         // X86 requires a second branch to handle UNE (and OEQ,
1086         // which is mapped to UNE above).
1087         BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1088           .addMBB(TrueMBB);
1089       }
1090
1091       FastEmitBranch(FalseMBB, DL);
1092       FuncInfo.MBB->addSuccessor(TrueMBB);
1093       return true;
1094     }
1095   } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1096     // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1097     // typically happen for _Bool and C++ bools.
1098     MVT SourceVT;
1099     if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1100         isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1101       unsigned TestOpc = 0;
1102       switch (SourceVT.SimpleTy) {
1103       default: break;
1104       case MVT::i8:  TestOpc = X86::TEST8ri; break;
1105       case MVT::i16: TestOpc = X86::TEST16ri; break;
1106       case MVT::i32: TestOpc = X86::TEST32ri; break;
1107       case MVT::i64: TestOpc = X86::TEST64ri32; break;
1108       }
1109       if (TestOpc) {
1110         unsigned OpReg = getRegForValue(TI->getOperand(0));
1111         if (OpReg == 0) return false;
1112         BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TestOpc))
1113           .addReg(OpReg).addImm(1);
1114
1115         unsigned JmpOpc = X86::JNE_4;
1116         if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1117           std::swap(TrueMBB, FalseMBB);
1118           JmpOpc = X86::JE_4;
1119         }
1120
1121         BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(JmpOpc))
1122           .addMBB(TrueMBB);
1123         FastEmitBranch(FalseMBB, DL);
1124         FuncInfo.MBB->addSuccessor(TrueMBB);
1125         return true;
1126       }
1127     }
1128   }
1129
1130   // Otherwise do a clumsy setcc and re-test it.
1131   // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1132   // in an explicit cast, so make sure to handle that correctly.
1133   unsigned OpReg = getRegForValue(BI->getCondition());
1134   if (OpReg == 0) return false;
1135
1136   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8ri))
1137     .addReg(OpReg).addImm(1);
1138   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1139     .addMBB(TrueMBB);
1140   FastEmitBranch(FalseMBB, DL);
1141   FuncInfo.MBB->addSuccessor(TrueMBB);
1142   return true;
1143 }
1144
1145 bool X86FastISel::X86SelectShift(const Instruction *I) {
1146   unsigned CReg = 0, OpReg = 0;
1147   const TargetRegisterClass *RC = NULL;
1148   if (I->getType()->isIntegerTy(8)) {
1149     CReg = X86::CL;
1150     RC = &X86::GR8RegClass;
1151     switch (I->getOpcode()) {
1152     case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1153     case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1154     case Instruction::Shl:  OpReg = X86::SHL8rCL; break;
1155     default: return false;
1156     }
1157   } else if (I->getType()->isIntegerTy(16)) {
1158     CReg = X86::CX;
1159     RC = &X86::GR16RegClass;
1160     switch (I->getOpcode()) {
1161     case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1162     case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1163     case Instruction::Shl:  OpReg = X86::SHL16rCL; break;
1164     default: return false;
1165     }
1166   } else if (I->getType()->isIntegerTy(32)) {
1167     CReg = X86::ECX;
1168     RC = &X86::GR32RegClass;
1169     switch (I->getOpcode()) {
1170     case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1171     case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1172     case Instruction::Shl:  OpReg = X86::SHL32rCL; break;
1173     default: return false;
1174     }
1175   } else if (I->getType()->isIntegerTy(64)) {
1176     CReg = X86::RCX;
1177     RC = &X86::GR64RegClass;
1178     switch (I->getOpcode()) {
1179     case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1180     case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1181     case Instruction::Shl:  OpReg = X86::SHL64rCL; break;
1182     default: return false;
1183     }
1184   } else {
1185     return false;
1186   }
1187
1188   MVT VT;
1189   if (!isTypeLegal(I->getType(), VT))
1190     return false;
1191
1192   unsigned Op0Reg = getRegForValue(I->getOperand(0));
1193   if (Op0Reg == 0) return false;
1194
1195   unsigned Op1Reg = getRegForValue(I->getOperand(1));
1196   if (Op1Reg == 0) return false;
1197   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1198           CReg).addReg(Op1Reg);
1199
1200   // The shift instruction uses X86::CL. If we defined a super-register
1201   // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
1202   if (CReg != X86::CL)
1203     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1204             TII.get(TargetOpcode::KILL), X86::CL)
1205       .addReg(CReg, RegState::Kill);
1206
1207   unsigned ResultReg = createResultReg(RC);
1208   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1209     .addReg(Op0Reg);
1210   UpdateValueMap(I, ResultReg);
1211   return true;
1212 }
1213
1214 bool X86FastISel::X86SelectSelect(const Instruction *I) {
1215   MVT VT;
1216   if (!isTypeLegal(I->getType(), VT))
1217     return false;
1218
1219   // We only use cmov here, if we don't have a cmov instruction bail.
1220   if (!Subtarget->hasCMov()) return false;
1221
1222   unsigned Opc = 0;
1223   const TargetRegisterClass *RC = NULL;
1224   if (VT == MVT::i16) {
1225     Opc = X86::CMOVE16rr;
1226     RC = &X86::GR16RegClass;
1227   } else if (VT == MVT::i32) {
1228     Opc = X86::CMOVE32rr;
1229     RC = &X86::GR32RegClass;
1230   } else if (VT == MVT::i64) {
1231     Opc = X86::CMOVE64rr;
1232     RC = &X86::GR64RegClass;
1233   } else {
1234     return false;
1235   }
1236
1237   unsigned Op0Reg = getRegForValue(I->getOperand(0));
1238   if (Op0Reg == 0) return false;
1239   unsigned Op1Reg = getRegForValue(I->getOperand(1));
1240   if (Op1Reg == 0) return false;
1241   unsigned Op2Reg = getRegForValue(I->getOperand(2));
1242   if (Op2Reg == 0) return false;
1243
1244   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1245     .addReg(Op0Reg).addReg(Op0Reg);
1246   unsigned ResultReg = createResultReg(RC);
1247   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1248     .addReg(Op1Reg).addReg(Op2Reg);
1249   UpdateValueMap(I, ResultReg);
1250   return true;
1251 }
1252
1253 bool X86FastISel::X86SelectFPExt(const Instruction *I) {
1254   // fpext from float to double.
1255   if (X86ScalarSSEf64 &&
1256       I->getType()->isDoubleTy()) {
1257     const Value *V = I->getOperand(0);
1258     if (V->getType()->isFloatTy()) {
1259       unsigned OpReg = getRegForValue(V);
1260       if (OpReg == 0) return false;
1261       unsigned ResultReg = createResultReg(&X86::FR64RegClass);
1262       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1263               TII.get(X86::CVTSS2SDrr), ResultReg)
1264         .addReg(OpReg);
1265       UpdateValueMap(I, ResultReg);
1266       return true;
1267     }
1268   }
1269
1270   return false;
1271 }
1272
1273 bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
1274   if (X86ScalarSSEf64) {
1275     if (I->getType()->isFloatTy()) {
1276       const Value *V = I->getOperand(0);
1277       if (V->getType()->isDoubleTy()) {
1278         unsigned OpReg = getRegForValue(V);
1279         if (OpReg == 0) return false;
1280         unsigned ResultReg = createResultReg(&X86::FR32RegClass);
1281         BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1282                 TII.get(X86::CVTSD2SSrr), ResultReg)
1283           .addReg(OpReg);
1284         UpdateValueMap(I, ResultReg);
1285         return true;
1286       }
1287     }
1288   }
1289
1290   return false;
1291 }
1292
1293 bool X86FastISel::X86SelectTrunc(const Instruction *I) {
1294   EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1295   EVT DstVT = TLI.getValueType(I->getType());
1296
1297   // This code only handles truncation to byte.
1298   if (DstVT != MVT::i8 && DstVT != MVT::i1)
1299     return false;
1300   if (!TLI.isTypeLegal(SrcVT))
1301     return false;
1302
1303   unsigned InputReg = getRegForValue(I->getOperand(0));
1304   if (!InputReg)
1305     // Unhandled operand.  Halt "fast" selection and bail.
1306     return false;
1307
1308   if (SrcVT == MVT::i8) {
1309     // Truncate from i8 to i1; no code needed.
1310     UpdateValueMap(I, InputReg);
1311     return true;
1312   }
1313
1314   if (!Subtarget->is64Bit()) {
1315     // If we're on x86-32; we can't extract an i8 from a general register.
1316     // First issue a copy to GR16_ABCD or GR32_ABCD.
1317     const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16) ?
1318       (const TargetRegisterClass*)&X86::GR16_ABCDRegClass :
1319       (const TargetRegisterClass*)&X86::GR32_ABCDRegClass;
1320     unsigned CopyReg = createResultReg(CopyRC);
1321     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1322             CopyReg).addReg(InputReg);
1323     InputReg = CopyReg;
1324   }
1325
1326   // Issue an extract_subreg.
1327   unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
1328                                                   InputReg, /*Kill=*/true,
1329                                                   X86::sub_8bit);
1330   if (!ResultReg)
1331     return false;
1332
1333   UpdateValueMap(I, ResultReg);
1334   return true;
1335 }
1336
1337 bool X86FastISel::IsMemcpySmall(uint64_t Len) {
1338   return Len <= (Subtarget->is64Bit() ? 32 : 16);
1339 }
1340
1341 bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1342                                      X86AddressMode SrcAM, uint64_t Len) {
1343
1344   // Make sure we don't bloat code by inlining very large memcpy's.
1345   if (!IsMemcpySmall(Len))
1346     return false;
1347
1348   bool i64Legal = Subtarget->is64Bit();
1349
1350   // We don't care about alignment here since we just emit integer accesses.
1351   while (Len) {
1352     MVT VT;
1353     if (Len >= 8 && i64Legal)
1354       VT = MVT::i64;
1355     else if (Len >= 4)
1356       VT = MVT::i32;
1357     else if (Len >= 2)
1358       VT = MVT::i16;
1359     else {
1360       assert(Len == 1);
1361       VT = MVT::i8;
1362     }
1363
1364     unsigned Reg;
1365     bool RV = X86FastEmitLoad(VT, SrcAM, Reg);
1366     RV &= X86FastEmitStore(VT, Reg, DestAM);
1367     assert(RV && "Failed to emit load or store??");
1368
1369     unsigned Size = VT.getSizeInBits()/8;
1370     Len -= Size;
1371     DestAM.Disp += Size;
1372     SrcAM.Disp += Size;
1373   }
1374
1375   return true;
1376 }
1377
1378 bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
1379   // FIXME: Handle more intrinsics.
1380   switch (I.getIntrinsicID()) {
1381   default: return false;
1382   case Intrinsic::memcpy: {
1383     const MemCpyInst &MCI = cast<MemCpyInst>(I);
1384     // Don't handle volatile or variable length memcpys.
1385     if (MCI.isVolatile())
1386       return false;
1387
1388     if (isa<ConstantInt>(MCI.getLength())) {
1389       // Small memcpy's are common enough that we want to do them
1390       // without a call if possible.
1391       uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
1392       if (IsMemcpySmall(Len)) {
1393         X86AddressMode DestAM, SrcAM;
1394         if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
1395             !X86SelectAddress(MCI.getRawSource(), SrcAM))
1396           return false;
1397         TryEmitSmallMemcpy(DestAM, SrcAM, Len);
1398         return true;
1399       }
1400     }
1401
1402     unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1403     if (!MCI.getLength()->getType()->isIntegerTy(SizeWidth))
1404       return false;
1405
1406     if (MCI.getSourceAddressSpace() > 255 || MCI.getDestAddressSpace() > 255)
1407       return false;
1408
1409     return DoSelectCall(&I, "memcpy");
1410   }
1411   case Intrinsic::memset: {
1412     const MemSetInst &MSI = cast<MemSetInst>(I);
1413
1414     if (MSI.isVolatile())
1415       return false;
1416
1417     unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1418     if (!MSI.getLength()->getType()->isIntegerTy(SizeWidth))
1419       return false;
1420
1421     if (MSI.getDestAddressSpace() > 255)
1422       return false;
1423
1424     return DoSelectCall(&I, "memset");
1425   }
1426   case Intrinsic::stackprotector: {
1427     // Emit code inline code to store the stack guard onto the stack.
1428     EVT PtrTy = TLI.getPointerTy();
1429
1430     const Value *Op1 = I.getArgOperand(0); // The guard's value.
1431     const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
1432
1433     // Grab the frame index.
1434     X86AddressMode AM;
1435     if (!X86SelectAddress(Slot, AM)) return false;
1436     if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
1437     return true;
1438   }
1439   case Intrinsic::dbg_declare: {
1440     const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
1441     X86AddressMode AM;
1442     assert(DI->getAddress() && "Null address should be checked earlier!");
1443     if (!X86SelectAddress(DI->getAddress(), AM))
1444       return false;
1445     const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
1446     // FIXME may need to add RegState::Debug to any registers produced,
1447     // although ESP/EBP should be the only ones at the moment.
1448     addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1449       addImm(0).addMetadata(DI->getVariable());
1450     return true;
1451   }
1452   case Intrinsic::trap: {
1453     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
1454     return true;
1455   }
1456   case Intrinsic::sadd_with_overflow:
1457   case Intrinsic::uadd_with_overflow: {
1458     // FIXME: Should fold immediates.
1459
1460     // Replace "add with overflow" intrinsics with an "add" instruction followed
1461     // by a seto/setc instruction.
1462     const Function *Callee = I.getCalledFunction();
1463     Type *RetTy =
1464       cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1465
1466     MVT VT;
1467     if (!isTypeLegal(RetTy, VT))
1468       return false;
1469
1470     const Value *Op1 = I.getArgOperand(0);
1471     const Value *Op2 = I.getArgOperand(1);
1472     unsigned Reg1 = getRegForValue(Op1);
1473     unsigned Reg2 = getRegForValue(Op2);
1474
1475     if (Reg1 == 0 || Reg2 == 0)
1476       // FIXME: Handle values *not* in registers.
1477       return false;
1478
1479     unsigned OpC = 0;
1480     if (VT == MVT::i32)
1481       OpC = X86::ADD32rr;
1482     else if (VT == MVT::i64)
1483       OpC = X86::ADD64rr;
1484     else
1485       return false;
1486
1487     // The call to CreateRegs builds two sequential registers, to store the
1488     // both the the returned values.
1489     unsigned ResultReg = FuncInfo.CreateRegs(I.getType());
1490     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1491       .addReg(Reg1).addReg(Reg2);
1492
1493     unsigned Opc = X86::SETBr;
1494     if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1495       Opc = X86::SETOr;
1496     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg+1);
1497
1498     UpdateValueMap(&I, ResultReg, 2);
1499     return true;
1500   }
1501   }
1502 }
1503
1504 bool X86FastISel::X86SelectCall(const Instruction *I) {
1505   const CallInst *CI = cast<CallInst>(I);
1506   const Value *Callee = CI->getCalledValue();
1507
1508   // Can't handle inline asm yet.
1509   if (isa<InlineAsm>(Callee))
1510     return false;
1511
1512   // Handle intrinsic calls.
1513   if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
1514     return X86VisitIntrinsicCall(*II);
1515
1516   return DoSelectCall(I, 0);
1517 }
1518
1519 // Select either a call, or an llvm.memcpy/memmove/memset intrinsic
1520 bool X86FastISel::DoSelectCall(const Instruction *I, const char *MemIntName) {
1521   const CallInst *CI = cast<CallInst>(I);
1522   const Value *Callee = CI->getCalledValue();
1523
1524   // Handle only C and fastcc calling conventions for now.
1525   ImmutableCallSite CS(CI);
1526   CallingConv::ID CC = CS.getCallingConv();
1527   if (CC != CallingConv::C && CC != CallingConv::Fast &&
1528       CC != CallingConv::X86_FastCall)
1529     return false;
1530
1531   // fastcc with -tailcallopt is intended to provide a guaranteed
1532   // tail call optimization. Fastisel doesn't know how to do that.
1533   if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
1534     return false;
1535
1536   PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1537   FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1538   bool isVarArg = FTy->isVarArg();
1539
1540   // Don't know how to handle Win64 varargs yet.  Nothing special needed for
1541   // x86-32.  Special handling for x86-64 is implemented.
1542   if (isVarArg && Subtarget->isTargetWin64())
1543     return false;
1544
1545   // Fast-isel doesn't know about callee-pop yet.
1546   if (X86::isCalleePop(CC, Subtarget->is64Bit(), isVarArg,
1547                        TM.Options.GuaranteedTailCallOpt))
1548     return false;
1549
1550   // Check whether the function can return without sret-demotion.
1551   SmallVector<ISD::OutputArg, 4> Outs;
1552   SmallVector<uint64_t, 4> Offsets;
1553   GetReturnInfo(I->getType(), CS.getAttributes().getRetAttributes(),
1554                 Outs, TLI, &Offsets);
1555   bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
1556                                            *FuncInfo.MF, FTy->isVarArg(),
1557                                            Outs, FTy->getContext());
1558   if (!CanLowerReturn)
1559     return false;
1560
1561   // Materialize callee address in a register. FIXME: GV address can be
1562   // handled with a CALLpcrel32 instead.
1563   X86AddressMode CalleeAM;
1564   if (!X86SelectCallAddress(Callee, CalleeAM))
1565     return false;
1566   unsigned CalleeOp = 0;
1567   const GlobalValue *GV = 0;
1568   if (CalleeAM.GV != 0) {
1569     GV = CalleeAM.GV;
1570   } else if (CalleeAM.Base.Reg != 0) {
1571     CalleeOp = CalleeAM.Base.Reg;
1572   } else
1573     return false;
1574
1575   // Deal with call operands first.
1576   SmallVector<const Value *, 8> ArgVals;
1577   SmallVector<unsigned, 8> Args;
1578   SmallVector<MVT, 8> ArgVTs;
1579   SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1580   unsigned arg_size = CS.arg_size();
1581   Args.reserve(arg_size);
1582   ArgVals.reserve(arg_size);
1583   ArgVTs.reserve(arg_size);
1584   ArgFlags.reserve(arg_size);
1585   for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1586        i != e; ++i) {
1587     // If we're lowering a mem intrinsic instead of a regular call, skip the
1588     // last two arguments, which should not passed to the underlying functions.
1589     if (MemIntName && e-i <= 2)
1590       break;
1591     Value *ArgVal = *i;
1592     ISD::ArgFlagsTy Flags;
1593     unsigned AttrInd = i - CS.arg_begin() + 1;
1594     if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1595       Flags.setSExt();
1596     if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1597       Flags.setZExt();
1598
1599     if (CS.paramHasAttr(AttrInd, Attribute::ByVal)) {
1600       PointerType *Ty = cast<PointerType>(ArgVal->getType());
1601       Type *ElementTy = Ty->getElementType();
1602       unsigned FrameSize = TD.getTypeAllocSize(ElementTy);
1603       unsigned FrameAlign = CS.getParamAlignment(AttrInd);
1604       if (!FrameAlign)
1605         FrameAlign = TLI.getByValTypeAlignment(ElementTy);
1606       Flags.setByVal();
1607       Flags.setByValSize(FrameSize);
1608       Flags.setByValAlign(FrameAlign);
1609       if (!IsMemcpySmall(FrameSize))
1610         return false;
1611     }
1612
1613     if (CS.paramHasAttr(AttrInd, Attribute::InReg))
1614       Flags.setInReg();
1615     if (CS.paramHasAttr(AttrInd, Attribute::Nest))
1616       Flags.setNest();
1617
1618     // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
1619     // instruction.  This is safe because it is common to all fastisel supported
1620     // calling conventions on x86.
1621     if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
1622       if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
1623           CI->getBitWidth() == 16) {
1624         if (Flags.isSExt())
1625           ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
1626         else
1627           ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
1628       }
1629     }
1630
1631     unsigned ArgReg;
1632
1633     // Passing bools around ends up doing a trunc to i1 and passing it.
1634     // Codegen this as an argument + "and 1".
1635     if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
1636         cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
1637         ArgVal->hasOneUse()) {
1638       ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
1639       ArgReg = getRegForValue(ArgVal);
1640       if (ArgReg == 0) return false;
1641
1642       MVT ArgVT;
1643       if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
1644
1645       ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
1646                            ArgVal->hasOneUse(), 1);
1647     } else {
1648       ArgReg = getRegForValue(ArgVal);
1649     }
1650
1651     if (ArgReg == 0) return false;
1652
1653     Type *ArgTy = ArgVal->getType();
1654     MVT ArgVT;
1655     if (!isTypeLegal(ArgTy, ArgVT))
1656       return false;
1657     if (ArgVT == MVT::x86mmx)
1658       return false;
1659     unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1660     Flags.setOrigAlign(OriginalAlignment);
1661
1662     Args.push_back(ArgReg);
1663     ArgVals.push_back(ArgVal);
1664     ArgVTs.push_back(ArgVT);
1665     ArgFlags.push_back(Flags);
1666   }
1667
1668   // Analyze operands of the call, assigning locations to each operand.
1669   SmallVector<CCValAssign, 16> ArgLocs;
1670   CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs,
1671                  I->getParent()->getContext());
1672
1673   // Allocate shadow area for Win64
1674   if (Subtarget->isTargetWin64())
1675     CCInfo.AllocateStack(32, 8);
1676
1677   CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
1678
1679   // Get a count of how many bytes are to be pushed on the stack.
1680   unsigned NumBytes = CCInfo.getNextStackOffset();
1681
1682   // Issue CALLSEQ_START
1683   unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
1684   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1685     .addImm(NumBytes);
1686
1687   // Process argument: walk the register/memloc assignments, inserting
1688   // copies / loads.
1689   SmallVector<unsigned, 4> RegArgs;
1690   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1691     CCValAssign &VA = ArgLocs[i];
1692     unsigned Arg = Args[VA.getValNo()];
1693     EVT ArgVT = ArgVTs[VA.getValNo()];
1694
1695     // Promote the value if needed.
1696     switch (VA.getLocInfo()) {
1697     default: llvm_unreachable("Unknown loc info!");
1698     case CCValAssign::Full: break;
1699     case CCValAssign::SExt: {
1700       assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1701              "Unexpected extend");
1702       bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1703                                        Arg, ArgVT, Arg);
1704       assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
1705       ArgVT = VA.getLocVT();
1706       break;
1707     }
1708     case CCValAssign::ZExt: {
1709       assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1710              "Unexpected extend");
1711       bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1712                                        Arg, ArgVT, Arg);
1713       assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
1714       ArgVT = VA.getLocVT();
1715       break;
1716     }
1717     case CCValAssign::AExt: {
1718       assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1719              "Unexpected extend");
1720       bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1721                                        Arg, ArgVT, Arg);
1722       if (!Emitted)
1723         Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1724                                     Arg, ArgVT, Arg);
1725       if (!Emitted)
1726         Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1727                                     Arg, ArgVT, Arg);
1728
1729       assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
1730       ArgVT = VA.getLocVT();
1731       break;
1732     }
1733     case CCValAssign::BCvt: {
1734       unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
1735                                ISD::BITCAST, Arg, /*TODO: Kill=*/false);
1736       assert(BC != 0 && "Failed to emit a bitcast!");
1737       Arg = BC;
1738       ArgVT = VA.getLocVT();
1739       break;
1740     }
1741     }
1742
1743     if (VA.isRegLoc()) {
1744       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1745               VA.getLocReg()).addReg(Arg);
1746       RegArgs.push_back(VA.getLocReg());
1747     } else {
1748       unsigned LocMemOffset = VA.getLocMemOffset();
1749       X86AddressMode AM;
1750       AM.Base.Reg = StackPtr;
1751       AM.Disp = LocMemOffset;
1752       const Value *ArgVal = ArgVals[VA.getValNo()];
1753       ISD::ArgFlagsTy Flags = ArgFlags[VA.getValNo()];
1754
1755       if (Flags.isByVal()) {
1756         X86AddressMode SrcAM;
1757         SrcAM.Base.Reg = Arg;
1758         bool Res = TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize());
1759         assert(Res && "memcpy length already checked!"); (void)Res;
1760       } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
1761         // If this is a really simple value, emit this with the Value* version
1762         // of X86FastEmitStore.  If it isn't simple, we don't want to do this,
1763         // as it can cause us to reevaluate the argument.
1764         if (!X86FastEmitStore(ArgVT, ArgVal, AM))
1765           return false;
1766       } else {
1767         if (!X86FastEmitStore(ArgVT, Arg, AM))
1768           return false;
1769       }
1770     }
1771   }
1772
1773   // ELF / PIC requires GOT in the EBX register before function calls via PLT
1774   // GOT pointer.
1775   if (Subtarget->isPICStyleGOT()) {
1776     unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
1777     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1778             X86::EBX).addReg(Base);
1779   }
1780
1781   if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64()) {
1782     // Count the number of XMM registers allocated.
1783     static const uint16_t XMMArgRegs[] = {
1784       X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1785       X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1786     };
1787     unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1788     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::MOV8ri),
1789             X86::AL).addImm(NumXMMRegs);
1790   }
1791
1792   // Issue the call.
1793   MachineInstrBuilder MIB;
1794   if (CalleeOp) {
1795     // Register-indirect call.
1796     unsigned CallOpc;
1797     if (Subtarget->is64Bit())
1798       CallOpc = X86::CALL64r;
1799     else
1800       CallOpc = X86::CALL32r;
1801     MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
1802       .addReg(CalleeOp);
1803
1804   } else {
1805     // Direct call.
1806     assert(GV && "Not a direct call");
1807     unsigned CallOpc;
1808     if (Subtarget->is64Bit())
1809       CallOpc = X86::CALL64pcrel32;
1810     else
1811       CallOpc = X86::CALLpcrel32;
1812
1813     // See if we need any target-specific flags on the GV operand.
1814     unsigned char OpFlags = 0;
1815
1816     // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1817     // external symbols most go through the PLT in PIC mode.  If the symbol
1818     // has hidden or protected visibility, or if it is static or local, then
1819     // we don't need to use the PLT - we can directly call it.
1820     if (Subtarget->isTargetELF() &&
1821         TM.getRelocationModel() == Reloc::PIC_ &&
1822         GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
1823       OpFlags = X86II::MO_PLT;
1824     } else if (Subtarget->isPICStyleStubAny() &&
1825                (GV->isDeclaration() || GV->isWeakForLinker()) &&
1826                (!Subtarget->getTargetTriple().isMacOSX() ||
1827                 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
1828       // PC-relative references to external symbols should go through $stub,
1829       // unless we're building with the leopard linker or later, which
1830       // automatically synthesizes these stubs.
1831       OpFlags = X86II::MO_DARWIN_STUB;
1832     }
1833
1834
1835     MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc));
1836     if (MemIntName)
1837       MIB.addExternalSymbol(MemIntName, OpFlags);
1838     else
1839       MIB.addGlobalAddress(GV, 0, OpFlags);
1840   }
1841
1842   // Add an implicit use GOT pointer in EBX.
1843   if (Subtarget->isPICStyleGOT())
1844     MIB.addReg(X86::EBX);
1845
1846   if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64())
1847     MIB.addReg(X86::AL);
1848
1849   // Add implicit physical register uses to the call.
1850   for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1851     MIB.addReg(RegArgs[i]);
1852
1853   // Add a register mask with the call-preserved registers.
1854   // Proper defs for return values will be added by setPhysRegsDeadExcept().
1855   MIB.addRegMask(TRI.getCallPreservedMask(CS.getCallingConv()));
1856
1857   // Issue CALLSEQ_END
1858   unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
1859   unsigned NumBytesCallee = 0;
1860   if (!Subtarget->is64Bit() && !Subtarget->isTargetWindows() &&
1861       CS.paramHasAttr(1, Attribute::StructRet))
1862     NumBytesCallee = 4;
1863   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
1864     .addImm(NumBytes).addImm(NumBytesCallee);
1865
1866   // Build info for return calling conv lowering code.
1867   // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
1868   SmallVector<ISD::InputArg, 32> Ins;
1869   SmallVector<EVT, 4> RetTys;
1870   ComputeValueVTs(TLI, I->getType(), RetTys);
1871   for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
1872     EVT VT = RetTys[i];
1873     EVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
1874     unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
1875     for (unsigned j = 0; j != NumRegs; ++j) {
1876       ISD::InputArg MyFlags;
1877       MyFlags.VT = RegisterVT.getSimpleVT();
1878       MyFlags.Used = !CS.getInstruction()->use_empty();
1879       if (CS.paramHasAttr(0, Attribute::SExt))
1880         MyFlags.Flags.setSExt();
1881       if (CS.paramHasAttr(0, Attribute::ZExt))
1882         MyFlags.Flags.setZExt();
1883       if (CS.paramHasAttr(0, Attribute::InReg))
1884         MyFlags.Flags.setInReg();
1885       Ins.push_back(MyFlags);
1886     }
1887   }
1888
1889   // Now handle call return values.
1890   SmallVector<unsigned, 4> UsedRegs;
1891   SmallVector<CCValAssign, 16> RVLocs;
1892   CCState CCRetInfo(CC, false, *FuncInfo.MF, TM, RVLocs,
1893                     I->getParent()->getContext());
1894   unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
1895   CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
1896   for (unsigned i = 0; i != RVLocs.size(); ++i) {
1897     EVT CopyVT = RVLocs[i].getValVT();
1898     unsigned CopyReg = ResultReg + i;
1899
1900     // If this is a call to a function that returns an fp value on the x87 fp
1901     // stack, but where we prefer to use the value in xmm registers, copy it
1902     // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
1903     if ((RVLocs[i].getLocReg() == X86::ST0 ||
1904          RVLocs[i].getLocReg() == X86::ST1)) {
1905       if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
1906         CopyVT = MVT::f80;
1907         CopyReg = createResultReg(&X86::RFP80RegClass);
1908       }
1909       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::FpPOP_RETVAL),
1910               CopyReg);
1911     } else {
1912       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1913               CopyReg).addReg(RVLocs[i].getLocReg());
1914       UsedRegs.push_back(RVLocs[i].getLocReg());
1915     }
1916
1917     if (CopyVT != RVLocs[i].getValVT()) {
1918       // Round the F80 the right size, which also moves to the appropriate xmm
1919       // register. This is accomplished by storing the F80 value in memory and
1920       // then loading it back. Ewww...
1921       EVT ResVT = RVLocs[i].getValVT();
1922       unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
1923       unsigned MemSize = ResVT.getSizeInBits()/8;
1924       int FI = MFI.CreateStackObject(MemSize, MemSize, false);
1925       addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1926                                 TII.get(Opc)), FI)
1927         .addReg(CopyReg);
1928       Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
1929       addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1930                                 TII.get(Opc), ResultReg + i), FI);
1931     }
1932   }
1933
1934   if (RVLocs.size())
1935     UpdateValueMap(I, ResultReg, RVLocs.size());
1936
1937   // Set all unused physreg defs as dead.
1938   static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1939
1940   return true;
1941 }
1942
1943
1944 bool
1945 X86FastISel::TargetSelectInstruction(const Instruction *I)  {
1946   switch (I->getOpcode()) {
1947   default: break;
1948   case Instruction::Load:
1949     return X86SelectLoad(I);
1950   case Instruction::Store:
1951     return X86SelectStore(I);
1952   case Instruction::Ret:
1953     return X86SelectRet(I);
1954   case Instruction::ICmp:
1955   case Instruction::FCmp:
1956     return X86SelectCmp(I);
1957   case Instruction::ZExt:
1958     return X86SelectZExt(I);
1959   case Instruction::Br:
1960     return X86SelectBranch(I);
1961   case Instruction::Call:
1962     return X86SelectCall(I);
1963   case Instruction::LShr:
1964   case Instruction::AShr:
1965   case Instruction::Shl:
1966     return X86SelectShift(I);
1967   case Instruction::Select:
1968     return X86SelectSelect(I);
1969   case Instruction::Trunc:
1970     return X86SelectTrunc(I);
1971   case Instruction::FPExt:
1972     return X86SelectFPExt(I);
1973   case Instruction::FPTrunc:
1974     return X86SelectFPTrunc(I);
1975   case Instruction::IntToPtr: // Deliberate fall-through.
1976   case Instruction::PtrToInt: {
1977     EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1978     EVT DstVT = TLI.getValueType(I->getType());
1979     if (DstVT.bitsGT(SrcVT))
1980       return X86SelectZExt(I);
1981     if (DstVT.bitsLT(SrcVT))
1982       return X86SelectTrunc(I);
1983     unsigned Reg = getRegForValue(I->getOperand(0));
1984     if (Reg == 0) return false;
1985     UpdateValueMap(I, Reg);
1986     return true;
1987   }
1988   }
1989
1990   return false;
1991 }
1992
1993 unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
1994   MVT VT;
1995   if (!isTypeLegal(C->getType(), VT))
1996     return false;
1997
1998   // Get opcode and regclass of the output for the given load instruction.
1999   unsigned Opc = 0;
2000   const TargetRegisterClass *RC = NULL;
2001   switch (VT.SimpleTy) {
2002   default: return false;
2003   case MVT::i8:
2004     Opc = X86::MOV8rm;
2005     RC  = &X86::GR8RegClass;
2006     break;
2007   case MVT::i16:
2008     Opc = X86::MOV16rm;
2009     RC  = &X86::GR16RegClass;
2010     break;
2011   case MVT::i32:
2012     Opc = X86::MOV32rm;
2013     RC  = &X86::GR32RegClass;
2014     break;
2015   case MVT::i64:
2016     // Must be in x86-64 mode.
2017     Opc = X86::MOV64rm;
2018     RC  = &X86::GR64RegClass;
2019     break;
2020   case MVT::f32:
2021     if (X86ScalarSSEf32) {
2022       Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
2023       RC  = &X86::FR32RegClass;
2024     } else {
2025       Opc = X86::LD_Fp32m;
2026       RC  = &X86::RFP32RegClass;
2027     }
2028     break;
2029   case MVT::f64:
2030     if (X86ScalarSSEf64) {
2031       Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
2032       RC  = &X86::FR64RegClass;
2033     } else {
2034       Opc = X86::LD_Fp64m;
2035       RC  = &X86::RFP64RegClass;
2036     }
2037     break;
2038   case MVT::f80:
2039     // No f80 support yet.
2040     return false;
2041   }
2042
2043   // Materialize addresses with LEA instructions.
2044   if (isa<GlobalValue>(C)) {
2045     X86AddressMode AM;
2046     if (X86SelectAddress(C, AM)) {
2047       // If the expression is just a basereg, then we're done, otherwise we need
2048       // to emit an LEA.
2049       if (AM.BaseType == X86AddressMode::RegBase &&
2050           AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == 0)
2051         return AM.Base.Reg;
2052
2053       Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
2054       unsigned ResultReg = createResultReg(RC);
2055       addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2056                              TII.get(Opc), ResultReg), AM);
2057       return ResultReg;
2058     }
2059     return 0;
2060   }
2061
2062   // MachineConstantPool wants an explicit alignment.
2063   unsigned Align = TD.getPrefTypeAlignment(C->getType());
2064   if (Align == 0) {
2065     // Alignment of vector types.  FIXME!
2066     Align = TD.getTypeAllocSize(C->getType());
2067   }
2068
2069   // x86-32 PIC requires a PIC base register for constant pools.
2070   unsigned PICBase = 0;
2071   unsigned char OpFlag = 0;
2072   if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
2073     OpFlag = X86II::MO_PIC_BASE_OFFSET;
2074     PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
2075   } else if (Subtarget->isPICStyleGOT()) {
2076     OpFlag = X86II::MO_GOTOFF;
2077     PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
2078   } else if (Subtarget->isPICStyleRIPRel() &&
2079              TM.getCodeModel() == CodeModel::Small) {
2080     PICBase = X86::RIP;
2081   }
2082
2083   // Create the load from the constant pool.
2084   unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
2085   unsigned ResultReg = createResultReg(RC);
2086   addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2087                                    TII.get(Opc), ResultReg),
2088                            MCPOffset, PICBase, OpFlag);
2089
2090   return ResultReg;
2091 }
2092
2093 unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
2094   // Fail on dynamic allocas. At this point, getRegForValue has already
2095   // checked its CSE maps, so if we're here trying to handle a dynamic
2096   // alloca, we're not going to succeed. X86SelectAddress has a
2097   // check for dynamic allocas, because it's called directly from
2098   // various places, but TargetMaterializeAlloca also needs a check
2099   // in order to avoid recursion between getRegForValue,
2100   // X86SelectAddrss, and TargetMaterializeAlloca.
2101   if (!FuncInfo.StaticAllocaMap.count(C))
2102     return 0;
2103
2104   X86AddressMode AM;
2105   if (!X86SelectAddress(C, AM))
2106     return 0;
2107   unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
2108   const TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
2109   unsigned ResultReg = createResultReg(RC);
2110   addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2111                          TII.get(Opc), ResultReg), AM);
2112   return ResultReg;
2113 }
2114
2115 unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
2116   MVT VT;
2117   if (!isTypeLegal(CF->getType(), VT))
2118     return false;
2119
2120   // Get opcode and regclass for the given zero.
2121   unsigned Opc = 0;
2122   const TargetRegisterClass *RC = NULL;
2123   switch (VT.SimpleTy) {
2124     default: return false;
2125     case MVT::f32:
2126       if (X86ScalarSSEf32) {
2127         Opc = X86::FsFLD0SS;
2128         RC  = &X86::FR32RegClass;
2129       } else {
2130         Opc = X86::LD_Fp032;
2131         RC  = &X86::RFP32RegClass;
2132       }
2133       break;
2134     case MVT::f64:
2135       if (X86ScalarSSEf64) {
2136         Opc = X86::FsFLD0SD;
2137         RC  = &X86::FR64RegClass;
2138       } else {
2139         Opc = X86::LD_Fp064;
2140         RC  = &X86::RFP64RegClass;
2141       }
2142       break;
2143     case MVT::f80:
2144       // No f80 support yet.
2145       return false;
2146   }
2147
2148   unsigned ResultReg = createResultReg(RC);
2149   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
2150   return ResultReg;
2151 }
2152
2153
2154 /// TryToFoldLoad - The specified machine instr operand is a vreg, and that
2155 /// vreg is being provided by the specified load instruction.  If possible,
2156 /// try to fold the load as an operand to the instruction, returning true if
2157 /// possible.
2158 bool X86FastISel::TryToFoldLoad(MachineInstr *MI, unsigned OpNo,
2159                                 const LoadInst *LI) {
2160   X86AddressMode AM;
2161   if (!X86SelectAddress(LI->getOperand(0), AM))
2162     return false;
2163
2164   X86InstrInfo &XII = (X86InstrInfo&)TII;
2165
2166   unsigned Size = TD.getTypeAllocSize(LI->getType());
2167   unsigned Alignment = LI->getAlignment();
2168
2169   SmallVector<MachineOperand, 8> AddrOps;
2170   AM.getFullAddress(AddrOps);
2171
2172   MachineInstr *Result =
2173     XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
2174   if (Result == 0) return false;
2175
2176   FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
2177   MI->eraseFromParent();
2178   return true;
2179 }
2180
2181
2182 namespace llvm {
2183   FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo) {
2184     return new X86FastISel(funcInfo);
2185   }
2186 }