blob: 45920ba4906bc4fd82de91640fd6143085c3f0ff [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 Gohmanad368ac2008-08-27 18:10:19 +000024unsigned FastISel::getRegForValue(Value *V, DenseMap<const Value*, unsigned> &ValueMap) {
25 unsigned &Reg = ValueMap[V];
26 if (Reg != 0)
27 return Reg;
28
29 MVT::SimpleValueType VT = TLI.getValueType(V->getType()).getSimpleVT();
30 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
31 if (CI->getValue().getActiveBits() > 64)
32 return 0;
33 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman205d9252008-08-28 21:19:07 +000034 } else if (isa<ConstantPointerNull>(V)) {
35 Reg = FastEmit_i(VT, VT, ISD::Constant, 0);
Dan Gohmanad368ac2008-08-27 18:10:19 +000036 } else if (ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
37 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
38
39 if (!Reg) {
40 const APFloat &Flt = CF->getValueAPF();
41 MVT IntVT = TLI.getPointerTy();
42
43 uint64_t x[2];
44 uint32_t IntBitWidth = IntVT.getSizeInBits();
45 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
46 APFloat::rmTowardZero) != APFloat::opOK)
47 return 0;
48 APInt IntVal(IntBitWidth, 2, x);
49
50 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
51 ISD::Constant, IntVal.getZExtValue());
52 if (IntegerReg == 0)
53 return 0;
54 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
55 if (Reg == 0)
56 return 0;
57 }
Dan Gohman205d9252008-08-28 21:19:07 +000058 } else if (isa<UndefValue>(V)) {
59 Reg = createResultReg(TLI.getRegClassFor(VT));
60 BuildMI(MBB, TII.get(TargetInstrInfo::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +000061 }
62
63 return Reg;
64}
65
Owen Andersoncc54e762008-08-30 00:38:46 +000066/// UpdateValueMap - Update the value map to include the new mapping for this
67/// instruction, or insert an extra copy to get the result in a previous
68/// determined register.
69/// NOTE: This is only necessary because we might select a block that uses
70/// a value before we select the block that defines the value. It might be
71/// possible to fix this by selecting blocks in reverse postorder.
72void FastISel::UpdateValueMap(Instruction* I, unsigned Reg,
73 DenseMap<const Value*, unsigned> &ValueMap) {
74 if (!ValueMap.count(I))
75 ValueMap[I] = Reg;
76 else
77 TII.copyRegToReg(*MBB, MBB->end(), ValueMap[I],
78 Reg, MRI.getRegClass(Reg), MRI.getRegClass(Reg));
79}
80
Dan Gohmanbdedd442008-08-20 00:11:48 +000081/// SelectBinaryOp - Select and emit code for a binary operator instruction,
82/// which has an opcode which directly corresponds to the given ISD opcode.
83///
84bool FastISel::SelectBinaryOp(Instruction *I, ISD::NodeType ISDOpcode,
85 DenseMap<const Value*, unsigned> &ValueMap) {
Dan Gohmanbdedd442008-08-20 00:11:48 +000086 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
87 if (VT == MVT::Other || !VT.isSimple())
88 // Unhandled type. Halt "fast" selection and bail.
89 return false;
Dan Gohmanb71fea22008-08-26 20:52:40 +000090 // We only handle legal types. For example, on x86-32 the instruction
91 // selector contains all of the 64-bit instructions from x86-64,
92 // under the assumption that i64 won't be used if the target doesn't
93 // support it.
94 if (!TLI.isTypeLegal(VT))
95 return false;
Dan Gohmanbdedd442008-08-20 00:11:48 +000096
Dan Gohmanad368ac2008-08-27 18:10:19 +000097 unsigned Op0 = getRegForValue(I->getOperand(0), ValueMap);
Dan Gohmand5fe57d2008-08-21 01:41:07 +000098 if (Op0 == 0)
99 // Unhandled operand. Halt "fast" selection and bail.
100 return false;
101
102 // Check if the second operand is a constant and handle it appropriately.
103 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000104 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
105 ISDOpcode, Op0, CI->getZExtValue());
106 if (ResultReg != 0) {
107 // We successfully emitted code for the given LLVM Instruction.
Owen Andersoncc54e762008-08-30 00:38:46 +0000108 UpdateValueMap(I, ResultReg, ValueMap);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000109 return true;
110 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000111 }
112
Dan Gohman10df0fa2008-08-27 01:09:54 +0000113 // Check if the second operand is a constant float.
114 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000115 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
116 ISDOpcode, Op0, CF);
117 if (ResultReg != 0) {
118 // We successfully emitted code for the given LLVM Instruction.
Owen Andersoncc54e762008-08-30 00:38:46 +0000119 UpdateValueMap(I, ResultReg, ValueMap);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000120 return true;
121 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000122 }
123
Dan Gohmanad368ac2008-08-27 18:10:19 +0000124 unsigned Op1 = getRegForValue(I->getOperand(1), ValueMap);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000125 if (Op1 == 0)
126 // Unhandled operand. Halt "fast" selection and bail.
127 return false;
128
Dan Gohmanad368ac2008-08-27 18:10:19 +0000129 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000130 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
131 ISDOpcode, Op0, Op1);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000132 if (ResultReg == 0)
133 // Target-specific code wasn't able to find a machine opcode for
134 // the given ISD opcode and type. Halt "fast" selection and bail.
135 return false;
136
Dan Gohman8014e862008-08-20 00:23:20 +0000137 // We successfully emitted code for the given LLVM Instruction.
Owen Andersoncc54e762008-08-30 00:38:46 +0000138 UpdateValueMap(I, ResultReg, ValueMap);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000139 return true;
140}
141
142bool FastISel::SelectGetElementPtr(Instruction *I,
143 DenseMap<const Value*, unsigned> &ValueMap) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000144 unsigned N = getRegForValue(I->getOperand(0), ValueMap);
Evan Cheng83785c82008-08-20 22:45:34 +0000145 if (N == 0)
146 // Unhandled operand. Halt "fast" selection and bail.
147 return false;
148
149 const Type *Ty = I->getOperand(0)->getType();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000150 MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
Evan Cheng83785c82008-08-20 22:45:34 +0000151 for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
152 OI != E; ++OI) {
153 Value *Idx = *OI;
154 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
155 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
156 if (Field) {
157 // N = N + Offset
158 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
159 // FIXME: This can be optimized by combining the add with a
160 // subsequent one.
Dan Gohman7a0e6592008-08-21 17:25:26 +0000161 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000162 if (N == 0)
163 // Unhandled operand. Halt "fast" selection and bail.
164 return false;
165 }
166 Ty = StTy->getElementType(Field);
167 } else {
168 Ty = cast<SequentialType>(Ty)->getElementType();
169
170 // If this is a constant subscript, handle it quickly.
171 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
172 if (CI->getZExtValue() == 0) continue;
173 uint64_t Offs =
174 TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000175 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000176 if (N == 0)
177 // Unhandled operand. Halt "fast" selection and bail.
178 return false;
179 continue;
180 }
181
182 // N = N + Idx * ElementSize;
183 uint64_t ElementSize = TD.getABITypeSize(Ty);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000184 unsigned IdxN = getRegForValue(Idx, ValueMap);
Evan Cheng83785c82008-08-20 22:45:34 +0000185 if (IdxN == 0)
186 // Unhandled operand. Halt "fast" selection and bail.
187 return false;
188
189 // If the index is smaller or larger than intptr_t, truncate or extend
190 // it.
Evan Cheng2076aa82008-08-21 01:19:11 +0000191 MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
Evan Cheng83785c82008-08-20 22:45:34 +0000192 if (IdxVT.bitsLT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000193 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::SIGN_EXTEND, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000194 else if (IdxVT.bitsGT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000195 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::TRUNCATE, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000196 if (IdxN == 0)
197 // Unhandled operand. Halt "fast" selection and bail.
198 return false;
199
Dan Gohman80bc6e22008-08-26 20:57:08 +0000200 if (ElementSize != 1) {
Dan Gohmanf93cf792008-08-21 17:37:05 +0000201 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000202 if (IdxN == 0)
203 // Unhandled operand. Halt "fast" selection and bail.
204 return false;
205 }
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000206 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000207 if (N == 0)
208 // Unhandled operand. Halt "fast" selection and bail.
209 return false;
210 }
211 }
212
213 // We successfully emitted code for the given LLVM Instruction.
Owen Andersoncc54e762008-08-30 00:38:46 +0000214 UpdateValueMap(I, N, ValueMap);
Evan Cheng83785c82008-08-20 22:45:34 +0000215 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000216}
217
Owen Andersond0533c92008-08-26 23:46:32 +0000218bool FastISel::SelectCast(Instruction *I, ISD::NodeType Opcode,
219 DenseMap<const Value*, unsigned> &ValueMap) {
Owen Anderson6336b702008-08-27 18:58:30 +0000220 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
221 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000222
223 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
224 DstVT == MVT::Other || !DstVT.isSimple() ||
225 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
226 // Unhandled type. Halt "fast" selection and bail.
227 return false;
228
Dan Gohmanad368ac2008-08-27 18:10:19 +0000229 unsigned InputReg = getRegForValue(I->getOperand(0), ValueMap);
Owen Andersond0533c92008-08-26 23:46:32 +0000230 if (!InputReg)
231 // Unhandled operand. Halt "fast" selection and bail.
232 return false;
233
234 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
235 DstVT.getSimpleVT(),
236 Opcode,
237 InputReg);
238 if (!ResultReg)
239 return false;
240
Owen Andersoncc54e762008-08-30 00:38:46 +0000241 UpdateValueMap(I, ResultReg, ValueMap);
Owen Andersond0533c92008-08-26 23:46:32 +0000242 return true;
243}
244
Dan Gohmanad368ac2008-08-27 18:10:19 +0000245bool FastISel::SelectBitCast(Instruction *I,
246 DenseMap<const Value*, unsigned> &ValueMap) {
247 // If the bitcast doesn't change the type, just use the operand value.
248 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohmana318dab2008-08-27 20:41:38 +0000249 unsigned Reg = getRegForValue(I->getOperand(0), ValueMap);
250 if (Reg == 0)
251 return false;
Owen Andersoncc54e762008-08-30 00:38:46 +0000252 UpdateValueMap(I, Reg, ValueMap);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000253 return true;
254 }
255
256 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Anderson6336b702008-08-27 18:58:30 +0000257 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
258 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000259
260 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
261 DstVT == MVT::Other || !DstVT.isSimple() ||
262 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
263 // Unhandled type. Halt "fast" selection and bail.
264 return false;
265
Dan Gohmanad368ac2008-08-27 18:10:19 +0000266 unsigned Op0 = getRegForValue(I->getOperand(0), ValueMap);
267 if (Op0 == 0)
268 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000269 return false;
270
Dan Gohmanad368ac2008-08-27 18:10:19 +0000271 // First, try to perform the bitcast by inserting a reg-reg copy.
272 unsigned ResultReg = 0;
273 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
274 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
275 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
276 ResultReg = createResultReg(DstClass);
277
278 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
279 Op0, DstClass, SrcClass);
280 if (!InsertedCopy)
281 ResultReg = 0;
282 }
283
284 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
285 if (!ResultReg)
286 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
287 ISD::BIT_CONVERT, Op0);
288
289 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000290 return false;
291
Owen Andersoncc54e762008-08-30 00:38:46 +0000292 UpdateValueMap(I, ResultReg, ValueMap);
Owen Andersond0533c92008-08-26 23:46:32 +0000293 return true;
294}
295
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000296BasicBlock::iterator
Dan Gohmanb7864a92008-08-20 18:09:02 +0000297FastISel::SelectInstructions(BasicBlock::iterator Begin,
298 BasicBlock::iterator End,
Dan Gohmanbb466332008-08-20 21:05:57 +0000299 DenseMap<const Value*, unsigned> &ValueMap,
Dan Gohman6ecf5092008-08-23 02:44:46 +0000300 DenseMap<const BasicBlock*,
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000301 MachineBasicBlock *> &MBBMap,
Dan Gohmanbb466332008-08-20 21:05:57 +0000302 MachineBasicBlock *mbb) {
303 MBB = mbb;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000304 BasicBlock::iterator I = Begin;
305
306 for (; I != End; ++I) {
307 switch (I->getOpcode()) {
Dan Gohman8014e862008-08-20 00:23:20 +0000308 case Instruction::Add: {
309 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
310 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
311 }
312 case Instruction::Sub: {
313 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
314 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
315 }
316 case Instruction::Mul: {
317 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
318 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
319 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000320 case Instruction::SDiv:
321 if (!SelectBinaryOp(I, ISD::SDIV, ValueMap)) return I; break;
322 case Instruction::UDiv:
323 if (!SelectBinaryOp(I, ISD::UDIV, ValueMap)) return I; break;
324 case Instruction::FDiv:
325 if (!SelectBinaryOp(I, ISD::FDIV, ValueMap)) return I; break;
326 case Instruction::SRem:
327 if (!SelectBinaryOp(I, ISD::SREM, ValueMap)) return I; break;
328 case Instruction::URem:
329 if (!SelectBinaryOp(I, ISD::UREM, ValueMap)) return I; break;
330 case Instruction::FRem:
331 if (!SelectBinaryOp(I, ISD::FREM, ValueMap)) return I; break;
332 case Instruction::Shl:
333 if (!SelectBinaryOp(I, ISD::SHL, ValueMap)) return I; break;
334 case Instruction::LShr:
335 if (!SelectBinaryOp(I, ISD::SRL, ValueMap)) return I; break;
336 case Instruction::AShr:
337 if (!SelectBinaryOp(I, ISD::SRA, ValueMap)) return I; break;
338 case Instruction::And:
339 if (!SelectBinaryOp(I, ISD::AND, ValueMap)) return I; break;
340 case Instruction::Or:
341 if (!SelectBinaryOp(I, ISD::OR, ValueMap)) return I; break;
342 case Instruction::Xor:
343 if (!SelectBinaryOp(I, ISD::XOR, ValueMap)) return I; break;
344
345 case Instruction::GetElementPtr:
346 if (!SelectGetElementPtr(I, ValueMap)) return I;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000347 break;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000348
Dan Gohman6f2766d2008-08-19 22:31:46 +0000349 case Instruction::Br: {
350 BranchInst *BI = cast<BranchInst>(I);
351
Dan Gohmane6798b72008-08-20 01:17:01 +0000352 if (BI->isUnconditional()) {
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000353 MachineFunction::iterator NextMBB =
Dan Gohmane6798b72008-08-20 01:17:01 +0000354 next(MachineFunction::iterator(MBB));
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000355 BasicBlock *LLVMSucc = BI->getSuccessor(0);
356 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
357
358 if (NextMBB != MF.end() && MSucc == NextMBB) {
359 // The unconditional fall-through case, which needs no instructions.
360 } else {
361 // The unconditional branch case.
362 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
Dan Gohmane6798b72008-08-20 01:17:01 +0000363 }
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000364 MBB->addSuccessor(MSucc);
365 break;
Dan Gohman6f2766d2008-08-19 22:31:46 +0000366 }
367
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000368 // Conditional branches are not handed yet.
369 // Halt "fast" selection and bail.
Dan Gohman6f2766d2008-08-19 22:31:46 +0000370 return I;
371 }
Dan Gohman3b7753b2008-08-22 17:37:48 +0000372
373 case Instruction::PHI:
374 // PHI nodes are already emitted.
375 break;
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000376
377 case Instruction::BitCast:
Owen Andersond0533c92008-08-26 23:46:32 +0000378 if (!SelectBitCast(I, ValueMap)) return I; break;
Owen Anderson46aa2f52008-08-26 17:44:42 +0000379
380 case Instruction::FPToSI:
Dan Gohmanad368ac2008-08-27 18:10:19 +0000381 if (!SelectCast(I, ISD::FP_TO_SINT, ValueMap)) return I;
Owen Andersond0533c92008-08-26 23:46:32 +0000382 break;
Owen Anderson97e25682008-08-26 23:14:49 +0000383 case Instruction::ZExt:
Dan Gohmanad368ac2008-08-27 18:10:19 +0000384 if (!SelectCast(I, ISD::ZERO_EXTEND, ValueMap)) return I;
Owen Andersond0533c92008-08-26 23:46:32 +0000385 break;
386 case Instruction::SExt:
Dan Gohmanad368ac2008-08-27 18:10:19 +0000387 if (!SelectCast(I, ISD::SIGN_EXTEND, ValueMap)) return I;
Owen Andersond0533c92008-08-26 23:46:32 +0000388 break;
Owen Andersonc0bb68b2008-08-28 18:26:01 +0000389 case Instruction::Trunc:
390 if (!SelectCast(I, ISD::TRUNCATE, ValueMap)) return I;
391 break;
Owen Andersona843b8d2008-08-26 20:37:00 +0000392 case Instruction::SIToFP:
Dan Gohmanad368ac2008-08-27 18:10:19 +0000393 if (!SelectCast(I, ISD::SINT_TO_FP, ValueMap)) return I;
Owen Andersond0533c92008-08-26 23:46:32 +0000394 break;
Dan Gohman763d8932008-08-26 21:28:54 +0000395
Owen Anderson9d5b4162008-08-27 00:31:01 +0000396 case Instruction::IntToPtr: // Deliberate fall-through.
397 case Instruction::PtrToInt: {
398 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
399 MVT DstVT = TLI.getValueType(I->getType());
400 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
Owen Anderson96c5ea82008-08-27 00:35:37 +0000401 if (ValueMap[I->getOperand(0)]) {
Owen Andersoncc54e762008-08-30 00:38:46 +0000402 UpdateValueMap(I, ValueMap[I->getOperand(0)], ValueMap);
Owen Anderson96c5ea82008-08-27 00:35:37 +0000403 break;
404 } else
405 // Unhandled operand
406 return I;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000407 } else if (DstVT.bitsGT(SrcVT)) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000408 if (!SelectCast(I, ISD::ZERO_EXTEND, ValueMap)) return I;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000409 break;
410 } else {
411 // TODO: Handle SrcVT > DstVT, where truncation is needed.
412 return I;
413 }
414 }
415
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000416 default:
417 // Unhandled instruction. Halt "fast" selection and bail.
418 return I;
419 }
420 }
421
422 return I;
423}
424
Dan Gohmanbb466332008-08-20 21:05:57 +0000425FastISel::FastISel(MachineFunction &mf)
Dan Gohman22bb3112008-08-22 00:20:26 +0000426 : MF(mf),
427 MRI(mf.getRegInfo()),
428 TM(mf.getTarget()),
429 TD(*TM.getTargetData()),
430 TII(*TM.getInstrInfo()),
431 TLI(*TM.getTargetLowering()) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000432}
433
Dan Gohmane285a742008-08-14 21:51:29 +0000434FastISel::~FastISel() {}
435
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000436unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType, ISD::NodeType) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000437 return 0;
438}
439
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000440unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
441 ISD::NodeType, unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000442 return 0;
443}
444
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000445unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType,
446 ISD::NodeType, unsigned /*Op0*/,
447 unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000448 return 0;
449}
450
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000451unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
452 ISD::NodeType, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000453 return 0;
454}
455
Dan Gohman10df0fa2008-08-27 01:09:54 +0000456unsigned FastISel::FastEmit_f(MVT::SimpleValueType, MVT::SimpleValueType,
457 ISD::NodeType, ConstantFP * /*FPImm*/) {
458 return 0;
459}
460
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000461unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
462 ISD::NodeType, unsigned /*Op0*/,
463 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000464 return 0;
465}
466
Dan Gohman10df0fa2008-08-27 01:09:54 +0000467unsigned FastISel::FastEmit_rf(MVT::SimpleValueType, MVT::SimpleValueType,
468 ISD::NodeType, unsigned /*Op0*/,
469 ConstantFP * /*FPImm*/) {
470 return 0;
471}
472
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000473unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
474 ISD::NodeType,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000475 unsigned /*Op0*/, unsigned /*Op1*/,
476 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000477 return 0;
478}
479
480/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
481/// to emit an instruction with an immediate operand using FastEmit_ri.
482/// If that fails, it materializes the immediate into a register and try
483/// FastEmit_rr instead.
484unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000485 unsigned Op0, uint64_t Imm,
486 MVT::SimpleValueType ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000487 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohman151ed612008-08-27 18:15:05 +0000488 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000489 if (ResultReg != 0)
490 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000491 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000492 if (MaterialReg == 0)
493 return 0;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000494 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000495}
496
Dan Gohman10df0fa2008-08-27 01:09:54 +0000497/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
498/// to emit an instruction with a floating-point immediate operand using
499/// FastEmit_rf. If that fails, it materializes the immediate into a register
500/// and try FastEmit_rr instead.
501unsigned FastISel::FastEmit_rf_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
502 unsigned Op0, ConstantFP *FPImm,
503 MVT::SimpleValueType ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000504 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohman151ed612008-08-27 18:15:05 +0000505 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000506 if (ResultReg != 0)
507 return ResultReg;
508
509 // Materialize the constant in a register.
510 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
511 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000512 // If the target doesn't have a way to directly enter a floating-point
513 // value into a register, use an alternate approach.
514 // TODO: The current approach only supports floating-point constants
515 // that can be constructed by conversion from integer values. This should
516 // be replaced by code that creates a load from a constant-pool entry,
517 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000518 const APFloat &Flt = FPImm->getValueAPF();
519 MVT IntVT = TLI.getPointerTy();
520
521 uint64_t x[2];
522 uint32_t IntBitWidth = IntVT.getSizeInBits();
523 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
524 APFloat::rmTowardZero) != APFloat::opOK)
525 return 0;
526 APInt IntVal(IntBitWidth, 2, x);
527
528 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
529 ISD::Constant, IntVal.getZExtValue());
530 if (IntegerReg == 0)
531 return 0;
532 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
533 ISD::SINT_TO_FP, IntegerReg);
534 if (MaterialReg == 0)
535 return 0;
536 }
537 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
538}
539
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000540unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
541 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000542}
543
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000544unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000545 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000546 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000547 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000548
Dan Gohmanfd903942008-08-20 23:53:10 +0000549 BuildMI(MBB, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000550 return ResultReg;
551}
552
553unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
554 const TargetRegisterClass *RC,
555 unsigned Op0) {
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).addReg(Op0);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000560 return ResultReg;
561}
562
563unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
564 const TargetRegisterClass *RC,
565 unsigned Op0, unsigned Op1) {
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).addReg(Op1);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000570 return ResultReg;
571}
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000572
573unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
574 const TargetRegisterClass *RC,
575 unsigned Op0, uint64_t Imm) {
576 unsigned ResultReg = createResultReg(RC);
577 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
578
579 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
580 return ResultReg;
581}
582
Dan Gohman10df0fa2008-08-27 01:09:54 +0000583unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
584 const TargetRegisterClass *RC,
585 unsigned Op0, ConstantFP *FPImm) {
586 unsigned ResultReg = createResultReg(RC);
587 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
588
589 BuildMI(MBB, II, ResultReg).addReg(Op0).addFPImm(FPImm);
590 return ResultReg;
591}
592
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000593unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
594 const TargetRegisterClass *RC,
595 unsigned Op0, unsigned Op1, uint64_t Imm) {
596 unsigned ResultReg = createResultReg(RC);
597 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
598
599 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
600 return ResultReg;
601}
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000602
603unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
604 const TargetRegisterClass *RC,
605 uint64_t Imm) {
606 unsigned ResultReg = createResultReg(RC);
607 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
608
609 BuildMI(MBB, II, ResultReg).addImm(Imm);
610 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +0000611}
Owen Anderson8970f002008-08-27 22:30:02 +0000612
Owen Anderson40a468f2008-08-28 17:47:37 +0000613unsigned FastISel::FastEmitInst_extractsubreg(unsigned Op0, uint32_t Idx) {
614 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson8970f002008-08-27 22:30:02 +0000615 const TargetRegisterClass* SRC = *(RC->subregclasses_begin()+Idx-1);
616
617 unsigned ResultReg = createResultReg(SRC);
618 const TargetInstrDesc &II = TII.get(TargetInstrInfo::EXTRACT_SUBREG);
619
Owen Andersonc0bb68b2008-08-28 18:26:01 +0000620 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Idx);
Owen Anderson8970f002008-08-27 22:30:02 +0000621 return ResultReg;
622}