blob: 3925b9c6451d35de53efed46040bd459c5e22855 [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;
110LLVM_CONSTEXPR StateSet ReadOnlyStateMask =
111 (1 << static_cast<uint8_t>(MatchState::FlowFromReadOnly)) |
112 (1 << static_cast<uint8_t>(MatchState::FlowFromMemAliasReadOnly));
113LLVM_CONSTEXPR StateSet WriteOnlyStateMask =
114 (1 << static_cast<uint8_t>(MatchState::FlowToWriteOnly)) |
115 (1 << static_cast<uint8_t>(MatchState::FlowToMemAliasWriteOnly));
116
George Burgess IV6d30aa02016-07-15 19:53:25 +0000117// We use ReachabilitySet to keep track of value aliases (The nonterminal "V" in
118// the paper) during the analysis.
119class ReachabilitySet {
George Burgess IV6d30aa02016-07-15 19:53:25 +0000120 typedef DenseMap<InstantiatedValue, StateSet> ValueStateMap;
121 typedef DenseMap<InstantiatedValue, ValueStateMap> ValueReachMap;
122 ValueReachMap ReachMap;
123
124public:
125 typedef ValueStateMap::const_iterator const_valuestate_iterator;
126 typedef ValueReachMap::const_iterator const_value_iterator;
127
128 // Insert edge 'From->To' at state 'State'
129 bool insert(InstantiatedValue From, InstantiatedValue To, MatchState State) {
George Burgess IV3b059842016-07-19 20:47:15 +0000130 assert(From != To);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000131 auto &States = ReachMap[To][From];
132 auto Idx = static_cast<size_t>(State);
133 if (!States.test(Idx)) {
134 States.set(Idx);
135 return true;
136 }
137 return false;
138 }
139
140 // Return the set of all ('From', 'State') pair for a given node 'To'
141 iterator_range<const_valuestate_iterator>
142 reachableValueAliases(InstantiatedValue V) const {
143 auto Itr = ReachMap.find(V);
144 if (Itr == ReachMap.end())
145 return make_range<const_valuestate_iterator>(const_valuestate_iterator(),
146 const_valuestate_iterator());
147 return make_range<const_valuestate_iterator>(Itr->second.begin(),
148 Itr->second.end());
149 }
150
151 iterator_range<const_value_iterator> value_mappings() const {
152 return make_range<const_value_iterator>(ReachMap.begin(), ReachMap.end());
153 }
154};
155
156// We use AliasMemSet to keep track of all memory aliases (the nonterminal "M"
157// in the paper) during the analysis.
158class AliasMemSet {
159 typedef DenseSet<InstantiatedValue> MemSet;
160 typedef DenseMap<InstantiatedValue, MemSet> MemMapType;
161 MemMapType MemMap;
162
163public:
164 typedef MemSet::const_iterator const_mem_iterator;
165
166 bool insert(InstantiatedValue LHS, InstantiatedValue RHS) {
167 // Top-level values can never be memory aliases because one cannot take the
168 // addresses of them
169 assert(LHS.DerefLevel > 0 && RHS.DerefLevel > 0);
170 return MemMap[LHS].insert(RHS).second;
171 }
172
173 const MemSet *getMemoryAliases(InstantiatedValue V) const {
174 auto Itr = MemMap.find(V);
175 if (Itr == MemMap.end())
176 return nullptr;
177 return &Itr->second;
178 }
179};
180
George Burgess IV22682e22016-07-15 20:02:49 +0000181// We use AliasAttrMap to keep track of the AliasAttr of each node.
182class AliasAttrMap {
183 typedef DenseMap<InstantiatedValue, AliasAttrs> MapType;
184 MapType AttrMap;
185
186public:
187 typedef MapType::const_iterator const_iterator;
188
189 bool add(InstantiatedValue V, AliasAttrs Attr) {
190 if (Attr.none())
191 return false;
192 auto &OldAttr = AttrMap[V];
193 auto NewAttr = OldAttr | Attr;
194 if (OldAttr == NewAttr)
195 return false;
196 OldAttr = NewAttr;
197 return true;
198 }
199
200 AliasAttrs getAttrs(InstantiatedValue V) const {
201 AliasAttrs Attr;
202 auto Itr = AttrMap.find(V);
203 if (Itr != AttrMap.end())
204 Attr = Itr->second;
205 return Attr;
206 }
207
208 iterator_range<const_iterator> mappings() const {
209 return make_range<const_iterator>(AttrMap.begin(), AttrMap.end());
210 }
211};
212
George Burgess IV6d30aa02016-07-15 19:53:25 +0000213struct WorkListItem {
214 InstantiatedValue From;
215 InstantiatedValue To;
216 MatchState State;
217};
George Burgess IV3b059842016-07-19 20:47:15 +0000218
219struct ValueSummary {
220 struct Record {
221 InterfaceValue IValue;
222 unsigned DerefLevel;
223 };
224 SmallVector<Record, 4> FromRecords, ToRecords;
225};
George Burgess IV6d30aa02016-07-15 19:53:25 +0000226}
227
228class CFLAndersAAResult::FunctionInfo {
229 /// Map a value to other values that may alias it
230 /// Since the alias relation is symmetric, to save some space we assume values
231 /// are properly ordered: if a and b alias each other, and a < b, then b is in
232 /// AliasMap[a] but not vice versa.
233 DenseMap<const Value *, std::vector<const Value *>> AliasMap;
234
George Burgess IV22682e22016-07-15 20:02:49 +0000235 /// Map a value to its corresponding AliasAttrs
236 DenseMap<const Value *, AliasAttrs> AttrMap;
237
George Burgess IV6d30aa02016-07-15 19:53:25 +0000238 /// Summary of externally visible effects.
239 AliasSummary Summary;
240
George Burgess IV22682e22016-07-15 20:02:49 +0000241 AliasAttrs getAttrs(const Value *) const;
242
George Burgess IV6d30aa02016-07-15 19:53:25 +0000243public:
George Burgess IV3b059842016-07-19 20:47:15 +0000244 FunctionInfo(const Function &, const SmallVectorImpl<Value *> &,
245 const ReachabilitySet &, AliasAttrMap);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000246
247 bool mayAlias(const Value *LHS, const Value *RHS) const;
248 const AliasSummary &getAliasSummary() const { return Summary; }
249};
250
George Burgess IV3b059842016-07-19 20:47:15 +0000251static bool hasReadOnlyState(StateSet Set) {
252 return (Set & ReadOnlyStateMask).any();
253}
254
255static bool hasWriteOnlyState(StateSet Set) {
256 return (Set & WriteOnlyStateMask).any();
257}
258
259static Optional<InterfaceValue>
260getInterfaceValue(InstantiatedValue IValue,
261 const SmallVectorImpl<Value *> &RetVals) {
262 auto Val = IValue.Val;
263
264 Optional<unsigned> Index;
265 if (auto Arg = dyn_cast<Argument>(Val))
266 Index = Arg->getArgNo() + 1;
267 else if (is_contained(RetVals, Val))
268 Index = 0;
269
270 if (Index)
271 return InterfaceValue{*Index, IValue.DerefLevel};
272 return None;
273}
274
275static void populateAttrMap(DenseMap<const Value *, AliasAttrs> &AttrMap,
276 const AliasAttrMap &AMap) {
George Burgess IV22682e22016-07-15 20:02:49 +0000277 for (const auto &Mapping : AMap.mappings()) {
278 auto IVal = Mapping.first;
279
280 // AttrMap only cares about top-level values
281 if (IVal.DerefLevel == 0)
282 AttrMap[IVal.Val] = Mapping.second;
283 }
George Burgess IV3b059842016-07-19 20:47:15 +0000284}
George Burgess IV22682e22016-07-15 20:02:49 +0000285
George Burgess IV3b059842016-07-19 20:47:15 +0000286static void
287populateAliasMap(DenseMap<const Value *, std::vector<const Value *>> &AliasMap,
288 const ReachabilitySet &ReachSet) {
George Burgess IV6d30aa02016-07-15 19:53:25 +0000289 for (const auto &OuterMapping : ReachSet.value_mappings()) {
290 // AliasMap only cares about top-level values
291 if (OuterMapping.first.DerefLevel > 0)
292 continue;
293
294 auto Val = OuterMapping.first.Val;
295 auto &AliasList = AliasMap[Val];
296 for (const auto &InnerMapping : OuterMapping.second) {
297 // Again, AliasMap only cares about top-level values
298 if (InnerMapping.first.DerefLevel == 0)
299 AliasList.push_back(InnerMapping.first.Val);
300 }
301
302 // Sort AliasList for faster lookup
303 std::sort(AliasList.begin(), AliasList.end(), std::less<const Value *>());
304 }
George Burgess IV3b059842016-07-19 20:47:15 +0000305}
George Burgess IV6d30aa02016-07-15 19:53:25 +0000306
George Burgess IV3b059842016-07-19 20:47:15 +0000307static void populateExternalRelations(
308 SmallVectorImpl<ExternalRelation> &ExtRelations, const Function &Fn,
309 const SmallVectorImpl<Value *> &RetVals, const ReachabilitySet &ReachSet) {
310 // If a function only returns one of its argument X, then X will be both an
311 // argument and a return value at the same time. This is an edge case that
312 // needs special handling here.
313 for (const auto &Arg : Fn.args()) {
314 if (is_contained(RetVals, &Arg)) {
315 auto ArgVal = InterfaceValue{Arg.getArgNo() + 1, 0};
316 auto RetVal = InterfaceValue{0, 0};
317 ExtRelations.push_back(ExternalRelation{ArgVal, RetVal});
318 }
319 }
320
321 // Below is the core summary construction logic.
322 // A naive solution of adding only the value aliases that are parameters or
323 // return values in ReachSet to the summary won't work: It is possible that a
324 // parameter P is written into an intermediate value I, and the function
325 // subsequently returns *I. In that case, *I is does not value alias anything
326 // in ReachSet, and the naive solution will miss a summary edge from (P, 1) to
327 // (I, 1).
328 // To account for the aforementioned case, we need to check each non-parameter
329 // and non-return value for the possibility of acting as an intermediate.
330 // 'ValueMap' here records, for each value, which InterfaceValues read from or
331 // write into it. If both the read list and the write list of a given value
332 // are non-empty, we know that a particular value is an intermidate and we
333 // need to add summary edges from the writes to the reads.
334 DenseMap<Value *, ValueSummary> ValueMap;
335 for (const auto &OuterMapping : ReachSet.value_mappings()) {
336 if (auto Dst = getInterfaceValue(OuterMapping.first, RetVals)) {
337 for (const auto &InnerMapping : OuterMapping.second) {
338 // If Src is a param/return value, we get a same-level assignment.
339 if (auto Src = getInterfaceValue(InnerMapping.first, RetVals)) {
340 // This may happen if both Dst and Src are return values
341 if (*Dst == *Src)
342 continue;
343
344 if (hasReadOnlyState(InnerMapping.second))
345 ExtRelations.push_back(ExternalRelation{*Dst, *Src});
346 // No need to check for WriteOnly state, since ReachSet is symmetric
347 } else {
348 // If Src is not a param/return, add it to ValueMap
349 auto SrcIVal = InnerMapping.first;
350 if (hasReadOnlyState(InnerMapping.second))
351 ValueMap[SrcIVal.Val].FromRecords.push_back(
352 ValueSummary::Record{*Dst, SrcIVal.DerefLevel});
353 if (hasWriteOnlyState(InnerMapping.second))
354 ValueMap[SrcIVal.Val].ToRecords.push_back(
355 ValueSummary::Record{*Dst, SrcIVal.DerefLevel});
356 }
357 }
358 }
359 }
360
361 for (const auto &Mapping : ValueMap) {
362 for (const auto &FromRecord : Mapping.second.FromRecords) {
363 for (const auto &ToRecord : Mapping.second.ToRecords) {
364 auto ToLevel = ToRecord.DerefLevel;
365 auto FromLevel = FromRecord.DerefLevel;
366 // Same-level assignments should have already been processed by now
367 if (ToLevel == FromLevel)
368 continue;
369
370 auto SrcIndex = FromRecord.IValue.Index;
371 auto SrcLevel = FromRecord.IValue.DerefLevel;
372 auto DstIndex = ToRecord.IValue.Index;
373 auto DstLevel = ToRecord.IValue.DerefLevel;
374 if (ToLevel > FromLevel)
375 SrcLevel += ToLevel - FromLevel;
376 else
377 DstLevel += FromLevel - ToLevel;
378
379 ExtRelations.push_back(
380 ExternalRelation{InterfaceValue{SrcIndex, SrcLevel},
381 InterfaceValue{DstIndex, DstLevel}});
382 }
383 }
384 }
385
386 // Remove duplicates in ExtRelations
387 std::sort(ExtRelations.begin(), ExtRelations.end());
388 ExtRelations.erase(std::unique(ExtRelations.begin(), ExtRelations.end()),
389 ExtRelations.end());
390}
391
392static void populateExternalAttributes(
393 SmallVectorImpl<ExternalAttribute> &ExtAttributes, const Function &Fn,
394 const SmallVectorImpl<Value *> &RetVals, const AliasAttrMap &AMap) {
395 for (const auto &Mapping : AMap.mappings()) {
396 if (auto IVal = getInterfaceValue(Mapping.first, RetVals)) {
397 auto Attr = getExternallyVisibleAttrs(Mapping.second);
398 if (Attr.any())
399 ExtAttributes.push_back(ExternalAttribute{*IVal, Attr});
400 }
401 }
402}
403
404CFLAndersAAResult::FunctionInfo::FunctionInfo(
405 const Function &Fn, const SmallVectorImpl<Value *> &RetVals,
406 const ReachabilitySet &ReachSet, AliasAttrMap AMap) {
407 populateAttrMap(AttrMap, AMap);
408 populateExternalAttributes(Summary.RetParamAttributes, Fn, RetVals, AMap);
409 populateAliasMap(AliasMap, ReachSet);
410 populateExternalRelations(Summary.RetParamRelations, Fn, RetVals, ReachSet);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000411}
412
George Burgess IV22682e22016-07-15 20:02:49 +0000413AliasAttrs CFLAndersAAResult::FunctionInfo::getAttrs(const Value *V) const {
414 assert(V != nullptr);
415
416 AliasAttrs Attr;
417 auto Itr = AttrMap.find(V);
418 if (Itr != AttrMap.end())
419 Attr = Itr->second;
420 return Attr;
421}
422
George Burgess IV6d30aa02016-07-15 19:53:25 +0000423bool CFLAndersAAResult::FunctionInfo::mayAlias(const Value *LHS,
424 const Value *RHS) const {
425 assert(LHS && RHS);
426
427 auto Itr = AliasMap.find(LHS);
George Burgess IV22682e22016-07-15 20:02:49 +0000428 if (Itr != AliasMap.end()) {
429 if (std::binary_search(Itr->second.begin(), Itr->second.end(), RHS,
430 std::less<const Value *>()))
431 return true;
432 }
433
434 // Even if LHS and RHS are not reachable, they may still alias due to their
435 // AliasAttrs
436 auto AttrsA = getAttrs(LHS);
437 auto AttrsB = getAttrs(RHS);
438
439 if (AttrsA.none() || AttrsB.none())
George Burgess IV6d30aa02016-07-15 19:53:25 +0000440 return false;
George Burgess IV22682e22016-07-15 20:02:49 +0000441 if (hasUnknownOrCallerAttr(AttrsA) || hasUnknownOrCallerAttr(AttrsB))
442 return true;
443 if (isGlobalOrArgAttr(AttrsA) && isGlobalOrArgAttr(AttrsB))
444 return true;
445 return false;
George Burgess IV6d30aa02016-07-15 19:53:25 +0000446}
447
448static void propagate(InstantiatedValue From, InstantiatedValue To,
449 MatchState State, ReachabilitySet &ReachSet,
450 std::vector<WorkListItem> &WorkList) {
451 if (From == To)
452 return;
453 if (ReachSet.insert(From, To, State))
454 WorkList.push_back(WorkListItem{From, To, State});
455}
456
457static void initializeWorkList(std::vector<WorkListItem> &WorkList,
458 ReachabilitySet &ReachSet,
459 const CFLGraph &Graph) {
460 for (const auto &Mapping : Graph.value_mappings()) {
461 auto Val = Mapping.first;
462 auto &ValueInfo = Mapping.second;
463 assert(ValueInfo.getNumLevels() > 0);
464
465 // Insert all immediate assignment neighbors to the worklist
466 for (unsigned I = 0, E = ValueInfo.getNumLevels(); I < E; ++I) {
467 auto Src = InstantiatedValue{Val, I};
468 // If there's an assignment edge from X to Y, it means Y is reachable from
469 // X at S2 and X is reachable from Y at S1
470 for (auto &Edge : ValueInfo.getNodeInfoAtLevel(I).Edges) {
George Burgess IVc01b42f2016-07-19 20:38:21 +0000471 propagate(Edge.Other, Src, MatchState::FlowFromReadOnly, ReachSet,
472 WorkList);
473 propagate(Src, Edge.Other, MatchState::FlowToWriteOnly, ReachSet,
474 WorkList);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000475 }
476 }
477 }
478}
479
480static Optional<InstantiatedValue> getNodeBelow(const CFLGraph &Graph,
481 InstantiatedValue V) {
482 auto NodeBelow = InstantiatedValue{V.Val, V.DerefLevel + 1};
483 if (Graph.getNode(NodeBelow))
484 return NodeBelow;
485 return None;
486}
487
488static void processWorkListItem(const WorkListItem &Item, const CFLGraph &Graph,
489 ReachabilitySet &ReachSet, AliasMemSet &MemSet,
490 std::vector<WorkListItem> &WorkList) {
491 auto FromNode = Item.From;
492 auto ToNode = Item.To;
493
494 auto NodeInfo = Graph.getNode(ToNode);
495 assert(NodeInfo != nullptr);
496
George Burgess IV6d30aa02016-07-15 19:53:25 +0000497 // TODO: propagate field offsets
498
499 // FIXME: Here is a neat trick we can do: since both ReachSet and MemSet holds
500 // relations that are symmetric, we could actually cut the storage by half by
501 // sorting FromNode and ToNode before insertion happens.
502
503 // The newly added value alias pair may pontentially generate more memory
504 // alias pairs. Check for them here.
505 auto FromNodeBelow = getNodeBelow(Graph, FromNode);
506 auto ToNodeBelow = getNodeBelow(Graph, ToNode);
507 if (FromNodeBelow && ToNodeBelow &&
508 MemSet.insert(*FromNodeBelow, *ToNodeBelow)) {
George Burgess IVc01b42f2016-07-19 20:38:21 +0000509 propagate(*FromNodeBelow, *ToNodeBelow,
510 MatchState::FlowFromMemAliasNoReadWrite, ReachSet, WorkList);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000511 for (const auto &Mapping : ReachSet.reachableValueAliases(*FromNodeBelow)) {
512 auto Src = Mapping.first;
George Burgess IVc01b42f2016-07-19 20:38:21 +0000513 auto MemAliasPropagate = [&](MatchState FromState, MatchState ToState) {
514 if (Mapping.second.test(static_cast<size_t>(FromState)))
515 propagate(Src, *ToNodeBelow, ToState, ReachSet, WorkList);
516 };
517
518 MemAliasPropagate(MatchState::FlowFromReadOnly,
519 MatchState::FlowFromMemAliasReadOnly);
520 MemAliasPropagate(MatchState::FlowToWriteOnly,
521 MatchState::FlowToMemAliasWriteOnly);
522 MemAliasPropagate(MatchState::FlowToReadWrite,
523 MatchState::FlowToMemAliasReadWrite);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000524 }
525 }
526
527 // This is the core of the state machine walking algorithm. We expand ReachSet
528 // based on which state we are at (which in turn dictates what edges we
529 // should examine)
530 // From a high-level point of view, the state machine here guarantees two
531 // properties:
532 // - If *X and *Y are memory aliases, then X and Y are value aliases
533 // - If Y is an alias of X, then reverse assignment edges (if there is any)
534 // should precede any assignment edges on the path from X to Y.
George Burgess IVc01b42f2016-07-19 20:38:21 +0000535 auto NextAssignState = [&](MatchState State) {
536 for (const auto &AssignEdge : NodeInfo->Edges)
537 propagate(FromNode, AssignEdge.Other, State, ReachSet, WorkList);
538 };
539 auto NextRevAssignState = [&](MatchState State) {
540 for (const auto &RevAssignEdge : NodeInfo->ReverseEdges)
541 propagate(FromNode, RevAssignEdge.Other, State, ReachSet, WorkList);
542 };
543 auto NextMemState = [&](MatchState State) {
544 if (auto AliasSet = MemSet.getMemoryAliases(ToNode)) {
545 for (const auto &MemAlias : *AliasSet)
546 propagate(FromNode, MemAlias, State, ReachSet, WorkList);
547 }
548 };
549
George Burgess IV6d30aa02016-07-15 19:53:25 +0000550 switch (Item.State) {
George Burgess IVc01b42f2016-07-19 20:38:21 +0000551 case MatchState::FlowFromReadOnly: {
552 NextRevAssignState(MatchState::FlowFromReadOnly);
553 NextAssignState(MatchState::FlowToReadWrite);
554 NextMemState(MatchState::FlowFromMemAliasReadOnly);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000555 break;
556 }
George Burgess IVc01b42f2016-07-19 20:38:21 +0000557 case MatchState::FlowFromMemAliasNoReadWrite: {
558 NextRevAssignState(MatchState::FlowFromReadOnly);
559 NextAssignState(MatchState::FlowToWriteOnly);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000560 break;
561 }
George Burgess IVc01b42f2016-07-19 20:38:21 +0000562 case MatchState::FlowFromMemAliasReadOnly: {
563 NextRevAssignState(MatchState::FlowFromReadOnly);
564 NextAssignState(MatchState::FlowToReadWrite);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000565 break;
566 }
George Burgess IVc01b42f2016-07-19 20:38:21 +0000567 case MatchState::FlowToWriteOnly: {
568 NextAssignState(MatchState::FlowToWriteOnly);
569 NextMemState(MatchState::FlowToMemAliasWriteOnly);
570 break;
571 }
572 case MatchState::FlowToReadWrite: {
573 NextAssignState(MatchState::FlowToReadWrite);
574 NextMemState(MatchState::FlowToMemAliasReadWrite);
575 break;
576 }
577 case MatchState::FlowToMemAliasWriteOnly: {
578 NextAssignState(MatchState::FlowToWriteOnly);
579 break;
580 }
581 case MatchState::FlowToMemAliasReadWrite: {
582 NextAssignState(MatchState::FlowToReadWrite);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000583 break;
584 }
585 }
586}
587
George Burgess IV22682e22016-07-15 20:02:49 +0000588static AliasAttrMap buildAttrMap(const CFLGraph &Graph,
589 const ReachabilitySet &ReachSet) {
590 AliasAttrMap AttrMap;
591 std::vector<InstantiatedValue> WorkList, NextList;
592
593 // Initialize each node with its original AliasAttrs in CFLGraph
594 for (const auto &Mapping : Graph.value_mappings()) {
595 auto Val = Mapping.first;
596 auto &ValueInfo = Mapping.second;
597 for (unsigned I = 0, E = ValueInfo.getNumLevels(); I < E; ++I) {
598 auto Node = InstantiatedValue{Val, I};
599 AttrMap.add(Node, ValueInfo.getNodeInfoAtLevel(I).Attr);
600 WorkList.push_back(Node);
601 }
602 }
603
604 while (!WorkList.empty()) {
605 for (const auto &Dst : WorkList) {
606 auto DstAttr = AttrMap.getAttrs(Dst);
607 if (DstAttr.none())
608 continue;
609
610 // Propagate attr on the same level
611 for (const auto &Mapping : ReachSet.reachableValueAliases(Dst)) {
612 auto Src = Mapping.first;
613 if (AttrMap.add(Src, DstAttr))
614 NextList.push_back(Src);
615 }
616
617 // Propagate attr to the levels below
618 auto DstBelow = getNodeBelow(Graph, Dst);
619 while (DstBelow) {
620 if (AttrMap.add(*DstBelow, DstAttr)) {
621 NextList.push_back(*DstBelow);
622 break;
623 }
624 DstBelow = getNodeBelow(Graph, *DstBelow);
625 }
626 }
627 WorkList.swap(NextList);
628 NextList.clear();
629 }
630
631 return AttrMap;
632}
633
George Burgess IV6d30aa02016-07-15 19:53:25 +0000634CFLAndersAAResult::FunctionInfo
635CFLAndersAAResult::buildInfoFrom(const Function &Fn) {
636 CFLGraphBuilder<CFLAndersAAResult> GraphBuilder(
637 *this, TLI,
638 // Cast away the constness here due to GraphBuilder's API requirement
639 const_cast<Function &>(Fn));
640 auto &Graph = GraphBuilder.getCFLGraph();
641
642 ReachabilitySet ReachSet;
643 AliasMemSet MemSet;
644
645 std::vector<WorkListItem> WorkList, NextList;
646 initializeWorkList(WorkList, ReachSet, Graph);
647 // TODO: make sure we don't stop before the fix point is reached
648 while (!WorkList.empty()) {
649 for (const auto &Item : WorkList)
650 processWorkListItem(Item, Graph, ReachSet, MemSet, NextList);
651
652 NextList.swap(WorkList);
653 NextList.clear();
654 }
655
George Burgess IV22682e22016-07-15 20:02:49 +0000656 // Now that we have all the reachability info, propagate AliasAttrs according
657 // to it
658 auto IValueAttrMap = buildAttrMap(Graph, ReachSet);
659
George Burgess IV3b059842016-07-19 20:47:15 +0000660 return FunctionInfo(Fn, GraphBuilder.getReturnValues(), ReachSet,
661 std::move(IValueAttrMap));
George Burgess IV6d30aa02016-07-15 19:53:25 +0000662}
663
664void CFLAndersAAResult::scan(const Function &Fn) {
665 auto InsertPair = Cache.insert(std::make_pair(&Fn, Optional<FunctionInfo>()));
666 (void)InsertPair;
667 assert(InsertPair.second &&
668 "Trying to scan a function that has already been cached");
669
670 // Note that we can't do Cache[Fn] = buildSetsFrom(Fn) here: the function call
671 // may get evaluated after operator[], potentially triggering a DenseMap
672 // resize and invalidating the reference returned by operator[]
673 auto FunInfo = buildInfoFrom(Fn);
674 Cache[&Fn] = std::move(FunInfo);
675 Handles.push_front(FunctionHandle(const_cast<Function *>(&Fn), this));
676}
677
678void CFLAndersAAResult::evict(const Function &Fn) { Cache.erase(&Fn); }
679
680const Optional<CFLAndersAAResult::FunctionInfo> &
681CFLAndersAAResult::ensureCached(const Function &Fn) {
682 auto Iter = Cache.find(&Fn);
683 if (Iter == Cache.end()) {
684 scan(Fn);
685 Iter = Cache.find(&Fn);
686 assert(Iter != Cache.end());
687 assert(Iter->second.hasValue());
688 }
689 return Iter->second;
690}
691
692const AliasSummary *CFLAndersAAResult::getAliasSummary(const Function &Fn) {
693 auto &FunInfo = ensureCached(Fn);
694 if (FunInfo.hasValue())
695 return &FunInfo->getAliasSummary();
696 else
697 return nullptr;
698}
699
700AliasResult CFLAndersAAResult::query(const MemoryLocation &LocA,
701 const MemoryLocation &LocB) {
702 auto *ValA = LocA.Ptr;
703 auto *ValB = LocB.Ptr;
704
705 if (!ValA->getType()->isPointerTy() || !ValB->getType()->isPointerTy())
706 return NoAlias;
707
708 auto *Fn = parentFunctionOfValue(ValA);
709 if (!Fn) {
710 Fn = parentFunctionOfValue(ValB);
711 if (!Fn) {
712 // The only times this is known to happen are when globals + InlineAsm are
713 // involved
714 DEBUG(dbgs()
715 << "CFLAndersAA: could not extract parent function information.\n");
716 return MayAlias;
717 }
718 } else {
719 assert(!parentFunctionOfValue(ValB) || parentFunctionOfValue(ValB) == Fn);
720 }
721
722 assert(Fn != nullptr);
723 auto &FunInfo = ensureCached(*Fn);
724
725 // AliasMap lookup
726 if (FunInfo->mayAlias(ValA, ValB))
727 return MayAlias;
728 return NoAlias;
729}
730
731AliasResult CFLAndersAAResult::alias(const MemoryLocation &LocA,
732 const MemoryLocation &LocB) {
733 if (LocA.Ptr == LocB.Ptr)
734 return LocA.Size == LocB.Size ? MustAlias : PartialAlias;
735
736 // Comparisons between global variables and other constants should be
737 // handled by BasicAA.
738 // CFLAndersAA may report NoAlias when comparing a GlobalValue and
739 // ConstantExpr, but every query needs to have at least one Value tied to a
740 // Function, and neither GlobalValues nor ConstantExprs are.
741 if (isa<Constant>(LocA.Ptr) && isa<Constant>(LocB.Ptr))
742 return AAResultBase::alias(LocA, LocB);
743
744 AliasResult QueryResult = query(LocA, LocB);
745 if (QueryResult == MayAlias)
746 return AAResultBase::alias(LocA, LocB);
747
748 return QueryResult;
749}
George Burgess IVbfa401e2016-07-06 00:26:41 +0000750
751char CFLAndersAA::PassID;
752
753CFLAndersAAResult CFLAndersAA::run(Function &F, AnalysisManager<Function> &AM) {
George Burgess IV6d30aa02016-07-15 19:53:25 +0000754 return CFLAndersAAResult(AM.getResult<TargetLibraryAnalysis>(F));
George Burgess IVbfa401e2016-07-06 00:26:41 +0000755}
756
757char CFLAndersAAWrapperPass::ID = 0;
758INITIALIZE_PASS(CFLAndersAAWrapperPass, "cfl-anders-aa",
759 "Inclusion-Based CFL Alias Analysis", false, true)
760
761ImmutablePass *llvm::createCFLAndersAAWrapperPass() {
762 return new CFLAndersAAWrapperPass();
763}
764
765CFLAndersAAWrapperPass::CFLAndersAAWrapperPass() : ImmutablePass(ID) {
766 initializeCFLAndersAAWrapperPassPass(*PassRegistry::getPassRegistry());
767}
768
George Burgess IV6d30aa02016-07-15 19:53:25 +0000769void CFLAndersAAWrapperPass::initializePass() {
770 auto &TLIWP = getAnalysis<TargetLibraryInfoWrapperPass>();
771 Result.reset(new CFLAndersAAResult(TLIWP.getTLI()));
772}
George Burgess IVbfa401e2016-07-06 00:26:41 +0000773
774void CFLAndersAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
775 AU.setPreservesAll();
George Burgess IV6d30aa02016-07-15 19:53:25 +0000776 AU.addRequired<TargetLibraryInfoWrapperPass>();
George Burgess IVbfa401e2016-07-06 00:26:41 +0000777}