9 #ifndef LLVM_ADT_STRATIFIEDSETS_H 10 #define LLVM_ADT_STRATIFIEDSETS_H 20 #include <type_traits> 58 bool hasBelow()
const {
return Below != SetSentinel; }
59 bool hasAbove()
const {
return Above != SetSentinel; }
91 std::vector<StratifiedLink> Links)
92 : Values(
std::move(Map)), Links(
std::move(Links)) {}
95 auto Iter = Values.find(Elem);
96 if (Iter == Values.end())
108 std::vector<StratifiedLink> Links;
110 bool inbounds(StratifiedIndex Idx)
const {
return Idx < Links.size(); }
178 const StratifiedIndex
Number;
180 BuilderLink(StratifiedIndex
N) : Number(N) {
184 bool hasAbove()
const {
186 return Link.hasAbove();
189 bool hasBelow()
const {
191 return Link.hasBelow();
194 void setBelow(StratifiedIndex
I) {
199 void setAbove(StratifiedIndex I) {
214 StratifiedIndex getBelow()
const {
220 StratifiedIndex getAbove()
const {
239 void remapTo(StratifiedIndex Other) {
244 StratifiedIndex getRemapIndex()
const {
250 void updateRemap(StratifiedIndex Other) {
262 StratifiedIndex Remap;
268 void finalizeSets(std::vector<StratifiedLink> &StratLinks) {
270 for (
auto &
Link : Links) {
271 if (
Link.isRemapped())
274 StratifiedIndex
Number = StratLinks.size();
275 Remaps.
insert(std::make_pair(
Link.Number, Number));
276 StratLinks.push_back(
Link.getLink());
279 for (
auto &
Link : StratLinks) {
280 if (
Link.hasAbove()) {
281 auto &Above = linksAt(
Link.Above);
282 auto Iter = Remaps.
find(Above.Number);
284 Link.Above = Iter->second;
287 if (
Link.hasBelow()) {
288 auto &Below = linksAt(
Link.Below);
289 auto Iter = Remaps.
find(Below.Number);
291 Link.Below = Iter->second;
295 for (
auto &Pair : Values) {
296 auto &
Info = Pair.second;
298 auto Iter = Remaps.
find(
Link.Number);
300 Info.Index = Iter->second;
306 static void propagateAttrs(std::vector<StratifiedLink> &Links) {
307 const auto getHighestParentAbove = [&Links](StratifiedIndex Idx) {
308 const auto *
Link = &Links[Idx];
309 while (
Link->hasAbove()) {
317 for (
unsigned I = 0,
E = Links.size();
I <
E; ++
I) {
318 auto CurrentIndex = getHighestParentAbove(
I);
319 if (!Visited.
insert(CurrentIndex).second)
322 while (Links[CurrentIndex].hasBelow()) {
323 auto &CurrentBits = Links[CurrentIndex].Attrs;
324 auto NextIndex = Links[CurrentIndex].Below;
325 auto &NextBits = Links[NextIndex].Attrs;
326 NextBits |= CurrentBits;
327 CurrentIndex = NextIndex;
336 std::vector<StratifiedLink> StratLinks;
337 finalizeSets(StratLinks);
338 propagateAttrs(StratLinks);
343 bool has(
const T &Elem)
const {
return get(Elem).hasValue(); }
346 if (
get(Main).hasValue())
349 auto NewIndex = getNewUnlinkedIndex();
350 return addAtMerging(Main, NewIndex);
358 auto Index = *indexOf(Main);
359 if (!linksAt(
Index).hasAbove())
362 auto Above = linksAt(
Index).getAbove();
363 return addAtMerging(ToAdd, Above);
371 auto Index = *indexOf(Main);
372 if (!linksAt(
Index).hasBelow())
375 auto Below = linksAt(
Index).getBelow();
376 return addAtMerging(ToAdd, Below);
381 auto MainIndex = *indexOf(Main);
382 return addAtMerging(ToAdd, MainIndex);
387 auto *
Info = *
get(Main);
389 Link.setAttrs(NewAttrs);
394 std::vector<BuilderLink> Links;
397 bool addAtMerging(
const T &ToAdd, StratifiedIndex
Index) {
399 auto Pair = Values.
insert(std::make_pair(ToAdd, Info));
403 auto &Iter = Pair.first;
404 auto &IterSet = linksAt(Iter->second.Index);
405 auto &ReqSet = linksAt(Index);
408 if (&IterSet != &ReqSet)
409 merge(IterSet.Number, ReqSet.Number);
416 BuilderLink &linksAt(StratifiedIndex Index) {
417 auto *Start = &Links[
Index];
418 if (!Start->isRemapped())
421 auto *Current = Start;
422 while (Current->isRemapped())
423 Current = &Links[Current->getRemapIndex()];
425 auto NewRemap = Current->Number;
430 while (Current->isRemapped()) {
431 auto *Next = &Links[Current->getRemapIndex()];
432 Current->updateRemap(NewRemap);
441 void merge(StratifiedIndex Idx1, StratifiedIndex Idx2) {
442 assert(inbounds(Idx1) && inbounds(Idx2));
443 assert(&linksAt(Idx1) != &linksAt(Idx2) &&
444 "Merging a set into itself is not allowed");
449 if (tryMergeUpwards(Idx1, Idx2))
452 if (tryMergeUpwards(Idx2, Idx1))
457 mergeDirect(Idx1, Idx2);
462 void mergeDirect(StratifiedIndex Idx1, StratifiedIndex Idx2) {
463 assert(inbounds(Idx1) && inbounds(Idx2));
465 auto *LinksInto = &linksAt(Idx1);
466 auto *LinksFrom = &linksAt(Idx2);
469 while (LinksInto->hasAbove() && LinksFrom->hasAbove()) {
470 LinksInto = &linksAt(LinksInto->getAbove());
471 LinksFrom = &linksAt(LinksFrom->getAbove());
474 if (LinksFrom->hasAbove()) {
475 LinksInto->setAbove(LinksFrom->getAbove());
476 auto &NewAbove = linksAt(LinksInto->getAbove());
477 NewAbove.setBelow(LinksInto->Number);
486 while (LinksInto->hasBelow() && LinksFrom->hasBelow()) {
487 auto FromAttrs = LinksFrom->getAttrs();
488 LinksInto->setAttrs(FromAttrs);
492 auto *NewLinksFrom = &linksAt(LinksFrom->getBelow());
493 LinksFrom->remapTo(LinksInto->Number);
494 LinksFrom = NewLinksFrom;
495 LinksInto = &linksAt(LinksInto->getBelow());
498 if (LinksFrom->hasBelow()) {
499 LinksInto->setBelow(LinksFrom->getBelow());
500 auto &NewBelow = linksAt(LinksInto->getBelow());
501 NewBelow.setAbove(LinksInto->Number);
504 LinksInto->setAttrs(LinksFrom->getAttrs());
505 LinksFrom->remapTo(LinksInto->Number);
511 bool tryMergeUpwards(StratifiedIndex LowerIndex, StratifiedIndex UpperIndex) {
512 assert(inbounds(LowerIndex) && inbounds(UpperIndex));
513 auto *Lower = &linksAt(LowerIndex);
514 auto *
Upper = &linksAt(UpperIndex);
519 auto *Current = Lower;
520 auto Attrs = Current->getAttrs();
521 while (Current->hasAbove() && Current !=
Upper) {
523 Attrs |= Current->getAttrs();
524 Current = &linksAt(Current->getAbove());
527 if (Current !=
Upper)
532 if (Lower->hasBelow()) {
533 auto NewBelowIndex = Lower->getBelow();
534 Upper->setBelow(NewBelowIndex);
535 auto &NewBelow = linksAt(NewBelowIndex);
536 NewBelow.setAbove(UpperIndex);
541 for (
const auto &Ptr : Found)
542 Ptr->remapTo(
Upper->Number);
548 auto Result = Values.
find(Val);
549 if (Result == Values.
end())
551 return &Result->second;
555 auto Result = Values.
find(Val);
556 if (Result == Values.
end())
558 return &Result->second;
562 auto MaybeVal =
get(Val);
563 if (!MaybeVal.hasValue())
565 auto *
Info = *MaybeVal;
570 StratifiedIndex addLinkBelow(StratifiedIndex Set) {
571 auto At = addLinks();
572 Links[Set].setBelow(At);
573 Links[At].setAbove(Set);
577 StratifiedIndex addLinkAbove(StratifiedIndex Set) {
578 auto At = addLinks();
579 Links[At].setBelow(Set);
580 Links[Set].setAbove(At);
584 StratifiedIndex getNewUnlinkedIndex() {
return addLinks(); }
586 StratifiedIndex addLinks() {
587 auto Link = Links.size();
588 Links.push_back(BuilderLink(
Link));
592 bool inbounds(StratifiedIndex
N)
const {
return N < Links.size(); }
596 #endif // LLVM_ADT_STRATIFIEDSETS_H
This class represents lattice values for constants.
StratifiedSets< T > build()
Builds a StratifiedSet from the information we've been given since either construction or the prior b...
void push_back(const T &Elt)
Optional< std::vector< StOtherPiece > > Other
StratifiedIndex Below
The link for the set "below" current.
bool addAbove(const T &Main, const T &ToAdd)
Restructures the stratified sets as necessary to make "ToAdd" in a set above "Main".
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
unsigned StratifiedIndex
An index into Stratified Sets.
bool addWith(const T &Main, const T &ToAdd)
static const StratifiedIndex SetSentinel
This is a value used to signify "does not exist" where the StratifiedIndex type is used...
These are stratified sets, as described in "Fast algorithms for Dyck-CFL-reachability with applicatio...
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
Analysis containing CSE Info
iterator find(const_arg_type_t< KeyT > Val)
const StratifiedLink & getLink(StratifiedIndex Index) const
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
A "link" between two StratifiedSets.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
bool addBelow(const T &Main, const T &ToAdd)
Restructures the stratified sets as necessary to make "ToAdd" in a set below "Main".
AliasAttrs Attrs
Attributes for these StratifiedSets.
This file defines various utility types and functions useful to summary-based alias analysis...
StratifiedSets(DenseMap< T, StratifiedInfo > Map, std::vector< StratifiedLink > Links)
std::bitset< NumAliasAttrs > AliasAttrs
These are attributes that an alias analysis can use to mark certain special properties of a given poi...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Generic Builder class that produces StratifiedSets instances.
NOTE: ^ This can't be a short – bootstrapping clang has a case where ~1M sets exist.
StratifiedIndex Above
The index for the set "above" current.
void noteAttributes(const T &Main, AliasAttrs NewAttrs)
Optional< StratifiedInfo > find(const T &Elem) const
bool has(const T &Elem) const