summaryrefslogtreecommitdiff
path: root/lib/Analysis/ConstantFolding.cpp
diff options
context:
space:
mode:
authorDan Gohman <gohman@apple.com>2012-04-27 17:50:22 +0000
committerDan Gohman <gohman@apple.com>2012-04-27 17:50:22 +0000
commit03e091f0b5f43beee12170efc00bbab86ffeb0dc (patch)
tree97aeaf6db27484a1e58a35584582d14a6ac89f71 /lib/Analysis/ConstantFolding.cpp
parent04a09a461beb4ec629fe53e601b7665547ac35c3 (diff)
downloadllvm-03e091f0b5f43beee12170efc00bbab86ffeb0dc.tar.gz
llvm-03e091f0b5f43beee12170efc00bbab86ffeb0dc.tar.bz2
llvm-03e091f0b5f43beee12170efc00bbab86ffeb0dc.tar.xz
Reapply r155682, making constant folding more consistent, with a fix to work
properly with how the code handles all-undef PHI nodes. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@155721 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Analysis/ConstantFolding.cpp')
-rw-r--r--lib/Analysis/ConstantFolding.cpp27
1 files changed, 20 insertions, 7 deletions
diff --git a/lib/Analysis/ConstantFolding.cpp b/lib/Analysis/ConstantFolding.cpp
index 783c32e666..7ced848aa1 100644
--- a/lib/Analysis/ConstantFolding.cpp
+++ b/lib/Analysis/ConstantFolding.cpp
@@ -780,14 +780,21 @@ Constant *llvm::ConstantFoldInstruction(Instruction *I,
// all operands are constants.
if (isa<UndefValue>(Incoming))
continue;
- // If the incoming value is not a constant, or is a different constant to
- // the one we saw previously, then give up.
+ // If the incoming value is not a constant, then give up.
Constant *C = dyn_cast<Constant>(Incoming);
- if (!C || (CommonValue && C != CommonValue))
+ if (!C)
+ return 0;
+ // Fold the PHI's operands.
+ if (ConstantExpr *NewC = dyn_cast<ConstantExpr>(C))
+ C = ConstantFoldConstantExpression(NewC, TD, TLI);
+ // If the incoming value is a different constant to
+ // the one we saw previously, then give up.
+ if (CommonValue && C != CommonValue)
return 0;
CommonValue = C;
}
+
// If we reach here, all incoming values are the same constant or undef.
return CommonValue ? CommonValue : UndefValue::get(PN->getType());
}
@@ -795,12 +802,18 @@ Constant *llvm::ConstantFoldInstruction(Instruction *I,
// Scan the operand list, checking to see if they are all constants, if so,
// hand off to ConstantFoldInstOperands.
SmallVector<Constant*, 8> Ops;
- for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i)
- if (Constant *Op = dyn_cast<Constant>(*i))
- Ops.push_back(Op);
- else
+ for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i) {
+ Constant *Op = dyn_cast<Constant>(*i);
+ if (!Op)
return 0; // All operands not constant!
+ // Fold the Instruction's operands.
+ if (ConstantExpr *NewCE = dyn_cast<ConstantExpr>(Op))
+ Op = ConstantFoldConstantExpression(NewCE, TD, TLI);
+
+ Ops.push_back(Op);
+ }
+
if (const CmpInst *CI = dyn_cast<CmpInst>(I))
return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1],
TD, TLI);