llvm / llvm/llvm-project

VectorCombine misses splat-and-identity fold when one fadd is commuted

Open
#206,147 3 comments 0 reactions 1 assignee Claimed by @he-weiwen View on GitHub
llvm:vectorcombine missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

I found a missed optimization. This case is based on a test file modified by #144690

```llvm
define <8 x half> @src(<8 x half> %a, <8 x half> %b) {
%ab = shufflevector <8 x half> %a, <8 x half> poison, <4 x i32>
%at = shufflevector <8 x half> %a, <8 x half> poison, <4 x i32>
%bs = shufflevector <8 x half> %a, <8 x half> poison, <4 x i32> zeroinitializer
%abt = fadd <4 x half> %at, %bs
%abb = fadd <4 x half> %ab, %bs
%r = shufflevector <4 x half> %abt, <4 x half> %abb, <8 x i32>
ret <8 x half> %r
}
```

can be optimized to

```llvm
define <8 x half> @tgt(<8 x half> %a, <8 x half> %b) {
%1 = shufflevector <8 x half> %a, <8 x half> poison, <8 x i32> zeroinitializer
%r = fadd <8 x half> %a, %1
ret <8 x half> %r
}
```

when i commute the operands of `fadd`

```llvm
define <8 x half> @src(<8 x half> %a, <8 x half> %b) {
%ab = shufflevector <8 x half> %a, <8 x half> poison, <4 x i32>
%at = shufflevector <8 x half> %a, <8 x half> poison, <4 x i32>
%bs = shufflevector <8 x half> %a, <8 x half> poison, <4 x i32> zeroinitializer
%abt = fadd <4 x half> %at, %bs
%abb = fadd <4 x half> %bs, %ab
%r = shufflevector <4 x half> %abt, <4 x half> %abb, <8 x i32>
ret <8 x half> %r
}
```

it won`t be folded
Godbolt link:https://godbolt.org/z/dWj9hnb8G

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.