llvm-project/polly/test/ScopInfo/complex_domain_binary_condition.ll
Yingwei Zheng 0b9f1cc024
[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 https://github.com/llvm/llvm-project/issues/114879.
2024-11-07 15:53:51 +08:00

60 lines
2.5 KiB
LLVM

; RUN: opt %loadNPMPolly -pass-remarks-analysis="polly-scops" '-passes=print<polly-function-scops>' \
; RUN: -disable-output < %s 2>&1 | FileCheck %s
;
; CHECK: Low complexity assumption: { : false }
;
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
%struct.bc_struct.0.2.4.6.13.20.27.43.44.46.50.52.58.60.81.89.90.99.107.108.109.111.116.118.149 = type { i32, i32, i32, i32, [1024 x i8] }
; Function Attrs: nounwind uwtable
define void @bc_multiply(ptr readonly %n1, i32 %scale) #0 {
entry:
%0 = load i32, ptr undef, align 4
%1 = load i32, ptr undef, align 4
%2 = load i32, ptr undef, align 4
%add3 = add nsw i32 %2, %1
%cmp = icmp sgt i32 %0, %2
%. = select i1 %cmp, i32 %0, i32 %2
%cmp12 = icmp slt i32 %., %scale
%scale.. = select i1 %cmp12, i32 %scale, i32 %.
%cmp26 = icmp sgt i32 0, %scale..
%scale...add7 = select i1 %cmp26, i32 %scale.., i32 0
%sub = sub nsw i32 0, %scale...add7
%add.ptr = getelementptr inbounds %struct.bc_struct.0.2.4.6.13.20.27.43.44.46.50.52.58.60.81.89.90.99.107.108.109.111.116.118.149, ptr %n1, i64 0, i32 4, i64 0
%add.ptr59 = getelementptr inbounds i8, ptr %add.ptr, i64 -1
%idx.ext62 = sext i32 %add3 to i64
%cmp70140 = icmp sgt i32 %sub, 0
br label %for.body104.lr.ph
for.body104.lr.ph: ; preds = %entry
%3 = add i32 0, -1
%4 = sub i32 %3, %scale...add7
%5 = add i32 %4, 1
%6 = sext i32 %5 to i64
br label %for.body104
for.body104: ; preds = %while.end146, %for.body104.lr.ph
%indvars.iv = phi i64 [ %6, %for.body104.lr.ph ], [ poison, %while.end146 ]
%7 = sub nsw i64 %indvars.iv, %idx.ext62
%cmp107 = icmp slt i64 %7, -1
%.op = xor i64 %7, -1
%idx.neg116 = select i1 %cmp107, i64 0, i64 %.op
%add.ptr117 = getelementptr inbounds i8, ptr %add.ptr59, i64 %idx.neg116
br label %while.body138
while.body138: ; preds = %while.body138, %for.body104
%n1ptr.1126 = phi ptr [ %incdec.ptr139, %while.body138 ], [ %add.ptr117, %for.body104 ]
%incdec.ptr139 = getelementptr inbounds i8, ptr %n1ptr.1126, i64 -1
%cmp132 = icmp uge ptr %incdec.ptr139, null
%cmp135 = icmp slt i64 0, -1
%or.cond99 = and i1 %cmp135, %cmp132
br i1 %or.cond99, label %while.body138, label %while.end146
while.end146: ; preds = %while.body138
br i1 undef, label %free_num.exit, label %for.body104
free_num.exit: ; preds = %while.end146
ret void
}