llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[SimplifyCFG] miss removing a conditional edge when a PHI-only detour provides the same value

Open 134ARG opened this issue 2 months ago • 1 comments

SimplifyCFG retains the conditional branch in %pred below even though both successors provide the same value to %join for that predecessor:

define i8 @src(i1 %cond, i8 %v, i32 %sw) {
entry:
  switch i32 %sw, label %pred [
    i32 0, label %join
    i32 1, label %detour
  ]

pred:
  br i1 %cond, label %join, label %detour

detour:
  %p = phi i8 [ %v, %pred ], [ 2, %entry ]
  br label %join

join:
  %r = phi i8 [ %v, %pred ], [ %p, %detour ], [ 1, %entry ]
  ret i8 %r
}

Run with either:

opt -passes=simplifycfg -S input.ll
opt -O3 -S input.ll

Current LLVM retains the branch and intermediate PHI (godbolt: https://godbolt.org/z/55W1Wrcrj):

pred:
  br i1 %cond, label %join, label %detour

detour:
  %p = phi i8 [ %v, %pred ], [ 2, %entry ]
  br label %join

join:
  %r = phi i8 [ %v, %pred ], [ %p, %detour ], [ 1, %entry ]
  ret i8 %r

For an execution arriving from %pred, the two routes are equivalent:

direct:  %pred -> %join             supplies %v
detour:  %pred -> %detour -> %join  supplies %p, whose %pred value is %v

Therefore %cond cannot affect the result along this predecessor, and the expected simplification is (alive2: https://alive2.llvm.org/ce/z/TEq5F9):

pred:
  br label %join

detour:
  br label %join

join:
  %r = phi i8 [ %v, %pred ], [ 2, %detour ], [ 1, %entry ]
  ret i8 %r

This survives because SimplifyCFG considers whether it can remove %detour as a whole. It cannot: %entry still needs that block for the %sw == 1 case, where it contributes 2 rather than the 1 contributed by the direct edge. For %pred, however, the distinction disappears: the direct edge contributes %v, and the detour contributes %p, whose incoming value from %pred is also %v. SimplifyCFG does not currently make this edge-local comparison through the PHI, so it retains %cond. Comparing the direct value with the value translated through the detour would allow only %pred -> %detour to be bypassed while leaving %detour available for %entry.

The same shape can be seen in multiple llvm-opt-benchmark modules. For example, the optimized IR of RocksDB partitioned_index_reader.bc contains the following fragment in rocksdb::InternalKeyComparator::Compare, simplified here only by renaming values and blocks:

pred:
  %cmp = icmp ult i64 %x, %y
  %value = select i1 %cmp, i32 1, i32 0
  br i1 %cmp, label %detour, label %join

detour:
  br label %join

join:
  %result = phi i32 [ %value, %pred ],
                    [ %value, %detour ],
                    [ -1, %other ]

This is the same direct-versus-detour pattern as the reduced report: the edge from %pred to %join supplies %value, and the edge through %detour supplies the same %value. The choice made by %cmp therefore cannot affect the PHI result, but the conditional branch and detour remain in the optimized IR.

134ARG avatar Sep 28 '26 09:09 134ARG

Currently I am testing a local fix for that. Surprisingly the fix for this issue grants over 3000 byte reduction in llvm-opt-benchmark .bc size across 51 testcases, but meanwhile brings some minor regressions in other 18 cases. Please assign this to me.

134ARG avatar Sep 28 '26 09:09 134ARG