blob: 78201c9adca0a6dbadc10ac3078aef4c5f3c8115 [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 Gohmanad368ac2008-08-27 18:10:19 +000024unsigned FastISel::getRegForValue(Value *V, DenseMap<const Value*, unsigned> &ValueMap) {
25 unsigned &Reg = ValueMap[V];
26 if (Reg != 0)
27 return Reg;
28
29 MVT::SimpleValueType VT = TLI.getValueType(V->getType()).getSimpleVT();
30 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
31 if (CI->getValue().getActiveBits() > 64)
32 return 0;
33 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
34 } else if (ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
35 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
36
37 if (!Reg) {
38 const APFloat &Flt = CF->getValueAPF();
39 MVT IntVT = TLI.getPointerTy();
40
41 uint64_t x[2];
42 uint32_t IntBitWidth = IntVT.getSizeInBits();
43 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
44 APFloat::rmTowardZero) != APFloat::opOK)
45 return 0;
46 APInt IntVal(IntBitWidth, 2, x);
47
48 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
49 ISD::Constant, IntVal.getZExtValue());
50 if (IntegerReg == 0)
51 return 0;
52 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
53 if (Reg == 0)
54 return 0;
55 }
56 }
57
58 return Reg;
59}
60
Dan Gohmanbdedd442008-08-20 00:11:48 +000061/// SelectBinaryOp - Select and emit code for a binary operator instruction,
62/// which has an opcode which directly corresponds to the given ISD opcode.
63///
64bool FastISel::SelectBinaryOp(Instruction *I, ISD::NodeType ISDOpcode,
65 DenseMap<const Value*, unsigned> &ValueMap) {
Dan Gohmanbdedd442008-08-20 00:11:48 +000066 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
67 if (VT == MVT::Other || !VT.isSimple())
68 // Unhandled type. Halt "fast" selection and bail.
69 return false;
Dan Gohmanb71fea22008-08-26 20:52:40 +000070 // We only handle legal types. For example, on x86-32 the instruction
71 // selector contains all of the 64-bit instructions from x86-64,
72 // under the assumption that i64 won't be used if the target doesn't
73 // support it.
74 if (!TLI.isTypeLegal(VT))
75 return false;
Dan Gohmanbdedd442008-08-20 00:11:48 +000076
Dan Gohmanad368ac2008-08-27 18:10:19 +000077 unsigned Op0 = getRegForValue(I->getOperand(0), ValueMap);
Dan Gohmand5fe57d2008-08-21 01:41:07 +000078 if (Op0 == 0)
79 // Unhandled operand. Halt "fast" selection and bail.
80 return false;
81
82 // Check if the second operand is a constant and handle it appropriately.
83 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +000084 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
85 ISDOpcode, Op0, CI->getZExtValue());
86 if (ResultReg != 0) {
87 // We successfully emitted code for the given LLVM Instruction.
88 ValueMap[I] = ResultReg;
89 return true;
90 }
Dan Gohmand5fe57d2008-08-21 01:41:07 +000091 }
92
Dan Gohman10df0fa2008-08-27 01:09:54 +000093 // Check if the second operand is a constant float.
94 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
Dan Gohmanad368ac2008-08-27 18:10:19 +000095 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
96 ISDOpcode, Op0, CF);
97 if (ResultReg != 0) {
98 // We successfully emitted code for the given LLVM Instruction.
99 ValueMap[I] = ResultReg;
100 return true;
101 }
Dan Gohman10df0fa2008-08-27 01:09:54 +0000102 }
103
Dan Gohmanad368ac2008-08-27 18:10:19 +0000104 unsigned Op1 = getRegForValue(I->getOperand(1), ValueMap);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000105 if (Op1 == 0)
106 // Unhandled operand. Halt "fast" selection and bail.
107 return false;
108
Dan Gohmanad368ac2008-08-27 18:10:19 +0000109 // Now we have both operands in registers. Emit the instruction.
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000110 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
111 ISDOpcode, Op0, Op1);
Dan Gohmanbdedd442008-08-20 00:11:48 +0000112 if (ResultReg == 0)
113 // Target-specific code wasn't able to find a machine opcode for
114 // the given ISD opcode and type. Halt "fast" selection and bail.
115 return false;
116
Dan Gohman8014e862008-08-20 00:23:20 +0000117 // We successfully emitted code for the given LLVM Instruction.
Dan Gohmanbdedd442008-08-20 00:11:48 +0000118 ValueMap[I] = ResultReg;
119 return true;
120}
121
122bool FastISel::SelectGetElementPtr(Instruction *I,
123 DenseMap<const Value*, unsigned> &ValueMap) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000124 unsigned N = getRegForValue(I->getOperand(0), ValueMap);
Evan Cheng83785c82008-08-20 22:45:34 +0000125 if (N == 0)
126 // Unhandled operand. Halt "fast" selection and bail.
127 return false;
128
129 const Type *Ty = I->getOperand(0)->getType();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000130 MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
Evan Cheng83785c82008-08-20 22:45:34 +0000131 for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
132 OI != E; ++OI) {
133 Value *Idx = *OI;
134 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
135 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
136 if (Field) {
137 // N = N + Offset
138 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
139 // FIXME: This can be optimized by combining the add with a
140 // subsequent one.
Dan Gohman7a0e6592008-08-21 17:25:26 +0000141 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000142 if (N == 0)
143 // Unhandled operand. Halt "fast" selection and bail.
144 return false;
145 }
146 Ty = StTy->getElementType(Field);
147 } else {
148 Ty = cast<SequentialType>(Ty)->getElementType();
149
150 // If this is a constant subscript, handle it quickly.
151 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
152 if (CI->getZExtValue() == 0) continue;
153 uint64_t Offs =
154 TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000155 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000156 if (N == 0)
157 // Unhandled operand. Halt "fast" selection and bail.
158 return false;
159 continue;
160 }
161
162 // N = N + Idx * ElementSize;
163 uint64_t ElementSize = TD.getABITypeSize(Ty);
Dan Gohmanad368ac2008-08-27 18:10:19 +0000164 unsigned IdxN = getRegForValue(Idx, ValueMap);
Evan Cheng83785c82008-08-20 22:45:34 +0000165 if (IdxN == 0)
166 // Unhandled operand. Halt "fast" selection and bail.
167 return false;
168
169 // If the index is smaller or larger than intptr_t, truncate or extend
170 // it.
Evan Cheng2076aa82008-08-21 01:19:11 +0000171 MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
Evan Cheng83785c82008-08-20 22:45:34 +0000172 if (IdxVT.bitsLT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000173 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::SIGN_EXTEND, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000174 else if (IdxVT.bitsGT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000175 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::TRUNCATE, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000176 if (IdxN == 0)
177 // Unhandled operand. Halt "fast" selection and bail.
178 return false;
179
Dan Gohman80bc6e22008-08-26 20:57:08 +0000180 if (ElementSize != 1) {
Dan Gohmanf93cf792008-08-21 17:37:05 +0000181 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000182 if (IdxN == 0)
183 // Unhandled operand. Halt "fast" selection and bail.
184 return false;
185 }
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000186 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000187 if (N == 0)
188 // Unhandled operand. Halt "fast" selection and bail.
189 return false;
190 }
191 }
192
193 // We successfully emitted code for the given LLVM Instruction.
194 ValueMap[I] = N;
195 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000196}
197
Owen Andersond0533c92008-08-26 23:46:32 +0000198bool FastISel::SelectCast(Instruction *I, ISD::NodeType Opcode,
199 DenseMap<const Value*, unsigned> &ValueMap) {
200 MVT SrcVT = MVT::getMVT(I->getOperand(0)->getType());
201 MVT DstVT = MVT::getMVT(I->getType());
202
203 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
204 DstVT == MVT::Other || !DstVT.isSimple() ||
205 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
206 // Unhandled type. Halt "fast" selection and bail.
207 return false;
208
Dan Gohmanad368ac2008-08-27 18:10:19 +0000209 unsigned InputReg = getRegForValue(I->getOperand(0), ValueMap);
Owen Andersond0533c92008-08-26 23:46:32 +0000210 if (!InputReg)
211 // Unhandled operand. Halt "fast" selection and bail.
212 return false;
213
214 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
215 DstVT.getSimpleVT(),
216 Opcode,
217 InputReg);
218 if (!ResultReg)
219 return false;
220
221 ValueMap[I] = ResultReg;
222 return true;
223}
224
Dan Gohmanad368ac2008-08-27 18:10:19 +0000225bool FastISel::SelectBitCast(Instruction *I,
226 DenseMap<const Value*, unsigned> &ValueMap) {
227 // If the bitcast doesn't change the type, just use the operand value.
228 if (I->getType() == I->getOperand(0)->getType()) {
229 ValueMap[I] = getRegForValue(I->getOperand(0), ValueMap);
230 return true;
231 }
232
233 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
234 MVT SrcVT = MVT::getMVT(I->getOperand(0)->getType());
Owen Andersond0533c92008-08-26 23:46:32 +0000235 MVT DstVT = MVT::getMVT(I->getType());
236
237 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
238 DstVT == MVT::Other || !DstVT.isSimple() ||
239 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
240 // Unhandled type. Halt "fast" selection and bail.
241 return false;
242
Dan Gohmanad368ac2008-08-27 18:10:19 +0000243 unsigned Op0 = getRegForValue(I->getOperand(0), ValueMap);
244 if (Op0 == 0)
245 // Unhandled operand. Halt "fast" selection and bail.
Owen Andersond0533c92008-08-26 23:46:32 +0000246 return false;
247
Dan Gohmanad368ac2008-08-27 18:10:19 +0000248 // First, try to perform the bitcast by inserting a reg-reg copy.
249 unsigned ResultReg = 0;
250 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
251 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
252 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
253 ResultReg = createResultReg(DstClass);
254
255 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
256 Op0, DstClass, SrcClass);
257 if (!InsertedCopy)
258 ResultReg = 0;
259 }
260
261 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
262 if (!ResultReg)
263 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
264 ISD::BIT_CONVERT, Op0);
265
266 if (!ResultReg)
Owen Andersond0533c92008-08-26 23:46:32 +0000267 return false;
268
Dan Gohmanad368ac2008-08-27 18:10:19 +0000269 ValueMap[I] = ResultReg;
Owen Andersond0533c92008-08-26 23:46:32 +0000270 return true;
271}
272
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000273BasicBlock::iterator
Dan Gohmanb7864a92008-08-20 18:09:02 +0000274FastISel::SelectInstructions(BasicBlock::iterator Begin,
275 BasicBlock::iterator End,
Dan Gohmanbb466332008-08-20 21:05:57 +0000276 DenseMap<const Value*, unsigned> &ValueMap,
Dan Gohman6ecf5092008-08-23 02:44:46 +0000277 DenseMap<const BasicBlock*,
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000278 MachineBasicBlock *> &MBBMap,
Dan Gohmanbb466332008-08-20 21:05:57 +0000279 MachineBasicBlock *mbb) {
280 MBB = mbb;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000281 BasicBlock::iterator I = Begin;
282
283 for (; I != End; ++I) {
284 switch (I->getOpcode()) {
Dan Gohman8014e862008-08-20 00:23:20 +0000285 case Instruction::Add: {
286 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
287 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
288 }
289 case Instruction::Sub: {
290 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
291 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
292 }
293 case Instruction::Mul: {
294 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
295 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
296 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000297 case Instruction::SDiv:
298 if (!SelectBinaryOp(I, ISD::SDIV, ValueMap)) return I; break;
299 case Instruction::UDiv:
300 if (!SelectBinaryOp(I, ISD::UDIV, ValueMap)) return I; break;
301 case Instruction::FDiv:
302 if (!SelectBinaryOp(I, ISD::FDIV, ValueMap)) return I; break;
303 case Instruction::SRem:
304 if (!SelectBinaryOp(I, ISD::SREM, ValueMap)) return I; break;
305 case Instruction::URem:
306 if (!SelectBinaryOp(I, ISD::UREM, ValueMap)) return I; break;
307 case Instruction::FRem:
308 if (!SelectBinaryOp(I, ISD::FREM, ValueMap)) return I; break;
309 case Instruction::Shl:
310 if (!SelectBinaryOp(I, ISD::SHL, ValueMap)) return I; break;
311 case Instruction::LShr:
312 if (!SelectBinaryOp(I, ISD::SRL, ValueMap)) return I; break;
313 case Instruction::AShr:
314 if (!SelectBinaryOp(I, ISD::SRA, ValueMap)) return I; break;
315 case Instruction::And:
316 if (!SelectBinaryOp(I, ISD::AND, ValueMap)) return I; break;
317 case Instruction::Or:
318 if (!SelectBinaryOp(I, ISD::OR, ValueMap)) return I; break;
319 case Instruction::Xor:
320 if (!SelectBinaryOp(I, ISD::XOR, ValueMap)) return I; break;
321
322 case Instruction::GetElementPtr:
323 if (!SelectGetElementPtr(I, ValueMap)) return I;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000324 break;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000325
Dan Gohman6f2766d2008-08-19 22:31:46 +0000326 case Instruction::Br: {
327 BranchInst *BI = cast<BranchInst>(I);
328
Dan Gohmane6798b72008-08-20 01:17:01 +0000329 if (BI->isUnconditional()) {
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000330 MachineFunction::iterator NextMBB =
Dan Gohmane6798b72008-08-20 01:17:01 +0000331 next(MachineFunction::iterator(MBB));
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000332 BasicBlock *LLVMSucc = BI->getSuccessor(0);
333 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
334
335 if (NextMBB != MF.end() && MSucc == NextMBB) {
336 // The unconditional fall-through case, which needs no instructions.
337 } else {
338 // The unconditional branch case.
339 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
Dan Gohmane6798b72008-08-20 01:17:01 +0000340 }
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000341 MBB->addSuccessor(MSucc);
342 break;
Dan Gohman6f2766d2008-08-19 22:31:46 +0000343 }
344
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000345 // Conditional branches are not handed yet.
346 // Halt "fast" selection and bail.
Dan Gohman6f2766d2008-08-19 22:31:46 +0000347 return I;
348 }
Dan Gohman3b7753b2008-08-22 17:37:48 +0000349
350 case Instruction::PHI:
351 // PHI nodes are already emitted.
352 break;
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000353
354 case Instruction::BitCast:
Owen Andersond0533c92008-08-26 23:46:32 +0000355 if (!SelectBitCast(I, ValueMap)) return I; break;
Owen Anderson46aa2f52008-08-26 17:44:42 +0000356
357 case Instruction::FPToSI:
Dan Gohmanad368ac2008-08-27 18:10:19 +0000358 if (!SelectCast(I, ISD::FP_TO_SINT, ValueMap)) return I;
Owen Andersond0533c92008-08-26 23:46:32 +0000359 break;
Owen Anderson97e25682008-08-26 23:14:49 +0000360 case Instruction::ZExt:
Dan Gohmanad368ac2008-08-27 18:10:19 +0000361 if (!SelectCast(I, ISD::ZERO_EXTEND, ValueMap)) return I;
Owen Andersond0533c92008-08-26 23:46:32 +0000362 break;
363 case Instruction::SExt:
Dan Gohmanad368ac2008-08-27 18:10:19 +0000364 if (!SelectCast(I, ISD::SIGN_EXTEND, ValueMap)) return I;
Owen Andersond0533c92008-08-26 23:46:32 +0000365 break;
Owen Andersona843b8d2008-08-26 20:37:00 +0000366 case Instruction::SIToFP:
Dan Gohmanad368ac2008-08-27 18:10:19 +0000367 if (!SelectCast(I, ISD::SINT_TO_FP, ValueMap)) return I;
Owen Andersond0533c92008-08-26 23:46:32 +0000368 break;
Dan Gohman763d8932008-08-26 21:28:54 +0000369
Owen Anderson9d5b4162008-08-27 00:31:01 +0000370 case Instruction::IntToPtr: // Deliberate fall-through.
371 case Instruction::PtrToInt: {
372 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
373 MVT DstVT = TLI.getValueType(I->getType());
374 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
Owen Anderson96c5ea82008-08-27 00:35:37 +0000375 if (ValueMap[I->getOperand(0)]) {
376 ValueMap[I] = ValueMap[I->getOperand(0)];
377 break;
378 } else
379 // Unhandled operand
380 return I;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000381 } else if (DstVT.bitsGT(SrcVT)) {
Dan Gohmanad368ac2008-08-27 18:10:19 +0000382 if (!SelectCast(I, ISD::ZERO_EXTEND, ValueMap)) return I;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000383 break;
384 } else {
385 // TODO: Handle SrcVT > DstVT, where truncation is needed.
386 return I;
387 }
388 }
389
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000390 default:
391 // Unhandled instruction. Halt "fast" selection and bail.
392 return I;
393 }
394 }
395
396 return I;
397}
398
Dan Gohmanbb466332008-08-20 21:05:57 +0000399FastISel::FastISel(MachineFunction &mf)
Dan Gohman22bb3112008-08-22 00:20:26 +0000400 : MF(mf),
401 MRI(mf.getRegInfo()),
402 TM(mf.getTarget()),
403 TD(*TM.getTargetData()),
404 TII(*TM.getInstrInfo()),
405 TLI(*TM.getTargetLowering()) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000406}
407
Dan Gohmane285a742008-08-14 21:51:29 +0000408FastISel::~FastISel() {}
409
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000410unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType, ISD::NodeType) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000411 return 0;
412}
413
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000414unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
415 ISD::NodeType, unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000416 return 0;
417}
418
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000419unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType,
420 ISD::NodeType, unsigned /*Op0*/,
421 unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000422 return 0;
423}
424
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000425unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
426 ISD::NodeType, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000427 return 0;
428}
429
Dan Gohman10df0fa2008-08-27 01:09:54 +0000430unsigned FastISel::FastEmit_f(MVT::SimpleValueType, MVT::SimpleValueType,
431 ISD::NodeType, ConstantFP * /*FPImm*/) {
432 return 0;
433}
434
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000435unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
436 ISD::NodeType, unsigned /*Op0*/,
437 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000438 return 0;
439}
440
Dan Gohman10df0fa2008-08-27 01:09:54 +0000441unsigned FastISel::FastEmit_rf(MVT::SimpleValueType, MVT::SimpleValueType,
442 ISD::NodeType, unsigned /*Op0*/,
443 ConstantFP * /*FPImm*/) {
444 return 0;
445}
446
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000447unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
448 ISD::NodeType,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000449 unsigned /*Op0*/, unsigned /*Op1*/,
450 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000451 return 0;
452}
453
454/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
455/// to emit an instruction with an immediate operand using FastEmit_ri.
456/// If that fails, it materializes the immediate into a register and try
457/// FastEmit_rr instead.
458unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000459 unsigned Op0, uint64_t Imm,
460 MVT::SimpleValueType ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000461 unsigned ResultReg = 0;
462 // First check if immediate type is legal. If not, we can't use the ri form.
463 if (TLI.getOperationAction(ISD::Constant, ImmType) == TargetLowering::Legal)
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000464 ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000465 if (ResultReg != 0)
466 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000467 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000468 if (MaterialReg == 0)
469 return 0;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000470 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000471}
472
Dan Gohman10df0fa2008-08-27 01:09:54 +0000473/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
474/// to emit an instruction with a floating-point immediate operand using
475/// FastEmit_rf. If that fails, it materializes the immediate into a register
476/// and try FastEmit_rr instead.
477unsigned FastISel::FastEmit_rf_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
478 unsigned Op0, ConstantFP *FPImm,
479 MVT::SimpleValueType ImmType) {
480 unsigned ResultReg = 0;
481 // First check if immediate type is legal. If not, we can't use the rf form.
482 if (TLI.getOperationAction(ISD::Constant, ImmType) == TargetLowering::Legal)
483 ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
484 if (ResultReg != 0)
485 return ResultReg;
486
487 // Materialize the constant in a register.
488 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
489 if (MaterialReg == 0) {
Dan Gohman96a99992008-08-27 18:01:42 +0000490 // If the target doesn't have a way to directly enter a floating-point
491 // value into a register, use an alternate approach.
492 // TODO: The current approach only supports floating-point constants
493 // that can be constructed by conversion from integer values. This should
494 // be replaced by code that creates a load from a constant-pool entry,
495 // which will require some target-specific work.
Dan Gohman10df0fa2008-08-27 01:09:54 +0000496 const APFloat &Flt = FPImm->getValueAPF();
497 MVT IntVT = TLI.getPointerTy();
498
499 uint64_t x[2];
500 uint32_t IntBitWidth = IntVT.getSizeInBits();
501 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
502 APFloat::rmTowardZero) != APFloat::opOK)
503 return 0;
504 APInt IntVal(IntBitWidth, 2, x);
505
506 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
507 ISD::Constant, IntVal.getZExtValue());
508 if (IntegerReg == 0)
509 return 0;
510 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
511 ISD::SINT_TO_FP, IntegerReg);
512 if (MaterialReg == 0)
513 return 0;
514 }
515 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
516}
517
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000518unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
519 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000520}
521
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000522unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000523 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000524 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000525 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000526
Dan Gohmanfd903942008-08-20 23:53:10 +0000527 BuildMI(MBB, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000528 return ResultReg;
529}
530
531unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
532 const TargetRegisterClass *RC,
533 unsigned Op0) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000534 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000535 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000536
Dan Gohmanfd903942008-08-20 23:53:10 +0000537 BuildMI(MBB, II, ResultReg).addReg(Op0);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000538 return ResultReg;
539}
540
541unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
542 const TargetRegisterClass *RC,
543 unsigned Op0, unsigned Op1) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000544 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000545 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000546
Dan Gohmanfd903942008-08-20 23:53:10 +0000547 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000548 return ResultReg;
549}
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000550
551unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
552 const TargetRegisterClass *RC,
553 unsigned Op0, uint64_t Imm) {
554 unsigned ResultReg = createResultReg(RC);
555 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
556
557 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
558 return ResultReg;
559}
560
Dan Gohman10df0fa2008-08-27 01:09:54 +0000561unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
562 const TargetRegisterClass *RC,
563 unsigned Op0, ConstantFP *FPImm) {
564 unsigned ResultReg = createResultReg(RC);
565 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
566
567 BuildMI(MBB, II, ResultReg).addReg(Op0).addFPImm(FPImm);
568 return ResultReg;
569}
570
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000571unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
572 const TargetRegisterClass *RC,
573 unsigned Op0, unsigned Op1, uint64_t Imm) {
574 unsigned ResultReg = createResultReg(RC);
575 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
576
577 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
578 return ResultReg;
579}
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000580
581unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
582 const TargetRegisterClass *RC,
583 uint64_t Imm) {
584 unsigned ResultReg = createResultReg(RC);
585 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
586
587 BuildMI(MBB, II, ResultReg).addImm(Imm);
588 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +0000589}