<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/63265>63265</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [clang] Multiple llvm.vector.reduce.add with identical inputs redundant computations
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Omeriko12
      </td>
    </tr>
</table>

<pre>
    ```cpp
void test(const short* in1, const short* in2, short* out, int width, int height) {

  short tmp = 0;
 short tmp2 = 0;

  
  for(int y=0;y<height;y++) {
    #pragma clang loop vectorize(enable)
    for(int x=0; x<width; x++) {
      tmp += in1[x + width * y];
      tmp2 += in1[x + width * y];
    }
 out[y] = tmp + tmp2;
  }
}
```
https://godbolt.org/z/rn9ozh9EM - Compiled with -O2.
I observed a missed optimization in the Clang compiler when using loop vectorization. In the provided code snippet, there are two variables, tmp and tmp2, which are computed independently, despite being assigned the same value:
```ll
41: ; preds = %24
  %42 = add <8 x i16> %36, %35, !dbg !37
  %43 = tail call i16 @llvm.vector.reduce.add.v8i16(<8 x i16> %42), !dbg !37
  %44 = add <8 x i16> %38, %37, !dbg !37
  %45 = tail call i16 @llvm.vector.reduce.add.v8i16(<8 x i16> %44), !dbg !37
  br i1 %14, label %51, label %46, !dbg !37
```
**Expected Behavior:**
During loop vectorization, the compiler should recognize that tmp and tmp2 are assigned the same value, and optimize the code accordingly. The redundant computation should be eliminated.


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVd1uqzgQfhrnZlQEYyDkIhdN0kjn4ujc7AsYPA3eNTayTdL06Vc2pGl7upVWOpIFHubnG898Y4T36mSItqzaseqwElPordv-Gsipf2yBq9bK65bV-by6cWT5geWPZ6skBPKBYdNZ4wP43rrA8BGUKRju4bevGL--yXYKUVYmwEXJ0N-EntSpDww3wNa7GWt-wuwLYRiB8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