-
Notifications
You must be signed in to change notification settings - Fork 15.2k
ValueTracking: strip stray break in recur-match #109794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
I had more time to think about this, and check reduced cases in Alive2. The diff seems to be correct in all cases, and I think the break was unintended. Marking as ready for review. |
|
@llvm/pr-subscribers-llvm-analysis @llvm/pr-subscribers-llvm-transforms Author: Ramkumar Ramachandra (artagnon) ChangesThere is a stray break statement in the recurrence-handling code in computeKnownBitsFromOperator, that seems to be unintended. Strip this statement so that we have the opportunity to go through the rest of phi-handling code, and refine KnownBits further. Full diff: https://github.com/llvm/llvm-project/pull/109794.diff 4 Files Affected:
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 56eb3f99b39d2c..04ab1ec6a21c83 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -1515,8 +1515,6 @@ static void computeKnownBitsFromOperator(const Operator *I,
Known3.isNonNegative())
Known.makeNonNegative();
}
-
- break;
}
}
diff --git a/llvm/test/Transforms/InstCombine/cast_phi.ll b/llvm/test/Transforms/InstCombine/cast_phi.ll
index 7dfe60539138d6..99da3ac1c7c810 100644
--- a/llvm/test/Transforms/InstCombine/cast_phi.ll
+++ b/llvm/test/Transforms/InstCombine/cast_phi.ll
@@ -29,7 +29,7 @@ define void @MainKernel(i32 %iNumSteps, i32 %tid, i32 %base) {
; CHECK-NEXT: [[TMP2:%.*]] = phi float [ [[TMP11:%.*]], [[DOTBB12]] ], [ [[CONV_I]], [[DOTBB2]] ]
; CHECK-NEXT: [[I12_06:%.*]] = phi i32 [ [[SUB:%.*]], [[DOTBB12]] ], [ [[INUMSTEPS]], [[DOTBB2]] ]
; CHECK-NEXT: [[TMP3:%.*]] = icmp ugt i32 [[I12_06]], [[BASE:%.*]]
-; CHECK-NEXT: [[ADD:%.*]] = add i32 [[I12_06]], 1
+; CHECK-NEXT: [[ADD:%.*]] = add nuw i32 [[I12_06]], 1
; CHECK-NEXT: [[CONV_I9:%.*]] = sext i32 [[ADD]] to i64
; CHECK-NEXT: [[ARRAYIDX20:%.*]] = getelementptr inbounds [258 x float], ptr [[CALLA]], i64 0, i64 [[CONV_I9]]
; CHECK-NEXT: [[ARRAYIDX24:%.*]] = getelementptr inbounds [258 x float], ptr [[CALLB]], i64 0, i64 [[CONV_I9]]
@@ -70,8 +70,8 @@ define void @MainKernel(i32 %iNumSteps, i32 %tid, i32 %base) {
; CHECK-NEXT: store float [[TMP10]], ptr [[ARRAYIDX6]], align 4
; CHECK-NEXT: br label [[DOTBB12]]
; CHECK: .bb12:
-; CHECK-NEXT: [[SUB]] = add i32 [[I12_06]], -4
-; CHECK-NEXT: [[CMP13:%.*]] = icmp sgt i32 [[SUB]], 0
+; CHECK-NEXT: [[SUB]] = add nsw i32 [[I12_06]], -4
+; CHECK-NEXT: [[CMP13:%.*]] = icmp sgt i32 [[I12_06]], 4
; CHECK-NEXT: br i1 [[CMP13]], label [[DOTBB3]], label [[DOTBB8]]
;
%callA = alloca [258 x float], align 4
diff --git a/llvm/test/Transforms/InstCombine/known-non-zero.ll b/llvm/test/Transforms/InstCombine/known-non-zero.ll
index b77c04eb81475a..7a2a8626379614 100644
--- a/llvm/test/Transforms/InstCombine/known-non-zero.ll
+++ b/llvm/test/Transforms/InstCombine/known-non-zero.ll
@@ -102,12 +102,11 @@ define void @D60846_miscompile(ptr %p) {
; CHECK-NEXT: [[IS_ZERO:%.*]] = icmp eq i16 [[I]], 0
; CHECK-NEXT: br i1 [[IS_ZERO]], label [[COMMON]], label [[NON_ZERO:%.*]]
; CHECK: non_zero:
-; CHECK-NEXT: [[IS_ONE:%.*]] = icmp eq i16 [[I]], 1
-; CHECK-NEXT: store i1 [[IS_ONE]], ptr [[P:%.*]], align 1
+; CHECK-NEXT: store i1 true, ptr [[P:%.*]], align 1
; CHECK-NEXT: br label [[COMMON]]
; CHECK: common:
-; CHECK-NEXT: [[I_INC]] = add i16 [[I]], 1
-; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ult i16 [[I_INC]], 2
+; CHECK-NEXT: [[I_INC]] = add nuw nsw i16 [[I]], 1
+; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp eq i16 [[I]], 0
; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]]
; CHECK: exit:
; CHECK-NEXT: ret void
diff --git a/llvm/test/Transforms/InstCombine/remove-loop-phi-multiply-by-zero.ll b/llvm/test/Transforms/InstCombine/remove-loop-phi-multiply-by-zero.ll
index d431055f0c21b7..4123bc5557899a 100644
--- a/llvm/test/Transforms/InstCombine/remove-loop-phi-multiply-by-zero.ll
+++ b/llvm/test/Transforms/InstCombine/remove-loop-phi-multiply-by-zero.ll
@@ -6,8 +6,8 @@ define double @test_mul_fast_flags(ptr %arr_d) {
; CHECK-NEXT: br label [[FOR_BODY:%.*]]
; CHECK: for.body:
; CHECK-NEXT: [[I_02:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[INC:%.*]], [[FOR_BODY]] ]
-; CHECK-NEXT: [[INC]] = add i64 [[I_02]], 1
-; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[INC]], 1000
+; CHECK-NEXT: [[INC]] = add nuw nsw i64 [[I_02]], 1
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[I_02]], 999
; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY]], label [[END:%.*]]
; CHECK: end:
; CHECK-NEXT: ret double 0.000000e+00
@@ -36,8 +36,8 @@ define double @test_nsz_nnan_flags_enabled(ptr %arr_d) {
; CHECK-NEXT: br label [[FOR_BODY:%.*]]
; CHECK: for.body:
; CHECK-NEXT: [[I_02:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[INC:%.*]], [[FOR_BODY]] ]
-; CHECK-NEXT: [[INC]] = add i64 [[I_02]], 1
-; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[INC]], 1000
+; CHECK-NEXT: [[INC]] = add nuw nsw i64 [[I_02]], 1
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[I_02]], 999
; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY]], label [[END:%.*]]
; CHECK: end:
; CHECK-NEXT: ret double 0.000000e+00
@@ -70,8 +70,8 @@ define double @test_nnan_flag_enabled(ptr %arr_d) {
; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [1000 x double], ptr [[ARR_D:%.*]], i64 0, i64 [[I_02]]
; CHECK-NEXT: [[TMP0:%.*]] = load double, ptr [[ARRAYIDX]], align 8
; CHECK-NEXT: [[MUL]] = fmul nnan double [[F_PROD_01]], [[TMP0]]
-; CHECK-NEXT: [[INC]] = add i64 [[I_02]], 1
-; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[INC]], 1000
+; CHECK-NEXT: [[INC]] = add nuw nsw i64 [[I_02]], 1
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[I_02]], 999
; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY]], label [[END:%.*]]
; CHECK: end:
; CHECK-NEXT: ret double [[MUL]]
@@ -104,8 +104,8 @@ define double @test_ninf_flag_enabled(ptr %arr_d) {
; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [1000 x double], ptr [[ARR_D:%.*]], i64 0, i64 [[I_02]]
; CHECK-NEXT: [[TMP0:%.*]] = load double, ptr [[ARRAYIDX]], align 8
; CHECK-NEXT: [[MUL]] = fmul ninf double [[F_PROD_01]], [[TMP0]]
-; CHECK-NEXT: [[INC]] = add i64 [[I_02]], 1
-; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[INC]], 1000
+; CHECK-NEXT: [[INC]] = add nuw nsw i64 [[I_02]], 1
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[I_02]], 999
; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY]], label [[END:%.*]]
; CHECK: end:
; CHECK-NEXT: ret double [[MUL]]
@@ -138,8 +138,8 @@ define double @test_nsz_flag_enabled(ptr %arr_d) {
; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [1000 x double], ptr [[ARR_D:%.*]], i64 0, i64 [[I_02]]
; CHECK-NEXT: [[TMP0:%.*]] = load double, ptr [[ARRAYIDX]], align 8
; CHECK-NEXT: [[MUL]] = fmul nsz double [[F_PROD_01]], [[TMP0]]
-; CHECK-NEXT: [[INC]] = add i64 [[I_02]], 1
-; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[INC]], 1000
+; CHECK-NEXT: [[INC]] = add nuw nsw i64 [[I_02]], 1
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[I_02]], 999
; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY]], label [[END:%.*]]
; CHECK: end:
; CHECK-NEXT: ret double [[MUL]]
@@ -172,8 +172,8 @@ define double @test_phi_initalise_to_non_zero(ptr %arr_d) {
; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [1000 x double], ptr [[ARR_D:%.*]], i64 0, i64 [[I_02]]
; CHECK-NEXT: [[TMP0:%.*]] = load double, ptr [[ARRAYIDX]], align 8
; CHECK-NEXT: [[MUL]] = fmul fast double [[F_PROD_01]], [[TMP0]]
-; CHECK-NEXT: [[INC]] = add i64 [[I_02]], 1
-; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[INC]], 1000
+; CHECK-NEXT: [[INC]] = add nuw nsw i64 [[I_02]], 1
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[I_02]], 999
; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY]], label [[END:%.*]]
; CHECK: end:
; CHECK-NEXT: ret double [[MUL]]
@@ -284,8 +284,8 @@ define i32 @test_int_phi_operands(ptr %arr_d) {
; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, ptr [[ARR_D:%.*]], i64 [[I_02]]
; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX]], align 4
; CHECK-NEXT: [[MUL]] = mul nsw i32 [[F_PROD_01]], [[TMP0]]
-; CHECK-NEXT: [[INC]] = add i64 [[I_02]], 1
-; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[INC]], 1000
+; CHECK-NEXT: [[INC]] = add nuw nsw i64 [[I_02]], 1
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[I_02]], 999
; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY]], label [[END:%.*]]
; CHECK: end:
; CHECK-NEXT: ret i32 [[MUL]]
@@ -318,8 +318,8 @@ define i32 @test_int_phi_operands_initalise_to_non_zero(ptr %arr_d) {
; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, ptr [[ARR_D:%.*]], i64 [[I_02]]
; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX]], align 4
; CHECK-NEXT: [[MUL]] = mul i32 [[F_PROD_01]], [[TMP0]]
-; CHECK-NEXT: [[INC]] = add i64 [[I_02]], 1
-; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[INC]], 1000
+; CHECK-NEXT: [[INC]] = add nuw nsw i64 [[I_02]], 1
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[I_02]], 999
; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY]], label [[END:%.*]]
; CHECK: end:
; CHECK-NEXT: ret i32 [[MUL]]
|
|
Gentle ping. |
nikic
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
It looks like falling through here has significant compile-time cost: https://llvm-compile-time-tracker.com/compare.php?from=33fa40cc9659b7b56a9b440edc0587ff58793cac&to=d432e22b2fce1dfd9456b85a095fd5f08e93c6dd&stat=instructions:u |
|
Hm, perhaps we can get @dtcxzyw to run the opt benchmarks and see if the cost outweighs the extra optimization? |
|
What strikes me as strange is that the |
This kind of break can be there if we expect a code path to be strictly better. Generally speaking, if we can analyze something as a recurrence, the results we get should always be better than a naive analysis, so falling through to the naive analysis is unnecessary. It's just that the exact way this was done here it is not actually true, because we miss the special handling for conditions on the predecessor branch. |
This is a good point, but I would have expected the |
There is a stray break statement in the recurrence-handling code in computeKnownBitsFromOperator, that seems to be unintended. Strip this statement so that we have the opportunity to go through the rest of phi-handling code, and refine KnownBits further.
|
Investigating it this morning, and I see the code guarded by: Doesn't this mean that any inserted |
There is a stray break statement in the recurrence-handling code in computeKnownBitsFromOperator, that seems to be unintended. Strip this statement so that we have the opportunity to go through the rest of phi-handling code, and refine KnownBits further.