blob: 8a70b061873ddd94b623bf733a6b270929b9a991 [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();
Owen Anderson0d952672008-09-09 20:47:17 +000036 if (!TLI.isTypeLegal(VT))
37 return 0;
Dan Gohmanad368ac2008-08-27 18:10:19 +000038 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
39 if (CI->getValue().getActiveBits() > 64)
Dan Gohman0586d912008-09-10 20:11:02 +000040 return TargetMaterializeConstant(CI);
Owen Anderson99aaf102008-09-03 17:37:03 +000041 // Don't cache constant materializations. To do so would require
42 // tracking what uses they dominate.
Dan Gohman104e4ce2008-09-03 23:32:19 +000043 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Evan Cheng59fbc802008-09-09 01:26:59 +000044 } else if (isa<GlobalValue>(V)) {
Dan Gohman0586d912008-09-10 20:11:02 +000045 return TargetMaterializeConstant(cast<Constant>(V));
46 } else if (isa<AllocaInst>(V)) {
47 return TargetMaterializeAlloca(cast<AllocaInst>(V));
Dan Gohman205d9252008-08-28 21:19:07 +000048 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000049 Reg = FastEmit_i(VT, VT, ISD::Constant, 0);
Dan Gohmanad368ac2008-08-27 18:10:19 +000050 } else if (ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000051 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +000052
53 if (!Reg) {
54 const APFloat &Flt = CF->getValueAPF();
55 MVT IntVT = TLI.getPointerTy();
56
57 uint64_t x[2];
58 uint32_t IntBitWidth = IntVT.getSizeInBits();
59 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
60 APFloat::rmTowardZero) != APFloat::opOK)
Dan Gohman0586d912008-09-10 20:11:02 +000061 return TargetMaterializeConstant(CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +000062 APInt IntVal(IntBitWidth, 2, x);
63
64 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
65 ISD::Constant, IntVal.getZExtValue());
66 if (IntegerReg == 0)
Dan Gohman0586d912008-09-10 20:11:02 +000067 return TargetMaterializeConstant(CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +000068 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
69 if (Reg == 0)
Dan Gohman0586d912008-09-10 20:11:02 +000070 return TargetMaterializeConstant(CF);
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 Gohman104e4ce2008-09-03 23:32:19 +000078 } else {
79 return 0;
Dan Gohmanad368ac2008-08-27 18:10:19 +000080 }
Owen Andersond5d81a42008-09-03 17:51:57 +000081
Owen Anderson6e607452008-09-05 23:36:01 +000082 if (!Reg && isa<Constant>(V))
Dan Gohman0586d912008-09-10 20:11:02 +000083 return TargetMaterializeConstant(cast<Constant>(V));
Owen Anderson6e607452008-09-05 23:36:01 +000084
Dan Gohman104e4ce2008-09-03 23:32:19 +000085 LocalValueMap[V] = Reg;
86 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +000087}
88
Evan Cheng59fbc802008-09-09 01:26:59 +000089unsigned FastISel::lookUpRegForValue(Value *V) {
90 // Look up the value to see if we already have a register for it. We
91 // cache values defined by Instructions across blocks, and other values
92 // only locally. This is because Instructions already have the SSA
93 // def-dominatess-use requirement enforced.
94 if (ValueMap.count(V))
95 return ValueMap[V];
96 return LocalValueMap[V];
97}
98
Owen Andersoncc54e762008-08-30 00:38:46 +000099/// UpdateValueMap - Update the value map to include the new mapping for this
100/// instruction, or insert an extra copy to get the result in a previous
101/// determined register.
102/// NOTE: This is only necessary because we might select a block that uses
103/// a value before we select the block that defines the value. It might be
104/// possible to fix this by selecting blocks in reverse postorder.
Owen Anderson95267a12008-09-05 00:06:23 +0000105void FastISel::UpdateValueMap(Value* I, unsigned Reg) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000106 if (!isa<Instruction>(I)) {
107 LocalValueMap[I] = Reg;
108 return;
109 }
Owen Andersoncc54e762008-08-30 00:38:46 +0000110 if (!ValueMap.count(I))
111 ValueMap[I] = Reg;
112 else
Evan Chengf0991782008-09-07 09:04:52 +0000113 TII.copyRegToReg(*MBB, MBB->end(), ValueMap[I],
114 Reg, MRI.getRegClass(Reg), MRI.getRegClass(Reg));
Owen Andersoncc54e762008-08-30 00:38:46 +0000115}
116
Dan Gohmanbdedd442008-08-20 00:11:48 +0000117/// SelectBinaryOp - Select and emit code for a binary operator instruction,
118/// which has an opcode which directly corresponds to the given ISD opcode.
119///
Dan Gohman40b189e2008-09-05 18:18:20 +0000120bool FastISel::SelectBinaryOp(User *I, ISD::NodeType ISDOpcode) {
Dan Gohmanbdedd442008-08-20 00:11:48 +0000121 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
122 if (VT == MVT::Other || !VT.isSimple())
123 // Unhandled type. Halt "fast" selection and bail.
124 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000125
Dan Gohmanb71fea22008-08-26 20:52:40 +0000126 // We only handle legal types. For example, on x86-32 the instruction
127 // selector contains all of the 64-bit instructions from x86-64,
128 // under the assumption that i64 won't be used if the target doesn't
129 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000130 if (!TLI.isTypeLegal(VT)) {
131 // MVT::i1 is special. Allow AND and OR (but not XOR) because they
132 // don't require additional zeroing, which makes them easy.
133 if (VT == MVT::i1 &&
134 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR))
135 VT = TLI.getTypeToTransformTo(VT);
136 else
137 return false;
138 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000139
Dan Gohman3df24e62008-09-03 23:12:08 +0000140 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000141 if (Op0 == 0)
142 // Unhandled operand. Halt "fast" selection and bail.
143 return false;
144
145 // Check if the second operand is a constant and handle it appropriately.
146 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000147 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
148 ISDOpcode, Op0, CI->getZExtValue());
149 if (ResultReg != 0) {
150 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000151 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000152 return true;
153 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000154 }
155
Dan Gohman10df0fa2008-08-27 01:09:54 +0000156 // Check if the second operand is a constant float.
157 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000158 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
159 ISDOpcode, Op0, CF);
160 if (ResultReg != 0) {
161 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000162 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000163 return true;
164 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000165 }
166
Dan Gohman3df24e62008-09-03 23:12:08 +0000167 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000168 if (Op1 == 0)
169 // Unhandled operand. Halt "fast" selection and bail.
170 return false;
171
Dan Gohmanad368ac2008-08-27 18:10:19 +0000172 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000173 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
174 ISDOpcode, Op0, Op1);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000175 if (ResultReg == 0)
176 // Target-specific code wasn't able to find a machine opcode for
177 // the given ISD opcode and type. Halt "fast" selection and bail.
178 return false;
179
Dan Gohman8014e862008-08-20 00:23:20 +0000180 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000181 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000182 return true;
183}
184
Dan Gohman40b189e2008-09-05 18:18:20 +0000185bool FastISel::SelectGetElementPtr(User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000186 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000187 if (N == 0)
188 // Unhandled operand. Halt "fast" selection and bail.
189 return false;
190
191 const Type *Ty = I->getOperand(0)->getType();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000192 MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
Evan Cheng83785c82008-08-20 22:45:34 +0000193 for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
194 OI != E; ++OI) {
195 Value *Idx = *OI;
196 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
197 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
198 if (Field) {
199 // N = N + Offset
200 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
201 // FIXME: This can be optimized by combining the add with a
202 // subsequent one.
Dan Gohman7a0e6592008-08-21 17:25:26 +0000203 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000204 if (N == 0)
205 // Unhandled operand. Halt "fast" selection and bail.
206 return false;
207 }
208 Ty = StTy->getElementType(Field);
209 } else {
210 Ty = cast<SequentialType>(Ty)->getElementType();
211
212 // If this is a constant subscript, handle it quickly.
213 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
214 if (CI->getZExtValue() == 0) continue;
215 uint64_t Offs =
216 TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000217 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000218 if (N == 0)
219 // Unhandled operand. Halt "fast" selection and bail.
220 return false;
221 continue;
222 }
223
224 // N = N + Idx * ElementSize;
225 uint64_t ElementSize = TD.getABITypeSize(Ty);
Dan Gohman3df24e62008-09-03 23:12:08 +0000226 unsigned IdxN = getRegForValue(Idx);
Evan Cheng83785c82008-08-20 22:45:34 +0000227 if (IdxN == 0)
228 // Unhandled operand. Halt "fast" selection and bail.
229 return false;
230
231 // If the index is smaller or larger than intptr_t, truncate or extend
232 // it.
Evan Cheng2076aa82008-08-21 01:19:11 +0000233 MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
Evan Cheng83785c82008-08-20 22:45:34 +0000234 if (IdxVT.bitsLT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000235 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::SIGN_EXTEND, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000236 else if (IdxVT.bitsGT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000237 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::TRUNCATE, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000238 if (IdxN == 0)
239 // Unhandled operand. Halt "fast" selection and bail.
240 return false;
241
Dan Gohman80bc6e22008-08-26 20:57:08 +0000242 if (ElementSize != 1) {
Dan Gohmanf93cf792008-08-21 17:37:05 +0000243 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000244 if (IdxN == 0)
245 // Unhandled operand. Halt "fast" selection and bail.
246 return false;
247 }
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000248 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000249 if (N == 0)
250 // Unhandled operand. Halt "fast" selection and bail.
251 return false;
252 }
253 }
254
255 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000256 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000257 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000258}
259
Dan Gohman40b189e2008-09-05 18:18:20 +0000260bool FastISel::SelectCast(User *I, ISD::NodeType Opcode) {
Owen Anderson6336b702008-08-27 18:58:30 +0000261 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
262 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000263
264 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
265 DstVT == MVT::Other || !DstVT.isSimple() ||
266 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
267 // Unhandled type. Halt "fast" selection and bail.
268 return false;
269
Dan Gohman3df24e62008-09-03 23:12:08 +0000270 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000271 if (!InputReg)
272 // Unhandled operand. Halt "fast" selection and bail.
273 return false;
274
275 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
276 DstVT.getSimpleVT(),
277 Opcode,
278 InputReg);
279 if (!ResultReg)
280 return false;
281
Dan Gohman3df24e62008-09-03 23:12:08 +0000282 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000283 return true;
284}
285
Dan Gohman40b189e2008-09-05 18:18:20 +0000286bool FastISel::SelectBitCast(User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000287 // If the bitcast doesn't change the type, just use the operand value.
288 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000289 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000290 if (Reg == 0)
291 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000292 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000293 return true;
294 }
295
296 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Anderson6336b702008-08-27 18:58:30 +0000297 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
298 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000299
300 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
301 DstVT == MVT::Other || !DstVT.isSimple() ||
302 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
303 // Unhandled type. Halt "fast" selection and bail.
304 return false;
305
Dan Gohman3df24e62008-09-03 23:12:08 +0000306 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000307 if (Op0 == 0)
308 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000309 return false;
310
Dan Gohmanad368ac2008-08-27 18:10:19 +0000311 // First, try to perform the bitcast by inserting a reg-reg copy.
312 unsigned ResultReg = 0;
313 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
314 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
315 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
316 ResultReg = createResultReg(DstClass);
317
318 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
319 Op0, DstClass, SrcClass);
320 if (!InsertedCopy)
321 ResultReg = 0;
322 }
323
324 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
325 if (!ResultReg)
326 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
327 ISD::BIT_CONVERT, Op0);
328
329 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000330 return false;
331
Dan Gohman3df24e62008-09-03 23:12:08 +0000332 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000333 return true;
334}
335
Dan Gohman3df24e62008-09-03 23:12:08 +0000336bool
337FastISel::SelectInstruction(Instruction *I) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000338 return SelectOperator(I, I->getOpcode());
339}
340
341bool
342FastISel::SelectOperator(User *I, unsigned Opcode) {
343 switch (Opcode) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000344 case Instruction::Add: {
345 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
346 return SelectBinaryOp(I, Opc);
347 }
348 case Instruction::Sub: {
349 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
350 return SelectBinaryOp(I, Opc);
351 }
352 case Instruction::Mul: {
353 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
354 return SelectBinaryOp(I, Opc);
355 }
356 case Instruction::SDiv:
357 return SelectBinaryOp(I, ISD::SDIV);
358 case Instruction::UDiv:
359 return SelectBinaryOp(I, ISD::UDIV);
360 case Instruction::FDiv:
361 return SelectBinaryOp(I, ISD::FDIV);
362 case Instruction::SRem:
363 return SelectBinaryOp(I, ISD::SREM);
364 case Instruction::URem:
365 return SelectBinaryOp(I, ISD::UREM);
366 case Instruction::FRem:
367 return SelectBinaryOp(I, ISD::FREM);
368 case Instruction::Shl:
369 return SelectBinaryOp(I, ISD::SHL);
370 case Instruction::LShr:
371 return SelectBinaryOp(I, ISD::SRL);
372 case Instruction::AShr:
373 return SelectBinaryOp(I, ISD::SRA);
374 case Instruction::And:
375 return SelectBinaryOp(I, ISD::AND);
376 case Instruction::Or:
377 return SelectBinaryOp(I, ISD::OR);
378 case Instruction::Xor:
379 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000380
Dan Gohman3df24e62008-09-03 23:12:08 +0000381 case Instruction::GetElementPtr:
382 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000383
Dan Gohman3df24e62008-09-03 23:12:08 +0000384 case Instruction::Br: {
385 BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000386
Dan Gohman3df24e62008-09-03 23:12:08 +0000387 if (BI->isUnconditional()) {
388 MachineFunction::iterator NextMBB =
389 next(MachineFunction::iterator(MBB));
390 BasicBlock *LLVMSucc = BI->getSuccessor(0);
391 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
Dan Gohman6f2766d2008-08-19 22:31:46 +0000392
Dan Gohman3df24e62008-09-03 23:12:08 +0000393 if (NextMBB != MF.end() && MSucc == NextMBB) {
394 // The unconditional fall-through case, which needs no instructions.
Owen Anderson9d5b4162008-08-27 00:31:01 +0000395 } else {
Dan Gohman3df24e62008-09-03 23:12:08 +0000396 // The unconditional branch case.
397 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
Owen Anderson9d5b4162008-08-27 00:31:01 +0000398 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000399 MBB->addSuccessor(MSucc);
400 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000401 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000402
403 // Conditional branches are not handed yet.
404 // Halt "fast" selection and bail.
405 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000406 }
407
Dan Gohman087c8502008-09-05 01:08:41 +0000408 case Instruction::Unreachable:
409 // Nothing to emit.
410 return true;
411
Dan Gohman3df24e62008-09-03 23:12:08 +0000412 case Instruction::PHI:
413 // PHI nodes are already emitted.
414 return true;
Dan Gohman0586d912008-09-10 20:11:02 +0000415
416 case Instruction::Alloca:
417 // FunctionLowering has the static-sized case covered.
418 if (StaticAllocaMap.count(cast<AllocaInst>(I)))
419 return true;
420
421 // Dynamic-sized alloca is not handled yet.
422 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000423
424 case Instruction::BitCast:
425 return SelectBitCast(I);
426
427 case Instruction::FPToSI:
428 return SelectCast(I, ISD::FP_TO_SINT);
429 case Instruction::ZExt:
430 return SelectCast(I, ISD::ZERO_EXTEND);
431 case Instruction::SExt:
432 return SelectCast(I, ISD::SIGN_EXTEND);
433 case Instruction::Trunc:
434 return SelectCast(I, ISD::TRUNCATE);
435 case Instruction::SIToFP:
436 return SelectCast(I, ISD::SINT_TO_FP);
437
438 case Instruction::IntToPtr: // Deliberate fall-through.
439 case Instruction::PtrToInt: {
440 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
441 MVT DstVT = TLI.getValueType(I->getType());
442 if (DstVT.bitsGT(SrcVT))
443 return SelectCast(I, ISD::ZERO_EXTEND);
444 if (DstVT.bitsLT(SrcVT))
445 return SelectCast(I, ISD::TRUNCATE);
446 unsigned Reg = getRegForValue(I->getOperand(0));
447 if (Reg == 0) return false;
448 UpdateValueMap(I, Reg);
449 return true;
450 }
451
452 default:
453 // Unhandled instruction. Halt "fast" selection and bail.
454 return false;
455 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000456}
457
Dan Gohman3df24e62008-09-03 23:12:08 +0000458FastISel::FastISel(MachineFunction &mf,
459 DenseMap<const Value *, unsigned> &vm,
Dan Gohman0586d912008-09-10 20:11:02 +0000460 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
461 DenseMap<const AllocaInst *, int> &am)
Dan Gohman3df24e62008-09-03 23:12:08 +0000462 : MBB(0),
463 ValueMap(vm),
464 MBBMap(bm),
Dan Gohman0586d912008-09-10 20:11:02 +0000465 StaticAllocaMap(am),
Dan Gohman3df24e62008-09-03 23:12:08 +0000466 MF(mf),
467 MRI(MF.getRegInfo()),
Dan Gohman0586d912008-09-10 20:11:02 +0000468 MFI(*MF.getFrameInfo()),
469 MCP(*MF.getConstantPool()),
Dan Gohman3df24e62008-09-03 23:12:08 +0000470 TM(MF.getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +0000471 TD(*TM.getTargetData()),
472 TII(*TM.getInstrInfo()),
473 TLI(*TM.getTargetLowering()) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000474}
475
Dan Gohmane285a742008-08-14 21:51:29 +0000476FastISel::~FastISel() {}
477
Evan Cheng36fd9412008-09-02 21:59:13 +0000478unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType,
479 ISD::NodeType) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000480 return 0;
481}
482
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000483unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
484 ISD::NodeType, unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000485 return 0;
486}
487
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000488unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType,
489 ISD::NodeType, unsigned /*Op0*/,
490 unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000491 return 0;
492}
493
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000494unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
495 ISD::NodeType, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000496 return 0;
497}
498
Dan Gohman10df0fa2008-08-27 01:09:54 +0000499unsigned FastISel::FastEmit_f(MVT::SimpleValueType, MVT::SimpleValueType,
500 ISD::NodeType, ConstantFP * /*FPImm*/) {
501 return 0;
502}
503
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000504unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
505 ISD::NodeType, unsigned /*Op0*/,
506 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000507 return 0;
508}
509
Dan Gohman10df0fa2008-08-27 01:09:54 +0000510unsigned FastISel::FastEmit_rf(MVT::SimpleValueType, MVT::SimpleValueType,
511 ISD::NodeType, unsigned /*Op0*/,
512 ConstantFP * /*FPImm*/) {
513 return 0;
514}
515
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000516unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
517 ISD::NodeType,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000518 unsigned /*Op0*/, unsigned /*Op1*/,
519 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000520 return 0;
521}
522
523/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
524/// to emit an instruction with an immediate operand using FastEmit_ri.
525/// If that fails, it materializes the immediate into a register and try
526/// FastEmit_rr instead.
527unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000528 unsigned Op0, uint64_t Imm,
529 MVT::SimpleValueType ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000530 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohman151ed612008-08-27 18:15:05 +0000531 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000532 if (ResultReg != 0)
533 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000534 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000535 if (MaterialReg == 0)
536 return 0;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000537 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000538}
539
Dan Gohman10df0fa2008-08-27 01:09:54 +0000540/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
541/// to emit an instruction with a floating-point immediate operand using
542/// FastEmit_rf. If that fails, it materializes the immediate into a register
543/// and try FastEmit_rr instead.
544unsigned FastISel::FastEmit_rf_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
545 unsigned Op0, ConstantFP *FPImm,
546 MVT::SimpleValueType ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000547 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohman151ed612008-08-27 18:15:05 +0000548 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000549 if (ResultReg != 0)
550 return ResultReg;
551
552 // Materialize the constant in a register.
553 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
554 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000555 // If the target doesn't have a way to directly enter a floating-point
556 // value into a register, use an alternate approach.
557 // TODO: The current approach only supports floating-point constants
558 // that can be constructed by conversion from integer values. This should
559 // be replaced by code that creates a load from a constant-pool entry,
560 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000561 const APFloat &Flt = FPImm->getValueAPF();
562 MVT IntVT = TLI.getPointerTy();
563
564 uint64_t x[2];
565 uint32_t IntBitWidth = IntVT.getSizeInBits();
566 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
567 APFloat::rmTowardZero) != APFloat::opOK)
568 return 0;
569 APInt IntVal(IntBitWidth, 2, x);
570
571 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
572 ISD::Constant, IntVal.getZExtValue());
573 if (IntegerReg == 0)
574 return 0;
575 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
576 ISD::SINT_TO_FP, IntegerReg);
577 if (MaterialReg == 0)
578 return 0;
579 }
580 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
581}
582
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000583unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
584 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000585}
586
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000587unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000588 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000589 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000590 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000591
Dan Gohmanfd903942008-08-20 23:53:10 +0000592 BuildMI(MBB, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000593 return ResultReg;
594}
595
596unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
597 const TargetRegisterClass *RC,
598 unsigned Op0) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000599 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000600 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000601
Evan Cheng5960e4e2008-09-08 08:38:20 +0000602 if (II.getNumDefs() >= 1)
603 BuildMI(MBB, II, ResultReg).addReg(Op0);
604 else {
605 BuildMI(MBB, II).addReg(Op0);
606 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
607 II.ImplicitDefs[0], RC, RC);
608 if (!InsertedCopy)
609 ResultReg = 0;
610 }
611
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000612 return ResultReg;
613}
614
615unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
616 const TargetRegisterClass *RC,
617 unsigned Op0, unsigned Op1) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000618 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000619 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000620
Evan Cheng5960e4e2008-09-08 08:38:20 +0000621 if (II.getNumDefs() >= 1)
622 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1);
623 else {
624 BuildMI(MBB, II).addReg(Op0).addReg(Op1);
625 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
626 II.ImplicitDefs[0], RC, RC);
627 if (!InsertedCopy)
628 ResultReg = 0;
629 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000630 return ResultReg;
631}
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000632
633unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
634 const TargetRegisterClass *RC,
635 unsigned Op0, uint64_t Imm) {
636 unsigned ResultReg = createResultReg(RC);
637 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
638
Evan Cheng5960e4e2008-09-08 08:38:20 +0000639 if (II.getNumDefs() >= 1)
640 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
641 else {
642 BuildMI(MBB, II).addReg(Op0).addImm(Imm);
643 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
644 II.ImplicitDefs[0], RC, RC);
645 if (!InsertedCopy)
646 ResultReg = 0;
647 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000648 return ResultReg;
649}
650
Dan Gohman10df0fa2008-08-27 01:09:54 +0000651unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
652 const TargetRegisterClass *RC,
653 unsigned Op0, ConstantFP *FPImm) {
654 unsigned ResultReg = createResultReg(RC);
655 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
656
Evan Cheng5960e4e2008-09-08 08:38:20 +0000657 if (II.getNumDefs() >= 1)
658 BuildMI(MBB, II, ResultReg).addReg(Op0).addFPImm(FPImm);
659 else {
660 BuildMI(MBB, II).addReg(Op0).addFPImm(FPImm);
661 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
662 II.ImplicitDefs[0], RC, RC);
663 if (!InsertedCopy)
664 ResultReg = 0;
665 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000666 return ResultReg;
667}
668
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000669unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
670 const TargetRegisterClass *RC,
671 unsigned Op0, unsigned Op1, uint64_t Imm) {
672 unsigned ResultReg = createResultReg(RC);
673 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
674
Evan Cheng5960e4e2008-09-08 08:38:20 +0000675 if (II.getNumDefs() >= 1)
676 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
677 else {
678 BuildMI(MBB, II).addReg(Op0).addReg(Op1).addImm(Imm);
679 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
680 II.ImplicitDefs[0], RC, RC);
681 if (!InsertedCopy)
682 ResultReg = 0;
683 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000684 return ResultReg;
685}
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000686
687unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
688 const TargetRegisterClass *RC,
689 uint64_t Imm) {
690 unsigned ResultReg = createResultReg(RC);
691 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
692
Evan Cheng5960e4e2008-09-08 08:38:20 +0000693 if (II.getNumDefs() >= 1)
694 BuildMI(MBB, II, ResultReg).addImm(Imm);
695 else {
696 BuildMI(MBB, II).addImm(Imm);
697 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
698 II.ImplicitDefs[0], RC, RC);
699 if (!InsertedCopy)
700 ResultReg = 0;
701 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000702 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +0000703}
Owen Anderson8970f002008-08-27 22:30:02 +0000704
Owen Anderson40a468f2008-08-28 17:47:37 +0000705unsigned FastISel::FastEmitInst_extractsubreg(unsigned Op0, uint32_t Idx) {
706 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson8970f002008-08-27 22:30:02 +0000707 const TargetRegisterClass* SRC = *(RC->subregclasses_begin()+Idx-1);
708
709 unsigned ResultReg = createResultReg(SRC);
710 const TargetInstrDesc &II = TII.get(TargetInstrInfo::EXTRACT_SUBREG);
711
Evan Cheng5960e4e2008-09-08 08:38:20 +0000712 if (II.getNumDefs() >= 1)
713 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Idx);
714 else {
715 BuildMI(MBB, II).addReg(Op0).addImm(Idx);
716 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
717 II.ImplicitDefs[0], RC, RC);
718 if (!InsertedCopy)
719 ResultReg = 0;
720 }
Owen Anderson8970f002008-08-27 22:30:02 +0000721 return ResultReg;
722}