[clang][Codegen] EmitBranchOnBoolExpr(): emit prof branch counts even at -O0
authorRoman Lebedev <lebedev.ri@gmail.com>
Sun, 21 Mar 2021 20:22:41 +0000 (23:22 +0300)
committerRoman Lebedev <lebedev.ri@gmail.com>
Sun, 21 Mar 2021 20:24:27 +0000 (23:24 +0300)
This restores the original behaviour before i unadvertedly broke it in
e3a470162738871bba982416748ae5f5e3572947 and clang/test/Profile/ caught it.

clang/lib/CodeGen/CodeGenFunction.cpp

index 1c53826..fd70884 100644 (file)
@@ -1774,27 +1774,27 @@ void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
   llvm::MDNode *Weights = nullptr;
   llvm::MDNode *Unpredictable = nullptr;
 
-  // If optimizing, lower unpredictability/probability knowledge about cond.
-  if (CGM.getCodeGenOpts().OptimizationLevel != 0) {
-    // If the branch has a condition wrapped by __builtin_unpredictable,
-    // create metadata that specifies that the branch is unpredictable.
-    if (auto *Call = dyn_cast<CallExpr>(Cond->IgnoreImpCasts())) {
-      auto *FD = dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl());
-      if (FD && FD->getBuiltinID() == Builtin::BI__builtin_unpredictable) {
-        llvm::MDBuilder MDHelper(getLLVMContext());
-        Unpredictable = MDHelper.createUnpredictable();
-      }
+  // If the branch has a condition wrapped by __builtin_unpredictable,
+  // create metadata that specifies that the branch is unpredictable.
+  // Don't bother if not optimizing because that metadata would not be used.
+  auto *Call = dyn_cast<CallExpr>(Cond->IgnoreImpCasts());
+  if (Call && CGM.getCodeGenOpts().OptimizationLevel != 0) {
+    auto *FD = dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl());
+    if (FD && FD->getBuiltinID() == Builtin::BI__builtin_unpredictable) {
+      llvm::MDBuilder MDHelper(getLLVMContext());
+      Unpredictable = MDHelper.createUnpredictable();
     }
+  }
 
-    // If there is a Likelihood knowledge for the cond, lower it.
-    llvm::Value *NewCondV = emitCondLikelihoodViaExpectIntrinsic(CondV, LH);
-    if (CondV != NewCondV)
-      CondV = NewCondV;
-    else {
-      // Otherwise, lower profile counts.
-      uint64_t CurrentCount = std::max(getCurrentProfileCount(), TrueCount);
-      Weights = createProfileWeights(TrueCount, CurrentCount - TrueCount);
-    }
+  // If there is a Likelihood knowledge for the cond, lower it.
+  // Note that if not optimizing this won't emit anything.
+  llvm::Value *NewCondV = emitCondLikelihoodViaExpectIntrinsic(CondV, LH);
+  if (CondV != NewCondV)
+    CondV = NewCondV;
+  else {
+    // Otherwise, lower profile counts. Note that we do this even at -O0.
+    uint64_t CurrentCount = std::max(getCurrentProfileCount(), TrueCount);
+    Weights = createProfileWeights(TrueCount, CurrentCount - TrueCount);
   }
 
   Builder.CreateCondBr(CondV, TrueBlock, FalseBlock, Weights, Unpredictable);