summaryrefslogtreecommitdiff
path: root/lib/Analysis/ConstantFolding.cpp
diff options
context:
space:
mode:
authorDuncan Sands <baldrick@free.fr>2010-11-23 10:16:18 +0000
committerDuncan Sands <baldrick@free.fr>2010-11-23 10:16:18 +0000
commitb9b369fa9983843c4ed77b3a35b6e9b7933955bb (patch)
tree0578f35f5923293f1c19353e16b42aadf76fc1ac /lib/Analysis/ConstantFolding.cpp
parenta9d4281cc0557ef679b7917e0741ddb01651dab1 (diff)
downloadllvm-b9b369fa9983843c4ed77b3a35b6e9b7933955bb.tar.gz
llvm-b9b369fa9983843c4ed77b3a35b6e9b7933955bb.tar.bz2
llvm-b9b369fa9983843c4ed77b3a35b6e9b7933955bb.tar.xz
Clarify that constant folding of instructions applies when all operands
are constant. There was in fact one exception to this (phi nodes) - so remove that exception (InstructionSimplify handles this so there should be no loss). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120015 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Analysis/ConstantFolding.cpp')
-rw-r--r--lib/Analysis/ConstantFolding.cpp21
1 files changed, 11 insertions, 10 deletions
diff --git a/lib/Analysis/ConstantFolding.cpp b/lib/Analysis/ConstantFolding.cpp
index 82b406f204..9d92853c43 100644
--- a/lib/Analysis/ConstantFolding.cpp
+++ b/lib/Analysis/ConstantFolding.cpp
@@ -695,22 +695,23 @@ static Constant *SymbolicallyEvaluateGEP(Constant *const *Ops, unsigned NumOps,
// Constant Folding public APIs
//===----------------------------------------------------------------------===//
-
-/// ConstantFoldInstruction - Attempt to constant fold the specified
-/// instruction. If successful, the constant result is returned, if not, null
-/// is returned. Note that this function can only fail when attempting to fold
-/// instructions like loads and stores, which have no constant expression form.
-///
+/// ConstantFoldInstruction - Try to constant fold the specified instruction.
+/// If successful, the constant result is returned, if not, null is returned.
+/// Note that this fails if not all of the operands are constant. Otherwise,
+/// this function can only fail when attempting to fold instructions like loads
+/// and stores, which have no constant expression form.
Constant *llvm::ConstantFoldInstruction(Instruction *I, const TargetData *TD) {
- // Handle PHI nodes specially here...
+ // Handle PHI nodes quickly here...
if (PHINode *PN = dyn_cast<PHINode>(I)) {
Constant *CommonValue = 0;
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
Value *Incoming = PN->getIncomingValue(i);
- // If the incoming value is equal to the phi node itself or is undef then
- // skip it.
- if (Incoming == PN || isa<UndefValue>(Incoming))
+ // If the incoming value is undef then skip it. Note that while we could
+ // skip the value if it is equal to the phi node itself we choose not to
+ // because that would break the rule that constant folding only applies if
+ // 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.