blob: 6b47e1808f0938fbeb4ecf70029e150409d237b1 [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
Owen Anderson99aaf102008-09-03 17:37:03 +000024// Don't cache constant materializations. To do so would require
25// tracking what uses they dominate. Non-constants, however, already
26// have the SSA def-doms-use requirement enforced, so we can cache their
27// computations.
Evan Cheng36fd9412008-09-02 21:59:13 +000028unsigned FastISel::getRegForValue(Value *V,
29 DenseMap<const Value*, unsigned> &ValueMap) {
Owen Anderson99aaf102008-09-03 17:37:03 +000030 if (ValueMap.count(V))
31 return ValueMap[V];
Dan Gohmanad368ac2008-08-27 18:10:19 +000032
33 MVT::SimpleValueType VT = TLI.getValueType(V->getType()).getSimpleVT();
34 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
35 if (CI->getValue().getActiveBits() > 64)
36 return 0;
Owen Anderson99aaf102008-09-03 17:37:03 +000037 // Don't cache constant materializations. To do so would require
38 // tracking what uses they dominate.
39 return FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman205d9252008-08-28 21:19:07 +000040 } else if (isa<ConstantPointerNull>(V)) {
Owen Anderson99aaf102008-09-03 17:37:03 +000041 return FastEmit_i(VT, VT, ISD::Constant, 0);
Dan Gohmanad368ac2008-08-27 18:10:19 +000042 } else if (ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Owen Anderson99aaf102008-09-03 17:37:03 +000043 unsigned Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +000044
45 if (!Reg) {
46 const APFloat &Flt = CF->getValueAPF();
47 MVT IntVT = TLI.getPointerTy();
48
49 uint64_t x[2];
50 uint32_t IntBitWidth = IntVT.getSizeInBits();
51 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
52 APFloat::rmTowardZero) != APFloat::opOK)
53 return 0;
54 APInt IntVal(IntBitWidth, 2, x);
55
56 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
57 ISD::Constant, IntVal.getZExtValue());
58 if (IntegerReg == 0)
59 return 0;
60 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
61 if (Reg == 0)
62 return 0;
63 }
Owen Anderson99aaf102008-09-03 17:37:03 +000064
65 return Reg;
Dan Gohman205d9252008-08-28 21:19:07 +000066 } else if (isa<UndefValue>(V)) {
Owen Anderson99aaf102008-09-03 17:37:03 +000067 unsigned Reg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman205d9252008-08-28 21:19:07 +000068 BuildMI(MBB, TII.get(TargetInstrInfo::IMPLICIT_DEF), Reg);
Owen Anderson99aaf102008-09-03 17:37:03 +000069 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +000070 }
Dan Gohmanad368ac2008-08-27 18:10:19 +000071}
72
Owen Andersoncc54e762008-08-30 00:38:46 +000073/// UpdateValueMap - Update the value map to include the new mapping for this
74/// instruction, or insert an extra copy to get the result in a previous
75/// determined register.
76/// NOTE: This is only necessary because we might select a block that uses
77/// a value before we select the block that defines the value. It might be
78/// possible to fix this by selecting blocks in reverse postorder.
79void FastISel::UpdateValueMap(Instruction* I, unsigned Reg,
80 DenseMap<const Value*, unsigned> &ValueMap) {
81 if (!ValueMap.count(I))
82 ValueMap[I] = Reg;
83 else
84 TII.copyRegToReg(*MBB, MBB->end(), ValueMap[I],
85 Reg, MRI.getRegClass(Reg), MRI.getRegClass(Reg));
86}
87
Dan Gohmanbdedd442008-08-20 00:11:48 +000088/// SelectBinaryOp - Select and emit code for a binary operator instruction,
89/// which has an opcode which directly corresponds to the given ISD opcode.
90///
91bool FastISel::SelectBinaryOp(Instruction *I, ISD::NodeType ISDOpcode,
92 DenseMap<const Value*, unsigned> &ValueMap) {
Dan Gohmanbdedd442008-08-20 00:11:48 +000093 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
94 if (VT == MVT::Other || !VT.isSimple())
95 // Unhandled type. Halt "fast" selection and bail.
96 return false;
Dan Gohmanb71fea22008-08-26 20:52:40 +000097 // We only handle legal types. For example, on x86-32 the instruction
98 // selector contains all of the 64-bit instructions from x86-64,
99 // under the assumption that i64 won't be used if the target doesn't
100 // support it.
101 if (!TLI.isTypeLegal(VT))
102 return false;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000103
Dan Gohmanad368ac2008-08-27 18:10:19 +0000104 unsigned Op0 = getRegForValue(I->getOperand(0), ValueMap);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000105 if (Op0 == 0)
106 // Unhandled operand. Halt "fast" selection and bail.
107 return false;
108
109 // Check if the second operand is a constant and handle it appropriately.
110 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000111 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
112 ISDOpcode, Op0, CI->getZExtValue());
113 if (ResultReg != 0) {
114 // We successfully emitted code for the given LLVM Instruction.
Owen Andersoncc54e762008-08-30 00:38:46 +0000115 UpdateValueMap(I, ResultReg, ValueMap);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000116 return true;
117 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000118 }
119
Dan Gohman10df0fa2008-08-27 01:09:54 +0000120 // Check if the second operand is a constant float.
121 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000122 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
123 ISDOpcode, Op0, CF);
124 if (ResultReg != 0) {
125 // We successfully emitted code for the given LLVM Instruction.
Owen Andersoncc54e762008-08-30 00:38:46 +0000126 UpdateValueMap(I, ResultReg, ValueMap);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000127 return true;
128 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000129 }
130
Dan Gohmanad368ac2008-08-27 18:10:19 +0000131 unsigned Op1 = getRegForValue(I->getOperand(1), ValueMap);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000132 if (Op1 == 0)
133 // Unhandled operand. Halt "fast" selection and bail.
134 return false;
135
Dan Gohmanad368ac2008-08-27 18:10:19 +0000136 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000137 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
138 ISDOpcode, Op0, Op1);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000139 if (ResultReg == 0)
140 // Target-specific code wasn't able to find a machine opcode for
141 // the given ISD opcode and type. Halt "fast" selection and bail.
142 return false;
143
Dan Gohman8014e862008-08-20 00:23:20 +0000144 // We successfully emitted code for the given LLVM Instruction.
Owen Andersoncc54e762008-08-30 00:38:46 +0000145 UpdateValueMap(I, ResultReg, ValueMap);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000146 return true;
147}
148
149bool FastISel::SelectGetElementPtr(Instruction *I,
150 DenseMap<const Value*, unsigned> &ValueMap) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000151 unsigned N = getRegForValue(I->getOperand(0), ValueMap);
Evan Cheng83785c82008-08-20 22:45:34 +0000152 if (N == 0)
153 // Unhandled operand. Halt "fast" selection and bail.
154 return false;
155
156 const Type *Ty = I->getOperand(0)->getType();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000157 MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
Evan Cheng83785c82008-08-20 22:45:34 +0000158 for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
159 OI != E; ++OI) {
160 Value *Idx = *OI;
161 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
162 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
163 if (Field) {
164 // N = N + Offset
165 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
166 // FIXME: This can be optimized by combining the add with a
167 // subsequent one.
Dan Gohman7a0e6592008-08-21 17:25:26 +0000168 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000169 if (N == 0)
170 // Unhandled operand. Halt "fast" selection and bail.
171 return false;
172 }
173 Ty = StTy->getElementType(Field);
174 } else {
175 Ty = cast<SequentialType>(Ty)->getElementType();
176
177 // If this is a constant subscript, handle it quickly.
178 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
179 if (CI->getZExtValue() == 0) continue;
180 uint64_t Offs =
181 TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000182 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000183 if (N == 0)
184 // Unhandled operand. Halt "fast" selection and bail.
185 return false;
186 continue;
187 }
188
189 // N = N + Idx * ElementSize;
190 uint64_t ElementSize = TD.getABITypeSize(Ty);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000191 unsigned IdxN = getRegForValue(Idx, ValueMap);
Evan Cheng83785c82008-08-20 22:45:34 +0000192 if (IdxN == 0)
193 // Unhandled operand. Halt "fast" selection and bail.
194 return false;
195
196 // If the index is smaller or larger than intptr_t, truncate or extend
197 // it.
Evan Cheng2076aa82008-08-21 01:19:11 +0000198 MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
Evan Cheng83785c82008-08-20 22:45:34 +0000199 if (IdxVT.bitsLT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000200 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::SIGN_EXTEND, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000201 else if (IdxVT.bitsGT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000202 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::TRUNCATE, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000203 if (IdxN == 0)
204 // Unhandled operand. Halt "fast" selection and bail.
205 return false;
206
Dan Gohman80bc6e22008-08-26 20:57:08 +0000207 if (ElementSize != 1) {
Dan Gohmanf93cf792008-08-21 17:37:05 +0000208 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000209 if (IdxN == 0)
210 // Unhandled operand. Halt "fast" selection and bail.
211 return false;
212 }
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000213 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000214 if (N == 0)
215 // Unhandled operand. Halt "fast" selection and bail.
216 return false;
217 }
218 }
219
220 // We successfully emitted code for the given LLVM Instruction.
Owen Andersoncc54e762008-08-30 00:38:46 +0000221 UpdateValueMap(I, N, ValueMap);
Evan Cheng83785c82008-08-20 22:45:34 +0000222 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000223}
224
Owen Andersond0533c92008-08-26 23:46:32 +0000225bool FastISel::SelectCast(Instruction *I, ISD::NodeType Opcode,
226 DenseMap<const Value*, unsigned> &ValueMap) {
Owen Anderson6336b702008-08-27 18:58:30 +0000227 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
228 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000229
230 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
231 DstVT == MVT::Other || !DstVT.isSimple() ||
232 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
233 // Unhandled type. Halt "fast" selection and bail.
234 return false;
235
Dan Gohmanad368ac2008-08-27 18:10:19 +0000236 unsigned InputReg = getRegForValue(I->getOperand(0), ValueMap);
Owen Andersond0533c92008-08-26 23:46:32 +0000237 if (!InputReg)
238 // Unhandled operand. Halt "fast" selection and bail.
239 return false;
240
241 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
242 DstVT.getSimpleVT(),
243 Opcode,
244 InputReg);
245 if (!ResultReg)
246 return false;
247
Owen Andersoncc54e762008-08-30 00:38:46 +0000248 UpdateValueMap(I, ResultReg, ValueMap);
Owen Andersond0533c92008-08-26 23:46:32 +0000249 return true;
250}
251
Dan Gohmanad368ac2008-08-27 18:10:19 +0000252bool FastISel::SelectBitCast(Instruction *I,
253 DenseMap<const Value*, unsigned> &ValueMap) {
254 // If the bitcast doesn't change the type, just use the operand value.
255 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohmana318dab2008-08-27 20:41:38 +0000256 unsigned Reg = getRegForValue(I->getOperand(0), ValueMap);
257 if (Reg == 0)
258 return false;
Owen Andersoncc54e762008-08-30 00:38:46 +0000259 UpdateValueMap(I, Reg, ValueMap);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000260 return true;
261 }
262
263 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Anderson6336b702008-08-27 18:58:30 +0000264 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
265 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000266
267 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
268 DstVT == MVT::Other || !DstVT.isSimple() ||
269 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
270 // Unhandled type. Halt "fast" selection and bail.
271 return false;
272
Dan Gohmanad368ac2008-08-27 18:10:19 +0000273 unsigned Op0 = getRegForValue(I->getOperand(0), ValueMap);
274 if (Op0 == 0)
275 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000276 return false;
277
Dan Gohmanad368ac2008-08-27 18:10:19 +0000278 // First, try to perform the bitcast by inserting a reg-reg copy.
279 unsigned ResultReg = 0;
280 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
281 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
282 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
283 ResultReg = createResultReg(DstClass);
284
285 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
286 Op0, DstClass, SrcClass);
287 if (!InsertedCopy)
288 ResultReg = 0;
289 }
290
291 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
292 if (!ResultReg)
293 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
294 ISD::BIT_CONVERT, Op0);
295
296 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000297 return false;
298
Owen Andersoncc54e762008-08-30 00:38:46 +0000299 UpdateValueMap(I, ResultReg, ValueMap);
Owen Andersond0533c92008-08-26 23:46:32 +0000300 return true;
301}
302
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000303BasicBlock::iterator
Dan Gohmanb7864a92008-08-20 18:09:02 +0000304FastISel::SelectInstructions(BasicBlock::iterator Begin,
305 BasicBlock::iterator End,
Dan Gohmanbb466332008-08-20 21:05:57 +0000306 DenseMap<const Value*, unsigned> &ValueMap,
Dan Gohman6ecf5092008-08-23 02:44:46 +0000307 DenseMap<const BasicBlock*,
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000308 MachineBasicBlock *> &MBBMap,
Dan Gohmanbb466332008-08-20 21:05:57 +0000309 MachineBasicBlock *mbb) {
310 MBB = mbb;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000311 BasicBlock::iterator I = Begin;
312
313 for (; I != End; ++I) {
314 switch (I->getOpcode()) {
Dan Gohman8014e862008-08-20 00:23:20 +0000315 case Instruction::Add: {
316 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
317 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
318 }
319 case Instruction::Sub: {
320 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
321 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
322 }
323 case Instruction::Mul: {
324 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
325 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
326 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000327 case Instruction::SDiv:
328 if (!SelectBinaryOp(I, ISD::SDIV, ValueMap)) return I; break;
329 case Instruction::UDiv:
330 if (!SelectBinaryOp(I, ISD::UDIV, ValueMap)) return I; break;
331 case Instruction::FDiv:
332 if (!SelectBinaryOp(I, ISD::FDIV, ValueMap)) return I; break;
333 case Instruction::SRem:
334 if (!SelectBinaryOp(I, ISD::SREM, ValueMap)) return I; break;
335 case Instruction::URem:
336 if (!SelectBinaryOp(I, ISD::UREM, ValueMap)) return I; break;
337 case Instruction::FRem:
338 if (!SelectBinaryOp(I, ISD::FREM, ValueMap)) return I; break;
339 case Instruction::Shl:
340 if (!SelectBinaryOp(I, ISD::SHL, ValueMap)) return I; break;
341 case Instruction::LShr:
342 if (!SelectBinaryOp(I, ISD::SRL, ValueMap)) return I; break;
343 case Instruction::AShr:
344 if (!SelectBinaryOp(I, ISD::SRA, ValueMap)) return I; break;
345 case Instruction::And:
346 if (!SelectBinaryOp(I, ISD::AND, ValueMap)) return I; break;
347 case Instruction::Or:
348 if (!SelectBinaryOp(I, ISD::OR, ValueMap)) return I; break;
349 case Instruction::Xor:
350 if (!SelectBinaryOp(I, ISD::XOR, ValueMap)) return I; break;
351
352 case Instruction::GetElementPtr:
353 if (!SelectGetElementPtr(I, ValueMap)) return I;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000354 break;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000355
Dan Gohman6f2766d2008-08-19 22:31:46 +0000356 case Instruction::Br: {
357 BranchInst *BI = cast<BranchInst>(I);
358
Dan Gohmane6798b72008-08-20 01:17:01 +0000359 if (BI->isUnconditional()) {
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000360 MachineFunction::iterator NextMBB =
Dan Gohmane6798b72008-08-20 01:17:01 +0000361 next(MachineFunction::iterator(MBB));
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000362 BasicBlock *LLVMSucc = BI->getSuccessor(0);
363 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
364
365 if (NextMBB != MF.end() && MSucc == NextMBB) {
366 // The unconditional fall-through case, which needs no instructions.
367 } else {
368 // The unconditional branch case.
369 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
Dan Gohmane6798b72008-08-20 01:17:01 +0000370 }
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000371 MBB->addSuccessor(MSucc);
372 break;
Dan Gohman6f2766d2008-08-19 22:31:46 +0000373 }
374
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000375 // Conditional branches are not handed yet.
376 // Halt "fast" selection and bail.
Dan Gohman6f2766d2008-08-19 22:31:46 +0000377 return I;
378 }
Dan Gohman3b7753b2008-08-22 17:37:48 +0000379
380 case Instruction::PHI:
381 // PHI nodes are already emitted.
382 break;
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000383
384 case Instruction::BitCast:
Owen Andersond0533c92008-08-26 23:46:32 +0000385 if (!SelectBitCast(I, ValueMap)) return I; break;
Owen Anderson46aa2f52008-08-26 17:44:42 +0000386
387 case Instruction::FPToSI:
Dan Gohmanad368ac2008-08-27 18:10:19 +0000388 if (!SelectCast(I, ISD::FP_TO_SINT, ValueMap)) return I;
Owen Andersond0533c92008-08-26 23:46:32 +0000389 break;
Owen Anderson97e25682008-08-26 23:14:49 +0000390 case Instruction::ZExt:
Dan Gohmanad368ac2008-08-27 18:10:19 +0000391 if (!SelectCast(I, ISD::ZERO_EXTEND, ValueMap)) return I;
Owen Andersond0533c92008-08-26 23:46:32 +0000392 break;
393 case Instruction::SExt:
Dan Gohmanad368ac2008-08-27 18:10:19 +0000394 if (!SelectCast(I, ISD::SIGN_EXTEND, ValueMap)) return I;
Owen Andersond0533c92008-08-26 23:46:32 +0000395 break;
Owen Andersonc0bb68b2008-08-28 18:26:01 +0000396 case Instruction::Trunc:
397 if (!SelectCast(I, ISD::TRUNCATE, ValueMap)) return I;
398 break;
Owen Andersona843b8d2008-08-26 20:37:00 +0000399 case Instruction::SIToFP:
Dan Gohmanad368ac2008-08-27 18:10:19 +0000400 if (!SelectCast(I, ISD::SINT_TO_FP, ValueMap)) return I;
Owen Andersond0533c92008-08-26 23:46:32 +0000401 break;
Dan Gohman763d8932008-08-26 21:28:54 +0000402
Owen Anderson9d5b4162008-08-27 00:31:01 +0000403 case Instruction::IntToPtr: // Deliberate fall-through.
404 case Instruction::PtrToInt: {
405 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
406 MVT DstVT = TLI.getValueType(I->getType());
407 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
Owen Anderson96c5ea82008-08-27 00:35:37 +0000408 if (ValueMap[I->getOperand(0)]) {
Owen Andersoncc54e762008-08-30 00:38:46 +0000409 UpdateValueMap(I, ValueMap[I->getOperand(0)], ValueMap);
Owen Anderson96c5ea82008-08-27 00:35:37 +0000410 break;
411 } else
412 // Unhandled operand
413 return I;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000414 } else if (DstVT.bitsGT(SrcVT)) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000415 if (!SelectCast(I, ISD::ZERO_EXTEND, ValueMap)) return I;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000416 break;
417 } else {
418 // TODO: Handle SrcVT > DstVT, where truncation is needed.
419 return I;
420 }
421 }
422
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000423 default:
424 // Unhandled instruction. Halt "fast" selection and bail.
425 return I;
426 }
427 }
428
429 return I;
430}
431
Dan Gohmanbb466332008-08-20 21:05:57 +0000432FastISel::FastISel(MachineFunction &mf)
Dan Gohman22bb3112008-08-22 00:20:26 +0000433 : MF(mf),
434 MRI(mf.getRegInfo()),
435 TM(mf.getTarget()),
436 TD(*TM.getTargetData()),
437 TII(*TM.getInstrInfo()),
438 TLI(*TM.getTargetLowering()) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000439}
440
Dan Gohmane285a742008-08-14 21:51:29 +0000441FastISel::~FastISel() {}
442
Evan Cheng36fd9412008-09-02 21:59:13 +0000443unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType,
444 ISD::NodeType) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000445 return 0;
446}
447
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000448unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
449 ISD::NodeType, unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000450 return 0;
451}
452
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000453unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType,
454 ISD::NodeType, unsigned /*Op0*/,
455 unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000456 return 0;
457}
458
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000459unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
460 ISD::NodeType, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000461 return 0;
462}
463
Dan Gohman10df0fa2008-08-27 01:09:54 +0000464unsigned FastISel::FastEmit_f(MVT::SimpleValueType, MVT::SimpleValueType,
465 ISD::NodeType, ConstantFP * /*FPImm*/) {
466 return 0;
467}
468
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000469unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
470 ISD::NodeType, unsigned /*Op0*/,
471 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000472 return 0;
473}
474
Dan Gohman10df0fa2008-08-27 01:09:54 +0000475unsigned FastISel::FastEmit_rf(MVT::SimpleValueType, MVT::SimpleValueType,
476 ISD::NodeType, unsigned /*Op0*/,
477 ConstantFP * /*FPImm*/) {
478 return 0;
479}
480
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000481unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
482 ISD::NodeType,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000483 unsigned /*Op0*/, unsigned /*Op1*/,
484 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000485 return 0;
486}
487
488/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
489/// to emit an instruction with an immediate operand using FastEmit_ri.
490/// If that fails, it materializes the immediate into a register and try
491/// FastEmit_rr instead.
492unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000493 unsigned Op0, uint64_t Imm,
494 MVT::SimpleValueType ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000495 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohman151ed612008-08-27 18:15:05 +0000496 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000497 if (ResultReg != 0)
498 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000499 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000500 if (MaterialReg == 0)
501 return 0;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000502 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000503}
504
Dan Gohman10df0fa2008-08-27 01:09:54 +0000505/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
506/// to emit an instruction with a floating-point immediate operand using
507/// FastEmit_rf. If that fails, it materializes the immediate into a register
508/// and try FastEmit_rr instead.
509unsigned FastISel::FastEmit_rf_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
510 unsigned Op0, ConstantFP *FPImm,
511 MVT::SimpleValueType ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000512 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohman151ed612008-08-27 18:15:05 +0000513 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000514 if (ResultReg != 0)
515 return ResultReg;
516
517 // Materialize the constant in a register.
518 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
519 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000520 // If the target doesn't have a way to directly enter a floating-point
521 // value into a register, use an alternate approach.
522 // TODO: The current approach only supports floating-point constants
523 // that can be constructed by conversion from integer values. This should
524 // be replaced by code that creates a load from a constant-pool entry,
525 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000526 const APFloat &Flt = FPImm->getValueAPF();
527 MVT IntVT = TLI.getPointerTy();
528
529 uint64_t x[2];
530 uint32_t IntBitWidth = IntVT.getSizeInBits();
531 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
532 APFloat::rmTowardZero) != APFloat::opOK)
533 return 0;
534 APInt IntVal(IntBitWidth, 2, x);
535
536 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
537 ISD::Constant, IntVal.getZExtValue());
538 if (IntegerReg == 0)
539 return 0;
540 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
541 ISD::SINT_TO_FP, IntegerReg);
542 if (MaterialReg == 0)
543 return 0;
544 }
545 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
546}
547
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000548unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
549 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000550}
551
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000552unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000553 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000554 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000555 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000556
Dan Gohmanfd903942008-08-20 23:53:10 +0000557 BuildMI(MBB, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000558 return ResultReg;
559}
560
561unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
562 const TargetRegisterClass *RC,
563 unsigned Op0) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000564 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000565 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000566
Dan Gohmanfd903942008-08-20 23:53:10 +0000567 BuildMI(MBB, II, ResultReg).addReg(Op0);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000568 return ResultReg;
569}
570
571unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
572 const TargetRegisterClass *RC,
573 unsigned Op0, unsigned Op1) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000574 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000575 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000576
Dan Gohmanfd903942008-08-20 23:53:10 +0000577 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000578 return ResultReg;
579}
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000580
581unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
582 const TargetRegisterClass *RC,
583 unsigned Op0, uint64_t Imm) {
584 unsigned ResultReg = createResultReg(RC);
585 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
586
587 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
588 return ResultReg;
589}
590
Dan Gohman10df0fa2008-08-27 01:09:54 +0000591unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
592 const TargetRegisterClass *RC,
593 unsigned Op0, ConstantFP *FPImm) {
594 unsigned ResultReg = createResultReg(RC);
595 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
596
597 BuildMI(MBB, II, ResultReg).addReg(Op0).addFPImm(FPImm);
598 return ResultReg;
599}
600
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000601unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
602 const TargetRegisterClass *RC,
603 unsigned Op0, unsigned Op1, uint64_t Imm) {
604 unsigned ResultReg = createResultReg(RC);
605 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
606
607 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
608 return ResultReg;
609}
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000610
611unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
612 const TargetRegisterClass *RC,
613 uint64_t Imm) {
614 unsigned ResultReg = createResultReg(RC);
615 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
616
617 BuildMI(MBB, II, ResultReg).addImm(Imm);
618 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +0000619}
Owen Anderson8970f002008-08-27 22:30:02 +0000620
Owen Anderson40a468f2008-08-28 17:47:37 +0000621unsigned FastISel::FastEmitInst_extractsubreg(unsigned Op0, uint32_t Idx) {
622 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson8970f002008-08-27 22:30:02 +0000623 const TargetRegisterClass* SRC = *(RC->subregclasses_begin()+Idx-1);
624
625 unsigned ResultReg = createResultReg(SRC);
626 const TargetInstrDesc &II = TII.get(TargetInstrInfo::EXTRACT_SUBREG);
627
Owen Andersonc0bb68b2008-08-28 18:26:01 +0000628 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Idx);
Owen Anderson8970f002008-08-27 22:30:02 +0000629 return ResultReg;
630}