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