]> granicus.if.org Git - clang/commitdiff
[ubsan] Emit branch weight metadata to hint towards common case.
authorWill Dietz <wdietz2@illinois.edu>
Sat, 15 Dec 2012 01:39:14 +0000 (01:39 +0000)
committerWill Dietz <wdietz2@illinois.edu>
Sat, 15 Dec 2012 01:39:14 +0000 (01:39 +0000)
Results in better block placement that helps close the
performance gap when making ubsan checks recoverable.

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

lib/CodeGen/CGExpr.cpp
test/CodeGen/catch-undef-behavior.c

index d74ae88990791764853bd2774898159a983154dc..0be483a9071a242fa37dcfc949ea24d2ae80d739 100644 (file)
@@ -1973,7 +1973,15 @@ void CodeGenFunction::EmitCheck(llvm::Value *Checked, StringRef CheckName,
   llvm::BasicBlock *Cont = createBasicBlock("cont");
 
   llvm::BasicBlock *Handler = createBasicBlock("handler." + CheckName);
-  Builder.CreateCondBr(Checked, Cont, Handler);
+
+  llvm::Instruction *Branch = Builder.CreateCondBr(Checked, Cont, Handler);
+
+  // Give hint that we very much don't expect to execute the handler
+  // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp
+  llvm::MDBuilder MDHelper(getLLVMContext());
+  llvm::MDNode *Node = MDHelper.createBranchWeights((1U << 20) - 1, 1);
+  Branch->setMetadata(llvm::LLVMContext::MD_prof, Node);
+
   EmitBlock(Handler);
 
   llvm::Constant *Info = llvm::ConstantStruct::getAnon(StaticArgs);
index 088e3a9e0f0476735be01b78b06ccc9ae2c07144..302ff2ee49704ac3e157ee1ee9b6f2f156342766 100644 (file)
@@ -38,7 +38,7 @@ void foo() {
   // CHECK-NEXT: %[[CHECK2:.*]] = icmp eq i64 %[[MISALIGN]], 0
 
   // CHECK:      %[[OK:.*]] = and i1 %[[CHECK01]], %[[CHECK2]]
-  // CHECK-NEXT: br i1 %[[OK]]
+  // CHECK-NEXT: br i1 %[[OK]], {{.*}} !prof ![[WEIGHT_MD:.*]]
 
   // CHECK:      %[[ARG:.*]] = ptrtoint {{.*}} %[[PTR]] to i64
   // CHECK-NEXT: call void @__ubsan_handle_type_mismatch_abort(i8* bitcast ({{.*}} @[[LINE_100]] to i8*), i64 %[[ARG]]) noreturn nounwind
@@ -85,7 +85,7 @@ int lsh_overflow(int a, int b) {
   // CHECK:      %[[SHIFTED_OUT_WIDTH:.*]] = sub nuw nsw i32 31, %[[RHS]]
   // CHECK-NEXT: %[[SHIFTED_OUT:.*]] = lshr i32 %[[LHS:.*]], %[[SHIFTED_OUT_WIDTH]]
   // CHECK-NEXT: %[[NO_OVERFLOW:.*]] = icmp eq i32 %[[SHIFTED_OUT]], 0
-  // CHECK-NEXT: br i1 %[[NO_OVERFLOW]]
+  // CHECK-NEXT: br i1 %[[NO_OVERFLOW]], {{.*}} !prof ![[WEIGHT_MD]]
 
   // CHECK:      %[[ARG1:.*]] = zext
   // CHECK-NEXT: %[[ARG2:.*]] = zext
@@ -254,3 +254,5 @@ _Bool sour_bool(_Bool *p) {
   // CHECK: call void @__ubsan_handle_load_invalid_value_abort(i8* bitcast ({{.*}}), i64 {{.*}})
   return *p;
 }
+
+// CHECK: ![[WEIGHT_MD]] = metadata !{metadata !"branch_weights", i32 1048575, i32 1}