-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDictionary.h
342 lines (289 loc) · 9.48 KB
/
Dictionary.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#ifndef DICTIONARY_H
#define DICTIONARY_H
#include "llvm/IR/Instructions.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Dominators.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/Crellvm/Structure.h"
#include "llvm/Crellvm/Infrules.h"
#include <memory>
#include <tuple>
namespace crellvm {
enum DictKeys {
// Common
ArgForStripPointerCasts,
ArgForFindAvailableLoadedValue,
ArgForIndices,
// InstSimplify
ArgForSimplifyAndInst,
ArgForSimplifyOrInst,
ArgForSimplifyShiftInst,
ArgForSimplifyXorInst,
// InstCombine
ArgForVisitMul,
ArgForFoldSelectOpOp,
ArgForLoadLoadStore,
ArgForSelectIcmpConst,
ArgForVisitICmp,
// Mem2Reg
ArgForMem2Reg,
// GVN
ArgForGVN,
// LICM
ArgForHoistOrSinkCond
};
/*
* Using Traits
* DictKeysTraits class defines which type to use for dictionary value
* per each key type (in DictKeys)
* If you define
* DEFINE_TRAITS(A, B)
* then Dictionary will use B as a value type for the key A
* (where A must be in DictKeys enum)
*/
#define DEFINE_TRAITS(KEYVAL, CLASSNAME) \
template <> struct DictKeysTraits<KEYVAL> { typedef CLASSNAME ty; }
template <DictKeys key> struct DictKeysTraits {
// DictKeysTraits class has no member for arbitrary type
};
// Used in InstructionSimplify.cpp : SimplifyAndInst(), SimplifyOrInst(),
// SimplifyXorInst(), SimplifyShiftInst()
struct SimplifyInstArg {
public:
SimplifyInstArg();
void setHintGenFunc(std::string microoptName,
std::function<void(llvm::Instruction *)> hintGenFunc);
void abort();
void generateHint(llvm::Instruction *arg) const;
bool isActivated() const;
std::string getMicroOptName() const;
private:
bool activated;
bool aborted;
llvm::Instruction *I;
std::string microoptName;
std::function<void(llvm::Instruction *)> hintGenFunc;
};
DEFINE_TRAITS(ArgForSimplifyAndInst, SimplifyInstArg);
DEFINE_TRAITS(ArgForSimplifyOrInst, SimplifyInstArg);
DEFINE_TRAITS(ArgForSimplifyXorInst, SimplifyInstArg);
DEFINE_TRAITS(ArgForSimplifyShiftInst, SimplifyInstArg);
// lib/IR/Value.cpp : Value::stripPointerCasts(), stripPointerCastsAndOffsets()
struct StripPointerCastsArg {
public:
typedef std::vector<llvm::Value *> TyStrippedValuesObj;
typedef std::shared_ptr<TyStrippedValuesObj> TyStrippedValues;
TyStrippedValues strippedValues;
StripPointerCastsArg();
};
DEFINE_TRAITS(ArgForStripPointerCasts, StripPointerCastsArg);
// lib/Analysis/Loads.cpp : FindAvailableLoadedValueArg
struct FindAvailableLoadedValueArg {
public:
typedef std::vector<std::pair<
crellvm::StripPointerCastsArg::TyStrippedValues,
std::pair<llvm::Instruction *, std::string>>> TyOrthogonalInsnsObj;
// Note that there are two kinds of orthogonal instructions :
// store, and 'call'.
typedef crellvm::StripPointerCastsArg::TyStrippedValuesObj TyPtrEqValuesObj;
typedef std::shared_ptr<TyOrthogonalInsnsObj> TyOrthogonalInsns;
typedef std::shared_ptr<TyPtrEqValuesObj> TyPtrEqValues;
TyOrthogonalInsns orthogonalInsns;
TyPtrEqValues ptr1EquivalentValues;
TyPtrEqValues ptr2EquivalentValues;
bool isLoadStore;
llvm::StoreInst *loadstoreStoreInst;
FindAvailableLoadedValueArg();
};
DEFINE_TRAITS(ArgForFindAvailableLoadedValue, FindAvailableLoadedValueArg);
struct IndicesArg {
public:
typedef std::map<const llvm::Instruction *, unsigned> TyInstrIndicesObj;
typedef std::shared_ptr<TyInstrIndicesObj> TyInstrIndices;
TyInstrIndices instrIndices;
typedef std::map<llvm::StringRef, unsigned> TyTermIndicesObj;
typedef std::shared_ptr<TyTermIndicesObj> TyTermIndices;
TyTermIndices termIndices;
typedef std::tuple<llvm::BasicBlock*,
llvm::Instruction*,
unsigned> UseTuple;
typedef std::map<llvm::Instruction*,
std::vector<UseTuple>> TyUseIndicesObj;
typedef std::shared_ptr<TyUseIndicesObj> TyUseIndices;
TyUseIndices useIndices;
IndicesArg();
};
DEFINE_TRAITS(ArgForIndices, IndicesArg);
// lib/Transforms/Utils/PromoteMemoryToRegister.cpp : Mem2RegArg
struct Mem2RegArg {
public:
struct StoreTriple {
std::shared_ptr<TyValue> value;
std::shared_ptr<TyExpr> expr;
std::string op0;
};
typedef std::map<const llvm::Instruction *, StoreTriple> TyStoreItemObj;
typedef std::shared_ptr<TyStoreItemObj> TyStoreItem;
TyStoreItem storeItem;
struct RenamePassTuple {
std::shared_ptr<TyExpr> instrL;
std::shared_ptr<TyExpr> instrR;
std::shared_ptr<TyPosition> instrPos;
std::string op0;
std::string op1;
llvm::BasicBlock* instrBB;
bool check;
};
typedef std::map<unsigned, RenamePassTuple> TyRecentInstrObj;
typedef std::shared_ptr<TyRecentInstrObj> TyRecentInstr;
TyRecentInstr recentInstr;
typedef std::vector<TyRecentInstrObj> TyInstrWorkListObj;
typedef std::shared_ptr<TyInstrWorkListObj> TyInstrWorkList;
TyInstrWorkList instrWorkList;
typedef std::vector<std::shared_ptr<TyExpr>> TyReplaceObj;
typedef std::shared_ptr<TyReplaceObj> TyReplace;
TyReplace replaceItem;
typedef std::vector<std::shared_ptr<TyExpr>> TyReplaceTObj;
typedef std::shared_ptr<TyReplaceTObj> TyReplaceT;
TyReplaceT replaceTag;
Mem2RegArg();
};
DEFINE_TRAITS(ArgForMem2Reg, Mem2RegArg);
// lib/Transforms/InstCombine/InstCombineMulDivRem.cpp : visitMul
struct VisitMulArg {
public:
bool needsComm;
};
DEFINE_TRAITS(ArgForVisitMul, VisitMulArg);
// lib/Transforms/InstCombine/InstCombineSelect.cpp : FoldSelectOpOp
struct FoldSelectOpOpArg {
public:
enum OperandCases { XY_XZ, YX_ZX, XY_ZX, YX_XZ };
OperandCases the_case;
};
DEFINE_TRAITS(ArgForFoldSelectOpOp, FoldSelectOpOpArg);
// lib/Transforms/Instcombine/InstCombineCompares.cpp
struct VisitICmpArg {
public:
VisitICmpArg();
bool swapOps;
};
DEFINE_TRAITS(ArgForVisitICmp, VisitICmpArg);
// lib/Transform/InstCombine/InstCombineLoadStoreAlloca.cpp : visitLoadInst
struct LoadLoadStoreArg {
public:
llvm::Instruction *v1_inst;
llvm::Value *v1;
llvm::Value *ptr1;
llvm::Value *ptr1src;
llvm::Value *ptr2;
llvm::Value *ptr2src;
std::shared_ptr<TyPosition> v2_org_position;
};
DEFINE_TRAITS(ArgForLoadLoadStore, LoadLoadStoreArg);
// lib/Transform/InstCombine/InstCombineSelect.cpp : visitSelectInstWithICmp
struct SelectIcmpConstArg {
public:
SelectIcmpConstArg();
bool activated;
bool isGtToLt;
bool isUnsigned;
bool selectCommutative;
llvm::SelectInst *Z;
llvm::ICmpInst *Y;
llvm::Value *X;
llvm::ConstantInt *C, *Cprime;
std::shared_ptr<TyPosition> Y_org_pos;
};
DEFINE_TRAITS(ArgForSelectIcmpConst, SelectIcmpConstArg);
// lib/Transforms/Scalar/GVN.cpp : processInstruction, findLeader
struct GVNArg {
public:
GVNArg();
bool isGVNReplace;
boost::any VNptr;
typedef std::pair<uint32_t, bool> TyInvTKey;
typedef std::map<llvm::Instruction*,
std::pair<llvm::SmallVector<uint32_t, 4>, llvm::SmallSetVector<TyInvTKey, 4>>> TyVETobj;
typedef std::shared_ptr<TyVETobj> TyVET;
TyVET VET;
typedef std::map<TyInvTKey,
std::pair<std::shared_ptr<crellvm::TyPropagateObject>, std::shared_ptr<crellvm::TyPropagateObject>>> TyInvTobj;
typedef std::shared_ptr<TyInvTobj> TyInvT;
TyInvT InvT;
typedef std::pair<llvm::BasicBlock *, std::pair<llvm::Instruction *, uint32_t>> TyCTElem;
typedef std::map<std::pair<llvm::Value*, uint32_t>, std::vector<TyCTElem>> TyCTobj;
typedef std::shared_ptr<TyCTobj> TyCT;
TyCT CT;
typedef std::map<llvm::BasicBlock*,
std::pair<std::vector<llvm::Instruction*>,
std::vector<std::shared_ptr<crellvm::TyInfrule>>>> TyCTInvobj;
typedef std::shared_ptr<TyCTInvobj> TyCTInv;
TyCTInv CTInv;
typedef std::map<llvm::PHINode*, uint32_t> TyCallPHIobj;
typedef std::shared_ptr<TyCallPHIobj> TyCallPHI;
TyCallPHI CallPHIs;
typedef std::map<uint32_t, uint32_t> TyVNCntobj;
typedef std::shared_ptr<TyVNCntobj> TyVNCnt;
TyVNCnt VNCnt;
std::vector<llvm::Instruction*> to_prop;
std::vector<std::shared_ptr<crellvm::TyInfrule>> infrules;
};
DEFINE_TRAITS(ArgForGVN, GVNArg);
// lib/Transforms/Scalar/LICM.cpp : to record whether sinking of hoisting
// an instruction can be validated
struct LICMHoistOrSinkCond {
public:
LICMHoistOrSinkCond();
bool useAA;
const llvm::Loop *CurLoop;
};
DEFINE_TRAITS(ArgForHoistOrSinkCond, LICMHoistOrSinkCond);
class Dictionary {
private:
std::map<DictKeys, boost::any> data;
public:
template <DictKeys key> bool exists() {
return data.find(key) != data.end();
}
template <DictKeys key> void assertExists() {
assert(data.find(key) != data.end() &&
"Dictionary does not contain the key.");
}
template <DictKeys key>
std::shared_ptr<typename DictKeysTraits<key>::ty> get() {
assertExists<key>();
std::shared_ptr<typename DictKeysTraits<key>::ty> ptr =
boost::any_cast<std::shared_ptr<typename DictKeysTraits<key>::ty>>(
data[key]);
return ptr;
}
template <DictKeys key>
std::shared_ptr<typename DictKeysTraits<key>::ty> create() {
auto ptr = std::shared_ptr<typename DictKeysTraits<key>::ty>(
new typename DictKeysTraits<key>::ty());
data[key] = ptr;
return ptr;
}
template <DictKeys key>
void set(std::shared_ptr<typename DictKeysTraits<key>::ty> &t) {
data[key] = t;
}
template <DictKeys key> void erase() {
assertExists<key>();
data.erase(key);
}
};
// PassDictionary: a dictionary shared throughout a pass
class PassDictionary : public Dictionary {
private:
static PassDictionary *_Instance;
public:
static void Create();
static PassDictionary &GetInstance();
static void Destroy();
};
}
#endif