<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/78214>78214</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
missed optimization on x86 : `gep + gep -> add + gep` when 3-address lea is more expensive than add
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
minglotus-6
</td>
</tr>
</table>
<pre>
https://gcc.godbolt.org/z/YjbY7snas shows the reduced C++ source code, IR and codegen for both clang 16.0.0 (better) and trunk (less efficient).
The C++ source code has an outer loop (`do {...} while`) and an inner-loop (unrolled into `LBB0_1` and `LBB0_4`)
* For `LBB0_1`, Clang 16.0.0 generates 14 instructions while trunk generates 15 instructions. Besides, code sequence from trunk are less efficient; `lea` instructions of format `[base + index*scale + displacement]` has higher latency and lower throughput than `add` on some x86 processors (see [Agner's instruction table](https://www.agner.org/optimize/instruction_tables.pdf))
The test case is reduced from a [function](https://github.com/google/snappy/blob/27f34a580be4a3becf5f8c0cba13433f53c21337/snappy.cc#L1194C38-L1194C58) in open-source compression/decompression library.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJxsVE2P4ygQ_TXkUoqFwV855JB0r6WV5rTay5xGGMo2sxi8FJ50z69f4XT3pkdzCaFSr-rVqxcUkZ084pnVV1Y_H9SW5hDPi_WTC2mjY3MYgnk9zymtxOSFiZ6JftK6mIIZgktFiBMT_U8m-q_fh68teUVAc7gRpBkhotk0Gnhi4srEFShsUSPoYJCJJ_jzL1De7NcJPYwhwhDSDNopP0HZFLzgwEQ3YEoYmTjt6Slu_p8cdkgEOI5WW_SJiVPB-DPjl79n_F1HmBWB8hC2hBFcCGsuwhpuArD2WhQFa5_hNluHrOHv3ZQH6z3G4ztg8zE4hwasTwFYw79cr_xbyRq-578HqnuNOyMmLtCH-Ck7CwBPj5NO6DGqhARlBdZTiptONni6k3ob_CGr_pRVw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