-
Notifications
You must be signed in to change notification settings - Fork 0
/
RedundantLoadPass.cpp
139 lines (121 loc) · 3.78 KB
/
RedundantLoadPass.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
//=============================================================================
// FILE:
// RedundantLoadPass.cpp
//
// DESCRIPTION:
// Prune all load instructions which load from a pointer whose value was stored
// within the basic block. Replace all previously loaded values with the SSA
// register value which was originally stored.
//
// USAGE:
// opt -load libRedundantLoadPass.so --legacy-redundant-load ...
// opt -load-pass-plugin=libRedundantLoadPass.so -passes=redundant-load ...
//
// 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*>;
//-----------------------------------------------------------------------------
// RedundantLoadPass implementation
//-----------------------------------------------------------------------------
namespace
{
bool elideRedundantLoads(BasicBlock& BB, ValuePtrMap& Pointers)
{
bool Changed{};
for (auto& Inst : make_early_inc_range(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::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 visitor(Function& F)
{
bool Changed{};
std::unordered_map<Value*, Value*> Pointers{};
for (auto& BB : F)
{
Pointers.clear();
Changed |= elideRedundantLoads(BB, Pointers);
}
return Changed;
}
struct RedundantLoadPass : public PassInfoMixin<RedundantLoadPass>
{
PreservedAnalyses run(Function& F, FunctionAnalysisManager&)
{
return (visitor(F) ? PreservedAnalyses::none() : PreservedAnalyses::all());
}
};
struct LegacyRedundantLoadPass : public llvm::FunctionPass
{
static char ID;
LegacyRedundantLoadPass() : FunctionPass{ ID }
{}
bool runOnFunction(llvm::Function &F) override
{
return visitor(F);
}
};
} // anon namespace
//-----------------------------------------------------------------------------
// New PM Registration
//-----------------------------------------------------------------------------
llvm::PassPluginLibraryInfo getRedundantLoadPassPluginInfo()
{
return {
LLVM_PLUGIN_API_VERSION, "RedundantLoadPass", LLVM_VERSION_STRING,
[](PassBuilder& PB)
{
PB.registerPipelineParsingCallback(
[](StringRef Name, FunctionPassManager &FPM, ArrayRef<PassBuilder::PipelineElement>)
{
if (Name == "redundant-load")
{
FPM.addPass(RedundantLoadPass());
return true;
}
return false;
}
);
}
};
}
// This is the core interface for pass plugins. It guarantees that 'opt' will
// be able to recognize RedundantLoadPass when added to the pass pipeline on the
// command line, i.e. via '-passes=redundant-load'
extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo
llvmGetPassPluginInfo()
{
return getRedundantLoadPassPluginInfo();
}
//-----------------------------------------------------------------------------
// Legacy PM Registration
//-----------------------------------------------------------------------------
char LegacyRedundantLoadPass::ID = 0;
static RegisterPass<LegacyRedundantLoadPass> X(
"legacy-redundant-load", "RedundantLoadPass", true, false
);