Skip to content

Conversation

c-rhodes
Copy link
Collaborator

@c-rhodes c-rhodes commented Sep 5, 2025

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

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
@c-rhodes c-rhodes requested a review from david-arm September 5, 2025 14:13
@llvmbot llvmbot added llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms labels Sep 5, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 5, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Cullen Rhodes (c-rhodes)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/157112.diff

1 Files Affected:

  • (modified) llvm/test/Transforms/InstCombine/freeze.ll (+49)
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 {

@c-rhodes c-rhodes requested review from nikic and dtcxzyw September 5, 2025 14:25
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG

@c-rhodes c-rhodes merged commit 5ec99c7 into llvm:main Sep 9, 2025
9 checks passed
c-rhodes added a commit to c-rhodes/llvm-project that referenced this pull request Sep 30, 2025
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants