<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/111697>111697</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Reassociate pass on `i128` created from two `i64`s changes subtraction to addition of a negative
</td>
</tr>
<tr>
<th>Labels</th>
<td>
llvm:optimizations,
llvm
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Bryce-MW
</td>
</tr>
</table>
<pre>
I noticed some weird codegen around subtracting `i128` when the input is two `i64`s (which often happens on x86_64`) and the output is to memory (whether it's to the same location or different). At least I think that seems to be the condition. It looks like ReassociatePass decides to rewrite the subtraction as addition with a negative. Here's a [Compiler Explorer link](https://clang.godbolt.org/z/oTqnEo4hf).
Here's some IR that does the same thing:
```ll
define void @sub_assign_int(ptr %a.0, i64 %b.0, i64 %b.1) {
%b.2 = zext i64 %b.1 to i128
%b.3 = shl nuw i128 %b.2, 64
%b.4 = zext i64 %b.0 to i128
%b.5 = or disjoint i128 %b.3, %b.4
%a.1 = load i128, ptr %a.0, align 8
%res = sub i128 %a.1, %b.5
store i128 %res, ptr %a.0, align 8
ret void
}
```
Which compiles to the following in `x86_64`:
```asm
xor eax, eax
neg rsi
sbb rax, rdx
add qword ptr [rdi], rsi
adc qword ptr [rdi + 8], rax
```
I expect to get just a `sub` and an `sbb`:
```asm
sub qword ptr [rdi], rsi
sbb qword ptr [rdi + 8], rdx
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyEVc1u2zgQfhr6MqghUT-2DzrESY3NYYFFsUCPASWOJCYURyWp2MnTL0jGieMN2iCwwOE33_wPhXNqMIgNq_asuluJxY9km7196fDb3z9XLcmX5h4MedWhBEcTwhGVldCRxAENCEuLkeCW1lvReWUGYHWmcr5ldQbHEQ34EUGZefGgHPgjRUBdsjpzwPj2OKpuBOo9GhjFPKNxQAZO2_ohghjfgTAy0tDizzwEE05kXxIF-hEtKM_4J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