[llvm] 5986559 - [SimplifyCFG] Guard branch folding by speculate blocks flag

Teresa Johnson via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 25 06:46:45 PDT 2023


Author: Teresa Johnson
Date: 2023-07-25T06:46:19-07:00
New Revision: 5986559caa400ba6d977fb9d2224b79b52968b8e

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

LOG: [SimplifyCFG] Guard branch folding by speculate blocks flag

Guard FoldBranchToCommonDest in SimplifyCFG with the SpeculateBlocks
flag as it can also speculate instructions.

This was split out of D155997.

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

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/SimplifyCFG.cpp
    llvm/test/Transforms/SimplifyCFG/speculate-blocks.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index d3a9a41aef1533..1d4cb851da4a21 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -6978,7 +6978,8 @@ bool SimplifyCFGOpt::simplifyUncondBranch(BranchInst *BI,
   // branches to us and our successor, fold the comparison into the
   // predecessor and use logical operations to update the incoming value
   // for PHI nodes in common successor.
-  if (FoldBranchToCommonDest(BI, DTU, /*MSSAU=*/nullptr, &TTI,
+  if (Options.SpeculateBlocks &&
+      FoldBranchToCommonDest(BI, DTU, /*MSSAU=*/nullptr, &TTI,
                              Options.BonusInstThreshold))
     return requestResimplify();
   return false;
@@ -7048,7 +7049,8 @@ bool SimplifyCFGOpt::simplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) {
   // If this basic block is ONLY a compare and a branch, and if a predecessor
   // branches to us and one of our successors, fold the comparison into the
   // predecessor and use logical operations to pick the right destination.
-  if (FoldBranchToCommonDest(BI, DTU, /*MSSAU=*/nullptr, &TTI,
+  if (Options.SpeculateBlocks &&
+      FoldBranchToCommonDest(BI, DTU, /*MSSAU=*/nullptr, &TTI,
                              Options.BonusInstThreshold))
     return requestResimplify();
 

diff  --git a/llvm/test/Transforms/SimplifyCFG/speculate-blocks.ll b/llvm/test/Transforms/SimplifyCFG/speculate-blocks.ll
index 9921942bbdea1a..07eac90186ef62 100644
--- a/llvm/test/Transforms/SimplifyCFG/speculate-blocks.ll
+++ b/llvm/test/Transforms/SimplifyCFG/speculate-blocks.ll
@@ -27,3 +27,54 @@ bb2:
   %r = phi i32 [ 2, %entry ], [ 5, %bb ]
   ret i32 %r
 }
+
+define void @fold_branch_to_common_dest(i8 %v0, i8 %v1) {
+; YES-LABEL: define void @fold_branch_to_common_dest
+; YES-SAME: (i8 [[V0:%.*]], i8 [[V1:%.*]]) {
+; YES-NEXT:  pred:
+; YES-NEXT:    [[C0:%.*]] = icmp eq i8 [[V0]], 0
+; YES-NEXT:    [[C1:%.*]] = icmp eq i8 [[V1]], 0
+; YES-NEXT:    [[OR_COND:%.*]] = select i1 [[C0]], i1 [[C1]], i1 false
+; YES-NEXT:    br i1 [[OR_COND]], label [[FINAL_LEFT:%.*]], label [[FINAL_RIGHT:%.*]]
+; YES:       common.ret:
+; YES-NEXT:    ret void
+; YES:       final_left:
+; YES-NEXT:    call void @sideeffect0()
+; YES-NEXT:    br label [[COMMON_RET:%.*]]
+; YES:       final_right:
+; YES-NEXT:    call void @sideeffect1()
+; YES-NEXT:    br label [[COMMON_RET]]
+;
+; NO-LABEL: define void @fold_branch_to_common_dest
+; NO-SAME: (i8 [[V0:%.*]], i8 [[V1:%.*]]) {
+; NO-NEXT:  pred:
+; NO-NEXT:    [[C0:%.*]] = icmp eq i8 [[V0]], 0
+; NO-NEXT:    br i1 [[C0]], label [[DISPATCH:%.*]], label [[FINAL_RIGHT:%.*]]
+; NO:       dispatch:
+; NO-NEXT:    [[C1:%.*]] = icmp eq i8 [[V1]], 0
+; NO-NEXT:    br i1 [[C1]], label [[FINAL_LEFT:%.*]], label [[FINAL_RIGHT]]
+; NO:       common.ret:
+; NO-NEXT:    ret void
+; NO:       final_left:
+; NO-NEXT:    call void @sideeffect0()
+; NO-NEXT:    br label [[COMMON_RET:%.*]]
+; NO:       final_right:
+; NO-NEXT:    call void @sideeffect1()
+; NO-NEXT:    br label [[COMMON_RET]]
+;
+pred:
+  %c0 = icmp eq i8 %v0, 0
+  br i1 %c0, label %dispatch, label %final_right
+dispatch:
+  %c1 = icmp eq i8 %v1, 0
+  br i1 %c1, label %final_left, label %final_right
+final_left:
+  call void @sideeffect0()
+  ret void
+final_right:
+  call void @sideeffect1()
+  ret void
+}
+
+declare void @sideeffect0()
+declare void @sideeffect1()


        


More information about the llvm-commits mailing list