summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Analysis/ConstantFolding.cpp
diff options
context:
space:
mode:
authorDuncan Sands <baldrick@free.fr>2010-11-14 12:53:18 +0000
committerDuncan Sands <baldrick@free.fr>2010-11-14 12:53:18 +0000
commit1d27f01210590b2e0e510aad39ac1006cd6f7e3d (patch)
treee1efd2623fbb9632ada16e25431821e11c3cb5bd /llvm/lib/Analysis/ConstantFolding.cpp
parentdddfdc81efd6d9121cf4b6a2341a4d4f82c337a6 (diff)
downloadbcm5719-llvm-1d27f01210590b2e0e510aad39ac1006cd6f7e3d.tar.gz
bcm5719-llvm-1d27f01210590b2e0e510aad39ac1006cd6f7e3d.zip
Boost the power of phi node constant folding slightly: if all
operands are the phi node itself or undef, then return undef. This logic already existed at a higher level so in practice it shouldn't make the slightest difference. Note that this code could be replaced by a call to PN->hasConstantValue(). However since we bail out the moment we see a non-constant operand, it is more efficient to have a specialized version of that logic. llvm-svn: 119041
Diffstat (limited to 'llvm/lib/Analysis/ConstantFolding.cpp')
-rw-r--r--llvm/lib/Analysis/ConstantFolding.cpp30
1 files changed, 18 insertions, 12 deletions
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index f512dd69a33..f2e89a773e7 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -695,20 +695,26 @@ static Constant *SymbolicallyEvaluateGEP(Constant *const *Ops, unsigned NumOps,
/// instructions like loads and stores, which have no constant expression form.
///
Constant *llvm::ConstantFoldInstruction(Instruction *I, const TargetData *TD) {
+ // Handle PHI nodes specially here...
if (PHINode *PN = dyn_cast<PHINode>(I)) {
- if (PN->getNumIncomingValues() == 0)
- return UndefValue::get(PN->getType());
-
- Constant *Result = dyn_cast<Constant>(PN->getIncomingValue(0));
- if (Result == 0) return 0;
-
- // Handle PHI nodes specially here...
- for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i)
- if (PN->getIncomingValue(i) != Result && PN->getIncomingValue(i) != PN)
- return 0; // Not all the same incoming constants...
+ 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))
+ continue;
+ // If the incoming value is not a constant, or is a different constant to
+ // the one we saw previously, then give up.
+ Constant *C = dyn_cast<Constant>(Incoming);
+ if (!C || (CommonValue && C != CommonValue))
+ return 0;
+ CommonValue = C;
+ }
- // If we reach here, all incoming values are the same constant.
- return Result;
+ // If we reach here, all incoming values are the same constant or undef.
+ return CommonValue ? CommonValue : UndefValue::get(PN->getType());
}
// Scan the operand list, checking to see if they are all constants, if so,
OpenPOWER on IntegriCloud