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