aboutsummaryrefslogtreecommitdiff
path: root/lib/Transforms/Scalar/ConstantProp.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2001-10-31 05:07:57 +0000
committerChris Lattner <sabre@nondot.org>2001-10-31 05:07:57 +0000
commit37aabf28b3ae64356b58d7e0bed2c6764bc52334 (patch)
treec63fdee814535adbe92a5ed801f44527d744f94d /lib/Transforms/Scalar/ConstantProp.cpp
parent11c862cf4661708bf4a52fe8a6b630bb844d76bd (diff)
Implemented constant propogation of cast instructions
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@1064 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms/Scalar/ConstantProp.cpp')
-rw-r--r--lib/Transforms/Scalar/ConstantProp.cpp27
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/Transforms/Scalar/ConstantProp.cpp b/lib/Transforms/Scalar/ConstantProp.cpp
index 6ab97808a6..babcde0f0c 100644
--- a/lib/Transforms/Scalar/ConstantProp.cpp
+++ b/lib/Transforms/Scalar/ConstantProp.cpp
@@ -54,6 +54,29 @@ ConstantFoldUnaryInst(Method *M, Method::inst_iterator &DI,
}
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,
ConstPoolVal *D1, ConstPoolVal *D2) {
@@ -142,6 +165,10 @@ ConstantFoldInstruction(Method *M, Method::inst_iterator &II) {
if (D1 && D2)
return ConstantFoldBinaryInst(M, II, cast<BinaryOperator>(Inst), D1, D2);
+ } else if (CastInst *CI = dyn_cast<CastInst>(Inst)) {
+ ConstPoolVal *D = dyn_cast<ConstPoolVal>(CI->getOperand(0));
+ if (D) return ConstantFoldCast(M, II, CI, D);
+
} else if (UnaryOperator *UInst = dyn_cast<UnaryOperator>(Inst)) {
ConstPoolVal *D = dyn_cast<ConstPoolVal>(UInst->getOperand(0));
if (D) return ConstantFoldUnaryInst(M, II, UInst, D);