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