blob: dcda44d1bd5009453860436e89a00a29a53caa4f [file] [log] [blame]
Rong Xu48596b62017-04-04 16:42:20 +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"
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"
Rong Xu48596b62017-04-04 16:42:20 +000020#include "llvm/Analysis/BlockFrequencyInfo.h"
Rong Xu2bf4c592017-04-06 20:56:00 +000021#include "llvm/Analysis/GlobalsModRef.h"
Teresa Johnson1e44b5d2016-07-12 21:13:44 +000022#include "llvm/Analysis/IndirectCallPromotionAnalysis.h"
23#include "llvm/Analysis/IndirectCallSiteVisitor.h"
Adam Nemet0965da22017-10-09 23:19:02 +000024#include "llvm/Analysis/OptimizationRemarkEmitter.h"
Dehao Chen34cfcb22017-08-08 20:57:33 +000025#include "llvm/Analysis/ProfileSummaryInfo.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"
Rong Xu6e34c492016-04-27 23:20:27 +000039#include "llvm/Pass.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000040#include "llvm/PassRegistry.h"
41#include "llvm/PassSupport.h"
42#include "llvm/ProfileData/InstrProf.h"
43#include "llvm/Support/Casting.h"
44#include "llvm/Support/CommandLine.h"
Rong Xu6e34c492016-04-27 23:20:27 +000045#include "llvm/Support/Debug.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000046#include "llvm/Support/ErrorHandling.h"
Rong Xu48596b62017-04-04 16:42:20 +000047#include "llvm/Support/MathExtras.h"
Rong Xu6e34c492016-04-27 23:20:27 +000048#include "llvm/Transforms/Instrumentation.h"
Xinliang David Lif3c7a352016-05-16 16:31:07 +000049#include "llvm/Transforms/PGOInstrumentation.h"
Rong Xu6e34c492016-04-27 23:20:27 +000050#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000051#include <cassert>
52#include <cstdint>
Rong Xu6e34c492016-04-27 23:20:27 +000053#include <vector>
54
55using namespace llvm;
56
Xinliang David Li0b293302016-06-02 01:52:05 +000057#define DEBUG_TYPE "pgo-icall-prom"
Rong Xu6e34c492016-04-27 23:20:27 +000058
59STATISTIC(NumOfPGOICallPromotion, "Number of indirect call promotions.");
60STATISTIC(NumOfPGOICallsites, "Number of indirect call candidate sites.");
61
62// Command line option to disable indirect-call promotion with the default as
63// false. This is for debug purpose.
64static cl::opt<bool> DisableICP("disable-icp", cl::init(false), cl::Hidden,
65 cl::desc("Disable indirect call promotion"));
66
Rong Xu6e34c492016-04-27 23:20:27 +000067// Set the cutoff value for the promotion. If the value is other than 0, we
68// stop the transformation once the total number of promotions equals the cutoff
69// value.
70// For debug use only.
71static cl::opt<unsigned>
72 ICPCutOff("icp-cutoff", cl::init(0), cl::Hidden, cl::ZeroOrMore,
Craig Toppera49e7682017-05-05 22:31:11 +000073 cl::desc("Max number of promotions for this compilation"));
Rong Xu6e34c492016-04-27 23:20:27 +000074
75// If ICPCSSkip is non zero, the first ICPCSSkip callsites will be skipped.
76// For debug use only.
77static cl::opt<unsigned>
78 ICPCSSkip("icp-csskip", cl::init(0), cl::Hidden, cl::ZeroOrMore,
Craig Toppera49e7682017-05-05 22:31:11 +000079 cl::desc("Skip Callsite up to this number for this compilation"));
Rong Xu6e34c492016-04-27 23:20:27 +000080
81// Set if the pass is called in LTO optimization. The difference for LTO mode
82// is the pass won't prefix the source module name to the internal linkage
83// symbols.
84static cl::opt<bool> ICPLTOMode("icp-lto", cl::init(false), cl::Hidden,
85 cl::desc("Run indirect-call promotion in LTO "
86 "mode"));
Teresa Johnson1e44b5d2016-07-12 21:13:44 +000087
Dehao Chencc75d242017-02-23 22:15:18 +000088// Set if the pass is called in SamplePGO mode. The difference for SamplePGO
89// mode is it will add prof metadatato the created direct call.
90static cl::opt<bool>
91 ICPSamplePGOMode("icp-samplepgo", cl::init(false), cl::Hidden,
92 cl::desc("Run indirect-call promotion in SamplePGO mode"));
93
Rong Xu6e34c492016-04-27 23:20:27 +000094// If the option is set to true, only call instructions will be considered for
95// transformation -- invoke instructions will be ignored.
96static cl::opt<bool>
97 ICPCallOnly("icp-call-only", cl::init(false), cl::Hidden,
98 cl::desc("Run indirect-call promotion for call instructions "
99 "only"));
100
101// If the option is set to true, only invoke instructions will be considered for
102// transformation -- call instructions will be ignored.
103static cl::opt<bool> ICPInvokeOnly("icp-invoke-only", cl::init(false),
104 cl::Hidden,
105 cl::desc("Run indirect-call promotion for "
106 "invoke instruction only"));
107
108// Dump the function level IR if the transformation happened in this
109// function. For debug use only.
110static cl::opt<bool>
111 ICPDUMPAFTER("icp-dumpafter", cl::init(false), cl::Hidden,
112 cl::desc("Dump IR after transformation happens"));
113
114namespace {
Xinliang David Li72616182016-05-15 01:04:24 +0000115class PGOIndirectCallPromotionLegacyPass : public ModulePass {
Rong Xu6e34c492016-04-27 23:20:27 +0000116public:
117 static char ID;
118
Dehao Chencc75d242017-02-23 22:15:18 +0000119 PGOIndirectCallPromotionLegacyPass(bool InLTO = false, bool SamplePGO = false)
120 : ModulePass(ID), InLTO(InLTO), SamplePGO(SamplePGO) {
Xinliang David Li72616182016-05-15 01:04:24 +0000121 initializePGOIndirectCallPromotionLegacyPassPass(
122 *PassRegistry::getPassRegistry());
Rong Xu6e34c492016-04-27 23:20:27 +0000123 }
124
Dehao Chen34cfcb22017-08-08 20:57:33 +0000125 void getAnalysisUsage(AnalysisUsage &AU) const override {
126 AU.addRequired<ProfileSummaryInfoWrapperPass>();
127 }
128
Mehdi Amini117296c2016-10-01 02:56:57 +0000129 StringRef getPassName() const override { return "PGOIndirectCallPromotion"; }
Rong Xu6e34c492016-04-27 23:20:27 +0000130
131private:
132 bool runOnModule(Module &M) override;
133
134 // If this pass is called in LTO. We need to special handling the PGOFuncName
135 // for the static variables due to LTO's internalization.
136 bool InLTO;
Dehao Chencc75d242017-02-23 22:15:18 +0000137
138 // If this pass is called in SamplePGO. We need to add the prof metadata to
139 // the promoted direct call.
140 bool SamplePGO;
Rong Xu6e34c492016-04-27 23:20:27 +0000141};
142} // end anonymous namespace
143
Xinliang David Li72616182016-05-15 01:04:24 +0000144char PGOIndirectCallPromotionLegacyPass::ID = 0;
Dehao Chen45847d32017-08-14 23:25:21 +0000145INITIALIZE_PASS_BEGIN(PGOIndirectCallPromotionLegacyPass, "pgo-icall-prom",
146 "Use PGO instrumentation profile to promote indirect "
147 "calls to direct calls.",
148 false, false)
149INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
150INITIALIZE_PASS_END(PGOIndirectCallPromotionLegacyPass, "pgo-icall-prom",
151 "Use PGO instrumentation profile to promote indirect "
152 "calls to direct calls.",
153 false, false)
Rong Xu6e34c492016-04-27 23:20:27 +0000154
Dehao Chencc75d242017-02-23 22:15:18 +0000155ModulePass *llvm::createPGOIndirectCallPromotionLegacyPass(bool InLTO,
156 bool SamplePGO) {
157 return new PGOIndirectCallPromotionLegacyPass(InLTO, SamplePGO);
Rong Xu6e34c492016-04-27 23:20:27 +0000158}
159
Benjamin Kramera65b6102016-05-15 15:18:11 +0000160namespace {
Rong Xu6e34c492016-04-27 23:20:27 +0000161// The class for main data structure to promote indirect calls to conditional
162// direct calls.
163class ICallPromotionFunc {
164private:
165 Function &F;
166 Module *M;
167
168 // Symtab that maps indirect call profile values to function names and
169 // defines.
170 InstrProfSymtab *Symtab;
171
Dehao Chencc75d242017-02-23 22:15:18 +0000172 bool SamplePGO;
173
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000174 OptimizationRemarkEmitter &ORE;
Rong Xu6e34c492016-04-27 23:20:27 +0000175
176 // A struct that records the direct target and it's call count.
177 struct PromotionCandidate {
178 Function *TargetFunction;
179 uint64_t Count;
180 PromotionCandidate(Function *F, uint64_t C) : TargetFunction(F), Count(C) {}
181 };
182
183 // Check if the indirect-call call site should be promoted. Return the number
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000184 // of promotions. Inst is the candidate indirect call, ValueDataRef
185 // contains the array of value profile data for profiled targets,
186 // TotalCount is the total profiled count of call executions, and
187 // NumCandidates is the number of candidate entries in ValueDataRef.
Rong Xu6e34c492016-04-27 23:20:27 +0000188 std::vector<PromotionCandidate> getPromotionCandidatesForCallSite(
189 Instruction *Inst, const ArrayRef<InstrProfValueData> &ValueDataRef,
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000190 uint64_t TotalCount, uint32_t NumCandidates);
Rong Xu6e34c492016-04-27 23:20:27 +0000191
Rong Xu6e34c492016-04-27 23:20:27 +0000192 // Promote a list of targets for one indirect-call callsite. Return
193 // the number of promotions.
194 uint32_t tryToPromote(Instruction *Inst,
195 const std::vector<PromotionCandidate> &Candidates,
196 uint64_t &TotalCount);
197
Rong Xu6e34c492016-04-27 23:20:27 +0000198 // Noncopyable
199 ICallPromotionFunc(const ICallPromotionFunc &other) = delete;
200 ICallPromotionFunc &operator=(const ICallPromotionFunc &other) = delete;
201
202public:
Dehao Chencc75d242017-02-23 22:15:18 +0000203 ICallPromotionFunc(Function &Func, Module *Modu, InstrProfSymtab *Symtab,
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000204 bool SamplePGO, OptimizationRemarkEmitter &ORE)
205 : F(Func), M(Modu), Symtab(Symtab), SamplePGO(SamplePGO), ORE(ORE) {}
Eugene Zelenkocdc71612016-08-11 17:20:18 +0000206
Dehao Chen34cfcb22017-08-08 20:57:33 +0000207 bool processFunction(ProfileSummaryInfo *PSI);
Rong Xu6e34c492016-04-27 23:20:27 +0000208};
Benjamin Kramera65b6102016-05-15 15:18:11 +0000209} // end anonymous namespace
Rong Xu6e34c492016-04-27 23:20:27 +0000210
Dehao Chen6775f5d2017-01-30 22:46:37 +0000211bool llvm::isLegalToPromote(Instruction *Inst, Function *F,
212 const char **Reason) {
Rong Xu6e34c492016-04-27 23:20:27 +0000213 // Check the return type.
214 Type *CallRetType = Inst->getType();
215 if (!CallRetType->isVoidTy()) {
Dehao Chen6775f5d2017-01-30 22:46:37 +0000216 Type *FuncRetType = F->getReturnType();
Rong Xu6e34c492016-04-27 23:20:27 +0000217 if (FuncRetType != CallRetType &&
Dehao Chen6775f5d2017-01-30 22:46:37 +0000218 !CastInst::isBitCastable(FuncRetType, CallRetType)) {
219 if (Reason)
220 *Reason = "Return type mismatch";
221 return false;
222 }
Rong Xu6e34c492016-04-27 23:20:27 +0000223 }
224
225 // Check if the arguments are compatible with the parameters
Dehao Chen6775f5d2017-01-30 22:46:37 +0000226 FunctionType *DirectCalleeType = F->getFunctionType();
Rong Xu6e34c492016-04-27 23:20:27 +0000227 unsigned ParamNum = DirectCalleeType->getFunctionNumParams();
228 CallSite CS(Inst);
229 unsigned ArgNum = CS.arg_size();
230
Dehao Chen6775f5d2017-01-30 22:46:37 +0000231 if (ParamNum != ArgNum && !DirectCalleeType->isVarArg()) {
232 if (Reason)
233 *Reason = "The number of arguments mismatch";
234 return false;
235 }
Rong Xu6e34c492016-04-27 23:20:27 +0000236
237 for (unsigned I = 0; I < ParamNum; ++I) {
238 Type *PTy = DirectCalleeType->getFunctionParamType(I);
239 Type *ATy = CS.getArgument(I)->getType();
240 if (PTy == ATy)
241 continue;
Dehao Chen6775f5d2017-01-30 22:46:37 +0000242 if (!CastInst::castIsValid(Instruction::BitCast, CS.getArgument(I), PTy)) {
243 if (Reason)
Benjamin Kramer365c9bd2017-01-30 23:11:29 +0000244 *Reason = "Argument type mismatch";
Dehao Chen6775f5d2017-01-30 22:46:37 +0000245 return false;
246 }
Rong Xu6e34c492016-04-27 23:20:27 +0000247 }
248
249 DEBUG(dbgs() << " #" << NumOfPGOICallPromotion << " Promote the icall to "
Dehao Chen6775f5d2017-01-30 22:46:37 +0000250 << F->getName() << "\n");
251 return true;
252}
253
Rong Xu6e34c492016-04-27 23:20:27 +0000254// Indirect-call promotion heuristic. The direct targets are sorted based on
255// the count. Stop at the first target that is not promoted.
256std::vector<ICallPromotionFunc::PromotionCandidate>
257ICallPromotionFunc::getPromotionCandidatesForCallSite(
258 Instruction *Inst, const ArrayRef<InstrProfValueData> &ValueDataRef,
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000259 uint64_t TotalCount, uint32_t NumCandidates) {
Rong Xu6e34c492016-04-27 23:20:27 +0000260 std::vector<PromotionCandidate> Ret;
261
262 DEBUG(dbgs() << " \nWork on callsite #" << NumOfPGOICallsites << *Inst
Teresa Johnson8950ad12016-07-12 21:29:05 +0000263 << " Num_targets: " << ValueDataRef.size()
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000264 << " Num_candidates: " << NumCandidates << "\n");
Rong Xu6e34c492016-04-27 23:20:27 +0000265 NumOfPGOICallsites++;
266 if (ICPCSSkip != 0 && NumOfPGOICallsites <= ICPCSSkip) {
267 DEBUG(dbgs() << " Skip: User options.\n");
268 return Ret;
269 }
270
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000271 for (uint32_t I = 0; I < NumCandidates; I++) {
Rong Xu6e34c492016-04-27 23:20:27 +0000272 uint64_t Count = ValueDataRef[I].Count;
273 assert(Count <= TotalCount);
274 uint64_t Target = ValueDataRef[I].Value;
275 DEBUG(dbgs() << " Candidate " << I << " Count=" << Count
276 << " Target_func: " << Target << "\n");
277
Teresa Johnsonce7de9b2016-07-17 14:46:58 +0000278 if (ICPInvokeOnly && dyn_cast<CallInst>(Inst)) {
279 DEBUG(dbgs() << " Not promote: User options.\n");
Vivek Pandya95906582017-10-11 17:12:59 +0000280 ORE.emit([&]() {
281 return OptimizationRemarkMissed(DEBUG_TYPE, "UserOptions", Inst)
282 << " Not promote: User options";
283 });
Teresa Johnsonce7de9b2016-07-17 14:46:58 +0000284 break;
285 }
286 if (ICPCallOnly && dyn_cast<InvokeInst>(Inst)) {
287 DEBUG(dbgs() << " Not promote: User option.\n");
Vivek Pandya95906582017-10-11 17:12:59 +0000288 ORE.emit([&]() {
289 return OptimizationRemarkMissed(DEBUG_TYPE, "UserOptions", Inst)
290 << " Not promote: User options";
291 });
Teresa Johnsonce7de9b2016-07-17 14:46:58 +0000292 break;
293 }
Rong Xu6e34c492016-04-27 23:20:27 +0000294 if (ICPCutOff != 0 && NumOfPGOICallPromotion >= ICPCutOff) {
295 DEBUG(dbgs() << " Not promote: Cutoff reached.\n");
Vivek Pandya95906582017-10-11 17:12:59 +0000296 ORE.emit([&]() {
297 return OptimizationRemarkMissed(DEBUG_TYPE, "CutOffReached", Inst)
298 << " Not promote: Cutoff reached";
299 });
Rong Xu6e34c492016-04-27 23:20:27 +0000300 break;
301 }
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000302
303 Function *TargetFunction = Symtab->getFunction(Target);
304 if (TargetFunction == nullptr) {
305 DEBUG(dbgs() << " Not promote: Cannot find the target\n");
Vivek Pandya95906582017-10-11 17:12:59 +0000306 ORE.emit([&]() {
307 return OptimizationRemarkMissed(DEBUG_TYPE, "UnableToFindTarget", Inst)
308 << "Cannot promote indirect call: target not found";
309 });
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000310 break;
311 }
312
Dehao Chen6775f5d2017-01-30 22:46:37 +0000313 const char *Reason = nullptr;
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000314 if (!isLegalToPromote(Inst, TargetFunction, &Reason)) {
315 using namespace ore;
Vivek Pandya95906582017-10-11 17:12:59 +0000316 ORE.emit([&]() {
317 return OptimizationRemarkMissed(DEBUG_TYPE, "UnableToPromote", Inst)
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000318 << "Cannot promote indirect call to "
319 << NV("TargetFunction", TargetFunction) << " with count of "
Vivek Pandya95906582017-10-11 17:12:59 +0000320 << NV("Count", Count) << ": " << Reason;
321 });
Rong Xu6e34c492016-04-27 23:20:27 +0000322 break;
323 }
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000324
Rong Xu6e34c492016-04-27 23:20:27 +0000325 Ret.push_back(PromotionCandidate(TargetFunction, Count));
326 TotalCount -= Count;
327 }
328 return Ret;
329}
330
331// Create a diamond structure for If_Then_Else. Also update the profile
332// count. Do the fix-up for the invoke instruction.
333static void createIfThenElse(Instruction *Inst, Function *DirectCallee,
334 uint64_t Count, uint64_t TotalCount,
335 BasicBlock **DirectCallBB,
336 BasicBlock **IndirectCallBB,
337 BasicBlock **MergeBB) {
338 CallSite CS(Inst);
339 Value *OrigCallee = CS.getCalledValue();
340
341 IRBuilder<> BBBuilder(Inst);
342 LLVMContext &Ctx = Inst->getContext();
343 Value *BCI1 =
344 BBBuilder.CreateBitCast(OrigCallee, Type::getInt8PtrTy(Ctx), "");
345 Value *BCI2 =
346 BBBuilder.CreateBitCast(DirectCallee, Type::getInt8PtrTy(Ctx), "");
347 Value *PtrCmp = BBBuilder.CreateICmpEQ(BCI1, BCI2, "");
348
349 uint64_t ElseCount = TotalCount - Count;
350 uint64_t MaxCount = (Count >= ElseCount ? Count : ElseCount);
351 uint64_t Scale = calculateCountScale(MaxCount);
352 MDBuilder MDB(Inst->getContext());
353 MDNode *BranchWeights = MDB.createBranchWeights(
354 scaleBranchCount(Count, Scale), scaleBranchCount(ElseCount, Scale));
355 TerminatorInst *ThenTerm, *ElseTerm;
356 SplitBlockAndInsertIfThenElse(PtrCmp, Inst, &ThenTerm, &ElseTerm,
357 BranchWeights);
358 *DirectCallBB = ThenTerm->getParent();
359 (*DirectCallBB)->setName("if.true.direct_targ");
360 *IndirectCallBB = ElseTerm->getParent();
361 (*IndirectCallBB)->setName("if.false.orig_indirect");
362 *MergeBB = Inst->getParent();
363 (*MergeBB)->setName("if.end.icp");
364
365 // Special handing of Invoke instructions.
366 InvokeInst *II = dyn_cast<InvokeInst>(Inst);
367 if (!II)
368 return;
369
370 // We don't need branch instructions for invoke.
371 ThenTerm->eraseFromParent();
372 ElseTerm->eraseFromParent();
373
374 // Add jump from Merge BB to the NormalDest. This is needed for the newly
375 // created direct invoke stmt -- as its NormalDst will be fixed up to MergeBB.
376 BranchInst::Create(II->getNormalDest(), *MergeBB);
377}
378
379// Find the PHI in BB that have the CallResult as the operand.
380static bool getCallRetPHINode(BasicBlock *BB, Instruction *Inst) {
381 BasicBlock *From = Inst->getParent();
382 for (auto &I : *BB) {
383 PHINode *PHI = dyn_cast<PHINode>(&I);
384 if (!PHI)
385 continue;
386 int IX = PHI->getBasicBlockIndex(From);
387 if (IX == -1)
388 continue;
389 Value *V = PHI->getIncomingValue(IX);
390 if (dyn_cast<Instruction>(V) == Inst)
391 return true;
392 }
393 return false;
394}
395
396// This method fixes up PHI nodes in BB where BB is the UnwindDest of an
397// invoke instruction. In BB, there may be PHIs with incoming block being
398// OrigBB (the MergeBB after if-then-else splitting). After moving the invoke
399// instructions to its own BB, OrigBB is no longer the predecessor block of BB.
400// Instead two new predecessors are added: IndirectCallBB and DirectCallBB,
401// so the PHI node's incoming BBs need to be fixed up accordingly.
402static void fixupPHINodeForUnwind(Instruction *Inst, BasicBlock *BB,
403 BasicBlock *OrigBB,
404 BasicBlock *IndirectCallBB,
405 BasicBlock *DirectCallBB) {
406 for (auto &I : *BB) {
407 PHINode *PHI = dyn_cast<PHINode>(&I);
408 if (!PHI)
409 continue;
410 int IX = PHI->getBasicBlockIndex(OrigBB);
411 if (IX == -1)
412 continue;
413 Value *V = PHI->getIncomingValue(IX);
414 PHI->addIncoming(V, IndirectCallBB);
415 PHI->setIncomingBlock(IX, DirectCallBB);
416 }
417}
418
419// This method fixes up PHI nodes in BB where BB is the NormalDest of an
420// invoke instruction. In BB, there may be PHIs with incoming block being
421// OrigBB (the MergeBB after if-then-else splitting). After moving the invoke
422// instructions to its own BB, a new incoming edge will be added to the original
423// NormalDstBB from the IndirectCallBB.
424static void fixupPHINodeForNormalDest(Instruction *Inst, BasicBlock *BB,
425 BasicBlock *OrigBB,
426 BasicBlock *IndirectCallBB,
427 Instruction *NewInst) {
428 for (auto &I : *BB) {
429 PHINode *PHI = dyn_cast<PHINode>(&I);
430 if (!PHI)
431 continue;
432 int IX = PHI->getBasicBlockIndex(OrigBB);
433 if (IX == -1)
434 continue;
435 Value *V = PHI->getIncomingValue(IX);
436 if (dyn_cast<Instruction>(V) == Inst) {
437 PHI->setIncomingBlock(IX, IndirectCallBB);
438 PHI->addIncoming(NewInst, OrigBB);
439 continue;
440 }
441 PHI->addIncoming(V, IndirectCallBB);
442 }
443}
444
445// Add a bitcast instruction to the direct-call return value if needed.
Rong Xu6e34c492016-04-27 23:20:27 +0000446static Instruction *insertCallRetCast(const Instruction *Inst,
447 Instruction *DirectCallInst,
448 Function *DirectCallee) {
449 if (Inst->getType()->isVoidTy())
450 return DirectCallInst;
451
452 Type *CallRetType = Inst->getType();
453 Type *FuncRetType = DirectCallee->getReturnType();
454 if (FuncRetType == CallRetType)
455 return DirectCallInst;
456
457 BasicBlock *InsertionBB;
458 if (CallInst *CI = dyn_cast<CallInst>(DirectCallInst))
459 InsertionBB = CI->getParent();
460 else
461 InsertionBB = (dyn_cast<InvokeInst>(DirectCallInst))->getNormalDest();
462
463 return (new BitCastInst(DirectCallInst, CallRetType, "",
464 InsertionBB->getTerminator()));
465}
466
467// Create a DirectCall instruction in the DirectCallBB.
468// Parameter Inst is the indirect-call (invoke) instruction.
469// DirectCallee is the decl of the direct-call (invoke) target.
470// DirecallBB is the BB that the direct-call (invoke) instruction is inserted.
471// MergeBB is the bottom BB of the if-then-else-diamond after the
472// transformation. For invoke instruction, the edges from DirectCallBB and
473// IndirectCallBB to MergeBB are removed before this call (during
Dehao Chen9bd60422017-10-06 17:04:55 +0000474// createIfThenElse). Stores the pointer to the Instruction that cast
475// the direct call in \p CastInst.
Rong Xu6e34c492016-04-27 23:20:27 +0000476static Instruction *createDirectCallInst(const Instruction *Inst,
477 Function *DirectCallee,
478 BasicBlock *DirectCallBB,
Dehao Chen9bd60422017-10-06 17:04:55 +0000479 BasicBlock *MergeBB,
480 Instruction *&CastInst) {
Rong Xu6e34c492016-04-27 23:20:27 +0000481 Instruction *NewInst = Inst->clone();
482 if (CallInst *CI = dyn_cast<CallInst>(NewInst)) {
483 CI->setCalledFunction(DirectCallee);
484 CI->mutateFunctionType(DirectCallee->getFunctionType());
485 } else {
486 // Must be an invoke instruction. Direct invoke's normal destination is
487 // fixed up to MergeBB. MergeBB is the place where return cast is inserted.
488 // Also since IndirectCallBB does not have an edge to MergeBB, there is no
489 // need to insert new PHIs into MergeBB.
490 InvokeInst *II = dyn_cast<InvokeInst>(NewInst);
491 assert(II);
492 II->setCalledFunction(DirectCallee);
493 II->mutateFunctionType(DirectCallee->getFunctionType());
494 II->setNormalDest(MergeBB);
495 }
496
497 DirectCallBB->getInstList().insert(DirectCallBB->getFirstInsertionPt(),
498 NewInst);
499
500 // Clear the value profile data.
Eugene Zelenkocdc71612016-08-11 17:20:18 +0000501 NewInst->setMetadata(LLVMContext::MD_prof, nullptr);
Rong Xu6e34c492016-04-27 23:20:27 +0000502 CallSite NewCS(NewInst);
503 FunctionType *DirectCalleeType = DirectCallee->getFunctionType();
504 unsigned ParamNum = DirectCalleeType->getFunctionNumParams();
505 for (unsigned I = 0; I < ParamNum; ++I) {
506 Type *ATy = NewCS.getArgument(I)->getType();
507 Type *PTy = DirectCalleeType->getParamType(I);
508 if (ATy != PTy) {
509 BitCastInst *BI = new BitCastInst(NewCS.getArgument(I), PTy, "", NewInst);
510 NewCS.setArgument(I, BI);
511 }
512 }
513
Dehao Chen9bd60422017-10-06 17:04:55 +0000514 CastInst = insertCallRetCast(Inst, NewInst, DirectCallee);
515 return NewInst;
Rong Xu6e34c492016-04-27 23:20:27 +0000516}
517
518// Create a PHI to unify the return values of calls.
519static void insertCallRetPHI(Instruction *Inst, Instruction *CallResult,
520 Function *DirectCallee) {
521 if (Inst->getType()->isVoidTy())
522 return;
523
Taewook Oh572f45a2017-08-28 18:57:00 +0000524 if (Inst->use_empty())
525 return;
526
Rong Xu6e34c492016-04-27 23:20:27 +0000527 BasicBlock *RetValBB = CallResult->getParent();
528
529 BasicBlock *PHIBB;
530 if (InvokeInst *II = dyn_cast<InvokeInst>(CallResult))
531 RetValBB = II->getNormalDest();
532
533 PHIBB = RetValBB->getSingleSuccessor();
534 if (getCallRetPHINode(PHIBB, Inst))
535 return;
536
537 PHINode *CallRetPHI = PHINode::Create(Inst->getType(), 0);
538 PHIBB->getInstList().push_front(CallRetPHI);
539 Inst->replaceAllUsesWith(CallRetPHI);
540 CallRetPHI->addIncoming(Inst, Inst->getParent());
541 CallRetPHI->addIncoming(CallResult, RetValBB);
542}
543
544// This function does the actual indirect-call promotion transformation:
545// For an indirect-call like:
546// Ret = (*Foo)(Args);
547// It transforms to:
548// if (Foo == DirectCallee)
549// Ret1 = DirectCallee(Args);
550// else
551// Ret2 = (*Foo)(Args);
552// Ret = phi(Ret1, Ret2);
553// It adds type casts for the args do not match the parameters and the return
554// value. Branch weights metadata also updated.
Dehao Chencc75d242017-02-23 22:15:18 +0000555// If \p AttachProfToDirectCall is true, a prof metadata is attached to the
556// new direct call to contain \p Count. This is used by SamplePGO inliner to
557// check callsite hotness.
Dehao Chen14bf0292017-01-23 23:18:24 +0000558// Returns the promoted direct call instruction.
559Instruction *llvm::promoteIndirectCall(Instruction *Inst,
560 Function *DirectCallee, uint64_t Count,
Dehao Chencc75d242017-02-23 22:15:18 +0000561 uint64_t TotalCount,
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000562 bool AttachProfToDirectCall,
563 OptimizationRemarkEmitter *ORE) {
Rong Xu6e34c492016-04-27 23:20:27 +0000564 assert(DirectCallee != nullptr);
565 BasicBlock *BB = Inst->getParent();
566 // Just to suppress the non-debug build warning.
567 (void)BB;
568 DEBUG(dbgs() << "\n\n== Basic Block Before ==\n");
569 DEBUG(dbgs() << *BB << "\n");
570
571 BasicBlock *DirectCallBB, *IndirectCallBB, *MergeBB;
572 createIfThenElse(Inst, DirectCallee, Count, TotalCount, &DirectCallBB,
573 &IndirectCallBB, &MergeBB);
574
Dehao Chen9bd60422017-10-06 17:04:55 +0000575 // If the return type of the NewInst is not the same as the Inst, a CastInst
576 // is needed for type casting. Otherwise CastInst is the same as NewInst.
577 Instruction *CastInst = nullptr;
Rong Xu6e34c492016-04-27 23:20:27 +0000578 Instruction *NewInst =
Dehao Chen9bd60422017-10-06 17:04:55 +0000579 createDirectCallInst(Inst, DirectCallee, DirectCallBB, MergeBB, CastInst);
Rong Xu6e34c492016-04-27 23:20:27 +0000580
Dehao Chencc75d242017-02-23 22:15:18 +0000581 if (AttachProfToDirectCall) {
582 SmallVector<uint32_t, 1> Weights;
583 Weights.push_back(Count);
584 MDBuilder MDB(NewInst->getContext());
Dehao Chen9bd60422017-10-06 17:04:55 +0000585 NewInst->setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights));
Dehao Chencc75d242017-02-23 22:15:18 +0000586 }
587
Rong Xu6e34c492016-04-27 23:20:27 +0000588 // Move Inst from MergeBB to IndirectCallBB.
589 Inst->removeFromParent();
590 IndirectCallBB->getInstList().insert(IndirectCallBB->getFirstInsertionPt(),
591 Inst);
592
593 if (InvokeInst *II = dyn_cast<InvokeInst>(Inst)) {
594 // At this point, the original indirect invoke instruction has the original
595 // UnwindDest and NormalDest. For the direct invoke instruction, the
596 // NormalDest points to MergeBB, and MergeBB jumps to the original
597 // NormalDest. MergeBB might have a new bitcast instruction for the return
598 // value. The PHIs are with the original NormalDest. Since we now have two
599 // incoming edges to NormalDest and UnwindDest, we have to do some fixups.
600 //
601 // UnwindDest will not use the return value. So pass nullptr here.
602 fixupPHINodeForUnwind(Inst, II->getUnwindDest(), MergeBB, IndirectCallBB,
603 DirectCallBB);
604 // We don't need to update the operand from NormalDest for DirectCallBB.
605 // Pass nullptr here.
606 fixupPHINodeForNormalDest(Inst, II->getNormalDest(), MergeBB,
Dehao Chen9bd60422017-10-06 17:04:55 +0000607 IndirectCallBB, CastInst);
Rong Xu6e34c492016-04-27 23:20:27 +0000608 }
609
Dehao Chen9bd60422017-10-06 17:04:55 +0000610 insertCallRetPHI(Inst, CastInst, DirectCallee);
Rong Xu6e34c492016-04-27 23:20:27 +0000611
612 DEBUG(dbgs() << "\n== Basic Blocks After ==\n");
613 DEBUG(dbgs() << *BB << *DirectCallBB << *IndirectCallBB << *MergeBB << "\n");
614
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000615 using namespace ore;
616 if (ORE)
Vivek Pandya95906582017-10-11 17:12:59 +0000617 ORE->emit([&]() {
618 return OptimizationRemark(DEBUG_TYPE, "Promoted", Inst)
619 << "Promote indirect call to " << NV("DirectCallee", DirectCallee)
620 << " with count " << NV("Count", Count) << " out of "
621 << NV("TotalCount", TotalCount);
622 });
Dehao Chen14bf0292017-01-23 23:18:24 +0000623 return NewInst;
Rong Xu6e34c492016-04-27 23:20:27 +0000624}
625
626// Promote indirect-call to conditional direct-call for one callsite.
627uint32_t ICallPromotionFunc::tryToPromote(
628 Instruction *Inst, const std::vector<PromotionCandidate> &Candidates,
629 uint64_t &TotalCount) {
630 uint32_t NumPromoted = 0;
631
632 for (auto &C : Candidates) {
633 uint64_t Count = C.Count;
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000634 promoteIndirectCall(Inst, C.TargetFunction, Count, TotalCount, SamplePGO,
635 &ORE);
Rong Xu6e34c492016-04-27 23:20:27 +0000636 assert(TotalCount >= Count);
637 TotalCount -= Count;
638 NumOfPGOICallPromotion++;
639 NumPromoted++;
640 }
641 return NumPromoted;
642}
643
644// Traverse all the indirect-call callsite and get the value profile
645// annotation to perform indirect-call promotion.
Dehao Chen34cfcb22017-08-08 20:57:33 +0000646bool ICallPromotionFunc::processFunction(ProfileSummaryInfo *PSI) {
Rong Xu6e34c492016-04-27 23:20:27 +0000647 bool Changed = false;
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000648 ICallPromotionAnalysis ICallAnalysis;
Rong Xu6e34c492016-04-27 23:20:27 +0000649 for (auto &I : findIndirectCallSites(F)) {
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000650 uint32_t NumVals, NumCandidates;
Rong Xu6e34c492016-04-27 23:20:27 +0000651 uint64_t TotalCount;
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000652 auto ICallProfDataRef = ICallAnalysis.getPromotionCandidatesForInstruction(
653 I, NumVals, TotalCount, NumCandidates);
Dehao Chen34cfcb22017-08-08 20:57:33 +0000654 if (!NumCandidates ||
655 (PSI && PSI->hasProfileSummary() && !PSI->isHotCount(TotalCount)))
Rong Xu6e34c492016-04-27 23:20:27 +0000656 continue;
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000657 auto PromotionCandidates = getPromotionCandidatesForCallSite(
658 I, ICallProfDataRef, TotalCount, NumCandidates);
Rong Xu6e34c492016-04-27 23:20:27 +0000659 uint32_t NumPromoted = tryToPromote(I, PromotionCandidates, TotalCount);
660 if (NumPromoted == 0)
661 continue;
662
663 Changed = true;
664 // Adjust the MD.prof metadata. First delete the old one.
Eugene Zelenkocdc71612016-08-11 17:20:18 +0000665 I->setMetadata(LLVMContext::MD_prof, nullptr);
Rong Xu6e34c492016-04-27 23:20:27 +0000666 // If all promoted, we don't need the MD.prof metadata.
667 if (TotalCount == 0 || NumPromoted == NumVals)
668 continue;
669 // Otherwise we need update with the un-promoted records back.
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000670 annotateValueSite(*M, *I, ICallProfDataRef.slice(NumPromoted), TotalCount,
671 IPVK_IndirectCallTarget, NumCandidates);
Rong Xu6e34c492016-04-27 23:20:27 +0000672 }
673 return Changed;
674}
675
676// A wrapper function that does the actual work.
Dehao Chen34cfcb22017-08-08 20:57:33 +0000677static bool promoteIndirectCalls(Module &M, ProfileSummaryInfo *PSI,
678 bool InLTO, bool SamplePGO,
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000679 ModuleAnalysisManager *AM = nullptr) {
Rong Xu6e34c492016-04-27 23:20:27 +0000680 if (DisableICP)
681 return false;
682 InstrProfSymtab Symtab;
Vedant Kumarb5794ca2017-06-20 01:38:56 +0000683 if (Error E = Symtab.create(M, InLTO)) {
684 std::string SymtabFailure = toString(std::move(E));
685 DEBUG(dbgs() << "Failed to create symtab: " << SymtabFailure << "\n");
686 (void)SymtabFailure;
687 return false;
688 }
Rong Xu6e34c492016-04-27 23:20:27 +0000689 bool Changed = false;
690 for (auto &F : M) {
691 if (F.isDeclaration())
692 continue;
693 if (F.hasFnAttribute(Attribute::OptimizeNone))
694 continue;
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000695
696 std::unique_ptr<OptimizationRemarkEmitter> OwnedORE;
697 OptimizationRemarkEmitter *ORE;
698 if (AM) {
699 auto &FAM =
700 AM->getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
701 ORE = &FAM.getResult<OptimizationRemarkEmitterAnalysis>(F);
702 } else {
703 OwnedORE = make_unique<OptimizationRemarkEmitter>(&F);
704 ORE = OwnedORE.get();
705 }
706
707 ICallPromotionFunc ICallPromotion(F, &M, &Symtab, SamplePGO, *ORE);
Dehao Chen34cfcb22017-08-08 20:57:33 +0000708 bool FuncChanged = ICallPromotion.processFunction(PSI);
Rong Xu6e34c492016-04-27 23:20:27 +0000709 if (ICPDUMPAFTER && FuncChanged) {
710 DEBUG(dbgs() << "\n== IR Dump After =="; F.print(dbgs()));
711 DEBUG(dbgs() << "\n");
712 }
713 Changed |= FuncChanged;
714 if (ICPCutOff != 0 && NumOfPGOICallPromotion >= ICPCutOff) {
715 DEBUG(dbgs() << " Stop: Cutoff reached.\n");
716 break;
717 }
718 }
719 return Changed;
720}
721
Xinliang David Li72616182016-05-15 01:04:24 +0000722bool PGOIndirectCallPromotionLegacyPass::runOnModule(Module &M) {
Dehao Chen34cfcb22017-08-08 20:57:33 +0000723 ProfileSummaryInfo *PSI =
724 getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
725
Rong Xu6e34c492016-04-27 23:20:27 +0000726 // Command-line option has the priority for InLTO.
Dehao Chen34cfcb22017-08-08 20:57:33 +0000727 return promoteIndirectCalls(M, PSI, InLTO | ICPLTOMode,
Dehao Chencc75d242017-02-23 22:15:18 +0000728 SamplePGO | ICPSamplePGOMode);
Rong Xu6e34c492016-04-27 23:20:27 +0000729}
Xinliang David Lif3c7a352016-05-16 16:31:07 +0000730
Dehao Chencc75d242017-02-23 22:15:18 +0000731PreservedAnalyses PGOIndirectCallPromotion::run(Module &M,
732 ModuleAnalysisManager &AM) {
Dehao Chen34cfcb22017-08-08 20:57:33 +0000733 ProfileSummaryInfo *PSI = &AM.getResult<ProfileSummaryAnalysis>(M);
734
735 if (!promoteIndirectCalls(M, PSI, InLTO | ICPLTOMode,
736 SamplePGO | ICPSamplePGOMode, &AM))
Xinliang David Lif3c7a352016-05-16 16:31:07 +0000737 return PreservedAnalyses::all();
738
739 return PreservedAnalyses::none();
740}