blob: 64cc5a8abee4b8390f513ea414cd2c727d68f4b1 [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 Gohman3df24e62008-09-03 23:12:08 +000024unsigned FastISel::getRegForValue(Value *V) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000025 // Look up the value to see if we already have a register for it. We
26 // cache values defined by Instructions across blocks, and other values
27 // only locally. This is because Instructions already have the SSA
28 // def-dominatess-use requirement enforced.
Owen Anderson99aaf102008-09-03 17:37:03 +000029 if (ValueMap.count(V))
30 return ValueMap[V];
Dan Gohman104e4ce2008-09-03 23:32:19 +000031 unsigned Reg = LocalValueMap[V];
32 if (Reg != 0)
33 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +000034
35 MVT::SimpleValueType VT = TLI.getValueType(V->getType()).getSimpleVT();
36 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
37 if (CI->getValue().getActiveBits() > 64)
38 return 0;
Owen Anderson99aaf102008-09-03 17:37:03 +000039 // Don't cache constant materializations. To do so would require
40 // tracking what uses they dominate.
Dan Gohman104e4ce2008-09-03 23:32:19 +000041 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman205d9252008-08-28 21:19:07 +000042 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000043 Reg = FastEmit_i(VT, VT, ISD::Constant, 0);
Dan Gohmanad368ac2008-08-27 18:10:19 +000044 } else if (ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000045 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +000046
47 if (!Reg) {
48 const APFloat &Flt = CF->getValueAPF();
49 MVT IntVT = TLI.getPointerTy();
50
51 uint64_t x[2];
52 uint32_t IntBitWidth = IntVT.getSizeInBits();
53 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
54 APFloat::rmTowardZero) != APFloat::opOK)
55 return 0;
56 APInt IntVal(IntBitWidth, 2, x);
57
58 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
59 ISD::Constant, IntVal.getZExtValue());
60 if (IntegerReg == 0)
61 return 0;
62 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
63 if (Reg == 0)
64 return 0;
65 }
Dan Gohman205d9252008-08-28 21:19:07 +000066 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000067 Reg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman205d9252008-08-28 21:19:07 +000068 BuildMI(MBB, TII.get(TargetInstrInfo::IMPLICIT_DEF), Reg);
Dan Gohman104e4ce2008-09-03 23:32:19 +000069 } else {
70 return 0;
Dan Gohmanad368ac2008-08-27 18:10:19 +000071 }
Owen Andersond5d81a42008-09-03 17:51:57 +000072
Dan Gohman104e4ce2008-09-03 23:32:19 +000073 LocalValueMap[V] = Reg;
74 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +000075}
76
Owen Andersoncc54e762008-08-30 00:38:46 +000077/// UpdateValueMap - Update the value map to include the new mapping for this
78/// instruction, or insert an extra copy to get the result in a previous
79/// determined register.
80/// NOTE: This is only necessary because we might select a block that uses
81/// a value before we select the block that defines the value. It might be
82/// possible to fix this by selecting blocks in reverse postorder.
Owen Anderson95267a12008-09-05 00:06:23 +000083void FastISel::UpdateValueMap(Value* I, unsigned Reg) {
Owen Andersoncc54e762008-08-30 00:38:46 +000084 if (!ValueMap.count(I))
85 ValueMap[I] = Reg;
86 else
87 TII.copyRegToReg(*MBB, MBB->end(), ValueMap[I],
88 Reg, MRI.getRegClass(Reg), MRI.getRegClass(Reg));
89}
90
Dan Gohmanbdedd442008-08-20 00:11:48 +000091/// SelectBinaryOp - Select and emit code for a binary operator instruction,
92/// which has an opcode which directly corresponds to the given ISD opcode.
93///
Dan Gohman3df24e62008-09-03 23:12:08 +000094bool FastISel::SelectBinaryOp(Instruction *I, ISD::NodeType ISDOpcode) {
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 Gohman3df24e62008-09-03 23:12:08 +0000106 unsigned Op0 = getRegForValue(I->getOperand(0));
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.
Dan Gohman3df24e62008-09-03 23:12:08 +0000117 UpdateValueMap(I, ResultReg);
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.
Dan Gohman3df24e62008-09-03 23:12:08 +0000128 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000129 return true;
130 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000131 }
132
Dan Gohman3df24e62008-09-03 23:12:08 +0000133 unsigned Op1 = getRegForValue(I->getOperand(1));
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.
Dan Gohman3df24e62008-09-03 23:12:08 +0000147 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000148 return true;
149}
150
Dan Gohman3df24e62008-09-03 23:12:08 +0000151bool FastISel::SelectGetElementPtr(Instruction *I) {
152 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000153 if (N == 0)
154 // Unhandled operand. Halt "fast" selection and bail.
155 return false;
156
157 const Type *Ty = I->getOperand(0)->getType();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000158 MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
Evan Cheng83785c82008-08-20 22:45:34 +0000159 for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
160 OI != E; ++OI) {
161 Value *Idx = *OI;
162 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
163 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
164 if (Field) {
165 // N = N + Offset
166 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
167 // FIXME: This can be optimized by combining the add with a
168 // subsequent one.
Dan Gohman7a0e6592008-08-21 17:25:26 +0000169 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000170 if (N == 0)
171 // Unhandled operand. Halt "fast" selection and bail.
172 return false;
173 }
174 Ty = StTy->getElementType(Field);
175 } else {
176 Ty = cast<SequentialType>(Ty)->getElementType();
177
178 // If this is a constant subscript, handle it quickly.
179 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
180 if (CI->getZExtValue() == 0) continue;
181 uint64_t Offs =
182 TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000183 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000184 if (N == 0)
185 // Unhandled operand. Halt "fast" selection and bail.
186 return false;
187 continue;
188 }
189
190 // N = N + Idx * ElementSize;
191 uint64_t ElementSize = TD.getABITypeSize(Ty);
Dan Gohman3df24e62008-09-03 23:12:08 +0000192 unsigned IdxN = getRegForValue(Idx);
Evan Cheng83785c82008-08-20 22:45:34 +0000193 if (IdxN == 0)
194 // Unhandled operand. Halt "fast" selection and bail.
195 return false;
196
197 // If the index is smaller or larger than intptr_t, truncate or extend
198 // it.
Evan Cheng2076aa82008-08-21 01:19:11 +0000199 MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
Evan Cheng83785c82008-08-20 22:45:34 +0000200 if (IdxVT.bitsLT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000201 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::SIGN_EXTEND, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000202 else if (IdxVT.bitsGT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000203 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::TRUNCATE, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000204 if (IdxN == 0)
205 // Unhandled operand. Halt "fast" selection and bail.
206 return false;
207
Dan Gohman80bc6e22008-08-26 20:57:08 +0000208 if (ElementSize != 1) {
Dan Gohmanf93cf792008-08-21 17:37:05 +0000209 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000210 if (IdxN == 0)
211 // Unhandled operand. Halt "fast" selection and bail.
212 return false;
213 }
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000214 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000215 if (N == 0)
216 // Unhandled operand. Halt "fast" selection and bail.
217 return false;
218 }
219 }
220
221 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000222 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000223 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000224}
225
Dan Gohman3df24e62008-09-03 23:12:08 +0000226bool FastISel::SelectCast(Instruction *I, ISD::NodeType Opcode) {
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 Gohman3df24e62008-09-03 23:12:08 +0000236 unsigned InputReg = getRegForValue(I->getOperand(0));
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
Dan Gohman3df24e62008-09-03 23:12:08 +0000248 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000249 return true;
250}
251
Dan Gohman3df24e62008-09-03 23:12:08 +0000252bool FastISel::SelectBitCast(Instruction *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000253 // If the bitcast doesn't change the type, just use the operand value.
254 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000255 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000256 if (Reg == 0)
257 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000258 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000259 return true;
260 }
261
262 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Anderson6336b702008-08-27 18:58:30 +0000263 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
264 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000265
266 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
267 DstVT == MVT::Other || !DstVT.isSimple() ||
268 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
269 // Unhandled type. Halt "fast" selection and bail.
270 return false;
271
Dan Gohman3df24e62008-09-03 23:12:08 +0000272 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000273 if (Op0 == 0)
274 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000275 return false;
276
Dan Gohmanad368ac2008-08-27 18:10:19 +0000277 // First, try to perform the bitcast by inserting a reg-reg copy.
278 unsigned ResultReg = 0;
279 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
280 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
281 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
282 ResultReg = createResultReg(DstClass);
283
284 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
285 Op0, DstClass, SrcClass);
286 if (!InsertedCopy)
287 ResultReg = 0;
288 }
289
290 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
291 if (!ResultReg)
292 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
293 ISD::BIT_CONVERT, Op0);
294
295 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000296 return false;
297
Dan Gohman3df24e62008-09-03 23:12:08 +0000298 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000299 return true;
300}
301
Dan Gohman3df24e62008-09-03 23:12:08 +0000302bool
303FastISel::SelectInstruction(Instruction *I) {
304 switch (I->getOpcode()) {
305 case Instruction::Add: {
306 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
307 return SelectBinaryOp(I, Opc);
308 }
309 case Instruction::Sub: {
310 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
311 return SelectBinaryOp(I, Opc);
312 }
313 case Instruction::Mul: {
314 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
315 return SelectBinaryOp(I, Opc);
316 }
317 case Instruction::SDiv:
318 return SelectBinaryOp(I, ISD::SDIV);
319 case Instruction::UDiv:
320 return SelectBinaryOp(I, ISD::UDIV);
321 case Instruction::FDiv:
322 return SelectBinaryOp(I, ISD::FDIV);
323 case Instruction::SRem:
324 return SelectBinaryOp(I, ISD::SREM);
325 case Instruction::URem:
326 return SelectBinaryOp(I, ISD::UREM);
327 case Instruction::FRem:
328 return SelectBinaryOp(I, ISD::FREM);
329 case Instruction::Shl:
330 return SelectBinaryOp(I, ISD::SHL);
331 case Instruction::LShr:
332 return SelectBinaryOp(I, ISD::SRL);
333 case Instruction::AShr:
334 return SelectBinaryOp(I, ISD::SRA);
335 case Instruction::And:
336 return SelectBinaryOp(I, ISD::AND);
337 case Instruction::Or:
338 return SelectBinaryOp(I, ISD::OR);
339 case Instruction::Xor:
340 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000341
Dan Gohman3df24e62008-09-03 23:12:08 +0000342 case Instruction::GetElementPtr:
343 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000344
Dan Gohman3df24e62008-09-03 23:12:08 +0000345 case Instruction::Br: {
346 BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000347
Dan Gohman3df24e62008-09-03 23:12:08 +0000348 if (BI->isUnconditional()) {
349 MachineFunction::iterator NextMBB =
350 next(MachineFunction::iterator(MBB));
351 BasicBlock *LLVMSucc = BI->getSuccessor(0);
352 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
Dan Gohman6f2766d2008-08-19 22:31:46 +0000353
Dan Gohman3df24e62008-09-03 23:12:08 +0000354 if (NextMBB != MF.end() && MSucc == NextMBB) {
355 // The unconditional fall-through case, which needs no instructions.
Owen Anderson9d5b4162008-08-27 00:31:01 +0000356 } else {
Dan Gohman3df24e62008-09-03 23:12:08 +0000357 // The unconditional branch case.
358 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
Owen Anderson9d5b4162008-08-27 00:31:01 +0000359 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000360 MBB->addSuccessor(MSucc);
361 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000362 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000363
364 // Conditional branches are not handed yet.
365 // Halt "fast" selection and bail.
366 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000367 }
368
Dan Gohman087c8502008-09-05 01:08:41 +0000369 case Instruction::Unreachable:
370 // Nothing to emit.
371 return true;
372
Dan Gohman3df24e62008-09-03 23:12:08 +0000373 case Instruction::PHI:
374 // PHI nodes are already emitted.
375 return true;
376
377 case Instruction::BitCast:
378 return SelectBitCast(I);
379
380 case Instruction::FPToSI:
381 return SelectCast(I, ISD::FP_TO_SINT);
382 case Instruction::ZExt:
383 return SelectCast(I, ISD::ZERO_EXTEND);
384 case Instruction::SExt:
385 return SelectCast(I, ISD::SIGN_EXTEND);
386 case Instruction::Trunc:
387 return SelectCast(I, ISD::TRUNCATE);
388 case Instruction::SIToFP:
389 return SelectCast(I, ISD::SINT_TO_FP);
390
391 case Instruction::IntToPtr: // Deliberate fall-through.
392 case Instruction::PtrToInt: {
393 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
394 MVT DstVT = TLI.getValueType(I->getType());
395 if (DstVT.bitsGT(SrcVT))
396 return SelectCast(I, ISD::ZERO_EXTEND);
397 if (DstVT.bitsLT(SrcVT))
398 return SelectCast(I, ISD::TRUNCATE);
399 unsigned Reg = getRegForValue(I->getOperand(0));
400 if (Reg == 0) return false;
401 UpdateValueMap(I, Reg);
402 return true;
403 }
404
405 default:
406 // Unhandled instruction. Halt "fast" selection and bail.
407 return false;
408 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000409}
410
Dan Gohman3df24e62008-09-03 23:12:08 +0000411FastISel::FastISel(MachineFunction &mf,
412 DenseMap<const Value *, unsigned> &vm,
413 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm)
414 : MBB(0),
415 ValueMap(vm),
416 MBBMap(bm),
417 MF(mf),
418 MRI(MF.getRegInfo()),
419 TM(MF.getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +0000420 TD(*TM.getTargetData()),
421 TII(*TM.getInstrInfo()),
422 TLI(*TM.getTargetLowering()) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000423}
424
Dan Gohmane285a742008-08-14 21:51:29 +0000425FastISel::~FastISel() {}
426
Evan Cheng36fd9412008-09-02 21:59:13 +0000427unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType,
428 ISD::NodeType) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000429 return 0;
430}
431
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000432unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
433 ISD::NodeType, unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000434 return 0;
435}
436
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000437unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType,
438 ISD::NodeType, unsigned /*Op0*/,
439 unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000440 return 0;
441}
442
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000443unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
444 ISD::NodeType, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000445 return 0;
446}
447
Dan Gohman10df0fa2008-08-27 01:09:54 +0000448unsigned FastISel::FastEmit_f(MVT::SimpleValueType, MVT::SimpleValueType,
449 ISD::NodeType, ConstantFP * /*FPImm*/) {
450 return 0;
451}
452
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000453unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
454 ISD::NodeType, unsigned /*Op0*/,
455 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000456 return 0;
457}
458
Dan Gohman10df0fa2008-08-27 01:09:54 +0000459unsigned FastISel::FastEmit_rf(MVT::SimpleValueType, MVT::SimpleValueType,
460 ISD::NodeType, unsigned /*Op0*/,
461 ConstantFP * /*FPImm*/) {
462 return 0;
463}
464
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000465unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
466 ISD::NodeType,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000467 unsigned /*Op0*/, unsigned /*Op1*/,
468 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000469 return 0;
470}
471
472/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
473/// to emit an instruction with an immediate operand using FastEmit_ri.
474/// If that fails, it materializes the immediate into a register and try
475/// FastEmit_rr instead.
476unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000477 unsigned Op0, uint64_t Imm,
478 MVT::SimpleValueType ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000479 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohman151ed612008-08-27 18:15:05 +0000480 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000481 if (ResultReg != 0)
482 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000483 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000484 if (MaterialReg == 0)
485 return 0;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000486 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000487}
488
Dan Gohman10df0fa2008-08-27 01:09:54 +0000489/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
490/// to emit an instruction with a floating-point immediate operand using
491/// FastEmit_rf. If that fails, it materializes the immediate into a register
492/// and try FastEmit_rr instead.
493unsigned FastISel::FastEmit_rf_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
494 unsigned Op0, ConstantFP *FPImm,
495 MVT::SimpleValueType ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000496 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohman151ed612008-08-27 18:15:05 +0000497 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000498 if (ResultReg != 0)
499 return ResultReg;
500
501 // Materialize the constant in a register.
502 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
503 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000504 // If the target doesn't have a way to directly enter a floating-point
505 // value into a register, use an alternate approach.
506 // TODO: The current approach only supports floating-point constants
507 // that can be constructed by conversion from integer values. This should
508 // be replaced by code that creates a load from a constant-pool entry,
509 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000510 const APFloat &Flt = FPImm->getValueAPF();
511 MVT IntVT = TLI.getPointerTy();
512
513 uint64_t x[2];
514 uint32_t IntBitWidth = IntVT.getSizeInBits();
515 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
516 APFloat::rmTowardZero) != APFloat::opOK)
517 return 0;
518 APInt IntVal(IntBitWidth, 2, x);
519
520 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
521 ISD::Constant, IntVal.getZExtValue());
522 if (IntegerReg == 0)
523 return 0;
524 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
525 ISD::SINT_TO_FP, IntegerReg);
526 if (MaterialReg == 0)
527 return 0;
528 }
529 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
530}
531
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000532unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
533 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000534}
535
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000536unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000537 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000538 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000539 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000540
Dan Gohmanfd903942008-08-20 23:53:10 +0000541 BuildMI(MBB, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000542 return ResultReg;
543}
544
545unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
546 const TargetRegisterClass *RC,
547 unsigned Op0) {
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).addReg(Op0);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000552 return ResultReg;
553}
554
555unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
556 const TargetRegisterClass *RC,
557 unsigned Op0, unsigned Op1) {
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).addReg(Op1);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000562 return ResultReg;
563}
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000564
565unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
566 const TargetRegisterClass *RC,
567 unsigned Op0, uint64_t Imm) {
568 unsigned ResultReg = createResultReg(RC);
569 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
570
571 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
572 return ResultReg;
573}
574
Dan Gohman10df0fa2008-08-27 01:09:54 +0000575unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
576 const TargetRegisterClass *RC,
577 unsigned Op0, ConstantFP *FPImm) {
578 unsigned ResultReg = createResultReg(RC);
579 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
580
581 BuildMI(MBB, II, ResultReg).addReg(Op0).addFPImm(FPImm);
582 return ResultReg;
583}
584
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000585unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
586 const TargetRegisterClass *RC,
587 unsigned Op0, unsigned Op1, uint64_t Imm) {
588 unsigned ResultReg = createResultReg(RC);
589 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
590
591 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
592 return ResultReg;
593}
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000594
595unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
596 const TargetRegisterClass *RC,
597 uint64_t Imm) {
598 unsigned ResultReg = createResultReg(RC);
599 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
600
601 BuildMI(MBB, II, ResultReg).addImm(Imm);
602 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +0000603}
Owen Anderson8970f002008-08-27 22:30:02 +0000604
Owen Anderson40a468f2008-08-28 17:47:37 +0000605unsigned FastISel::FastEmitInst_extractsubreg(unsigned Op0, uint32_t Idx) {
606 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson8970f002008-08-27 22:30:02 +0000607 const TargetRegisterClass* SRC = *(RC->subregclasses_begin()+Idx-1);
608
609 unsigned ResultReg = createResultReg(SRC);
610 const TargetInstrDesc &II = TII.get(TargetInstrInfo::EXTRACT_SUBREG);
611
Owen Andersonc0bb68b2008-08-28 18:26:01 +0000612 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Idx);
Owen Anderson8970f002008-08-27 22:30:02 +0000613 return ResultReg;
614}