From 57578aaf96129bb2afbb75bc2523aa47fc1aa993 Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Mon, 7 Jan 2019 07:15:51 +0000 Subject: [PATCH] [CallSite removal] Port `IndirectCallSiteVisitor` to use `CallBase` and update client code. Also rename it to use the more generic term `call` instead of something that could be confused with a praticular type. Differential Revision: https://reviews.llvm.org/D56183 llvm-svn: 350508 --- .../llvm/Analysis/IndirectCallSiteVisitor.h | 40 ---------------------- llvm/include/llvm/Analysis/IndirectCallVisitor.h | 39 +++++++++++++++++++++ llvm/include/llvm/IR/InstrTypes.h | 3 ++ .../lib/Analysis/IndirectCallPromotionAnalysis.cpp | 2 +- llvm/lib/IR/Instructions.cpp | 10 ++++++ .../Instrumentation/IndirectCallPromotion.cpp | 6 ++-- .../Instrumentation/PGOInstrumentation.cpp | 12 +++---- 7 files changed, 62 insertions(+), 50 deletions(-) delete mode 100644 llvm/include/llvm/Analysis/IndirectCallSiteVisitor.h create mode 100644 llvm/include/llvm/Analysis/IndirectCallVisitor.h diff --git a/llvm/include/llvm/Analysis/IndirectCallSiteVisitor.h b/llvm/include/llvm/Analysis/IndirectCallSiteVisitor.h deleted file mode 100644 index a30b59f..0000000 --- a/llvm/include/llvm/Analysis/IndirectCallSiteVisitor.h +++ /dev/null @@ -1,40 +0,0 @@ -//===-- IndirectCallSiteVisitor.h - indirect call-sites visitor -----------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file implements defines a visitor class and a helper function that find -// all indirect call-sites in a function. - -#ifndef LLVM_ANALYSIS_INDIRECTCALLSITEVISITOR_H -#define LLVM_ANALYSIS_INDIRECTCALLSITEVISITOR_H - -#include "llvm/IR/InstVisitor.h" -#include - -namespace llvm { -// Visitor class that finds all indirect call sites. -struct PGOIndirectCallSiteVisitor - : public InstVisitor { - std::vector IndirectCallInsts; - PGOIndirectCallSiteVisitor() {} - - void visitCallSite(CallSite CS) { - if (CS.isIndirectCall()) - IndirectCallInsts.push_back(CS.getInstruction()); - } -}; - -// Helper function that finds all indirect call sites. -inline std::vector findIndirectCallSites(Function &F) { - PGOIndirectCallSiteVisitor ICV; - ICV.visit(F); - return ICV.IndirectCallInsts; -} -} - -#endif diff --git a/llvm/include/llvm/Analysis/IndirectCallVisitor.h b/llvm/include/llvm/Analysis/IndirectCallVisitor.h new file mode 100644 index 0000000..d00cf63 --- /dev/null +++ b/llvm/include/llvm/Analysis/IndirectCallVisitor.h @@ -0,0 +1,39 @@ +//===-- IndirectCallVisitor.h - indirect call visitor ---------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements defines a visitor class and a helper function that find +// all indirect call-sites in a function. + +#ifndef LLVM_ANALYSIS_INDIRECTCALLVISITOR_H +#define LLVM_ANALYSIS_INDIRECTCALLVISITOR_H + +#include "llvm/IR/InstVisitor.h" +#include + +namespace llvm { +// Visitor class that finds all indirect call. +struct PGOIndirectCallVisitor : public InstVisitor { + std::vector IndirectCalls; + PGOIndirectCallVisitor() {} + + void visitCallBase(CallBase &Call) { + if (Call.isIndirectCall()) + IndirectCalls.push_back(&Call); + } +}; + +// Helper function that finds all indirect call sites. +inline std::vector findIndirectCalls(Function &F) { + PGOIndirectCallVisitor ICV; + ICV.visit(F); + return ICV.IndirectCalls; +} +} // namespace llvm + +#endif diff --git a/llvm/include/llvm/IR/InstrTypes.h b/llvm/include/llvm/IR/InstrTypes.h index 5136eb5..0ee811a 100644 --- a/llvm/include/llvm/IR/InstrTypes.h +++ b/llvm/include/llvm/IR/InstrTypes.h @@ -1182,6 +1182,9 @@ public: return dyn_cast_or_null(getCalledOperand()); } + /// Return true if the callsite is an indirect call. + bool isIndirectCall() const; + /// Helper to get the caller (the parent function). Function *getCaller(); const Function *getCaller() const { diff --git a/llvm/lib/Analysis/IndirectCallPromotionAnalysis.cpp b/llvm/lib/Analysis/IndirectCallPromotionAnalysis.cpp index 4659c0a..d6e6e76 100644 --- a/llvm/lib/Analysis/IndirectCallPromotionAnalysis.cpp +++ b/llvm/lib/Analysis/IndirectCallPromotionAnalysis.cpp @@ -15,7 +15,7 @@ #include "llvm/Analysis/IndirectCallPromotionAnalysis.h" #include "llvm/ADT/STLExtras.h" -#include "llvm/Analysis/IndirectCallSiteVisitor.h" +#include "llvm/Analysis/IndirectCallVisitor.h" #include "llvm/IR/CallSite.h" #include "llvm/IR/InstIterator.h" #include "llvm/IR/InstVisitor.h" diff --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp index e8b5811..c7437f6 100644 --- a/llvm/lib/IR/Instructions.cpp +++ b/llvm/lib/IR/Instructions.cpp @@ -257,6 +257,16 @@ void LandingPadInst::addClause(Constant *Val) { Function *CallBase::getCaller() { return getParent()->getParent(); } +bool CallBase::isIndirectCall() const { + const Value *V = getCalledValue(); + if (isa(V) || isa(V)) + return false; + if (const CallInst *CI = dyn_cast(this)) + if (CI->isInlineAsm()) + return false; + return true; +} + Intrinsic::ID CallBase::getIntrinsicID() const { if (auto *F = getCalledFunction()) return F->getIntrinsicID(); diff --git a/llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp b/llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp index 7edc53d..58436c8 100644 --- a/llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp +++ b/llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp @@ -19,7 +19,7 @@ #include "llvm/ADT/Statistic.h" #include "llvm/ADT/StringRef.h" #include "llvm/Analysis/IndirectCallPromotionAnalysis.h" -#include "llvm/Analysis/IndirectCallSiteVisitor.h" +#include "llvm/Analysis/IndirectCallVisitor.h" #include "llvm/Analysis/OptimizationRemarkEmitter.h" #include "llvm/Analysis/ProfileSummaryInfo.h" #include "llvm/IR/Attributes.h" @@ -41,8 +41,8 @@ #include "llvm/ProfileData/InstrProf.h" #include "llvm/Support/Casting.h" #include "llvm/Support/CommandLine.h" -#include "llvm/Support/Error.h" #include "llvm/Support/Debug.h" +#include "llvm/Support/Error.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Transforms/Instrumentation.h" #include "llvm/Transforms/Instrumentation/PGOInstrumentation.h" @@ -352,7 +352,7 @@ uint32_t ICallPromotionFunc::tryToPromote( bool ICallPromotionFunc::processFunction(ProfileSummaryInfo *PSI) { bool Changed = false; ICallPromotionAnalysis ICallAnalysis; - for (auto &I : findIndirectCallSites(F)) { + for (auto &I : findIndirectCalls(F)) { uint32_t NumVals, NumCandidates; uint64_t TotalCount; auto ICallProfDataRef = ICallAnalysis.getPromotionCandidatesForInstruction( diff --git a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp index 876ae23..f043325 100644 --- a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp +++ b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp @@ -63,7 +63,7 @@ #include "llvm/Analysis/BlockFrequencyInfo.h" #include "llvm/Analysis/BranchProbabilityInfo.h" #include "llvm/Analysis/CFG.h" -#include "llvm/Analysis/IndirectCallSiteVisitor.h" +#include "llvm/Analysis/IndirectCallVisitor.h" #include "llvm/Analysis/LoopInfo.h" #include "llvm/Analysis/OptimizationRemarkEmitter.h" #include "llvm/IR/Attributes.h" @@ -544,7 +544,7 @@ public: MIVisitor.countMemIntrinsics(Func); NumOfPGOSelectInsts += SIVisitor.getNumOfSelectInsts(); NumOfPGOMemIntrinsics += MIVisitor.getNumOfMemIntrinsics(); - ValueSites[IPVK_IndirectCallTarget] = findIndirectCallSites(Func); + ValueSites[IPVK_IndirectCallTarget] = findIndirectCalls(Func); ValueSites[IPVK_MemOPSize] = MIVisitor.findMemIntrinsics(Func); FuncName = getPGOFuncName(F); @@ -754,12 +754,12 @@ static void instrumentOneFunc( if (DisableValueProfiling) return; - unsigned NumIndirectCallSites = 0; + unsigned NumIndirectCalls = 0; for (auto &I : FuncInfo.ValueSites[IPVK_IndirectCallTarget]) { CallSite CS(I); Value *Callee = CS.getCalledValue(); LLVM_DEBUG(dbgs() << "Instrument one indirect call: CallSite Index = " - << NumIndirectCallSites << "\n"); + << NumIndirectCalls << "\n"); IRBuilder<> Builder(I); assert(Builder.GetInsertPoint() != I->getParent()->end() && "Cannot get the Instrumentation point"); @@ -769,9 +769,9 @@ static void instrumentOneFunc( Builder.getInt64(FuncInfo.FunctionHash), Builder.CreatePtrToInt(Callee, Builder.getInt64Ty()), Builder.getInt32(IPVK_IndirectCallTarget), - Builder.getInt32(NumIndirectCallSites++)}); + Builder.getInt32(NumIndirectCalls++)}); } - NumOfPGOICall += NumIndirectCallSites; + NumOfPGOICall += NumIndirectCalls; // Now instrument memop intrinsic calls. FuncInfo.MIVisitor.instrumentMemIntrinsics( -- 2.7.4