<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/116249>116249</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[IndVarSimplify] samesign flag should be dropped after narrowing
</td>
</tr>
<tr>
<th>Labels</th>
<td>
miscompilation,
loopoptim,
llvm:transforms
</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/fcnvLv
```
; bin/opt -passes=indvars test.ll -S
define i8 @test(i1 %cond, i32 range(i32 0, 32) %x) {
entry:
br i1 %cond, label %for.body, label %else
else:
%call1 = call i8 @callee()
br label %exit
for.body:
%call2 = call i8 @callee()
%ext = zext i8 %call2 to i32
%cond2 = icmp samesign ugt i32 %x, %ext
br i1 %cond2, label %for.body, label %exit
exit:
%res = phi i8 [ %call1, %else ], [ %call2, %for.body ]
ret i8 %res
}
```
```
----------------------------------------
declare i8 @callee()
define i8 @test(i1 %cond, i32 %x) {
init:
%#range_0_%x = !range i32 %x, i32 0, i32 32
br label %entry
entry:
br i1 %cond, label %for.body, label %else
else:
%call1 = call i8 @callee()
br label %exit
for.body:
%call2 = call i8 @callee()
%ext = zext i8 %call2 to i32
%cond2 = icmp samesign ugt i32 %#range_0_%x, %ext
br i1 %cond2, label %for.body, label %exit
exit:
%res = phi i8 [ %call1, %else ], [ %call2, %for.body ]
ret i8 %res
}
=>
declare i8 @callee()
define i8 @test(i1 %cond, i32 %x) {
init:
%#range_0_%x = !range i32 %x, i32 0, i32 32
br label %entry
entry:
br i1 %cond, label %for.body.preheader, label %else
else:
%call1 = call i8 @callee()
br label %exit
for.body.preheader:
%#0 = trunc i32 %#range_0_%x to i8
br label %for.body
for.body:
%call2 = call i8 @callee()
%cond2 = icmp samesign ugt i8 %#0, %call2
br i1 %cond2, label %for.body, label %exit.loopexit
exit.loopexit:
%call2.lcssa = phi i8 [ %call2, %for.body ]
br label %exit
exit:
%res = phi i8 [ %call1, %else ], [ %call2.lcssa, %exit.loopexit ]
ret i8 %res
}
Transformation doesn't verify!
ERROR: Source is more defined than target
Example:
i1 %cond = #x1 (1)
i32 %x = #x00000000 (0)
Source:
i32 %#range_0_%x = #x00000000 (0)
>> Jump to %entry
>> Jump to %for.body
i8 %call2 = #x80 (128, -128)
i32 %ext = #x00000080 (128)
i1 %cond2 = #x0 (0)
>> Jump to %exit
i8 %res = #x80 (128, -128)
SOURCE MEMORY STATE
===================
NON-LOCAL BLOCKS:
Block 0 > size: 0 align: 1 alloc type: 0 alive: false address: 0
Block 1 > size: 4 align: 1 alloc type: 0 alive: true address: 1
Target:
i32 %#range_0_%x = #x00000000 (0)
>> Jump to %entry
>> Jump to %for.body.preheader
i8 %#0 = #x00 (0)
>> Jump to %for.body
i8 %call2 = #x80 (128, -128)
i1 %cond2 = poison
UB triggered on br
Summary:
0 correct transformations
1 incorrect transformations
0 failed-to-prove transformations
0 Alive2 errors
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWNuO4jgTfhpzUwIlNjTkgguO0v9vzyDRPSvt1cjETvCuE0e2wzT99Cs7IQemD8zOrEYaLUIQuypfnb6qYKgxIs05n6PJEmHMbPz0fP6CMEaT9YCW9qj0vN4cHBQ7z_e80IqVMdeILOBobWEQWSC8RXhLpThxPJLylI2UThHexhzh7TPC2yTOT_cnFKxRsEB3Qf2ulmQJB5EjvFWFhWFBjeEGkbXI2YlqA5YbO5IShg-VPuOJyDmIGaBx4IQIz0QICE9ilTOEVyAIBk3zlDsJwRC4TYIRjpzWk_-eLis0nlt9dhH4FcBBQx9M0gOXbiNReuRS0Nvk0vA6jArOrRs0B0OlDAGRNbir2mt3yZ13CEcdwy3qk7Bd1MZ062cNjW-B9ojWaz67C6d5ud0ql64uqspZhSrirABDM-4oAmVqfWKrDK5q0BfThm_I21WEft2LTnPjvSiOwjs8WTbpvJiXhgO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