<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/96267>96267</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
IndVarSimplifyPass miscompiles if/else with underlying-type-not-specified enum
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
luke-jr
</td>
</tr>
</table>
<pre>
CI caught an incorrect result in Bitcoin Knots which appears to be a bug in the IndVarSimplifyPass optimisation as of Clang 14.0.0-1ubuntu1.1 (Ubuntu 22.04.4) when targeting i686-pc-linux-gnu.
The test case has been somewhat (but only barely) minimised in this branch: https://github.com/bitcoinknots/bitcoin/tree/tmp2
Compilation flow:
```
apt install build-essential cmake llvm clang g++-multilib autoconf pkg-config libtool # not sure all these are needed
./autogen.sh
( cd depends && make HOST=i686-pc-linux-gnu DEBUG=1 MULTIPROCESS=1 NO_QT=1; )
CONFIG_SITE=$PWD/depends/i686-pc-linux-gnu/share/config.site ./configure --enable-debug CC='clang -m32' CXX='clang++ -m32' CPPFLAGS='-DBOOST_MULTI_INDEX_ENABLE_SAFE_MODE' --disable-wallet
make -C src test/test_bitcoin
src/test/test_bitcoin -t script_tests/script_DataCarrierBytes # the test fails due to the bug
```
The bug can be "fixed" by:
* Disabling optimizations. (-O0)
* Removing the `else` part of the `else if` statement-chain.
* Printing the `last_opcode` variable at the end, or inside the loop.
* Specifying `enum opcodetype` to have an underlying type of `unsigned int` (which appears to be its default) or `unsigned char` (other types not tested).
* Removing the `&& last_opcode <= OP_PUSHDATA4` condition from the `else if` clause.
I bisected the issue to occur at exactly `-opt-bisect-limit=11829` (out of 35046, if that matters).
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx8Vd1y2zwOfRr6BiONTDn-ufCFf_tlto29dbLbOw9FwRIbitSQUFLv0-9Acpt0m_1mMpFJggfAwQGoYjSVQ1yKu7W4245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