blob: c109a096a1f7f5c01b166f8bd430268515158ff4 [file] [log] [blame]
Dan Gohmanb0cf29c2008-08-13 20:19:35 +00001///===-- FastISel.cpp - Implementation of the FastISel class --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the implementation of the FastISel class.
11//
12//===----------------------------------------------------------------------===//
13
Dan Gohman6f2766d2008-08-19 22:31:46 +000014#include "llvm/Instructions.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000015#include "llvm/CodeGen/FastISel.h"
16#include "llvm/CodeGen/MachineInstrBuilder.h"
17#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng83785c82008-08-20 22:45:34 +000018#include "llvm/Target/TargetData.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000019#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng83785c82008-08-20 22:45:34 +000020#include "llvm/Target/TargetLowering.h"
Dan Gohmanbb466332008-08-20 21:05:57 +000021#include "llvm/Target/TargetMachine.h"
Dan Gohmanb0cf29c2008-08-13 20:19:35 +000022using namespace llvm;
23
Dan Gohman3df24e62008-09-03 23:12:08 +000024unsigned FastISel::getRegForValue(Value *V) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000025 // Look up the value to see if we already have a register for it. We
26 // cache values defined by Instructions across blocks, and other values
27 // only locally. This is because Instructions already have the SSA
28 // def-dominatess-use requirement enforced.
Owen Anderson99aaf102008-09-03 17:37:03 +000029 if (ValueMap.count(V))
30 return ValueMap[V];
Dan Gohman104e4ce2008-09-03 23:32:19 +000031 unsigned Reg = LocalValueMap[V];
32 if (Reg != 0)
33 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +000034
35 MVT::SimpleValueType VT = TLI.getValueType(V->getType()).getSimpleVT();
36 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
37 if (CI->getValue().getActiveBits() > 64)
Owen Anderson6e607452008-09-05 23:36:01 +000038 return TargetMaterializeConstant(CI,
39 MBB->getParent()->getConstantPool());
Owen Anderson99aaf102008-09-03 17:37:03 +000040 // Don't cache constant materializations. To do so would require
41 // tracking what uses they dominate.
Dan Gohman104e4ce2008-09-03 23:32:19 +000042 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
Dan Gohman205d9252008-08-28 21:19:07 +000043 } else if (isa<ConstantPointerNull>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000044 Reg = FastEmit_i(VT, VT, ISD::Constant, 0);
Dan Gohmanad368ac2008-08-27 18:10:19 +000045 } else if (ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000046 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
Dan Gohmanad368ac2008-08-27 18:10:19 +000047
48 if (!Reg) {
49 const APFloat &Flt = CF->getValueAPF();
50 MVT IntVT = TLI.getPointerTy();
51
52 uint64_t x[2];
53 uint32_t IntBitWidth = IntVT.getSizeInBits();
54 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
55 APFloat::rmTowardZero) != APFloat::opOK)
Owen Anderson6e607452008-09-05 23:36:01 +000056 return TargetMaterializeConstant(CF,
57 MBB->getParent()->getConstantPool());
Dan Gohmanad368ac2008-08-27 18:10:19 +000058 APInt IntVal(IntBitWidth, 2, x);
59
60 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
61 ISD::Constant, IntVal.getZExtValue());
62 if (IntegerReg == 0)
Owen Anderson6e607452008-09-05 23:36:01 +000063 return TargetMaterializeConstant(CF,
64 MBB->getParent()->getConstantPool());
Dan Gohmanad368ac2008-08-27 18:10:19 +000065 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
66 if (Reg == 0)
Owen Anderson6e607452008-09-05 23:36:01 +000067 return TargetMaterializeConstant(CF,
68 MBB->getParent()->getConstantPool());;
Dan Gohmanad368ac2008-08-27 18:10:19 +000069 }
Dan Gohman40b189e2008-09-05 18:18:20 +000070 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
71 if (!SelectOperator(CE, CE->getOpcode())) return 0;
72 Reg = LocalValueMap[CE];
Dan Gohman205d9252008-08-28 21:19:07 +000073 } else if (isa<UndefValue>(V)) {
Dan Gohman104e4ce2008-09-03 23:32:19 +000074 Reg = createResultReg(TLI.getRegClassFor(VT));
Dan Gohman205d9252008-08-28 21:19:07 +000075 BuildMI(MBB, TII.get(TargetInstrInfo::IMPLICIT_DEF), Reg);
Dan Gohman104e4ce2008-09-03 23:32:19 +000076 } else {
77 return 0;
Dan Gohmanad368ac2008-08-27 18:10:19 +000078 }
Owen Andersond5d81a42008-09-03 17:51:57 +000079
Owen Anderson6e607452008-09-05 23:36:01 +000080 if (!Reg && isa<Constant>(V))
81 return TargetMaterializeConstant(cast<Constant>(V),
82 MBB->getParent()->getConstantPool());
83
Dan Gohman104e4ce2008-09-03 23:32:19 +000084 LocalValueMap[V] = Reg;
85 return Reg;
Dan Gohmanad368ac2008-08-27 18:10:19 +000086}
87
Owen Andersoncc54e762008-08-30 00:38:46 +000088/// UpdateValueMap - Update the value map to include the new mapping for this
89/// instruction, or insert an extra copy to get the result in a previous
90/// determined register.
91/// NOTE: This is only necessary because we might select a block that uses
92/// a value before we select the block that defines the value. It might be
93/// possible to fix this by selecting blocks in reverse postorder.
Owen Anderson95267a12008-09-05 00:06:23 +000094void FastISel::UpdateValueMap(Value* I, unsigned Reg) {
Dan Gohman40b189e2008-09-05 18:18:20 +000095 if (!isa<Instruction>(I)) {
96 LocalValueMap[I] = Reg;
97 return;
98 }
Owen Andersoncc54e762008-08-30 00:38:46 +000099 if (!ValueMap.count(I))
100 ValueMap[I] = Reg;
101 else
102 TII.copyRegToReg(*MBB, MBB->end(), ValueMap[I],
103 Reg, MRI.getRegClass(Reg), MRI.getRegClass(Reg));
104}
105
Dan Gohmanbdedd442008-08-20 00:11:48 +0000106/// SelectBinaryOp - Select and emit code for a binary operator instruction,
107/// which has an opcode which directly corresponds to the given ISD opcode.
108///
Dan Gohman40b189e2008-09-05 18:18:20 +0000109bool FastISel::SelectBinaryOp(User *I, ISD::NodeType ISDOpcode) {
Dan Gohmanbdedd442008-08-20 00:11:48 +0000110 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
111 if (VT == MVT::Other || !VT.isSimple())
112 // Unhandled type. Halt "fast" selection and bail.
113 return false;
Dan Gohman638c6832008-09-05 18:44:22 +0000114
Dan Gohmanb71fea22008-08-26 20:52:40 +0000115 // We only handle legal types. For example, on x86-32 the instruction
116 // selector contains all of the 64-bit instructions from x86-64,
117 // under the assumption that i64 won't be used if the target doesn't
118 // support it.
Dan Gohman638c6832008-09-05 18:44:22 +0000119 if (!TLI.isTypeLegal(VT)) {
120 // MVT::i1 is special. Allow AND and OR (but not XOR) because they
121 // don't require additional zeroing, which makes them easy.
122 if (VT == MVT::i1 &&
123 (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR))
124 VT = TLI.getTypeToTransformTo(VT);
125 else
126 return false;
127 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000128
Dan Gohman3df24e62008-09-03 23:12:08 +0000129 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000130 if (Op0 == 0)
131 // Unhandled operand. Halt "fast" selection and bail.
132 return false;
133
134 // Check if the second operand is a constant and handle it appropriately.
135 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000136 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
137 ISDOpcode, Op0, CI->getZExtValue());
138 if (ResultReg != 0) {
139 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000140 UpdateValueMap(I, ResultReg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000141 return true;
142 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000143 }
144
Dan Gohman10df0fa2008-08-27 01:09:54 +0000145 // Check if the second operand is a constant float.
146 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000147 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
148 ISDOpcode, Op0, CF);
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 Gohman10df0fa2008-08-27 01:09:54 +0000154 }
155
Dan Gohman3df24e62008-09-03 23:12:08 +0000156 unsigned Op1 = getRegForValue(I->getOperand(1));
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000157 if (Op1 == 0)
158 // Unhandled operand. Halt "fast" selection and bail.
159 return false;
160
Dan Gohmanad368ac2008-08-27 18:10:19 +0000161 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000162 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
163 ISDOpcode, Op0, Op1);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000164 if (ResultReg == 0)
165 // Target-specific code wasn't able to find a machine opcode for
166 // the given ISD opcode and type. Halt "fast" selection and bail.
167 return false;
168
Dan Gohman8014e862008-08-20 00:23:20 +0000169 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000170 UpdateValueMap(I, ResultReg);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000171 return true;
172}
173
Dan Gohman40b189e2008-09-05 18:18:20 +0000174bool FastISel::SelectGetElementPtr(User *I) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000175 unsigned N = getRegForValue(I->getOperand(0));
Evan Cheng83785c82008-08-20 22:45:34 +0000176 if (N == 0)
177 // Unhandled operand. Halt "fast" selection and bail.
178 return false;
179
180 const Type *Ty = I->getOperand(0)->getType();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000181 MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
Evan Cheng83785c82008-08-20 22:45:34 +0000182 for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
183 OI != E; ++OI) {
184 Value *Idx = *OI;
185 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
186 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
187 if (Field) {
188 // N = N + Offset
189 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
190 // FIXME: This can be optimized by combining the add with a
191 // subsequent one.
Dan Gohman7a0e6592008-08-21 17:25:26 +0000192 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000193 if (N == 0)
194 // Unhandled operand. Halt "fast" selection and bail.
195 return false;
196 }
197 Ty = StTy->getElementType(Field);
198 } else {
199 Ty = cast<SequentialType>(Ty)->getElementType();
200
201 // If this is a constant subscript, handle it quickly.
202 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
203 if (CI->getZExtValue() == 0) continue;
204 uint64_t Offs =
205 TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000206 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000207 if (N == 0)
208 // Unhandled operand. Halt "fast" selection and bail.
209 return false;
210 continue;
211 }
212
213 // N = N + Idx * ElementSize;
214 uint64_t ElementSize = TD.getABITypeSize(Ty);
Dan Gohman3df24e62008-09-03 23:12:08 +0000215 unsigned IdxN = getRegForValue(Idx);
Evan Cheng83785c82008-08-20 22:45:34 +0000216 if (IdxN == 0)
217 // Unhandled operand. Halt "fast" selection and bail.
218 return false;
219
220 // If the index is smaller or larger than intptr_t, truncate or extend
221 // it.
Evan Cheng2076aa82008-08-21 01:19:11 +0000222 MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
Evan Cheng83785c82008-08-20 22:45:34 +0000223 if (IdxVT.bitsLT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000224 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::SIGN_EXTEND, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000225 else if (IdxVT.bitsGT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000226 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::TRUNCATE, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000227 if (IdxN == 0)
228 // Unhandled operand. Halt "fast" selection and bail.
229 return false;
230
Dan Gohman80bc6e22008-08-26 20:57:08 +0000231 if (ElementSize != 1) {
Dan Gohmanf93cf792008-08-21 17:37:05 +0000232 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000233 if (IdxN == 0)
234 // Unhandled operand. Halt "fast" selection and bail.
235 return false;
236 }
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000237 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000238 if (N == 0)
239 // Unhandled operand. Halt "fast" selection and bail.
240 return false;
241 }
242 }
243
244 // We successfully emitted code for the given LLVM Instruction.
Dan Gohman3df24e62008-09-03 23:12:08 +0000245 UpdateValueMap(I, N);
Evan Cheng83785c82008-08-20 22:45:34 +0000246 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000247}
248
Dan Gohman40b189e2008-09-05 18:18:20 +0000249bool FastISel::SelectCast(User *I, ISD::NodeType Opcode) {
Owen Anderson6336b702008-08-27 18:58:30 +0000250 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
251 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000252
253 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
254 DstVT == MVT::Other || !DstVT.isSimple() ||
255 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
256 // Unhandled type. Halt "fast" selection and bail.
257 return false;
258
Dan Gohman3df24e62008-09-03 23:12:08 +0000259 unsigned InputReg = getRegForValue(I->getOperand(0));
Owen Andersond0533c92008-08-26 23:46:32 +0000260 if (!InputReg)
261 // Unhandled operand. Halt "fast" selection and bail.
262 return false;
263
264 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
265 DstVT.getSimpleVT(),
266 Opcode,
267 InputReg);
268 if (!ResultReg)
269 return false;
270
Dan Gohman3df24e62008-09-03 23:12:08 +0000271 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000272 return true;
273}
274
Dan Gohman40b189e2008-09-05 18:18:20 +0000275bool FastISel::SelectBitCast(User *I) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000276 // If the bitcast doesn't change the type, just use the operand value.
277 if (I->getType() == I->getOperand(0)->getType()) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000278 unsigned Reg = getRegForValue(I->getOperand(0));
Dan Gohmana318dab2008-08-27 20:41:38 +0000279 if (Reg == 0)
280 return false;
Dan Gohman3df24e62008-09-03 23:12:08 +0000281 UpdateValueMap(I, Reg);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000282 return true;
283 }
284
285 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
Owen Anderson6336b702008-08-27 18:58:30 +0000286 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
287 MVT DstVT = TLI.getValueType(I->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000288
289 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
290 DstVT == MVT::Other || !DstVT.isSimple() ||
291 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
292 // Unhandled type. Halt "fast" selection and bail.
293 return false;
294
Dan Gohman3df24e62008-09-03 23:12:08 +0000295 unsigned Op0 = getRegForValue(I->getOperand(0));
Dan Gohmanad368ac2008-08-27 18:10:19 +0000296 if (Op0 == 0)
297 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000298 return false;
299
Dan Gohmanad368ac2008-08-27 18:10:19 +0000300 // First, try to perform the bitcast by inserting a reg-reg copy.
301 unsigned ResultReg = 0;
302 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
303 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
304 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
305 ResultReg = createResultReg(DstClass);
306
307 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
308 Op0, DstClass, SrcClass);
309 if (!InsertedCopy)
310 ResultReg = 0;
311 }
312
313 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
314 if (!ResultReg)
315 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
316 ISD::BIT_CONVERT, Op0);
317
318 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000319 return false;
320
Dan Gohman3df24e62008-09-03 23:12:08 +0000321 UpdateValueMap(I, ResultReg);
Owen Andersond0533c92008-08-26 23:46:32 +0000322 return true;
323}
324
Dan Gohman3df24e62008-09-03 23:12:08 +0000325bool
326FastISel::SelectInstruction(Instruction *I) {
Dan Gohman40b189e2008-09-05 18:18:20 +0000327 return SelectOperator(I, I->getOpcode());
328}
329
330bool
331FastISel::SelectOperator(User *I, unsigned Opcode) {
332 switch (Opcode) {
Dan Gohman3df24e62008-09-03 23:12:08 +0000333 case Instruction::Add: {
334 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
335 return SelectBinaryOp(I, Opc);
336 }
337 case Instruction::Sub: {
338 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
339 return SelectBinaryOp(I, Opc);
340 }
341 case Instruction::Mul: {
342 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
343 return SelectBinaryOp(I, Opc);
344 }
345 case Instruction::SDiv:
346 return SelectBinaryOp(I, ISD::SDIV);
347 case Instruction::UDiv:
348 return SelectBinaryOp(I, ISD::UDIV);
349 case Instruction::FDiv:
350 return SelectBinaryOp(I, ISD::FDIV);
351 case Instruction::SRem:
352 return SelectBinaryOp(I, ISD::SREM);
353 case Instruction::URem:
354 return SelectBinaryOp(I, ISD::UREM);
355 case Instruction::FRem:
356 return SelectBinaryOp(I, ISD::FREM);
357 case Instruction::Shl:
358 return SelectBinaryOp(I, ISD::SHL);
359 case Instruction::LShr:
360 return SelectBinaryOp(I, ISD::SRL);
361 case Instruction::AShr:
362 return SelectBinaryOp(I, ISD::SRA);
363 case Instruction::And:
364 return SelectBinaryOp(I, ISD::AND);
365 case Instruction::Or:
366 return SelectBinaryOp(I, ISD::OR);
367 case Instruction::Xor:
368 return SelectBinaryOp(I, ISD::XOR);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000369
Dan Gohman3df24e62008-09-03 23:12:08 +0000370 case Instruction::GetElementPtr:
371 return SelectGetElementPtr(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000372
Dan Gohman3df24e62008-09-03 23:12:08 +0000373 case Instruction::Br: {
374 BranchInst *BI = cast<BranchInst>(I);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000375
Dan Gohman3df24e62008-09-03 23:12:08 +0000376 if (BI->isUnconditional()) {
377 MachineFunction::iterator NextMBB =
378 next(MachineFunction::iterator(MBB));
379 BasicBlock *LLVMSucc = BI->getSuccessor(0);
380 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
Dan Gohman6f2766d2008-08-19 22:31:46 +0000381
Dan Gohman3df24e62008-09-03 23:12:08 +0000382 if (NextMBB != MF.end() && MSucc == NextMBB) {
383 // The unconditional fall-through case, which needs no instructions.
Owen Anderson9d5b4162008-08-27 00:31:01 +0000384 } else {
Dan Gohman3df24e62008-09-03 23:12:08 +0000385 // The unconditional branch case.
386 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
Owen Anderson9d5b4162008-08-27 00:31:01 +0000387 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000388 MBB->addSuccessor(MSucc);
389 return true;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000390 }
Dan Gohman3df24e62008-09-03 23:12:08 +0000391
392 // Conditional branches are not handed yet.
393 // Halt "fast" selection and bail.
394 return false;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000395 }
396
Dan Gohman087c8502008-09-05 01:08:41 +0000397 case Instruction::Unreachable:
398 // Nothing to emit.
399 return true;
400
Dan Gohman3df24e62008-09-03 23:12:08 +0000401 case Instruction::PHI:
402 // PHI nodes are already emitted.
403 return true;
404
405 case Instruction::BitCast:
406 return SelectBitCast(I);
407
408 case Instruction::FPToSI:
409 return SelectCast(I, ISD::FP_TO_SINT);
410 case Instruction::ZExt:
411 return SelectCast(I, ISD::ZERO_EXTEND);
412 case Instruction::SExt:
413 return SelectCast(I, ISD::SIGN_EXTEND);
414 case Instruction::Trunc:
415 return SelectCast(I, ISD::TRUNCATE);
416 case Instruction::SIToFP:
417 return SelectCast(I, ISD::SINT_TO_FP);
418
419 case Instruction::IntToPtr: // Deliberate fall-through.
420 case Instruction::PtrToInt: {
421 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
422 MVT DstVT = TLI.getValueType(I->getType());
423 if (DstVT.bitsGT(SrcVT))
424 return SelectCast(I, ISD::ZERO_EXTEND);
425 if (DstVT.bitsLT(SrcVT))
426 return SelectCast(I, ISD::TRUNCATE);
427 unsigned Reg = getRegForValue(I->getOperand(0));
428 if (Reg == 0) return false;
429 UpdateValueMap(I, Reg);
430 return true;
431 }
432
433 default:
434 // Unhandled instruction. Halt "fast" selection and bail.
435 return false;
436 }
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000437}
438
Dan Gohman3df24e62008-09-03 23:12:08 +0000439FastISel::FastISel(MachineFunction &mf,
440 DenseMap<const Value *, unsigned> &vm,
441 DenseMap<const BasicBlock *, MachineBasicBlock *> &bm)
442 : MBB(0),
443 ValueMap(vm),
444 MBBMap(bm),
445 MF(mf),
446 MRI(MF.getRegInfo()),
447 TM(MF.getTarget()),
Dan Gohman22bb3112008-08-22 00:20:26 +0000448 TD(*TM.getTargetData()),
449 TII(*TM.getInstrInfo()),
450 TLI(*TM.getTargetLowering()) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000451}
452
Dan Gohmane285a742008-08-14 21:51:29 +0000453FastISel::~FastISel() {}
454
Evan Cheng36fd9412008-09-02 21:59:13 +0000455unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType,
456 ISD::NodeType) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000457 return 0;
458}
459
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000460unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
461 ISD::NodeType, unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000462 return 0;
463}
464
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000465unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType,
466 ISD::NodeType, unsigned /*Op0*/,
467 unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000468 return 0;
469}
470
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000471unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
472 ISD::NodeType, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000473 return 0;
474}
475
Dan Gohman10df0fa2008-08-27 01:09:54 +0000476unsigned FastISel::FastEmit_f(MVT::SimpleValueType, MVT::SimpleValueType,
477 ISD::NodeType, ConstantFP * /*FPImm*/) {
478 return 0;
479}
480
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000481unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
482 ISD::NodeType, unsigned /*Op0*/,
483 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000484 return 0;
485}
486
Dan Gohman10df0fa2008-08-27 01:09:54 +0000487unsigned FastISel::FastEmit_rf(MVT::SimpleValueType, MVT::SimpleValueType,
488 ISD::NodeType, unsigned /*Op0*/,
489 ConstantFP * /*FPImm*/) {
490 return 0;
491}
492
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000493unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
494 ISD::NodeType,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000495 unsigned /*Op0*/, unsigned /*Op1*/,
496 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000497 return 0;
498}
499
500/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
501/// to emit an instruction with an immediate operand using FastEmit_ri.
502/// If that fails, it materializes the immediate into a register and try
503/// FastEmit_rr instead.
504unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000505 unsigned Op0, uint64_t Imm,
506 MVT::SimpleValueType ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000507 // First check if immediate type is legal. If not, we can't use the ri form.
Dan Gohman151ed612008-08-27 18:15:05 +0000508 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000509 if (ResultReg != 0)
510 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000511 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000512 if (MaterialReg == 0)
513 return 0;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000514 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000515}
516
Dan Gohman10df0fa2008-08-27 01:09:54 +0000517/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
518/// to emit an instruction with a floating-point immediate operand using
519/// FastEmit_rf. If that fails, it materializes the immediate into a register
520/// and try FastEmit_rr instead.
521unsigned FastISel::FastEmit_rf_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
522 unsigned Op0, ConstantFP *FPImm,
523 MVT::SimpleValueType ImmType) {
Dan Gohman10df0fa2008-08-27 01:09:54 +0000524 // First check if immediate type is legal. If not, we can't use the rf form.
Dan Gohman151ed612008-08-27 18:15:05 +0000525 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
Dan Gohman10df0fa2008-08-27 01:09:54 +0000526 if (ResultReg != 0)
527 return ResultReg;
528
529 // Materialize the constant in a register.
530 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
531 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000532 // If the target doesn't have a way to directly enter a floating-point
533 // value into a register, use an alternate approach.
534 // TODO: The current approach only supports floating-point constants
535 // that can be constructed by conversion from integer values. This should
536 // be replaced by code that creates a load from a constant-pool entry,
537 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000538 const APFloat &Flt = FPImm->getValueAPF();
539 MVT IntVT = TLI.getPointerTy();
540
541 uint64_t x[2];
542 uint32_t IntBitWidth = IntVT.getSizeInBits();
543 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
544 APFloat::rmTowardZero) != APFloat::opOK)
545 return 0;
546 APInt IntVal(IntBitWidth, 2, x);
547
548 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
549 ISD::Constant, IntVal.getZExtValue());
550 if (IntegerReg == 0)
551 return 0;
552 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
553 ISD::SINT_TO_FP, IntegerReg);
554 if (MaterialReg == 0)
555 return 0;
556 }
557 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
558}
559
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000560unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
561 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000562}
563
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000564unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000565 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000566 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000567 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000568
Dan Gohmanfd903942008-08-20 23:53:10 +0000569 BuildMI(MBB, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000570 return ResultReg;
571}
572
573unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
574 const TargetRegisterClass *RC,
575 unsigned Op0) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000576 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000577 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000578
Dan Gohmanfd903942008-08-20 23:53:10 +0000579 BuildMI(MBB, II, ResultReg).addReg(Op0);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000580 return ResultReg;
581}
582
583unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
584 const TargetRegisterClass *RC,
585 unsigned Op0, unsigned Op1) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000586 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000587 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000588
Dan Gohmanfd903942008-08-20 23:53:10 +0000589 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000590 return ResultReg;
591}
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000592
593unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
594 const TargetRegisterClass *RC,
595 unsigned Op0, uint64_t Imm) {
596 unsigned ResultReg = createResultReg(RC);
597 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
598
599 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
600 return ResultReg;
601}
602
Dan Gohman10df0fa2008-08-27 01:09:54 +0000603unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
604 const TargetRegisterClass *RC,
605 unsigned Op0, ConstantFP *FPImm) {
606 unsigned ResultReg = createResultReg(RC);
607 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
608
609 BuildMI(MBB, II, ResultReg).addReg(Op0).addFPImm(FPImm);
610 return ResultReg;
611}
612
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000613unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
614 const TargetRegisterClass *RC,
615 unsigned Op0, unsigned Op1, uint64_t Imm) {
616 unsigned ResultReg = createResultReg(RC);
617 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
618
619 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
620 return ResultReg;
621}
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000622
623unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
624 const TargetRegisterClass *RC,
625 uint64_t Imm) {
626 unsigned ResultReg = createResultReg(RC);
627 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
628
629 BuildMI(MBB, II, ResultReg).addImm(Imm);
630 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +0000631}
Owen Anderson8970f002008-08-27 22:30:02 +0000632
Owen Anderson40a468f2008-08-28 17:47:37 +0000633unsigned FastISel::FastEmitInst_extractsubreg(unsigned Op0, uint32_t Idx) {
634 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
Owen Anderson8970f002008-08-27 22:30:02 +0000635 const TargetRegisterClass* SRC = *(RC->subregclasses_begin()+Idx-1);
636
637 unsigned ResultReg = createResultReg(SRC);
638 const TargetInstrDesc &II = TII.get(TargetInstrInfo::EXTRACT_SUBREG);
639
Owen Andersonc0bb68b2008-08-28 18:26:01 +0000640 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Idx);
Owen Anderson8970f002008-08-27 22:30:02 +0000641 return ResultReg;
642}