summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Transforms/Utils
diff options
context:
space:
mode:
authorSanjay Patel <spatel@rotateright.com>2016-01-27 19:22:45 +0000
committerSanjay Patel <spatel@rotateright.com>2016-01-27 19:22:45 +0000
commit5264cc772c4075964abf40dbeb80ea29cfcd8cd4 (patch)
treea1dad9aa622b32a933950c8d0995f3d1e487fe6d /llvm/lib/Transforms/Utils
parent3fe604f89fccfe1534eeaf4b4e328185d01659f6 (diff)
downloadbcm5719-llvm-5264cc772c4075964abf40dbeb80ea29cfcd8cd4.tar.gz
bcm5719-llvm-5264cc772c4075964abf40dbeb80ea29cfcd8cd4.zip
[SimplifyCFG] limit recursion depth when speculating instructions (PR26308)
This is a fix for: https://llvm.org/bugs/show_bug.cgi?id=26308 With the switch to using the TTI cost model in: http://reviews.llvm.org/rL228826 ...it became possible to hit a zero-cost cycle of instructions (gep -> phi -> gep...), so we need a cap for the recursion in DominatesMergePoint(). A recursion depth parameter was already added for a different reason in: http://reviews.llvm.org/rL255660 ...so we can just set a limit for it. I pulled "10" out of the air and made it an independent parameter that we can play with. It might be higher than it needs to be given the currently low default value of PHINodeFoldingThreshold (2). That's the starting cost value that we enter the recursion with, and most instructions have cost set to TCC_Basic (1), so I don't think we're going to speculate more than 2 instructions with the current parameters. As noted in the review and the TODO comment, we can do better than just limiting recursion depth. Differential Revision: http://reviews.llvm.org/D16637 llvm-svn: 258971
Diffstat (limited to 'llvm/lib/Transforms/Utils')
-rw-r--r--llvm/lib/Transforms/Utils/SimplifyCFG.cpp12
1 files changed, 12 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index fefe448d239..8370968065d 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -90,6 +90,11 @@ static cl::opt<bool> SpeculateOneExpensiveInst(
cl::desc("Allow exactly one expensive instruction to be speculatively "
"executed"));
+static cl::opt<unsigned> MaxSpeculationDepth(
+ "max-speculation-depth", cl::Hidden, cl::init(10),
+ cl::desc("Limit maximum recursion depth when calculating costs of "
+ "speculatively executed instructions"));
+
STATISTIC(NumBitMaps, "Number of switch instructions turned into bitmaps");
STATISTIC(NumLinearMaps, "Number of switch instructions turned into linear mapping");
STATISTIC(NumLookupTables, "Number of switch instructions turned into lookup tables");
@@ -269,6 +274,13 @@ static bool DominatesMergePoint(Value *V, BasicBlock *BB,
unsigned &CostRemaining,
const TargetTransformInfo &TTI,
unsigned Depth = 0) {
+ // It is possible to hit a zero-cost cycle (phi/gep instructions for example),
+ // so limit the recursion depth.
+ // TODO: While this recursion limit does prevent pathological behavior, it
+ // would be better to track visited instructions to avoid cycles.
+ if (Depth == MaxSpeculationDepth)
+ return false;
+
Instruction *I = dyn_cast<Instruction>(V);
if (!I) {
// Non-instructions all dominate instructions, but not all constantexprs
OpenPOWER on IntegriCloud