[llvm] r269791 - [Guards] Add branch metadata when lowering
Sanjoy Das via llvm-commits
llvm-commits at lists.llvm.org
Tue May 17 10:51:19 PDT 2016
Author: sanjoy
Date: Tue May 17 12:51:19 2016
New Revision: 269791
URL: http://llvm.org/viewvc/llvm-project?rev=269791&view=rev
Log:
[Guards] Add branch metadata when lowering
Guards are expected to basically never fail. Reflect this in the branch
probabilities in their lowered form.
Modified:
llvm/trunk/lib/Transforms/Scalar/LowerGuardIntrinsic.cpp
llvm/trunk/test/Transforms/LowerGuardIntrinsic/basic.ll
Modified: llvm/trunk/lib/Transforms/Scalar/LowerGuardIntrinsic.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LowerGuardIntrinsic.cpp?rev=269791&r1=269790&r2=269791&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/LowerGuardIntrinsic.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/LowerGuardIntrinsic.cpp Tue May 17 12:51:19 2016
@@ -21,12 +21,18 @@
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/Pass.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
using namespace llvm;
+static cl::opt<uint32_t> LikelyBranchWeight(
+ "guards-likely-branch-weight", cl::Hidden, cl::init(1 << 20),
+ cl::desc("The probability of a guard failing is assumed to be the "
+ "reciprocal of this value (default = 1 << 20)"));
+
namespace {
struct LowerGuardIntrinsic : public FunctionPass {
static char ID;
@@ -59,6 +65,10 @@ static void MakeGuardControlFlowExplicit
if (auto *MD = CI->getMetadata(LLVMContext::MD_make_implicit))
CheckBI->setMetadata(LLVMContext::MD_make_implicit, MD);
+ MDBuilder MDB(CI->getContext());
+ CheckBI->setMetadata(LLVMContext::MD_prof,
+ MDB.createBranchWeights(LikelyBranchWeight, 1));
+
IRBuilder<> B(DeoptBlockTerm);
auto *DeoptCall = B.CreateCall(DeoptIntrinsic, Args, {DeoptOB}, "");
Modified: llvm/trunk/test/Transforms/LowerGuardIntrinsic/basic.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LowerGuardIntrinsic/basic.ll?rev=269791&r1=269790&r2=269791&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/LowerGuardIntrinsic/basic.ll (original)
+++ llvm/trunk/test/Transforms/LowerGuardIntrinsic/basic.ll Tue May 17 12:51:19 2016
@@ -9,7 +9,7 @@ define i8 @f_basic(i1* %c_ptr) {
call void(i1, ...) @llvm.experimental.guard(i1 %c, i32 1) [ "deopt"(i32 1) ]
ret i8 5
-; CHECK: br i1 %c, label %guarded, label %deopt
+; CHECK: br i1 %c, label %guarded, label %deopt, !prof !0
; CHECK: deopt:
; CHECK-NEXT: %deoptcall = call i8 (...) @llvm.experimental.deoptimize.i8(i32 1) [ "deopt"(i32 1) ]
; CHECK-NEXT: ret i8 %deoptcall
@@ -24,7 +24,7 @@ define void @f_void_return_ty(i1* %c_ptr
call void(i1, ...) @llvm.experimental.guard(i1 %c, i32 1) [ "deopt"() ]
ret void
-; CHECK: br i1 %c, label %guarded, label %deopt
+; CHECK: br i1 %c, label %guarded, label %deopt, !prof !0
; CHECK: deopt:
; CHECK-NEXT: call void (...) @llvm.experimental.deoptimize.isVoid(i32 1) [ "deopt"() ]
; CHECK-NEXT: ret void
@@ -39,7 +39,7 @@ define void @f_multiple_args(i1* %c_ptr)
call void(i1, ...) @llvm.experimental.guard(i1 %c, i32 1, i32 2, double 500.0) [ "deopt"(i32 2, i32 3) ]
ret void
-; CHECK: br i1 %c, label %guarded, label %deopt
+; CHECK: br i1 %c, label %guarded, label %deopt, !prof !0
; CHECK: deopt:
; CHECK-NEXT: call void (...) @llvm.experimental.deoptimize.isVoid(i32 1, i32 2, double 5.000000e+02) [ "deopt"(i32 2, i32 3) ]
; CHECK-NEXT: ret void
@@ -53,7 +53,7 @@ define i32 @f_zero_args(i1* %c_ptr) {
call void(i1, ...) @llvm.experimental.guard(i1 %c) [ "deopt"(i32 2, i32 3) ]
ret i32 500
-; CHECK: br i1 %c, label %guarded, label %deopt
+; CHECK: br i1 %c, label %guarded, label %deopt, !prof !0
; CHECK: deopt:
; CHECK-NEXT: %deoptcall = call i32 (...) @llvm.experimental.deoptimize.i32() [ "deopt"(i32 2, i32 3) ]
; CHECK-NEXT: ret i32 %deoptcall
@@ -63,7 +63,7 @@ define i32 @f_zero_args(i1* %c_ptr) {
define i8 @f_with_make_implicit_md(i32* %ptr) {
; CHECK-LABEL: @f_with_make_implicit_md(
-; CHECK: br i1 %notNull, label %guarded, label %deopt, !make.implicit !0
+; CHECK: br i1 %notNull, label %guarded, label %deopt, !prof !0, !make.implicit !1
; CHECK: deopt:
; CHECK-NEXT: %deoptcall = call i8 (...) @llvm.experimental.deoptimize.i8(i32 1) [ "deopt"(i32 1) ]
; CHECK-NEXT: ret i8 %deoptcall
@@ -72,3 +72,5 @@ define i8 @f_with_make_implicit_md(i32*
call void(i1, ...) @llvm.experimental.guard(i1 %notNull, i32 1) [ "deopt"(i32 1) ], !make.implicit !{}
ret i8 5
}
+
+!0 = !{!"branch_weights", i32 1048576, i32 1}
More information about the llvm-commits
mailing list