blob: 47e21bde12bfee903232951de024dabfa900f40f [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 Nemet0d8b5d62017-07-27 16:54:15 +000024#include "llvm/Analysis/OptimizationDiagnosticInfo.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;
145INITIALIZE_PASS(PGOIndirectCallPromotionLegacyPass, "pgo-icall-prom",
Rong Xu6e34c492016-04-27 23:20:27 +0000146 "Use PGO instrumentation profile to promote indirect calls to "
147 "direct calls.",
148 false, false)
149
Dehao Chencc75d242017-02-23 22:15:18 +0000150ModulePass *llvm::createPGOIndirectCallPromotionLegacyPass(bool InLTO,
151 bool SamplePGO) {
152 return new PGOIndirectCallPromotionLegacyPass(InLTO, SamplePGO);
Rong Xu6e34c492016-04-27 23:20:27 +0000153}
154
Benjamin Kramera65b6102016-05-15 15:18:11 +0000155namespace {
Rong Xu6e34c492016-04-27 23:20:27 +0000156// The class for main data structure to promote indirect calls to conditional
157// direct calls.
158class ICallPromotionFunc {
159private:
160 Function &F;
161 Module *M;
162
163 // Symtab that maps indirect call profile values to function names and
164 // defines.
165 InstrProfSymtab *Symtab;
166
Dehao Chencc75d242017-02-23 22:15:18 +0000167 bool SamplePGO;
168
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000169 OptimizationRemarkEmitter &ORE;
Rong Xu6e34c492016-04-27 23:20:27 +0000170
171 // A struct that records the direct target and it's call count.
172 struct PromotionCandidate {
173 Function *TargetFunction;
174 uint64_t Count;
175 PromotionCandidate(Function *F, uint64_t C) : TargetFunction(F), Count(C) {}
176 };
177
178 // Check if the indirect-call call site should be promoted. Return the number
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000179 // of promotions. Inst is the candidate indirect call, ValueDataRef
180 // contains the array of value profile data for profiled targets,
181 // TotalCount is the total profiled count of call executions, and
182 // NumCandidates is the number of candidate entries in ValueDataRef.
Rong Xu6e34c492016-04-27 23:20:27 +0000183 std::vector<PromotionCandidate> getPromotionCandidatesForCallSite(
184 Instruction *Inst, const ArrayRef<InstrProfValueData> &ValueDataRef,
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000185 uint64_t TotalCount, uint32_t NumCandidates);
Rong Xu6e34c492016-04-27 23:20:27 +0000186
Rong Xu6e34c492016-04-27 23:20:27 +0000187 // Promote a list of targets for one indirect-call callsite. Return
188 // the number of promotions.
189 uint32_t tryToPromote(Instruction *Inst,
190 const std::vector<PromotionCandidate> &Candidates,
191 uint64_t &TotalCount);
192
Rong Xu6e34c492016-04-27 23:20:27 +0000193 // Noncopyable
194 ICallPromotionFunc(const ICallPromotionFunc &other) = delete;
195 ICallPromotionFunc &operator=(const ICallPromotionFunc &other) = delete;
196
197public:
Dehao Chencc75d242017-02-23 22:15:18 +0000198 ICallPromotionFunc(Function &Func, Module *Modu, InstrProfSymtab *Symtab,
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000199 bool SamplePGO, OptimizationRemarkEmitter &ORE)
200 : F(Func), M(Modu), Symtab(Symtab), SamplePGO(SamplePGO), ORE(ORE) {}
Eugene Zelenkocdc71612016-08-11 17:20:18 +0000201
Dehao Chen34cfcb22017-08-08 20:57:33 +0000202 bool processFunction(ProfileSummaryInfo *PSI);
Rong Xu6e34c492016-04-27 23:20:27 +0000203};
Benjamin Kramera65b6102016-05-15 15:18:11 +0000204} // end anonymous namespace
Rong Xu6e34c492016-04-27 23:20:27 +0000205
Dehao Chen6775f5d2017-01-30 22:46:37 +0000206bool llvm::isLegalToPromote(Instruction *Inst, Function *F,
207 const char **Reason) {
Rong Xu6e34c492016-04-27 23:20:27 +0000208 // Check the return type.
209 Type *CallRetType = Inst->getType();
210 if (!CallRetType->isVoidTy()) {
Dehao Chen6775f5d2017-01-30 22:46:37 +0000211 Type *FuncRetType = F->getReturnType();
Rong Xu6e34c492016-04-27 23:20:27 +0000212 if (FuncRetType != CallRetType &&
Dehao Chen6775f5d2017-01-30 22:46:37 +0000213 !CastInst::isBitCastable(FuncRetType, CallRetType)) {
214 if (Reason)
215 *Reason = "Return type mismatch";
216 return false;
217 }
Rong Xu6e34c492016-04-27 23:20:27 +0000218 }
219
220 // Check if the arguments are compatible with the parameters
Dehao Chen6775f5d2017-01-30 22:46:37 +0000221 FunctionType *DirectCalleeType = F->getFunctionType();
Rong Xu6e34c492016-04-27 23:20:27 +0000222 unsigned ParamNum = DirectCalleeType->getFunctionNumParams();
223 CallSite CS(Inst);
224 unsigned ArgNum = CS.arg_size();
225
Dehao Chen6775f5d2017-01-30 22:46:37 +0000226 if (ParamNum != ArgNum && !DirectCalleeType->isVarArg()) {
227 if (Reason)
228 *Reason = "The number of arguments mismatch";
229 return false;
230 }
Rong Xu6e34c492016-04-27 23:20:27 +0000231
232 for (unsigned I = 0; I < ParamNum; ++I) {
233 Type *PTy = DirectCalleeType->getFunctionParamType(I);
234 Type *ATy = CS.getArgument(I)->getType();
235 if (PTy == ATy)
236 continue;
Dehao Chen6775f5d2017-01-30 22:46:37 +0000237 if (!CastInst::castIsValid(Instruction::BitCast, CS.getArgument(I), PTy)) {
238 if (Reason)
Benjamin Kramer365c9bd2017-01-30 23:11:29 +0000239 *Reason = "Argument type mismatch";
Dehao Chen6775f5d2017-01-30 22:46:37 +0000240 return false;
241 }
Rong Xu6e34c492016-04-27 23:20:27 +0000242 }
243
244 DEBUG(dbgs() << " #" << NumOfPGOICallPromotion << " Promote the icall to "
Dehao Chen6775f5d2017-01-30 22:46:37 +0000245 << F->getName() << "\n");
246 return true;
247}
248
Rong Xu6e34c492016-04-27 23:20:27 +0000249// 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");
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000275 ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "UserOptions", Inst)
276 << " Not promote: User options");
Teresa Johnsonce7de9b2016-07-17 14:46:58 +0000277 break;
278 }
279 if (ICPCallOnly && dyn_cast<InvokeInst>(Inst)) {
280 DEBUG(dbgs() << " Not promote: User option.\n");
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000281 ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "UserOptions", Inst)
282 << " Not promote: User options");
Teresa Johnsonce7de9b2016-07-17 14:46:58 +0000283 break;
284 }
Rong Xu6e34c492016-04-27 23:20:27 +0000285 if (ICPCutOff != 0 && NumOfPGOICallPromotion >= ICPCutOff) {
286 DEBUG(dbgs() << " Not promote: Cutoff reached.\n");
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000287 ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "CutOffReached", Inst)
288 << " Not promote: Cutoff reached");
Rong Xu6e34c492016-04-27 23:20:27 +0000289 break;
290 }
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000291
292 Function *TargetFunction = Symtab->getFunction(Target);
293 if (TargetFunction == nullptr) {
294 DEBUG(dbgs() << " Not promote: Cannot find the target\n");
295 ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "UnableToFindTarget", Inst)
296 << "Cannot promote indirect call: target not found");
297 break;
298 }
299
Dehao Chen6775f5d2017-01-30 22:46:37 +0000300 const char *Reason = nullptr;
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000301 if (!isLegalToPromote(Inst, TargetFunction, &Reason)) {
302 using namespace ore;
303 ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "UnableToPromote", Inst)
304 << "Cannot promote indirect call to "
305 << NV("TargetFunction", TargetFunction) << " with count of "
306 << NV("Count", Count) << ": " << Reason);
Rong Xu6e34c492016-04-27 23:20:27 +0000307 break;
308 }
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000309
Rong Xu6e34c492016-04-27 23:20:27 +0000310 Ret.push_back(PromotionCandidate(TargetFunction, Count));
311 TotalCount -= Count;
312 }
313 return Ret;
314}
315
316// Create a diamond structure for If_Then_Else. Also update the profile
317// count. Do the fix-up for the invoke instruction.
318static void createIfThenElse(Instruction *Inst, Function *DirectCallee,
319 uint64_t Count, uint64_t TotalCount,
320 BasicBlock **DirectCallBB,
321 BasicBlock **IndirectCallBB,
322 BasicBlock **MergeBB) {
323 CallSite CS(Inst);
324 Value *OrigCallee = CS.getCalledValue();
325
326 IRBuilder<> BBBuilder(Inst);
327 LLVMContext &Ctx = Inst->getContext();
328 Value *BCI1 =
329 BBBuilder.CreateBitCast(OrigCallee, Type::getInt8PtrTy(Ctx), "");
330 Value *BCI2 =
331 BBBuilder.CreateBitCast(DirectCallee, Type::getInt8PtrTy(Ctx), "");
332 Value *PtrCmp = BBBuilder.CreateICmpEQ(BCI1, BCI2, "");
333
334 uint64_t ElseCount = TotalCount - Count;
335 uint64_t MaxCount = (Count >= ElseCount ? Count : ElseCount);
336 uint64_t Scale = calculateCountScale(MaxCount);
337 MDBuilder MDB(Inst->getContext());
338 MDNode *BranchWeights = MDB.createBranchWeights(
339 scaleBranchCount(Count, Scale), scaleBranchCount(ElseCount, Scale));
340 TerminatorInst *ThenTerm, *ElseTerm;
341 SplitBlockAndInsertIfThenElse(PtrCmp, Inst, &ThenTerm, &ElseTerm,
342 BranchWeights);
343 *DirectCallBB = ThenTerm->getParent();
344 (*DirectCallBB)->setName("if.true.direct_targ");
345 *IndirectCallBB = ElseTerm->getParent();
346 (*IndirectCallBB)->setName("if.false.orig_indirect");
347 *MergeBB = Inst->getParent();
348 (*MergeBB)->setName("if.end.icp");
349
350 // Special handing of Invoke instructions.
351 InvokeInst *II = dyn_cast<InvokeInst>(Inst);
352 if (!II)
353 return;
354
355 // We don't need branch instructions for invoke.
356 ThenTerm->eraseFromParent();
357 ElseTerm->eraseFromParent();
358
359 // Add jump from Merge BB to the NormalDest. This is needed for the newly
360 // created direct invoke stmt -- as its NormalDst will be fixed up to MergeBB.
361 BranchInst::Create(II->getNormalDest(), *MergeBB);
362}
363
364// Find the PHI in BB that have the CallResult as the operand.
365static bool getCallRetPHINode(BasicBlock *BB, Instruction *Inst) {
366 BasicBlock *From = Inst->getParent();
367 for (auto &I : *BB) {
368 PHINode *PHI = dyn_cast<PHINode>(&I);
369 if (!PHI)
370 continue;
371 int IX = PHI->getBasicBlockIndex(From);
372 if (IX == -1)
373 continue;
374 Value *V = PHI->getIncomingValue(IX);
375 if (dyn_cast<Instruction>(V) == Inst)
376 return true;
377 }
378 return false;
379}
380
381// This method fixes up PHI nodes in BB where BB is the UnwindDest of an
382// invoke instruction. In BB, there may be PHIs with incoming block being
383// OrigBB (the MergeBB after if-then-else splitting). After moving the invoke
384// instructions to its own BB, OrigBB is no longer the predecessor block of BB.
385// Instead two new predecessors are added: IndirectCallBB and DirectCallBB,
386// so the PHI node's incoming BBs need to be fixed up accordingly.
387static void fixupPHINodeForUnwind(Instruction *Inst, BasicBlock *BB,
388 BasicBlock *OrigBB,
389 BasicBlock *IndirectCallBB,
390 BasicBlock *DirectCallBB) {
391 for (auto &I : *BB) {
392 PHINode *PHI = dyn_cast<PHINode>(&I);
393 if (!PHI)
394 continue;
395 int IX = PHI->getBasicBlockIndex(OrigBB);
396 if (IX == -1)
397 continue;
398 Value *V = PHI->getIncomingValue(IX);
399 PHI->addIncoming(V, IndirectCallBB);
400 PHI->setIncomingBlock(IX, DirectCallBB);
401 }
402}
403
404// This method fixes up PHI nodes in BB where BB is the NormalDest of an
405// invoke instruction. In BB, there may be PHIs with incoming block being
406// OrigBB (the MergeBB after if-then-else splitting). After moving the invoke
407// instructions to its own BB, a new incoming edge will be added to the original
408// NormalDstBB from the IndirectCallBB.
409static void fixupPHINodeForNormalDest(Instruction *Inst, BasicBlock *BB,
410 BasicBlock *OrigBB,
411 BasicBlock *IndirectCallBB,
412 Instruction *NewInst) {
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 if (dyn_cast<Instruction>(V) == Inst) {
422 PHI->setIncomingBlock(IX, IndirectCallBB);
423 PHI->addIncoming(NewInst, OrigBB);
424 continue;
425 }
426 PHI->addIncoming(V, IndirectCallBB);
427 }
428}
429
430// Add a bitcast instruction to the direct-call return value if needed.
Rong Xu6e34c492016-04-27 23:20:27 +0000431static Instruction *insertCallRetCast(const Instruction *Inst,
432 Instruction *DirectCallInst,
433 Function *DirectCallee) {
434 if (Inst->getType()->isVoidTy())
435 return DirectCallInst;
436
437 Type *CallRetType = Inst->getType();
438 Type *FuncRetType = DirectCallee->getReturnType();
439 if (FuncRetType == CallRetType)
440 return DirectCallInst;
441
442 BasicBlock *InsertionBB;
443 if (CallInst *CI = dyn_cast<CallInst>(DirectCallInst))
444 InsertionBB = CI->getParent();
445 else
446 InsertionBB = (dyn_cast<InvokeInst>(DirectCallInst))->getNormalDest();
447
448 return (new BitCastInst(DirectCallInst, CallRetType, "",
449 InsertionBB->getTerminator()));
450}
451
452// Create a DirectCall instruction in the DirectCallBB.
453// Parameter Inst is the indirect-call (invoke) instruction.
454// DirectCallee is the decl of the direct-call (invoke) target.
455// DirecallBB is the BB that the direct-call (invoke) instruction is inserted.
456// MergeBB is the bottom BB of the if-then-else-diamond after the
457// transformation. For invoke instruction, the edges from DirectCallBB and
458// IndirectCallBB to MergeBB are removed before this call (during
459// createIfThenElse).
460static Instruction *createDirectCallInst(const Instruction *Inst,
461 Function *DirectCallee,
462 BasicBlock *DirectCallBB,
463 BasicBlock *MergeBB) {
464 Instruction *NewInst = Inst->clone();
465 if (CallInst *CI = dyn_cast<CallInst>(NewInst)) {
466 CI->setCalledFunction(DirectCallee);
467 CI->mutateFunctionType(DirectCallee->getFunctionType());
468 } else {
469 // Must be an invoke instruction. Direct invoke's normal destination is
470 // fixed up to MergeBB. MergeBB is the place where return cast is inserted.
471 // Also since IndirectCallBB does not have an edge to MergeBB, there is no
472 // need to insert new PHIs into MergeBB.
473 InvokeInst *II = dyn_cast<InvokeInst>(NewInst);
474 assert(II);
475 II->setCalledFunction(DirectCallee);
476 II->mutateFunctionType(DirectCallee->getFunctionType());
477 II->setNormalDest(MergeBB);
478 }
479
480 DirectCallBB->getInstList().insert(DirectCallBB->getFirstInsertionPt(),
481 NewInst);
482
483 // Clear the value profile data.
Eugene Zelenkocdc71612016-08-11 17:20:18 +0000484 NewInst->setMetadata(LLVMContext::MD_prof, nullptr);
Rong Xu6e34c492016-04-27 23:20:27 +0000485 CallSite NewCS(NewInst);
486 FunctionType *DirectCalleeType = DirectCallee->getFunctionType();
487 unsigned ParamNum = DirectCalleeType->getFunctionNumParams();
488 for (unsigned I = 0; I < ParamNum; ++I) {
489 Type *ATy = NewCS.getArgument(I)->getType();
490 Type *PTy = DirectCalleeType->getParamType(I);
491 if (ATy != PTy) {
492 BitCastInst *BI = new BitCastInst(NewCS.getArgument(I), PTy, "", NewInst);
493 NewCS.setArgument(I, BI);
494 }
495 }
496
497 return insertCallRetCast(Inst, NewInst, DirectCallee);
498}
499
500// Create a PHI to unify the return values of calls.
501static void insertCallRetPHI(Instruction *Inst, Instruction *CallResult,
502 Function *DirectCallee) {
503 if (Inst->getType()->isVoidTy())
504 return;
505
506 BasicBlock *RetValBB = CallResult->getParent();
507
508 BasicBlock *PHIBB;
509 if (InvokeInst *II = dyn_cast<InvokeInst>(CallResult))
510 RetValBB = II->getNormalDest();
511
512 PHIBB = RetValBB->getSingleSuccessor();
513 if (getCallRetPHINode(PHIBB, Inst))
514 return;
515
516 PHINode *CallRetPHI = PHINode::Create(Inst->getType(), 0);
517 PHIBB->getInstList().push_front(CallRetPHI);
518 Inst->replaceAllUsesWith(CallRetPHI);
519 CallRetPHI->addIncoming(Inst, Inst->getParent());
520 CallRetPHI->addIncoming(CallResult, RetValBB);
521}
522
523// This function does the actual indirect-call promotion transformation:
524// For an indirect-call like:
525// Ret = (*Foo)(Args);
526// It transforms to:
527// if (Foo == DirectCallee)
528// Ret1 = DirectCallee(Args);
529// else
530// Ret2 = (*Foo)(Args);
531// Ret = phi(Ret1, Ret2);
532// It adds type casts for the args do not match the parameters and the return
533// value. Branch weights metadata also updated.
Dehao Chencc75d242017-02-23 22:15:18 +0000534// If \p AttachProfToDirectCall is true, a prof metadata is attached to the
535// new direct call to contain \p Count. This is used by SamplePGO inliner to
536// check callsite hotness.
Dehao Chen14bf0292017-01-23 23:18:24 +0000537// Returns the promoted direct call instruction.
538Instruction *llvm::promoteIndirectCall(Instruction *Inst,
539 Function *DirectCallee, uint64_t Count,
Dehao Chencc75d242017-02-23 22:15:18 +0000540 uint64_t TotalCount,
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000541 bool AttachProfToDirectCall,
542 OptimizationRemarkEmitter *ORE) {
Rong Xu6e34c492016-04-27 23:20:27 +0000543 assert(DirectCallee != nullptr);
544 BasicBlock *BB = Inst->getParent();
545 // Just to suppress the non-debug build warning.
546 (void)BB;
547 DEBUG(dbgs() << "\n\n== Basic Block Before ==\n");
548 DEBUG(dbgs() << *BB << "\n");
549
550 BasicBlock *DirectCallBB, *IndirectCallBB, *MergeBB;
551 createIfThenElse(Inst, DirectCallee, Count, TotalCount, &DirectCallBB,
552 &IndirectCallBB, &MergeBB);
553
554 Instruction *NewInst =
555 createDirectCallInst(Inst, DirectCallee, DirectCallBB, MergeBB);
556
Dehao Chencc75d242017-02-23 22:15:18 +0000557 if (AttachProfToDirectCall) {
558 SmallVector<uint32_t, 1> Weights;
559 Weights.push_back(Count);
560 MDBuilder MDB(NewInst->getContext());
561 dyn_cast<Instruction>(NewInst->stripPointerCasts())
562 ->setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights));
563 }
564
Rong Xu6e34c492016-04-27 23:20:27 +0000565 // Move Inst from MergeBB to IndirectCallBB.
566 Inst->removeFromParent();
567 IndirectCallBB->getInstList().insert(IndirectCallBB->getFirstInsertionPt(),
568 Inst);
569
570 if (InvokeInst *II = dyn_cast<InvokeInst>(Inst)) {
571 // At this point, the original indirect invoke instruction has the original
572 // UnwindDest and NormalDest. For the direct invoke instruction, the
573 // NormalDest points to MergeBB, and MergeBB jumps to the original
574 // NormalDest. MergeBB might have a new bitcast instruction for the return
575 // value. The PHIs are with the original NormalDest. Since we now have two
576 // incoming edges to NormalDest and UnwindDest, we have to do some fixups.
577 //
578 // UnwindDest will not use the return value. So pass nullptr here.
579 fixupPHINodeForUnwind(Inst, II->getUnwindDest(), MergeBB, IndirectCallBB,
580 DirectCallBB);
581 // We don't need to update the operand from NormalDest for DirectCallBB.
582 // Pass nullptr here.
583 fixupPHINodeForNormalDest(Inst, II->getNormalDest(), MergeBB,
584 IndirectCallBB, NewInst);
585 }
586
587 insertCallRetPHI(Inst, NewInst, DirectCallee);
588
589 DEBUG(dbgs() << "\n== Basic Blocks After ==\n");
590 DEBUG(dbgs() << *BB << *DirectCallBB << *IndirectCallBB << *MergeBB << "\n");
591
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000592 using namespace ore;
593 if (ORE)
594 ORE->emit(OptimizationRemark(DEBUG_TYPE, "Promoted", Inst)
595 << "Promote indirect call to " << NV("DirectCallee", DirectCallee)
596 << " with count " << NV("Count", Count) << " out of "
597 << NV("TotalCount", TotalCount));
Dehao Chen14bf0292017-01-23 23:18:24 +0000598 return NewInst;
Rong Xu6e34c492016-04-27 23:20:27 +0000599}
600
601// Promote indirect-call to conditional direct-call for one callsite.
602uint32_t ICallPromotionFunc::tryToPromote(
603 Instruction *Inst, const std::vector<PromotionCandidate> &Candidates,
604 uint64_t &TotalCount) {
605 uint32_t NumPromoted = 0;
606
607 for (auto &C : Candidates) {
608 uint64_t Count = C.Count;
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000609 promoteIndirectCall(Inst, C.TargetFunction, Count, TotalCount, SamplePGO,
610 &ORE);
Rong Xu6e34c492016-04-27 23:20:27 +0000611 assert(TotalCount >= Count);
612 TotalCount -= Count;
613 NumOfPGOICallPromotion++;
614 NumPromoted++;
615 }
616 return NumPromoted;
617}
618
619// Traverse all the indirect-call callsite and get the value profile
620// annotation to perform indirect-call promotion.
Dehao Chen34cfcb22017-08-08 20:57:33 +0000621bool ICallPromotionFunc::processFunction(ProfileSummaryInfo *PSI) {
Rong Xu6e34c492016-04-27 23:20:27 +0000622 bool Changed = false;
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000623 ICallPromotionAnalysis ICallAnalysis;
Rong Xu6e34c492016-04-27 23:20:27 +0000624 for (auto &I : findIndirectCallSites(F)) {
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000625 uint32_t NumVals, NumCandidates;
Rong Xu6e34c492016-04-27 23:20:27 +0000626 uint64_t TotalCount;
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000627 auto ICallProfDataRef = ICallAnalysis.getPromotionCandidatesForInstruction(
628 I, NumVals, TotalCount, NumCandidates);
Dehao Chen34cfcb22017-08-08 20:57:33 +0000629 if (!NumCandidates ||
630 (PSI && PSI->hasProfileSummary() && !PSI->isHotCount(TotalCount)))
Rong Xu6e34c492016-04-27 23:20:27 +0000631 continue;
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000632 auto PromotionCandidates = getPromotionCandidatesForCallSite(
633 I, ICallProfDataRef, TotalCount, NumCandidates);
Rong Xu6e34c492016-04-27 23:20:27 +0000634 uint32_t NumPromoted = tryToPromote(I, PromotionCandidates, TotalCount);
635 if (NumPromoted == 0)
636 continue;
637
638 Changed = true;
639 // Adjust the MD.prof metadata. First delete the old one.
Eugene Zelenkocdc71612016-08-11 17:20:18 +0000640 I->setMetadata(LLVMContext::MD_prof, nullptr);
Rong Xu6e34c492016-04-27 23:20:27 +0000641 // If all promoted, we don't need the MD.prof metadata.
642 if (TotalCount == 0 || NumPromoted == NumVals)
643 continue;
644 // Otherwise we need update with the un-promoted records back.
Teresa Johnson1e44b5d2016-07-12 21:13:44 +0000645 annotateValueSite(*M, *I, ICallProfDataRef.slice(NumPromoted), TotalCount,
646 IPVK_IndirectCallTarget, NumCandidates);
Rong Xu6e34c492016-04-27 23:20:27 +0000647 }
648 return Changed;
649}
650
651// A wrapper function that does the actual work.
Dehao Chen34cfcb22017-08-08 20:57:33 +0000652static bool promoteIndirectCalls(Module &M, ProfileSummaryInfo *PSI,
653 bool InLTO, bool SamplePGO,
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000654 ModuleAnalysisManager *AM = nullptr) {
Rong Xu6e34c492016-04-27 23:20:27 +0000655 if (DisableICP)
656 return false;
657 InstrProfSymtab Symtab;
Vedant Kumarb5794ca2017-06-20 01:38:56 +0000658 if (Error E = Symtab.create(M, InLTO)) {
659 std::string SymtabFailure = toString(std::move(E));
660 DEBUG(dbgs() << "Failed to create symtab: " << SymtabFailure << "\n");
661 (void)SymtabFailure;
662 return false;
663 }
Rong Xu6e34c492016-04-27 23:20:27 +0000664 bool Changed = false;
665 for (auto &F : M) {
666 if (F.isDeclaration())
667 continue;
668 if (F.hasFnAttribute(Attribute::OptimizeNone))
669 continue;
Adam Nemet0d8b5d62017-07-27 16:54:15 +0000670
671 std::unique_ptr<OptimizationRemarkEmitter> OwnedORE;
672 OptimizationRemarkEmitter *ORE;
673 if (AM) {
674 auto &FAM =
675 AM->getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
676 ORE = &FAM.getResult<OptimizationRemarkEmitterAnalysis>(F);
677 } else {
678 OwnedORE = make_unique<OptimizationRemarkEmitter>(&F);
679 ORE = OwnedORE.get();
680 }
681
682 ICallPromotionFunc ICallPromotion(F, &M, &Symtab, SamplePGO, *ORE);
Dehao Chen34cfcb22017-08-08 20:57:33 +0000683 bool FuncChanged = ICallPromotion.processFunction(PSI);
Rong Xu6e34c492016-04-27 23:20:27 +0000684 if (ICPDUMPAFTER && FuncChanged) {
685 DEBUG(dbgs() << "\n== IR Dump After =="; F.print(dbgs()));
686 DEBUG(dbgs() << "\n");
687 }
688 Changed |= FuncChanged;
689 if (ICPCutOff != 0 && NumOfPGOICallPromotion >= ICPCutOff) {
690 DEBUG(dbgs() << " Stop: Cutoff reached.\n");
691 break;
692 }
693 }
694 return Changed;
695}
696
Xinliang David Li72616182016-05-15 01:04:24 +0000697bool PGOIndirectCallPromotionLegacyPass::runOnModule(Module &M) {
Dehao Chen34cfcb22017-08-08 20:57:33 +0000698 ProfileSummaryInfo *PSI =
699 getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
700
Rong Xu6e34c492016-04-27 23:20:27 +0000701 // Command-line option has the priority for InLTO.
Dehao Chen34cfcb22017-08-08 20:57:33 +0000702 return promoteIndirectCalls(M, PSI, InLTO | ICPLTOMode,
Dehao Chencc75d242017-02-23 22:15:18 +0000703 SamplePGO | ICPSamplePGOMode);
Rong Xu6e34c492016-04-27 23:20:27 +0000704}
Xinliang David Lif3c7a352016-05-16 16:31:07 +0000705
Dehao Chencc75d242017-02-23 22:15:18 +0000706PreservedAnalyses PGOIndirectCallPromotion::run(Module &M,
707 ModuleAnalysisManager &AM) {
Dehao Chen34cfcb22017-08-08 20:57:33 +0000708 ProfileSummaryInfo *PSI = &AM.getResult<ProfileSummaryAnalysis>(M);
709
710 if (!promoteIndirectCalls(M, PSI, InLTO | ICPLTOMode,
711 SamplePGO | ICPSamplePGOMode, &AM))
Xinliang David Lif3c7a352016-05-16 16:31:07 +0000712 return PreservedAnalyses::all();
713
714 return PreservedAnalyses::none();
715}