[PATCH] D19299: lower __builtin_expect() directly to prof metadata instead of LLVM intrinsic

Amaury SECHET via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 19 22:44:41 PDT 2016


deadalnix added a subscriber: deadalnix.

================
Comment at: lib/CodeGen/CGStmt.cpp:1560-1588
@@ -1560,1 +1559,31 @@
+                                MDHelper.createUnpredictable());
+      } else if (FD->getBuiltinID() == Builtin::BI__builtin_expect) {
+
+        // FIXME: builtin_expect should use the same metadata type as
+        // builtin_unpredictable and be handled above. For now, we're mimicking
+        // the LLVM behavior of the 'LowerExpectIntrinsic' pass.
+
+        // HACK: Hardcode the taken/not-taken weights based on the existing LLVM
+        // default values. This code is expected to be very temporary. Once we
+        // have prepared LLVM to handle builtin_expect using 'unpredictable'
+        // metadata, this gets deleted.
+
+        const int LikelyWeight = 64;
+        const int UnlikelyWeight = 4;
+
+        llvm::Value *ExpectedVal = EmitScalarExpr(Call->getArg(1));
+        if (auto *ExpectConst = dyn_cast<llvm::ConstantInt>(ExpectedVal)) {
+          // The +1 is for the default case.
+          SmallVector<uint32_t, 16> Weights(SwitchInsn->getNumCases() + 1,
+                                            UnlikelyWeight);
+          auto ExpectedCase = SwitchInsn->findCaseValue(ExpectConst);
+          if (ExpectedCase == SwitchInsn->case_default())
+            Weights[0] = LikelyWeight;
+          else
+            Weights[ExpectedCase.getCaseIndex() + 1] = LikelyWeight;
+
+          SwitchInsn->setMetadata(llvm::LLVMContext::MD_prof,
+                                  MDHelper.createBranchWeights(Weights));
+        }
+      }
     }
----------------
If I understand properly this is transitional and eventually, you want to remove the intrinsic ? I think I like it, having 2 ways to hint here is only making things more complicated without adding much value.


http://reviews.llvm.org/D19299





More information about the cfe-commits mailing list