blob: d0ac9544968128d2a8831f1f20f10d3745ce40b3 [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
109// We use ReachabilitySet to keep track of value aliases (The nonterminal "V" in
110// the paper) during the analysis.
111class ReachabilitySet {
George Burgess IVc01b42f2016-07-19 20:38:21 +0000112 typedef std::bitset<7> StateSet;
George Burgess IV6d30aa02016-07-15 19:53:25 +0000113 typedef DenseMap<InstantiatedValue, StateSet> ValueStateMap;
114 typedef DenseMap<InstantiatedValue, ValueStateMap> ValueReachMap;
115 ValueReachMap ReachMap;
116
117public:
118 typedef ValueStateMap::const_iterator const_valuestate_iterator;
119 typedef ValueReachMap::const_iterator const_value_iterator;
120
121 // Insert edge 'From->To' at state 'State'
122 bool insert(InstantiatedValue From, InstantiatedValue To, MatchState State) {
123 auto &States = ReachMap[To][From];
124 auto Idx = static_cast<size_t>(State);
125 if (!States.test(Idx)) {
126 States.set(Idx);
127 return true;
128 }
129 return false;
130 }
131
132 // Return the set of all ('From', 'State') pair for a given node 'To'
133 iterator_range<const_valuestate_iterator>
134 reachableValueAliases(InstantiatedValue V) const {
135 auto Itr = ReachMap.find(V);
136 if (Itr == ReachMap.end())
137 return make_range<const_valuestate_iterator>(const_valuestate_iterator(),
138 const_valuestate_iterator());
139 return make_range<const_valuestate_iterator>(Itr->second.begin(),
140 Itr->second.end());
141 }
142
143 iterator_range<const_value_iterator> value_mappings() const {
144 return make_range<const_value_iterator>(ReachMap.begin(), ReachMap.end());
145 }
146};
147
148// We use AliasMemSet to keep track of all memory aliases (the nonterminal "M"
149// in the paper) during the analysis.
150class AliasMemSet {
151 typedef DenseSet<InstantiatedValue> MemSet;
152 typedef DenseMap<InstantiatedValue, MemSet> MemMapType;
153 MemMapType MemMap;
154
155public:
156 typedef MemSet::const_iterator const_mem_iterator;
157
158 bool insert(InstantiatedValue LHS, InstantiatedValue RHS) {
159 // Top-level values can never be memory aliases because one cannot take the
160 // addresses of them
161 assert(LHS.DerefLevel > 0 && RHS.DerefLevel > 0);
162 return MemMap[LHS].insert(RHS).second;
163 }
164
165 const MemSet *getMemoryAliases(InstantiatedValue V) const {
166 auto Itr = MemMap.find(V);
167 if (Itr == MemMap.end())
168 return nullptr;
169 return &Itr->second;
170 }
171};
172
George Burgess IV22682e22016-07-15 20:02:49 +0000173// We use AliasAttrMap to keep track of the AliasAttr of each node.
174class AliasAttrMap {
175 typedef DenseMap<InstantiatedValue, AliasAttrs> MapType;
176 MapType AttrMap;
177
178public:
179 typedef MapType::const_iterator const_iterator;
180
181 bool add(InstantiatedValue V, AliasAttrs Attr) {
182 if (Attr.none())
183 return false;
184 auto &OldAttr = AttrMap[V];
185 auto NewAttr = OldAttr | Attr;
186 if (OldAttr == NewAttr)
187 return false;
188 OldAttr = NewAttr;
189 return true;
190 }
191
192 AliasAttrs getAttrs(InstantiatedValue V) const {
193 AliasAttrs Attr;
194 auto Itr = AttrMap.find(V);
195 if (Itr != AttrMap.end())
196 Attr = Itr->second;
197 return Attr;
198 }
199
200 iterator_range<const_iterator> mappings() const {
201 return make_range<const_iterator>(AttrMap.begin(), AttrMap.end());
202 }
203};
204
George Burgess IV6d30aa02016-07-15 19:53:25 +0000205struct WorkListItem {
206 InstantiatedValue From;
207 InstantiatedValue To;
208 MatchState State;
209};
210}
211
212class CFLAndersAAResult::FunctionInfo {
213 /// Map a value to other values that may alias it
214 /// Since the alias relation is symmetric, to save some space we assume values
215 /// are properly ordered: if a and b alias each other, and a < b, then b is in
216 /// AliasMap[a] but not vice versa.
217 DenseMap<const Value *, std::vector<const Value *>> AliasMap;
218
George Burgess IV22682e22016-07-15 20:02:49 +0000219 /// Map a value to its corresponding AliasAttrs
220 DenseMap<const Value *, AliasAttrs> AttrMap;
221
George Burgess IV6d30aa02016-07-15 19:53:25 +0000222 /// Summary of externally visible effects.
223 AliasSummary Summary;
224
George Burgess IV22682e22016-07-15 20:02:49 +0000225 AliasAttrs getAttrs(const Value *) const;
226
George Burgess IV6d30aa02016-07-15 19:53:25 +0000227public:
George Burgess IV22682e22016-07-15 20:02:49 +0000228 FunctionInfo(const ReachabilitySet &, AliasAttrMap);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000229
230 bool mayAlias(const Value *LHS, const Value *RHS) const;
231 const AliasSummary &getAliasSummary() const { return Summary; }
232};
233
George Burgess IV22682e22016-07-15 20:02:49 +0000234CFLAndersAAResult::FunctionInfo::FunctionInfo(const ReachabilitySet &ReachSet,
235 AliasAttrMap AMap) {
236 // Populate AttrMap
237 for (const auto &Mapping : AMap.mappings()) {
238 auto IVal = Mapping.first;
239
240 // AttrMap only cares about top-level values
241 if (IVal.DerefLevel == 0)
242 AttrMap[IVal.Val] = Mapping.second;
243 }
244
245 // Populate AliasMap
George Burgess IV6d30aa02016-07-15 19:53:25 +0000246 for (const auto &OuterMapping : ReachSet.value_mappings()) {
247 // AliasMap only cares about top-level values
248 if (OuterMapping.first.DerefLevel > 0)
249 continue;
250
251 auto Val = OuterMapping.first.Val;
252 auto &AliasList = AliasMap[Val];
253 for (const auto &InnerMapping : OuterMapping.second) {
254 // Again, AliasMap only cares about top-level values
255 if (InnerMapping.first.DerefLevel == 0)
256 AliasList.push_back(InnerMapping.first.Val);
257 }
258
259 // Sort AliasList for faster lookup
260 std::sort(AliasList.begin(), AliasList.end(), std::less<const Value *>());
261 }
262
263 // TODO: Populate function summary here
264}
265
George Burgess IV22682e22016-07-15 20:02:49 +0000266AliasAttrs CFLAndersAAResult::FunctionInfo::getAttrs(const Value *V) const {
267 assert(V != nullptr);
268
269 AliasAttrs Attr;
270 auto Itr = AttrMap.find(V);
271 if (Itr != AttrMap.end())
272 Attr = Itr->second;
273 return Attr;
274}
275
George Burgess IV6d30aa02016-07-15 19:53:25 +0000276bool CFLAndersAAResult::FunctionInfo::mayAlias(const Value *LHS,
277 const Value *RHS) const {
278 assert(LHS && RHS);
279
280 auto Itr = AliasMap.find(LHS);
George Burgess IV22682e22016-07-15 20:02:49 +0000281 if (Itr != AliasMap.end()) {
282 if (std::binary_search(Itr->second.begin(), Itr->second.end(), RHS,
283 std::less<const Value *>()))
284 return true;
285 }
286
287 // Even if LHS and RHS are not reachable, they may still alias due to their
288 // AliasAttrs
289 auto AttrsA = getAttrs(LHS);
290 auto AttrsB = getAttrs(RHS);
291
292 if (AttrsA.none() || AttrsB.none())
George Burgess IV6d30aa02016-07-15 19:53:25 +0000293 return false;
George Burgess IV22682e22016-07-15 20:02:49 +0000294 if (hasUnknownOrCallerAttr(AttrsA) || hasUnknownOrCallerAttr(AttrsB))
295 return true;
296 if (isGlobalOrArgAttr(AttrsA) && isGlobalOrArgAttr(AttrsB))
297 return true;
298 return false;
George Burgess IV6d30aa02016-07-15 19:53:25 +0000299}
300
301static void propagate(InstantiatedValue From, InstantiatedValue To,
302 MatchState State, ReachabilitySet &ReachSet,
303 std::vector<WorkListItem> &WorkList) {
304 if (From == To)
305 return;
306 if (ReachSet.insert(From, To, State))
307 WorkList.push_back(WorkListItem{From, To, State});
308}
309
310static void initializeWorkList(std::vector<WorkListItem> &WorkList,
311 ReachabilitySet &ReachSet,
312 const CFLGraph &Graph) {
313 for (const auto &Mapping : Graph.value_mappings()) {
314 auto Val = Mapping.first;
315 auto &ValueInfo = Mapping.second;
316 assert(ValueInfo.getNumLevels() > 0);
317
318 // Insert all immediate assignment neighbors to the worklist
319 for (unsigned I = 0, E = ValueInfo.getNumLevels(); I < E; ++I) {
320 auto Src = InstantiatedValue{Val, I};
321 // If there's an assignment edge from X to Y, it means Y is reachable from
322 // X at S2 and X is reachable from Y at S1
323 for (auto &Edge : ValueInfo.getNodeInfoAtLevel(I).Edges) {
George Burgess IVc01b42f2016-07-19 20:38:21 +0000324 propagate(Edge.Other, Src, MatchState::FlowFromReadOnly, ReachSet,
325 WorkList);
326 propagate(Src, Edge.Other, MatchState::FlowToWriteOnly, ReachSet,
327 WorkList);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000328 }
329 }
330 }
331}
332
333static Optional<InstantiatedValue> getNodeBelow(const CFLGraph &Graph,
334 InstantiatedValue V) {
335 auto NodeBelow = InstantiatedValue{V.Val, V.DerefLevel + 1};
336 if (Graph.getNode(NodeBelow))
337 return NodeBelow;
338 return None;
339}
340
341static void processWorkListItem(const WorkListItem &Item, const CFLGraph &Graph,
342 ReachabilitySet &ReachSet, AliasMemSet &MemSet,
343 std::vector<WorkListItem> &WorkList) {
344 auto FromNode = Item.From;
345 auto ToNode = Item.To;
346
347 auto NodeInfo = Graph.getNode(ToNode);
348 assert(NodeInfo != nullptr);
349
George Burgess IV6d30aa02016-07-15 19:53:25 +0000350 // TODO: propagate field offsets
351
352 // FIXME: Here is a neat trick we can do: since both ReachSet and MemSet holds
353 // relations that are symmetric, we could actually cut the storage by half by
354 // sorting FromNode and ToNode before insertion happens.
355
356 // The newly added value alias pair may pontentially generate more memory
357 // alias pairs. Check for them here.
358 auto FromNodeBelow = getNodeBelow(Graph, FromNode);
359 auto ToNodeBelow = getNodeBelow(Graph, ToNode);
360 if (FromNodeBelow && ToNodeBelow &&
361 MemSet.insert(*FromNodeBelow, *ToNodeBelow)) {
George Burgess IVc01b42f2016-07-19 20:38:21 +0000362 propagate(*FromNodeBelow, *ToNodeBelow,
363 MatchState::FlowFromMemAliasNoReadWrite, ReachSet, WorkList);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000364 for (const auto &Mapping : ReachSet.reachableValueAliases(*FromNodeBelow)) {
365 auto Src = Mapping.first;
George Burgess IVc01b42f2016-07-19 20:38:21 +0000366 auto MemAliasPropagate = [&](MatchState FromState, MatchState ToState) {
367 if (Mapping.second.test(static_cast<size_t>(FromState)))
368 propagate(Src, *ToNodeBelow, ToState, ReachSet, WorkList);
369 };
370
371 MemAliasPropagate(MatchState::FlowFromReadOnly,
372 MatchState::FlowFromMemAliasReadOnly);
373 MemAliasPropagate(MatchState::FlowToWriteOnly,
374 MatchState::FlowToMemAliasWriteOnly);
375 MemAliasPropagate(MatchState::FlowToReadWrite,
376 MatchState::FlowToMemAliasReadWrite);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000377 }
378 }
379
380 // This is the core of the state machine walking algorithm. We expand ReachSet
381 // based on which state we are at (which in turn dictates what edges we
382 // should examine)
383 // From a high-level point of view, the state machine here guarantees two
384 // properties:
385 // - If *X and *Y are memory aliases, then X and Y are value aliases
386 // - If Y is an alias of X, then reverse assignment edges (if there is any)
387 // should precede any assignment edges on the path from X to Y.
George Burgess IVc01b42f2016-07-19 20:38:21 +0000388 auto NextAssignState = [&](MatchState State) {
389 for (const auto &AssignEdge : NodeInfo->Edges)
390 propagate(FromNode, AssignEdge.Other, State, ReachSet, WorkList);
391 };
392 auto NextRevAssignState = [&](MatchState State) {
393 for (const auto &RevAssignEdge : NodeInfo->ReverseEdges)
394 propagate(FromNode, RevAssignEdge.Other, State, ReachSet, WorkList);
395 };
396 auto NextMemState = [&](MatchState State) {
397 if (auto AliasSet = MemSet.getMemoryAliases(ToNode)) {
398 for (const auto &MemAlias : *AliasSet)
399 propagate(FromNode, MemAlias, State, ReachSet, WorkList);
400 }
401 };
402
George Burgess IV6d30aa02016-07-15 19:53:25 +0000403 switch (Item.State) {
George Burgess IVc01b42f2016-07-19 20:38:21 +0000404 case MatchState::FlowFromReadOnly: {
405 NextRevAssignState(MatchState::FlowFromReadOnly);
406 NextAssignState(MatchState::FlowToReadWrite);
407 NextMemState(MatchState::FlowFromMemAliasReadOnly);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000408 break;
409 }
George Burgess IVc01b42f2016-07-19 20:38:21 +0000410 case MatchState::FlowFromMemAliasNoReadWrite: {
411 NextRevAssignState(MatchState::FlowFromReadOnly);
412 NextAssignState(MatchState::FlowToWriteOnly);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000413 break;
414 }
George Burgess IVc01b42f2016-07-19 20:38:21 +0000415 case MatchState::FlowFromMemAliasReadOnly: {
416 NextRevAssignState(MatchState::FlowFromReadOnly);
417 NextAssignState(MatchState::FlowToReadWrite);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000418 break;
419 }
George Burgess IVc01b42f2016-07-19 20:38:21 +0000420 case MatchState::FlowToWriteOnly: {
421 NextAssignState(MatchState::FlowToWriteOnly);
422 NextMemState(MatchState::FlowToMemAliasWriteOnly);
423 break;
424 }
425 case MatchState::FlowToReadWrite: {
426 NextAssignState(MatchState::FlowToReadWrite);
427 NextMemState(MatchState::FlowToMemAliasReadWrite);
428 break;
429 }
430 case MatchState::FlowToMemAliasWriteOnly: {
431 NextAssignState(MatchState::FlowToWriteOnly);
432 break;
433 }
434 case MatchState::FlowToMemAliasReadWrite: {
435 NextAssignState(MatchState::FlowToReadWrite);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000436 break;
437 }
438 }
439}
440
George Burgess IV22682e22016-07-15 20:02:49 +0000441static AliasAttrMap buildAttrMap(const CFLGraph &Graph,
442 const ReachabilitySet &ReachSet) {
443 AliasAttrMap AttrMap;
444 std::vector<InstantiatedValue> WorkList, NextList;
445
446 // Initialize each node with its original AliasAttrs in CFLGraph
447 for (const auto &Mapping : Graph.value_mappings()) {
448 auto Val = Mapping.first;
449 auto &ValueInfo = Mapping.second;
450 for (unsigned I = 0, E = ValueInfo.getNumLevels(); I < E; ++I) {
451 auto Node = InstantiatedValue{Val, I};
452 AttrMap.add(Node, ValueInfo.getNodeInfoAtLevel(I).Attr);
453 WorkList.push_back(Node);
454 }
455 }
456
457 while (!WorkList.empty()) {
458 for (const auto &Dst : WorkList) {
459 auto DstAttr = AttrMap.getAttrs(Dst);
460 if (DstAttr.none())
461 continue;
462
463 // Propagate attr on the same level
464 for (const auto &Mapping : ReachSet.reachableValueAliases(Dst)) {
465 auto Src = Mapping.first;
466 if (AttrMap.add(Src, DstAttr))
467 NextList.push_back(Src);
468 }
469
470 // Propagate attr to the levels below
471 auto DstBelow = getNodeBelow(Graph, Dst);
472 while (DstBelow) {
473 if (AttrMap.add(*DstBelow, DstAttr)) {
474 NextList.push_back(*DstBelow);
475 break;
476 }
477 DstBelow = getNodeBelow(Graph, *DstBelow);
478 }
479 }
480 WorkList.swap(NextList);
481 NextList.clear();
482 }
483
484 return AttrMap;
485}
486
George Burgess IV6d30aa02016-07-15 19:53:25 +0000487CFLAndersAAResult::FunctionInfo
488CFLAndersAAResult::buildInfoFrom(const Function &Fn) {
489 CFLGraphBuilder<CFLAndersAAResult> GraphBuilder(
490 *this, TLI,
491 // Cast away the constness here due to GraphBuilder's API requirement
492 const_cast<Function &>(Fn));
493 auto &Graph = GraphBuilder.getCFLGraph();
494
495 ReachabilitySet ReachSet;
496 AliasMemSet MemSet;
497
498 std::vector<WorkListItem> WorkList, NextList;
499 initializeWorkList(WorkList, ReachSet, Graph);
500 // TODO: make sure we don't stop before the fix point is reached
501 while (!WorkList.empty()) {
502 for (const auto &Item : WorkList)
503 processWorkListItem(Item, Graph, ReachSet, MemSet, NextList);
504
505 NextList.swap(WorkList);
506 NextList.clear();
507 }
508
George Burgess IV22682e22016-07-15 20:02:49 +0000509 // Now that we have all the reachability info, propagate AliasAttrs according
510 // to it
511 auto IValueAttrMap = buildAttrMap(Graph, ReachSet);
512
513 return FunctionInfo(ReachSet, std::move(IValueAttrMap));
George Burgess IV6d30aa02016-07-15 19:53:25 +0000514}
515
516void CFLAndersAAResult::scan(const Function &Fn) {
517 auto InsertPair = Cache.insert(std::make_pair(&Fn, Optional<FunctionInfo>()));
518 (void)InsertPair;
519 assert(InsertPair.second &&
520 "Trying to scan a function that has already been cached");
521
522 // Note that we can't do Cache[Fn] = buildSetsFrom(Fn) here: the function call
523 // may get evaluated after operator[], potentially triggering a DenseMap
524 // resize and invalidating the reference returned by operator[]
525 auto FunInfo = buildInfoFrom(Fn);
526 Cache[&Fn] = std::move(FunInfo);
527 Handles.push_front(FunctionHandle(const_cast<Function *>(&Fn), this));
528}
529
530void CFLAndersAAResult::evict(const Function &Fn) { Cache.erase(&Fn); }
531
532const Optional<CFLAndersAAResult::FunctionInfo> &
533CFLAndersAAResult::ensureCached(const Function &Fn) {
534 auto Iter = Cache.find(&Fn);
535 if (Iter == Cache.end()) {
536 scan(Fn);
537 Iter = Cache.find(&Fn);
538 assert(Iter != Cache.end());
539 assert(Iter->second.hasValue());
540 }
541 return Iter->second;
542}
543
544const AliasSummary *CFLAndersAAResult::getAliasSummary(const Function &Fn) {
545 auto &FunInfo = ensureCached(Fn);
546 if (FunInfo.hasValue())
547 return &FunInfo->getAliasSummary();
548 else
549 return nullptr;
550}
551
552AliasResult CFLAndersAAResult::query(const MemoryLocation &LocA,
553 const MemoryLocation &LocB) {
554 auto *ValA = LocA.Ptr;
555 auto *ValB = LocB.Ptr;
556
557 if (!ValA->getType()->isPointerTy() || !ValB->getType()->isPointerTy())
558 return NoAlias;
559
560 auto *Fn = parentFunctionOfValue(ValA);
561 if (!Fn) {
562 Fn = parentFunctionOfValue(ValB);
563 if (!Fn) {
564 // The only times this is known to happen are when globals + InlineAsm are
565 // involved
566 DEBUG(dbgs()
567 << "CFLAndersAA: could not extract parent function information.\n");
568 return MayAlias;
569 }
570 } else {
571 assert(!parentFunctionOfValue(ValB) || parentFunctionOfValue(ValB) == Fn);
572 }
573
574 assert(Fn != nullptr);
575 auto &FunInfo = ensureCached(*Fn);
576
577 // AliasMap lookup
578 if (FunInfo->mayAlias(ValA, ValB))
579 return MayAlias;
580 return NoAlias;
581}
582
583AliasResult CFLAndersAAResult::alias(const MemoryLocation &LocA,
584 const MemoryLocation &LocB) {
585 if (LocA.Ptr == LocB.Ptr)
586 return LocA.Size == LocB.Size ? MustAlias : PartialAlias;
587
588 // Comparisons between global variables and other constants should be
589 // handled by BasicAA.
590 // CFLAndersAA may report NoAlias when comparing a GlobalValue and
591 // ConstantExpr, but every query needs to have at least one Value tied to a
592 // Function, and neither GlobalValues nor ConstantExprs are.
593 if (isa<Constant>(LocA.Ptr) && isa<Constant>(LocB.Ptr))
594 return AAResultBase::alias(LocA, LocB);
595
596 AliasResult QueryResult = query(LocA, LocB);
597 if (QueryResult == MayAlias)
598 return AAResultBase::alias(LocA, LocB);
599
600 return QueryResult;
601}
George Burgess IVbfa401e2016-07-06 00:26:41 +0000602
603char CFLAndersAA::PassID;
604
605CFLAndersAAResult CFLAndersAA::run(Function &F, AnalysisManager<Function> &AM) {
George Burgess IV6d30aa02016-07-15 19:53:25 +0000606 return CFLAndersAAResult(AM.getResult<TargetLibraryAnalysis>(F));
George Burgess IVbfa401e2016-07-06 00:26:41 +0000607}
608
609char CFLAndersAAWrapperPass::ID = 0;
610INITIALIZE_PASS(CFLAndersAAWrapperPass, "cfl-anders-aa",
611 "Inclusion-Based CFL Alias Analysis", false, true)
612
613ImmutablePass *llvm::createCFLAndersAAWrapperPass() {
614 return new CFLAndersAAWrapperPass();
615}
616
617CFLAndersAAWrapperPass::CFLAndersAAWrapperPass() : ImmutablePass(ID) {
618 initializeCFLAndersAAWrapperPassPass(*PassRegistry::getPassRegistry());
619}
620
George Burgess IV6d30aa02016-07-15 19:53:25 +0000621void CFLAndersAAWrapperPass::initializePass() {
622 auto &TLIWP = getAnalysis<TargetLibraryInfoWrapperPass>();
623 Result.reset(new CFLAndersAAResult(TLIWP.getTLI()));
624}
George Burgess IVbfa401e2016-07-06 00:26:41 +0000625
626void CFLAndersAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
627 AU.setPreservesAll();
George Burgess IV6d30aa02016-07-15 19:53:25 +0000628 AU.addRequired<TargetLibraryInfoWrapperPass>();
George Burgess IVbfa401e2016-07-06 00:26:41 +0000629}