2014-09-03 05:43:13 +08:00
|
|
|
//===- StratifiedSets.h - Abstract stratified sets implementation. --------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ADT_STRATIFIEDSETS_H
|
|
|
|
#define LLVM_ADT_STRATIFIEDSETS_H
|
|
|
|
|
2016-07-06 08:47:21 +08:00
|
|
|
#include "AliasAnalysisSummary.h"
|
2014-09-03 05:43:13 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/ADT/SmallSet.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include <bitset>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cmath>
|
|
|
|
#include <type_traits>
|
2014-09-03 06:52:30 +08:00
|
|
|
#include <utility>
|
2014-09-03 05:43:13 +08:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace llvm {
|
2016-07-06 08:36:12 +08:00
|
|
|
namespace cflaa {
|
2016-04-14 07:27:37 +08:00
|
|
|
/// An index into Stratified Sets.
|
2014-09-03 05:43:13 +08:00
|
|
|
typedef unsigned StratifiedIndex;
|
2016-04-14 07:27:37 +08:00
|
|
|
/// NOTE: ^ This can't be a short -- bootstrapping clang has a case where
|
|
|
|
/// ~1M sets exist.
|
2014-09-03 05:43:13 +08:00
|
|
|
|
|
|
|
// \brief Container of information related to a value in a StratifiedSet.
|
|
|
|
struct StratifiedInfo {
|
|
|
|
StratifiedIndex Index;
|
2016-04-14 07:27:37 +08:00
|
|
|
/// For field sensitivity, etc. we can tack fields on here.
|
2014-09-03 05:43:13 +08:00
|
|
|
};
|
|
|
|
|
2016-04-14 07:27:37 +08:00
|
|
|
/// A "link" between two StratifiedSets.
|
2014-09-03 05:43:13 +08:00
|
|
|
struct StratifiedLink {
|
2016-04-14 07:27:37 +08:00
|
|
|
/// \brief This is a value used to signify "does not exist" where the
|
|
|
|
/// StratifiedIndex type is used.
|
|
|
|
///
|
|
|
|
/// This is used instead of Optional<StratifiedIndex> because
|
|
|
|
/// Optional<StratifiedIndex> would eat up a considerable amount of extra
|
|
|
|
/// memory, after struct padding/alignment is taken into account.
|
2014-09-03 07:50:01 +08:00
|
|
|
static const StratifiedIndex SetSentinel;
|
2014-09-03 05:43:13 +08:00
|
|
|
|
2016-04-14 07:27:37 +08:00
|
|
|
/// The index for the set "above" current
|
2014-09-03 05:43:13 +08:00
|
|
|
StratifiedIndex Above;
|
|
|
|
|
2016-04-14 07:27:37 +08:00
|
|
|
/// The link for the set "below" current
|
2014-09-03 05:43:13 +08:00
|
|
|
StratifiedIndex Below;
|
|
|
|
|
2016-04-14 07:27:37 +08:00
|
|
|
/// Attributes for these StratifiedSets.
|
2016-07-06 08:47:21 +08:00
|
|
|
AliasAttrs Attrs;
|
2014-09-03 05:43:13 +08:00
|
|
|
|
|
|
|
StratifiedLink() : Above(SetSentinel), Below(SetSentinel) {}
|
|
|
|
|
|
|
|
bool hasBelow() const { return Below != SetSentinel; }
|
|
|
|
bool hasAbove() const { return Above != SetSentinel; }
|
|
|
|
|
|
|
|
void clearBelow() { Below = SetSentinel; }
|
|
|
|
void clearAbove() { Above = SetSentinel; }
|
|
|
|
};
|
|
|
|
|
2016-04-14 07:27:37 +08:00
|
|
|
/// \brief These are stratified sets, as described in "Fast algorithms for
|
|
|
|
/// Dyck-CFL-reachability with applications to Alias Analysis" by Zhang Q, Lyu M
|
|
|
|
/// R, Yuan H, and Su Z. -- in short, this is meant to represent different sets
|
|
|
|
/// of Value*s. If two Value*s are in the same set, or if both sets have
|
|
|
|
/// overlapping attributes, then the Value*s are said to alias.
|
|
|
|
///
|
|
|
|
/// Sets may be related by position, meaning that one set may be considered as
|
|
|
|
/// above or below another. In CFL Alias Analysis, this gives us an indication
|
|
|
|
/// of how two variables are related; if the set of variable A is below a set
|
|
|
|
/// containing variable B, then at some point, a variable that has interacted
|
|
|
|
/// with B (or B itself) was either used in order to extract the variable A, or
|
|
|
|
/// was used as storage of variable A.
|
|
|
|
///
|
|
|
|
/// Sets may also have attributes (as noted above). These attributes are
|
|
|
|
/// generally used for noting whether a variable in the set has interacted with
|
|
|
|
/// a variable whose origins we don't quite know (i.e. globals/arguments), or if
|
|
|
|
/// the variable may have had operations performed on it (modified in a function
|
|
|
|
/// call). All attributes that exist in a set A must exist in all sets marked as
|
|
|
|
/// below set A.
|
2014-09-03 05:43:13 +08:00
|
|
|
template <typename T> class StratifiedSets {
|
|
|
|
public:
|
2016-06-08 05:41:18 +08:00
|
|
|
StratifiedSets() = default;
|
2016-06-09 01:56:35 +08:00
|
|
|
|
|
|
|
// TODO: Figure out how to make MSVC not call the copy ctor here, and delete
|
|
|
|
// it.
|
2016-06-09 01:27:14 +08:00
|
|
|
|
|
|
|
// Can't default these due to compile errors in MSVC2013
|
|
|
|
StratifiedSets(StratifiedSets &&Other) { *this = std::move(Other); }
|
|
|
|
StratifiedSets &operator=(StratifiedSets &&Other) {
|
|
|
|
Values = std::move(Other.Values);
|
|
|
|
Links = std::move(Other.Links);
|
|
|
|
return *this;
|
|
|
|
}
|
2014-09-03 05:43:13 +08:00
|
|
|
|
|
|
|
StratifiedSets(DenseMap<T, StratifiedInfo> Map,
|
|
|
|
std::vector<StratifiedLink> Links)
|
|
|
|
: Values(std::move(Map)), Links(std::move(Links)) {}
|
|
|
|
|
|
|
|
Optional<StratifiedInfo> find(const T &Elem) const {
|
|
|
|
auto Iter = Values.find(Elem);
|
2016-04-14 07:27:37 +08:00
|
|
|
if (Iter == Values.end())
|
|
|
|
return None;
|
2014-09-03 05:43:13 +08:00
|
|
|
return Iter->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
const StratifiedLink &getLink(StratifiedIndex Index) const {
|
|
|
|
assert(inbounds(Index));
|
|
|
|
return Links[Index];
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
DenseMap<T, StratifiedInfo> Values;
|
|
|
|
std::vector<StratifiedLink> Links;
|
|
|
|
|
|
|
|
bool inbounds(StratifiedIndex Idx) const { return Idx < Links.size(); }
|
|
|
|
};
|
|
|
|
|
2016-04-14 07:27:37 +08:00
|
|
|
/// Generic Builder class that produces StratifiedSets instances.
|
|
|
|
///
|
|
|
|
/// The goal of this builder is to efficiently produce correct StratifiedSets
|
|
|
|
/// instances. To this end, we use a few tricks:
|
|
|
|
/// > Set chains (A method for linking sets together)
|
|
|
|
/// > Set remaps (A method for marking a set as an alias [irony?] of another)
|
|
|
|
///
|
|
|
|
/// ==== Set chains ====
|
|
|
|
/// This builder has a notion of some value A being above, below, or with some
|
|
|
|
/// other value B:
|
|
|
|
/// > The `A above B` relationship implies that there is a reference edge
|
|
|
|
/// going from A to B. Namely, it notes that A can store anything in B's set.
|
|
|
|
/// > The `A below B` relationship is the opposite of `A above B`. It implies
|
|
|
|
/// that there's a dereference edge going from A to B.
|
|
|
|
/// > The `A with B` relationship states that there's an assignment edge going
|
|
|
|
/// from A to B, and that A and B should be treated as equals.
|
|
|
|
///
|
|
|
|
/// As an example, take the following code snippet:
|
|
|
|
///
|
|
|
|
/// %a = alloca i32, align 4
|
|
|
|
/// %ap = alloca i32*, align 8
|
|
|
|
/// %app = alloca i32**, align 8
|
|
|
|
/// store %a, %ap
|
|
|
|
/// store %ap, %app
|
2016-06-08 05:41:18 +08:00
|
|
|
/// %aw = getelementptr %ap, i32 0
|
2016-04-14 07:27:37 +08:00
|
|
|
///
|
2016-06-08 05:41:18 +08:00
|
|
|
/// Given this, the following relations exist:
|
2016-04-14 07:27:37 +08:00
|
|
|
/// - %a below %ap & %ap above %a
|
|
|
|
/// - %ap below %app & %app above %ap
|
|
|
|
/// - %aw with %ap & %ap with %aw
|
|
|
|
///
|
|
|
|
/// These relations produce the following sets:
|
|
|
|
/// [{%a}, {%ap, %aw}, {%app}]
|
|
|
|
///
|
2016-06-08 05:41:18 +08:00
|
|
|
/// ...Which state that the only MayAlias relationship in the above program is
|
2016-04-14 07:27:37 +08:00
|
|
|
/// between %ap and %aw.
|
|
|
|
///
|
2016-06-08 05:41:18 +08:00
|
|
|
/// Because LLVM allows arbitrary casts, code like the following needs to be
|
|
|
|
/// supported:
|
|
|
|
/// %ip = alloca i64, align 8
|
|
|
|
/// %ipp = alloca i64*, align 8
|
|
|
|
/// %i = bitcast i64** ipp to i64
|
|
|
|
/// store i64* %ip, i64** %ipp
|
|
|
|
/// store i64 %i, i64* %ip
|
2016-04-14 07:27:37 +08:00
|
|
|
///
|
2016-06-08 05:41:18 +08:00
|
|
|
/// Which, because %ipp ends up *both* above and below %ip, is fun.
|
2016-04-14 07:27:37 +08:00
|
|
|
///
|
2016-06-08 05:41:18 +08:00
|
|
|
/// This is solved by merging %i and %ipp into a single set (...which is the
|
|
|
|
/// only way to solve this, since their bit patterns are equivalent). Any sets
|
|
|
|
/// that ended up in between %i and %ipp at the time of merging (in this case,
|
|
|
|
/// the set containing %ip) also get conservatively merged into the set of %i
|
|
|
|
/// and %ipp. In short, the resulting StratifiedSet from the above code would be
|
|
|
|
/// {%ip, %ipp, %i}.
|
2016-04-14 07:27:37 +08:00
|
|
|
///
|
|
|
|
/// ==== Set remaps ====
|
|
|
|
/// More of an implementation detail than anything -- when merging sets, we need
|
|
|
|
/// to update the numbers of all of the elements mapped to those sets. Rather
|
|
|
|
/// than doing this at each merge, we note in the BuilderLink structure that a
|
|
|
|
/// remap has occurred, and use this information so we can defer renumbering set
|
|
|
|
/// elements until build time.
|
2014-09-03 05:43:13 +08:00
|
|
|
template <typename T> class StratifiedSetsBuilder {
|
2016-04-14 07:27:37 +08:00
|
|
|
/// \brief Represents a Stratified Set, with information about the Stratified
|
|
|
|
/// Set above it, the set below it, and whether the current set has been
|
|
|
|
/// remapped to another.
|
2014-09-03 05:43:13 +08:00
|
|
|
struct BuilderLink {
|
|
|
|
const StratifiedIndex Number;
|
|
|
|
|
|
|
|
BuilderLink(StratifiedIndex N) : Number(N) {
|
|
|
|
Remap = StratifiedLink::SetSentinel;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasAbove() const {
|
|
|
|
assert(!isRemapped());
|
|
|
|
return Link.hasAbove();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasBelow() const {
|
|
|
|
assert(!isRemapped());
|
|
|
|
return Link.hasBelow();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setBelow(StratifiedIndex I) {
|
|
|
|
assert(!isRemapped());
|
|
|
|
Link.Below = I;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setAbove(StratifiedIndex I) {
|
|
|
|
assert(!isRemapped());
|
|
|
|
Link.Above = I;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clearBelow() {
|
|
|
|
assert(!isRemapped());
|
|
|
|
Link.clearBelow();
|
|
|
|
}
|
|
|
|
|
|
|
|
void clearAbove() {
|
|
|
|
assert(!isRemapped());
|
|
|
|
Link.clearAbove();
|
|
|
|
}
|
|
|
|
|
|
|
|
StratifiedIndex getBelow() const {
|
|
|
|
assert(!isRemapped());
|
|
|
|
assert(hasBelow());
|
|
|
|
return Link.Below;
|
|
|
|
}
|
|
|
|
|
|
|
|
StratifiedIndex getAbove() const {
|
|
|
|
assert(!isRemapped());
|
|
|
|
assert(hasAbove());
|
|
|
|
return Link.Above;
|
|
|
|
}
|
|
|
|
|
2016-07-06 08:47:21 +08:00
|
|
|
AliasAttrs getAttrs() {
|
2014-09-03 05:43:13 +08:00
|
|
|
assert(!isRemapped());
|
|
|
|
return Link.Attrs;
|
|
|
|
}
|
|
|
|
|
2016-07-06 08:47:21 +08:00
|
|
|
void setAttrs(AliasAttrs Other) {
|
2014-09-03 05:43:13 +08:00
|
|
|
assert(!isRemapped());
|
2016-07-06 08:47:21 +08:00
|
|
|
Link.Attrs |= Other;
|
2014-09-03 05:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isRemapped() const { return Remap != StratifiedLink::SetSentinel; }
|
|
|
|
|
2016-04-14 07:27:37 +08:00
|
|
|
/// For initial remapping to another set
|
2014-09-03 05:43:13 +08:00
|
|
|
void remapTo(StratifiedIndex Other) {
|
|
|
|
assert(!isRemapped());
|
|
|
|
Remap = Other;
|
|
|
|
}
|
|
|
|
|
|
|
|
StratifiedIndex getRemapIndex() const {
|
|
|
|
assert(isRemapped());
|
|
|
|
return Remap;
|
|
|
|
}
|
|
|
|
|
2016-04-14 07:27:37 +08:00
|
|
|
/// Should only be called when we're already remapped.
|
2014-09-03 05:43:13 +08:00
|
|
|
void updateRemap(StratifiedIndex Other) {
|
|
|
|
assert(isRemapped());
|
|
|
|
Remap = Other;
|
|
|
|
}
|
|
|
|
|
2016-04-14 07:27:37 +08:00
|
|
|
/// Prefer the above functions to calling things directly on what's returned
|
|
|
|
/// from this -- they guard against unexpected calls when the current
|
|
|
|
/// BuilderLink is remapped.
|
2014-09-03 05:43:13 +08:00
|
|
|
const StratifiedLink &getLink() const { return Link; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
StratifiedLink Link;
|
|
|
|
StratifiedIndex Remap;
|
|
|
|
};
|
|
|
|
|
2016-04-14 07:27:37 +08:00
|
|
|
/// \brief This function performs all of the set unioning/value renumbering
|
|
|
|
/// that we've been putting off, and generates a vector<StratifiedLink> that
|
|
|
|
/// may be placed in a StratifiedSets instance.
|
2014-09-03 05:43:13 +08:00
|
|
|
void finalizeSets(std::vector<StratifiedLink> &StratLinks) {
|
|
|
|
DenseMap<StratifiedIndex, StratifiedIndex> Remaps;
|
|
|
|
for (auto &Link : Links) {
|
2016-04-14 07:27:37 +08:00
|
|
|
if (Link.isRemapped())
|
2014-09-03 05:43:13 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
StratifiedIndex Number = StratLinks.size();
|
2014-09-03 06:52:30 +08:00
|
|
|
Remaps.insert(std::make_pair(Link.Number, Number));
|
2014-09-03 05:43:13 +08:00
|
|
|
StratLinks.push_back(Link.getLink());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &Link : StratLinks) {
|
|
|
|
if (Link.hasAbove()) {
|
|
|
|
auto &Above = linksAt(Link.Above);
|
|
|
|
auto Iter = Remaps.find(Above.Number);
|
|
|
|
assert(Iter != Remaps.end());
|
|
|
|
Link.Above = Iter->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Link.hasBelow()) {
|
|
|
|
auto &Below = linksAt(Link.Below);
|
|
|
|
auto Iter = Remaps.find(Below.Number);
|
|
|
|
assert(Iter != Remaps.end());
|
|
|
|
Link.Below = Iter->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &Pair : Values) {
|
|
|
|
auto &Info = Pair.second;
|
|
|
|
auto &Link = linksAt(Info.Index);
|
|
|
|
auto Iter = Remaps.find(Link.Number);
|
|
|
|
assert(Iter != Remaps.end());
|
|
|
|
Info.Index = Iter->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-14 07:27:37 +08:00
|
|
|
/// \brief There's a guarantee in StratifiedLink where all bits set in a
|
|
|
|
/// Link.externals will be set in all Link.externals "below" it.
|
2014-09-03 05:43:13 +08:00
|
|
|
static void propagateAttrs(std::vector<StratifiedLink> &Links) {
|
|
|
|
const auto getHighestParentAbove = [&Links](StratifiedIndex Idx) {
|
|
|
|
const auto *Link = &Links[Idx];
|
|
|
|
while (Link->hasAbove()) {
|
|
|
|
Idx = Link->Above;
|
|
|
|
Link = &Links[Idx];
|
|
|
|
}
|
|
|
|
return Idx;
|
|
|
|
};
|
|
|
|
|
|
|
|
SmallSet<StratifiedIndex, 16> Visited;
|
|
|
|
for (unsigned I = 0, E = Links.size(); I < E; ++I) {
|
|
|
|
auto CurrentIndex = getHighestParentAbove(I);
|
2016-04-14 07:27:37 +08:00
|
|
|
if (!Visited.insert(CurrentIndex).second)
|
2014-09-03 05:43:13 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
while (Links[CurrentIndex].hasBelow()) {
|
|
|
|
auto &CurrentBits = Links[CurrentIndex].Attrs;
|
|
|
|
auto NextIndex = Links[CurrentIndex].Below;
|
|
|
|
auto &NextBits = Links[NextIndex].Attrs;
|
|
|
|
NextBits |= CurrentBits;
|
|
|
|
CurrentIndex = NextIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2016-04-14 07:27:37 +08:00
|
|
|
/// Builds a StratifiedSet from the information we've been given since either
|
|
|
|
/// construction or the prior build() call.
|
2014-09-03 05:43:13 +08:00
|
|
|
StratifiedSets<T> build() {
|
|
|
|
std::vector<StratifiedLink> StratLinks;
|
|
|
|
finalizeSets(StratLinks);
|
|
|
|
propagateAttrs(StratLinks);
|
|
|
|
Links.clear();
|
|
|
|
return StratifiedSets<T>(std::move(Values), std::move(StratLinks));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool has(const T &Elem) const { return get(Elem).hasValue(); }
|
|
|
|
|
|
|
|
bool add(const T &Main) {
|
|
|
|
if (get(Main).hasValue())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto NewIndex = getNewUnlinkedIndex();
|
|
|
|
return addAtMerging(Main, NewIndex);
|
|
|
|
}
|
|
|
|
|
2016-04-14 07:27:37 +08:00
|
|
|
/// \brief Restructures the stratified sets as necessary to make "ToAdd" in a
|
|
|
|
/// set above "Main". There are some cases where this is not possible (see
|
|
|
|
/// above), so we merge them such that ToAdd and Main are in the same set.
|
2014-09-03 05:43:13 +08:00
|
|
|
bool addAbove(const T &Main, const T &ToAdd) {
|
|
|
|
assert(has(Main));
|
|
|
|
auto Index = *indexOf(Main);
|
|
|
|
if (!linksAt(Index).hasAbove())
|
|
|
|
addLinkAbove(Index);
|
|
|
|
|
|
|
|
auto Above = linksAt(Index).getAbove();
|
|
|
|
return addAtMerging(ToAdd, Above);
|
|
|
|
}
|
|
|
|
|
2016-04-14 07:27:37 +08:00
|
|
|
/// \brief Restructures the stratified sets as necessary to make "ToAdd" in a
|
|
|
|
/// set below "Main". There are some cases where this is not possible (see
|
|
|
|
/// above), so we merge them such that ToAdd and Main are in the same set.
|
2014-09-03 05:43:13 +08:00
|
|
|
bool addBelow(const T &Main, const T &ToAdd) {
|
|
|
|
assert(has(Main));
|
|
|
|
auto Index = *indexOf(Main);
|
|
|
|
if (!linksAt(Index).hasBelow())
|
|
|
|
addLinkBelow(Index);
|
|
|
|
|
|
|
|
auto Below = linksAt(Index).getBelow();
|
|
|
|
return addAtMerging(ToAdd, Below);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool addWith(const T &Main, const T &ToAdd) {
|
|
|
|
assert(has(Main));
|
|
|
|
auto MainIndex = *indexOf(Main);
|
|
|
|
return addAtMerging(ToAdd, MainIndex);
|
|
|
|
}
|
|
|
|
|
2016-07-06 08:47:21 +08:00
|
|
|
void noteAttributes(const T &Main, AliasAttrs NewAttrs) {
|
2014-09-03 05:43:13 +08:00
|
|
|
assert(has(Main));
|
|
|
|
auto *Info = *get(Main);
|
|
|
|
auto &Link = linksAt(Info->Index);
|
|
|
|
Link.setAttrs(NewAttrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
DenseMap<T, StratifiedInfo> Values;
|
|
|
|
std::vector<BuilderLink> Links;
|
|
|
|
|
2016-04-14 07:27:37 +08:00
|
|
|
/// Adds the given element at the given index, merging sets if necessary.
|
2014-09-03 05:43:13 +08:00
|
|
|
bool addAtMerging(const T &ToAdd, StratifiedIndex Index) {
|
|
|
|
StratifiedInfo Info = {Index};
|
2014-09-03 06:52:30 +08:00
|
|
|
auto Pair = Values.insert(std::make_pair(ToAdd, Info));
|
2014-09-03 05:43:13 +08:00
|
|
|
if (Pair.second)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
auto &Iter = Pair.first;
|
|
|
|
auto &IterSet = linksAt(Iter->second.Index);
|
|
|
|
auto &ReqSet = linksAt(Index);
|
|
|
|
|
|
|
|
// Failed to add where we wanted to. Merge the sets.
|
|
|
|
if (&IterSet != &ReqSet)
|
|
|
|
merge(IterSet.Number, ReqSet.Number);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-14 07:27:37 +08:00
|
|
|
/// Gets the BuilderLink at the given index, taking set remapping into
|
|
|
|
/// account.
|
2014-09-03 05:43:13 +08:00
|
|
|
BuilderLink &linksAt(StratifiedIndex Index) {
|
|
|
|
auto *Start = &Links[Index];
|
|
|
|
if (!Start->isRemapped())
|
|
|
|
return *Start;
|
|
|
|
|
|
|
|
auto *Current = Start;
|
|
|
|
while (Current->isRemapped())
|
|
|
|
Current = &Links[Current->getRemapIndex()];
|
|
|
|
|
|
|
|
auto NewRemap = Current->Number;
|
|
|
|
|
|
|
|
// Run through everything that has yet to be updated, and update them to
|
|
|
|
// remap to NewRemap
|
|
|
|
Current = Start;
|
|
|
|
while (Current->isRemapped()) {
|
|
|
|
auto *Next = &Links[Current->getRemapIndex()];
|
|
|
|
Current->updateRemap(NewRemap);
|
|
|
|
Current = Next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *Current;
|
|
|
|
}
|
|
|
|
|
2016-04-14 07:27:37 +08:00
|
|
|
/// \brief Merges two sets into one another. Assumes that these sets are not
|
|
|
|
/// already one in the same.
|
2014-09-03 05:43:13 +08:00
|
|
|
void merge(StratifiedIndex Idx1, StratifiedIndex Idx2) {
|
|
|
|
assert(inbounds(Idx1) && inbounds(Idx2));
|
|
|
|
assert(&linksAt(Idx1) != &linksAt(Idx2) &&
|
|
|
|
"Merging a set into itself is not allowed");
|
|
|
|
|
|
|
|
// CASE 1: If the set at `Idx1` is above or below `Idx2`, we need to merge
|
|
|
|
// both the
|
|
|
|
// given sets, and all sets between them, into one.
|
|
|
|
if (tryMergeUpwards(Idx1, Idx2))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (tryMergeUpwards(Idx2, Idx1))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// CASE 2: The set at `Idx1` is not in the same chain as the set at `Idx2`.
|
|
|
|
// We therefore need to merge the two chains together.
|
|
|
|
mergeDirect(Idx1, Idx2);
|
|
|
|
}
|
|
|
|
|
2016-04-14 07:27:37 +08:00
|
|
|
/// \brief Merges two sets assuming that the set at `Idx1` is unreachable from
|
|
|
|
/// traversing above or below the set at `Idx2`.
|
2014-09-03 05:43:13 +08:00
|
|
|
void mergeDirect(StratifiedIndex Idx1, StratifiedIndex Idx2) {
|
|
|
|
assert(inbounds(Idx1) && inbounds(Idx2));
|
|
|
|
|
|
|
|
auto *LinksInto = &linksAt(Idx1);
|
|
|
|
auto *LinksFrom = &linksAt(Idx2);
|
|
|
|
// Merging everything above LinksInto then proceeding to merge everything
|
|
|
|
// below LinksInto becomes problematic, so we go as far "up" as possible!
|
|
|
|
while (LinksInto->hasAbove() && LinksFrom->hasAbove()) {
|
|
|
|
LinksInto = &linksAt(LinksInto->getAbove());
|
|
|
|
LinksFrom = &linksAt(LinksFrom->getAbove());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LinksFrom->hasAbove()) {
|
|
|
|
LinksInto->setAbove(LinksFrom->getAbove());
|
|
|
|
auto &NewAbove = linksAt(LinksInto->getAbove());
|
|
|
|
NewAbove.setBelow(LinksInto->Number);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merging strategy:
|
|
|
|
// > If neither has links below, stop.
|
|
|
|
// > If only `LinksInto` has links below, stop.
|
|
|
|
// > If only `LinksFrom` has links below, reset `LinksInto.Below` to
|
|
|
|
// match `LinksFrom.Below`
|
|
|
|
// > If both have links above, deal with those next.
|
|
|
|
while (LinksInto->hasBelow() && LinksFrom->hasBelow()) {
|
2016-07-06 08:47:21 +08:00
|
|
|
auto FromAttrs = LinksFrom->getAttrs();
|
2014-09-03 05:43:13 +08:00
|
|
|
LinksInto->setAttrs(FromAttrs);
|
|
|
|
|
|
|
|
// Remap needs to happen after getBelow(), but before
|
|
|
|
// assignment of LinksFrom
|
|
|
|
auto *NewLinksFrom = &linksAt(LinksFrom->getBelow());
|
|
|
|
LinksFrom->remapTo(LinksInto->Number);
|
|
|
|
LinksFrom = NewLinksFrom;
|
|
|
|
LinksInto = &linksAt(LinksInto->getBelow());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LinksFrom->hasBelow()) {
|
|
|
|
LinksInto->setBelow(LinksFrom->getBelow());
|
|
|
|
auto &NewBelow = linksAt(LinksInto->getBelow());
|
|
|
|
NewBelow.setAbove(LinksInto->Number);
|
|
|
|
}
|
|
|
|
|
2016-07-12 06:59:09 +08:00
|
|
|
LinksInto->setAttrs(LinksFrom->getAttrs());
|
2014-09-03 05:43:13 +08:00
|
|
|
LinksFrom->remapTo(LinksInto->Number);
|
|
|
|
}
|
|
|
|
|
2016-04-14 07:27:37 +08:00
|
|
|
/// Checks to see if lowerIndex is at a level lower than upperIndex. If so, it
|
|
|
|
/// will merge lowerIndex with upperIndex (and all of the sets between) and
|
|
|
|
/// return true. Otherwise, it will return false.
|
2014-09-03 05:43:13 +08:00
|
|
|
bool tryMergeUpwards(StratifiedIndex LowerIndex, StratifiedIndex UpperIndex) {
|
|
|
|
assert(inbounds(LowerIndex) && inbounds(UpperIndex));
|
|
|
|
auto *Lower = &linksAt(LowerIndex);
|
|
|
|
auto *Upper = &linksAt(UpperIndex);
|
|
|
|
if (Lower == Upper)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
SmallVector<BuilderLink *, 8> Found;
|
|
|
|
auto *Current = Lower;
|
|
|
|
auto Attrs = Current->getAttrs();
|
|
|
|
while (Current->hasAbove() && Current != Upper) {
|
|
|
|
Found.push_back(Current);
|
|
|
|
Attrs |= Current->getAttrs();
|
|
|
|
Current = &linksAt(Current->getAbove());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Current != Upper)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Upper->setAttrs(Attrs);
|
|
|
|
|
|
|
|
if (Lower->hasBelow()) {
|
|
|
|
auto NewBelowIndex = Lower->getBelow();
|
|
|
|
Upper->setBelow(NewBelowIndex);
|
|
|
|
auto &NewBelow = linksAt(NewBelowIndex);
|
|
|
|
NewBelow.setAbove(UpperIndex);
|
|
|
|
} else {
|
|
|
|
Upper->clearBelow();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &Ptr : Found)
|
|
|
|
Ptr->remapTo(Upper->Number);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<const StratifiedInfo *> get(const T &Val) const {
|
|
|
|
auto Result = Values.find(Val);
|
|
|
|
if (Result == Values.end())
|
2016-04-14 07:27:37 +08:00
|
|
|
return None;
|
2014-09-03 05:43:13 +08:00
|
|
|
return &Result->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<StratifiedInfo *> get(const T &Val) {
|
|
|
|
auto Result = Values.find(Val);
|
|
|
|
if (Result == Values.end())
|
2016-04-14 07:27:37 +08:00
|
|
|
return None;
|
2014-09-03 05:43:13 +08:00
|
|
|
return &Result->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<StratifiedIndex> indexOf(const T &Val) {
|
|
|
|
auto MaybeVal = get(Val);
|
|
|
|
if (!MaybeVal.hasValue())
|
2016-04-14 07:27:37 +08:00
|
|
|
return None;
|
2014-09-03 05:43:13 +08:00
|
|
|
auto *Info = *MaybeVal;
|
|
|
|
auto &Link = linksAt(Info->Index);
|
|
|
|
return Link.Number;
|
|
|
|
}
|
|
|
|
|
|
|
|
StratifiedIndex addLinkBelow(StratifiedIndex Set) {
|
|
|
|
auto At = addLinks();
|
|
|
|
Links[Set].setBelow(At);
|
|
|
|
Links[At].setAbove(Set);
|
|
|
|
return At;
|
|
|
|
}
|
|
|
|
|
|
|
|
StratifiedIndex addLinkAbove(StratifiedIndex Set) {
|
|
|
|
auto At = addLinks();
|
|
|
|
Links[At].setBelow(Set);
|
|
|
|
Links[Set].setAbove(At);
|
|
|
|
return At;
|
|
|
|
}
|
|
|
|
|
|
|
|
StratifiedIndex getNewUnlinkedIndex() { return addLinks(); }
|
|
|
|
|
|
|
|
StratifiedIndex addLinks() {
|
|
|
|
auto Link = Links.size();
|
|
|
|
Links.push_back(BuilderLink(Link));
|
|
|
|
return Link;
|
|
|
|
}
|
|
|
|
|
2014-09-03 06:36:58 +08:00
|
|
|
bool inbounds(StratifiedIndex N) const { return N < Links.size(); }
|
2014-09-03 05:43:13 +08:00
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2016-07-06 08:36:12 +08:00
|
|
|
}
|
2016-06-24 04:59:13 +08:00
|
|
|
#endif // LLVM_ADT_STRATIFIEDSETS_H
|