-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[InstCombine] Add test for freeze of PHI with noundef start value (NFC) #157112
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
[InstCombine] Add test for freeze of PHI with noundef start value (NFC) #157112
Conversation
We should be able to remove this freeze as the incoming values to the PHI have the same well-defined start value and the GEP can't produce poison, but this is currently unsupported. If the freeze is pushed to the incoming values we can remove it: https://godbolt.org/z/8dE4o1bKf
@llvm/pr-subscribers-llvm-transforms Author: Cullen Rhodes (c-rhodes) ChangesWe should be able to remove this freeze as the incoming values to the PHI have the same well-defined start value and the GEP can't produce poison, but this is currently unsupported. If the freeze is pushed to the incoming values we can remove it: https://godbolt.org/z/8dE4o1bKf Full diff: https://github.com/llvm/llvm-project/pull/157112.diff 1 Files Affected:
diff --git a/llvm/test/Transforms/InstCombine/freeze.ll b/llvm/test/Transforms/InstCombine/freeze.ll
index b29421a655fa8..7eac456bcca1e 100644
--- a/llvm/test/Transforms/InstCombine/freeze.ll
+++ b/llvm/test/Transforms/InstCombine/freeze.ll
@@ -1135,6 +1135,55 @@ exit:
ret void
}
+; We can remove this freeze as the incoming values to the PHI have the same
+; well-defined start value and the GEP can't produce poison, but this is
+; currently unsupported.
+define void @fold_phi_noundef_start_value(ptr noundef %init, i1 %cond.0, i1 %cond.1, i1 %cond.2) {
+; CHECK-LABEL: define void @fold_phi_noundef_start_value(
+; CHECK-SAME: ptr noundef [[INIT:%.*]], i1 [[COND_0:%.*]], i1 [[COND_1:%.*]], i1 [[COND_2:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*]]:
+; CHECK-NEXT: br label %[[LOOP_PH_0:.*]]
+; CHECK: [[LOOP_PH_0]]:
+; CHECK-NEXT: [[IV_0:%.*]] = phi ptr [ [[INIT]], %[[ENTRY]] ], [ [[IV_0_NEXT:%.*]], %[[LOOP:.*]] ]
+; CHECK-NEXT: br i1 [[COND_0]], label %[[LOOP]], label %[[LOOP_PH_1:.*]]
+; CHECK: [[LOOP_PH_1]]:
+; CHECK-NEXT: [[IV_1:%.*]] = getelementptr i8, ptr [[IV_0]], i64 -8
+; CHECK-NEXT: br label %[[LOOP]]
+; CHECK: [[LOOP]]:
+; CHECK-NEXT: [[IV_2:%.*]] = phi ptr [ [[IV_0]], %[[LOOP_PH_0]] ], [ [[IV_1]], %[[LOOP_PH_1]] ]
+; CHECK-NEXT: [[IV_2_FR:%.*]] = freeze ptr [[IV_2]]
+; CHECK-NEXT: [[IV_2_FR_INT:%.*]] = ptrtoint ptr [[IV_2_FR]] to i64
+; CHECK-NEXT: [[IV_0_INT:%.*]] = ptrtoint ptr [[IV_0]] to i64
+; CHECK-NEXT: [[IDX:%.*]] = sub i64 [[IV_0_INT]], [[IV_2_FR_INT]]
+; CHECK-NEXT: [[IV_0_NEXT]] = getelementptr i8, ptr [[IV_0]], i64 [[IDX]]
+; CHECK-NEXT: br i1 [[COND_2]], label %[[EXIT:.*]], label %[[LOOP_PH_0]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: ret void
+;
+entry:
+ br label %loop.ph.0
+
+loop.ph.0:
+ %iv.0 = phi ptr [ %init, %entry ], [ %iv.0.next, %loop ]
+ br i1 %cond.0, label %loop, label %loop.ph.1
+
+loop.ph.1:
+ %iv.1 = getelementptr i8, ptr %iv.0, i64 -8
+ br label %loop
+
+loop:
+ %iv.2 = phi ptr [ %iv.0, %loop.ph.0 ], [ %iv.1, %loop.ph.1 ]
+ %iv.2.fr = freeze ptr %iv.2
+ %iv.2.fr.int = ptrtoint ptr %iv.2.fr to i64
+ %iv.0.int = ptrtoint ptr %iv.0 to i64
+ %idx = sub i64 %iv.0.int, %iv.2.fr.int
+ %iv.0.next = getelementptr i8, ptr %iv.0, i64 %idx
+ br i1 %cond.2, label %exit, label %loop.ph.0
+
+exit:
+ ret void
+}
+
define void @fold_phi_invoke_start_value(i32 %n) personality ptr undef {
; CHECK-LABEL: define void @fold_phi_invoke_start_value(
; CHECK-SAME: i32 [[N:%.*]]) personality ptr undef {
|
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.
LG
PR llvm#157112 adds a test '@fold_phi_noundef_start_value' where we can't currently remove the freeze, even though it's possible. The two places in instcombine that deal with freezes are 'pushFreezeToPreventPoisonFromPropagating' and 'foldFreezeIntoRecurrence'. The former doesn't support PHIs at all and the latter is restricted to recurrences. It doesn't consider this test a recurrence as the BB of the frozen PHI node '%loop.latch' does not dominate either of the BBs ('%loop', '%if.else') of the incoming values. Therefore, I've updated 'pushFreezeToPreventPoisonFromPropagating' to support pushing the freeze to the incoming PHI values, as long as the BB of the frozen PHI *does not* dominate the BB of the incoming value(s). This fixes the test case added in llvm#157112 and the other test changes look sensible, although perhaps I'm missing some edge cases (?). The 'match(U.get(), m_Undef())' check for undef PHI incoming value looks necessary to catch the case covered by '@two_undef' test in freeze-phi.ll. Without this check the undef becomes zero which doesnt seem right.
We should be able to remove this freeze as the incoming values to the PHI have the same well-defined start value and the GEP can't produce poison, but this is currently unsupported.
If the freeze is pushed to the incoming values we can remove it: https://godbolt.org/z/8dE4o1bKf