Skip to content

Commit 1373f20

Browse files
committed
[ConstraintSystem] Remove last variable, use move instead of copy. (NFC)
At the moment, a large amount of time is spent construction vectors by pushing back all elements except the first variable. For large inputs, such as discussed in https://reviews.llvm.org/D135915#4057050 this can result in excessive compile-time. Instead, it is more efficient to remove the last variable. Then the original vector can be re-used by simply popping the last element and then moving the contents to the new system. This improves time spent in ConstraintElimination for the linked reproducer from ~43s to ~3s.
1 parent 735f117 commit 1373f20

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

llvm/lib/Analysis/ConstraintSystem.cpp

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,13 @@ bool ConstraintSystem::eliminateUsingFM() {
3232

3333
unsigned NumConstraints = Constraints.size();
3434
uint32_t NewGCD = 1;
35-
// FIXME do not use copy
35+
unsigned LastIdx = NumVariables - 1;
36+
3637
for (unsigned R1 = 0; R1 < NumConstraints; R1++) {
37-
if (Constraints[R1][1] == 0) {
38-
SmallVector<int64_t, 8> NR;
39-
NR.push_back(Constraints[R1][0]);
40-
for (unsigned i = 2; i < NumVariables; i++) {
41-
NR.push_back(Constraints[R1][i]);
42-
}
43-
NewSystem.push_back(std::move(NR));
38+
SmallVector<int64_t, 8> &Row1 = Constraints[R1];
39+
if (Row1[LastIdx] == 0) {
40+
Row1.pop_back();
41+
NewSystem.push_back(std::move(Row1));
4442
continue;
4543
}
4644

@@ -50,29 +48,26 @@ bool ConstraintSystem::eliminateUsingFM() {
5048
continue;
5149

5250
// FIXME: can we do better than just dropping things here?
53-
if (Constraints[R2][1] == 0)
51+
if (Constraints[R2][LastIdx] == 0)
5452
continue;
5553

56-
if ((Constraints[R1][1] < 0 && Constraints[R2][1] < 0) ||
57-
(Constraints[R1][1] > 0 && Constraints[R2][1] > 0))
54+
if ((Constraints[R1][LastIdx] < 0 && Constraints[R2][LastIdx] < 0) ||
55+
(Constraints[R1][LastIdx] > 0 && Constraints[R2][LastIdx] > 0))
5856
continue;
5957

6058
unsigned LowerR = R1;
6159
unsigned UpperR = R2;
62-
if (Constraints[UpperR][1] < 0)
60+
if (Constraints[UpperR][LastIdx] < 0)
6361
std::swap(LowerR, UpperR);
6462

6563
SmallVector<int64_t, 8> NR;
66-
for (unsigned I = 0; I < NumVariables; I++) {
67-
if (I == 1)
68-
continue;
69-
64+
for (unsigned I = 0; I < LastIdx; I++) {
7065
int64_t M1, M2, N;
7166
if (MulOverflow(Constraints[UpperR][I],
72-
((-1) * Constraints[LowerR][1] / GCD), M1))
67+
((-1) * Constraints[LowerR][LastIdx] / GCD), M1))
7368
return false;
7469
if (MulOverflow(Constraints[LowerR][I],
75-
(Constraints[UpperR][1] / GCD), M2))
70+
(Constraints[UpperR][LastIdx] / GCD), M2))
7671
return false;
7772
if (AddOverflow(M1, M2, N))
7873
return false;

0 commit comments

Comments
 (0)