blob: 220be221d569e974eeadfb048199ad1ee61339c1 [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 Gohman40b189e2008-09-05 18:18:20 +000066 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
67 if (!SelectOperator(CE, CE->getOpcode())) return 0;
68 Reg = LocalValueMap[CE];
Dan Gohman205d9252008-08-28 21:19:07 +000069 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000070 Reg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman205d9252008-08-28 21:19:07 +000071 BuildMI(MBB, TII.get(TargetInstrInfo::IMPLICIT_DEF), Reg);
Dan Gohman104e4ce2008-09-03 23:32:19 +000072 } else {
73 return 0;
Dan Gohmanad368ac2008-08-27 18:10:19 +000074 }
Owen Andersond5d81a42008-09-03 17:51:57 +000075
Dan Gohman104e4ce2008-09-03 23:32:19 +000076 LocalValueMap[V] = Reg;
77 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +000078}
79
Owen Andersoncc54e762008-08-30 00:38:46 +000080/// UpdateValueMap - Update the value map to include the new mapping for this
81/// instruction, or insert an extra copy to get the result in a previous
82/// determined register.
83/// NOTE: This is only necessary because we might select a block that uses
84/// a value before we select the block that defines the value. It might be
85/// possible to fix this by selecting blocks in reverse postorder.
Owen Anderson95267a12008-09-05 00:06:23 +000086void FastISel::UpdateValueMap(Value* I, unsigned Reg) {
Dan Gohman40b189e2008-09-05 18:18:20 +000087 if (!isa<Instruction>(I)) {
88 LocalValueMap[I] = Reg;
89 return;
90 }
Owen Andersoncc54e762008-08-30 00:38:46 +000091 if (!ValueMap.count(I))
92 ValueMap[I] = Reg;
93 else
94 TII.copyRegToReg(*MBB, MBB->end(), ValueMap[I],
95 Reg, MRI.getRegClass(Reg), MRI.getRegClass(Reg));
96}
97
Dan Gohmanbdedd442008-08-20 00:11:48 +000098/// SelectBinaryOp - Select and emit code for a binary operator instruction,
99/// which has an opcode which directly corresponds to the given ISD opcode.
100///
Dan Gohman40b189e2008-09-05 18:18:20 +0000101bool FastISel::SelectBinaryOp(User *I, ISD::NodeType ISDOpcode) {
Dan Gohmanbdedd442008-08-20 00:11:48 +0000102 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
103 if (VT == MVT::Other || !VT.isSimple())
104 // Unhandled type. Halt "fast" selection and bail.
105 return false;
Dan Gohmanb71fea22008-08-26 20:52:40 +0000106 // We only handle legal types. For example, on x86-32 the instruction
107 // selector contains all of the 64-bit instructions from x86-64,
108 // under the assumption that i64 won't be used if the target doesn't
109 // support it.
110 if (!TLI.isTypeLegal(VT))
111 return false;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000112
Dan Gohman3df24e62008-09-03 23:12:08 +0000113 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000114 if (Op0 == 0)
115 // Unhandled operand. Halt "fast" selection and bail.
116 return false;
117
118 // Check if the second operand is a constant and handle it appropriately.
119 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000120 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
121 ISDOpcode, Op0, CI->getZExtValue());
122 if (ResultReg != 0) {
123 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000124 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000125 return true;
126 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000127 }
128
Dan Gohman10df0fa2008-08-27 01:09:54 +0000129 // Check if the second operand is a constant float.
130 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000131 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
132 ISDOpcode, Op0, CF);
133 if (ResultReg != 0) {
134 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000135 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000136 return true;
137 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000138 }
139
Dan Gohman3df24e62008-09-03 23:12:08 +0000140 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000141 if (Op1 == 0)
142 // Unhandled operand. Halt "fast" selection and bail.
143 return false;
144
Dan Gohmanad368ac2008-08-27 18:10:19 +0000145 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000146 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
147 ISDOpcode, Op0, Op1);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000148 if (ResultReg == 0)
149 // Target-specific code wasn't able to find a machine opcode for
150 // the given ISD opcode and type. Halt "fast" selection and bail.
151 return false;
152
Dan Gohman8014e862008-08-20 00:23:20 +0000153 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000154 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000155 return true;
156}
157
Dan Gohman40b189e2008-09-05 18:18:20 +0000158bool FastISel::SelectGetElementPtr(User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000159 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000160 if (N == 0)
161 // Unhandled operand. Halt "fast" selection and bail.
162 return false;
163
164 const Type *Ty = I->getOperand(0)->getType();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000165 MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
Evan Cheng83785c82008-08-20 22:45:34 +0000166 for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
167 OI != E; ++OI) {
168 Value *Idx = *OI;
169 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
170 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
171 if (Field) {
172 // N = N + Offset
173 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
174 // FIXME: This can be optimized by combining the add with a
175 // subsequent one.
Dan Gohman7a0e6592008-08-21 17:25:26 +0000176 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000177 if (N == 0)
178 // Unhandled operand. Halt "fast" selection and bail.
179 return false;
180 }
181 Ty = StTy->getElementType(Field);
182 } else {
183 Ty = cast<SequentialType>(Ty)->getElementType();
184
185 // If this is a constant subscript, handle it quickly.
186 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
187 if (CI->getZExtValue() == 0) continue;
188 uint64_t Offs =
189 TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000190 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000191 if (N == 0)
192 // Unhandled operand. Halt "fast" selection and bail.
193 return false;
194 continue;
195 }
196
197 // N = N + Idx * ElementSize;
198 uint64_t ElementSize = TD.getABITypeSize(Ty);
Dan Gohman3df24e62008-09-03 23:12:08 +0000199 unsigned IdxN = getRegForValue(Idx);
Evan Cheng83785c82008-08-20 22:45:34 +0000200 if (IdxN == 0)
201 // Unhandled operand. Halt "fast" selection and bail.
202 return false;
203
204 // If the index is smaller or larger than intptr_t, truncate or extend
205 // it.
Evan Cheng2076aa82008-08-21 01:19:11 +0000206 MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
Evan Cheng83785c82008-08-20 22:45:34 +0000207 if (IdxVT.bitsLT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000208 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::SIGN_EXTEND, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000209 else if (IdxVT.bitsGT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000210 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::TRUNCATE, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000211 if (IdxN == 0)
212 // Unhandled operand. Halt "fast" selection and bail.
213 return false;
214
Dan Gohman80bc6e22008-08-26 20:57:08 +0000215 if (ElementSize != 1) {
Dan Gohmanf93cf792008-08-21 17:37:05 +0000216 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000217 if (IdxN == 0)
218 // Unhandled operand. Halt "fast" selection and bail.
219 return false;
220 }
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000221 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000222 if (N == 0)
223 // Unhandled operand. Halt "fast" selection and bail.
224 return false;
225 }
226 }
227
228 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000229 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000230 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000231}
232
Dan Gohman40b189e2008-09-05 18:18:20 +0000233bool FastISel::SelectCast(User *I, ISD::NodeType Opcode) {
Owen Anderson6336b702008-08-27 18:58:30 +0000234 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
235 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000236
237 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
238 DstVT == MVT::Other || !DstVT.isSimple() ||
239 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
240 // Unhandled type. Halt "fast" selection and bail.
241 return false;
242
Dan Gohman3df24e62008-09-03 23:12:08 +0000243 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000244 if (!InputReg)
245 // Unhandled operand. Halt "fast" selection and bail.
246 return false;
247
248 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
249 DstVT.getSimpleVT(),
250 Opcode,
251 InputReg);
252 if (!ResultReg)
253 return false;
254
Dan Gohman3df24e62008-09-03 23:12:08 +0000255 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000256 return true;
257}
258
Dan Gohman40b189e2008-09-05 18:18:20 +0000259bool FastISel::SelectBitCast(User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000260 // If the bitcast doesn't change the type, just use the operand value.
261 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000262 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000263 if (Reg == 0)
264 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000265 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000266 return true;
267 }
268
269 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Anderson6336b702008-08-27 18:58:30 +0000270 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
271 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000272
273 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
274 DstVT == MVT::Other || !DstVT.isSimple() ||
275 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
276 // Unhandled type. Halt "fast" selection and bail.
277 return false;
278
Dan Gohman3df24e62008-09-03 23:12:08 +0000279 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000280 if (Op0 == 0)
281 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000282 return false;
283
Dan Gohmanad368ac2008-08-27 18:10:19 +0000284 // First, try to perform the bitcast by inserting a reg-reg copy.
285 unsigned ResultReg = 0;
286 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
287 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
288 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
289 ResultReg = createResultReg(DstClass);
290
291 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
292 Op0, DstClass, SrcClass);
293 if (!InsertedCopy)
294 ResultReg = 0;
295 }
296
297 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
298 if (!ResultReg)
299 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
300 ISD::BIT_CONVERT, Op0);
301
302 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000303 return false;
304
Dan Gohman3df24e62008-09-03 23:12:08 +0000305 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000306 return true;
307}
308
Dan Gohman3df24e62008-09-03 23:12:08 +0000309bool
310FastISel::SelectInstruction(Instruction *I) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000311 return SelectOperator(I, I->getOpcode());
312}
313
314bool
315FastISel::SelectOperator(User *I, unsigned Opcode) {
316 switch (Opcode) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000317 case Instruction::Add: {
318 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
319 return SelectBinaryOp(I, Opc);
320 }
321 case Instruction::Sub: {
322 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
323 return SelectBinaryOp(I, Opc);
324 }
325 case Instruction::Mul: {
326 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
327 return SelectBinaryOp(I, Opc);
328 }
329 case Instruction::SDiv:
330 return SelectBinaryOp(I, ISD::SDIV);
331 case Instruction::UDiv:
332 return SelectBinaryOp(I, ISD::UDIV);
333 case Instruction::FDiv:
334 return SelectBinaryOp(I, ISD::FDIV);
335 case Instruction::SRem:
336 return SelectBinaryOp(I, ISD::SREM);
337 case Instruction::URem:
338 return SelectBinaryOp(I, ISD::UREM);
339 case Instruction::FRem:
340 return SelectBinaryOp(I, ISD::FREM);
341 case Instruction::Shl:
342 return SelectBinaryOp(I, ISD::SHL);
343 case Instruction::LShr:
344 return SelectBinaryOp(I, ISD::SRL);
345 case Instruction::AShr:
346 return SelectBinaryOp(I, ISD::SRA);
347 case Instruction::And:
348 return SelectBinaryOp(I, ISD::AND);
349 case Instruction::Or:
350 return SelectBinaryOp(I, ISD::OR);
351 case Instruction::Xor:
352 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000353
Dan Gohman3df24e62008-09-03 23:12:08 +0000354 case Instruction::GetElementPtr:
355 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000356
Dan Gohman3df24e62008-09-03 23:12:08 +0000357 case Instruction::Br: {
358 BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000359
Dan Gohman3df24e62008-09-03 23:12:08 +0000360 if (BI->isUnconditional()) {
361 MachineFunction::iterator NextMBB =
362 next(MachineFunction::iterator(MBB));
363 BasicBlock *LLVMSucc = BI->getSuccessor(0);
364 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
Dan Gohman6f2766d2008-08-19 22:31:46 +0000365
Dan Gohman3df24e62008-09-03 23:12:08 +0000366 if (NextMBB != MF.end() && MSucc == NextMBB) {
367 // The unconditional fall-through case, which needs no instructions.
Owen Anderson9d5b4162008-08-27 00:31:01 +0000368 } else {
Dan Gohman3df24e62008-09-03 23:12:08 +0000369 // The unconditional branch case.
370 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
Owen Anderson9d5b4162008-08-27 00:31:01 +0000371 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000372 MBB->addSuccessor(MSucc);
373 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000374 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000375
376 // Conditional branches are not handed yet.
377 // Halt "fast" selection and bail.
378 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000379 }
380
Dan Gohman087c8502008-09-05 01:08:41 +0000381 case Instruction::Unreachable:
382 // Nothing to emit.
383 return true;
384
Dan Gohman3df24e62008-09-03 23:12:08 +0000385 case Instruction::PHI:
386 // PHI nodes are already emitted.
387 return true;
388
389 case Instruction::BitCast:
390 return SelectBitCast(I);
391
392 case Instruction::FPToSI:
393 return SelectCast(I, ISD::FP_TO_SINT);
394 case Instruction::ZExt:
395 return SelectCast(I, ISD::ZERO_EXTEND);
396 case Instruction::SExt:
397 return SelectCast(I, ISD::SIGN_EXTEND);
398 case Instruction::Trunc:
399 return SelectCast(I, ISD::TRUNCATE);
400 case Instruction::SIToFP:
401 return SelectCast(I, ISD::SINT_TO_FP);
402
403 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 (DstVT.bitsGT(SrcVT))
408 return SelectCast(I, ISD::ZERO_EXTEND);
409 if (DstVT.bitsLT(SrcVT))
410 return SelectCast(I, ISD::TRUNCATE);
411 unsigned Reg = getRegForValue(I->getOperand(0));
412 if (Reg == 0) return false;
413 UpdateValueMap(I, Reg);
414 return true;
415 }
416
417 default:
418 // Unhandled instruction. Halt "fast" selection and bail.
419 return false;
420 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000421}
422
Dan Gohman3df24e62008-09-03 23:12:08 +0000423FastISel::FastISel(MachineFunction &mf,
424 DenseMap<const Value *, unsigned> &vm,
425 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm)
426 : MBB(0),
427 ValueMap(vm),
428 MBBMap(bm),
429 MF(mf),
430 MRI(MF.getRegInfo()),
431 TM(MF.getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +0000432 TD(*TM.getTargetData()),
433 TII(*TM.getInstrInfo()),
434 TLI(*TM.getTargetLowering()) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000435}
436
Dan Gohmane285a742008-08-14 21:51:29 +0000437FastISel::~FastISel() {}
438
Evan Cheng36fd9412008-09-02 21:59:13 +0000439unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType,
440 ISD::NodeType) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000441 return 0;
442}
443
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000444unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
445 ISD::NodeType, unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000446 return 0;
447}
448
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000449unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType,
450 ISD::NodeType, unsigned /*Op0*/,
451 unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000452 return 0;
453}
454
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000455unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
456 ISD::NodeType, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000457 return 0;
458}
459
Dan Gohman10df0fa2008-08-27 01:09:54 +0000460unsigned FastISel::FastEmit_f(MVT::SimpleValueType, MVT::SimpleValueType,
461 ISD::NodeType, ConstantFP * /*FPImm*/) {
462 return 0;
463}
464
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000465unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
466 ISD::NodeType, unsigned /*Op0*/,
467 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000468 return 0;
469}
470
Dan Gohman10df0fa2008-08-27 01:09:54 +0000471unsigned FastISel::FastEmit_rf(MVT::SimpleValueType, MVT::SimpleValueType,
472 ISD::NodeType, unsigned /*Op0*/,
473 ConstantFP * /*FPImm*/) {
474 return 0;
475}
476
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000477unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
478 ISD::NodeType,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000479 unsigned /*Op0*/, unsigned /*Op1*/,
480 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000481 return 0;
482}
483
484/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
485/// to emit an instruction with an immediate operand using FastEmit_ri.
486/// If that fails, it materializes the immediate into a register and try
487/// FastEmit_rr instead.
488unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000489 unsigned Op0, uint64_t Imm,
490 MVT::SimpleValueType ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000491 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohman151ed612008-08-27 18:15:05 +0000492 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000493 if (ResultReg != 0)
494 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000495 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000496 if (MaterialReg == 0)
497 return 0;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000498 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000499}
500
Dan Gohman10df0fa2008-08-27 01:09:54 +0000501/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
502/// to emit an instruction with a floating-point immediate operand using
503/// FastEmit_rf. If that fails, it materializes the immediate into a register
504/// and try FastEmit_rr instead.
505unsigned FastISel::FastEmit_rf_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
506 unsigned Op0, ConstantFP *FPImm,
507 MVT::SimpleValueType ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000508 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohman151ed612008-08-27 18:15:05 +0000509 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000510 if (ResultReg != 0)
511 return ResultReg;
512
513 // Materialize the constant in a register.
514 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
515 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000516 // If the target doesn't have a way to directly enter a floating-point
517 // value into a register, use an alternate approach.
518 // TODO: The current approach only supports floating-point constants
519 // that can be constructed by conversion from integer values. This should
520 // be replaced by code that creates a load from a constant-pool entry,
521 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000522 const APFloat &Flt = FPImm->getValueAPF();
523 MVT IntVT = TLI.getPointerTy();
524
525 uint64_t x[2];
526 uint32_t IntBitWidth = IntVT.getSizeInBits();
527 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
528 APFloat::rmTowardZero) != APFloat::opOK)
529 return 0;
530 APInt IntVal(IntBitWidth, 2, x);
531
532 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
533 ISD::Constant, IntVal.getZExtValue());
534 if (IntegerReg == 0)
535 return 0;
536 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
537 ISD::SINT_TO_FP, IntegerReg);
538 if (MaterialReg == 0)
539 return 0;
540 }
541 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
542}
543
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000544unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
545 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000546}
547
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000548unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000549 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000550 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000551 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000552
Dan Gohmanfd903942008-08-20 23:53:10 +0000553 BuildMI(MBB, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000554 return ResultReg;
555}
556
557unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
558 const TargetRegisterClass *RC,
559 unsigned Op0) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000560 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000561 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000562
Dan Gohmanfd903942008-08-20 23:53:10 +0000563 BuildMI(MBB, II, ResultReg).addReg(Op0);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000564 return ResultReg;
565}
566
567unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
568 const TargetRegisterClass *RC,
569 unsigned Op0, unsigned Op1) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000570 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000571 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000572
Dan Gohmanfd903942008-08-20 23:53:10 +0000573 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000574 return ResultReg;
575}
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000576
577unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
578 const TargetRegisterClass *RC,
579 unsigned Op0, uint64_t Imm) {
580 unsigned ResultReg = createResultReg(RC);
581 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
582
583 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
584 return ResultReg;
585}
586
Dan Gohman10df0fa2008-08-27 01:09:54 +0000587unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
588 const TargetRegisterClass *RC,
589 unsigned Op0, ConstantFP *FPImm) {
590 unsigned ResultReg = createResultReg(RC);
591 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
592
593 BuildMI(MBB, II, ResultReg).addReg(Op0).addFPImm(FPImm);
594 return ResultReg;
595}
596
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000597unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
598 const TargetRegisterClass *RC,
599 unsigned Op0, unsigned Op1, uint64_t Imm) {
600 unsigned ResultReg = createResultReg(RC);
601 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
602
603 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
604 return ResultReg;
605}
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000606
607unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
608 const TargetRegisterClass *RC,
609 uint64_t Imm) {
610 unsigned ResultReg = createResultReg(RC);
611 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
612
613 BuildMI(MBB, II, ResultReg).addImm(Imm);
614 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +0000615}
Owen Anderson8970f002008-08-27 22:30:02 +0000616
Owen Anderson40a468f2008-08-28 17:47:37 +0000617unsigned FastISel::FastEmitInst_extractsubreg(unsigned Op0, uint32_t Idx) {
618 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson8970f002008-08-27 22:30:02 +0000619 const TargetRegisterClass* SRC = *(RC->subregclasses_begin()+Idx-1);
620
621 unsigned ResultReg = createResultReg(SRC);
622 const TargetInstrDesc &II = TII.get(TargetInstrInfo::EXTRACT_SUBREG);
623
Owen Andersonc0bb68b2008-08-28 18:26:01 +0000624 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Idx);
Owen Anderson8970f002008-08-27 22:30:02 +0000625 return ResultReg;
626}