From a6cd16522e48468cc3e7fec6ed85e123c487b0b2 Mon Sep 17 00:00:00 2001 From: "jacob@baines@darkwolfsolutions.com" Date: Fri, 30 Oct 2020 07:32:41 -0700 Subject: [PATCH] Fix infinite loop in copy propagation optimizer --- .../optimizers/copy_propagation_optimizer.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/llvmir2hll/optimizer/optimizers/copy_propagation_optimizer.cpp b/src/llvmir2hll/optimizer/optimizers/copy_propagation_optimizer.cpp index 8fb68191f..4bfb1203a 100644 --- a/src/llvmir2hll/optimizer/optimizers/copy_propagation_optimizer.cpp +++ b/src/llvmir2hll/optimizer/optimizers/copy_propagation_optimizer.cpp @@ -841,14 +841,14 @@ void CopyPropagationOptimizer::handleCaseInductionVariable( // Other value is undefined before the other definition. bool ok = false; - auto prev = commonOtherDef->getUniquePredecessor(); - while (prev) { + std::set> visited; + for (auto prev = commonOtherDef->getUniquePredecessor(); prev && visited.insert(prev).second; + prev = prev->getUniquePredecessor()) { auto vds = cast(prev); if (vds && vds->getVar() == otherValue && vds->getInitializer() == nullptr) { ok = true; break; } - prev = prev->getUniquePredecessor(); } // Other value may be in another BB. // In such a case, check for very specific and restrictive pattern. @@ -1043,8 +1043,9 @@ void CopyPropagationOptimizer::handleCaseInductionVariable2( // y is undefined before xZero. // y = undef bool ok = false; - auto prev = xZero->getUniquePredecessor(); - while (prev) { + std::set> visited; + for (auto prev = xZero->getUniquePredecessor(); prev && visited.insert(prev).second; + prev = prev->getUniquePredecessor()) { auto vds = cast(prev); if (vds && vds->getVar() == y) { ok = (vds->getInitializer() == nullptr); @@ -1054,7 +1055,6 @@ void CopyPropagationOptimizer::handleCaseInductionVariable2( if (as && as->getLhs() == y) { break; } - prev = prev->getUniquePredecessor(); } if (!ok) { LOG << "\t" << "end 10" << std::endl;