<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/156978>156978</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [LoopVectorize] Miscompilation: needs use ptrtoaddr instead of using ptrtoint.
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          zitongzhoueric
      </td>
    </tr>
</table>

<pre>
    This C program encounters miscompilation in LoopVectorize when compiled with `-O3` It is [said](https://github.com/AliveToolkit/alive2/issues/1237#issuecomment-3233543958) this is a known issue, but I cannot find an open issue relating it.
 ```c
void conflicting_loops ( int * a , int * b , int n ) {
    for ( int i = 0 ; i < n ; i ++ ) { 
        a [ i ] = b [ i ] + i ; 
    }
    for ( int i = 0 ; i < n ; i ++ ) {
        a [ i ] = a [ i ] + i ;
 }
}
```
Note that the online alive-tv cannot reproduce this yet because of a [bug](https://github.com/AliveToolkit/alive2/issues/1237). You can reproduce this using either running `alivecc -O3` on the C program, or a very recent build of `alive-tv` that includes the bug fix.
`alive-tv -passes=loop-vectorize -always-verify test.bc`: before and after the optimization. 
```

----------------------------------------
define void @conflicting_loops(ptr nocapture noundef %a, ptr nocapture nowrite noundef %b, i32 noundef %n) nofree memory(argmem: readwrite) {
entry:
  %cmp27 = icmp sgt i32 noundef %n, 0
  br i1 %cmp27, label %for.body.preheader, label %for.cond.cleanup6

for.body.preheader:
  %wide.trip.count = zext nneg i32 noundef %n to i64
  br label %for.body

for.body:
  %indvars.iv = phi i64 [ 0, %for.body.preheader ], [ %indvars.iv.next, %for.body ]
  %arrayidx = gep inbounds nuw ptr nocapture nowrite noundef %b, 4 x i64 %indvars.iv
  %#0 = load i32, ptr %arrayidx, align 4
  %#1 = trunc nsw nuw i64 %indvars.iv to i32
  %add = add nsw i32 %#0, %#1
  %arrayidx2 = gep inbounds nuw ptr nocapture noundef %a, 4 x i64 %indvars.iv
  store i32 %add, ptr %arrayidx2, align 4
  %indvars.iv.next = add nsw nuw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %for.body7.preheader, label %for.body

for.body7.preheader:
 %wide.trip.count35 = zext nneg i32 noundef %n to i64
  br label %for.body7

for.body7:
  %indvars.iv32 = phi i64 [ 0, %for.body7.preheader ], [ %indvars.iv.next33, %for.body7 ]
  %arrayidx9 = gep inbounds nuw ptr nocapture noundef %a, 4 x i64 %indvars.iv32
  %#2 = load i32, ptr %arrayidx9, align 4
  %#3 = trunc nsw nuw i64 %indvars.iv32 to i32
  %add10 = add nsw i32 %#2, %#3
  store i32 %add10, ptr %arrayidx9, align 4
 %indvars.iv.next33 = add nsw nuw i64 %indvars.iv32, 1
  %exitcond36.not = icmp eq i64 %indvars.iv.next33, %wide.trip.count35
  br i1 %exitcond36.not, label %for.cond.cleanup6, label %for.body7

for.cond.cleanup6:
  ret void
}
=>
define void @conflicting_loops(ptr nocapture noundef %a, ptr nocapture nowrite noundef %b, i32 noundef %n) nofree memory(argmem: readwrite) {
entry:
  %b2 = ptrtoint ptr nocapture nowrite noundef %b to i64
  %a1 = ptrtoint ptr nocapture noundef %a to i64
  %cmp27 = icmp sgt i32 noundef %n, 0
  br i1 %cmp27, label %for.body.preheader, label %for.cond.cleanup6

for.body.preheader:
  %wide.trip.count = zext nneg i32 noundef %n to i64
  %min.iters.check = icmp ult i64 %wide.trip.count, 8
 br i1 %min.iters.check, label %scalar.ph, label %vector.memcheck

vector.memcheck:
  %#0 = sub i64 %a1, %b2
  %diff.check = icmp ult i64 %#0, 32
  br i1 %diff.check, label %scalar.ph, label %vector.ph

vector.ph:
  %n.mod.vf = urem i64 %wide.trip.count, 8
 %n.vec = sub i64 %wide.trip.count, %n.mod.vf
  br label %vector.body

vector.body:
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.ind = phi <4 x i32> [ { 0, 1, 2, 3 }, %vector.ph ], [ %vec.ind.next, %vector.body ]
  %step.add = add <4 x i32> %vec.ind, { 4, 4, 4, 4 }
  %#1 = gep inbounds nuw ptr nocapture nowrite noundef %b, 4 x i64 %index
  %#2 = gep inbounds nuw ptr %#1, 4 x i32 4
 %wide.load = load <4 x i32>, ptr %#1, align 4
  %wide.load3 = load <4 x i32>, ptr %#2, align 4
  %#3 = add nsw <4 x i32> %wide.load, %vec.ind
  %#4 = add nsw <4 x i32> %wide.load3, %step.add
  %#5 = gep inbounds nuw ptr nocapture noundef %a, 4 x i64 %index
  %#6 = gep inbounds nuw ptr %#5, 4 x i32 4
  store <4 x i32> %#3, ptr %#5, align 4
  store <4 x i32> %#4, ptr %#6, align 4
  %index.next = add nuw i64 %index, 8
  %vec.ind.next = add <4 x i32> %step.add, { 4, 4, 4, 4 }
  %#7 = icmp eq i64 %index.next, %n.vec
 br i1 %#7, label %middle.block, label %vector.body

middle.block:
  %cmp.n = icmp eq i64 %wide.trip.count, %n.vec
  br i1 %cmp.n, label %for.body7.preheader, label %scalar.ph

scalar.ph:
  %bc.resume.val = phi i64 [ %n.vec, %middle.block ], [ 0, %for.body.preheader ], [ 0, %vector.memcheck ]
  br label %for.body

for.body:
  %indvars.iv = phi i64 [ %bc.resume.val, %scalar.ph ], [ %indvars.iv.next, %for.body ]
 %arrayidx = gep inbounds nuw ptr nocapture nowrite noundef %b, 4 x i64 %indvars.iv
  %#8 = load i32, ptr %arrayidx, align 4
  %#9 = trunc nsw nuw i64 %indvars.iv to i32
  %add = add nsw i32 %#8, %#9
  %arrayidx2 = gep inbounds nuw ptr nocapture noundef %a, 4 x i64 %indvars.iv
  store i32 %add, ptr %arrayidx2, align 4
  %indvars.iv.next = add nsw nuw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %for.body7.preheader, label %for.body

for.body7.preheader:
  %wide.trip.count35 = zext nneg i32 noundef %n to i64
  %min.iters.check5 = icmp ult i64 %wide.trip.count, 8
 br i1 %min.iters.check5, label %scalar.ph4, label %vector.ph6

vector.ph6:
  %n.mod.vf7 = urem i64 %wide.trip.count, 8
 %n.vec8 = sub i64 %wide.trip.count, %n.mod.vf7
  br label %vector.body9

vector.body9:
  %index10 = phi i64 [ 0, %vector.ph6 ], [ %index.next15, %vector.body9 ]
  %vec.ind11 = phi <4 x i32> [ { 0, 1, 2, 3 }, %vector.ph6 ], [ %vec.ind.next16, %vector.body9 ]
  %step.add12 = add <4 x i32> %vec.ind11, { 4, 4, 4, 4 }
  %#10 = gep inbounds nuw ptr nocapture noundef %a, 4 x i64 %index10
  %#11 = gep inbounds nuw ptr %#10, 4 x i32 4
 %wide.load13 = load <4 x i32>, ptr %#10, align 4
  %wide.load14 = load <4 x i32>, ptr %#11, align 4
  %#12 = add nsw <4 x i32> %wide.load13, %vec.ind11
  %#13 = add nsw <4 x i32> %wide.load14, %step.add12
  store <4 x i32> %#12, ptr %#10, align 4
  store <4 x i32> %#13, ptr %#11, align 4
 %index.next15 = add nuw i64 %index10, 8
  %vec.ind.next16 = add <4 x i32> %step.add12, { 4, 4, 4, 4 }
  %#14 = icmp eq i64 %index.next15, %n.vec8
 br i1 %#14, label %middle.block17, label %vector.body9

middle.block17:
 %cmp.n18 = icmp eq i64 %wide.trip.count, %n.vec8
  br i1 %cmp.n18, label %for.cond.cleanup6.loopexit, label %scalar.ph4

scalar.ph4:
 %bc.resume.val19 = phi i64 [ %n.vec8, %middle.block17 ], [ 0, %for.body7.preheader ]
  br label %for.body7

for.body7:
  %indvars.iv32 = phi i64 [ %bc.resume.val19, %scalar.ph4 ], [ %indvars.iv.next33, %for.body7 ]
  %arrayidx9 = gep inbounds nuw ptr nocapture noundef %a, 4 x i64 %indvars.iv32
  %#15 = load i32, ptr %arrayidx9, align 4
  %#16 = trunc nsw nuw i64 %indvars.iv32 to i32
  %add10 = add nsw i32 %#15, %#16
  store i32 %add10, ptr %arrayidx9, align 4
  %indvars.iv.next33 = add nsw nuw i64 %indvars.iv32, 1
  %exitcond36.not = icmp eq i64 %indvars.iv.next33, %wide.trip.count35
  br i1 %exitcond36.not, label %for.cond.cleanup6.loopexit, label %for.body7

for.cond.cleanup6.loopexit:
  br label %for.cond.cleanup6

for.cond.cleanup6:
  ret void
}
Transformation doesn't verify!

ERROR: Source is more defined than target

Example:
ptr nocapture noundef %a = null
ptr nocapture nowrite noundef %b = null
i32 noundef %n = #x00000000 (0)

Source:
i1 %cmp27 = #x0 (0)
  >> Jump to %for.cond.cleanup6

SOURCE MEMORY STATE
===================
NON-LOCAL BLOCKS:
Block 0 >       size: 0 align: 4        alloc type: 0   alive: false address: #x0000000000000000
Block 1 >       size: 0 align: 1        alloc type: 0   alive: true     address: #x000000000000000d
Block 2 >       size: 0 align: 1        alloc type: 0   alive: true     address: #x0000000000000004

Target:
i64 %b2 = UB triggered!


Summary:
  0 correct transformations
  1 incorrect transformations
  0 failed-to-prove transformations
  0 Alive2 errors
```


</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsWtmO2zjWfhrWzYEFibIs-6IuXEuA_v90F5CkB-irASUd25xIpIaiXK48_YDUvnhJUmh0D8YIKpbMs_J8PJ8osqLge4F4T4IHEjzdsVIfpLr_xrUU-28HWaLi8V0kk7f7LwdewCPkSu4VywBFLEuhURWQ8SKWWc5TprkUwAV8lDL_B8ZaKv4N4fWAAqoRmMAr1wcgK3fx4pOVC79o4AWQ4KFgPCHBE6Hrg9Z5QfwtoR8I_bDn-lBGTiwzQj9sU37EL1KmX7km9AMzl5TQD7woSiwI_eBRPyTUt9exzDIUeuFT3w-W_iZYE7oBbeLgBTD4KuSrADuU0EeISg2_QMyEkBp2XCTABMgc6yGg0AQo9sC1Q9ytiaH6FxN3e5Q8gViKXcpjM-ifqZR5AYSugQsNhG6BgbHSXEXtlQDjFgkfjFIA2EnVynEg_hO4QPwH-_3RjK6-0wdCHxpRqGXNh5lsmhHBk5WO-te00t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