summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Stuttard <david.stuttard@amd.com>2019-05-09 15:02:10 +0000
committerDavid Stuttard <david.stuttard@amd.com>2019-05-09 15:02:10 +0000
commit411488b11edf0fe16d8e839451d97f974255d00b (patch)
tree0ca56a12a15fae7b21c26baf0b68e34dd6aee244
parentf58a5c8803e3d06014031a4551ea9429db9662d9 (diff)
downloadbcm5719-llvm-411488b11edf0fe16d8e839451d97f974255d00b.tar.gz
bcm5719-llvm-411488b11edf0fe16d8e839451d97f974255d00b.zip
[CodeGenPrepare] Limit recursion depth for collectBitParts
Summary: Seeing some issues for windows debug pathological cases with collectBitParts recursion (1525 levels of recursion!) Setting the limit to 64 as this should be sufficient - passes all lit cases Subscribers: llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D61728 Change-Id: I7f44cdc6c1badf1c2ccbf1b0c4b6afe27ecb39a1 llvm-svn: 360347
-rw-r--r--llvm/lib/Transforms/Utils/Local.cpp24
1 files changed, 17 insertions, 7 deletions
diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index f9dc09e88e9..bf57fcdbdee 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -91,6 +91,10 @@ using namespace llvm::PatternMatch;
STATISTIC(NumRemoved, "Number of unreachable basic blocks removed");
+// Max recursion depth for collectBitParts used when detecting bswap and
+// bitreverse idioms
+static const unsigned BitPartRecursionMaxDepth = 64;
+
//===----------------------------------------------------------------------===//
// Local constant propagation.
//
@@ -2619,7 +2623,7 @@ struct BitPart {
/// does not invalidate internal references (std::map instead of DenseMap).
static const Optional<BitPart> &
collectBitParts(Value *V, bool MatchBSwaps, bool MatchBitReversals,
- std::map<Value *, Optional<BitPart>> &BPS) {
+ std::map<Value *, Optional<BitPart>> &BPS, int Depth) {
auto I = BPS.find(V);
if (I != BPS.end())
return I->second;
@@ -2627,13 +2631,19 @@ collectBitParts(Value *V, bool MatchBSwaps, bool MatchBitReversals,
auto &Result = BPS[V] = None;
auto BitWidth = cast<IntegerType>(V->getType())->getBitWidth();
+ // Prevent stack overflow by limiting the recursion depth
+ if (Depth == BitPartRecursionMaxDepth) {
+ LLVM_DEBUG(dbgs() << "collectBitParts max recursion depth reached.\n");
+ return Result;
+ }
+
if (Instruction *I = dyn_cast<Instruction>(V)) {
// If this is an or instruction, it may be an inner node of the bswap.
if (I->getOpcode() == Instruction::Or) {
auto &A = collectBitParts(I->getOperand(0), MatchBSwaps,
- MatchBitReversals, BPS);
+ MatchBitReversals, BPS, Depth + 1);
auto &B = collectBitParts(I->getOperand(1), MatchBSwaps,
- MatchBitReversals, BPS);
+ MatchBitReversals, BPS, Depth + 1);
if (!A || !B)
return Result;
@@ -2666,7 +2676,7 @@ collectBitParts(Value *V, bool MatchBSwaps, bool MatchBitReversals,
return Result;
auto &Res = collectBitParts(I->getOperand(0), MatchBSwaps,
- MatchBitReversals, BPS);
+ MatchBitReversals, BPS, Depth + 1);
if (!Res)
return Result;
Result = Res;
@@ -2698,7 +2708,7 @@ collectBitParts(Value *V, bool MatchBSwaps, bool MatchBitReversals,
return Result;
auto &Res = collectBitParts(I->getOperand(0), MatchBSwaps,
- MatchBitReversals, BPS);
+ MatchBitReversals, BPS, Depth + 1);
if (!Res)
return Result;
Result = Res;
@@ -2713,7 +2723,7 @@ collectBitParts(Value *V, bool MatchBSwaps, bool MatchBitReversals,
// If this is a zext instruction zero extend the result.
if (I->getOpcode() == Instruction::ZExt) {
auto &Res = collectBitParts(I->getOperand(0), MatchBSwaps,
- MatchBitReversals, BPS);
+ MatchBitReversals, BPS, Depth + 1);
if (!Res)
return Result;
@@ -2775,7 +2785,7 @@ bool llvm::recognizeBSwapOrBitReverseIdiom(
// Try to find all the pieces corresponding to the bswap.
std::map<Value *, Optional<BitPart>> BPS;
- auto Res = collectBitParts(I, MatchBSwaps, MatchBitReversals, BPS);
+ auto Res = collectBitParts(I, MatchBSwaps, MatchBitReversals, BPS, 0);
if (!Res)
return false;
auto &BitProvenance = Res->Provenance;
OpenPOWER on IntegriCloud