Revert "Move function to obtain branch weights into the BranchInst class. NFC."
authorJuergen Ributzka <juergen@apple.com>
Tue, 9 Dec 2014 17:32:12 +0000 (17:32 +0000)
committerJuergen Ributzka <juergen@apple.com>
Tue, 9 Dec 2014 17:32:12 +0000 (17:32 +0000)
This reverts commit r223784 and copies the 'ExtractBranchMetadata' to CodeGenPrepare.

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

include/llvm/IR/Instructions.h
lib/CodeGen/CodeGenPrepare.cpp
lib/IR/Instructions.cpp
lib/Transforms/Utils/SimplifyCFG.cpp

index 5157ca6f447c55693feb14e62fc0eb91470bb335..dcf19e0972d16b261d681e104a78120ee3a70ec9 100644 (file)
@@ -2535,10 +2535,6 @@ public:
   /// continues to map correctly to each operand.
   void swapSuccessors();
 
-  /// \brief Retrieve the probabilities of a conditional branch. Returns true on
-  /// success, or returns false if no or invalid metadata was found.
-  bool getBranchWeights(uint64_t &TrueWeight, uint64_t &FalseWeight) const;
-
   // Methods for support type inquiry through isa, cast, and dyn_cast:
   static inline bool classof(const Instruction *I) {
     return (I->getOpcode() == Instruction::Br);
index 8b92cf2e9f4f327cf7cec7dda937a7afec282a16..e043bfbfa270229a527da16d5ce3099ba5e09b4a 100644 (file)
@@ -3800,6 +3800,27 @@ bool CodeGenPrepare::sinkAndCmp(Function &F) {
   return MadeChange;
 }
 
+/// \brief Retrieve the probabilities of a conditional branch. Returns true on
+/// success, or returns false if no or invalid metadata was found.
+static bool extractBranchMetadata(BranchInst *BI,
+                                  uint64_t &ProbTrue, uint64_t &ProbFalse) {
+  assert(BI->isConditional() &&
+         "Looking for probabilities on unconditional branch?");
+  auto *ProfileData = BI->getMetadata(LLVMContext::MD_prof);
+  if (!ProfileData || ProfileData->getNumOperands() != 3)
+    return false;
+
+  const auto *CITrue = dyn_cast<ConstantInt>(ProfileData->getOperand(1));
+  const auto *CIFalse = dyn_cast<ConstantInt>(ProfileData->getOperand(2));
+  if (!CITrue || !CIFalse)
+    return false;
+
+  ProbTrue = CITrue->getValue().getZExtValue();
+  ProbFalse = CIFalse->getValue().getZExtValue();
+
+  return true;
+}
+
 /// \brief Scale down both weights to fit into uint32_t.
 static void scaleWeights(uint64_t &NewTrue, uint64_t &NewFalse) {
   uint64_t NewMax = (NewTrue > NewFalse) ? NewTrue : NewFalse;
@@ -3942,7 +3963,7 @@ bool CodeGenPrepare::splitBranchCondition(Function &F) {
       // Another choice is to assume TrueProb for BB1 equals to TrueProb for
       // TmpBB, but the math is more complicated.
       uint64_t TrueWeight, FalseWeight;
-      if (Br1->getBranchWeights(TrueWeight, FalseWeight)) {
+      if (extractBranchMetadata(Br1, TrueWeight, FalseWeight)) {
         uint64_t NewTrueWeight = TrueWeight;
         uint64_t NewFalseWeight = TrueWeight + 2 * FalseWeight;
         scaleWeights(NewTrueWeight, NewFalseWeight);
@@ -3975,7 +3996,7 @@ bool CodeGenPrepare::splitBranchCondition(Function &F) {
       // assumes that
       //   FalseProb for BB1 == TrueProb for BB1 * FalseProb for TmpBB.
       uint64_t TrueWeight, FalseWeight;
-      if (Br1->getBranchWeights(TrueWeight, FalseWeight)) {
+      if (extractBranchMetadata(Br1, TrueWeight, FalseWeight)) {
         uint64_t NewTrueWeight = 2 * TrueWeight + FalseWeight;
         uint64_t NewFalseWeight = FalseWeight;
         scaleWeights(NewTrueWeight, NewFalseWeight);
index 5ea453578648addfd6171b4986e69dd4d2304d0d..f4c6a289b804e0ef60c410b5544fba7255adc681 100644 (file)
@@ -805,26 +805,6 @@ void BranchInst::swapSuccessors() {
               MDNode::get(ProfileData->getContext(), Ops));
 }
 
-bool BranchInst::getBranchWeights(uint64_t &TrueWeight,
-                                  uint64_t &FalseWeight) const {
-  if (isUnconditional())
-    return false;
-
-  auto *MD = getMetadata(LLVMContext::MD_prof);
-  if (!MD || MD->getNumOperands() != 3)
-    return false;
-
-  auto *TrueCI = dyn_cast<ConstantInt>(MD->getOperand(1));
-  auto *FalseCI = dyn_cast<ConstantInt>(MD->getOperand(2));
-  if (!TrueCI || !FalseCI)
-    return false;
-
-  TrueWeight = TrueCI->getValue().getZExtValue();
-  FalseWeight = FalseCI->getValue().getZExtValue();
-
-  return true;
-}
-
 BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
   return getSuccessor(idx);
 }
index 3efb89a4d7fbfabe75f2942f5d9d5b0b576de658..daa576cfbdc9fe5e2ef2c80a59439d71fc33ca54 100644 (file)
@@ -2027,6 +2027,24 @@ static bool SimplifyCondBranchToTwoReturns(BranchInst *BI,
   return true;
 }
 
+/// ExtractBranchMetadata - Given a conditional BranchInstruction, retrieve the
+/// probabilities of the branch taking each edge. Fills in the two APInt
+/// parameters and return true, or returns false if no or invalid metadata was
+/// found.
+static bool ExtractBranchMetadata(BranchInst *BI,
+                                  uint64_t &ProbTrue, uint64_t &ProbFalse) {
+  assert(BI->isConditional() &&
+         "Looking for probabilities on unconditional branch?");
+  MDNode *ProfileData = BI->getMetadata(LLVMContext::MD_prof);
+  if (!ProfileData || ProfileData->getNumOperands() != 3) return false;
+  ConstantInt *CITrue = dyn_cast<ConstantInt>(ProfileData->getOperand(1));
+  ConstantInt *CIFalse = dyn_cast<ConstantInt>(ProfileData->getOperand(2));
+  if (!CITrue || !CIFalse) return false;
+  ProbTrue = CITrue->getValue().getZExtValue();
+  ProbFalse = CIFalse->getValue().getZExtValue();
+  return true;
+}
+
 /// checkCSEInPredecessor - Return true if the given instruction is available
 /// in its predecessor block. If yes, the instruction will be removed.
 ///
@@ -2232,10 +2250,10 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, const DataLayout *DL,
       PBI->setCondition(NewCond);
 
       uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
-      bool PredHasWeights =
-          PBI->getBranchWeights(PredTrueWeight, PredFalseWeight);
-      bool SuccHasWeights =
-          BI->getBranchWeights(SuccTrueWeight, SuccFalseWeight);
+      bool PredHasWeights = ExtractBranchMetadata(PBI, PredTrueWeight,
+                                                  PredFalseWeight);
+      bool SuccHasWeights = ExtractBranchMetadata(BI, SuccTrueWeight,
+                                                  SuccFalseWeight);
       SmallVector<uint64_t, 8> NewWeights;
 
       if (PBI->getSuccessor(0) == BB) {
@@ -2505,8 +2523,10 @@ static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI) {
 
   // Update branch weight for PBI.
   uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
-  bool PredHasWeights = PBI->getBranchWeights(PredTrueWeight, PredFalseWeight);
-  bool SuccHasWeights = BI->getBranchWeights(SuccTrueWeight, SuccFalseWeight);
+  bool PredHasWeights = ExtractBranchMetadata(PBI, PredTrueWeight,
+                                              PredFalseWeight);
+  bool SuccHasWeights = ExtractBranchMetadata(BI, SuccTrueWeight,
+                                              SuccFalseWeight);
   if (PredHasWeights && SuccHasWeights) {
     uint64_t PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight;
     uint64_t PredOther = PBIOp ?PredTrueWeight : PredFalseWeight;