From 50f10eb432f6f6d75a8a477b826ee2621c3d553f Mon Sep 17 00:00:00 2001 From: Juergen Ributzka Date: Tue, 9 Dec 2014 16:36:06 +0000 Subject: [PATCH] Move function to obtain branch weights into the BranchInst class. NFC. Make this function available to other parts of LLVM. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223784 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/IR/Instructions.h | 4 ++++ lib/IR/Instructions.cpp | 20 +++++++++++++++++ lib/Transforms/Utils/SimplifyCFG.cpp | 32 ++++++---------------------- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/include/llvm/IR/Instructions.h b/include/llvm/IR/Instructions.h index dcf19e0972d..5157ca6f447 100644 --- a/include/llvm/IR/Instructions.h +++ b/include/llvm/IR/Instructions.h @@ -2535,6 +2535,10 @@ 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); diff --git a/lib/IR/Instructions.cpp b/lib/IR/Instructions.cpp index f4c6a289b80..5ea45357864 100644 --- a/lib/IR/Instructions.cpp +++ b/lib/IR/Instructions.cpp @@ -805,6 +805,26 @@ 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(MD->getOperand(1)); + auto *FalseCI = dyn_cast(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); } diff --git a/lib/Transforms/Utils/SimplifyCFG.cpp b/lib/Transforms/Utils/SimplifyCFG.cpp index daa576cfbdc..3efb89a4d7f 100644 --- a/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/lib/Transforms/Utils/SimplifyCFG.cpp @@ -2027,24 +2027,6 @@ 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(ProfileData->getOperand(1)); - ConstantInt *CIFalse = dyn_cast(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. /// @@ -2250,10 +2232,10 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI, const DataLayout *DL, PBI->setCondition(NewCond); uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight; - bool PredHasWeights = ExtractBranchMetadata(PBI, PredTrueWeight, - PredFalseWeight); - bool SuccHasWeights = ExtractBranchMetadata(BI, SuccTrueWeight, - SuccFalseWeight); + bool PredHasWeights = + PBI->getBranchWeights(PredTrueWeight, PredFalseWeight); + bool SuccHasWeights = + BI->getBranchWeights(SuccTrueWeight, SuccFalseWeight); SmallVector NewWeights; if (PBI->getSuccessor(0) == BB) { @@ -2523,10 +2505,8 @@ static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI) { // Update branch weight for PBI. uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight; - bool PredHasWeights = ExtractBranchMetadata(PBI, PredTrueWeight, - PredFalseWeight); - bool SuccHasWeights = ExtractBranchMetadata(BI, SuccTrueWeight, - SuccFalseWeight); + bool PredHasWeights = PBI->getBranchWeights(PredTrueWeight, PredFalseWeight); + bool SuccHasWeights = BI->getBranchWeights(SuccTrueWeight, SuccFalseWeight); if (PredHasWeights && SuccHasWeights) { uint64_t PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight; uint64_t PredOther = PBIOp ?PredTrueWeight : PredFalseWeight; -- 2.34.1