-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemoryTransferPass.cpp
161 lines (140 loc) · 4.24 KB
/
MemoryTransferPass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//=============================================================================
// FILE:
// MemoryTranferPass.cpp
//
// DESCRIPTION:
// Prune all store / load pairs which brace the transitions between basic blocks
// local to a single function where the loading basic block has a single predecessor.
//
// USAGE:
// opt -load libMemoryTransferPass.so --legacy-memory-transfer ...
// opt -load-pass-plugin=libMemoryTranferPass.so -passes=memory-transfer ...
//
// License: MIT
//=============================================================================
#include <unordered_map>
#include "llvm/Pass.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Passes/PassPlugin.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
using ValuePtrMap = std::unordered_map<Value*, Value*>;
//-----------------------------------------------------------------------------
// MemoryTranferPass implementation
//-----------------------------------------------------------------------------
namespace
{
bool elideRedundantLoads(BasicBlock* BB, ValuePtrMap const& Pointers)
{
bool Changed{};
for (auto& Inst : make_early_inc_range(*BB))
{
if (Inst.getOpcode() == Instruction::Load)
{
auto Pair{ Pointers.find(Inst.getOperand(0)) };
// Replace every load inst whose data was set as a store inst
// within this basic block with the SSA register value of the store.
// Prune the redundant load inst from the basic block.
if (Pair != Pointers.end())
{
Inst.replaceAllUsesWith(Pair->second);
Inst.eraseFromParent();
Changed = true;
}
}
}
return Changed;
}
bool elideMemoryTransfers(BasicBlock& BB, ValuePtrMap& Pointers)
{
bool Changed{};
for (auto& Inst : BB)
{
if (Inst.getOpcode() == Instruction::Store)
{
// Cache the pointer and data operands of every store inst.
Pointers[Inst.getOperand(1)] = Inst.getOperand(0);
}
else if (Inst.getOpcode() == Instruction::Br)
{
auto& Branch{ cast<BranchInst>(Inst) };
// Prune memory transfer loads to successors basic blocks with a single predecessor.
for (auto* Successor : Branch.successors())
{
if (Successor->getSinglePredecessor())
{
Changed |= elideRedundantLoads(Successor, Pointers);
}
}
}
}
return Changed;
}
bool visitor(Function& F)
{
bool Changed{};
ValuePtrMap Pointers{};
for (auto& BB : F)
{
Pointers.clear();
Changed |= elideMemoryTransfers(BB, Pointers);
}
return Changed;
}
struct MemoryTranferPass : public PassInfoMixin<MemoryTranferPass>
{
PreservedAnalyses run(Function& F, FunctionAnalysisManager&)
{
return (visitor(F) ? PreservedAnalyses::none() : PreservedAnalyses::all());
}
};
struct LegacyMemoryTransferPass : public llvm::FunctionPass
{
static char ID;
LegacyMemoryTransferPass() : FunctionPass{ ID }
{}
bool runOnFunction(llvm::Function &F) override
{
return visitor(F);
}
};
} // anon namespace
//-----------------------------------------------------------------------------
// New PM Registration
//-----------------------------------------------------------------------------
llvm::PassPluginLibraryInfo getMemoryTranferPassPluginInfo()
{
return {
LLVM_PLUGIN_API_VERSION, "MemoryTranferPass", LLVM_VERSION_STRING,
[](PassBuilder& PB)
{
PB.registerPipelineParsingCallback(
[](StringRef Name, FunctionPassManager &FPM, ArrayRef<PassBuilder::PipelineElement>)
{
if (Name == "memory-transfer")
{
FPM.addPass(MemoryTranferPass());
return true;
}
return false;
}
);
}
};
}
// This is the core interface for pass plugins. It guarantees that 'opt' will
// be able to recognize MemoryTranferPass when added to the pass pipeline on the
// command line, i.e. via '-passes=memory-transfer'
extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo
llvmGetPassPluginInfo()
{
return getMemoryTranferPassPluginInfo();
}
//-----------------------------------------------------------------------------
// Legacy PM Registration
//-----------------------------------------------------------------------------
char LegacyMemoryTransferPass::ID = 0;
static RegisterPass<LegacyMemoryTransferPass> X(
"legacy-memory-transfer", "MemoryTransferPass", true, false
);