/// \brief Generate branch weight metadata for all branches in \p F. /// /// For every branch instruction B in \p F, we compute the weight of the /// target block for each of the edges out of B. This is the weight /// that we associate with that branch. /// /// TODO - This weight assignment will most likely be wrong if the /// target branch has more than two predecessors. This needs to be done /// using some form of flow propagation. /// /// Once all the branch weights are computed, we emit the MD_prof /// metadata on B using the computed values. /// /// \param F The function to query. bool SampleProfile::emitAnnotations(Function &F) { bool Changed = false; FunctionProfile &FProfile = Profiles[F.getName()]; unsigned FirstLineno = inst_begin(F)->getDebugLoc().getLine(); MDBuilder MDB(F.getContext()); // Clear the block weights cache. FProfile.BlockWeights.clear(); // When we find a branch instruction: For each edge E out of the branch, // the weight of E is the weight of the target block. for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) { BasicBlock *B = I; TerminatorInst *TI = B->getTerminator(); if (TI->getNumSuccessors() == 1) continue; if (!isa<BranchInst>(TI) && !isa<SwitchInst>(TI)) continue; SmallVector<uint32_t, 4> Weights; unsigned NSuccs = TI->getNumSuccessors(); for (unsigned I = 0; I < NSuccs; ++I) { BasicBlock *Succ = TI->getSuccessor(I); uint32_t Weight = computeBlockWeight(Succ, FirstLineno, FProfile.BodySamples); Weights.push_back(Weight); } TI->setMetadata(llvm::LLVMContext::MD_prof, MDB.createBranchWeights(Weights)); Changed = true; } return Changed; }
void Loop::setLoopID(MDNode *LoopID) const { assert(LoopID && "Loop ID should not be null"); assert(LoopID->getNumOperands() > 0 && "Loop ID needs at least one operand"); assert(LoopID->getOperand(0) == LoopID && "Loop ID should refer to itself"); if (isLoopSimplifyForm()) { getLoopLatch()->getTerminator()->setMetadata(LoopMDName, LoopID); return; } BasicBlock *H = getHeader(); for (block_iterator I = block_begin(), IE = block_end(); I != IE; ++I) { TerminatorInst *TI = (*I)->getTerminator(); for (unsigned i = 0, ie = TI->getNumSuccessors(); i != ie; ++i) { if (TI->getSuccessor(i) == H) TI->setMetadata(LoopMDName, LoopID); } } }
void Loop::setLoopID(MDNode *LoopID) const { assert(LoopID && "Loop ID should not be null"); assert(LoopID->getNumOperands() > 0 && "Loop ID needs at least one operand"); assert(LoopID->getOperand(0) == LoopID && "Loop ID should refer to itself"); if (isLoopSimplifyForm()) { getLoopLatch()->getTerminator()->setMetadata(LLVMContext::MD_loop, LoopID); return; } BasicBlock *H = getHeader(); for (BasicBlock *BB : this->blocks()) { TerminatorInst *TI = BB->getTerminator(); for (BasicBlock *Successor : TI->successors()) { if (Successor == H) TI->setMetadata(LLVMContext::MD_loop, LoopID); } } }
/// setBranchWeightMetadata - Translate the counter values associated with each /// edge into branch weights for each conditional branch (a branch with 2 or /// more desinations). void ProfileMetadataLoaderPass::setBranchWeightMetadata(Module &M, ProfileData &PB) { for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { if (F->isDeclaration()) continue; DEBUG(dbgs() << "Setting branch metadata in '" << F->getName() << "'\n"); for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { TerminatorInst *TI = BB->getTerminator(); unsigned NumSuccessors = TI->getNumSuccessors(); // If there is only one successor then we can not set a branch // probability as the target is certain. if (NumSuccessors < 2) continue; // Load the weights of all edges leading from this terminator. DEBUG(dbgs() << "-- Terminator with " << NumSuccessors << " successors:\n"); SmallVector<uint32_t, 4> Weights(NumSuccessors); for (unsigned s = 0 ; s < NumSuccessors ; ++s) { ProfileData::Edge edge = PB.getEdge(BB, TI->getSuccessor(s)); Weights[s] = (uint32_t)PB.getEdgeWeight(edge); DEBUG(dbgs() << "---- Edge '" << edge << "' has weight " << Weights[s] << "\n"); } // Set branch weight metadata. This will set branch probabilities of // 100%/0% if that is true of the dynamic execution. // BranchProbabilityInfo can account for this when it loads this metadata // (it gives the unexectuted branch a weight of 1 for the purposes of // probability calculations). MDBuilder MDB(TI->getContext()); MDNode *Node = MDB.createBranchWeights(Weights); TI->setMetadata(LLVMContext::MD_prof, Node); NumTermsAnnotated++; } } }