Hook up support for fast-isel of trunc instructions, using the newly working support...
[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, DenseMap<const Value*, unsigned> &ValueMap) {
25   unsigned &Reg = ValueMap[V];
26   if (Reg != 0)
27     return Reg;
28
29   MVT::SimpleValueType VT = TLI.getValueType(V->getType()).getSimpleVT();
30   if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
31     if (CI->getValue().getActiveBits() > 64)
32       return 0;
33     Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
34   } else if (ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
35     Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
36
37     if (!Reg) {
38       const APFloat &Flt = CF->getValueAPF();
39       MVT IntVT = TLI.getPointerTy();
40
41       uint64_t x[2];
42       uint32_t IntBitWidth = IntVT.getSizeInBits();
43       if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
44                                APFloat::rmTowardZero) != APFloat::opOK)
45         return 0;
46       APInt IntVal(IntBitWidth, 2, x);
47
48       unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
49                                        ISD::Constant, IntVal.getZExtValue());
50       if (IntegerReg == 0)
51         return 0;
52       Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
53       if (Reg == 0)
54         return 0;
55     }
56   }
57
58   return Reg;
59 }
60
61 /// SelectBinaryOp - Select and emit code for a binary operator instruction,
62 /// which has an opcode which directly corresponds to the given ISD opcode.
63 ///
64 bool FastISel::SelectBinaryOp(Instruction *I, ISD::NodeType ISDOpcode,
65                               DenseMap<const Value*, unsigned> &ValueMap) {
66   MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
67   if (VT == MVT::Other || !VT.isSimple())
68     // Unhandled type. Halt "fast" selection and bail.
69     return false;
70   // We only handle legal types. For example, on x86-32 the instruction
71   // selector contains all of the 64-bit instructions from x86-64,
72   // under the assumption that i64 won't be used if the target doesn't
73   // support it.
74   if (!TLI.isTypeLegal(VT))
75     return false;
76
77   unsigned Op0 = getRegForValue(I->getOperand(0), ValueMap);
78   if (Op0 == 0)
79     // Unhandled operand. Halt "fast" selection and bail.
80     return false;
81
82   // Check if the second operand is a constant and handle it appropriately.
83   if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
84     unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
85                                      ISDOpcode, Op0, CI->getZExtValue());
86     if (ResultReg != 0) {
87       // We successfully emitted code for the given LLVM Instruction.
88       ValueMap[I] = ResultReg;
89       return true;
90     }
91   }
92
93   // Check if the second operand is a constant float.
94   if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
95     unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
96                                      ISDOpcode, Op0, CF);
97     if (ResultReg != 0) {
98       // We successfully emitted code for the given LLVM Instruction.
99       ValueMap[I] = ResultReg;
100       return true;
101     }
102   }
103
104   unsigned Op1 = getRegForValue(I->getOperand(1), ValueMap);
105   if (Op1 == 0)
106     // Unhandled operand. Halt "fast" selection and bail.
107     return false;
108
109   // Now we have both operands in registers. Emit the instruction.
110   unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
111                                    ISDOpcode, Op0, Op1);
112   if (ResultReg == 0)
113     // Target-specific code wasn't able to find a machine opcode for
114     // the given ISD opcode and type. Halt "fast" selection and bail.
115     return false;
116
117   // We successfully emitted code for the given LLVM Instruction.
118   ValueMap[I] = ResultReg;
119   return true;
120 }
121
122 bool FastISel::SelectGetElementPtr(Instruction *I,
123                                    DenseMap<const Value*, unsigned> &ValueMap) {
124   unsigned N = getRegForValue(I->getOperand(0), ValueMap);
125   if (N == 0)
126     // Unhandled operand. Halt "fast" selection and bail.
127     return false;
128
129   const Type *Ty = I->getOperand(0)->getType();
130   MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
131   for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
132        OI != E; ++OI) {
133     Value *Idx = *OI;
134     if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
135       unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
136       if (Field) {
137         // N = N + Offset
138         uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
139         // FIXME: This can be optimized by combining the add with a
140         // subsequent one.
141         N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
142         if (N == 0)
143           // Unhandled operand. Halt "fast" selection and bail.
144           return false;
145       }
146       Ty = StTy->getElementType(Field);
147     } else {
148       Ty = cast<SequentialType>(Ty)->getElementType();
149
150       // If this is a constant subscript, handle it quickly.
151       if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
152         if (CI->getZExtValue() == 0) continue;
153         uint64_t Offs = 
154           TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
155         N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
156         if (N == 0)
157           // Unhandled operand. Halt "fast" selection and bail.
158           return false;
159         continue;
160       }
161       
162       // N = N + Idx * ElementSize;
163       uint64_t ElementSize = TD.getABITypeSize(Ty);
164       unsigned IdxN = getRegForValue(Idx, ValueMap);
165       if (IdxN == 0)
166         // Unhandled operand. Halt "fast" selection and bail.
167         return false;
168
169       // If the index is smaller or larger than intptr_t, truncate or extend
170       // it.
171       MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
172       if (IdxVT.bitsLT(VT))
173         IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::SIGN_EXTEND, IdxN);
174       else if (IdxVT.bitsGT(VT))
175         IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::TRUNCATE, IdxN);
176       if (IdxN == 0)
177         // Unhandled operand. Halt "fast" selection and bail.
178         return false;
179
180       if (ElementSize != 1) {
181         IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
182         if (IdxN == 0)
183           // Unhandled operand. Halt "fast" selection and bail.
184           return false;
185       }
186       N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
187       if (N == 0)
188         // Unhandled operand. Halt "fast" selection and bail.
189         return false;
190     }
191   }
192
193   // We successfully emitted code for the given LLVM Instruction.
194   ValueMap[I] = N;
195   return true;
196 }
197
198 bool FastISel::SelectCast(Instruction *I, ISD::NodeType Opcode,
199                           DenseMap<const Value*, unsigned> &ValueMap) {
200   MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
201   MVT DstVT = TLI.getValueType(I->getType());
202     
203   if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
204       DstVT == MVT::Other || !DstVT.isSimple() ||
205       !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
206     // Unhandled type. Halt "fast" selection and bail.
207     return false;
208     
209   unsigned InputReg = getRegForValue(I->getOperand(0), ValueMap);
210   if (!InputReg)
211     // Unhandled operand.  Halt "fast" selection and bail.
212     return false;
213     
214   unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
215                                   DstVT.getSimpleVT(),
216                                   Opcode,
217                                   InputReg);
218   if (!ResultReg)
219     return false;
220     
221   ValueMap[I] = ResultReg;
222   return true;
223 }
224
225 bool FastISel::SelectBitCast(Instruction *I,
226                              DenseMap<const Value*, unsigned> &ValueMap) {
227   // If the bitcast doesn't change the type, just use the operand value.
228   if (I->getType() == I->getOperand(0)->getType()) {
229     unsigned Reg = getRegForValue(I->getOperand(0), ValueMap);
230     if (Reg == 0)
231       return false;
232     ValueMap[I] = Reg;
233     return true;
234   }
235
236   // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
237   MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
238   MVT DstVT = TLI.getValueType(I->getType());
239   
240   if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
241       DstVT == MVT::Other || !DstVT.isSimple() ||
242       !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
243     // Unhandled type. Halt "fast" selection and bail.
244     return false;
245   
246   unsigned Op0 = getRegForValue(I->getOperand(0), ValueMap);
247   if (Op0 == 0)
248     // Unhandled operand. Halt "fast" selection and bail.
249     return false;
250   
251   // First, try to perform the bitcast by inserting a reg-reg copy.
252   unsigned ResultReg = 0;
253   if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
254     TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
255     TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
256     ResultReg = createResultReg(DstClass);
257     
258     bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
259                                          Op0, DstClass, SrcClass);
260     if (!InsertedCopy)
261       ResultReg = 0;
262   }
263   
264   // If the reg-reg copy failed, select a BIT_CONVERT opcode.
265   if (!ResultReg)
266     ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
267                            ISD::BIT_CONVERT, Op0);
268   
269   if (!ResultReg)
270     return false;
271   
272   ValueMap[I] = ResultReg;
273   return true;
274 }
275
276 BasicBlock::iterator
277 FastISel::SelectInstructions(BasicBlock::iterator Begin,
278                              BasicBlock::iterator End,
279                              DenseMap<const Value*, unsigned> &ValueMap,
280                              DenseMap<const BasicBlock*,
281                                       MachineBasicBlock *> &MBBMap,
282                              MachineBasicBlock *mbb) {
283   MBB = mbb;
284   BasicBlock::iterator I = Begin;
285
286   for (; I != End; ++I) {
287     switch (I->getOpcode()) {
288     case Instruction::Add: {
289       ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
290       if (!SelectBinaryOp(I, Opc, ValueMap))  return I; break;
291     }
292     case Instruction::Sub: {
293       ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
294       if (!SelectBinaryOp(I, Opc, ValueMap))  return I; break;
295     }
296     case Instruction::Mul: {
297       ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
298       if (!SelectBinaryOp(I, Opc, ValueMap))  return I; break;
299     }
300     case Instruction::SDiv:
301       if (!SelectBinaryOp(I, ISD::SDIV, ValueMap)) return I; break;
302     case Instruction::UDiv:
303       if (!SelectBinaryOp(I, ISD::UDIV, ValueMap)) return I; break;
304     case Instruction::FDiv:
305       if (!SelectBinaryOp(I, ISD::FDIV, ValueMap)) return I; break;
306     case Instruction::SRem:
307       if (!SelectBinaryOp(I, ISD::SREM, ValueMap)) return I; break;
308     case Instruction::URem:
309       if (!SelectBinaryOp(I, ISD::UREM, ValueMap)) return I; break;
310     case Instruction::FRem:
311       if (!SelectBinaryOp(I, ISD::FREM, ValueMap)) return I; break;
312     case Instruction::Shl:
313       if (!SelectBinaryOp(I, ISD::SHL, ValueMap)) return I; break;
314     case Instruction::LShr:
315       if (!SelectBinaryOp(I, ISD::SRL, ValueMap)) return I; break;
316     case Instruction::AShr:
317       if (!SelectBinaryOp(I, ISD::SRA, ValueMap)) return I; break;
318     case Instruction::And:
319       if (!SelectBinaryOp(I, ISD::AND, ValueMap)) return I; break;
320     case Instruction::Or:
321       if (!SelectBinaryOp(I, ISD::OR, ValueMap)) return I; break;
322     case Instruction::Xor:
323       if (!SelectBinaryOp(I, ISD::XOR, ValueMap)) return I; break;
324
325     case Instruction::GetElementPtr:
326       if (!SelectGetElementPtr(I, ValueMap)) return I;
327       break;
328
329     case Instruction::Br: {
330       BranchInst *BI = cast<BranchInst>(I);
331
332       if (BI->isUnconditional()) {
333         MachineFunction::iterator NextMBB =
334            next(MachineFunction::iterator(MBB));
335         BasicBlock *LLVMSucc = BI->getSuccessor(0);
336         MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
337
338         if (NextMBB != MF.end() && MSucc == NextMBB) {
339           // The unconditional fall-through case, which needs no instructions.
340         } else {
341           // The unconditional branch case.
342           TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
343         }
344         MBB->addSuccessor(MSucc);
345         break;
346       }
347
348       // Conditional branches are not handed yet.
349       // Halt "fast" selection and bail.
350       return I;
351     }
352
353     case Instruction::PHI:
354       // PHI nodes are already emitted.
355       break;
356       
357     case Instruction::BitCast:
358       if (!SelectBitCast(I, ValueMap)) return I; break;
359
360     case Instruction::FPToSI:
361       if (!SelectCast(I, ISD::FP_TO_SINT, ValueMap)) return I; 
362       break;
363     case Instruction::ZExt:
364       if (!SelectCast(I, ISD::ZERO_EXTEND, ValueMap)) return I;
365       break;
366     case Instruction::SExt:
367       if (!SelectCast(I, ISD::SIGN_EXTEND, ValueMap)) return I;
368       break;
369     case Instruction::Trunc:
370       if (!SelectCast(I, ISD::TRUNCATE, ValueMap)) return I;
371       break;
372     case Instruction::SIToFP:
373       if (!SelectCast(I, ISD::SINT_TO_FP, ValueMap)) return I;
374       break;
375
376     case Instruction::IntToPtr: // Deliberate fall-through.
377     case Instruction::PtrToInt: {
378       MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
379       MVT DstVT = TLI.getValueType(I->getType());
380       if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
381         if (ValueMap[I->getOperand(0)]) {
382           ValueMap[I] = ValueMap[I->getOperand(0)];
383           break;
384         } else
385           // Unhandled operand
386           return I;
387       } else if (DstVT.bitsGT(SrcVT)) {
388         if (!SelectCast(I, ISD::ZERO_EXTEND, ValueMap)) return I;
389         break;
390       } else {
391         // TODO: Handle SrcVT > DstVT, where truncation is needed.
392         return I;
393       }
394     }
395     
396     default:
397       // Unhandled instruction. Halt "fast" selection and bail.
398       return I;
399     }
400   }
401
402   return I;
403 }
404
405 FastISel::FastISel(MachineFunction &mf)
406   : MF(mf),
407     MRI(mf.getRegInfo()),
408     TM(mf.getTarget()),
409     TD(*TM.getTargetData()),
410     TII(*TM.getInstrInfo()),
411     TLI(*TM.getTargetLowering()) {
412 }
413
414 FastISel::~FastISel() {}
415
416 unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType, ISD::NodeType) {
417   return 0;
418 }
419
420 unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
421                               ISD::NodeType, unsigned /*Op0*/) {
422   return 0;
423 }
424
425 unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType, 
426                                ISD::NodeType, unsigned /*Op0*/,
427                                unsigned /*Op0*/) {
428   return 0;
429 }
430
431 unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
432                               ISD::NodeType, uint64_t /*Imm*/) {
433   return 0;
434 }
435
436 unsigned FastISel::FastEmit_f(MVT::SimpleValueType, MVT::SimpleValueType,
437                               ISD::NodeType, ConstantFP * /*FPImm*/) {
438   return 0;
439 }
440
441 unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
442                                ISD::NodeType, unsigned /*Op0*/,
443                                uint64_t /*Imm*/) {
444   return 0;
445 }
446
447 unsigned FastISel::FastEmit_rf(MVT::SimpleValueType, MVT::SimpleValueType,
448                                ISD::NodeType, unsigned /*Op0*/,
449                                ConstantFP * /*FPImm*/) {
450   return 0;
451 }
452
453 unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
454                                 ISD::NodeType,
455                                 unsigned /*Op0*/, unsigned /*Op1*/,
456                                 uint64_t /*Imm*/) {
457   return 0;
458 }
459
460 /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
461 /// to emit an instruction with an immediate operand using FastEmit_ri.
462 /// If that fails, it materializes the immediate into a register and try
463 /// FastEmit_rr instead.
464 unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
465                                 unsigned Op0, uint64_t Imm,
466                                 MVT::SimpleValueType ImmType) {
467   // First check if immediate type is legal. If not, we can't use the ri form.
468   unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
469   if (ResultReg != 0)
470     return ResultReg;
471   unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
472   if (MaterialReg == 0)
473     return 0;
474   return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
475 }
476
477 /// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
478 /// to emit an instruction with a floating-point immediate operand using
479 /// FastEmit_rf. If that fails, it materializes the immediate into a register
480 /// and try FastEmit_rr instead.
481 unsigned FastISel::FastEmit_rf_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
482                                 unsigned Op0, ConstantFP *FPImm,
483                                 MVT::SimpleValueType ImmType) {
484   // First check if immediate type is legal. If not, we can't use the rf form.
485   unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
486   if (ResultReg != 0)
487     return ResultReg;
488
489   // Materialize the constant in a register.
490   unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
491   if (MaterialReg == 0) {
492     // If the target doesn't have a way to directly enter a floating-point
493     // value into a register, use an alternate approach.
494     // TODO: The current approach only supports floating-point constants
495     // that can be constructed by conversion from integer values. This should
496     // be replaced by code that creates a load from a constant-pool entry,
497     // which will require some target-specific work.
498     const APFloat &Flt = FPImm->getValueAPF();
499     MVT IntVT = TLI.getPointerTy();
500
501     uint64_t x[2];
502     uint32_t IntBitWidth = IntVT.getSizeInBits();
503     if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
504                              APFloat::rmTowardZero) != APFloat::opOK)
505       return 0;
506     APInt IntVal(IntBitWidth, 2, x);
507
508     unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
509                                      ISD::Constant, IntVal.getZExtValue());
510     if (IntegerReg == 0)
511       return 0;
512     MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
513                              ISD::SINT_TO_FP, IntegerReg);
514     if (MaterialReg == 0)
515       return 0;
516   }
517   return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
518 }
519
520 unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
521   return MRI.createVirtualRegister(RC);
522 }
523
524 unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
525                                  const TargetRegisterClass* RC) {
526   unsigned ResultReg = createResultReg(RC);
527   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
528
529   BuildMI(MBB, II, ResultReg);
530   return ResultReg;
531 }
532
533 unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
534                                   const TargetRegisterClass *RC,
535                                   unsigned Op0) {
536   unsigned ResultReg = createResultReg(RC);
537   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
538
539   BuildMI(MBB, II, ResultReg).addReg(Op0);
540   return ResultReg;
541 }
542
543 unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
544                                    const TargetRegisterClass *RC,
545                                    unsigned Op0, unsigned Op1) {
546   unsigned ResultReg = createResultReg(RC);
547   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
548
549   BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1);
550   return ResultReg;
551 }
552
553 unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
554                                    const TargetRegisterClass *RC,
555                                    unsigned Op0, uint64_t Imm) {
556   unsigned ResultReg = createResultReg(RC);
557   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
558
559   BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
560   return ResultReg;
561 }
562
563 unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
564                                    const TargetRegisterClass *RC,
565                                    unsigned Op0, ConstantFP *FPImm) {
566   unsigned ResultReg = createResultReg(RC);
567   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
568
569   BuildMI(MBB, II, ResultReg).addReg(Op0).addFPImm(FPImm);
570   return ResultReg;
571 }
572
573 unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
574                                     const TargetRegisterClass *RC,
575                                     unsigned Op0, unsigned Op1, uint64_t Imm) {
576   unsigned ResultReg = createResultReg(RC);
577   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
578
579   BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
580   return ResultReg;
581 }
582
583 unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
584                                   const TargetRegisterClass *RC,
585                                   uint64_t Imm) {
586   unsigned ResultReg = createResultReg(RC);
587   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
588   
589   BuildMI(MBB, II, ResultReg).addImm(Imm);
590   return ResultReg;
591 }
592
593 unsigned FastISel::FastEmitInst_extractsubreg(unsigned Op0, uint32_t Idx) {
594   const TargetRegisterClass* RC = MRI.getRegClass(Op0);
595   const TargetRegisterClass* SRC = *(RC->subregclasses_begin()+Idx-1);
596   
597   unsigned ResultReg = createResultReg(SRC);
598   const TargetInstrDesc &II = TII.get(TargetInstrInfo::EXTRACT_SUBREG);
599   
600   BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Idx);
601   return ResultReg;
602 }