blob: 2e91d21a7a42470e9813a5f89d49ea2cf91cd76c [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
Eugene Zelenkocdc71612016-08-11 17:20:18 +000016#include "llvm/ADT/ArrayRef.h"
Rong Xu6e34c492016-04-27 23:20:27 +000017#include "llvm/ADT/Statistic.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000018#include "llvm/ADT/StringRef.h"
19#include "llvm/ADT/Twine.h"
Teresa Johnson1e44b5d2016-07-12 21:13:44 +000020#include "llvm/Analysis/IndirectCallPromotionAnalysis.h"
21#include "llvm/Analysis/IndirectCallSiteVisitor.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000022#include "llvm/IR/BasicBlock.h"
Rong Xu6e34c492016-04-27 23:20:27 +000023#include "llvm/IR/CallSite.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000024#include "llvm/IR/DerivedTypes.h"
Rong Xu6e34c492016-04-27 23:20:27 +000025#include "llvm/IR/DiagnosticInfo.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000026#include "llvm/IR/Function.h"
Rong Xu6e34c492016-04-27 23:20:27 +000027#include "llvm/IR/IRBuilder.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000028#include "llvm/IR/InstrTypes.h"
29#include "llvm/IR/Instruction.h"
Rong Xu6e34c492016-04-27 23:20:27 +000030#include "llvm/IR/Instructions.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000031#include "llvm/IR/LLVMContext.h"
Rong Xu6e34c492016-04-27 23:20:27 +000032#include "llvm/IR/MDBuilder.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000033#include "llvm/IR/PassManager.h"
34#include "llvm/IR/Type.h"
Rong Xu6e34c492016-04-27 23:20:27 +000035#include "llvm/Pass.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000036#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"
Rong Xu6e34c492016-04-27 23:20:27 +000041#include "llvm/Support/Debug.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000042#include "llvm/Support/ErrorHandling.h"
Rong Xu6e34c492016-04-27 23:20:27 +000043#include "llvm/Transforms/Instrumentation.h"
Xinliang David Lif3c7a352016-05-16 16:31:07 +000044#include "llvm/Transforms/PGOInstrumentation.h"
Rong Xu6e34c492016-04-27 23:20:27 +000045#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000046#include <cassert>
47#include <cstdint>
Rong Xu6e34c492016-04-27 23:20:27 +000048#include <vector>
49
50using namespace llvm;
51
Xinliang David Li0b293302016-06-02 01:52:05 +000052#define DEBUG_TYPE "pgo-icall-prom"
Rong Xu6e34c492016-04-27 23:20:27 +000053
54STATISTIC(NumOfPGOICallPromotion, "Number of indirect call promotions.");
55STATISTIC(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.
59static cl::opt<bool> DisableICP("disable-icp", cl::init(false), cl::Hidden,
60 cl::desc("Disable indirect call promotion"));
61
Rong Xu6e34c492016-04-27 23:20:27 +000062// 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.
66static 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.
72static 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.
79static cl::opt<bool> ICPLTOMode("icp-lto", cl::init(false), cl::Hidden,
80 cl::desc("Run indirect-call promotion in LTO "
81 "mode"));
Teresa Johnson1e44b5d2016-07-12 21:13:44 +000082
Rong Xu6e34c492016-04-27 23:20:27 +000083// If the option is set to true, only call instructions will be considered for
84// transformation -- invoke instructions will be ignored.
85static 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.
92static 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.
99static cl::opt<bool>
100 ICPDUMPAFTER("icp-dumpafter", cl::init(false), cl::Hidden,
101 cl::desc("Dump IR after transformation happens"));
102
103namespace {
Xinliang David Li72616182016-05-15 01:04:24 +0000104class PGOIndirectCallPromotionLegacyPass : public ModulePass {
Rong Xu6e34c492016-04-27 23:20:27 +0000105public:
106 static char ID;
107
Xinliang David Li72616182016-05-15 01:04:24 +0000108 PGOIndirectCallPromotionLegacyPass(bool InLTO = false)
109 : ModulePass(ID), InLTO(InLTO) {
110 initializePGOIndirectCallPromotionLegacyPassPass(
111 *PassRegistry::getPassRegistry());
Rong Xu6e34c492016-04-27 23:20:27 +0000112 }
113
114 const char *getPassName() const override {
115 return "PGOIndirectCallPromotion";
116 }
117
118private:
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
Xinliang David Li72616182016-05-15 01:04:24 +0000127char PGOIndirectCallPromotionLegacyPass::ID = 0;
128INITIALIZE_PASS(PGOIndirectCallPromotionLegacyPass, "pgo-icall-prom",
Rong Xu6e34c492016-04-27 23:20:27 +0000129 "Use PGO instrumentation profile to promote indirect calls to "
130 "direct calls.",
131 false, false)
132
Xinliang David Li72616182016-05-15 01:04:24 +0000133ModulePass *llvm::createPGOIndirectCallPromotionLegacyPass(bool InLTO) {
134 return new PGOIndirectCallPromotionLegacyPass(InLTO);
Rong Xu6e34c492016-04-27 23:20:27 +0000135}
136
Benjamin Kramera65b6102016-05-15 15:18:11 +0000137namespace {
Rong Xu6e34c492016-04-27 23:20:27 +0000138// The class for main data structure to promote indirect calls to conditional
139// direct calls.
140class ICallPromotionFunc {
141private:
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
Rong Xu6e34c492016-04-27 23:20:27 +0000149 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
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000169 // 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.
Rong Xu6e34c492016-04-27 23:20:27 +0000173 std::vector<PromotionCandidate> getPromotionCandidatesForCallSite(
174 Instruction *Inst, const ArrayRef<InstrProfValueData> &ValueDataRef,
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000175 uint64_t TotalCount, uint32_t NumCandidates);
Rong Xu6e34c492016-04-27 23:20:27 +0000176
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
216public:
217 ICallPromotionFunc(Function &Func, Module *Modu, InstrProfSymtab *Symtab)
218 : F(Func), M(Modu), Symtab(Symtab) {
Rong Xu6e34c492016-04-27 23:20:27 +0000219 }
Eugene Zelenkocdc71612016-08-11 17:20:18 +0000220
Rong Xu6e34c492016-04-27 23:20:27 +0000221 bool processFunction();
222};
Benjamin Kramera65b6102016-05-15 15:18:11 +0000223} // end anonymous namespace
Rong Xu6e34c492016-04-27 23:20:27 +0000224
Rong Xu6e34c492016-04-27 23:20:27 +0000225ICallPromotionFunc::TargetStatus
226ICallPromotionFunc::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.
266std::vector<ICallPromotionFunc::PromotionCandidate>
267ICallPromotionFunc::getPromotionCandidatesForCallSite(
268 Instruction *Inst, const ArrayRef<InstrProfValueData> &ValueDataRef,
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000269 uint64_t TotalCount, uint32_t NumCandidates) {
Rong Xu6e34c492016-04-27 23:20:27 +0000270 std::vector<PromotionCandidate> Ret;
271
272 DEBUG(dbgs() << " \nWork on callsite #" << NumOfPGOICallsites << *Inst
Teresa Johnson8950ad12016-07-12 21:29:05 +0000273 << " Num_targets: " << ValueDataRef.size()
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000274 << " Num_candidates: " << NumCandidates << "\n");
Rong Xu6e34c492016-04-27 23:20:27 +0000275 NumOfPGOICallsites++;
276 if (ICPCSSkip != 0 && NumOfPGOICallsites <= ICPCSSkip) {
277 DEBUG(dbgs() << " Skip: User options.\n");
278 return Ret;
279 }
280
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000281 for (uint32_t I = 0; I < NumCandidates; I++) {
Rong Xu6e34c492016-04-27 23:20:27 +0000282 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
Teresa Johnsonce7de9b2016-07-17 14:46:58 +0000288 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 }
Rong Xu6e34c492016-04-27 23:20:27 +0000296 if (ICPCutOff != 0 && NumOfPGOICallPromotion >= ICPCutOff) {
297 DEBUG(dbgs() << " Not promote: Cutoff reached.\n");
298 break;
299 }
Rong Xu6e34c492016-04-27 23:20:27 +0000300 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");
Rong Xu62d5e472016-04-28 17:49:56 +0000306 emitOptimizationRemarkMissed(
Xinliang David Li0b293302016-06-02 01:52:05 +0000307 F.getContext(), "pgo-icall-prom", F, Inst->getDebugLoc(),
Rong Xu6e34c492016-04-27 23:20:27 +0000308 Twine("Cannot promote indirect call to ") +
Rong Xu62d5e472016-04-28 17:49:56 +0000309 (TargetFuncName.empty() ? Twine(Target) : Twine(TargetFuncName)) +
310 Twine(" with count of ") + Twine(Count) + ": " + Reason);
Rong Xu6e34c492016-04-27 23:20:27 +0000311 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.
321static 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.
368static 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.
390static 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.
412static 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.
Rong Xu6e34c492016-04-27 23:20:27 +0000434static 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).
463static 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.
Eugene Zelenkocdc71612016-08-11 17:20:18 +0000487 NewInst->setMetadata(LLVMContext::MD_prof, nullptr);
Rong Xu6e34c492016-04-27 23:20:27 +0000488 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.
504static 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.
537void 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
Rong Xu62d5e472016-04-28 17:49:56 +0000580 emitOptimizationRemark(
Xinliang David Li0b293302016-06-02 01:52:05 +0000581 F.getContext(), "pgo-icall-prom", F, Inst->getDebugLoc(),
Rong Xu62d5e472016-04-28 17:49:56 +0000582 Twine("Promote indirect call to ") + DirectCallee->getName() +
583 " with count " + Twine(Count) + " out of " + Twine(TotalCount));
Rong Xu6e34c492016-04-27 23:20:27 +0000584}
585
586// Promote indirect-call to conditional direct-call for one callsite.
587uint32_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.
605bool ICallPromotionFunc::processFunction() {
606 bool Changed = false;
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000607 ICallPromotionAnalysis ICallAnalysis;
Rong Xu6e34c492016-04-27 23:20:27 +0000608 for (auto &I : findIndirectCallSites(F)) {
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000609 uint32_t NumVals, NumCandidates;
Rong Xu6e34c492016-04-27 23:20:27 +0000610 uint64_t TotalCount;
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000611 auto ICallProfDataRef = ICallAnalysis.getPromotionCandidatesForInstruction(
612 I, NumVals, TotalCount, NumCandidates);
613 if (!NumCandidates)
Rong Xu6e34c492016-04-27 23:20:27 +0000614 continue;
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000615 auto PromotionCandidates = getPromotionCandidatesForCallSite(
616 I, ICallProfDataRef, TotalCount, NumCandidates);
Rong Xu6e34c492016-04-27 23:20:27 +0000617 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.
Eugene Zelenkocdc71612016-08-11 17:20:18 +0000623 I->setMetadata(LLVMContext::MD_prof, nullptr);
Rong Xu6e34c492016-04-27 23:20:27 +0000624 // 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.
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000628 annotateValueSite(*M, *I, ICallProfDataRef.slice(NumPromoted), TotalCount,
629 IPVK_IndirectCallTarget, NumCandidates);
Rong Xu6e34c492016-04-27 23:20:27 +0000630 }
631 return Changed;
632}
633
634// A wrapper function that does the actual work.
635static 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
Xinliang David Li72616182016-05-15 01:04:24 +0000661bool PGOIndirectCallPromotionLegacyPass::runOnModule(Module &M) {
Rong Xu6e34c492016-04-27 23:20:27 +0000662 // Command-line option has the priority for InLTO.
Xinliang David Li7d0fed72016-05-17 21:06:16 +0000663 return promoteIndirectCalls(M, InLTO | ICPLTOMode);
Rong Xu6e34c492016-04-27 23:20:27 +0000664}
Xinliang David Lif3c7a352016-05-16 16:31:07 +0000665
Sean Silvafd03ac62016-08-09 00:28:38 +0000666PreservedAnalyses PGOIndirectCallPromotion::run(Module &M, ModuleAnalysisManager &AM) {
Xinliang David Li7d0fed72016-05-17 21:06:16 +0000667 if (!promoteIndirectCalls(M, InLTO | ICPLTOMode))
Xinliang David Lif3c7a352016-05-16 16:31:07 +0000668 return PreservedAnalyses::all();
669
670 return PreservedAnalyses::none();
671}