From 7390d96b1f3a49976eaee11e3d6a1848773d566d Mon Sep 17 00:00:00 2001 From: Egor Mayorov Date: Wed, 1 Apr 2026 17:12:20 +0300 Subject: [PATCH] change reordering logic --- .../MoveOperators/move_operators.cpp | 52 ++++++++++++++----- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/src/Transformations/MoveOperators/move_operators.cpp b/src/Transformations/MoveOperators/move_operators.cpp index 0793913..c68a9c4 100644 --- a/src/Transformations/MoveOperators/move_operators.cpp +++ b/src/Transformations/MoveOperators/move_operators.cpp @@ -470,6 +470,8 @@ static bool reorderOperatorsInBasicBlockUsingDeps(SAPFOR::BasicBlock* bb, const // as close as possible after its last dependency (if any). const auto depsMap = analyzeBasicBlockIntraDependencies(bb); vector order = ops; + const vector originalOrder = ops; + const int nOrig = (int)originalOrder.size(); auto indexIn = [](const vector& v, SgStatement* s) -> int { @@ -479,37 +481,59 @@ static bool reorderOperatorsInBasicBlockUsingDeps(SAPFOR::BasicBlock* bb, const return -1; }; + auto indexInOriginal = [&](SgStatement* s) -> int + { + return indexIn(originalOrder, s); + }; + for (SgStatement* s : ops) { auto itDeps = depsMap.find(s); if (itDeps == depsMap.end() || itDeps->second.empty()) continue; + int lastDepOrigIdx = -1; + for (SgStatement* dep : itDeps->second) + { + const int j = indexInOriginal(dep); + if (j >= 0) + lastDepOrigIdx = max(lastDepOrigIdx, j); + } + if (lastDepOrigIdx < 0) + continue; + + SgStatement* successor = nullptr; + if (lastDepOrigIdx + 1 < nOrig) + successor = originalOrder[lastDepOrigIdx + 1]; + int posS = indexIn(order, s); if (posS < 0) continue; - int lastDepIdx = -1; - for (SgStatement* dep : itDeps->second) + if (successor == nullptr) { - const int j = indexIn(order, dep); - if (j >= 0) - lastDepIdx = max(lastDepIdx, j); + if (posS == (int)order.size() - 1) + continue; + order.erase(order.begin() + posS); + order.push_back(s); + continue; } - if (lastDepIdx < 0) + + if (successor == s) continue; - if (posS == lastDepIdx + 1) + const int posSucc = indexIn(order, successor); + if (posSucc < 0) + continue; + + if (posS + 1 == posSucc) continue; order.erase(order.begin() + posS); - - int lp = lastDepIdx; - if (posS < lastDepIdx) - lp = lastDepIdx - 1; - - const int insertAt = lp + 1; - order.insert(order.begin() + insertAt, s); + const int posSucc2 = indexIn(order, successor); + if (posSucc2 < 0) + printInternalError(convertFileName(__FILE__).c_str(), __LINE__); + order.insert(order.begin() + posSucc2, s); } bool changed = false;