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

    <tr>
        <th>Summary</th>
        <td>
            Miscompile with GVN, load is replaced with wrong value
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

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

<pre>
    llvm commit: d9e5462da6
Reproduce with:
```opt bbi-72447_x86.ll -S -o bbi-72447_x86_result.ll -passes='gvn' ```

[bbi-72447_x86.ll.gz](https://github.com/llvm/llvm-project/files/9289887/bbi-72447_x86.ll.gz)
[bbi-72447_x86_result.ll.gz](https://github.com/llvm/llvm-project/files/9289888/bbi-72447_x86_result.ll.gz)

I've annotated the instructions in the input bbi-72447_x86.ll as well as in the output bbi-72447_x86_result.ll with what I think is the value of each instruction when executing loops_test, and there we can see that we end up passing 1 to the verify function in the input but after gvn we pass 0 instead.
Input:
```
sw.bb38:                                          ; preds = %while.end
  %cmp43 = icmp eq i16 %i6, 10                                   ; 10 == 10 -> 1
  %conv44 = zext i1 %cmp43 to i16                                ; 1
  tail call void @verify(i16 %conv44)
```
output:
```
sw.bb38:                                          ; preds = %while.end
  %cmp43 = icmp eq i16 %i6, 10                                  ; 23130 != 10 -> 0
  %conv44 = zext i1 %cmp43 to i16                               ; 0
  tail call void @verify(i16 %conv44)
```
It's sort of like the write to %ub in
```
cond.end.thread:                                  ; preds = %for.body5
  store i16 10, ptr %ub, align 1                           ; 10 -> %ub
  br label %sw.epilog                                      ; -> %sw.epilog
```
is ignored and gvn instead has used the value written to %ub in %entry instead:
```
entry:
  %ub = alloca [4 x i16], align 1
  store i16 23130, ptr %ub, align 1
```
and then propagated this value into this phi
```
for.body5:                                        ; preds = %sw.epilog46, %entry
  %i62 = phi i16 [ 23130, %entry ], [ %i10, %sw.epilog46 ] ; 23130
```
With debug printouts GVN says the following:
```
Merging: cond.end25 into while.body
Inserting edge %while.body -> %while.body
        Reachable %while.body -> %while.body
                NCA == %while.body
Inserting edge %while.body -> %while.end
        Reachable %while.body -> %while.end
                NCA == %while.body
Mark %while.endas affected, CurrentLevel 5
        Successor %sw.epilog46, level = 6
                Marking visited not affected %sw.epilog46
        Successor %sw.epilog46, level = 6
        Successor %sw.bb38, level = 6
                Marking visited not affected %sw.bb38
        Successor %sw.default45, level = 6
                Marking visited not affected %sw.default45
 Next: %sw.default45
        Successor %sw.epilog46, level = 6
 Next: %sw.bb38
        Successor %sw.epilog46, level = 6
 Next: %sw.epilog46
        Successor %for.end54, level = 7
                Marking visited not affected %for.end54
        Successor %for.body5, level = 1
 Next: %for.end54
Updating NCD = %while.body
        IDom(%while.end) = %while.body
Deleting edge %cond.end25 -> %while.body
Deleting edge %cond.end25 -> %while.end
        NCD %while.body, ToIDom %while.body
Deleting reachable %cond.end25 -> %while.end
        Rebuilding subtree
        Top of subtree: %while.body
        Running Semi-NCA
Deleting edge %while.body -> %cond.end25
        NCD %while.body, ToIDom %while.body
IsReachableFromIDom %cond.end25
Deleting unreachable subtree %cond.end25
Erasing node %cond.end25
GVN iteration: 0
Inserting edge %while.body -> %while.body.while.body_crit_edge
Inserting %while.body -> (unreachable) %while.body.while.body_crit_edge
After adding unreachable nodes
Inserted %while.body -> (prev unreachable) %while.body.while.body_crit_edge
Inserting edge %while.body.while.body_crit_edge -> %while.body
        Reachable %while.body.while.body_crit_edge -> %while.body
                NCA == %while.body
Deleting edge %while.body -> %while.body
GVN REMOVING NONLOCAL LOAD:   %i6 = load i16, ptr %arrayidx31, align 1
  Inserted PHI:   %i62 = phi i16 [ 23130, %entry ], [ %i10, %sw.epilog46 ]
  Inserted PHI:   %i61 = phi i16 [ %i62, %cond.end.thread ], [ %mul, %cond.end ]
GVN removed:   %0 = load i16, ptr %arrayidx31, align 1
GVN REMOVING NONLOCAL LOAD:   %i8 = load i16, ptr @g, align 1
GVN removed:   %i8 = load i16, ptr @g, align 1
GVN iteration: 1
GVN removed:   %i5 = phi i16 [ %i, %sw.epilog ], [ poison, %while.body.while.body_crit_edge ]
GVN: load i16 %i10 is clobbered by   store i16 10, ptr %ub, align 1
GVN iteration: 2
GVN: load i16 %i2 is clobbered by   store i16 10, ptr %ub, align 1
GVN: load i16 %i10 is clobbered by   store i16 10, ptr %ub, align 1
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzVWEtz4jgQ_jVwUUHZxuZx4JAJM7NUJWQrmZ09pmRbgDbG8koySebXb7fkF8YkkMllUwRsS91f99cPSQ5F_DpPkv2ORGK347o3uiLxjAX-2IvpuOcses7VPcukiPOIkWeutzDDPu6NHfsRmSZhyAcTz_cnjy_T8TBJyOCBDMTh40fJVJ5oM5pRpZjqjRY9b7LZp_BNKn2FevsdfGmrHm5-9QKQm261zhSa432DzwZsy8MhuAE36FHxMwDj_2GRhts1TwDT-zbzprPpdAJXXbq9WTd0bf5nWTBtW3AIURlivpfA0Z4RmqZCU81ioreM8FRpmUeai1TBTfEsyzsCQhV5Zva3mChyfTSzESMMNnneUk2WMJ2nT4QrI7enSQ7Sa8JotG2aALNZStgLi3LN0w1JhMjUo2YKXL8Gy43NEtKIkYimRDEGD0A_3DMYzDOCaYGSLtHCYjHJ169knacW4dBH-KdrzSSBFEItKE4cYxKj8bAgDuceZ629Vc_DMBxNMe3P_uuNvpBMsljB1YL0vOB5C2EdggtWJ8Fn0S7zR2YCh0vC_iXcHeMAHyMZrnMmEEzEKgE9cDXojb4St4ki0r3vG5hf7EUDRo0NDCLkORilRk15AqGB4O8Fj0nPdyz_kOqF9RawTs1DNm1G_R-5Rhxv5I6Abc9tsu18JtuI4nwW2UuoqokiSkiNxZjwJ2Zq41lyzdAe0JCHUAyd0qA7RhqHeiuhVs4KylEw1kIOQ1hBgtInpQXUN5rvOkh9pqU1wzSAhG9SKO23AUrirVihN5QkoSFL8CmkEct4IjbvG1zqLBVWop2UQH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