Add X86FastISel support for static allocas, and refences
[oota-llvm.git] / lib / CodeGen / SelectionDAG / FastISel.cpp
1 ///===-- FastISel.cpp - Implementation of the FastISel class --------------===//
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 contains the implementation of the FastISel class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Instructions.h"
15 #include "llvm/CodeGen/FastISel.h"
16 #include "llvm/CodeGen/MachineInstrBuilder.h"
17 #include "llvm/CodeGen/MachineRegisterInfo.h"
18 #include "llvm/Target/TargetData.h"
19 #include "llvm/Target/TargetInstrInfo.h"
20 #include "llvm/Target/TargetLowering.h"
21 #include "llvm/Target/TargetMachine.h"
22 using namespace llvm;
23
24 unsigned FastISel::getRegForValue(Value *V) {
25   // Look up the value to see if we already have a register for it. We
26   // cache values defined by Instructions across blocks, and other values
27   // only locally. This is because Instructions already have the SSA
28   // def-dominatess-use requirement enforced.
29   if (ValueMap.count(V))
30     return ValueMap[V];
31   unsigned Reg = LocalValueMap[V];
32   if (Reg != 0)
33     return Reg;
34
35   MVT::SimpleValueType VT = TLI.getValueType(V->getType()).getSimpleVT();
36   if (!TLI.isTypeLegal(VT))
37     return 0;
38   if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
39     if (CI->getValue().getActiveBits() > 64)
40       return TargetMaterializeConstant(CI);
41     // Don't cache constant materializations.  To do so would require
42     // tracking what uses they dominate.
43     Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
44   } else if (isa<GlobalValue>(V)) {
45     return TargetMaterializeConstant(cast<Constant>(V));
46   } else if (isa<AllocaInst>(V)) {
47     return TargetMaterializeAlloca(cast<AllocaInst>(V));
48   } else if (isa<ConstantPointerNull>(V)) {
49     Reg = FastEmit_i(VT, VT, ISD::Constant, 0);
50   } else if (ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
51     Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
52
53     if (!Reg) {
54       const APFloat &Flt = CF->getValueAPF();
55       MVT IntVT = TLI.getPointerTy();
56
57       uint64_t x[2];
58       uint32_t IntBitWidth = IntVT.getSizeInBits();
59       if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
60                                APFloat::rmTowardZero) != APFloat::opOK)
61         return TargetMaterializeConstant(CF);
62       APInt IntVal(IntBitWidth, 2, x);
63
64       unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
65                                        ISD::Constant, IntVal.getZExtValue());
66       if (IntegerReg == 0)
67         return TargetMaterializeConstant(CF);
68       Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
69       if (Reg == 0)
70         return TargetMaterializeConstant(CF);
71     }
72   } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
73     if (!SelectOperator(CE, CE->getOpcode())) return 0;
74     Reg = LocalValueMap[CE];
75   } else if (isa<UndefValue>(V)) {
76     Reg = createResultReg(TLI.getRegClassFor(VT));
77     BuildMI(MBB, TII.get(TargetInstrInfo::IMPLICIT_DEF), Reg);
78   } else {
79     return 0;
80   }
81   
82   if (!Reg && isa<Constant>(V))
83     return TargetMaterializeConstant(cast<Constant>(V));
84   
85   LocalValueMap[V] = Reg;
86   return Reg;
87 }
88
89 unsigned FastISel::lookUpRegForValue(Value *V) {
90   // Look up the value to see if we already have a register for it. We
91   // cache values defined by Instructions across blocks, and other values
92   // only locally. This is because Instructions already have the SSA
93   // def-dominatess-use requirement enforced.
94   if (ValueMap.count(V))
95     return ValueMap[V];
96   return LocalValueMap[V];
97 }
98
99 /// UpdateValueMap - Update the value map to include the new mapping for this
100 /// instruction, or insert an extra copy to get the result in a previous
101 /// determined register.
102 /// NOTE: This is only necessary because we might select a block that uses
103 /// a value before we select the block that defines the value.  It might be
104 /// possible to fix this by selecting blocks in reverse postorder.
105 void FastISel::UpdateValueMap(Value* I, unsigned Reg) {
106   if (!isa<Instruction>(I)) {
107     LocalValueMap[I] = Reg;
108     return;
109   }
110   if (!ValueMap.count(I))
111     ValueMap[I] = Reg;
112   else
113     TII.copyRegToReg(*MBB, MBB->end(), ValueMap[I],
114                      Reg, MRI.getRegClass(Reg), MRI.getRegClass(Reg));
115 }
116
117 /// SelectBinaryOp - Select and emit code for a binary operator instruction,
118 /// which has an opcode which directly corresponds to the given ISD opcode.
119 ///
120 bool FastISel::SelectBinaryOp(User *I, ISD::NodeType ISDOpcode) {
121   MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
122   if (VT == MVT::Other || !VT.isSimple())
123     // Unhandled type. Halt "fast" selection and bail.
124     return false;
125
126   // We only handle legal types. For example, on x86-32 the instruction
127   // selector contains all of the 64-bit instructions from x86-64,
128   // under the assumption that i64 won't be used if the target doesn't
129   // support it.
130   if (!TLI.isTypeLegal(VT)) {
131     // MVT::i1 is special. Allow AND and OR (but not XOR) because they
132     // don't require additional zeroing, which makes them easy.
133     if (VT == MVT::i1 &&
134         (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR))
135       VT = TLI.getTypeToTransformTo(VT);
136     else
137       return false;
138   }
139
140   unsigned Op0 = getRegForValue(I->getOperand(0));
141   if (Op0 == 0)
142     // Unhandled operand. Halt "fast" selection and bail.
143     return false;
144
145   // Check if the second operand is a constant and handle it appropriately.
146   if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
147     unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
148                                      ISDOpcode, Op0, CI->getZExtValue());
149     if (ResultReg != 0) {
150       // We successfully emitted code for the given LLVM Instruction.
151       UpdateValueMap(I, ResultReg);
152       return true;
153     }
154   }
155
156   // Check if the second operand is a constant float.
157   if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
158     unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
159                                      ISDOpcode, Op0, CF);
160     if (ResultReg != 0) {
161       // We successfully emitted code for the given LLVM Instruction.
162       UpdateValueMap(I, ResultReg);
163       return true;
164     }
165   }
166
167   unsigned Op1 = getRegForValue(I->getOperand(1));
168   if (Op1 == 0)
169     // Unhandled operand. Halt "fast" selection and bail.
170     return false;
171
172   // Now we have both operands in registers. Emit the instruction.
173   unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
174                                    ISDOpcode, Op0, Op1);
175   if (ResultReg == 0)
176     // Target-specific code wasn't able to find a machine opcode for
177     // the given ISD opcode and type. Halt "fast" selection and bail.
178     return false;
179
180   // We successfully emitted code for the given LLVM Instruction.
181   UpdateValueMap(I, ResultReg);
182   return true;
183 }
184
185 bool FastISel::SelectGetElementPtr(User *I) {
186   unsigned N = getRegForValue(I->getOperand(0));
187   if (N == 0)
188     // Unhandled operand. Halt "fast" selection and bail.
189     return false;
190
191   const Type *Ty = I->getOperand(0)->getType();
192   MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
193   for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
194        OI != E; ++OI) {
195     Value *Idx = *OI;
196     if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
197       unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
198       if (Field) {
199         // N = N + Offset
200         uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
201         // FIXME: This can be optimized by combining the add with a
202         // subsequent one.
203         N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
204         if (N == 0)
205           // Unhandled operand. Halt "fast" selection and bail.
206           return false;
207       }
208       Ty = StTy->getElementType(Field);
209     } else {
210       Ty = cast<SequentialType>(Ty)->getElementType();
211
212       // If this is a constant subscript, handle it quickly.
213       if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
214         if (CI->getZExtValue() == 0) continue;
215         uint64_t Offs = 
216           TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
217         N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
218         if (N == 0)
219           // Unhandled operand. Halt "fast" selection and bail.
220           return false;
221         continue;
222       }
223       
224       // N = N + Idx * ElementSize;
225       uint64_t ElementSize = TD.getABITypeSize(Ty);
226       unsigned IdxN = getRegForValue(Idx);
227       if (IdxN == 0)
228         // Unhandled operand. Halt "fast" selection and bail.
229         return false;
230
231       // If the index is smaller or larger than intptr_t, truncate or extend
232       // it.
233       MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
234       if (IdxVT.bitsLT(VT))
235         IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::SIGN_EXTEND, IdxN);
236       else if (IdxVT.bitsGT(VT))
237         IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::TRUNCATE, IdxN);
238       if (IdxN == 0)
239         // Unhandled operand. Halt "fast" selection and bail.
240         return false;
241
242       if (ElementSize != 1) {
243         IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
244         if (IdxN == 0)
245           // Unhandled operand. Halt "fast" selection and bail.
246           return false;
247       }
248       N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
249       if (N == 0)
250         // Unhandled operand. Halt "fast" selection and bail.
251         return false;
252     }
253   }
254
255   // We successfully emitted code for the given LLVM Instruction.
256   UpdateValueMap(I, N);
257   return true;
258 }
259
260 bool FastISel::SelectCast(User *I, ISD::NodeType Opcode) {
261   MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
262   MVT DstVT = TLI.getValueType(I->getType());
263     
264   if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
265       DstVT == MVT::Other || !DstVT.isSimple() ||
266       !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
267     // Unhandled type. Halt "fast" selection and bail.
268     return false;
269     
270   unsigned InputReg = getRegForValue(I->getOperand(0));
271   if (!InputReg)
272     // Unhandled operand.  Halt "fast" selection and bail.
273     return false;
274     
275   unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
276                                   DstVT.getSimpleVT(),
277                                   Opcode,
278                                   InputReg);
279   if (!ResultReg)
280     return false;
281     
282   UpdateValueMap(I, ResultReg);
283   return true;
284 }
285
286 bool FastISel::SelectBitCast(User *I) {
287   // If the bitcast doesn't change the type, just use the operand value.
288   if (I->getType() == I->getOperand(0)->getType()) {
289     unsigned Reg = getRegForValue(I->getOperand(0));
290     if (Reg == 0)
291       return false;
292     UpdateValueMap(I, Reg);
293     return true;
294   }
295
296   // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
297   MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
298   MVT DstVT = TLI.getValueType(I->getType());
299   
300   if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
301       DstVT == MVT::Other || !DstVT.isSimple() ||
302       !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
303     // Unhandled type. Halt "fast" selection and bail.
304     return false;
305   
306   unsigned Op0 = getRegForValue(I->getOperand(0));
307   if (Op0 == 0)
308     // Unhandled operand. Halt "fast" selection and bail.
309     return false;
310   
311   // First, try to perform the bitcast by inserting a reg-reg copy.
312   unsigned ResultReg = 0;
313   if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
314     TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
315     TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
316     ResultReg = createResultReg(DstClass);
317     
318     bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
319                                          Op0, DstClass, SrcClass);
320     if (!InsertedCopy)
321       ResultReg = 0;
322   }
323   
324   // If the reg-reg copy failed, select a BIT_CONVERT opcode.
325   if (!ResultReg)
326     ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
327                            ISD::BIT_CONVERT, Op0);
328   
329   if (!ResultReg)
330     return false;
331   
332   UpdateValueMap(I, ResultReg);
333   return true;
334 }
335
336 bool
337 FastISel::SelectInstruction(Instruction *I) {
338   return SelectOperator(I, I->getOpcode());
339 }
340
341 bool
342 FastISel::SelectOperator(User *I, unsigned Opcode) {
343   switch (Opcode) {
344   case Instruction::Add: {
345     ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
346     return SelectBinaryOp(I, Opc);
347   }
348   case Instruction::Sub: {
349     ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
350     return SelectBinaryOp(I, Opc);
351   }
352   case Instruction::Mul: {
353     ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
354     return SelectBinaryOp(I, Opc);
355   }
356   case Instruction::SDiv:
357     return SelectBinaryOp(I, ISD::SDIV);
358   case Instruction::UDiv:
359     return SelectBinaryOp(I, ISD::UDIV);
360   case Instruction::FDiv:
361     return SelectBinaryOp(I, ISD::FDIV);
362   case Instruction::SRem:
363     return SelectBinaryOp(I, ISD::SREM);
364   case Instruction::URem:
365     return SelectBinaryOp(I, ISD::UREM);
366   case Instruction::FRem:
367     return SelectBinaryOp(I, ISD::FREM);
368   case Instruction::Shl:
369     return SelectBinaryOp(I, ISD::SHL);
370   case Instruction::LShr:
371     return SelectBinaryOp(I, ISD::SRL);
372   case Instruction::AShr:
373     return SelectBinaryOp(I, ISD::SRA);
374   case Instruction::And:
375     return SelectBinaryOp(I, ISD::AND);
376   case Instruction::Or:
377     return SelectBinaryOp(I, ISD::OR);
378   case Instruction::Xor:
379     return SelectBinaryOp(I, ISD::XOR);
380
381   case Instruction::GetElementPtr:
382     return SelectGetElementPtr(I);
383
384   case Instruction::Br: {
385     BranchInst *BI = cast<BranchInst>(I);
386
387     if (BI->isUnconditional()) {
388       MachineFunction::iterator NextMBB =
389          next(MachineFunction::iterator(MBB));
390       BasicBlock *LLVMSucc = BI->getSuccessor(0);
391       MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
392
393       if (NextMBB != MF.end() && MSucc == NextMBB) {
394         // The unconditional fall-through case, which needs no instructions.
395       } else {
396         // The unconditional branch case.
397         TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
398       }
399       MBB->addSuccessor(MSucc);
400       return true;
401     }
402
403     // Conditional branches are not handed yet.
404     // Halt "fast" selection and bail.
405     return false;
406   }
407
408   case Instruction::Unreachable:
409     // Nothing to emit.
410     return true;
411
412   case Instruction::PHI:
413     // PHI nodes are already emitted.
414     return true;
415
416   case Instruction::Alloca:
417     // FunctionLowering has the static-sized case covered.
418     if (StaticAllocaMap.count(cast<AllocaInst>(I)))
419       return true;
420
421     // Dynamic-sized alloca is not handled yet.
422     return false;
423     
424   case Instruction::BitCast:
425     return SelectBitCast(I);
426
427   case Instruction::FPToSI:
428     return SelectCast(I, ISD::FP_TO_SINT);
429   case Instruction::ZExt:
430     return SelectCast(I, ISD::ZERO_EXTEND);
431   case Instruction::SExt:
432     return SelectCast(I, ISD::SIGN_EXTEND);
433   case Instruction::Trunc:
434     return SelectCast(I, ISD::TRUNCATE);
435   case Instruction::SIToFP:
436     return SelectCast(I, ISD::SINT_TO_FP);
437
438   case Instruction::IntToPtr: // Deliberate fall-through.
439   case Instruction::PtrToInt: {
440     MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
441     MVT DstVT = TLI.getValueType(I->getType());
442     if (DstVT.bitsGT(SrcVT))
443       return SelectCast(I, ISD::ZERO_EXTEND);
444     if (DstVT.bitsLT(SrcVT))
445       return SelectCast(I, ISD::TRUNCATE);
446     unsigned Reg = getRegForValue(I->getOperand(0));
447     if (Reg == 0) return false;
448     UpdateValueMap(I, Reg);
449     return true;
450   }
451   
452   default:
453     // Unhandled instruction. Halt "fast" selection and bail.
454     return false;
455   }
456 }
457
458 FastISel::FastISel(MachineFunction &mf,
459                    DenseMap<const Value *, unsigned> &vm,
460                    DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
461                    DenseMap<const AllocaInst *, int> &am)
462   : MBB(0),
463     ValueMap(vm),
464     MBBMap(bm),
465     StaticAllocaMap(am),
466     MF(mf),
467     MRI(MF.getRegInfo()),
468     MFI(*MF.getFrameInfo()),
469     MCP(*MF.getConstantPool()),
470     TM(MF.getTarget()),
471     TD(*TM.getTargetData()),
472     TII(*TM.getInstrInfo()),
473     TLI(*TM.getTargetLowering()) {
474 }
475
476 FastISel::~FastISel() {}
477
478 unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType,
479                              ISD::NodeType) {
480   return 0;
481 }
482
483 unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
484                               ISD::NodeType, unsigned /*Op0*/) {
485   return 0;
486 }
487
488 unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType, 
489                                ISD::NodeType, unsigned /*Op0*/,
490                                unsigned /*Op0*/) {
491   return 0;
492 }
493
494 unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
495                               ISD::NodeType, uint64_t /*Imm*/) {
496   return 0;
497 }
498
499 unsigned FastISel::FastEmit_f(MVT::SimpleValueType, MVT::SimpleValueType,
500                               ISD::NodeType, ConstantFP * /*FPImm*/) {
501   return 0;
502 }
503
504 unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
505                                ISD::NodeType, unsigned /*Op0*/,
506                                uint64_t /*Imm*/) {
507   return 0;
508 }
509
510 unsigned FastISel::FastEmit_rf(MVT::SimpleValueType, MVT::SimpleValueType,
511                                ISD::NodeType, unsigned /*Op0*/,
512                                ConstantFP * /*FPImm*/) {
513   return 0;
514 }
515
516 unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
517                                 ISD::NodeType,
518                                 unsigned /*Op0*/, unsigned /*Op1*/,
519                                 uint64_t /*Imm*/) {
520   return 0;
521 }
522
523 /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
524 /// to emit an instruction with an immediate operand using FastEmit_ri.
525 /// If that fails, it materializes the immediate into a register and try
526 /// FastEmit_rr instead.
527 unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
528                                 unsigned Op0, uint64_t Imm,
529                                 MVT::SimpleValueType ImmType) {
530   // First check if immediate type is legal. If not, we can't use the ri form.
531   unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
532   if (ResultReg != 0)
533     return ResultReg;
534   unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
535   if (MaterialReg == 0)
536     return 0;
537   return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
538 }
539
540 /// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
541 /// to emit an instruction with a floating-point immediate operand using
542 /// FastEmit_rf. If that fails, it materializes the immediate into a register
543 /// and try FastEmit_rr instead.
544 unsigned FastISel::FastEmit_rf_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
545                                 unsigned Op0, ConstantFP *FPImm,
546                                 MVT::SimpleValueType ImmType) {
547   // First check if immediate type is legal. If not, we can't use the rf form.
548   unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
549   if (ResultReg != 0)
550     return ResultReg;
551
552   // Materialize the constant in a register.
553   unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
554   if (MaterialReg == 0) {
555     // If the target doesn't have a way to directly enter a floating-point
556     // value into a register, use an alternate approach.
557     // TODO: The current approach only supports floating-point constants
558     // that can be constructed by conversion from integer values. This should
559     // be replaced by code that creates a load from a constant-pool entry,
560     // which will require some target-specific work.
561     const APFloat &Flt = FPImm->getValueAPF();
562     MVT IntVT = TLI.getPointerTy();
563
564     uint64_t x[2];
565     uint32_t IntBitWidth = IntVT.getSizeInBits();
566     if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
567                              APFloat::rmTowardZero) != APFloat::opOK)
568       return 0;
569     APInt IntVal(IntBitWidth, 2, x);
570
571     unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
572                                      ISD::Constant, IntVal.getZExtValue());
573     if (IntegerReg == 0)
574       return 0;
575     MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
576                              ISD::SINT_TO_FP, IntegerReg);
577     if (MaterialReg == 0)
578       return 0;
579   }
580   return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
581 }
582
583 unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
584   return MRI.createVirtualRegister(RC);
585 }
586
587 unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
588                                  const TargetRegisterClass* RC) {
589   unsigned ResultReg = createResultReg(RC);
590   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
591
592   BuildMI(MBB, II, ResultReg);
593   return ResultReg;
594 }
595
596 unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
597                                   const TargetRegisterClass *RC,
598                                   unsigned Op0) {
599   unsigned ResultReg = createResultReg(RC);
600   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
601
602   if (II.getNumDefs() >= 1)
603     BuildMI(MBB, II, ResultReg).addReg(Op0);
604   else {
605     BuildMI(MBB, II).addReg(Op0);
606     bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
607                                          II.ImplicitDefs[0], RC, RC);
608     if (!InsertedCopy)
609       ResultReg = 0;
610   }
611
612   return ResultReg;
613 }
614
615 unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
616                                    const TargetRegisterClass *RC,
617                                    unsigned Op0, unsigned Op1) {
618   unsigned ResultReg = createResultReg(RC);
619   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
620
621   if (II.getNumDefs() >= 1)
622     BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1);
623   else {
624     BuildMI(MBB, II).addReg(Op0).addReg(Op1);
625     bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
626                                          II.ImplicitDefs[0], RC, RC);
627     if (!InsertedCopy)
628       ResultReg = 0;
629   }
630   return ResultReg;
631 }
632
633 unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
634                                    const TargetRegisterClass *RC,
635                                    unsigned Op0, uint64_t Imm) {
636   unsigned ResultReg = createResultReg(RC);
637   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
638
639   if (II.getNumDefs() >= 1)
640     BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
641   else {
642     BuildMI(MBB, II).addReg(Op0).addImm(Imm);
643     bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
644                                          II.ImplicitDefs[0], RC, RC);
645     if (!InsertedCopy)
646       ResultReg = 0;
647   }
648   return ResultReg;
649 }
650
651 unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
652                                    const TargetRegisterClass *RC,
653                                    unsigned Op0, ConstantFP *FPImm) {
654   unsigned ResultReg = createResultReg(RC);
655   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
656
657   if (II.getNumDefs() >= 1)
658     BuildMI(MBB, II, ResultReg).addReg(Op0).addFPImm(FPImm);
659   else {
660     BuildMI(MBB, II).addReg(Op0).addFPImm(FPImm);
661     bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
662                                          II.ImplicitDefs[0], RC, RC);
663     if (!InsertedCopy)
664       ResultReg = 0;
665   }
666   return ResultReg;
667 }
668
669 unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
670                                     const TargetRegisterClass *RC,
671                                     unsigned Op0, unsigned Op1, uint64_t Imm) {
672   unsigned ResultReg = createResultReg(RC);
673   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
674
675   if (II.getNumDefs() >= 1)
676     BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
677   else {
678     BuildMI(MBB, II).addReg(Op0).addReg(Op1).addImm(Imm);
679     bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
680                                          II.ImplicitDefs[0], RC, RC);
681     if (!InsertedCopy)
682       ResultReg = 0;
683   }
684   return ResultReg;
685 }
686
687 unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
688                                   const TargetRegisterClass *RC,
689                                   uint64_t Imm) {
690   unsigned ResultReg = createResultReg(RC);
691   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
692   
693   if (II.getNumDefs() >= 1)
694     BuildMI(MBB, II, ResultReg).addImm(Imm);
695   else {
696     BuildMI(MBB, II).addImm(Imm);
697     bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
698                                          II.ImplicitDefs[0], RC, RC);
699     if (!InsertedCopy)
700       ResultReg = 0;
701   }
702   return ResultReg;
703 }
704
705 unsigned FastISel::FastEmitInst_extractsubreg(unsigned Op0, uint32_t Idx) {
706   const TargetRegisterClass* RC = MRI.getRegClass(Op0);
707   const TargetRegisterClass* SRC = *(RC->subregclasses_begin()+Idx-1);
708   
709   unsigned ResultReg = createResultReg(SRC);
710   const TargetInstrDesc &II = TII.get(TargetInstrInfo::EXTRACT_SUBREG);
711   
712   if (II.getNumDefs() >= 1)
713     BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Idx);
714   else {
715     BuildMI(MBB, II).addReg(Op0).addImm(Idx);
716     bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
717                                          II.ImplicitDefs[0], RC, RC);
718     if (!InsertedCopy)
719       ResultReg = 0;
720   }
721   return ResultReg;
722 }