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/SmallVector.h"
21#include "llvm/ADT/StringExtras.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/CodeGen/BasicBlockSectionUtils.h"
24#include "llvm/CodeGen/BasicBlockSectionsProfileReader.h"
25#include "llvm/CodeGen/MachineBasicBlock.h"
26#include "llvm/CodeGen/MachineFunction.h"
27#include "llvm/CodeGen/MachineFunctionPass.h"
28#include "llvm/CodeGen/Passes.h"
29#include "llvm/InitializePasses.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) {
40 initializeInsertCodePrefetchPass(*PassRegistry::getPassRegistry());
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)
62INITIALIZE_PASS_DEPENDENCY(BasicBlockSectionsProfileReaderWrapperPass)
63INITIALIZE_PASS_END(InsertCodePrefetch, DEBUG_TYPE, "Code prefetch insertion",
64 true, false)
65
66bool InsertCodePrefetch::runOnMachineFunction(MachineFunction &MF) {
67 assert(MF.getTarget().getBBSectionsType() == BasicBlockSection::List &&
68 "BB Sections list not enabled!");
69 if (hasInstrProfHashMismatch(MF))
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(FuncName: MF.getName());
76 DenseMap<UniqueBBID, SmallVector<unsigned>> PrefetchTargetsByBBID;
77 for (const auto &Target : PrefetchTargets)
78 PrefetchTargetsByBBID[Target.BBID].push_back(Elt: Target.CallsiteIndex);
79 // Sort and uniquify the callsite indices for every block.
80 for (auto &[K, V] : PrefetchTargetsByBBID) {
81 llvm::sort(C&: V);
82 V.erase(CS: llvm::unique(R&: V), CE: V.end());
83 }
84 for (auto &MBB : MF) {
85 auto R = PrefetchTargetsByBBID.find(Val: *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>();
96 MachineFunctionPass::getAnalysisUsage(AU);
97}
98
99MachineFunctionPass *llvm::createInsertCodePrefetchPass() {
100 return new InsertCodePrefetch();
101}
102