+// 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;
+}
+