CFLAndersAliasAnalysis.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. //===- CFLAndersAliasAnalysis.cpp - Unification-based Alias Analysis ------===//
  2. //
  3. // 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
  6. //
  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. //
  18. // 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
  20. // Rugina. The general idea is to extend the traditional transitive closure
  21. // 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. //
  29. // 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.
  46. //
  47. //===----------------------------------------------------------------------===//
  48. // N.B. AliasAnalysis as a whole is phrased as a FunctionPass at the moment, and
  49. // CFLAndersAA is interprocedural. This is *technically* A Bad Thing, because
  50. // FunctionPasses are only allowed to inspect the Function that they're being
  51. // run on. Realistically, this likely isn't a problem until we allow
  52. // FunctionPasses to run concurrently.
  53. #include "llvm/Analysis/CFLAndersAliasAnalysis.h"
  54. #include "AliasAnalysisSummary.h"
  55. #include "CFLGraph.h"
  56. #include "llvm/ADT/DenseMap.h"
  57. #include "llvm/ADT/DenseMapInfo.h"
  58. #include "llvm/ADT/DenseSet.h"
  59. #include "llvm/ADT/None.h"
  60. #include "llvm/ADT/Optional.h"
  61. #include "llvm/ADT/STLExtras.h"
  62. #include "llvm/ADT/SmallVector.h"
  63. #include "llvm/ADT/iterator_range.h"
  64. #include "llvm/Analysis/AliasAnalysis.h"
  65. #include "llvm/Analysis/MemoryLocation.h"
  66. #include "llvm/IR/Argument.h"
  67. #include "llvm/IR/Function.h"
  68. #include "llvm/IR/PassManager.h"
  69. #include "llvm/IR/Type.h"
  70. #include "llvm/InitializePasses.h"
  71. #include "llvm/Pass.h"
  72. #include "llvm/Support/Casting.h"
  73. #include "llvm/Support/Compiler.h"
  74. #include "llvm/Support/Debug.h"
  75. #include "llvm/Support/raw_ostream.h"
  76. #include <algorithm>
  77. #include <bitset>
  78. #include <cassert>
  79. #include <cstddef>
  80. #include <cstdint>
  81. #include <functional>
  82. #include <utility>
  83. #include <vector>
  84. using namespace llvm;
  85. using namespace llvm::cflaa;
  86. #define DEBUG_TYPE "cfl-anders-aa"
  87. CFLAndersAAResult::CFLAndersAAResult(
  88. std::function<const TargetLibraryInfo &(Function &F)> GetTLI)
  89. : GetTLI(std::move(GetTLI)) {}
  90. CFLAndersAAResult::CFLAndersAAResult(CFLAndersAAResult &&RHS)
  91. : AAResultBase(std::move(RHS)), GetTLI(std::move(RHS.GetTLI)) {}
  92. CFLAndersAAResult::~CFLAndersAAResult() = default;
  93. namespace {
  94. enum class MatchState : uint8_t {
  95. // The following state represents S1 in the paper.
  96. FlowFromReadOnly = 0,
  97. // The following two states together represent S2 in the paper.
  98. // The 'NoReadWrite' suffix indicates that there exists an alias path that
  99. // does not contain assignment and reverse assignment edges.
  100. // The 'ReadOnly' suffix indicates that there exists an alias path that
  101. // contains reverse assignment edges only.
  102. FlowFromMemAliasNoReadWrite,
  103. FlowFromMemAliasReadOnly,
  104. // The following two states together represent S3 in the paper.
  105. // The 'WriteOnly' suffix indicates that there exists an alias path that
  106. // contains assignment edges only.
  107. // The 'ReadWrite' suffix indicates that there exists an alias path that
  108. // contains both assignment and reverse assignment edges. Note that if X and Y
  109. // are reachable at 'ReadWrite' state, it does NOT mean X is both read from
  110. // and written to Y. Instead, it means that a third value Z is written to both
  111. // X and Y.
  112. FlowToWriteOnly,
  113. FlowToReadWrite,
  114. // The following two states together represent S4 in the paper.
  115. FlowToMemAliasWriteOnly,
  116. FlowToMemAliasReadWrite,
  117. };
  118. using StateSet = std::bitset<7>;
  119. const unsigned ReadOnlyStateMask =
  120. (1U << static_cast<uint8_t>(MatchState::FlowFromReadOnly)) |
  121. (1U << static_cast<uint8_t>(MatchState::FlowFromMemAliasReadOnly));
  122. const unsigned WriteOnlyStateMask =
  123. (1U << static_cast<uint8_t>(MatchState::FlowToWriteOnly)) |
  124. (1U << static_cast<uint8_t>(MatchState::FlowToMemAliasWriteOnly));
  125. // A pair that consists of a value and an offset
  126. struct OffsetValue {
  127. const Value *Val;
  128. int64_t Offset;
  129. };
  130. bool operator==(OffsetValue LHS, OffsetValue RHS) {
  131. return LHS.Val == RHS.Val && LHS.Offset == RHS.Offset;
  132. }
  133. bool operator<(OffsetValue LHS, OffsetValue RHS) {
  134. return std::less<const Value *>()(LHS.Val, RHS.Val) ||
  135. (LHS.Val == RHS.Val && LHS.Offset < RHS.Offset);
  136. }
  137. // A pair that consists of an InstantiatedValue and an offset
  138. struct OffsetInstantiatedValue {
  139. InstantiatedValue IVal;
  140. int64_t Offset;
  141. };
  142. bool operator==(OffsetInstantiatedValue LHS, OffsetInstantiatedValue RHS) {
  143. return LHS.IVal == RHS.IVal && LHS.Offset == RHS.Offset;
  144. }
  145. // We use ReachabilitySet to keep track of value aliases (The nonterminal "V" in
  146. // the paper) during the analysis.
  147. class ReachabilitySet {
  148. using ValueStateMap = DenseMap<InstantiatedValue, StateSet>;
  149. using ValueReachMap = DenseMap<InstantiatedValue, ValueStateMap>;
  150. ValueReachMap ReachMap;
  151. public:
  152. using const_valuestate_iterator = ValueStateMap::const_iterator;
  153. using const_value_iterator = ValueReachMap::const_iterator;
  154. // Insert edge 'From->To' at state 'State'
  155. bool insert(InstantiatedValue From, InstantiatedValue To, MatchState State) {
  156. assert(From != To);
  157. auto &States = ReachMap[To][From];
  158. auto Idx = static_cast<size_t>(State);
  159. if (!States.test(Idx)) {
  160. States.set(Idx);
  161. return true;
  162. }
  163. return false;
  164. }
  165. // Return the set of all ('From', 'State') pair for a given node 'To'
  166. iterator_range<const_valuestate_iterator>
  167. reachableValueAliases(InstantiatedValue V) const {
  168. auto Itr = ReachMap.find(V);
  169. if (Itr == ReachMap.end())
  170. return make_range<const_valuestate_iterator>(const_valuestate_iterator(),
  171. const_valuestate_iterator());
  172. return make_range<const_valuestate_iterator>(Itr->second.begin(),
  173. Itr->second.end());
  174. }
  175. iterator_range<const_value_iterator> value_mappings() const {
  176. return make_range<const_value_iterator>(ReachMap.begin(), ReachMap.end());
  177. }
  178. };
  179. // We use AliasMemSet to keep track of all memory aliases (the nonterminal "M"
  180. // in the paper) during the analysis.
  181. class AliasMemSet {
  182. using MemSet = DenseSet<InstantiatedValue>;
  183. using MemMapType = DenseMap<InstantiatedValue, MemSet>;
  184. MemMapType MemMap;
  185. public:
  186. using const_mem_iterator = MemSet::const_iterator;
  187. bool insert(InstantiatedValue LHS, InstantiatedValue RHS) {
  188. // Top-level values can never be memory aliases because one cannot take the
  189. // addresses of them
  190. assert(LHS.DerefLevel > 0 && RHS.DerefLevel > 0);
  191. return MemMap[LHS].insert(RHS).second;
  192. }
  193. const MemSet *getMemoryAliases(InstantiatedValue V) const {
  194. auto Itr = MemMap.find(V);
  195. if (Itr == MemMap.end())
  196. return nullptr;
  197. return &Itr->second;
  198. }
  199. };
  200. // We use AliasAttrMap to keep track of the AliasAttr of each node.
  201. class AliasAttrMap {
  202. using MapType = DenseMap<InstantiatedValue, AliasAttrs>;
  203. MapType AttrMap;
  204. public:
  205. using const_iterator = MapType::const_iterator;
  206. bool add(InstantiatedValue V, AliasAttrs Attr) {
  207. auto &OldAttr = AttrMap[V];
  208. auto NewAttr = OldAttr | Attr;
  209. if (OldAttr == NewAttr)
  210. return false;
  211. OldAttr = NewAttr;
  212. return true;
  213. }
  214. AliasAttrs getAttrs(InstantiatedValue V) const {
  215. AliasAttrs Attr;
  216. auto Itr = AttrMap.find(V);
  217. if (Itr != AttrMap.end())
  218. Attr = Itr->second;
  219. return Attr;
  220. }
  221. iterator_range<const_iterator> mappings() const {
  222. return make_range<const_iterator>(AttrMap.begin(), AttrMap.end());
  223. }
  224. };
  225. struct WorkListItem {
  226. InstantiatedValue From;
  227. InstantiatedValue To;
  228. MatchState State;
  229. };
  230. struct ValueSummary {
  231. struct Record {
  232. InterfaceValue IValue;
  233. unsigned DerefLevel;
  234. };
  235. SmallVector<Record, 4> FromRecords, ToRecords;
  236. };
  237. } // end anonymous namespace
  238. namespace llvm {
  239. // Specialize DenseMapInfo for OffsetValue.
  240. template <> struct DenseMapInfo<OffsetValue> {
  241. static OffsetValue getEmptyKey() {
  242. return OffsetValue{DenseMapInfo<const Value *>::getEmptyKey(),
  243. DenseMapInfo<int64_t>::getEmptyKey()};
  244. }
  245. static OffsetValue getTombstoneKey() {
  246. return OffsetValue{DenseMapInfo<const Value *>::getTombstoneKey(),
  247. DenseMapInfo<int64_t>::getEmptyKey()};
  248. }
  249. static unsigned getHashValue(const OffsetValue &OVal) {
  250. return DenseMapInfo<std::pair<const Value *, int64_t>>::getHashValue(
  251. std::make_pair(OVal.Val, OVal.Offset));
  252. }
  253. static bool isEqual(const OffsetValue &LHS, const OffsetValue &RHS) {
  254. return LHS == RHS;
  255. }
  256. };
  257. // Specialize DenseMapInfo for OffsetInstantiatedValue.
  258. template <> struct DenseMapInfo<OffsetInstantiatedValue> {
  259. static OffsetInstantiatedValue getEmptyKey() {
  260. return OffsetInstantiatedValue{
  261. DenseMapInfo<InstantiatedValue>::getEmptyKey(),
  262. DenseMapInfo<int64_t>::getEmptyKey()};
  263. }
  264. static OffsetInstantiatedValue getTombstoneKey() {
  265. return OffsetInstantiatedValue{
  266. DenseMapInfo<InstantiatedValue>::getTombstoneKey(),
  267. DenseMapInfo<int64_t>::getEmptyKey()};
  268. }
  269. static unsigned getHashValue(const OffsetInstantiatedValue &OVal) {
  270. return DenseMapInfo<std::pair<InstantiatedValue, int64_t>>::getHashValue(
  271. std::make_pair(OVal.IVal, OVal.Offset));
  272. }
  273. static bool isEqual(const OffsetInstantiatedValue &LHS,
  274. const OffsetInstantiatedValue &RHS) {
  275. return LHS == RHS;
  276. }
  277. };
  278. } // end namespace llvm
  279. class CFLAndersAAResult::FunctionInfo {
  280. /// Map a value to other values that may alias it
  281. /// Since the alias relation is symmetric, to save some space we assume values
  282. /// are properly ordered: if a and b alias each other, and a < b, then b is in
  283. /// AliasMap[a] but not vice versa.
  284. DenseMap<const Value *, std::vector<OffsetValue>> AliasMap;
  285. /// Map a value to its corresponding AliasAttrs
  286. DenseMap<const Value *, AliasAttrs> AttrMap;
  287. /// Summary of externally visible effects.
  288. AliasSummary Summary;
  289. Optional<AliasAttrs> getAttrs(const Value *) const;
  290. public:
  291. FunctionInfo(const Function &, const SmallVectorImpl<Value *> &,
  292. const ReachabilitySet &, const AliasAttrMap &);
  293. bool mayAlias(const Value *, LocationSize, const Value *, LocationSize) const;
  294. const AliasSummary &getAliasSummary() const { return Summary; }
  295. };
  296. static bool hasReadOnlyState(StateSet Set) {
  297. return (Set & StateSet(ReadOnlyStateMask)).any();
  298. }
  299. static bool hasWriteOnlyState(StateSet Set) {
  300. return (Set & StateSet(WriteOnlyStateMask)).any();
  301. }
  302. static Optional<InterfaceValue>
  303. getInterfaceValue(InstantiatedValue IValue,
  304. const SmallVectorImpl<Value *> &RetVals) {
  305. auto Val = IValue.Val;
  306. Optional<unsigned> Index;
  307. if (auto Arg = dyn_cast<Argument>(Val))
  308. Index = Arg->getArgNo() + 1;
  309. else if (is_contained(RetVals, Val))
  310. Index = 0;
  311. if (Index)
  312. return InterfaceValue{*Index, IValue.DerefLevel};
  313. return None;
  314. }
  315. static void populateAttrMap(DenseMap<const Value *, AliasAttrs> &AttrMap,
  316. const AliasAttrMap &AMap) {
  317. for (const auto &Mapping : AMap.mappings()) {
  318. auto IVal = Mapping.first;
  319. // Insert IVal into the map
  320. auto &Attr = AttrMap[IVal.Val];
  321. // AttrMap only cares about top-level values
  322. if (IVal.DerefLevel == 0)
  323. Attr |= Mapping.second;
  324. }
  325. }
  326. static void
  327. populateAliasMap(DenseMap<const Value *, std::vector<OffsetValue>> &AliasMap,
  328. const ReachabilitySet &ReachSet) {
  329. for (const auto &OuterMapping : ReachSet.value_mappings()) {
  330. // AliasMap only cares about top-level values
  331. if (OuterMapping.first.DerefLevel > 0)
  332. continue;
  333. auto Val = OuterMapping.first.Val;
  334. auto &AliasList = AliasMap[Val];
  335. for (const auto &InnerMapping : OuterMapping.second) {
  336. // Again, AliasMap only cares about top-level values
  337. if (InnerMapping.first.DerefLevel == 0)
  338. AliasList.push_back(OffsetValue{InnerMapping.first.Val, UnknownOffset});
  339. }
  340. // Sort AliasList for faster lookup
  341. llvm::sort(AliasList);
  342. }
  343. }
  344. static void populateExternalRelations(
  345. SmallVectorImpl<ExternalRelation> &ExtRelations, const Function &Fn,
  346. const SmallVectorImpl<Value *> &RetVals, const ReachabilitySet &ReachSet) {
  347. // If a function only returns one of its argument X, then X will be both an
  348. // argument and a return value at the same time. This is an edge case that
  349. // needs special handling here.
  350. for (const auto &Arg : Fn.args()) {
  351. if (is_contained(RetVals, &Arg)) {
  352. auto ArgVal = InterfaceValue{Arg.getArgNo() + 1, 0};
  353. auto RetVal = InterfaceValue{0, 0};
  354. ExtRelations.push_back(ExternalRelation{ArgVal, RetVal, 0});
  355. }
  356. }
  357. // Below is the core summary construction logic.
  358. // A naive solution of adding only the value aliases that are parameters or
  359. // return values in ReachSet to the summary won't work: It is possible that a
  360. // parameter P is written into an intermediate value I, and the function
  361. // subsequently returns *I. In that case, *I is does not value alias anything
  362. // in ReachSet, and the naive solution will miss a summary edge from (P, 1) to
  363. // (I, 1).
  364. // To account for the aforementioned case, we need to check each non-parameter
  365. // and non-return value for the possibility of acting as an intermediate.
  366. // 'ValueMap' here records, for each value, which InterfaceValues read from or
  367. // write into it. If both the read list and the write list of a given value
  368. // are non-empty, we know that a particular value is an intermidate and we
  369. // need to add summary edges from the writes to the reads.
  370. DenseMap<Value *, ValueSummary> ValueMap;
  371. for (const auto &OuterMapping : ReachSet.value_mappings()) {
  372. if (auto Dst = getInterfaceValue(OuterMapping.first, RetVals)) {
  373. for (const auto &InnerMapping : OuterMapping.second) {
  374. // If Src is a param/return value, we get a same-level assignment.
  375. if (auto Src = getInterfaceValue(InnerMapping.first, RetVals)) {
  376. // This may happen if both Dst and Src are return values
  377. if (*Dst == *Src)
  378. continue;
  379. if (hasReadOnlyState(InnerMapping.second))
  380. ExtRelations.push_back(ExternalRelation{*Dst, *Src, UnknownOffset});
  381. // No need to check for WriteOnly state, since ReachSet is symmetric
  382. } else {
  383. // If Src is not a param/return, add it to ValueMap
  384. auto SrcIVal = InnerMapping.first;
  385. if (hasReadOnlyState(InnerMapping.second))
  386. ValueMap[SrcIVal.Val].FromRecords.push_back(
  387. ValueSummary::Record{*Dst, SrcIVal.DerefLevel});
  388. if (hasWriteOnlyState(InnerMapping.second))
  389. ValueMap[SrcIVal.Val].ToRecords.push_back(
  390. ValueSummary::Record{*Dst, SrcIVal.DerefLevel});
  391. }
  392. }
  393. }
  394. }
  395. for (const auto &Mapping : ValueMap) {
  396. for (const auto &FromRecord : Mapping.second.FromRecords) {
  397. for (const auto &ToRecord : Mapping.second.ToRecords) {
  398. auto ToLevel = ToRecord.DerefLevel;
  399. auto FromLevel = FromRecord.DerefLevel;
  400. // Same-level assignments should have already been processed by now
  401. if (ToLevel == FromLevel)
  402. continue;
  403. auto SrcIndex = FromRecord.IValue.Index;
  404. auto SrcLevel = FromRecord.IValue.DerefLevel;
  405. auto DstIndex = ToRecord.IValue.Index;
  406. auto DstLevel = ToRecord.IValue.DerefLevel;
  407. if (ToLevel > FromLevel)
  408. SrcLevel += ToLevel - FromLevel;
  409. else
  410. DstLevel += FromLevel - ToLevel;
  411. ExtRelations.push_back(ExternalRelation{
  412. InterfaceValue{SrcIndex, SrcLevel},
  413. InterfaceValue{DstIndex, DstLevel}, UnknownOffset});
  414. }
  415. }
  416. }
  417. // Remove duplicates in ExtRelations
  418. llvm::sort(ExtRelations);
  419. ExtRelations.erase(std::unique(ExtRelations.begin(), ExtRelations.end()),
  420. ExtRelations.end());
  421. }
  422. static void populateExternalAttributes(
  423. SmallVectorImpl<ExternalAttribute> &ExtAttributes, const Function &Fn,
  424. const SmallVectorImpl<Value *> &RetVals, const AliasAttrMap &AMap) {
  425. for (const auto &Mapping : AMap.mappings()) {
  426. if (auto IVal = getInterfaceValue(Mapping.first, RetVals)) {
  427. auto Attr = getExternallyVisibleAttrs(Mapping.second);
  428. if (Attr.any())
  429. ExtAttributes.push_back(ExternalAttribute{*IVal, Attr});
  430. }
  431. }
  432. }
  433. CFLAndersAAResult::FunctionInfo::FunctionInfo(
  434. const Function &Fn, const SmallVectorImpl<Value *> &RetVals,
  435. const ReachabilitySet &ReachSet, const AliasAttrMap &AMap) {
  436. populateAttrMap(AttrMap, AMap);
  437. populateExternalAttributes(Summary.RetParamAttributes, Fn, RetVals, AMap);
  438. populateAliasMap(AliasMap, ReachSet);
  439. populateExternalRelations(Summary.RetParamRelations, Fn, RetVals, ReachSet);
  440. }
  441. Optional<AliasAttrs>
  442. CFLAndersAAResult::FunctionInfo::getAttrs(const Value *V) const {
  443. assert(V != nullptr);
  444. auto Itr = AttrMap.find(V);
  445. if (Itr != AttrMap.end())
  446. return Itr->second;
  447. return None;
  448. }
  449. bool CFLAndersAAResult::FunctionInfo::mayAlias(
  450. const Value *LHS, LocationSize MaybeLHSSize, const Value *RHS,
  451. LocationSize MaybeRHSSize) const {
  452. assert(LHS && RHS);
  453. // Check if we've seen LHS and RHS before. Sometimes LHS or RHS can be created
  454. // after the analysis gets executed, and we want to be conservative in those
  455. // cases.
  456. auto MaybeAttrsA = getAttrs(LHS);
  457. auto MaybeAttrsB = getAttrs(RHS);
  458. if (!MaybeAttrsA || !MaybeAttrsB)
  459. return true;
  460. // Check AliasAttrs before AliasMap lookup since it's cheaper
  461. auto AttrsA = *MaybeAttrsA;
  462. auto AttrsB = *MaybeAttrsB;
  463. if (hasUnknownOrCallerAttr(AttrsA))
  464. return AttrsB.any();
  465. if (hasUnknownOrCallerAttr(AttrsB))
  466. return AttrsA.any();
  467. if (isGlobalOrArgAttr(AttrsA))
  468. return isGlobalOrArgAttr(AttrsB);
  469. if (isGlobalOrArgAttr(AttrsB))
  470. return isGlobalOrArgAttr(AttrsA);
  471. // At this point both LHS and RHS should point to locally allocated objects
  472. auto Itr = AliasMap.find(LHS);
  473. if (Itr != AliasMap.end()) {
  474. // Find out all (X, Offset) where X == RHS
  475. auto Comparator = [](OffsetValue LHS, OffsetValue RHS) {
  476. return std::less<const Value *>()(LHS.Val, RHS.Val);
  477. };
  478. #ifdef EXPENSIVE_CHECKS
  479. assert(llvm::is_sorted(Itr->second, Comparator));
  480. #endif
  481. auto RangePair = std::equal_range(Itr->second.begin(), Itr->second.end(),
  482. OffsetValue{RHS, 0}, Comparator);
  483. if (RangePair.first != RangePair.second) {
  484. // Be conservative about unknown sizes
  485. if (!MaybeLHSSize.hasValue() || !MaybeRHSSize.hasValue())
  486. return true;
  487. const uint64_t LHSSize = MaybeLHSSize.getValue();
  488. const uint64_t RHSSize = MaybeRHSSize.getValue();
  489. for (const auto &OVal : make_range(RangePair)) {
  490. // Be conservative about UnknownOffset
  491. if (OVal.Offset == UnknownOffset)
  492. return true;
  493. // We know that LHS aliases (RHS + OVal.Offset) if the control flow
  494. // reaches here. The may-alias query essentially becomes integer
  495. // range-overlap queries over two ranges [OVal.Offset, OVal.Offset +
  496. // LHSSize) and [0, RHSSize).
  497. // Try to be conservative on super large offsets
  498. if (LLVM_UNLIKELY(LHSSize > INT64_MAX || RHSSize > INT64_MAX))
  499. return true;
  500. auto LHSStart = OVal.Offset;
  501. // FIXME: Do we need to guard against integer overflow?
  502. auto LHSEnd = OVal.Offset + static_cast<int64_t>(LHSSize);
  503. auto RHSStart = 0;
  504. auto RHSEnd = static_cast<int64_t>(RHSSize);
  505. if (LHSEnd > RHSStart && LHSStart < RHSEnd)
  506. return true;
  507. }
  508. }
  509. }
  510. return false;
  511. }
  512. static void propagate(InstantiatedValue From, InstantiatedValue To,
  513. MatchState State, ReachabilitySet &ReachSet,
  514. std::vector<WorkListItem> &WorkList) {
  515. if (From == To)
  516. return;
  517. if (ReachSet.insert(From, To, State))
  518. WorkList.push_back(WorkListItem{From, To, State});
  519. }
  520. static void initializeWorkList(std::vector<WorkListItem> &WorkList,
  521. ReachabilitySet &ReachSet,
  522. const CFLGraph &Graph) {
  523. for (const auto &Mapping : Graph.value_mappings()) {
  524. auto Val = Mapping.first;
  525. auto &ValueInfo = Mapping.second;
  526. assert(ValueInfo.getNumLevels() > 0);
  527. // Insert all immediate assignment neighbors to the worklist
  528. for (unsigned I = 0, E = ValueInfo.getNumLevels(); I < E; ++I) {
  529. auto Src = InstantiatedValue{Val, I};
  530. // If there's an assignment edge from X to Y, it means Y is reachable from
  531. // X at S3 and X is reachable from Y at S1
  532. for (auto &Edge : ValueInfo.getNodeInfoAtLevel(I).Edges) {
  533. propagate(Edge.Other, Src, MatchState::FlowFromReadOnly, ReachSet,
  534. WorkList);
  535. propagate(Src, Edge.Other, MatchState::FlowToWriteOnly, ReachSet,
  536. WorkList);
  537. }
  538. }
  539. }
  540. }
  541. static Optional<InstantiatedValue> getNodeBelow(const CFLGraph &Graph,
  542. InstantiatedValue V) {
  543. auto NodeBelow = InstantiatedValue{V.Val, V.DerefLevel + 1};
  544. if (Graph.getNode(NodeBelow))
  545. return NodeBelow;
  546. return None;
  547. }
  548. static void processWorkListItem(const WorkListItem &Item, const CFLGraph &Graph,
  549. ReachabilitySet &ReachSet, AliasMemSet &MemSet,
  550. std::vector<WorkListItem> &WorkList) {
  551. auto FromNode = Item.From;
  552. auto ToNode = Item.To;
  553. auto NodeInfo = Graph.getNode(ToNode);
  554. assert(NodeInfo != nullptr);
  555. // TODO: propagate field offsets
  556. // FIXME: Here is a neat trick we can do: since both ReachSet and MemSet holds
  557. // relations that are symmetric, we could actually cut the storage by half by
  558. // sorting FromNode and ToNode before insertion happens.
  559. // The newly added value alias pair may potentially generate more memory
  560. // alias pairs. Check for them here.
  561. auto FromNodeBelow = getNodeBelow(Graph, FromNode);
  562. auto ToNodeBelow = getNodeBelow(Graph, ToNode);
  563. if (FromNodeBelow && ToNodeBelow &&
  564. MemSet.insert(*FromNodeBelow, *ToNodeBelow)) {
  565. propagate(*FromNodeBelow, *ToNodeBelow,
  566. MatchState::FlowFromMemAliasNoReadWrite, ReachSet, WorkList);
  567. for (const auto &Mapping : ReachSet.reachableValueAliases(*FromNodeBelow)) {
  568. auto Src = Mapping.first;
  569. auto MemAliasPropagate = [&](MatchState FromState, MatchState ToState) {
  570. if (Mapping.second.test(static_cast<size_t>(FromState)))
  571. propagate(Src, *ToNodeBelow, ToState, ReachSet, WorkList);
  572. };
  573. MemAliasPropagate(MatchState::FlowFromReadOnly,
  574. MatchState::FlowFromMemAliasReadOnly);
  575. MemAliasPropagate(MatchState::FlowToWriteOnly,
  576. MatchState::FlowToMemAliasWriteOnly);
  577. MemAliasPropagate(MatchState::FlowToReadWrite,
  578. MatchState::FlowToMemAliasReadWrite);
  579. }
  580. }
  581. // This is the core of the state machine walking algorithm. We expand ReachSet
  582. // based on which state we are at (which in turn dictates what edges we
  583. // should examine)
  584. // From a high-level point of view, the state machine here guarantees two
  585. // properties:
  586. // - If *X and *Y are memory aliases, then X and Y are value aliases
  587. // - If Y is an alias of X, then reverse assignment edges (if there is any)
  588. // should precede any assignment edges on the path from X to Y.
  589. auto NextAssignState = [&](MatchState State) {
  590. for (const auto &AssignEdge : NodeInfo->Edges)
  591. propagate(FromNode, AssignEdge.Other, State, ReachSet, WorkList);
  592. };
  593. auto NextRevAssignState = [&](MatchState State) {
  594. for (const auto &RevAssignEdge : NodeInfo->ReverseEdges)
  595. propagate(FromNode, RevAssignEdge.Other, State, ReachSet, WorkList);
  596. };
  597. auto NextMemState = [&](MatchState State) {
  598. if (auto AliasSet = MemSet.getMemoryAliases(ToNode)) {
  599. for (const auto &MemAlias : *AliasSet)
  600. propagate(FromNode, MemAlias, State, ReachSet, WorkList);
  601. }
  602. };
  603. switch (Item.State) {
  604. case MatchState::FlowFromReadOnly:
  605. NextRevAssignState(MatchState::FlowFromReadOnly);
  606. NextAssignState(MatchState::FlowToReadWrite);
  607. NextMemState(MatchState::FlowFromMemAliasReadOnly);
  608. break;
  609. case MatchState::FlowFromMemAliasNoReadWrite:
  610. NextRevAssignState(MatchState::FlowFromReadOnly);
  611. NextAssignState(MatchState::FlowToWriteOnly);
  612. break;
  613. case MatchState::FlowFromMemAliasReadOnly:
  614. NextRevAssignState(MatchState::FlowFromReadOnly);
  615. NextAssignState(MatchState::FlowToReadWrite);
  616. break;
  617. case MatchState::FlowToWriteOnly:
  618. NextAssignState(MatchState::FlowToWriteOnly);
  619. NextMemState(MatchState::FlowToMemAliasWriteOnly);
  620. break;
  621. case MatchState::FlowToReadWrite:
  622. NextAssignState(MatchState::FlowToReadWrite);
  623. NextMemState(MatchState::FlowToMemAliasReadWrite);
  624. break;
  625. case MatchState::FlowToMemAliasWriteOnly:
  626. NextAssignState(MatchState::FlowToWriteOnly);
  627. break;
  628. case MatchState::FlowToMemAliasReadWrite:
  629. NextAssignState(MatchState::FlowToReadWrite);
  630. break;
  631. }
  632. }
  633. static AliasAttrMap buildAttrMap(const CFLGraph &Graph,
  634. const ReachabilitySet &ReachSet) {
  635. AliasAttrMap AttrMap;
  636. std::vector<InstantiatedValue> WorkList, NextList;
  637. // Initialize each node with its original AliasAttrs in CFLGraph
  638. for (const auto &Mapping : Graph.value_mappings()) {
  639. auto Val = Mapping.first;
  640. auto &ValueInfo = Mapping.second;
  641. for (unsigned I = 0, E = ValueInfo.getNumLevels(); I < E; ++I) {
  642. auto Node = InstantiatedValue{Val, I};
  643. AttrMap.add(Node, ValueInfo.getNodeInfoAtLevel(I).Attr);
  644. WorkList.push_back(Node);
  645. }
  646. }
  647. while (!WorkList.empty()) {
  648. for (const auto &Dst : WorkList) {
  649. auto DstAttr = AttrMap.getAttrs(Dst);
  650. if (DstAttr.none())
  651. continue;
  652. // Propagate attr on the same level
  653. for (const auto &Mapping : ReachSet.reachableValueAliases(Dst)) {
  654. auto Src = Mapping.first;
  655. if (AttrMap.add(Src, DstAttr))
  656. NextList.push_back(Src);
  657. }
  658. // Propagate attr to the levels below
  659. auto DstBelow = getNodeBelow(Graph, Dst);
  660. while (DstBelow) {
  661. if (AttrMap.add(*DstBelow, DstAttr)) {
  662. NextList.push_back(*DstBelow);
  663. break;
  664. }
  665. DstBelow = getNodeBelow(Graph, *DstBelow);
  666. }
  667. }
  668. WorkList.swap(NextList);
  669. NextList.clear();
  670. }
  671. return AttrMap;
  672. }
  673. CFLAndersAAResult::FunctionInfo
  674. CFLAndersAAResult::buildInfoFrom(const Function &Fn) {
  675. CFLGraphBuilder<CFLAndersAAResult> GraphBuilder(
  676. *this, GetTLI(const_cast<Function &>(Fn)),
  677. // Cast away the constness here due to GraphBuilder's API requirement
  678. const_cast<Function &>(Fn));
  679. auto &Graph = GraphBuilder.getCFLGraph();
  680. ReachabilitySet ReachSet;
  681. AliasMemSet MemSet;
  682. std::vector<WorkListItem> WorkList, NextList;
  683. initializeWorkList(WorkList, ReachSet, Graph);
  684. // TODO: make sure we don't stop before the fix point is reached
  685. while (!WorkList.empty()) {
  686. for (const auto &Item : WorkList)
  687. processWorkListItem(Item, Graph, ReachSet, MemSet, NextList);
  688. NextList.swap(WorkList);
  689. NextList.clear();
  690. }
  691. // Now that we have all the reachability info, propagate AliasAttrs according
  692. // to it
  693. auto IValueAttrMap = buildAttrMap(Graph, ReachSet);
  694. return FunctionInfo(Fn, GraphBuilder.getReturnValues(), ReachSet,
  695. std::move(IValueAttrMap));
  696. }
  697. void CFLAndersAAResult::scan(const Function &Fn) {
  698. auto InsertPair = Cache.insert(std::make_pair(&Fn, Optional<FunctionInfo>()));
  699. (void)InsertPair;
  700. assert(InsertPair.second &&
  701. "Trying to scan a function that has already been cached");
  702. // Note that we can't do Cache[Fn] = buildSetsFrom(Fn) here: the function call
  703. // may get evaluated after operator[], potentially triggering a DenseMap
  704. // resize and invalidating the reference returned by operator[]
  705. auto FunInfo = buildInfoFrom(Fn);
  706. Cache[&Fn] = std::move(FunInfo);
  707. Handles.emplace_front(const_cast<Function *>(&Fn), this);
  708. }
  709. void CFLAndersAAResult::evict(const Function *Fn) { Cache.erase(Fn); }
  710. const Optional<CFLAndersAAResult::FunctionInfo> &
  711. CFLAndersAAResult::ensureCached(const Function &Fn) {
  712. auto Iter = Cache.find(&Fn);
  713. if (Iter == Cache.end()) {
  714. scan(Fn);
  715. Iter = Cache.find(&Fn);
  716. assert(Iter != Cache.end());
  717. assert(Iter->second.hasValue());
  718. }
  719. return Iter->second;
  720. }
  721. const AliasSummary *CFLAndersAAResult::getAliasSummary(const Function &Fn) {
  722. auto &FunInfo = ensureCached(Fn);
  723. if (FunInfo.hasValue())
  724. return &FunInfo->getAliasSummary();
  725. else
  726. return nullptr;
  727. }
  728. AliasResult CFLAndersAAResult::query(const MemoryLocation &LocA,
  729. const MemoryLocation &LocB) {
  730. auto *ValA = LocA.Ptr;
  731. auto *ValB = LocB.Ptr;
  732. if (!ValA->getType()->isPointerTy() || !ValB->getType()->isPointerTy())
  733. return AliasResult::NoAlias;
  734. auto *Fn = parentFunctionOfValue(ValA);
  735. if (!Fn) {
  736. Fn = parentFunctionOfValue(ValB);
  737. if (!Fn) {
  738. // The only times this is known to happen are when globals + InlineAsm are
  739. // involved
  740. LLVM_DEBUG(
  741. dbgs()
  742. << "CFLAndersAA: could not extract parent function information.\n");
  743. return AliasResult::MayAlias;
  744. }
  745. } else {
  746. assert(!parentFunctionOfValue(ValB) || parentFunctionOfValue(ValB) == Fn);
  747. }
  748. assert(Fn != nullptr);
  749. auto &FunInfo = ensureCached(*Fn);
  750. // AliasMap lookup
  751. if (FunInfo->mayAlias(ValA, LocA.Size, ValB, LocB.Size))
  752. return AliasResult::MayAlias;
  753. return AliasResult::NoAlias;
  754. }
  755. AliasResult CFLAndersAAResult::alias(const MemoryLocation &LocA,
  756. const MemoryLocation &LocB,
  757. AAQueryInfo &AAQI) {
  758. if (LocA.Ptr == LocB.Ptr)
  759. return AliasResult::MustAlias;
  760. // Comparisons between global variables and other constants should be
  761. // handled by BasicAA.
  762. // CFLAndersAA may report NoAlias when comparing a GlobalValue and
  763. // ConstantExpr, but every query needs to have at least one Value tied to a
  764. // Function, and neither GlobalValues nor ConstantExprs are.
  765. if (isa<Constant>(LocA.Ptr) && isa<Constant>(LocB.Ptr))
  766. return AAResultBase::alias(LocA, LocB, AAQI);
  767. AliasResult QueryResult = query(LocA, LocB);
  768. if (QueryResult == AliasResult::MayAlias)
  769. return AAResultBase::alias(LocA, LocB, AAQI);
  770. return QueryResult;
  771. }
  772. AnalysisKey CFLAndersAA::Key;
  773. CFLAndersAAResult CFLAndersAA::run(Function &F, FunctionAnalysisManager &AM) {
  774. auto GetTLI = [&AM](Function &F) -> TargetLibraryInfo & {
  775. return AM.getResult<TargetLibraryAnalysis>(F);
  776. };
  777. return CFLAndersAAResult(GetTLI);
  778. }
  779. char CFLAndersAAWrapperPass::ID = 0;
  780. INITIALIZE_PASS(CFLAndersAAWrapperPass, "cfl-anders-aa",
  781. "Inclusion-Based CFL Alias Analysis", false, true)
  782. ImmutablePass *llvm::createCFLAndersAAWrapperPass() {
  783. return new CFLAndersAAWrapperPass();
  784. }
  785. CFLAndersAAWrapperPass::CFLAndersAAWrapperPass() : ImmutablePass(ID) {
  786. initializeCFLAndersAAWrapperPassPass(*PassRegistry::getPassRegistry());
  787. }
  788. void CFLAndersAAWrapperPass::initializePass() {
  789. auto GetTLI = [this](Function &F) -> TargetLibraryInfo & {
  790. return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
  791. };
  792. Result.reset(new CFLAndersAAResult(GetTLI));
  793. }
  794. void CFLAndersAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
  795. AU.setPreservesAll();
  796. AU.addRequired<TargetLibraryInfoWrapperPass>();
  797. }