Skip to content

Commit c41b5b5

Browse files
author
v01dxyz
committed
[ConstraintSystem] FM Elimination loop code simplification and comments
Simplify the code of the loop performing the element-wise multiplication as there were strange patterns. I added some comments to help understand the intent of the code for some external viewers.
1 parent febd89c commit c41b5b5

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

llvm/lib/Analysis/ConstraintSystem.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,62 +52,62 @@ bool ConstraintSystem::eliminateUsingFM() {
5252
for (unsigned R1 = 0; R1 < NumRemainingConstraints; R1++) {
5353
// FIXME do not use copy
5454
for (unsigned R2 = R1 + 1; R2 < NumRemainingConstraints; R2++) {
55-
if (R1 == R2)
56-
continue;
57-
5855
int64_t UpperLast = getLastCoefficient(RemainingRows[R2], LastIdx);
5956
int64_t LowerLast = getLastCoefficient(RemainingRows[R1], LastIdx);
6057
assert(
6158
UpperLast != 0 && LowerLast != 0 &&
6259
"RemainingRows should only contain rows where the variable is != 0");
6360

61+
// ensure signs are different then swap if necessary to only
62+
// deal with UpperLast > 0
6463
if ((LowerLast < 0 && UpperLast < 0) || (LowerLast > 0 && UpperLast > 0))
6564
continue;
66-
6765
unsigned LowerR = R1;
6866
unsigned UpperR = R2;
6967
if (UpperLast < 0) {
7068
std::swap(LowerR, UpperR);
7169
std::swap(LowerLast, UpperLast);
7270
}
7371

72+
// The following loop does the element-wise operation on sparse
73+
// vectors:
74+
//
75+
// NR = - UpperRow * LowerLast + LowerRow * UpperLast
7476
SmallVector<Entry, 8> NR;
7577
unsigned IdxUpper = 0;
7678
unsigned IdxLower = 0;
7779
auto &LowerRow = RemainingRows[LowerR];
7880
auto &UpperRow = RemainingRows[UpperR];
79-
while (true) {
80-
if (IdxUpper >= UpperRow.size() || IdxLower >= LowerRow.size())
81-
break;
81+
82+
while (IdxUpper < UpperRow.size() && IdxLower < LowerRow.size()) {
8283
int64_t M1, M2, N;
8384
int64_t UpperV = 0;
8485
int64_t LowerV = 0;
85-
uint16_t CurrentId = std::numeric_limits<uint16_t>::max();
86-
if (IdxUpper < UpperRow.size()) {
87-
CurrentId = std::min(UpperRow[IdxUpper].Id, CurrentId);
88-
}
89-
if (IdxLower < LowerRow.size()) {
90-
CurrentId = std::min(LowerRow[IdxLower].Id, CurrentId);
91-
}
86+
uint16_t CurrentId =
87+
std::min(UpperRow[IdxUpper].Id, LowerRow[IdxLower].Id);
9288

93-
if (IdxUpper < UpperRow.size() && UpperRow[IdxUpper].Id == CurrentId) {
89+
if (UpperRow[IdxUpper].Id == CurrentId) {
9490
UpperV = UpperRow[IdxUpper].Coefficient;
95-
IdxUpper++;
91+
IdxUpper += 1;
92+
}
93+
if (LowerRow[IdxLower].Id == CurrentId) {
94+
LowerV = LowerRow[IdxLower].Coefficient;
95+
IdxLower += 1;
9696
}
9797

9898
if (MulOverflow(UpperV, -1 * LowerLast, M1))
9999
return false;
100-
if (IdxLower < LowerRow.size() && LowerRow[IdxLower].Id == CurrentId) {
101-
LowerV = LowerRow[IdxLower].Coefficient;
102-
IdxLower++;
103-
}
104100

105101
if (MulOverflow(LowerV, UpperLast, M2))
106102
return false;
103+
107104
if (AddOverflow(M1, M2, N))
108105
return false;
106+
107+
// useless to add a 0 to a sparse vector
109108
if (N == 0)
110109
continue;
110+
111111
NR.emplace_back(N, CurrentId);
112112
}
113113
if (NR.empty())

0 commit comments

Comments
 (0)