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 /// SelectBinaryOp - Select and emit code for a binary operator instruction,
25 /// which has an opcode which directly corresponds to the given ISD opcode.
27 bool FastISel::SelectBinaryOp(Instruction *I, ISD::NodeType ISDOpcode,
28 DenseMap<const Value*, unsigned> &ValueMap) {
29 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
30 if (VT == MVT::Other || !VT.isSimple())
31 // Unhandled type. Halt "fast" selection and bail.
33 // 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
37 if (!TLI.isTypeLegal(VT))
40 unsigned Op0 = ValueMap[I->getOperand(0)];
42 // Unhandled operand. Halt "fast" selection and bail.
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());
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.
54 // We successfully emitted code for the given LLVM Instruction.
55 ValueMap[I] = ResultReg;
59 unsigned Op1 = ValueMap[I->getOperand(1)];
61 // Unhandled operand. Halt "fast" selection and bail.
64 unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
67 // Target-specific code wasn't able to find a machine opcode for
68 // the given ISD opcode and type. Halt "fast" selection and bail.
71 // We successfully emitted code for the given LLVM Instruction.
72 ValueMap[I] = ResultReg;
76 bool FastISel::SelectGetElementPtr(Instruction *I,
77 DenseMap<const Value*, unsigned> &ValueMap) {
78 unsigned N = ValueMap[I->getOperand(0)];
80 // Unhandled operand. Halt "fast" selection and bail.
83 const Type *Ty = I->getOperand(0)->getType();
84 MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
85 for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
88 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
89 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
92 uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
93 // FIXME: This can be optimized by combining the add with a
95 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
97 // Unhandled operand. Halt "fast" selection and bail.
100 Ty = StTy->getElementType(Field);
102 Ty = cast<SequentialType>(Ty)->getElementType();
104 // If this is a constant subscript, handle it quickly.
105 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
106 if (CI->getZExtValue() == 0) continue;
108 TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
109 N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
111 // Unhandled operand. Halt "fast" selection and bail.
116 // N = N + Idx * ElementSize;
117 uint64_t ElementSize = TD.getABITypeSize(Ty);
118 unsigned IdxN = ValueMap[Idx];
120 // Unhandled operand. Halt "fast" selection and bail.
123 // If the index is smaller or larger than intptr_t, truncate or extend
125 MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
126 if (IdxVT.bitsLT(VT))
127 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::SIGN_EXTEND, IdxN);
128 else if (IdxVT.bitsGT(VT))
129 IdxN = FastEmit_r(IdxVT.getSimpleVT(), VT, ISD::TRUNCATE, IdxN);
131 // Unhandled operand. Halt "fast" selection and bail.
134 if (ElementSize != 1) {
135 IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
137 // Unhandled operand. Halt "fast" selection and bail.
140 N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
142 // Unhandled operand. Halt "fast" selection and bail.
147 // We successfully emitted code for the given LLVM Instruction.
153 FastISel::SelectInstructions(BasicBlock::iterator Begin,
154 BasicBlock::iterator End,
155 DenseMap<const Value*, unsigned> &ValueMap,
156 DenseMap<const BasicBlock*,
157 MachineBasicBlock *> &MBBMap,
158 MachineBasicBlock *mbb) {
160 BasicBlock::iterator I = Begin;
162 for (; I != End; ++I) {
163 switch (I->getOpcode()) {
164 case Instruction::Add: {
165 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
166 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
168 case Instruction::Sub: {
169 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
170 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
172 case Instruction::Mul: {
173 ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
174 if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break;
176 case Instruction::SDiv:
177 if (!SelectBinaryOp(I, ISD::SDIV, ValueMap)) return I; break;
178 case Instruction::UDiv:
179 if (!SelectBinaryOp(I, ISD::UDIV, ValueMap)) return I; break;
180 case Instruction::FDiv:
181 if (!SelectBinaryOp(I, ISD::FDIV, ValueMap)) return I; break;
182 case Instruction::SRem:
183 if (!SelectBinaryOp(I, ISD::SREM, ValueMap)) return I; break;
184 case Instruction::URem:
185 if (!SelectBinaryOp(I, ISD::UREM, ValueMap)) return I; break;
186 case Instruction::FRem:
187 if (!SelectBinaryOp(I, ISD::FREM, ValueMap)) return I; break;
188 case Instruction::Shl:
189 if (!SelectBinaryOp(I, ISD::SHL, ValueMap)) return I; break;
190 case Instruction::LShr:
191 if (!SelectBinaryOp(I, ISD::SRL, ValueMap)) return I; break;
192 case Instruction::AShr:
193 if (!SelectBinaryOp(I, ISD::SRA, ValueMap)) return I; break;
194 case Instruction::And:
195 if (!SelectBinaryOp(I, ISD::AND, ValueMap)) return I; break;
196 case Instruction::Or:
197 if (!SelectBinaryOp(I, ISD::OR, ValueMap)) return I; break;
198 case Instruction::Xor:
199 if (!SelectBinaryOp(I, ISD::XOR, ValueMap)) return I; break;
201 case Instruction::GetElementPtr:
202 if (!SelectGetElementPtr(I, ValueMap)) return I;
205 case Instruction::Br: {
206 BranchInst *BI = cast<BranchInst>(I);
208 if (BI->isUnconditional()) {
209 MachineFunction::iterator NextMBB =
210 next(MachineFunction::iterator(MBB));
211 BasicBlock *LLVMSucc = BI->getSuccessor(0);
212 MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
214 if (NextMBB != MF.end() && MSucc == NextMBB) {
215 // The unconditional fall-through case, which needs no instructions.
217 // The unconditional branch case.
218 TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
220 MBB->addSuccessor(MSucc);
224 // Conditional branches are not handed yet.
225 // Halt "fast" selection and bail.
229 case Instruction::PHI:
230 // PHI nodes are already emitted.
233 case Instruction::BitCast:
234 // BitCast consists of either an immediate to register move
235 // or a register to register move.
236 if (ConstantInt* CI = dyn_cast<ConstantInt>(I->getOperand(0))) {
237 if (I->getType()->isInteger()) {
238 MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/false);
239 unsigned result = FastEmit_i(VT.getSimpleVT(), VT.getSimpleVT(),
245 ValueMap[I] = result;
248 // TODO: Support vector and fp constants.
250 } else if (!isa<Constant>(I->getOperand(0))) {
251 // Bitcasts of non-constant values become reg-reg copies.
252 MVT SrcVT = MVT::getMVT(I->getOperand(0)->getType());
253 MVT DstVT = MVT::getMVT(I->getType());
255 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
256 DstVT == MVT::Other || !DstVT.isSimple() ||
257 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
258 // Unhandled type. Halt "fast" selection and bail.
261 unsigned Op0 = ValueMap[I->getOperand(0)];
263 // Unhandled operand. Halt "fast" selection and bail.
266 // First, try to perform the bitcast by inserting a reg-reg copy.
267 unsigned ResultReg = 0;
268 if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
269 TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
270 TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
271 ResultReg = createResultReg(DstClass);
273 bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
274 Op0, DstClass, SrcClass);
279 // If the reg-reg copy failed, select a BIT_CONVERT opcode.
281 ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
282 ISD::BIT_CONVERT, Op0);
287 ValueMap[I] = ResultReg;
290 // TODO: Casting a non-integral constant?
293 case Instruction::FPToSI:
294 if (!isa<ConstantFP>(I->getOperand(0))) {
295 MVT SrcVT = MVT::getMVT(I->getOperand(0)->getType());
296 MVT DstVT = MVT::getMVT(I->getType());
298 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
299 DstVT == MVT::Other || !DstVT.isSimple() ||
300 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
301 // Unhandled type. Halt "fast" selection and bail.
304 unsigned InputReg = ValueMap[I->getOperand(0)];
306 // Unhandled operand. Halt "fast" selection and bail.
309 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
316 ValueMap[I] = ResultReg;
319 // TODO: Materialize the FP constant and then convert,
320 // or attempt constant folding.
323 case Instruction::SIToFP:
324 if (!isa<ConstantInt>(I->getOperand(0))) {
325 MVT SrcVT = MVT::getMVT(I->getOperand(0)->getType());
326 MVT DstVT = MVT::getMVT(I->getType());
328 if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
329 DstVT == MVT::Other || !DstVT.isSimple() ||
330 !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
331 // Unhandled type. Halt "fast" selection and bail.
334 unsigned InputReg = ValueMap[I->getOperand(0)];
336 // Unhandled operan. Halt "fast" selection and bail.
339 unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
346 ValueMap[I] = ResultReg;
349 // TODO: Materialize constant and convert to FP.
352 // Unhandled instruction. Halt "fast" selection and bail.
360 FastISel::FastISel(MachineFunction &mf)
362 MRI(mf.getRegInfo()),
364 TD(*TM.getTargetData()),
365 TII(*TM.getInstrInfo()),
366 TLI(*TM.getTargetLowering()) {
369 FastISel::~FastISel() {}
371 unsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType, ISD::NodeType) {
375 unsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
376 ISD::NodeType, unsigned /*Op0*/) {
380 unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType,
381 ISD::NodeType, unsigned /*Op0*/,
386 unsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
387 ISD::NodeType, uint64_t /*Imm*/) {
391 unsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
392 ISD::NodeType, unsigned /*Op0*/,
397 unsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
399 unsigned /*Op0*/, unsigned /*Op1*/,
404 /// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
405 /// to emit an instruction with an immediate operand using FastEmit_ri.
406 /// If that fails, it materializes the immediate into a register and try
407 /// FastEmit_rr instead.
408 unsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
409 unsigned Op0, uint64_t Imm,
410 MVT::SimpleValueType ImmType) {
411 unsigned ResultReg = 0;
412 // First check if immediate type is legal. If not, we can't use the ri form.
413 if (TLI.getOperationAction(ISD::Constant, ImmType) == TargetLowering::Legal)
414 ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
417 unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
418 if (MaterialReg == 0)
420 return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
423 unsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
424 return MRI.createVirtualRegister(RC);
427 unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
428 const TargetRegisterClass* RC) {
429 unsigned ResultReg = createResultReg(RC);
430 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
432 BuildMI(MBB, II, ResultReg);
436 unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
437 const TargetRegisterClass *RC,
439 unsigned ResultReg = createResultReg(RC);
440 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
442 BuildMI(MBB, II, ResultReg).addReg(Op0);
446 unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
447 const TargetRegisterClass *RC,
448 unsigned Op0, unsigned Op1) {
449 unsigned ResultReg = createResultReg(RC);
450 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
452 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1);
456 unsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
457 const TargetRegisterClass *RC,
458 unsigned Op0, uint64_t Imm) {
459 unsigned ResultReg = createResultReg(RC);
460 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
462 BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
466 unsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
467 const TargetRegisterClass *RC,
468 unsigned Op0, unsigned Op1, uint64_t Imm) {
469 unsigned ResultReg = createResultReg(RC);
470 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
472 BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
476 unsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
477 const TargetRegisterClass *RC,
479 unsigned ResultReg = createResultReg(RC);
480 const TargetInstrDesc &II = TII.get(MachineInstOpcode);
482 BuildMI(MBB, II, ResultReg).addImm(Imm);