llvm / llvm/llvm-project

If a is 0 or 1, then (a ? 2 : 1) to a + 1

Open
#206,708 2 comments 0 reactions 1 assignee Claimed by @ThePo1es View on GitHub
llvm:instcombine missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

[Godbolt](https://godbolt.org/z/PTfb4x437)
Like [bug 206577](https://github.com/llvm/llvm-project/issues/206577),

With flags -O3:

```c
int src (int a) {
if (a < 0 || a > 1)
__builtin_unreachable ();
return (a ? 2 : 1);
}

int tgt (int a) {
if (a < 0 || a > 1)
__builtin_unreachable ();
return a + 1;
}
```

```llvm
define dso_local noundef range(i32 1, 3) i32 @src(i32 noundef %a) local_unnamed_addr {
entry:
%or.cond = icmp ult i32 %a, 2
tail call void @llvm.assume(i1 %or.cond)
%tobool.not = icmp eq i32 %a, 0
%cond = select i1 %tobool.not, i32 1, i32 2
ret i32 %cond
}

define dso_local range(i32 1, 3) i32 @tgt(i32 noundef %a) local_unnamed_addr {
entry:
%or.cond = icmp ult i32 %a, 2
tail call void @llvm.assume(i1 %or.cond)
%add = add nuw nsw i32 %a, 1
ret i32 %add
}

declare void @llvm.assume(i1 noundef) #1
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.