Skip to content

Commit 55d46b5

Browse files
committed
llvmir2hll/copy_propagation: #479, fix UD ordering to prevent inf cycles
This is preventing the infinite loop by storing already seen values to sets. It is working, but is not a very elegant solution.
1 parent 36e138d commit 55d46b5

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/llvmir2hll/optimizer/optimizers/copy_propagation_optimizer.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ int compareStmtsInDUChains(const ShPtr<Statement> &s1, const ShPtr<Statement> &s
9191
auto ordered(const DefUseChains::DefUseChain &du) {
9292
std::vector<std::pair<DefUseChains::StmtVarPair, StmtSet>> v(du.begin(), du.end());
9393
std::sort(v.begin(), v.end(), [](const auto &p1, const auto &p2) {
94+
auto v1 = p1.first.second;
95+
auto v2 = p2.first.second;
96+
auto s1 = p1.first.first;
97+
auto s2 = p2.first.first;
98+
99+
// We are comparing the same variable in the same statement.
100+
if (v1 == v2 && s1 == s2) {
101+
return false;
102+
}
103+
94104
// Begin by checking the sizes of the uses because it's faster than
95105
// checking the statements and variables.
96106
auto uses1Size = p1.second.size();
@@ -100,17 +110,15 @@ auto ordered(const DefUseChains::DefUseChain &du) {
100110
}
101111

102112
// The uses sets have the same size, so continue to variables.
103-
const auto &v1Name = p1.first.second->getName();
104-
const auto &v2Name = p2.first.second->getName();
113+
const auto &v1Name = v1->getName();
114+
const auto &v2Name = v2->getName();
105115
auto cmpResult = v1Name.compare(v2Name);
106116
if (cmpResult != 0) {
107117
return cmpResult < 0;
108118
}
109119

110120
// Check the statements. We have to use getTextRepr() because
111121
// there is no other way of comparing the statements.
112-
auto s1 = p1.first.first;
113-
auto s2 = p2.first.first;
114122
cmpResult = compareStmtsInDUChains(s1, s2);
115123
if (cmpResult != 0) {
116124
return cmpResult < 0;
@@ -139,6 +147,8 @@ auto ordered(const DefUseChains::DefUseChain &du) {
139147
return cmpResult < 0;
140148
}
141149
// Predecessors:
150+
std::set<ShPtr<Statement>> s1Seen;
151+
std::set<ShPtr<Statement>> s2Seen;
142152
while (true) {
143153
const auto &s1PredSize = s1->getNumberOfPredecessors();
144154
const auto &s2PredSize = s2->getNumberOfPredecessors();
@@ -151,6 +161,14 @@ auto ordered(const DefUseChains::DefUseChain &du) {
151161
if (cmpResult != 0) {
152162
return cmpResult < 0;
153163
}
164+
s1Seen.insert(s1);
165+
s2Seen.insert(s2);
166+
if (s1Seen.count(s1Pred)) {
167+
return false;
168+
}
169+
if (s2Seen.count(s2Pred)) {
170+
return false;
171+
}
154172
s1 = s1Pred;
155173
s2 = s2Pred;
156174
} else {

0 commit comments

Comments
 (0)