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

    <tr>
        <th>Summary</th>
        <td>
            Add __builtin_reduce_max_index / __builtin_reduce_max_index
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang:codegen,
            llvm:codegen
      </td>
    </tr>

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

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

<pre>
    Clang reduction builtins enable us to determine the min/max values across a vector, but in many cases we require the element index of that value as well / instead of.

In the cases that I've encountered we always needed the lowest index in cases of multiple elements with the same min/max value, I'm not sure if we'd need to support other cases, a vector reverse pre-shuffle could support the highest index case quite easily.

This is a very basic attempt using existing builtins, which I'm hoping we can do a lot better than:
```c
typedef int v4si __attribute__((vector_size(16)));
typedef unsigned int v4su __attribute__((vector_size(16)));

auto minidx(v4si src)
{
  int val = __builtin_reduce_min(src);
  v4si eq = src == (v4si) { val, val, val, val };
  v4si idx = (v4si) { 0, 1, 2, 3 };
  return __builtin_reduce_max(~eq | idx);
}
```
https://godbolt.org/z/Pd6h5Wcbj
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVE2PpDYQ_TXmUpoWbZqGPnCYmRbSKpcoiZQjMnbReGVsxi73xx7y2yNDz2zmI5eVUJmPeu9VvcIWIeiTRWxY-cTKYyYijc43f_z2p56czXqnbs2zEfYEHlWUpJ2FPmpD2gZAK3qDEAOQA4WEftIWgUaESVvG20lc4SxMxABCehcCCDijJOcZf4Y-EmgLk7A3kCJggAuCx5eo_UqCBie0KUnhFdwANApaCUGkbGOA8Ra0DYRCgRs2LD-y_HGN3-zCslIv0G-MV2cEtNJFS-hRJUlhLuIWwCIqVAvEuAuGV11t7xRugCka0rN5Ky3ARdO4YIKYPrWdukyaE1hHEKJH0ANckPFKLXrJuBDn2XkCRyP6VSrhXp0Cj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