-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SCEV] Disallow simplifying phi(undef, X) to X (#115109)
See the following case: ``` @GlobIntONE = global i32 0, align 4 define ptr @src() { entry: br label %for.body.peel.begin for.body.peel.begin: ; preds = %entry br label %for.body.peel for.body.peel: ; preds = %for.body.peel.begin br i1 true, label %cleanup.peel, label %cleanup.loopexit.peel cleanup.loopexit.peel: ; preds = %for.body.peel br label %cleanup.peel cleanup.peel: ; preds = %cleanup.loopexit.peel, %for.body.peel %retval.2.peel = phi ptr [ undef, %for.body.peel ], [ @GlobIntONE, %cleanup.loopexit.peel ] br i1 true, label %for.body.peel.next, label %cleanup7 for.body.peel.next: ; preds = %cleanup.peel br label %for.body.peel.next1 for.body.peel.next1: ; preds = %for.body.peel.next br label %entry.peel.newph entry.peel.newph: ; preds = %for.body.peel.next1 br label %for.body for.body: ; preds = %cleanup, %entry.peel.newph %retval.0 = phi ptr [ %retval.2.peel, %entry.peel.newph ], [ %retval.2, %cleanup ] br i1 false, label %cleanup, label %cleanup.loopexit cleanup.loopexit: ; preds = %for.body br label %cleanup cleanup: ; preds = %cleanup.loopexit, %for.body %retval.2 = phi ptr [ %retval.0, %for.body ], [ @GlobIntONE, %cleanup.loopexit ] br i1 false, label %for.body, label %cleanup7.loopexit cleanup7.loopexit: ; preds = %cleanup %retval.2.lcssa.ph = phi ptr [ %retval.2, %cleanup ] br label %cleanup7 cleanup7: ; preds = %cleanup7.loopexit, %cleanup.peel %retval.2.lcssa = phi ptr [ %retval.2.peel, %cleanup.peel ], [ %retval.2.lcssa.ph, %cleanup7.loopexit ] ret ptr %retval.2.lcssa } define ptr @tgt() { entry: br label %for.body.peel.begin for.body.peel.begin: ; preds = %entry br label %for.body.peel for.body.peel: ; preds = %for.body.peel.begin br i1 true, label %cleanup.peel, label %cleanup.loopexit.peel cleanup.loopexit.peel: ; preds = %for.body.peel br label %cleanup.peel cleanup.peel: ; preds = %cleanup.loopexit.peel, %for.body.peel %retval.2.peel = phi ptr [ undef, %for.body.peel ], [ @GlobIntONE, %cleanup.loopexit.peel ] br i1 true, label %for.body.peel.next, label %cleanup7 for.body.peel.next: ; preds = %cleanup.peel br label %for.body.peel.next1 for.body.peel.next1: ; preds = %for.body.peel.next br label %entry.peel.newph entry.peel.newph: ; preds = %for.body.peel.next1 br label %for.body for.body: ; preds = %cleanup, %entry.peel.newph br i1 false, label %cleanup, label %cleanup.loopexit cleanup.loopexit: ; preds = %for.body br label %cleanup cleanup: ; preds = %cleanup.loopexit, %for.body br i1 false, label %for.body, label %cleanup7.loopexit cleanup7.loopexit: ; preds = %cleanup %retval.2.lcssa.ph = phi ptr [ %retval.2.peel, %cleanup ] br label %cleanup7 cleanup7: ; preds = %cleanup7.loopexit, %cleanup.peel %retval.2.lcssa = phi ptr [ %retval.2.peel, %cleanup.peel ], [ %retval.2.lcssa.ph, %cleanup7.loopexit ] ret ptr %retval.2.lcssa } ``` 1. `simplifyInstruction(%retval.2.peel)` returns `@GlobIntONE`. Thus, `ScalarEvolution::createNodeForPHI` returns SCEV expr `@GlobIntONE` for `%retval.2.peel`. 2. `SimplifyIndvar::replaceIVUserWithLoopInvariant` tries to replace the use of `%retval.2.peel` in `%retval.2.lcssa.ph` with `@GlobIntONE`. 3. `simplifyLoopAfterUnroll -> simplifyLoopIVs -> SCEVExpander::expand` reuses `%retval.2.peel = phi ptr [ undef, %for.body.peel ], [ @GlobIntONE, %cleanup.loopexit.peel ]` to generate code for `@GlobIntONE`. It is incorrect. This patch disallows simplifying `phi(undef, X)` to `X` by setting `CanUseUndef` to false. Closes #114879.
- Loading branch information
Showing
16 changed files
with
106 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 | ||
; RUN: opt -S -passes=loop-unroll-full -unroll-full-max-count=1 %s | FileCheck %s | ||
|
||
@GlobIntONE = global i32 0, align 4 | ||
|
||
; Make sure we don't reuse the phi (undef, X) for X. | ||
|
||
define ptr @test() { | ||
; CHECK-LABEL: define ptr @test() { | ||
; CHECK-NEXT: [[ENTRY:.*:]] | ||
; CHECK-NEXT: br label %[[FOR_BODY_PEEL_BEGIN:.*]] | ||
; CHECK: [[FOR_BODY_PEEL_BEGIN]]: | ||
; CHECK-NEXT: br label %[[FOR_BODY_PEEL:.*]] | ||
; CHECK: [[FOR_BODY_PEEL]]: | ||
; CHECK-NEXT: br i1 true, label %[[CLEANUP_PEEL:.*]], label %[[CLEANUP_LOOPEXIT_PEEL:.*]] | ||
; CHECK: [[CLEANUP_LOOPEXIT_PEEL]]: | ||
; CHECK-NEXT: br label %[[CLEANUP_PEEL]] | ||
; CHECK: [[CLEANUP_PEEL]]: | ||
; CHECK-NEXT: [[RETVAL_2_PEEL:%.*]] = phi ptr [ undef, %[[FOR_BODY_PEEL]] ], [ @GlobIntONE, %[[CLEANUP_LOOPEXIT_PEEL]] ] | ||
; CHECK-NEXT: br i1 true, label %[[FOR_BODY_PEEL_NEXT:.*]], label %[[CLEANUP2:.*]] | ||
; CHECK: [[FOR_BODY_PEEL_NEXT]]: | ||
; CHECK-NEXT: br label %[[FOR_BODY_PEEL_NEXT1:.*]] | ||
; CHECK: [[FOR_BODY_PEEL_NEXT1]]: | ||
; CHECK-NEXT: br label %[[ENTRY_PEEL_NEWPH:.*]] | ||
; CHECK: [[ENTRY_PEEL_NEWPH]]: | ||
; CHECK-NEXT: br label %[[FOR_BODY:.*]] | ||
; CHECK: [[FOR_BODY]]: | ||
; CHECK-NEXT: br i1 false, label %[[CLEANUP:.*]], label %[[CLEANUP_LOOPEXIT:.*]] | ||
; CHECK: [[CLEANUP_LOOPEXIT]]: | ||
; CHECK-NEXT: br label %[[CLEANUP]] | ||
; CHECK: [[CLEANUP]]: | ||
; CHECK-NEXT: br i1 false, label %[[FOR_BODY]], label %[[CLEANUP2_LOOPEXIT:.*]], !llvm.loop [[LOOP0:![0-9]+]] | ||
; CHECK: [[CLEANUP2_LOOPEXIT]]: | ||
; CHECK-NEXT: [[RETVAL_2_LCSSA_PH:%.*]] = phi ptr [ @GlobIntONE, %[[CLEANUP]] ] | ||
; CHECK-NEXT: br label %[[CLEANUP2]] | ||
; CHECK: [[CLEANUP2]]: | ||
; CHECK-NEXT: [[RETVAL_2_LCSSA:%.*]] = phi ptr [ [[RETVAL_2_PEEL]], %[[CLEANUP_PEEL]] ], [ [[RETVAL_2_LCSSA_PH]], %[[CLEANUP2_LOOPEXIT]] ] | ||
; CHECK-NEXT: ret ptr [[RETVAL_2_LCSSA]] | ||
; | ||
entry: | ||
br label %for.body | ||
|
||
for.body: | ||
%retval.0 = phi ptr [ undef, %entry ], [ %retval.2, %cleanup ] | ||
%cmp1.not = phi i1 [ true, %entry ], [ false, %cleanup ] | ||
br i1 %cmp1.not, label %cleanup, label %cleanup.loopexit | ||
|
||
cleanup.loopexit: | ||
br label %cleanup | ||
|
||
cleanup: | ||
%retval.2 = phi ptr [ %retval.0, %for.body ], [ @GlobIntONE, %cleanup.loopexit ] | ||
br i1 %cmp1.not, label %for.body, label %cleanup2 | ||
|
||
cleanup2: | ||
%retval.2.lcssa = phi ptr [ %retval.2, %cleanup ] | ||
ret ptr %retval.2.lcssa | ||
} | ||
;. | ||
; CHECK: [[LOOP0]] = distinct !{[[LOOP0]], [[META1:![0-9]+]]} | ||
; CHECK: [[META1]] = !{!"llvm.loop.peeled.count", i32 1} | ||
;. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters