Index: include/llvm/Analysis/CFGPrinter.h =================================================================== --- include/llvm/Analysis/CFGPrinter.h +++ include/llvm/Analysis/CFGPrinter.h @@ -118,6 +118,32 @@ } return ""; } + + /// Display the raw branch weights from PGO. + std::string getEdgeAttributes(const BasicBlock *Node, succ_const_iterator I, + const Function *F) { + const TerminatorInst *TI = Node->getTerminator(); + if (TI->getNumSuccessors() == 1) + return ""; + + MDNode *WeightsNode = TI->getMetadata(LLVMContext::MD_prof); + if (!WeightsNode) + return ""; + + // Ensure there are weights for all of the successors. Note that the first + // operand to the metadata node is a name, not a weight. + if (WeightsNode->getNumOperands() != TI->getNumSuccessors() + 1) + return ""; + + unsigned SuccNo = I.getSuccessorIndex(); + ConstantInt *Weight = + mdconst::dyn_extract(WeightsNode->getOperand(SuccNo + 1)); + if (!Weight) + return ""; + + Twine Attrs = "label=\"" + Twine(Weight->getZExtValue()) + "\""; + return Attrs.str(); + } }; } // End llvm namespace