Issue 75560
Summary [JumpThreading][SimplifyCFG] Missing optimization : thread through BB based on known condition on edge
Labels
Assignees
Reporter XChy
    Alive2 proof: https://alive2.llvm.org/ce/z/VvFox-

### Description:
For example:

```llvm
define i1 @src(i1 noundef %c) {
entry:
  br i1 %c, label %then, label %land.lhs.true

land.lhs.true:
  %c1 = tail call noundef i1 @cond() #2
  br label %then

then:
  %tobool = phi i1 [ false, %entry ], [ %c1, %land.lhs.true ]
  %or.cond = or i1 %c, %tobool
  ret i1 %or.cond
}
```

When `%c` is true, we will go into BB `%then` and then `%tobool` must be false. Therefore we can infer that `%or.cond` is true when `%c` is true. With this fact, we could directly thread the true branch of `%entry` to return true. Like:

```llvm
define i1 @tgt(i1 noundef %c) {
entry:
  br i1 %c, label %else, label  %land.lhs.true

land.lhs.true:
  %c1 = tail call noundef i1 @cond() #2
  br label %then

then:
  ret i1 %c1

else:
  ret i1 1
}
```

### Real-world motivation

This snippet of IR is derived from [redis/src/expire.c@expireSlaveKeys](https://github.com/redis/redis/blob/d8a21c5767b29d53e72c4f52c00c6bd15d8648ec/src/expire.c#L380C1-L380C1) (after O3 pipeline).
The example above is a reduced version. If you're interested in the original suboptimal IR and optimal IR, see also: https://godbolt.org/z/Gdbba3Eve

**Let me know if you can confirm that it's an optimization opportunity, thanks.**
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to