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