[PATCH] D86017: [BPI] Improve static heuristics for integer comparisons with CST

Dávid Bolvanský via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 15 03:31:37 PDT 2020


xbolva00 created this revision.
xbolva00 added a reviewer: ebrevnov.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.
xbolva00 requested review of this revision.

For integers a == CST is usually false.

This heuristic is part of many papers about static branch prediction. GCC also uses this heuristic for C \ {0, 1}, since 0, 1 constants are often used as bools.

Tested with zstd bench, decompression speed was improved a bit.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D86017

Files:
  llvm/lib/Analysis/BranchProbabilityInfo.cpp
  llvm/test/Analysis/BranchProbabilityInfo/integer_heuristics.ll


Index: llvm/test/Analysis/BranchProbabilityInfo/integer_heuristics.ll
===================================================================
--- llvm/test/Analysis/BranchProbabilityInfo/integer_heuristics.ll
+++ llvm/test/Analysis/BranchProbabilityInfo/integer_heuristics.ll
@@ -152,3 +152,37 @@
   %r = sext i8 %v to i32
   ret i32 %r
 }
+
+
+; CHECK-LABEL: cst1
+define i32 @cst1(i32 %x, i8 signext %z, i8 signext %w) {
+entry: 
+  %c = icmp ne i32 %x, 7
+  br i1 %c, label %then, label %else
+; CHECK: edge entry -> then probability is 0x50000000 / 0x80000000 = 62.50%
+; CHECK: edge entry -> else probability is 0x30000000 / 0x80000000 = 37.50%
+then:
+  br label %else
+; CHECK: edge then -> else probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
+else:
+  %v = phi i8 [ %z, %then ], [ %w, %entry ]
+  %r = sext i8 %v to i32
+  ret i32 %r
+}
+
+; CHECK-LABEL: cst2
+define i32 @cst2(i32 %x, i8 signext %z, i8 signext %w) {
+entry: 
+  %c = icmp eq i32 %x, -5
+  br i1 %c, label %then, label %else
+; CHECK: edge entry -> then probability is 0x30000000 / 0x80000000 = 37.50%
+; CHECK: edge entry -> else probability is 0x50000000 / 0x80000000 = 62.50%
+then:
+  br label %else
+; CHECK: edge then -> else probability is 0x80000000 / 0x80000000 = 100.00% [HOT edge]
+else:
+  %v = phi i8 [ %z, %then ], [ %w, %entry ]
+  %r = sext i8 %v to i32
+  ret i32 %r
+}
+
Index: llvm/lib/Analysis/BranchProbabilityInfo.cpp
===================================================================
--- llvm/lib/Analysis/BranchProbabilityInfo.cpp
+++ llvm/lib/Analysis/BranchProbabilityInfo.cpp
@@ -949,10 +949,13 @@
     default:
       return false;
     }
-  } else if (CV->isOne() && CI->getPredicate() == CmpInst::ICMP_SLT) {
-    // InstCombine canonicalizes X <= 0 into X < 1.
-    // X <= 0   ->  Unlikely
-    isProb = false;
+  } else if (CV->isOne()) {
+    if (CI->getPredicate() == CmpInst::ICMP_SLT)
+      // InstCombine canonicalizes X <= 0 into X < 1.
+      // X <= 0   ->  Unlikely
+      isProb = false;
+    else
+      return false;
   } else if (CV->isMinusOne()) {
     switch (CI->getPredicate()) {
     case CmpInst::ICMP_EQ:
@@ -972,7 +975,18 @@
       return false;
     }
   } else {
-    return false;
+    // X == C -> Unlikely
+    // X != C -> Likely
+    switch (CI->getPredicate()) {
+    case CmpInst::ICMP_EQ:
+      isProb = false;
+      break;
+    case CmpInst::ICMP_NE:
+      isProb = true;
+      break;
+    default:
+      return false;
+    }
   }
 
   if (!isProb)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D86017.285835.patch
Type: text/x-patch
Size: 2506 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200815/7b21da9f/attachment.bin>


More information about the llvm-commits mailing list