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

    <tr>
        <th>Summary</th>
        <td>
            LSR gives different result with/without debug info present
        </td>
    </tr>

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

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

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

<pre>
    llvm commit: b4cf74dc9e8
Reproduce with: ```llc -O1 bbi-70835_x86.ll -o - -stop-after=loop-reduce```
Result: Depending on if we leave the dbg.value in the input or not we get
```
  define dso_local void @k() {
  entry:
    %l = alloca i16, align 1
    %0 = load i32, ptr @f, align 1
    %inc = add i32 %0, 1
    store i32 %inc, ptr @g, align 1
    call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %l)
    br label %for.cond
  
  for.cond:                                         ; preds = %for.cond, %entry
    %l.0.l.0.l.0. = load volatile i16, ptr %l, align 1
    %tobool.not = icmp eq i16 %l.0.l.0.l.0., 0
    br i1 %tobool.not, label %for.cond.cleanup, label %for.cond
  
  for.cond.cleanup:                                 ; preds = %for.cond
    call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %l)
    %1 = trunc i32 %inc to i16
    %arrayidx = getelementptr inbounds [0 x ptr], ptr @h, i16 0, i16 %1
    %2 = load ptr, ptr %arrayidx, align 1
    %3 = load ptr, ptr %2, align 1
    br label %for.body3
  
  for.cond.cleanup2:                                ; preds = %for.body3
    tail call void @j()
    ret void
  
  for.body3:                                        ; preds = %for.body3, %for.cond.cleanup
    %lsr.iv1 = phi i16 [ %lsr.iv.next2, %for.body3 ], [ 26, %for.cond.cleanup ]
    %lsr.iv = phi i32 [ %lsr.iv.next, %for.body3 ], [ %inc, %for.cond.cleanup ]
    store i32 %lsr.iv, ptr @f, align 1
    call void @llvm.dbg.value(metadata i32 %inc, metadata !8, metadata !DIExpression(DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_LLVM_convert, 16, DW_ATE_unsigned, DW_OP_stack_value)), !dbg !19
    %4 = load i16, ptr %3, align 1
    store volatile i16 %4, ptr @i, align 1
    %lsr.iv.next = add i32 %lsr.iv, 1
    %lsr.iv.next2 = add nsw i16 %lsr.iv1, -1
    %exitcond.not = icmp eq i16 %lsr.iv.next2, 0
    br i1 %exitcond.not, label %for.cond.cleanup2, label %for.body3
  }
```
with or
```
  define dso_local void @k() {
  entry:
    %l = alloca i16, align 1
    %0 = load i32, ptr @f, align 1
    %inc = add nsw i32 %0, 1
    store i32 %inc, ptr @g, align 1
    call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %l)
    br label %for.cond
  
  for.cond:                                         ; preds = %for.cond, %entry
    %l.0.l.0.l.0. = load volatile i16, ptr %l, align 1
    %tobool.not = icmp eq i16 %l.0.l.0.l.0., 0
    br i1 %tobool.not, label %for.cond.cleanup, label %for.cond
  
  for.cond.cleanup:                                 ; preds = %for.cond
    call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %l)
    %1 = trunc i32 %inc to i16
    %arrayidx = getelementptr inbounds [0 x ptr], ptr @h, i16 0, i16 %1
    %2 = load ptr, ptr %arrayidx, align 1
    %3 = load ptr, ptr %2, align 1
    %4 = add i32 %0, 1
    br label %for.body3
  
  for.cond.cleanup2:                                ; preds = %for.body3
    tail call void @j()
    ret void
  
  for.body3:                                        ; preds = %for.body3, %for.cond.cleanup
    %lsr.iv1 = phi i16 [ %lsr.iv.next2, %for.body3 ], [ 26, %for.cond.cleanup ]
    %lsr.iv = phi i32 [ %lsr.iv.next, %for.body3 ], [ %4, %for.cond.cleanup ]
    store i32 %lsr.iv, ptr @f, align 1
    %5 = load i16, ptr %3, align 1
    store volatile i16 %5, ptr @i, align 1
    %lsr.iv.next = add i32 %lsr.iv, 1
    %lsr.iv.next2 = add nsw i16 %lsr.iv1, -1
    %exitcond.not = icmp eq i16 %lsr.iv.next2, 0
    br i1 %exitcond.not, label %for.cond.cleanup2, label %for.body3
  }
```
without.

This starts happening with 8906a0fe64
```
    [SCEVExpander] Drop poison generating flags when reusing instructions
    
    The basic problem we have is that we're trying to reuse an instruction which is mapped to some SCEV. Since we can have multiple such instructions (potentially with different flags), this is analogous to our need to drop flags when performing CSE. A trivial implementation would simply drop flags on any instruction we decided to reuse, and that would be correct.
    
    This patch is almost that trivial patch except that we preserve flags on the reused instruction when existing users would imply UB on overflow already. Adding new users can, at most, refine this program to one which doesn't execute UB which is valid.
    
    In practice, this fixes two conceptual problems with the previous code: 1) a binop could have been canonicalized into a form with different opcode or operands, or 2) the inbounds GEP case which was simply unhandled.
    
    On the test changes, most are pretty straight forward. We loose some flags (in some cases, they'd have been dropped on the next CSE pass anyways). The one that took me the longest to understand was the ashr-expansion test. What's happening there is that we're considering reuse of the mul, previously we disallowed it entirely, now we allow it with no flags. The surrounding diffs are all effects of generating the same mul with a different operand order, and then doing simple DCE.
    
    The loss of the inbounds is unfortunate, but even there, we can recover most of those once we actually treat branch-on-poison as immediate UB.
    
    Differential Revision: https://reviews.llvm.org/D112734
```
[bbi-70835_x86.ll.gz](https://github.com/llvm/llvm-project/files/8899610/bbi-70835_x86.ll.gz)

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztWVtz27YS_jXyC0YckbJuD36II7vTmZxJJ8lpHz0guZLQkAALgJLVX3--BXShZClxT9uHdtxRY5HAXr_Ft0sqN-X2rqrWtShMXSvfG74T-W2xmNyWxYymvcG8N3j3iRpryrYgsVF-xVt640H8VFUh-h9TkeeqPxlMh6On5-k4qSrRN6Iv-s6bpi8XnmxvOK8MLiyxooP83oBrq2B7Tg3pUumlMFqohdiQqEiuSfgViTJfJmtZtSSUDjeUblovjBXaeN66JB8VnukXoqSF0tDgzFNlClmJtVGl6N0OvvayaS-bid7kfr-XtLdbOLO_FqKXjSqBCISsWFqodNzL3uNKLbVIT_YNwr7KyFKoYca7Gm_Z0OKahNJF1F0GkaCE93Z2IY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