GEPs with all zero indices are trivially coalesced by fast-isel. For example,
[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 // "Fast" instruction selection is designed to emit very poor code quickly.
13 // Also, it is not designed to be able to do much lowering, so most illegal
14 // types (e.g. i64 on 32-bit targets) and operations are not supported.  It is
15 // also not intended to be able to do much optimization, except in a few cases
16 // where doing optimizations reduces overall compile time.  For example, folding
17 // constants into immediate fields is often done, because it's cheap and it
18 // reduces the number of instructions later phases have to examine.
19 //
20 // "Fast" instruction selection is able to fail gracefully and transfer
21 // control to the SelectionDAG selector for operations that it doesn't
22 // support.  In many cases, this allows us to avoid duplicating a lot of
23 // the complicated lowering logic that SelectionDAG currently has.
24 //
25 // The intended use for "fast" instruction selection is "-O0" mode
26 // compilation, where the quality of the generated code is irrelevant when
27 // weighed against the speed at which the code can be generated.  Also,
28 // at -O0, the LLVM optimizers are not running, and this makes the
29 // compile time of codegen a much higher portion of the overall compile
30 // time.  Despite its limitations, "fast" instruction selection is able to
31 // handle enough code on its own to provide noticeable overall speedups
32 // in -O0 compiles.
33 //
34 // Basic operations are supported in a target-independent way, by reading
35 // the same instruction descriptions that the SelectionDAG selector reads,
36 // and identifying simple arithmetic operations that can be directly selected
37 // from simple operators.  More complicated operations currently require
38 // target-specific code.
39 //
40 //===----------------------------------------------------------------------===//
41
42 #include "llvm/Function.h"
43 #include "llvm/GlobalVariable.h"
44 #include "llvm/Instructions.h"
45 #include "llvm/IntrinsicInst.h"
46 #include "llvm/Operator.h"
47 #include "llvm/CodeGen/Analysis.h"
48 #include "llvm/CodeGen/FastISel.h"
49 #include "llvm/CodeGen/FunctionLoweringInfo.h"
50 #include "llvm/CodeGen/MachineInstrBuilder.h"
51 #include "llvm/CodeGen/MachineModuleInfo.h"
52 #include "llvm/CodeGen/MachineRegisterInfo.h"
53 #include "llvm/Analysis/DebugInfo.h"
54 #include "llvm/Analysis/Loads.h"
55 #include "llvm/Target/TargetData.h"
56 #include "llvm/Target/TargetInstrInfo.h"
57 #include "llvm/Target/TargetLowering.h"
58 #include "llvm/Target/TargetMachine.h"
59 #include "llvm/Support/ErrorHandling.h"
60 #include "llvm/Support/Debug.h"
61 using namespace llvm;
62
63 /// startNewBlock - Set the current block to which generated machine
64 /// instructions will be appended, and clear the local CSE map.
65 ///
66 void FastISel::startNewBlock() {
67   LocalValueMap.clear();
68
69   EmitStartPt = 0;
70
71   // Advance the emit start point past any EH_LABEL instructions.
72   MachineBasicBlock::iterator
73     I = FuncInfo.MBB->begin(), E = FuncInfo.MBB->end();
74   while (I != E && I->getOpcode() == TargetOpcode::EH_LABEL) {
75     EmitStartPt = I;
76     ++I;
77   }
78   LastLocalValue = EmitStartPt;
79 }
80
81 void FastISel::flushLocalValueMap() {
82   LocalValueMap.clear();
83   LastLocalValue = EmitStartPt;
84   recomputeInsertPt();
85 }
86
87 bool FastISel::hasTrivialKill(const Value *V) const {
88   // Don't consider constants or arguments to have trivial kills.
89   const Instruction *I = dyn_cast<Instruction>(V);
90   if (!I)
91     return false;
92
93   // No-op casts are trivially coalesced by fast-isel.
94   if (const CastInst *Cast = dyn_cast<CastInst>(I))
95     if (Cast->isNoopCast(TD.getIntPtrType(Cast->getContext())) &&
96         !hasTrivialKill(Cast->getOperand(0)))
97       return false;
98
99   // GEPs with all zero indices are trivially coalesced by fast-isel.
100   if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I))
101     if (GEP->hasAllZeroIndices() && !hasTrivialKill(GEP->getOperand(0)))
102       return false;
103
104   // Only instructions with a single use in the same basic block are considered
105   // to have trivial kills.
106   return I->hasOneUse() &&
107          !(I->getOpcode() == Instruction::BitCast ||
108            I->getOpcode() == Instruction::PtrToInt ||
109            I->getOpcode() == Instruction::IntToPtr) &&
110          cast<Instruction>(*I->use_begin())->getParent() == I->getParent();
111 }
112
113 unsigned FastISel::getRegForValue(const Value *V) {
114   EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
115   // Don't handle non-simple values in FastISel.
116   if (!RealVT.isSimple())
117     return 0;
118
119   // Ignore illegal types. We must do this before looking up the value
120   // in ValueMap because Arguments are given virtual registers regardless
121   // of whether FastISel can handle them.
122   MVT VT = RealVT.getSimpleVT();
123   if (!TLI.isTypeLegal(VT)) {
124     // Handle integer promotions, though, because they're common and easy.
125     if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
126       VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
127     else
128       return 0;
129   }
130
131   // Look up the value to see if we already have a register for it. We
132   // cache values defined by Instructions across blocks, and other values
133   // only locally. This is because Instructions already have the SSA
134   // def-dominates-use requirement enforced.
135   DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
136   if (I != FuncInfo.ValueMap.end())
137     return I->second;
138
139   unsigned Reg = LocalValueMap[V];
140   if (Reg != 0)
141     return Reg;
142
143   // In bottom-up mode, just create the virtual register which will be used
144   // to hold the value. It will be materialized later.
145   if (isa<Instruction>(V) &&
146       (!isa<AllocaInst>(V) ||
147        !FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(V))))
148     return FuncInfo.InitializeRegForValue(V);
149
150   SavePoint SaveInsertPt = enterLocalValueArea();
151
152   // Materialize the value in a register. Emit any instructions in the
153   // local value area.
154   Reg = materializeRegForValue(V, VT);
155
156   leaveLocalValueArea(SaveInsertPt);
157
158   return Reg;
159 }
160
161 /// materializeRegForValue - Helper for getRegForValue. This function is
162 /// called when the value isn't already available in a register and must
163 /// be materialized with new instructions.
164 unsigned FastISel::materializeRegForValue(const Value *V, MVT VT) {
165   unsigned Reg = 0;
166
167   if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
168     if (CI->getValue().getActiveBits() <= 64)
169       Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
170   } else if (isa<AllocaInst>(V)) {
171     Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
172   } else if (isa<ConstantPointerNull>(V)) {
173     // Translate this as an integer zero so that it can be
174     // local-CSE'd with actual integer zeros.
175     Reg =
176       getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext())));
177   } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
178     if (CF->isNullValue()) {
179       Reg = TargetMaterializeFloatZero(CF);
180     } else {
181       // Try to emit the constant directly.
182       Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
183     }
184
185     if (!Reg) {
186       // Try to emit the constant by using an integer constant with a cast.
187       const APFloat &Flt = CF->getValueAPF();
188       EVT IntVT = TLI.getPointerTy();
189
190       uint64_t x[2];
191       uint32_t IntBitWidth = IntVT.getSizeInBits();
192       bool isExact;
193       (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
194                                 APFloat::rmTowardZero, &isExact);
195       if (isExact) {
196         APInt IntVal(IntBitWidth, x);
197
198         unsigned IntegerReg =
199           getRegForValue(ConstantInt::get(V->getContext(), IntVal));
200         if (IntegerReg != 0)
201           Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP,
202                            IntegerReg, /*Kill=*/false);
203       }
204     }
205   } else if (const Operator *Op = dyn_cast<Operator>(V)) {
206     if (!SelectOperator(Op, Op->getOpcode()))
207       if (!isa<Instruction>(Op) ||
208           !TargetSelectInstruction(cast<Instruction>(Op)))
209         return 0;
210     Reg = lookUpRegForValue(Op);
211   } else if (isa<UndefValue>(V)) {
212     Reg = createResultReg(TLI.getRegClassFor(VT));
213     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
214             TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
215   }
216
217   // If target-independent code couldn't handle the value, give target-specific
218   // code a try.
219   if (!Reg && isa<Constant>(V))
220     Reg = TargetMaterializeConstant(cast<Constant>(V));
221
222   // Don't cache constant materializations in the general ValueMap.
223   // To do so would require tracking what uses they dominate.
224   if (Reg != 0) {
225     LocalValueMap[V] = Reg;
226     LastLocalValue = MRI.getVRegDef(Reg);
227   }
228   return Reg;
229 }
230
231 unsigned FastISel::lookUpRegForValue(const Value *V) {
232   // Look up the value to see if we already have a register for it. We
233   // cache values defined by Instructions across blocks, and other values
234   // only locally. This is because Instructions already have the SSA
235   // def-dominates-use requirement enforced.
236   DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(V);
237   if (I != FuncInfo.ValueMap.end())
238     return I->second;
239   return LocalValueMap[V];
240 }
241
242 /// UpdateValueMap - Update the value map to include the new mapping for this
243 /// instruction, or insert an extra copy to get the result in a previous
244 /// determined register.
245 /// NOTE: This is only necessary because we might select a block that uses
246 /// a value before we select the block that defines the value.  It might be
247 /// possible to fix this by selecting blocks in reverse postorder.
248 void FastISel::UpdateValueMap(const Value *I, unsigned Reg, unsigned NumRegs) {
249   if (!isa<Instruction>(I)) {
250     LocalValueMap[I] = Reg;
251     return;
252   }
253
254   unsigned &AssignedReg = FuncInfo.ValueMap[I];
255   if (AssignedReg == 0)
256     // Use the new register.
257     AssignedReg = Reg;
258   else if (Reg != AssignedReg) {
259     // Arrange for uses of AssignedReg to be replaced by uses of Reg.
260     for (unsigned i = 0; i < NumRegs; i++)
261       FuncInfo.RegFixups[AssignedReg+i] = Reg+i;
262
263     AssignedReg = Reg;
264   }
265 }
266
267 std::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
268   unsigned IdxN = getRegForValue(Idx);
269   if (IdxN == 0)
270     // Unhandled operand. Halt "fast" selection and bail.
271     return std::pair<unsigned, bool>(0, false);
272
273   bool IdxNIsKill = hasTrivialKill(Idx);
274
275   // If the index is smaller or larger than intptr_t, truncate or extend it.
276   MVT PtrVT = TLI.getPointerTy();
277   EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
278   if (IdxVT.bitsLT(PtrVT)) {
279     IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND,
280                       IdxN, IdxNIsKill);
281     IdxNIsKill = true;
282   }
283   else if (IdxVT.bitsGT(PtrVT)) {
284     IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE,
285                       IdxN, IdxNIsKill);
286     IdxNIsKill = true;
287   }
288   return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
289 }
290
291 void FastISel::recomputeInsertPt() {
292   if (getLastLocalValue()) {
293     FuncInfo.InsertPt = getLastLocalValue();
294     FuncInfo.MBB = FuncInfo.InsertPt->getParent();
295     ++FuncInfo.InsertPt;
296   } else
297     FuncInfo.InsertPt = FuncInfo.MBB->getFirstNonPHI();
298
299   // Now skip past any EH_LABELs, which must remain at the beginning.
300   while (FuncInfo.InsertPt != FuncInfo.MBB->end() &&
301          FuncInfo.InsertPt->getOpcode() == TargetOpcode::EH_LABEL)
302     ++FuncInfo.InsertPt;
303 }
304
305 FastISel::SavePoint FastISel::enterLocalValueArea() {
306   MachineBasicBlock::iterator OldInsertPt = FuncInfo.InsertPt;
307   DebugLoc OldDL = DL;
308   recomputeInsertPt();
309   DL = DebugLoc();
310   SavePoint SP = { OldInsertPt, OldDL };
311   return SP;
312 }
313
314 void FastISel::leaveLocalValueArea(SavePoint OldInsertPt) {
315   if (FuncInfo.InsertPt != FuncInfo.MBB->begin())
316     LastLocalValue = llvm::prior(FuncInfo.InsertPt);
317
318   // Restore the previous insert position.
319   FuncInfo.InsertPt = OldInsertPt.InsertPt;
320   DL = OldInsertPt.DL;
321 }
322
323 /// SelectBinaryOp - Select and emit code for a binary operator instruction,
324 /// which has an opcode which directly corresponds to the given ISD opcode.
325 ///
326 bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
327   EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
328   if (VT == MVT::Other || !VT.isSimple())
329     // Unhandled type. Halt "fast" selection and bail.
330     return false;
331
332   // We only handle legal types. For example, on x86-32 the instruction
333   // selector contains all of the 64-bit instructions from x86-64,
334   // under the assumption that i64 won't be used if the target doesn't
335   // support it.
336   if (!TLI.isTypeLegal(VT)) {
337     // MVT::i1 is special. Allow AND, OR, or XOR because they
338     // don't require additional zeroing, which makes them easy.
339     if (VT == MVT::i1 &&
340         (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
341          ISDOpcode == ISD::XOR))
342       VT = TLI.getTypeToTransformTo(I->getContext(), VT);
343     else
344       return false;
345   }
346
347   // Check if the first operand is a constant, and handle it as "ri".  At -O0,
348   // we don't have anything that canonicalizes operand order.
349   if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(0)))
350     if (isa<Instruction>(I) && cast<Instruction>(I)->isCommutative()) {
351       unsigned Op1 = getRegForValue(I->getOperand(1));
352       if (Op1 == 0) return false;
353
354       bool Op1IsKill = hasTrivialKill(I->getOperand(1));
355
356       unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op1,
357                                         Op1IsKill, CI->getZExtValue(),
358                                         VT.getSimpleVT());
359       if (ResultReg == 0) return false;
360
361       // We successfully emitted code for the given LLVM Instruction.
362       UpdateValueMap(I, ResultReg);
363       return true;
364     }
365
366
367   unsigned Op0 = getRegForValue(I->getOperand(0));
368   if (Op0 == 0)   // Unhandled operand. Halt "fast" selection and bail.
369     return false;
370
371   bool Op0IsKill = hasTrivialKill(I->getOperand(0));
372
373   // Check if the second operand is a constant and handle it appropriately.
374   if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
375     uint64_t Imm = CI->getZExtValue();
376
377     // Transform "sdiv exact X, 8" -> "sra X, 3".
378     if (ISDOpcode == ISD::SDIV && isa<BinaryOperator>(I) &&
379         cast<BinaryOperator>(I)->isExact() &&
380         isPowerOf2_64(Imm)) {
381       Imm = Log2_64(Imm);
382       ISDOpcode = ISD::SRA;
383     }
384
385     unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0,
386                                       Op0IsKill, Imm, VT.getSimpleVT());
387     if (ResultReg == 0) return false;
388
389     // We successfully emitted code for the given LLVM Instruction.
390     UpdateValueMap(I, ResultReg);
391     return true;
392   }
393
394   // Check if the second operand is a constant float.
395   if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
396     unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
397                                      ISDOpcode, Op0, Op0IsKill, CF);
398     if (ResultReg != 0) {
399       // We successfully emitted code for the given LLVM Instruction.
400       UpdateValueMap(I, ResultReg);
401       return true;
402     }
403   }
404
405   unsigned Op1 = getRegForValue(I->getOperand(1));
406   if (Op1 == 0)
407     // Unhandled operand. Halt "fast" selection and bail.
408     return false;
409
410   bool Op1IsKill = hasTrivialKill(I->getOperand(1));
411
412   // Now we have both operands in registers. Emit the instruction.
413   unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
414                                    ISDOpcode,
415                                    Op0, Op0IsKill,
416                                    Op1, Op1IsKill);
417   if (ResultReg == 0)
418     // Target-specific code wasn't able to find a machine opcode for
419     // the given ISD opcode and type. Halt "fast" selection and bail.
420     return false;
421
422   // We successfully emitted code for the given LLVM Instruction.
423   UpdateValueMap(I, ResultReg);
424   return true;
425 }
426
427 bool FastISel::SelectGetElementPtr(const User *I) {
428   unsigned N = getRegForValue(I->getOperand(0));
429   if (N == 0)
430     // Unhandled operand. Halt "fast" selection and bail.
431     return false;
432
433   bool NIsKill = hasTrivialKill(I->getOperand(0));
434
435   Type *Ty = I->getOperand(0)->getType();
436   MVT VT = TLI.getPointerTy();
437   for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1,
438        E = I->op_end(); OI != E; ++OI) {
439     const Value *Idx = *OI;
440     if (StructType *StTy = dyn_cast<StructType>(Ty)) {
441       unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
442       if (Field) {
443         // N = N + Offset
444         uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
445         // FIXME: This can be optimized by combining the add with a
446         // subsequent one.
447         N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, Offs, VT);
448         if (N == 0)
449           // Unhandled operand. Halt "fast" selection and bail.
450           return false;
451         NIsKill = true;
452       }
453       Ty = StTy->getElementType(Field);
454     } else {
455       Ty = cast<SequentialType>(Ty)->getElementType();
456
457       // If this is a constant subscript, handle it quickly.
458       if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
459         if (CI->isZero()) continue;
460         uint64_t Offs =
461           TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
462         N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, Offs, VT);
463         if (N == 0)
464           // Unhandled operand. Halt "fast" selection and bail.
465           return false;
466         NIsKill = true;
467         continue;
468       }
469
470       // N = N + Idx * ElementSize;
471       uint64_t ElementSize = TD.getTypeAllocSize(Ty);
472       std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
473       unsigned IdxN = Pair.first;
474       bool IdxNIsKill = Pair.second;
475       if (IdxN == 0)
476         // Unhandled operand. Halt "fast" selection and bail.
477         return false;
478
479       if (ElementSize != 1) {
480         IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
481         if (IdxN == 0)
482           // Unhandled operand. Halt "fast" selection and bail.
483           return false;
484         IdxNIsKill = true;
485       }
486       N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
487       if (N == 0)
488         // Unhandled operand. Halt "fast" selection and bail.
489         return false;
490     }
491   }
492
493   // We successfully emitted code for the given LLVM Instruction.
494   UpdateValueMap(I, N);
495   return true;
496 }
497
498 bool FastISel::SelectCall(const User *I) {
499   const CallInst *Call = cast<CallInst>(I);
500
501   // Handle simple inline asms.
502   if (const InlineAsm *IA = dyn_cast<InlineAsm>(Call->getCalledValue())) {
503     // Don't attempt to handle constraints.
504     if (!IA->getConstraintString().empty())
505       return false;
506
507     unsigned ExtraInfo = 0;
508     if (IA->hasSideEffects())
509       ExtraInfo |= InlineAsm::Extra_HasSideEffects;
510     if (IA->isAlignStack())
511       ExtraInfo |= InlineAsm::Extra_IsAlignStack;
512
513     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
514             TII.get(TargetOpcode::INLINEASM))
515       .addExternalSymbol(IA->getAsmString().c_str())
516       .addImm(ExtraInfo);
517     return true;
518   }
519
520   const Function *F = Call->getCalledFunction();
521   if (!F) return false;
522
523   // Handle selected intrinsic function calls.
524   switch (F->getIntrinsicID()) {
525   default: break;
526   case Intrinsic::dbg_declare: {
527     const DbgDeclareInst *DI = cast<DbgDeclareInst>(Call);
528     if (!DIVariable(DI->getVariable()).Verify() ||
529         !FuncInfo.MF->getMMI().hasDebugInfo())
530       return true;
531
532     const Value *Address = DI->getAddress();
533     if (!Address || isa<UndefValue>(Address) || isa<AllocaInst>(Address))
534       return true;
535
536     unsigned Reg = 0;
537     unsigned Offset = 0;
538     if (const Argument *Arg = dyn_cast<Argument>(Address)) {
539       // Some arguments' frame index is recorded during argument lowering.
540       Offset = FuncInfo.getArgumentFrameIndex(Arg);
541       if (Offset)
542         Reg = TRI.getFrameRegister(*FuncInfo.MF);
543     }
544     if (!Reg)
545       Reg = getRegForValue(Address);
546
547     if (Reg)
548       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
549               TII.get(TargetOpcode::DBG_VALUE))
550         .addReg(Reg, RegState::Debug).addImm(Offset)
551         .addMetadata(DI->getVariable());
552     return true;
553   }
554   case Intrinsic::dbg_value: {
555     // This form of DBG_VALUE is target-independent.
556     const DbgValueInst *DI = cast<DbgValueInst>(Call);
557     const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
558     const Value *V = DI->getValue();
559     if (!V) {
560       // Currently the optimizer can produce this; insert an undef to
561       // help debugging.  Probably the optimizer should not do this.
562       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
563         .addReg(0U).addImm(DI->getOffset())
564         .addMetadata(DI->getVariable());
565     } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
566       if (CI->getBitWidth() > 64)
567         BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
568           .addCImm(CI).addImm(DI->getOffset())
569           .addMetadata(DI->getVariable());
570       else 
571         BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
572           .addImm(CI->getZExtValue()).addImm(DI->getOffset())
573           .addMetadata(DI->getVariable());
574     } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
575       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
576         .addFPImm(CF).addImm(DI->getOffset())
577         .addMetadata(DI->getVariable());
578     } else if (unsigned Reg = lookUpRegForValue(V)) {
579       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
580         .addReg(Reg, RegState::Debug).addImm(DI->getOffset())
581         .addMetadata(DI->getVariable());
582     } else {
583       // We can't yet handle anything else here because it would require
584       // generating code, thus altering codegen because of debug info.
585       DEBUG(dbgs() << "Dropping debug info for " << DI);
586     }
587     return true;
588   }
589   case Intrinsic::eh_exception: {
590     EVT VT = TLI.getValueType(Call->getType());
591     if (TLI.getOperationAction(ISD::EXCEPTIONADDR, VT)!=TargetLowering::Expand)
592       break;
593
594     assert(FuncInfo.MBB->isLandingPad() &&
595            "Call to eh.exception not in landing pad!");
596     unsigned Reg = TLI.getExceptionAddressRegister();
597     const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
598     unsigned ResultReg = createResultReg(RC);
599     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
600             ResultReg).addReg(Reg);
601     UpdateValueMap(Call, ResultReg);
602     return true;
603   }
604   case Intrinsic::eh_selector: {
605     EVT VT = TLI.getValueType(Call->getType());
606     if (TLI.getOperationAction(ISD::EHSELECTION, VT) != TargetLowering::Expand)
607       break;
608     if (FuncInfo.MBB->isLandingPad())
609       AddCatchInfo(*Call, &FuncInfo.MF->getMMI(), FuncInfo.MBB);
610     else {
611 #ifndef NDEBUG
612       FuncInfo.CatchInfoLost.insert(Call);
613 #endif
614       // FIXME: Mark exception selector register as live in.  Hack for PR1508.
615       unsigned Reg = TLI.getExceptionSelectorRegister();
616       if (Reg) FuncInfo.MBB->addLiveIn(Reg);
617     }
618
619     unsigned Reg = TLI.getExceptionSelectorRegister();
620     EVT SrcVT = TLI.getPointerTy();
621     const TargetRegisterClass *RC = TLI.getRegClassFor(SrcVT);
622     unsigned ResultReg = createResultReg(RC);
623     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
624             ResultReg).addReg(Reg);
625
626     bool ResultRegIsKill = hasTrivialKill(Call);
627
628     // Cast the register to the type of the selector.
629     if (SrcVT.bitsGT(MVT::i32))
630       ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32, ISD::TRUNCATE,
631                              ResultReg, ResultRegIsKill);
632     else if (SrcVT.bitsLT(MVT::i32))
633       ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32,
634                              ISD::SIGN_EXTEND, ResultReg, ResultRegIsKill);
635     if (ResultReg == 0)
636       // Unhandled operand. Halt "fast" selection and bail.
637       return false;
638
639     UpdateValueMap(Call, ResultReg);
640
641     return true;
642   }
643   case Intrinsic::objectsize: {
644     ConstantInt *CI = cast<ConstantInt>(Call->getArgOperand(1));
645     unsigned long long Res = CI->isZero() ? -1ULL : 0;
646     Constant *ResCI = ConstantInt::get(Call->getType(), Res);
647     unsigned ResultReg = getRegForValue(ResCI);
648     if (ResultReg == 0)
649       return false;
650     UpdateValueMap(Call, ResultReg);
651     return true;
652   }
653   }
654
655   // Usually, it does not make sense to initialize a value,
656   // make an unrelated function call and use the value, because
657   // it tends to be spilled on the stack. So, we move the pointer
658   // to the last local value to the beginning of the block, so that
659   // all the values which have already been materialized,
660   // appear after the call. It also makes sense to skip intrinsics
661   // since they tend to be inlined.
662   if (!isa<IntrinsicInst>(F))
663     flushLocalValueMap();
664
665   // An arbitrary call. Bail.
666   return false;
667 }
668
669 bool FastISel::SelectCast(const User *I, unsigned Opcode) {
670   EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
671   EVT DstVT = TLI.getValueType(I->getType());
672
673   if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
674       DstVT == MVT::Other || !DstVT.isSimple())
675     // Unhandled type. Halt "fast" selection and bail.
676     return false;
677
678   // Check if the destination type is legal.
679   if (!TLI.isTypeLegal(DstVT))
680     return false;
681
682   // Check if the source operand is legal.
683   if (!TLI.isTypeLegal(SrcVT))
684     return false;
685
686   unsigned InputReg = getRegForValue(I->getOperand(0));
687   if (!InputReg)
688     // Unhandled operand.  Halt "fast" selection and bail.
689     return false;
690
691   bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
692
693   unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
694                                   DstVT.getSimpleVT(),
695                                   Opcode,
696                                   InputReg, InputRegIsKill);
697   if (!ResultReg)
698     return false;
699
700   UpdateValueMap(I, ResultReg);
701   return true;
702 }
703
704 bool FastISel::SelectBitCast(const User *I) {
705   // If the bitcast doesn't change the type, just use the operand value.
706   if (I->getType() == I->getOperand(0)->getType()) {
707     unsigned Reg = getRegForValue(I->getOperand(0));
708     if (Reg == 0)
709       return false;
710     UpdateValueMap(I, Reg);
711     return true;
712   }
713
714   // Bitcasts of other values become reg-reg copies or BITCAST operators.
715   EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
716   EVT DstVT = TLI.getValueType(I->getType());
717
718   if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
719       DstVT == MVT::Other || !DstVT.isSimple() ||
720       !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
721     // Unhandled type. Halt "fast" selection and bail.
722     return false;
723
724   unsigned Op0 = getRegForValue(I->getOperand(0));
725   if (Op0 == 0)
726     // Unhandled operand. Halt "fast" selection and bail.
727     return false;
728
729   bool Op0IsKill = hasTrivialKill(I->getOperand(0));
730
731   // First, try to perform the bitcast by inserting a reg-reg copy.
732   unsigned ResultReg = 0;
733   if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
734     TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
735     TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
736     // Don't attempt a cross-class copy. It will likely fail.
737     if (SrcClass == DstClass) {
738       ResultReg = createResultReg(DstClass);
739       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
740               ResultReg).addReg(Op0);
741     }
742   }
743
744   // If the reg-reg copy failed, select a BITCAST opcode.
745   if (!ResultReg)
746     ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
747                            ISD::BITCAST, Op0, Op0IsKill);
748
749   if (!ResultReg)
750     return false;
751
752   UpdateValueMap(I, ResultReg);
753   return true;
754 }
755
756 bool
757 FastISel::SelectInstruction(const Instruction *I) {
758   // Just before the terminator instruction, insert instructions to
759   // feed PHI nodes in successor blocks.
760   if (isa<TerminatorInst>(I))
761     if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
762       return false;
763
764   DL = I->getDebugLoc();
765
766   // First, try doing target-independent selection.
767   if (SelectOperator(I, I->getOpcode())) {
768     DL = DebugLoc();
769     return true;
770   }
771
772   // Next, try calling the target to attempt to handle the instruction.
773   if (TargetSelectInstruction(I)) {
774     DL = DebugLoc();
775     return true;
776   }
777
778   DL = DebugLoc();
779   return false;
780 }
781
782 /// FastEmitBranch - Emit an unconditional branch to the given block,
783 /// unless it is the immediate (fall-through) successor, and update
784 /// the CFG.
785 void
786 FastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DL) {
787   if (FuncInfo.MBB->isLayoutSuccessor(MSucc)) {
788     // The unconditional fall-through case, which needs no instructions.
789   } else {
790     // The unconditional branch case.
791     TII.InsertBranch(*FuncInfo.MBB, MSucc, NULL,
792                      SmallVector<MachineOperand, 0>(), DL);
793   }
794   FuncInfo.MBB->addSuccessor(MSucc);
795 }
796
797 /// SelectFNeg - Emit an FNeg operation.
798 ///
799 bool
800 FastISel::SelectFNeg(const User *I) {
801   unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
802   if (OpReg == 0) return false;
803
804   bool OpRegIsKill = hasTrivialKill(I);
805
806   // If the target has ISD::FNEG, use it.
807   EVT VT = TLI.getValueType(I->getType());
808   unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
809                                   ISD::FNEG, OpReg, OpRegIsKill);
810   if (ResultReg != 0) {
811     UpdateValueMap(I, ResultReg);
812     return true;
813   }
814
815   // Bitcast the value to integer, twiddle the sign bit with xor,
816   // and then bitcast it back to floating-point.
817   if (VT.getSizeInBits() > 64) return false;
818   EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
819   if (!TLI.isTypeLegal(IntVT))
820     return false;
821
822   unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
823                                ISD::BITCAST, OpReg, OpRegIsKill);
824   if (IntReg == 0)
825     return false;
826
827   unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR,
828                                        IntReg, /*Kill=*/true,
829                                        UINT64_C(1) << (VT.getSizeInBits()-1),
830                                        IntVT.getSimpleVT());
831   if (IntResultReg == 0)
832     return false;
833
834   ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
835                          ISD::BITCAST, IntResultReg, /*Kill=*/true);
836   if (ResultReg == 0)
837     return false;
838
839   UpdateValueMap(I, ResultReg);
840   return true;
841 }
842
843 bool
844 FastISel::SelectExtractValue(const User *U) {
845   const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(U);
846   if (!EVI)
847     return false;
848
849   // Make sure we only try to handle extracts with a legal result.  But also
850   // allow i1 because it's easy.
851   EVT RealVT = TLI.getValueType(EVI->getType(), /*AllowUnknown=*/true);
852   if (!RealVT.isSimple())
853     return false;
854   MVT VT = RealVT.getSimpleVT();
855   if (!TLI.isTypeLegal(VT) && VT != MVT::i1)
856     return false;
857
858   const Value *Op0 = EVI->getOperand(0);
859   Type *AggTy = Op0->getType();
860
861   // Get the base result register.
862   unsigned ResultReg;
863   DenseMap<const Value *, unsigned>::iterator I = FuncInfo.ValueMap.find(Op0);
864   if (I != FuncInfo.ValueMap.end())
865     ResultReg = I->second;
866   else if (isa<Instruction>(Op0))
867     ResultReg = FuncInfo.InitializeRegForValue(Op0);
868   else
869     return false; // fast-isel can't handle aggregate constants at the moment
870
871   // Get the actual result register, which is an offset from the base register.
872   unsigned VTIndex = ComputeLinearIndex(AggTy, EVI->getIndices());
873
874   SmallVector<EVT, 4> AggValueVTs;
875   ComputeValueVTs(TLI, AggTy, AggValueVTs);
876
877   for (unsigned i = 0; i < VTIndex; i++)
878     ResultReg += TLI.getNumRegisters(FuncInfo.Fn->getContext(), AggValueVTs[i]);
879
880   UpdateValueMap(EVI, ResultReg);
881   return true;
882 }
883
884 bool
885 FastISel::SelectOperator(const User *I, unsigned Opcode) {
886   switch (Opcode) {
887   case Instruction::Add:
888     return SelectBinaryOp(I, ISD::ADD);
889   case Instruction::FAdd:
890     return SelectBinaryOp(I, ISD::FADD);
891   case Instruction::Sub:
892     return SelectBinaryOp(I, ISD::SUB);
893   case Instruction::FSub:
894     // FNeg is currently represented in LLVM IR as a special case of FSub.
895     if (BinaryOperator::isFNeg(I))
896       return SelectFNeg(I);
897     return SelectBinaryOp(I, ISD::FSUB);
898   case Instruction::Mul:
899     return SelectBinaryOp(I, ISD::MUL);
900   case Instruction::FMul:
901     return SelectBinaryOp(I, ISD::FMUL);
902   case Instruction::SDiv:
903     return SelectBinaryOp(I, ISD::SDIV);
904   case Instruction::UDiv:
905     return SelectBinaryOp(I, ISD::UDIV);
906   case Instruction::FDiv:
907     return SelectBinaryOp(I, ISD::FDIV);
908   case Instruction::SRem:
909     return SelectBinaryOp(I, ISD::SREM);
910   case Instruction::URem:
911     return SelectBinaryOp(I, ISD::UREM);
912   case Instruction::FRem:
913     return SelectBinaryOp(I, ISD::FREM);
914   case Instruction::Shl:
915     return SelectBinaryOp(I, ISD::SHL);
916   case Instruction::LShr:
917     return SelectBinaryOp(I, ISD::SRL);
918   case Instruction::AShr:
919     return SelectBinaryOp(I, ISD::SRA);
920   case Instruction::And:
921     return SelectBinaryOp(I, ISD::AND);
922   case Instruction::Or:
923     return SelectBinaryOp(I, ISD::OR);
924   case Instruction::Xor:
925     return SelectBinaryOp(I, ISD::XOR);
926
927   case Instruction::GetElementPtr:
928     return SelectGetElementPtr(I);
929
930   case Instruction::Br: {
931     const BranchInst *BI = cast<BranchInst>(I);
932
933     if (BI->isUnconditional()) {
934       const BasicBlock *LLVMSucc = BI->getSuccessor(0);
935       MachineBasicBlock *MSucc = FuncInfo.MBBMap[LLVMSucc];
936       FastEmitBranch(MSucc, BI->getDebugLoc());
937       return true;
938     }
939
940     // Conditional branches are not handed yet.
941     // Halt "fast" selection and bail.
942     return false;
943   }
944
945   case Instruction::Unreachable:
946     // Nothing to emit.
947     return true;
948
949   case Instruction::Alloca:
950     // FunctionLowering has the static-sized case covered.
951     if (FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(I)))
952       return true;
953
954     // Dynamic-sized alloca is not handled yet.
955     return false;
956
957   case Instruction::Call:
958     return SelectCall(I);
959
960   case Instruction::BitCast:
961     return SelectBitCast(I);
962
963   case Instruction::FPToSI:
964     return SelectCast(I, ISD::FP_TO_SINT);
965   case Instruction::ZExt:
966     return SelectCast(I, ISD::ZERO_EXTEND);
967   case Instruction::SExt:
968     return SelectCast(I, ISD::SIGN_EXTEND);
969   case Instruction::Trunc:
970     return SelectCast(I, ISD::TRUNCATE);
971   case Instruction::SIToFP:
972     return SelectCast(I, ISD::SINT_TO_FP);
973
974   case Instruction::IntToPtr: // Deliberate fall-through.
975   case Instruction::PtrToInt: {
976     EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
977     EVT DstVT = TLI.getValueType(I->getType());
978     if (DstVT.bitsGT(SrcVT))
979       return SelectCast(I, ISD::ZERO_EXTEND);
980     if (DstVT.bitsLT(SrcVT))
981       return SelectCast(I, ISD::TRUNCATE);
982     unsigned Reg = getRegForValue(I->getOperand(0));
983     if (Reg == 0) return false;
984     UpdateValueMap(I, Reg);
985     return true;
986   }
987
988   case Instruction::ExtractValue:
989     return SelectExtractValue(I);
990
991   case Instruction::PHI:
992     llvm_unreachable("FastISel shouldn't visit PHI nodes!");
993
994   default:
995     // Unhandled instruction. Halt "fast" selection and bail.
996     return false;
997   }
998 }
999
1000 FastISel::FastISel(FunctionLoweringInfo &funcInfo)
1001   : FuncInfo(funcInfo),
1002     MRI(FuncInfo.MF->getRegInfo()),
1003     MFI(*FuncInfo.MF->getFrameInfo()),
1004     MCP(*FuncInfo.MF->getConstantPool()),
1005     TM(FuncInfo.MF->getTarget()),
1006     TD(*TM.getTargetData()),
1007     TII(*TM.getInstrInfo()),
1008     TLI(*TM.getTargetLowering()),
1009     TRI(*TM.getRegisterInfo()) {
1010 }
1011
1012 FastISel::~FastISel() {}
1013
1014 unsigned FastISel::FastEmit_(MVT, MVT,
1015                              unsigned) {
1016   return 0;
1017 }
1018
1019 unsigned FastISel::FastEmit_r(MVT, MVT,
1020                               unsigned,
1021                               unsigned /*Op0*/, bool /*Op0IsKill*/) {
1022   return 0;
1023 }
1024
1025 unsigned FastISel::FastEmit_rr(MVT, MVT,
1026                                unsigned,
1027                                unsigned /*Op0*/, bool /*Op0IsKill*/,
1028                                unsigned /*Op1*/, bool /*Op1IsKill*/) {
1029   return 0;
1030 }
1031
1032 unsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
1033   return 0;
1034 }
1035
1036 unsigned FastISel::FastEmit_f(MVT, MVT,
1037                               unsigned, const ConstantFP * /*FPImm*/) {
1038   return 0;
1039 }
1040
1041 unsigned FastISel::FastEmit_ri(MVT, MVT,
1042                                unsigned,
1043                                unsigned /*Op0*/, bool /*Op0IsKill*/,
1044                                uint64_t /*Imm*/) {
1045   return 0;
1046 }
1047
1048 unsigned FastISel::FastEmit_rf(MVT, MVT,
1049                                unsigned,
1050                                unsigned /*Op0*/, bool /*Op0IsKill*/,
1051                                const ConstantFP * /*FPImm*/) {
1052   return 0;
1053 }
1054
1055 unsigned FastISel::FastEmit_rri(MVT, MVT,
1056                                 unsigned,
1057                                 unsigned /*Op0*/, bool /*Op0IsKill*/,
1058                                 unsigned /*Op1*/, bool /*Op1IsKill*/,
1059                                 uint64_t /*Imm*/) {
1060   return 0;
1061 }
1062
1063 /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
1064 /// to emit an instruction with an immediate operand using FastEmit_ri.
1065 /// If that fails, it materializes the immediate into a register and try
1066 /// FastEmit_rr instead.
1067 unsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
1068                                 unsigned Op0, bool Op0IsKill,
1069                                 uint64_t Imm, MVT ImmType) {
1070   // If this is a multiply by a power of two, emit this as a shift left.
1071   if (Opcode == ISD::MUL && isPowerOf2_64(Imm)) {
1072     Opcode = ISD::SHL;
1073     Imm = Log2_64(Imm);
1074   } else if (Opcode == ISD::UDIV && isPowerOf2_64(Imm)) {
1075     // div x, 8 -> srl x, 3
1076     Opcode = ISD::SRL;
1077     Imm = Log2_64(Imm);
1078   }
1079
1080   // Horrible hack (to be removed), check to make sure shift amounts are
1081   // in-range.
1082   if ((Opcode == ISD::SHL || Opcode == ISD::SRA || Opcode == ISD::SRL) &&
1083       Imm >= VT.getSizeInBits())
1084     return 0;
1085
1086   // First check if immediate type is legal. If not, we can't use the ri form.
1087   unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
1088   if (ResultReg != 0)
1089     return ResultReg;
1090   unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
1091   if (MaterialReg == 0) {
1092     // This is a bit ugly/slow, but failing here means falling out of
1093     // fast-isel, which would be very slow.
1094     IntegerType *ITy = IntegerType::get(FuncInfo.Fn->getContext(),
1095                                               VT.getSizeInBits());
1096     MaterialReg = getRegForValue(ConstantInt::get(ITy, Imm));
1097   }
1098   return FastEmit_rr(VT, VT, Opcode,
1099                      Op0, Op0IsKill,
1100                      MaterialReg, /*Kill=*/true);
1101 }
1102
1103 unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
1104   return MRI.createVirtualRegister(RC);
1105 }
1106
1107 unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
1108                                  const TargetRegisterClass* RC) {
1109   unsigned ResultReg = createResultReg(RC);
1110   const MCInstrDesc &II = TII.get(MachineInstOpcode);
1111
1112   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg);
1113   return ResultReg;
1114 }
1115
1116 unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
1117                                   const TargetRegisterClass *RC,
1118                                   unsigned Op0, bool Op0IsKill) {
1119   unsigned ResultReg = createResultReg(RC);
1120   const MCInstrDesc &II = TII.get(MachineInstOpcode);
1121
1122   if (II.getNumDefs() >= 1)
1123     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1124       .addReg(Op0, Op0IsKill * RegState::Kill);
1125   else {
1126     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1127       .addReg(Op0, Op0IsKill * RegState::Kill);
1128     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1129             ResultReg).addReg(II.ImplicitDefs[0]);
1130   }
1131
1132   return ResultReg;
1133 }
1134
1135 unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
1136                                    const TargetRegisterClass *RC,
1137                                    unsigned Op0, bool Op0IsKill,
1138                                    unsigned Op1, bool Op1IsKill) {
1139   unsigned ResultReg = createResultReg(RC);
1140   const MCInstrDesc &II = TII.get(MachineInstOpcode);
1141
1142   if (II.getNumDefs() >= 1)
1143     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1144       .addReg(Op0, Op0IsKill * RegState::Kill)
1145       .addReg(Op1, Op1IsKill * RegState::Kill);
1146   else {
1147     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1148       .addReg(Op0, Op0IsKill * RegState::Kill)
1149       .addReg(Op1, Op1IsKill * RegState::Kill);
1150     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1151             ResultReg).addReg(II.ImplicitDefs[0]);
1152   }
1153   return ResultReg;
1154 }
1155
1156 unsigned FastISel::FastEmitInst_rrr(unsigned MachineInstOpcode,
1157                                    const TargetRegisterClass *RC,
1158                                    unsigned Op0, bool Op0IsKill,
1159                                    unsigned Op1, bool Op1IsKill,
1160                                    unsigned Op2, bool Op2IsKill) {
1161   unsigned ResultReg = createResultReg(RC);
1162   const MCInstrDesc &II = TII.get(MachineInstOpcode);
1163
1164   if (II.getNumDefs() >= 1)
1165     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1166       .addReg(Op0, Op0IsKill * RegState::Kill)
1167       .addReg(Op1, Op1IsKill * RegState::Kill)
1168       .addReg(Op2, Op2IsKill * RegState::Kill);
1169   else {
1170     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1171       .addReg(Op0, Op0IsKill * RegState::Kill)
1172       .addReg(Op1, Op1IsKill * RegState::Kill)
1173       .addReg(Op2, Op2IsKill * RegState::Kill);
1174     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1175             ResultReg).addReg(II.ImplicitDefs[0]);
1176   }
1177   return ResultReg;
1178 }
1179
1180 unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
1181                                    const TargetRegisterClass *RC,
1182                                    unsigned Op0, bool Op0IsKill,
1183                                    uint64_t Imm) {
1184   unsigned ResultReg = createResultReg(RC);
1185   const MCInstrDesc &II = TII.get(MachineInstOpcode);
1186
1187   if (II.getNumDefs() >= 1)
1188     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1189       .addReg(Op0, Op0IsKill * RegState::Kill)
1190       .addImm(Imm);
1191   else {
1192     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1193       .addReg(Op0, Op0IsKill * RegState::Kill)
1194       .addImm(Imm);
1195     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1196             ResultReg).addReg(II.ImplicitDefs[0]);
1197   }
1198   return ResultReg;
1199 }
1200
1201 unsigned FastISel::FastEmitInst_rii(unsigned MachineInstOpcode,
1202                                    const TargetRegisterClass *RC,
1203                                    unsigned Op0, bool Op0IsKill,
1204                                    uint64_t Imm1, uint64_t Imm2) {
1205   unsigned ResultReg = createResultReg(RC);
1206   const MCInstrDesc &II = TII.get(MachineInstOpcode);
1207
1208   if (II.getNumDefs() >= 1)
1209     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1210       .addReg(Op0, Op0IsKill * RegState::Kill)
1211       .addImm(Imm1)
1212       .addImm(Imm2);
1213   else {
1214     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1215       .addReg(Op0, Op0IsKill * RegState::Kill)
1216       .addImm(Imm1)
1217       .addImm(Imm2);
1218     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1219             ResultReg).addReg(II.ImplicitDefs[0]);
1220   }
1221   return ResultReg;
1222 }
1223
1224 unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
1225                                    const TargetRegisterClass *RC,
1226                                    unsigned Op0, bool Op0IsKill,
1227                                    const ConstantFP *FPImm) {
1228   unsigned ResultReg = createResultReg(RC);
1229   const MCInstrDesc &II = TII.get(MachineInstOpcode);
1230
1231   if (II.getNumDefs() >= 1)
1232     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1233       .addReg(Op0, Op0IsKill * RegState::Kill)
1234       .addFPImm(FPImm);
1235   else {
1236     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1237       .addReg(Op0, Op0IsKill * RegState::Kill)
1238       .addFPImm(FPImm);
1239     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1240             ResultReg).addReg(II.ImplicitDefs[0]);
1241   }
1242   return ResultReg;
1243 }
1244
1245 unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
1246                                     const TargetRegisterClass *RC,
1247                                     unsigned Op0, bool Op0IsKill,
1248                                     unsigned Op1, bool Op1IsKill,
1249                                     uint64_t Imm) {
1250   unsigned ResultReg = createResultReg(RC);
1251   const MCInstrDesc &II = TII.get(MachineInstOpcode);
1252
1253   if (II.getNumDefs() >= 1)
1254     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1255       .addReg(Op0, Op0IsKill * RegState::Kill)
1256       .addReg(Op1, Op1IsKill * RegState::Kill)
1257       .addImm(Imm);
1258   else {
1259     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II)
1260       .addReg(Op0, Op0IsKill * RegState::Kill)
1261       .addReg(Op1, Op1IsKill * RegState::Kill)
1262       .addImm(Imm);
1263     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1264             ResultReg).addReg(II.ImplicitDefs[0]);
1265   }
1266   return ResultReg;
1267 }
1268
1269 unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
1270                                   const TargetRegisterClass *RC,
1271                                   uint64_t Imm) {
1272   unsigned ResultReg = createResultReg(RC);
1273   const MCInstrDesc &II = TII.get(MachineInstOpcode);
1274
1275   if (II.getNumDefs() >= 1)
1276     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg).addImm(Imm);
1277   else {
1278     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm);
1279     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1280             ResultReg).addReg(II.ImplicitDefs[0]);
1281   }
1282   return ResultReg;
1283 }
1284
1285 unsigned FastISel::FastEmitInst_ii(unsigned MachineInstOpcode,
1286                                   const TargetRegisterClass *RC,
1287                                   uint64_t Imm1, uint64_t Imm2) {
1288   unsigned ResultReg = createResultReg(RC);
1289   const MCInstrDesc &II = TII.get(MachineInstOpcode);
1290
1291   if (II.getNumDefs() >= 1)
1292     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II, ResultReg)
1293       .addImm(Imm1).addImm(Imm2);
1294   else {
1295     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II).addImm(Imm1).addImm(Imm2);
1296     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1297             ResultReg).addReg(II.ImplicitDefs[0]);
1298   }
1299   return ResultReg;
1300 }
1301
1302 unsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
1303                                               unsigned Op0, bool Op0IsKill,
1304                                               uint32_t Idx) {
1305   unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
1306   assert(TargetRegisterInfo::isVirtualRegister(Op0) &&
1307          "Cannot yet extract from physregs");
1308   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
1309           DL, TII.get(TargetOpcode::COPY), ResultReg)
1310     .addReg(Op0, getKillRegState(Op0IsKill), Idx);
1311   return ResultReg;
1312 }
1313
1314 /// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
1315 /// with all but the least significant bit set to zero.
1316 unsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
1317   return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
1318 }
1319
1320 /// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
1321 /// Emit code to ensure constants are copied into registers when needed.
1322 /// Remember the virtual registers that need to be added to the Machine PHI
1323 /// nodes as input.  We cannot just directly add them, because expansion
1324 /// might result in multiple MBB's for one BB.  As such, the start of the
1325 /// BB might correspond to a different MBB than the end.
1326 bool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
1327   const TerminatorInst *TI = LLVMBB->getTerminator();
1328
1329   SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
1330   unsigned OrigNumPHINodesToUpdate = FuncInfo.PHINodesToUpdate.size();
1331
1332   // Check successor nodes' PHI nodes that expect a constant to be available
1333   // from this block.
1334   for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1335     const BasicBlock *SuccBB = TI->getSuccessor(succ);
1336     if (!isa<PHINode>(SuccBB->begin())) continue;
1337     MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
1338
1339     // If this terminator has multiple identical successors (common for
1340     // switches), only handle each succ once.
1341     if (!SuccsHandled.insert(SuccMBB)) continue;
1342
1343     MachineBasicBlock::iterator MBBI = SuccMBB->begin();
1344
1345     // At this point we know that there is a 1-1 correspondence between LLVM PHI
1346     // nodes and Machine PHI nodes, but the incoming operands have not been
1347     // emitted yet.
1348     for (BasicBlock::const_iterator I = SuccBB->begin();
1349          const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
1350
1351       // Ignore dead phi's.
1352       if (PN->use_empty()) continue;
1353
1354       // Only handle legal types. Two interesting things to note here. First,
1355       // by bailing out early, we may leave behind some dead instructions,
1356       // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
1357       // own moves. Second, this check is necessary because FastISel doesn't
1358       // use CreateRegs to create registers, so it always creates
1359       // exactly one register for each non-void instruction.
1360       EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
1361       if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
1362         // Promote MVT::i1.
1363         if (VT == MVT::i1)
1364           VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
1365         else {
1366           FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
1367           return false;
1368         }
1369       }
1370
1371       const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
1372
1373       // Set the DebugLoc for the copy. Prefer the location of the operand
1374       // if there is one; use the location of the PHI otherwise.
1375       DL = PN->getDebugLoc();
1376       if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
1377         DL = Inst->getDebugLoc();
1378
1379       unsigned Reg = getRegForValue(PHIOp);
1380       if (Reg == 0) {
1381         FuncInfo.PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
1382         return false;
1383       }
1384       FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
1385       DL = DebugLoc();
1386     }
1387   }
1388
1389   return true;
1390 }