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, DenseMap<const Value*, unsigned> &ValueMap) {
25 unsigned &Reg = ValueMap[V];
29 MVT::SimpleValueType VT = TLI.getValueType(V->getType()).getSimpleVT();
30 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
31 if (CI->getValue().getActiveBits() > 64)
33 Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
34 } else if (isa<ConstantPointerNull>(V)) {
35 Reg = FastEmit_i(VT, VT, ISD::Constant, 0);
36 } else if (ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
37 Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
40 const APFloat &Flt = CF->getValueAPF();
41 MVT IntVT = TLI.getPointerTy();
44 uint32_t IntBitWidth = IntVT.getSizeInBits();
45 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
46 APFloat::rmTowardZero) != APFloat::opOK)
48 APInt IntVal(IntBitWidth, 2, x);
50 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
51 ISD::Constant, IntVal.getZExtValue());
54 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
58 } else if (isa<UndefValue>(V)) {
59 Reg = createResultReg(TLI.getRegClassFor(VT));
60 BuildMI(MBB, TII.get(TargetInstrInfo::IMPLICIT_DEF), Reg);
66 /// UpdateValueMap - Update the value map to include the new mapping for this
67 /// instruction, or insert an extra copy to get the result in a previous
68 /// determined register.
69 /// NOTE: This is only necessary because we might select a block that uses
70 /// a value before we select the block that defines the value. It might be
71 /// possible to fix this by selecting blocks in reverse postorder.
72 void FastISel::UpdateValueMap(Instruction* I, unsigned Reg,
73 DenseMap<const Value*, unsigned> &ValueMap) {
74 if (!ValueMap.count(I))
77 TII.copyRegToReg(*MBB, MBB->end(), ValueMap[I],
78 Reg, MRI.getRegClass(Reg), MRI.getRegClass(Reg));
81 /// SelectBinaryOp - Select and emit code for a binary operator instruction,
82 /// which has an opcode which directly corresponds to the given ISD opcode.
84 bool FastISel::SelectBinaryOp(Instruction *I, ISD::NodeType ISDOpcode,
85 DenseMap<const Value*, unsigned> &ValueMap) {
86 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
87 if (VT == MVT::Other || !VT.isSimple())
88 // Unhandled type. Halt "fast" selection and bail.
90 // We only handle legal types. For example, on x86-32 the instruction
91 // selector contains all of the 64-bit instructions from x86-64,
92 // under the assumption that i64 won't be used if the target doesn't
94 if (!TLI.isTypeLegal(VT))
97 unsigned Op0 = getRegForValue(I->getOperand(0), ValueMap);
99 // Unhandled operand. Halt "fast" selection and bail.
102 // Check if the second operand is a constant and handle it appropriately.
103 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
104 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
105 ISDOpcode, Op0, CI->getZExtValue());
106 if (ResultReg != 0) {
107 // We successfully emitted code for the given LLVM Instruction.
108 UpdateValueMap(I, ResultReg, ValueMap);
113 // Check if the second operand is a constant float.
114 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
115 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
117 if (ResultReg != 0) {
118 // We successfully emitted code for the given LLVM Instruction.
119 UpdateValueMap(I, ResultReg, ValueMap);
124 unsigned Op1 = getRegForValue(I->getOperand(1), ValueMap);
126 // Unhandled operand. Halt "fast" selection and bail.
129 // Now we have both operands in registers. Emit the instruction.
130 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
131 ISDOpcode, Op0, Op1);
133 // Target-specific code wasn't able to find a machine opcode for
134 // the given ISD opcode and type. Halt "fast" selection and bail.
137 // We successfully emitted code for the given LLVM Instruction.
138 UpdateValueMap(I, ResultReg, ValueMap);
142 bool FastISel::SelectGetElementPtr(Instruction *I,
143 DenseMap<const Value*, unsigned> &ValueMap) {
144 unsigned N = getRegForValue(I->getOperand(0), ValueMap);
146 // Unhandled operand. Halt "fast" selection and bail.
149 const Type *Ty = I->getOperand(0)->getType();
150 MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
151 for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
154 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
155 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
158 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
159 // FIXME: This can be optimized by combining the add with a
161 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
163 // Unhandled operand. Halt "fast" selection and bail.
166 Ty = StTy->getElementType(Field);
168 Ty = cast<SequentialType>(Ty)->getElementType();
170 // If this is a constant subscript, handle it quickly.
171 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
172 if (CI->getZExtValue() == 0) continue;
174 TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
175 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
177 // Unhandled operand. Halt "fast" selection and bail.
182 // N = N + Idx * ElementSize;
183 uint64_t ElementSize = TD.getABITypeSize(Ty);
184 unsigned IdxN = getRegForValue(Idx, ValueMap);
186 // Unhandled operand. Halt "fast" selection and bail.
189 // If the index is smaller or larger than intptr_t, truncate or extend
191 MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
192 if (IdxVT.bitsLT(VT))
193 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::SIGN_EXTEND, IdxN);
194 else if (IdxVT.bitsGT(VT))
195 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::TRUNCATE, IdxN);
197 // Unhandled operand. Halt "fast" selection and bail.
200 if (ElementSize != 1) {
201 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
203 // Unhandled operand. Halt "fast" selection and bail.
206 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
208 // Unhandled operand. Halt "fast" selection and bail.
213 // We successfully emitted code for the given LLVM Instruction.
214 UpdateValueMap(I, N, ValueMap);
218 bool FastISel::SelectCast(Instruction *I, ISD::NodeType Opcode,
219 DenseMap<const Value*, unsigned> &ValueMap) {
220 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
221 MVT DstVT = TLI.getValueType(I->getType());
223 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
224 DstVT == MVT::Other || !DstVT.isSimple() ||
225 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
226 // Unhandled type. Halt "fast" selection and bail.
229 unsigned InputReg = getRegForValue(I->getOperand(0), ValueMap);
231 // Unhandled operand. Halt "fast" selection and bail.
234 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
241 UpdateValueMap(I, ResultReg, ValueMap);
245 bool FastISel::SelectBitCast(Instruction *I,
246 DenseMap<const Value*, unsigned> &ValueMap) {
247 // If the bitcast doesn't change the type, just use the operand value.
248 if (I->getType() == I->getOperand(0)->getType()) {
249 unsigned Reg = getRegForValue(I->getOperand(0), ValueMap);
252 UpdateValueMap(I, Reg, ValueMap);
256 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
257 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
258 MVT DstVT = TLI.getValueType(I->getType());
260 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
261 DstVT == MVT::Other || !DstVT.isSimple() ||
262 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
263 // Unhandled type. Halt "fast" selection and bail.
266 unsigned Op0 = getRegForValue(I->getOperand(0), ValueMap);
268 // Unhandled operand. Halt "fast" selection and bail.
271 // First, try to perform the bitcast by inserting a reg-reg copy.
272 unsigned ResultReg = 0;
273 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
274 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
275 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
276 ResultReg = createResultReg(DstClass);
278 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
279 Op0, DstClass, SrcClass);
284 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
286 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
287 ISD::BIT_CONVERT, Op0);
292 UpdateValueMap(I, ResultReg, ValueMap);
297 FastISel::SelectInstructions(BasicBlock::iterator Begin,
298 BasicBlock::iterator End,
299 DenseMap<const Value*, unsigned> &ValueMap,
300 DenseMap<const BasicBlock*,
301 MachineBasicBlock *> &MBBMap,
302 MachineBasicBlock *mbb) {
304 BasicBlock::iterator I = Begin;
306 for (; I != End; ++I) {
307 switch (I->getOpcode()) {
308 case Instruction::Add: {
309 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
310 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
312 case Instruction::Sub: {
313 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
314 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
316 case Instruction::Mul: {
317 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
318 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
320 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;
345 case Instruction::GetElementPtr:
346 if (!SelectGetElementPtr(I, ValueMap)) return I;
349 case Instruction::Br: {
350 BranchInst *BI = cast<BranchInst>(I);
352 if (BI->isUnconditional()) {
353 MachineFunction::iterator NextMBB =
354 next(MachineFunction::iterator(MBB));
355 BasicBlock *LLVMSucc = BI->getSuccessor(0);
356 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
358 if (NextMBB != MF.end() && MSucc == NextMBB) {
359 // The unconditional fall-through case, which needs no instructions.
361 // The unconditional branch case.
362 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
364 MBB->addSuccessor(MSucc);
368 // Conditional branches are not handed yet.
369 // Halt "fast" selection and bail.
373 case Instruction::PHI:
374 // PHI nodes are already emitted.
377 case Instruction::BitCast:
378 if (!SelectBitCast(I, ValueMap)) return I; break;
380 case Instruction::FPToSI:
381 if (!SelectCast(I, ISD::FP_TO_SINT, ValueMap)) return I;
383 case Instruction::ZExt:
384 if (!SelectCast(I, ISD::ZERO_EXTEND, ValueMap)) return I;
386 case Instruction::SExt:
387 if (!SelectCast(I, ISD::SIGN_EXTEND, ValueMap)) return I;
389 case Instruction::Trunc:
390 if (!SelectCast(I, ISD::TRUNCATE, ValueMap)) return I;
392 case Instruction::SIToFP:
393 if (!SelectCast(I, ISD::SINT_TO_FP, ValueMap)) return I;
396 case Instruction::IntToPtr: // Deliberate fall-through.
397 case Instruction::PtrToInt: {
398 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
399 MVT DstVT = TLI.getValueType(I->getType());
400 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
401 if (ValueMap[I->getOperand(0)]) {
402 UpdateValueMap(I, ValueMap[I->getOperand(0)], ValueMap);
407 } else if (DstVT.bitsGT(SrcVT)) {
408 if (!SelectCast(I, ISD::ZERO_EXTEND, ValueMap)) return I;
411 // TODO: Handle SrcVT > DstVT, where truncation is needed.
417 // Unhandled instruction. Halt "fast" selection and bail.
425 FastISel::FastISel(MachineFunction &mf)
427 MRI(mf.getRegInfo()),
429 TD(*TM.getTargetData()),
430 TII(*TM.getInstrInfo()),
431 TLI(*TM.getTargetLowering()) {
434 FastISel::~FastISel() {}
436 unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType, ISD::NodeType) {
440 unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
441 ISD::NodeType, unsigned /*Op0*/) {
445 unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType,
446 ISD::NodeType, unsigned /*Op0*/,
451 unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
452 ISD::NodeType, uint64_t /*Imm*/) {
456 unsigned FastISel::FastEmit_f(MVT::SimpleValueType, MVT::SimpleValueType,
457 ISD::NodeType, ConstantFP * /*FPImm*/) {
461 unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
462 ISD::NodeType, unsigned /*Op0*/,
467 unsigned FastISel::FastEmit_rf(MVT::SimpleValueType, MVT::SimpleValueType,
468 ISD::NodeType, unsigned /*Op0*/,
469 ConstantFP * /*FPImm*/) {
473 unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
475 unsigned /*Op0*/, unsigned /*Op1*/,
480 /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
481 /// to emit an instruction with an immediate operand using FastEmit_ri.
482 /// If that fails, it materializes the immediate into a register and try
483 /// FastEmit_rr instead.
484 unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
485 unsigned Op0, uint64_t Imm,
486 MVT::SimpleValueType ImmType) {
487 // First check if immediate type is legal. If not, we can't use the ri form.
488 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
491 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
492 if (MaterialReg == 0)
494 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
497 /// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
498 /// to emit an instruction with a floating-point immediate operand using
499 /// FastEmit_rf. If that fails, it materializes the immediate into a register
500 /// and try FastEmit_rr instead.
501 unsigned FastISel::FastEmit_rf_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
502 unsigned Op0, ConstantFP *FPImm,
503 MVT::SimpleValueType ImmType) {
504 // First check if immediate type is legal. If not, we can't use the rf form.
505 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
509 // Materialize the constant in a register.
510 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
511 if (MaterialReg == 0) {
512 // If the target doesn't have a way to directly enter a floating-point
513 // value into a register, use an alternate approach.
514 // TODO: The current approach only supports floating-point constants
515 // that can be constructed by conversion from integer values. This should
516 // be replaced by code that creates a load from a constant-pool entry,
517 // which will require some target-specific work.
518 const APFloat &Flt = FPImm->getValueAPF();
519 MVT IntVT = TLI.getPointerTy();
522 uint32_t IntBitWidth = IntVT.getSizeInBits();
523 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
524 APFloat::rmTowardZero) != APFloat::opOK)
526 APInt IntVal(IntBitWidth, 2, x);
528 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
529 ISD::Constant, IntVal.getZExtValue());
532 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
533 ISD::SINT_TO_FP, IntegerReg);
534 if (MaterialReg == 0)
537 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
540 unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
541 return MRI.createVirtualRegister(RC);
544 unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
545 const TargetRegisterClass* RC) {
546 unsigned ResultReg = createResultReg(RC);
547 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
549 BuildMI(MBB, II, ResultReg);
553 unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
554 const TargetRegisterClass *RC,
556 unsigned ResultReg = createResultReg(RC);
557 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
559 BuildMI(MBB, II, ResultReg).addReg(Op0);
563 unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
564 const TargetRegisterClass *RC,
565 unsigned Op0, unsigned Op1) {
566 unsigned ResultReg = createResultReg(RC);
567 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
569 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1);
573 unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
574 const TargetRegisterClass *RC,
575 unsigned Op0, uint64_t Imm) {
576 unsigned ResultReg = createResultReg(RC);
577 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
579 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
583 unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
584 const TargetRegisterClass *RC,
585 unsigned Op0, ConstantFP *FPImm) {
586 unsigned ResultReg = createResultReg(RC);
587 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
589 BuildMI(MBB, II, ResultReg).addReg(Op0).addFPImm(FPImm);
593 unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
594 const TargetRegisterClass *RC,
595 unsigned Op0, unsigned Op1, uint64_t Imm) {
596 unsigned ResultReg = createResultReg(RC);
597 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
599 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
603 unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
604 const TargetRegisterClass *RC,
606 unsigned ResultReg = createResultReg(RC);
607 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
609 BuildMI(MBB, II, ResultReg).addImm(Imm);
613 unsigned FastISel::FastEmitInst_extractsubreg(unsigned Op0, uint32_t Idx) {
614 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
615 const TargetRegisterClass* SRC = *(RC->subregclasses_begin()+Idx-1);
617 unsigned ResultReg = createResultReg(SRC);
618 const TargetInstrDesc &II = TII.get(TargetInstrInfo::EXTRACT_SUBREG);
620 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Idx);