blob: 12f12bd22c5180910e88236e2e668fb251d5264f [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 }
Owen Andersond5d81a42008-09-03 17:51:57 +000071
72 return 0;
Dan Gohmanad368ac2008-08-27 18:10:19 +000073}
74
Owen Andersoncc54e762008-08-30 00:38:46 +000075/// UpdateValueMap - Update the value map to include the new mapping for this
76/// instruction, or insert an extra copy to get the result in a previous
77/// determined register.
78/// NOTE: This is only necessary because we might select a block that uses
79/// a value before we select the block that defines the value. It might be
80/// possible to fix this by selecting blocks in reverse postorder.
81void FastISel::UpdateValueMap(Instruction* I, unsigned Reg,
82 DenseMap<const Value*, unsigned> &ValueMap) {
83 if (!ValueMap.count(I))
84 ValueMap[I] = Reg;
85 else
86 TII.copyRegToReg(*MBB, MBB->end(), ValueMap[I],
87 Reg, MRI.getRegClass(Reg), MRI.getRegClass(Reg));
88}
89
Dan Gohmanbdedd442008-08-20 00:11:48 +000090/// SelectBinaryOp - Select and emit code for a binary operator instruction,
91/// which has an opcode which directly corresponds to the given ISD opcode.
92///
93bool FastISel::SelectBinaryOp(Instruction *I, ISD::NodeType ISDOpcode,
94 DenseMap<const Value*, unsigned> &ValueMap) {
Dan Gohmanbdedd442008-08-20 00:11:48 +000095 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
96 if (VT == MVT::Other || !VT.isSimple())
97 // Unhandled type. Halt "fast" selection and bail.
98 return false;
Dan Gohmanb71fea22008-08-26 20:52:40 +000099 // We only handle legal types. For example, on x86-32 the instruction
100 // selector contains all of the 64-bit instructions from x86-64,
101 // under the assumption that i64 won't be used if the target doesn't
102 // support it.
103 if (!TLI.isTypeLegal(VT))
104 return false;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000105
Dan Gohmanad368ac2008-08-27 18:10:19 +0000106 unsigned Op0 = getRegForValue(I->getOperand(0), ValueMap);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000107 if (Op0 == 0)
108 // Unhandled operand. Halt "fast" selection and bail.
109 return false;
110
111 // Check if the second operand is a constant and handle it appropriately.
112 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000113 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
114 ISDOpcode, Op0, CI->getZExtValue());
115 if (ResultReg != 0) {
116 // We successfully emitted code for the given LLVM Instruction.
Owen Andersoncc54e762008-08-30 00:38:46 +0000117 UpdateValueMap(I, ResultReg, ValueMap);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000118 return true;
119 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000120 }
121
Dan Gohman10df0fa2008-08-27 01:09:54 +0000122 // Check if the second operand is a constant float.
123 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000124 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
125 ISDOpcode, Op0, CF);
126 if (ResultReg != 0) {
127 // We successfully emitted code for the given LLVM Instruction.
Owen Andersoncc54e762008-08-30 00:38:46 +0000128 UpdateValueMap(I, ResultReg, ValueMap);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000129 return true;
130 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000131 }
132
Dan Gohmanad368ac2008-08-27 18:10:19 +0000133 unsigned Op1 = getRegForValue(I->getOperand(1), ValueMap);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000134 if (Op1 == 0)
135 // Unhandled operand. Halt "fast" selection and bail.
136 return false;
137
Dan Gohmanad368ac2008-08-27 18:10:19 +0000138 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000139 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
140 ISDOpcode, Op0, Op1);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000141 if (ResultReg == 0)
142 // Target-specific code wasn't able to find a machine opcode for
143 // the given ISD opcode and type. Halt "fast" selection and bail.
144 return false;
145
Dan Gohman8014e862008-08-20 00:23:20 +0000146 // We successfully emitted code for the given LLVM Instruction.
Owen Andersoncc54e762008-08-30 00:38:46 +0000147 UpdateValueMap(I, ResultReg, ValueMap);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000148 return true;
149}
150
151bool FastISel::SelectGetElementPtr(Instruction *I,
152 DenseMap<const Value*, unsigned> &ValueMap) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000153 unsigned N = getRegForValue(I->getOperand(0), ValueMap);
Evan Cheng83785c82008-08-20 22:45:34 +0000154 if (N == 0)
155 // Unhandled operand. Halt "fast" selection and bail.
156 return false;
157
158 const Type *Ty = I->getOperand(0)->getType();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000159 MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
Evan Cheng83785c82008-08-20 22:45:34 +0000160 for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
161 OI != E; ++OI) {
162 Value *Idx = *OI;
163 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
164 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
165 if (Field) {
166 // N = N + Offset
167 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
168 // FIXME: This can be optimized by combining the add with a
169 // subsequent one.
Dan Gohman7a0e6592008-08-21 17:25:26 +0000170 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000171 if (N == 0)
172 // Unhandled operand. Halt "fast" selection and bail.
173 return false;
174 }
175 Ty = StTy->getElementType(Field);
176 } else {
177 Ty = cast<SequentialType>(Ty)->getElementType();
178
179 // If this is a constant subscript, handle it quickly.
180 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
181 if (CI->getZExtValue() == 0) continue;
182 uint64_t Offs =
183 TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000184 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000185 if (N == 0)
186 // Unhandled operand. Halt "fast" selection and bail.
187 return false;
188 continue;
189 }
190
191 // N = N + Idx * ElementSize;
192 uint64_t ElementSize = TD.getABITypeSize(Ty);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000193 unsigned IdxN = getRegForValue(Idx, ValueMap);
Evan Cheng83785c82008-08-20 22:45:34 +0000194 if (IdxN == 0)
195 // Unhandled operand. Halt "fast" selection and bail.
196 return false;
197
198 // If the index is smaller or larger than intptr_t, truncate or extend
199 // it.
Evan Cheng2076aa82008-08-21 01:19:11 +0000200 MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
Evan Cheng83785c82008-08-20 22:45:34 +0000201 if (IdxVT.bitsLT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000202 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::SIGN_EXTEND, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000203 else if (IdxVT.bitsGT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000204 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::TRUNCATE, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000205 if (IdxN == 0)
206 // Unhandled operand. Halt "fast" selection and bail.
207 return false;
208
Dan Gohman80bc6e22008-08-26 20:57:08 +0000209 if (ElementSize != 1) {
Dan Gohmanf93cf792008-08-21 17:37:05 +0000210 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000211 if (IdxN == 0)
212 // Unhandled operand. Halt "fast" selection and bail.
213 return false;
214 }
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000215 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000216 if (N == 0)
217 // Unhandled operand. Halt "fast" selection and bail.
218 return false;
219 }
220 }
221
222 // We successfully emitted code for the given LLVM Instruction.
Owen Andersoncc54e762008-08-30 00:38:46 +0000223 UpdateValueMap(I, N, ValueMap);
Evan Cheng83785c82008-08-20 22:45:34 +0000224 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000225}
226
Owen Andersond0533c92008-08-26 23:46:32 +0000227bool FastISel::SelectCast(Instruction *I, ISD::NodeType Opcode,
228 DenseMap<const Value*, unsigned> &ValueMap) {
Owen Anderson6336b702008-08-27 18:58:30 +0000229 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
230 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000231
232 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
233 DstVT == MVT::Other || !DstVT.isSimple() ||
234 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
235 // Unhandled type. Halt "fast" selection and bail.
236 return false;
237
Dan Gohmanad368ac2008-08-27 18:10:19 +0000238 unsigned InputReg = getRegForValue(I->getOperand(0), ValueMap);
Owen Andersond0533c92008-08-26 23:46:32 +0000239 if (!InputReg)
240 // Unhandled operand. Halt "fast" selection and bail.
241 return false;
242
243 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
244 DstVT.getSimpleVT(),
245 Opcode,
246 InputReg);
247 if (!ResultReg)
248 return false;
249
Owen Andersoncc54e762008-08-30 00:38:46 +0000250 UpdateValueMap(I, ResultReg, ValueMap);
Owen Andersond0533c92008-08-26 23:46:32 +0000251 return true;
252}
253
Dan Gohmanad368ac2008-08-27 18:10:19 +0000254bool FastISel::SelectBitCast(Instruction *I,
255 DenseMap<const Value*, unsigned> &ValueMap) {
256 // If the bitcast doesn't change the type, just use the operand value.
257 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohmana318dab2008-08-27 20:41:38 +0000258 unsigned Reg = getRegForValue(I->getOperand(0), ValueMap);
259 if (Reg == 0)
260 return false;
Owen Andersoncc54e762008-08-30 00:38:46 +0000261 UpdateValueMap(I, Reg, ValueMap);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000262 return true;
263 }
264
265 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Anderson6336b702008-08-27 18:58:30 +0000266 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
267 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000268
269 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
270 DstVT == MVT::Other || !DstVT.isSimple() ||
271 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
272 // Unhandled type. Halt "fast" selection and bail.
273 return false;
274
Dan Gohmanad368ac2008-08-27 18:10:19 +0000275 unsigned Op0 = getRegForValue(I->getOperand(0), ValueMap);
276 if (Op0 == 0)
277 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000278 return false;
279
Dan Gohmanad368ac2008-08-27 18:10:19 +0000280 // First, try to perform the bitcast by inserting a reg-reg copy.
281 unsigned ResultReg = 0;
282 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
283 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
284 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
285 ResultReg = createResultReg(DstClass);
286
287 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
288 Op0, DstClass, SrcClass);
289 if (!InsertedCopy)
290 ResultReg = 0;
291 }
292
293 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
294 if (!ResultReg)
295 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
296 ISD::BIT_CONVERT, Op0);
297
298 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000299 return false;
300
Owen Andersoncc54e762008-08-30 00:38:46 +0000301 UpdateValueMap(I, ResultReg, ValueMap);
Owen Andersond0533c92008-08-26 23:46:32 +0000302 return true;
303}
304
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000305BasicBlock::iterator
Dan Gohmanb7864a92008-08-20 18:09:02 +0000306FastISel::SelectInstructions(BasicBlock::iterator Begin,
307 BasicBlock::iterator End,
Dan Gohmanbb466332008-08-20 21:05:57 +0000308 DenseMap<const Value*, unsigned> &ValueMap,
Dan Gohman6ecf5092008-08-23 02:44:46 +0000309 DenseMap<const BasicBlock*,
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000310 MachineBasicBlock *> &MBBMap,
Dan Gohmanbb466332008-08-20 21:05:57 +0000311 MachineBasicBlock *mbb) {
312 MBB = mbb;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000313 BasicBlock::iterator I = Begin;
314
315 for (; I != End; ++I) {
316 switch (I->getOpcode()) {
Dan Gohman8014e862008-08-20 00:23:20 +0000317 case Instruction::Add: {
318 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
319 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
320 }
321 case Instruction::Sub: {
322 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
323 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
324 }
325 case Instruction::Mul: {
326 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
327 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
328 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000329 case Instruction::SDiv:
330 if (!SelectBinaryOp(I, ISD::SDIV, ValueMap)) return I; break;
331 case Instruction::UDiv:
332 if (!SelectBinaryOp(I, ISD::UDIV, ValueMap)) return I; break;
333 case Instruction::FDiv:
334 if (!SelectBinaryOp(I, ISD::FDIV, ValueMap)) return I; break;
335 case Instruction::SRem:
336 if (!SelectBinaryOp(I, ISD::SREM, ValueMap)) return I; break;
337 case Instruction::URem:
338 if (!SelectBinaryOp(I, ISD::UREM, ValueMap)) return I; break;
339 case Instruction::FRem:
340 if (!SelectBinaryOp(I, ISD::FREM, ValueMap)) return I; break;
341 case Instruction::Shl:
342 if (!SelectBinaryOp(I, ISD::SHL, ValueMap)) return I; break;
343 case Instruction::LShr:
344 if (!SelectBinaryOp(I, ISD::SRL, ValueMap)) return I; break;
345 case Instruction::AShr:
346 if (!SelectBinaryOp(I, ISD::SRA, ValueMap)) return I; break;
347 case Instruction::And:
348 if (!SelectBinaryOp(I, ISD::AND, ValueMap)) return I; break;
349 case Instruction::Or:
350 if (!SelectBinaryOp(I, ISD::OR, ValueMap)) return I; break;
351 case Instruction::Xor:
352 if (!SelectBinaryOp(I, ISD::XOR, ValueMap)) return I; break;
353
354 case Instruction::GetElementPtr:
355 if (!SelectGetElementPtr(I, ValueMap)) return I;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000356 break;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000357
Dan Gohman6f2766d2008-08-19 22:31:46 +0000358 case Instruction::Br: {
359 BranchInst *BI = cast<BranchInst>(I);
360
Dan Gohmane6798b72008-08-20 01:17:01 +0000361 if (BI->isUnconditional()) {
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000362 MachineFunction::iterator NextMBB =
Dan Gohmane6798b72008-08-20 01:17:01 +0000363 next(MachineFunction::iterator(MBB));
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000364 BasicBlock *LLVMSucc = BI->getSuccessor(0);
365 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
366
367 if (NextMBB != MF.end() && MSucc == NextMBB) {
368 // The unconditional fall-through case, which needs no instructions.
369 } else {
370 // The unconditional branch case.
371 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
Dan Gohmane6798b72008-08-20 01:17:01 +0000372 }
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000373 MBB->addSuccessor(MSucc);
374 break;
Dan Gohman6f2766d2008-08-19 22:31:46 +0000375 }
376
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000377 // Conditional branches are not handed yet.
378 // Halt "fast" selection and bail.
Dan Gohman6f2766d2008-08-19 22:31:46 +0000379 return I;
380 }
Dan Gohman3b7753b2008-08-22 17:37:48 +0000381
382 case Instruction::PHI:
383 // PHI nodes are already emitted.
384 break;
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000385
386 case Instruction::BitCast:
Owen Andersond0533c92008-08-26 23:46:32 +0000387 if (!SelectBitCast(I, ValueMap)) return I; break;
Owen Anderson46aa2f52008-08-26 17:44:42 +0000388
389 case Instruction::FPToSI:
Dan Gohmanad368ac2008-08-27 18:10:19 +0000390 if (!SelectCast(I, ISD::FP_TO_SINT, ValueMap)) return I;
Owen Andersond0533c92008-08-26 23:46:32 +0000391 break;
Owen Anderson97e25682008-08-26 23:14:49 +0000392 case Instruction::ZExt:
Dan Gohmanad368ac2008-08-27 18:10:19 +0000393 if (!SelectCast(I, ISD::ZERO_EXTEND, ValueMap)) return I;
Owen Andersond0533c92008-08-26 23:46:32 +0000394 break;
395 case Instruction::SExt:
Dan Gohmanad368ac2008-08-27 18:10:19 +0000396 if (!SelectCast(I, ISD::SIGN_EXTEND, ValueMap)) return I;
Owen Andersond0533c92008-08-26 23:46:32 +0000397 break;
Owen Andersonc0bb68b2008-08-28 18:26:01 +0000398 case Instruction::Trunc:
399 if (!SelectCast(I, ISD::TRUNCATE, ValueMap)) return I;
400 break;
Owen Andersona843b8d2008-08-26 20:37:00 +0000401 case Instruction::SIToFP:
Dan Gohmanad368ac2008-08-27 18:10:19 +0000402 if (!SelectCast(I, ISD::SINT_TO_FP, ValueMap)) return I;
Owen Andersond0533c92008-08-26 23:46:32 +0000403 break;
Dan Gohman763d8932008-08-26 21:28:54 +0000404
Owen Anderson9d5b4162008-08-27 00:31:01 +0000405 case Instruction::IntToPtr: // Deliberate fall-through.
406 case Instruction::PtrToInt: {
407 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
408 MVT DstVT = TLI.getValueType(I->getType());
409 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
Owen Anderson96c5ea82008-08-27 00:35:37 +0000410 if (ValueMap[I->getOperand(0)]) {
Owen Andersoncc54e762008-08-30 00:38:46 +0000411 UpdateValueMap(I, ValueMap[I->getOperand(0)], ValueMap);
Owen Anderson96c5ea82008-08-27 00:35:37 +0000412 break;
413 } else
414 // Unhandled operand
415 return I;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000416 } else if (DstVT.bitsGT(SrcVT)) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000417 if (!SelectCast(I, ISD::ZERO_EXTEND, ValueMap)) return I;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000418 break;
419 } else {
420 // TODO: Handle SrcVT > DstVT, where truncation is needed.
421 return I;
422 }
423 }
424
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000425 default:
426 // Unhandled instruction. Halt "fast" selection and bail.
427 return I;
428 }
429 }
430
431 return I;
432}
433
Dan Gohmanbb466332008-08-20 21:05:57 +0000434FastISel::FastISel(MachineFunction &mf)
Dan Gohman22bb3112008-08-22 00:20:26 +0000435 : MF(mf),
436 MRI(mf.getRegInfo()),
437 TM(mf.getTarget()),
438 TD(*TM.getTargetData()),
439 TII(*TM.getInstrInfo()),
440 TLI(*TM.getTargetLowering()) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000441}
442
Dan Gohmane285a742008-08-14 21:51:29 +0000443FastISel::~FastISel() {}
444
Evan Cheng36fd9412008-09-02 21:59:13 +0000445unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType,
446 ISD::NodeType) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000447 return 0;
448}
449
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000450unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
451 ISD::NodeType, unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000452 return 0;
453}
454
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000455unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType,
456 ISD::NodeType, unsigned /*Op0*/,
457 unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000458 return 0;
459}
460
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000461unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
462 ISD::NodeType, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000463 return 0;
464}
465
Dan Gohman10df0fa2008-08-27 01:09:54 +0000466unsigned FastISel::FastEmit_f(MVT::SimpleValueType, MVT::SimpleValueType,
467 ISD::NodeType, ConstantFP * /*FPImm*/) {
468 return 0;
469}
470
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000471unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
472 ISD::NodeType, unsigned /*Op0*/,
473 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000474 return 0;
475}
476
Dan Gohman10df0fa2008-08-27 01:09:54 +0000477unsigned FastISel::FastEmit_rf(MVT::SimpleValueType, MVT::SimpleValueType,
478 ISD::NodeType, unsigned /*Op0*/,
479 ConstantFP * /*FPImm*/) {
480 return 0;
481}
482
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000483unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
484 ISD::NodeType,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000485 unsigned /*Op0*/, unsigned /*Op1*/,
486 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000487 return 0;
488}
489
490/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
491/// to emit an instruction with an immediate operand using FastEmit_ri.
492/// If that fails, it materializes the immediate into a register and try
493/// FastEmit_rr instead.
494unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000495 unsigned Op0, uint64_t Imm,
496 MVT::SimpleValueType ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000497 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohman151ed612008-08-27 18:15:05 +0000498 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000499 if (ResultReg != 0)
500 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000501 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000502 if (MaterialReg == 0)
503 return 0;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000504 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000505}
506
Dan Gohman10df0fa2008-08-27 01:09:54 +0000507/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
508/// to emit an instruction with a floating-point immediate operand using
509/// FastEmit_rf. If that fails, it materializes the immediate into a register
510/// and try FastEmit_rr instead.
511unsigned FastISel::FastEmit_rf_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
512 unsigned Op0, ConstantFP *FPImm,
513 MVT::SimpleValueType ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000514 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohman151ed612008-08-27 18:15:05 +0000515 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000516 if (ResultReg != 0)
517 return ResultReg;
518
519 // Materialize the constant in a register.
520 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
521 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000522 // If the target doesn't have a way to directly enter a floating-point
523 // value into a register, use an alternate approach.
524 // TODO: The current approach only supports floating-point constants
525 // that can be constructed by conversion from integer values. This should
526 // be replaced by code that creates a load from a constant-pool entry,
527 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000528 const APFloat &Flt = FPImm->getValueAPF();
529 MVT IntVT = TLI.getPointerTy();
530
531 uint64_t x[2];
532 uint32_t IntBitWidth = IntVT.getSizeInBits();
533 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
534 APFloat::rmTowardZero) != APFloat::opOK)
535 return 0;
536 APInt IntVal(IntBitWidth, 2, x);
537
538 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
539 ISD::Constant, IntVal.getZExtValue());
540 if (IntegerReg == 0)
541 return 0;
542 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
543 ISD::SINT_TO_FP, IntegerReg);
544 if (MaterialReg == 0)
545 return 0;
546 }
547 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
548}
549
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000550unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
551 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000552}
553
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000554unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000555 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000556 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000557 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000558
Dan Gohmanfd903942008-08-20 23:53:10 +0000559 BuildMI(MBB, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000560 return ResultReg;
561}
562
563unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
564 const TargetRegisterClass *RC,
565 unsigned Op0) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000566 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000567 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000568
Dan Gohmanfd903942008-08-20 23:53:10 +0000569 BuildMI(MBB, II, ResultReg).addReg(Op0);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000570 return ResultReg;
571}
572
573unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
574 const TargetRegisterClass *RC,
575 unsigned Op0, unsigned Op1) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000576 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000577 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000578
Dan Gohmanfd903942008-08-20 23:53:10 +0000579 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000580 return ResultReg;
581}
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000582
583unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
584 const TargetRegisterClass *RC,
585 unsigned Op0, uint64_t Imm) {
586 unsigned ResultReg = createResultReg(RC);
587 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
588
589 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
590 return ResultReg;
591}
592
Dan Gohman10df0fa2008-08-27 01:09:54 +0000593unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
594 const TargetRegisterClass *RC,
595 unsigned Op0, ConstantFP *FPImm) {
596 unsigned ResultReg = createResultReg(RC);
597 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
598
599 BuildMI(MBB, II, ResultReg).addReg(Op0).addFPImm(FPImm);
600 return ResultReg;
601}
602
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000603unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
604 const TargetRegisterClass *RC,
605 unsigned Op0, unsigned Op1, uint64_t Imm) {
606 unsigned ResultReg = createResultReg(RC);
607 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
608
609 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
610 return ResultReg;
611}
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000612
613unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
614 const TargetRegisterClass *RC,
615 uint64_t Imm) {
616 unsigned ResultReg = createResultReg(RC);
617 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
618
619 BuildMI(MBB, II, ResultReg).addImm(Imm);
620 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +0000621}
Owen Anderson8970f002008-08-27 22:30:02 +0000622
Owen Anderson40a468f2008-08-28 17:47:37 +0000623unsigned FastISel::FastEmitInst_extractsubreg(unsigned Op0, uint32_t Idx) {
624 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson8970f002008-08-27 22:30:02 +0000625 const TargetRegisterClass* SRC = *(RC->subregclasses_begin()+Idx-1);
626
627 unsigned ResultReg = createResultReg(SRC);
628 const TargetInstrDesc &II = TII.get(TargetInstrInfo::EXTRACT_SUBREG);
629
Owen Andersonc0bb68b2008-08-28 18:26:01 +0000630 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Idx);
Owen Anderson8970f002008-08-27 22:30:02 +0000631 return ResultReg;
632}