blob: 8b9bbb4995589ba46cc694f18cb29d84f7b6a7fd [file] [log] [blame]
Eugene Zelenkofce43572017-10-21 00:57:46 +00001//===- IndirectCallPromotion.cpp - Optimizations based on value profiling -===//
Rong Xu6e34c492016-04-27 23:20:27 +00002//
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"
Eugene Zelenkofce43572017-10-21 00:57:46 +000017#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/SmallVector.h"
Rong Xu6e34c492016-04-27 23:20:27 +000019#include "llvm/ADT/Statistic.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000020#include "llvm/ADT/StringRef.h"
Teresa Johnson1e44b5d2016-07-12 21:13:44 +000021#include "llvm/Analysis/IndirectCallPromotionAnalysis.h"
22#include "llvm/Analysis/IndirectCallSiteVisitor.h"
Adam Nemet0965da22017-10-09 23:19:02 +000023#include "llvm/Analysis/OptimizationRemarkEmitter.h"
Dehao Chen34cfcb22017-08-08 20:57:33 +000024#include "llvm/Analysis/ProfileSummaryInfo.h"
Eugene Zelenkofce43572017-10-21 00:57:46 +000025#include "llvm/IR/Attributes.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000026#include "llvm/IR/BasicBlock.h"
Rong Xu6e34c492016-04-27 23:20:27 +000027#include "llvm/IR/CallSite.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000028#include "llvm/IR/DerivedTypes.h"
Rong Xu6e34c492016-04-27 23:20:27 +000029#include "llvm/IR/DiagnosticInfo.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000030#include "llvm/IR/Function.h"
Rong Xu6e34c492016-04-27 23:20:27 +000031#include "llvm/IR/IRBuilder.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000032#include "llvm/IR/InstrTypes.h"
33#include "llvm/IR/Instruction.h"
Rong Xu6e34c492016-04-27 23:20:27 +000034#include "llvm/IR/Instructions.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000035#include "llvm/IR/LLVMContext.h"
Rong Xu6e34c492016-04-27 23:20:27 +000036#include "llvm/IR/MDBuilder.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000037#include "llvm/IR/PassManager.h"
38#include "llvm/IR/Type.h"
Eugene Zelenkofce43572017-10-21 00:57:46 +000039#include "llvm/IR/Value.h"
Rong Xu6e34c492016-04-27 23:20:27 +000040#include "llvm/Pass.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000041#include "llvm/ProfileData/InstrProf.h"
42#include "llvm/Support/Casting.h"
43#include "llvm/Support/CommandLine.h"
Eugene Zelenkofce43572017-10-21 00:57:46 +000044#include "llvm/Support/Error.h"
Rong Xu6e34c492016-04-27 23:20:27 +000045#include "llvm/Support/Debug.h"
Eugene Zelenkofce43572017-10-21 00:57:46 +000046#include "llvm/Support/raw_ostream.h"
Rong Xu6e34c492016-04-27 23:20:27 +000047#include "llvm/Transforms/Instrumentation.h"
Xinliang David Lif3c7a352016-05-16 16:31:07 +000048#include "llvm/Transforms/PGOInstrumentation.h"
Rong Xu6e34c492016-04-27 23:20:27 +000049#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000050#include <cassert>
51#include <cstdint>
Eugene Zelenkofce43572017-10-21 00:57:46 +000052#include <memory>
53#include <string>
54#include <utility>
Rong Xu6e34c492016-04-27 23:20:27 +000055#include <vector>
56
57using namespace llvm;
58
Xinliang David Li0b293302016-06-02 01:52:05 +000059#define DEBUG_TYPE "pgo-icall-prom"
Rong Xu6e34c492016-04-27 23:20:27 +000060
61STATISTIC(NumOfPGOICallPromotion, "Number of indirect call promotions.");
62STATISTIC(NumOfPGOICallsites, "Number of indirect call candidate sites.");
63
64// Command line option to disable indirect-call promotion with the default as
65// false. This is for debug purpose.
66static cl::opt<bool> DisableICP("disable-icp", cl::init(false), cl::Hidden,
67 cl::desc("Disable indirect call promotion"));
68
Rong Xu6e34c492016-04-27 23:20:27 +000069// Set the cutoff value for the promotion. If the value is other than 0, we
70// stop the transformation once the total number of promotions equals the cutoff
71// value.
72// For debug use only.
73static cl::opt<unsigned>
74 ICPCutOff("icp-cutoff", cl::init(0), cl::Hidden, cl::ZeroOrMore,
Craig Toppera49e7682017-05-05 22:31:11 +000075 cl::desc("Max number of promotions for this compilation"));
Rong Xu6e34c492016-04-27 23:20:27 +000076
77// If ICPCSSkip is non zero, the first ICPCSSkip callsites will be skipped.
78// For debug use only.
79static cl::opt<unsigned>
80 ICPCSSkip("icp-csskip", cl::init(0), cl::Hidden, cl::ZeroOrMore,
Craig Toppera49e7682017-05-05 22:31:11 +000081 cl::desc("Skip Callsite up to this number for this compilation"));
Rong Xu6e34c492016-04-27 23:20:27 +000082
83// Set if the pass is called in LTO optimization. The difference for LTO mode
84// is the pass won't prefix the source module name to the internal linkage
85// symbols.
86static cl::opt<bool> ICPLTOMode("icp-lto", cl::init(false), cl::Hidden,
87 cl::desc("Run indirect-call promotion in LTO "
88 "mode"));
Teresa Johnson1e44b5d2016-07-12 21:13:44 +000089
Dehao Chencc75d242017-02-23 22:15:18 +000090// Set if the pass is called in SamplePGO mode. The difference for SamplePGO
91// mode is it will add prof metadatato the created direct call.
92static cl::opt<bool>
93 ICPSamplePGOMode("icp-samplepgo", cl::init(false), cl::Hidden,
94 cl::desc("Run indirect-call promotion in SamplePGO mode"));
95
Rong Xu6e34c492016-04-27 23:20:27 +000096// If the option is set to true, only call instructions will be considered for
97// transformation -- invoke instructions will be ignored.
98static cl::opt<bool>
99 ICPCallOnly("icp-call-only", cl::init(false), cl::Hidden,
100 cl::desc("Run indirect-call promotion for call instructions "
101 "only"));
102
103// If the option is set to true, only invoke instructions will be considered for
104// transformation -- call instructions will be ignored.
105static cl::opt<bool> ICPInvokeOnly("icp-invoke-only", cl::init(false),
106 cl::Hidden,
107 cl::desc("Run indirect-call promotion for "
108 "invoke instruction only"));
109
110// Dump the function level IR if the transformation happened in this
111// function. For debug use only.
112static cl::opt<bool>
113 ICPDUMPAFTER("icp-dumpafter", cl::init(false), cl::Hidden,
114 cl::desc("Dump IR after transformation happens"));
115
116namespace {
Eugene Zelenkofce43572017-10-21 00:57:46 +0000117
Xinliang David Li72616182016-05-15 01:04:24 +0000118class PGOIndirectCallPromotionLegacyPass : public ModulePass {
Rong Xu6e34c492016-04-27 23:20:27 +0000119public:
120 static char ID;
121
Dehao Chencc75d242017-02-23 22:15:18 +0000122 PGOIndirectCallPromotionLegacyPass(bool InLTO = false, bool SamplePGO = false)
123 : ModulePass(ID), InLTO(InLTO), SamplePGO(SamplePGO) {
Xinliang David Li72616182016-05-15 01:04:24 +0000124 initializePGOIndirectCallPromotionLegacyPassPass(
125 *PassRegistry::getPassRegistry());
Rong Xu6e34c492016-04-27 23:20:27 +0000126 }
127
Dehao Chen34cfcb22017-08-08 20:57:33 +0000128 void getAnalysisUsage(AnalysisUsage &AU) const override {
129 AU.addRequired<ProfileSummaryInfoWrapperPass>();
130 }
131
Mehdi Amini117296c2016-10-01 02:56:57 +0000132 StringRef getPassName() const override { return "PGOIndirectCallPromotion"; }
Rong Xu6e34c492016-04-27 23:20:27 +0000133
134private:
135 bool runOnModule(Module &M) override;
136
137 // If this pass is called in LTO. We need to special handling the PGOFuncName
138 // for the static variables due to LTO's internalization.
139 bool InLTO;
Dehao Chencc75d242017-02-23 22:15:18 +0000140
141 // If this pass is called in SamplePGO. We need to add the prof metadata to
142 // the promoted direct call.
143 bool SamplePGO;
Rong Xu6e34c492016-04-27 23:20:27 +0000144};
Eugene Zelenkofce43572017-10-21 00:57:46 +0000145
Rong Xu6e34c492016-04-27 23:20:27 +0000146} // end anonymous namespace
147
Xinliang David Li72616182016-05-15 01:04:24 +0000148char PGOIndirectCallPromotionLegacyPass::ID = 0;
Eugene Zelenkofce43572017-10-21 00:57:46 +0000149
Dehao Chen45847d32017-08-14 23:25:21 +0000150INITIALIZE_PASS_BEGIN(PGOIndirectCallPromotionLegacyPass, "pgo-icall-prom",
151 "Use PGO instrumentation profile to promote indirect "
152 "calls to direct calls.",
153 false, false)
154INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
155INITIALIZE_PASS_END(PGOIndirectCallPromotionLegacyPass, "pgo-icall-prom",
156 "Use PGO instrumentation profile to promote indirect "
157 "calls to direct calls.",
158 false, false)
Rong Xu6e34c492016-04-27 23:20:27 +0000159
Dehao Chencc75d242017-02-23 22:15:18 +0000160ModulePass *llvm::createPGOIndirectCallPromotionLegacyPass(bool InLTO,
161 bool SamplePGO) {
162 return new PGOIndirectCallPromotionLegacyPass(InLTO, SamplePGO);
Rong Xu6e34c492016-04-27 23:20:27 +0000163}
164
Benjamin Kramera65b6102016-05-15 15:18:11 +0000165namespace {
Eugene Zelenkofce43572017-10-21 00:57:46 +0000166
Rong Xu6e34c492016-04-27 23:20:27 +0000167// The class for main data structure to promote indirect calls to conditional
168// direct calls.
169class ICallPromotionFunc {
170private:
171 Function &F;
172 Module *M;
173
174 // Symtab that maps indirect call profile values to function names and
175 // defines.
176 InstrProfSymtab *Symtab;
177
Dehao Chencc75d242017-02-23 22:15:18 +0000178 bool SamplePGO;
179
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000180 OptimizationRemarkEmitter &ORE;
Rong Xu6e34c492016-04-27 23:20:27 +0000181
182 // A struct that records the direct target and it's call count.
183 struct PromotionCandidate {
184 Function *TargetFunction;
185 uint64_t Count;
Eugene Zelenkofce43572017-10-21 00:57:46 +0000186
Rong Xu6e34c492016-04-27 23:20:27 +0000187 PromotionCandidate(Function *F, uint64_t C) : TargetFunction(F), Count(C) {}
188 };
189
190 // Check if the indirect-call call site should be promoted. Return the number
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000191 // of promotions. Inst is the candidate indirect call, ValueDataRef
192 // contains the array of value profile data for profiled targets,
193 // TotalCount is the total profiled count of call executions, and
194 // NumCandidates is the number of candidate entries in ValueDataRef.
Rong Xu6e34c492016-04-27 23:20:27 +0000195 std::vector<PromotionCandidate> getPromotionCandidatesForCallSite(
196 Instruction *Inst, const ArrayRef<InstrProfValueData> &ValueDataRef,
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000197 uint64_t TotalCount, uint32_t NumCandidates);
Rong Xu6e34c492016-04-27 23:20:27 +0000198
Rong Xu6e34c492016-04-27 23:20:27 +0000199 // Promote a list of targets for one indirect-call callsite. Return
200 // the number of promotions.
201 uint32_t tryToPromote(Instruction *Inst,
202 const std::vector<PromotionCandidate> &Candidates,
203 uint64_t &TotalCount);
204
Rong Xu6e34c492016-04-27 23:20:27 +0000205public:
Dehao Chencc75d242017-02-23 22:15:18 +0000206 ICallPromotionFunc(Function &Func, Module *Modu, InstrProfSymtab *Symtab,
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000207 bool SamplePGO, OptimizationRemarkEmitter &ORE)
208 : F(Func), M(Modu), Symtab(Symtab), SamplePGO(SamplePGO), ORE(ORE) {}
Eugene Zelenkofce43572017-10-21 00:57:46 +0000209 ICallPromotionFunc(const ICallPromotionFunc &) = delete;
210 ICallPromotionFunc &operator=(const ICallPromotionFunc &) = delete;
Eugene Zelenkocdc71612016-08-11 17:20:18 +0000211
Dehao Chen34cfcb22017-08-08 20:57:33 +0000212 bool processFunction(ProfileSummaryInfo *PSI);
Rong Xu6e34c492016-04-27 23:20:27 +0000213};
Eugene Zelenkofce43572017-10-21 00:57:46 +0000214
Benjamin Kramera65b6102016-05-15 15:18:11 +0000215} // end anonymous namespace
Rong Xu6e34c492016-04-27 23:20:27 +0000216
Dehao Chen6775f5d2017-01-30 22:46:37 +0000217bool llvm::isLegalToPromote(Instruction *Inst, Function *F,
218 const char **Reason) {
Rong Xu6e34c492016-04-27 23:20:27 +0000219 // Check the return type.
220 Type *CallRetType = Inst->getType();
221 if (!CallRetType->isVoidTy()) {
Dehao Chen6775f5d2017-01-30 22:46:37 +0000222 Type *FuncRetType = F->getReturnType();
Rong Xu6e34c492016-04-27 23:20:27 +0000223 if (FuncRetType != CallRetType &&
Dehao Chen6775f5d2017-01-30 22:46:37 +0000224 !CastInst::isBitCastable(FuncRetType, CallRetType)) {
225 if (Reason)
226 *Reason = "Return type mismatch";
227 return false;
228 }
Rong Xu6e34c492016-04-27 23:20:27 +0000229 }
230
231 // Check if the arguments are compatible with the parameters
Dehao Chen6775f5d2017-01-30 22:46:37 +0000232 FunctionType *DirectCalleeType = F->getFunctionType();
Rong Xu6e34c492016-04-27 23:20:27 +0000233 unsigned ParamNum = DirectCalleeType->getFunctionNumParams();
234 CallSite CS(Inst);
235 unsigned ArgNum = CS.arg_size();
236
Dehao Chen6775f5d2017-01-30 22:46:37 +0000237 if (ParamNum != ArgNum && !DirectCalleeType->isVarArg()) {
238 if (Reason)
239 *Reason = "The number of arguments mismatch";
240 return false;
241 }
Rong Xu6e34c492016-04-27 23:20:27 +0000242
243 for (unsigned I = 0; I < ParamNum; ++I) {
244 Type *PTy = DirectCalleeType->getFunctionParamType(I);
245 Type *ATy = CS.getArgument(I)->getType();
246 if (PTy == ATy)
247 continue;
Dehao Chen6775f5d2017-01-30 22:46:37 +0000248 if (!CastInst::castIsValid(Instruction::BitCast, CS.getArgument(I), PTy)) {
249 if (Reason)
Benjamin Kramer365c9bd2017-01-30 23:11:29 +0000250 *Reason = "Argument type mismatch";
Dehao Chen6775f5d2017-01-30 22:46:37 +0000251 return false;
252 }
Rong Xu6e34c492016-04-27 23:20:27 +0000253 }
254
255 DEBUG(dbgs() << " #" << NumOfPGOICallPromotion << " Promote the icall to "
Dehao Chen6775f5d2017-01-30 22:46:37 +0000256 << F->getName() << "\n");
257 return true;
258}
259
Rong Xu6e34c492016-04-27 23:20:27 +0000260// Indirect-call promotion heuristic. The direct targets are sorted based on
261// the count. Stop at the first target that is not promoted.
262std::vector<ICallPromotionFunc::PromotionCandidate>
263ICallPromotionFunc::getPromotionCandidatesForCallSite(
264 Instruction *Inst, const ArrayRef<InstrProfValueData> &ValueDataRef,
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000265 uint64_t TotalCount, uint32_t NumCandidates) {
Rong Xu6e34c492016-04-27 23:20:27 +0000266 std::vector<PromotionCandidate> Ret;
267
268 DEBUG(dbgs() << " \nWork on callsite #" << NumOfPGOICallsites << *Inst
Teresa Johnson8950ad12016-07-12 21:29:05 +0000269 << " Num_targets: " << ValueDataRef.size()
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000270 << " Num_candidates: " << NumCandidates << "\n");
Rong Xu6e34c492016-04-27 23:20:27 +0000271 NumOfPGOICallsites++;
272 if (ICPCSSkip != 0 && NumOfPGOICallsites <= ICPCSSkip) {
273 DEBUG(dbgs() << " Skip: User options.\n");
274 return Ret;
275 }
276
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000277 for (uint32_t I = 0; I < NumCandidates; I++) {
Rong Xu6e34c492016-04-27 23:20:27 +0000278 uint64_t Count = ValueDataRef[I].Count;
279 assert(Count <= TotalCount);
280 uint64_t Target = ValueDataRef[I].Value;
281 DEBUG(dbgs() << " Candidate " << I << " Count=" << Count
282 << " Target_func: " << Target << "\n");
283
Teresa Johnsonce7de9b2016-07-17 14:46:58 +0000284 if (ICPInvokeOnly && dyn_cast<CallInst>(Inst)) {
285 DEBUG(dbgs() << " Not promote: User options.\n");
Vivek Pandya95906582017-10-11 17:12:59 +0000286 ORE.emit([&]() {
287 return OptimizationRemarkMissed(DEBUG_TYPE, "UserOptions", Inst)
288 << " Not promote: User options";
289 });
Teresa Johnsonce7de9b2016-07-17 14:46:58 +0000290 break;
291 }
292 if (ICPCallOnly && dyn_cast<InvokeInst>(Inst)) {
293 DEBUG(dbgs() << " Not promote: User option.\n");
Vivek Pandya95906582017-10-11 17:12:59 +0000294 ORE.emit([&]() {
295 return OptimizationRemarkMissed(DEBUG_TYPE, "UserOptions", Inst)
296 << " Not promote: User options";
297 });
Teresa Johnsonce7de9b2016-07-17 14:46:58 +0000298 break;
299 }
Rong Xu6e34c492016-04-27 23:20:27 +0000300 if (ICPCutOff != 0 && NumOfPGOICallPromotion >= ICPCutOff) {
301 DEBUG(dbgs() << " Not promote: Cutoff reached.\n");
Vivek Pandya95906582017-10-11 17:12:59 +0000302 ORE.emit([&]() {
303 return OptimizationRemarkMissed(DEBUG_TYPE, "CutOffReached", Inst)
304 << " Not promote: Cutoff reached";
305 });
Rong Xu6e34c492016-04-27 23:20:27 +0000306 break;
307 }
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000308
309 Function *TargetFunction = Symtab->getFunction(Target);
310 if (TargetFunction == nullptr) {
311 DEBUG(dbgs() << " Not promote: Cannot find the target\n");
Vivek Pandya95906582017-10-11 17:12:59 +0000312 ORE.emit([&]() {
313 return OptimizationRemarkMissed(DEBUG_TYPE, "UnableToFindTarget", Inst)
314 << "Cannot promote indirect call: target not found";
315 });
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000316 break;
317 }
318
Dehao Chen6775f5d2017-01-30 22:46:37 +0000319 const char *Reason = nullptr;
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000320 if (!isLegalToPromote(Inst, TargetFunction, &Reason)) {
321 using namespace ore;
Eugene Zelenkofce43572017-10-21 00:57:46 +0000322
Vivek Pandya95906582017-10-11 17:12:59 +0000323 ORE.emit([&]() {
324 return OptimizationRemarkMissed(DEBUG_TYPE, "UnableToPromote", Inst)
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000325 << "Cannot promote indirect call to "
326 << NV("TargetFunction", TargetFunction) << " with count of "
Vivek Pandya95906582017-10-11 17:12:59 +0000327 << NV("Count", Count) << ": " << Reason;
328 });
Rong Xu6e34c492016-04-27 23:20:27 +0000329 break;
330 }
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000331
Rong Xu6e34c492016-04-27 23:20:27 +0000332 Ret.push_back(PromotionCandidate(TargetFunction, Count));
333 TotalCount -= Count;
334 }
335 return Ret;
336}
337
338// Create a diamond structure for If_Then_Else. Also update the profile
339// count. Do the fix-up for the invoke instruction.
340static void createIfThenElse(Instruction *Inst, Function *DirectCallee,
341 uint64_t Count, uint64_t TotalCount,
342 BasicBlock **DirectCallBB,
343 BasicBlock **IndirectCallBB,
344 BasicBlock **MergeBB) {
345 CallSite CS(Inst);
346 Value *OrigCallee = CS.getCalledValue();
347
348 IRBuilder<> BBBuilder(Inst);
349 LLVMContext &Ctx = Inst->getContext();
350 Value *BCI1 =
351 BBBuilder.CreateBitCast(OrigCallee, Type::getInt8PtrTy(Ctx), "");
352 Value *BCI2 =
353 BBBuilder.CreateBitCast(DirectCallee, Type::getInt8PtrTy(Ctx), "");
354 Value *PtrCmp = BBBuilder.CreateICmpEQ(BCI1, BCI2, "");
355
356 uint64_t ElseCount = TotalCount - Count;
357 uint64_t MaxCount = (Count >= ElseCount ? Count : ElseCount);
358 uint64_t Scale = calculateCountScale(MaxCount);
359 MDBuilder MDB(Inst->getContext());
360 MDNode *BranchWeights = MDB.createBranchWeights(
361 scaleBranchCount(Count, Scale), scaleBranchCount(ElseCount, Scale));
362 TerminatorInst *ThenTerm, *ElseTerm;
363 SplitBlockAndInsertIfThenElse(PtrCmp, Inst, &ThenTerm, &ElseTerm,
364 BranchWeights);
365 *DirectCallBB = ThenTerm->getParent();
366 (*DirectCallBB)->setName("if.true.direct_targ");
367 *IndirectCallBB = ElseTerm->getParent();
368 (*IndirectCallBB)->setName("if.false.orig_indirect");
369 *MergeBB = Inst->getParent();
370 (*MergeBB)->setName("if.end.icp");
371
372 // Special handing of Invoke instructions.
373 InvokeInst *II = dyn_cast<InvokeInst>(Inst);
374 if (!II)
375 return;
376
377 // We don't need branch instructions for invoke.
378 ThenTerm->eraseFromParent();
379 ElseTerm->eraseFromParent();
380
381 // Add jump from Merge BB to the NormalDest. This is needed for the newly
382 // created direct invoke stmt -- as its NormalDst will be fixed up to MergeBB.
383 BranchInst::Create(II->getNormalDest(), *MergeBB);
384}
385
386// Find the PHI in BB that have the CallResult as the operand.
387static bool getCallRetPHINode(BasicBlock *BB, Instruction *Inst) {
388 BasicBlock *From = Inst->getParent();
389 for (auto &I : *BB) {
390 PHINode *PHI = dyn_cast<PHINode>(&I);
391 if (!PHI)
392 continue;
393 int IX = PHI->getBasicBlockIndex(From);
394 if (IX == -1)
395 continue;
396 Value *V = PHI->getIncomingValue(IX);
397 if (dyn_cast<Instruction>(V) == Inst)
398 return true;
399 }
400 return false;
401}
402
403// This method fixes up PHI nodes in BB where BB is the UnwindDest of an
404// invoke instruction. In BB, there may be PHIs with incoming block being
405// OrigBB (the MergeBB after if-then-else splitting). After moving the invoke
406// instructions to its own BB, OrigBB is no longer the predecessor block of BB.
407// Instead two new predecessors are added: IndirectCallBB and DirectCallBB,
408// so the PHI node's incoming BBs need to be fixed up accordingly.
409static void fixupPHINodeForUnwind(Instruction *Inst, BasicBlock *BB,
410 BasicBlock *OrigBB,
411 BasicBlock *IndirectCallBB,
412 BasicBlock *DirectCallBB) {
413 for (auto &I : *BB) {
414 PHINode *PHI = dyn_cast<PHINode>(&I);
415 if (!PHI)
416 continue;
417 int IX = PHI->getBasicBlockIndex(OrigBB);
418 if (IX == -1)
419 continue;
420 Value *V = PHI->getIncomingValue(IX);
421 PHI->addIncoming(V, IndirectCallBB);
422 PHI->setIncomingBlock(IX, DirectCallBB);
423 }
424}
425
426// This method fixes up PHI nodes in BB where BB is the NormalDest of an
427// invoke instruction. In BB, there may be PHIs with incoming block being
428// OrigBB (the MergeBB after if-then-else splitting). After moving the invoke
429// instructions to its own BB, a new incoming edge will be added to the original
430// NormalDstBB from the IndirectCallBB.
431static void fixupPHINodeForNormalDest(Instruction *Inst, BasicBlock *BB,
432 BasicBlock *OrigBB,
433 BasicBlock *IndirectCallBB,
434 Instruction *NewInst) {
435 for (auto &I : *BB) {
436 PHINode *PHI = dyn_cast<PHINode>(&I);
437 if (!PHI)
438 continue;
439 int IX = PHI->getBasicBlockIndex(OrigBB);
440 if (IX == -1)
441 continue;
442 Value *V = PHI->getIncomingValue(IX);
443 if (dyn_cast<Instruction>(V) == Inst) {
444 PHI->setIncomingBlock(IX, IndirectCallBB);
445 PHI->addIncoming(NewInst, OrigBB);
446 continue;
447 }
448 PHI->addIncoming(V, IndirectCallBB);
449 }
450}
451
452// Add a bitcast instruction to the direct-call return value if needed.
Rong Xu6e34c492016-04-27 23:20:27 +0000453static Instruction *insertCallRetCast(const Instruction *Inst,
454 Instruction *DirectCallInst,
455 Function *DirectCallee) {
456 if (Inst->getType()->isVoidTy())
457 return DirectCallInst;
458
459 Type *CallRetType = Inst->getType();
460 Type *FuncRetType = DirectCallee->getReturnType();
461 if (FuncRetType == CallRetType)
462 return DirectCallInst;
463
464 BasicBlock *InsertionBB;
465 if (CallInst *CI = dyn_cast<CallInst>(DirectCallInst))
466 InsertionBB = CI->getParent();
467 else
468 InsertionBB = (dyn_cast<InvokeInst>(DirectCallInst))->getNormalDest();
469
470 return (new BitCastInst(DirectCallInst, CallRetType, "",
471 InsertionBB->getTerminator()));
472}
473
474// Create a DirectCall instruction in the DirectCallBB.
475// Parameter Inst is the indirect-call (invoke) instruction.
476// DirectCallee is the decl of the direct-call (invoke) target.
477// DirecallBB is the BB that the direct-call (invoke) instruction is inserted.
478// MergeBB is the bottom BB of the if-then-else-diamond after the
479// transformation. For invoke instruction, the edges from DirectCallBB and
480// IndirectCallBB to MergeBB are removed before this call (during
Dehao Chen9bd60422017-10-06 17:04:55 +0000481// createIfThenElse). Stores the pointer to the Instruction that cast
482// the direct call in \p CastInst.
Rong Xu6e34c492016-04-27 23:20:27 +0000483static Instruction *createDirectCallInst(const Instruction *Inst,
484 Function *DirectCallee,
485 BasicBlock *DirectCallBB,
Dehao Chen9bd60422017-10-06 17:04:55 +0000486 BasicBlock *MergeBB,
487 Instruction *&CastInst) {
Rong Xu6e34c492016-04-27 23:20:27 +0000488 Instruction *NewInst = Inst->clone();
489 if (CallInst *CI = dyn_cast<CallInst>(NewInst)) {
490 CI->setCalledFunction(DirectCallee);
491 CI->mutateFunctionType(DirectCallee->getFunctionType());
492 } else {
493 // Must be an invoke instruction. Direct invoke's normal destination is
494 // fixed up to MergeBB. MergeBB is the place where return cast is inserted.
495 // Also since IndirectCallBB does not have an edge to MergeBB, there is no
496 // need to insert new PHIs into MergeBB.
497 InvokeInst *II = dyn_cast<InvokeInst>(NewInst);
498 assert(II);
499 II->setCalledFunction(DirectCallee);
500 II->mutateFunctionType(DirectCallee->getFunctionType());
501 II->setNormalDest(MergeBB);
502 }
503
504 DirectCallBB->getInstList().insert(DirectCallBB->getFirstInsertionPt(),
505 NewInst);
506
507 // Clear the value profile data.
Eugene Zelenkocdc71612016-08-11 17:20:18 +0000508 NewInst->setMetadata(LLVMContext::MD_prof, nullptr);
Rong Xu6e34c492016-04-27 23:20:27 +0000509 CallSite NewCS(NewInst);
510 FunctionType *DirectCalleeType = DirectCallee->getFunctionType();
511 unsigned ParamNum = DirectCalleeType->getFunctionNumParams();
512 for (unsigned I = 0; I < ParamNum; ++I) {
513 Type *ATy = NewCS.getArgument(I)->getType();
514 Type *PTy = DirectCalleeType->getParamType(I);
515 if (ATy != PTy) {
516 BitCastInst *BI = new BitCastInst(NewCS.getArgument(I), PTy, "", NewInst);
517 NewCS.setArgument(I, BI);
518 }
519 }
520
Dehao Chen9bd60422017-10-06 17:04:55 +0000521 CastInst = insertCallRetCast(Inst, NewInst, DirectCallee);
522 return NewInst;
Rong Xu6e34c492016-04-27 23:20:27 +0000523}
524
525// Create a PHI to unify the return values of calls.
526static void insertCallRetPHI(Instruction *Inst, Instruction *CallResult,
527 Function *DirectCallee) {
528 if (Inst->getType()->isVoidTy())
529 return;
530
Taewook Oh572f45a2017-08-28 18:57:00 +0000531 if (Inst->use_empty())
532 return;
533
Rong Xu6e34c492016-04-27 23:20:27 +0000534 BasicBlock *RetValBB = CallResult->getParent();
535
536 BasicBlock *PHIBB;
537 if (InvokeInst *II = dyn_cast<InvokeInst>(CallResult))
538 RetValBB = II->getNormalDest();
539
540 PHIBB = RetValBB->getSingleSuccessor();
541 if (getCallRetPHINode(PHIBB, Inst))
542 return;
543
544 PHINode *CallRetPHI = PHINode::Create(Inst->getType(), 0);
545 PHIBB->getInstList().push_front(CallRetPHI);
546 Inst->replaceAllUsesWith(CallRetPHI);
547 CallRetPHI->addIncoming(Inst, Inst->getParent());
548 CallRetPHI->addIncoming(CallResult, RetValBB);
549}
550
551// This function does the actual indirect-call promotion transformation:
552// For an indirect-call like:
553// Ret = (*Foo)(Args);
554// It transforms to:
555// if (Foo == DirectCallee)
556// Ret1 = DirectCallee(Args);
557// else
558// Ret2 = (*Foo)(Args);
559// Ret = phi(Ret1, Ret2);
560// It adds type casts for the args do not match the parameters and the return
561// value. Branch weights metadata also updated.
Dehao Chencc75d242017-02-23 22:15:18 +0000562// If \p AttachProfToDirectCall is true, a prof metadata is attached to the
563// new direct call to contain \p Count. This is used by SamplePGO inliner to
564// check callsite hotness.
Dehao Chen14bf0292017-01-23 23:18:24 +0000565// Returns the promoted direct call instruction.
566Instruction *llvm::promoteIndirectCall(Instruction *Inst,
567 Function *DirectCallee, uint64_t Count,
Dehao Chencc75d242017-02-23 22:15:18 +0000568 uint64_t TotalCount,
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000569 bool AttachProfToDirectCall,
570 OptimizationRemarkEmitter *ORE) {
Rong Xu6e34c492016-04-27 23:20:27 +0000571 assert(DirectCallee != nullptr);
572 BasicBlock *BB = Inst->getParent();
573 // Just to suppress the non-debug build warning.
574 (void)BB;
575 DEBUG(dbgs() << "\n\n== Basic Block Before ==\n");
576 DEBUG(dbgs() << *BB << "\n");
577
578 BasicBlock *DirectCallBB, *IndirectCallBB, *MergeBB;
579 createIfThenElse(Inst, DirectCallee, Count, TotalCount, &DirectCallBB,
580 &IndirectCallBB, &MergeBB);
581
Dehao Chen9bd60422017-10-06 17:04:55 +0000582 // If the return type of the NewInst is not the same as the Inst, a CastInst
583 // is needed for type casting. Otherwise CastInst is the same as NewInst.
584 Instruction *CastInst = nullptr;
Rong Xu6e34c492016-04-27 23:20:27 +0000585 Instruction *NewInst =
Dehao Chen9bd60422017-10-06 17:04:55 +0000586 createDirectCallInst(Inst, DirectCallee, DirectCallBB, MergeBB, CastInst);
Rong Xu6e34c492016-04-27 23:20:27 +0000587
Dehao Chencc75d242017-02-23 22:15:18 +0000588 if (AttachProfToDirectCall) {
589 SmallVector<uint32_t, 1> Weights;
590 Weights.push_back(Count);
591 MDBuilder MDB(NewInst->getContext());
Dehao Chen9bd60422017-10-06 17:04:55 +0000592 NewInst->setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights));
Dehao Chencc75d242017-02-23 22:15:18 +0000593 }
594
Rong Xu6e34c492016-04-27 23:20:27 +0000595 // Move Inst from MergeBB to IndirectCallBB.
596 Inst->removeFromParent();
597 IndirectCallBB->getInstList().insert(IndirectCallBB->getFirstInsertionPt(),
598 Inst);
599
600 if (InvokeInst *II = dyn_cast<InvokeInst>(Inst)) {
601 // At this point, the original indirect invoke instruction has the original
602 // UnwindDest and NormalDest. For the direct invoke instruction, the
603 // NormalDest points to MergeBB, and MergeBB jumps to the original
604 // NormalDest. MergeBB might have a new bitcast instruction for the return
605 // value. The PHIs are with the original NormalDest. Since we now have two
606 // incoming edges to NormalDest and UnwindDest, we have to do some fixups.
607 //
608 // UnwindDest will not use the return value. So pass nullptr here.
609 fixupPHINodeForUnwind(Inst, II->getUnwindDest(), MergeBB, IndirectCallBB,
610 DirectCallBB);
611 // We don't need to update the operand from NormalDest for DirectCallBB.
612 // Pass nullptr here.
613 fixupPHINodeForNormalDest(Inst, II->getNormalDest(), MergeBB,
Dehao Chen9bd60422017-10-06 17:04:55 +0000614 IndirectCallBB, CastInst);
Rong Xu6e34c492016-04-27 23:20:27 +0000615 }
616
Dehao Chen9bd60422017-10-06 17:04:55 +0000617 insertCallRetPHI(Inst, CastInst, DirectCallee);
Rong Xu6e34c492016-04-27 23:20:27 +0000618
619 DEBUG(dbgs() << "\n== Basic Blocks After ==\n");
620 DEBUG(dbgs() << *BB << *DirectCallBB << *IndirectCallBB << *MergeBB << "\n");
621
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000622 using namespace ore;
Eugene Zelenkofce43572017-10-21 00:57:46 +0000623
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000624 if (ORE)
Vivek Pandya95906582017-10-11 17:12:59 +0000625 ORE->emit([&]() {
626 return OptimizationRemark(DEBUG_TYPE, "Promoted", Inst)
627 << "Promote indirect call to " << NV("DirectCallee", DirectCallee)
628 << " with count " << NV("Count", Count) << " out of "
629 << NV("TotalCount", TotalCount);
630 });
Dehao Chen14bf0292017-01-23 23:18:24 +0000631 return NewInst;
Rong Xu6e34c492016-04-27 23:20:27 +0000632}
633
634// Promote indirect-call to conditional direct-call for one callsite.
635uint32_t ICallPromotionFunc::tryToPromote(
636 Instruction *Inst, const std::vector<PromotionCandidate> &Candidates,
637 uint64_t &TotalCount) {
638 uint32_t NumPromoted = 0;
639
640 for (auto &C : Candidates) {
641 uint64_t Count = C.Count;
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000642 promoteIndirectCall(Inst, C.TargetFunction, Count, TotalCount, SamplePGO,
643 &ORE);
Rong Xu6e34c492016-04-27 23:20:27 +0000644 assert(TotalCount >= Count);
645 TotalCount -= Count;
646 NumOfPGOICallPromotion++;
647 NumPromoted++;
648 }
649 return NumPromoted;
650}
651
652// Traverse all the indirect-call callsite and get the value profile
653// annotation to perform indirect-call promotion.
Dehao Chen34cfcb22017-08-08 20:57:33 +0000654bool ICallPromotionFunc::processFunction(ProfileSummaryInfo *PSI) {
Rong Xu6e34c492016-04-27 23:20:27 +0000655 bool Changed = false;
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000656 ICallPromotionAnalysis ICallAnalysis;
Rong Xu6e34c492016-04-27 23:20:27 +0000657 for (auto &I : findIndirectCallSites(F)) {
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000658 uint32_t NumVals, NumCandidates;
Rong Xu6e34c492016-04-27 23:20:27 +0000659 uint64_t TotalCount;
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000660 auto ICallProfDataRef = ICallAnalysis.getPromotionCandidatesForInstruction(
661 I, NumVals, TotalCount, NumCandidates);
Dehao Chen34cfcb22017-08-08 20:57:33 +0000662 if (!NumCandidates ||
663 (PSI && PSI->hasProfileSummary() && !PSI->isHotCount(TotalCount)))
Rong Xu6e34c492016-04-27 23:20:27 +0000664 continue;
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000665 auto PromotionCandidates = getPromotionCandidatesForCallSite(
666 I, ICallProfDataRef, TotalCount, NumCandidates);
Rong Xu6e34c492016-04-27 23:20:27 +0000667 uint32_t NumPromoted = tryToPromote(I, PromotionCandidates, TotalCount);
668 if (NumPromoted == 0)
669 continue;
670
671 Changed = true;
672 // Adjust the MD.prof metadata. First delete the old one.
Eugene Zelenkocdc71612016-08-11 17:20:18 +0000673 I->setMetadata(LLVMContext::MD_prof, nullptr);
Rong Xu6e34c492016-04-27 23:20:27 +0000674 // If all promoted, we don't need the MD.prof metadata.
675 if (TotalCount == 0 || NumPromoted == NumVals)
676 continue;
677 // Otherwise we need update with the un-promoted records back.
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000678 annotateValueSite(*M, *I, ICallProfDataRef.slice(NumPromoted), TotalCount,
679 IPVK_IndirectCallTarget, NumCandidates);
Rong Xu6e34c492016-04-27 23:20:27 +0000680 }
681 return Changed;
682}
683
684// A wrapper function that does the actual work.
Dehao Chen34cfcb22017-08-08 20:57:33 +0000685static bool promoteIndirectCalls(Module &M, ProfileSummaryInfo *PSI,
686 bool InLTO, bool SamplePGO,
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000687 ModuleAnalysisManager *AM = nullptr) {
Rong Xu6e34c492016-04-27 23:20:27 +0000688 if (DisableICP)
689 return false;
690 InstrProfSymtab Symtab;
Vedant Kumarb5794ca2017-06-20 01:38:56 +0000691 if (Error E = Symtab.create(M, InLTO)) {
692 std::string SymtabFailure = toString(std::move(E));
693 DEBUG(dbgs() << "Failed to create symtab: " << SymtabFailure << "\n");
694 (void)SymtabFailure;
695 return false;
696 }
Rong Xu6e34c492016-04-27 23:20:27 +0000697 bool Changed = false;
698 for (auto &F : M) {
699 if (F.isDeclaration())
700 continue;
701 if (F.hasFnAttribute(Attribute::OptimizeNone))
702 continue;
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000703
704 std::unique_ptr<OptimizationRemarkEmitter> OwnedORE;
705 OptimizationRemarkEmitter *ORE;
706 if (AM) {
707 auto &FAM =
708 AM->getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
709 ORE = &FAM.getResult<OptimizationRemarkEmitterAnalysis>(F);
710 } else {
Eugene Zelenkofce43572017-10-21 00:57:46 +0000711 OwnedORE = llvm::make_unique<OptimizationRemarkEmitter>(&F);
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000712 ORE = OwnedORE.get();
713 }
714
715 ICallPromotionFunc ICallPromotion(F, &M, &Symtab, SamplePGO, *ORE);
Dehao Chen34cfcb22017-08-08 20:57:33 +0000716 bool FuncChanged = ICallPromotion.processFunction(PSI);
Rong Xu6e34c492016-04-27 23:20:27 +0000717 if (ICPDUMPAFTER && FuncChanged) {
718 DEBUG(dbgs() << "\n== IR Dump After =="; F.print(dbgs()));
719 DEBUG(dbgs() << "\n");
720 }
721 Changed |= FuncChanged;
722 if (ICPCutOff != 0 && NumOfPGOICallPromotion >= ICPCutOff) {
723 DEBUG(dbgs() << " Stop: Cutoff reached.\n");
724 break;
725 }
726 }
727 return Changed;
728}
729
Xinliang David Li72616182016-05-15 01:04:24 +0000730bool PGOIndirectCallPromotionLegacyPass::runOnModule(Module &M) {
Dehao Chen34cfcb22017-08-08 20:57:33 +0000731 ProfileSummaryInfo *PSI =
732 getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
733
Rong Xu6e34c492016-04-27 23:20:27 +0000734 // Command-line option has the priority for InLTO.
Dehao Chen34cfcb22017-08-08 20:57:33 +0000735 return promoteIndirectCalls(M, PSI, InLTO | ICPLTOMode,
Dehao Chencc75d242017-02-23 22:15:18 +0000736 SamplePGO | ICPSamplePGOMode);
Rong Xu6e34c492016-04-27 23:20:27 +0000737}
Xinliang David Lif3c7a352016-05-16 16:31:07 +0000738
Dehao Chencc75d242017-02-23 22:15:18 +0000739PreservedAnalyses PGOIndirectCallPromotion::run(Module &M,
740 ModuleAnalysisManager &AM) {
Dehao Chen34cfcb22017-08-08 20:57:33 +0000741 ProfileSummaryInfo *PSI = &AM.getResult<ProfileSummaryAnalysis>(M);
742
743 if (!promoteIndirectCalls(M, PSI, InLTO | ICPLTOMode,
744 SamplePGO | ICPSamplePGOMode, &AM))
Xinliang David Lif3c7a352016-05-16 16:31:07 +0000745 return PreservedAnalyses::all();
746
747 return PreservedAnalyses::none();
748}