Enhance BranchProbabilityInfo::calcUnreachableHeuristics for InvokeInst
authorJun Bum Lim <junbuml@codeaurora.org>
Mon, 21 Dec 2015 22:00:51 +0000 (22:00 +0000)
committerJun Bum Lim <junbuml@codeaurora.org>
Mon, 21 Dec 2015 22:00:51 +0000 (22:00 +0000)
This is recommit of r256028 with minor fixes in unittests:
  CodeGen/Mips/eh.ll
  CodeGen/Mips/insn-zero-size-bb.ll

Original commit message:

When identifying blocks post-dominated by an unreachable-terminated block
in BranchProbabilityInfo, consider only the edge to the normal destination
block if the terminator is InvokeInst and let calcInvokeHeuristics() decide
edge weights for the InvokeInst.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@256202 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/BranchProbabilityInfo.cpp
test/Analysis/BranchProbabilityInfo/noreturn.ll
test/CodeGen/Mips/eh.ll
test/CodeGen/Mips/insn-zero-size-bb.ll
test/CodeGen/X86/catchret-empty-fallthrough.ll

index 6cdf43a06a9f559f47a725814e327420d9144f0b..a0d6123b5835fad20a3d1e210f203b5e3d951981 100644 (file)
@@ -147,6 +147,16 @@ bool BranchProbabilityInfo::calcUnreachableHeuristics(BasicBlock *BB) {
   if (TI->getNumSuccessors() == 1 || UnreachableEdges.empty())
     return false;
 
+  // If the terminator is an InvokeInst, check only the normal destination block
+  // as the unwind edge of InvokeInst is also very unlikely taken.
+  if (auto *II = dyn_cast<InvokeInst>(TI))
+    if (PostDominatedByUnreachable.count(II->getNormalDest())) {
+      PostDominatedByUnreachable.insert(BB);
+      // Return false here so that edge weights for InvokeInst could be decided
+      // in calcInvokeHeuristics().
+      return false;
+    }
+
   uint32_t UnreachableWeight =
     std::max(UR_TAKEN_WEIGHT / (unsigned)UnreachableEdges.size(), MIN_WEIGHT);
   for (SmallVectorImpl<unsigned>::iterator I = UnreachableEdges.begin(),
index 63926370cdab820b6e9d488dbcd458b7e3fb559c..513818d8da6ae67265b5ea6f4235ed39f38c2fd8 100644 (file)
@@ -77,3 +77,48 @@ abort:
 exit:
   ret i32 %b
 }
+
+@_ZTIi = external global i8*
+
+; CHECK-LABEL: throwSmallException
+; CHECK-NOT: invoke i32 @smallFunction
+define i32 @throwSmallException(i32 %idx, i32 %limit) #0 personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
+entry:
+  %cmp = icmp sge i32 %idx, %limit
+  br i1 %cmp, label %if.then, label %if.end
+; CHECK: edge entry -> if.then probability is 0x00000800 / 0x80000000 = 0.00%
+; CHECK: edge entry -> if.end probability is 0x7ffff800 / 0x80000000 = 100.00% [HOT edge]
+
+if.then:                                          ; preds = %entry
+  %exception = call i8* @__cxa_allocate_exception(i64 1) #0
+  invoke i32 @smallFunction(i32 %idx)
+          to label %invoke.cont unwind label %lpad
+; CHECK: edge if.then -> invoke.cont probability is 0x7ffff800 / 0x80000000 = 100.00% [HOT edge]
+; CHECK: edge if.then -> lpad probability is 0x00000800 / 0x80000000 = 0.00%
+
+invoke.cont:                                      ; preds = %if.then
+  call void @__cxa_throw(i8* %exception, i8* bitcast (i8** @_ZTIi  to i8*), i8* null) #1
+  unreachable
+
+lpad:                                             ; preds = %if.then
+  %ll = landingpad { i8*, i32 }
+          cleanup
+  ret i32 %idx
+
+if.end:                                           ; preds = %entry
+  ret i32 %idx
+}
+
+@a = global i32 4
+define i32 @smallFunction(i32 %a) {
+entry:
+  %r = load volatile i32, i32* @a
+  ret i32 %r
+}
+
+attributes #0 = { nounwind }
+attributes #1 = { noreturn }
+
+declare i8* @__cxa_allocate_exception(i64)
+declare i32 @__gxx_personality_v0(...)
+declare void @__cxa_throw(i8*, i8*, i8*)
index 2f843d9da9a612e317b9980968d70a6fad97fdf4..19f3d4d23d640dc449877e5480cba47bf8f9d9d9 100644 (file)
@@ -24,7 +24,7 @@ entry:
 
 lpad:                                             ; preds = %entry
 ; CHECK-EL:  # %lpad
-; CHECK-EL:  bne $5
+; CHECK-EL:  beq $5
 
   %exn.val = landingpad { i8*, i32 }
            cleanup
index a9c2069b3044ff5404a4a28bb5efabfece7aebb2..d2124c407a0d8eddc2c406b4ee9d8f67deb178ec 100644 (file)
@@ -14,7 +14,7 @@ entry:
           to label %unreachable unwind label %return
 
 unreachable:
-; CHECK:          ${{.*}}: # %unreachable
+; CHECK:          {{.*}}: # %unreachable
 ; CHECK-NEXT:         .insn
   unreachable
 
index 5405d570b15e396a86184f17c4bbbc8dba87125b..7ad10330317121275feb18fff56bc12bba8f33f6 100644 (file)
@@ -34,12 +34,14 @@ return:                                           ; preds = %catch, %entry
 
 ; CHECK-LABEL: foo: # @foo
 ; CHECK: testb $1, %cl
-; CHECK: jne .LBB0_[[return:[0-9]+]]
+; CHECK: je .LBB0_[[try:[0-9]+]]
+; CHECK: .LBB0_[[return:[0-9]+]]:
+; CHECK: retq
+; CHECK: .LBB0_[[try]]:
 ; CHECK: .Ltmp0:
 ; CHECK: callq bar
 ; CHECK: .Ltmp1:
 ; CHECK: .LBB0_[[catch:[0-9]+]]:
-; CHECK: .LBB0_[[return]]:
 
 ; CHECK: .seh_handlerdata
 ; CHECK-NEXT: .Lfoo$parent_frame_offset = 32