[SimplifyCFG] Skip dbg intrinsics when checking for branch-only BBs.

Debug intrinsics are free to hoist and should be skipped when looking
for terminator-only blocks. As a consequence, we have to delegate to the
main hoisting loop to hoist any dbg intrinsics instead of jumping to the
terminator case directly.

This fixes PR49982.

Reviewed By: lebedev.ri

Differential Revision: https://reviews.llvm.org/D100640

GitOrigin-RevId: af523514c4b9e0bd04bffb1f6ca2922c83df4c36
diff --git a/lib/Transforms/Utils/SimplifyCFG.cpp b/lib/Transforms/Utils/SimplifyCFG.cpp
index cb98227..3a2da84 100644
--- a/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -1434,11 +1434,15 @@
   // Check if only hoisting terminators is allowed. This does not add new
   // instructions to the hoist location.
   if (EqTermsOnly) {
-    if (!I1->isIdenticalToWhenDefined(I2))
+    // Skip any debug intrinsics, as they are free to hoist.
+    auto *I1NonDbg = &*skipDebugIntrinsics(I1->getIterator());
+    auto *I2NonDbg = &*skipDebugIntrinsics(I2->getIterator());
+    if (!I1NonDbg->isIdenticalToWhenDefined(I2NonDbg))
       return false;
-    if (!I1->isTerminator())
+    if (!I1NonDbg->isTerminator())
       return false;
-    goto HoistTerminator;
+    // Now we know that we only need to hoist debug instrinsics and the
+    // terminator. Let the loop below handle those 2 cases.
   }
 
   do {
diff --git a/test/Transforms/SimplifyCFG/hoist-dbgvalue.ll b/test/Transforms/SimplifyCFG/hoist-dbgvalue.ll
index aff0ac3..ca0c0b6 100644
--- a/test/Transforms/SimplifyCFG/hoist-dbgvalue.ll
+++ b/test/Transforms/SimplifyCFG/hoist-dbgvalue.ll
@@ -45,8 +45,10 @@
 ; CHECK-LABEL: @hoist_with_debug2(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[TOBOOL_NOT:%.*]] = icmp ugt i32 [[X:%.*]], 2
-; CHECK-NEXT:    [[P:%.*]] = select i1 [[TOBOOL_NOT]], i1 false, i1 true
-; CHECK-NEXT:    ret i1 [[P]]
+; CHECK-NEXT:    call void @llvm.dbg.value(metadata i32 [[X]], metadata [[META21:![0-9]+]], metadata !DIExpression()), !dbg [[DBG23:![0-9]+]]
+; CHECK-NEXT:    call void @llvm.dbg.value(metadata i32 [[X]], metadata [[META21]], metadata !DIExpression()), !dbg [[DBG23]]
+; CHECK-NEXT:    [[DOT:%.*]] = select i1 [[TOBOOL_NOT]], i1 false, i1 true
+; CHECK-NEXT:    ret i1 [[DOT]]
 ;
 entry:
   %tobool.not = icmp ugt i32 %x, 2
@@ -72,15 +74,11 @@
 ; CHECK-NEXT:    br label [[FOR_COND:%.*]]
 ; CHECK:       for.cond:
 ; CHECK-NEXT:    [[C_0:%.*]] = icmp sgt i32 [[X:%.*]], 0
-; CHECK-NEXT:    br i1 [[C_0]], label [[CHECK:%.*]], label [[LATCH:%.*]]
-; CHECK:       check:
-; CHECK-NEXT:    [[C_1:%.*]] = icmp ugt i32 [[X]], 2
-; CHECK-NEXT:    br label [[EXIT_1:%.*]]
-; CHECK:       latch:
-; CHECK-NEXT:    br i1 [[C_2:%.*]], label [[EXIT_1]], label [[FOR_COND]]
+; CHECK-NEXT:    [[BRMERGE:%.*]] = or i1 [[C_0]], [[C_2:%.*]]
+; CHECK-NEXT:    [[DOTMUX:%.*]] = select i1 [[C_0]], i16 0, i16 20
+; CHECK-NEXT:    br i1 [[BRMERGE]], label [[EXIT_1:%.*]], label [[FOR_COND]]
 ; CHECK:       exit.1:
-; CHECK-NEXT:    [[MERGE:%.*]] = phi i16 [ 20, [[LATCH]] ], [ 0, [[CHECK]] ]
-; CHECK-NEXT:    ret i16 [[MERGE]]
+; CHECK-NEXT:    ret i16 [[DOTMUX]]
 ;
 entry:
   br label %for.cond