@@ -91,6 +91,16 @@ int compareStmtsInDUChains(const ShPtr<Statement> &s1, const ShPtr<Statement> &s
91
91
auto ordered (const DefUseChains::DefUseChain &du) {
92
92
std::vector<std::pair<DefUseChains::StmtVarPair, StmtSet>> v (du.begin (), du.end ());
93
93
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
+
94
104
// Begin by checking the sizes of the uses because it's faster than
95
105
// checking the statements and variables.
96
106
auto uses1Size = p1.second .size ();
@@ -100,17 +110,15 @@ auto ordered(const DefUseChains::DefUseChain &du) {
100
110
}
101
111
102
112
// 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 ();
105
115
auto cmpResult = v1Name.compare (v2Name);
106
116
if (cmpResult != 0 ) {
107
117
return cmpResult < 0 ;
108
118
}
109
119
110
120
// Check the statements. We have to use getTextRepr() because
111
121
// there is no other way of comparing the statements.
112
- auto s1 = p1.first .first ;
113
- auto s2 = p2.first .first ;
114
122
cmpResult = compareStmtsInDUChains (s1, s2);
115
123
if (cmpResult != 0 ) {
116
124
return cmpResult < 0 ;
@@ -139,6 +147,8 @@ auto ordered(const DefUseChains::DefUseChain &du) {
139
147
return cmpResult < 0 ;
140
148
}
141
149
// Predecessors:
150
+ std::set<ShPtr<Statement>> s1Seen;
151
+ std::set<ShPtr<Statement>> s2Seen;
142
152
while (true ) {
143
153
const auto &s1PredSize = s1->getNumberOfPredecessors ();
144
154
const auto &s2PredSize = s2->getNumberOfPredecessors ();
@@ -151,6 +161,14 @@ auto ordered(const DefUseChains::DefUseChain &du) {
151
161
if (cmpResult != 0 ) {
152
162
return cmpResult < 0 ;
153
163
}
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
+ }
154
172
s1 = s1Pred;
155
173
s2 = s2Pred;
156
174
} else {
0 commit comments