[llvm] r371541 - [BPI] Adjust the probability for floating point unordered comparison

Guozhi Wei via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 10 10:25:11 PDT 2019


Author: carrot
Date: Tue Sep 10 10:25:11 2019
New Revision: 371541

URL: http://llvm.org/viewvc/llvm-project?rev=371541&view=rev
Log:
[BPI] Adjust the probability for floating point unordered comparison

Since NaN is very rare in normal programs, so the probability for floating point unordered comparison should be extremely small. Current probability is 3/8, it is too large, this patch changes it to a tiny number.

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


Added:
    llvm/trunk/test/Analysis/BranchProbabilityInfo/fcmp.ll
Modified:
    llvm/trunk/lib/Analysis/BranchProbabilityInfo.cpp
    llvm/trunk/test/CodeGen/SystemZ/call-05.ll

Modified: llvm/trunk/lib/Analysis/BranchProbabilityInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/BranchProbabilityInfo.cpp?rev=371541&r1=371540&r2=371541&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/BranchProbabilityInfo.cpp (original)
+++ llvm/trunk/lib/Analysis/BranchProbabilityInfo.cpp Tue Sep 10 10:25:11 2019
@@ -118,6 +118,13 @@ static const uint32_t ZH_NONTAKEN_WEIGHT
 static const uint32_t FPH_TAKEN_WEIGHT = 20;
 static const uint32_t FPH_NONTAKEN_WEIGHT = 12;
 
+/// This is the probability for an ordered floating point comparison.
+static const uint32_t FPH_ORD_WEIGHT = 1024 * 1024 - 1;
+/// This is the probability for an unordered floating point comparison, it means
+/// one or two of the operands are NaN. Usually it is used to test for an
+/// exceptional case, so the result is unlikely.
+static const uint32_t FPH_UNO_WEIGHT = 1;
+
 /// Invoke-terminating normal branch taken weight
 ///
 /// This is the weight for branching to the normal destination of an invoke
@@ -778,6 +785,8 @@ bool BranchProbabilityInfo::calcFloating
   if (!FCmp)
     return false;
 
+  uint32_t TakenWeight = FPH_TAKEN_WEIGHT;
+  uint32_t NontakenWeight = FPH_NONTAKEN_WEIGHT;
   bool isProb;
   if (FCmp->isEquality()) {
     // f1 == f2 -> Unlikely
@@ -786,9 +795,13 @@ bool BranchProbabilityInfo::calcFloating
   } else if (FCmp->getPredicate() == FCmpInst::FCMP_ORD) {
     // !isnan -> Likely
     isProb = true;
+    TakenWeight = FPH_ORD_WEIGHT;
+    NontakenWeight = FPH_UNO_WEIGHT;
   } else if (FCmp->getPredicate() == FCmpInst::FCMP_UNO) {
     // isnan -> Unlikely
     isProb = false;
+    TakenWeight = FPH_ORD_WEIGHT;
+    NontakenWeight = FPH_UNO_WEIGHT;
   } else {
     return false;
   }
@@ -798,8 +811,7 @@ bool BranchProbabilityInfo::calcFloating
   if (!isProb)
     std::swap(TakenIdx, NonTakenIdx);
 
-  BranchProbability TakenProb(FPH_TAKEN_WEIGHT,
-                              FPH_TAKEN_WEIGHT + FPH_NONTAKEN_WEIGHT);
+  BranchProbability TakenProb(TakenWeight, TakenWeight + NontakenWeight);
   setEdgeProbability(BB, TakenIdx, TakenProb);
   setEdgeProbability(BB, NonTakenIdx, TakenProb.getCompl());
   return true;

Added: llvm/trunk/test/Analysis/BranchProbabilityInfo/fcmp.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/BranchProbabilityInfo/fcmp.ll?rev=371541&view=auto
==============================================================================
--- llvm/trunk/test/Analysis/BranchProbabilityInfo/fcmp.ll (added)
+++ llvm/trunk/test/Analysis/BranchProbabilityInfo/fcmp.ll Tue Sep 10 10:25:11 2019
@@ -0,0 +1,41 @@
+; RUN: opt < %s -analyze -branch-prob | FileCheck %s
+
+; This function tests the floating point unorder comparison. The probability
+; of NaN should be extremely small.
+; CHECK: Printing analysis 'Branch Probability Analysis' for function 'uno'
+; CHECK:  edge  -> a probability is 0x00000800 / 0x80000000 = 0.00%
+; CHECK:  edge  -> b probability is 0x7ffff800 / 0x80000000 = 100.00% [HOT edge]
+
+define void @uno(float %val1, float %val2) {
+  %cond = fcmp uno float %val1, %val2
+  br i1 %cond, label %a, label %b
+
+a:
+  call void @fa()
+  ret void
+
+b:
+  call void @fb()
+  ret void
+}
+
+; This function tests the floating point order comparison.
+; CHECK: Printing analysis 'Branch Probability Analysis' for function 'ord'
+; CHECK:  edge  -> a probability is 0x7ffff800 / 0x80000000 = 100.00% [HOT edge]
+; CHECK:  edge  -> b probability is 0x00000800 / 0x80000000 = 0.00%
+
+define void @ord(float %val1, float %val2) {
+  %cond = fcmp ord float %val1, %val2
+  br i1 %cond, label %a, label %b
+
+a:
+  call void @fa()
+  ret void
+
+b:
+  call void @fb()
+  ret void
+}
+
+declare void @fa()
+declare void @fb()

Modified: llvm/trunk/test/CodeGen/SystemZ/call-05.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/SystemZ/call-05.ll?rev=371541&r1=371540&r2=371541&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/SystemZ/call-05.ll (original)
+++ llvm/trunk/test/CodeGen/SystemZ/call-05.ll Tue Sep 10 10:25:11 2019
@@ -451,8 +451,9 @@ b:
 define void @f25(float %val1, float %val2) {
 ; CHECK-LABEL: f25:
 ; CHECK: cebr %f0, %f2
-; CHECK: bor %r1
+; CHECK: jo
 ; CHECK: br %r14
+; CHECK: br %r1
   %fun_a = load volatile void() *, void()** @fun_a;
   %cond = fcmp uno float %val1, %val2;
   br i1 %cond, label %a, label %b;




More information about the llvm-commits mailing list