Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 1 | ///===-- FastISel.cpp - Implementation of the FastISel class --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains the implementation of the FastISel class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/CodeGen/FastISel.h" |
| 15 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 16 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 17 | #include "llvm/Target/TargetInstrInfo.h" |
| 18 | using namespace llvm; |
| 19 | |
| 20 | BasicBlock::iterator |
| 21 | FastISel::SelectInstructions(BasicBlock::iterator Begin, BasicBlock::iterator End, |
| 22 | DenseMap<const Value*, unsigned> &ValueMap) { |
| 23 | BasicBlock::iterator I = Begin; |
| 24 | |
| 25 | for (; I != End; ++I) { |
| 26 | switch (I->getOpcode()) { |
| 27 | case Instruction::Add: { |
| 28 | unsigned Op0 = ValueMap[I->getOperand(0)]; |
| 29 | unsigned Op1 = ValueMap[I->getOperand(1)]; |
| 30 | MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true); |
| 31 | if (VT == MVT::Other || !VT.isSimple()) { |
| 32 | // Unhandled type. Halt "fast" selection and bail. |
| 33 | return I; |
| 34 | } |
| 35 | unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), ISD::ADD, Op0, Op1); |
Dan Gohman | 32155ac | 2008-08-19 20:43:22 +0000 | [diff] [blame] | 36 | if (ResultReg == 0) { |
| 37 | // Target-specific code wasn't able to find a machine opcode for |
| 38 | // the given ISD opcode and type. Halt "fast" selection and bail. |
| 39 | return I; |
| 40 | } |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 41 | ValueMap[I] = ResultReg; |
| 42 | break; |
| 43 | } |
| 44 | default: |
| 45 | // Unhandled instruction. Halt "fast" selection and bail. |
| 46 | return I; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | return I; |
| 51 | } |
| 52 | |
Dan Gohman | e285a74 | 2008-08-14 21:51:29 +0000 | [diff] [blame] | 53 | FastISel::~FastISel() {} |
| 54 | |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 55 | unsigned FastISel::FastEmit_(MVT::SimpleValueType, ISD::NodeType) { |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | unsigned FastISel::FastEmit_r(MVT::SimpleValueType, ISD::NodeType, |
| 60 | unsigned /*Op0*/) { |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | unsigned FastISel::FastEmit_rr(MVT::SimpleValueType, ISD::NodeType, |
| 65 | unsigned /*Op0*/, unsigned /*Op0*/) { |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | unsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode, |
| 70 | const TargetRegisterClass* RC) { |
| 71 | MachineRegisterInfo &MRI = MF->getRegInfo(); |
| 72 | const TargetInstrDesc &II = TII->get(MachineInstOpcode); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 73 | unsigned ResultReg = MRI.createVirtualRegister(RC); |
| 74 | |
Dan Gohman | 8133a52 | 2008-08-19 20:46:54 +0000 | [diff] [blame^] | 75 | MachineInstr *MI = BuildMI(*MF, II, ResultReg); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 76 | |
| 77 | MBB->push_back(MI); |
| 78 | return ResultReg; |
| 79 | } |
| 80 | |
| 81 | unsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode, |
| 82 | const TargetRegisterClass *RC, |
| 83 | unsigned Op0) { |
| 84 | MachineRegisterInfo &MRI = MF->getRegInfo(); |
| 85 | const TargetInstrDesc &II = TII->get(MachineInstOpcode); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 86 | unsigned ResultReg = MRI.createVirtualRegister(RC); |
| 87 | |
Dan Gohman | 8133a52 | 2008-08-19 20:46:54 +0000 | [diff] [blame^] | 88 | MachineInstr *MI = BuildMI(*MF, II, ResultReg); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 89 | MI->addOperand(MachineOperand::CreateReg(Op0, false)); |
| 90 | |
| 91 | MBB->push_back(MI); |
| 92 | return ResultReg; |
| 93 | } |
| 94 | |
| 95 | unsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode, |
| 96 | const TargetRegisterClass *RC, |
| 97 | unsigned Op0, unsigned Op1) { |
| 98 | MachineRegisterInfo &MRI = MF->getRegInfo(); |
| 99 | const TargetInstrDesc &II = TII->get(MachineInstOpcode); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 100 | unsigned ResultReg = MRI.createVirtualRegister(RC); |
| 101 | |
Dan Gohman | 8133a52 | 2008-08-19 20:46:54 +0000 | [diff] [blame^] | 102 | MachineInstr *MI = BuildMI(*MF, II, ResultReg); |
Dan Gohman | b0cf29c | 2008-08-13 20:19:35 +0000 | [diff] [blame] | 103 | MI->addOperand(MachineOperand::CreateReg(Op0, false)); |
| 104 | MI->addOperand(MachineOperand::CreateReg(Op1, false)); |
| 105 | |
| 106 | MBB->push_back(MI); |
| 107 | return ResultReg; |
| 108 | } |