blob: ac35c43e7878d64428c549a104217eb7da1c5bee [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 Gohmanbdedd442008-08-20 00:11:48 +000024/// SelectBinaryOp - Select and emit code for a binary operator instruction,
25/// which has an opcode which directly corresponds to the given ISD opcode.
26///
27bool FastISel::SelectBinaryOp(Instruction *I, ISD::NodeType ISDOpcode,
28 DenseMap<const Value*, unsigned> &ValueMap) {
Dan Gohmanbdedd442008-08-20 00:11:48 +000029 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
30 if (VT == MVT::Other || !VT.isSimple())
31 // Unhandled type. Halt "fast" selection and bail.
32 return false;
Dan Gohmanb71fea22008-08-26 20:52:40 +000033 // We only handle legal types. For example, on x86-32 the instruction
34 // selector contains all of the 64-bit instructions from x86-64,
35 // under the assumption that i64 won't be used if the target doesn't
36 // support it.
37 if (!TLI.isTypeLegal(VT))
38 return false;
Dan Gohmanbdedd442008-08-20 00:11:48 +000039
Dan Gohmand5fe57d2008-08-21 01:41:07 +000040 unsigned Op0 = ValueMap[I->getOperand(0)];
41 if (Op0 == 0)
42 // Unhandled operand. Halt "fast" selection and bail.
43 return false;
44
45 // Check if the second operand is a constant and handle it appropriately.
46 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
47 unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0,
48 CI->getZExtValue(), VT.getSimpleVT());
49 if (ResultReg == 0)
50 // Target-specific code wasn't able to find a machine opcode for
51 // the given ISD opcode and type. Halt "fast" selection and bail.
52 return false;
53
54 // We successfully emitted code for the given LLVM Instruction.
55 ValueMap[I] = ResultReg;
56 return true;
57 }
58
Dan Gohman10df0fa2008-08-27 01:09:54 +000059 // Check if the second operand is a constant float.
60 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
61 unsigned ResultReg = FastEmit_rf_(VT.getSimpleVT(), ISDOpcode, Op0,
62 CF, VT.getSimpleVT());
63 if (ResultReg == 0)
64 // Target-specific code wasn't able to find a machine opcode for
65 // the given ISD opcode and type. Halt "fast" selection and bail.
66 return false;
67
68 // We successfully emitted code for the given LLVM Instruction.
69 ValueMap[I] = ResultReg;
70 return true;
71 }
72
Dan Gohmand5fe57d2008-08-21 01:41:07 +000073 unsigned Op1 = ValueMap[I->getOperand(1)];
74 if (Op1 == 0)
75 // Unhandled operand. Halt "fast" selection and bail.
76 return false;
77
Owen Anderson0f84e4e2008-08-25 23:58:18 +000078 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
79 ISDOpcode, Op0, Op1);
Dan Gohmanbdedd442008-08-20 00:11:48 +000080 if (ResultReg == 0)
81 // Target-specific code wasn't able to find a machine opcode for
82 // the given ISD opcode and type. Halt "fast" selection and bail.
83 return false;
84
Dan Gohman8014e862008-08-20 00:23:20 +000085 // We successfully emitted code for the given LLVM Instruction.
Dan Gohmanbdedd442008-08-20 00:11:48 +000086 ValueMap[I] = ResultReg;
87 return true;
88}
89
90bool FastISel::SelectGetElementPtr(Instruction *I,
91 DenseMap<const Value*, unsigned> &ValueMap) {
Evan Cheng83785c82008-08-20 22:45:34 +000092 unsigned N = ValueMap[I->getOperand(0)];
93 if (N == 0)
94 // Unhandled operand. Halt "fast" selection and bail.
95 return false;
96
97 const Type *Ty = I->getOperand(0)->getType();
Dan Gohman7a0e6592008-08-21 17:25:26 +000098 MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
Evan Cheng83785c82008-08-20 22:45:34 +000099 for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
100 OI != E; ++OI) {
101 Value *Idx = *OI;
102 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
103 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
104 if (Field) {
105 // N = N + Offset
106 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
107 // FIXME: This can be optimized by combining the add with a
108 // subsequent one.
Dan Gohman7a0e6592008-08-21 17:25:26 +0000109 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000110 if (N == 0)
111 // Unhandled operand. Halt "fast" selection and bail.
112 return false;
113 }
114 Ty = StTy->getElementType(Field);
115 } else {
116 Ty = cast<SequentialType>(Ty)->getElementType();
117
118 // If this is a constant subscript, handle it quickly.
119 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
120 if (CI->getZExtValue() == 0) continue;
121 uint64_t Offs =
122 TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dan Gohman7a0e6592008-08-21 17:25:26 +0000123 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
Evan Cheng83785c82008-08-20 22:45:34 +0000124 if (N == 0)
125 // Unhandled operand. Halt "fast" selection and bail.
126 return false;
127 continue;
128 }
129
130 // N = N + Idx * ElementSize;
131 uint64_t ElementSize = TD.getABITypeSize(Ty);
132 unsigned IdxN = ValueMap[Idx];
133 if (IdxN == 0)
134 // Unhandled operand. Halt "fast" selection and bail.
135 return false;
136
137 // If the index is smaller or larger than intptr_t, truncate or extend
138 // it.
Evan Cheng2076aa82008-08-21 01:19:11 +0000139 MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
Evan Cheng83785c82008-08-20 22:45:34 +0000140 if (IdxVT.bitsLT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000141 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::SIGN_EXTEND, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000142 else if (IdxVT.bitsGT(VT))
Dan Gohman80bc6e22008-08-26 20:57:08 +0000143 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::TRUNCATE, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000144 if (IdxN == 0)
145 // Unhandled operand. Halt "fast" selection and bail.
146 return false;
147
Dan Gohman80bc6e22008-08-26 20:57:08 +0000148 if (ElementSize != 1) {
Dan Gohmanf93cf792008-08-21 17:37:05 +0000149 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
Dan Gohman80bc6e22008-08-26 20:57:08 +0000150 if (IdxN == 0)
151 // Unhandled operand. Halt "fast" selection and bail.
152 return false;
153 }
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000154 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
Evan Cheng83785c82008-08-20 22:45:34 +0000155 if (N == 0)
156 // Unhandled operand. Halt "fast" selection and bail.
157 return false;
158 }
159 }
160
161 // We successfully emitted code for the given LLVM Instruction.
162 ValueMap[I] = N;
163 return true;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000164}
165
Owen Andersond0533c92008-08-26 23:46:32 +0000166bool FastISel::SelectCast(Instruction *I, ISD::NodeType Opcode,
167 DenseMap<const Value*, unsigned> &ValueMap) {
168 MVT SrcVT = MVT::getMVT(I->getOperand(0)->getType());
169 MVT DstVT = MVT::getMVT(I->getType());
170
171 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
172 DstVT == MVT::Other || !DstVT.isSimple() ||
173 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
174 // Unhandled type. Halt "fast" selection and bail.
175 return false;
176
177 unsigned InputReg = ValueMap[I->getOperand(0)];
178 if (!InputReg)
179 // Unhandled operand. Halt "fast" selection and bail.
180 return false;
181
182 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
183 DstVT.getSimpleVT(),
184 Opcode,
185 InputReg);
186 if (!ResultReg)
187 return false;
188
189 ValueMap[I] = ResultReg;
190 return true;
191}
192
193bool FastISel::SelectConstantCast(Instruction* I, ISD::NodeType Opcode,
194 DenseMap<const Value*, unsigned> &ValueMap) {
195 // Materialize constant and convert.
196 ConstantInt* CI = cast<ConstantInt>(I->getOperand(0));
197 MVT SrcVT = MVT::getMVT(CI->getType());
198 MVT DstVT = MVT::getMVT(I->getType());
199
200 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
201 DstVT == MVT::Other || !DstVT.isSimple() ||
202 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
203 // Unhandled type. Halt "fast" selection and bail.
204 return false;
205
206 unsigned ResultReg1 = FastEmit_i(SrcVT.getSimpleVT(),
207 SrcVT.getSimpleVT(),
208 ISD::Constant, CI->getZExtValue());
209 if (!ResultReg1)
210 return false;
211
212 unsigned ResultReg2 = FastEmit_r(SrcVT.getSimpleVT(),
213 DstVT.getSimpleVT(),
214 Opcode,
215 ResultReg1);
216 if (!ResultReg2)
217 return false;
218
219 ValueMap[I] = ResultReg2;
220 return true;
221}
222
223bool FastISel::SelectConstantFPCast(Instruction* I, ISD::NodeType Opcode,
224 DenseMap<const Value*, unsigned> &ValueMap) {
225 // TODO: Implement casting of FP constants by materialization
226 // followed by conversion.
227 return false;
228}
229
Dan Gohman763d8932008-08-26 21:28:54 +0000230bool FastISel::SelectBitCast(Instruction *I,
231 DenseMap<const Value*, unsigned> &ValueMap) {
232 // BitCast consists of either an immediate to register move
233 // or a register to register move.
234 if (ConstantInt* CI = dyn_cast<ConstantInt>(I->getOperand(0))) {
235 if (I->getType()->isInteger()) {
236 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/false);
237 unsigned result = FastEmit_i(VT.getSimpleVT(), VT.getSimpleVT(),
238 ISD::Constant,
239 CI->getZExtValue());
240 if (!result)
241 return false;
242
243 ValueMap[I] = result;
244 return true;
245 }
246
247 // TODO: Support vector and fp constants.
248 return false;
249 }
250
251 if (!isa<Constant>(I->getOperand(0))) {
252 // Bitcasts of non-constant values become reg-reg copies.
253 MVT SrcVT = MVT::getMVT(I->getOperand(0)->getType());
254 MVT DstVT = MVT::getMVT(I->getType());
255
256 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
257 DstVT == MVT::Other || !DstVT.isSimple() ||
258 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
259 // Unhandled type. Halt "fast" selection and bail.
260 return false;
261
262 unsigned Op0 = ValueMap[I->getOperand(0)];
263 if (Op0 == 0)
264 // Unhandled operand. Halt "fast" selection and bail.
265 return false;
266
267 // First, try to perform the bitcast by inserting a reg-reg copy.
268 unsigned ResultReg = 0;
269 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
270 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
271 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
272 ResultReg = createResultReg(DstClass);
273
274 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
275 Op0, DstClass, SrcClass);
276 if (!InsertedCopy)
277 ResultReg = 0;
278 }
279
280 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
281 if (!ResultReg)
282 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
283 ISD::BIT_CONVERT, Op0);
284
285 if (!ResultReg)
286 return false;
287
288 ValueMap[I] = ResultReg;
289 return true;
290 }
291
292 // TODO: Casting a non-integral constant?
293 return false;
294}
295
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000296BasicBlock::iterator
Dan Gohmanb7864a92008-08-20 18:09:02 +0000297FastISel::SelectInstructions(BasicBlock::iterator Begin,
298 BasicBlock::iterator End,
Dan Gohmanbb466332008-08-20 21:05:57 +0000299 DenseMap<const Value*, unsigned> &ValueMap,
Dan Gohman6ecf5092008-08-23 02:44:46 +0000300 DenseMap<const BasicBlock*,
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000301 MachineBasicBlock *> &MBBMap,
Dan Gohmanbb466332008-08-20 21:05:57 +0000302 MachineBasicBlock *mbb) {
303 MBB = mbb;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000304 BasicBlock::iterator I = Begin;
305
306 for (; I != End; ++I) {
307 switch (I->getOpcode()) {
Dan Gohman8014e862008-08-20 00:23:20 +0000308 case Instruction::Add: {
309 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
310 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
311 }
312 case Instruction::Sub: {
313 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
314 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
315 }
316 case Instruction::Mul: {
317 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
318 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
319 }
Dan Gohmanbdedd442008-08-20 00:11:48 +0000320 case Instruction::SDiv:
321 if (!SelectBinaryOp(I, ISD::SDIV, ValueMap)) return I; break;
322 case Instruction::UDiv:
323 if (!SelectBinaryOp(I, ISD::UDIV, ValueMap)) return I; break;
324 case Instruction::FDiv:
325 if (!SelectBinaryOp(I, ISD::FDIV, ValueMap)) return I; break;
326 case Instruction::SRem:
327 if (!SelectBinaryOp(I, ISD::SREM, ValueMap)) return I; break;
328 case Instruction::URem:
329 if (!SelectBinaryOp(I, ISD::UREM, ValueMap)) return I; break;
330 case Instruction::FRem:
331 if (!SelectBinaryOp(I, ISD::FREM, ValueMap)) return I; break;
332 case Instruction::Shl:
333 if (!SelectBinaryOp(I, ISD::SHL, ValueMap)) return I; break;
334 case Instruction::LShr:
335 if (!SelectBinaryOp(I, ISD::SRL, ValueMap)) return I; break;
336 case Instruction::AShr:
337 if (!SelectBinaryOp(I, ISD::SRA, ValueMap)) return I; break;
338 case Instruction::And:
339 if (!SelectBinaryOp(I, ISD::AND, ValueMap)) return I; break;
340 case Instruction::Or:
341 if (!SelectBinaryOp(I, ISD::OR, ValueMap)) return I; break;
342 case Instruction::Xor:
343 if (!SelectBinaryOp(I, ISD::XOR, ValueMap)) return I; break;
344
345 case Instruction::GetElementPtr:
346 if (!SelectGetElementPtr(I, ValueMap)) return I;
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000347 break;
Dan Gohmanbdedd442008-08-20 00:11:48 +0000348
Dan Gohman6f2766d2008-08-19 22:31:46 +0000349 case Instruction::Br: {
350 BranchInst *BI = cast<BranchInst>(I);
351
Dan Gohmane6798b72008-08-20 01:17:01 +0000352 if (BI->isUnconditional()) {
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000353 MachineFunction::iterator NextMBB =
Dan Gohmane6798b72008-08-20 01:17:01 +0000354 next(MachineFunction::iterator(MBB));
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000355 BasicBlock *LLVMSucc = BI->getSuccessor(0);
356 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
357
358 if (NextMBB != MF.end() && MSucc == NextMBB) {
359 // The unconditional fall-through case, which needs no instructions.
360 } else {
361 // The unconditional branch case.
362 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
Dan Gohmane6798b72008-08-20 01:17:01 +0000363 }
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000364 MBB->addSuccessor(MSucc);
365 break;
Dan Gohman6f2766d2008-08-19 22:31:46 +0000366 }
367
Dan Gohman3c8f36f2008-08-22 21:28:19 +0000368 // Conditional branches are not handed yet.
369 // Halt "fast" selection and bail.
Dan Gohman6f2766d2008-08-19 22:31:46 +0000370 return I;
371 }
Dan Gohman3b7753b2008-08-22 17:37:48 +0000372
373 case Instruction::PHI:
374 // PHI nodes are already emitted.
375 break;
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000376
377 case Instruction::BitCast:
Owen Andersond0533c92008-08-26 23:46:32 +0000378 if (!SelectBitCast(I, ValueMap)) return I; break;
Owen Anderson46aa2f52008-08-26 17:44:42 +0000379
380 case Instruction::FPToSI:
381 if (!isa<ConstantFP>(I->getOperand(0))) {
Owen Andersond0533c92008-08-26 23:46:32 +0000382 if (!SelectCast(I, ISD::FP_TO_SINT, ValueMap)) return I;
Owen Anderson46aa2f52008-08-26 17:44:42 +0000383 } else
Owen Andersond0533c92008-08-26 23:46:32 +0000384 if (!SelectConstantFPCast(I, ISD::FP_TO_SINT, ValueMap)) return I;
385 break;
Owen Anderson97e25682008-08-26 23:14:49 +0000386 case Instruction::ZExt:
387 if (!isa<ConstantInt>(I->getOperand(0))) {
Owen Andersond0533c92008-08-26 23:46:32 +0000388 if (!SelectCast(I, ISD::ZERO_EXTEND, ValueMap)) return I;
Owen Anderson97e25682008-08-26 23:14:49 +0000389 } else
Owen Andersond0533c92008-08-26 23:46:32 +0000390 if (!SelectConstantCast(I, ISD::ZERO_EXTEND, ValueMap)) return I;
391 break;
392 case Instruction::SExt:
393 if (!isa<ConstantInt>(I->getOperand(0))) {
394 if (!SelectCast(I, ISD::SIGN_EXTEND, ValueMap)) return I;
395 } else
396 if (!SelectConstantCast(I, ISD::SIGN_EXTEND, ValueMap)) return I;
397 break;
Owen Andersona843b8d2008-08-26 20:37:00 +0000398 case Instruction::SIToFP:
399 if (!isa<ConstantInt>(I->getOperand(0))) {
Owen Andersond0533c92008-08-26 23:46:32 +0000400 if (!SelectCast(I, ISD::SINT_TO_FP, ValueMap)) return I;
401 } else
402 if (!SelectConstantCast(I, ISD::SINT_TO_FP, ValueMap)) return I;
403 break;
Dan Gohman763d8932008-08-26 21:28:54 +0000404
Owen Anderson9d5b4162008-08-27 00:31:01 +0000405 case Instruction::IntToPtr: // Deliberate fall-through.
406 case Instruction::PtrToInt: {
407 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
408 MVT DstVT = TLI.getValueType(I->getType());
409 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
Owen Anderson96c5ea82008-08-27 00:35:37 +0000410 if (ValueMap[I->getOperand(0)]) {
411 ValueMap[I] = ValueMap[I->getOperand(0)];
412 break;
413 } else
414 // Unhandled operand
415 return I;
Owen Anderson9d5b4162008-08-27 00:31:01 +0000416 } else if (DstVT.bitsGT(SrcVT)) {
417 if (!isa<ConstantInt>(I->getOperand(0))) {
418 if (!SelectCast(I, ISD::ZERO_EXTEND, ValueMap)) return I;
419 } else
420 if (!SelectConstantCast(I, ISD::ZERO_EXTEND, ValueMap)) return I;
421 break;
422 } else {
423 // TODO: Handle SrcVT > DstVT, where truncation is needed.
424 return I;
425 }
426 }
427
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000428 default:
429 // Unhandled instruction. Halt "fast" selection and bail.
430 return I;
431 }
432 }
433
434 return I;
435}
436
Dan Gohmanbb466332008-08-20 21:05:57 +0000437FastISel::FastISel(MachineFunction &mf)
Dan Gohman22bb3112008-08-22 00:20:26 +0000438 : MF(mf),
439 MRI(mf.getRegInfo()),
440 TM(mf.getTarget()),
441 TD(*TM.getTargetData()),
442 TII(*TM.getInstrInfo()),
443 TLI(*TM.getTargetLowering()) {
Dan Gohmanbb466332008-08-20 21:05:57 +0000444}
445
Dan Gohmane285a742008-08-14 21:51:29 +0000446FastISel::~FastISel() {}
447
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000448unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType, ISD::NodeType) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000449 return 0;
450}
451
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000452unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
453 ISD::NodeType, unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000454 return 0;
455}
456
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000457unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType,
458 ISD::NodeType, unsigned /*Op0*/,
459 unsigned /*Op0*/) {
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000460 return 0;
461}
462
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000463unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
464 ISD::NodeType, uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000465 return 0;
466}
467
Dan Gohman10df0fa2008-08-27 01:09:54 +0000468unsigned FastISel::FastEmit_f(MVT::SimpleValueType, MVT::SimpleValueType,
469 ISD::NodeType, ConstantFP * /*FPImm*/) {
470 return 0;
471}
472
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000473unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
474 ISD::NodeType, unsigned /*Op0*/,
475 uint64_t /*Imm*/) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000476 return 0;
477}
478
Dan Gohman10df0fa2008-08-27 01:09:54 +0000479unsigned FastISel::FastEmit_rf(MVT::SimpleValueType, MVT::SimpleValueType,
480 ISD::NodeType, unsigned /*Op0*/,
481 ConstantFP * /*FPImm*/) {
482 return 0;
483}
484
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000485unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
486 ISD::NodeType,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000487 unsigned /*Op0*/, unsigned /*Op1*/,
488 uint64_t /*Imm*/) {
Evan Cheng83785c82008-08-20 22:45:34 +0000489 return 0;
490}
491
492/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
493/// to emit an instruction with an immediate operand using FastEmit_ri.
494/// If that fails, it materializes the immediate into a register and try
495/// FastEmit_rr instead.
496unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000497 unsigned Op0, uint64_t Imm,
498 MVT::SimpleValueType ImmType) {
Evan Cheng83785c82008-08-20 22:45:34 +0000499 unsigned ResultReg = 0;
500 // First check if immediate type is legal. If not, we can't use the ri form.
501 if (TLI.getOperationAction(ISD::Constant, ImmType) == TargetLowering::Legal)
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000502 ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
Evan Cheng83785c82008-08-20 22:45:34 +0000503 if (ResultReg != 0)
504 return ResultReg;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000505 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000506 if (MaterialReg == 0)
507 return 0;
Owen Anderson0f84e4e2008-08-25 23:58:18 +0000508 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000509}
510
Dan Gohman10df0fa2008-08-27 01:09:54 +0000511/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
512/// to emit an instruction with a floating-point immediate operand using
513/// FastEmit_rf. If that fails, it materializes the immediate into a register
514/// and try FastEmit_rr instead.
515unsigned FastISel::FastEmit_rf_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
516 unsigned Op0, ConstantFP *FPImm,
517 MVT::SimpleValueType ImmType) {
518 unsigned ResultReg = 0;
519 // First check if immediate type is legal. If not, we can't use the rf form.
520 if (TLI.getOperationAction(ISD::Constant, ImmType) == TargetLowering::Legal)
521 ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
522 if (ResultReg != 0)
523 return ResultReg;
524
525 // Materialize the constant in a register.
526 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
527 if (MaterialReg == 0) {
528 const APFloat &Flt = FPImm->getValueAPF();
529 MVT IntVT = TLI.getPointerTy();
530
531 uint64_t x[2];
532 uint32_t IntBitWidth = IntVT.getSizeInBits();
533 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
534 APFloat::rmTowardZero) != APFloat::opOK)
535 return 0;
536 APInt IntVal(IntBitWidth, 2, x);
537
538 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
539 ISD::Constant, IntVal.getZExtValue());
540 if (IntegerReg == 0)
541 return 0;
542 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
543 ISD::SINT_TO_FP, IntegerReg);
544 if (MaterialReg == 0)
545 return 0;
546 }
547 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
548}
549
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000550unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
551 return MRI.createVirtualRegister(RC);
Evan Cheng83785c82008-08-20 22:45:34 +0000552}
553
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000554unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
Dan Gohman77ad7962008-08-20 18:09:38 +0000555 const TargetRegisterClass* RC) {
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000556 unsigned ResultReg = createResultReg(RC);
Dan Gohmanbb466332008-08-20 21:05:57 +0000557 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000558
Dan Gohmanfd903942008-08-20 23:53:10 +0000559 BuildMI(MBB, II, ResultReg);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000560 return ResultReg;
561}
562
563unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
564 const TargetRegisterClass *RC,
565 unsigned Op0) {
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).addReg(Op0);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000570 return ResultReg;
571}
572
573unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
574 const TargetRegisterClass *RC,
575 unsigned Op0, unsigned Op1) {
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).addReg(Op1);
Dan Gohmanb0cf29c2008-08-13 20:19:35 +0000580 return ResultReg;
581}
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000582
583unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
584 const TargetRegisterClass *RC,
585 unsigned Op0, uint64_t Imm) {
586 unsigned ResultReg = createResultReg(RC);
587 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
588
589 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
590 return ResultReg;
591}
592
Dan Gohman10df0fa2008-08-27 01:09:54 +0000593unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
594 const TargetRegisterClass *RC,
595 unsigned Op0, ConstantFP *FPImm) {
596 unsigned ResultReg = createResultReg(RC);
597 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
598
599 BuildMI(MBB, II, ResultReg).addReg(Op0).addFPImm(FPImm);
600 return ResultReg;
601}
602
Dan Gohmand5fe57d2008-08-21 01:41:07 +0000603unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
604 const TargetRegisterClass *RC,
605 unsigned Op0, unsigned Op1, uint64_t Imm) {
606 unsigned ResultReg = createResultReg(RC);
607 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
608
609 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
610 return ResultReg;
611}
Owen Anderson6d0c25e2008-08-25 20:20:32 +0000612
613unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
614 const TargetRegisterClass *RC,
615 uint64_t Imm) {
616 unsigned ResultReg = createResultReg(RC);
617 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
618
619 BuildMI(MBB, II, ResultReg).addImm(Imm);
620 return ResultReg;
Evan Chengb41aec52008-08-25 22:20:39 +0000621}