blob: c8c219d39d4d8727eff54cc4818716f8398c9049 [file] [log] [blame]
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001///===-- 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
Dan Gohman6f2766d2008-08-19 22:31:46 +000014#include "llvm/Instructions.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000015#include "llvm/CodeGen/FastISel.h"
16#include "llvm/CodeGen/MachineInstrBuilder.h"
17#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng83785c82008-08-20 22:45:34 +000018#include "llvm/Target/TargetData.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000019#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng83785c82008-08-20 22:45:34 +000020#include "llvm/Target/TargetLowering.h"
Dan Gohmanbb466332008-08-20 21:05:57 +000021#include "llvm/Target/TargetMachine.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000022using namespace llvm;
23
Dan Gohmanbdedd442008-08-20 00:11:48 +000024/// SelectBinaryOp - Select and emit code for a binary operator instruction,
25/// which has an opcode which directly corresponds to the given ISD opcode.
26///
27bool FastISel::SelectBinaryOp(Instruction *I, ISD::NodeType ISDOpcode,
28 DenseMap<const Value*, unsigned> &ValueMap) {
Dan Gohmanbdedd442008-08-20 00:11:48 +000029 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
Dan Gohmand5fe57d2008-08-21 01:41:07 +000034 unsigned Op0 = ValueMap[I->getOperand(0)];
35 if (Op0 == 0)
36 // Unhandled operand. Halt "fast" selection and bail.
37 return false;
38
39 // Check if the second operand is a constant and handle it appropriately.
40 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
41 unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0,
42 CI->getZExtValue(), VT.getSimpleVT());
43 if (ResultReg == 0)
44 // Target-specific code wasn't able to find a machine opcode for
45 // the given ISD opcode and type. Halt "fast" selection and bail.
46 return false;
47
48 // We successfully emitted code for the given LLVM Instruction.
49 ValueMap[I] = ResultReg;
50 return true;
51 }
52
53 unsigned Op1 = ValueMap[I->getOperand(1)];
54 if (Op1 == 0)
55 // Unhandled operand. Halt "fast" selection and bail.
56 return false;
57
Dan Gohmanbdedd442008-08-20 00:11:48 +000058 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), ISDOpcode, Op0, Op1);
59 if (ResultReg == 0)
60 // Target-specific code wasn't able to find a machine opcode for
61 // the given ISD opcode and type. Halt "fast" selection and bail.
62 return false;
63
Dan Gohman8014e862008-08-20 00:23:20 +000064 // We successfully emitted code for the given LLVM Instruction.
Dan Gohmanbdedd442008-08-20 00:11:48 +000065 ValueMap[I] = ResultReg;
66 return true;
67}
68
69bool FastISel::SelectGetElementPtr(Instruction *I,
70 DenseMap<const Value*, unsigned> &ValueMap) {
Evan Cheng83785c82008-08-20 22:45:34 +000071 unsigned N = ValueMap[I->getOperand(0)];
72 if (N == 0)
73 // Unhandled operand. Halt "fast" selection and bail.
74 return false;
75
76 const Type *Ty = I->getOperand(0)->getType();
Dan Gohman7a0e6592008-08-21 17:25:26 +000077 MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
Evan Cheng83785c82008-08-20 22:45:34 +000078 for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
79 OI != E; ++OI) {
80 Value *Idx = *OI;
81 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
82 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
83 if (Field) {
84 // N = N + Offset
85 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
86 // FIXME: This can be optimized by combining the add with a
87 // subsequent one.
Dan Gohman7a0e6592008-08-21 17:25:26 +000088 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +000089 if (N == 0)
90 // Unhandled operand. Halt "fast" selection and bail.
91 return false;
92 }
93 Ty = StTy->getElementType(Field);
94 } else {
95 Ty = cast<SequentialType>(Ty)->getElementType();
96
97 // If this is a constant subscript, handle it quickly.
98 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
99 if (CI->getZExtValue() == 0) continue;
100 uint64_t Offs =
101 TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000102 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000103 if (N == 0)
104 // Unhandled operand. Halt "fast" selection and bail.
105 return false;
106 continue;
107 }
108
109 // N = N + Idx * ElementSize;
110 uint64_t ElementSize = TD.getABITypeSize(Ty);
111 unsigned IdxN = ValueMap[Idx];
112 if (IdxN == 0)
113 // Unhandled operand. Halt "fast" selection and bail.
114 return false;
115
116 // If the index is smaller or larger than intptr_t, truncate or extend
117 // it.
Evan Cheng2076aa82008-08-21 01:19:11 +0000118 MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
Evan Cheng83785c82008-08-20 22:45:34 +0000119 if (IdxVT.bitsLT(VT))
Dan Gohman7a0e6592008-08-21 17:25:26 +0000120 IdxN = FastEmit_r(VT, ISD::SIGN_EXTEND, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000121 else if (IdxVT.bitsGT(VT))
Dan Gohman7a0e6592008-08-21 17:25:26 +0000122 IdxN = FastEmit_r(VT, ISD::TRUNCATE, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000123 if (IdxN == 0)
124 // Unhandled operand. Halt "fast" selection and bail.
125 return false;
126
Dan Gohmanf93cf792008-08-21 17:37:05 +0000127 if (ElementSize != 1)
128 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000129 if (IdxN == 0)
130 // Unhandled operand. Halt "fast" selection and bail.
131 return false;
Dan Gohman7a0e6592008-08-21 17:25:26 +0000132 N = FastEmit_rr(VT, ISD::ADD, N, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000133 if (N == 0)
134 // Unhandled operand. Halt "fast" selection and bail.
135 return false;
136 }
137 }
138
139 // We successfully emitted code for the given LLVM Instruction.
140 ValueMap[I] = N;
141 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000142}
143
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000144BasicBlock::iterator
Dan Gohmanb7864a92008-08-20 18:09:02 +0000145FastISel::SelectInstructions(BasicBlock::iterator Begin,
146 BasicBlock::iterator End,
Dan Gohmanbb466332008-08-20 21:05:57 +0000147 DenseMap<const Value*, unsigned> &ValueMap,
Dan Gohman6ecf5092008-08-23 02:44:46 +0000148 DenseMap<const BasicBlock*,
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000149 MachineBasicBlock *> &MBBMap,
Dan Gohmanbb466332008-08-20 21:05:57 +0000150 MachineBasicBlock *mbb) {
151 MBB = mbb;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000152 BasicBlock::iterator I = Begin;
153
154 for (; I != End; ++I) {
155 switch (I->getOpcode()) {
Dan Gohman8014e862008-08-20 00:23:20 +0000156 case Instruction::Add: {
157 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
158 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
159 }
160 case Instruction::Sub: {
161 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
162 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
163 }
164 case Instruction::Mul: {
165 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
166 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
167 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000168 case Instruction::SDiv:
169 if (!SelectBinaryOp(I, ISD::SDIV, ValueMap)) return I; break;
170 case Instruction::UDiv:
171 if (!SelectBinaryOp(I, ISD::UDIV, ValueMap)) return I; break;
172 case Instruction::FDiv:
173 if (!SelectBinaryOp(I, ISD::FDIV, ValueMap)) return I; break;
174 case Instruction::SRem:
175 if (!SelectBinaryOp(I, ISD::SREM, ValueMap)) return I; break;
176 case Instruction::URem:
177 if (!SelectBinaryOp(I, ISD::UREM, ValueMap)) return I; break;
178 case Instruction::FRem:
179 if (!SelectBinaryOp(I, ISD::FREM, ValueMap)) return I; break;
180 case Instruction::Shl:
181 if (!SelectBinaryOp(I, ISD::SHL, ValueMap)) return I; break;
182 case Instruction::LShr:
183 if (!SelectBinaryOp(I, ISD::SRL, ValueMap)) return I; break;
184 case Instruction::AShr:
185 if (!SelectBinaryOp(I, ISD::SRA, ValueMap)) return I; break;
186 case Instruction::And:
187 if (!SelectBinaryOp(I, ISD::AND, ValueMap)) return I; break;
188 case Instruction::Or:
189 if (!SelectBinaryOp(I, ISD::OR, ValueMap)) return I; break;
190 case Instruction::Xor:
191 if (!SelectBinaryOp(I, ISD::XOR, ValueMap)) return I; break;
192
193 case Instruction::GetElementPtr:
194 if (!SelectGetElementPtr(I, ValueMap)) return I;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000195 break;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000196
Dan Gohman6f2766d2008-08-19 22:31:46 +0000197 case Instruction::Br: {
198 BranchInst *BI = cast<BranchInst>(I);
199
Dan Gohmane6798b72008-08-20 01:17:01 +0000200 if (BI->isUnconditional()) {
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000201 MachineFunction::iterator NextMBB =
Dan Gohmane6798b72008-08-20 01:17:01 +0000202 next(MachineFunction::iterator(MBB));
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000203 BasicBlock *LLVMSucc = BI->getSuccessor(0);
204 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
205
206 if (NextMBB != MF.end() && MSucc == NextMBB) {
207 // The unconditional fall-through case, which needs no instructions.
208 } else {
209 // The unconditional branch case.
210 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
Dan Gohmane6798b72008-08-20 01:17:01 +0000211 }
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000212 MBB->addSuccessor(MSucc);
213 break;
Dan Gohman6f2766d2008-08-19 22:31:46 +0000214 }
215
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000216 // Conditional branches are not handed yet.
217 // Halt "fast" selection and bail.
Dan Gohman6f2766d2008-08-19 22:31:46 +0000218 return I;
219 }
Dan Gohman3b7753b2008-08-22 17:37:48 +0000220
221 case Instruction::PHI:
222 // PHI nodes are already emitted.
223 break;
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000224
225 case Instruction::BitCast:
226 // BitCast consists of either an immediate to register move
227 // or a register to register move.
228 if (ConstantInt* CI = dyn_cast<ConstantInt>(I->getOperand(0))) {
229 if (I->getType()->isInteger()) {
230 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/false);
231 ValueMap[I] = FastEmit_i(VT.getSimpleVT(), ISD::Constant,
232 CI->getZExtValue());
233 break;
234 } else
235 // TODO: Support vector and fp constants.
236 return I;
237 } else
238 // TODO: Support non-constant bitcasts.
239 return I;
Dan Gohman3b7753b2008-08-22 17:37:48 +0000240
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000241 default:
242 // Unhandled instruction. Halt "fast" selection and bail.
243 return I;
244 }
245 }
246
247 return I;
248}
249
Dan Gohmanbb466332008-08-20 21:05:57 +0000250FastISel::FastISel(MachineFunction &mf)
Dan Gohman22bb3112008-08-22 00:20:26 +0000251 : MF(mf),
252 MRI(mf.getRegInfo()),
253 TM(mf.getTarget()),
254 TD(*TM.getTargetData()),
255 TII(*TM.getInstrInfo()),
256 TLI(*TM.getTargetLowering()) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000257}
258
Dan Gohmane285a742008-08-14 21:51:29 +0000259FastISel::~FastISel() {}
260
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000261unsigned FastISel::FastEmit_(MVT::SimpleValueType, ISD::NodeType) {
262 return 0;
263}
264
265unsigned FastISel::FastEmit_r(MVT::SimpleValueType, ISD::NodeType,
266 unsigned /*Op0*/) {
267 return 0;
268}
269
270unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, ISD::NodeType,
271 unsigned /*Op0*/, unsigned /*Op0*/) {
272 return 0;
273}
274
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000275unsigned FastISel::FastEmit_i(MVT::SimpleValueType, ISD::NodeType,
276 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000277 return 0;
278}
279
280unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, ISD::NodeType,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000281 unsigned /*Op0*/, uint64_t /*Imm*/) {
282 return 0;
283}
284
285unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, ISD::NodeType,
286 unsigned /*Op0*/, unsigned /*Op1*/,
287 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000288 return 0;
289}
290
291/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
292/// to emit an instruction with an immediate operand using FastEmit_ri.
293/// If that fails, it materializes the immediate into a register and try
294/// FastEmit_rr instead.
295unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000296 unsigned Op0, uint64_t Imm,
297 MVT::SimpleValueType ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000298 unsigned ResultReg = 0;
299 // First check if immediate type is legal. If not, we can't use the ri form.
300 if (TLI.getOperationAction(ISD::Constant, ImmType) == TargetLowering::Legal)
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000301 ResultReg = FastEmit_ri(VT, Opcode, Op0, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000302 if (ResultReg != 0)
303 return ResultReg;
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000304 unsigned MaterialReg = FastEmit_i(ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000305 if (MaterialReg == 0)
306 return 0;
307 return FastEmit_rr(VT, Opcode, Op0, MaterialReg);
308}
309
310unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
311 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000312}
313
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000314unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000315 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000316 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000317 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000318
Dan Gohmanfd903942008-08-20 23:53:10 +0000319 BuildMI(MBB, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000320 return ResultReg;
321}
322
323unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
324 const TargetRegisterClass *RC,
325 unsigned Op0) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000326 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000327 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000328
Dan Gohmanfd903942008-08-20 23:53:10 +0000329 BuildMI(MBB, II, ResultReg).addReg(Op0);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000330 return ResultReg;
331}
332
333unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
334 const TargetRegisterClass *RC,
335 unsigned Op0, unsigned Op1) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000336 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000337 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000338
Dan Gohmanfd903942008-08-20 23:53:10 +0000339 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000340 return ResultReg;
341}
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000342
343unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
344 const TargetRegisterClass *RC,
345 unsigned Op0, uint64_t Imm) {
346 unsigned ResultReg = createResultReg(RC);
347 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
348
349 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
350 return ResultReg;
351}
352
353unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
354 const TargetRegisterClass *RC,
355 unsigned Op0, unsigned Op1, uint64_t Imm) {
356 unsigned ResultReg = createResultReg(RC);
357 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
358
359 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
360 return ResultReg;
361}
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000362
363unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
364 const TargetRegisterClass *RC,
365 uint64_t Imm) {
366 unsigned ResultReg = createResultReg(RC);
367 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
368
369 BuildMI(MBB, II, ResultReg).addImm(Imm);
370 return ResultReg;
371}