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);
73 /// UpdateValueMap - Update the value map to include the new mapping for this
74 /// instruction, or insert an extra copy to get the result in a previous
75 /// determined register.
76 /// NOTE: This is only necessary because we might select a block that uses
77 /// a value before we select the block that defines the value. It might be
78 /// possible to fix this by selecting blocks in reverse postorder.
79 void FastISel::UpdateValueMap(Instruction* I, unsigned Reg,
80 DenseMap<const Value*, unsigned> &ValueMap) {
81 if (!ValueMap.count(I))
84 TII.copyRegToReg(*MBB, MBB->end(), ValueMap[I],
85 Reg, MRI.getRegClass(Reg), MRI.getRegClass(Reg));
88 /// SelectBinaryOp - Select and emit code for a binary operator instruction,
89 /// which has an opcode which directly corresponds to the given ISD opcode.
91 bool FastISel::SelectBinaryOp(Instruction *I, ISD::NodeType ISDOpcode,
92 DenseMap<const Value*, unsigned> &ValueMap) {
93 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
94 if (VT == MVT::Other || !VT.isSimple())
95 // Unhandled type. Halt "fast" selection and bail.
97 // We only handle legal types. For example, on x86-32 the instruction
98 // selector contains all of the 64-bit instructions from x86-64,
99 // under the assumption that i64 won't be used if the target doesn't
101 if (!TLI.isTypeLegal(VT))
104 unsigned Op0 = getRegForValue(I->getOperand(0), ValueMap);
106 // Unhandled operand. Halt "fast" selection and bail.
109 // Check if the second operand is a constant and handle it appropriately.
110 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
111 unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
112 ISDOpcode, Op0, CI->getZExtValue());
113 if (ResultReg != 0) {
114 // We successfully emitted code for the given LLVM Instruction.
115 UpdateValueMap(I, ResultReg, ValueMap);
120 // Check if the second operand is a constant float.
121 if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
122 unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
124 if (ResultReg != 0) {
125 // We successfully emitted code for the given LLVM Instruction.
126 UpdateValueMap(I, ResultReg, ValueMap);
131 unsigned Op1 = getRegForValue(I->getOperand(1), ValueMap);
133 // Unhandled operand. Halt "fast" selection and bail.
136 // Now we have both operands in registers. Emit the instruction.
137 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
138 ISDOpcode, Op0, Op1);
140 // Target-specific code wasn't able to find a machine opcode for
141 // the given ISD opcode and type. Halt "fast" selection and bail.
144 // We successfully emitted code for the given LLVM Instruction.
145 UpdateValueMap(I, ResultReg, ValueMap);
149 bool FastISel::SelectGetElementPtr(Instruction *I,
150 DenseMap<const Value*, unsigned> &ValueMap) {
151 unsigned N = getRegForValue(I->getOperand(0), ValueMap);
153 // Unhandled operand. Halt "fast" selection and bail.
156 const Type *Ty = I->getOperand(0)->getType();
157 MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
158 for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
161 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
162 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
165 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
166 // FIXME: This can be optimized by combining the add with a
168 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
170 // Unhandled operand. Halt "fast" selection and bail.
173 Ty = StTy->getElementType(Field);
175 Ty = cast<SequentialType>(Ty)->getElementType();
177 // If this is a constant subscript, handle it quickly.
178 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
179 if (CI->getZExtValue() == 0) continue;
181 TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
182 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
184 // Unhandled operand. Halt "fast" selection and bail.
189 // N = N + Idx * ElementSize;
190 uint64_t ElementSize = TD.getABITypeSize(Ty);
191 unsigned IdxN = getRegForValue(Idx, ValueMap);
193 // Unhandled operand. Halt "fast" selection and bail.
196 // If the index is smaller or larger than intptr_t, truncate or extend
198 MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
199 if (IdxVT.bitsLT(VT))
200 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::SIGN_EXTEND, IdxN);
201 else if (IdxVT.bitsGT(VT))
202 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::TRUNCATE, IdxN);
204 // Unhandled operand. Halt "fast" selection and bail.
207 if (ElementSize != 1) {
208 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
210 // Unhandled operand. Halt "fast" selection and bail.
213 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
215 // Unhandled operand. Halt "fast" selection and bail.
220 // We successfully emitted code for the given LLVM Instruction.
221 UpdateValueMap(I, N, ValueMap);
225 bool FastISel::SelectCast(Instruction *I, ISD::NodeType Opcode,
226 DenseMap<const Value*, unsigned> &ValueMap) {
227 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
228 MVT DstVT = TLI.getValueType(I->getType());
230 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
231 DstVT == MVT::Other || !DstVT.isSimple() ||
232 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
233 // Unhandled type. Halt "fast" selection and bail.
236 unsigned InputReg = getRegForValue(I->getOperand(0), ValueMap);
238 // Unhandled operand. Halt "fast" selection and bail.
241 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
248 UpdateValueMap(I, ResultReg, ValueMap);
252 bool FastISel::SelectBitCast(Instruction *I,
253 DenseMap<const Value*, unsigned> &ValueMap) {
254 // If the bitcast doesn't change the type, just use the operand value.
255 if (I->getType() == I->getOperand(0)->getType()) {
256 unsigned Reg = getRegForValue(I->getOperand(0), ValueMap);
259 UpdateValueMap(I, Reg, ValueMap);
263 // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
264 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
265 MVT DstVT = TLI.getValueType(I->getType());
267 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
268 DstVT == MVT::Other || !DstVT.isSimple() ||
269 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
270 // Unhandled type. Halt "fast" selection and bail.
273 unsigned Op0 = getRegForValue(I->getOperand(0), ValueMap);
275 // Unhandled operand. Halt "fast" selection and bail.
278 // First, try to perform the bitcast by inserting a reg-reg copy.
279 unsigned ResultReg = 0;
280 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
281 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
282 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
283 ResultReg = createResultReg(DstClass);
285 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
286 Op0, DstClass, SrcClass);
291 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
293 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
294 ISD::BIT_CONVERT, Op0);
299 UpdateValueMap(I, ResultReg, ValueMap);
304 FastISel::SelectInstructions(BasicBlock::iterator Begin,
305 BasicBlock::iterator End,
306 DenseMap<const Value*, unsigned> &ValueMap,
307 DenseMap<const BasicBlock*,
308 MachineBasicBlock *> &MBBMap,
309 MachineBasicBlock *mbb) {
311 BasicBlock::iterator I = Begin;
313 for (; I != End; ++I) {
314 switch (I->getOpcode()) {
315 case Instruction::Add: {
316 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
317 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
319 case Instruction::Sub: {
320 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
321 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
323 case Instruction::Mul: {
324 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
325 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
327 case Instruction::SDiv:
328 if (!SelectBinaryOp(I, ISD::SDIV, ValueMap)) return I; break;
329 case Instruction::UDiv:
330 if (!SelectBinaryOp(I, ISD::UDIV, ValueMap)) return I; break;
331 case Instruction::FDiv:
332 if (!SelectBinaryOp(I, ISD::FDIV, ValueMap)) return I; break;
333 case Instruction::SRem:
334 if (!SelectBinaryOp(I, ISD::SREM, ValueMap)) return I; break;
335 case Instruction::URem:
336 if (!SelectBinaryOp(I, ISD::UREM, ValueMap)) return I; break;
337 case Instruction::FRem:
338 if (!SelectBinaryOp(I, ISD::FREM, ValueMap)) return I; break;
339 case Instruction::Shl:
340 if (!SelectBinaryOp(I, ISD::SHL, ValueMap)) return I; break;
341 case Instruction::LShr:
342 if (!SelectBinaryOp(I, ISD::SRL, ValueMap)) return I; break;
343 case Instruction::AShr:
344 if (!SelectBinaryOp(I, ISD::SRA, ValueMap)) return I; break;
345 case Instruction::And:
346 if (!SelectBinaryOp(I, ISD::AND, ValueMap)) return I; break;
347 case Instruction::Or:
348 if (!SelectBinaryOp(I, ISD::OR, ValueMap)) return I; break;
349 case Instruction::Xor:
350 if (!SelectBinaryOp(I, ISD::XOR, ValueMap)) return I; break;
352 case Instruction::GetElementPtr:
353 if (!SelectGetElementPtr(I, ValueMap)) return I;
356 case Instruction::Br: {
357 BranchInst *BI = cast<BranchInst>(I);
359 if (BI->isUnconditional()) {
360 MachineFunction::iterator NextMBB =
361 next(MachineFunction::iterator(MBB));
362 BasicBlock *LLVMSucc = BI->getSuccessor(0);
363 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
365 if (NextMBB != MF.end() && MSucc == NextMBB) {
366 // The unconditional fall-through case, which needs no instructions.
368 // The unconditional branch case.
369 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
371 MBB->addSuccessor(MSucc);
375 // Conditional branches are not handed yet.
376 // Halt "fast" selection and bail.
380 case Instruction::PHI:
381 // PHI nodes are already emitted.
384 case Instruction::BitCast:
385 if (!SelectBitCast(I, ValueMap)) return I; break;
387 case Instruction::FPToSI:
388 if (!SelectCast(I, ISD::FP_TO_SINT, ValueMap)) return I;
390 case Instruction::ZExt:
391 if (!SelectCast(I, ISD::ZERO_EXTEND, ValueMap)) return I;
393 case Instruction::SExt:
394 if (!SelectCast(I, ISD::SIGN_EXTEND, ValueMap)) return I;
396 case Instruction::Trunc:
397 if (!SelectCast(I, ISD::TRUNCATE, ValueMap)) return I;
399 case Instruction::SIToFP:
400 if (!SelectCast(I, ISD::SINT_TO_FP, ValueMap)) return I;
403 case Instruction::IntToPtr: // Deliberate fall-through.
404 case Instruction::PtrToInt: {
405 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
406 MVT DstVT = TLI.getValueType(I->getType());
407 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
408 if (ValueMap[I->getOperand(0)]) {
409 UpdateValueMap(I, ValueMap[I->getOperand(0)], ValueMap);
414 } else if (DstVT.bitsGT(SrcVT)) {
415 if (!SelectCast(I, ISD::ZERO_EXTEND, ValueMap)) return I;
418 // TODO: Handle SrcVT > DstVT, where truncation is needed.
424 // Unhandled instruction. Halt "fast" selection and bail.
432 FastISel::FastISel(MachineFunction &mf)
434 MRI(mf.getRegInfo()),
436 TD(*TM.getTargetData()),
437 TII(*TM.getInstrInfo()),
438 TLI(*TM.getTargetLowering()) {
441 FastISel::~FastISel() {}
443 unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType,
448 unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
449 ISD::NodeType, unsigned /*Op0*/) {
453 unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType,
454 ISD::NodeType, unsigned /*Op0*/,
459 unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
460 ISD::NodeType, uint64_t /*Imm*/) {
464 unsigned FastISel::FastEmit_f(MVT::SimpleValueType, MVT::SimpleValueType,
465 ISD::NodeType, ConstantFP * /*FPImm*/) {
469 unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
470 ISD::NodeType, unsigned /*Op0*/,
475 unsigned FastISel::FastEmit_rf(MVT::SimpleValueType, MVT::SimpleValueType,
476 ISD::NodeType, unsigned /*Op0*/,
477 ConstantFP * /*FPImm*/) {
481 unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
483 unsigned /*Op0*/, unsigned /*Op1*/,
488 /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
489 /// to emit an instruction with an immediate operand using FastEmit_ri.
490 /// If that fails, it materializes the immediate into a register and try
491 /// FastEmit_rr instead.
492 unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
493 unsigned Op0, uint64_t Imm,
494 MVT::SimpleValueType ImmType) {
495 // First check if immediate type is legal. If not, we can't use the ri form.
496 unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
499 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
500 if (MaterialReg == 0)
502 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
505 /// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
506 /// to emit an instruction with a floating-point immediate operand using
507 /// FastEmit_rf. If that fails, it materializes the immediate into a register
508 /// and try FastEmit_rr instead.
509 unsigned FastISel::FastEmit_rf_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
510 unsigned Op0, ConstantFP *FPImm,
511 MVT::SimpleValueType ImmType) {
512 // First check if immediate type is legal. If not, we can't use the rf form.
513 unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
517 // Materialize the constant in a register.
518 unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
519 if (MaterialReg == 0) {
520 // If the target doesn't have a way to directly enter a floating-point
521 // value into a register, use an alternate approach.
522 // TODO: The current approach only supports floating-point constants
523 // that can be constructed by conversion from integer values. This should
524 // be replaced by code that creates a load from a constant-pool entry,
525 // which will require some target-specific work.
526 const APFloat &Flt = FPImm->getValueAPF();
527 MVT IntVT = TLI.getPointerTy();
530 uint32_t IntBitWidth = IntVT.getSizeInBits();
531 if (Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
532 APFloat::rmTowardZero) != APFloat::opOK)
534 APInt IntVal(IntBitWidth, 2, x);
536 unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
537 ISD::Constant, IntVal.getZExtValue());
540 MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
541 ISD::SINT_TO_FP, IntegerReg);
542 if (MaterialReg == 0)
545 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
548 unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
549 return MRI.createVirtualRegister(RC);
552 unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
553 const TargetRegisterClass* RC) {
554 unsigned ResultReg = createResultReg(RC);
555 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
557 BuildMI(MBB, II, ResultReg);
561 unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
562 const TargetRegisterClass *RC,
564 unsigned ResultReg = createResultReg(RC);
565 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
567 BuildMI(MBB, II, ResultReg).addReg(Op0);
571 unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
572 const TargetRegisterClass *RC,
573 unsigned Op0, unsigned Op1) {
574 unsigned ResultReg = createResultReg(RC);
575 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
577 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1);
581 unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
582 const TargetRegisterClass *RC,
583 unsigned Op0, uint64_t Imm) {
584 unsigned ResultReg = createResultReg(RC);
585 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
587 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
591 unsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
592 const TargetRegisterClass *RC,
593 unsigned Op0, ConstantFP *FPImm) {
594 unsigned ResultReg = createResultReg(RC);
595 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
597 BuildMI(MBB, II, ResultReg).addReg(Op0).addFPImm(FPImm);
601 unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
602 const TargetRegisterClass *RC,
603 unsigned Op0, unsigned Op1, uint64_t Imm) {
604 unsigned ResultReg = createResultReg(RC);
605 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
607 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
611 unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
612 const TargetRegisterClass *RC,
614 unsigned ResultReg = createResultReg(RC);
615 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
617 BuildMI(MBB, II, ResultReg).addImm(Imm);
621 unsigned FastISel::FastEmitInst_extractsubreg(unsigned Op0, uint32_t Idx) {
622 const TargetRegisterClass* RC = MRI.getRegClass(Op0);
623 const TargetRegisterClass* SRC = *(RC->subregclasses_begin()+Idx-1);
625 unsigned ResultReg = createResultReg(SRC);
626 const TargetInstrDesc &II = TII.get(TargetInstrInfo::EXTRACT_SUBREG);
628 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Idx);