[PATCH] D106650: [SimplifyCFG] Don't speculatively execute BB if it's predictably not taken

Roman Lebedev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 25 17:01:32 PDT 2021


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGe58ce35f7b67: [SimplifyCFG] Don't speculatively execute BB if it's predictably not taken (authored by lebedev.ri).

Changed prior to commit:
  https://reviews.llvm.org/D106650?vs=361157&id=361559#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106650/new/

https://reviews.llvm.org/D106650

Files:
  llvm/lib/Transforms/Utils/SimplifyCFG.cpp
  llvm/test/Transforms/SimplifyCFG/speculatively-execute-block-profmd.ll


Index: llvm/test/Transforms/SimplifyCFG/speculatively-execute-block-profmd.ll
===================================================================
--- llvm/test/Transforms/SimplifyCFG/speculatively-execute-block-profmd.ll
+++ llvm/test/Transforms/SimplifyCFG/speculatively-execute-block-profmd.ll
@@ -83,11 +83,12 @@
 ; CHECK:       dispatch:
 ; CHECK-NEXT:    call void @sideeffect1()
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT:    br i1 [[CMP]], label [[END]], label [[COND_TRUE:%.*]], !prof [[PROF0]]
+; CHECK:       cond.true:
 ; CHECK-NEXT:    [[VAL:%.*]] = add i32 [[A]], [[B]]
-; CHECK-NEXT:    [[SPEC_SELECT:%.*]] = select i1 [[CMP]], i32 0, i32 [[VAL]], !prof [[PROF0]]
 ; CHECK-NEXT:    br label [[END]]
 ; CHECK:       end:
-; CHECK-NEXT:    [[RES:%.*]] = phi i32 [ -1, [[ENTRY:%.*]] ], [ [[SPEC_SELECT]], [[DISPATCH]] ]
+; CHECK-NEXT:    [[RES:%.*]] = phi i32 [ -1, [[ENTRY:%.*]] ], [ 0, [[DISPATCH]] ], [ [[VAL]], [[COND_TRUE]] ]
 ; CHECK-NEXT:    call void @sideeffect2()
 ; CHECK-NEXT:    ret i32 [[RES]]
 ;
Index: llvm/lib/Transforms/Utils/SimplifyCFG.cpp
===================================================================
--- llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -2372,6 +2372,20 @@
   }
   assert(EndBB == BI->getSuccessor(!Invert) && "No edge from to end block");
 
+  // If the branch is non-unpredictable, and is predicted to *not* branch to
+  // the `then` block, then avoid speculating it.
+  if (!BI->getMetadata(LLVMContext::MD_unpredictable)) {
+    uint64_t TWeight, FWeight;
+    if (BI->extractProfMetadata(TWeight, FWeight) && (TWeight + FWeight) != 0) {
+      uint64_t EndWeight = Invert ? TWeight : FWeight;
+      BranchProbability BIEndProb =
+          BranchProbability::getBranchProbability(EndWeight, TWeight + FWeight);
+      BranchProbability Likely = TTI.getPredictableBranchThreshold();
+      if (BIEndProb >= Likely)
+        return false;
+    }
+  }
+
   // Keep a count of how many times instructions are used within ThenBB when
   // they are candidates for sinking into ThenBB. Specifically:
   // - They are defined in BB, and


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D106650.361559.patch
Type: text/x-patch
Size: 2170 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210726/505e2079/attachment.bin>


More information about the llvm-commits mailing list