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