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