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

    <tr>
        <th>Summary</th>
        <td>
            LICM behaving differently with and without debug information present
        </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: 19647e5b3b77
Reproduce with: ```opt -opaque-pointers=0 -passes="default<O2>" bbi-70612_typed.ll -S -o -```
Result:
```
[...]
%.promoted = load i32, i32* getelementptr inbounds ([5 x i32], [5 x i32]* @c, i64 0, i64 3), align 1
%.promoted5 = load i32, i32* getelementptr inbounds ([5 x i32], [5 x i32]* @c, i64 0, i64 4), align 1
%.promoted8 = load i32, i32* getelementptr inbounds ([5 x i32], [5 x i32]* @c, i64 0, i64 2), align 1
```
If we comment out the call to llvm.dbg.declare we instead get these corresponding loads:
```
[...]
%.promoted = load i32, i32* getelementptr inbounds ([5 x i32], [5 x i32]* @c, i64 0, i64 4), align 1
%.promoted5 = load i32, i32* getelementptr inbounds ([5 x i32], [5 x i32]* @c, i64 0, i64 2), align 1
%.promoted8 = load i32, i32* getelementptr inbounds ([5 x i32], [5 x i32]* @c, i64 0, i64 3), align 1
```
Note that we load in a different order.

This starts happening with commit 2cdc6f2ca62
```
    Reland "[LICM] Hoist LOAD without sinking the STORE"
    
    When doing load/store promotion within LICM, if we
    cannot prove that it is safe to sink the store we won't
    hoist the load, even though we can prove the load could
    be dereferenced and moved outside the loop. This patch
    implements the load promotion by moving it in the loop
    preheader by inserting proper PHI in the loop. The store
    is kept as is in the loop. By doing this, we avoid doing
    the load from a memory location in each iteration.
    
    Please consider this small example:
    
    loop {
      var = *ptr;
      if (var) break;
      *ptr= var + 1;
    }
    After this patch, it will be:
    
    var0 = *ptr;
    loop {
      var1 = phi (var0, var2);
      if (var1) break;
      var2 = var1 + 1;
      *ptr = var2;
    }
    This addresses some problems from [0].
    
    [0] https://bugs.llvm.org/show_bug.cgi?id=51193
    
    Differential revision: https://reviews.llvm.org/D113289
```
[bbi-70612_typed.ll.gz](https://github.com/llvm/llvm-project/files/8852978/bbi-70612_typed.ll.gz)


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzNVk2P2zYQ_TXyhbAgUZY_Dj54111kgbQbJAFyDChxZLFLiSpJe-P--s5QlrXe2Ah62dYw9EHNPL4ZzjyyMPK41vrQsNI0jfJRtmHpaj5bQF5kxWIRJdso2XyGzhq5L4G9KF-TTTRP-r_pPJuaTvy1h2lnVOvBuijbJmzaCeeAniPOJVRirxH9_olH2W84wopCTRfJPOXf_bEDGWvNpl8Qik3P2MPkLrhu-tc3X6P8Lo7jKN-eXnkeI9fGeJAM52baCMlUxiN-3982bAceNDTQ-s5bptrC7FvpWMSXCJazH8EOAdHjzQDGPUvKADWfsWR4yCK-omeh1a5l6c9M8nejMvsVleW7UeFXqVyu3mPFXiCUHs7LzN4zX-O7wGLwhlFdxrLYxRJKLSyQrWqdBySPdMnWkbe14DrTStXuQmTu_1wsv1yh9yuW6yv0nxTL9Ra6XL0_kBKuufBUBz2plgkmVVWBDfVjJdj45BuuX2vlmPPCesdq0XXQUo2QiJ30jvFSlvOKl2LOr07K8PcZtGixQjiGcPfx8f53jIR9MMp59vFpsw14VLtOtc-ETzX85evTZ9K5EWV8-lZDy6QZyjXiD84bLO8-7cq0ARGDC3NRhqhNRv9StK3xZH44JQQDoUBFBdQ3xCOQ6GExWy-mjfjCjxB1YE82PYN7BgckRXHs6tCToj1PcMp2afZajhAFMEw3hNyX2ECUogYdJPWxU3LwNF3Mwjp0wpf16K-ari8hN04xZqA4EhiliGJrz1ijf2ehRiUAS7YoC2A9mSNEh2OfPjy-diMKp3y8YuDYM-D-JRw9XljfHU8LhOvgKD2YEnEwSvbDI8aZeoXUsRobaIw94lApQhyICqKsMQqw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