<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/62124>62124</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Collapse `x/N*N` → `x` when the multiplication is inside a GEP and the division exact
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
scottmcm
</td>
</tr>
</table>
<pre>
This is, of course, already handed for normal `udiv`+`mul`.
But it looks like it's *not* handled when the "multiply by N" is because of a GEP over `[N x T]`.
Alive2 says it's legal: <https://alive2.llvm.org/ce/z/6azqo5>
```llvm
define ptr @src(ptr nonnull noundef %0, i64 noundef %1) {
%start:
%d = udiv exact i64 noundef %1, 3
%p = gep inbounds ptr nonnull noundef %0, 3 x i64 %d
ret ptr %p
}
=>
define ptr @tgt(ptr nonnull noundef %0, i64 noundef %1) {
%start:
%p = gep inbounds ptr nonnull noundef %0, 1 x i64 noundef %1
ret ptr %p
}
Transformation seems to be correct!
```
But opt doesn't do it: <https://llvm.godbolt.org/z/KGfPMnbh6>
And thus the resulting code ends up with a division followed by a LEA multiply:
```nasm
src: # @src
mov rax, rsi
movabs rcx, -6148914691236517205
mul rcx
shr rdx
lea rax, [rdx + 2*rdx]
add rax, rdi
ret
```
This should probably happen also for when the denominator of the `udiv exact` is just a factor of the array length in the `GEP`, like in <https://alive2.llvm.org/ce/z/aPjgyB>
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVdty4jgTfprmpmtSchub-IILCJCL_99ULvICstVgZYTklWQC8_Rbsp0D7NTsTtW6KHzog7pb3_dJhqAPlnkJxRqKzUz2sXV-GRoX47E5zmqnLsuXVgfUAegB3R4b1_vA6UUaz1JdsJVWscK982idP0qDUIpe6ROUAmgNpTj2BkpxB2IDYjX-r_uIOqJx7ntAo78z6gi0CAi0si4CrYa8hhW-tWwxtoxAdOxN1J25YH3BJyBCHbDm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