StratifiedSets.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. //===- StratifiedSets.h - Abstract stratified sets implementation. --------===//
  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. #ifndef LLVM_ADT_STRATIFIEDSETS_H
  9. #define LLVM_ADT_STRATIFIEDSETS_H
  10. #include "AliasAnalysisSummary.h"
  11. #include "llvm/ADT/DenseMap.h"
  12. #include "llvm/ADT/Optional.h"
  13. #include "llvm/ADT/SmallSet.h"
  14. #include "llvm/ADT/SmallVector.h"
  15. #include <bitset>
  16. #include <cassert>
  17. #include <cmath>
  18. #include <type_traits>
  19. #include <utility>
  20. #include <vector>
  21. namespace llvm {
  22. namespace cflaa {
  23. /// An index into Stratified Sets.
  24. typedef unsigned StratifiedIndex;
  25. /// NOTE: ^ This can't be a short -- bootstrapping clang has a case where
  26. /// ~1M sets exist.
  27. // Container of information related to a value in a StratifiedSet.
  28. struct StratifiedInfo {
  29. StratifiedIndex Index;
  30. /// For field sensitivity, etc. we can tack fields on here.
  31. };
  32. /// A "link" between two StratifiedSets.
  33. struct StratifiedLink {
  34. /// This is a value used to signify "does not exist" where the
  35. /// StratifiedIndex type is used.
  36. ///
  37. /// This is used instead of Optional<StratifiedIndex> because
  38. /// Optional<StratifiedIndex> would eat up a considerable amount of extra
  39. /// memory, after struct padding/alignment is taken into account.
  40. static const StratifiedIndex SetSentinel;
  41. /// The index for the set "above" current
  42. StratifiedIndex Above;
  43. /// The link for the set "below" current
  44. StratifiedIndex Below;
  45. /// Attributes for these StratifiedSets.
  46. AliasAttrs Attrs;
  47. StratifiedLink() : Above(SetSentinel), Below(SetSentinel) {}
  48. bool hasBelow() const { return Below != SetSentinel; }
  49. bool hasAbove() const { return Above != SetSentinel; }
  50. void clearBelow() { Below = SetSentinel; }
  51. void clearAbove() { Above = SetSentinel; }
  52. };
  53. /// These are stratified sets, as described in "Fast algorithms for
  54. /// Dyck-CFL-reachability with applications to Alias Analysis" by Zhang Q, Lyu M
  55. /// R, Yuan H, and Su Z. -- in short, this is meant to represent different sets
  56. /// of Value*s. If two Value*s are in the same set, or if both sets have
  57. /// overlapping attributes, then the Value*s are said to alias.
  58. ///
  59. /// Sets may be related by position, meaning that one set may be considered as
  60. /// above or below another. In CFL Alias Analysis, this gives us an indication
  61. /// of how two variables are related; if the set of variable A is below a set
  62. /// containing variable B, then at some point, a variable that has interacted
  63. /// with B (or B itself) was either used in order to extract the variable A, or
  64. /// was used as storage of variable A.
  65. ///
  66. /// Sets may also have attributes (as noted above). These attributes are
  67. /// generally used for noting whether a variable in the set has interacted with
  68. /// a variable whose origins we don't quite know (i.e. globals/arguments), or if
  69. /// the variable may have had operations performed on it (modified in a function
  70. /// call). All attributes that exist in a set A must exist in all sets marked as
  71. /// below set A.
  72. template <typename T> class StratifiedSets {
  73. public:
  74. StratifiedSets() = default;
  75. StratifiedSets(StratifiedSets &&) = default;
  76. StratifiedSets &operator=(StratifiedSets &&) = default;
  77. StratifiedSets(DenseMap<T, StratifiedInfo> Map,
  78. std::vector<StratifiedLink> Links)
  79. : Values(std::move(Map)), Links(std::move(Links)) {}
  80. Optional<StratifiedInfo> find(const T &Elem) const {
  81. auto Iter = Values.find(Elem);
  82. if (Iter == Values.end())
  83. return None;
  84. return Iter->second;
  85. }
  86. const StratifiedLink &getLink(StratifiedIndex Index) const {
  87. assert(inbounds(Index));
  88. return Links[Index];
  89. }
  90. private:
  91. DenseMap<T, StratifiedInfo> Values;
  92. std::vector<StratifiedLink> Links;
  93. bool inbounds(StratifiedIndex Idx) const { return Idx < Links.size(); }
  94. };
  95. /// Generic Builder class that produces StratifiedSets instances.
  96. ///
  97. /// The goal of this builder is to efficiently produce correct StratifiedSets
  98. /// instances. To this end, we use a few tricks:
  99. /// > Set chains (A method for linking sets together)
  100. /// > Set remaps (A method for marking a set as an alias [irony?] of another)
  101. ///
  102. /// ==== Set chains ====
  103. /// This builder has a notion of some value A being above, below, or with some
  104. /// other value B:
  105. /// > The `A above B` relationship implies that there is a reference edge
  106. /// going from A to B. Namely, it notes that A can store anything in B's set.
  107. /// > The `A below B` relationship is the opposite of `A above B`. It implies
  108. /// that there's a dereference edge going from A to B.
  109. /// > The `A with B` relationship states that there's an assignment edge going
  110. /// from A to B, and that A and B should be treated as equals.
  111. ///
  112. /// As an example, take the following code snippet:
  113. ///
  114. /// %a = alloca i32, align 4
  115. /// %ap = alloca i32*, align 8
  116. /// %app = alloca i32**, align 8
  117. /// store %a, %ap
  118. /// store %ap, %app
  119. /// %aw = getelementptr %ap, i32 0
  120. ///
  121. /// Given this, the following relations exist:
  122. /// - %a below %ap & %ap above %a
  123. /// - %ap below %app & %app above %ap
  124. /// - %aw with %ap & %ap with %aw
  125. ///
  126. /// These relations produce the following sets:
  127. /// [{%a}, {%ap, %aw}, {%app}]
  128. ///
  129. /// ...Which state that the only MayAlias relationship in the above program is
  130. /// between %ap and %aw.
  131. ///
  132. /// Because LLVM allows arbitrary casts, code like the following needs to be
  133. /// supported:
  134. /// %ip = alloca i64, align 8
  135. /// %ipp = alloca i64*, align 8
  136. /// %i = bitcast i64** ipp to i64
  137. /// store i64* %ip, i64** %ipp
  138. /// store i64 %i, i64* %ip
  139. ///
  140. /// Which, because %ipp ends up *both* above and below %ip, is fun.
  141. ///
  142. /// This is solved by merging %i and %ipp into a single set (...which is the
  143. /// only way to solve this, since their bit patterns are equivalent). Any sets
  144. /// that ended up in between %i and %ipp at the time of merging (in this case,
  145. /// the set containing %ip) also get conservatively merged into the set of %i
  146. /// and %ipp. In short, the resulting StratifiedSet from the above code would be
  147. /// {%ip, %ipp, %i}.
  148. ///
  149. /// ==== Set remaps ====
  150. /// More of an implementation detail than anything -- when merging sets, we need
  151. /// to update the numbers of all of the elements mapped to those sets. Rather
  152. /// than doing this at each merge, we note in the BuilderLink structure that a
  153. /// remap has occurred, and use this information so we can defer renumbering set
  154. /// elements until build time.
  155. template <typename T> class StratifiedSetsBuilder {
  156. /// Represents a Stratified Set, with information about the Stratified
  157. /// Set above it, the set below it, and whether the current set has been
  158. /// remapped to another.
  159. struct BuilderLink {
  160. const StratifiedIndex Number;
  161. BuilderLink(StratifiedIndex N) : Number(N) {
  162. Remap = StratifiedLink::SetSentinel;
  163. }
  164. bool hasAbove() const {
  165. assert(!isRemapped());
  166. return Link.hasAbove();
  167. }
  168. bool hasBelow() const {
  169. assert(!isRemapped());
  170. return Link.hasBelow();
  171. }
  172. void setBelow(StratifiedIndex I) {
  173. assert(!isRemapped());
  174. Link.Below = I;
  175. }
  176. void setAbove(StratifiedIndex I) {
  177. assert(!isRemapped());
  178. Link.Above = I;
  179. }
  180. void clearBelow() {
  181. assert(!isRemapped());
  182. Link.clearBelow();
  183. }
  184. void clearAbove() {
  185. assert(!isRemapped());
  186. Link.clearAbove();
  187. }
  188. StratifiedIndex getBelow() const {
  189. assert(!isRemapped());
  190. assert(hasBelow());
  191. return Link.Below;
  192. }
  193. StratifiedIndex getAbove() const {
  194. assert(!isRemapped());
  195. assert(hasAbove());
  196. return Link.Above;
  197. }
  198. AliasAttrs getAttrs() {
  199. assert(!isRemapped());
  200. return Link.Attrs;
  201. }
  202. void setAttrs(AliasAttrs Other) {
  203. assert(!isRemapped());
  204. Link.Attrs |= Other;
  205. }
  206. bool isRemapped() const { return Remap != StratifiedLink::SetSentinel; }
  207. /// For initial remapping to another set
  208. void remapTo(StratifiedIndex Other) {
  209. assert(!isRemapped());
  210. Remap = Other;
  211. }
  212. StratifiedIndex getRemapIndex() const {
  213. assert(isRemapped());
  214. return Remap;
  215. }
  216. /// Should only be called when we're already remapped.
  217. void updateRemap(StratifiedIndex Other) {
  218. assert(isRemapped());
  219. Remap = Other;
  220. }
  221. /// Prefer the above functions to calling things directly on what's returned
  222. /// from this -- they guard against unexpected calls when the current
  223. /// BuilderLink is remapped.
  224. const StratifiedLink &getLink() const { return Link; }
  225. private:
  226. StratifiedLink Link;
  227. StratifiedIndex Remap;
  228. };
  229. /// This function performs all of the set unioning/value renumbering
  230. /// that we've been putting off, and generates a vector<StratifiedLink> that
  231. /// may be placed in a StratifiedSets instance.
  232. void finalizeSets(std::vector<StratifiedLink> &StratLinks) {
  233. DenseMap<StratifiedIndex, StratifiedIndex> Remaps;
  234. for (auto &Link : Links) {
  235. if (Link.isRemapped())
  236. continue;
  237. StratifiedIndex Number = StratLinks.size();
  238. Remaps.insert(std::make_pair(Link.Number, Number));
  239. StratLinks.push_back(Link.getLink());
  240. }
  241. for (auto &Link : StratLinks) {
  242. if (Link.hasAbove()) {
  243. auto &Above = linksAt(Link.Above);
  244. auto Iter = Remaps.find(Above.Number);
  245. assert(Iter != Remaps.end());
  246. Link.Above = Iter->second;
  247. }
  248. if (Link.hasBelow()) {
  249. auto &Below = linksAt(Link.Below);
  250. auto Iter = Remaps.find(Below.Number);
  251. assert(Iter != Remaps.end());
  252. Link.Below = Iter->second;
  253. }
  254. }
  255. for (auto &Pair : Values) {
  256. auto &Info = Pair.second;
  257. auto &Link = linksAt(Info.Index);
  258. auto Iter = Remaps.find(Link.Number);
  259. assert(Iter != Remaps.end());
  260. Info.Index = Iter->second;
  261. }
  262. }
  263. /// There's a guarantee in StratifiedLink where all bits set in a
  264. /// Link.externals will be set in all Link.externals "below" it.
  265. static void propagateAttrs(std::vector<StratifiedLink> &Links) {
  266. const auto getHighestParentAbove = [&Links](StratifiedIndex Idx) {
  267. const auto *Link = &Links[Idx];
  268. while (Link->hasAbove()) {
  269. Idx = Link->Above;
  270. Link = &Links[Idx];
  271. }
  272. return Idx;
  273. };
  274. SmallSet<StratifiedIndex, 16> Visited;
  275. for (unsigned I = 0, E = Links.size(); I < E; ++I) {
  276. auto CurrentIndex = getHighestParentAbove(I);
  277. if (!Visited.insert(CurrentIndex).second)
  278. continue;
  279. while (Links[CurrentIndex].hasBelow()) {
  280. auto &CurrentBits = Links[CurrentIndex].Attrs;
  281. auto NextIndex = Links[CurrentIndex].Below;
  282. auto &NextBits = Links[NextIndex].Attrs;
  283. NextBits |= CurrentBits;
  284. CurrentIndex = NextIndex;
  285. }
  286. }
  287. }
  288. public:
  289. /// Builds a StratifiedSet from the information we've been given since either
  290. /// construction or the prior build() call.
  291. StratifiedSets<T> build() {
  292. std::vector<StratifiedLink> StratLinks;
  293. finalizeSets(StratLinks);
  294. propagateAttrs(StratLinks);
  295. Links.clear();
  296. return StratifiedSets<T>(std::move(Values), std::move(StratLinks));
  297. }
  298. bool has(const T &Elem) const { return get(Elem).hasValue(); }
  299. bool add(const T &Main) {
  300. if (get(Main).hasValue())
  301. return false;
  302. auto NewIndex = getNewUnlinkedIndex();
  303. return addAtMerging(Main, NewIndex);
  304. }
  305. /// Restructures the stratified sets as necessary to make "ToAdd" in a
  306. /// set above "Main". There are some cases where this is not possible (see
  307. /// above), so we merge them such that ToAdd and Main are in the same set.
  308. bool addAbove(const T &Main, const T &ToAdd) {
  309. assert(has(Main));
  310. auto Index = *indexOf(Main);
  311. if (!linksAt(Index).hasAbove())
  312. addLinkAbove(Index);
  313. auto Above = linksAt(Index).getAbove();
  314. return addAtMerging(ToAdd, Above);
  315. }
  316. /// Restructures the stratified sets as necessary to make "ToAdd" in a
  317. /// set below "Main". There are some cases where this is not possible (see
  318. /// above), so we merge them such that ToAdd and Main are in the same set.
  319. bool addBelow(const T &Main, const T &ToAdd) {
  320. assert(has(Main));
  321. auto Index = *indexOf(Main);
  322. if (!linksAt(Index).hasBelow())
  323. addLinkBelow(Index);
  324. auto Below = linksAt(Index).getBelow();
  325. return addAtMerging(ToAdd, Below);
  326. }
  327. bool addWith(const T &Main, const T &ToAdd) {
  328. assert(has(Main));
  329. auto MainIndex = *indexOf(Main);
  330. return addAtMerging(ToAdd, MainIndex);
  331. }
  332. void noteAttributes(const T &Main, AliasAttrs NewAttrs) {
  333. assert(has(Main));
  334. auto *Info = *get(Main);
  335. auto &Link = linksAt(Info->Index);
  336. Link.setAttrs(NewAttrs);
  337. }
  338. private:
  339. DenseMap<T, StratifiedInfo> Values;
  340. std::vector<BuilderLink> Links;
  341. /// Adds the given element at the given index, merging sets if necessary.
  342. bool addAtMerging(const T &ToAdd, StratifiedIndex Index) {
  343. StratifiedInfo Info = {Index};
  344. auto Pair = Values.insert(std::make_pair(ToAdd, Info));
  345. if (Pair.second)
  346. return true;
  347. auto &Iter = Pair.first;
  348. auto &IterSet = linksAt(Iter->second.Index);
  349. auto &ReqSet = linksAt(Index);
  350. // Failed to add where we wanted to. Merge the sets.
  351. if (&IterSet != &ReqSet)
  352. merge(IterSet.Number, ReqSet.Number);
  353. return false;
  354. }
  355. /// Gets the BuilderLink at the given index, taking set remapping into
  356. /// account.
  357. BuilderLink &linksAt(StratifiedIndex Index) {
  358. auto *Start = &Links[Index];
  359. if (!Start->isRemapped())
  360. return *Start;
  361. auto *Current = Start;
  362. while (Current->isRemapped())
  363. Current = &Links[Current->getRemapIndex()];
  364. auto NewRemap = Current->Number;
  365. // Run through everything that has yet to be updated, and update them to
  366. // remap to NewRemap
  367. Current = Start;
  368. while (Current->isRemapped()) {
  369. auto *Next = &Links[Current->getRemapIndex()];
  370. Current->updateRemap(NewRemap);
  371. Current = Next;
  372. }
  373. return *Current;
  374. }
  375. /// Merges two sets into one another. Assumes that these sets are not
  376. /// already one in the same.
  377. void merge(StratifiedIndex Idx1, StratifiedIndex Idx2) {
  378. assert(inbounds(Idx1) && inbounds(Idx2));
  379. assert(&linksAt(Idx1) != &linksAt(Idx2) &&
  380. "Merging a set into itself is not allowed");
  381. // CASE 1: If the set at `Idx1` is above or below `Idx2`, we need to merge
  382. // both the
  383. // given sets, and all sets between them, into one.
  384. if (tryMergeUpwards(Idx1, Idx2))
  385. return;
  386. if (tryMergeUpwards(Idx2, Idx1))
  387. return;
  388. // CASE 2: The set at `Idx1` is not in the same chain as the set at `Idx2`.
  389. // We therefore need to merge the two chains together.
  390. mergeDirect(Idx1, Idx2);
  391. }
  392. /// Merges two sets assuming that the set at `Idx1` is unreachable from
  393. /// traversing above or below the set at `Idx2`.
  394. void mergeDirect(StratifiedIndex Idx1, StratifiedIndex Idx2) {
  395. assert(inbounds(Idx1) && inbounds(Idx2));
  396. auto *LinksInto = &linksAt(Idx1);
  397. auto *LinksFrom = &linksAt(Idx2);
  398. // Merging everything above LinksInto then proceeding to merge everything
  399. // below LinksInto becomes problematic, so we go as far "up" as possible!
  400. while (LinksInto->hasAbove() && LinksFrom->hasAbove()) {
  401. LinksInto = &linksAt(LinksInto->getAbove());
  402. LinksFrom = &linksAt(LinksFrom->getAbove());
  403. }
  404. if (LinksFrom->hasAbove()) {
  405. LinksInto->setAbove(LinksFrom->getAbove());
  406. auto &NewAbove = linksAt(LinksInto->getAbove());
  407. NewAbove.setBelow(LinksInto->Number);
  408. }
  409. // Merging strategy:
  410. // > If neither has links below, stop.
  411. // > If only `LinksInto` has links below, stop.
  412. // > If only `LinksFrom` has links below, reset `LinksInto.Below` to
  413. // match `LinksFrom.Below`
  414. // > If both have links above, deal with those next.
  415. while (LinksInto->hasBelow() && LinksFrom->hasBelow()) {
  416. auto FromAttrs = LinksFrom->getAttrs();
  417. LinksInto->setAttrs(FromAttrs);
  418. // Remap needs to happen after getBelow(), but before
  419. // assignment of LinksFrom
  420. auto *NewLinksFrom = &linksAt(LinksFrom->getBelow());
  421. LinksFrom->remapTo(LinksInto->Number);
  422. LinksFrom = NewLinksFrom;
  423. LinksInto = &linksAt(LinksInto->getBelow());
  424. }
  425. if (LinksFrom->hasBelow()) {
  426. LinksInto->setBelow(LinksFrom->getBelow());
  427. auto &NewBelow = linksAt(LinksInto->getBelow());
  428. NewBelow.setAbove(LinksInto->Number);
  429. }
  430. LinksInto->setAttrs(LinksFrom->getAttrs());
  431. LinksFrom->remapTo(LinksInto->Number);
  432. }
  433. /// Checks to see if lowerIndex is at a level lower than upperIndex. If so, it
  434. /// will merge lowerIndex with upperIndex (and all of the sets between) and
  435. /// return true. Otherwise, it will return false.
  436. bool tryMergeUpwards(StratifiedIndex LowerIndex, StratifiedIndex UpperIndex) {
  437. assert(inbounds(LowerIndex) && inbounds(UpperIndex));
  438. auto *Lower = &linksAt(LowerIndex);
  439. auto *Upper = &linksAt(UpperIndex);
  440. if (Lower == Upper)
  441. return true;
  442. SmallVector<BuilderLink *, 8> Found;
  443. auto *Current = Lower;
  444. auto Attrs = Current->getAttrs();
  445. while (Current->hasAbove() && Current != Upper) {
  446. Found.push_back(Current);
  447. Attrs |= Current->getAttrs();
  448. Current = &linksAt(Current->getAbove());
  449. }
  450. if (Current != Upper)
  451. return false;
  452. Upper->setAttrs(Attrs);
  453. if (Lower->hasBelow()) {
  454. auto NewBelowIndex = Lower->getBelow();
  455. Upper->setBelow(NewBelowIndex);
  456. auto &NewBelow = linksAt(NewBelowIndex);
  457. NewBelow.setAbove(UpperIndex);
  458. } else {
  459. Upper->clearBelow();
  460. }
  461. for (const auto &Ptr : Found)
  462. Ptr->remapTo(Upper->Number);
  463. return true;
  464. }
  465. Optional<const StratifiedInfo *> get(const T &Val) const {
  466. auto Result = Values.find(Val);
  467. if (Result == Values.end())
  468. return None;
  469. return &Result->second;
  470. }
  471. Optional<StratifiedInfo *> get(const T &Val) {
  472. auto Result = Values.find(Val);
  473. if (Result == Values.end())
  474. return None;
  475. return &Result->second;
  476. }
  477. Optional<StratifiedIndex> indexOf(const T &Val) {
  478. auto MaybeVal = get(Val);
  479. if (!MaybeVal.hasValue())
  480. return None;
  481. auto *Info = *MaybeVal;
  482. auto &Link = linksAt(Info->Index);
  483. return Link.Number;
  484. }
  485. StratifiedIndex addLinkBelow(StratifiedIndex Set) {
  486. auto At = addLinks();
  487. Links[Set].setBelow(At);
  488. Links[At].setAbove(Set);
  489. return At;
  490. }
  491. StratifiedIndex addLinkAbove(StratifiedIndex Set) {
  492. auto At = addLinks();
  493. Links[At].setBelow(Set);
  494. Links[Set].setAbove(At);
  495. return At;
  496. }
  497. StratifiedIndex getNewUnlinkedIndex() { return addLinks(); }
  498. StratifiedIndex addLinks() {
  499. auto Link = Links.size();
  500. Links.push_back(BuilderLink(Link));
  501. return Link;
  502. }
  503. bool inbounds(StratifiedIndex N) const { return N < Links.size(); }
  504. };
  505. }
  506. }
  507. #endif // LLVM_ADT_STRATIFIEDSETS_H