summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Analysis/BranchProbabilityInfo.cpp
diff options
context:
space:
mode:
authorChandler Carruth <chandlerc@gmail.com>2011-10-19 10:30:30 +0000
committerChandler Carruth <chandlerc@gmail.com>2011-10-19 10:30:30 +0000
commitd27a7a947bddca602d4c21da68146deaf322c1d6 (patch)
tree34b30447a59f2c7a9038e348fa3f73e5db6fa251 /llvm/lib/Analysis/BranchProbabilityInfo.cpp
parent343fad44ea20085ca1095d46a386f525b3f68fce (diff)
downloadbcm5719-llvm-d27a7a947bddca602d4c21da68146deaf322c1d6.tar.gz
bcm5719-llvm-d27a7a947bddca602d4c21da68146deaf322c1d6.zip
Teach the BranchProbabilityInfo analysis pass to read any metadata
encoding of probabilities. In the absense of metadata, it continues to fall back on static heuristics. This allows __builtin_expect, after lowering through llvm.expect a branch instruction's metadata, to actually enter the branch probability model. This is one component of resolving PR2577. llvm-svn: 142492
Diffstat (limited to 'llvm/lib/Analysis/BranchProbabilityInfo.cpp')
-rw-r--r--llvm/lib/Analysis/BranchProbabilityInfo.cpp38
1 files changed, 38 insertions, 0 deletions
diff --git a/llvm/lib/Analysis/BranchProbabilityInfo.cpp b/llvm/lib/Analysis/BranchProbabilityInfo.cpp
index c37987e1be7..70de3d1c49d 100644
--- a/llvm/lib/Analysis/BranchProbabilityInfo.cpp
+++ b/llvm/lib/Analysis/BranchProbabilityInfo.cpp
@@ -13,6 +13,8 @@
#include "llvm/Constants.h"
#include "llvm/Instructions.h"
+#include "llvm/LLVMContext.h"
+#include "llvm/Metadata.h"
#include "llvm/Analysis/BranchProbabilityInfo.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Support/Debug.h"
@@ -117,6 +119,9 @@ public:
: BP(BP), LI(LI) {
}
+ // Metadata Weights
+ bool calcMetadataWeights(BasicBlock *BB);
+
// Return Heuristics
bool calcReturnHeuristics(BasicBlock *BB);
@@ -133,6 +138,36 @@ public:
};
} // end anonymous namespace
+// Propagate existing explicit probabilities from either profile data or
+// 'expect' intrinsic processing.
+// FIXME: This doesn't correctly extract probabilities for switches.
+bool BranchProbabilityAnalysis::calcMetadataWeights(BasicBlock *BB) {
+ BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
+ if (!BI || !BI->isConditional())
+ return false;
+
+ MDNode *WeightsNode = BI->getMetadata(LLVMContext::MD_prof);
+ if (!WeightsNode || WeightsNode->getNumOperands() < 3)
+ return false;
+
+ // Pull the weights out of the metadata. Note that the zero operand is the
+ // name.
+ ConstantInt *Weights[] = {
+ dyn_cast<ConstantInt>(WeightsNode->getOperand(1)),
+ dyn_cast<ConstantInt>(WeightsNode->getOperand(2))
+ };
+ if (!Weights[0] || !Weights[1])
+ return false;
+
+ uint32_t WeightLimit = getMaxWeightFor(BB);
+ BP->setEdgeWeight(BB, BI->getSuccessor(0),
+ Weights[0]->getLimitedValue(WeightLimit));
+ BP->setEdgeWeight(BB, BI->getSuccessor(1),
+ Weights[1]->getLimitedValue(WeightLimit));
+
+ return true;
+}
+
// Calculate Edge Weights using "Return Heuristics". Predict a successor which
// leads directly to Return Instruction will not be taken.
bool BranchProbabilityAnalysis::calcReturnHeuristics(BasicBlock *BB){
@@ -341,6 +376,9 @@ bool BranchProbabilityAnalysis::runOnFunction(Function &F) {
for (Function::iterator I = F.begin(), E = F.end(); I != E; ) {
BasicBlock *BB = I++;
+ if (calcMetadataWeights(BB))
+ continue;
+
if (calcLoopBranchHeuristics(BB))
continue;
OpenPOWER on IntegriCloud