1 //===-- IndirectCallPromotion.cpp - Optimizations based on value profiling ===//
2 //
3 //                      The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the transformation that promotes indirect calls to
11 // conditional direct calls when the indirect-call value profile metadata is
12 // available.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/Analysis/BlockFrequencyInfo.h"
21 #include "llvm/Analysis/GlobalsModRef.h"
22 #include "llvm/Analysis/IndirectCallPromotionAnalysis.h"
23 #include "llvm/Analysis/IndirectCallSiteVisitor.h"
24 #include "llvm/Analysis/OptimizationDiagnosticInfo.h"
25 #include "llvm/Analysis/ProfileSummaryInfo.h"
26 #include "llvm/IR/BasicBlock.h"
27 #include "llvm/IR/CallSite.h"
28 #include "llvm/IR/DerivedTypes.h"
29 #include "llvm/IR/DiagnosticInfo.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/IRBuilder.h"
32 #include "llvm/IR/InstrTypes.h"
33 #include "llvm/IR/Instruction.h"
34 #include "llvm/IR/Instructions.h"
35 #include "llvm/IR/LLVMContext.h"
36 #include "llvm/IR/MDBuilder.h"
37 #include "llvm/IR/PassManager.h"
38 #include "llvm/IR/Type.h"
39 #include "llvm/Pass.h"
40 #include "llvm/PassRegistry.h"
41 #include "llvm/PassSupport.h"
42 #include "llvm/ProfileData/InstrProf.h"
43 #include "llvm/Support/Casting.h"
44 #include "llvm/Support/CommandLine.h"
45 #include "llvm/Support/Debug.h"
46 #include "llvm/Support/ErrorHandling.h"
47 #include "llvm/Support/MathExtras.h"
48 #include "llvm/Transforms/Instrumentation.h"
49 #include "llvm/Transforms/PGOInstrumentation.h"
50 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
51 #include <cassert>
52 #include <cstdint>
53 #include <vector>
54 
55 using namespace llvm;
56 
57 #define DEBUG_TYPE "pgo-icall-prom"
58 
59 STATISTIC(NumOfPGOICallPromotion, "Number of indirect call promotions.");
60 STATISTIC(NumOfPGOICallsites, "Number of indirect call candidate sites.");
61 
62 // Command line option to disable indirect-call promotion with the default as
63 // false. This is for debug purpose.
64 static cl::opt<bool> DisableICP("disable-icp", cl::init(false), cl::Hidden,
65                                 cl::desc("Disable indirect call promotion"));
66 
67 // Set the cutoff value for the promotion. If the value is other than 0, we
68 // stop the transformation once the total number of promotions equals the cutoff
69 // value.
70 // For debug use only.
71 static cl::opt<unsigned>
72     ICPCutOff("icp-cutoff", cl::init(0), cl::Hidden, cl::ZeroOrMore,
73               cl::desc("Max number of promotions for this compilation"));
74 
75 // If ICPCSSkip is non zero, the first ICPCSSkip callsites will be skipped.
76 // For debug use only.
77 static cl::opt<unsigned>
78     ICPCSSkip("icp-csskip", cl::init(0), cl::Hidden, cl::ZeroOrMore,
79               cl::desc("Skip Callsite up to this number for this compilation"));
80 
81 // Set if the pass is called in LTO optimization. The difference for LTO mode
82 // is the pass won't prefix the source module name to the internal linkage
83 // symbols.
84 static cl::opt<bool> ICPLTOMode("icp-lto", cl::init(false), cl::Hidden,
85                                 cl::desc("Run indirect-call promotion in LTO "
86                                          "mode"));
87 
88 // Set if the pass is called in SamplePGO mode. The difference for SamplePGO
89 // mode is it will add prof metadatato the created direct call.
90 static cl::opt<bool>
91     ICPSamplePGOMode("icp-samplepgo", cl::init(false), cl::Hidden,
92                      cl::desc("Run indirect-call promotion in SamplePGO mode"));
93 
94 // If the option is set to true, only call instructions will be considered for
95 // transformation -- invoke instructions will be ignored.
96 static cl::opt<bool>
97     ICPCallOnly("icp-call-only", cl::init(false), cl::Hidden,
98                 cl::desc("Run indirect-call promotion for call instructions "
99                          "only"));
100 
101 // If the option is set to true, only invoke instructions will be considered for
102 // transformation -- call instructions will be ignored.
103 static cl::opt<bool> ICPInvokeOnly("icp-invoke-only", cl::init(false),
104                                    cl::Hidden,
105                                    cl::desc("Run indirect-call promotion for "
106                                             "invoke instruction only"));
107 
108 // Dump the function level IR if the transformation happened in this
109 // function. For debug use only.
110 static cl::opt<bool>
111     ICPDUMPAFTER("icp-dumpafter", cl::init(false), cl::Hidden,
112                  cl::desc("Dump IR after transformation happens"));
113 
114 namespace {
115 class PGOIndirectCallPromotionLegacyPass : public ModulePass {
116 public:
117   static char ID;
118 
119   PGOIndirectCallPromotionLegacyPass(bool InLTO = false, bool SamplePGO = false)
120       : ModulePass(ID), InLTO(InLTO), SamplePGO(SamplePGO) {
121     initializePGOIndirectCallPromotionLegacyPassPass(
122         *PassRegistry::getPassRegistry());
123   }
124 
125   void getAnalysisUsage(AnalysisUsage &AU) const override {
126     AU.addRequired<ProfileSummaryInfoWrapperPass>();
127   }
128 
129   StringRef getPassName() const override { return "PGOIndirectCallPromotion"; }
130 
131 private:
132   bool runOnModule(Module &M) override;
133 
134   // If this pass is called in LTO. We need to special handling the PGOFuncName
135   // for the static variables due to LTO's internalization.
136   bool InLTO;
137 
138   // If this pass is called in SamplePGO. We need to add the prof metadata to
139   // the promoted direct call.
140   bool SamplePGO;
141 };
142 } // end anonymous namespace
143 
144 char PGOIndirectCallPromotionLegacyPass::ID = 0;
145 INITIALIZE_PASS(PGOIndirectCallPromotionLegacyPass, "pgo-icall-prom",
146                 "Use PGO instrumentation profile to promote indirect calls to "
147                 "direct calls.",
148                 false, false)
149 
150 ModulePass *llvm::createPGOIndirectCallPromotionLegacyPass(bool InLTO,
151                                                            bool SamplePGO) {
152   return new PGOIndirectCallPromotionLegacyPass(InLTO, SamplePGO);
153 }
154 
155 namespace {
156 // The class for main data structure to promote indirect calls to conditional
157 // direct calls.
158 class ICallPromotionFunc {
159 private:
160   Function &F;
161   Module *M;
162 
163   // Symtab that maps indirect call profile values to function names and
164   // defines.
165   InstrProfSymtab *Symtab;
166 
167   bool SamplePGO;
168 
169   OptimizationRemarkEmitter &ORE;
170 
171   // A struct that records the direct target and it's call count.
172   struct PromotionCandidate {
173     Function *TargetFunction;
174     uint64_t Count;
175     PromotionCandidate(Function *F, uint64_t C) : TargetFunction(F), Count(C) {}
176   };
177 
178   // Check if the indirect-call call site should be promoted. Return the number
179   // of promotions. Inst is the candidate indirect call, ValueDataRef
180   // contains the array of value profile data for profiled targets,
181   // TotalCount is the total profiled count of call executions, and
182   // NumCandidates is the number of candidate entries in ValueDataRef.
183   std::vector<PromotionCandidate> getPromotionCandidatesForCallSite(
184       Instruction *Inst, const ArrayRef<InstrProfValueData> &ValueDataRef,
185       uint64_t TotalCount, uint32_t NumCandidates);
186 
187   // Promote a list of targets for one indirect-call callsite. Return
188   // the number of promotions.
189   uint32_t tryToPromote(Instruction *Inst,
190                         const std::vector<PromotionCandidate> &Candidates,
191                         uint64_t &TotalCount);
192 
193   // Noncopyable
194   ICallPromotionFunc(const ICallPromotionFunc &other) = delete;
195   ICallPromotionFunc &operator=(const ICallPromotionFunc &other) = delete;
196 
197 public:
198   ICallPromotionFunc(Function &Func, Module *Modu, InstrProfSymtab *Symtab,
199                      bool SamplePGO, OptimizationRemarkEmitter &ORE)
200       : F(Func), M(Modu), Symtab(Symtab), SamplePGO(SamplePGO), ORE(ORE) {}
201 
202   bool processFunction(ProfileSummaryInfo *PSI);
203 };
204 } // end anonymous namespace
205 
206 bool llvm::isLegalToPromote(Instruction *Inst, Function *F,
207                             const char **Reason) {
208   // Check the return type.
209   Type *CallRetType = Inst->getType();
210   if (!CallRetType->isVoidTy()) {
211     Type *FuncRetType = F->getReturnType();
212     if (FuncRetType != CallRetType &&
213         !CastInst::isBitCastable(FuncRetType, CallRetType)) {
214       if (Reason)
215         *Reason = "Return type mismatch";
216       return false;
217     }
218   }
219 
220   // Check if the arguments are compatible with the parameters
221   FunctionType *DirectCalleeType = F->getFunctionType();
222   unsigned ParamNum = DirectCalleeType->getFunctionNumParams();
223   CallSite CS(Inst);
224   unsigned ArgNum = CS.arg_size();
225 
226   if (ParamNum != ArgNum && !DirectCalleeType->isVarArg()) {
227     if (Reason)
228       *Reason = "The number of arguments mismatch";
229     return false;
230   }
231 
232   for (unsigned I = 0; I < ParamNum; ++I) {
233     Type *PTy = DirectCalleeType->getFunctionParamType(I);
234     Type *ATy = CS.getArgument(I)->getType();
235     if (PTy == ATy)
236       continue;
237     if (!CastInst::castIsValid(Instruction::BitCast, CS.getArgument(I), PTy)) {
238       if (Reason)
239         *Reason = "Argument type mismatch";
240       return false;
241     }
242   }
243 
244   DEBUG(dbgs() << " #" << NumOfPGOICallPromotion << " Promote the icall to "
245                << F->getName() << "\n");
246   return true;
247 }
248 
249 // Indirect-call promotion heuristic. The direct targets are sorted based on
250 // the count. Stop at the first target that is not promoted.
251 std::vector<ICallPromotionFunc::PromotionCandidate>
252 ICallPromotionFunc::getPromotionCandidatesForCallSite(
253     Instruction *Inst, const ArrayRef<InstrProfValueData> &ValueDataRef,
254     uint64_t TotalCount, uint32_t NumCandidates) {
255   std::vector<PromotionCandidate> Ret;
256 
257   DEBUG(dbgs() << " \nWork on callsite #" << NumOfPGOICallsites << *Inst
258                << " Num_targets: " << ValueDataRef.size()
259                << " Num_candidates: " << NumCandidates << "\n");
260   NumOfPGOICallsites++;
261   if (ICPCSSkip != 0 && NumOfPGOICallsites <= ICPCSSkip) {
262     DEBUG(dbgs() << " Skip: User options.\n");
263     return Ret;
264   }
265 
266   for (uint32_t I = 0; I < NumCandidates; I++) {
267     uint64_t Count = ValueDataRef[I].Count;
268     assert(Count <= TotalCount);
269     uint64_t Target = ValueDataRef[I].Value;
270     DEBUG(dbgs() << " Candidate " << I << " Count=" << Count
271                  << "  Target_func: " << Target << "\n");
272 
273     if (ICPInvokeOnly && dyn_cast<CallInst>(Inst)) {
274       DEBUG(dbgs() << " Not promote: User options.\n");
275       ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "UserOptions", Inst)
276                << " Not promote: User options");
277       break;
278     }
279     if (ICPCallOnly && dyn_cast<InvokeInst>(Inst)) {
280       DEBUG(dbgs() << " Not promote: User option.\n");
281       ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "UserOptions", Inst)
282                << " Not promote: User options");
283       break;
284     }
285     if (ICPCutOff != 0 && NumOfPGOICallPromotion >= ICPCutOff) {
286       DEBUG(dbgs() << " Not promote: Cutoff reached.\n");
287       ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "CutOffReached", Inst)
288                << " Not promote: Cutoff reached");
289       break;
290     }
291 
292     Function *TargetFunction = Symtab->getFunction(Target);
293     if (TargetFunction == nullptr) {
294       DEBUG(dbgs() << " Not promote: Cannot find the target\n");
295       ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "UnableToFindTarget", Inst)
296                << "Cannot promote indirect call: target not found");
297       break;
298     }
299 
300     const char *Reason = nullptr;
301     if (!isLegalToPromote(Inst, TargetFunction, &Reason)) {
302       using namespace ore;
303       ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "UnableToPromote", Inst)
304                << "Cannot promote indirect call to "
305                << NV("TargetFunction", TargetFunction) << " with count of "
306                << NV("Count", Count) << ": " << Reason);
307       break;
308     }
309 
310     Ret.push_back(PromotionCandidate(TargetFunction, Count));
311     TotalCount -= Count;
312   }
313   return Ret;
314 }
315 
316 // Create a diamond structure for If_Then_Else. Also update the profile
317 // count. Do the fix-up for the invoke instruction.
318 static void createIfThenElse(Instruction *Inst, Function *DirectCallee,
319                              uint64_t Count, uint64_t TotalCount,
320                              BasicBlock **DirectCallBB,
321                              BasicBlock **IndirectCallBB,
322                              BasicBlock **MergeBB) {
323   CallSite CS(Inst);
324   Value *OrigCallee = CS.getCalledValue();
325 
326   IRBuilder<> BBBuilder(Inst);
327   LLVMContext &Ctx = Inst->getContext();
328   Value *BCI1 =
329       BBBuilder.CreateBitCast(OrigCallee, Type::getInt8PtrTy(Ctx), "");
330   Value *BCI2 =
331       BBBuilder.CreateBitCast(DirectCallee, Type::getInt8PtrTy(Ctx), "");
332   Value *PtrCmp = BBBuilder.CreateICmpEQ(BCI1, BCI2, "");
333 
334   uint64_t ElseCount = TotalCount - Count;
335   uint64_t MaxCount = (Count >= ElseCount ? Count : ElseCount);
336   uint64_t Scale = calculateCountScale(MaxCount);
337   MDBuilder MDB(Inst->getContext());
338   MDNode *BranchWeights = MDB.createBranchWeights(
339       scaleBranchCount(Count, Scale), scaleBranchCount(ElseCount, Scale));
340   TerminatorInst *ThenTerm, *ElseTerm;
341   SplitBlockAndInsertIfThenElse(PtrCmp, Inst, &ThenTerm, &ElseTerm,
342                                 BranchWeights);
343   *DirectCallBB = ThenTerm->getParent();
344   (*DirectCallBB)->setName("if.true.direct_targ");
345   *IndirectCallBB = ElseTerm->getParent();
346   (*IndirectCallBB)->setName("if.false.orig_indirect");
347   *MergeBB = Inst->getParent();
348   (*MergeBB)->setName("if.end.icp");
349 
350   // Special handing of Invoke instructions.
351   InvokeInst *II = dyn_cast<InvokeInst>(Inst);
352   if (!II)
353     return;
354 
355   // We don't need branch instructions for invoke.
356   ThenTerm->eraseFromParent();
357   ElseTerm->eraseFromParent();
358 
359   // Add jump from Merge BB to the NormalDest. This is needed for the newly
360   // created direct invoke stmt -- as its NormalDst will be fixed up to MergeBB.
361   BranchInst::Create(II->getNormalDest(), *MergeBB);
362 }
363 
364 // Find the PHI in BB that have the CallResult as the operand.
365 static bool getCallRetPHINode(BasicBlock *BB, Instruction *Inst) {
366   BasicBlock *From = Inst->getParent();
367   for (auto &I : *BB) {
368     PHINode *PHI = dyn_cast<PHINode>(&I);
369     if (!PHI)
370       continue;
371     int IX = PHI->getBasicBlockIndex(From);
372     if (IX == -1)
373       continue;
374     Value *V = PHI->getIncomingValue(IX);
375     if (dyn_cast<Instruction>(V) == Inst)
376       return true;
377   }
378   return false;
379 }
380 
381 // This method fixes up PHI nodes in BB where BB is the UnwindDest of an
382 // invoke instruction. In BB, there may be PHIs with incoming block being
383 // OrigBB (the MergeBB after if-then-else splitting). After moving the invoke
384 // instructions to its own BB, OrigBB is no longer the predecessor block of BB.
385 // Instead two new predecessors are added: IndirectCallBB and DirectCallBB,
386 // so the PHI node's incoming BBs need to be fixed up accordingly.
387 static void fixupPHINodeForUnwind(Instruction *Inst, BasicBlock *BB,
388                                   BasicBlock *OrigBB,
389                                   BasicBlock *IndirectCallBB,
390                                   BasicBlock *DirectCallBB) {
391   for (auto &I : *BB) {
392     PHINode *PHI = dyn_cast<PHINode>(&I);
393     if (!PHI)
394       continue;
395     int IX = PHI->getBasicBlockIndex(OrigBB);
396     if (IX == -1)
397       continue;
398     Value *V = PHI->getIncomingValue(IX);
399     PHI->addIncoming(V, IndirectCallBB);
400     PHI->setIncomingBlock(IX, DirectCallBB);
401   }
402 }
403 
404 // This method fixes up PHI nodes in BB where BB is the NormalDest of an
405 // invoke instruction. In BB, there may be PHIs with incoming block being
406 // OrigBB (the MergeBB after if-then-else splitting). After moving the invoke
407 // instructions to its own BB, a new incoming edge will be added to the original
408 // NormalDstBB from the IndirectCallBB.
409 static void fixupPHINodeForNormalDest(Instruction *Inst, BasicBlock *BB,
410                                       BasicBlock *OrigBB,
411                                       BasicBlock *IndirectCallBB,
412                                       Instruction *NewInst) {
413   for (auto &I : *BB) {
414     PHINode *PHI = dyn_cast<PHINode>(&I);
415     if (!PHI)
416       continue;
417     int IX = PHI->getBasicBlockIndex(OrigBB);
418     if (IX == -1)
419       continue;
420     Value *V = PHI->getIncomingValue(IX);
421     if (dyn_cast<Instruction>(V) == Inst) {
422       PHI->setIncomingBlock(IX, IndirectCallBB);
423       PHI->addIncoming(NewInst, OrigBB);
424       continue;
425     }
426     PHI->addIncoming(V, IndirectCallBB);
427   }
428 }
429 
430 // Add a bitcast instruction to the direct-call return value if needed.
431 static Instruction *insertCallRetCast(const Instruction *Inst,
432                                       Instruction *DirectCallInst,
433                                       Function *DirectCallee) {
434   if (Inst->getType()->isVoidTy())
435     return DirectCallInst;
436 
437   Type *CallRetType = Inst->getType();
438   Type *FuncRetType = DirectCallee->getReturnType();
439   if (FuncRetType == CallRetType)
440     return DirectCallInst;
441 
442   BasicBlock *InsertionBB;
443   if (CallInst *CI = dyn_cast<CallInst>(DirectCallInst))
444     InsertionBB = CI->getParent();
445   else
446     InsertionBB = (dyn_cast<InvokeInst>(DirectCallInst))->getNormalDest();
447 
448   return (new BitCastInst(DirectCallInst, CallRetType, "",
449                           InsertionBB->getTerminator()));
450 }
451 
452 // Create a DirectCall instruction in the DirectCallBB.
453 // Parameter Inst is the indirect-call (invoke) instruction.
454 // DirectCallee is the decl of the direct-call (invoke) target.
455 // DirecallBB is the BB that the direct-call (invoke) instruction is inserted.
456 // MergeBB is the bottom BB of the if-then-else-diamond after the
457 // transformation. For invoke instruction, the edges from DirectCallBB and
458 // IndirectCallBB to MergeBB are removed before this call (during
459 // createIfThenElse).
460 static Instruction *createDirectCallInst(const Instruction *Inst,
461                                          Function *DirectCallee,
462                                          BasicBlock *DirectCallBB,
463                                          BasicBlock *MergeBB) {
464   Instruction *NewInst = Inst->clone();
465   if (CallInst *CI = dyn_cast<CallInst>(NewInst)) {
466     CI->setCalledFunction(DirectCallee);
467     CI->mutateFunctionType(DirectCallee->getFunctionType());
468   } else {
469     // Must be an invoke instruction. Direct invoke's normal destination is
470     // fixed up to MergeBB. MergeBB is the place where return cast is inserted.
471     // Also since IndirectCallBB does not have an edge to MergeBB, there is no
472     // need to insert new PHIs into MergeBB.
473     InvokeInst *II = dyn_cast<InvokeInst>(NewInst);
474     assert(II);
475     II->setCalledFunction(DirectCallee);
476     II->mutateFunctionType(DirectCallee->getFunctionType());
477     II->setNormalDest(MergeBB);
478   }
479 
480   DirectCallBB->getInstList().insert(DirectCallBB->getFirstInsertionPt(),
481                                      NewInst);
482 
483   // Clear the value profile data.
484   NewInst->setMetadata(LLVMContext::MD_prof, nullptr);
485   CallSite NewCS(NewInst);
486   FunctionType *DirectCalleeType = DirectCallee->getFunctionType();
487   unsigned ParamNum = DirectCalleeType->getFunctionNumParams();
488   for (unsigned I = 0; I < ParamNum; ++I) {
489     Type *ATy = NewCS.getArgument(I)->getType();
490     Type *PTy = DirectCalleeType->getParamType(I);
491     if (ATy != PTy) {
492       BitCastInst *BI = new BitCastInst(NewCS.getArgument(I), PTy, "", NewInst);
493       NewCS.setArgument(I, BI);
494     }
495   }
496 
497   return insertCallRetCast(Inst, NewInst, DirectCallee);
498 }
499 
500 // Create a PHI to unify the return values of calls.
501 static void insertCallRetPHI(Instruction *Inst, Instruction *CallResult,
502                              Function *DirectCallee) {
503   if (Inst->getType()->isVoidTy())
504     return;
505 
506   BasicBlock *RetValBB = CallResult->getParent();
507 
508   BasicBlock *PHIBB;
509   if (InvokeInst *II = dyn_cast<InvokeInst>(CallResult))
510     RetValBB = II->getNormalDest();
511 
512   PHIBB = RetValBB->getSingleSuccessor();
513   if (getCallRetPHINode(PHIBB, Inst))
514     return;
515 
516   PHINode *CallRetPHI = PHINode::Create(Inst->getType(), 0);
517   PHIBB->getInstList().push_front(CallRetPHI);
518   Inst->replaceAllUsesWith(CallRetPHI);
519   CallRetPHI->addIncoming(Inst, Inst->getParent());
520   CallRetPHI->addIncoming(CallResult, RetValBB);
521 }
522 
523 // This function does the actual indirect-call promotion transformation:
524 // For an indirect-call like:
525 //     Ret = (*Foo)(Args);
526 // It transforms to:
527 //     if (Foo == DirectCallee)
528 //        Ret1 = DirectCallee(Args);
529 //     else
530 //        Ret2 = (*Foo)(Args);
531 //     Ret = phi(Ret1, Ret2);
532 // It adds type casts for the args do not match the parameters and the return
533 // value. Branch weights metadata also updated.
534 // If \p AttachProfToDirectCall is true, a prof metadata is attached to the
535 // new direct call to contain \p Count. This is used by SamplePGO inliner to
536 // check callsite hotness.
537 // Returns the promoted direct call instruction.
538 Instruction *llvm::promoteIndirectCall(Instruction *Inst,
539                                        Function *DirectCallee, uint64_t Count,
540                                        uint64_t TotalCount,
541                                        bool AttachProfToDirectCall,
542                                        OptimizationRemarkEmitter *ORE) {
543   assert(DirectCallee != nullptr);
544   BasicBlock *BB = Inst->getParent();
545   // Just to suppress the non-debug build warning.
546   (void)BB;
547   DEBUG(dbgs() << "\n\n== Basic Block Before ==\n");
548   DEBUG(dbgs() << *BB << "\n");
549 
550   BasicBlock *DirectCallBB, *IndirectCallBB, *MergeBB;
551   createIfThenElse(Inst, DirectCallee, Count, TotalCount, &DirectCallBB,
552                    &IndirectCallBB, &MergeBB);
553 
554   Instruction *NewInst =
555       createDirectCallInst(Inst, DirectCallee, DirectCallBB, MergeBB);
556 
557   if (AttachProfToDirectCall) {
558     SmallVector<uint32_t, 1> Weights;
559     Weights.push_back(Count);
560     MDBuilder MDB(NewInst->getContext());
561     dyn_cast<Instruction>(NewInst->stripPointerCasts())
562         ->setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights));
563   }
564 
565   // Move Inst from MergeBB to IndirectCallBB.
566   Inst->removeFromParent();
567   IndirectCallBB->getInstList().insert(IndirectCallBB->getFirstInsertionPt(),
568                                        Inst);
569 
570   if (InvokeInst *II = dyn_cast<InvokeInst>(Inst)) {
571     // At this point, the original indirect invoke instruction has the original
572     // UnwindDest and NormalDest. For the direct invoke instruction, the
573     // NormalDest points to MergeBB, and MergeBB jumps to the original
574     // NormalDest. MergeBB might have a new bitcast instruction for the return
575     // value. The PHIs are with the original NormalDest. Since we now have two
576     // incoming edges to NormalDest and UnwindDest, we have to do some fixups.
577     //
578     // UnwindDest will not use the return value. So pass nullptr here.
579     fixupPHINodeForUnwind(Inst, II->getUnwindDest(), MergeBB, IndirectCallBB,
580                           DirectCallBB);
581     // We don't need to update the operand from NormalDest for DirectCallBB.
582     // Pass nullptr here.
583     fixupPHINodeForNormalDest(Inst, II->getNormalDest(), MergeBB,
584                               IndirectCallBB, NewInst);
585   }
586 
587   insertCallRetPHI(Inst, NewInst, DirectCallee);
588 
589   DEBUG(dbgs() << "\n== Basic Blocks After ==\n");
590   DEBUG(dbgs() << *BB << *DirectCallBB << *IndirectCallBB << *MergeBB << "\n");
591 
592   using namespace ore;
593   if (ORE)
594     ORE->emit(OptimizationRemark(DEBUG_TYPE, "Promoted", Inst)
595               << "Promote indirect call to " << NV("DirectCallee", DirectCallee)
596               << " with count " << NV("Count", Count) << " out of "
597               << NV("TotalCount", TotalCount));
598   return NewInst;
599 }
600 
601 // Promote indirect-call to conditional direct-call for one callsite.
602 uint32_t ICallPromotionFunc::tryToPromote(
603     Instruction *Inst, const std::vector<PromotionCandidate> &Candidates,
604     uint64_t &TotalCount) {
605   uint32_t NumPromoted = 0;
606 
607   for (auto &C : Candidates) {
608     uint64_t Count = C.Count;
609     promoteIndirectCall(Inst, C.TargetFunction, Count, TotalCount, SamplePGO,
610                         &ORE);
611     assert(TotalCount >= Count);
612     TotalCount -= Count;
613     NumOfPGOICallPromotion++;
614     NumPromoted++;
615   }
616   return NumPromoted;
617 }
618 
619 // Traverse all the indirect-call callsite and get the value profile
620 // annotation to perform indirect-call promotion.
621 bool ICallPromotionFunc::processFunction(ProfileSummaryInfo *PSI) {
622   bool Changed = false;
623   ICallPromotionAnalysis ICallAnalysis;
624   for (auto &I : findIndirectCallSites(F)) {
625     uint32_t NumVals, NumCandidates;
626     uint64_t TotalCount;
627     auto ICallProfDataRef = ICallAnalysis.getPromotionCandidatesForInstruction(
628         I, NumVals, TotalCount, NumCandidates);
629     if (!NumCandidates ||
630         (PSI && PSI->hasProfileSummary() && !PSI->isHotCount(TotalCount)))
631       continue;
632     auto PromotionCandidates = getPromotionCandidatesForCallSite(
633         I, ICallProfDataRef, TotalCount, NumCandidates);
634     uint32_t NumPromoted = tryToPromote(I, PromotionCandidates, TotalCount);
635     if (NumPromoted == 0)
636       continue;
637 
638     Changed = true;
639     // Adjust the MD.prof metadata. First delete the old one.
640     I->setMetadata(LLVMContext::MD_prof, nullptr);
641     // If all promoted, we don't need the MD.prof metadata.
642     if (TotalCount == 0 || NumPromoted == NumVals)
643       continue;
644     // Otherwise we need update with the un-promoted records back.
645     annotateValueSite(*M, *I, ICallProfDataRef.slice(NumPromoted), TotalCount,
646                       IPVK_IndirectCallTarget, NumCandidates);
647   }
648   return Changed;
649 }
650 
651 // A wrapper function that does the actual work.
652 static bool promoteIndirectCalls(Module &M, ProfileSummaryInfo *PSI,
653                                  bool InLTO, bool SamplePGO,
654                                  ModuleAnalysisManager *AM = nullptr) {
655   if (DisableICP)
656     return false;
657   InstrProfSymtab Symtab;
658   if (Error E = Symtab.create(M, InLTO)) {
659     std::string SymtabFailure = toString(std::move(E));
660     DEBUG(dbgs() << "Failed to create symtab: " << SymtabFailure << "\n");
661     (void)SymtabFailure;
662     return false;
663   }
664   bool Changed = false;
665   for (auto &F : M) {
666     if (F.isDeclaration())
667       continue;
668     if (F.hasFnAttribute(Attribute::OptimizeNone))
669       continue;
670 
671     std::unique_ptr<OptimizationRemarkEmitter> OwnedORE;
672     OptimizationRemarkEmitter *ORE;
673     if (AM) {
674       auto &FAM =
675           AM->getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
676       ORE = &FAM.getResult<OptimizationRemarkEmitterAnalysis>(F);
677     } else {
678       OwnedORE = make_unique<OptimizationRemarkEmitter>(&F);
679       ORE = OwnedORE.get();
680     }
681 
682     ICallPromotionFunc ICallPromotion(F, &M, &Symtab, SamplePGO, *ORE);
683     bool FuncChanged = ICallPromotion.processFunction(PSI);
684     if (ICPDUMPAFTER && FuncChanged) {
685       DEBUG(dbgs() << "\n== IR Dump After =="; F.print(dbgs()));
686       DEBUG(dbgs() << "\n");
687     }
688     Changed |= FuncChanged;
689     if (ICPCutOff != 0 && NumOfPGOICallPromotion >= ICPCutOff) {
690       DEBUG(dbgs() << " Stop: Cutoff reached.\n");
691       break;
692     }
693   }
694   return Changed;
695 }
696 
697 bool PGOIndirectCallPromotionLegacyPass::runOnModule(Module &M) {
698   ProfileSummaryInfo *PSI =
699       getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
700 
701   // Command-line option has the priority for InLTO.
702   return promoteIndirectCalls(M, PSI, InLTO | ICPLTOMode,
703                               SamplePGO | ICPSamplePGOMode);
704 }
705 
706 PreservedAnalyses PGOIndirectCallPromotion::run(Module &M,
707                                                 ModuleAnalysisManager &AM) {
708   ProfileSummaryInfo *PSI = &AM.getResult<ProfileSummaryAnalysis>(M);
709 
710   if (!promoteIndirectCalls(M, PSI, InLTO | ICPLTOMode,
711                             SamplePGO | ICPSamplePGOMode, &AM))
712     return PreservedAnalyses::all();
713 
714   return PreservedAnalyses::none();
715 }
716