blob: 9371407f74ae21f3cd465b60ee11896ca47db08d [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//
30// There is one difference between our current implementation and the one
31// described in the paper: out algorithm eagerly computes all alias pairs after
32// the CFLGraph is built, while in the paper the authors did the computation in
33// a demand-driven fashion. We did not implement the demand-driven algorithm due
34// to the additional coding complexity and higher memory profile, but if we
35// found it necessary we may switch to it eventually.
36//
George Burgess IVbfa401e2016-07-06 00:26:41 +000037//===----------------------------------------------------------------------===//
38
39// N.B. AliasAnalysis as a whole is phrased as a FunctionPass at the moment, and
40// CFLAndersAA is interprocedural. This is *technically* A Bad Thing, because
41// FunctionPasses are only allowed to inspect the Function that they're being
42// run on. Realistically, this likely isn't a problem until we allow
43// FunctionPasses to run concurrently.
44
45#include "llvm/Analysis/CFLAndersAliasAnalysis.h"
George Burgess IV1ca8aff2016-07-06 00:36:12 +000046#include "CFLGraph.h"
George Burgess IV6d30aa02016-07-15 19:53:25 +000047#include "llvm/ADT/DenseSet.h"
George Burgess IVbfa401e2016-07-06 00:26:41 +000048#include "llvm/Pass.h"
49
50using namespace llvm;
George Burgess IV1ca8aff2016-07-06 00:36:12 +000051using namespace llvm::cflaa;
George Burgess IVbfa401e2016-07-06 00:26:41 +000052
53#define DEBUG_TYPE "cfl-anders-aa"
54
George Burgess IV6d30aa02016-07-15 19:53:25 +000055CFLAndersAAResult::CFLAndersAAResult(const TargetLibraryInfo &TLI) : TLI(TLI) {}
56CFLAndersAAResult::CFLAndersAAResult(CFLAndersAAResult &&RHS)
57 : AAResultBase(std::move(RHS)), TLI(RHS.TLI) {}
58CFLAndersAAResult::~CFLAndersAAResult() {}
59
60static const Function *parentFunctionOfValue(const Value *Val) {
61 if (auto *Inst = dyn_cast<Instruction>(Val)) {
62 auto *Bb = Inst->getParent();
63 return Bb->getParent();
64 }
65
66 if (auto *Arg = dyn_cast<Argument>(Val))
67 return Arg->getParent();
68 return nullptr;
69}
70
71namespace {
72
73enum class MatchState : uint8_t {
74 FlowFrom = 0, // S1 in the paper
75 FlowFromMemAlias, // S2 in the paper
76 FlowTo, // S3 in the paper
77 FlowToMemAlias // S4 in the paper
78};
79
80// We use ReachabilitySet to keep track of value aliases (The nonterminal "V" in
81// the paper) during the analysis.
82class ReachabilitySet {
83 typedef std::bitset<4> StateSet;
84 typedef DenseMap<InstantiatedValue, StateSet> ValueStateMap;
85 typedef DenseMap<InstantiatedValue, ValueStateMap> ValueReachMap;
86 ValueReachMap ReachMap;
87
88public:
89 typedef ValueStateMap::const_iterator const_valuestate_iterator;
90 typedef ValueReachMap::const_iterator const_value_iterator;
91
92 // Insert edge 'From->To' at state 'State'
93 bool insert(InstantiatedValue From, InstantiatedValue To, MatchState State) {
94 auto &States = ReachMap[To][From];
95 auto Idx = static_cast<size_t>(State);
96 if (!States.test(Idx)) {
97 States.set(Idx);
98 return true;
99 }
100 return false;
101 }
102
103 // Return the set of all ('From', 'State') pair for a given node 'To'
104 iterator_range<const_valuestate_iterator>
105 reachableValueAliases(InstantiatedValue V) const {
106 auto Itr = ReachMap.find(V);
107 if (Itr == ReachMap.end())
108 return make_range<const_valuestate_iterator>(const_valuestate_iterator(),
109 const_valuestate_iterator());
110 return make_range<const_valuestate_iterator>(Itr->second.begin(),
111 Itr->second.end());
112 }
113
114 iterator_range<const_value_iterator> value_mappings() const {
115 return make_range<const_value_iterator>(ReachMap.begin(), ReachMap.end());
116 }
117};
118
119// We use AliasMemSet to keep track of all memory aliases (the nonterminal "M"
120// in the paper) during the analysis.
121class AliasMemSet {
122 typedef DenseSet<InstantiatedValue> MemSet;
123 typedef DenseMap<InstantiatedValue, MemSet> MemMapType;
124 MemMapType MemMap;
125
126public:
127 typedef MemSet::const_iterator const_mem_iterator;
128
129 bool insert(InstantiatedValue LHS, InstantiatedValue RHS) {
130 // Top-level values can never be memory aliases because one cannot take the
131 // addresses of them
132 assert(LHS.DerefLevel > 0 && RHS.DerefLevel > 0);
133 return MemMap[LHS].insert(RHS).second;
134 }
135
136 const MemSet *getMemoryAliases(InstantiatedValue V) const {
137 auto Itr = MemMap.find(V);
138 if (Itr == MemMap.end())
139 return nullptr;
140 return &Itr->second;
141 }
142};
143
144struct WorkListItem {
145 InstantiatedValue From;
146 InstantiatedValue To;
147 MatchState State;
148};
149}
150
151class CFLAndersAAResult::FunctionInfo {
152 /// Map a value to other values that may alias it
153 /// Since the alias relation is symmetric, to save some space we assume values
154 /// are properly ordered: if a and b alias each other, and a < b, then b is in
155 /// AliasMap[a] but not vice versa.
156 DenseMap<const Value *, std::vector<const Value *>> AliasMap;
157
158 /// Summary of externally visible effects.
159 AliasSummary Summary;
160
161public:
162 FunctionInfo(const ReachabilitySet &);
163
164 bool mayAlias(const Value *LHS, const Value *RHS) const;
165 const AliasSummary &getAliasSummary() const { return Summary; }
166};
167
168CFLAndersAAResult::FunctionInfo::FunctionInfo(const ReachabilitySet &ReachSet) {
169 for (const auto &OuterMapping : ReachSet.value_mappings()) {
170 // AliasMap only cares about top-level values
171 if (OuterMapping.first.DerefLevel > 0)
172 continue;
173
174 auto Val = OuterMapping.first.Val;
175 auto &AliasList = AliasMap[Val];
176 for (const auto &InnerMapping : OuterMapping.second) {
177 // Again, AliasMap only cares about top-level values
178 if (InnerMapping.first.DerefLevel == 0)
179 AliasList.push_back(InnerMapping.first.Val);
180 }
181
182 // Sort AliasList for faster lookup
183 std::sort(AliasList.begin(), AliasList.end(), std::less<const Value *>());
184 }
185
186 // TODO: Populate function summary here
187}
188
189bool CFLAndersAAResult::FunctionInfo::mayAlias(const Value *LHS,
190 const Value *RHS) const {
191 assert(LHS && RHS);
192
193 auto Itr = AliasMap.find(LHS);
194 if (Itr == AliasMap.end())
195 return false;
196
197 // TODO: Check AliasAttrs before drawing any conclusions
198
199 return std::binary_search(Itr->second.begin(), Itr->second.end(), RHS,
200 std::less<const Value *>());
201}
202
203static void propagate(InstantiatedValue From, InstantiatedValue To,
204 MatchState State, ReachabilitySet &ReachSet,
205 std::vector<WorkListItem> &WorkList) {
206 if (From == To)
207 return;
208 if (ReachSet.insert(From, To, State))
209 WorkList.push_back(WorkListItem{From, To, State});
210}
211
212static void initializeWorkList(std::vector<WorkListItem> &WorkList,
213 ReachabilitySet &ReachSet,
214 const CFLGraph &Graph) {
215 for (const auto &Mapping : Graph.value_mappings()) {
216 auto Val = Mapping.first;
217 auto &ValueInfo = Mapping.second;
218 assert(ValueInfo.getNumLevels() > 0);
219
220 // Insert all immediate assignment neighbors to the worklist
221 for (unsigned I = 0, E = ValueInfo.getNumLevels(); I < E; ++I) {
222 auto Src = InstantiatedValue{Val, I};
223 // If there's an assignment edge from X to Y, it means Y is reachable from
224 // X at S2 and X is reachable from Y at S1
225 for (auto &Edge : ValueInfo.getNodeInfoAtLevel(I).Edges) {
226 propagate(Edge.Other, Src, MatchState::FlowFrom, ReachSet, WorkList);
227 propagate(Src, Edge.Other, MatchState::FlowTo, ReachSet, WorkList);
228 }
229 }
230 }
231}
232
233static Optional<InstantiatedValue> getNodeBelow(const CFLGraph &Graph,
234 InstantiatedValue V) {
235 auto NodeBelow = InstantiatedValue{V.Val, V.DerefLevel + 1};
236 if (Graph.getNode(NodeBelow))
237 return NodeBelow;
238 return None;
239}
240
241static void processWorkListItem(const WorkListItem &Item, const CFLGraph &Graph,
242 ReachabilitySet &ReachSet, AliasMemSet &MemSet,
243 std::vector<WorkListItem> &WorkList) {
244 auto FromNode = Item.From;
245 auto ToNode = Item.To;
246
247 auto NodeInfo = Graph.getNode(ToNode);
248 assert(NodeInfo != nullptr);
249
250 // TODO: propagate AliasAttr as well
251 // TODO: propagate field offsets
252
253 // FIXME: Here is a neat trick we can do: since both ReachSet and MemSet holds
254 // relations that are symmetric, we could actually cut the storage by half by
255 // sorting FromNode and ToNode before insertion happens.
256
257 // The newly added value alias pair may pontentially generate more memory
258 // alias pairs. Check for them here.
259 auto FromNodeBelow = getNodeBelow(Graph, FromNode);
260 auto ToNodeBelow = getNodeBelow(Graph, ToNode);
261 if (FromNodeBelow && ToNodeBelow &&
262 MemSet.insert(*FromNodeBelow, *ToNodeBelow)) {
263 propagate(*FromNodeBelow, *ToNodeBelow, MatchState::FlowFromMemAlias,
264 ReachSet, WorkList);
265 for (const auto &Mapping : ReachSet.reachableValueAliases(*FromNodeBelow)) {
266 auto Src = Mapping.first;
267 if (Mapping.second.test(static_cast<size_t>(MatchState::FlowFrom)))
268 propagate(Src, *ToNodeBelow, MatchState::FlowFromMemAlias, ReachSet,
269 WorkList);
270 if (Mapping.second.test(static_cast<size_t>(MatchState::FlowTo)))
271 propagate(Src, *ToNodeBelow, MatchState::FlowToMemAlias, ReachSet,
272 WorkList);
273 }
274 }
275
276 // This is the core of the state machine walking algorithm. We expand ReachSet
277 // based on which state we are at (which in turn dictates what edges we
278 // should examine)
279 // From a high-level point of view, the state machine here guarantees two
280 // properties:
281 // - If *X and *Y are memory aliases, then X and Y are value aliases
282 // - If Y is an alias of X, then reverse assignment edges (if there is any)
283 // should precede any assignment edges on the path from X to Y.
284 switch (Item.State) {
285 case MatchState::FlowFrom: {
286 for (const auto &RevAssignEdge : NodeInfo->ReverseEdges)
287 propagate(FromNode, RevAssignEdge.Other, MatchState::FlowFrom, ReachSet,
288 WorkList);
289 for (const auto &AssignEdge : NodeInfo->Edges)
290 propagate(FromNode, AssignEdge.Other, MatchState::FlowTo, ReachSet,
291 WorkList);
292 if (auto AliasSet = MemSet.getMemoryAliases(ToNode)) {
293 for (const auto &MemAlias : *AliasSet)
294 propagate(FromNode, MemAlias, MatchState::FlowFromMemAlias, ReachSet,
295 WorkList);
296 }
297 break;
298 }
299 case MatchState::FlowFromMemAlias: {
300 for (const auto &RevAssignEdge : NodeInfo->ReverseEdges)
301 propagate(FromNode, RevAssignEdge.Other, MatchState::FlowFrom, ReachSet,
302 WorkList);
303 for (const auto &AssignEdge : NodeInfo->Edges)
304 propagate(FromNode, AssignEdge.Other, MatchState::FlowTo, ReachSet,
305 WorkList);
306 break;
307 }
308 case MatchState::FlowTo: {
309 for (const auto &AssignEdge : NodeInfo->Edges)
310 propagate(FromNode, AssignEdge.Other, MatchState::FlowTo, ReachSet,
311 WorkList);
312 if (auto AliasSet = MemSet.getMemoryAliases(ToNode)) {
313 for (const auto &MemAlias : *AliasSet)
314 propagate(FromNode, MemAlias, MatchState::FlowToMemAlias, ReachSet,
315 WorkList);
316 }
317 break;
318 }
319 case MatchState::FlowToMemAlias: {
320 for (const auto &AssignEdge : NodeInfo->Edges)
321 propagate(FromNode, AssignEdge.Other, MatchState::FlowTo, ReachSet,
322 WorkList);
323 break;
324 }
325 }
326}
327
328CFLAndersAAResult::FunctionInfo
329CFLAndersAAResult::buildInfoFrom(const Function &Fn) {
330 CFLGraphBuilder<CFLAndersAAResult> GraphBuilder(
331 *this, TLI,
332 // Cast away the constness here due to GraphBuilder's API requirement
333 const_cast<Function &>(Fn));
334 auto &Graph = GraphBuilder.getCFLGraph();
335
336 ReachabilitySet ReachSet;
337 AliasMemSet MemSet;
338
339 std::vector<WorkListItem> WorkList, NextList;
340 initializeWorkList(WorkList, ReachSet, Graph);
341 // TODO: make sure we don't stop before the fix point is reached
342 while (!WorkList.empty()) {
343 for (const auto &Item : WorkList)
344 processWorkListItem(Item, Graph, ReachSet, MemSet, NextList);
345
346 NextList.swap(WorkList);
347 NextList.clear();
348 }
349
350 return FunctionInfo(ReachSet);
351}
352
353void CFLAndersAAResult::scan(const Function &Fn) {
354 auto InsertPair = Cache.insert(std::make_pair(&Fn, Optional<FunctionInfo>()));
355 (void)InsertPair;
356 assert(InsertPair.second &&
357 "Trying to scan a function that has already been cached");
358
359 // Note that we can't do Cache[Fn] = buildSetsFrom(Fn) here: the function call
360 // may get evaluated after operator[], potentially triggering a DenseMap
361 // resize and invalidating the reference returned by operator[]
362 auto FunInfo = buildInfoFrom(Fn);
363 Cache[&Fn] = std::move(FunInfo);
364 Handles.push_front(FunctionHandle(const_cast<Function *>(&Fn), this));
365}
366
367void CFLAndersAAResult::evict(const Function &Fn) { Cache.erase(&Fn); }
368
369const Optional<CFLAndersAAResult::FunctionInfo> &
370CFLAndersAAResult::ensureCached(const Function &Fn) {
371 auto Iter = Cache.find(&Fn);
372 if (Iter == Cache.end()) {
373 scan(Fn);
374 Iter = Cache.find(&Fn);
375 assert(Iter != Cache.end());
376 assert(Iter->second.hasValue());
377 }
378 return Iter->second;
379}
380
381const AliasSummary *CFLAndersAAResult::getAliasSummary(const Function &Fn) {
382 auto &FunInfo = ensureCached(Fn);
383 if (FunInfo.hasValue())
384 return &FunInfo->getAliasSummary();
385 else
386 return nullptr;
387}
388
389AliasResult CFLAndersAAResult::query(const MemoryLocation &LocA,
390 const MemoryLocation &LocB) {
391 auto *ValA = LocA.Ptr;
392 auto *ValB = LocB.Ptr;
393
394 if (!ValA->getType()->isPointerTy() || !ValB->getType()->isPointerTy())
395 return NoAlias;
396
397 auto *Fn = parentFunctionOfValue(ValA);
398 if (!Fn) {
399 Fn = parentFunctionOfValue(ValB);
400 if (!Fn) {
401 // The only times this is known to happen are when globals + InlineAsm are
402 // involved
403 DEBUG(dbgs()
404 << "CFLAndersAA: could not extract parent function information.\n");
405 return MayAlias;
406 }
407 } else {
408 assert(!parentFunctionOfValue(ValB) || parentFunctionOfValue(ValB) == Fn);
409 }
410
411 assert(Fn != nullptr);
412 auto &FunInfo = ensureCached(*Fn);
413
414 // AliasMap lookup
415 if (FunInfo->mayAlias(ValA, ValB))
416 return MayAlias;
417 return NoAlias;
418}
419
420AliasResult CFLAndersAAResult::alias(const MemoryLocation &LocA,
421 const MemoryLocation &LocB) {
422 if (LocA.Ptr == LocB.Ptr)
423 return LocA.Size == LocB.Size ? MustAlias : PartialAlias;
424
425 // Comparisons between global variables and other constants should be
426 // handled by BasicAA.
427 // CFLAndersAA may report NoAlias when comparing a GlobalValue and
428 // ConstantExpr, but every query needs to have at least one Value tied to a
429 // Function, and neither GlobalValues nor ConstantExprs are.
430 if (isa<Constant>(LocA.Ptr) && isa<Constant>(LocB.Ptr))
431 return AAResultBase::alias(LocA, LocB);
432
433 AliasResult QueryResult = query(LocA, LocB);
434 if (QueryResult == MayAlias)
435 return AAResultBase::alias(LocA, LocB);
436
437 return QueryResult;
438}
George Burgess IVbfa401e2016-07-06 00:26:41 +0000439
440char CFLAndersAA::PassID;
441
442CFLAndersAAResult CFLAndersAA::run(Function &F, AnalysisManager<Function> &AM) {
George Burgess IV6d30aa02016-07-15 19:53:25 +0000443 return CFLAndersAAResult(AM.getResult<TargetLibraryAnalysis>(F));
George Burgess IVbfa401e2016-07-06 00:26:41 +0000444}
445
446char CFLAndersAAWrapperPass::ID = 0;
447INITIALIZE_PASS(CFLAndersAAWrapperPass, "cfl-anders-aa",
448 "Inclusion-Based CFL Alias Analysis", false, true)
449
450ImmutablePass *llvm::createCFLAndersAAWrapperPass() {
451 return new CFLAndersAAWrapperPass();
452}
453
454CFLAndersAAWrapperPass::CFLAndersAAWrapperPass() : ImmutablePass(ID) {
455 initializeCFLAndersAAWrapperPassPass(*PassRegistry::getPassRegistry());
456}
457
George Burgess IV6d30aa02016-07-15 19:53:25 +0000458void CFLAndersAAWrapperPass::initializePass() {
459 auto &TLIWP = getAnalysis<TargetLibraryInfoWrapperPass>();
460 Result.reset(new CFLAndersAAResult(TLIWP.getTLI()));
461}
George Burgess IVbfa401e2016-07-06 00:26:41 +0000462
463void CFLAndersAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
464 AU.setPreservesAll();
George Burgess IV6d30aa02016-07-15 19:53:25 +0000465 AU.addRequired<TargetLibraryInfoWrapperPass>();
George Burgess IVbfa401e2016-07-06 00:26:41 +0000466}