[llvm] b0f7876 - [SCCP] Propagate equality of a not-constant
via llvm-commits
llvm-commits at lists.llvm.org
Mon Dec 5 03:54:30 PST 2022
Author: luxufan
Date: 2022-12-05T19:54:13+08:00
New Revision: b0f78769178ebb5e0691aca5143fead7f79ee55c
URL: https://github.com/llvm/llvm-project/commit/b0f78769178ebb5e0691aca5143fead7f79ee55c
DIFF: https://github.com/llvm/llvm-project/commit/b0f78769178ebb5e0691aca5143fead7f79ee55c.diff
LOG: [SCCP] Propagate equality of a not-constant
The equality state of a not-constant can be used to do constant
propagation. For example,
```
define i32 @equal_not_constant(ptr noundef %p, ptr noundef %q) {
entry:
%cmp = icmp ne ptr %p, null
br i1 %cmp, label %if.then, label %if.end
if.then: ; preds = %entry
%cmp.then = icmp eq ptr %p, %q
br i1 %cmp.then, label %if.then1, label %if.end
if.then1: ; preds = %if.then
%cmp.then1 = icmp ne ptr %q, null
call void @use(i1 %cmp.then1)
br label %if.end
if.end:
ret i32 0
}
```
In this case, we can fold `%cmp.then1` as `true`.
Reviewed By: nikic
Differential Revision: https://reviews.llvm.org/D139289
Added:
Modified:
llvm/lib/Transforms/Utils/SCCPSolver.cpp
llvm/test/Transforms/SCCP/conditions-ranges.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Utils/SCCPSolver.cpp b/llvm/lib/Transforms/Utils/SCCPSolver.cpp
index 4b779c58bbcf0..4bfec669dd103 100644
--- a/llvm/lib/Transforms/Utils/SCCPSolver.cpp
+++ b/llvm/lib/Transforms/Utils/SCCPSolver.cpp
@@ -1344,9 +1344,10 @@ void SCCPInstVisitor::handleCallResult(CallBase &CB) {
IV, &CB,
ValueLatticeElement::getRange(NewCR, /*MayIncludeUndef*/ false));
return;
- } else if (Pred == CmpInst::ICMP_EQ && CondVal.isConstant()) {
+ } else if (Pred == CmpInst::ICMP_EQ &&
+ (CondVal.isConstant() || CondVal.isNotConstant())) {
// For non-integer values or integer constant expressions, only
- // propagate equal constants.
+ // propagate equal constants or not-constants.
addAdditionalUser(OtherOp, &CB);
mergeInValue(IV, &CB, CondVal);
return;
diff --git a/llvm/test/Transforms/SCCP/conditions-ranges.ll b/llvm/test/Transforms/SCCP/conditions-ranges.ll
index e108d63e9091f..3de2e9ca72a94 100644
--- a/llvm/test/Transforms/SCCP/conditions-ranges.ll
+++ b/llvm/test/Transforms/SCCP/conditions-ranges.ll
@@ -1370,6 +1370,69 @@ bb142: ; preds = %bb139
ret void
}
+define i32 @equal_not_constant(ptr noundef %p, ptr noundef %q) {
+; CHECK-LABEL: @equal_not_constant(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CMP:%.*]] = icmp ne ptr [[P:%.*]], null
+; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
+; CHECK: if.then:
+; CHECK-NEXT: [[CMP_THEN:%.*]] = icmp eq ptr [[P]], [[Q:%.*]]
+; CHECK-NEXT: br i1 [[CMP_THEN]], label [[IF_THEN1:%.*]], label [[IF_END]]
+; CHECK: if.then1:
+; CHECK-NEXT: call void @use(i1 true)
+; CHECK-NEXT: br label [[IF_END]]
+; CHECK: if.end:
+; CHECK-NEXT: ret i32 0
+;
+entry:
+ %cmp = icmp ne ptr %p, null
+ br i1 %cmp, label %if.then, label %if.end
+
+if.then: ; preds = %entry
+ %cmp.then = icmp eq ptr %p, %q
+ br i1 %cmp.then, label %if.then1, label %if.end
+
+if.then1: ; preds = %if.then
+ %cmp.then1 = icmp ne ptr %q, null
+ call void @use(i1 %cmp.then1)
+ br label %if.end
+
+if.end:
+ ret i32 0
+}
+
+define i32 @not_equal_not_constant(ptr noundef %p, ptr noundef %q) {
+; CHECK-LABEL: @not_equal_not_constant(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CMP:%.*]] = icmp ne ptr [[P:%.*]], null
+; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
+; CHECK: if.then:
+; CHECK-NEXT: [[CMP_THEN:%.*]] = icmp ne ptr [[P]], [[Q:%.*]]
+; CHECK-NEXT: br i1 [[CMP_THEN]], label [[IF_THEN1:%.*]], label [[IF_END]]
+; CHECK: if.then1:
+; CHECK-NEXT: [[CMP_THEN1:%.*]] = icmp ne ptr [[Q]], null
+; CHECK-NEXT: call void @use(i1 [[CMP_THEN1]])
+; CHECK-NEXT: br label [[IF_END]]
+; CHECK: if.end:
+; CHECK-NEXT: ret i32 0
+;
+entry:
+ %cmp = icmp ne ptr %p, null
+ br i1 %cmp, label %if.then, label %if.end
+
+if.then: ; preds = %entry
+ %cmp.then = icmp ne ptr %p, %q
+ br i1 %cmp.then, label %if.then1, label %if.end
+
+if.then1: ; preds = %if.then
+ %cmp.then1 = icmp ne ptr %q, null
+ call void @use(i1 %cmp.then1)
+ br label %if.end
+
+if.end:
+ ret i32 0
+}
+
define i1 @ptr_icmp_data_layout() {
; CHECK-LABEL: @ptr_icmp_data_layout(
; CHECK-NEXT: ret i1 false
More information about the llvm-commits
mailing list