Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 1 | //===- StratifiedSets.h - Abstract stratified sets implementation. --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef LLVM_ADT_STRATIFIEDSETS_H |
| 11 | #define LLVM_ADT_STRATIFIEDSETS_H |
| 12 | |
| 13 | #include "llvm/ADT/DenseMap.h" |
| 14 | #include "llvm/ADT/Optional.h" |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallSet.h" |
| 16 | #include "llvm/ADT/SmallVector.h" |
| 17 | #include <bitset> |
| 18 | #include <cassert> |
| 19 | #include <cmath> |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 20 | #include <type_traits> |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 21 | #include <utility> |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
| 24 | namespace llvm { |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 25 | /// An index into Stratified Sets. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 26 | typedef unsigned StratifiedIndex; |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 27 | /// NOTE: ^ This can't be a short -- bootstrapping clang has a case where |
| 28 | /// ~1M sets exist. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 29 | |
| 30 | // \brief Container of information related to a value in a StratifiedSet. |
| 31 | struct StratifiedInfo { |
| 32 | StratifiedIndex Index; |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 33 | /// For field sensitivity, etc. we can tack fields on here. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 36 | /// The number of attributes that StratifiedAttrs should contain. Attributes are |
| 37 | /// described below, and 32 was an arbitrary choice because it fits nicely in 32 |
| 38 | /// bits (because we use a bitset for StratifiedAttrs). |
Hal Finkel | 981602a | 2014-09-02 22:26:06 +0000 | [diff] [blame] | 39 | static const unsigned NumStratifiedAttrs = 32; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 40 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 41 | /// These are attributes that the users of StratifiedSets/StratifiedSetBuilders |
| 42 | /// may use for various purposes. These also have the special property of that |
| 43 | /// they are merged down. So, if set A is above set B, and one decides to set an |
| 44 | /// attribute in set A, then the attribute will automatically be set in set B. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 45 | typedef std::bitset<NumStratifiedAttrs> StratifiedAttrs; |
| 46 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 47 | /// A "link" between two StratifiedSets. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 48 | struct StratifiedLink { |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 49 | /// \brief This is a value used to signify "does not exist" where the |
| 50 | /// StratifiedIndex type is used. |
| 51 | /// |
| 52 | /// This is used instead of Optional<StratifiedIndex> because |
| 53 | /// Optional<StratifiedIndex> would eat up a considerable amount of extra |
| 54 | /// memory, after struct padding/alignment is taken into account. |
Hal Finkel | 1ae325f | 2014-09-02 23:50:01 +0000 | [diff] [blame] | 55 | static const StratifiedIndex SetSentinel; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 56 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 57 | /// The index for the set "above" current |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 58 | StratifiedIndex Above; |
| 59 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 60 | /// The link for the set "below" current |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 61 | StratifiedIndex Below; |
| 62 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 63 | /// Attributes for these StratifiedSets. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 64 | StratifiedAttrs Attrs; |
| 65 | |
| 66 | StratifiedLink() : Above(SetSentinel), Below(SetSentinel) {} |
| 67 | |
| 68 | bool hasBelow() const { return Below != SetSentinel; } |
| 69 | bool hasAbove() const { return Above != SetSentinel; } |
| 70 | |
| 71 | void clearBelow() { Below = SetSentinel; } |
| 72 | void clearAbove() { Above = SetSentinel; } |
| 73 | }; |
| 74 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 75 | /// \brief These are stratified sets, as described in "Fast algorithms for |
| 76 | /// Dyck-CFL-reachability with applications to Alias Analysis" by Zhang Q, Lyu M |
| 77 | /// R, Yuan H, and Su Z. -- in short, this is meant to represent different sets |
| 78 | /// of Value*s. If two Value*s are in the same set, or if both sets have |
| 79 | /// overlapping attributes, then the Value*s are said to alias. |
| 80 | /// |
| 81 | /// Sets may be related by position, meaning that one set may be considered as |
| 82 | /// above or below another. In CFL Alias Analysis, this gives us an indication |
| 83 | /// of how two variables are related; if the set of variable A is below a set |
| 84 | /// containing variable B, then at some point, a variable that has interacted |
| 85 | /// with B (or B itself) was either used in order to extract the variable A, or |
| 86 | /// was used as storage of variable A. |
| 87 | /// |
| 88 | /// Sets may also have attributes (as noted above). These attributes are |
| 89 | /// generally used for noting whether a variable in the set has interacted with |
| 90 | /// a variable whose origins we don't quite know (i.e. globals/arguments), or if |
| 91 | /// the variable may have had operations performed on it (modified in a function |
| 92 | /// call). All attributes that exist in a set A must exist in all sets marked as |
| 93 | /// below set A. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 94 | template <typename T> class StratifiedSets { |
| 95 | public: |
| 96 | StratifiedSets() {} |
| 97 | |
| 98 | StratifiedSets(DenseMap<T, StratifiedInfo> Map, |
| 99 | std::vector<StratifiedLink> Links) |
| 100 | : Values(std::move(Map)), Links(std::move(Links)) {} |
| 101 | |
| 102 | StratifiedSets(StratifiedSets<T> &&Other) { *this = std::move(Other); } |
| 103 | |
| 104 | StratifiedSets &operator=(StratifiedSets<T> &&Other) { |
| 105 | Values = std::move(Other.Values); |
| 106 | Links = std::move(Other.Links); |
| 107 | return *this; |
| 108 | } |
| 109 | |
| 110 | Optional<StratifiedInfo> find(const T &Elem) const { |
| 111 | auto Iter = Values.find(Elem); |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 112 | if (Iter == Values.end()) |
| 113 | return None; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 114 | return Iter->second; |
| 115 | } |
| 116 | |
| 117 | const StratifiedLink &getLink(StratifiedIndex Index) const { |
| 118 | assert(inbounds(Index)); |
| 119 | return Links[Index]; |
| 120 | } |
| 121 | |
| 122 | private: |
| 123 | DenseMap<T, StratifiedInfo> Values; |
| 124 | std::vector<StratifiedLink> Links; |
| 125 | |
| 126 | bool inbounds(StratifiedIndex Idx) const { return Idx < Links.size(); } |
| 127 | }; |
| 128 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 129 | /// Generic Builder class that produces StratifiedSets instances. |
| 130 | /// |
| 131 | /// The goal of this builder is to efficiently produce correct StratifiedSets |
| 132 | /// instances. To this end, we use a few tricks: |
| 133 | /// > Set chains (A method for linking sets together) |
| 134 | /// > Set remaps (A method for marking a set as an alias [irony?] of another) |
| 135 | /// |
| 136 | /// ==== Set chains ==== |
| 137 | /// This builder has a notion of some value A being above, below, or with some |
| 138 | /// other value B: |
| 139 | /// > The `A above B` relationship implies that there is a reference edge |
| 140 | /// going from A to B. Namely, it notes that A can store anything in B's set. |
| 141 | /// > The `A below B` relationship is the opposite of `A above B`. It implies |
| 142 | /// that there's a dereference edge going from A to B. |
| 143 | /// > The `A with B` relationship states that there's an assignment edge going |
| 144 | /// from A to B, and that A and B should be treated as equals. |
| 145 | /// |
| 146 | /// As an example, take the following code snippet: |
| 147 | /// |
| 148 | /// %a = alloca i32, align 4 |
| 149 | /// %ap = alloca i32*, align 8 |
| 150 | /// %app = alloca i32**, align 8 |
| 151 | /// store %a, %ap |
| 152 | /// store %ap, %app |
| 153 | /// %aw = getelementptr %ap, 0 |
| 154 | /// |
| 155 | /// Given this, the follow relations exist: |
| 156 | /// - %a below %ap & %ap above %a |
| 157 | /// - %ap below %app & %app above %ap |
| 158 | /// - %aw with %ap & %ap with %aw |
| 159 | /// |
| 160 | /// These relations produce the following sets: |
| 161 | /// [{%a}, {%ap, %aw}, {%app}] |
| 162 | /// |
| 163 | /// ...Which states that the only MayAlias relationship in the above program is |
| 164 | /// between %ap and %aw. |
| 165 | /// |
| 166 | /// Life gets more complicated when we actually have logic in our programs. So, |
| 167 | /// we either must remove this logic from our programs, or make consessions for |
| 168 | /// it in our AA algorithms. In this case, we have decided to select the latter |
| 169 | /// option. |
| 170 | /// |
| 171 | /// First complication: Conditionals |
| 172 | /// Motivation: |
| 173 | /// %ad = alloca int, align 4 |
| 174 | /// %a = alloca int*, align 8 |
| 175 | /// %b = alloca int*, align 8 |
| 176 | /// %bp = alloca int**, align 8 |
| 177 | /// %c = call i1 @SomeFunc() |
| 178 | /// %k = select %c, %ad, %bp |
| 179 | /// store %ad, %a |
| 180 | /// store %b, %bp |
| 181 | /// |
| 182 | /// %k has 'with' edges to both %a and %b, which ordinarily would not be linked |
| 183 | /// together. So, we merge the set that contains %a with the set that contains |
| 184 | /// %b. We then recursively merge the set above %a with the set above %b, and |
| 185 | /// the set below %a with the set below %b, etc. Ultimately, the sets for this |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 186 | // program would end up like: {%ad}, {%a, %b, %k}, {%bp}, where {%ad} is below |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 187 | /// {%a, %b, %c} is below {%ad}. |
| 188 | /// |
| 189 | /// Second complication: Arbitrary casts |
| 190 | /// Motivation: |
| 191 | /// %ip = alloca int*, align 8 |
| 192 | /// %ipp = alloca int**, align 8 |
| 193 | /// %i = bitcast ipp to int |
| 194 | /// store %ip, %ipp |
| 195 | /// store %i, %ip |
| 196 | /// |
| 197 | /// This is impossible to construct with any of the rules above, because a set |
| 198 | /// containing both {%i, %ipp} is supposed to exist, the set with %i is supposed |
| 199 | /// to be below the set with %ip, and the set with %ip is supposed to be below |
| 200 | /// the set with %ipp. Because we don't allow circular relationships like this, |
| 201 | /// we merge all concerned sets into one. So, the above code would generate a |
| 202 | /// single StratifiedSet: {%ip, %ipp, %i}. |
| 203 | /// |
| 204 | /// ==== Set remaps ==== |
| 205 | /// More of an implementation detail than anything -- when merging sets, we need |
| 206 | /// to update the numbers of all of the elements mapped to those sets. Rather |
| 207 | /// than doing this at each merge, we note in the BuilderLink structure that a |
| 208 | /// remap has occurred, and use this information so we can defer renumbering set |
| 209 | /// elements until build time. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 210 | template <typename T> class StratifiedSetsBuilder { |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 211 | /// \brief Represents a Stratified Set, with information about the Stratified |
| 212 | /// Set above it, the set below it, and whether the current set has been |
| 213 | /// remapped to another. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 214 | struct BuilderLink { |
| 215 | const StratifiedIndex Number; |
| 216 | |
| 217 | BuilderLink(StratifiedIndex N) : Number(N) { |
| 218 | Remap = StratifiedLink::SetSentinel; |
| 219 | } |
| 220 | |
| 221 | bool hasAbove() const { |
| 222 | assert(!isRemapped()); |
| 223 | return Link.hasAbove(); |
| 224 | } |
| 225 | |
| 226 | bool hasBelow() const { |
| 227 | assert(!isRemapped()); |
| 228 | return Link.hasBelow(); |
| 229 | } |
| 230 | |
| 231 | void setBelow(StratifiedIndex I) { |
| 232 | assert(!isRemapped()); |
| 233 | Link.Below = I; |
| 234 | } |
| 235 | |
| 236 | void setAbove(StratifiedIndex I) { |
| 237 | assert(!isRemapped()); |
| 238 | Link.Above = I; |
| 239 | } |
| 240 | |
| 241 | void clearBelow() { |
| 242 | assert(!isRemapped()); |
| 243 | Link.clearBelow(); |
| 244 | } |
| 245 | |
| 246 | void clearAbove() { |
| 247 | assert(!isRemapped()); |
| 248 | Link.clearAbove(); |
| 249 | } |
| 250 | |
| 251 | StratifiedIndex getBelow() const { |
| 252 | assert(!isRemapped()); |
| 253 | assert(hasBelow()); |
| 254 | return Link.Below; |
| 255 | } |
| 256 | |
| 257 | StratifiedIndex getAbove() const { |
| 258 | assert(!isRemapped()); |
| 259 | assert(hasAbove()); |
| 260 | return Link.Above; |
| 261 | } |
| 262 | |
| 263 | StratifiedAttrs &getAttrs() { |
| 264 | assert(!isRemapped()); |
| 265 | return Link.Attrs; |
| 266 | } |
| 267 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 268 | void setAttrs(const StratifiedAttrs &other) { |
| 269 | assert(!isRemapped()); |
| 270 | Link.Attrs |= other; |
| 271 | } |
| 272 | |
| 273 | bool isRemapped() const { return Remap != StratifiedLink::SetSentinel; } |
| 274 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 275 | /// For initial remapping to another set |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 276 | void remapTo(StratifiedIndex Other) { |
| 277 | assert(!isRemapped()); |
| 278 | Remap = Other; |
| 279 | } |
| 280 | |
| 281 | StratifiedIndex getRemapIndex() const { |
| 282 | assert(isRemapped()); |
| 283 | return Remap; |
| 284 | } |
| 285 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 286 | /// Should only be called when we're already remapped. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 287 | void updateRemap(StratifiedIndex Other) { |
| 288 | assert(isRemapped()); |
| 289 | Remap = Other; |
| 290 | } |
| 291 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 292 | /// Prefer the above functions to calling things directly on what's returned |
| 293 | /// from this -- they guard against unexpected calls when the current |
| 294 | /// BuilderLink is remapped. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 295 | const StratifiedLink &getLink() const { return Link; } |
| 296 | |
| 297 | private: |
| 298 | StratifiedLink Link; |
| 299 | StratifiedIndex Remap; |
| 300 | }; |
| 301 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 302 | /// \brief This function performs all of the set unioning/value renumbering |
| 303 | /// that we've been putting off, and generates a vector<StratifiedLink> that |
| 304 | /// may be placed in a StratifiedSets instance. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 305 | void finalizeSets(std::vector<StratifiedLink> &StratLinks) { |
| 306 | DenseMap<StratifiedIndex, StratifiedIndex> Remaps; |
| 307 | for (auto &Link : Links) { |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 308 | if (Link.isRemapped()) |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 309 | continue; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 310 | |
| 311 | StratifiedIndex Number = StratLinks.size(); |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 312 | Remaps.insert(std::make_pair(Link.Number, Number)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 313 | StratLinks.push_back(Link.getLink()); |
| 314 | } |
| 315 | |
| 316 | for (auto &Link : StratLinks) { |
| 317 | if (Link.hasAbove()) { |
| 318 | auto &Above = linksAt(Link.Above); |
| 319 | auto Iter = Remaps.find(Above.Number); |
| 320 | assert(Iter != Remaps.end()); |
| 321 | Link.Above = Iter->second; |
| 322 | } |
| 323 | |
| 324 | if (Link.hasBelow()) { |
| 325 | auto &Below = linksAt(Link.Below); |
| 326 | auto Iter = Remaps.find(Below.Number); |
| 327 | assert(Iter != Remaps.end()); |
| 328 | Link.Below = Iter->second; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | for (auto &Pair : Values) { |
| 333 | auto &Info = Pair.second; |
| 334 | auto &Link = linksAt(Info.Index); |
| 335 | auto Iter = Remaps.find(Link.Number); |
| 336 | assert(Iter != Remaps.end()); |
| 337 | Info.Index = Iter->second; |
| 338 | } |
| 339 | } |
| 340 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 341 | /// \brief There's a guarantee in StratifiedLink where all bits set in a |
| 342 | /// Link.externals will be set in all Link.externals "below" it. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 343 | static void propagateAttrs(std::vector<StratifiedLink> &Links) { |
| 344 | const auto getHighestParentAbove = [&Links](StratifiedIndex Idx) { |
| 345 | const auto *Link = &Links[Idx]; |
| 346 | while (Link->hasAbove()) { |
| 347 | Idx = Link->Above; |
| 348 | Link = &Links[Idx]; |
| 349 | } |
| 350 | return Idx; |
| 351 | }; |
| 352 | |
| 353 | SmallSet<StratifiedIndex, 16> Visited; |
| 354 | for (unsigned I = 0, E = Links.size(); I < E; ++I) { |
| 355 | auto CurrentIndex = getHighestParentAbove(I); |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 356 | if (!Visited.insert(CurrentIndex).second) |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 357 | continue; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 358 | |
| 359 | while (Links[CurrentIndex].hasBelow()) { |
| 360 | auto &CurrentBits = Links[CurrentIndex].Attrs; |
| 361 | auto NextIndex = Links[CurrentIndex].Below; |
| 362 | auto &NextBits = Links[NextIndex].Attrs; |
| 363 | NextBits |= CurrentBits; |
| 364 | CurrentIndex = NextIndex; |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | public: |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 370 | /// Builds a StratifiedSet from the information we've been given since either |
| 371 | /// construction or the prior build() call. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 372 | StratifiedSets<T> build() { |
| 373 | std::vector<StratifiedLink> StratLinks; |
| 374 | finalizeSets(StratLinks); |
| 375 | propagateAttrs(StratLinks); |
| 376 | Links.clear(); |
| 377 | return StratifiedSets<T>(std::move(Values), std::move(StratLinks)); |
| 378 | } |
| 379 | |
| 380 | std::size_t size() const { return Values.size(); } |
| 381 | std::size_t numSets() const { return Links.size(); } |
| 382 | |
| 383 | bool has(const T &Elem) const { return get(Elem).hasValue(); } |
| 384 | |
| 385 | bool add(const T &Main) { |
| 386 | if (get(Main).hasValue()) |
| 387 | return false; |
| 388 | |
| 389 | auto NewIndex = getNewUnlinkedIndex(); |
| 390 | return addAtMerging(Main, NewIndex); |
| 391 | } |
| 392 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 393 | /// \brief Restructures the stratified sets as necessary to make "ToAdd" in a |
| 394 | /// set above "Main". There are some cases where this is not possible (see |
| 395 | /// above), so we merge them such that ToAdd and Main are in the same set. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 396 | bool addAbove(const T &Main, const T &ToAdd) { |
| 397 | assert(has(Main)); |
| 398 | auto Index = *indexOf(Main); |
| 399 | if (!linksAt(Index).hasAbove()) |
| 400 | addLinkAbove(Index); |
| 401 | |
| 402 | auto Above = linksAt(Index).getAbove(); |
| 403 | return addAtMerging(ToAdd, Above); |
| 404 | } |
| 405 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 406 | /// \brief Restructures the stratified sets as necessary to make "ToAdd" in a |
| 407 | /// set below "Main". There are some cases where this is not possible (see |
| 408 | /// above), so we merge them such that ToAdd and Main are in the same set. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 409 | bool addBelow(const T &Main, const T &ToAdd) { |
| 410 | assert(has(Main)); |
| 411 | auto Index = *indexOf(Main); |
| 412 | if (!linksAt(Index).hasBelow()) |
| 413 | addLinkBelow(Index); |
| 414 | |
| 415 | auto Below = linksAt(Index).getBelow(); |
| 416 | return addAtMerging(ToAdd, Below); |
| 417 | } |
| 418 | |
| 419 | bool addWith(const T &Main, const T &ToAdd) { |
| 420 | assert(has(Main)); |
| 421 | auto MainIndex = *indexOf(Main); |
| 422 | return addAtMerging(ToAdd, MainIndex); |
| 423 | } |
| 424 | |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 425 | void noteAttributes(const T &Main, const StratifiedAttrs &NewAttrs) { |
| 426 | assert(has(Main)); |
| 427 | auto *Info = *get(Main); |
| 428 | auto &Link = linksAt(Info->Index); |
| 429 | Link.setAttrs(NewAttrs); |
| 430 | } |
| 431 | |
| 432 | StratifiedAttrs getAttributes(const T &Main) { |
| 433 | assert(has(Main)); |
| 434 | auto *Info = *get(Main); |
| 435 | auto *Link = &linksAt(Info->Index); |
| 436 | auto Attrs = Link->getAttrs(); |
| 437 | while (Link->hasAbove()) { |
| 438 | Link = &linksAt(Link->getAbove()); |
| 439 | Attrs |= Link->getAttrs(); |
| 440 | } |
| 441 | |
| 442 | return Attrs; |
| 443 | } |
| 444 | |
| 445 | bool getAttribute(const T &Main, unsigned AttrNum) { |
| 446 | assert(AttrNum < StratifiedLink::SetSentinel); |
| 447 | auto Attrs = getAttributes(Main); |
| 448 | return Attrs[AttrNum]; |
| 449 | } |
| 450 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 451 | /// \brief Gets the attributes that have been applied to the set that Main |
| 452 | /// belongs to. It ignores attributes in any sets above the one that Main |
| 453 | /// resides in. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 454 | StratifiedAttrs getRawAttributes(const T &Main) { |
| 455 | assert(has(Main)); |
| 456 | auto *Info = *get(Main); |
| 457 | auto &Link = linksAt(Info->Index); |
| 458 | return Link.getAttrs(); |
| 459 | } |
| 460 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 461 | /// \brief Gets an attribute from the attributes that have been applied to the |
| 462 | /// set that Main belongs to. It ignores attributes in any sets above the one |
| 463 | /// that Main resides in. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 464 | bool getRawAttribute(const T &Main, unsigned AttrNum) { |
| 465 | assert(AttrNum < StratifiedLink::SetSentinel); |
| 466 | auto Attrs = getRawAttributes(Main); |
| 467 | return Attrs[AttrNum]; |
| 468 | } |
| 469 | |
| 470 | private: |
| 471 | DenseMap<T, StratifiedInfo> Values; |
| 472 | std::vector<BuilderLink> Links; |
| 473 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 474 | /// Adds the given element at the given index, merging sets if necessary. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 475 | bool addAtMerging(const T &ToAdd, StratifiedIndex Index) { |
| 476 | StratifiedInfo Info = {Index}; |
Hal Finkel | 8d1590d | 2014-09-02 22:52:30 +0000 | [diff] [blame] | 477 | auto Pair = Values.insert(std::make_pair(ToAdd, Info)); |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 478 | if (Pair.second) |
| 479 | return true; |
| 480 | |
| 481 | auto &Iter = Pair.first; |
| 482 | auto &IterSet = linksAt(Iter->second.Index); |
| 483 | auto &ReqSet = linksAt(Index); |
| 484 | |
| 485 | // Failed to add where we wanted to. Merge the sets. |
| 486 | if (&IterSet != &ReqSet) |
| 487 | merge(IterSet.Number, ReqSet.Number); |
| 488 | |
| 489 | return false; |
| 490 | } |
| 491 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 492 | /// Gets the BuilderLink at the given index, taking set remapping into |
| 493 | /// account. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 494 | BuilderLink &linksAt(StratifiedIndex Index) { |
| 495 | auto *Start = &Links[Index]; |
| 496 | if (!Start->isRemapped()) |
| 497 | return *Start; |
| 498 | |
| 499 | auto *Current = Start; |
| 500 | while (Current->isRemapped()) |
| 501 | Current = &Links[Current->getRemapIndex()]; |
| 502 | |
| 503 | auto NewRemap = Current->Number; |
| 504 | |
| 505 | // Run through everything that has yet to be updated, and update them to |
| 506 | // remap to NewRemap |
| 507 | Current = Start; |
| 508 | while (Current->isRemapped()) { |
| 509 | auto *Next = &Links[Current->getRemapIndex()]; |
| 510 | Current->updateRemap(NewRemap); |
| 511 | Current = Next; |
| 512 | } |
| 513 | |
| 514 | return *Current; |
| 515 | } |
| 516 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 517 | /// \brief Merges two sets into one another. Assumes that these sets are not |
| 518 | /// already one in the same. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 519 | void merge(StratifiedIndex Idx1, StratifiedIndex Idx2) { |
| 520 | assert(inbounds(Idx1) && inbounds(Idx2)); |
| 521 | assert(&linksAt(Idx1) != &linksAt(Idx2) && |
| 522 | "Merging a set into itself is not allowed"); |
| 523 | |
| 524 | // CASE 1: If the set at `Idx1` is above or below `Idx2`, we need to merge |
| 525 | // both the |
| 526 | // given sets, and all sets between them, into one. |
| 527 | if (tryMergeUpwards(Idx1, Idx2)) |
| 528 | return; |
| 529 | |
| 530 | if (tryMergeUpwards(Idx2, Idx1)) |
| 531 | return; |
| 532 | |
| 533 | // CASE 2: The set at `Idx1` is not in the same chain as the set at `Idx2`. |
| 534 | // We therefore need to merge the two chains together. |
| 535 | mergeDirect(Idx1, Idx2); |
| 536 | } |
| 537 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 538 | /// \brief Merges two sets assuming that the set at `Idx1` is unreachable from |
| 539 | /// traversing above or below the set at `Idx2`. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 540 | void mergeDirect(StratifiedIndex Idx1, StratifiedIndex Idx2) { |
| 541 | assert(inbounds(Idx1) && inbounds(Idx2)); |
| 542 | |
| 543 | auto *LinksInto = &linksAt(Idx1); |
| 544 | auto *LinksFrom = &linksAt(Idx2); |
| 545 | // Merging everything above LinksInto then proceeding to merge everything |
| 546 | // below LinksInto becomes problematic, so we go as far "up" as possible! |
| 547 | while (LinksInto->hasAbove() && LinksFrom->hasAbove()) { |
| 548 | LinksInto = &linksAt(LinksInto->getAbove()); |
| 549 | LinksFrom = &linksAt(LinksFrom->getAbove()); |
| 550 | } |
| 551 | |
| 552 | if (LinksFrom->hasAbove()) { |
| 553 | LinksInto->setAbove(LinksFrom->getAbove()); |
| 554 | auto &NewAbove = linksAt(LinksInto->getAbove()); |
| 555 | NewAbove.setBelow(LinksInto->Number); |
| 556 | } |
| 557 | |
| 558 | // Merging strategy: |
| 559 | // > If neither has links below, stop. |
| 560 | // > If only `LinksInto` has links below, stop. |
| 561 | // > If only `LinksFrom` has links below, reset `LinksInto.Below` to |
| 562 | // match `LinksFrom.Below` |
| 563 | // > If both have links above, deal with those next. |
| 564 | while (LinksInto->hasBelow() && LinksFrom->hasBelow()) { |
| 565 | auto &FromAttrs = LinksFrom->getAttrs(); |
| 566 | LinksInto->setAttrs(FromAttrs); |
| 567 | |
| 568 | // Remap needs to happen after getBelow(), but before |
| 569 | // assignment of LinksFrom |
| 570 | auto *NewLinksFrom = &linksAt(LinksFrom->getBelow()); |
| 571 | LinksFrom->remapTo(LinksInto->Number); |
| 572 | LinksFrom = NewLinksFrom; |
| 573 | LinksInto = &linksAt(LinksInto->getBelow()); |
| 574 | } |
| 575 | |
| 576 | if (LinksFrom->hasBelow()) { |
| 577 | LinksInto->setBelow(LinksFrom->getBelow()); |
| 578 | auto &NewBelow = linksAt(LinksInto->getBelow()); |
| 579 | NewBelow.setAbove(LinksInto->Number); |
| 580 | } |
| 581 | |
| 582 | LinksFrom->remapTo(LinksInto->Number); |
| 583 | } |
| 584 | |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 585 | /// Checks to see if lowerIndex is at a level lower than upperIndex. If so, it |
| 586 | /// will merge lowerIndex with upperIndex (and all of the sets between) and |
| 587 | /// return true. Otherwise, it will return false. |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 588 | bool tryMergeUpwards(StratifiedIndex LowerIndex, StratifiedIndex UpperIndex) { |
| 589 | assert(inbounds(LowerIndex) && inbounds(UpperIndex)); |
| 590 | auto *Lower = &linksAt(LowerIndex); |
| 591 | auto *Upper = &linksAt(UpperIndex); |
| 592 | if (Lower == Upper) |
| 593 | return true; |
| 594 | |
| 595 | SmallVector<BuilderLink *, 8> Found; |
| 596 | auto *Current = Lower; |
| 597 | auto Attrs = Current->getAttrs(); |
| 598 | while (Current->hasAbove() && Current != Upper) { |
| 599 | Found.push_back(Current); |
| 600 | Attrs |= Current->getAttrs(); |
| 601 | Current = &linksAt(Current->getAbove()); |
| 602 | } |
| 603 | |
| 604 | if (Current != Upper) |
| 605 | return false; |
| 606 | |
| 607 | Upper->setAttrs(Attrs); |
| 608 | |
| 609 | if (Lower->hasBelow()) { |
| 610 | auto NewBelowIndex = Lower->getBelow(); |
| 611 | Upper->setBelow(NewBelowIndex); |
| 612 | auto &NewBelow = linksAt(NewBelowIndex); |
| 613 | NewBelow.setAbove(UpperIndex); |
| 614 | } else { |
| 615 | Upper->clearBelow(); |
| 616 | } |
| 617 | |
| 618 | for (const auto &Ptr : Found) |
| 619 | Ptr->remapTo(Upper->Number); |
| 620 | |
| 621 | return true; |
| 622 | } |
| 623 | |
| 624 | Optional<const StratifiedInfo *> get(const T &Val) const { |
| 625 | auto Result = Values.find(Val); |
| 626 | if (Result == Values.end()) |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 627 | return None; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 628 | return &Result->second; |
| 629 | } |
| 630 | |
| 631 | Optional<StratifiedInfo *> get(const T &Val) { |
| 632 | auto Result = Values.find(Val); |
| 633 | if (Result == Values.end()) |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 634 | return None; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 635 | return &Result->second; |
| 636 | } |
| 637 | |
| 638 | Optional<StratifiedIndex> indexOf(const T &Val) { |
| 639 | auto MaybeVal = get(Val); |
| 640 | if (!MaybeVal.hasValue()) |
George Burgess IV | cae581d | 2016-04-13 23:27:37 +0000 | [diff] [blame] | 641 | return None; |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 642 | auto *Info = *MaybeVal; |
| 643 | auto &Link = linksAt(Info->Index); |
| 644 | return Link.Number; |
| 645 | } |
| 646 | |
| 647 | StratifiedIndex addLinkBelow(StratifiedIndex Set) { |
| 648 | auto At = addLinks(); |
| 649 | Links[Set].setBelow(At); |
| 650 | Links[At].setAbove(Set); |
| 651 | return At; |
| 652 | } |
| 653 | |
| 654 | StratifiedIndex addLinkAbove(StratifiedIndex Set) { |
| 655 | auto At = addLinks(); |
| 656 | Links[At].setBelow(Set); |
| 657 | Links[Set].setAbove(At); |
| 658 | return At; |
| 659 | } |
| 660 | |
| 661 | StratifiedIndex getNewUnlinkedIndex() { return addLinks(); } |
| 662 | |
| 663 | StratifiedIndex addLinks() { |
| 664 | auto Link = Links.size(); |
| 665 | Links.push_back(BuilderLink(Link)); |
| 666 | return Link; |
| 667 | } |
| 668 | |
Hal Finkel | 42b7e01 | 2014-09-02 22:36:58 +0000 | [diff] [blame] | 669 | bool inbounds(StratifiedIndex N) const { return N < Links.size(); } |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 670 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 671 | } |
Hal Finkel | 7529c55 | 2014-09-02 21:43:13 +0000 | [diff] [blame] | 672 | #endif // LLVM_ADT_STRATIFIEDSETS_H |