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