<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/64575>64575</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[SCEV] Overflow handling in delta computation leads LoopVectorize to miscompile
</td>
</tr>
<tr>
<th>Labels</th>
<td>
regression,
miscompilation,
SCEV
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
antoniofrighetto
</td>
</tr>
</table>
<pre>
The following C code:
```c++
extern int g_var;
void src(int x) {
for (int i = 0; i < 1; i += x)
g_var += x;
}
```
is lowered to the following LLVM IR by latest clang with `O2` (https://godbolt.org/z/T894Tsz68):
```llvm
define void @src(i32 %0, i32 %1) {
%3 = load i32, ptr @g_var, align 4
%4 = tail call i32 @llvm.smax.i32(i32 %1, i32 1)
%5 = add nsw i32 %4, -1
%6 = udiv i32 %5, %1
%7 = add nuw nsw i32 %6, 1
%8 = icmp ult i32 %6, 7
br i1 %8, label %27, label %9
9: ; preds = %2
%10 = and i32 %7, -8
%11 = mul i32 %10, %1
%12 = insertelement <4 x i32> <i32 poison, i32 0, i32 0, i32 0>, i32 %3, i64 0
%13 = insertelement <4 x i32> poison, i32 %0, i64 0
%14 = shufflevector <4 x i32> %13, <4 x i32> poison, <4 x i32> zeroinitializer
br label %15
15: ; preds = %15, %9
%16 = phi i32 [ 0, %9 ], [ %21, %15 ]
%17 = phi <4 x i32> [ %12, %9 ], [ %19, %15 ]
%18 = phi <4 x i32> [ zeroinitializer, %9 ], [ %20, %15 ]
%19 = add <4 x i32> %17, %14
%20 = add <4 x i32> %18, %14
%21 = add nuw i32 %16, 8
%22 = icmp eq i32 %21, %10
br i1 %22, label %23, label %15
23: ; preds = %15
%24 = add <4 x i32> %20, %19
%25 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %24)
%26 = icmp eq i32 %7, %10
br i1 %26, label %30, label %27
27: ; preds = %2, %23
%28 = phi i32 [ 0, %2 ], [ %11, %23 ]
%29 = phi i32 [ %3, %2 ], [ %25, %23 ]
br label %32
30: ; preds = %32, %23
%31 = phi i32 [ %25, %23 ], [ %35, %32 ]
store i32 %31, ptr @g_var, align 4
ret void
32: ; preds = %27, %32
%33 = phi i32 [ %36, %32 ], [ %28, %27 ]
%34 = phi i32 [ %35, %32 ], [ %29, %27 ]
%35 = add nsw i32 %34, %0
%36 = add nsw i32 %33, %1
%37 = icmp slt i32 %36, 1
br i1 %37, label %32, label %30
}
```
Current calculation on how the number of times a loop back-edge is taken seem to result in wrong code generation for certain loops, as bisecting points to cbba71bfb50fb668b80ed430125a460279928272 (cc/ @efriedma-quic, @preames).
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysV11v4ygX_jXkBjWCgz8vcjFtJ9IrzauRdkdzu8L2ccIOMRnATae_fgWOEzt1urvSRlGL4eE53yfH0jm16xA3JH0k6fNK9n5v7EZ23nTKtFbt9ui9WVWm-bX5tkfaGq3NSXU7-kRr0yARnwh7JuwTydjwrQk8hm_cxVePtqOq83T3x4u0RJwPhr8vRjXU2ZpAESCvBEpK8jOEtsbS84miRDxTRsRjXD5Rfl7CYzgYLg63zp8o7np-kZs_3-g7PCpHtTmhxYZ6Q_3M0i9fvv-f_u83Wv2iWnp0ntZadjt6Un5PSca-AslY0HTv_dEFj8CWwHZnmspovzZ2R2D7RmD7rSiTb-4tKwiU7x2n9cth2GqwVR3S6B2SsLODBFACKSPwRM9rPvdX2BLRUdrIJoAC9uhtIBncD09UarXraDK5k8Q7XipNa6n1wJ5EfdbuIF_Xkam4Ch0UCNInLGlkkU1DO3caNUwC-IFPYFmE9Y16GTFpwETiKyq_kvWnKWEWwFNkEZGqPhxpr_0Mlo-wylLFIzhsa1mhDk-Qzx7LaWaG-NCQY0eLjYsywo2JYM4GHbtmFBrpHoophkfModeXkLEba8MSBhs6h9ajxgN2PiR5Ql9jEMXn8BQIjkY5040BYAsL8XmSHyKus4SyqU7ib6XNxVyS7pZoy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