Skip to content

Commit 1d23d60

Browse files
author
Zain Jaffal
committed
[ConstraintElimination] Add function arguments to constraint system before solving
If there is an optimisation opportunity and the function argument hasn’t been added to constraint system through previous facts we fail to optimise it. It might be a good idea to start the constraint system with all the function arguments added to the system Reviewed By: fhahn Differential Revision: https://reviews.llvm.org/D144879
1 parent af2ed59 commit 1d23d60

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

llvm/include/llvm/Analysis/ConstraintSystem.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
namespace llvm {
2020

2121
class Value;
22-
2322
class ConstraintSystem {
2423
struct Entry {
2524
int64_t Coefficient;
@@ -68,8 +67,14 @@ class ConstraintSystem {
6867

6968
public:
7069
ConstraintSystem() {}
70+
ConstraintSystem(ArrayRef<Value *> FunctionArgs) {
71+
NumVariables += FunctionArgs.size();
72+
for (auto *Arg : FunctionArgs) {
73+
Value2Index.insert({Arg, Value2Index.size() + 1});
74+
}
75+
}
7176
ConstraintSystem(const DenseMap<Value *, unsigned> &Value2Index)
72-
: Value2Index(Value2Index) {}
77+
: NumVariables(Value2Index.size()), Value2Index(Value2Index) {}
7378

7479
bool addVariableRow(ArrayRef<int64_t> R) {
7580
assert(Constraints.empty() || R.size() == NumVariables);

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ class ConstraintInfo {
139139
const DataLayout &DL;
140140

141141
public:
142-
ConstraintInfo(const DataLayout &DL) : DL(DL) {}
142+
ConstraintInfo(const DataLayout &DL, ArrayRef<Value *> FunctionArgs)
143+
: UnsignedCS(FunctionArgs), SignedCS(FunctionArgs), DL(DL) {}
143144

144145
DenseMap<Value *, unsigned> &getValue2Index(bool Signed) {
145146
return Signed ? SignedCS.getValue2Index() : UnsignedCS.getValue2Index();
@@ -1087,8 +1088,10 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT,
10871088
OptimizationRemarkEmitter &ORE) {
10881089
bool Changed = false;
10891090
DT.updateDFSNumbers();
1090-
1091-
ConstraintInfo Info(F.getParent()->getDataLayout());
1091+
SmallVector<Value *> FunctionArgs;
1092+
for (Value &Arg : F.args())
1093+
FunctionArgs.push_back(&Arg);
1094+
ConstraintInfo Info(F.getParent()->getDataLayout(), FunctionArgs);
10921095
State S(DT);
10931096
std::unique_ptr<Module> ReproducerModule(
10941097
DumpReproducers ? new Module(F.getName(), F.getContext()) : nullptr);

llvm/test/Transforms/ConstraintElimination/transfer-signed-facts-to-unsigned.ll

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,6 @@ else:
295295
ret i1 1
296296
}
297297

298-
; TODO: Even though %cnt is not known signed positive %cmp can be simplified
299-
; because %add.ptr uses it zero-extended.
300298
define i1 @cnt_not_known_positive_sgt_against_base_with_zext(ptr %p, i32 %cnt) {
301299
; CHECK-LABEL: @cnt_not_known_positive_sgt_against_base_with_zext(
302300
; CHECK-NEXT: entry:
@@ -305,7 +303,7 @@ define i1 @cnt_not_known_positive_sgt_against_base_with_zext(ptr %p, i32 %cnt) {
305303
; CHECK-NEXT: [[EXT:%.*]] = zext i32 [[CNT]] to i64
306304
; CHECK-NEXT: [[ADD_PTR:%.*]] = getelementptr inbounds i32, ptr [[P:%.*]], i64 [[EXT]]
307305
; CHECK-NEXT: [[CMP_1:%.*]] = icmp uge ptr [[ADD_PTR]], [[P]]
308-
; CHECK-NEXT: br i1 [[CMP_1]], label [[THEN:%.*]], label [[ELSE:%.*]]
306+
; CHECK-NEXT: br i1 true, label [[THEN:%.*]], label [[ELSE:%.*]]
309307
; CHECK: then:
310308
; CHECK-NEXT: ret i1 false
311309
; CHECK: else:
@@ -326,8 +324,6 @@ else:
326324
ret i1 1
327325
}
328326

329-
; TODO: Even though %cnt is not known signed positive %cmp can be simplified
330-
; because %add.ptr uses it zero-extended.
331327
define i1 @cnt_not_known_positive_sge_against_base_with_zext(ptr %p, i32 %cnt) {
332328
; CHECK-LABEL: @cnt_not_known_positive_sge_against_base_with_zext(
333329
; CHECK-NEXT: entry:
@@ -336,7 +332,7 @@ define i1 @cnt_not_known_positive_sge_against_base_with_zext(ptr %p, i32 %cnt) {
336332
; CHECK-NEXT: [[EXT:%.*]] = zext i32 [[CNT]] to i64
337333
; CHECK-NEXT: [[ADD_PTR:%.*]] = getelementptr inbounds i32, ptr [[P:%.*]], i64 [[EXT]]
338334
; CHECK-NEXT: [[CMP_1:%.*]] = icmp uge ptr [[ADD_PTR]], [[P]]
339-
; CHECK-NEXT: br i1 [[CMP_1]], label [[THEN:%.*]], label [[ELSE:%.*]]
335+
; CHECK-NEXT: br i1 true, label [[THEN:%.*]], label [[ELSE:%.*]]
340336
; CHECK: then:
341337
; CHECK-NEXT: ret i1 false
342338
; CHECK: else:
@@ -453,7 +449,6 @@ else:
453449
ret i1 1
454450
}
455451

456-
; TODO: Even though %cnt is not known signed positive %cmp can be simplified
457452
; because %add.ptr uses it zero-extended.
458453
define i1 @cnt_not_known_positive_from_branch_check_against_base_struct_ugt_with_zext(ptr %p, i32 %cnt) {
459454
; CHECK-LABEL: @cnt_not_known_positive_from_branch_check_against_base_struct_ugt_with_zext(
@@ -464,7 +459,7 @@ define i1 @cnt_not_known_positive_from_branch_check_against_base_struct_ugt_with
464459
; CHECK-NEXT: [[EXT:%.*]] = zext i32 [[CNT]] to i64
465460
; CHECK-NEXT: [[GEP_EXT:%.*]] = getelementptr inbounds [[T:%.*]], ptr [[P:%.*]], i64 0, i32 1, i64 [[EXT]]
466461
; CHECK-NEXT: [[CMP_1:%.*]] = icmp ugt ptr [[GEP_EXT]], [[P]]
467-
; CHECK-NEXT: br i1 [[CMP_1]], label [[THEN:%.*]], label [[ELSE]]
462+
; CHECK-NEXT: br i1 true, label [[THEN:%.*]], label [[ELSE]]
468463
; CHECK: then:
469464
; CHECK-NEXT: ret i1 false
470465
; CHECK: else:

0 commit comments

Comments
 (0)