#ifndef LLVM_ADT_STRATIFIEDSETS_H
#define LLVM_ADT_STRATIFIEDSETS_H
#include "AliasAnalysisSummary.h"
#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>
#include <utility>
#include <vector>
namespace llvm {
namespace cflaa {
typedef unsigned StratifiedIndex;
struct StratifiedInfo {
StratifiedIndex Index;
};
struct StratifiedLink {
static const StratifiedIndex SetSentinel;
StratifiedIndex Above;
StratifiedIndex Below;
AliasAttrs Attrs;
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; }
};
template <typename T> class StratifiedSets {
public:
StratifiedSets() = default;
StratifiedSets(StratifiedSets &&) = default;
StratifiedSets &operator=(StratifiedSets &&) = default;
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);
if (Iter == Values.end())
return None;
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(); }
};
template <typename T> class StratifiedSetsBuilder {
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;
}
AliasAttrs getAttrs() {
assert(!isRemapped());
return Link.Attrs;
}
void setAttrs(AliasAttrs Other) {
assert(!isRemapped());
Link.Attrs |= Other;
}
bool isRemapped() const { return Remap != StratifiedLink::SetSentinel; }
void remapTo(StratifiedIndex Other) {
assert(!isRemapped());
Remap = Other;
}
StratifiedIndex getRemapIndex() const {
assert(isRemapped());
return Remap;
}
void updateRemap(StratifiedIndex Other) {
assert(isRemapped());
Remap = Other;
}
const StratifiedLink &getLink() const { return Link; }
private:
StratifiedLink Link;
StratifiedIndex Remap;
};
void finalizeSets(std::vector<StratifiedLink> &StratLinks) {
DenseMap<StratifiedIndex, StratifiedIndex> Remaps;
for (auto &Link : Links) {
if (Link.isRemapped())
continue;
StratifiedIndex Number = StratLinks.size();
Remaps.insert(std::make_pair(Link.Number, Number));
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;
}
}
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);
if (!Visited.insert(CurrentIndex).second)
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:
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).has_value(); }
bool add(const T &Main) {
if (get(Main))
return false;
auto NewIndex = getNewUnlinkedIndex();
return addAtMerging(Main, NewIndex);
}
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);
}
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);
}
void noteAttributes(const T &Main, AliasAttrs NewAttrs) {
assert(has(Main));
auto *Info = *get(Main);
auto &Link = linksAt(Info->Index);
Link.setAttrs(NewAttrs);
}
private:
DenseMap<T, StratifiedInfo> Values;
std::vector<BuilderLink> Links;
bool addAtMerging(const T &ToAdd, StratifiedIndex Index) {
StratifiedInfo Info = {Index};
auto Pair = Values.insert(std::make_pair(ToAdd, Info));
if (Pair.second)
return true;
auto &Iter = Pair.first;
auto &IterSet = linksAt(Iter->second.Index);
auto &ReqSet = linksAt(Index);
if (&IterSet != &ReqSet)
merge(IterSet.Number, ReqSet.Number);
return false;
}
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;
Current = Start;
while (Current->isRemapped()) {
auto *Next = &Links[Current->getRemapIndex()];
Current->updateRemap(NewRemap);
Current = Next;
}
return *Current;
}
void merge(StratifiedIndex Idx1, StratifiedIndex Idx2) {
assert(inbounds(Idx1) && inbounds(Idx2));
assert(&linksAt(Idx1) != &linksAt(Idx2) &&
"Merging a set into itself is not allowed");
if (tryMergeUpwards(Idx1, Idx2))
return;
if (tryMergeUpwards(Idx2, Idx1))
return;
mergeDirect(Idx1, Idx2);
}
void mergeDirect(StratifiedIndex Idx1, StratifiedIndex Idx2) {
assert(inbounds(Idx1) && inbounds(Idx2));
auto *LinksInto = &linksAt(Idx1);
auto *LinksFrom = &linksAt(Idx2);
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);
}
while (LinksInto->hasBelow() && LinksFrom->hasBelow()) {
auto FromAttrs = LinksFrom->getAttrs();
LinksInto->setAttrs(FromAttrs);
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);
}
LinksInto->setAttrs(LinksFrom->getAttrs());
LinksFrom->remapTo(LinksInto->Number);
}
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())
return None;
return &Result->second;
}
Optional<StratifiedInfo *> get(const T &Val) {
auto Result = Values.find(Val);
if (Result == Values.end())
return None;
return &Result->second;
}
Optional<StratifiedIndex> indexOf(const T &Val) {
auto MaybeVal = get(Val);
if (!MaybeVal)
return None;
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;
}
bool inbounds(StratifiedIndex N) const { return N < Links.size(); }
};
}
}
#endif