summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Analysis/ConstantFolding.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2007-08-11 23:49:01 +0000
committerChris Lattner <sabre@nondot.org>2007-08-11 23:49:01 +0000
commit460e34afeda6256070e19960ceec4408e64216ae (patch)
tree4090e769d86fe6d514fb5adf8114511e87b1d1f9 /llvm/lib/Analysis/ConstantFolding.cpp
parent99c8ee29778903b93710ddc5631f93f86b10eaa6 (diff)
downloadbcm5719-llvm-460e34afeda6256070e19960ceec4408e64216ae.tar.gz
bcm5719-llvm-460e34afeda6256070e19960ceec4408e64216ae.zip
constant fold ptrtoint(inttoptr) with target data when available. This allows
us to fold the entry block of PR1602 to false instead of: br i1 icmp eq (i32 and (i32 ptrtoint (void (%struct.S*)* inttoptr (i64 1 to void (%struct.S*)*) to i32), i32 1), i32 0), label %cond_next, label %cond_true llvm-svn: 41023
Diffstat (limited to 'llvm/lib/Analysis/ConstantFolding.cpp')
-rw-r--r--llvm/lib/Analysis/ConstantFolding.cpp19
1 files changed, 17 insertions, 2 deletions
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 9599a900f26..70ce34969c9 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -217,6 +217,23 @@ Constant *llvm::ConstantFoldInstOperands(const Instruction* I,
case Instruction::FCmp:
return ConstantExpr::getCompare(cast<CmpInst>(I)->getPredicate(), Ops[0],
Ops[1]);
+ case Instruction::PtrToInt:
+ // If the input is a inttoptr, eliminate the pair. This requires knowing
+ // the width of a pointer, so it can't be done in ConstantExpr::getCast.
+ if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) {
+ if (TD && CE->getOpcode() == Instruction::IntToPtr) {
+ Constant *Input = CE->getOperand(0);
+ unsigned InWidth = Input->getType()->getPrimitiveSizeInBits();
+ Constant *Mask =
+ ConstantInt::get(APInt::getLowBitsSet(InWidth,
+ TD->getPointerSizeInBits()));
+ Input = ConstantExpr::getAnd(Input, Mask);
+ // Do a zext or trunc to get to the dest size.
+ return ConstantExpr::getIntegerCast(Input, I->getType(), false);
+ }
+ }
+ // FALL THROUGH.
+ case Instruction::IntToPtr:
case Instruction::Trunc:
case Instruction::ZExt:
case Instruction::SExt:
@@ -226,8 +243,6 @@ Constant *llvm::ConstantFoldInstOperands(const Instruction* I,
case Instruction::SIToFP:
case Instruction::FPToUI:
case Instruction::FPToSI:
- case Instruction::PtrToInt:
- case Instruction::IntToPtr:
case Instruction::BitCast:
return ConstantExpr::getCast(Opc, Ops[0], DestTy);
case Instruction::Select:
OpenPOWER on IntegriCloud