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

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 17 07:18:20 PDT 2021


Author: Florian Hahn
Date: 2021-04-17T15:17:50+01:00
New Revision: af523514c4b9e0bd04bffb1f6ca2922c83df4c36

URL: https://github.com/llvm/llvm-project/commit/af523514c4b9e0bd04bffb1f6ca2922c83df4c36
DIFF: https://github.com/llvm/llvm-project/commit/af523514c4b9e0bd04bffb1f6ca2922c83df4c36.diff

LOG: [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

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/SimplifyCFG.cpp
    llvm/test/Transforms/SimplifyCFG/hoist-dbgvalue.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index cb98227e0bf78..3a2da84e0338a 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -1434,11 +1434,15 @@ bool SimplifyCFGOpt::HoistThenElseCodeToIf(BranchInst *BI,
   // 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/llvm/test/Transforms/SimplifyCFG/hoist-dbgvalue.ll b/llvm/test/Transforms/SimplifyCFG/hoist-dbgvalue.ll
index aff0ac318357d..ca0c0b6140c84 100644
--- a/llvm/test/Transforms/SimplifyCFG/hoist-dbgvalue.ll
+++ b/llvm/test/Transforms/SimplifyCFG/hoist-dbgvalue.ll
@@ -45,8 +45,10 @@ define i1 @hoist_with_debug2(i32 %x) !dbg !22 {
 ; 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 @@ define i16 @hoist_with_debug3_pr49982(i32 %x, i1 %c.2) !dbg !26 {
 ; 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


        


More information about the llvm-commits mailing list