LLVM 22.0.0git
InsertCodePrefetch.cpp
Go to the documentation of this file.
1//===-- InsertCodePrefetch.cpp ---=========--------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9/// \file
10/// Code Prefetch Insertion Pass.
11//===----------------------------------------------------------------------===//
12/// This pass inserts code prefetch instructions according to the prefetch
13/// directives in the basic block section profile. The target of a prefetch can
14/// be the beginning of any dynamic basic block, that is the beginning of a
15/// machine basic block, or immediately after a callsite. A global symbol is
16/// emitted at the position of the target so it can be addressed from the
17/// prefetch instruction from any module.
18//===----------------------------------------------------------------------===//
19
22#include "llvm/ADT/StringRef.h"
28#include "llvm/CodeGen/Passes.h"
30
31using namespace llvm;
32#define DEBUG_TYPE "insert-code-prefetch"
33
34namespace {
35class InsertCodePrefetch : public MachineFunctionPass {
36public:
37 static char ID;
38
39 InsertCodePrefetch() : MachineFunctionPass(ID) {
41 }
42
43 StringRef getPassName() const override {
44 return "Code Prefetch Inserter Pass";
45 }
46
47 void getAnalysisUsage(AnalysisUsage &AU) const override;
48
49 // Sets prefetch targets based on the bb section profile.
50 bool runOnMachineFunction(MachineFunction &MF) override;
51};
52
53} // end anonymous namespace
54
55//===----------------------------------------------------------------------===//
56// Implementation
57//===----------------------------------------------------------------------===//
58
59char InsertCodePrefetch::ID = 0;
60INITIALIZE_PASS_BEGIN(InsertCodePrefetch, DEBUG_TYPE, "Code prefetch insertion",
61 true, false)
63INITIALIZE_PASS_END(InsertCodePrefetch, DEBUG_TYPE, "Code prefetch insertion",
65
66bool InsertCodePrefetch::runOnMachineFunction(MachineFunction &MF) {
67 assert(MF.getTarget().getBBSectionsType() == BasicBlockSection::List &&
68 "BB Sections list not enabled!");
70 return false;
71 // Set each block's prefetch targets so AsmPrinter can emit a special symbol
72 // there.
73 SmallVector<CallsiteID> PrefetchTargets =
74 getAnalysis<BasicBlockSectionsProfileReaderWrapperPass>()
75 .getPrefetchTargetsForFunction(MF.getName());
76 DenseMap<UniqueBBID, SmallVector<unsigned>> PrefetchTargetsByBBID;
77 for (const auto &Target : PrefetchTargets)
78 PrefetchTargetsByBBID[Target.BBID].push_back(Target.CallsiteIndex);
79 // Sort and uniquify the callsite indices for every block.
80 for (auto &[K, V] : PrefetchTargetsByBBID) {
81 llvm::sort(V);
82 V.erase(llvm::unique(V), V.end());
83 }
84 for (auto &MBB : MF) {
85 auto R = PrefetchTargetsByBBID.find(*MBB.getBBID());
86 if (R == PrefetchTargetsByBBID.end())
87 continue;
88 MBB.setPrefetchTargetCallsiteIndexes(R->second);
89 }
90 return false;
91}
92
93void InsertCodePrefetch::getAnalysisUsage(AnalysisUsage &AU) const {
94 AU.setPreservesAll();
95 AU.addRequired<BasicBlockSectionsProfileReaderWrapperPass>();
97}
98
100 return new InsertCodePrefetch();
101}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
#define DEBUG_TYPE
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file defines the SmallVector class.
This file contains some functions that are useful when dealing with strings.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:178
iterator end()
Definition DenseMap.h:81
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Target - Wrapper for Target specific information.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
auto unique(Range &&R, Predicate P)
Definition STLExtras.h:2124
LLVM_ABI void initializeInsertCodePrefetchPass(PassRegistry &)
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1634
bool hasInstrProfHashMismatch(MachineFunction &MF)
This checks if the source of this function has drifted since this binary was profiled previously.
LLVM_ABI MachineFunctionPass * createInsertCodePrefetchPass()