summaryrefslogtreecommitdiff
path: root/lib/Analysis
diff options
context:
space:
mode:
authorBenjamin Kramer <benny.kra@googlemail.com>2013-04-13 12:53:18 +0000
committerBenjamin Kramer <benny.kra@googlemail.com>2013-04-13 12:53:18 +0000
commit8848680ce0ab416cb646d0a03aa6f4f6f25e7623 (patch)
tree8cb969d787c126e7e54de60aa90c7977169fcffc /lib/Analysis
parentb99c995825a49f0da5af40ee1b61269deb8994b5 (diff)
Fix a scalability issue with complex ConstantExprs.
This is basically the same fix in three different places. We use a set to avoid walking the whole tree of a big ConstantExprs multiple times. For example: (select cmp, (add big_expr 1), (add big_expr 2)) We don't want to visit big_expr twice here, it may consist of thousands of nodes. The testcase exercises this by creating an insanely large ConstantExprs out of a loop. It's questionable if the optimizer should ever create those, but this can be triggered with real C code. Fixes PR15714. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@179458 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Analysis')
-rw-r--r--lib/Analysis/ConstantFolding.cpp36
1 files changed, 24 insertions, 12 deletions
diff --git a/lib/Analysis/ConstantFolding.cpp b/lib/Analysis/ConstantFolding.cpp
index 09d7608c51d..b5286e8f29d 100644
--- a/lib/Analysis/ConstantFolding.cpp
+++ b/lib/Analysis/ConstantFolding.cpp
@@ -17,6 +17,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/ConstantFolding.h"
+#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Analysis/ValueTracking.h"
@@ -880,19 +881,20 @@ Constant *llvm::ConstantFoldInstruction(Instruction *I,
return ConstantFoldInstOperands(I->getOpcode(), I->getType(), Ops, TD, TLI);
}
-/// ConstantFoldConstantExpression - Attempt to fold the constant expression
-/// using the specified DataLayout. If successful, the constant result is
-/// result is returned, if not, null is returned.
-Constant *llvm::ConstantFoldConstantExpression(const ConstantExpr *CE,
- const DataLayout *TD,
- const TargetLibraryInfo *TLI) {
- SmallVector<Constant*, 8> Ops;
- for (User::const_op_iterator i = CE->op_begin(), e = CE->op_end();
- i != e; ++i) {
+static Constant *
+ConstantFoldConstantExpressionImpl(const ConstantExpr *CE, const DataLayout *TD,
+ const TargetLibraryInfo *TLI,
+ SmallPtrSet<ConstantExpr *, 4> &FoldedOps) {
+ SmallVector<Constant *, 8> Ops;
+ for (User::const_op_iterator i = CE->op_begin(), e = CE->op_end(); i != e;
+ ++i) {
Constant *NewC = cast<Constant>(*i);
- // Recursively fold the ConstantExpr's operands.
- if (ConstantExpr *NewCE = dyn_cast<ConstantExpr>(NewC))
- NewC = ConstantFoldConstantExpression(NewCE, TD, TLI);
+ // Recursively fold the ConstantExpr's operands. If we have already folded
+ // a ConstantExpr, we don't have to process it again.
+ if (ConstantExpr *NewCE = dyn_cast<ConstantExpr>(NewC)) {
+ if (FoldedOps.insert(NewCE))
+ NewC = ConstantFoldConstantExpressionImpl(NewCE, TD, TLI, FoldedOps);
+ }
Ops.push_back(NewC);
}
@@ -902,6 +904,16 @@ Constant *llvm::ConstantFoldConstantExpression(const ConstantExpr *CE,
return ConstantFoldInstOperands(CE->getOpcode(), CE->getType(), Ops, TD, TLI);
}
+/// ConstantFoldConstantExpression - Attempt to fold the constant expression
+/// using the specified DataLayout. If successful, the constant result is
+/// result is returned, if not, null is returned.
+Constant *llvm::ConstantFoldConstantExpression(const ConstantExpr *CE,
+ const DataLayout *TD,
+ const TargetLibraryInfo *TLI) {
+ SmallPtrSet<ConstantExpr *, 4> FoldedOps;
+ return ConstantFoldConstantExpressionImpl(CE, TD, TLI, FoldedOps);
+}
+
/// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
/// specified opcode and operands. If successful, the constant result is
/// returned, if not, null is returned. Note that this function can fail when