[llvm] [SCEV] Simplify SCEVExpr for PHI to SCEV for operand if operands are identical (PR #115945)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 1 22:36:19 PST 2024


dtcxzyw wrote:

Not sure if this regression matters...

Regression (reduced from https://github.com/dtcxzyw/llvm-opt-benchmark/pull/1696/files#r1843118408):
```
; bin/opt -O3 reduced.ll -S
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

declare i32 @_ZN6icu_7512_GLOBAL__N_115appendUnchangedEPDsiiPKDsijPNS_5EditsE(i32)

define i32 @_ZN6icu_7512_GLOBAL__N_17toUpperEijPDsiPKDsP12UCaseContextiPNS_5EditsER10UErrorCode(ptr nocapture readonly %src, i32 %srcLength) local_unnamed_addr {
entry:
  br label %while.cond.outer

while.cond.outer:                                 ; preds = %if.end13, %entry
  %srcIndex.0.ph = phi i32 [ %inc, %if.end13 ], [ 0, %entry ]
  %destIndex.0.ph = phi i32 [ 1, %if.end13 ], [ 0, %entry ]
  %cmp12 = icmp slt i32 %srcIndex.0.ph, %srcLength
  br i1 %cmp12, label %while.body, label %common.ret

while.body:                                       ; preds = %while.cond.outer, %while.cond.backedge
  %srcIndex.03 = phi i32 [ %srcIndex.0.be, %while.cond.backedge ], [ %srcIndex.0.ph, %while.cond.outer ]
  %idxprom = sext i32 %srcIndex.03 to i64
  %arrayidx = getelementptr i16, ptr %src, i64 %idxprom
  %0 = load i16, ptr %arrayidx, align 2
  %cmp2 = icmp eq i16 %0, 0
  br i1 %cmp2, label %if.then3, label %if.else15

if.then3:                                         ; preds = %while.body
  %1 = load i8, ptr %src, align 1
  %inc = add nsw i32 %srcIndex.03, 1
  %cmp11 = icmp eq i8 %1, 0
  br i1 %cmp11, label %while.cond.backedge, label %if.end13

while.cond.backedge:                              ; preds = %if.then3, %if.else15
  %srcIndex.0.be = phi i32 [ %inc, %if.then3 ], [ %inc33, %if.else15 ]
  %cmp1 = icmp slt i32 %srcIndex.0.be, %srcLength
  br i1 %cmp1, label %while.body, label %common.ret

if.end13:                                         ; preds = %if.then3
  %call50 = tail call i32 @_ZN6icu_7512_GLOBAL__N_115appendUnchangedEPDsiiPKDsijPNS_5EditsE(i32 %destIndex.0.ph)
  %cmp58 = icmp slt i32 %call50, 0
  br i1 %cmp58, label %common.ret, label %while.cond.outer

if.else15:                                        ; preds = %while.body
  %inc33 = add nsw i32 %srcIndex.03, 1
  br label %while.cond.backedge

common.ret:                                       ; preds = %if.end13, %while.cond.outer, %while.cond.backedge
  ret i32 0
}
```
Before (22417ec6cca0ed8ccecb0c2b77011e591378fd2a):
```
; ModuleID = 'reduced.ll'
source_filename = "reduced.ll"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

declare i32 @_ZN6icu_7512_GLOBAL__N_115appendUnchangedEPDsiiPKDsijPNS_5EditsE(i32) local_unnamed_addr

define noundef i32 @_ZN6icu_7512_GLOBAL__N_17toUpperEijPDsiPKDsP12UCaseContextiPNS_5EditsER10UErrorCode(ptr nocapture readonly %src, i32 %srcLength) local_unnamed_addr {
entry:
  br label %while.cond.outer

while.cond.outer:                                 ; preds = %if.end13, %entry
  %srcIndex.0.ph = phi i32 [ %inc, %if.end13 ], [ 0, %entry ]
  %destIndex.0.ph = phi i32 [ 1, %if.end13 ], [ 0, %entry ]
  %cmp12 = icmp slt i32 %srcIndex.0.ph, %srcLength
  br i1 %cmp12, label %while.body, label %common.ret

while.body:                                       ; preds = %while.cond.outer, %while.cond.backedge
  %srcIndex.03 = phi i32 [ %srcIndex.0.be, %while.cond.backedge ], [ %srcIndex.0.ph, %while.cond.outer ]
  %idxprom = sext i32 %srcIndex.03 to i64
  %arrayidx = getelementptr i16, ptr %src, i64 %idxprom
  %0 = load i16, ptr %arrayidx, align 2
  %cmp2 = icmp eq i16 %0, 0
  br i1 %cmp2, label %if.then3, label %if.else15

if.then3:                                         ; preds = %while.body
  %1 = load i8, ptr %src, align 1
  %inc = add nsw i32 %srcIndex.03, 1
  %cmp11 = icmp eq i8 %1, 0
  br i1 %cmp11, label %while.cond.backedge, label %if.end13

while.cond.backedge:                              ; preds = %if.else15, %if.then3
  %srcIndex.0.be = phi i32 [ %inc, %if.then3 ], [ %inc33, %if.else15 ]
  %cmp1 = icmp slt i32 %srcIndex.0.be, %srcLength
  br i1 %cmp1, label %while.body, label %common.ret

if.end13:                                         ; preds = %if.then3
  %call50 = tail call i32 @_ZN6icu_7512_GLOBAL__N_115appendUnchangedEPDsiiPKDsijPNS_5EditsE(i32 %destIndex.0.ph)
  %cmp58 = icmp slt i32 %call50, 0
  br i1 %cmp58, label %common.ret, label %while.cond.outer

if.else15:                                        ; preds = %while.body
  %inc33 = add nsw i32 %srcIndex.03, 1
  br label %while.cond.backedge

common.ret:                                       ; preds = %if.end13, %while.cond.outer, %while.cond.backedge
  ret i32 0
}
```
After:
```
; ModuleID = 'reduced.ll'
source_filename = "reduced.ll"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

declare i32 @_ZN6icu_7512_GLOBAL__N_115appendUnchangedEPDsiiPKDsijPNS_5EditsE(i32) local_unnamed_addr

define noundef i32 @_ZN6icu_7512_GLOBAL__N_17toUpperEijPDsiPKDsP12UCaseContextiPNS_5EditsER10UErrorCode(ptr nocapture readonly %src, i32 %srcLength) local_unnamed_addr {
entry:
  br label %while.cond.outer

while.cond.outer:                                 ; preds = %if.end13, %entry
  %srcIndex.0.ph = phi i32 [ %4, %if.end13 ], [ 0, %entry ]
  %destIndex.0.ph = phi i32 [ 1, %if.end13 ], [ 0, %entry ]
  %cmp12 = icmp slt i32 %srcIndex.0.ph, %srcLength
  br i1 %cmp12, label %while.body.preheader, label %common.ret

while.body.preheader:                             ; preds = %while.cond.outer
  %0 = sext i32 %srcIndex.0.ph to i64
  br label %while.body

while.body:                                       ; preds = %while.body.preheader, %while.cond.backedge
  %indvars.iv = phi i64 [ %0, %while.body.preheader ], [ %2, %while.cond.backedge ]
  %srcIndex.03 = phi i32 [ %srcIndex.0.ph, %while.body.preheader ], [ %srcIndex.0.be, %while.cond.backedge ]
  %arrayidx = getelementptr i16, ptr %src, i64 %indvars.iv
  %1 = load i16, ptr %arrayidx, align 2
  %cmp2 = icmp eq i16 %1, 0
  %2 = add nsw i64 %indvars.iv, 1
  br i1 %cmp2, label %if.then3, label %if.else15

if.then3:                                         ; preds = %while.body
  %3 = load i8, ptr %src, align 1
  %cmp11 = icmp eq i8 %3, 0
  %4 = trunc nsw i64 %2 to i32
  br i1 %cmp11, label %while.cond.backedge, label %if.end13

while.cond.backedge:                              ; preds = %if.else15, %if.then3
  %srcIndex.0.be = phi i32 [ %inc33, %if.else15 ], [ %4, %if.then3 ]
  %cmp1 = icmp slt i32 %srcIndex.0.be, %srcLength
  br i1 %cmp1, label %while.body, label %common.ret

if.end13:                                         ; preds = %if.then3
  %call50 = tail call i32 @_ZN6icu_7512_GLOBAL__N_115appendUnchangedEPDsiiPKDsijPNS_5EditsE(i32 %destIndex.0.ph)
  %cmp58 = icmp slt i32 %call50, 0
  br i1 %cmp58, label %common.ret, label %while.cond.outer

if.else15:                                        ; preds = %while.body
  %inc33 = add nsw i32 %srcIndex.03, 1
  br label %while.cond.backedge

common.ret:                                       ; preds = %if.end13, %while.cond.outer, %while.cond.backedge
  ret i32 0
}
```


https://github.com/llvm/llvm-project/pull/115945


More information about the llvm-commits mailing list