Make FastISel use the correct argument type when casting GEP indices.
[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 /// SelectBinaryOp - Select and emit code for a binary operator instruction,
25 /// which has an opcode which directly corresponds to the given ISD opcode.
26 ///
27 bool FastISel::SelectBinaryOp(Instruction *I, ISD::NodeType ISDOpcode,
28                               DenseMap<const Value*, unsigned> &ValueMap) {
29   MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
30   if (VT == MVT::Other || !VT.isSimple())
31     // Unhandled type. Halt "fast" selection and bail.
32     return false;
33   // We only handle legal types. For example, on x86-32 the instruction
34   // selector contains all of the 64-bit instructions from x86-64,
35   // under the assumption that i64 won't be used if the target doesn't
36   // support it.
37   if (!TLI.isTypeLegal(VT))
38     return false;
39
40   unsigned Op0 = ValueMap[I->getOperand(0)];
41   if (Op0 == 0)
42     // Unhandled operand. Halt "fast" selection and bail.
43     return false;
44
45   // Check if the second operand is a constant and handle it appropriately.
46   if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
47     unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0,
48                                       CI->getZExtValue(), VT.getSimpleVT());
49     if (ResultReg == 0)
50       // Target-specific code wasn't able to find a machine opcode for
51       // the given ISD opcode and type. Halt "fast" selection and bail.
52       return false;
53
54     // We successfully emitted code for the given LLVM Instruction.
55     ValueMap[I] = ResultReg;
56     return true;
57   }
58
59   unsigned Op1 = ValueMap[I->getOperand(1)];
60   if (Op1 == 0)
61     // Unhandled operand. Halt "fast" selection and bail.
62     return false;
63
64   unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
65                                    ISDOpcode, Op0, Op1);
66   if (ResultReg == 0)
67     // Target-specific code wasn't able to find a machine opcode for
68     // the given ISD opcode and type. Halt "fast" selection and bail.
69     return false;
70
71   // We successfully emitted code for the given LLVM Instruction.
72   ValueMap[I] = ResultReg;
73   return true;
74 }
75
76 bool FastISel::SelectGetElementPtr(Instruction *I,
77                                    DenseMap<const Value*, unsigned> &ValueMap) {
78   unsigned N = ValueMap[I->getOperand(0)];
79   if (N == 0)
80     // Unhandled operand. Halt "fast" selection and bail.
81     return false;
82
83   const Type *Ty = I->getOperand(0)->getType();
84   MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
85   for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
86        OI != E; ++OI) {
87     Value *Idx = *OI;
88     if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
89       unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
90       if (Field) {
91         // N = N + Offset
92         uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
93         // FIXME: This can be optimized by combining the add with a
94         // subsequent one.
95         N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
96         if (N == 0)
97           // Unhandled operand. Halt "fast" selection and bail.
98           return false;
99       }
100       Ty = StTy->getElementType(Field);
101     } else {
102       Ty = cast<SequentialType>(Ty)->getElementType();
103
104       // If this is a constant subscript, handle it quickly.
105       if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
106         if (CI->getZExtValue() == 0) continue;
107         uint64_t Offs = 
108           TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
109         N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
110         if (N == 0)
111           // Unhandled operand. Halt "fast" selection and bail.
112           return false;
113         continue;
114       }
115       
116       // N = N + Idx * ElementSize;
117       uint64_t ElementSize = TD.getABITypeSize(Ty);
118       unsigned IdxN = ValueMap[Idx];
119       if (IdxN == 0)
120         // Unhandled operand. Halt "fast" selection and bail.
121         return false;
122
123       // If the index is smaller or larger than intptr_t, truncate or extend
124       // it.
125       MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
126       if (IdxVT.bitsLT(VT))
127         IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::SIGN_EXTEND, IdxN);
128       else if (IdxVT.bitsGT(VT))
129         IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::TRUNCATE, IdxN);
130       if (IdxN == 0)
131         // Unhandled operand. Halt "fast" selection and bail.
132         return false;
133
134       if (ElementSize != 1) {
135         IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
136         if (IdxN == 0)
137           // Unhandled operand. Halt "fast" selection and bail.
138           return false;
139       }
140       N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
141       if (N == 0)
142         // Unhandled operand. Halt "fast" selection and bail.
143         return false;
144     }
145   }
146
147   // We successfully emitted code for the given LLVM Instruction.
148   ValueMap[I] = N;
149   return true;
150 }
151
152 BasicBlock::iterator
153 FastISel::SelectInstructions(BasicBlock::iterator Begin,
154                              BasicBlock::iterator End,
155                              DenseMap<const Value*, unsigned> &ValueMap,
156                              DenseMap<const BasicBlock*,
157                                       MachineBasicBlock *> &MBBMap,
158                              MachineBasicBlock *mbb) {
159   MBB = mbb;
160   BasicBlock::iterator I = Begin;
161
162   for (; I != End; ++I) {
163     switch (I->getOpcode()) {
164     case Instruction::Add: {
165       ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
166       if (!SelectBinaryOp(I, Opc, ValueMap))  return I; break;
167     }
168     case Instruction::Sub: {
169       ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
170       if (!SelectBinaryOp(I, Opc, ValueMap))  return I; break;
171     }
172     case Instruction::Mul: {
173       ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
174       if (!SelectBinaryOp(I, Opc, ValueMap))  return I; break;
175     }
176     case Instruction::SDiv:
177       if (!SelectBinaryOp(I, ISD::SDIV, ValueMap)) return I; break;
178     case Instruction::UDiv:
179       if (!SelectBinaryOp(I, ISD::UDIV, ValueMap)) return I; break;
180     case Instruction::FDiv:
181       if (!SelectBinaryOp(I, ISD::FDIV, ValueMap)) return I; break;
182     case Instruction::SRem:
183       if (!SelectBinaryOp(I, ISD::SREM, ValueMap)) return I; break;
184     case Instruction::URem:
185       if (!SelectBinaryOp(I, ISD::UREM, ValueMap)) return I; break;
186     case Instruction::FRem:
187       if (!SelectBinaryOp(I, ISD::FREM, ValueMap)) return I; break;
188     case Instruction::Shl:
189       if (!SelectBinaryOp(I, ISD::SHL, ValueMap)) return I; break;
190     case Instruction::LShr:
191       if (!SelectBinaryOp(I, ISD::SRL, ValueMap)) return I; break;
192     case Instruction::AShr:
193       if (!SelectBinaryOp(I, ISD::SRA, ValueMap)) return I; break;
194     case Instruction::And:
195       if (!SelectBinaryOp(I, ISD::AND, ValueMap)) return I; break;
196     case Instruction::Or:
197       if (!SelectBinaryOp(I, ISD::OR, ValueMap)) return I; break;
198     case Instruction::Xor:
199       if (!SelectBinaryOp(I, ISD::XOR, ValueMap)) return I; break;
200
201     case Instruction::GetElementPtr:
202       if (!SelectGetElementPtr(I, ValueMap)) return I;
203       break;
204
205     case Instruction::Br: {
206       BranchInst *BI = cast<BranchInst>(I);
207
208       if (BI->isUnconditional()) {
209         MachineFunction::iterator NextMBB =
210            next(MachineFunction::iterator(MBB));
211         BasicBlock *LLVMSucc = BI->getSuccessor(0);
212         MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
213
214         if (NextMBB != MF.end() && MSucc == NextMBB) {
215           // The unconditional fall-through case, which needs no instructions.
216         } else {
217           // The unconditional branch case.
218           TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
219         }
220         MBB->addSuccessor(MSucc);
221         break;
222       }
223
224       // Conditional branches are not handed yet.
225       // Halt "fast" selection and bail.
226       return I;
227     }
228
229     case Instruction::PHI:
230       // PHI nodes are already emitted.
231       break;
232       
233     case Instruction::BitCast:
234       // BitCast consists of either an immediate to register move
235       // or a register to register move.
236       if (ConstantInt* CI = dyn_cast<ConstantInt>(I->getOperand(0))) {
237         if (I->getType()->isInteger()) {
238           MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/false);
239           unsigned result = FastEmit_i(VT.getSimpleVT(), VT.getSimpleVT(),
240                                        ISD::Constant,
241                                        CI->getZExtValue());
242           if (!result)
243             return I;
244           
245           ValueMap[I] = result;
246           break;
247         } else
248           // TODO: Support vector and fp constants.
249           return I;
250       } else if (!isa<Constant>(I->getOperand(0))) {
251         // Bitcasts of non-constant values become reg-reg copies.
252         MVT SrcVT = MVT::getMVT(I->getOperand(0)->getType());
253         MVT DstVT = MVT::getMVT(I->getType());
254         
255         if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
256             DstVT == MVT::Other || !DstVT.isSimple() ||
257             !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
258           // Unhandled type. Halt "fast" selection and bail.
259           return I;
260         
261         unsigned Op0 = ValueMap[I->getOperand(0)];
262         if (Op0 == 0)
263           // Unhandled operand. Halt "fast" selection and bail.
264           return false;
265         
266         // First, try to perform the bitcast by inserting a reg-reg copy.
267         unsigned ResultReg = 0;
268         if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
269           TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
270           TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
271           ResultReg = createResultReg(DstClass);
272           
273           bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
274                                                Op0, DstClass, SrcClass);
275           if (!InsertedCopy)
276             ResultReg = 0;
277         }
278         
279         // If the reg-reg copy failed, select a BIT_CONVERT opcode.
280         if (!ResultReg)
281           ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
282                                  ISD::BIT_CONVERT, Op0);
283         
284         if (!ResultReg)
285           return I;
286         
287         ValueMap[I] = ResultReg;
288         break;
289       } else
290         // TODO: Casting a non-integral constant?
291         return I;
292
293     case Instruction::FPToSI:
294       if (!isa<ConstantFP>(I->getOperand(0))) {
295         MVT SrcVT = MVT::getMVT(I->getOperand(0)->getType());
296         MVT DstVT = MVT::getMVT(I->getType());
297         
298         if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
299             DstVT == MVT::Other || !DstVT.isSimple() ||
300             !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
301           // Unhandled type. Halt "fast" selection and bail.
302           return I;
303         
304         unsigned InputReg = ValueMap[I->getOperand(0)];
305         if (!InputReg)
306           // Unhandled operand.  Halt "fast" selection and bail.
307           return I;
308         
309         unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
310                                         DstVT.getSimpleVT(),
311                                         ISD::FP_TO_SINT,
312                                         InputReg);
313         if (!ResultReg)
314           return I;
315         
316         ValueMap[I] = ResultReg;
317         break;
318       } else
319         // TODO: Materialize the FP constant and then convert,
320         // or attempt constant folding.
321         return I;
322
323     case Instruction::SIToFP:
324       if (!isa<ConstantInt>(I->getOperand(0))) {
325         MVT SrcVT = MVT::getMVT(I->getOperand(0)->getType());
326         MVT DstVT = MVT::getMVT(I->getType());
327         
328         if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
329             DstVT == MVT::Other || !DstVT.isSimple() ||
330             !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
331           // Unhandled type. Halt "fast" selection and bail.
332           return I;
333         
334         unsigned InputReg = ValueMap[I->getOperand(0)];
335         if (!InputReg)
336           // Unhandled operan.  Halt "fast" selection and bail.
337           return I;
338         
339         unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
340                                         DstVT.getSimpleVT(),
341                                         ISD::SINT_TO_FP,
342                                         InputReg);
343         if (!ResultReg)
344           return I;
345         
346         ValueMap[I] = ResultReg;
347         break;
348       } else
349         // TODO: Materialize constant and convert to FP.
350         return I;
351     default:
352       // Unhandled instruction. Halt "fast" selection and bail.
353       return I;
354     }
355   }
356
357   return I;
358 }
359
360 FastISel::FastISel(MachineFunction &mf)
361   : MF(mf),
362     MRI(mf.getRegInfo()),
363     TM(mf.getTarget()),
364     TD(*TM.getTargetData()),
365     TII(*TM.getInstrInfo()),
366     TLI(*TM.getTargetLowering()) {
367 }
368
369 FastISel::~FastISel() {}
370
371 unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType, ISD::NodeType) {
372   return 0;
373 }
374
375 unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
376                               ISD::NodeType, unsigned /*Op0*/) {
377   return 0;
378 }
379
380 unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType, 
381                                ISD::NodeType, unsigned /*Op0*/,
382                                unsigned /*Op0*/) {
383   return 0;
384 }
385
386 unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
387                               ISD::NodeType, uint64_t /*Imm*/) {
388   return 0;
389 }
390
391 unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
392                                ISD::NodeType, unsigned /*Op0*/,
393                                uint64_t /*Imm*/) {
394   return 0;
395 }
396
397 unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
398                                 ISD::NodeType,
399                                 unsigned /*Op0*/, unsigned /*Op1*/,
400                                 uint64_t /*Imm*/) {
401   return 0;
402 }
403
404 /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
405 /// to emit an instruction with an immediate operand using FastEmit_ri.
406 /// If that fails, it materializes the immediate into a register and try
407 /// FastEmit_rr instead.
408 unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
409                                 unsigned Op0, uint64_t Imm,
410                                 MVT::SimpleValueType ImmType) {
411   unsigned ResultReg = 0;
412   // First check if immediate type is legal. If not, we can't use the ri form.
413   if (TLI.getOperationAction(ISD::Constant, ImmType) == TargetLowering::Legal)
414     ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
415   if (ResultReg != 0)
416     return ResultReg;
417   unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
418   if (MaterialReg == 0)
419     return 0;
420   return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
421 }
422
423 unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
424   return MRI.createVirtualRegister(RC);
425 }
426
427 unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
428                                  const TargetRegisterClass* RC) {
429   unsigned ResultReg = createResultReg(RC);
430   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
431
432   BuildMI(MBB, II, ResultReg);
433   return ResultReg;
434 }
435
436 unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
437                                   const TargetRegisterClass *RC,
438                                   unsigned Op0) {
439   unsigned ResultReg = createResultReg(RC);
440   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
441
442   BuildMI(MBB, II, ResultReg).addReg(Op0);
443   return ResultReg;
444 }
445
446 unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
447                                    const TargetRegisterClass *RC,
448                                    unsigned Op0, unsigned Op1) {
449   unsigned ResultReg = createResultReg(RC);
450   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
451
452   BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1);
453   return ResultReg;
454 }
455
456 unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
457                                    const TargetRegisterClass *RC,
458                                    unsigned Op0, uint64_t Imm) {
459   unsigned ResultReg = createResultReg(RC);
460   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
461
462   BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
463   return ResultReg;
464 }
465
466 unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
467                                     const TargetRegisterClass *RC,
468                                     unsigned Op0, unsigned Op1, uint64_t Imm) {
469   unsigned ResultReg = createResultReg(RC);
470   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
471
472   BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
473   return ResultReg;
474 }
475
476 unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
477                                   const TargetRegisterClass *RC,
478                                   uint64_t Imm) {
479   unsigned ResultReg = createResultReg(RC);
480   const TargetInstrDesc &II = TII.get(MachineInstOpcode);
481   
482   BuildMI(MBB, II, ResultReg).addImm(Imm);
483   return ResultReg;
484 }