blob: 13b1793e05c21d4053154d66ff521b69572e639f [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)) {
47 if (CI->getValue().getActiveBits() > 64)
Dan Gohman0586d912008-09-10 20:11:02 +000048 return TargetMaterializeConstant(CI);
Owen Anderson99aaf102008-09-03 17:37:03 +000049 // Don't cache constant materializations. To do so would require
50 // tracking what uses they dominate.
Dan Gohman104e4ce2008-09-03 23:32:19 +000051 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Evan Cheng59fbc802008-09-09 01:26:59 +000052 } else if (isa<GlobalValue>(V)) {
Dan Gohman0586d912008-09-10 20:11:02 +000053 return TargetMaterializeConstant(cast<Constant>(V));
54 } else if (isa<AllocaInst>(V)) {
55 return TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohman205d9252008-08-28 21:19:07 +000056 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000057 Reg = FastEmit_i(VT, VT, ISD::Constant, 0);
Dan Gohmanad368ac2008-08-27 18:10:19 +000058 } else if (ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000059 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +000060
61 if (!Reg) {
62 const APFloat &Flt = CF->getValueAPF();
63 MVT IntVT = TLI.getPointerTy();
64
65 uint64_t x[2];
66 uint32_t IntBitWidth = IntVT.getSizeInBits();
67 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
68 APFloat::rmTowardZero) != APFloat::opOK)
Dan Gohman0586d912008-09-10 20:11:02 +000069 return TargetMaterializeConstant(CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +000070 APInt IntVal(IntBitWidth, 2, x);
71
72 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
73 ISD::Constant, IntVal.getZExtValue());
74 if (IntegerReg == 0)
Dan Gohman0586d912008-09-10 20:11:02 +000075 return TargetMaterializeConstant(CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +000076 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
77 if (Reg == 0)
Dan Gohman0586d912008-09-10 20:11:02 +000078 return TargetMaterializeConstant(CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +000079 }
Dan Gohman40b189e2008-09-05 18:18:20 +000080 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
81 if (!SelectOperator(CE, CE->getOpcode())) return 0;
82 Reg = LocalValueMap[CE];
Dan Gohman205d9252008-08-28 21:19:07 +000083 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000084 Reg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman205d9252008-08-28 21:19:07 +000085 BuildMI(MBB, TII.get(TargetInstrInfo::IMPLICIT_DEF), Reg);
Dan Gohman104e4ce2008-09-03 23:32:19 +000086 } else {
87 return 0;
Dan Gohmanad368ac2008-08-27 18:10:19 +000088 }
Owen Andersond5d81a42008-09-03 17:51:57 +000089
Owen Anderson6e607452008-09-05 23:36:01 +000090 if (!Reg && isa<Constant>(V))
Dan Gohman0586d912008-09-10 20:11:02 +000091 return TargetMaterializeConstant(cast<Constant>(V));
Owen Anderson6e607452008-09-05 23:36:01 +000092
Dan Gohman104e4ce2008-09-03 23:32:19 +000093 LocalValueMap[V] = Reg;
94 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +000095}
96
Evan Cheng59fbc802008-09-09 01:26:59 +000097unsigned FastISel::lookUpRegForValue(Value *V) {
98 // Look up the value to see if we already have a register for it. We
99 // cache values defined by Instructions across blocks, and other values
100 // only locally. This is because Instructions already have the SSA
101 // def-dominatess-use requirement enforced.
102 if (ValueMap.count(V))
103 return ValueMap[V];
104 return LocalValueMap[V];
105}
106
Owen Andersoncc54e762008-08-30 00:38:46 +0000107/// UpdateValueMap - Update the value map to include the new mapping for this
108/// instruction, or insert an extra copy to get the result in a previous
109/// determined register.
110/// NOTE: This is only necessary because we might select a block that uses
111/// a value before we select the block that defines the value. It might be
112/// possible to fix this by selecting blocks in reverse postorder.
Owen Anderson95267a12008-09-05 00:06:23 +0000113void FastISel::UpdateValueMap(Value* I, unsigned Reg) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000114 if (!isa<Instruction>(I)) {
115 LocalValueMap[I] = Reg;
116 return;
117 }
Owen Andersoncc54e762008-08-30 00:38:46 +0000118 if (!ValueMap.count(I))
119 ValueMap[I] = Reg;
120 else
Evan Chengf0991782008-09-07 09:04:52 +0000121 TII.copyRegToReg(*MBB, MBB->end(), ValueMap[I],
122 Reg, MRI.getRegClass(Reg), MRI.getRegClass(Reg));
Owen Andersoncc54e762008-08-30 00:38:46 +0000123}
124
Dan Gohmanbdedd442008-08-20 00:11:48 +0000125/// SelectBinaryOp - Select and emit code for a binary operator instruction,
126/// which has an opcode which directly corresponds to the given ISD opcode.
127///
Dan Gohman40b189e2008-09-05 18:18:20 +0000128bool FastISel::SelectBinaryOp(User *I, ISD::NodeType ISDOpcode) {
Dan Gohmanbdedd442008-08-20 00:11:48 +0000129 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
130 if (VT == MVT::Other || !VT.isSimple())
131 // Unhandled type. Halt "fast" selection and bail.
132 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000133
Dan Gohmanb71fea22008-08-26 20:52:40 +0000134 // We only handle legal types. For example, on x86-32 the instruction
135 // selector contains all of the 64-bit instructions from x86-64,
136 // under the assumption that i64 won't be used if the target doesn't
137 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000138 if (!TLI.isTypeLegal(VT)) {
139 // MVT::i1 is special. Allow AND and OR (but not XOR) because they
140 // don't require additional zeroing, which makes them easy.
141 if (VT == MVT::i1 &&
142 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR))
143 VT = TLI.getTypeToTransformTo(VT);
144 else
145 return false;
146 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000147
Dan Gohman3df24e62008-09-03 23:12:08 +0000148 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000149 if (Op0 == 0)
150 // Unhandled operand. Halt "fast" selection and bail.
151 return false;
152
153 // Check if the second operand is a constant and handle it appropriately.
154 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000155 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
156 ISDOpcode, Op0, CI->getZExtValue());
157 if (ResultReg != 0) {
158 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000159 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000160 return true;
161 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000162 }
163
Dan Gohman10df0fa2008-08-27 01:09:54 +0000164 // Check if the second operand is a constant float.
165 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000166 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
167 ISDOpcode, Op0, CF);
168 if (ResultReg != 0) {
169 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000170 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000171 return true;
172 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000173 }
174
Dan Gohman3df24e62008-09-03 23:12:08 +0000175 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000176 if (Op1 == 0)
177 // Unhandled operand. Halt "fast" selection and bail.
178 return false;
179
Dan Gohmanad368ac2008-08-27 18:10:19 +0000180 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000181 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
182 ISDOpcode, Op0, Op1);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000183 if (ResultReg == 0)
184 // Target-specific code wasn't able to find a machine opcode for
185 // the given ISD opcode and type. Halt "fast" selection and bail.
186 return false;
187
Dan Gohman8014e862008-08-20 00:23:20 +0000188 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000189 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000190 return true;
191}
192
Dan Gohman40b189e2008-09-05 18:18:20 +0000193bool FastISel::SelectGetElementPtr(User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000194 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000195 if (N == 0)
196 // Unhandled operand. Halt "fast" selection and bail.
197 return false;
198
199 const Type *Ty = I->getOperand(0)->getType();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000200 MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
Evan Cheng83785c82008-08-20 22:45:34 +0000201 for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
202 OI != E; ++OI) {
203 Value *Idx = *OI;
204 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
205 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
206 if (Field) {
207 // N = N + Offset
208 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
209 // FIXME: This can be optimized by combining the add with a
210 // subsequent one.
Dan Gohman7a0e6592008-08-21 17:25:26 +0000211 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000212 if (N == 0)
213 // Unhandled operand. Halt "fast" selection and bail.
214 return false;
215 }
216 Ty = StTy->getElementType(Field);
217 } else {
218 Ty = cast<SequentialType>(Ty)->getElementType();
219
220 // If this is a constant subscript, handle it quickly.
221 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
222 if (CI->getZExtValue() == 0) continue;
223 uint64_t Offs =
224 TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000225 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000226 if (N == 0)
227 // Unhandled operand. Halt "fast" selection and bail.
228 return false;
229 continue;
230 }
231
232 // N = N + Idx * ElementSize;
233 uint64_t ElementSize = TD.getABITypeSize(Ty);
Dan Gohman3df24e62008-09-03 23:12:08 +0000234 unsigned IdxN = getRegForValue(Idx);
Evan Cheng83785c82008-08-20 22:45:34 +0000235 if (IdxN == 0)
236 // Unhandled operand. Halt "fast" selection and bail.
237 return false;
238
239 // If the index is smaller or larger than intptr_t, truncate or extend
240 // it.
Evan Cheng2076aa82008-08-21 01:19:11 +0000241 MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
Evan Cheng83785c82008-08-20 22:45:34 +0000242 if (IdxVT.bitsLT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000243 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::SIGN_EXTEND, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000244 else if (IdxVT.bitsGT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000245 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::TRUNCATE, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000246 if (IdxN == 0)
247 // Unhandled operand. Halt "fast" selection and bail.
248 return false;
249
Dan Gohman80bc6e22008-08-26 20:57:08 +0000250 if (ElementSize != 1) {
Dan Gohmanf93cf792008-08-21 17:37:05 +0000251 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000252 if (IdxN == 0)
253 // Unhandled operand. Halt "fast" selection and bail.
254 return false;
255 }
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000256 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000257 if (N == 0)
258 // Unhandled operand. Halt "fast" selection and bail.
259 return false;
260 }
261 }
262
263 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000264 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000265 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000266}
267
Dan Gohman40b189e2008-09-05 18:18:20 +0000268bool FastISel::SelectCast(User *I, ISD::NodeType Opcode) {
Owen Anderson6336b702008-08-27 18:58:30 +0000269 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
270 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000271
272 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
273 DstVT == MVT::Other || !DstVT.isSimple() ||
274 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
275 // Unhandled type. Halt "fast" selection and bail.
276 return false;
277
Dan Gohman3df24e62008-09-03 23:12:08 +0000278 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000279 if (!InputReg)
280 // Unhandled operand. Halt "fast" selection and bail.
281 return false;
282
283 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
284 DstVT.getSimpleVT(),
285 Opcode,
286 InputReg);
287 if (!ResultReg)
288 return false;
289
Dan Gohman3df24e62008-09-03 23:12:08 +0000290 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000291 return true;
292}
293
Dan Gohman40b189e2008-09-05 18:18:20 +0000294bool FastISel::SelectBitCast(User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000295 // If the bitcast doesn't change the type, just use the operand value.
296 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000297 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000298 if (Reg == 0)
299 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000300 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000301 return true;
302 }
303
304 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Anderson6336b702008-08-27 18:58:30 +0000305 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
306 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000307
308 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
309 DstVT == MVT::Other || !DstVT.isSimple() ||
310 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
311 // Unhandled type. Halt "fast" selection and bail.
312 return false;
313
Dan Gohman3df24e62008-09-03 23:12:08 +0000314 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000315 if (Op0 == 0)
316 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000317 return false;
318
Dan Gohmanad368ac2008-08-27 18:10:19 +0000319 // First, try to perform the bitcast by inserting a reg-reg copy.
320 unsigned ResultReg = 0;
321 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
322 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
323 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
324 ResultReg = createResultReg(DstClass);
325
326 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
327 Op0, DstClass, SrcClass);
328 if (!InsertedCopy)
329 ResultReg = 0;
330 }
331
332 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
333 if (!ResultReg)
334 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
335 ISD::BIT_CONVERT, Op0);
336
337 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000338 return false;
339
Dan Gohman3df24e62008-09-03 23:12:08 +0000340 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000341 return true;
342}
343
Dan Gohman3df24e62008-09-03 23:12:08 +0000344bool
345FastISel::SelectInstruction(Instruction *I) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000346 return SelectOperator(I, I->getOpcode());
347}
348
349bool
350FastISel::SelectOperator(User *I, unsigned Opcode) {
351 switch (Opcode) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000352 case Instruction::Add: {
353 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
354 return SelectBinaryOp(I, Opc);
355 }
356 case Instruction::Sub: {
357 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
358 return SelectBinaryOp(I, Opc);
359 }
360 case Instruction::Mul: {
361 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
362 return SelectBinaryOp(I, Opc);
363 }
364 case Instruction::SDiv:
365 return SelectBinaryOp(I, ISD::SDIV);
366 case Instruction::UDiv:
367 return SelectBinaryOp(I, ISD::UDIV);
368 case Instruction::FDiv:
369 return SelectBinaryOp(I, ISD::FDIV);
370 case Instruction::SRem:
371 return SelectBinaryOp(I, ISD::SREM);
372 case Instruction::URem:
373 return SelectBinaryOp(I, ISD::UREM);
374 case Instruction::FRem:
375 return SelectBinaryOp(I, ISD::FREM);
376 case Instruction::Shl:
377 return SelectBinaryOp(I, ISD::SHL);
378 case Instruction::LShr:
379 return SelectBinaryOp(I, ISD::SRL);
380 case Instruction::AShr:
381 return SelectBinaryOp(I, ISD::SRA);
382 case Instruction::And:
383 return SelectBinaryOp(I, ISD::AND);
384 case Instruction::Or:
385 return SelectBinaryOp(I, ISD::OR);
386 case Instruction::Xor:
387 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000388
Dan Gohman3df24e62008-09-03 23:12:08 +0000389 case Instruction::GetElementPtr:
390 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000391
Dan Gohman3df24e62008-09-03 23:12:08 +0000392 case Instruction::Br: {
393 BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000394
Dan Gohman3df24e62008-09-03 23:12:08 +0000395 if (BI->isUnconditional()) {
396 MachineFunction::iterator NextMBB =
397 next(MachineFunction::iterator(MBB));
398 BasicBlock *LLVMSucc = BI->getSuccessor(0);
399 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
Dan Gohman6f2766d2008-08-19 22:31:46 +0000400
Dan Gohman3df24e62008-09-03 23:12:08 +0000401 if (NextMBB != MF.end() && MSucc == NextMBB) {
402 // The unconditional fall-through case, which needs no instructions.
Owen Anderson9d5b4162008-08-27 00:31:01 +0000403 } else {
Dan Gohman3df24e62008-09-03 23:12:08 +0000404 // The unconditional branch case.
405 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
Owen Anderson9d5b4162008-08-27 00:31:01 +0000406 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000407 MBB->addSuccessor(MSucc);
408 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000409 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000410
411 // Conditional branches are not handed yet.
412 // Halt "fast" selection and bail.
413 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000414 }
415
Dan Gohman087c8502008-09-05 01:08:41 +0000416 case Instruction::Unreachable:
417 // Nothing to emit.
418 return true;
419
Dan Gohman3df24e62008-09-03 23:12:08 +0000420 case Instruction::PHI:
421 // PHI nodes are already emitted.
422 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000423
424 case Instruction::Alloca:
425 // FunctionLowering has the static-sized case covered.
426 if (StaticAllocaMap.count(cast<AllocaInst>(I)))
427 return true;
428
429 // Dynamic-sized alloca is not handled yet.
430 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000431
432 case Instruction::BitCast:
433 return SelectBitCast(I);
434
435 case Instruction::FPToSI:
436 return SelectCast(I, ISD::FP_TO_SINT);
437 case Instruction::ZExt:
438 return SelectCast(I, ISD::ZERO_EXTEND);
439 case Instruction::SExt:
440 return SelectCast(I, ISD::SIGN_EXTEND);
441 case Instruction::Trunc:
442 return SelectCast(I, ISD::TRUNCATE);
443 case Instruction::SIToFP:
444 return SelectCast(I, ISD::SINT_TO_FP);
445
446 case Instruction::IntToPtr: // Deliberate fall-through.
447 case Instruction::PtrToInt: {
448 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
449 MVT DstVT = TLI.getValueType(I->getType());
450 if (DstVT.bitsGT(SrcVT))
451 return SelectCast(I, ISD::ZERO_EXTEND);
452 if (DstVT.bitsLT(SrcVT))
453 return SelectCast(I, ISD::TRUNCATE);
454 unsigned Reg = getRegForValue(I->getOperand(0));
455 if (Reg == 0) return false;
456 UpdateValueMap(I, Reg);
457 return true;
458 }
459
460 default:
461 // Unhandled instruction. Halt "fast" selection and bail.
462 return false;
463 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000464}
465
Dan Gohman3df24e62008-09-03 23:12:08 +0000466FastISel::FastISel(MachineFunction &mf,
467 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +0000468 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
469 DenseMap<const AllocaInst *, int> &am)
Dan Gohman3df24e62008-09-03 23:12:08 +0000470 : MBB(0),
471 ValueMap(vm),
472 MBBMap(bm),
Dan Gohman0586d912008-09-10 20:11:02 +0000473 StaticAllocaMap(am),
Dan Gohman3df24e62008-09-03 23:12:08 +0000474 MF(mf),
475 MRI(MF.getRegInfo()),
Dan Gohman0586d912008-09-10 20:11:02 +0000476 MFI(*MF.getFrameInfo()),
477 MCP(*MF.getConstantPool()),
Dan Gohman3df24e62008-09-03 23:12:08 +0000478 TM(MF.getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +0000479 TD(*TM.getTargetData()),
480 TII(*TM.getInstrInfo()),
481 TLI(*TM.getTargetLowering()) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000482}
483
Dan Gohmane285a742008-08-14 21:51:29 +0000484FastISel::~FastISel() {}
485
Evan Cheng36fd9412008-09-02 21:59:13 +0000486unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType,
487 ISD::NodeType) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000488 return 0;
489}
490
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000491unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
492 ISD::NodeType, unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000493 return 0;
494}
495
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000496unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType,
497 ISD::NodeType, unsigned /*Op0*/,
498 unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000499 return 0;
500}
501
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000502unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
503 ISD::NodeType, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000504 return 0;
505}
506
Dan Gohman10df0fa2008-08-27 01:09:54 +0000507unsigned FastISel::FastEmit_f(MVT::SimpleValueType, MVT::SimpleValueType,
508 ISD::NodeType, ConstantFP * /*FPImm*/) {
509 return 0;
510}
511
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000512unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
513 ISD::NodeType, unsigned /*Op0*/,
514 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000515 return 0;
516}
517
Dan Gohman10df0fa2008-08-27 01:09:54 +0000518unsigned FastISel::FastEmit_rf(MVT::SimpleValueType, MVT::SimpleValueType,
519 ISD::NodeType, unsigned /*Op0*/,
520 ConstantFP * /*FPImm*/) {
521 return 0;
522}
523
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000524unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
525 ISD::NodeType,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000526 unsigned /*Op0*/, unsigned /*Op1*/,
527 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000528 return 0;
529}
530
531/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
532/// to emit an instruction with an immediate operand using FastEmit_ri.
533/// If that fails, it materializes the immediate into a register and try
534/// FastEmit_rr instead.
535unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000536 unsigned Op0, uint64_t Imm,
537 MVT::SimpleValueType ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000538 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohman151ed612008-08-27 18:15:05 +0000539 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000540 if (ResultReg != 0)
541 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000542 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000543 if (MaterialReg == 0)
544 return 0;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000545 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000546}
547
Dan Gohman10df0fa2008-08-27 01:09:54 +0000548/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
549/// to emit an instruction with a floating-point immediate operand using
550/// FastEmit_rf. If that fails, it materializes the immediate into a register
551/// and try FastEmit_rr instead.
552unsigned FastISel::FastEmit_rf_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
553 unsigned Op0, ConstantFP *FPImm,
554 MVT::SimpleValueType ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000555 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohman151ed612008-08-27 18:15:05 +0000556 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000557 if (ResultReg != 0)
558 return ResultReg;
559
560 // Materialize the constant in a register.
561 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
562 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000563 // If the target doesn't have a way to directly enter a floating-point
564 // value into a register, use an alternate approach.
565 // TODO: The current approach only supports floating-point constants
566 // that can be constructed by conversion from integer values. This should
567 // be replaced by code that creates a load from a constant-pool entry,
568 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000569 const APFloat &Flt = FPImm->getValueAPF();
570 MVT IntVT = TLI.getPointerTy();
571
572 uint64_t x[2];
573 uint32_t IntBitWidth = IntVT.getSizeInBits();
574 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
575 APFloat::rmTowardZero) != APFloat::opOK)
576 return 0;
577 APInt IntVal(IntBitWidth, 2, x);
578
579 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
580 ISD::Constant, IntVal.getZExtValue());
581 if (IntegerReg == 0)
582 return 0;
583 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
584 ISD::SINT_TO_FP, IntegerReg);
585 if (MaterialReg == 0)
586 return 0;
587 }
588 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
589}
590
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000591unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
592 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000593}
594
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000595unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000596 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000597 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000598 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000599
Dan Gohmanfd903942008-08-20 23:53:10 +0000600 BuildMI(MBB, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000601 return ResultReg;
602}
603
604unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
605 const TargetRegisterClass *RC,
606 unsigned Op0) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000607 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000608 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000609
Evan Cheng5960e4e2008-09-08 08:38:20 +0000610 if (II.getNumDefs() >= 1)
611 BuildMI(MBB, II, ResultReg).addReg(Op0);
612 else {
613 BuildMI(MBB, II).addReg(Op0);
614 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
615 II.ImplicitDefs[0], RC, RC);
616 if (!InsertedCopy)
617 ResultReg = 0;
618 }
619
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000620 return ResultReg;
621}
622
623unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
624 const TargetRegisterClass *RC,
625 unsigned Op0, unsigned Op1) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000626 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000627 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000628
Evan Cheng5960e4e2008-09-08 08:38:20 +0000629 if (II.getNumDefs() >= 1)
630 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1);
631 else {
632 BuildMI(MBB, II).addReg(Op0).addReg(Op1);
633 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
634 II.ImplicitDefs[0], RC, RC);
635 if (!InsertedCopy)
636 ResultReg = 0;
637 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000638 return ResultReg;
639}
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000640
641unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
642 const TargetRegisterClass *RC,
643 unsigned Op0, uint64_t Imm) {
644 unsigned ResultReg = createResultReg(RC);
645 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
646
Evan Cheng5960e4e2008-09-08 08:38:20 +0000647 if (II.getNumDefs() >= 1)
648 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
649 else {
650 BuildMI(MBB, II).addReg(Op0).addImm(Imm);
651 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
652 II.ImplicitDefs[0], RC, RC);
653 if (!InsertedCopy)
654 ResultReg = 0;
655 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000656 return ResultReg;
657}
658
Dan Gohman10df0fa2008-08-27 01:09:54 +0000659unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
660 const TargetRegisterClass *RC,
661 unsigned Op0, ConstantFP *FPImm) {
662 unsigned ResultReg = createResultReg(RC);
663 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
664
Evan Cheng5960e4e2008-09-08 08:38:20 +0000665 if (II.getNumDefs() >= 1)
666 BuildMI(MBB, II, ResultReg).addReg(Op0).addFPImm(FPImm);
667 else {
668 BuildMI(MBB, II).addReg(Op0).addFPImm(FPImm);
669 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
670 II.ImplicitDefs[0], RC, RC);
671 if (!InsertedCopy)
672 ResultReg = 0;
673 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000674 return ResultReg;
675}
676
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000677unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
678 const TargetRegisterClass *RC,
679 unsigned Op0, unsigned Op1, uint64_t Imm) {
680 unsigned ResultReg = createResultReg(RC);
681 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
682
Evan Cheng5960e4e2008-09-08 08:38:20 +0000683 if (II.getNumDefs() >= 1)
684 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
685 else {
686 BuildMI(MBB, II).addReg(Op0).addReg(Op1).addImm(Imm);
687 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
688 II.ImplicitDefs[0], RC, RC);
689 if (!InsertedCopy)
690 ResultReg = 0;
691 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000692 return ResultReg;
693}
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000694
695unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
696 const TargetRegisterClass *RC,
697 uint64_t Imm) {
698 unsigned ResultReg = createResultReg(RC);
699 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
700
Evan Cheng5960e4e2008-09-08 08:38:20 +0000701 if (II.getNumDefs() >= 1)
702 BuildMI(MBB, II, ResultReg).addImm(Imm);
703 else {
704 BuildMI(MBB, II).addImm(Imm);
705 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
706 II.ImplicitDefs[0], RC, RC);
707 if (!InsertedCopy)
708 ResultReg = 0;
709 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000710 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +0000711}
Owen Anderson8970f002008-08-27 22:30:02 +0000712
Owen Anderson40a468f2008-08-28 17:47:37 +0000713unsigned FastISel::FastEmitInst_extractsubreg(unsigned Op0, uint32_t Idx) {
714 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson8970f002008-08-27 22:30:02 +0000715 const TargetRegisterClass* SRC = *(RC->subregclasses_begin()+Idx-1);
716
717 unsigned ResultReg = createResultReg(SRC);
718 const TargetInstrDesc &II = TII.get(TargetInstrInfo::EXTRACT_SUBREG);
719
Evan Cheng5960e4e2008-09-08 08:38:20 +0000720 if (II.getNumDefs() >= 1)
721 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Idx);
722 else {
723 BuildMI(MBB, II).addReg(Op0).addImm(Idx);
724 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
725 II.ImplicitDefs[0], RC, RC);
726 if (!InsertedCopy)
727 ResultReg = 0;
728 }
Owen Anderson8970f002008-08-27 22:30:02 +0000729 return ResultReg;
730}