From d0533c9998d3baf41848ba559a9b2f2c65296d14 Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Tue, 26 Aug 2008 23:46:32 +0000 Subject: [PATCH] Factor out a large amoutn of the cast handling code in fast isel into helper methods. This simultaneously makes the code simpler and adds support for sext as well. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@55398 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/CodeGen/FastISel.h | 8 ++ lib/CodeGen/SelectionDAG/FastISel.cpp | 188 +++++++++++--------------- 2 files changed, 89 insertions(+), 107 deletions(-) diff --git a/include/llvm/CodeGen/FastISel.h b/include/llvm/CodeGen/FastISel.h index 591c8bde30e..9ed66f10e45 100644 --- a/include/llvm/CodeGen/FastISel.h +++ b/include/llvm/CodeGen/FastISel.h @@ -170,6 +170,14 @@ private: bool SelectBitCast(Instruction *I, DenseMap &ValueMap); + + bool SelectCast(Instruction *I, ISD::NodeType Opcode, + DenseMap &ValueMap); + + bool SelectConstantCast(Instruction *I, ISD::NodeType Opcode, + DenseMap &ValueMap); + bool SelectConstantFPCast(Instruction *I, ISD::NodeType Opcode, + DenseMap &ValueMap); }; } diff --git a/lib/CodeGen/SelectionDAG/FastISel.cpp b/lib/CodeGen/SelectionDAG/FastISel.cpp index 99ffc3f7817..59bc8c5b333 100644 --- a/lib/CodeGen/SelectionDAG/FastISel.cpp +++ b/lib/CodeGen/SelectionDAG/FastISel.cpp @@ -149,6 +149,70 @@ bool FastISel::SelectGetElementPtr(Instruction *I, return true; } +bool FastISel::SelectCast(Instruction *I, ISD::NodeType Opcode, + DenseMap &ValueMap) { + MVT SrcVT = MVT::getMVT(I->getOperand(0)->getType()); + MVT DstVT = MVT::getMVT(I->getType()); + + if (SrcVT == MVT::Other || !SrcVT.isSimple() || + DstVT == MVT::Other || !DstVT.isSimple() || + !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT)) + // Unhandled type. Halt "fast" selection and bail. + return false; + + unsigned InputReg = ValueMap[I->getOperand(0)]; + if (!InputReg) + // Unhandled operand. Halt "fast" selection and bail. + return false; + + unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(), + DstVT.getSimpleVT(), + Opcode, + InputReg); + if (!ResultReg) + return false; + + ValueMap[I] = ResultReg; + return true; +} + +bool FastISel::SelectConstantCast(Instruction* I, ISD::NodeType Opcode, + DenseMap &ValueMap) { + // Materialize constant and convert. + ConstantInt* CI = cast(I->getOperand(0)); + MVT SrcVT = MVT::getMVT(CI->getType()); + MVT DstVT = MVT::getMVT(I->getType()); + + if (SrcVT == MVT::Other || !SrcVT.isSimple() || + DstVT == MVT::Other || !DstVT.isSimple() || + !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT)) + // Unhandled type. Halt "fast" selection and bail. + return false; + + unsigned ResultReg1 = FastEmit_i(SrcVT.getSimpleVT(), + SrcVT.getSimpleVT(), + ISD::Constant, CI->getZExtValue()); + if (!ResultReg1) + return false; + + unsigned ResultReg2 = FastEmit_r(SrcVT.getSimpleVT(), + DstVT.getSimpleVT(), + Opcode, + ResultReg1); + if (!ResultReg2) + return false; + + ValueMap[I] = ResultReg2; + return true; +} + +bool FastISel::SelectConstantFPCast(Instruction* I, ISD::NodeType Opcode, + DenseMap &ValueMap) { + // TODO: Implement casting of FP constants by materialization + // followed by conversion. + return false; +} + bool FastISel::SelectBitCast(Instruction *I, DenseMap &ValueMap) { // BitCast consists of either an immediate to register move @@ -297,122 +361,32 @@ FastISel::SelectInstructions(BasicBlock::iterator Begin, break; case Instruction::BitCast: - if (!SelectBitCast(I, ValueMap)) return I; - break; + if (!SelectBitCast(I, ValueMap)) return I; break; case Instruction::FPToSI: if (!isa(I->getOperand(0))) { - MVT SrcVT = MVT::getMVT(I->getOperand(0)->getType()); - MVT DstVT = MVT::getMVT(I->getType()); - - if (SrcVT == MVT::Other || !SrcVT.isSimple() || - DstVT == MVT::Other || !DstVT.isSimple() || - !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT)) - // Unhandled type. Halt "fast" selection and bail. - return I; - - unsigned InputReg = ValueMap[I->getOperand(0)]; - if (!InputReg) - // Unhandled operand. Halt "fast" selection and bail. - return I; - - unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(), - DstVT.getSimpleVT(), - ISD::FP_TO_SINT, - InputReg); - if (!ResultReg) - return I; - - ValueMap[I] = ResultReg; - break; + if (!SelectCast(I, ISD::FP_TO_SINT, ValueMap)) return I; } else - // TODO: Materialize the FP constant and then convert, - // or attempt constant folding. - return I; - + if (!SelectConstantFPCast(I, ISD::FP_TO_SINT, ValueMap)) return I; + break; case Instruction::ZExt: if (!isa(I->getOperand(0))) { - MVT SrcVT = MVT::getMVT(I->getOperand(0)->getType()); - MVT DstVT = MVT::getMVT(I->getType()); - - if (SrcVT == MVT::Other || !SrcVT.isSimple() || - DstVT == MVT::Other || !DstVT.isSimple() || - !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT)) - // Unhandled type. Halt "fast" selection and bail. - return I; - - unsigned InputReg = ValueMap[I->getOperand(0)]; - if (!InputReg) - // Unhandled operand. Halt "fast" selection and bail. - return I; - - unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(), - DstVT.getSimpleVT(), - ISD::ZERO_EXTEND, - InputReg); - if (!ResultReg) - return I; - - ValueMap[I] = ResultReg; - break; + if (!SelectCast(I, ISD::ZERO_EXTEND, ValueMap)) return I; } else - // TODO: Support constant operands - return I; - + if (!SelectConstantCast(I, ISD::ZERO_EXTEND, ValueMap)) return I; + break; + case Instruction::SExt: + if (!isa(I->getOperand(0))) { + if (!SelectCast(I, ISD::SIGN_EXTEND, ValueMap)) return I; + } else + if (!SelectConstantCast(I, ISD::SIGN_EXTEND, ValueMap)) return I; + break; case Instruction::SIToFP: if (!isa(I->getOperand(0))) { - MVT SrcVT = MVT::getMVT(I->getOperand(0)->getType()); - MVT DstVT = MVT::getMVT(I->getType()); - - if (SrcVT == MVT::Other || !SrcVT.isSimple() || - DstVT == MVT::Other || !DstVT.isSimple() || - !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT)) - // Unhandled type. Halt "fast" selection and bail. - return I; - - unsigned InputReg = ValueMap[I->getOperand(0)]; - if (!InputReg) - // Unhandled operan. Halt "fast" selection and bail. - return I; - - unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(), - DstVT.getSimpleVT(), - ISD::SINT_TO_FP, - InputReg); - if (!ResultReg) - return I; - - ValueMap[I] = ResultReg; - break; - } else { - // Materialize constant and convert to FP. - // TODO: Attempt constant folding? - ConstantInt* CI = cast(I->getOperand(0)); - MVT SrcVT = MVT::getMVT(CI->getType()); - MVT DstVT = MVT::getMVT(I->getType()); - - if (SrcVT == MVT::Other || !SrcVT.isSimple() || - DstVT == MVT::Other || !DstVT.isSimple() || - !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT)) - // Unhandled type. Halt "fast" selection and bail. - return I; - - unsigned ResultReg1 = FastEmit_i(SrcVT.getSimpleVT(), - SrcVT.getSimpleVT(), - ISD::Constant, CI->getZExtValue()); - if (!ResultReg1) - return I; - - unsigned ResultReg2 = FastEmit_r(SrcVT.getSimpleVT(), - DstVT.getSimpleVT(), - ISD::SINT_TO_FP, - ResultReg1); - if (!ResultReg2) - return I; - - ValueMap[I] = ResultReg2; - break; - } + if (!SelectCast(I, ISD::SINT_TO_FP, ValueMap)) return I; + } else + if (!SelectConstantCast(I, ISD::SINT_TO_FP, ValueMap)) return I; + break; default: // Unhandled instruction. Halt "fast" selection and bail.