/
KnowledgeRetention.cpp
299 lines (266 loc) · 10.7 KB
/
KnowledgeRetention.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
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
//===- KnowledgeRetention.h - utilities to preserve informations *- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/IR/KnowledgeRetention.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/CommandLine.h"
using namespace llvm;
cl::opt<bool> ShouldPreserveAllAttributes(
"assume-preserve-all", cl::init(false), cl::Hidden,
cl::desc("enable preservation of all attrbitues. even those that are "
"unlikely to be usefull"));
namespace {
struct AssumedKnowledge {
const char *Name;
Value *Argument;
enum {
None,
Empty,
Tombstone,
};
/// Contain the argument and a flag if needed.
llvm::PointerIntPair<Value *, 2> WasOn;
};
} // namespace
namespace llvm {
template <> struct DenseMapInfo<AssumedKnowledge> {
static AssumedKnowledge getEmptyKey() {
return {nullptr, nullptr, {nullptr, AssumedKnowledge::Empty}};
}
static AssumedKnowledge getTombstoneKey() {
return {nullptr, nullptr, {nullptr, AssumedKnowledge::Tombstone}};
}
static unsigned getHashValue(const AssumedKnowledge &AK) {
return hash_combine(AK.Name, AK.Argument, AK.WasOn.getPointer());
}
static bool isEqual(const AssumedKnowledge &LHS,
const AssumedKnowledge &RHS) {
return LHS.WasOn == RHS.WasOn && LHS.Name == RHS.Name &&
LHS.Argument == RHS.Argument;
}
};
} // namespace llvm
namespace {
/// Index of elements in the operand bundle.
/// If the element exist it is guaranteed to be what is specified in this enum
/// but it may not exist.
enum BundleOpInfoElem {
BOIE_WasOn = 0,
BOIE_Argument = 1,
};
/// Deterministically compare OperandBundleDef.
/// The ordering is:
/// - by the attribute's name aka operand bundle tag, (doesn't change)
/// - then by the numeric Value of the argument, (doesn't change)
/// - lastly by the Name of the current Value it WasOn. (may change)
/// This order is deterministic and allows looking for the right kind of
/// attribute with binary search. However finding the right WasOn needs to be
/// done via linear search because values can get replaced.
bool isLowerOpBundle(const OperandBundleDef &LHS, const OperandBundleDef &RHS) {
auto getTuple = [](const OperandBundleDef &Op) {
return std::make_tuple(
Op.getTag(),
Op.input_size() <= BOIE_Argument
? 0
: cast<ConstantInt>(*(Op.input_begin() + BOIE_Argument))
->getZExtValue(),
Op.input_size() <= BOIE_WasOn
? StringRef("")
: (*(Op.input_begin() + BOIE_WasOn))->getName());
};
return getTuple(LHS) < getTuple(RHS);
}
/// This class contain all knowledge that have been gather while building an
/// llvm.assume and the function to manipulate it.
struct AssumeBuilderState {
Module *M;
SmallDenseSet<AssumedKnowledge, 8> AssumedKnowledgeSet;
AssumeBuilderState(Module *M) : M(M) {}
void addAttribute(Attribute Attr, Value *WasOn) {
StringRef Name;
Value *AttrArg = nullptr;
if (Attr.isStringAttribute())
if (ShouldPreserveAllAttributes)
Name = Attr.getKindAsString();
else
return;
else
Name = Attribute::getNameFromAttrKind(Attr.getKindAsEnum());
if (Attr.isIntAttribute())
AttrArg = ConstantInt::get(Type::getInt64Ty(M->getContext()),
Attr.getValueAsInt());
AssumedKnowledgeSet.insert(
{Name.data(), AttrArg, {WasOn, AssumedKnowledge::None}});
}
void addCall(const CallBase *Call) {
auto addAttrList = [&](AttributeList AttrList) {
for (unsigned Idx = AttributeList::FirstArgIndex;
Idx < AttrList.getNumAttrSets(); Idx++)
for (Attribute Attr : AttrList.getAttributes(Idx))
addAttribute(Attr, Call->getArgOperand(Idx - 1));
if (ShouldPreserveAllAttributes)
for (Attribute Attr : AttrList.getFnAttributes())
addAttribute(Attr, nullptr);
};
addAttrList(Call->getAttributes());
if (Function *Fn = Call->getCalledFunction())
addAttrList(Fn->getAttributes());
}
CallInst *build() {
if (AssumedKnowledgeSet.empty())
return nullptr;
Function *FnAssume = Intrinsic::getDeclaration(M, Intrinsic::assume);
LLVMContext &C = M->getContext();
SmallVector<OperandBundleDef, 8> OpBundle;
for (const AssumedKnowledge &Elem : AssumedKnowledgeSet) {
SmallVector<Value *, 2> Args;
assert(Attribute::getAttrKindFromName(Elem.Name) ==
Attribute::AttrKind::None ||
static_cast<bool>(Elem.Argument) ==
Attribute::doesAttrKindHaveArgument(
Attribute::getAttrKindFromName(Elem.Name)));
if (Elem.WasOn.getPointer())
Args.push_back(Elem.WasOn.getPointer());
if (Elem.Argument)
Args.push_back(Elem.Argument);
OpBundle.push_back(OperandBundleDefT<Value *>(Elem.Name, Args));
}
llvm::sort(OpBundle, isLowerOpBundle);
return CallInst::Create(
FnAssume, ArrayRef<Value *>({ConstantInt::getTrue(C)}), OpBundle);
}
void addInstruction(const Instruction *I) {
if (auto *Call = dyn_cast<CallBase>(I))
addCall(Call);
// TODO: Add support for the other Instructions.
// TODO: Maybe we should look around and merge with other llvm.assume.
}
};
} // namespace
CallInst *llvm::BuildAssumeFromInst(const Instruction *I, Module *M) {
AssumeBuilderState Builder(M);
Builder.addInstruction(I);
return Builder.build();
}
static bool BundleHasArguement(const CallBase::BundleOpInfo &BOI,
unsigned Idx) {
return BOI.End - BOI.Begin > Idx;
}
static Value *getValueFromBundleOpInfo(IntrinsicInst &Assume,
const CallBase::BundleOpInfo &BOI,
unsigned Idx) {
assert(BundleHasArguement(BOI, Idx) && "index out of range");
return (Assume.op_begin() + BOI.Begin + Idx)->get();
}
bool llvm::hasAttributeInAssume(CallInst &AssumeCI, Value *IsOn,
StringRef AttrName, uint64_t *ArgVal,
AssumeQuery AQR) {
IntrinsicInst &Assume = cast<IntrinsicInst>(AssumeCI);
assert(Assume.getIntrinsicID() == Intrinsic::assume &&
"this function is intended to be used on llvm.assume");
assert(Attribute::isExistingAttribute(AttrName) &&
"this attribute doesn't exist");
assert((ArgVal == nullptr || Attribute::doesAttrKindHaveArgument(
Attribute::getAttrKindFromName(AttrName))) &&
"requested value for an attribute that has no argument");
if (Assume.bundle_op_infos().empty())
return false;
CallInst::bundle_op_iterator Lookup;
/// The right attribute can be found by binary search. After this finding the
/// right WasOn needs to be done via linear search.
/// Element have been ordered by argument value so the first we find is the
/// one we need.
if (AQR == AssumeQuery::Lowest)
Lookup =
llvm::lower_bound(Assume.bundle_op_infos(), AttrName,
[](const CallBase::BundleOpInfo &BOI, StringRef RHS) {
return BOI.Tag->getKey() < RHS;
});
else
Lookup = std::prev(
llvm::upper_bound(Assume.bundle_op_infos(), AttrName,
[](StringRef LHS, const CallBase::BundleOpInfo &BOI) {
return LHS < BOI.Tag->getKey();
}));
if (Lookup == Assume.bundle_op_info_end() ||
Lookup->Tag->getKey() != AttrName)
return false;
if (IsOn) {
assert((Lookup->End - Lookup->Begin > BOIE_WasOn) &&
"missing argument of attribute");
while (true) {
if (Lookup == Assume.bundle_op_info_end() ||
Lookup->Tag->getKey() != AttrName)
return false;
if (getValueFromBundleOpInfo(Assume, *Lookup, BOIE_WasOn) == IsOn)
break;
if (AQR == AssumeQuery::Highest &&
Lookup == Assume.bundle_op_info_begin())
return false;
Lookup = Lookup + (AQR == AssumeQuery::Lowest ? 1 : -1);
}
}
if (Lookup->End - Lookup->Begin < BOIE_Argument)
return true;
if (ArgVal)
*ArgVal = cast<ConstantInt>(
getValueFromBundleOpInfo(Assume, *Lookup, BOIE_Argument))
->getZExtValue();
return true;
}
void llvm::fillMapFromAssume(CallInst &AssumeCI, RetainedKnowledgeMap &Result) {
IntrinsicInst &Assume = cast<IntrinsicInst>(AssumeCI);
assert(Assume.getIntrinsicID() == Intrinsic::assume &&
"this function is intended to be used on llvm.assume");
for (auto &Bundles : Assume.bundle_op_infos()) {
std::pair<Value *, Attribute::AttrKind> Key{
nullptr, Attribute::getAttrKindFromName(Bundles.Tag->getKey())};
if (BundleHasArguement(Bundles, BOIE_WasOn))
Key.first = getValueFromBundleOpInfo(Assume, Bundles, BOIE_WasOn);
if (Key.first == nullptr && Key.second == Attribute::None)
continue;
if (!BundleHasArguement(Bundles, BOIE_Argument)) {
Result[Key] = {0, 0};
continue;
}
unsigned Val = cast<ConstantInt>(
getValueFromBundleOpInfo(Assume, Bundles, BOIE_Argument))
->getZExtValue();
auto Lookup = Result.find(Key);
if (Lookup == Result.end()) {
Result[Key] = {Val, Val};
continue;
}
Lookup->second.Min = std::min(Val, Lookup->second.Min);
Lookup->second.Max = std::max(Val, Lookup->second.Max);
}
}
RetainedKnowledge llvm::getKnowledgeFromOperandInAssume(CallInst &AssumeCI,
unsigned Idx) {
IntrinsicInst &Assume = cast<IntrinsicInst>(AssumeCI);
assert(Assume.getIntrinsicID() == Intrinsic::assume &&
"this function is intended to be used on llvm.assume");
CallBase::BundleOpInfo BOI = Assume.getBundleOpInfoForOperand(Idx);
RetainedKnowledge Result;
Result.AttrKind = Attribute::getAttrKindFromName(BOI.Tag->getKey());
Result.WasOn = getValueFromBundleOpInfo(Assume, BOI, BOIE_WasOn);
if (BOI.End - BOI.Begin > BOIE_Argument)
Result.ArgValue =
cast<ConstantInt>(getValueFromBundleOpInfo(Assume, BOI, BOIE_Argument))
->getZExtValue();
return Result;
}
PreservedAnalyses AssumeBuilderPass::run(Function &F,
FunctionAnalysisManager &AM) {
for (Instruction &I : instructions(F))
if (Instruction *Assume = BuildAssumeFromInst(&I))
Assume->insertBefore(&I);
return PreservedAnalyses::all();
}