#include "llvm/ADT/APInt.h"
#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include <string>
bool mayHaveSolutionImpl();
public:
- void addVariableRow(const SmallVector<int64_t, 8> &R) {
+ bool addVariableRow(const SmallVector<int64_t, 8> &R) {
assert(Constraints.empty() || R.size() == Constraints.back().size());
+ // If all variable coefficients are 0, the constraint does not provide any
+ // usable information.
+ if (all_of(makeArrayRef(R).drop_front(1), [](int64_t C) { return C == 0; }))
+ return false;
+
for (const auto &C : R) {
auto A = std::abs(C);
GCD = APIntOps::GreatestCommonDivisor({32, (uint32_t)A}, {32, GCD})
.getZExtValue();
}
Constraints.push_back(R);
+ return true;
}
- void addVariableRowFill(const SmallVector<int64_t, 8> &R) {
+ bool addVariableRowFill(const SmallVector<int64_t, 8> &R) {
for (auto &CR : Constraints) {
while (CR.size() != R.size())
CR.push_back(0);
}
- addVariableRow(R);
+ return addVariableRow(R);
}
/// Returns true if there may be a solution for the constraints in the system.
}
bool ConstraintSystem::isConditionImplied(SmallVector<int64_t, 8> R) {
+ // If all variable coefficients are 0, we have 'C >= 0'. If the constant is >=
+ // 0, R is always true, regardless of the system.
+ if (all_of(makeArrayRef(R).drop_front(1), [](int64_t C) { return C == 0; }))
+ return R[0] >= 0;
+
// If there is no solution with the negation of R added to the system, the
// condition must hold based on the existing constraints.
R = ConstraintSystem::negate(R);
if (CB.Not)
R = ConstraintSystem::negate(R);
- CS.addVariableRowFill(R);
- DFSInStack.emplace_back(CB.NumIn, CB.NumOut, CB.Condition, CB.Not);
+ // If R has been added to the system, queue it for removal once it goes
+ // out-of-scope.
+ if (CS.addVariableRowFill(R))
+ DFSInStack.emplace_back(CB.NumIn, CB.NumOut, CB.Condition, CB.Not);
}
return Changed;
--- /dev/null
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -constraint-elimination -S %s | FileCheck %s
+
+; Make sure constraints where all variable coefficients are 0 are handled
+; properly.
+
+define i1 @test_1_always_false(i32 %A, i32 %B) {
+; CHECK-LABEL: @test_1_always_false(
+; CHECK-NEXT: [[C_1:%.*]] = icmp ugt i32 [[A:%.*]], [[A]]
+; CHECK-NEXT: br i1 [[C_1]], label [[IF_END_I16:%.*]], label [[IF_THEN_I10:%.*]]
+; CHECK: if.then.i10:
+; CHECK-NEXT: ret i1 false
+; CHECK: if.end.i16:
+; CHECK-NEXT: [[C_2:%.*]] = icmp ugt i32 [[A]], [[A]]
+; CHECK-NEXT: ret i1 false
+;
+ %c.1 = icmp ugt i32 %A, %A
+ br i1 %c.1, label %if.end.i16, label %if.then.i10
+
+if.then.i10:
+ ret i1 false
+
+if.end.i16:
+ %c.2 = icmp ugt i32 %A, %A
+ ret i1 %c.2
+}
+
+define i1 @test_2_always_true(i32 %A, i32 %B) {
+; CHECK-LABEL: @test_2_always_true(
+; CHECK-NEXT: [[C_1:%.*]] = icmp uge i32 [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT: br i1 [[C_1]], label [[IF_END_I16:%.*]], label [[IF_THEN_I10:%.*]]
+; CHECK: if.then.i10:
+; CHECK-NEXT: ret i1 false
+; CHECK: if.end.i16:
+; CHECK-NEXT: [[C_2:%.*]] = icmp uge i32 [[A]], [[A]]
+; CHECK-NEXT: ret i1 true
+;
+ %c.1 = icmp uge i32 %A, %B
+ br i1 %c.1, label %if.end.i16, label %if.then.i10
+
+if.then.i10:
+ ret i1 false
+
+if.end.i16:
+ %c.2 = icmp uge i32 %A, %A
+ ret i1 %c.2
+}