forked from google/souper
/
Inst.h
351 lines (302 loc) · 10.1 KB
/
Inst.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
343
344
345
346
347
348
349
350
351
// Copyright 2014 The Souper Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SOUPER_INST_INST_H
#define SOUPER_INST_INST_H
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/FoldingSet.h"
#include "llvm/IR/ConstantRange.h"
#include "llvm/IR/Value.h"
#include "souper/SMTLIB2/Solver.h"
#include <map>
#include <memory>
#include <set>
#include <string>
#include <unordered_set>
#include <vector>
namespace souper {
enum class HarvestType { HarvestedFromDef, HarvestedFromUse };
const unsigned MaxPreds = 100000;
extern const std::string ReservedConstPrefix;
extern const std::string ReservedInstPrefix;
extern const std::string BlockPred;
struct Inst;
struct Block {
std::string Name;
unsigned Preds;
unsigned Number;
unsigned ConcretePred = -1;
std::vector<Inst *> PredVars;
};
struct Inst : llvm::FoldingSetNode {
typedef enum {
Const,
UntypedConst,
Var,
Phi,
Hole,
Add,
AddNSW,
AddNUW,
AddNW,
Sub,
SubNSW,
SubNUW,
SubNW,
Mul,
MulNSW,
MulNUW,
MulNW,
UDiv,
SDiv,
UDivExact,
SDivExact,
URem,
SRem,
And,
Or,
Xor,
Shl,
ShlNSW,
ShlNUW,
ShlNW,
LShr,
LShrExact,
AShr,
AShrExact,
Select,
ZExt,
SExt,
Trunc,
Eq,
Ne,
Ult,
Slt,
Ule,
Sle,
CtPop,
BSwap,
Cttz,
Ctlz,
BitReverse,
FShl,
FShr,
ExtractValue,
SAddWithOverflow,
SAddO,
UAddWithOverflow,
UAddO,
SSubWithOverflow,
SSubO,
USubWithOverflow,
USubO,
SMulWithOverflow,
SMulO,
UMulWithOverflow,
UMulO,
SAddSat,
UAddSat,
SSubSat,
USubSat,
Freeze,
ReservedConst,
ReservedInst,
None,
} Kind;
Kind K;
unsigned Number;
unsigned Width;
Block *B;
bool Available = true;
llvm::APInt Val;
std::string Name;
std::unordered_set<Inst *> DepsWithExternalUses;
std::vector<Inst *> Ops;
mutable std::vector<Inst *> OrderedOps;
std::vector<llvm::Value *> Origins;
bool operator<(const Inst &I) const;
const std::vector<Inst *> &orderedOps() const;
bool hasOrigin(llvm::Value *V) const;
void Profile(llvm::FoldingSetNodeID &ID) const;
#ifndef NDEBUG
// Helpful for debugging. Prints instruction using llvm::err with a newly created replacement context.
void Print();
#endif
static const char *getKindName(Kind K);
static std::string getKnownBitsString(llvm::APInt Zero, llvm::APInt One);
static std::string getMoreKnownBitsString(bool NonZero, bool NonNegative,
bool PowOfTwo, bool Negative);
static std::string getDemandedBitsString(llvm::APInt DBVal);
static Kind getKind(std::string Name);
static bool isAssociative(Kind K);
static bool isCmp(Kind K);
static bool isTernary(Kind K);
static bool isOverflowIntrinsicMain(Kind K);
static bool isOverflowIntrinsicSub(Kind K);
static Kind getOverflowComplement(Kind K);
static Kind getBasicInstrForOverflow(Kind K);
static bool isCommutative(Kind K);
static bool isShift(Kind K);
static bool isDivRem(Kind K);
static int getCost(Kind K);
llvm::APInt KnownZeros;
llvm::APInt KnownOnes;
bool NonZero;
bool NonNegative;
bool PowOfTwo;
bool Negative;
unsigned NumSignBits;
llvm::APInt DemandedBits;
unsigned SynthesisConstID;
HarvestType HarvestKind;
llvm::BasicBlock* HarvestFrom;
llvm::ConstantRange Range=llvm::ConstantRange(1, true);
std::vector<llvm::ConstantRange> RangeRefinement;
int nReservedConsts = -1;
int nHoles = -1;
};
/// A mapping from an Inst to a replacement. This may either represent a
/// path condition or a candidate replacement.
struct InstMapping {
InstMapping() : LHS(0), RHS(0) {}
InstMapping(Inst *LHS, Inst *RHS)
: LHS(LHS), RHS(RHS) {}
Inst *LHS, *RHS;
};
struct BlockPCMapping {
BlockPCMapping() : B(0) {}
BlockPCMapping(Block *B, unsigned I, InstMapping PC)
: B(B), PredIdx(I), PC(PC) {}
Block *B;
unsigned PredIdx;
InstMapping PC;
};
typedef std::vector<BlockPCMapping> BlockPCs;
class ReplacementContext {
llvm::DenseMap<Inst *, std::string> InstNames;
llvm::DenseMap<Block *, std::string> BlockNames;
std::map<std::string, Inst *> NameToInst;
std::map<std::string, Block *> NameToBlock;
std::string printInstImpl(Inst *I, llvm::raw_ostream &Out, bool printNames, Inst *OrigI);
public:
void printPCs(const std::vector<InstMapping> &PCs,
llvm::raw_ostream &Out, bool printNames);
void printBlockPCs(const BlockPCs &BPCs,
llvm::raw_ostream &Out, bool printNames);
std::string printInst(Inst *I, llvm::raw_ostream &Out, bool printNames);
std::string printBlock(Block *B, llvm::raw_ostream &Out);
Inst *getInst(llvm::StringRef Name);
void setInst(llvm::StringRef Name, Inst *I);
Block *getBlock(llvm::StringRef Name);
void setBlock(llvm::StringRef Name, Block *B);
void clear();
bool empty();
};
class InstContext {
typedef llvm::DenseMap<unsigned, std::vector<std::unique_ptr<Block>>>
BlockMap;
BlockMap BlocksByPreds;
typedef llvm::DenseMap<unsigned, std::vector<std::unique_ptr<Inst>>> InstMap;
InstMap VarInstsByWidth;
std::vector<std::unique_ptr<Inst>> Insts;
llvm::FoldingSet<Inst> InstSet;
unsigned ReservedConstCounter = 0;
public:
Inst *getConst(const llvm::APInt &I);
Inst *getUntypedConst(const llvm::APInt &I);
Inst *getReservedConst();
Inst *getReservedInst();
Inst *createHole(unsigned Width);
Inst *createSynthesisConstant(unsigned Width, unsigned SynthesisConstID);
Inst *createVar(unsigned Width, llvm::StringRef Name);
Inst *createVar(unsigned Width, llvm::StringRef Name,
llvm::ConstantRange Range,
llvm::APInt Zero, llvm::APInt One,
bool NonZero, bool NonNegative, bool PowOfTwo,
bool Negative, unsigned NumSignBits,
llvm::APInt Demandedbits, unsigned SynthesisConstID);
Block *createBlock(unsigned Preds);
Inst *getPhi(Block *B, const std::vector<Inst *> &Ops);
Inst *getPhi(Block *B, const std::vector<Inst *> &Ops, llvm::APInt Demandedbits);
Inst *getInst(Inst::Kind K, unsigned Width, const std::vector<Inst *> &Ops,
bool Available=true);
Inst *getInst(Inst::Kind K, unsigned Width, const std::vector<Inst *> &Ops,
llvm::APInt DemandedBits, bool Available);
std::vector<Inst *> getVariables() const;
};
struct SynthesisContext {
InstContext &IC;
SMTLIBSolver *SMTSolver;
Inst *LHS;
Inst *LHSUB;
const std::vector<InstMapping> &PCs;
const BlockPCs &BPCs;
bool CheckAllGuesses;
unsigned Timeout;
};
int cost(Inst *I, bool IgnoreDepsWithExternalUses = false);
int countHelper(Inst *I, std::set<Inst *> &Visited);
int instCount(Inst *I);
int benefit(Inst *LHS, Inst *RHS);
void PrintReplacement(llvm::raw_ostream &Out, const BlockPCs &BPCs,
const std::vector<InstMapping> &PCs, InstMapping Mapping,
bool printNames = false);
std::string GetReplacementString(const BlockPCs &BPCs,
const std::vector<InstMapping> &PCs,
InstMapping Mapping,
bool printNames = false);
void PrintReplacementLHS(llvm::raw_ostream &Out, const BlockPCs &BPCs,
const std::vector<InstMapping> &PCs,
Inst *LHS, ReplacementContext &Context,
bool printNames = false);
std::string GetReplacementLHSString(const BlockPCs &BPCs,
const std::vector<InstMapping> &PCs,
Inst *LHS, ReplacementContext &Context,
bool printNames = false);
void PrintReplacementRHS(llvm::raw_ostream &Out, Inst *RHS,
ReplacementContext &Context,
bool printNames = false);
std::string GetReplacementRHSString(Inst *RHS, ReplacementContext &Context,
bool printNames = false);
void findCands(Inst *Root, std::vector<Inst *> &Guesses,
bool WidthMustMatch, bool FilterVars, int Max);
Inst *getInstCopy(Inst *I, InstContext &IC,
std::map<Inst *, Inst *> &InstCache,
std::map<Block *, Block *> &BlockCache,
std::map<Inst *, llvm::APInt> *ConstMap,
bool CloneVars, bool CloneBlocks = true);
Inst *instJoin(Inst *I, Inst *Reserved, Inst *NewInst,
std::map<Inst *, Inst *> &InstCache, InstContext &IC);
void findVars(Inst *Root, std::vector<Inst *> &Vars);
void findInsts(Inst *Root, std::vector<Inst *> &Insts, std::function<bool(Inst*)> Condition);
bool hasGivenInst(Inst *Root, std::function<bool(Inst*)> InstTester);
void getHoles(Inst *Root, std::vector<Inst *> &Holes);
void getConstants(Inst *Root, std::set<Inst *> &ConstSet);
void getReservedInsts(Inst *Root, std::vector<Inst *> &ReservedInsts);
void separateBlockPCs(const BlockPCs &BPCs, BlockPCs &BPCsCopy,
std::map<Inst *, Inst *> &InstCache,
std::map<Block *, Block *> &BlockCache,
InstContext &IC,
std::map<Inst *, llvm::APInt> *ConstMap,
bool CloneVars);
void separatePCs(const std::vector<InstMapping> &PCs,
std::vector<InstMapping> &PCsCopy,
std::map<Inst *, Inst *> &InstCache,
std::map<Block *, Block *> &BlockCache,
InstContext &IC,
std::map<Inst *, llvm::APInt> *ConstMap,
bool CloneVars);
std::vector<Block *> getBlocksFromPhis(Inst *I);
}
#endif // SOUPER_INST_INST_H