FastISel.cpp revision b41aec54767a825ac54c8822e787700bb08a3460
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 146f2766d59744bb3d48867f3151643eac7111e773Dan Gohman#include "llvm/Instructions.h" 15b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman#include "llvm/CodeGen/FastISel.h" 16b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman#include "llvm/CodeGen/MachineInstrBuilder.h" 17b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman#include "llvm/CodeGen/MachineRegisterInfo.h" 1883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng#include "llvm/Target/TargetData.h" 19b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman#include "llvm/Target/TargetInstrInfo.h" 2083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng#include "llvm/Target/TargetLowering.h" 21bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman#include "llvm/Target/TargetMachine.h" 22b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanusing namespace llvm; 23b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman 24bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman/// SelectBinaryOp - Select and emit code for a binary operator instruction, 25bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman/// which has an opcode which directly corresponds to the given ISD opcode. 26bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman/// 27bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohmanbool FastISel::SelectBinaryOp(Instruction *I, ISD::NodeType ISDOpcode, 28bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman DenseMap<const Value*, unsigned> &ValueMap) { 29d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true); 30d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman if (VT == MVT::Other || !VT.isSimple()) 31d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman // Unhandled type. Halt "fast" selection and bail. 32d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman return false; 33d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman 34bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman unsigned Op0 = ValueMap[I->getOperand(0)]; 35d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman if (Op0 == 0) 36a7f2dff98e68ed8b2ac32f953768c04f26b52beaDan Gohman // Unhandled operand. Halt "fast" selection and bail. 37a7f2dff98e68ed8b2ac32f953768c04f26b52beaDan Gohman return false; 38a7f2dff98e68ed8b2ac32f953768c04f26b52beaDan Gohman 39d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman // Check if the second operand is a constant and handle it appropriately. 40d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { 41d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0, 42d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman CI->getZExtValue(), VT.getSimpleVT()); 43d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman if (ResultReg == 0) 44d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman // Target-specific code wasn't able to find a machine opcode for 45d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman // the given ISD opcode and type. Halt "fast" selection and bail. 46d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman return false; 47d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman 48d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman // We successfully emitted code for the given LLVM Instruction. 49d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman ValueMap[I] = ResultReg; 50d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman return true; 51d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman } 52d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman 53d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman unsigned Op1 = ValueMap[I->getOperand(1)]; 54d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman if (Op1 == 0) 55d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman // Unhandled operand. Halt "fast" selection and bail. 56bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman return false; 57bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman 58bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), ISDOpcode, Op0, Op1); 59bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman if (ResultReg == 0) 60bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman // Target-specific code wasn't able to find a machine opcode for 61bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman // the given ISD opcode and type. Halt "fast" selection and bail. 62bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman return false; 63bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman 648014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman // We successfully emitted code for the given LLVM Instruction. 65bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman ValueMap[I] = ResultReg; 66bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman return true; 67bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman} 68bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman 69bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohmanbool FastISel::SelectGetElementPtr(Instruction *I, 70bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman DenseMap<const Value*, unsigned> &ValueMap) { 7183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng unsigned N = ValueMap[I->getOperand(0)]; 7283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng if (N == 0) 7383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng // Unhandled operand. Halt "fast" selection and bail. 7483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng return false; 7583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng 7683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng const Type *Ty = I->getOperand(0)->getType(); 777a0e6593d03bd2dd21c3ac7dcf189f1da86b16daDan Gohman MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT(); 7883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end(); 7983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng OI != E; ++OI) { 8083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng Value *Idx = *OI; 8183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng if (const StructType *StTy = dyn_cast<StructType>(Ty)) { 8283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng unsigned Field = cast<ConstantInt>(Idx)->getZExtValue(); 8383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng if (Field) { 8483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng // N = N + Offset 8583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field); 8683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng // FIXME: This can be optimized by combining the add with a 8783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng // subsequent one. 887a0e6593d03bd2dd21c3ac7dcf189f1da86b16daDan Gohman N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT); 8983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng if (N == 0) 9083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng // Unhandled operand. Halt "fast" selection and bail. 9183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng return false; 9283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng } 9383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng Ty = StTy->getElementType(Field); 9483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng } else { 9583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng Ty = cast<SequentialType>(Ty)->getElementType(); 9683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng 9783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng // If this is a constant subscript, handle it quickly. 9883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) { 9983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng if (CI->getZExtValue() == 0) continue; 10083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng uint64_t Offs = 10183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue(); 1027a0e6593d03bd2dd21c3ac7dcf189f1da86b16daDan Gohman N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT); 10383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng if (N == 0) 10483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng // Unhandled operand. Halt "fast" selection and bail. 10583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng return false; 10683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng continue; 10783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng } 10883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng 10983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng // N = N + Idx * ElementSize; 11083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng uint64_t ElementSize = TD.getABITypeSize(Ty); 11183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng unsigned IdxN = ValueMap[Idx]; 11283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng if (IdxN == 0) 11383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng // Unhandled operand. Halt "fast" selection and bail. 11483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng return false; 11583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng 11683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng // If the index is smaller or larger than intptr_t, truncate or extend 11783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng // it. 1182076aa800e78a2e196eac47cc8413a074a761d8dEvan Cheng MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false); 11983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng if (IdxVT.bitsLT(VT)) 1207a0e6593d03bd2dd21c3ac7dcf189f1da86b16daDan Gohman IdxN = FastEmit_r(VT, ISD::SIGN_EXTEND, IdxN); 12183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng else if (IdxVT.bitsGT(VT)) 1227a0e6593d03bd2dd21c3ac7dcf189f1da86b16daDan Gohman IdxN = FastEmit_r(VT, ISD::TRUNCATE, IdxN); 12383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng if (IdxN == 0) 12483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng // Unhandled operand. Halt "fast" selection and bail. 12583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng return false; 12683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng 127f93cf79505f07cb97597fbc5955462ad7670ca5cDan Gohman if (ElementSize != 1) 128f93cf79505f07cb97597fbc5955462ad7670ca5cDan Gohman IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT); 12983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng if (IdxN == 0) 13083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng // Unhandled operand. Halt "fast" selection and bail. 13183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng return false; 1327a0e6593d03bd2dd21c3ac7dcf189f1da86b16daDan Gohman N = FastEmit_rr(VT, ISD::ADD, N, IdxN); 13383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng if (N == 0) 13483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng // Unhandled operand. Halt "fast" selection and bail. 13583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng return false; 13683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng } 13783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng } 13883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng 13983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng // We successfully emitted code for the given LLVM Instruction. 14083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng ValueMap[I] = N; 14183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng return true; 142bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman} 143bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman 144b0cf29c5cfff797284b3660dc233e135feb65d9aDan GohmanBasicBlock::iterator 145b7864a9e23be526f4f8cde529a57c8943c61c0c7Dan GohmanFastISel::SelectInstructions(BasicBlock::iterator Begin, 146b7864a9e23be526f4f8cde529a57c8943c61c0c7Dan Gohman BasicBlock::iterator End, 147bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman DenseMap<const Value*, unsigned> &ValueMap, 1486ecf50908c78aae0feff1c378fbb75dcf013ed21Dan Gohman DenseMap<const BasicBlock*, 1493c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman MachineBasicBlock *> &MBBMap, 150bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman MachineBasicBlock *mbb) { 151bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman MBB = mbb; 152b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman BasicBlock::iterator I = Begin; 153b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman 154b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman for (; I != End; ++I) { 155b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman switch (I->getOpcode()) { 1568014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman case Instruction::Add: { 1578014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD; 1588014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break; 1598014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman } 1608014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman case Instruction::Sub: { 1618014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB; 1628014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break; 1638014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman } 1648014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman case Instruction::Mul: { 1658014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL; 1668014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman if (!SelectBinaryOp(I, Opc, ValueMap)) return I; break; 1678014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman } 168bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman case Instruction::SDiv: 169bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman if (!SelectBinaryOp(I, ISD::SDIV, ValueMap)) return I; break; 170bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman case Instruction::UDiv: 171bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman if (!SelectBinaryOp(I, ISD::UDIV, ValueMap)) return I; break; 172bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman case Instruction::FDiv: 173bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman if (!SelectBinaryOp(I, ISD::FDIV, ValueMap)) return I; break; 174bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman case Instruction::SRem: 175bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman if (!SelectBinaryOp(I, ISD::SREM, ValueMap)) return I; break; 176bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman case Instruction::URem: 177bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman if (!SelectBinaryOp(I, ISD::UREM, ValueMap)) return I; break; 178bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman case Instruction::FRem: 179bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman if (!SelectBinaryOp(I, ISD::FREM, ValueMap)) return I; break; 180bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman case Instruction::Shl: 181bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman if (!SelectBinaryOp(I, ISD::SHL, ValueMap)) return I; break; 182bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman case Instruction::LShr: 183bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman if (!SelectBinaryOp(I, ISD::SRL, ValueMap)) return I; break; 184bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman case Instruction::AShr: 185bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman if (!SelectBinaryOp(I, ISD::SRA, ValueMap)) return I; break; 186bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman case Instruction::And: 187bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman if (!SelectBinaryOp(I, ISD::AND, ValueMap)) return I; break; 188bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman case Instruction::Or: 189bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman if (!SelectBinaryOp(I, ISD::OR, ValueMap)) return I; break; 190bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman case Instruction::Xor: 191bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman if (!SelectBinaryOp(I, ISD::XOR, ValueMap)) return I; break; 192bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman 193bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman case Instruction::GetElementPtr: 194bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman if (!SelectGetElementPtr(I, ValueMap)) return I; 195b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman break; 196bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman 1976f2766d59744bb3d48867f3151643eac7111e773Dan Gohman case Instruction::Br: { 1986f2766d59744bb3d48867f3151643eac7111e773Dan Gohman BranchInst *BI = cast<BranchInst>(I); 1996f2766d59744bb3d48867f3151643eac7111e773Dan Gohman 200e6798b757acb3a2077c2498e9913fff2f5e4325cDan Gohman if (BI->isUnconditional()) { 2013c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman MachineFunction::iterator NextMBB = 202e6798b757acb3a2077c2498e9913fff2f5e4325cDan Gohman next(MachineFunction::iterator(MBB)); 2033c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman BasicBlock *LLVMSucc = BI->getSuccessor(0); 2043c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman MachineBasicBlock *MSucc = MBBMap[LLVMSucc]; 2053c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman 2063c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman if (NextMBB != MF.end() && MSucc == NextMBB) { 2073c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman // The unconditional fall-through case, which needs no instructions. 2083c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman } else { 2093c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman // The unconditional branch case. 2103c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>()); 211e6798b757acb3a2077c2498e9913fff2f5e4325cDan Gohman } 2123c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman MBB->addSuccessor(MSucc); 2133c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman break; 2146f2766d59744bb3d48867f3151643eac7111e773Dan Gohman } 2156f2766d59744bb3d48867f3151643eac7111e773Dan Gohman 2163c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman // Conditional branches are not handed yet. 2173c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman // Halt "fast" selection and bail. 2186f2766d59744bb3d48867f3151643eac7111e773Dan Gohman return I; 2196f2766d59744bb3d48867f3151643eac7111e773Dan Gohman } 2203b7753be2e899cf3a79835a8ff810e869035c87bDan Gohman 2213b7753be2e899cf3a79835a8ff810e869035c87bDan Gohman case Instruction::PHI: 2223b7753be2e899cf3a79835a8ff810e869035c87bDan Gohman // PHI nodes are already emitted. 2233b7753be2e899cf3a79835a8ff810e869035c87bDan Gohman break; 2246d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson 2256d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson case Instruction::BitCast: 2266d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson // BitCast consists of either an immediate to register move 2276d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson // or a register to register move. 2286d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson if (ConstantInt* CI = dyn_cast<ConstantInt>(I->getOperand(0))) { 2296d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson if (I->getType()->isInteger()) { 2306d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/false); 2316d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson ValueMap[I] = FastEmit_i(VT.getSimpleVT(), ISD::Constant, 2326d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson CI->getZExtValue()); 2336d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson break; 2346d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson } else 2356d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson // TODO: Support vector and fp constants. 2366d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson return I; 237d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson } else if (!isa<Constant>(I->getOperand(0))) { 238d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson // Bitcasts of non-constant values become reg-reg copies. 239d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson MVT SrcVT = MVT::getMVT(I->getOperand(0)->getType()); 240d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson MVT DstVT = MVT::getMVT(I->getOperand(0)->getType()); 241d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson 242d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson if (SrcVT == MVT::Other || !SrcVT.isSimple() || 243d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson DstVT == MVT::Other || !DstVT.isSimple() || 244d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT)) 245d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson // Unhandled type. Halt "fast" selection and bail. 246d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson return I; 247d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson if (!TLI.isConvertLegal(SrcVT, DstVT)) 248d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson // Illegal conversion. Halt "fast" selection and bail. 249b41aec54767a825ac54c8822e787700bb08a3460Evan Cheng return I; 250d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson 251d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson // Otherwise, insert a register-to-register copy. 252d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT); 253d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT); 254d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson unsigned Op0 = ValueMap[I->getOperand(0)]; 255d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson unsigned ResultReg = createResultReg(DstClass); 256d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson 257d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson if (Op0 == 0) 258d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson // Unhandled operand. Halt "fast" selection and bail. 259d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson return false; 260d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson 261d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson TII.copyRegToReg(*MBB, MBB->end(), ResultReg, Op0, DstClass, SrcClass); 262d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson ValueMap[I] = ResultReg; 263d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson 264d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson break; 2656d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson } else 266d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson // Casting a non-integral constant? 2676d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson return I; 2683b7753be2e899cf3a79835a8ff810e869035c87bDan Gohman 269b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman default: 270b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman // Unhandled instruction. Halt "fast" selection and bail. 271b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman return I; 272b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman } 273b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman } 274b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman 275b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman return I; 276b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman} 277b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman 278bb466331e7e50d03497ce40ee344870236fd9c32Dan GohmanFastISel::FastISel(MachineFunction &mf) 27922bb31103de3337f0bb74c7bee16d1817d4dca14Dan Gohman : MF(mf), 28022bb31103de3337f0bb74c7bee16d1817d4dca14Dan Gohman MRI(mf.getRegInfo()), 28122bb31103de3337f0bb74c7bee16d1817d4dca14Dan Gohman TM(mf.getTarget()), 28222bb31103de3337f0bb74c7bee16d1817d4dca14Dan Gohman TD(*TM.getTargetData()), 28322bb31103de3337f0bb74c7bee16d1817d4dca14Dan Gohman TII(*TM.getInstrInfo()), 28422bb31103de3337f0bb74c7bee16d1817d4dca14Dan Gohman TLI(*TM.getTargetLowering()) { 285bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman} 286bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman 287e285a74f7cf9dd3ccf4fe758576cf83301f8a43eDan GohmanFastISel::~FastISel() {} 288e285a74f7cf9dd3ccf4fe758576cf83301f8a43eDan Gohman 289b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanunsigned FastISel::FastEmit_(MVT::SimpleValueType, ISD::NodeType) { 290b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman return 0; 291b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman} 292b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman 293b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanunsigned FastISel::FastEmit_r(MVT::SimpleValueType, ISD::NodeType, 294b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman unsigned /*Op0*/) { 295b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman return 0; 296b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman} 297b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman 298b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanunsigned FastISel::FastEmit_rr(MVT::SimpleValueType, ISD::NodeType, 299b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman unsigned /*Op0*/, unsigned /*Op0*/) { 300b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman return 0; 301b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman} 302b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman 3036d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Andersonunsigned FastISel::FastEmit_i(MVT::SimpleValueType, ISD::NodeType, 3046d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson uint64_t /*Imm*/) { 30583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng return 0; 30683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng} 30783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng 30883785c80968165b30fcdd111ceb2c28d38bcff86Evan Chengunsigned FastISel::FastEmit_ri(MVT::SimpleValueType, ISD::NodeType, 309d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman unsigned /*Op0*/, uint64_t /*Imm*/) { 310d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman return 0; 311d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman} 312d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman 313d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohmanunsigned FastISel::FastEmit_rri(MVT::SimpleValueType, ISD::NodeType, 314d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman unsigned /*Op0*/, unsigned /*Op1*/, 315d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman uint64_t /*Imm*/) { 31683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng return 0; 31783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng} 31883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng 31983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries 32083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng/// to emit an instruction with an immediate operand using FastEmit_ri. 32183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng/// If that fails, it materializes the immediate into a register and try 32283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng/// FastEmit_rr instead. 32383785c80968165b30fcdd111ceb2c28d38bcff86Evan Chengunsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode, 324d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman unsigned Op0, uint64_t Imm, 325d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman MVT::SimpleValueType ImmType) { 32683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng unsigned ResultReg = 0; 32783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng // First check if immediate type is legal. If not, we can't use the ri form. 32883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng if (TLI.getOperationAction(ISD::Constant, ImmType) == TargetLowering::Legal) 329d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman ResultReg = FastEmit_ri(VT, Opcode, Op0, Imm); 33083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng if (ResultReg != 0) 33183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng return ResultReg; 3326d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson unsigned MaterialReg = FastEmit_i(ImmType, ISD::Constant, Imm); 333d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman if (MaterialReg == 0) 334d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman return 0; 335d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman return FastEmit_rr(VT, Opcode, Op0, MaterialReg); 336d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman} 337d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman 338d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohmanunsigned FastISel::createResultReg(const TargetRegisterClass* RC) { 339d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman return MRI.createVirtualRegister(RC); 34083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng} 34183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng 342b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanunsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode, 34377ad79689d755c49146f534107421cb3d9703fedDan Gohman const TargetRegisterClass* RC) { 344d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman unsigned ResultReg = createResultReg(RC); 345bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman const TargetInstrDesc &II = TII.get(MachineInstOpcode); 346b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman 347fd903944de8ddcbe88902fa1eca9366eb9641201Dan Gohman BuildMI(MBB, II, ResultReg); 348b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman return ResultReg; 349b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman} 350b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman 351b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanunsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode, 352b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman const TargetRegisterClass *RC, 353b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman unsigned Op0) { 354d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman unsigned ResultReg = createResultReg(RC); 355bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman const TargetInstrDesc &II = TII.get(MachineInstOpcode); 356b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman 357fd903944de8ddcbe88902fa1eca9366eb9641201Dan Gohman BuildMI(MBB, II, ResultReg).addReg(Op0); 358b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman return ResultReg; 359b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman} 360b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman 361b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanunsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode, 362b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman const TargetRegisterClass *RC, 363b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman unsigned Op0, unsigned Op1) { 364d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman unsigned ResultReg = createResultReg(RC); 365bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman const TargetInstrDesc &II = TII.get(MachineInstOpcode); 366b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman 367fd903944de8ddcbe88902fa1eca9366eb9641201Dan Gohman BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1); 368b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman return ResultReg; 369b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman} 370d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman 371d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohmanunsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode, 372d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman const TargetRegisterClass *RC, 373d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman unsigned Op0, uint64_t Imm) { 374d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman unsigned ResultReg = createResultReg(RC); 375d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman const TargetInstrDesc &II = TII.get(MachineInstOpcode); 376d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman 377d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm); 378d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman return ResultReg; 379d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman} 380d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman 381d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohmanunsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode, 382d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman const TargetRegisterClass *RC, 383d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman unsigned Op0, unsigned Op1, uint64_t Imm) { 384d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman unsigned ResultReg = createResultReg(RC); 385d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman const TargetInstrDesc &II = TII.get(MachineInstOpcode); 386d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman 387d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm); 388d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman return ResultReg; 389d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman} 3906d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson 3916d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Andersonunsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode, 3926d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson const TargetRegisterClass *RC, 3936d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson uint64_t Imm) { 3946d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson unsigned ResultReg = createResultReg(RC); 3956d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson const TargetInstrDesc &II = TII.get(MachineInstOpcode); 3966d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson 3976d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson BuildMI(MBB, II, ResultReg).addImm(Imm); 3986d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson return ResultReg; 399b41aec54767a825ac54c8822e787700bb08a3460Evan Cheng} 400