10b57cec5SDimitry Andric //===-- IndirectCallPromotionAnalysis.cpp - Find promotion candidates ===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // Helper methods for identifying profitable indirect call promotion
100b57cec5SDimitry Andric // candidates for an instruction when the indirect-call value profile metadata
110b57cec5SDimitry Andric // is available.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "llvm/Analysis/IndirectCallPromotionAnalysis.h"
160b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
170b57cec5SDimitry Andric #include "llvm/Analysis/IndirectCallVisitor.h"
180b57cec5SDimitry Andric #include "llvm/IR/InstIterator.h"
190b57cec5SDimitry Andric #include "llvm/IR/InstVisitor.h"
200b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
210b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
220b57cec5SDimitry Andric #include "llvm/ProfileData/InstrProf.h"
23480093f4SDimitry Andric #include "llvm/Support/CommandLine.h"
240b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
25*af732203SDimitry Andric #include <memory>
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric using namespace llvm;
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric #define DEBUG_TYPE "pgo-icall-prom-analysis"
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric // The percent threshold for the direct-call target (this call site vs the
320b57cec5SDimitry Andric // remaining call count) for it to be considered as the promotion target.
330b57cec5SDimitry Andric static cl::opt<unsigned> ICPRemainingPercentThreshold(
340b57cec5SDimitry Andric     "icp-remaining-percent-threshold", cl::init(30), cl::Hidden, cl::ZeroOrMore,
350b57cec5SDimitry Andric     cl::desc("The percentage threshold against remaining unpromoted indirect "
360b57cec5SDimitry Andric              "call count for the promotion"));
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric // The percent threshold for the direct-call target (this call site vs the
390b57cec5SDimitry Andric // total call count) for it to be considered as the promotion target.
400b57cec5SDimitry Andric static cl::opt<unsigned>
410b57cec5SDimitry Andric     ICPTotalPercentThreshold("icp-total-percent-threshold", cl::init(5),
420b57cec5SDimitry Andric                              cl::Hidden, cl::ZeroOrMore,
430b57cec5SDimitry Andric                              cl::desc("The percentage threshold against total "
440b57cec5SDimitry Andric                                       "count for the promotion"));
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric // Set the maximum number of targets to promote for a single indirect-call
470b57cec5SDimitry Andric // callsite.
480b57cec5SDimitry Andric static cl::opt<unsigned>
490b57cec5SDimitry Andric     MaxNumPromotions("icp-max-prom", cl::init(3), cl::Hidden, cl::ZeroOrMore,
500b57cec5SDimitry Andric                      cl::desc("Max number of promotions for a single indirect "
510b57cec5SDimitry Andric                               "call callsite"));
520b57cec5SDimitry Andric 
ICallPromotionAnalysis()530b57cec5SDimitry Andric ICallPromotionAnalysis::ICallPromotionAnalysis() {
548bcb0991SDimitry Andric   ValueDataArray = std::make_unique<InstrProfValueData[]>(MaxNumPromotions);
550b57cec5SDimitry Andric }
560b57cec5SDimitry Andric 
isPromotionProfitable(uint64_t Count,uint64_t TotalCount,uint64_t RemainingCount)570b57cec5SDimitry Andric bool ICallPromotionAnalysis::isPromotionProfitable(uint64_t Count,
580b57cec5SDimitry Andric                                                    uint64_t TotalCount,
590b57cec5SDimitry Andric                                                    uint64_t RemainingCount) {
600b57cec5SDimitry Andric   return Count * 100 >= ICPRemainingPercentThreshold * RemainingCount &&
610b57cec5SDimitry Andric          Count * 100 >= ICPTotalPercentThreshold * TotalCount;
620b57cec5SDimitry Andric }
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric // Indirect-call promotion heuristic. The direct targets are sorted based on
650b57cec5SDimitry Andric // the count. Stop at the first target that is not promoted. Returns the
660b57cec5SDimitry Andric // number of candidates deemed profitable.
getProfitablePromotionCandidates(const Instruction * Inst,uint32_t NumVals,uint64_t TotalCount)670b57cec5SDimitry Andric uint32_t ICallPromotionAnalysis::getProfitablePromotionCandidates(
680b57cec5SDimitry Andric     const Instruction *Inst, uint32_t NumVals, uint64_t TotalCount) {
690b57cec5SDimitry Andric   ArrayRef<InstrProfValueData> ValueDataRef(ValueDataArray.get(), NumVals);
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << " \nWork on callsite " << *Inst
720b57cec5SDimitry Andric                     << " Num_targets: " << NumVals << "\n");
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric   uint32_t I = 0;
750b57cec5SDimitry Andric   uint64_t RemainingCount = TotalCount;
760b57cec5SDimitry Andric   for (; I < MaxNumPromotions && I < NumVals; I++) {
770b57cec5SDimitry Andric     uint64_t Count = ValueDataRef[I].Count;
780b57cec5SDimitry Andric     assert(Count <= RemainingCount);
790b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << " Candidate " << I << " Count=" << Count
800b57cec5SDimitry Andric                       << "  Target_func: " << ValueDataRef[I].Value << "\n");
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric     if (!isPromotionProfitable(Count, TotalCount, RemainingCount)) {
830b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << " Not promote: Cold target.\n");
840b57cec5SDimitry Andric       return I;
850b57cec5SDimitry Andric     }
860b57cec5SDimitry Andric     RemainingCount -= Count;
870b57cec5SDimitry Andric   }
880b57cec5SDimitry Andric   return I;
890b57cec5SDimitry Andric }
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric ArrayRef<InstrProfValueData>
getPromotionCandidatesForInstruction(const Instruction * I,uint32_t & NumVals,uint64_t & TotalCount,uint32_t & NumCandidates)920b57cec5SDimitry Andric ICallPromotionAnalysis::getPromotionCandidatesForInstruction(
930b57cec5SDimitry Andric     const Instruction *I, uint32_t &NumVals, uint64_t &TotalCount,
940b57cec5SDimitry Andric     uint32_t &NumCandidates) {
950b57cec5SDimitry Andric   bool Res =
960b57cec5SDimitry Andric       getValueProfDataFromInst(*I, IPVK_IndirectCallTarget, MaxNumPromotions,
970b57cec5SDimitry Andric                                ValueDataArray.get(), NumVals, TotalCount);
980b57cec5SDimitry Andric   if (!Res) {
990b57cec5SDimitry Andric     NumCandidates = 0;
1000b57cec5SDimitry Andric     return ArrayRef<InstrProfValueData>();
1010b57cec5SDimitry Andric   }
1020b57cec5SDimitry Andric   NumCandidates = getProfitablePromotionCandidates(I, NumVals, TotalCount);
1030b57cec5SDimitry Andric   return ArrayRef<InstrProfValueData>(ValueDataArray.get(), NumVals);
1040b57cec5SDimitry Andric }
105