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