<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/105785>105785</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[ConstraintElim] Miscompilation with cmp intrinsic
</td>
</tr>
<tr>
<th>Labels</th>
<td>
miscompilation,
llvm:optimizations
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
dtcxzyw
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
dtcxzyw
</td>
</tr>
</table>
<pre>
Reproducer: https://alive2.llvm.org/ce/z/Tvz2NA
```
; bin/opt -passes=constraint-elimination test.ll -S
define void @h(ptr %p) {
entry:
br label %for.cond
for.cond: ; preds = %for.cond1, %entry
%storemerge = phi i32 [ 0, %entry ], [ 1, %for.cond1 ]
%cmp = icmp eq i32 %storemerge, 0
br i1 %cmp, label %for.cond1, label %for.end6
for.cond1: ; preds = %for.cond, %for.body3
%i.0 = phi i32 [ %inc, %for.body3 ], [ 0, %for.cond ]
%cmp2 = icmp ult i32 %i.0, 3
br i1 %cmp2, label %for.body3, label %for.cond
for.body3: ; preds = %for.cond1
%sub.i = tail call range(i32 -1, 2) i32 @llvm.scmp.i32.i32(i32 1, i32 %storemerge)
store i32 %sub.i, ptr %p, align 4
%inc = add nuw nsw i32 %i.0, 1
br label %for.cond1
for.end6:
ret void
}
```
```
define void @h(ptr %p) {
entry:
br label %for.cond
for.cond: ; preds = %for.cond1, %entry
%storemerge = phi i32 [ 0, %entry ], [ 1, %for.cond1 ]
%cmp = icmp eq i32 %storemerge, 0
br i1 %cmp, label %for.cond1, label %for.end6
for.cond1: ; preds = %for.body3, %for.cond
%i.0 = phi i32 [ %inc, %for.body3 ], [ 0, %for.cond ]
%cmp2 = icmp ult i32 %i.0, 3
br i1 %cmp2, label %for.body3, label %for.cond
for.body3: ; preds = %for.cond1
store i32 0, ptr %p, align 4
%inc = add nuw nsw i32 %i.0, 1
br label %for.cond1
for.end6: ; preds = %for.cond
ret void
}
```
The result of scmp should be 1 instead of 0.
Reduced C reproducer:
```
#include <stdio.h>
#include <stdint.h>
int builtin_scmp(int d, int e) { return (d > e) - (d < e); }
int32_t f = 0;
int64_t g = 0;
void h() {
for (f = 0; f <= 0; f++) {
int i;
for (i = 0; i < 3; i++)
g = builtin_scmp(1, f);
}
}
int main() {
h();
printf("%d\n", (int)g);
return 0;
}
```
I will post a fix later.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsVktv4zYQ_jX0ZWBBGlp-HHTwYw300B62ew9okbanoCiVpJJNfn1BSpEU2w2KHppLA8cW58WZj_NRI5yji1GqYPmOIUpf_nx7fWGILD_MROuvtS164exUy9fiu2psLdtSWca3cPW-cYxvGR4ZHoWmZ4WJ1s9VUtsLw2OpGB7fGB5_PL_hb1uWHli6Zcu0_3RLvoMTGYbHuvEwb4RzyjF-KGvjvBVk_FxpqsgIT7UBr5xPtIb57527VGcyCp5rksAW6ZXhuvEWGOYNww2w1a6zU8bb15BqXAGcLGhxUjpYnmublLWRfULxe5DxLYQUG6ukA8YPU4eM4T6su-B9ZIa587VVlbIXFT2aKwFxBJbvIJ26AMsPcZ3v4D3WEDsqx5hl1cRgFB7Un13A6VbBP52UR1nvFhR3tWZ3UmXk8hEC2acQTLIODcInGVOS3pUfxKa8dZrikN7gcA8Djji02r8DQUn0fE9gCgDe1dql-giXWwQ6y8-bYHLw7SmhaOAFaSiF1mCFCYezDnnOI-wYOjOmvUgjW1xZNQlxDP-9ZTR8cMab992icLAI-waPsff3IDRdDCymJ2LKmJyQEkz7Asa93MCXfcaP7Bac2DIjp6zykYi92erwmPCPhP_T-D-h8T_8e4jUwJk7qL-e7V9B95GA6Zcx75N7-V9w8sdVgVUuwFyfIVxK4K51qyWcFGRAxnklZNClydTvuwoTgYQ92Ol48Hgn5GRK3crAqb3zkurkyvi3v9MaP1XHbzIeTi1pT-bJRWKsgyi-isKD6m-MUHtrDTBcS2D8W6eYv6_3cR0AHFAh4zk-eThHNFPGd4N8uXjycL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