blob: e44a6c29f55a1fa21c3e29f305dc9369a182d2d3 [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
Evan Cheng36fd9412008-09-02 21:59:13 +000024unsigned FastISel::getRegForValue(Value *V,
25 DenseMap<const Value*, unsigned> &ValueMap) {
Dan Gohmanad368ac2008-08-27 18:10:19 +000026 unsigned &Reg = ValueMap[V];
27 if (Reg != 0)
28 return Reg;
29
30 MVT::SimpleValueType VT = TLI.getValueType(V->getType()).getSimpleVT();
31 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
32 if (CI->getValue().getActiveBits() > 64)
33 return 0;
34 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman205d9252008-08-28 21:19:07 +000035 } else if (isa<ConstantPointerNull>(V)) {
36 Reg = FastEmit_i(VT, VT, ISD::Constant, 0);
Dan Gohmanad368ac2008-08-27 18:10:19 +000037 } else if (ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
38 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
39
40 if (!Reg) {
41 const APFloat &Flt = CF->getValueAPF();
42 MVT IntVT = TLI.getPointerTy();
43
44 uint64_t x[2];
45 uint32_t IntBitWidth = IntVT.getSizeInBits();
46 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
47 APFloat::rmTowardZero) != APFloat::opOK)
48 return 0;
49 APInt IntVal(IntBitWidth, 2, x);
50
51 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
52 ISD::Constant, IntVal.getZExtValue());
53 if (IntegerReg == 0)
54 return 0;
55 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
56 if (Reg == 0)
57 return 0;
58 }
Dan Gohman205d9252008-08-28 21:19:07 +000059 } else if (isa<UndefValue>(V)) {
60 Reg = createResultReg(TLI.getRegClassFor(VT));
61 BuildMI(MBB, TII.get(TargetInstrInfo::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +000062 }
63
64 return Reg;
65}
66
Owen Andersoncc54e762008-08-30 00:38:46 +000067/// UpdateValueMap - Update the value map to include the new mapping for this
68/// instruction, or insert an extra copy to get the result in a previous
69/// determined register.
70/// NOTE: This is only necessary because we might select a block that uses
71/// a value before we select the block that defines the value. It might be
72/// possible to fix this by selecting blocks in reverse postorder.
73void FastISel::UpdateValueMap(Instruction* I, unsigned Reg,
74 DenseMap<const Value*, unsigned> &ValueMap) {
75 if (!ValueMap.count(I))
76 ValueMap[I] = Reg;
77 else
78 TII.copyRegToReg(*MBB, MBB->end(), ValueMap[I],
79 Reg, MRI.getRegClass(Reg), MRI.getRegClass(Reg));
80}
81
Dan Gohmanbdedd442008-08-20 00:11:48 +000082/// SelectBinaryOp - Select and emit code for a binary operator instruction,
83/// which has an opcode which directly corresponds to the given ISD opcode.
84///
85bool FastISel::SelectBinaryOp(Instruction *I, ISD::NodeType ISDOpcode,
86 DenseMap<const Value*, unsigned> &ValueMap) {
Dan Gohmanbdedd442008-08-20 00:11:48 +000087 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
88 if (VT == MVT::Other || !VT.isSimple())
89 // Unhandled type. Halt "fast" selection and bail.
90 return false;
Dan Gohmanb71fea22008-08-26 20:52:40 +000091 // We only handle legal types. For example, on x86-32 the instruction
92 // selector contains all of the 64-bit instructions from x86-64,
93 // under the assumption that i64 won't be used if the target doesn't
94 // support it.
95 if (!TLI.isTypeLegal(VT))
96 return false;
Dan Gohmanbdedd442008-08-20 00:11:48 +000097
Dan Gohmanad368ac2008-08-27 18:10:19 +000098 unsigned Op0 = getRegForValue(I->getOperand(0), ValueMap);
Dan Gohmand5fe57d2008-08-21 01:41:07 +000099 if (Op0 == 0)
100 // Unhandled operand. Halt "fast" selection and bail.
101 return false;
102
103 // Check if the second operand is a constant and handle it appropriately.
104 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000105 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
106 ISDOpcode, Op0, CI->getZExtValue());
107 if (ResultReg != 0) {
108 // We successfully emitted code for the given LLVM Instruction.
Owen Andersoncc54e762008-08-30 00:38:46 +0000109 UpdateValueMap(I, ResultReg, ValueMap);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000110 return true;
111 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000112 }
113
Dan Gohman10df0fa2008-08-27 01:09:54 +0000114 // Check if the second operand is a constant float.
115 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000116 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
117 ISDOpcode, Op0, CF);
118 if (ResultReg != 0) {
119 // We successfully emitted code for the given LLVM Instruction.
Owen Andersoncc54e762008-08-30 00:38:46 +0000120 UpdateValueMap(I, ResultReg, ValueMap);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000121 return true;
122 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000123 }
124
Dan Gohmanad368ac2008-08-27 18:10:19 +0000125 unsigned Op1 = getRegForValue(I->getOperand(1), ValueMap);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000126 if (Op1 == 0)
127 // Unhandled operand. Halt "fast" selection and bail.
128 return false;
129
Dan Gohmanad368ac2008-08-27 18:10:19 +0000130 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000131 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
132 ISDOpcode, Op0, Op1);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000133 if (ResultReg == 0)
134 // Target-specific code wasn't able to find a machine opcode for
135 // the given ISD opcode and type. Halt "fast" selection and bail.
136 return false;
137
Dan Gohman8014e862008-08-20 00:23:20 +0000138 // We successfully emitted code for the given LLVM Instruction.
Owen Andersoncc54e762008-08-30 00:38:46 +0000139 UpdateValueMap(I, ResultReg, ValueMap);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000140 return true;
141}
142
143bool FastISel::SelectGetElementPtr(Instruction *I,
144 DenseMap<const Value*, unsigned> &ValueMap) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000145 unsigned N = getRegForValue(I->getOperand(0), ValueMap);
Evan Cheng83785c82008-08-20 22:45:34 +0000146 if (N == 0)
147 // Unhandled operand. Halt "fast" selection and bail.
148 return false;
149
150 const Type *Ty = I->getOperand(0)->getType();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000151 MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
Evan Cheng83785c82008-08-20 22:45:34 +0000152 for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
153 OI != E; ++OI) {
154 Value *Idx = *OI;
155 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
156 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
157 if (Field) {
158 // N = N + Offset
159 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
160 // FIXME: This can be optimized by combining the add with a
161 // subsequent one.
Dan Gohman7a0e6592008-08-21 17:25:26 +0000162 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000163 if (N == 0)
164 // Unhandled operand. Halt "fast" selection and bail.
165 return false;
166 }
167 Ty = StTy->getElementType(Field);
168 } else {
169 Ty = cast<SequentialType>(Ty)->getElementType();
170
171 // If this is a constant subscript, handle it quickly.
172 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
173 if (CI->getZExtValue() == 0) continue;
174 uint64_t Offs =
175 TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000176 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000177 if (N == 0)
178 // Unhandled operand. Halt "fast" selection and bail.
179 return false;
180 continue;
181 }
182
183 // N = N + Idx * ElementSize;
184 uint64_t ElementSize = TD.getABITypeSize(Ty);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000185 unsigned IdxN = getRegForValue(Idx, ValueMap);
Evan Cheng83785c82008-08-20 22:45:34 +0000186 if (IdxN == 0)
187 // Unhandled operand. Halt "fast" selection and bail.
188 return false;
189
190 // If the index is smaller or larger than intptr_t, truncate or extend
191 // it.
Evan Cheng2076aa82008-08-21 01:19:11 +0000192 MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
Evan Cheng83785c82008-08-20 22:45:34 +0000193 if (IdxVT.bitsLT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000194 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::SIGN_EXTEND, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000195 else if (IdxVT.bitsGT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000196 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::TRUNCATE, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000197 if (IdxN == 0)
198 // Unhandled operand. Halt "fast" selection and bail.
199 return false;
200
Dan Gohman80bc6e22008-08-26 20:57:08 +0000201 if (ElementSize != 1) {
Dan Gohmanf93cf792008-08-21 17:37:05 +0000202 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000203 if (IdxN == 0)
204 // Unhandled operand. Halt "fast" selection and bail.
205 return false;
206 }
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000207 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000208 if (N == 0)
209 // Unhandled operand. Halt "fast" selection and bail.
210 return false;
211 }
212 }
213
214 // We successfully emitted code for the given LLVM Instruction.
Owen Andersoncc54e762008-08-30 00:38:46 +0000215 UpdateValueMap(I, N, ValueMap);
Evan Cheng83785c82008-08-20 22:45:34 +0000216 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000217}
218
Owen Andersond0533c92008-08-26 23:46:32 +0000219bool FastISel::SelectCast(Instruction *I, ISD::NodeType Opcode,
220 DenseMap<const Value*, unsigned> &ValueMap) {
Owen Anderson6336b702008-08-27 18:58:30 +0000221 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
222 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000223
224 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
225 DstVT == MVT::Other || !DstVT.isSimple() ||
226 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
227 // Unhandled type. Halt "fast" selection and bail.
228 return false;
229
Dan Gohmanad368ac2008-08-27 18:10:19 +0000230 unsigned InputReg = getRegForValue(I->getOperand(0), ValueMap);
Owen Andersond0533c92008-08-26 23:46:32 +0000231 if (!InputReg)
232 // Unhandled operand. Halt "fast" selection and bail.
233 return false;
234
235 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
236 DstVT.getSimpleVT(),
237 Opcode,
238 InputReg);
239 if (!ResultReg)
240 return false;
241
Owen Andersoncc54e762008-08-30 00:38:46 +0000242 UpdateValueMap(I, ResultReg, ValueMap);
Owen Andersond0533c92008-08-26 23:46:32 +0000243 return true;
244}
245
Dan Gohmanad368ac2008-08-27 18:10:19 +0000246bool FastISel::SelectBitCast(Instruction *I,
247 DenseMap<const Value*, unsigned> &ValueMap) {
248 // If the bitcast doesn't change the type, just use the operand value.
249 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohmana318dab2008-08-27 20:41:38 +0000250 unsigned Reg = getRegForValue(I->getOperand(0), ValueMap);
251 if (Reg == 0)
252 return false;
Owen Andersoncc54e762008-08-30 00:38:46 +0000253 UpdateValueMap(I, Reg, ValueMap);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000254 return true;
255 }
256
257 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Anderson6336b702008-08-27 18:58:30 +0000258 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
259 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000260
261 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
262 DstVT == MVT::Other || !DstVT.isSimple() ||
263 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
264 // Unhandled type. Halt "fast" selection and bail.
265 return false;
266
Dan Gohmanad368ac2008-08-27 18:10:19 +0000267 unsigned Op0 = getRegForValue(I->getOperand(0), ValueMap);
268 if (Op0 == 0)
269 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000270 return false;
271
Dan Gohmanad368ac2008-08-27 18:10:19 +0000272 // First, try to perform the bitcast by inserting a reg-reg copy.
273 unsigned ResultReg = 0;
274 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
275 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
276 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
277 ResultReg = createResultReg(DstClass);
278
279 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
280 Op0, DstClass, SrcClass);
281 if (!InsertedCopy)
282 ResultReg = 0;
283 }
284
285 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
286 if (!ResultReg)
287 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
288 ISD::BIT_CONVERT, Op0);
289
290 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000291 return false;
292
Owen Andersoncc54e762008-08-30 00:38:46 +0000293 UpdateValueMap(I, ResultReg, ValueMap);
Owen Andersond0533c92008-08-26 23:46:32 +0000294 return true;
295}
296
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000297BasicBlock::iterator
Dan Gohmanb7864a92008-08-20 18:09:02 +0000298FastISel::SelectInstructions(BasicBlock::iterator Begin,
299 BasicBlock::iterator End,
Dan Gohmanbb466332008-08-20 21:05:57 +0000300 DenseMap<const Value*, unsigned> &ValueMap,
Dan Gohman6ecf5092008-08-23 02:44:46 +0000301 DenseMap<const BasicBlock*,
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000302 MachineBasicBlock *> &MBBMap,
Dan Gohmanbb466332008-08-20 21:05:57 +0000303 MachineBasicBlock *mbb) {
304 MBB = mbb;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000305 BasicBlock::iterator I = Begin;
306
307 for (; I != End; ++I) {
308 switch (I->getOpcode()) {
Dan Gohman8014e862008-08-20 00:23:20 +0000309 case Instruction::Add: {
310 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
311 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
312 }
313 case Instruction::Sub: {
314 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
315 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
316 }
317 case Instruction::Mul: {
318 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
319 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
320 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000321 case Instruction::SDiv:
322 if (!SelectBinaryOp(I, ISD::SDIV, ValueMap)) return I; break;
323 case Instruction::UDiv:
324 if (!SelectBinaryOp(I, ISD::UDIV, ValueMap)) return I; break;
325 case Instruction::FDiv:
326 if (!SelectBinaryOp(I, ISD::FDIV, ValueMap)) return I; break;
327 case Instruction::SRem:
328 if (!SelectBinaryOp(I, ISD::SREM, ValueMap)) return I; break;
329 case Instruction::URem:
330 if (!SelectBinaryOp(I, ISD::UREM, ValueMap)) return I; break;
331 case Instruction::FRem:
332 if (!SelectBinaryOp(I, ISD::FREM, ValueMap)) return I; break;
333 case Instruction::Shl:
334 if (!SelectBinaryOp(I, ISD::SHL, ValueMap)) return I; break;
335 case Instruction::LShr:
336 if (!SelectBinaryOp(I, ISD::SRL, ValueMap)) return I; break;
337 case Instruction::AShr:
338 if (!SelectBinaryOp(I, ISD::SRA, ValueMap)) return I; break;
339 case Instruction::And:
340 if (!SelectBinaryOp(I, ISD::AND, ValueMap)) return I; break;
341 case Instruction::Or:
342 if (!SelectBinaryOp(I, ISD::OR, ValueMap)) return I; break;
343 case Instruction::Xor:
344 if (!SelectBinaryOp(I, ISD::XOR, ValueMap)) return I; break;
345
346 case Instruction::GetElementPtr:
347 if (!SelectGetElementPtr(I, ValueMap)) return I;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000348 break;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000349
Dan Gohman6f2766d2008-08-19 22:31:46 +0000350 case Instruction::Br: {
351 BranchInst *BI = cast<BranchInst>(I);
352
Dan Gohmane6798b72008-08-20 01:17:01 +0000353 if (BI->isUnconditional()) {
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000354 MachineFunction::iterator NextMBB =
Dan Gohmane6798b72008-08-20 01:17:01 +0000355 next(MachineFunction::iterator(MBB));
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000356 BasicBlock *LLVMSucc = BI->getSuccessor(0);
357 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
358
359 if (NextMBB != MF.end() && MSucc == NextMBB) {
360 // The unconditional fall-through case, which needs no instructions.
361 } else {
362 // The unconditional branch case.
363 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
Dan Gohmane6798b72008-08-20 01:17:01 +0000364 }
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000365 MBB->addSuccessor(MSucc);
366 break;
Dan Gohman6f2766d2008-08-19 22:31:46 +0000367 }
368
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000369 // Conditional branches are not handed yet.
370 // Halt "fast" selection and bail.
Dan Gohman6f2766d2008-08-19 22:31:46 +0000371 return I;
372 }
Dan Gohman3b7753b2008-08-22 17:37:48 +0000373
374 case Instruction::PHI:
375 // PHI nodes are already emitted.
376 break;
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000377
378 case Instruction::BitCast:
Owen Andersond0533c92008-08-26 23:46:32 +0000379 if (!SelectBitCast(I, ValueMap)) return I; break;
Owen Anderson46aa2f52008-08-26 17:44:42 +0000380
381 case Instruction::FPToSI:
Dan Gohmanad368ac2008-08-27 18:10:19 +0000382 if (!SelectCast(I, ISD::FP_TO_SINT, ValueMap)) return I;
Owen Andersond0533c92008-08-26 23:46:32 +0000383 break;
Owen Anderson97e25682008-08-26 23:14:49 +0000384 case Instruction::ZExt:
Dan Gohmanad368ac2008-08-27 18:10:19 +0000385 if (!SelectCast(I, ISD::ZERO_EXTEND, ValueMap)) return I;
Owen Andersond0533c92008-08-26 23:46:32 +0000386 break;
387 case Instruction::SExt:
Dan Gohmanad368ac2008-08-27 18:10:19 +0000388 if (!SelectCast(I, ISD::SIGN_EXTEND, ValueMap)) return I;
Owen Andersond0533c92008-08-26 23:46:32 +0000389 break;
Owen Andersonc0bb68b2008-08-28 18:26:01 +0000390 case Instruction::Trunc:
391 if (!SelectCast(I, ISD::TRUNCATE, ValueMap)) return I;
392 break;
Owen Andersona843b8d2008-08-26 20:37:00 +0000393 case Instruction::SIToFP:
Dan Gohmanad368ac2008-08-27 18:10:19 +0000394 if (!SelectCast(I, ISD::SINT_TO_FP, ValueMap)) return I;
Owen Andersond0533c92008-08-26 23:46:32 +0000395 break;
Dan Gohman763d8932008-08-26 21:28:54 +0000396
Owen Anderson9d5b4162008-08-27 00:31:01 +0000397 case Instruction::IntToPtr: // Deliberate fall-through.
398 case Instruction::PtrToInt: {
399 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
400 MVT DstVT = TLI.getValueType(I->getType());
401 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
Owen Anderson96c5ea82008-08-27 00:35:37 +0000402 if (ValueMap[I->getOperand(0)]) {
Owen Andersoncc54e762008-08-30 00:38:46 +0000403 UpdateValueMap(I, ValueMap[I->getOperand(0)], ValueMap);
Owen Anderson96c5ea82008-08-27 00:35:37 +0000404 break;
405 } else
406 // Unhandled operand
407 return I;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000408 } else if (DstVT.bitsGT(SrcVT)) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000409 if (!SelectCast(I, ISD::ZERO_EXTEND, ValueMap)) return I;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000410 break;
411 } else {
412 // TODO: Handle SrcVT > DstVT, where truncation is needed.
413 return I;
414 }
415 }
416
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000417 default:
418 // Unhandled instruction. Halt "fast" selection and bail.
419 return I;
420 }
421 }
422
423 return I;
424}
425
Dan Gohmanbb466332008-08-20 21:05:57 +0000426FastISel::FastISel(MachineFunction &mf)
Dan Gohman22bb3112008-08-22 00:20:26 +0000427 : MF(mf),
428 MRI(mf.getRegInfo()),
429 TM(mf.getTarget()),
430 TD(*TM.getTargetData()),
431 TII(*TM.getInstrInfo()),
432 TLI(*TM.getTargetLowering()) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000433}
434
Dan Gohmane285a742008-08-14 21:51:29 +0000435FastISel::~FastISel() {}
436
Evan Cheng36fd9412008-09-02 21:59:13 +0000437unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType,
438 ISD::NodeType) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000439 return 0;
440}
441
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000442unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
443 ISD::NodeType, unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000444 return 0;
445}
446
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000447unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType,
448 ISD::NodeType, unsigned /*Op0*/,
449 unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000450 return 0;
451}
452
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000453unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
454 ISD::NodeType, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000455 return 0;
456}
457
Dan Gohman10df0fa2008-08-27 01:09:54 +0000458unsigned FastISel::FastEmit_f(MVT::SimpleValueType, MVT::SimpleValueType,
459 ISD::NodeType, ConstantFP * /*FPImm*/) {
460 return 0;
461}
462
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000463unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
464 ISD::NodeType, unsigned /*Op0*/,
465 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000466 return 0;
467}
468
Dan Gohman10df0fa2008-08-27 01:09:54 +0000469unsigned FastISel::FastEmit_rf(MVT::SimpleValueType, MVT::SimpleValueType,
470 ISD::NodeType, unsigned /*Op0*/,
471 ConstantFP * /*FPImm*/) {
472 return 0;
473}
474
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000475unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
476 ISD::NodeType,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000477 unsigned /*Op0*/, unsigned /*Op1*/,
478 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000479 return 0;
480}
481
482/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
483/// to emit an instruction with an immediate operand using FastEmit_ri.
484/// If that fails, it materializes the immediate into a register and try
485/// FastEmit_rr instead.
486unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000487 unsigned Op0, uint64_t Imm,
488 MVT::SimpleValueType ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000489 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohman151ed612008-08-27 18:15:05 +0000490 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000491 if (ResultReg != 0)
492 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000493 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000494 if (MaterialReg == 0)
495 return 0;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000496 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000497}
498
Dan Gohman10df0fa2008-08-27 01:09:54 +0000499/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
500/// to emit an instruction with a floating-point immediate operand using
501/// FastEmit_rf. If that fails, it materializes the immediate into a register
502/// and try FastEmit_rr instead.
503unsigned FastISel::FastEmit_rf_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
504 unsigned Op0, ConstantFP *FPImm,
505 MVT::SimpleValueType ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000506 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohman151ed612008-08-27 18:15:05 +0000507 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000508 if (ResultReg != 0)
509 return ResultReg;
510
511 // Materialize the constant in a register.
512 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
513 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000514 // If the target doesn't have a way to directly enter a floating-point
515 // value into a register, use an alternate approach.
516 // TODO: The current approach only supports floating-point constants
517 // that can be constructed by conversion from integer values. This should
518 // be replaced by code that creates a load from a constant-pool entry,
519 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000520 const APFloat &Flt = FPImm->getValueAPF();
521 MVT IntVT = TLI.getPointerTy();
522
523 uint64_t x[2];
524 uint32_t IntBitWidth = IntVT.getSizeInBits();
525 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
526 APFloat::rmTowardZero) != APFloat::opOK)
527 return 0;
528 APInt IntVal(IntBitWidth, 2, x);
529
530 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
531 ISD::Constant, IntVal.getZExtValue());
532 if (IntegerReg == 0)
533 return 0;
534 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
535 ISD::SINT_TO_FP, IntegerReg);
536 if (MaterialReg == 0)
537 return 0;
538 }
539 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
540}
541
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000542unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
543 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000544}
545
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000546unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000547 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000548 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000549 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000550
Dan Gohmanfd903942008-08-20 23:53:10 +0000551 BuildMI(MBB, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000552 return ResultReg;
553}
554
555unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
556 const TargetRegisterClass *RC,
557 unsigned Op0) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000558 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000559 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000560
Dan Gohmanfd903942008-08-20 23:53:10 +0000561 BuildMI(MBB, II, ResultReg).addReg(Op0);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000562 return ResultReg;
563}
564
565unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
566 const TargetRegisterClass *RC,
567 unsigned Op0, unsigned Op1) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000568 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000569 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000570
Dan Gohmanfd903942008-08-20 23:53:10 +0000571 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000572 return ResultReg;
573}
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000574
575unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
576 const TargetRegisterClass *RC,
577 unsigned Op0, uint64_t Imm) {
578 unsigned ResultReg = createResultReg(RC);
579 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
580
581 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
582 return ResultReg;
583}
584
Dan Gohman10df0fa2008-08-27 01:09:54 +0000585unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
586 const TargetRegisterClass *RC,
587 unsigned Op0, ConstantFP *FPImm) {
588 unsigned ResultReg = createResultReg(RC);
589 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
590
591 BuildMI(MBB, II, ResultReg).addReg(Op0).addFPImm(FPImm);
592 return ResultReg;
593}
594
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000595unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
596 const TargetRegisterClass *RC,
597 unsigned Op0, unsigned Op1, uint64_t Imm) {
598 unsigned ResultReg = createResultReg(RC);
599 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
600
601 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
602 return ResultReg;
603}
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000604
605unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
606 const TargetRegisterClass *RC,
607 uint64_t Imm) {
608 unsigned ResultReg = createResultReg(RC);
609 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
610
611 BuildMI(MBB, II, ResultReg).addImm(Imm);
612 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +0000613}
Owen Anderson8970f002008-08-27 22:30:02 +0000614
Owen Anderson40a468f2008-08-28 17:47:37 +0000615unsigned FastISel::FastEmitInst_extractsubreg(unsigned Op0, uint32_t Idx) {
616 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson8970f002008-08-27 22:30:02 +0000617 const TargetRegisterClass* SRC = *(RC->subregclasses_begin()+Idx-1);
618
619 unsigned ResultReg = createResultReg(SRC);
620 const TargetInstrDesc &II = TII.get(TargetInstrInfo::EXTRACT_SUBREG);
621
Owen Andersonc0bb68b2008-08-28 18:26:01 +0000622 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Idx);
Owen Anderson8970f002008-08-27 22:30:02 +0000623 return ResultReg;
624}