<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/61930>61930</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Fold adds with global addresses into load offset
</td>
</tr>
<tr>
<th>Labels</th>
<td>
backend:WebAssembly,
llvm:codegen
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
bubba
</td>
</tr>
</table>
<pre>
From https://reviews.llvm.org/D139645:
Given the following C:
```c
extern int *data;
int f(unsigned idx) {
return data[idx];
}
```
You would think we could emit
```wasm
i32.load8_u data($0)
```
But instead we currently emit
```wasm
h: # @h
.functype h (i32) -> (i32)
# %bb.0: # %entry
i64.const data
local.get 0
i64.extend_i32_u
i64.add
i32.load8_s 0
```
We can't naively fold the add into the load offset though, because the effective address calculation is unsigned, which can overflow with negative offsets:
> OK, so looking back at https://github.com/llvm/llvm-project/issues/29497 and [D24053](https://reviews.llvm.org/D24053) this actually does look like the problem that we fixed back then.
> IIRC the problem is that sometimes the address operand of the store (i.e. local 0, or [L1](https://reviews.llvm.org/L1) here) can have a negative value (here it's -128), but the store's effective address calculation (i.e. the operand plus the constant offset, [L1](https://reviews.llvm.org/L1) + args + 128 with this CL applied) is unsigned, so it will overflow.
> So we have to ensure that the calculation that recombines the native local value with the compile-time constant (here 128) happens with an add instruction rather than getting folded.
For signed types, this is a problem. But there should be a way to do the folding for unsigned types.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVUuv4jgT_TVmU7qR4yRAFizg0nxqfS2NNLNozarlxEXiuY6NbAeafz8qJzz6qtVzZxAi-FGvc05VZAi6s4gbVu1YtV_IMfbOb5qxaeSiceq6OXg3QB_jKbBiy8SBiYPHs8ZLyIw5D5nzHROHfV7Uy7KiK3zP-Pz7P31GC7FHODpj3EXbDl7f3WFLPn3baY3fI3oL2kZgYqtklKzYTUe0d2RiPdqUtAKtvjNRA1vNFwA8xtFbSGbV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