From 880d95d1618a7f2b79c81997bb595badb2a41cfd Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Thu, 22 Apr 2021 18:31:35 -0700 Subject: [PATCH] merged in more detailed alias analysis fixes for C from incubator --- src/mapleall/maple_me/include/alias_class.h | 3 +- src/mapleall/maple_me/src/alias_class.cpp | 187 +++++++++++++++---- src/mapleall/maple_me/src/me_alias_class.cpp | 6 +- src/mapleall/maple_me/src/orig_symbol.cpp | 1 + src/mapleall/maple_me/src/ssa_pre.cpp | 12 ++ 5 files changed, 167 insertions(+), 42 deletions(-) diff --git a/src/mapleall/maple_me/include/alias_class.h b/src/mapleall/maple_me/include/alias_class.h index 2f21e41e51..876edfd80b 100644 --- a/src/mapleall/maple_me/include/alias_class.h +++ b/src/mapleall/maple_me/include/alias_class.h @@ -156,6 +156,7 @@ class AliasClass : public AnalysisResult { void ApplyUnionForPointedTos(); void CollectRootIDOfNextLevelNodes(const OriginalSt &ost, std::set &rootIDOfNADSs); void UnionForNotAllDefsSeen(); + void UnionForNotAllDefsSeenCLang(); void ApplyUnionForStorageOverlaps(); void UnionForAggAndFields(); void CollectAliasGroups(std::map> &aliasGroups); @@ -217,8 +218,6 @@ class AliasClass : public AnalysisResult { void InsertMayDefUseClinitCheck(IntrinsiccallNode &stmt, BBId bbid); virtual BB *GetBB(BBId id) = 0; void ProcessIdsAliasWithRoot(const std::set &idsAliasWithRoot, std::vector &newGroups); - void UpdateNextLevelNodes(std::vector &nextLevelOsts, const AliasElem &aliasElem); - void UnionNodes(std::vector &nextLevelOsts); int GetOffset(const Klass &super, const Klass &base) const; MemPool &acMemPool; diff --git a/src/mapleall/maple_me/src/alias_class.cpp b/src/mapleall/maple_me/src/alias_class.cpp index cb19c1202e..b0985da21b 100644 --- a/src/mapleall/maple_me/src/alias_class.cpp +++ b/src/mapleall/maple_me/src/alias_class.cpp @@ -308,10 +308,7 @@ void AliasClass::SetPtrOpndsNextLevNADS(unsigned int start, unsigned int end, // based on ost1's extra level ost's, ensure corresponding ones exist for ost2 void AliasClass::CreateMirroringAliasElems(OriginalSt *ost1, OriginalSt *ost2) { - if (!ost1->IsSymbolOst() || !ost2->IsSymbolOst()) { - return; - } - if (ost1->GetMIRSymbol() == ost2->GetMIRSymbol()) { + if (ost1->IsSameSymOrPreg(ost2)) { return; } MapleVector *nextLevelNodes = GetAliasAnalysisTable()->GetNextLevelNodes(*ost1); @@ -344,9 +341,6 @@ void AliasClass::ApplyUnionForCopies(StmtNode &stmt) { AliasElem *lhsAe = FindOrCreateAliasElem(*ost); ASSERT_NOT_NULL(lhsAe); ApplyUnionForDassignCopy(*lhsAe, rhsAinfo.ae, *stmt.Opnd(0)); - if (stmt.GetOpCode() == OP_regassign) { - return; - } // at p = x, if the next level of either side exists, create other // side's next level if (mirModule.IsCModule() && rhsAinfo.ae && @@ -411,15 +405,13 @@ void AliasClass::ApplyUnionForCopies(StmtNode &stmt) { auto &intrnNode = static_cast(stmt); if (intrnNode.GetIntrinsic() == INTRN_JAVA_POLYMORPHIC_CALL) { SetPtrOpndsNextLevNADS(0, static_cast(intrnNode.NumOpnds()), intrnNode.GetNopnd(), false); - break; } - // fallthrough; + break; } - [[clang::fallthrough]]; - default: - for (size_t i = 0; i < stmt.NumOpnds(); ++i) { - CreateAliasElemsExpr(*stmt.Opnd(i)); - } + default: ; + } + for (size_t i = 0; i < stmt.NumOpnds(); ++i) { + CreateAliasElemsExpr(*stmt.Opnd(i)); } if (kOpcodeInfo.IsCallAssigned(stmt.GetOpCode())) { SetNotAllDefsSeenForMustDefs(stmt); @@ -478,39 +470,90 @@ void AliasClass::UnionAllPointedTos() { unionFind.Union(pointedTos[0]->GetClassID(), pointedTos[i]->GetClassID()); } } - -void AliasClass::UpdateNextLevelNodes(std::vector &nextLevelOsts, const AliasElem &aliasElem) { - for (size_t elemID : *(aliasElem.GetAssignSet())) { - for (OriginalSt *nextLevelNode : *(GetAliasAnalysisTable()->GetNextLevelNodes(id2Elem[elemID]->GetOriginalSt()))) { - nextLevelOsts.push_back(nextLevelNode); +// process the union among the pointed's of assignsets +void AliasClass::ApplyUnionForPointedTos() { + // first, process nextLevNotAllDefsSeen for alias elems with no assignment + for (AliasElem *aliaselem : id2Elem) { + if (aliaselem->GetAssignSet() == nullptr) { + if (aliaselem->IsNextLevNotAllDefsSeen()) { + MapleVector *nextLevelNodes = GetAliasAnalysisTable()->GetNextLevelNodes(aliaselem->GetOriginalSt()); + MapleVector::iterator ostit = nextLevelNodes->begin(); + for (; ostit != nextLevelNodes->end(); ostit++) { + AliasElem *indae = FindAliasElem(**ostit); + if (!indae->GetOriginalSt().IsFinal()) { + indae->SetNotAllDefsSeen(true); + } + } + } } } -} -void AliasClass::UnionNodes(std::vector &nextLevelOsts) { - for (size_t i = 0; i < nextLevelOsts.size(); ++i) { - OriginalSt *ost1 = nextLevelOsts[i]; - for (size_t j = i + 1; j < nextLevelOsts.size(); ++j) { - OriginalSt *ost2 = nextLevelOsts[j]; - if ((ost1->GetFieldID() == 0 || ost2->GetFieldID() == 0 || ost1->GetFieldID() == ost2->GetFieldID()) && - !(ost1->IsFinal() || ost2->IsFinal())) { - unionFind.Union(FindAliasElem(*ost1)->GetClassID(), FindAliasElem(*ost2)->GetClassID()); + MapleSet tempset(std::less(), acAlloc.Adapter()); + for (AliasElem *aliaselem : id2Elem) { + if (aliaselem->GetAssignSet() == nullptr) { + continue; + } + + // iterate through all the alias elems to check if any has indirectLev > 0 + // or if any has nextLevNotAllDefsSeen being true + bool hasNextLevNotAllDefsSeen = false; + for (MapleSet::iterator setit = aliaselem->GetAssignSet()->begin(); setit != aliaselem->GetAssignSet()->end(); + setit++) { + AliasElem *ae0 = id2Elem[*setit]; + if (ae0->GetOriginalSt().GetIndirectLev() > 0 || ae0->IsNotAllDefsSeen() || ae0->IsNextLevNotAllDefsSeen()) { + hasNextLevNotAllDefsSeen = true; break; } } - } -} - -// process the union among the pointed's of assignsets -void AliasClass::ApplyUnionForPointedTos() { - for (auto *aliasElem : id2Elem) { - if (aliasElem->GetAssignSet() == nullptr) { + if (hasNextLevNotAllDefsSeen) { + // make all pointedto's in this assignSet notAllDefsSeen + for (MapleSet::iterator setit = aliaselem->GetAssignSet()->begin(); setit != aliaselem->GetAssignSet()->end(); + setit++) { + AliasElem *ae0 = id2Elem[*setit]; + MapleVector *nextLevelNodes = GetAliasAnalysisTable()->GetNextLevelNodes(ae0->GetOriginalSt()); + MapleVector::iterator ostit = nextLevelNodes->begin(); + for (; ostit != nextLevelNodes->end(); ostit++) { + AliasElem *indae = FindAliasElem(**ostit); + if (!indae->GetOriginalSt().IsFinal()) { + indae->SetNotAllDefsSeen(true); + } + } + } continue; } + // apply union among the assignSet elements - std::vector nextLevelOsts; - UpdateNextLevelNodes(nextLevelOsts, *aliasElem); - UnionNodes(nextLevelOsts); + tempset = *(aliaselem->GetAssignSet()); + do { + // pick one alias element + MapleSet::iterator pickit = tempset.begin(); + if (pickit == tempset.end()) { + break; // done processing all elements in assignSet + } + AliasElem *ae1 = id2Elem[*pickit]; + tempset.erase(pickit); + for (MapleSet::iterator setit = tempset.begin(); setit != tempset.end(); setit++) { + AliasElem *ae2 = id2Elem[*setit]; + MapleVector *nextLevelNodes1 = GetAliasAnalysisTable()->GetNextLevelNodes(ae1->GetOriginalSt()); + MapleVector::iterator ost1it = nextLevelNodes1->begin(); + for (; ost1it != nextLevelNodes1->end(); ost1it++) { + MapleVector *nextLevelNodes2 = GetAliasAnalysisTable()->GetNextLevelNodes(ae2->GetOriginalSt()); + MapleVector::iterator ost2it = nextLevelNodes2->begin(); + for (; ost2it != nextLevelNodes2->end(); ost2it++) { + bool hasFieldid0 = (*ost1it)->GetFieldID() == 0 || (*ost2it)->GetFieldID() == 0; + if (((*ost1it)->GetFieldID() != (*ost2it)->GetFieldID()) && !hasFieldid0) { + continue; + } + if (((*ost1it)->IsFinal() || (*ost2it)->IsFinal())) { + continue; + } + AliasElem *indae1 = FindAliasElem(**ost1it); + AliasElem *indae2 = FindAliasElem(**ost2it); + unionFind.Union(indae1->GetClassID(), indae2->GetClassID()); + } + } + } + } while (true); } } @@ -557,6 +600,72 @@ void AliasClass::UnionForNotAllDefsSeen() { } } +void AliasClass::UnionForNotAllDefsSeenCLang() { + std::vector notAllDefsSeenAes; + for (AliasElem *ae : id2Elem) { + if (ae->IsNotAllDefsSeen()) { + notAllDefsSeenAes.push_back(ae); + } + } + + if (notAllDefsSeenAes.empty()) { + return; + } + + // notAllDefsSeenAe is the first notAllDefsSeen AliasElem. + // Union notAllDefsSeenAe with the other notAllDefsSeen aes. + AliasElem *notAllDefsSeenAe = notAllDefsSeenAes[0]; + notAllDefsSeenAes.erase(notAllDefsSeenAes.begin()); + for (AliasElem *ae : notAllDefsSeenAes) { + unionFind.Union(notAllDefsSeenAe->GetClassID(), ae->GetClassID()); + } + + uint rootIdOfNotAllDefsSeenAe = unionFind.Root(notAllDefsSeenAe->GetClassID()); + for (AliasElem *ae : id2Elem) { + if (unionFind.Root(ae->GetClassID()) == rootIdOfNotAllDefsSeenAe) { + ae->SetNotAllDefsSeen(true); + } + } + + // iterate through originalStTable; if the symbol (at level 0) is + // notAllDefsSeen, then set the same for all its level 1 members; then + // for each level 1 member of each symbol, if any is set notAllDefsSeen, + // set all members at that level to same; + for (uint32 i = 1; i < ssaTab.GetOriginalStTableSize(); i++) { + OriginalSt *ost = ssaTab.GetOriginalStTable().GetOriginalStFromID(OStIdx(i)); + if (!ost->IsSymbolOst()) { + continue; + } + AliasElem *ae = osym2Elem[ost->GetIndex()]; + if (ae == nullptr) { + continue; + } + OriginalSt *ostOfAe = &(ae->GetOriginalSt()); + bool hasNotAllDefsSeen = ae->IsNotAllDefsSeen() || ostOfAe->GetMIRSymbol()->GetStorageClass() == kScFormal; + MapleForwardList::iterator it; + if (!hasNotAllDefsSeen) { + // see if any at level 1 has notAllDefsSeen set + for (OriginalSt *nextLevelNode : *GetAliasAnalysisTable()->GetNextLevelNodes(*ost)) { + ae = osym2Elem[nextLevelNode->GetIndex()]; + if (ae != nullptr && ae->IsNotAllDefsSeen()) { + hasNotAllDefsSeen = true; + break; + } + } + } + if (hasNotAllDefsSeen) { + // set to true for all members at this level + for (OriginalSt *nextLevelNode : *GetAliasAnalysisTable()->GetNextLevelNodes(*ost)) { + AliasElem *ae = osym2Elem[nextLevelNode->GetIndex()]; + if (ae != nullptr) { + ae->SetNotAllDefsSeen(true); + unionFind.Union(notAllDefsSeenAe->GetClassID(), ae->GetClassID()); + } + } + } + } +} + void AliasClass::UnionForAggAndFields() { // key: index of MIRSymbol; value: id of alias element. std::map> symbol2AEs; @@ -565,7 +674,7 @@ void AliasClass::UnionForAggAndFields() { for (auto *aliasElem : id2Elem) { OriginalSt &ost = aliasElem->GetOriginalSt(); if (ost.GetIndirectLev() == 0 && ost.IsSymbolOst()) { - (void)symbol2AEs[ost.GetMIRSymbol()->GetStIndex()].insert(aliasElem->GetClassID()); + (void)symbol2AEs[ost.GetMIRSymbol()->GetStIdx().FullIdx()].insert(aliasElem->GetClassID()); } } diff --git a/src/mapleall/maple_me/src/me_alias_class.cpp b/src/mapleall/maple_me/src/me_alias_class.cpp index c8ff14b1c8..690094c831 100644 --- a/src/mapleall/maple_me/src/me_alias_class.cpp +++ b/src/mapleall/maple_me/src/me_alias_class.cpp @@ -56,7 +56,11 @@ void MeAliasClass::DoAliasAnalysis() { UnionAllPointedTos(); } else { ApplyUnionForPointedTos(); - UnionForNotAllDefsSeen(); + if (mirModule.IsJavaModule()) { + UnionForNotAllDefsSeen(); + } else { + UnionForNotAllDefsSeenCLang(); + } if (mirModule.IsCModule()) { ApplyUnionForStorageOverlaps(); } diff --git a/src/mapleall/maple_me/src/orig_symbol.cpp b/src/mapleall/maple_me/src/orig_symbol.cpp index a44a9338eb..fb3603d2eb 100644 --- a/src/mapleall/maple_me/src/orig_symbol.cpp +++ b/src/mapleall/maple_me/src/orig_symbol.cpp @@ -42,6 +42,7 @@ void OriginalSt::Dump() const { } } else if (IsPregOst()) { LogInfo::MapleLogger() << "%" << GetMIRPreg()->GetPregNo(); + LogInfo::MapleLogger() << "<" << static_cast(indirectLev) << ">"; } } diff --git a/src/mapleall/maple_me/src/ssa_pre.cpp b/src/mapleall/maple_me/src/ssa_pre.cpp index 3df98356af..daeb5e10a1 100644 --- a/src/mapleall/maple_me/src/ssa_pre.cpp +++ b/src/mapleall/maple_me/src/ssa_pre.cpp @@ -1242,6 +1242,18 @@ bool SSAPre::DefVarDominateOcc(const MeExpr *meExpr, const MeOccur &meOcc) const } return dom->Dominate(*defBB, *occBB); } + case kDefByChi: { + MeStmt *mestmt = regMeExpr->GetDefChi().GetBase(); + if (!mestmt) { + return true; // it's a original variable dominate everything + } + BB *defBB = mestmt->GetBB(); + if (occBB == defBB) { + return false; + } else { + return dom->Dominate(*defBB, *occBB); + } + } default: CHECK_FATAL(false, "NYI"); } -- Gitee