blob: dd7e95294f116d3e54a8d3224b24fb9f364014c7 [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();
Dan Gohman82116482008-09-10 21:01:08 +000036
37 // Ignore illegal types.
38 if (!TLI.isTypeLegal(VT)) {
39 // Promote MVT::i1 to a legal type though, because it's common and easy.
40 if (VT == MVT::i1)
41 VT = TLI.getTypeToTransformTo(VT).getSimpleVT();
42 else
43 return 0;
44 }
45
Dan Gohmanad368ac2008-08-27 18:10:19 +000046 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +000047 if (CI->getValue().getActiveBits() <= 64)
48 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman0586d912008-09-10 20:11:02 +000049 } else if (isa<AllocaInst>(V)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +000050 Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohman205d9252008-08-28 21:19:07 +000051 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000052 Reg = FastEmit_i(VT, VT, ISD::Constant, 0);
Dan Gohmanad368ac2008-08-27 18:10:19 +000053 } else if (ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000054 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +000055
56 if (!Reg) {
57 const APFloat &Flt = CF->getValueAPF();
58 MVT IntVT = TLI.getPointerTy();
59
60 uint64_t x[2];
61 uint32_t IntBitWidth = IntVT.getSizeInBits();
Dan Gohman2ff7fd12008-09-19 22:16:54 +000062 if (!Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
63 APFloat::rmTowardZero) != APFloat::opOK) {
64 APInt IntVal(IntBitWidth, 2, x);
Dan Gohmanad368ac2008-08-27 18:10:19 +000065
Dan Gohman2ff7fd12008-09-19 22:16:54 +000066 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
67 ISD::Constant, IntVal.getZExtValue());
68 if (IntegerReg != 0)
69 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
70 }
Dan Gohmanad368ac2008-08-27 18:10:19 +000071 }
Dan Gohman40b189e2008-09-05 18:18:20 +000072 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
73 if (!SelectOperator(CE, CE->getOpcode())) return 0;
74 Reg = LocalValueMap[CE];
Dan Gohman205d9252008-08-28 21:19:07 +000075 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000076 Reg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman205d9252008-08-28 21:19:07 +000077 BuildMI(MBB, TII.get(TargetInstrInfo::IMPLICIT_DEF), Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +000078 }
Owen Andersond5d81a42008-09-03 17:51:57 +000079
Dan Gohmandceffe62008-09-25 01:28:51 +000080 // If target-independent code couldn't handle the value, give target-specific
81 // code a try.
Owen Anderson6e607452008-09-05 23:36:01 +000082 if (!Reg && isa<Constant>(V))
Dan Gohman2ff7fd12008-09-19 22:16:54 +000083 Reg = TargetMaterializeConstant(cast<Constant>(V));
Owen Anderson6e607452008-09-05 23:36:01 +000084
Dan Gohman2ff7fd12008-09-19 22:16:54 +000085 // Don't cache constant materializations in the general ValueMap.
86 // To do so would require tracking what uses they dominate.
Dan Gohmandceffe62008-09-25 01:28:51 +000087 if (Reg != 0)
88 LocalValueMap[V] = Reg;
Dan Gohman104e4ce2008-09-03 23:32:19 +000089 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +000090}
91
Evan Cheng59fbc802008-09-09 01:26:59 +000092unsigned FastISel::lookUpRegForValue(Value *V) {
93 // Look up the value to see if we already have a register for it. We
94 // cache values defined by Instructions across blocks, and other values
95 // only locally. This is because Instructions already have the SSA
96 // def-dominatess-use requirement enforced.
97 if (ValueMap.count(V))
98 return ValueMap[V];
99 return LocalValueMap[V];
100}
101
Owen Andersoncc54e762008-08-30 00:38:46 +0000102/// UpdateValueMap - Update the value map to include the new mapping for this
103/// instruction, or insert an extra copy to get the result in a previous
104/// determined register.
105/// NOTE: This is only necessary because we might select a block that uses
106/// a value before we select the block that defines the value. It might be
107/// possible to fix this by selecting blocks in reverse postorder.
Owen Anderson95267a12008-09-05 00:06:23 +0000108void FastISel::UpdateValueMap(Value* I, unsigned Reg) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000109 if (!isa<Instruction>(I)) {
110 LocalValueMap[I] = Reg;
111 return;
112 }
Owen Andersoncc54e762008-08-30 00:38:46 +0000113 if (!ValueMap.count(I))
114 ValueMap[I] = Reg;
115 else
Evan Chengf0991782008-09-07 09:04:52 +0000116 TII.copyRegToReg(*MBB, MBB->end(), ValueMap[I],
117 Reg, MRI.getRegClass(Reg), MRI.getRegClass(Reg));
Owen Andersoncc54e762008-08-30 00:38:46 +0000118}
119
Dan Gohmanbdedd442008-08-20 00:11:48 +0000120/// SelectBinaryOp - Select and emit code for a binary operator instruction,
121/// which has an opcode which directly corresponds to the given ISD opcode.
122///
Dan Gohman40b189e2008-09-05 18:18:20 +0000123bool FastISel::SelectBinaryOp(User *I, ISD::NodeType ISDOpcode) {
Dan Gohmanbdedd442008-08-20 00:11:48 +0000124 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
125 if (VT == MVT::Other || !VT.isSimple())
126 // Unhandled type. Halt "fast" selection and bail.
127 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000128
Dan Gohmanb71fea22008-08-26 20:52:40 +0000129 // We only handle legal types. For example, on x86-32 the instruction
130 // selector contains all of the 64-bit instructions from x86-64,
131 // under the assumption that i64 won't be used if the target doesn't
132 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000133 if (!TLI.isTypeLegal(VT)) {
134 // MVT::i1 is special. Allow AND and OR (but not XOR) because they
135 // don't require additional zeroing, which makes them easy.
136 if (VT == MVT::i1 &&
137 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR))
138 VT = TLI.getTypeToTransformTo(VT);
139 else
140 return false;
141 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000142
Dan Gohman3df24e62008-09-03 23:12:08 +0000143 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000144 if (Op0 == 0)
145 // Unhandled operand. Halt "fast" selection and bail.
146 return false;
147
148 // Check if the second operand is a constant and handle it appropriately.
149 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000150 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
151 ISDOpcode, Op0, CI->getZExtValue());
152 if (ResultReg != 0) {
153 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000154 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000155 return true;
156 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000157 }
158
Dan Gohman10df0fa2008-08-27 01:09:54 +0000159 // Check if the second operand is a constant float.
160 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000161 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
162 ISDOpcode, Op0, CF);
163 if (ResultReg != 0) {
164 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000165 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000166 return true;
167 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000168 }
169
Dan Gohman3df24e62008-09-03 23:12:08 +0000170 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000171 if (Op1 == 0)
172 // Unhandled operand. Halt "fast" selection and bail.
173 return false;
174
Dan Gohmanad368ac2008-08-27 18:10:19 +0000175 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000176 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
177 ISDOpcode, Op0, Op1);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000178 if (ResultReg == 0)
179 // Target-specific code wasn't able to find a machine opcode for
180 // the given ISD opcode and type. Halt "fast" selection and bail.
181 return false;
182
Dan Gohman8014e862008-08-20 00:23:20 +0000183 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000184 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000185 return true;
186}
187
Dan Gohman40b189e2008-09-05 18:18:20 +0000188bool FastISel::SelectGetElementPtr(User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000189 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000190 if (N == 0)
191 // Unhandled operand. Halt "fast" selection and bail.
192 return false;
193
194 const Type *Ty = I->getOperand(0)->getType();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000195 MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
Evan Cheng83785c82008-08-20 22:45:34 +0000196 for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
197 OI != E; ++OI) {
198 Value *Idx = *OI;
199 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
200 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
201 if (Field) {
202 // N = N + Offset
203 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
204 // FIXME: This can be optimized by combining the add with a
205 // subsequent one.
Dan Gohman7a0e6592008-08-21 17:25:26 +0000206 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000207 if (N == 0)
208 // Unhandled operand. Halt "fast" selection and bail.
209 return false;
210 }
211 Ty = StTy->getElementType(Field);
212 } else {
213 Ty = cast<SequentialType>(Ty)->getElementType();
214
215 // If this is a constant subscript, handle it quickly.
216 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
217 if (CI->getZExtValue() == 0) continue;
218 uint64_t Offs =
219 TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000220 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000221 if (N == 0)
222 // Unhandled operand. Halt "fast" selection and bail.
223 return false;
224 continue;
225 }
226
227 // N = N + Idx * ElementSize;
228 uint64_t ElementSize = TD.getABITypeSize(Ty);
Dan Gohman3df24e62008-09-03 23:12:08 +0000229 unsigned IdxN = getRegForValue(Idx);
Evan Cheng83785c82008-08-20 22:45:34 +0000230 if (IdxN == 0)
231 // Unhandled operand. Halt "fast" selection and bail.
232 return false;
233
234 // If the index is smaller or larger than intptr_t, truncate or extend
235 // it.
Evan Cheng2076aa82008-08-21 01:19:11 +0000236 MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
Evan Cheng83785c82008-08-20 22:45:34 +0000237 if (IdxVT.bitsLT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000238 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::SIGN_EXTEND, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000239 else if (IdxVT.bitsGT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000240 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::TRUNCATE, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000241 if (IdxN == 0)
242 // Unhandled operand. Halt "fast" selection and bail.
243 return false;
244
Dan Gohman80bc6e22008-08-26 20:57:08 +0000245 if (ElementSize != 1) {
Dan Gohmanf93cf792008-08-21 17:37:05 +0000246 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000247 if (IdxN == 0)
248 // Unhandled operand. Halt "fast" selection and bail.
249 return false;
250 }
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000251 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000252 if (N == 0)
253 // Unhandled operand. Halt "fast" selection and bail.
254 return false;
255 }
256 }
257
258 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000259 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000260 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000261}
262
Dan Gohman40b189e2008-09-05 18:18:20 +0000263bool FastISel::SelectCast(User *I, ISD::NodeType Opcode) {
Owen Anderson6336b702008-08-27 18:58:30 +0000264 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
265 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000266
267 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
268 DstVT == MVT::Other || !DstVT.isSimple() ||
269 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
270 // Unhandled type. Halt "fast" selection and bail.
271 return false;
272
Dan Gohman3df24e62008-09-03 23:12:08 +0000273 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000274 if (!InputReg)
275 // Unhandled operand. Halt "fast" selection and bail.
276 return false;
277
278 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
279 DstVT.getSimpleVT(),
280 Opcode,
281 InputReg);
282 if (!ResultReg)
283 return false;
284
Dan Gohman3df24e62008-09-03 23:12:08 +0000285 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000286 return true;
287}
288
Dan Gohman40b189e2008-09-05 18:18:20 +0000289bool FastISel::SelectBitCast(User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000290 // If the bitcast doesn't change the type, just use the operand value.
291 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000292 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000293 if (Reg == 0)
294 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000295 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000296 return true;
297 }
298
299 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Anderson6336b702008-08-27 18:58:30 +0000300 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
301 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000302
303 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
304 DstVT == MVT::Other || !DstVT.isSimple() ||
305 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
306 // Unhandled type. Halt "fast" selection and bail.
307 return false;
308
Dan Gohman3df24e62008-09-03 23:12:08 +0000309 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000310 if (Op0 == 0)
311 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000312 return false;
313
Dan Gohmanad368ac2008-08-27 18:10:19 +0000314 // First, try to perform the bitcast by inserting a reg-reg copy.
315 unsigned ResultReg = 0;
316 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
317 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
318 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
319 ResultReg = createResultReg(DstClass);
320
321 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
322 Op0, DstClass, SrcClass);
323 if (!InsertedCopy)
324 ResultReg = 0;
325 }
326
327 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
328 if (!ResultReg)
329 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
330 ISD::BIT_CONVERT, Op0);
331
332 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000333 return false;
334
Dan Gohman3df24e62008-09-03 23:12:08 +0000335 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000336 return true;
337}
338
Dan Gohman3df24e62008-09-03 23:12:08 +0000339bool
340FastISel::SelectInstruction(Instruction *I) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000341 return SelectOperator(I, I->getOpcode());
342}
343
344bool
345FastISel::SelectOperator(User *I, unsigned Opcode) {
346 switch (Opcode) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000347 case Instruction::Add: {
348 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
349 return SelectBinaryOp(I, Opc);
350 }
351 case Instruction::Sub: {
352 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
353 return SelectBinaryOp(I, Opc);
354 }
355 case Instruction::Mul: {
356 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
357 return SelectBinaryOp(I, Opc);
358 }
359 case Instruction::SDiv:
360 return SelectBinaryOp(I, ISD::SDIV);
361 case Instruction::UDiv:
362 return SelectBinaryOp(I, ISD::UDIV);
363 case Instruction::FDiv:
364 return SelectBinaryOp(I, ISD::FDIV);
365 case Instruction::SRem:
366 return SelectBinaryOp(I, ISD::SREM);
367 case Instruction::URem:
368 return SelectBinaryOp(I, ISD::UREM);
369 case Instruction::FRem:
370 return SelectBinaryOp(I, ISD::FREM);
371 case Instruction::Shl:
372 return SelectBinaryOp(I, ISD::SHL);
373 case Instruction::LShr:
374 return SelectBinaryOp(I, ISD::SRL);
375 case Instruction::AShr:
376 return SelectBinaryOp(I, ISD::SRA);
377 case Instruction::And:
378 return SelectBinaryOp(I, ISD::AND);
379 case Instruction::Or:
380 return SelectBinaryOp(I, ISD::OR);
381 case Instruction::Xor:
382 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000383
Dan Gohman3df24e62008-09-03 23:12:08 +0000384 case Instruction::GetElementPtr:
385 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000386
Dan Gohman3df24e62008-09-03 23:12:08 +0000387 case Instruction::Br: {
388 BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000389
Dan Gohman3df24e62008-09-03 23:12:08 +0000390 if (BI->isUnconditional()) {
391 MachineFunction::iterator NextMBB =
392 next(MachineFunction::iterator(MBB));
393 BasicBlock *LLVMSucc = BI->getSuccessor(0);
394 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
Dan Gohman6f2766d2008-08-19 22:31:46 +0000395
Dan Gohman3df24e62008-09-03 23:12:08 +0000396 if (NextMBB != MF.end() && MSucc == NextMBB) {
397 // The unconditional fall-through case, which needs no instructions.
Owen Anderson9d5b4162008-08-27 00:31:01 +0000398 } else {
Dan Gohman3df24e62008-09-03 23:12:08 +0000399 // The unconditional branch case.
400 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
Owen Anderson9d5b4162008-08-27 00:31:01 +0000401 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000402 MBB->addSuccessor(MSucc);
403 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000404 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000405
406 // Conditional branches are not handed yet.
407 // Halt "fast" selection and bail.
408 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000409 }
410
Dan Gohman087c8502008-09-05 01:08:41 +0000411 case Instruction::Unreachable:
412 // Nothing to emit.
413 return true;
414
Dan Gohman3df24e62008-09-03 23:12:08 +0000415 case Instruction::PHI:
416 // PHI nodes are already emitted.
417 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000418
419 case Instruction::Alloca:
420 // FunctionLowering has the static-sized case covered.
421 if (StaticAllocaMap.count(cast<AllocaInst>(I)))
422 return true;
423
424 // Dynamic-sized alloca is not handled yet.
425 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000426
427 case Instruction::BitCast:
428 return SelectBitCast(I);
429
430 case Instruction::FPToSI:
431 return SelectCast(I, ISD::FP_TO_SINT);
432 case Instruction::ZExt:
433 return SelectCast(I, ISD::ZERO_EXTEND);
434 case Instruction::SExt:
435 return SelectCast(I, ISD::SIGN_EXTEND);
436 case Instruction::Trunc:
437 return SelectCast(I, ISD::TRUNCATE);
438 case Instruction::SIToFP:
439 return SelectCast(I, ISD::SINT_TO_FP);
440
441 case Instruction::IntToPtr: // Deliberate fall-through.
442 case Instruction::PtrToInt: {
443 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
444 MVT DstVT = TLI.getValueType(I->getType());
445 if (DstVT.bitsGT(SrcVT))
446 return SelectCast(I, ISD::ZERO_EXTEND);
447 if (DstVT.bitsLT(SrcVT))
448 return SelectCast(I, ISD::TRUNCATE);
449 unsigned Reg = getRegForValue(I->getOperand(0));
450 if (Reg == 0) return false;
451 UpdateValueMap(I, Reg);
452 return true;
453 }
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000454
Dan Gohman3df24e62008-09-03 23:12:08 +0000455 default:
456 // Unhandled instruction. Halt "fast" selection and bail.
457 return false;
458 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000459}
460
Dan Gohman3df24e62008-09-03 23:12:08 +0000461FastISel::FastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000462 MachineModuleInfo *mmi,
Dan Gohman3df24e62008-09-03 23:12:08 +0000463 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +0000464 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
465 DenseMap<const AllocaInst *, int> &am)
Dan Gohman3df24e62008-09-03 23:12:08 +0000466 : MBB(0),
467 ValueMap(vm),
468 MBBMap(bm),
Dan Gohman0586d912008-09-10 20:11:02 +0000469 StaticAllocaMap(am),
Dan Gohman3df24e62008-09-03 23:12:08 +0000470 MF(mf),
Dan Gohmand57dd5f2008-09-23 21:53:34 +0000471 MMI(mmi),
Dan Gohman3df24e62008-09-03 23:12:08 +0000472 MRI(MF.getRegInfo()),
Dan Gohman0586d912008-09-10 20:11:02 +0000473 MFI(*MF.getFrameInfo()),
474 MCP(*MF.getConstantPool()),
Dan Gohman3df24e62008-09-03 23:12:08 +0000475 TM(MF.getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +0000476 TD(*TM.getTargetData()),
477 TII(*TM.getInstrInfo()),
478 TLI(*TM.getTargetLowering()) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000479}
480
Dan Gohmane285a742008-08-14 21:51:29 +0000481FastISel::~FastISel() {}
482
Evan Cheng36fd9412008-09-02 21:59:13 +0000483unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType,
484 ISD::NodeType) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000485 return 0;
486}
487
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000488unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
489 ISD::NodeType, unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000490 return 0;
491}
492
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000493unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType,
494 ISD::NodeType, unsigned /*Op0*/,
495 unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000496 return 0;
497}
498
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000499unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
500 ISD::NodeType, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000501 return 0;
502}
503
Dan Gohman10df0fa2008-08-27 01:09:54 +0000504unsigned FastISel::FastEmit_f(MVT::SimpleValueType, MVT::SimpleValueType,
505 ISD::NodeType, ConstantFP * /*FPImm*/) {
506 return 0;
507}
508
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000509unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
510 ISD::NodeType, unsigned /*Op0*/,
511 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000512 return 0;
513}
514
Dan Gohman10df0fa2008-08-27 01:09:54 +0000515unsigned FastISel::FastEmit_rf(MVT::SimpleValueType, MVT::SimpleValueType,
516 ISD::NodeType, unsigned /*Op0*/,
517 ConstantFP * /*FPImm*/) {
518 return 0;
519}
520
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000521unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
522 ISD::NodeType,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000523 unsigned /*Op0*/, unsigned /*Op1*/,
524 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000525 return 0;
526}
527
528/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
529/// to emit an instruction with an immediate operand using FastEmit_ri.
530/// If that fails, it materializes the immediate into a register and try
531/// FastEmit_rr instead.
532unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000533 unsigned Op0, uint64_t Imm,
534 MVT::SimpleValueType ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000535 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohman151ed612008-08-27 18:15:05 +0000536 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000537 if (ResultReg != 0)
538 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000539 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000540 if (MaterialReg == 0)
541 return 0;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000542 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000543}
544
Dan Gohman10df0fa2008-08-27 01:09:54 +0000545/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
546/// to emit an instruction with a floating-point immediate operand using
547/// FastEmit_rf. If that fails, it materializes the immediate into a register
548/// and try FastEmit_rr instead.
549unsigned FastISel::FastEmit_rf_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
550 unsigned Op0, ConstantFP *FPImm,
551 MVT::SimpleValueType ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000552 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohman151ed612008-08-27 18:15:05 +0000553 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000554 if (ResultReg != 0)
555 return ResultReg;
556
557 // Materialize the constant in a register.
558 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
559 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000560 // If the target doesn't have a way to directly enter a floating-point
561 // value into a register, use an alternate approach.
562 // TODO: The current approach only supports floating-point constants
563 // that can be constructed by conversion from integer values. This should
564 // be replaced by code that creates a load from a constant-pool entry,
565 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000566 const APFloat &Flt = FPImm->getValueAPF();
567 MVT IntVT = TLI.getPointerTy();
568
569 uint64_t x[2];
570 uint32_t IntBitWidth = IntVT.getSizeInBits();
571 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
572 APFloat::rmTowardZero) != APFloat::opOK)
573 return 0;
574 APInt IntVal(IntBitWidth, 2, x);
575
576 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
577 ISD::Constant, IntVal.getZExtValue());
578 if (IntegerReg == 0)
579 return 0;
580 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
581 ISD::SINT_TO_FP, IntegerReg);
582 if (MaterialReg == 0)
583 return 0;
584 }
585 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
586}
587
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000588unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
589 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000590}
591
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000592unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000593 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000594 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000595 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000596
Dan Gohmanfd903942008-08-20 23:53:10 +0000597 BuildMI(MBB, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000598 return ResultReg;
599}
600
601unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
602 const TargetRegisterClass *RC,
603 unsigned Op0) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000604 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000605 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000606
Evan Cheng5960e4e2008-09-08 08:38:20 +0000607 if (II.getNumDefs() >= 1)
608 BuildMI(MBB, II, ResultReg).addReg(Op0);
609 else {
610 BuildMI(MBB, II).addReg(Op0);
611 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
612 II.ImplicitDefs[0], RC, RC);
613 if (!InsertedCopy)
614 ResultReg = 0;
615 }
616
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000617 return ResultReg;
618}
619
620unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
621 const TargetRegisterClass *RC,
622 unsigned Op0, unsigned Op1) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000623 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000624 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000625
Evan Cheng5960e4e2008-09-08 08:38:20 +0000626 if (II.getNumDefs() >= 1)
627 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1);
628 else {
629 BuildMI(MBB, II).addReg(Op0).addReg(Op1);
630 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
631 II.ImplicitDefs[0], RC, RC);
632 if (!InsertedCopy)
633 ResultReg = 0;
634 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000635 return ResultReg;
636}
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000637
638unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
639 const TargetRegisterClass *RC,
640 unsigned Op0, uint64_t Imm) {
641 unsigned ResultReg = createResultReg(RC);
642 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
643
Evan Cheng5960e4e2008-09-08 08:38:20 +0000644 if (II.getNumDefs() >= 1)
645 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
646 else {
647 BuildMI(MBB, II).addReg(Op0).addImm(Imm);
648 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
649 II.ImplicitDefs[0], RC, RC);
650 if (!InsertedCopy)
651 ResultReg = 0;
652 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000653 return ResultReg;
654}
655
Dan Gohman10df0fa2008-08-27 01:09:54 +0000656unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
657 const TargetRegisterClass *RC,
658 unsigned Op0, ConstantFP *FPImm) {
659 unsigned ResultReg = createResultReg(RC);
660 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
661
Evan Cheng5960e4e2008-09-08 08:38:20 +0000662 if (II.getNumDefs() >= 1)
663 BuildMI(MBB, II, ResultReg).addReg(Op0).addFPImm(FPImm);
664 else {
665 BuildMI(MBB, II).addReg(Op0).addFPImm(FPImm);
666 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
667 II.ImplicitDefs[0], RC, RC);
668 if (!InsertedCopy)
669 ResultReg = 0;
670 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000671 return ResultReg;
672}
673
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000674unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
675 const TargetRegisterClass *RC,
676 unsigned Op0, unsigned Op1, uint64_t Imm) {
677 unsigned ResultReg = createResultReg(RC);
678 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
679
Evan Cheng5960e4e2008-09-08 08:38:20 +0000680 if (II.getNumDefs() >= 1)
681 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
682 else {
683 BuildMI(MBB, II).addReg(Op0).addReg(Op1).addImm(Imm);
684 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
685 II.ImplicitDefs[0], RC, RC);
686 if (!InsertedCopy)
687 ResultReg = 0;
688 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000689 return ResultReg;
690}
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000691
692unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
693 const TargetRegisterClass *RC,
694 uint64_t Imm) {
695 unsigned ResultReg = createResultReg(RC);
696 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
697
Evan Cheng5960e4e2008-09-08 08:38:20 +0000698 if (II.getNumDefs() >= 1)
699 BuildMI(MBB, II, ResultReg).addImm(Imm);
700 else {
701 BuildMI(MBB, II).addImm(Imm);
702 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
703 II.ImplicitDefs[0], RC, RC);
704 if (!InsertedCopy)
705 ResultReg = 0;
706 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000707 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +0000708}
Owen Anderson8970f002008-08-27 22:30:02 +0000709
Owen Anderson40a468f2008-08-28 17:47:37 +0000710unsigned FastISel::FastEmitInst_extractsubreg(unsigned Op0, uint32_t Idx) {
711 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson8970f002008-08-27 22:30:02 +0000712 const TargetRegisterClass* SRC = *(RC->subregclasses_begin()+Idx-1);
713
714 unsigned ResultReg = createResultReg(SRC);
715 const TargetInstrDesc &II = TII.get(TargetInstrInfo::EXTRACT_SUBREG);
716
Evan Cheng5960e4e2008-09-08 08:38:20 +0000717 if (II.getNumDefs() >= 1)
718 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Idx);
719 else {
720 BuildMI(MBB, II).addReg(Op0).addImm(Idx);
721 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
722 II.ImplicitDefs[0], RC, RC);
723 if (!InsertedCopy)
724 ResultReg = 0;
725 }
Owen Anderson8970f002008-08-27 22:30:02 +0000726 return ResultReg;
727}