[PATCH] Don't pretend to have probability information if the result of a bitwise and is compared against 0

Daniel Jasper djasper at google.com
Mon Mar 30 05:20:53 PDT 2015


- Addressed review comments
- Extended change to look at more comparisons
- Narrowed check to only trigger on single bit bitmasks
- Added specific branch probability tests

All test changes now seem correct.


http://reviews.llvm.org/D8165

Files:
  lib/Analysis/BranchProbabilityInfo.cpp
  test/Analysis/BranchProbabilityInfo/basic.ll
  test/CodeGen/AArch64/arm64-call-tailcalls.ll
  test/CodeGen/Mips/octeon.ll

Index: lib/Analysis/BranchProbabilityInfo.cpp
===================================================================
--- lib/Analysis/BranchProbabilityInfo.cpp
+++ lib/Analysis/BranchProbabilityInfo.cpp
@@ -379,6 +379,17 @@
   if (!CV)
     return false;
 
+  // If the LHS is the result of AND'ing a value with a single bit bitmask,
+  // we don't have information about probabilities.
+  if (Instruction *LHS = dyn_cast<Instruction>(CI->getOperand(0))) {
+    if (LHS->getOpcode() == Instruction::And) {
+      if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) {
+        if (AndRHS->getUniqueInteger().isPowerOf2())
+          return false;
+      }
+    }
+  }
+
   bool isProb;
   if (CV->isZero()) {
     switch (CI->getPredicate()) {
Index: test/Analysis/BranchProbabilityInfo/basic.ll
===================================================================
--- test/Analysis/BranchProbabilityInfo/basic.ll
+++ test/Analysis/BranchProbabilityInfo/basic.ll
@@ -212,3 +212,31 @@
   ret i32 %result
 }
 
+define i32 @zero3(i32 %i, i32 %a, i32 %b) {
+; CHECK: Printing analysis {{.*}} for function 'zero3'
+entry:
+; AND'ing with a single bit bitmask essentially leads to a bool comparison,
+; meaning we don't have probability information.
+  %and = and i32 %i, 2
+  %tobool = icmp eq i32 %and, 0
+  br i1 %tobool, label %then, label %else
+; CHECK: edge entry -> then probability is 16 / 32
+; CHECK: edge entry -> else probability is 16 / 32
+
+then:
+; AND'ing with other bitmask might be something else, so we still assume the
+; usual probabilities.
+  %and2 = and i32 %i, 5
+  %tobool2 = icmp eq i32 %and2, 0
+  br i1 %tobool2, label %else, label %exit
+; CHECK: edge then -> else probability is 12 / 32
+; CHECK: edge then -> exit probability is 20 / 32
+
+else:
+  br label %exit
+
+exit:
+  %result = phi i32 [ %a, %then ], [ %b, %else ]
+  ret i32 %result
+}
+
Index: test/CodeGen/AArch64/arm64-call-tailcalls.ll
===================================================================
--- test/CodeGen/AArch64/arm64-call-tailcalls.ll
+++ test/CodeGen/AArch64/arm64-call-tailcalls.ll
@@ -53,9 +53,9 @@
 
 define i32 @t8(i32 %x) nounwind ssp {
 ; CHECK-LABEL: t8:
+; CHECK: b	_c
 ; CHECK: b	_a
 ; CHECK: b	_b
-; CHECK: b	_c
   %and = and i32 %x, 1
   %tobool = icmp eq i32 %and, 0
   br i1 %tobool, label %if.end, label %if.then
Index: test/CodeGen/Mips/octeon.ll
===================================================================
--- test/CodeGen/Mips/octeon.ll
+++ test/CodeGen/Mips/octeon.ll
@@ -93,7 +93,7 @@
 ; ALL-LABEL: bbit0:
 ; OCTEON: bbit0   $4, 3, $[[BB0:BB[0-9_]+]]
 ; MIPS64: andi  $[[T0:[0-9]+]], $4, 8
-; MIPS64: beqz  $[[T0]], $[[BB0:BB[0-9_]+]]
+; MIPS64: bnez  $[[T0]], $[[BB0:BB[0-9_]+]]
   %bit = and i64 %a, 8
   %res = icmp eq i64 %bit, 0
   br i1 %res, label %endif, label %if
@@ -111,7 +111,7 @@
 ; MIPS64: daddiu  $[[T0:[0-9]+]], $zero, 1
 ; MIPS64: dsll    $[[T1:[0-9]+]], $[[T0]], 35
 ; MIPS64: and     $[[T2:[0-9]+]], $4, $[[T1]]
-; MIPS64: beqz    $[[T2]], $[[BB0:BB[0-9_]+]]
+; MIPS64: bnez    $[[T2]], $[[BB0:BB[0-9_]+]]
   %bit = and i64 %a, 34359738368
   %res = icmp eq i64 %bit, 0
   br i1 %res, label %endif, label %if

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D8165.22878.patch
Type: text/x-patch
Size: 3177 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150330/05bdaf9c/attachment.bin>


More information about the llvm-commits mailing list