summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorTeresa Johnson <tejohnson@google.com>2016-07-12 21:13:44 +0000
committerTeresa Johnson <tejohnson@google.com>2016-07-12 21:13:44 +0000
commite0c5ceae903f6b1bd8c40e62d65e0cc1fdf6e10a (patch)
tree98765fb0f3d7e7a1437b2f66aaa0aa50e2d24a48 /include
parentcb071cb0e319e6ee29079345826a84257168e979 (diff)
Refactor indirect call promotion profitability analysis (NFC)
Summary: Refactored the profitability analysis out of the IC promotion pass and into lib/Analysis so that it can be accessed by the summary index builder in a follow-on patch to enable IC promotion in ThinLTO (D21932). Reviewers: davidxl, xur Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D22182 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@275216 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include')
-rw-r--r--include/llvm/Analysis/IndirectCallPromotionAnalysis.h67
-rw-r--r--include/llvm/Analysis/IndirectCallSiteVisitor.h43
2 files changed, 110 insertions, 0 deletions
diff --git a/include/llvm/Analysis/IndirectCallPromotionAnalysis.h b/include/llvm/Analysis/IndirectCallPromotionAnalysis.h
new file mode 100644
index 00000000000..007e4d8602f
--- /dev/null
+++ b/include/llvm/Analysis/IndirectCallPromotionAnalysis.h
@@ -0,0 +1,67 @@
+//===- IndirectCallPromotionAnalysis.h - Indirect call analysis -*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+/// \file
+/// Interface to identify indirect call promotion candidates.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_ANALYSIS_INDIRECTCALLPROMOTIONANALYSIS_H
+#define LLVM_ANALYSIS_INDIRECTCALLPROMOTIONANALYSIS_H
+
+#include "llvm/ProfileData/InstrProf.h"
+
+namespace llvm {
+
+class Instruction;
+
+// Class for identifying profitable indirect call promotion candidates when
+// the indirect-call value profile metadata is available.
+class ICallPromotionAnalysis {
+private:
+ // Allocate space to read the profile annotation.
+ std::unique_ptr<InstrProfValueData[]> ValueDataArray;
+
+ // Count is the call count for the direct-call target and
+ // TotalCount is the call count for the indirect-call callsite.
+ // Return true we should promote this indirect-call target.
+ bool isPromotionProfitable(uint64_t Count, uint64_t TotalCount);
+
+ // Returns the number of profitable candidates to promote for the
+ // current ValueDataArray and the given \p Inst.
+ uint32_t getProfitablePromotionCandidates(const Instruction *Inst,
+ uint32_t NumVals,
+ uint64_t TotalCount);
+
+ // Noncopyable
+ ICallPromotionAnalysis(const ICallPromotionAnalysis &other) = delete;
+ ICallPromotionAnalysis &
+ operator=(const ICallPromotionAnalysis &other) = delete;
+
+public:
+ ICallPromotionAnalysis();
+
+ /// \brief Returns reference to array of InstrProfValueData for the given
+ /// instruction \p I.
+ ///
+ /// The \p NumVals, \p TotalCount and \p NumCandidates
+ /// are set to the number of values in the array, the total profile count
+ /// of the indirect call \p I, and the number of profitable candidates
+ /// in the given array (which is sorted in reverse order of profitability).
+ ///
+ /// The returned array space is owned by this class, and overwritten on
+ /// subsequent calls.
+ ArrayRef<InstrProfValueData>
+ getPromotionCandidatesForInstruction(const Instruction *I, uint32_t &NumVals,
+ uint64_t &TotalCount,
+ uint32_t &NumCandidates);
+};
+
+} // end namespace llvm
+
+#endif
diff --git a/include/llvm/Analysis/IndirectCallSiteVisitor.h b/include/llvm/Analysis/IndirectCallSiteVisitor.h
new file mode 100644
index 00000000000..71a8cb88632
--- /dev/null
+++ b/include/llvm/Analysis/IndirectCallSiteVisitor.h
@@ -0,0 +1,43 @@
+//===-- 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;
+ }
+ if (isa<Constant>(CS.getCalledValue()))
+ 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;
+}
+}