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

    <tr>
        <th>Summary</th>
        <td>
            Wrong loop-metadata for nested loop after loop-simplify splitting out a new outer loop
        </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: 4bf84e433d
Reproduce with: ```opt < bbi-73295.ll -passes='print<loops>,loop-simplify,print<loops>' -S -debug```
[bbi-73295.ll.gz](https://github.com/llvm/llvm-project/files/9505467/bbi-73295.ll.gz)

Result:
```
Parallel Loop at depth 1 containing: %while.cond<header>,%lor.end<exiting>,%while.body<latch>,%while.body.thread<latch>
LoopSimplify: Splitting out a new outer loop
Parallel Loop at depth 1 containing: %while.cond.outer<header>,%lor.end<exiting>,%while.body<latch>,%while.body.thread,%while.cond
    Parallel Loop at depth 2 containing: %while.cond<header><exiting>,%while.body.thread<latch>
; ModuleID = 'bbi-73295.ll'
source_filename = "bbi-73295.ll"

define i16 @foo() {
entry:
  br label %while.cond.outer

while.cond.outer:                                 ; preds = %while.body, %entry
  %i.0.ph = phi i16 [ %inc, %while.body ], [ 0, %entry ]
  %j.0.ph = phi i16 [ %spec.select7, %while.body ], [ 0, %entry ]
  br label %while.cond

while.cond:                                       ; preds = %while.cond.outer, %while.body.thread
  %i.0 = phi i16 [ %inc4, %while.body.thread ], [ %i.0.ph, %while.cond.outer ]
  %cmp = icmp slt i16 %i.0, 10
  br i1 %cmp, label %while.body.thread, label %lor.end

while.body.thread:                                ; preds = %while.cond
  %inc4 = add nsw i16 %i.0, 1
  br label %while.cond, !llvm.loop !5

lor.end:                                          ; preds = %while.cond
  %cmp1.not = icmp eq i16 %j.0.ph, 5
  br i1 %cmp1.not, label %while.end, label %while.body

while.body:                                       ; preds = %lor.end
  %inc = add nuw nsw i16 %i.0, 1
  %cmp2 = icmp eq i16 %inc, 12
  %spec.select7 = select i1 %cmp2, i16 5, i16 %j.0.ph
  br label %while.cond.outer, !llvm.loop !5

while.end:                                        ; preds = %lor.end
  ret i16 %i.0
}

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 7, !"Dwarf Version", i32 4}
!1 = !{i32 2, !"Debug Info Version", i32 3}
!2 = !{i32 1, !"wchar_size", i32 1}
!3 = !{i32 7, !"frame-pointer", i32 2}
!4 = !{!"clang version 16.0.0.prerel"}
!5 = distinct !{!5, !6}
!6 = !{!"llvm.loop.usefulinfo"}
```
So instead of having one loop with two latches, after LoopSimplifyCFG we have two nested loops with one latch each.
Since these are two different loops, then I really think that they should have different loop ID:s.
Also the "usefulinfo" metadata should be dropped, at least for the inner loop, but not sure if we can guarantee that it is valid for the outer loop either.

(We originally found this for our out-of-tree target where the loop-metadata specified made sense for the original program, but it did not do it for the inner loop after loop-simplify's rewrite to nested loops.)

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy1V1Fv4zYM_jXOixDDlmM7echD2-yGAhswrMDu8SBbcqw7RfIkuVnv14-SnVhOk7Z3w4K0sS3yI_mRouhK0ZetEM8HVKvDgdsou0Orqlmv2CrLaJTsouTuT9ZpRfuaoSO3rZOIimT4qs6iKHtAVcWXZYY3eSwEWnbEGGaibBfhstNcAuqDUKqDR79E2F8uDT90gjcvcP9apETLJ7SkrOr3Z1ODL1F-H9qK99-jHMysW2ud7l2EP8F3D372VQwhwY2LbvxZQiBfWW3htuECXMSfNnmSr4oSri5x8WY0OZJgeuHoGR_O3fqDaCIEE-g3iAERiyjrbItSYFVawiWXe88bzo8tGAbPJIWIW0Yo0wMrsCaUjplfYP9w63XGlUGrgmQ5noit2ytLsW01AIYS3jnn09OJbvDiCa6sg0eqt4ggyY7uimnkMvDzAcUe5H8LK3js6fN-Ivjc8BV_mPy3HLtFap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