forked from revng/revng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemoryaccess.h
245 lines (208 loc) · 7.1 KB
/
memoryaccess.h
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#ifndef _MEMORYACCESS_H
#define _MEMORYACCESS_H
//
// This file is distributed under the MIT License. See LICENSE.md for details.
//
// Standard includes
#include <cstdint>
#include <unordered_map>
#include <utility>
// LLVM includes
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Instructions.h"
#include "llvm/Support/Casting.h"
// Local includes
#include "ir-helpers.h"
class TypeSizeProvider {
public:
TypeSizeProvider(const llvm::DataLayout &DL) : DL(DL) { }
unsigned getSize(llvm::Type *T) {
auto CacheIt = Cache.find(T);
if (CacheIt != Cache.end()) {
return CacheIt->second;
} else {
auto Result = DL.getTypeSizeInBits(T) * 8;
Cache[T] = Result;
return Result;
}
}
private:
std::unordered_map<llvm::Type *, unsigned> Cache;
const llvm::DataLayout &DL;
};
/// \brief Represents an access to the CPU state or the memory
class MemoryAccess {
public:
MemoryAccess() : Type(Invalid), Base(nullptr), Offset(0), Size(0) { }
MemoryAccess(llvm::Instruction *I, TypeSizeProvider &TSP) {
if (auto *Load = llvm::dyn_cast<llvm::LoadInst>(I)) {
initialize(Load->getPointerOperand(), I, TSP);
} else if (auto *Store = llvm::dyn_cast<llvm::StoreInst>(I)) {
initialize(Store->getPointerOperand(), Store->getValueOperand(), TSP);
} else {
assert(false);
}
}
MemoryAccess(llvm::LoadInst *Load, TypeSizeProvider &TSP) {
initialize(Load->getPointerOperand(), Load, TSP);
}
MemoryAccess(llvm::StoreInst *Store, TypeSizeProvider &TSP) {
initialize(Store->getPointerOperand(), Store->getValueOperand(), TSP);
}
MemoryAccess(llvm::Instruction *I, const llvm::DataLayout &DL) {
if (auto *Load = llvm::dyn_cast<llvm::LoadInst>(I)) {
initialize(Load->getPointerOperand(), I, DL);
} else if (auto *Store = llvm::dyn_cast<llvm::StoreInst>(I)) {
initialize(Store->getPointerOperand(), Store->getValueOperand(), DL);
} else {
assert(false);
}
}
MemoryAccess(llvm::LoadInst *Load, const llvm::DataLayout &DL) {
initialize(Load->getPointerOperand(), Load, DL);
}
MemoryAccess(llvm::StoreInst *Store, const llvm::DataLayout &DL) {
initialize(Store->getPointerOperand(), Store->getValueOperand(), DL);
}
bool operator==(const MemoryAccess &Other) const {
if (Type != Other.Type || Size != Other.Size)
return false;
switch (Type) {
case Invalid:
return true;
break;
case CPUState:
return Base == Other.Base;
break;
case RegisterAndOffset:
return Base == Other.Base && Offset == Other.Offset;
break;
}
assert(false);
}
bool operator!=(const MemoryAccess &Other) const { return !(*this == Other); }
bool mayAlias(const MemoryAccess &Other) const {
if (Type == Invalid || Other.Type == Invalid)
return true;
// If they're both CPU state, they alias only if the are the same part of
// the CPU state. If one of them is CPU state and the other is a register +
// offset, they alias only if the register written by the first memory
// access is the one read by the second one.
if ((Type == CPUState && Other.Type == CPUState)
|| (Type == CPUState && Other.Type == RegisterAndOffset)
|| (Type == RegisterAndOffset && Other.Type == CPUState))
return Base == Other.Base;
// If they're RegisterAndOffset and they're not relative to the same
// register we known nothing about the content of the base register,
// therefore they may alias.
// If they're relative to the same register, we check if the two memory
// accesses overlap, if they don't there's no alias.
// Note that we can assume the content of the register is the same, since if
// this wasn't the case we'd have already had an alias situation when
// writing the register.
if (Type == RegisterAndOffset && Other.Type == RegisterAndOffset) {
if (Base != Other.Base)
return true;
return intersect({ Offset, Size }, { Other.Offset, Other.Size });
}
assert(false);
}
bool isValid() const { return Type != Invalid; }
static bool mayAlias(llvm::BasicBlock *BB,
const MemoryAccess &Other,
const llvm::DataLayout &DL) {
for (llvm::Instruction &I : *BB)
if (auto *Store = llvm::dyn_cast<llvm::StoreInst>(&I))
if (MemoryAccess(Store, DL).mayAlias(Other))
return true;
return false;
}
private:
bool intersect(std::pair<uint64_t, uint64_t> A,
std::pair<uint64_t, uint64_t> B) const {
return A.first < (B.first + B.second) && B.first < (A.first + A.second);
}
bool isVariable(llvm::Value *V) const {
auto *CSV = llvm::dyn_cast<llvm::GlobalVariable>(V);
return (CSV != nullptr && CSV->getName() != "env")
|| llvm::isa<llvm::AllocaInst>(V);
}
void initialize(llvm::Value *Pointer,
llvm::Value *PointeeValue,
const llvm::DataLayout &DL) {
// Set the size
Size = DL.getTypeSizeInBits(PointeeValue->getType()) * 8;
initialize(Pointer);
}
void initialize(llvm::Value *Pointer,
llvm::Value *PointeeValue,
TypeSizeProvider &TSP) {
// Set the size
Size = TSP.getSize(PointeeValue->getType());
initialize(Pointer);
}
void initialize(llvm::Value *Pointer) {
// Default situation: we can't handle this load
Type = Invalid;
Base = nullptr;
Offset = 0;
if (isVariable(Pointer)) {
// Load from CPU state
Type = CPUState;
Base = Pointer;
} else if (auto *V = llvm::dyn_cast<llvm::Instruction>(Pointer)) {
// Try to handle load from an address stored in a register plus an offset
// This mainly aims to handle very simple variables stored on the stack
uint64_t Addend = 0;
while (true) {
switch (V->getOpcode()) {
case llvm::Instruction::IntToPtr:
case llvm::Instruction::PtrToInt:
{
auto *Operand = llvm::dyn_cast<llvm::Instruction>(V->getOperand(0));
if (Operand != nullptr)
V = Operand;
else
return;
}
break;
case llvm::Instruction::Add:
{
auto Operands = operandsByType<llvm::Instruction *,
llvm::ConstantInt *>(V);
llvm::Instruction *FirstOp;
llvm::ConstantInt *SecondOp;
std::tie(FirstOp, SecondOp) = Operands;
if (Addend != 0 || SecondOp == nullptr || FirstOp == nullptr)
return;
Addend = SecondOp->getLimitedValue();
V = FirstOp;
break;
}
case llvm::Instruction::Load:
{
llvm::Value *LoadOperand = V->getOperand(0);
if (isVariable(LoadOperand)) {
Type = RegisterAndOffset;
Base = LoadOperand;
Offset = Addend;
}
return;
}
default:
return;
}
}
}
}
private:
enum {
Invalid,
CPUState,
RegisterAndOffset
} Type;
const llvm::Value *Base;
uint64_t Offset;
uint64_t Size;
};
#endif // _MEMORYACCESS_H