LLVM 23.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
20#include "llvm/ADT/DenseMap.h"
23#include "llvm/ADT/StringRef.h"
29#include "llvm/CodeGen/Passes.h"
31
32using namespace llvm;
33#define DEBUG_TYPE "insert-code-prefetch"
34
35namespace {
36class InsertCodePrefetch : public MachineFunctionPass {
37public:
38 static char ID;
39
40 InsertCodePrefetch() : MachineFunctionPass(ID) {}
41
42 StringRef getPassName() const override {
43 return "Code Prefetch Inserter Pass";
44 }
45
46 void getAnalysisUsage(AnalysisUsage &AU) const override;
47
48 // Sets prefetch targets based on the bb section profile.
49 bool runOnMachineFunction(MachineFunction &MF) override;
50};
51
52} // end anonymous namespace
53
54//===----------------------------------------------------------------------===//
55// Implementation
56//===----------------------------------------------------------------------===//
57
58char InsertCodePrefetch::ID = 0;
59INITIALIZE_PASS_BEGIN(InsertCodePrefetch, DEBUG_TYPE, "Code prefetch insertion",
60 true, false)
62INITIALIZE_PASS_END(InsertCodePrefetch, DEBUG_TYPE, "Code prefetch insertion",
64
65bool InsertCodePrefetch::runOnMachineFunction(MachineFunction &MF) {
66 assert(MF.getTarget().getBBSectionsType() == BasicBlockSection::List &&
67 "BB Sections list not enabled!");
69 return false;
70 // Set each block's prefetch targets so AsmPrinter can emit a special symbol
71 // there.
72 SmallVector<CallsiteID> PrefetchTargets =
73 getAnalysis<BasicBlockSectionsProfileReaderWrapperPass>()
74 .getPrefetchTargetsForFunction(MF.getName());
75 DenseMap<UniqueBBID, SmallVector<unsigned>> PrefetchTargetsByBBID;
76 for (const auto &Target : PrefetchTargets)
77 PrefetchTargetsByBBID[Target.BBID].push_back(Target.CallsiteIndex);
78 // Sort and uniquify the callsite indices for every block.
79 for (auto &[K, V] : PrefetchTargetsByBBID) {
80 llvm::sort(V);
81 V.erase(llvm::unique(V), V.end());
82 }
83 MF.setPrefetchTargets(PrefetchTargetsByBBID);
84 return false;
85}
86
87void InsertCodePrefetch::getAnalysisUsage(AnalysisUsage &AU) const {
88 AU.setPreservesAll();
89 AU.addRequired<BasicBlockSectionsProfileReaderWrapperPass>();
91}
92
94 return new InsertCodePrefetch();
95}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the DenseMap class.
#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.
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.
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:2134
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1636
bool hasInstrProfHashMismatch(MachineFunction &MF)
This checks if the source of this function has drifted since this binary was profiled previously.
LLVM_ABI MachineFunctionPass * createInsertCodePrefetchPass()