From: Chris Lattner Date: Wed, 31 Oct 2001 05:07:57 +0000 (+0000) Subject: Implemented constant propogation of cast instructions X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=37aabf28b3ae64356b58d7e0bed2c6764bc52334;p=oota-llvm.git Implemented constant propogation of cast instructions git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@1064 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/docs/ChrisNotes.txt b/docs/ChrisNotes.txt index 735e2b4d964..158c5be9867 100644 --- a/docs/ChrisNotes.txt +++ b/docs/ChrisNotes.txt @@ -5,7 +5,6 @@ arrays and multidim arrays * Rewrite the llvm parser/lexer in http://www.antlr.org when time permits. They actually do C++. Imagine that. -* Need to implement constant propogation of cast instructions! * Fix DCE to elminate br , %L1, %L1 so that it can optimize the main of fib.ll better. Currently I have to do this to get best results: as < fib.ll | opt -inline -sccp -dce -sccp -dce |dis diff --git a/include/llvm/ConstantHandling.h b/include/llvm/ConstantHandling.h index 9ce1b2cb087..d3af1bda464 100644 --- a/include/llvm/ConstantHandling.h +++ b/include/llvm/ConstantHandling.h @@ -175,10 +175,16 @@ inline ConstPoolBool *operator<=(const ConstPoolVal &V1, // Implement higher level instruction folding type instructions //===----------------------------------------------------------------------===// +inline ConstPoolVal *ConstantFoldCastInstruction(ConstPoolVal *V, + const Type *DestTy) { + return ConstRules::get(*V)->castTo(V, DestTy); +} + inline ConstPoolVal *ConstantFoldUnaryInstruction(unsigned Opcode, ConstPoolVal *V) { switch (Opcode) { case Instruction::Not: return !*V; + // TODO: Handle get element ptr instruction here in the future? GEP null? } return 0; } diff --git a/lib/Transforms/Scalar/ConstantProp.cpp b/lib/Transforms/Scalar/ConstantProp.cpp index 6ab97808a65..babcde0f0c0 100644 --- a/lib/Transforms/Scalar/ConstantProp.cpp +++ b/lib/Transforms/Scalar/ConstantProp.cpp @@ -53,6 +53,29 @@ ConstantFoldUnaryInst(Method *M, Method::inst_iterator &DI, return true; } +inline static bool +ConstantFoldCast(Method *M, Method::inst_iterator &DI, + CastInst *CI, ConstPoolVal *D) { + ConstPoolVal *ReplaceWith = + opt::ConstantFoldCastInstruction(D, CI->getType()); + + if (!ReplaceWith) return false; // Nothing new to change... + + // Replaces all of the uses of a variable with uses of the constant. + CI->replaceAllUsesWith(ReplaceWith); + + // Remove the cast from the list of definitions... + CI->getParent()->getInstList().remove(DI.getInstructionIterator()); + + // The new constant inherits the old name of the cast... + if (CI->hasName()) + ReplaceWith->setName(CI->getName(), M->getSymbolTableSure()); + + // Delete the cast now... + delete CI; + return true; +} + inline static bool ConstantFoldBinaryInst(Method *M, Method::inst_iterator &DI, BinaryOperator *Op, @@ -142,6 +165,10 @@ ConstantFoldInstruction(Method *M, Method::inst_iterator &II) { if (D1 && D2) return ConstantFoldBinaryInst(M, II, cast(Inst), D1, D2); + } else if (CastInst *CI = dyn_cast(Inst)) { + ConstPoolVal *D = dyn_cast(CI->getOperand(0)); + if (D) return ConstantFoldCast(M, II, CI, D); + } else if (UnaryOperator *UInst = dyn_cast(Inst)) { ConstPoolVal *D = dyn_cast(UInst->getOperand(0)); if (D) return ConstantFoldUnaryInst(M, II, UInst, D); diff --git a/lib/Transforms/Scalar/SCCP.cpp b/lib/Transforms/Scalar/SCCP.cpp index 101e892653d..70d52a6b14b 100644 --- a/lib/Transforms/Scalar/SCCP.cpp +++ b/lib/Transforms/Scalar/SCCP.cpp @@ -442,9 +442,10 @@ void SCCP::UpdateInstruction(Instruction *I) { if (VState.isOverdefined()) { // Inherit overdefinedness of operand markOverdefined(I); } else if (VState.isConstant()) { // Propogate constant value - ConstPoolVal *Result = - opt::ConstantFoldUnaryInstruction(I->getOpcode(), - VState.getConstant()); + ConstPoolVal *Result = isa(I) + ? opt::ConstantFoldCastInstruction(VState.getConstant(), I->getType()) + : opt::ConstantFoldUnaryInstruction(I->getOpcode(), + VState.getConstant()); if (Result) { // This instruction constant folds! diff --git a/lib/VMCore/ConstantFold.h b/lib/VMCore/ConstantFold.h index 9ce1b2cb087..d3af1bda464 100644 --- a/lib/VMCore/ConstantFold.h +++ b/lib/VMCore/ConstantFold.h @@ -175,10 +175,16 @@ inline ConstPoolBool *operator<=(const ConstPoolVal &V1, // Implement higher level instruction folding type instructions //===----------------------------------------------------------------------===// +inline ConstPoolVal *ConstantFoldCastInstruction(ConstPoolVal *V, + const Type *DestTy) { + return ConstRules::get(*V)->castTo(V, DestTy); +} + inline ConstPoolVal *ConstantFoldUnaryInstruction(unsigned Opcode, ConstPoolVal *V) { switch (Opcode) { case Instruction::Not: return !*V; + // TODO: Handle get element ptr instruction here in the future? GEP null? } return 0; } diff --git a/lib/VMCore/ConstantFolding.h b/lib/VMCore/ConstantFolding.h index 9ce1b2cb087..d3af1bda464 100644 --- a/lib/VMCore/ConstantFolding.h +++ b/lib/VMCore/ConstantFolding.h @@ -175,10 +175,16 @@ inline ConstPoolBool *operator<=(const ConstPoolVal &V1, // Implement higher level instruction folding type instructions //===----------------------------------------------------------------------===// +inline ConstPoolVal *ConstantFoldCastInstruction(ConstPoolVal *V, + const Type *DestTy) { + return ConstRules::get(*V)->castTo(V, DestTy); +} + inline ConstPoolVal *ConstantFoldUnaryInstruction(unsigned Opcode, ConstPoolVal *V) { switch (Opcode) { case Instruction::Not: return !*V; + // TODO: Handle get element ptr instruction here in the future? GEP null? } return 0; }