From 6f99bce7c6eb3fa9b2d0d49d9c4df56fc1593f93 Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Tue, 13 Apr 2021 09:10:02 -0700 Subject: [PATCH 1/3] More mapleme fixes for problems found optimizing C torture tests --- src/mapleall/maple_me/include/orig_symbol.h | 10 ++++++++++ src/mapleall/maple_me/src/alias_class.cpp | 15 ++++++++++----- src/mapleall/maple_me/src/hdse.cpp | 9 +++++++++ src/mapleall/maple_me/src/irmap_build.cpp | 8 ++++++-- src/mapleall/maple_me/src/prop.cpp | 1 - 5 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/mapleall/maple_me/include/orig_symbol.h b/src/mapleall/maple_me/include/orig_symbol.h index e4670ba4b0..3c34b86a17 100644 --- a/src/mapleall/maple_me/include/orig_symbol.h +++ b/src/mapleall/maple_me/include/orig_symbol.h @@ -121,6 +121,16 @@ class OriginalSt { return ostType == kPregOst && symOrPreg.pregIdx < 0; } + bool IsSameSymOrPreg(OriginalSt *ost) const { + if (ostType != ost->ostType) { + return false; + } + if (IsSymbolOst()) { + return symOrPreg.mirSt == ost->symOrPreg.mirSt; + } + return symOrPreg.pregIdx == ost->symOrPreg.pregIdx; + } + int8 GetIndirectLev() const { return indirectLev; } diff --git a/src/mapleall/maple_me/src/alias_class.cpp b/src/mapleall/maple_me/src/alias_class.cpp index 0e0896110a..8c042609a2 100644 --- a/src/mapleall/maple_me/src/alias_class.cpp +++ b/src/mapleall/maple_me/src/alias_class.cpp @@ -591,7 +591,7 @@ AliasElem *AliasClass::FindOrCreateDummyNADSAe() { ASSERT_NOT_NULL(dummySym); dummySym->SetIsTmp(true); dummySym->SetIsDeleted(); - OriginalSt *dummyOst = ssaTab.GetOriginalStTable().CreateSymbolOriginalSt(*dummySym, 0, 0); + OriginalSt *dummyOst = ssaTab.GetOriginalStTable().FindOrCreateSymbolOriginalSt(*dummySym, 0, 0); ssaTab.GetVersionStTable().CreateZeroVersionSt(dummyOst); if (osym2Elem.size() == dummyOst->GetIndex()) { AliasElem *dummyAe = acMemPool.New(osym2Elem.size(), *dummyOst); @@ -984,7 +984,7 @@ void AliasClass::CollectMayDefForDassign(const StmtNode &stmt, std::setGetMIRSymbol() == ostOfLhsAe->GetMIRSymbol() && + if (ostOfAliasAe->IsSameSymOrPreg(ostOfLhsAe) && fldIDA != fldIDB && lhsSymType->GetKind() != kTypeUnion) { MIRType *aliasAeType = GlobalTables::GetTypeTable().GetTypeFromTyIdx(ostOfAliasAe->GetTyIdx()); @@ -1085,8 +1085,8 @@ void AliasClass::CollectMayDefForIassign(StmtNode &stmt, std::set & if (ostOfAliasAE.GetTyIdx() != pointedTyIdx && pointedTyIdx != 0) { continue; } - } else if (ostOfAliasAE.IsSymbolOst()) { - if (ostOfAliasAE.GetMIRSymbol() == ostOfLhsAe->GetMIRSymbol() && + } else { + if (ostOfAliasAE.IsSameSymOrPreg(ostOfLhsAe) && ostOfAliasAE.GetFieldID() != ostOfLhsAe->GetFieldID() && ostOfAliasAE.GetFieldID() != 0 && ostOfLhsAe->GetFieldID() != 0) { MIRType *aliasAeType = GlobalTables::GetTypeTable().GetTypeFromTyIdx(ostOfAliasAE.GetTyIdx()); @@ -1202,7 +1202,12 @@ void AliasClass::CollectMayDefForMustDefs(const StmtNode &stmt, std::setGetClassSet())) { bool isNotAllDefsSeen = false; for (AliasElem *notAllDefsSeenAe : notAllDefsSeenClassSetRoots) { - if (notAllDefsSeenAe->GetClassSet()->find(elemID) != notAllDefsSeenAe->GetClassSet()->end()) { + if (notAllDefsSeenAe->GetClassSet() == nullptr) { + if (elemID == notAllDefsSeenAe->GetClassID()) { + isNotAllDefsSeen = true; + break; // inserted already + } + } else if (notAllDefsSeenAe->GetClassSet()->find(elemID) != notAllDefsSeenAe->GetClassSet()->end()) { isNotAllDefsSeen = true; break; // inserted already } diff --git a/src/mapleall/maple_me/src/hdse.cpp b/src/mapleall/maple_me/src/hdse.cpp index 36efa6b951..561ea7be24 100644 --- a/src/mapleall/maple_me/src/hdse.cpp +++ b/src/mapleall/maple_me/src/hdse.cpp @@ -173,6 +173,15 @@ void HDSE::MarkRegDefByStmt(RegMeExpr ®MeExpr) { case kDefByPhi: MarkPhiRequired(regMeExpr.GetDefPhi()); break; + case kDefByChi: { + ASSERT(regMeExpr.GetOst()->GetIndirectLev() > 0, + "MarkRegDefByStmt: preg cannot be defined by chi"); + auto *defChi = ®MeExpr.GetDefChi(); + if (defChi != nullptr) { + MarkChiNodeRequired(*defChi); + } + break; + } case kDefByMustDef: { MustDefMeNode *mustDef = ®MeExpr.GetDefMustDef(); if (!mustDef->GetIsLive()) { diff --git a/src/mapleall/maple_me/src/irmap_build.cpp b/src/mapleall/maple_me/src/irmap_build.cpp index 71af6a79fc..12e42f0187 100644 --- a/src/mapleall/maple_me/src/irmap_build.cpp +++ b/src/mapleall/maple_me/src/irmap_build.cpp @@ -91,8 +91,12 @@ void IRMapBuild::BuildMustDefList(MeStmt &meStmt, TypeOfMustDefList &mustDefList MapleVector &mustDefMeList) { for (auto &mustDefNode : mustDefList) { VersionSt *vst = mustDefNode.GetResult(); - VarMeExpr *lhs = GetOrCreateVarFromVerSt(*vst); - ASSERT(lhs->GetMeOp() == kMeOpReg || lhs->GetMeOp() == kMeOpVar, "unexpected opcode"); + ScalarMeExpr *lhs = nullptr; + if (vst->GetOst()->IsSymbolOst()) { + lhs = GetOrCreateVarFromVerSt(*vst); + } else { + lhs = GetOrCreateRegFromVerSt(*vst); + } mustDefMeList.emplace_back(MustDefMeNode(lhs, &meStmt)); } } diff --git a/src/mapleall/maple_me/src/prop.cpp b/src/mapleall/maple_me/src/prop.cpp index d6e588de52..2b01e7de1a 100644 --- a/src/mapleall/maple_me/src/prop.cpp +++ b/src/mapleall/maple_me/src/prop.cpp @@ -561,7 +561,6 @@ void Prop::TraversalMeStmt(MeStmt &meStmt) { MapleVector *mustDefList = meStmt.GetMustDefList(); for (auto &node : utils::ToRef(mustDefList)) { MeExpr *meLhs = node.GetLHS(); - CHECK_FATAL(meLhs->GetMeOp() == kMeOpVar, "error: lhs is not var"); PropUpdateDef(utils::ToRef(static_cast(meLhs))); } } -- Gitee From 7c8c59bc6fb40401cf6e75e34e41a7f5aca8c269 Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Wed, 14 Apr 2021 08:27:32 -0700 Subject: [PATCH 2/3] Still more fixes for optimizing C code from gcc torture tests --- src/mapleall/maple_ir/include/mir_symbol.h | 2 +- src/mapleall/maple_ir/src/bin_mpl_import.cpp | 2 +- src/mapleall/maple_ir/src/mir_symbol.cpp | 6 +++--- src/mapleall/maple_me/src/alias_class.cpp | 8 +++++--- src/mapleall/maple_me/src/ssa_pre.cpp | 4 ++-- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/mapleall/maple_ir/include/mir_symbol.h b/src/mapleall/maple_ir/include/mir_symbol.h index b4e142494f..8c26527990 100644 --- a/src/mapleall/maple_ir/include/mir_symbol.h +++ b/src/mapleall/maple_ir/include/mir_symbol.h @@ -413,7 +413,7 @@ class MIRSymbol { bool IsGctibSym() const; bool IsPrimordialObject() const; bool IgnoreRC() const; - void Dump(bool isLocal, int32 indent, bool suppressinit = false) const; + void Dump(bool isLocal, int32 indent, bool suppressinit = false, const MIRSymbolTable *localsymtab = nullptr) const; void DumpAsLiteralVar() const; bool operator==(const MIRSymbol &msym) const { return nameStrIdx == msym.nameStrIdx; diff --git a/src/mapleall/maple_ir/src/bin_mpl_import.cpp b/src/mapleall/maple_ir/src/bin_mpl_import.cpp index 2ac33c8b17..706dd335d4 100644 --- a/src/mapleall/maple_ir/src/bin_mpl_import.cpp +++ b/src/mapleall/maple_ir/src/bin_mpl_import.cpp @@ -108,7 +108,7 @@ MIRConst *BinaryMplImport::ImportConst(MIRFunction *func) { MIRConstKind kind; MIRType *type = nullptr; uint32 fieldID; - MemPool *memPool = (func == nullptr) ? mod.GetMemPool() : func->GetCodeMempool(); + MemPool *memPool = mod.GetMemPool(); ImportConstBase(kind, type, fieldID); switch (tag) { diff --git a/src/mapleall/maple_ir/src/mir_symbol.cpp b/src/mapleall/maple_ir/src/mir_symbol.cpp index da9d66e6c3..35edfe7a4f 100644 --- a/src/mapleall/maple_ir/src/mir_symbol.cpp +++ b/src/mapleall/maple_ir/src/mir_symbol.cpp @@ -307,7 +307,7 @@ bool MIRSymbol::IgnoreRC() const { return strIdx == reflectClassNameIdx; } -void MIRSymbol::Dump(bool isLocal, int32 indent, bool suppressInit) const { +void MIRSymbol::Dump(bool isLocal, int32 indent, bool suppressInit, const MIRSymbolTable *localsymtab) const { if (sKind == kStVar || sKind == kStFunc) { if (srcPosition.FileNum() != 0 && srcPosition.LineNum() != 0 && srcPosition.LineNum() != lastPrintedLineNum) { LogInfo::MapleLogger() << "LOC " << srcPosition.FileNum() << " " << srcPosition.LineNum() << std::endl; @@ -360,7 +360,7 @@ void MIRSymbol::Dump(bool isLocal, int32 indent, bool suppressInit) const { } if (IsConst() && !suppressInit && !(IsLiteral() && GetStorageClass() == kScExtern)) { LogInfo::MapleLogger() << " = "; - GetKonst()->Dump(); + GetKonst()->Dump(localsymtab); } LogInfo::MapleLogger() << '\n'; } @@ -387,7 +387,7 @@ void MIRSymbolTable::Dump(bool isLocal, int32 indent, bool printDeleted) const { if (!printDeleted && symbol->IsDeleted()) { continue; } - symbol->Dump(isLocal, indent); + symbol->Dump(isLocal, indent, false/*suppressinit*/, this); } } diff --git a/src/mapleall/maple_me/src/alias_class.cpp b/src/mapleall/maple_me/src/alias_class.cpp index 8c042609a2..fdee58c29b 100644 --- a/src/mapleall/maple_me/src/alias_class.cpp +++ b/src/mapleall/maple_me/src/alias_class.cpp @@ -593,14 +593,16 @@ AliasElem *AliasClass::FindOrCreateDummyNADSAe() { dummySym->SetIsDeleted(); OriginalSt *dummyOst = ssaTab.GetOriginalStTable().FindOrCreateSymbolOriginalSt(*dummySym, 0, 0); ssaTab.GetVersionStTable().CreateZeroVersionSt(dummyOst); - if (osym2Elem.size() == dummyOst->GetIndex()) { - AliasElem *dummyAe = acMemPool.New(osym2Elem.size(), *dummyOst); + if (osym2Elem.size() > dummyOst->GetIndex() && osym2Elem[dummyOst->GetIndex()] != nullptr) { + return osym2Elem[dummyOst->GetIndex()]; + } else { + AliasElem *dummyAe = acMemPool.New(id2Elem.size(), *dummyOst); dummyAe->SetNotAllDefsSeen(true); id2Elem.push_back(dummyAe); osym2Elem.push_back(dummyAe); unionFind.NewMember(); + return dummyAe; } - return osym2Elem[dummyOst->GetIndex()]; } // This is applicable only for C language. For each ost that is a struct, diff --git a/src/mapleall/maple_me/src/ssa_pre.cpp b/src/mapleall/maple_me/src/ssa_pre.cpp index fc53d8751e..d40f5b2e30 100644 --- a/src/mapleall/maple_me/src/ssa_pre.cpp +++ b/src/mapleall/maple_me/src/ssa_pre.cpp @@ -67,8 +67,8 @@ ScalarMeExpr *SSAPre::CreateNewCurTemp(const MeExpr &meExpr) { } else { ty = realMIRType->GetPointedType(); } - CHECK_FATAL(ty->GetPrimType() == meExpr.GetPrimType() || - !(IsAddress(ty->GetPrimType()) && IsAddress(meExpr.GetPrimType())) || + CHECK_FATAL(GetRegPrimType(ty->GetPrimType()) == GetRegPrimType(meExpr.GetPrimType()) || + (IsAddress(ty->GetPrimType()) && IsAddress(meExpr.GetPrimType())) || (ty->GetPrimType() == PTY_ptr && meExpr.GetPrimType() == PTY_ref) || (ty->GetPrimType() == PTY_ref && meExpr.GetPrimType() == PTY_ptr), "inconsistent type"); -- Gitee From e78f1aba115311f844d580321a96967dd5918c10 Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Wed, 14 Apr 2021 19:21:30 -0700 Subject: [PATCH 3/3] relaxed the assertion in SSAPre::CreateNewCurTemp() --- src/mapleall/maple_ir/include/mir_type.h | 1 + src/mapleall/maple_ir/src/mir_type.cpp | 16 ++++++++++++++++ src/mapleall/maple_me/src/ssa_pre.cpp | 1 + 3 files changed, 18 insertions(+) diff --git a/src/mapleall/maple_ir/include/mir_type.h b/src/mapleall/maple_ir/include/mir_type.h index 5dced4dd0a..1d382e26a2 100644 --- a/src/mapleall/maple_ir/include/mir_type.h +++ b/src/mapleall/maple_ir/include/mir_type.h @@ -51,6 +51,7 @@ inline uint32 GetPrimTypeBitSize(PrimType primType) { #endif // MIR_FEATURE_FULL // return the same type with size increased to register size PrimType GetRegPrimType(PrimType primType); +PrimType GetReg64PrimType(PrimType primType); PrimType GetDynType(PrimType primType); PrimType GetNonDynType(PrimType primType); diff --git a/src/mapleall/maple_ir/src/mir_type.cpp b/src/mapleall/maple_ir/src/mir_type.cpp index 789ec1683c..a221f2da12 100644 --- a/src/mapleall/maple_ir/src/mir_type.cpp +++ b/src/mapleall/maple_ir/src/mir_type.cpp @@ -57,6 +57,22 @@ PrimType GetRegPrimType(PrimType primType) { } } +PrimType GetReg64PrimType(PrimType primType) { + switch (primType) { + case PTY_i8: + case PTY_i16: + case PTY_i32: + return PTY_i64; + case PTY_u1: + case PTY_u8: + case PTY_u16: + case PTY_u32: + return PTY_u64; + default: + return primType; + } +} + bool VerifyPrimType(PrimType primType1, PrimType primType2) { switch (primType1) { case PTY_u1: diff --git a/src/mapleall/maple_me/src/ssa_pre.cpp b/src/mapleall/maple_me/src/ssa_pre.cpp index d40f5b2e30..3df98356af 100644 --- a/src/mapleall/maple_me/src/ssa_pre.cpp +++ b/src/mapleall/maple_me/src/ssa_pre.cpp @@ -68,6 +68,7 @@ ScalarMeExpr *SSAPre::CreateNewCurTemp(const MeExpr &meExpr) { ty = realMIRType->GetPointedType(); } CHECK_FATAL(GetRegPrimType(ty->GetPrimType()) == GetRegPrimType(meExpr.GetPrimType()) || + GetReg64PrimType(ty->GetPrimType()) == GetReg64PrimType(meExpr.GetPrimType()) || (IsAddress(ty->GetPrimType()) && IsAddress(meExpr.GetPrimType())) || (ty->GetPrimType() == PTY_ptr && meExpr.GetPrimType() == PTY_ref) || (ty->GetPrimType() == PTY_ref && meExpr.GetPrimType() == PTY_ptr), -- Gitee