1 ///===-- FastISel.cpp - Implementation of the FastISel class --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the implementation of the FastISel class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Instructions.h"
15 #include "llvm/CodeGen/FastISel.h"
16 #include "llvm/CodeGen/MachineInstrBuilder.h"
17 #include "llvm/CodeGen/MachineRegisterInfo.h"
18 #include "llvm/Target/TargetData.h"
19 #include "llvm/Target/TargetInstrInfo.h"
20 #include "llvm/Target/TargetLowering.h"
21 #include "llvm/Target/TargetMachine.h"
24 // Don't cache constant materializations. To do so would require
25 // tracking what uses they dominate. Non-constants, however, already
26 // have the SSA def-doms-use requirement enforced, so we can cache their
28 unsigned FastISel::getRegForValue(Value *V,
29 DenseMap<const Value*, unsigned> &ValueMap) {
30 if (ValueMap.count(V))
33 MVT::SimpleValueType VT = TLI.getValueType(V->getType()).getSimpleVT();
34 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
35 if (CI->getValue().getActiveBits() > 64)
37 // Don't cache constant materializations. To do so would require
38 // tracking what uses they dominate.
39 return FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
40 } else if (isa<ConstantPointerNull>(V)) {
41 return FastEmit_i(VT, VT, ISD::Constant, 0);
42 } else if (ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
43 unsigned Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
46 const APFloat &Flt = CF->getValueAPF();
47 MVT IntVT = TLI.getPointerTy();
50 uint32_t IntBitWidth = IntVT.getSizeInBits();
51 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
52 APFloat::rmTowardZero) != APFloat::opOK)
54 APInt IntVal(IntBitWidth, 2, x);
56 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
57 ISD::Constant, IntVal.getZExtValue());
60 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
66 } else if (isa<UndefValue>(V)) {
67 unsigned Reg = createResultReg(TLI.getRegClassFor(VT));
68 BuildMI(MBB, TII.get(TargetInstrInfo::IMPLICIT_DEF), Reg);
75 /// UpdateValueMap - Update the value map to include the new mapping for this
76 /// instruction, or insert an extra copy to get the result in a previous
77 /// determined register.
78 /// NOTE: This is only necessary because we might select a block that uses
79 /// a value before we select the block that defines the value. It might be
80 /// possible to fix this by selecting blocks in reverse postorder.
81 void FastISel::UpdateValueMap(Instruction* I, unsigned Reg,
82 DenseMap<const Value*, unsigned> &ValueMap) {
83 if (!ValueMap.count(I))
86 TII.copyRegToReg(*MBB, MBB->end(), ValueMap[I],
87 Reg, MRI.getRegClass(Reg), MRI.getRegClass(Reg));
90 /// SelectBinaryOp - Select and emit code for a binary operator instruction,
91 /// which has an opcode which directly corresponds to the given ISD opcode.
93 bool FastISel::SelectBinaryOp(Instruction *I, ISD::NodeType ISDOpcode,
94 DenseMap<const Value*, unsigned> &ValueMap) {
95 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
96 if (VT == MVT::Other || !VT.isSimple())
97 // Unhandled type. Halt "fast" selection and bail.
99 // We only handle legal types. For example, on x86-32 the instruction
100 // selector contains all of the 64-bit instructions from x86-64,
101 // under the assumption that i64 won't be used if the target doesn't
103 if (!TLI.isTypeLegal(VT))
106 unsigned Op0 = getRegForValue(I->getOperand(0), ValueMap);
108 // Unhandled operand. Halt "fast" selection and bail.
111 // Check if the second operand is a constant and handle it appropriately.
112 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
113 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
114 ISDOpcode, Op0, CI->getZExtValue());
115 if (ResultReg != 0) {
116 // We successfully emitted code for the given LLVM Instruction.
117 UpdateValueMap(I, ResultReg, ValueMap);
122 // Check if the second operand is a constant float.
123 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
124 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
126 if (ResultReg != 0) {
127 // We successfully emitted code for the given LLVM Instruction.
128 UpdateValueMap(I, ResultReg, ValueMap);
133 unsigned Op1 = getRegForValue(I->getOperand(1), ValueMap);
135 // Unhandled operand. Halt "fast" selection and bail.
138 // Now we have both operands in registers. Emit the instruction.
139 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
140 ISDOpcode, Op0, Op1);
142 // Target-specific code wasn't able to find a machine opcode for
143 // the given ISD opcode and type. Halt "fast" selection and bail.
146 // We successfully emitted code for the given LLVM Instruction.
147 UpdateValueMap(I, ResultReg, ValueMap);
151 bool FastISel::SelectGetElementPtr(Instruction *I,
152 DenseMap<const Value*, unsigned> &ValueMap) {
153 unsigned N = getRegForValue(I->getOperand(0), ValueMap);
155 // Unhandled operand. Halt "fast" selection and bail.
158 const Type *Ty = I->getOperand(0)->getType();
159 MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
160 for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
163 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
164 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
167 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
168 // FIXME: This can be optimized by combining the add with a
170 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
172 // Unhandled operand. Halt "fast" selection and bail.
175 Ty = StTy->getElementType(Field);
177 Ty = cast<SequentialType>(Ty)->getElementType();
179 // If this is a constant subscript, handle it quickly.
180 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
181 if (CI->getZExtValue() == 0) continue;
183 TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
184 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
186 // Unhandled operand. Halt "fast" selection and bail.
191 // N = N + Idx * ElementSize;
192 uint64_t ElementSize = TD.getABITypeSize(Ty);
193 unsigned IdxN = getRegForValue(Idx, ValueMap);
195 // Unhandled operand. Halt "fast" selection and bail.
198 // If the index is smaller or larger than intptr_t, truncate or extend
200 MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
201 if (IdxVT.bitsLT(VT))
202 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::SIGN_EXTEND, IdxN);
203 else if (IdxVT.bitsGT(VT))
204 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::TRUNCATE, IdxN);
206 // Unhandled operand. Halt "fast" selection and bail.
209 if (ElementSize != 1) {
210 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
212 // Unhandled operand. Halt "fast" selection and bail.
215 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
217 // Unhandled operand. Halt "fast" selection and bail.
222 // We successfully emitted code for the given LLVM Instruction.
223 UpdateValueMap(I, N, ValueMap);
227 bool FastISel::SelectCast(Instruction *I, ISD::NodeType Opcode,
228 DenseMap<const Value*, unsigned> &ValueMap) {
229 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
230 MVT DstVT = TLI.getValueType(I->getType());
232 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
233 DstVT == MVT::Other || !DstVT.isSimple() ||
234 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
235 // Unhandled type. Halt "fast" selection and bail.
238 unsigned InputReg = getRegForValue(I->getOperand(0), ValueMap);
240 // Unhandled operand. Halt "fast" selection and bail.
243 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
250 UpdateValueMap(I, ResultReg, ValueMap);
254 bool FastISel::SelectBitCast(Instruction *I,
255 DenseMap<const Value*, unsigned> &ValueMap) {
256 // If the bitcast doesn't change the type, just use the operand value.
257 if (I->getType() == I->getOperand(0)->getType()) {
258 unsigned Reg = getRegForValue(I->getOperand(0), ValueMap);
261 UpdateValueMap(I, Reg, ValueMap);
265 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
266 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
267 MVT DstVT = TLI.getValueType(I->getType());
269 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
270 DstVT == MVT::Other || !DstVT.isSimple() ||
271 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
272 // Unhandled type. Halt "fast" selection and bail.
275 unsigned Op0 = getRegForValue(I->getOperand(0), ValueMap);
277 // Unhandled operand. Halt "fast" selection and bail.
280 // First, try to perform the bitcast by inserting a reg-reg copy.
281 unsigned ResultReg = 0;
282 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
283 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
284 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
285 ResultReg = createResultReg(DstClass);
287 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
288 Op0, DstClass, SrcClass);
293 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
295 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
296 ISD::BIT_CONVERT, Op0);
301 UpdateValueMap(I, ResultReg, ValueMap);
306 FastISel::SelectInstructions(BasicBlock::iterator Begin,
307 BasicBlock::iterator End,
308 DenseMap<const Value*, unsigned> &ValueMap,
309 DenseMap<const BasicBlock*,
310 MachineBasicBlock *> &MBBMap,
311 MachineBasicBlock *mbb) {
313 BasicBlock::iterator I = Begin;
315 for (; I != End; ++I) {
316 switch (I->getOpcode()) {
317 case Instruction::Add: {
318 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
319 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
321 case Instruction::Sub: {
322 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
323 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
325 case Instruction::Mul: {
326 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
327 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
329 case Instruction::SDiv:
330 if (!SelectBinaryOp(I, ISD::SDIV, ValueMap)) return I; break;
331 case Instruction::UDiv:
332 if (!SelectBinaryOp(I, ISD::UDIV, ValueMap)) return I; break;
333 case Instruction::FDiv:
334 if (!SelectBinaryOp(I, ISD::FDIV, ValueMap)) return I; break;
335 case Instruction::SRem:
336 if (!SelectBinaryOp(I, ISD::SREM, ValueMap)) return I; break;
337 case Instruction::URem:
338 if (!SelectBinaryOp(I, ISD::UREM, ValueMap)) return I; break;
339 case Instruction::FRem:
340 if (!SelectBinaryOp(I, ISD::FREM, ValueMap)) return I; break;
341 case Instruction::Shl:
342 if (!SelectBinaryOp(I, ISD::SHL, ValueMap)) return I; break;
343 case Instruction::LShr:
344 if (!SelectBinaryOp(I, ISD::SRL, ValueMap)) return I; break;
345 case Instruction::AShr:
346 if (!SelectBinaryOp(I, ISD::SRA, ValueMap)) return I; break;
347 case Instruction::And:
348 if (!SelectBinaryOp(I, ISD::AND, ValueMap)) return I; break;
349 case Instruction::Or:
350 if (!SelectBinaryOp(I, ISD::OR, ValueMap)) return I; break;
351 case Instruction::Xor:
352 if (!SelectBinaryOp(I, ISD::XOR, ValueMap)) return I; break;
354 case Instruction::GetElementPtr:
355 if (!SelectGetElementPtr(I, ValueMap)) return I;
358 case Instruction::Br: {
359 BranchInst *BI = cast<BranchInst>(I);
361 if (BI->isUnconditional()) {
362 MachineFunction::iterator NextMBB =
363 next(MachineFunction::iterator(MBB));
364 BasicBlock *LLVMSucc = BI->getSuccessor(0);
365 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
367 if (NextMBB != MF.end() && MSucc == NextMBB) {
368 // The unconditional fall-through case, which needs no instructions.
370 // The unconditional branch case.
371 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
373 MBB->addSuccessor(MSucc);
377 // Conditional branches are not handed yet.
378 // Halt "fast" selection and bail.
382 case Instruction::PHI:
383 // PHI nodes are already emitted.
386 case Instruction::BitCast:
387 if (!SelectBitCast(I, ValueMap)) return I; break;
389 case Instruction::FPToSI:
390 if (!SelectCast(I, ISD::FP_TO_SINT, ValueMap)) return I;
392 case Instruction::ZExt:
393 if (!SelectCast(I, ISD::ZERO_EXTEND, ValueMap)) return I;
395 case Instruction::SExt:
396 if (!SelectCast(I, ISD::SIGN_EXTEND, ValueMap)) return I;
398 case Instruction::Trunc:
399 if (!SelectCast(I, ISD::TRUNCATE, ValueMap)) return I;
401 case Instruction::SIToFP:
402 if (!SelectCast(I, ISD::SINT_TO_FP, ValueMap)) return I;
405 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()) {
410 if (ValueMap[I->getOperand(0)]) {
411 UpdateValueMap(I, ValueMap[I->getOperand(0)], ValueMap);
416 } else if (DstVT.bitsGT(SrcVT)) {
417 if (!SelectCast(I, ISD::ZERO_EXTEND, ValueMap)) return I;
420 // TODO: Handle SrcVT > DstVT, where truncation is needed.
426 // Unhandled instruction. Halt "fast" selection and bail.
434 FastISel::FastISel(MachineFunction &mf)
436 MRI(mf.getRegInfo()),
438 TD(*TM.getTargetData()),
439 TII(*TM.getInstrInfo()),
440 TLI(*TM.getTargetLowering()) {
443 FastISel::~FastISel() {}
445 unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType,
450 unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
451 ISD::NodeType, unsigned /*Op0*/) {
455 unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType,
456 ISD::NodeType, unsigned /*Op0*/,
461 unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
462 ISD::NodeType, uint64_t /*Imm*/) {
466 unsigned FastISel::FastEmit_f(MVT::SimpleValueType, MVT::SimpleValueType,
467 ISD::NodeType, ConstantFP * /*FPImm*/) {
471 unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
472 ISD::NodeType, unsigned /*Op0*/,
477 unsigned FastISel::FastEmit_rf(MVT::SimpleValueType, MVT::SimpleValueType,
478 ISD::NodeType, unsigned /*Op0*/,
479 ConstantFP * /*FPImm*/) {
483 unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
485 unsigned /*Op0*/, unsigned /*Op1*/,
490 /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
491 /// to emit an instruction with an immediate operand using FastEmit_ri.
492 /// If that fails, it materializes the immediate into a register and try
493 /// FastEmit_rr instead.
494 unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
495 unsigned Op0, uint64_t Imm,
496 MVT::SimpleValueType ImmType) {
497 // First check if immediate type is legal. If not, we can't use the ri form.
498 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
501 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
502 if (MaterialReg == 0)
504 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
507 /// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
508 /// to emit an instruction with a floating-point immediate operand using
509 /// FastEmit_rf. If that fails, it materializes the immediate into a register
510 /// and try FastEmit_rr instead.
511 unsigned FastISel::FastEmit_rf_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
512 unsigned Op0, ConstantFP *FPImm,
513 MVT::SimpleValueType ImmType) {
514 // First check if immediate type is legal. If not, we can't use the rf form.
515 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
519 // Materialize the constant in a register.
520 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
521 if (MaterialReg == 0) {
522 // If the target doesn't have a way to directly enter a floating-point
523 // value into a register, use an alternate approach.
524 // TODO: The current approach only supports floating-point constants
525 // that can be constructed by conversion from integer values. This should
526 // be replaced by code that creates a load from a constant-pool entry,
527 // which will require some target-specific work.
528 const APFloat &Flt = FPImm->getValueAPF();
529 MVT IntVT = TLI.getPointerTy();
532 uint32_t IntBitWidth = IntVT.getSizeInBits();
533 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
534 APFloat::rmTowardZero) != APFloat::opOK)
536 APInt IntVal(IntBitWidth, 2, x);
538 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
539 ISD::Constant, IntVal.getZExtValue());
542 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
543 ISD::SINT_TO_FP, IntegerReg);
544 if (MaterialReg == 0)
547 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
550 unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
551 return MRI.createVirtualRegister(RC);
554 unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
555 const TargetRegisterClass* RC) {
556 unsigned ResultReg = createResultReg(RC);
557 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
559 BuildMI(MBB, II, ResultReg);
563 unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
564 const TargetRegisterClass *RC,
566 unsigned ResultReg = createResultReg(RC);
567 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
569 BuildMI(MBB, II, ResultReg).addReg(Op0);
573 unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
574 const TargetRegisterClass *RC,
575 unsigned Op0, unsigned Op1) {
576 unsigned ResultReg = createResultReg(RC);
577 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
579 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1);
583 unsigned 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);
589 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
593 unsigned 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);
599 BuildMI(MBB, II, ResultReg).addReg(Op0).addFPImm(FPImm);
603 unsigned 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);
609 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
613 unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
614 const TargetRegisterClass *RC,
616 unsigned ResultReg = createResultReg(RC);
617 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
619 BuildMI(MBB, II, ResultReg).addImm(Imm);
623 unsigned FastISel::FastEmitInst_extractsubreg(unsigned Op0, uint32_t Idx) {
624 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
625 const TargetRegisterClass* SRC = *(RC->subregclasses_begin()+Idx-1);
627 unsigned ResultReg = createResultReg(SRC);
628 const TargetInstrDesc &II = TII.get(TargetInstrInfo::EXTRACT_SUBREG);
630 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Idx);