blob: fd90bd1521d69e9fe2eff48251e8329c374204fc [file] [log] [blame]
Eugene Zelenko530851c2017-08-11 21:30:02 +00001//===- CFLAndersAliasAnalysis.cpp - Unification-based Alias Analysis ------===//
George Burgess IVbfa401e2016-07-06 00:26:41 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
George Burgess IVbfa401e2016-07-06 00:26:41 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements a CFL-based, summary-based alias analysis algorithm. It
10// differs from CFLSteensAliasAnalysis in its inclusion-based nature while
11// CFLSteensAliasAnalysis is unification-based. This pass has worse performance
12// than CFLSteensAliasAnalysis (the worst case complexity of
13// CFLAndersAliasAnalysis is cubic, while the worst case complexity of
14// CFLSteensAliasAnalysis is almost linear), but it is able to yield more
15// precise analysis result. The precision of this analysis is roughly the same
16// as that of an one level context-sensitive Andersen's algorithm.
17//
George Burgess IV6d30aa02016-07-15 19:53:25 +000018// The algorithm used here is based on recursive state machine matching scheme
19// proposed in "Demand-driven alias analysis for C" by Xin Zheng and Radu
Vedant Kumar1a8456d2018-03-02 18:57:02 +000020// Rugina. The general idea is to extend the traditional transitive closure
George Burgess IV6d30aa02016-07-15 19:53:25 +000021// algorithm to perform CFL matching along the way: instead of recording
22// "whether X is reachable from Y", we keep track of "whether X is reachable
23// from Y at state Z", where the "state" field indicates where we are in the CFL
24// matching process. To understand the matching better, it is advisable to have
25// the state machine shown in Figure 3 of the paper available when reading the
26// codes: all we do here is to selectively expand the transitive closure by
27// discarding edges that are not recognized by the state machine.
28//
George Burgess IVc01b42f2016-07-19 20:38:21 +000029// There are two differences between our current implementation and the one
30// described in the paper:
31// - Our algorithm eagerly computes all alias pairs after the CFLGraph is built,
32// while in the paper the authors did the computation in a demand-driven
33// fashion. We did not implement the demand-driven algorithm due to the
34// additional coding complexity and higher memory profile, but if we found it
35// necessary we may switch to it eventually.
36// - In the paper the authors use a state machine that does not distinguish
37// value reads from value writes. For example, if Y is reachable from X at state
38// S3, it may be the case that X is written into Y, or it may be the case that
39// there's a third value Z that writes into both X and Y. To make that
40// distinction (which is crucial in building function summary as well as
41// retrieving mod-ref info), we choose to duplicate some of the states in the
42// paper's proposed state machine. The duplication does not change the set the
43// machine accepts. Given a pair of reachable values, it only provides more
44// detailed information on which value is being written into and which is being
45// read from.
George Burgess IV6d30aa02016-07-15 19:53:25 +000046//
George Burgess IVbfa401e2016-07-06 00:26:41 +000047//===----------------------------------------------------------------------===//
48
49// N.B. AliasAnalysis as a whole is phrased as a FunctionPass at the moment, and
50// CFLAndersAA is interprocedural. This is *technically* A Bad Thing, because
51// FunctionPasses are only allowed to inspect the Function that they're being
52// run on. Realistically, this likely isn't a problem until we allow
53// FunctionPasses to run concurrently.
54
55#include "llvm/Analysis/CFLAndersAliasAnalysis.h"
Eugene Zelenko530851c2017-08-11 21:30:02 +000056#include "AliasAnalysisSummary.h"
George Burgess IV1ca8aff2016-07-06 00:36:12 +000057#include "CFLGraph.h"
Eugene Zelenko530851c2017-08-11 21:30:02 +000058#include "llvm/ADT/DenseMap.h"
59#include "llvm/ADT/DenseMapInfo.h"
George Burgess IV6d30aa02016-07-15 19:53:25 +000060#include "llvm/ADT/DenseSet.h"
Eugene Zelenko530851c2017-08-11 21:30:02 +000061#include "llvm/ADT/None.h"
62#include "llvm/ADT/Optional.h"
63#include "llvm/ADT/STLExtras.h"
64#include "llvm/ADT/SmallVector.h"
65#include "llvm/ADT/iterator_range.h"
66#include "llvm/Analysis/AliasAnalysis.h"
67#include "llvm/Analysis/MemoryLocation.h"
68#include "llvm/IR/Argument.h"
69#include "llvm/IR/Function.h"
70#include "llvm/IR/PassManager.h"
71#include "llvm/IR/Type.h"
George Burgess IVbfa401e2016-07-06 00:26:41 +000072#include "llvm/Pass.h"
Eugene Zelenko530851c2017-08-11 21:30:02 +000073#include "llvm/Support/Casting.h"
74#include "llvm/Support/Compiler.h"
75#include "llvm/Support/Debug.h"
76#include "llvm/Support/raw_ostream.h"
77#include <algorithm>
78#include <bitset>
79#include <cassert>
80#include <cstddef>
81#include <cstdint>
82#include <functional>
83#include <utility>
84#include <vector>
George Burgess IVbfa401e2016-07-06 00:26:41 +000085
86using namespace llvm;
George Burgess IV1ca8aff2016-07-06 00:36:12 +000087using namespace llvm::cflaa;
George Burgess IVbfa401e2016-07-06 00:26:41 +000088
89#define DEBUG_TYPE "cfl-anders-aa"
90
Teresa Johnson9c27b592019-09-07 03:09:36 +000091CFLAndersAAResult::CFLAndersAAResult(
92 std::function<const TargetLibraryInfo &(Function &F)> GetTLI)
93 : GetTLI(std::move(GetTLI)) {}
George Burgess IV6d30aa02016-07-15 19:53:25 +000094CFLAndersAAResult::CFLAndersAAResult(CFLAndersAAResult &&RHS)
Teresa Johnson9c27b592019-09-07 03:09:36 +000095 : AAResultBase(std::move(RHS)), GetTLI(std::move(RHS.GetTLI)) {}
Eugene Zelenko530851c2017-08-11 21:30:02 +000096CFLAndersAAResult::~CFLAndersAAResult() = default;
George Burgess IV6d30aa02016-07-15 19:53:25 +000097
George Burgess IV6d30aa02016-07-15 19:53:25 +000098namespace {
99
100enum class MatchState : uint8_t {
George Burgess IVc01b42f2016-07-19 20:38:21 +0000101 // The following state represents S1 in the paper.
102 FlowFromReadOnly = 0,
103 // The following two states together represent S2 in the paper.
104 // The 'NoReadWrite' suffix indicates that there exists an alias path that
105 // does not contain assignment and reverse assignment edges.
106 // The 'ReadOnly' suffix indicates that there exists an alias path that
107 // contains reverse assignment edges only.
108 FlowFromMemAliasNoReadWrite,
109 FlowFromMemAliasReadOnly,
110 // The following two states together represent S3 in the paper.
111 // The 'WriteOnly' suffix indicates that there exists an alias path that
112 // contains assignment edges only.
113 // The 'ReadWrite' suffix indicates that there exists an alias path that
114 // contains both assignment and reverse assignment edges. Note that if X and Y
115 // are reachable at 'ReadWrite' state, it does NOT mean X is both read from
116 // and written to Y. Instead, it means that a third value Z is written to both
117 // X and Y.
118 FlowToWriteOnly,
119 FlowToReadWrite,
120 // The following two states together represent S4 in the paper.
121 FlowToMemAliasWriteOnly,
122 FlowToMemAliasReadWrite,
George Burgess IV6d30aa02016-07-15 19:53:25 +0000123};
124
Eugene Zelenko530851c2017-08-11 21:30:02 +0000125using StateSet = std::bitset<7>;
126
George Burgess IV381fc0e2016-08-25 01:05:08 +0000127const unsigned ReadOnlyStateMask =
George Burgess IV22a0f1a2016-07-19 21:35:47 +0000128 (1U << static_cast<uint8_t>(MatchState::FlowFromReadOnly)) |
129 (1U << static_cast<uint8_t>(MatchState::FlowFromMemAliasReadOnly));
George Burgess IV381fc0e2016-08-25 01:05:08 +0000130const unsigned WriteOnlyStateMask =
George Burgess IV22a0f1a2016-07-19 21:35:47 +0000131 (1U << static_cast<uint8_t>(MatchState::FlowToWriteOnly)) |
132 (1U << static_cast<uint8_t>(MatchState::FlowToMemAliasWriteOnly));
George Burgess IV3b059842016-07-19 20:47:15 +0000133
George Burgess IV4ec17532016-07-22 22:30:48 +0000134// A pair that consists of a value and an offset
135struct OffsetValue {
136 const Value *Val;
137 int64_t Offset;
138};
139
140bool operator==(OffsetValue LHS, OffsetValue RHS) {
141 return LHS.Val == RHS.Val && LHS.Offset == RHS.Offset;
142}
143bool operator<(OffsetValue LHS, OffsetValue RHS) {
144 return std::less<const Value *>()(LHS.Val, RHS.Val) ||
145 (LHS.Val == RHS.Val && LHS.Offset < RHS.Offset);
146}
147
148// A pair that consists of an InstantiatedValue and an offset
149struct OffsetInstantiatedValue {
150 InstantiatedValue IVal;
151 int64_t Offset;
152};
153
154bool operator==(OffsetInstantiatedValue LHS, OffsetInstantiatedValue RHS) {
155 return LHS.IVal == RHS.IVal && LHS.Offset == RHS.Offset;
156}
157
George Burgess IV6d30aa02016-07-15 19:53:25 +0000158// We use ReachabilitySet to keep track of value aliases (The nonterminal "V" in
159// the paper) during the analysis.
160class ReachabilitySet {
George Burgess IVc6526172018-05-17 21:56:39 +0000161 using ValueStateMap = DenseMap<InstantiatedValue, StateSet>;
Eugene Zelenko530851c2017-08-11 21:30:02 +0000162 using ValueReachMap = DenseMap<InstantiatedValue, ValueStateMap>;
163
George Burgess IV6d30aa02016-07-15 19:53:25 +0000164 ValueReachMap ReachMap;
165
166public:
Eugene Zelenko530851c2017-08-11 21:30:02 +0000167 using const_valuestate_iterator = ValueStateMap::const_iterator;
168 using const_value_iterator = ValueReachMap::const_iterator;
George Burgess IV6d30aa02016-07-15 19:53:25 +0000169
George Burgess IVc6526172018-05-17 21:56:39 +0000170 // Insert edge 'From->To' at state 'State'
171 bool insert(InstantiatedValue From, InstantiatedValue To, MatchState State) {
George Burgess IV3b059842016-07-19 20:47:15 +0000172 assert(From != To);
George Burgess IVc6526172018-05-17 21:56:39 +0000173 auto &States = ReachMap[To][From];
George Burgess IV6d30aa02016-07-15 19:53:25 +0000174 auto Idx = static_cast<size_t>(State);
175 if (!States.test(Idx)) {
176 States.set(Idx);
177 return true;
178 }
179 return false;
180 }
181
182 // Return the set of all ('From', 'State') pair for a given node 'To'
183 iterator_range<const_valuestate_iterator>
184 reachableValueAliases(InstantiatedValue V) const {
185 auto Itr = ReachMap.find(V);
186 if (Itr == ReachMap.end())
187 return make_range<const_valuestate_iterator>(const_valuestate_iterator(),
188 const_valuestate_iterator());
189 return make_range<const_valuestate_iterator>(Itr->second.begin(),
190 Itr->second.end());
191 }
192
193 iterator_range<const_value_iterator> value_mappings() const {
194 return make_range<const_value_iterator>(ReachMap.begin(), ReachMap.end());
195 }
196};
197
198// We use AliasMemSet to keep track of all memory aliases (the nonterminal "M"
199// in the paper) during the analysis.
200class AliasMemSet {
Eugene Zelenko530851c2017-08-11 21:30:02 +0000201 using MemSet = DenseSet<InstantiatedValue>;
202 using MemMapType = DenseMap<InstantiatedValue, MemSet>;
203
George Burgess IV6d30aa02016-07-15 19:53:25 +0000204 MemMapType MemMap;
205
206public:
Eugene Zelenko530851c2017-08-11 21:30:02 +0000207 using const_mem_iterator = MemSet::const_iterator;
George Burgess IV6d30aa02016-07-15 19:53:25 +0000208
209 bool insert(InstantiatedValue LHS, InstantiatedValue RHS) {
210 // Top-level values can never be memory aliases because one cannot take the
211 // addresses of them
212 assert(LHS.DerefLevel > 0 && RHS.DerefLevel > 0);
213 return MemMap[LHS].insert(RHS).second;
214 }
215
216 const MemSet *getMemoryAliases(InstantiatedValue V) const {
217 auto Itr = MemMap.find(V);
218 if (Itr == MemMap.end())
219 return nullptr;
220 return &Itr->second;
221 }
222};
223
George Burgess IV22682e22016-07-15 20:02:49 +0000224// We use AliasAttrMap to keep track of the AliasAttr of each node.
225class AliasAttrMap {
Eugene Zelenko530851c2017-08-11 21:30:02 +0000226 using MapType = DenseMap<InstantiatedValue, AliasAttrs>;
227
George Burgess IV22682e22016-07-15 20:02:49 +0000228 MapType AttrMap;
229
230public:
Eugene Zelenko530851c2017-08-11 21:30:02 +0000231 using const_iterator = MapType::const_iterator;
George Burgess IV22682e22016-07-15 20:02:49 +0000232
233 bool add(InstantiatedValue V, AliasAttrs Attr) {
George Burgess IV22682e22016-07-15 20:02:49 +0000234 auto &OldAttr = AttrMap[V];
235 auto NewAttr = OldAttr | Attr;
236 if (OldAttr == NewAttr)
237 return false;
238 OldAttr = NewAttr;
239 return true;
240 }
241
242 AliasAttrs getAttrs(InstantiatedValue V) const {
243 AliasAttrs Attr;
244 auto Itr = AttrMap.find(V);
245 if (Itr != AttrMap.end())
246 Attr = Itr->second;
247 return Attr;
248 }
249
250 iterator_range<const_iterator> mappings() const {
251 return make_range<const_iterator>(AttrMap.begin(), AttrMap.end());
252 }
253};
254
George Burgess IV6d30aa02016-07-15 19:53:25 +0000255struct WorkListItem {
256 InstantiatedValue From;
257 InstantiatedValue To;
258 MatchState State;
259};
George Burgess IV3b059842016-07-19 20:47:15 +0000260
261struct ValueSummary {
262 struct Record {
263 InterfaceValue IValue;
264 unsigned DerefLevel;
265 };
266 SmallVector<Record, 4> FromRecords, ToRecords;
267};
Eugene Zelenko530851c2017-08-11 21:30:02 +0000268
269} // end anonymous namespace
George Burgess IV6d30aa02016-07-15 19:53:25 +0000270
George Burgess IVc6526172018-05-17 21:56:39 +0000271namespace llvm {
272
273// Specialize DenseMapInfo for OffsetValue.
274template <> struct DenseMapInfo<OffsetValue> {
275 static OffsetValue getEmptyKey() {
276 return OffsetValue{DenseMapInfo<const Value *>::getEmptyKey(),
277 DenseMapInfo<int64_t>::getEmptyKey()};
278 }
279
280 static OffsetValue getTombstoneKey() {
281 return OffsetValue{DenseMapInfo<const Value *>::getTombstoneKey(),
282 DenseMapInfo<int64_t>::getEmptyKey()};
283 }
284
285 static unsigned getHashValue(const OffsetValue &OVal) {
286 return DenseMapInfo<std::pair<const Value *, int64_t>>::getHashValue(
287 std::make_pair(OVal.Val, OVal.Offset));
288 }
289
290 static bool isEqual(const OffsetValue &LHS, const OffsetValue &RHS) {
291 return LHS == RHS;
292 }
293};
294
295// Specialize DenseMapInfo for OffsetInstantiatedValue.
296template <> struct DenseMapInfo<OffsetInstantiatedValue> {
297 static OffsetInstantiatedValue getEmptyKey() {
298 return OffsetInstantiatedValue{
299 DenseMapInfo<InstantiatedValue>::getEmptyKey(),
300 DenseMapInfo<int64_t>::getEmptyKey()};
301 }
302
303 static OffsetInstantiatedValue getTombstoneKey() {
304 return OffsetInstantiatedValue{
305 DenseMapInfo<InstantiatedValue>::getTombstoneKey(),
306 DenseMapInfo<int64_t>::getEmptyKey()};
307 }
308
309 static unsigned getHashValue(const OffsetInstantiatedValue &OVal) {
310 return DenseMapInfo<std::pair<InstantiatedValue, int64_t>>::getHashValue(
311 std::make_pair(OVal.IVal, OVal.Offset));
312 }
313
314 static bool isEqual(const OffsetInstantiatedValue &LHS,
315 const OffsetInstantiatedValue &RHS) {
316 return LHS == RHS;
317 }
318};
319
320} // end namespace llvm
321
George Burgess IV6d30aa02016-07-15 19:53:25 +0000322class CFLAndersAAResult::FunctionInfo {
323 /// Map a value to other values that may alias it
324 /// Since the alias relation is symmetric, to save some space we assume values
325 /// are properly ordered: if a and b alias each other, and a < b, then b is in
326 /// AliasMap[a] but not vice versa.
George Burgess IV4ec17532016-07-22 22:30:48 +0000327 DenseMap<const Value *, std::vector<OffsetValue>> AliasMap;
George Burgess IV6d30aa02016-07-15 19:53:25 +0000328
George Burgess IV22682e22016-07-15 20:02:49 +0000329 /// Map a value to its corresponding AliasAttrs
330 DenseMap<const Value *, AliasAttrs> AttrMap;
331
George Burgess IV6d30aa02016-07-15 19:53:25 +0000332 /// Summary of externally visible effects.
333 AliasSummary Summary;
334
George Burgess IV777efb12016-08-02 22:17:25 +0000335 Optional<AliasAttrs> getAttrs(const Value *) const;
George Burgess IV22682e22016-07-15 20:02:49 +0000336
George Burgess IV6d30aa02016-07-15 19:53:25 +0000337public:
George Burgess IV3b059842016-07-19 20:47:15 +0000338 FunctionInfo(const Function &, const SmallVectorImpl<Value *> &,
Benjamin Kramer061f4a52017-01-13 14:39:03 +0000339 const ReachabilitySet &, const AliasAttrMap &);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000340
George Burgess IV319be3a2018-05-25 21:16:58 +0000341 bool mayAlias(const Value *, LocationSize, const Value *, LocationSize) const;
George Burgess IV6d30aa02016-07-15 19:53:25 +0000342 const AliasSummary &getAliasSummary() const { return Summary; }
343};
344
George Burgess IV3b059842016-07-19 20:47:15 +0000345static bool hasReadOnlyState(StateSet Set) {
George Burgess IV22a0f1a2016-07-19 21:35:47 +0000346 return (Set & StateSet(ReadOnlyStateMask)).any();
George Burgess IV3b059842016-07-19 20:47:15 +0000347}
348
349static bool hasWriteOnlyState(StateSet Set) {
George Burgess IV22a0f1a2016-07-19 21:35:47 +0000350 return (Set & StateSet(WriteOnlyStateMask)).any();
George Burgess IV3b059842016-07-19 20:47:15 +0000351}
352
353static Optional<InterfaceValue>
354getInterfaceValue(InstantiatedValue IValue,
355 const SmallVectorImpl<Value *> &RetVals) {
356 auto Val = IValue.Val;
357
358 Optional<unsigned> Index;
359 if (auto Arg = dyn_cast<Argument>(Val))
360 Index = Arg->getArgNo() + 1;
361 else if (is_contained(RetVals, Val))
362 Index = 0;
363
364 if (Index)
365 return InterfaceValue{*Index, IValue.DerefLevel};
366 return None;
367}
368
369static void populateAttrMap(DenseMap<const Value *, AliasAttrs> &AttrMap,
370 const AliasAttrMap &AMap) {
George Burgess IV22682e22016-07-15 20:02:49 +0000371 for (const auto &Mapping : AMap.mappings()) {
372 auto IVal = Mapping.first;
373
George Burgess IV4c582662016-08-01 18:27:33 +0000374 // Insert IVal into the map
375 auto &Attr = AttrMap[IVal.Val];
George Burgess IV22682e22016-07-15 20:02:49 +0000376 // AttrMap only cares about top-level values
377 if (IVal.DerefLevel == 0)
George Burgess IV4c582662016-08-01 18:27:33 +0000378 Attr |= Mapping.second;
George Burgess IV22682e22016-07-15 20:02:49 +0000379 }
George Burgess IV3b059842016-07-19 20:47:15 +0000380}
George Burgess IV22682e22016-07-15 20:02:49 +0000381
George Burgess IV3b059842016-07-19 20:47:15 +0000382static void
George Burgess IV4ec17532016-07-22 22:30:48 +0000383populateAliasMap(DenseMap<const Value *, std::vector<OffsetValue>> &AliasMap,
George Burgess IV3b059842016-07-19 20:47:15 +0000384 const ReachabilitySet &ReachSet) {
George Burgess IV6d30aa02016-07-15 19:53:25 +0000385 for (const auto &OuterMapping : ReachSet.value_mappings()) {
386 // AliasMap only cares about top-level values
387 if (OuterMapping.first.DerefLevel > 0)
388 continue;
389
390 auto Val = OuterMapping.first.Val;
391 auto &AliasList = AliasMap[Val];
392 for (const auto &InnerMapping : OuterMapping.second) {
393 // Again, AliasMap only cares about top-level values
George Burgess IVc6526172018-05-17 21:56:39 +0000394 if (InnerMapping.first.DerefLevel == 0)
395 AliasList.push_back(OffsetValue{InnerMapping.first.Val, UnknownOffset});
George Burgess IV6d30aa02016-07-15 19:53:25 +0000396 }
397
398 // Sort AliasList for faster lookup
Fangrui Song0cac7262018-09-27 02:13:45 +0000399 llvm::sort(AliasList);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000400 }
George Burgess IV3b059842016-07-19 20:47:15 +0000401}
George Burgess IV6d30aa02016-07-15 19:53:25 +0000402
George Burgess IV3b059842016-07-19 20:47:15 +0000403static void populateExternalRelations(
404 SmallVectorImpl<ExternalRelation> &ExtRelations, const Function &Fn,
405 const SmallVectorImpl<Value *> &RetVals, const ReachabilitySet &ReachSet) {
406 // If a function only returns one of its argument X, then X will be both an
407 // argument and a return value at the same time. This is an edge case that
408 // needs special handling here.
409 for (const auto &Arg : Fn.args()) {
410 if (is_contained(RetVals, &Arg)) {
411 auto ArgVal = InterfaceValue{Arg.getArgNo() + 1, 0};
412 auto RetVal = InterfaceValue{0, 0};
George Burgess IV4ec17532016-07-22 22:30:48 +0000413 ExtRelations.push_back(ExternalRelation{ArgVal, RetVal, 0});
George Burgess IV3b059842016-07-19 20:47:15 +0000414 }
415 }
416
417 // Below is the core summary construction logic.
418 // A naive solution of adding only the value aliases that are parameters or
419 // return values in ReachSet to the summary won't work: It is possible that a
420 // parameter P is written into an intermediate value I, and the function
421 // subsequently returns *I. In that case, *I is does not value alias anything
422 // in ReachSet, and the naive solution will miss a summary edge from (P, 1) to
423 // (I, 1).
424 // To account for the aforementioned case, we need to check each non-parameter
425 // and non-return value for the possibility of acting as an intermediate.
426 // 'ValueMap' here records, for each value, which InterfaceValues read from or
427 // write into it. If both the read list and the write list of a given value
428 // are non-empty, we know that a particular value is an intermidate and we
429 // need to add summary edges from the writes to the reads.
430 DenseMap<Value *, ValueSummary> ValueMap;
431 for (const auto &OuterMapping : ReachSet.value_mappings()) {
432 if (auto Dst = getInterfaceValue(OuterMapping.first, RetVals)) {
433 for (const auto &InnerMapping : OuterMapping.second) {
434 // If Src is a param/return value, we get a same-level assignment.
George Burgess IVc6526172018-05-17 21:56:39 +0000435 if (auto Src = getInterfaceValue(InnerMapping.first, RetVals)) {
George Burgess IV3b059842016-07-19 20:47:15 +0000436 // This may happen if both Dst and Src are return values
437 if (*Dst == *Src)
438 continue;
439
440 if (hasReadOnlyState(InnerMapping.second))
George Burgess IVc6526172018-05-17 21:56:39 +0000441 ExtRelations.push_back(ExternalRelation{*Dst, *Src, UnknownOffset});
George Burgess IV3b059842016-07-19 20:47:15 +0000442 // No need to check for WriteOnly state, since ReachSet is symmetric
443 } else {
444 // If Src is not a param/return, add it to ValueMap
George Burgess IVc6526172018-05-17 21:56:39 +0000445 auto SrcIVal = InnerMapping.first;
George Burgess IV3b059842016-07-19 20:47:15 +0000446 if (hasReadOnlyState(InnerMapping.second))
447 ValueMap[SrcIVal.Val].FromRecords.push_back(
George Burgess IVc6526172018-05-17 21:56:39 +0000448 ValueSummary::Record{*Dst, SrcIVal.DerefLevel});
George Burgess IV3b059842016-07-19 20:47:15 +0000449 if (hasWriteOnlyState(InnerMapping.second))
450 ValueMap[SrcIVal.Val].ToRecords.push_back(
George Burgess IVc6526172018-05-17 21:56:39 +0000451 ValueSummary::Record{*Dst, SrcIVal.DerefLevel});
George Burgess IV3b059842016-07-19 20:47:15 +0000452 }
453 }
454 }
455 }
456
457 for (const auto &Mapping : ValueMap) {
458 for (const auto &FromRecord : Mapping.second.FromRecords) {
459 for (const auto &ToRecord : Mapping.second.ToRecords) {
460 auto ToLevel = ToRecord.DerefLevel;
461 auto FromLevel = FromRecord.DerefLevel;
462 // Same-level assignments should have already been processed by now
463 if (ToLevel == FromLevel)
464 continue;
465
466 auto SrcIndex = FromRecord.IValue.Index;
467 auto SrcLevel = FromRecord.IValue.DerefLevel;
468 auto DstIndex = ToRecord.IValue.Index;
469 auto DstLevel = ToRecord.IValue.DerefLevel;
470 if (ToLevel > FromLevel)
471 SrcLevel += ToLevel - FromLevel;
472 else
473 DstLevel += FromLevel - ToLevel;
474
George Burgess IVc6526172018-05-17 21:56:39 +0000475 ExtRelations.push_back(ExternalRelation{
476 InterfaceValue{SrcIndex, SrcLevel},
477 InterfaceValue{DstIndex, DstLevel}, UnknownOffset});
George Burgess IV3b059842016-07-19 20:47:15 +0000478 }
479 }
480 }
481
482 // Remove duplicates in ExtRelations
Fangrui Song0cac7262018-09-27 02:13:45 +0000483 llvm::sort(ExtRelations);
George Burgess IV3b059842016-07-19 20:47:15 +0000484 ExtRelations.erase(std::unique(ExtRelations.begin(), ExtRelations.end()),
485 ExtRelations.end());
486}
487
488static void populateExternalAttributes(
489 SmallVectorImpl<ExternalAttribute> &ExtAttributes, const Function &Fn,
490 const SmallVectorImpl<Value *> &RetVals, const AliasAttrMap &AMap) {
491 for (const auto &Mapping : AMap.mappings()) {
492 if (auto IVal = getInterfaceValue(Mapping.first, RetVals)) {
493 auto Attr = getExternallyVisibleAttrs(Mapping.second);
494 if (Attr.any())
495 ExtAttributes.push_back(ExternalAttribute{*IVal, Attr});
496 }
497 }
498}
499
500CFLAndersAAResult::FunctionInfo::FunctionInfo(
501 const Function &Fn, const SmallVectorImpl<Value *> &RetVals,
Benjamin Kramer061f4a52017-01-13 14:39:03 +0000502 const ReachabilitySet &ReachSet, const AliasAttrMap &AMap) {
George Burgess IV3b059842016-07-19 20:47:15 +0000503 populateAttrMap(AttrMap, AMap);
504 populateExternalAttributes(Summary.RetParamAttributes, Fn, RetVals, AMap);
505 populateAliasMap(AliasMap, ReachSet);
506 populateExternalRelations(Summary.RetParamRelations, Fn, RetVals, ReachSet);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000507}
508
George Burgess IV777efb12016-08-02 22:17:25 +0000509Optional<AliasAttrs>
510CFLAndersAAResult::FunctionInfo::getAttrs(const Value *V) const {
George Burgess IV22682e22016-07-15 20:02:49 +0000511 assert(V != nullptr);
512
George Burgess IV22682e22016-07-15 20:02:49 +0000513 auto Itr = AttrMap.find(V);
514 if (Itr != AttrMap.end())
George Burgess IV777efb12016-08-02 22:17:25 +0000515 return Itr->second;
516 return None;
George Burgess IV22682e22016-07-15 20:02:49 +0000517}
518
George Burgess IVfefc42c2018-10-09 02:14:33 +0000519bool CFLAndersAAResult::FunctionInfo::mayAlias(
520 const Value *LHS, LocationSize MaybeLHSSize, const Value *RHS,
521 LocationSize MaybeRHSSize) const {
George Burgess IV6d30aa02016-07-15 19:53:25 +0000522 assert(LHS && RHS);
523
George Burgess IV777efb12016-08-02 22:17:25 +0000524 // Check if we've seen LHS and RHS before. Sometimes LHS or RHS can be created
525 // after the analysis gets executed, and we want to be conservative in those
526 // cases.
527 auto MaybeAttrsA = getAttrs(LHS);
528 auto MaybeAttrsB = getAttrs(RHS);
529 if (!MaybeAttrsA || !MaybeAttrsB)
530 return true;
531
532 // Check AliasAttrs before AliasMap lookup since it's cheaper
533 auto AttrsA = *MaybeAttrsA;
534 auto AttrsB = *MaybeAttrsB;
George Burgess IV4ec17532016-07-22 22:30:48 +0000535 if (hasUnknownOrCallerAttr(AttrsA))
536 return AttrsB.any();
537 if (hasUnknownOrCallerAttr(AttrsB))
538 return AttrsA.any();
539 if (isGlobalOrArgAttr(AttrsA))
540 return isGlobalOrArgAttr(AttrsB);
541 if (isGlobalOrArgAttr(AttrsB))
542 return isGlobalOrArgAttr(AttrsA);
543
544 // At this point both LHS and RHS should point to locally allocated objects
545
George Burgess IV6d30aa02016-07-15 19:53:25 +0000546 auto Itr = AliasMap.find(LHS);
George Burgess IV22682e22016-07-15 20:02:49 +0000547 if (Itr != AliasMap.end()) {
George Burgess IV4ec17532016-07-22 22:30:48 +0000548
549 // Find out all (X, Offset) where X == RHS
550 auto Comparator = [](OffsetValue LHS, OffsetValue RHS) {
551 return std::less<const Value *>()(LHS.Val, RHS.Val);
552 };
553#ifdef EXPENSIVE_CHECKS
554 assert(std::is_sorted(Itr->second.begin(), Itr->second.end(), Comparator));
555#endif
556 auto RangePair = std::equal_range(Itr->second.begin(), Itr->second.end(),
557 OffsetValue{RHS, 0}, Comparator);
558
559 if (RangePair.first != RangePair.second) {
George Burgess IV6ef80022018-10-10 21:28:44 +0000560 // Be conservative about unknown sizes
561 if (MaybeLHSSize == LocationSize::unknown() ||
562 MaybeRHSSize == LocationSize::unknown())
George Burgess IV4ec17532016-07-22 22:30:48 +0000563 return true;
564
George Burgess IVf96e6182018-10-09 03:18:56 +0000565 const uint64_t LHSSize = MaybeLHSSize.getValue();
566 const uint64_t RHSSize = MaybeRHSSize.getValue();
George Burgess IVfefc42c2018-10-09 02:14:33 +0000567
George Burgess IV4ec17532016-07-22 22:30:48 +0000568 for (const auto &OVal : make_range(RangePair)) {
569 // Be conservative about UnknownOffset
570 if (OVal.Offset == UnknownOffset)
571 return true;
572
573 // We know that LHS aliases (RHS + OVal.Offset) if the control flow
574 // reaches here. The may-alias query essentially becomes integer
575 // range-overlap queries over two ranges [OVal.Offset, OVal.Offset +
576 // LHSSize) and [0, RHSSize).
577
578 // Try to be conservative on super large offsets
579 if (LLVM_UNLIKELY(LHSSize > INT64_MAX || RHSSize > INT64_MAX))
580 return true;
581
582 auto LHSStart = OVal.Offset;
583 // FIXME: Do we need to guard against integer overflow?
584 auto LHSEnd = OVal.Offset + static_cast<int64_t>(LHSSize);
585 auto RHSStart = 0;
586 auto RHSEnd = static_cast<int64_t>(RHSSize);
587 if (LHSEnd > RHSStart && LHSStart < RHSEnd)
588 return true;
589 }
590 }
George Burgess IV22682e22016-07-15 20:02:49 +0000591 }
592
George Burgess IV22682e22016-07-15 20:02:49 +0000593 return false;
George Burgess IV6d30aa02016-07-15 19:53:25 +0000594}
595
596static void propagate(InstantiatedValue From, InstantiatedValue To,
George Burgess IVc6526172018-05-17 21:56:39 +0000597 MatchState State, ReachabilitySet &ReachSet,
George Burgess IV6d30aa02016-07-15 19:53:25 +0000598 std::vector<WorkListItem> &WorkList) {
599 if (From == To)
600 return;
George Burgess IVc6526172018-05-17 21:56:39 +0000601 if (ReachSet.insert(From, To, State))
602 WorkList.push_back(WorkListItem{From, To, State});
George Burgess IV6d30aa02016-07-15 19:53:25 +0000603}
604
605static void initializeWorkList(std::vector<WorkListItem> &WorkList,
606 ReachabilitySet &ReachSet,
607 const CFLGraph &Graph) {
608 for (const auto &Mapping : Graph.value_mappings()) {
609 auto Val = Mapping.first;
610 auto &ValueInfo = Mapping.second;
611 assert(ValueInfo.getNumLevels() > 0);
612
613 // Insert all immediate assignment neighbors to the worklist
614 for (unsigned I = 0, E = ValueInfo.getNumLevels(); I < E; ++I) {
615 auto Src = InstantiatedValue{Val, I};
George Burgess IVc6526172018-05-17 21:56:39 +0000616 // If there's an assignment edge from X to Y, it means Y is reachable from
George Burgess IV4ea679f2019-03-08 19:28:55 +0000617 // X at S3 and X is reachable from Y at S1
George Burgess IV6d30aa02016-07-15 19:53:25 +0000618 for (auto &Edge : ValueInfo.getNodeInfoAtLevel(I).Edges) {
George Burgess IVc6526172018-05-17 21:56:39 +0000619 propagate(Edge.Other, Src, MatchState::FlowFromReadOnly, ReachSet,
620 WorkList);
621 propagate(Src, Edge.Other, MatchState::FlowToWriteOnly, ReachSet,
622 WorkList);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000623 }
624 }
625 }
626}
627
628static Optional<InstantiatedValue> getNodeBelow(const CFLGraph &Graph,
629 InstantiatedValue V) {
630 auto NodeBelow = InstantiatedValue{V.Val, V.DerefLevel + 1};
631 if (Graph.getNode(NodeBelow))
632 return NodeBelow;
633 return None;
634}
635
636static void processWorkListItem(const WorkListItem &Item, const CFLGraph &Graph,
637 ReachabilitySet &ReachSet, AliasMemSet &MemSet,
638 std::vector<WorkListItem> &WorkList) {
639 auto FromNode = Item.From;
640 auto ToNode = Item.To;
641
642 auto NodeInfo = Graph.getNode(ToNode);
643 assert(NodeInfo != nullptr);
644
George Burgess IVc6526172018-05-17 21:56:39 +0000645 // TODO: propagate field offsets
646
George Burgess IV6d30aa02016-07-15 19:53:25 +0000647 // FIXME: Here is a neat trick we can do: since both ReachSet and MemSet holds
648 // relations that are symmetric, we could actually cut the storage by half by
649 // sorting FromNode and ToNode before insertion happens.
650
Vedant Kumar1a8456d2018-03-02 18:57:02 +0000651 // The newly added value alias pair may potentially generate more memory
George Burgess IV6d30aa02016-07-15 19:53:25 +0000652 // alias pairs. Check for them here.
George Burgess IVc6526172018-05-17 21:56:39 +0000653 auto FromNodeBelow = getNodeBelow(Graph, FromNode);
654 auto ToNodeBelow = getNodeBelow(Graph, ToNode);
655 if (FromNodeBelow && ToNodeBelow &&
656 MemSet.insert(*FromNodeBelow, *ToNodeBelow)) {
657 propagate(*FromNodeBelow, *ToNodeBelow,
658 MatchState::FlowFromMemAliasNoReadWrite, ReachSet, WorkList);
659 for (const auto &Mapping : ReachSet.reachableValueAliases(*FromNodeBelow)) {
660 auto Src = Mapping.first;
661 auto MemAliasPropagate = [&](MatchState FromState, MatchState ToState) {
662 if (Mapping.second.test(static_cast<size_t>(FromState)))
663 propagate(Src, *ToNodeBelow, ToState, ReachSet, WorkList);
664 };
George Burgess IVc01b42f2016-07-19 20:38:21 +0000665
George Burgess IVc6526172018-05-17 21:56:39 +0000666 MemAliasPropagate(MatchState::FlowFromReadOnly,
667 MatchState::FlowFromMemAliasReadOnly);
668 MemAliasPropagate(MatchState::FlowToWriteOnly,
669 MatchState::FlowToMemAliasWriteOnly);
670 MemAliasPropagate(MatchState::FlowToReadWrite,
671 MatchState::FlowToMemAliasReadWrite);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000672 }
673 }
674
675 // This is the core of the state machine walking algorithm. We expand ReachSet
676 // based on which state we are at (which in turn dictates what edges we
677 // should examine)
678 // From a high-level point of view, the state machine here guarantees two
679 // properties:
680 // - If *X and *Y are memory aliases, then X and Y are value aliases
681 // - If Y is an alias of X, then reverse assignment edges (if there is any)
682 // should precede any assignment edges on the path from X to Y.
George Burgess IVc01b42f2016-07-19 20:38:21 +0000683 auto NextAssignState = [&](MatchState State) {
George Burgess IVc6526172018-05-17 21:56:39 +0000684 for (const auto &AssignEdge : NodeInfo->Edges)
685 propagate(FromNode, AssignEdge.Other, State, ReachSet, WorkList);
George Burgess IVc01b42f2016-07-19 20:38:21 +0000686 };
687 auto NextRevAssignState = [&](MatchState State) {
George Burgess IVc6526172018-05-17 21:56:39 +0000688 for (const auto &RevAssignEdge : NodeInfo->ReverseEdges)
689 propagate(FromNode, RevAssignEdge.Other, State, ReachSet, WorkList);
George Burgess IVc01b42f2016-07-19 20:38:21 +0000690 };
691 auto NextMemState = [&](MatchState State) {
692 if (auto AliasSet = MemSet.getMemoryAliases(ToNode)) {
693 for (const auto &MemAlias : *AliasSet)
George Burgess IVc6526172018-05-17 21:56:39 +0000694 propagate(FromNode, MemAlias, State, ReachSet, WorkList);
George Burgess IVc01b42f2016-07-19 20:38:21 +0000695 }
696 };
697
George Burgess IV6d30aa02016-07-15 19:53:25 +0000698 switch (Item.State) {
Eugene Zelenko530851c2017-08-11 21:30:02 +0000699 case MatchState::FlowFromReadOnly:
George Burgess IVc01b42f2016-07-19 20:38:21 +0000700 NextRevAssignState(MatchState::FlowFromReadOnly);
701 NextAssignState(MatchState::FlowToReadWrite);
702 NextMemState(MatchState::FlowFromMemAliasReadOnly);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000703 break;
Eugene Zelenko530851c2017-08-11 21:30:02 +0000704
705 case MatchState::FlowFromMemAliasNoReadWrite:
George Burgess IVc01b42f2016-07-19 20:38:21 +0000706 NextRevAssignState(MatchState::FlowFromReadOnly);
707 NextAssignState(MatchState::FlowToWriteOnly);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000708 break;
Eugene Zelenko530851c2017-08-11 21:30:02 +0000709
710 case MatchState::FlowFromMemAliasReadOnly:
George Burgess IVc01b42f2016-07-19 20:38:21 +0000711 NextRevAssignState(MatchState::FlowFromReadOnly);
712 NextAssignState(MatchState::FlowToReadWrite);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000713 break;
Eugene Zelenko530851c2017-08-11 21:30:02 +0000714
715 case MatchState::FlowToWriteOnly:
George Burgess IVc01b42f2016-07-19 20:38:21 +0000716 NextAssignState(MatchState::FlowToWriteOnly);
717 NextMemState(MatchState::FlowToMemAliasWriteOnly);
718 break;
Eugene Zelenko530851c2017-08-11 21:30:02 +0000719
720 case MatchState::FlowToReadWrite:
George Burgess IVc01b42f2016-07-19 20:38:21 +0000721 NextAssignState(MatchState::FlowToReadWrite);
722 NextMemState(MatchState::FlowToMemAliasReadWrite);
723 break;
Eugene Zelenko530851c2017-08-11 21:30:02 +0000724
725 case MatchState::FlowToMemAliasWriteOnly:
George Burgess IVc01b42f2016-07-19 20:38:21 +0000726 NextAssignState(MatchState::FlowToWriteOnly);
727 break;
Eugene Zelenko530851c2017-08-11 21:30:02 +0000728
729 case MatchState::FlowToMemAliasReadWrite:
George Burgess IVc01b42f2016-07-19 20:38:21 +0000730 NextAssignState(MatchState::FlowToReadWrite);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000731 break;
732 }
George Burgess IV6d30aa02016-07-15 19:53:25 +0000733}
734
George Burgess IV22682e22016-07-15 20:02:49 +0000735static AliasAttrMap buildAttrMap(const CFLGraph &Graph,
736 const ReachabilitySet &ReachSet) {
737 AliasAttrMap AttrMap;
738 std::vector<InstantiatedValue> WorkList, NextList;
739
740 // Initialize each node with its original AliasAttrs in CFLGraph
741 for (const auto &Mapping : Graph.value_mappings()) {
742 auto Val = Mapping.first;
743 auto &ValueInfo = Mapping.second;
744 for (unsigned I = 0, E = ValueInfo.getNumLevels(); I < E; ++I) {
745 auto Node = InstantiatedValue{Val, I};
746 AttrMap.add(Node, ValueInfo.getNodeInfoAtLevel(I).Attr);
747 WorkList.push_back(Node);
748 }
749 }
750
751 while (!WorkList.empty()) {
752 for (const auto &Dst : WorkList) {
753 auto DstAttr = AttrMap.getAttrs(Dst);
754 if (DstAttr.none())
755 continue;
756
757 // Propagate attr on the same level
758 for (const auto &Mapping : ReachSet.reachableValueAliases(Dst)) {
George Burgess IVc6526172018-05-17 21:56:39 +0000759 auto Src = Mapping.first;
George Burgess IV22682e22016-07-15 20:02:49 +0000760 if (AttrMap.add(Src, DstAttr))
761 NextList.push_back(Src);
762 }
763
764 // Propagate attr to the levels below
765 auto DstBelow = getNodeBelow(Graph, Dst);
766 while (DstBelow) {
767 if (AttrMap.add(*DstBelow, DstAttr)) {
768 NextList.push_back(*DstBelow);
769 break;
770 }
771 DstBelow = getNodeBelow(Graph, *DstBelow);
772 }
773 }
774 WorkList.swap(NextList);
775 NextList.clear();
776 }
777
778 return AttrMap;
779}
780
George Burgess IV6d30aa02016-07-15 19:53:25 +0000781CFLAndersAAResult::FunctionInfo
782CFLAndersAAResult::buildInfoFrom(const Function &Fn) {
783 CFLGraphBuilder<CFLAndersAAResult> GraphBuilder(
Teresa Johnson9c27b592019-09-07 03:09:36 +0000784 *this, GetTLI(const_cast<Function &>(Fn)),
George Burgess IV6d30aa02016-07-15 19:53:25 +0000785 // Cast away the constness here due to GraphBuilder's API requirement
786 const_cast<Function &>(Fn));
787 auto &Graph = GraphBuilder.getCFLGraph();
788
789 ReachabilitySet ReachSet;
790 AliasMemSet MemSet;
791
792 std::vector<WorkListItem> WorkList, NextList;
793 initializeWorkList(WorkList, ReachSet, Graph);
George Burgess IVc6526172018-05-17 21:56:39 +0000794 // TODO: make sure we don't stop before the fix point is reached
George Burgess IV6d30aa02016-07-15 19:53:25 +0000795 while (!WorkList.empty()) {
796 for (const auto &Item : WorkList)
797 processWorkListItem(Item, Graph, ReachSet, MemSet, NextList);
798
799 NextList.swap(WorkList);
800 NextList.clear();
801 }
802
George Burgess IV22682e22016-07-15 20:02:49 +0000803 // Now that we have all the reachability info, propagate AliasAttrs according
804 // to it
805 auto IValueAttrMap = buildAttrMap(Graph, ReachSet);
806
George Burgess IV3b059842016-07-19 20:47:15 +0000807 return FunctionInfo(Fn, GraphBuilder.getReturnValues(), ReachSet,
808 std::move(IValueAttrMap));
George Burgess IV6d30aa02016-07-15 19:53:25 +0000809}
810
811void CFLAndersAAResult::scan(const Function &Fn) {
812 auto InsertPair = Cache.insert(std::make_pair(&Fn, Optional<FunctionInfo>()));
813 (void)InsertPair;
814 assert(InsertPair.second &&
815 "Trying to scan a function that has already been cached");
816
817 // Note that we can't do Cache[Fn] = buildSetsFrom(Fn) here: the function call
818 // may get evaluated after operator[], potentially triggering a DenseMap
819 // resize and invalidating the reference returned by operator[]
820 auto FunInfo = buildInfoFrom(Fn);
821 Cache[&Fn] = std::move(FunInfo);
Davide Italianoe34a8062017-06-26 23:59:14 +0000822 Handles.emplace_front(const_cast<Function *>(&Fn), this);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000823}
824
Davide Italianoe34a8062017-06-26 23:59:14 +0000825void CFLAndersAAResult::evict(const Function *Fn) { Cache.erase(Fn); }
George Burgess IV6d30aa02016-07-15 19:53:25 +0000826
827const Optional<CFLAndersAAResult::FunctionInfo> &
828CFLAndersAAResult::ensureCached(const Function &Fn) {
829 auto Iter = Cache.find(&Fn);
830 if (Iter == Cache.end()) {
831 scan(Fn);
832 Iter = Cache.find(&Fn);
833 assert(Iter != Cache.end());
834 assert(Iter->second.hasValue());
835 }
836 return Iter->second;
837}
838
839const AliasSummary *CFLAndersAAResult::getAliasSummary(const Function &Fn) {
840 auto &FunInfo = ensureCached(Fn);
841 if (FunInfo.hasValue())
842 return &FunInfo->getAliasSummary();
843 else
844 return nullptr;
845}
846
847AliasResult CFLAndersAAResult::query(const MemoryLocation &LocA,
848 const MemoryLocation &LocB) {
849 auto *ValA = LocA.Ptr;
850 auto *ValB = LocB.Ptr;
851
852 if (!ValA->getType()->isPointerTy() || !ValB->getType()->isPointerTy())
853 return NoAlias;
854
855 auto *Fn = parentFunctionOfValue(ValA);
856 if (!Fn) {
857 Fn = parentFunctionOfValue(ValB);
858 if (!Fn) {
859 // The only times this is known to happen are when globals + InlineAsm are
860 // involved
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000861 LLVM_DEBUG(
862 dbgs()
863 << "CFLAndersAA: could not extract parent function information.\n");
George Burgess IV6d30aa02016-07-15 19:53:25 +0000864 return MayAlias;
865 }
866 } else {
867 assert(!parentFunctionOfValue(ValB) || parentFunctionOfValue(ValB) == Fn);
868 }
869
870 assert(Fn != nullptr);
871 auto &FunInfo = ensureCached(*Fn);
872
873 // AliasMap lookup
George Burgess IV4ec17532016-07-22 22:30:48 +0000874 if (FunInfo->mayAlias(ValA, LocA.Size, ValB, LocB.Size))
George Burgess IV6d30aa02016-07-15 19:53:25 +0000875 return MayAlias;
876 return NoAlias;
877}
878
879AliasResult CFLAndersAAResult::alias(const MemoryLocation &LocA,
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000880 const MemoryLocation &LocB,
881 AAQueryInfo &AAQI) {
George Burgess IV6d30aa02016-07-15 19:53:25 +0000882 if (LocA.Ptr == LocB.Ptr)
Nuno Lopes78295062017-08-09 17:02:18 +0000883 return MustAlias;
George Burgess IV6d30aa02016-07-15 19:53:25 +0000884
885 // Comparisons between global variables and other constants should be
886 // handled by BasicAA.
887 // CFLAndersAA may report NoAlias when comparing a GlobalValue and
888 // ConstantExpr, but every query needs to have at least one Value tied to a
889 // Function, and neither GlobalValues nor ConstantExprs are.
890 if (isa<Constant>(LocA.Ptr) && isa<Constant>(LocB.Ptr))
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000891 return AAResultBase::alias(LocA, LocB, AAQI);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000892
893 AliasResult QueryResult = query(LocA, LocB);
894 if (QueryResult == MayAlias)
Alina Sbirleabfc779e2019-03-22 17:22:19 +0000895 return AAResultBase::alias(LocA, LocB, AAQI);
George Burgess IV6d30aa02016-07-15 19:53:25 +0000896
897 return QueryResult;
898}
George Burgess IVbfa401e2016-07-06 00:26:41 +0000899
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000900AnalysisKey CFLAndersAA::Key;
George Burgess IVbfa401e2016-07-06 00:26:41 +0000901
Sean Silva36e0d012016-08-09 00:28:15 +0000902CFLAndersAAResult CFLAndersAA::run(Function &F, FunctionAnalysisManager &AM) {
Teresa Johnson9c27b592019-09-07 03:09:36 +0000903 auto GetTLI = [&AM](Function &F) -> TargetLibraryInfo & {
904 return AM.getResult<TargetLibraryAnalysis>(F);
905 };
906 return CFLAndersAAResult(GetTLI);
George Burgess IVbfa401e2016-07-06 00:26:41 +0000907}
908
909char CFLAndersAAWrapperPass::ID = 0;
910INITIALIZE_PASS(CFLAndersAAWrapperPass, "cfl-anders-aa",
911 "Inclusion-Based CFL Alias Analysis", false, true)
912
913ImmutablePass *llvm::createCFLAndersAAWrapperPass() {
914 return new CFLAndersAAWrapperPass();
915}
916
917CFLAndersAAWrapperPass::CFLAndersAAWrapperPass() : ImmutablePass(ID) {
918 initializeCFLAndersAAWrapperPassPass(*PassRegistry::getPassRegistry());
919}
920
George Burgess IV6d30aa02016-07-15 19:53:25 +0000921void CFLAndersAAWrapperPass::initializePass() {
Teresa Johnson9c27b592019-09-07 03:09:36 +0000922 auto GetTLI = [this](Function &F) -> TargetLibraryInfo & {
923 return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
924 };
925 Result.reset(new CFLAndersAAResult(GetTLI));
George Burgess IV6d30aa02016-07-15 19:53:25 +0000926}
George Burgess IVbfa401e2016-07-06 00:26:41 +0000927
928void CFLAndersAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
929 AU.setPreservesAll();
George Burgess IV6d30aa02016-07-15 19:53:25 +0000930 AU.addRequired<TargetLibraryInfoWrapperPass>();
George Burgess IVbfa401e2016-07-06 00:26:41 +0000931}