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