-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryAccessPatterns.cpp
More file actions
183 lines (154 loc) · 6.27 KB
/
Copy pathMemoryAccessPatterns.cpp
File metadata and controls
183 lines (154 loc) · 6.27 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "llvm/Transforms/Utils/MemoryAccessPatterns.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Instruction.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Value.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/GetElementPtrTypeIterator.h"
#include "llvm/IR/Constants.h"
#include "llvm/Support/CommandLine.h"
#include <map>
#include <string>
static llvm::cl::opt<bool> AnalyzeMemoryAccessPatterns(
"analyze-memory-access-patterns",
llvm::cl::desc("Enable memory access patterns analysis"),
llvm::cl::init(false));
static llvm::cl::opt<bool> MemoryAccesses(
"memory-access",
llvm::cl::desc("Enable memory access count analysis"),
llvm::cl::init(false));
using namespace llvm;
PreservedAnalyses MemoryAccessPatternsPass::run(Module &M, ModuleAnalysisManager &AM) {
if (MemoryAccesses){
for (Function &F : M) {
unsigned LoadCount = 0;
unsigned StoreCount = 0;
// Iterate over all basic blocks in the function
for (BasicBlock &BB : F) {
// Iterate over all instructions in the basic block
for (Instruction &I : BB) {
if (isa<LoadInst>(&I)) { // Use &I to get a pointer to Instruction
LoadCount++;
} else if (isa<StoreInst>(&I)) {
StoreCount++;
}
}
}
// Print the counts of loads and stores for the function
errs() << "Function: " << F.getName() << "\n";
errs() << " Load instructions: " << LoadCount << "\n";
errs() << " Store instructions: " << StoreCount << "\n";
}
}
if (!AnalyzeMemoryAccessPatterns) {
return PreservedAnalyses::all(); // Skip analysis if the option is not enabled
}
const DataLayout &DL = M.getDataLayout();
std::map<Value*, std::string> VariableNames;
std::map<std::string, MemoryAccessInfo> AccessMap;
// First pass: Collect variable names
for (Function &F : M) {
for (BasicBlock &BB : F) {
for (Instruction &I : BB) {
if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
Value *Ptr = LI->getPointerOperand();
std::string VarName = getVariableName(Ptr, &F, &I);
uint64_t Size = DL.getTypeStoreSize(LI->getType());
VariableNames[Ptr] = VarName;
AccessMap[VarName].Size = Size;
AccessMap[VarName].Count = 0;
} else if (StoreInst *SI = dyn_cast<StoreInst>(&I)) {
Value *Ptr = SI->getPointerOperand();
std::string VarName = getVariableName(Ptr, &F, &I);
uint64_t Size = DL.getTypeStoreSize(SI->getValueOperand()->getType());
VariableNames[Ptr] = VarName;
AccessMap[VarName].Size = Size;
AccessMap[VarName].Count = 0;
}
}
}
}
// Second pass: Perform counting using pre-collected names
for (Function &F : M) {
for (const auto &Entry : AccessMap) {
AccessMap[Entry.first].Count=0;
}
for (BasicBlock &BB : F) {
for (Instruction &I : BB) {
if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
Value *Ptr = LI->getPointerOperand();
std::string VarName = VariableNames[Ptr];
uint64_t Size = DL.getTypeStoreSize(LI->getType());
AccessMap[VarName].Count++;
} else if (StoreInst *SI = dyn_cast<StoreInst>(&I)) {
Value *Ptr = SI->getPointerOperand();
std::string VarName = VariableNames[Ptr];
uint64_t Size = DL.getTypeStoreSize(SI->getValueOperand()->getType());
AccessMap[VarName].Count++;
}
}
}
// Print the memory access patterns for the function
errs() << F.getParent()->getSourceFileName() << ":" << F.getName() << "()\n";
for (const auto &Entry : AccessMap) {
if (Entry.second.Count!=0){
errs() << Entry.first << ": " << (Entry.second.Count)*(Entry.second.Size) << " bytes ("
<< Entry.second.Count << " time" << (Entry.second.Count > 1 ? "s" : "") << ")\n";
}
}
errs() << "\n";
}
return PreservedAnalyses::all();
}
std::string MemoryAccessPatternsPass::getVariableName(Value *V, Function *F, Instruction *I) {
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
return GV->getName().str();
}
if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
if (AI->hasName()) {
return AI->getName().str();
} else {
unsigned &Counter = LocalVarCounters[F];
return "UnnamedLocalVar[" + std::to_string(Counter++) + "]";
}
}
if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V)) {
return getArrayOrStructElementName(GEP, F, I);
}
if (Instruction *Inst = dyn_cast<Instruction>(V)) {
if (Inst->hasName()) {
return Inst->getName().str();
} else {
unsigned &Counter = LocalVarCounters[F];
return "UnnamedVar[" + std::to_string(Counter++) + "]";
}
}
return "UnnamedVar";
}
std::string MemoryAccessPatternsPass::getArrayOrStructElementName(GetElementPtrInst *GEP, Function *F, Instruction *I) {
Value *PtrOperand = GEP->getPointerOperand();
std::string BaseName = getVariableName(PtrOperand, F, I);
for (unsigned i = 1; i < GEP->getNumOperands(); ++i) {
Value *IndexValue = GEP->getOperand(i);
Type *SourceElementType = GEP->getSourceElementType();
if (SourceElementType->isArrayTy()) {
if (ConstantInt *CI = dyn_cast<ConstantInt>(IndexValue)) {
unsigned Index = CI->getZExtValue();
BaseName += "[" + std::to_string(Index) + "]";
} else {
BaseName += "[?]";
}
} else if (SourceElementType->isStructTy()) {
if (ConstantInt *CI = dyn_cast<ConstantInt>(IndexValue)) {
unsigned FieldIndex = CI->getZExtValue();
BaseName += ".field" + std::to_string(FieldIndex);
}
}
}
return BaseName;
}