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 unsigned FastISel::getRegForValue(Value *V,
25 DenseMap<const Value*, unsigned> &ValueMap) {
26 unsigned &Reg = ValueMap[V];
30 MVT::SimpleValueType VT = TLI.getValueType(V->getType()).getSimpleVT();
31 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
32 if (CI->getValue().getActiveBits() > 64)
34 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
35 } else if (isa<ConstantPointerNull>(V)) {
36 Reg = FastEmit_i(VT, VT, ISD::Constant, 0);
37 } else if (ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
38 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
41 const APFloat &Flt = CF->getValueAPF();
42 MVT IntVT = TLI.getPointerTy();
45 uint32_t IntBitWidth = IntVT.getSizeInBits();
46 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
47 APFloat::rmTowardZero) != APFloat::opOK)
49 APInt IntVal(IntBitWidth, 2, x);
51 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
52 ISD::Constant, IntVal.getZExtValue());
55 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
59 } else if (isa<UndefValue>(V)) {
60 Reg = createResultReg(TLI.getRegClassFor(VT));
61 BuildMI(MBB, TII.get(TargetInstrInfo::IMPLICIT_DEF), Reg);
67 /// UpdateValueMap - Update the value map to include the new mapping for this
68 /// instruction, or insert an extra copy to get the result in a previous
69 /// determined register.
70 /// NOTE: This is only necessary because we might select a block that uses
71 /// a value before we select the block that defines the value. It might be
72 /// possible to fix this by selecting blocks in reverse postorder.
73 void FastISel::UpdateValueMap(Instruction* I, unsigned Reg,
74 DenseMap<const Value*, unsigned> &ValueMap) {
75 if (!ValueMap.count(I))
78 TII.copyRegToReg(*MBB, MBB->end(), ValueMap[I],
79 Reg, MRI.getRegClass(Reg), MRI.getRegClass(Reg));
82 /// SelectBinaryOp - Select and emit code for a binary operator instruction,
83 /// which has an opcode which directly corresponds to the given ISD opcode.
85 bool FastISel::SelectBinaryOp(Instruction *I, ISD::NodeType ISDOpcode,
86 DenseMap<const Value*, unsigned> &ValueMap) {
87 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
88 if (VT == MVT::Other || !VT.isSimple())
89 // Unhandled type. Halt "fast" selection and bail.
91 // We only handle legal types. For example, on x86-32 the instruction
92 // selector contains all of the 64-bit instructions from x86-64,
93 // under the assumption that i64 won't be used if the target doesn't
95 if (!TLI.isTypeLegal(VT))
98 unsigned Op0 = getRegForValue(I->getOperand(0), ValueMap);
100 // Unhandled operand. Halt "fast" selection and bail.
103 // Check if the second operand is a constant and handle it appropriately.
104 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
105 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
106 ISDOpcode, Op0, CI->getZExtValue());
107 if (ResultReg != 0) {
108 // We successfully emitted code for the given LLVM Instruction.
109 UpdateValueMap(I, ResultReg, ValueMap);
114 // Check if the second operand is a constant float.
115 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
116 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
118 if (ResultReg != 0) {
119 // We successfully emitted code for the given LLVM Instruction.
120 UpdateValueMap(I, ResultReg, ValueMap);
125 unsigned Op1 = getRegForValue(I->getOperand(1), ValueMap);
127 // Unhandled operand. Halt "fast" selection and bail.
130 // Now we have both operands in registers. Emit the instruction.
131 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
132 ISDOpcode, Op0, Op1);
134 // Target-specific code wasn't able to find a machine opcode for
135 // the given ISD opcode and type. Halt "fast" selection and bail.
138 // We successfully emitted code for the given LLVM Instruction.
139 UpdateValueMap(I, ResultReg, ValueMap);
143 bool FastISel::SelectGetElementPtr(Instruction *I,
144 DenseMap<const Value*, unsigned> &ValueMap) {
145 unsigned N = getRegForValue(I->getOperand(0), ValueMap);
147 // Unhandled operand. Halt "fast" selection and bail.
150 const Type *Ty = I->getOperand(0)->getType();
151 MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
152 for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
155 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
156 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
159 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
160 // FIXME: This can be optimized by combining the add with a
162 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
164 // Unhandled operand. Halt "fast" selection and bail.
167 Ty = StTy->getElementType(Field);
169 Ty = cast<SequentialType>(Ty)->getElementType();
171 // If this is a constant subscript, handle it quickly.
172 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
173 if (CI->getZExtValue() == 0) continue;
175 TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
176 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
178 // Unhandled operand. Halt "fast" selection and bail.
183 // N = N + Idx * ElementSize;
184 uint64_t ElementSize = TD.getABITypeSize(Ty);
185 unsigned IdxN = getRegForValue(Idx, ValueMap);
187 // Unhandled operand. Halt "fast" selection and bail.
190 // If the index is smaller or larger than intptr_t, truncate or extend
192 MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
193 if (IdxVT.bitsLT(VT))
194 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::SIGN_EXTEND, IdxN);
195 else if (IdxVT.bitsGT(VT))
196 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::TRUNCATE, IdxN);
198 // Unhandled operand. Halt "fast" selection and bail.
201 if (ElementSize != 1) {
202 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
204 // Unhandled operand. Halt "fast" selection and bail.
207 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
209 // Unhandled operand. Halt "fast" selection and bail.
214 // We successfully emitted code for the given LLVM Instruction.
215 UpdateValueMap(I, N, ValueMap);
219 bool FastISel::SelectCast(Instruction *I, ISD::NodeType Opcode,
220 DenseMap<const Value*, unsigned> &ValueMap) {
221 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
222 MVT DstVT = TLI.getValueType(I->getType());
224 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
225 DstVT == MVT::Other || !DstVT.isSimple() ||
226 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
227 // Unhandled type. Halt "fast" selection and bail.
230 unsigned InputReg = getRegForValue(I->getOperand(0), ValueMap);
232 // Unhandled operand. Halt "fast" selection and bail.
235 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
242 UpdateValueMap(I, ResultReg, ValueMap);
246 bool FastISel::SelectBitCast(Instruction *I,
247 DenseMap<const Value*, unsigned> &ValueMap) {
248 // If the bitcast doesn't change the type, just use the operand value.
249 if (I->getType() == I->getOperand(0)->getType()) {
250 unsigned Reg = getRegForValue(I->getOperand(0), ValueMap);
253 UpdateValueMap(I, Reg, ValueMap);
257 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
258 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
259 MVT DstVT = TLI.getValueType(I->getType());
261 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
262 DstVT == MVT::Other || !DstVT.isSimple() ||
263 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
264 // Unhandled type. Halt "fast" selection and bail.
267 unsigned Op0 = getRegForValue(I->getOperand(0), ValueMap);
269 // Unhandled operand. Halt "fast" selection and bail.
272 // First, try to perform the bitcast by inserting a reg-reg copy.
273 unsigned ResultReg = 0;
274 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
275 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
276 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
277 ResultReg = createResultReg(DstClass);
279 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
280 Op0, DstClass, SrcClass);
285 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
287 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
288 ISD::BIT_CONVERT, Op0);
293 UpdateValueMap(I, ResultReg, ValueMap);
298 FastISel::SelectInstructions(BasicBlock::iterator Begin,
299 BasicBlock::iterator End,
300 DenseMap<const Value*, unsigned> &ValueMap,
301 DenseMap<const BasicBlock*,
302 MachineBasicBlock *> &MBBMap,
303 MachineBasicBlock *mbb) {
305 BasicBlock::iterator I = Begin;
307 for (; I != End; ++I) {
308 switch (I->getOpcode()) {
309 case Instruction::Add: {
310 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
311 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
313 case Instruction::Sub: {
314 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
315 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
317 case Instruction::Mul: {
318 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
319 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
321 case Instruction::SDiv:
322 if (!SelectBinaryOp(I, ISD::SDIV, ValueMap)) return I; break;
323 case Instruction::UDiv:
324 if (!SelectBinaryOp(I, ISD::UDIV, ValueMap)) return I; break;
325 case Instruction::FDiv:
326 if (!SelectBinaryOp(I, ISD::FDIV, ValueMap)) return I; break;
327 case Instruction::SRem:
328 if (!SelectBinaryOp(I, ISD::SREM, ValueMap)) return I; break;
329 case Instruction::URem:
330 if (!SelectBinaryOp(I, ISD::UREM, ValueMap)) return I; break;
331 case Instruction::FRem:
332 if (!SelectBinaryOp(I, ISD::FREM, ValueMap)) return I; break;
333 case Instruction::Shl:
334 if (!SelectBinaryOp(I, ISD::SHL, ValueMap)) return I; break;
335 case Instruction::LShr:
336 if (!SelectBinaryOp(I, ISD::SRL, ValueMap)) return I; break;
337 case Instruction::AShr:
338 if (!SelectBinaryOp(I, ISD::SRA, ValueMap)) return I; break;
339 case Instruction::And:
340 if (!SelectBinaryOp(I, ISD::AND, ValueMap)) return I; break;
341 case Instruction::Or:
342 if (!SelectBinaryOp(I, ISD::OR, ValueMap)) return I; break;
343 case Instruction::Xor:
344 if (!SelectBinaryOp(I, ISD::XOR, ValueMap)) return I; break;
346 case Instruction::GetElementPtr:
347 if (!SelectGetElementPtr(I, ValueMap)) return I;
350 case Instruction::Br: {
351 BranchInst *BI = cast<BranchInst>(I);
353 if (BI->isUnconditional()) {
354 MachineFunction::iterator NextMBB =
355 next(MachineFunction::iterator(MBB));
356 BasicBlock *LLVMSucc = BI->getSuccessor(0);
357 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
359 if (NextMBB != MF.end() && MSucc == NextMBB) {
360 // The unconditional fall-through case, which needs no instructions.
362 // The unconditional branch case.
363 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
365 MBB->addSuccessor(MSucc);
369 // Conditional branches are not handed yet.
370 // Halt "fast" selection and bail.
374 case Instruction::PHI:
375 // PHI nodes are already emitted.
378 case Instruction::BitCast:
379 if (!SelectBitCast(I, ValueMap)) return I; break;
381 case Instruction::FPToSI:
382 if (!SelectCast(I, ISD::FP_TO_SINT, ValueMap)) return I;
384 case Instruction::ZExt:
385 if (!SelectCast(I, ISD::ZERO_EXTEND, ValueMap)) return I;
387 case Instruction::SExt:
388 if (!SelectCast(I, ISD::SIGN_EXTEND, ValueMap)) return I;
390 case Instruction::Trunc:
391 if (!SelectCast(I, ISD::TRUNCATE, ValueMap)) return I;
393 case Instruction::SIToFP:
394 if (!SelectCast(I, ISD::SINT_TO_FP, ValueMap)) return I;
397 case Instruction::IntToPtr: // Deliberate fall-through.
398 case Instruction::PtrToInt: {
399 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
400 MVT DstVT = TLI.getValueType(I->getType());
401 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
402 if (ValueMap[I->getOperand(0)]) {
403 UpdateValueMap(I, ValueMap[I->getOperand(0)], ValueMap);
408 } else if (DstVT.bitsGT(SrcVT)) {
409 if (!SelectCast(I, ISD::ZERO_EXTEND, ValueMap)) return I;
412 // TODO: Handle SrcVT > DstVT, where truncation is needed.
418 // Unhandled instruction. Halt "fast" selection and bail.
426 FastISel::FastISel(MachineFunction &mf)
428 MRI(mf.getRegInfo()),
430 TD(*TM.getTargetData()),
431 TII(*TM.getInstrInfo()),
432 TLI(*TM.getTargetLowering()) {
435 FastISel::~FastISel() {}
437 unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType,
442 unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
443 ISD::NodeType, unsigned /*Op0*/) {
447 unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType,
448 ISD::NodeType, unsigned /*Op0*/,
453 unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
454 ISD::NodeType, uint64_t /*Imm*/) {
458 unsigned FastISel::FastEmit_f(MVT::SimpleValueType, MVT::SimpleValueType,
459 ISD::NodeType, ConstantFP * /*FPImm*/) {
463 unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
464 ISD::NodeType, unsigned /*Op0*/,
469 unsigned FastISel::FastEmit_rf(MVT::SimpleValueType, MVT::SimpleValueType,
470 ISD::NodeType, unsigned /*Op0*/,
471 ConstantFP * /*FPImm*/) {
475 unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
477 unsigned /*Op0*/, unsigned /*Op1*/,
482 /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
483 /// to emit an instruction with an immediate operand using FastEmit_ri.
484 /// If that fails, it materializes the immediate into a register and try
485 /// FastEmit_rr instead.
486 unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
487 unsigned Op0, uint64_t Imm,
488 MVT::SimpleValueType ImmType) {
489 // First check if immediate type is legal. If not, we can't use the ri form.
490 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
493 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
494 if (MaterialReg == 0)
496 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
499 /// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
500 /// to emit an instruction with a floating-point immediate operand using
501 /// FastEmit_rf. If that fails, it materializes the immediate into a register
502 /// and try FastEmit_rr instead.
503 unsigned FastISel::FastEmit_rf_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
504 unsigned Op0, ConstantFP *FPImm,
505 MVT::SimpleValueType ImmType) {
506 // First check if immediate type is legal. If not, we can't use the rf form.
507 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
511 // Materialize the constant in a register.
512 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
513 if (MaterialReg == 0) {
514 // If the target doesn't have a way to directly enter a floating-point
515 // value into a register, use an alternate approach.
516 // TODO: The current approach only supports floating-point constants
517 // that can be constructed by conversion from integer values. This should
518 // be replaced by code that creates a load from a constant-pool entry,
519 // which will require some target-specific work.
520 const APFloat &Flt = FPImm->getValueAPF();
521 MVT IntVT = TLI.getPointerTy();
524 uint32_t IntBitWidth = IntVT.getSizeInBits();
525 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
526 APFloat::rmTowardZero) != APFloat::opOK)
528 APInt IntVal(IntBitWidth, 2, x);
530 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
531 ISD::Constant, IntVal.getZExtValue());
534 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
535 ISD::SINT_TO_FP, IntegerReg);
536 if (MaterialReg == 0)
539 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
542 unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
543 return MRI.createVirtualRegister(RC);
546 unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
547 const TargetRegisterClass* RC) {
548 unsigned ResultReg = createResultReg(RC);
549 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
551 BuildMI(MBB, II, ResultReg);
555 unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
556 const TargetRegisterClass *RC,
558 unsigned ResultReg = createResultReg(RC);
559 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
561 BuildMI(MBB, II, ResultReg).addReg(Op0);
565 unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
566 const TargetRegisterClass *RC,
567 unsigned Op0, unsigned Op1) {
568 unsigned ResultReg = createResultReg(RC);
569 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
571 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1);
575 unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
576 const TargetRegisterClass *RC,
577 unsigned Op0, uint64_t Imm) {
578 unsigned ResultReg = createResultReg(RC);
579 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
581 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
585 unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
586 const TargetRegisterClass *RC,
587 unsigned Op0, ConstantFP *FPImm) {
588 unsigned ResultReg = createResultReg(RC);
589 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
591 BuildMI(MBB, II, ResultReg).addReg(Op0).addFPImm(FPImm);
595 unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
596 const TargetRegisterClass *RC,
597 unsigned Op0, unsigned Op1, uint64_t Imm) {
598 unsigned ResultReg = createResultReg(RC);
599 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
601 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
605 unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
606 const TargetRegisterClass *RC,
608 unsigned ResultReg = createResultReg(RC);
609 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
611 BuildMI(MBB, II, ResultReg).addImm(Imm);
615 unsigned FastISel::FastEmitInst_extractsubreg(unsigned Op0, uint32_t Idx) {
616 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
617 const TargetRegisterClass* SRC = *(RC->subregclasses_begin()+Idx-1);
619 unsigned ResultReg = createResultReg(SRC);
620 const TargetInstrDesc &II = TII.get(TargetInstrInfo::EXTRACT_SUBREG);
622 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Idx);