<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/145875>145875</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[SCEV] Repeated max not hoisted out of loop
</td>
</tr>
<tr>
<th>Labels</th>
<td>
llvm:optimizations,
llvm:SCEV,
missed-optimization
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
aengelke
</td>
</tr>
</table>
<pre>
The repeated computation of a maximum/minimum is not hoisted outside of a loop, preventing the elimination of the loop (instead, the loop gets pointlessly vectorized).
Example (https://godbolt.org/z/faM4od4dM; see also: https://alive2.llvm.org/ce/z/YqPC82)
```c++
#include <cstdint>
uint8_t f(uint8_t n, uint8_t a, uint8_t b) {
for (uint8_t i = 0; i < n; i++)
a = a < b ? b : a;
return a;
}
```
To me this looks like something that SCEV should recognize (I might be wrong here, but in the end, the loop should get eliminated).
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJxcU02P4zYM_TX0hZjAluM4PviQScZADwsU7aJAT4VsMTY7suRadDo7v34h52OzCwgJRZHPfHp6OgTuHVENxSsUp0QvMvi51uR6su-UtN58q78OhDNNpIUMdn6cFtHC3qE_o8ZRf_C4jKCakV2MkAM6Lzh4DrHDLxLY0LXaej-BOuI004WcsOtRBkKyPLJ7oMZUrERQe3ZBSJvY9Ej3JAEnz04shWC_4YU68TN_kgFVbSA9QHp4-9DjZCliDCJTgPwAqgHV9N603srGzz2o5hNUc9Zftt5szRfIXzEQobbBQ37Anxu15QupjbWX8dbc0Q3h7_9-P-4VqCp-e5deVwfqNa70ACpn19nFEEJ-7IIYdgL5G6SHhZ3s_xE8g9rfYxfZ3jf6edOCqhDKiImIePYzPvUxQn7CNLKI4RHdGt7GWIdDvdbo9bhFyJv194Aa8jvqTLLM7p6B8vRM6nq5Xz2OhDJwiIq8B7T8Thj8SDJcRdWCfx7f_sIw-MUanKnzvePPVY_fcOR-EGwJ_5-963GgmSLNdhFkd30S7mfNbzg9yeO1XNVOTJ2bKq90QnVWFmmVl2WWJUOdbdtdt9tui7Mus31uqOwyUufC7Iosq9os4Vqlqkh3apdleZFlG52bSuu9NmlaqiotYZvSqNk-JE84hIXqbFvsyyKxuiUbVu8oFUsgP_hJeOTP9SkHUArU8cdhvJBHbuQQyLw8N8Sz4pTMdax_aZc-wDa1HCT8mEBY7OrXFaw44R93a47641fjRTPF20uW2da_uIBlWNpN56N11_mufy_T7P-lTkA1K9kAqrnxvdTqewAAAP__T5NRJg">