blob: 5aa21d6a5091b61be6b7fa1815be8ddf1a8f0032 [file] [log] [blame]
George Burgess IVbfa401e2016-07-06 00:26:41 +00001//- CFLAndersAliasAnalysis.cpp - Unification-based Alias Analysis ---*- C++-*-//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a CFL-based, summary-based alias analysis algorithm. It
11// differs from CFLSteensAliasAnalysis in its inclusion-based nature while
12// CFLSteensAliasAnalysis is unification-based. This pass has worse performance
13// than CFLSteensAliasAnalysis (the worst case complexity of
14// CFLAndersAliasAnalysis is cubic, while the worst case complexity of
15// CFLSteensAliasAnalysis is almost linear), but it is able to yield more
16// precise analysis result. The precision of this analysis is roughly the same
17// as that of an one level context-sensitive Andersen's algorithm.
18//
George Burgess IV6d30aa02016-07-15 19:53:25 +000019// The algorithm used here is based on recursive state machine matching scheme
20// proposed in "Demand-driven alias analysis for C" by Xin Zheng and Radu
21// Rugina. The general idea is to extend the tranditional transitive closure
22// algorithm to perform CFL matching along the way: instead of recording
23// "whether X is reachable from Y", we keep track of "whether X is reachable
24// from Y at state Z", where the "state" field indicates where we are in the CFL
25// matching process. To understand the matching better, it is advisable to have
26// the state machine shown in Figure 3 of the paper available when reading the
27// codes: all we do here is to selectively expand the transitive closure by
28// discarding edges that are not recognized by the state machine.
29//
George Burgess IVc01b42f2016-07-19 20:38:21 +000030// There are two differences between our current implementation and the one
31// described in the paper:
32// - Our algorithm eagerly computes all alias pairs after the CFLGraph is built,
33// while in the paper the authors did the computation in a demand-driven
34// fashion. We did not implement the demand-driven algorithm due to the
35// additional coding complexity and higher memory profile, but if we found it
36// necessary we may switch to it eventually.
37// - In the paper the authors use a state machine that does not distinguish
38// value reads from value writes. For example, if Y is reachable from X at state
39// S3, it may be the case that X is written into Y, or it may be the case that
40// there's a third value Z that writes into both X and Y. To make that
41// distinction (which is crucial in building function summary as well as
42// retrieving mod-ref info), we choose to duplicate some of the states in the
43// paper's proposed state machine. The duplication does not change the set the
44// machine accepts. Given a pair of reachable values, it only provides more
45// detailed information on which value is being written into and which is being
46// read from.
George Burgess IV6d30aa02016-07-15 19:53:25 +000047//
George Burgess IVbfa401e2016-07-06 00:26:41 +000048//===----------------------------------------------------------------------===//
49
50// N.B. AliasAnalysis as a whole is phrased as a FunctionPass at the moment, and
51// CFLAndersAA is interprocedural. This is *technically* A Bad Thing, because
52// FunctionPasses are only allowed to inspect the Function that they're being
53// run on. Realistically, this likely isn't a problem until we allow
54// FunctionPasses to run concurrently.
55
56#include "llvm/Analysis/CFLAndersAliasAnalysis.h"
George Burgess IV1ca8aff2016-07-06 00:36:12 +000057#include "CFLGraph.h"
George Burgess IV6d30aa02016-07-15 19:53:25 +000058#include "llvm/ADT/DenseSet.h"
George Burgess IVbfa401e2016-07-06 00:26:41 +000059#include "llvm/Pass.h"
60
61using namespace llvm;
George Burgess IV1ca8aff2016-07-06 00:36:12 +000062using namespace llvm::cflaa;
George Burgess IVbfa401e2016-07-06 00:26:41 +000063
64#define DEBUG_TYPE "cfl-anders-aa"
65
George Burgess IV6d30aa02016-07-15 19:53:25 +000066CFLAndersAAResult::CFLAndersAAResult(const TargetLibraryInfo &TLI) : TLI(TLI) {}
67CFLAndersAAResult::CFLAndersAAResult(CFLAndersAAResult &&RHS)
68 : AAResultBase(std::move(RHS)), TLI(RHS.TLI) {}
69CFLAndersAAResult::~CFLAndersAAResult() {}
70
71static const Function *parentFunctionOfValue(const Value *Val) {
72 if (auto *Inst = dyn_cast<Instruction>(Val)) {
73 auto *Bb = Inst->getParent();
74 return Bb->getParent();
75 }
76
77 if (auto *Arg = dyn_cast<Argument>(Val))
78 return Arg->getParent();
79 return nullptr;
80}
81
82namespace {
83
84enum class MatchState : uint8_t {
George Burgess IVc01b42f2016-07-19 20:38:21 +000085 // The following state represents S1 in the paper.
86 FlowFromReadOnly = 0,
87 // The following two states together represent S2 in the paper.
88 // The 'NoReadWrite' suffix indicates that there exists an alias path that
89 // does not contain assignment and reverse assignment edges.
90 // The 'ReadOnly' suffix indicates that there exists an alias path that
91 // contains reverse assignment edges only.
92 FlowFromMemAliasNoReadWrite,
93 FlowFromMemAliasReadOnly,
94 // The following two states together represent S3 in the paper.
95 // The 'WriteOnly' suffix indicates that there exists an alias path that
96 // contains assignment edges only.
97 // The 'ReadWrite' suffix indicates that there exists an alias path that
98 // contains both assignment and reverse assignment edges. Note that if X and Y
99 // are reachable at 'ReadWrite' state, it does NOT mean X is both read from
100 // and written to Y. Instead, it means that a third value Z is written to both
101 // X and Y.
102 FlowToWriteOnly,
103 FlowToReadWrite,
104 // The following two states together represent S4 in the paper.
105 FlowToMemAliasWriteOnly,
106 FlowToMemAliasReadWrite,
George Burgess IV6d30aa02016-07-15 19:53:25 +0000107};
108
George Burgess IV3b059842016-07-19 20:47:15 +0000109typedef std::bitset<7> StateSet;
George Burgess IV22a0f1a2016-07-19 21:35:47 +0000110// N.B. These are unsigned instead of StateSets because some MSVC versions
111// apparently lack constexpr bitset ctors.
112LLVM_CONSTEXPR unsigned ReadOnlyStateMask =
113 (1U << static_cast<uint8_t>(MatchState::FlowFromReadOnly)) |
114 (1U << static_cast<uint8_t>(MatchState::FlowFromMemAliasReadOnly));
115LLVM_CONSTEXPR unsigned WriteOnlyStateMask =
116 (1U << static_cast<uint8_t>(MatchState::FlowToWriteOnly)) |
117 (1U << static_cast<uint8_t>(MatchState::FlowToMemAliasWriteOnly));
George Burgess IV3b059842016-07-19 20:47:15 +0000118
George Burgess IV4ec17532016-07-22 22:30:48 +0000119// A pair that consists of a value and an offset
120struct OffsetValue {
121 const Value *Val;
122 int64_t Offset;
123};
124
125bool operator==(OffsetValue LHS, OffsetValue RHS) {
126 return LHS.Val == RHS.Val && LHS.Offset == RHS.Offset;
127}
128bool operator<(OffsetValue LHS, OffsetValue RHS) {
129 return std::less<const Value *>()(LHS.Val, RHS.Val) ||
130 (LHS.Val == RHS.Val && LHS.Offset < RHS.Offset);
131}
132
133// A pair that consists of an InstantiatedValue and an offset
134struct OffsetInstantiatedValue {
135 InstantiatedValue IVal;
136 int64_t Offset;
137};
138
139bool operator==(OffsetInstantiatedValue LHS, OffsetInstantiatedValue RHS) {
140 return LHS.IVal == RHS.IVal && LHS.Offset == RHS.Offset;
141}
142
George Burgess IV6d30aa02016-07-15 19:53:25 +0000143// We use ReachabilitySet to keep track of value aliases (The nonterminal "V" in
144// the paper) during the analysis.
145class ReachabilitySet {
George Burgess IV6d30aa02016-07-15 19:53:25 +0000146 typedef DenseMap<InstantiatedValue, StateSet> ValueStateMap;
147 typedef DenseMap<InstantiatedValue, ValueStateMap> ValueReachMap;
148 ValueReachMap ReachMap;
149
150public:
151 typedef ValueStateMap::const_iterator const_valuestate_iterator;
152 typedef ValueReachMap::const_iterator const_value_iterator;
153
154 // Insert edge 'From->To' at state 'State'
155 bool insert(InstantiatedValue From, InstantiatedValue To, MatchState State) {
George Burgess IV3b059842016-07-19 20:47:15 +0000156 assert(From != To);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000157 auto &States = ReachMap[To][From];
158 auto Idx = static_cast<size_t>(State);
159 if (!States.test(Idx)) {
160 States.set(Idx);
161 return true;
162 }
163 return false;
164 }
165
166 // Return the set of all ('From', 'State') pair for a given node 'To'
167 iterator_range<const_valuestate_iterator>
168 reachableValueAliases(InstantiatedValue V) const {
169 auto Itr = ReachMap.find(V);
170 if (Itr == ReachMap.end())
171 return make_range<const_valuestate_iterator>(const_valuestate_iterator(),
172 const_valuestate_iterator());
173 return make_range<const_valuestate_iterator>(Itr->second.begin(),
174 Itr->second.end());
175 }
176
177 iterator_range<const_value_iterator> value_mappings() const {
178 return make_range<const_value_iterator>(ReachMap.begin(), ReachMap.end());
179 }
180};
181
182// We use AliasMemSet to keep track of all memory aliases (the nonterminal "M"
183// in the paper) during the analysis.
184class AliasMemSet {
185 typedef DenseSet<InstantiatedValue> MemSet;
186 typedef DenseMap<InstantiatedValue, MemSet> MemMapType;
187 MemMapType MemMap;
188
189public:
190 typedef MemSet::const_iterator const_mem_iterator;
191
192 bool insert(InstantiatedValue LHS, InstantiatedValue RHS) {
193 // Top-level values can never be memory aliases because one cannot take the
194 // addresses of them
195 assert(LHS.DerefLevel > 0 && RHS.DerefLevel > 0);
196 return MemMap[LHS].insert(RHS).second;
197 }
198
199 const MemSet *getMemoryAliases(InstantiatedValue V) const {
200 auto Itr = MemMap.find(V);
201 if (Itr == MemMap.end())
202 return nullptr;
203 return &Itr->second;
204 }
205};
206
George Burgess IV22682e22016-07-15 20:02:49 +0000207// We use AliasAttrMap to keep track of the AliasAttr of each node.
208class AliasAttrMap {
209 typedef DenseMap<InstantiatedValue, AliasAttrs> MapType;
210 MapType AttrMap;
211
212public:
213 typedef MapType::const_iterator const_iterator;
214
215 bool add(InstantiatedValue V, AliasAttrs Attr) {
216 if (Attr.none())
217 return false;
218 auto &OldAttr = AttrMap[V];
219 auto NewAttr = OldAttr | Attr;
220 if (OldAttr == NewAttr)
221 return false;
222 OldAttr = NewAttr;
223 return true;
224 }
225
226 AliasAttrs getAttrs(InstantiatedValue V) const {
227 AliasAttrs Attr;
228 auto Itr = AttrMap.find(V);
229 if (Itr != AttrMap.end())
230 Attr = Itr->second;
231 return Attr;
232 }
233
234 iterator_range<const_iterator> mappings() const {
235 return make_range<const_iterator>(AttrMap.begin(), AttrMap.end());
236 }
237};
238
George Burgess IV6d30aa02016-07-15 19:53:25 +0000239struct WorkListItem {
240 InstantiatedValue From;
241 InstantiatedValue To;
242 MatchState State;
243};
George Burgess IV3b059842016-07-19 20:47:15 +0000244
245struct ValueSummary {
246 struct Record {
247 InterfaceValue IValue;
248 unsigned DerefLevel;
249 };
250 SmallVector<Record, 4> FromRecords, ToRecords;
251};
George Burgess IV6d30aa02016-07-15 19:53:25 +0000252}
253
George Burgess IV4ec17532016-07-22 22:30:48 +0000254namespace llvm {
255// Specialize DenseMapInfo for OffsetValue.
256template <> struct DenseMapInfo<OffsetValue> {
257 static OffsetValue getEmptyKey() {
258 return OffsetValue{DenseMapInfo<const Value *>::getEmptyKey(),
259 DenseMapInfo<int64_t>::getEmptyKey()};
260 }
261 static OffsetValue getTombstoneKey() {
262 return OffsetValue{DenseMapInfo<const Value *>::getTombstoneKey(),
263 DenseMapInfo<int64_t>::getEmptyKey()};
264 }
265 static unsigned getHashValue(const OffsetValue &OVal) {
266 return DenseMapInfo<std::pair<const Value *, int64_t>>::getHashValue(
267 std::make_pair(OVal.Val, OVal.Offset));
268 }
269 static bool isEqual(const OffsetValue &LHS, const OffsetValue &RHS) {
270 return LHS == RHS;
271 }
272};
273
274// Specialize DenseMapInfo for OffsetInstantiatedValue.
275template <> struct DenseMapInfo<OffsetInstantiatedValue> {
276 static OffsetInstantiatedValue getEmptyKey() {
277 return OffsetInstantiatedValue{
278 DenseMapInfo<InstantiatedValue>::getEmptyKey(),
279 DenseMapInfo<int64_t>::getEmptyKey()};
280 }
281 static OffsetInstantiatedValue getTombstoneKey() {
282 return OffsetInstantiatedValue{
283 DenseMapInfo<InstantiatedValue>::getTombstoneKey(),
284 DenseMapInfo<int64_t>::getEmptyKey()};
285 }
286 static unsigned getHashValue(const OffsetInstantiatedValue &OVal) {
287 return DenseMapInfo<std::pair<InstantiatedValue, int64_t>>::getHashValue(
288 std::make_pair(OVal.IVal, OVal.Offset));
289 }
290 static bool isEqual(const OffsetInstantiatedValue &LHS,
291 const OffsetInstantiatedValue &RHS) {
292 return LHS == RHS;
293 }
294};
295}
296
George Burgess IV6d30aa02016-07-15 19:53:25 +0000297class CFLAndersAAResult::FunctionInfo {
298 /// Map a value to other values that may alias it
299 /// Since the alias relation is symmetric, to save some space we assume values
300 /// are properly ordered: if a and b alias each other, and a < b, then b is in
301 /// AliasMap[a] but not vice versa.
George Burgess IV4ec17532016-07-22 22:30:48 +0000302 DenseMap<const Value *, std::vector<OffsetValue>> AliasMap;
George Burgess IV6d30aa02016-07-15 19:53:25 +0000303
George Burgess IV22682e22016-07-15 20:02:49 +0000304 /// Map a value to its corresponding AliasAttrs
305 DenseMap<const Value *, AliasAttrs> AttrMap;
306
George Burgess IV6d30aa02016-07-15 19:53:25 +0000307 /// Summary of externally visible effects.
308 AliasSummary Summary;
309
George Burgess IV22682e22016-07-15 20:02:49 +0000310 AliasAttrs getAttrs(const Value *) const;
311
George Burgess IV6d30aa02016-07-15 19:53:25 +0000312public:
George Burgess IV3b059842016-07-19 20:47:15 +0000313 FunctionInfo(const Function &, const SmallVectorImpl<Value *> &,
314 const ReachabilitySet &, AliasAttrMap);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000315
George Burgess IV4ec17532016-07-22 22:30:48 +0000316 bool mayAlias(const Value *, uint64_t, const Value *, uint64_t) const;
George Burgess IV6d30aa02016-07-15 19:53:25 +0000317 const AliasSummary &getAliasSummary() const { return Summary; }
318};
319
George Burgess IV3b059842016-07-19 20:47:15 +0000320static bool hasReadOnlyState(StateSet Set) {
George Burgess IV22a0f1a2016-07-19 21:35:47 +0000321 return (Set & StateSet(ReadOnlyStateMask)).any();
George Burgess IV3b059842016-07-19 20:47:15 +0000322}
323
324static bool hasWriteOnlyState(StateSet Set) {
George Burgess IV22a0f1a2016-07-19 21:35:47 +0000325 return (Set & StateSet(WriteOnlyStateMask)).any();
George Burgess IV3b059842016-07-19 20:47:15 +0000326}
327
328static Optional<InterfaceValue>
329getInterfaceValue(InstantiatedValue IValue,
330 const SmallVectorImpl<Value *> &RetVals) {
331 auto Val = IValue.Val;
332
333 Optional<unsigned> Index;
334 if (auto Arg = dyn_cast<Argument>(Val))
335 Index = Arg->getArgNo() + 1;
336 else if (is_contained(RetVals, Val))
337 Index = 0;
338
339 if (Index)
340 return InterfaceValue{*Index, IValue.DerefLevel};
341 return None;
342}
343
344static void populateAttrMap(DenseMap<const Value *, AliasAttrs> &AttrMap,
345 const AliasAttrMap &AMap) {
George Burgess IV22682e22016-07-15 20:02:49 +0000346 for (const auto &Mapping : AMap.mappings()) {
347 auto IVal = Mapping.first;
348
349 // AttrMap only cares about top-level values
350 if (IVal.DerefLevel == 0)
George Burgess IV4ec17532016-07-22 22:30:48 +0000351 AttrMap[IVal.Val] |= Mapping.second;
George Burgess IV22682e22016-07-15 20:02:49 +0000352 }
George Burgess IV3b059842016-07-19 20:47:15 +0000353}
George Burgess IV22682e22016-07-15 20:02:49 +0000354
George Burgess IV3b059842016-07-19 20:47:15 +0000355static void
George Burgess IV4ec17532016-07-22 22:30:48 +0000356populateAliasMap(DenseMap<const Value *, std::vector<OffsetValue>> &AliasMap,
George Burgess IV3b059842016-07-19 20:47:15 +0000357 const ReachabilitySet &ReachSet) {
George Burgess IV6d30aa02016-07-15 19:53:25 +0000358 for (const auto &OuterMapping : ReachSet.value_mappings()) {
359 // AliasMap only cares about top-level values
360 if (OuterMapping.first.DerefLevel > 0)
361 continue;
362
363 auto Val = OuterMapping.first.Val;
364 auto &AliasList = AliasMap[Val];
365 for (const auto &InnerMapping : OuterMapping.second) {
366 // Again, AliasMap only cares about top-level values
367 if (InnerMapping.first.DerefLevel == 0)
George Burgess IV4ec17532016-07-22 22:30:48 +0000368 AliasList.push_back(OffsetValue{InnerMapping.first.Val, UnknownOffset});
George Burgess IV6d30aa02016-07-15 19:53:25 +0000369 }
370
371 // Sort AliasList for faster lookup
George Burgess IV4ec17532016-07-22 22:30:48 +0000372 std::sort(AliasList.begin(), AliasList.end());
George Burgess IV6d30aa02016-07-15 19:53:25 +0000373 }
George Burgess IV3b059842016-07-19 20:47:15 +0000374}
George Burgess IV6d30aa02016-07-15 19:53:25 +0000375
George Burgess IV3b059842016-07-19 20:47:15 +0000376static void populateExternalRelations(
377 SmallVectorImpl<ExternalRelation> &ExtRelations, const Function &Fn,
378 const SmallVectorImpl<Value *> &RetVals, const ReachabilitySet &ReachSet) {
379 // If a function only returns one of its argument X, then X will be both an
380 // argument and a return value at the same time. This is an edge case that
381 // needs special handling here.
382 for (const auto &Arg : Fn.args()) {
383 if (is_contained(RetVals, &Arg)) {
384 auto ArgVal = InterfaceValue{Arg.getArgNo() + 1, 0};
385 auto RetVal = InterfaceValue{0, 0};
George Burgess IV4ec17532016-07-22 22:30:48 +0000386 ExtRelations.push_back(ExternalRelation{ArgVal, RetVal, 0});
George Burgess IV3b059842016-07-19 20:47:15 +0000387 }
388 }
389
390 // Below is the core summary construction logic.
391 // A naive solution of adding only the value aliases that are parameters or
392 // return values in ReachSet to the summary won't work: It is possible that a
393 // parameter P is written into an intermediate value I, and the function
394 // subsequently returns *I. In that case, *I is does not value alias anything
395 // in ReachSet, and the naive solution will miss a summary edge from (P, 1) to
396 // (I, 1).
397 // To account for the aforementioned case, we need to check each non-parameter
398 // and non-return value for the possibility of acting as an intermediate.
399 // 'ValueMap' here records, for each value, which InterfaceValues read from or
400 // write into it. If both the read list and the write list of a given value
401 // are non-empty, we know that a particular value is an intermidate and we
402 // need to add summary edges from the writes to the reads.
403 DenseMap<Value *, ValueSummary> ValueMap;
404 for (const auto &OuterMapping : ReachSet.value_mappings()) {
405 if (auto Dst = getInterfaceValue(OuterMapping.first, RetVals)) {
406 for (const auto &InnerMapping : OuterMapping.second) {
407 // If Src is a param/return value, we get a same-level assignment.
408 if (auto Src = getInterfaceValue(InnerMapping.first, RetVals)) {
409 // This may happen if both Dst and Src are return values
410 if (*Dst == *Src)
411 continue;
412
413 if (hasReadOnlyState(InnerMapping.second))
George Burgess IV4ec17532016-07-22 22:30:48 +0000414 ExtRelations.push_back(ExternalRelation{*Dst, *Src, UnknownOffset});
George Burgess IV3b059842016-07-19 20:47:15 +0000415 // No need to check for WriteOnly state, since ReachSet is symmetric
416 } else {
417 // If Src is not a param/return, add it to ValueMap
418 auto SrcIVal = InnerMapping.first;
419 if (hasReadOnlyState(InnerMapping.second))
420 ValueMap[SrcIVal.Val].FromRecords.push_back(
421 ValueSummary::Record{*Dst, SrcIVal.DerefLevel});
422 if (hasWriteOnlyState(InnerMapping.second))
423 ValueMap[SrcIVal.Val].ToRecords.push_back(
424 ValueSummary::Record{*Dst, SrcIVal.DerefLevel});
425 }
426 }
427 }
428 }
429
430 for (const auto &Mapping : ValueMap) {
431 for (const auto &FromRecord : Mapping.second.FromRecords) {
432 for (const auto &ToRecord : Mapping.second.ToRecords) {
433 auto ToLevel = ToRecord.DerefLevel;
434 auto FromLevel = FromRecord.DerefLevel;
435 // Same-level assignments should have already been processed by now
436 if (ToLevel == FromLevel)
437 continue;
438
439 auto SrcIndex = FromRecord.IValue.Index;
440 auto SrcLevel = FromRecord.IValue.DerefLevel;
441 auto DstIndex = ToRecord.IValue.Index;
442 auto DstLevel = ToRecord.IValue.DerefLevel;
443 if (ToLevel > FromLevel)
444 SrcLevel += ToLevel - FromLevel;
445 else
446 DstLevel += FromLevel - ToLevel;
447
George Burgess IV4ec17532016-07-22 22:30:48 +0000448 ExtRelations.push_back(ExternalRelation{
449 InterfaceValue{SrcIndex, SrcLevel},
450 InterfaceValue{DstIndex, DstLevel}, UnknownOffset});
George Burgess IV3b059842016-07-19 20:47:15 +0000451 }
452 }
453 }
454
455 // Remove duplicates in ExtRelations
456 std::sort(ExtRelations.begin(), ExtRelations.end());
457 ExtRelations.erase(std::unique(ExtRelations.begin(), ExtRelations.end()),
458 ExtRelations.end());
459}
460
461static void populateExternalAttributes(
462 SmallVectorImpl<ExternalAttribute> &ExtAttributes, const Function &Fn,
463 const SmallVectorImpl<Value *> &RetVals, const AliasAttrMap &AMap) {
464 for (const auto &Mapping : AMap.mappings()) {
465 if (auto IVal = getInterfaceValue(Mapping.first, RetVals)) {
466 auto Attr = getExternallyVisibleAttrs(Mapping.second);
467 if (Attr.any())
468 ExtAttributes.push_back(ExternalAttribute{*IVal, Attr});
469 }
470 }
471}
472
473CFLAndersAAResult::FunctionInfo::FunctionInfo(
474 const Function &Fn, const SmallVectorImpl<Value *> &RetVals,
475 const ReachabilitySet &ReachSet, AliasAttrMap AMap) {
476 populateAttrMap(AttrMap, AMap);
477 populateExternalAttributes(Summary.RetParamAttributes, Fn, RetVals, AMap);
478 populateAliasMap(AliasMap, ReachSet);
479 populateExternalRelations(Summary.RetParamRelations, Fn, RetVals, ReachSet);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000480}
481
George Burgess IV22682e22016-07-15 20:02:49 +0000482AliasAttrs CFLAndersAAResult::FunctionInfo::getAttrs(const Value *V) const {
483 assert(V != nullptr);
484
485 AliasAttrs Attr;
486 auto Itr = AttrMap.find(V);
487 if (Itr != AttrMap.end())
488 Attr = Itr->second;
489 return Attr;
490}
491
George Burgess IV6d30aa02016-07-15 19:53:25 +0000492bool CFLAndersAAResult::FunctionInfo::mayAlias(const Value *LHS,
George Burgess IV4ec17532016-07-22 22:30:48 +0000493 uint64_t LHSSize,
494 const Value *RHS,
495 uint64_t RHSSize) const {
George Burgess IV6d30aa02016-07-15 19:53:25 +0000496 assert(LHS && RHS);
497
George Burgess IV4ec17532016-07-22 22:30:48 +0000498 // Check AliasAttrs first since it's cheaper
499 auto AttrsA = getAttrs(LHS);
500 auto AttrsB = getAttrs(RHS);
501 if (hasUnknownOrCallerAttr(AttrsA))
502 return AttrsB.any();
503 if (hasUnknownOrCallerAttr(AttrsB))
504 return AttrsA.any();
505 if (isGlobalOrArgAttr(AttrsA))
506 return isGlobalOrArgAttr(AttrsB);
507 if (isGlobalOrArgAttr(AttrsB))
508 return isGlobalOrArgAttr(AttrsA);
509
510 // At this point both LHS and RHS should point to locally allocated objects
511
George Burgess IV6d30aa02016-07-15 19:53:25 +0000512 auto Itr = AliasMap.find(LHS);
George Burgess IV22682e22016-07-15 20:02:49 +0000513 if (Itr != AliasMap.end()) {
George Burgess IV4ec17532016-07-22 22:30:48 +0000514
515 // Find out all (X, Offset) where X == RHS
516 auto Comparator = [](OffsetValue LHS, OffsetValue RHS) {
517 return std::less<const Value *>()(LHS.Val, RHS.Val);
518 };
519#ifdef EXPENSIVE_CHECKS
520 assert(std::is_sorted(Itr->second.begin(), Itr->second.end(), Comparator));
521#endif
522 auto RangePair = std::equal_range(Itr->second.begin(), Itr->second.end(),
523 OffsetValue{RHS, 0}, Comparator);
524
525 if (RangePair.first != RangePair.second) {
526 // Be conservative about UnknownSize
527 if (LHSSize == MemoryLocation::UnknownSize ||
528 RHSSize == MemoryLocation::UnknownSize)
529 return true;
530
531 for (const auto &OVal : make_range(RangePair)) {
532 // Be conservative about UnknownOffset
533 if (OVal.Offset == UnknownOffset)
534 return true;
535
536 // We know that LHS aliases (RHS + OVal.Offset) if the control flow
537 // reaches here. The may-alias query essentially becomes integer
538 // range-overlap queries over two ranges [OVal.Offset, OVal.Offset +
539 // LHSSize) and [0, RHSSize).
540
541 // Try to be conservative on super large offsets
542 if (LLVM_UNLIKELY(LHSSize > INT64_MAX || RHSSize > INT64_MAX))
543 return true;
544
545 auto LHSStart = OVal.Offset;
546 // FIXME: Do we need to guard against integer overflow?
547 auto LHSEnd = OVal.Offset + static_cast<int64_t>(LHSSize);
548 auto RHSStart = 0;
549 auto RHSEnd = static_cast<int64_t>(RHSSize);
550 if (LHSEnd > RHSStart && LHSStart < RHSEnd)
551 return true;
552 }
553 }
George Burgess IV22682e22016-07-15 20:02:49 +0000554 }
555
George Burgess IV22682e22016-07-15 20:02:49 +0000556 return false;
George Burgess IV6d30aa02016-07-15 19:53:25 +0000557}
558
559static void propagate(InstantiatedValue From, InstantiatedValue To,
560 MatchState State, ReachabilitySet &ReachSet,
561 std::vector<WorkListItem> &WorkList) {
562 if (From == To)
563 return;
564 if (ReachSet.insert(From, To, State))
565 WorkList.push_back(WorkListItem{From, To, State});
566}
567
568static void initializeWorkList(std::vector<WorkListItem> &WorkList,
569 ReachabilitySet &ReachSet,
570 const CFLGraph &Graph) {
571 for (const auto &Mapping : Graph.value_mappings()) {
572 auto Val = Mapping.first;
573 auto &ValueInfo = Mapping.second;
574 assert(ValueInfo.getNumLevels() > 0);
575
576 // Insert all immediate assignment neighbors to the worklist
577 for (unsigned I = 0, E = ValueInfo.getNumLevels(); I < E; ++I) {
578 auto Src = InstantiatedValue{Val, I};
579 // If there's an assignment edge from X to Y, it means Y is reachable from
580 // X at S2 and X is reachable from Y at S1
581 for (auto &Edge : ValueInfo.getNodeInfoAtLevel(I).Edges) {
George Burgess IVc01b42f2016-07-19 20:38:21 +0000582 propagate(Edge.Other, Src, MatchState::FlowFromReadOnly, ReachSet,
583 WorkList);
584 propagate(Src, Edge.Other, MatchState::FlowToWriteOnly, ReachSet,
585 WorkList);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000586 }
587 }
588 }
589}
590
591static Optional<InstantiatedValue> getNodeBelow(const CFLGraph &Graph,
592 InstantiatedValue V) {
593 auto NodeBelow = InstantiatedValue{V.Val, V.DerefLevel + 1};
594 if (Graph.getNode(NodeBelow))
595 return NodeBelow;
596 return None;
597}
598
599static void processWorkListItem(const WorkListItem &Item, const CFLGraph &Graph,
600 ReachabilitySet &ReachSet, AliasMemSet &MemSet,
601 std::vector<WorkListItem> &WorkList) {
602 auto FromNode = Item.From;
603 auto ToNode = Item.To;
604
605 auto NodeInfo = Graph.getNode(ToNode);
606 assert(NodeInfo != nullptr);
607
George Burgess IV6d30aa02016-07-15 19:53:25 +0000608 // TODO: propagate field offsets
609
610 // FIXME: Here is a neat trick we can do: since both ReachSet and MemSet holds
611 // relations that are symmetric, we could actually cut the storage by half by
612 // sorting FromNode and ToNode before insertion happens.
613
614 // The newly added value alias pair may pontentially generate more memory
615 // alias pairs. Check for them here.
616 auto FromNodeBelow = getNodeBelow(Graph, FromNode);
617 auto ToNodeBelow = getNodeBelow(Graph, ToNode);
618 if (FromNodeBelow && ToNodeBelow &&
619 MemSet.insert(*FromNodeBelow, *ToNodeBelow)) {
George Burgess IVc01b42f2016-07-19 20:38:21 +0000620 propagate(*FromNodeBelow, *ToNodeBelow,
621 MatchState::FlowFromMemAliasNoReadWrite, ReachSet, WorkList);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000622 for (const auto &Mapping : ReachSet.reachableValueAliases(*FromNodeBelow)) {
623 auto Src = Mapping.first;
George Burgess IVc01b42f2016-07-19 20:38:21 +0000624 auto MemAliasPropagate = [&](MatchState FromState, MatchState ToState) {
625 if (Mapping.second.test(static_cast<size_t>(FromState)))
626 propagate(Src, *ToNodeBelow, ToState, ReachSet, WorkList);
627 };
628
629 MemAliasPropagate(MatchState::FlowFromReadOnly,
630 MatchState::FlowFromMemAliasReadOnly);
631 MemAliasPropagate(MatchState::FlowToWriteOnly,
632 MatchState::FlowToMemAliasWriteOnly);
633 MemAliasPropagate(MatchState::FlowToReadWrite,
634 MatchState::FlowToMemAliasReadWrite);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000635 }
636 }
637
638 // This is the core of the state machine walking algorithm. We expand ReachSet
639 // based on which state we are at (which in turn dictates what edges we
640 // should examine)
641 // From a high-level point of view, the state machine here guarantees two
642 // properties:
643 // - If *X and *Y are memory aliases, then X and Y are value aliases
644 // - If Y is an alias of X, then reverse assignment edges (if there is any)
645 // should precede any assignment edges on the path from X to Y.
George Burgess IVc01b42f2016-07-19 20:38:21 +0000646 auto NextAssignState = [&](MatchState State) {
647 for (const auto &AssignEdge : NodeInfo->Edges)
648 propagate(FromNode, AssignEdge.Other, State, ReachSet, WorkList);
649 };
650 auto NextRevAssignState = [&](MatchState State) {
651 for (const auto &RevAssignEdge : NodeInfo->ReverseEdges)
652 propagate(FromNode, RevAssignEdge.Other, State, ReachSet, WorkList);
653 };
654 auto NextMemState = [&](MatchState State) {
655 if (auto AliasSet = MemSet.getMemoryAliases(ToNode)) {
656 for (const auto &MemAlias : *AliasSet)
657 propagate(FromNode, MemAlias, State, ReachSet, WorkList);
658 }
659 };
660
George Burgess IV6d30aa02016-07-15 19:53:25 +0000661 switch (Item.State) {
George Burgess IVc01b42f2016-07-19 20:38:21 +0000662 case MatchState::FlowFromReadOnly: {
663 NextRevAssignState(MatchState::FlowFromReadOnly);
664 NextAssignState(MatchState::FlowToReadWrite);
665 NextMemState(MatchState::FlowFromMemAliasReadOnly);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000666 break;
667 }
George Burgess IVc01b42f2016-07-19 20:38:21 +0000668 case MatchState::FlowFromMemAliasNoReadWrite: {
669 NextRevAssignState(MatchState::FlowFromReadOnly);
670 NextAssignState(MatchState::FlowToWriteOnly);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000671 break;
672 }
George Burgess IVc01b42f2016-07-19 20:38:21 +0000673 case MatchState::FlowFromMemAliasReadOnly: {
674 NextRevAssignState(MatchState::FlowFromReadOnly);
675 NextAssignState(MatchState::FlowToReadWrite);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000676 break;
677 }
George Burgess IVc01b42f2016-07-19 20:38:21 +0000678 case MatchState::FlowToWriteOnly: {
679 NextAssignState(MatchState::FlowToWriteOnly);
680 NextMemState(MatchState::FlowToMemAliasWriteOnly);
681 break;
682 }
683 case MatchState::FlowToReadWrite: {
684 NextAssignState(MatchState::FlowToReadWrite);
685 NextMemState(MatchState::FlowToMemAliasReadWrite);
686 break;
687 }
688 case MatchState::FlowToMemAliasWriteOnly: {
689 NextAssignState(MatchState::FlowToWriteOnly);
690 break;
691 }
692 case MatchState::FlowToMemAliasReadWrite: {
693 NextAssignState(MatchState::FlowToReadWrite);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000694 break;
695 }
696 }
697}
698
George Burgess IV22682e22016-07-15 20:02:49 +0000699static AliasAttrMap buildAttrMap(const CFLGraph &Graph,
700 const ReachabilitySet &ReachSet) {
701 AliasAttrMap AttrMap;
702 std::vector<InstantiatedValue> WorkList, NextList;
703
704 // Initialize each node with its original AliasAttrs in CFLGraph
705 for (const auto &Mapping : Graph.value_mappings()) {
706 auto Val = Mapping.first;
707 auto &ValueInfo = Mapping.second;
708 for (unsigned I = 0, E = ValueInfo.getNumLevels(); I < E; ++I) {
709 auto Node = InstantiatedValue{Val, I};
710 AttrMap.add(Node, ValueInfo.getNodeInfoAtLevel(I).Attr);
711 WorkList.push_back(Node);
712 }
713 }
714
715 while (!WorkList.empty()) {
716 for (const auto &Dst : WorkList) {
717 auto DstAttr = AttrMap.getAttrs(Dst);
718 if (DstAttr.none())
719 continue;
720
721 // Propagate attr on the same level
722 for (const auto &Mapping : ReachSet.reachableValueAliases(Dst)) {
723 auto Src = Mapping.first;
724 if (AttrMap.add(Src, DstAttr))
725 NextList.push_back(Src);
726 }
727
728 // Propagate attr to the levels below
729 auto DstBelow = getNodeBelow(Graph, Dst);
730 while (DstBelow) {
731 if (AttrMap.add(*DstBelow, DstAttr)) {
732 NextList.push_back(*DstBelow);
733 break;
734 }
735 DstBelow = getNodeBelow(Graph, *DstBelow);
736 }
737 }
738 WorkList.swap(NextList);
739 NextList.clear();
740 }
741
742 return AttrMap;
743}
744
George Burgess IV6d30aa02016-07-15 19:53:25 +0000745CFLAndersAAResult::FunctionInfo
746CFLAndersAAResult::buildInfoFrom(const Function &Fn) {
747 CFLGraphBuilder<CFLAndersAAResult> GraphBuilder(
748 *this, TLI,
749 // Cast away the constness here due to GraphBuilder's API requirement
750 const_cast<Function &>(Fn));
751 auto &Graph = GraphBuilder.getCFLGraph();
752
753 ReachabilitySet ReachSet;
754 AliasMemSet MemSet;
755
756 std::vector<WorkListItem> WorkList, NextList;
757 initializeWorkList(WorkList, ReachSet, Graph);
758 // TODO: make sure we don't stop before the fix point is reached
759 while (!WorkList.empty()) {
760 for (const auto &Item : WorkList)
761 processWorkListItem(Item, Graph, ReachSet, MemSet, NextList);
762
763 NextList.swap(WorkList);
764 NextList.clear();
765 }
766
George Burgess IV22682e22016-07-15 20:02:49 +0000767 // Now that we have all the reachability info, propagate AliasAttrs according
768 // to it
769 auto IValueAttrMap = buildAttrMap(Graph, ReachSet);
770
George Burgess IV3b059842016-07-19 20:47:15 +0000771 return FunctionInfo(Fn, GraphBuilder.getReturnValues(), ReachSet,
772 std::move(IValueAttrMap));
George Burgess IV6d30aa02016-07-15 19:53:25 +0000773}
774
775void CFLAndersAAResult::scan(const Function &Fn) {
776 auto InsertPair = Cache.insert(std::make_pair(&Fn, Optional<FunctionInfo>()));
777 (void)InsertPair;
778 assert(InsertPair.second &&
779 "Trying to scan a function that has already been cached");
780
781 // Note that we can't do Cache[Fn] = buildSetsFrom(Fn) here: the function call
782 // may get evaluated after operator[], potentially triggering a DenseMap
783 // resize and invalidating the reference returned by operator[]
784 auto FunInfo = buildInfoFrom(Fn);
785 Cache[&Fn] = std::move(FunInfo);
786 Handles.push_front(FunctionHandle(const_cast<Function *>(&Fn), this));
787}
788
789void CFLAndersAAResult::evict(const Function &Fn) { Cache.erase(&Fn); }
790
791const Optional<CFLAndersAAResult::FunctionInfo> &
792CFLAndersAAResult::ensureCached(const Function &Fn) {
793 auto Iter = Cache.find(&Fn);
794 if (Iter == Cache.end()) {
795 scan(Fn);
796 Iter = Cache.find(&Fn);
797 assert(Iter != Cache.end());
798 assert(Iter->second.hasValue());
799 }
800 return Iter->second;
801}
802
803const AliasSummary *CFLAndersAAResult::getAliasSummary(const Function &Fn) {
804 auto &FunInfo = ensureCached(Fn);
805 if (FunInfo.hasValue())
806 return &FunInfo->getAliasSummary();
807 else
808 return nullptr;
809}
810
811AliasResult CFLAndersAAResult::query(const MemoryLocation &LocA,
812 const MemoryLocation &LocB) {
813 auto *ValA = LocA.Ptr;
814 auto *ValB = LocB.Ptr;
815
816 if (!ValA->getType()->isPointerTy() || !ValB->getType()->isPointerTy())
817 return NoAlias;
818
819 auto *Fn = parentFunctionOfValue(ValA);
820 if (!Fn) {
821 Fn = parentFunctionOfValue(ValB);
822 if (!Fn) {
823 // The only times this is known to happen are when globals + InlineAsm are
824 // involved
825 DEBUG(dbgs()
826 << "CFLAndersAA: could not extract parent function information.\n");
827 return MayAlias;
828 }
829 } else {
830 assert(!parentFunctionOfValue(ValB) || parentFunctionOfValue(ValB) == Fn);
831 }
832
833 assert(Fn != nullptr);
834 auto &FunInfo = ensureCached(*Fn);
835
836 // AliasMap lookup
George Burgess IV4ec17532016-07-22 22:30:48 +0000837 if (FunInfo->mayAlias(ValA, LocA.Size, ValB, LocB.Size))
George Burgess IV6d30aa02016-07-15 19:53:25 +0000838 return MayAlias;
839 return NoAlias;
840}
841
842AliasResult CFLAndersAAResult::alias(const MemoryLocation &LocA,
843 const MemoryLocation &LocB) {
844 if (LocA.Ptr == LocB.Ptr)
845 return LocA.Size == LocB.Size ? MustAlias : PartialAlias;
846
847 // Comparisons between global variables and other constants should be
848 // handled by BasicAA.
849 // CFLAndersAA may report NoAlias when comparing a GlobalValue and
850 // ConstantExpr, but every query needs to have at least one Value tied to a
851 // Function, and neither GlobalValues nor ConstantExprs are.
852 if (isa<Constant>(LocA.Ptr) && isa<Constant>(LocB.Ptr))
853 return AAResultBase::alias(LocA, LocB);
854
855 AliasResult QueryResult = query(LocA, LocB);
856 if (QueryResult == MayAlias)
857 return AAResultBase::alias(LocA, LocB);
858
859 return QueryResult;
860}
George Burgess IVbfa401e2016-07-06 00:26:41 +0000861
862char CFLAndersAA::PassID;
863
864CFLAndersAAResult CFLAndersAA::run(Function &F, AnalysisManager<Function> &AM) {
George Burgess IV6d30aa02016-07-15 19:53:25 +0000865 return CFLAndersAAResult(AM.getResult<TargetLibraryAnalysis>(F));
George Burgess IVbfa401e2016-07-06 00:26:41 +0000866}
867
868char CFLAndersAAWrapperPass::ID = 0;
869INITIALIZE_PASS(CFLAndersAAWrapperPass, "cfl-anders-aa",
870 "Inclusion-Based CFL Alias Analysis", false, true)
871
872ImmutablePass *llvm::createCFLAndersAAWrapperPass() {
873 return new CFLAndersAAWrapperPass();
874}
875
876CFLAndersAAWrapperPass::CFLAndersAAWrapperPass() : ImmutablePass(ID) {
877 initializeCFLAndersAAWrapperPassPass(*PassRegistry::getPassRegistry());
878}
879
George Burgess IV6d30aa02016-07-15 19:53:25 +0000880void CFLAndersAAWrapperPass::initializePass() {
881 auto &TLIWP = getAnalysis<TargetLibraryInfoWrapperPass>();
882 Result.reset(new CFLAndersAAResult(TLIWP.getTLI()));
883}
George Burgess IVbfa401e2016-07-06 00:26:41 +0000884
885void CFLAndersAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
886 AU.setPreservesAll();
George Burgess IV6d30aa02016-07-15 19:53:25 +0000887 AU.addRequired<TargetLibraryInfoWrapperPass>();
George Burgess IVbfa401e2016-07-06 00:26:41 +0000888}