<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/62058>62058</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[SimpleLoopUnswitch] Assertion failed: isa<To>(Val) && "cast<Ty>() argument of incompatible type!
</td>
</tr>
<tr>
<th>Labels</th>
<td>
c,
llvm:optimizations
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
kbelochapka
</td>
</tr>
</table>
<pre>
Reproducible:
clang -c -O3 1.c
cat 1.c
```c++
long a;
void *b();
void c() {
for (;;) {
a = (long)b();
if (a < 0)
return;
}
}
```
Assertion happens inside of the following code fragment:
```c++
[llvm\lib\Transforms\Scalar\SimpleLoopUnswitch.cpp]
.....
if (Pred == ICmpInst::ICMP_SGE && match(RHS, m_Zero()) {
// Turn "x >=s 0" into "x <u UMIN_INT"
Pred = ICmpInst::ICMP_ULT;
RHS = ConstantInt::get(
RHS->getContext(),
APInt::getSignedMinValue(RHS->getType()->getIntegerBitWidth()));
}
```
More precisely:
```c++
[llvm\include\llvm\IR\DerivedTypes.h]
unsigned Type::getIntegerBitWidth() const {return cast<IntegerType>(this)->getBitWidth();}
...
[llvm\include\llvm\Support\Casting.h]
template <typename To, typename From>
[[nodiscard]] inline decltype(auto) cast(From *Val) {
assert(isa<To>(Val) && "cast<Ty>() argument of incompatible type!"); ------>>> ASSERTION
return CastInfo<To, From *>::doCast(Val);
}
```
The assertion happens because the code expects that the RHS variable has an INTEGER type (IntegerTyID) but in reality the variable type is a POINTER (PointerTyID) (ptr null).
The LHS variable is also a POINTER ( %call = tail call ptr (...) @b() )
The code fragment is a part of the following commit:
```
commit 5d10753314ed58e1f55d41118c8f082c5fc7b2d7
Author: Max Kazantsev <mkazantsev@azul.com>
Date: Fri Feb 10 11:56:08 2023 +0700
[SimpleLoopUnswitch] Inject loop-invariant conditions and unswitch them when it's profitable
Based on https://discourse.llvm.org/t/rfc-inject-invariant-conditions-to-loops-to-enable-unswitching-and-constraint-elimination
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVkFv2zoS_jX0ZSBDoixbPvhgy3FrbJMGsdsF9lJQFGVxS5ECSaVJf_1iKNmOs-nDOzxDSKThcPTNNzMfxZyTJy3EimQbkm0nrPeNsaufpVCGN6z7ySalqV5XT6Kzpuq5LJUg6ZrEWxKPf7li-gQRh-hrCsmUj1bmrw9kHg8XJ3SDV7Aqo0_ASDo-PhtZAaHrktCc0OWtnQ9GIIvRCrWxgMZ0g9fNEgAwIOkW1_ElhC7fB0UfWaMDehYQ4-JlBcAK31t9dSeL7ZjK5eac0_C4dk5YL42GhnWd0A6kdrISYGrwjYDaKGV-SX0CbioBtWWnVmh_5fJDiki2Ueq5JVmhZEmy4miZdrWxrSNZceBMMYs3su2U-GJM9027X9LzZsq7jmQj0in-htsh5UcrKuQHKdoXbbfXLgBJ1_vi_vHH4dMdEDondA4t87whNH_6fCC0gPbHf4Q1I5V0CbecE7ojdAfH3moglL4ASe9IunVILgWpvTmbix6-3e8ffuwfjoTSa4QzsI9QfftyvKne0-dDcC2Mdp5pv9ej90l4RPimmME5IundSfjCaC9e_DmHYvRbP77df8CZqO6l_s5UL4b8x_3H104Mm0fDXntxEnYj_b9l5ZsLOTfd9qeuuTdWQGcFl06o17_dDFJz1VcC22Iw7J9IVmyFlc-iQoRu2lzK3-sw4hUE6OcUP4QNHMnEqg79D5xhDYrReQhwR2juG-muFLzLPd1c0r003l9gP_RdZ6wnWVEw56U-vcHuRdsp5gX2jH_thGatgKPBXrw87qxpEdX5PSTbaFNJx5mtMFC2BamV1AIqwZUf6sd6b0LCmCDNMQZqz3em3kkJC3NNaC4dI2lxNAMBZ8dhTAilI1PH12EdF5k99TjjKAFSc9N2zKN-woAhwdYPdEEUfmFc8IL14XD3dNx_fTijGMuBBO11bQYgtIAz7rAPK1uZYshoAHjpwD814LERY4pvpasUnPVOBOEKciVeOsG9A98wH6w4fc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