blob: 912dc322ff2646c66be098ec6eac95e4005d26b3 [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)
Owen Anderson6e607452008-09-05 23:36:01 +000040 return TargetMaterializeConstant(CI,
41 MBB->getParent()->getConstantPool());
Owen Anderson99aaf102008-09-03 17:37:03 +000042 // Don't cache constant materializations. To do so would require
43 // tracking what uses they dominate.
Dan Gohman104e4ce2008-09-03 23:32:19 +000044 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Evan Cheng59fbc802008-09-09 01:26:59 +000045 } else if (isa<GlobalValue>(V)) {
46 return TargetMaterializeConstant(dyn_cast<Constant>(V),
47 MBB->getParent()->getConstantPool());
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)
Owen Anderson6e607452008-09-05 23:36:01 +000061 return TargetMaterializeConstant(CF,
62 MBB->getParent()->getConstantPool());
Dan Gohmanad368ac2008-08-27 18:10:19 +000063 APInt IntVal(IntBitWidth, 2, x);
64
65 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
66 ISD::Constant, IntVal.getZExtValue());
67 if (IntegerReg == 0)
Owen Anderson6e607452008-09-05 23:36:01 +000068 return TargetMaterializeConstant(CF,
69 MBB->getParent()->getConstantPool());
Dan Gohmanad368ac2008-08-27 18:10:19 +000070 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
71 if (Reg == 0)
Owen Anderson6e607452008-09-05 23:36:01 +000072 return TargetMaterializeConstant(CF,
73 MBB->getParent()->getConstantPool());;
Dan Gohmanad368ac2008-08-27 18:10:19 +000074 }
Dan Gohman40b189e2008-09-05 18:18:20 +000075 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
76 if (!SelectOperator(CE, CE->getOpcode())) return 0;
77 Reg = LocalValueMap[CE];
Dan Gohman205d9252008-08-28 21:19:07 +000078 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000079 Reg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman205d9252008-08-28 21:19:07 +000080 BuildMI(MBB, TII.get(TargetInstrInfo::IMPLICIT_DEF), Reg);
Dan Gohman104e4ce2008-09-03 23:32:19 +000081 } else {
82 return 0;
Dan Gohmanad368ac2008-08-27 18:10:19 +000083 }
Owen Andersond5d81a42008-09-03 17:51:57 +000084
Owen Anderson6e607452008-09-05 23:36:01 +000085 if (!Reg && isa<Constant>(V))
86 return TargetMaterializeConstant(cast<Constant>(V),
87 MBB->getParent()->getConstantPool());
88
Dan Gohman104e4ce2008-09-03 23:32:19 +000089 LocalValueMap[V] = Reg;
90 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +000091}
92
Evan Cheng59fbc802008-09-09 01:26:59 +000093unsigned FastISel::lookUpRegForValue(Value *V) {
94 // Look up the value to see if we already have a register for it. We
95 // cache values defined by Instructions across blocks, and other values
96 // only locally. This is because Instructions already have the SSA
97 // def-dominatess-use requirement enforced.
98 if (ValueMap.count(V))
99 return ValueMap[V];
100 return LocalValueMap[V];
101}
102
Owen Andersoncc54e762008-08-30 00:38:46 +0000103/// UpdateValueMap - Update the value map to include the new mapping for this
104/// instruction, or insert an extra copy to get the result in a previous
105/// determined register.
106/// NOTE: This is only necessary because we might select a block that uses
107/// a value before we select the block that defines the value. It might be
108/// possible to fix this by selecting blocks in reverse postorder.
Owen Anderson95267a12008-09-05 00:06:23 +0000109void FastISel::UpdateValueMap(Value* I, unsigned Reg) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000110 if (!isa<Instruction>(I)) {
111 LocalValueMap[I] = Reg;
112 return;
113 }
Owen Andersoncc54e762008-08-30 00:38:46 +0000114 if (!ValueMap.count(I))
115 ValueMap[I] = Reg;
116 else
Evan Chengf0991782008-09-07 09:04:52 +0000117 TII.copyRegToReg(*MBB, MBB->end(), ValueMap[I],
118 Reg, MRI.getRegClass(Reg), MRI.getRegClass(Reg));
Owen Andersoncc54e762008-08-30 00:38:46 +0000119}
120
Dan Gohmanbdedd442008-08-20 00:11:48 +0000121/// SelectBinaryOp - Select and emit code for a binary operator instruction,
122/// which has an opcode which directly corresponds to the given ISD opcode.
123///
Dan Gohman40b189e2008-09-05 18:18:20 +0000124bool FastISel::SelectBinaryOp(User *I, ISD::NodeType ISDOpcode) {
Dan Gohmanbdedd442008-08-20 00:11:48 +0000125 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
126 if (VT == MVT::Other || !VT.isSimple())
127 // Unhandled type. Halt "fast" selection and bail.
128 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000129
Dan Gohmanb71fea22008-08-26 20:52:40 +0000130 // We only handle legal types. For example, on x86-32 the instruction
131 // selector contains all of the 64-bit instructions from x86-64,
132 // under the assumption that i64 won't be used if the target doesn't
133 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000134 if (!TLI.isTypeLegal(VT)) {
135 // MVT::i1 is special. Allow AND and OR (but not XOR) because they
136 // don't require additional zeroing, which makes them easy.
137 if (VT == MVT::i1 &&
138 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR))
139 VT = TLI.getTypeToTransformTo(VT);
140 else
141 return false;
142 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000143
Dan Gohman3df24e62008-09-03 23:12:08 +0000144 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000145 if (Op0 == 0)
146 // Unhandled operand. Halt "fast" selection and bail.
147 return false;
148
149 // Check if the second operand is a constant and handle it appropriately.
150 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000151 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
152 ISDOpcode, Op0, CI->getZExtValue());
153 if (ResultReg != 0) {
154 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000155 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000156 return true;
157 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000158 }
159
Dan Gohman10df0fa2008-08-27 01:09:54 +0000160 // Check if the second operand is a constant float.
161 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000162 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
163 ISDOpcode, Op0, CF);
164 if (ResultReg != 0) {
165 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000166 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000167 return true;
168 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000169 }
170
Dan Gohman3df24e62008-09-03 23:12:08 +0000171 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000172 if (Op1 == 0)
173 // Unhandled operand. Halt "fast" selection and bail.
174 return false;
175
Dan Gohmanad368ac2008-08-27 18:10:19 +0000176 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000177 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
178 ISDOpcode, Op0, Op1);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000179 if (ResultReg == 0)
180 // Target-specific code wasn't able to find a machine opcode for
181 // the given ISD opcode and type. Halt "fast" selection and bail.
182 return false;
183
Dan Gohman8014e862008-08-20 00:23:20 +0000184 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000185 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000186 return true;
187}
188
Dan Gohman40b189e2008-09-05 18:18:20 +0000189bool FastISel::SelectGetElementPtr(User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000190 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000191 if (N == 0)
192 // Unhandled operand. Halt "fast" selection and bail.
193 return false;
194
195 const Type *Ty = I->getOperand(0)->getType();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000196 MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
Evan Cheng83785c82008-08-20 22:45:34 +0000197 for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
198 OI != E; ++OI) {
199 Value *Idx = *OI;
200 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
201 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
202 if (Field) {
203 // N = N + Offset
204 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
205 // FIXME: This can be optimized by combining the add with a
206 // subsequent one.
Dan Gohman7a0e6592008-08-21 17:25:26 +0000207 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000208 if (N == 0)
209 // Unhandled operand. Halt "fast" selection and bail.
210 return false;
211 }
212 Ty = StTy->getElementType(Field);
213 } else {
214 Ty = cast<SequentialType>(Ty)->getElementType();
215
216 // If this is a constant subscript, handle it quickly.
217 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
218 if (CI->getZExtValue() == 0) continue;
219 uint64_t Offs =
220 TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000221 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000222 if (N == 0)
223 // Unhandled operand. Halt "fast" selection and bail.
224 return false;
225 continue;
226 }
227
228 // N = N + Idx * ElementSize;
229 uint64_t ElementSize = TD.getABITypeSize(Ty);
Dan Gohman3df24e62008-09-03 23:12:08 +0000230 unsigned IdxN = getRegForValue(Idx);
Evan Cheng83785c82008-08-20 22:45:34 +0000231 if (IdxN == 0)
232 // Unhandled operand. Halt "fast" selection and bail.
233 return false;
234
235 // If the index is smaller or larger than intptr_t, truncate or extend
236 // it.
Evan Cheng2076aa82008-08-21 01:19:11 +0000237 MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
Evan Cheng83785c82008-08-20 22:45:34 +0000238 if (IdxVT.bitsLT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000239 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::SIGN_EXTEND, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000240 else if (IdxVT.bitsGT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000241 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::TRUNCATE, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000242 if (IdxN == 0)
243 // Unhandled operand. Halt "fast" selection and bail.
244 return false;
245
Dan Gohman80bc6e22008-08-26 20:57:08 +0000246 if (ElementSize != 1) {
Dan Gohmanf93cf792008-08-21 17:37:05 +0000247 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000248 if (IdxN == 0)
249 // Unhandled operand. Halt "fast" selection and bail.
250 return false;
251 }
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000252 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000253 if (N == 0)
254 // Unhandled operand. Halt "fast" selection and bail.
255 return false;
256 }
257 }
258
259 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000260 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000261 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000262}
263
Dan Gohman40b189e2008-09-05 18:18:20 +0000264bool FastISel::SelectCast(User *I, ISD::NodeType Opcode) {
Owen Anderson6336b702008-08-27 18:58:30 +0000265 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
266 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000267
268 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
269 DstVT == MVT::Other || !DstVT.isSimple() ||
270 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
271 // Unhandled type. Halt "fast" selection and bail.
272 return false;
273
Dan Gohman3df24e62008-09-03 23:12:08 +0000274 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000275 if (!InputReg)
276 // Unhandled operand. Halt "fast" selection and bail.
277 return false;
278
279 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
280 DstVT.getSimpleVT(),
281 Opcode,
282 InputReg);
283 if (!ResultReg)
284 return false;
285
Dan Gohman3df24e62008-09-03 23:12:08 +0000286 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000287 return true;
288}
289
Dan Gohman40b189e2008-09-05 18:18:20 +0000290bool FastISel::SelectBitCast(User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000291 // If the bitcast doesn't change the type, just use the operand value.
292 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000293 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000294 if (Reg == 0)
295 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000296 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000297 return true;
298 }
299
300 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Anderson6336b702008-08-27 18:58:30 +0000301 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
302 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000303
304 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
305 DstVT == MVT::Other || !DstVT.isSimple() ||
306 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
307 // Unhandled type. Halt "fast" selection and bail.
308 return false;
309
Dan Gohman3df24e62008-09-03 23:12:08 +0000310 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000311 if (Op0 == 0)
312 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000313 return false;
314
Dan Gohmanad368ac2008-08-27 18:10:19 +0000315 // First, try to perform the bitcast by inserting a reg-reg copy.
316 unsigned ResultReg = 0;
317 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
318 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
319 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
320 ResultReg = createResultReg(DstClass);
321
322 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
323 Op0, DstClass, SrcClass);
324 if (!InsertedCopy)
325 ResultReg = 0;
326 }
327
328 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
329 if (!ResultReg)
330 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
331 ISD::BIT_CONVERT, Op0);
332
333 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000334 return false;
335
Dan Gohman3df24e62008-09-03 23:12:08 +0000336 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000337 return true;
338}
339
Dan Gohman3df24e62008-09-03 23:12:08 +0000340bool
341FastISel::SelectInstruction(Instruction *I) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000342 return SelectOperator(I, I->getOpcode());
343}
344
345bool
346FastISel::SelectOperator(User *I, unsigned Opcode) {
347 switch (Opcode) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000348 case Instruction::Add: {
349 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
350 return SelectBinaryOp(I, Opc);
351 }
352 case Instruction::Sub: {
353 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
354 return SelectBinaryOp(I, Opc);
355 }
356 case Instruction::Mul: {
357 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
358 return SelectBinaryOp(I, Opc);
359 }
360 case Instruction::SDiv:
361 return SelectBinaryOp(I, ISD::SDIV);
362 case Instruction::UDiv:
363 return SelectBinaryOp(I, ISD::UDIV);
364 case Instruction::FDiv:
365 return SelectBinaryOp(I, ISD::FDIV);
366 case Instruction::SRem:
367 return SelectBinaryOp(I, ISD::SREM);
368 case Instruction::URem:
369 return SelectBinaryOp(I, ISD::UREM);
370 case Instruction::FRem:
371 return SelectBinaryOp(I, ISD::FREM);
372 case Instruction::Shl:
373 return SelectBinaryOp(I, ISD::SHL);
374 case Instruction::LShr:
375 return SelectBinaryOp(I, ISD::SRL);
376 case Instruction::AShr:
377 return SelectBinaryOp(I, ISD::SRA);
378 case Instruction::And:
379 return SelectBinaryOp(I, ISD::AND);
380 case Instruction::Or:
381 return SelectBinaryOp(I, ISD::OR);
382 case Instruction::Xor:
383 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000384
Dan Gohman3df24e62008-09-03 23:12:08 +0000385 case Instruction::GetElementPtr:
386 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000387
Dan Gohman3df24e62008-09-03 23:12:08 +0000388 case Instruction::Br: {
389 BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000390
Dan Gohman3df24e62008-09-03 23:12:08 +0000391 if (BI->isUnconditional()) {
392 MachineFunction::iterator NextMBB =
393 next(MachineFunction::iterator(MBB));
394 BasicBlock *LLVMSucc = BI->getSuccessor(0);
395 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
Dan Gohman6f2766d2008-08-19 22:31:46 +0000396
Dan Gohman3df24e62008-09-03 23:12:08 +0000397 if (NextMBB != MF.end() && MSucc == NextMBB) {
398 // The unconditional fall-through case, which needs no instructions.
Owen Anderson9d5b4162008-08-27 00:31:01 +0000399 } else {
Dan Gohman3df24e62008-09-03 23:12:08 +0000400 // The unconditional branch case.
401 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
Owen Anderson9d5b4162008-08-27 00:31:01 +0000402 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000403 MBB->addSuccessor(MSucc);
404 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000405 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000406
407 // Conditional branches are not handed yet.
408 // Halt "fast" selection and bail.
409 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000410 }
411
Dan Gohman087c8502008-09-05 01:08:41 +0000412 case Instruction::Unreachable:
413 // Nothing to emit.
414 return true;
415
Dan Gohman3df24e62008-09-03 23:12:08 +0000416 case Instruction::PHI:
417 // PHI nodes are already emitted.
418 return true;
419
420 case Instruction::BitCast:
421 return SelectBitCast(I);
422
423 case Instruction::FPToSI:
424 return SelectCast(I, ISD::FP_TO_SINT);
425 case Instruction::ZExt:
426 return SelectCast(I, ISD::ZERO_EXTEND);
427 case Instruction::SExt:
428 return SelectCast(I, ISD::SIGN_EXTEND);
429 case Instruction::Trunc:
430 return SelectCast(I, ISD::TRUNCATE);
431 case Instruction::SIToFP:
432 return SelectCast(I, ISD::SINT_TO_FP);
433
434 case Instruction::IntToPtr: // Deliberate fall-through.
435 case Instruction::PtrToInt: {
436 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
437 MVT DstVT = TLI.getValueType(I->getType());
438 if (DstVT.bitsGT(SrcVT))
439 return SelectCast(I, ISD::ZERO_EXTEND);
440 if (DstVT.bitsLT(SrcVT))
441 return SelectCast(I, ISD::TRUNCATE);
442 unsigned Reg = getRegForValue(I->getOperand(0));
443 if (Reg == 0) return false;
444 UpdateValueMap(I, Reg);
445 return true;
446 }
447
448 default:
449 // Unhandled instruction. Halt "fast" selection and bail.
450 return false;
451 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000452}
453
Dan Gohman3df24e62008-09-03 23:12:08 +0000454FastISel::FastISel(MachineFunction &mf,
455 DenseMap<const Value *, unsigned> &vm,
456 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm)
457 : MBB(0),
458 ValueMap(vm),
459 MBBMap(bm),
460 MF(mf),
461 MRI(MF.getRegInfo()),
462 TM(MF.getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +0000463 TD(*TM.getTargetData()),
464 TII(*TM.getInstrInfo()),
465 TLI(*TM.getTargetLowering()) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000466}
467
Dan Gohmane285a742008-08-14 21:51:29 +0000468FastISel::~FastISel() {}
469
Evan Cheng36fd9412008-09-02 21:59:13 +0000470unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType,
471 ISD::NodeType) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000472 return 0;
473}
474
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000475unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
476 ISD::NodeType, unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000477 return 0;
478}
479
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000480unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType,
481 ISD::NodeType, unsigned /*Op0*/,
482 unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000483 return 0;
484}
485
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000486unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
487 ISD::NodeType, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000488 return 0;
489}
490
Dan Gohman10df0fa2008-08-27 01:09:54 +0000491unsigned FastISel::FastEmit_f(MVT::SimpleValueType, MVT::SimpleValueType,
492 ISD::NodeType, ConstantFP * /*FPImm*/) {
493 return 0;
494}
495
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000496unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
497 ISD::NodeType, unsigned /*Op0*/,
498 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000499 return 0;
500}
501
Dan Gohman10df0fa2008-08-27 01:09:54 +0000502unsigned FastISel::FastEmit_rf(MVT::SimpleValueType, MVT::SimpleValueType,
503 ISD::NodeType, unsigned /*Op0*/,
504 ConstantFP * /*FPImm*/) {
505 return 0;
506}
507
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000508unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
509 ISD::NodeType,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000510 unsigned /*Op0*/, unsigned /*Op1*/,
511 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000512 return 0;
513}
514
515/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
516/// to emit an instruction with an immediate operand using FastEmit_ri.
517/// If that fails, it materializes the immediate into a register and try
518/// FastEmit_rr instead.
519unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000520 unsigned Op0, uint64_t Imm,
521 MVT::SimpleValueType ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000522 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohman151ed612008-08-27 18:15:05 +0000523 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000524 if (ResultReg != 0)
525 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000526 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000527 if (MaterialReg == 0)
528 return 0;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000529 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000530}
531
Dan Gohman10df0fa2008-08-27 01:09:54 +0000532/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
533/// to emit an instruction with a floating-point immediate operand using
534/// FastEmit_rf. If that fails, it materializes the immediate into a register
535/// and try FastEmit_rr instead.
536unsigned FastISel::FastEmit_rf_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
537 unsigned Op0, ConstantFP *FPImm,
538 MVT::SimpleValueType ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000539 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohman151ed612008-08-27 18:15:05 +0000540 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000541 if (ResultReg != 0)
542 return ResultReg;
543
544 // Materialize the constant in a register.
545 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
546 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000547 // If the target doesn't have a way to directly enter a floating-point
548 // value into a register, use an alternate approach.
549 // TODO: The current approach only supports floating-point constants
550 // that can be constructed by conversion from integer values. This should
551 // be replaced by code that creates a load from a constant-pool entry,
552 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000553 const APFloat &Flt = FPImm->getValueAPF();
554 MVT IntVT = TLI.getPointerTy();
555
556 uint64_t x[2];
557 uint32_t IntBitWidth = IntVT.getSizeInBits();
558 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
559 APFloat::rmTowardZero) != APFloat::opOK)
560 return 0;
561 APInt IntVal(IntBitWidth, 2, x);
562
563 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
564 ISD::Constant, IntVal.getZExtValue());
565 if (IntegerReg == 0)
566 return 0;
567 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
568 ISD::SINT_TO_FP, IntegerReg);
569 if (MaterialReg == 0)
570 return 0;
571 }
572 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
573}
574
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000575unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
576 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000577}
578
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000579unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000580 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000581 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000582 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000583
Dan Gohmanfd903942008-08-20 23:53:10 +0000584 BuildMI(MBB, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000585 return ResultReg;
586}
587
588unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
589 const TargetRegisterClass *RC,
590 unsigned Op0) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000591 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000592 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000593
Evan Cheng5960e4e2008-09-08 08:38:20 +0000594 if (II.getNumDefs() >= 1)
595 BuildMI(MBB, II, ResultReg).addReg(Op0);
596 else {
597 BuildMI(MBB, II).addReg(Op0);
598 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
599 II.ImplicitDefs[0], RC, RC);
600 if (!InsertedCopy)
601 ResultReg = 0;
602 }
603
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000604 return ResultReg;
605}
606
607unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
608 const TargetRegisterClass *RC,
609 unsigned Op0, unsigned Op1) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000610 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000611 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000612
Evan Cheng5960e4e2008-09-08 08:38:20 +0000613 if (II.getNumDefs() >= 1)
614 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1);
615 else {
616 BuildMI(MBB, II).addReg(Op0).addReg(Op1);
617 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
618 II.ImplicitDefs[0], RC, RC);
619 if (!InsertedCopy)
620 ResultReg = 0;
621 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000622 return ResultReg;
623}
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000624
625unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
626 const TargetRegisterClass *RC,
627 unsigned Op0, uint64_t Imm) {
628 unsigned ResultReg = createResultReg(RC);
629 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
630
Evan Cheng5960e4e2008-09-08 08:38:20 +0000631 if (II.getNumDefs() >= 1)
632 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
633 else {
634 BuildMI(MBB, II).addReg(Op0).addImm(Imm);
635 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
636 II.ImplicitDefs[0], RC, RC);
637 if (!InsertedCopy)
638 ResultReg = 0;
639 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000640 return ResultReg;
641}
642
Dan Gohman10df0fa2008-08-27 01:09:54 +0000643unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
644 const TargetRegisterClass *RC,
645 unsigned Op0, ConstantFP *FPImm) {
646 unsigned ResultReg = createResultReg(RC);
647 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
648
Evan Cheng5960e4e2008-09-08 08:38:20 +0000649 if (II.getNumDefs() >= 1)
650 BuildMI(MBB, II, ResultReg).addReg(Op0).addFPImm(FPImm);
651 else {
652 BuildMI(MBB, II).addReg(Op0).addFPImm(FPImm);
653 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
654 II.ImplicitDefs[0], RC, RC);
655 if (!InsertedCopy)
656 ResultReg = 0;
657 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000658 return ResultReg;
659}
660
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000661unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
662 const TargetRegisterClass *RC,
663 unsigned Op0, unsigned Op1, uint64_t Imm) {
664 unsigned ResultReg = createResultReg(RC);
665 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
666
Evan Cheng5960e4e2008-09-08 08:38:20 +0000667 if (II.getNumDefs() >= 1)
668 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
669 else {
670 BuildMI(MBB, II).addReg(Op0).addReg(Op1).addImm(Imm);
671 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
672 II.ImplicitDefs[0], RC, RC);
673 if (!InsertedCopy)
674 ResultReg = 0;
675 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000676 return ResultReg;
677}
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000678
679unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
680 const TargetRegisterClass *RC,
681 uint64_t Imm) {
682 unsigned ResultReg = createResultReg(RC);
683 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
684
Evan Cheng5960e4e2008-09-08 08:38:20 +0000685 if (II.getNumDefs() >= 1)
686 BuildMI(MBB, II, ResultReg).addImm(Imm);
687 else {
688 BuildMI(MBB, II).addImm(Imm);
689 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
690 II.ImplicitDefs[0], RC, RC);
691 if (!InsertedCopy)
692 ResultReg = 0;
693 }
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000694 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +0000695}
Owen Anderson8970f002008-08-27 22:30:02 +0000696
Owen Anderson40a468f2008-08-28 17:47:37 +0000697unsigned FastISel::FastEmitInst_extractsubreg(unsigned Op0, uint32_t Idx) {
698 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson8970f002008-08-27 22:30:02 +0000699 const TargetRegisterClass* SRC = *(RC->subregclasses_begin()+Idx-1);
700
701 unsigned ResultReg = createResultReg(SRC);
702 const TargetInstrDesc &II = TII.get(TargetInstrInfo::EXTRACT_SUBREG);
703
Evan Cheng5960e4e2008-09-08 08:38:20 +0000704 if (II.getNumDefs() >= 1)
705 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Idx);
706 else {
707 BuildMI(MBB, II).addReg(Op0).addImm(Idx);
708 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
709 II.ImplicitDefs[0], RC, RC);
710 if (!InsertedCopy)
711 ResultReg = 0;
712 }
Owen Anderson8970f002008-08-27 22:30:02 +0000713 return ResultReg;
714}