--- /dev/null
+//===-- 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.
+
+#include "llvm/IR/InstVisitor.h"
+#include <vector>
+
+namespace llvm {
+// Visitor class that finds all indirect call sites.
+struct PGOIndirectCallSiteVisitor
+ : public InstVisitor<PGOIndirectCallSiteVisitor> {
+ std::vector<Instruction *> IndirectCallInsts;
+ PGOIndirectCallSiteVisitor() {}
+
+ void visitCallSite(CallSite CS) {
+ if (CS.getCalledFunction() || !CS.getCalledValue())
+ return;
+ Instruction *I = CS.getInstruction();
+ if (CallInst *CI = dyn_cast<CallInst>(I)) {
+ if (CI->isInlineAsm())
+ return;
+ }
+ IndirectCallInsts.push_back(I);
+ }
+};
+
+// Helper function that finds all indirect call sites.
+static inline std::vector<Instruction *> findIndirectCallSites(Function &F) {
+ PGOIndirectCallSiteVisitor ICV;
+ ICV.visit(F);
+ return ICV.IndirectCallInsts;
+}
+}
//===----------------------------------------------------------------------===//
#include "CFGMST.h"
+#include "IndirectCallSiteVisitor.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InstIterator.h"
-#include "llvm/IR/InstVisitor.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/MDBuilder.h"
return InstrBB;
}
-// Visitor class that finds all indirect call sites.
-struct PGOIndirectCallSiteVisitor
- : public InstVisitor<PGOIndirectCallSiteVisitor> {
- std::vector<Instruction *> IndirectCallInsts;
- PGOIndirectCallSiteVisitor() {}
-
- void visitCallSite(CallSite CS) {
- if (CS.getCalledFunction() || !CS.getCalledValue())
- return;
- Instruction *I = CS.getInstruction();
- if (CallInst *CI = dyn_cast<CallInst>(I)) {
- if (CI->isInlineAsm())
- return;
- }
- IndirectCallInsts.push_back(I);
- }
-};
-
// Visit all edge and instrument the edges not in MST, and do value profiling.
// Critical edges will be split.
static void instrumentOneFunc(Function &F, Module *M,
return;
unsigned NumIndirectCallSites = 0;
- PGOIndirectCallSiteVisitor ICV;
- ICV.visit(F);
- for (auto &I : ICV.IndirectCallInsts) {
+ for (auto &I : findIndirectCallSites(F)) {
CallSite CS(I);
Value *Callee = CS.getCalledValue();
DEBUG(dbgs() << "Instrument one indirect call: CallSite Index = "
createPGOFuncNameMetadata(F);
unsigned IndirectCallSiteIndex = 0;
- PGOIndirectCallSiteVisitor ICV;
- ICV.visit(F);
+ auto IndirectCallSites = findIndirectCallSites(F);
unsigned NumValueSites =
ProfileRecord.getNumValueSites(IPVK_IndirectCallTarget);
- if (NumValueSites != ICV.IndirectCallInsts.size()) {
+ if (NumValueSites != IndirectCallSites.size()) {
std::string Msg =
std::string("Inconsistent number of indirect call sites: ") +
F.getName().str();
return;
}
- for (auto &I : ICV.IndirectCallInsts) {
+ for (auto &I : IndirectCallSites) {
DEBUG(dbgs() << "Read one indirect call instrumentation: Index="
<< IndirectCallSiteIndex << " out of " << NumValueSites
<< "\n");