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