diff --git a/src/mapleall/maple_me/include/prop.h b/src/mapleall/maple_me/include/prop.h index 7dc128da6f1fdbd2a4e9d92df08e9149a5594b94..ab5e0a0795d1378e609a0ad8df37ef85650a4159 100644 --- a/src/mapleall/maple_me/include/prop.h +++ b/src/mapleall/maple_me/include/prop.h @@ -38,6 +38,7 @@ class Prop { Prop(IRMap&, Dominance&, MemPool&, uint32 bbvecsize, const PropConfig &config); virtual ~Prop() = default; + MeExpr *CheckTruncation(MeExpr *lhs, MeExpr *rhs) const; MeExpr &PropVar(VarMeExpr &varmeExpr, bool atParm, bool checkPhi) const; MeExpr &PropReg(RegMeExpr ®meExpr, bool atParm) const; MeExpr &PropIvar(IvarMeExpr &ivarMeExpr) const; diff --git a/src/mapleall/maple_me/src/alias_class.cpp b/src/mapleall/maple_me/src/alias_class.cpp index 5f381bb1846ec1f4c59426172e7928be4d51ec54..408b41504ea74f4e001cb556013d5d732205d6a3 100644 --- a/src/mapleall/maple_me/src/alias_class.cpp +++ b/src/mapleall/maple_me/src/alias_class.cpp @@ -561,7 +561,7 @@ void AliasClass::UnionForAggAndFields() { // collect alias elements with same MIRSymbol, and the ost is zero level. for (auto *aliasElem : id2Elem) { OriginalSt &ost = aliasElem->GetOriginalSt(); - if (ost.GetIndirectLev() == 0) { + if (ost.GetIndirectLev() == 0 && ost.IsSymbolOst()) { (void)symbol2AEs[ost.GetMIRSymbol()->GetStIndex()].insert(aliasElem->GetClassID()); } } @@ -1080,7 +1080,7 @@ void AliasClass::CollectMayDefForIassign(StmtNode &stmt, std::set & if (ostOfAliasAE.GetTyIdx() != pointedTyIdx && pointedTyIdx != 0) { continue; } - } else { + } else if (ostOfAliasAE.IsSymbolOst()) { if (ostOfAliasAE.GetMIRSymbol() == ostOfLhsAe->GetMIRSymbol() && ostOfAliasAE.GetFieldID() != ostOfLhsAe->GetFieldID() && ostOfAliasAE.GetFieldID() != 0 && ostOfLhsAe->GetFieldID() != 0) { diff --git a/src/mapleall/maple_me/src/irmap_build.cpp b/src/mapleall/maple_me/src/irmap_build.cpp index 39d63437f4b05fbd932a8f9f3328fc3c6b38667a..71af6a79fc424ff1bfbfb535370bc59bf30fff12 100644 --- a/src/mapleall/maple_me/src/irmap_build.cpp +++ b/src/mapleall/maple_me/src/irmap_build.cpp @@ -31,7 +31,7 @@ VarMeExpr *IRMapBuild::GetOrCreateVarFromVerSt(const VersionSt &vst) { return static_cast(meExpr); } OriginalSt *ost = vst.GetOst(); - ASSERT(ost->IsSymbolOst(), "GetOrCreateVarFromVerSt: wrong ost_type"); + ASSERT(ost->IsSymbolOst() || ost->GetIndirectLev() > 0, "GetOrCreateVarFromVerSt: wrong ost_type"); auto *varx = irMap->New(irMap->exprID++, ost, vindex, GlobalTables::GetTypeTable().GetTypeTable()[ost->GetTyIdx().GetIdx()]->GetPrimType()); ASSERT(!GlobalTables::GetTypeTable().GetTypeTable().empty(), "container check"); diff --git a/src/mapleall/maple_me/src/prop.cpp b/src/mapleall/maple_me/src/prop.cpp index d23a366b6462713562cadac4873774ce19107678..d6e588de52cd56ad2a27834c216cc88803e4bd1f 100644 --- a/src/mapleall/maple_me/src/prop.cpp +++ b/src/mapleall/maple_me/src/prop.cpp @@ -17,6 +17,8 @@ #include "dominance.h" #include "constantfold.h" +#define JAVALANG (mirModule.IsJavaModule()) + using namespace maple; const int kPropTreeLevel = 15; // tree height threshold to increase to @@ -229,6 +231,101 @@ bool Prop::Propagatable(const MeExpr &expr, const BB &fromBB, bool atParm) const return true; } +// if lhs is smaller than rhs, insert operation to simulate the truncation +// effect of rhs being stored into lhs; otherwise, just return rhs +MeExpr *Prop::CheckTruncation(MeExpr *lhs, MeExpr *rhs) const { + if (JAVALANG || !IsPrimitiveInteger(rhs->GetPrimType())) { + return rhs; + } + TyIdx lhsTyIdx(0); + MIRType *lhsTy = nullptr; + if (lhs->GetMeOp() == kMeOpVar) { + VarMeExpr *varx = static_cast(lhs); + lhsTyIdx = varx->GetOst()->GetTyIdx(); + lhsTy = GlobalTables::GetTypeTable().GetTypeFromTyIdx(lhsTyIdx); + } else if (lhs->GetMeOp() == kMeOpIvar) { + IvarMeExpr *ivarx = static_cast(lhs); + MIRPtrType *ptType = static_cast(GlobalTables::GetTypeTable().GetTypeFromTyIdx(ivarx->GetTyIdx())); + lhsTyIdx = ptType->GetPointedTyIdx(); + lhsTy = GlobalTables::GetTypeTable().GetTypeFromTyIdx(lhsTyIdx); + if (ivarx->GetFieldID() != 0) { + lhsTy = static_cast(lhsTy)->GetFieldType(ivarx->GetFieldID()); + } + } else { + return rhs; + } + if (lhsTy->GetKind() == kTypeBitField) { + MIRBitFieldType *bitfieldTy = static_cast(lhsTy); + if (GetPrimTypeBitSize(rhs->GetPrimType()) <= bitfieldTy->GetFieldSize()) { + return rhs; + } + // insert OP_zext or OP_sext + Opcode extOp = IsSignedInteger(lhsTy->GetPrimType()) ? OP_sext : OP_zext; + PrimType newPrimType = PTY_u32; + if (bitfieldTy->GetFieldSize() <= 32) { + if (IsSignedInteger(lhsTy->GetPrimType())) { + newPrimType = PTY_i32; + } + } else { + if (IsSignedInteger(lhsTy->GetPrimType())) { + newPrimType = PTY_i64; + } else { + newPrimType = PTY_u64; + } + } + OpMeExpr opmeexpr(-1, extOp, newPrimType, 1); + opmeexpr.SetBitsSize(bitfieldTy->GetFieldSize()); + opmeexpr.SetOpnd(0, rhs); + return irMap.HashMeExpr(opmeexpr); + } + if (IsPrimitiveInteger(lhsTy->GetPrimType()) && + lhsTy->GetPrimType() != PTY_ptr && lhsTy->GetPrimType() != PTY_ref && + GetPrimTypeSize(lhsTy->GetPrimType()) < rhs->GetPrimType()) { + if (GetPrimTypeSize(lhsTy->GetPrimType()) >= 4) { + return irMap.CreateMeExprTypeCvt(lhsTy->GetPrimType(), rhs->GetPrimType(), *rhs); + } else { + Opcode extOp = IsSignedInteger(lhsTy->GetPrimType()) ? OP_sext : OP_zext; + PrimType newPrimType = PTY_u32; + if (IsSignedInteger(lhsTy->GetPrimType())) { + newPrimType = PTY_i32; + } + OpMeExpr opmeexpr(-1, extOp, newPrimType, 1); + opmeexpr.SetBitsSize(GetPrimTypeSize(lhsTy->GetPrimType()) * 8); + opmeexpr.SetOpnd(0, rhs); + return irMap.HashMeExpr(opmeexpr); + } + } + // if lhs is function pointer and rhs is not, insert a retype + if (lhsTy->GetKind() == kTypePointer) { + MIRPtrType *lhsPtrType = static_cast(lhsTy); + if (lhsPtrType->GetPointedType()->GetKind() == kTypeFunction) { + bool needRetype = true; + MIRType *rhsTy = nullptr; + if (rhs->GetMeOp() == kMeOpVar) { + VarMeExpr *rhsvarx = static_cast(rhs); + rhsTy = GlobalTables::GetTypeTable().GetTypeFromTyIdx(rhsvarx->GetOst()->GetTyIdx()); + } else if (rhs->GetMeOp() == kMeOpIvar) { + IvarMeExpr *rhsivarx = static_cast(rhs); + MIRPtrType *rhsPtrType = static_cast(GlobalTables::GetTypeTable().GetTypeFromTyIdx(rhsivarx->GetTyIdx())); + rhsTy = rhsPtrType->GetPointedType(); + if (rhsivarx->GetFieldID() != 0) { + rhsTy = static_cast(rhsTy)->GetFieldType(rhsivarx->GetFieldID()); + } + } + if (rhsTy != nullptr && rhsTy == lhsPtrType) { + needRetype = false; + } + if (needRetype) { + OpMeExpr opmeexpr(-1, OP_retype, lhsPtrType->GetPrimType(), 1); + opmeexpr.SetTyIdx(lhsPtrType->GetTypeIndex()); + opmeexpr.SetOpnd(0, rhs); + return irMap.HashMeExpr(opmeexpr); + } + } + } + return rhs; +} + // return varMeExpr itself if no propagation opportunity MeExpr &Prop::PropVar(VarMeExpr &varMeExpr, bool atParm, bool checkPhi) const { const MIRSymbol *st = varMeExpr.GetOst()->GetMIRSymbol(); @@ -246,6 +343,7 @@ MeExpr &Prop::PropVar(VarMeExpr &varMeExpr, bool atParm, bool checkPhi) const { if (rhs->GetMeOp() == kMeOpIvar && rhs->GetPrimType() == PTY_ref) { defStmt->SetPropagated(true); } + rhs = CheckTruncation(&varMeExpr, rhs); return utils::ToRef(rhs); } else { return varMeExpr; diff --git a/src/mapleall/maple_me/src/ssa.cpp b/src/mapleall/maple_me/src/ssa.cpp index 77021ae62abc055055f43e6ef5ef0d6b2dc0c831..8d3410aae00d8f3603516d122ec4fc63da818144 100644 --- a/src/mapleall/maple_me/src/ssa.cpp +++ b/src/mapleall/maple_me/src/ssa.cpp @@ -70,7 +70,7 @@ void SSA::RenameDefs(StmtNode &stmt, BB &defBB) { newVersionSym->SetAssignNode(&stmt); theSSAPart->SetSSAVar(*newVersionSym); } - if (kOpcodeInfo.HasSSADef(opcode)) { + if (kOpcodeInfo.HasSSADef(opcode) && opcode != OP_regassign) { TypeOfMayDefList &mayDefList = theSSAPart->GetMayDefNodes(); for (auto it = mayDefList.begin(); it != mayDefList.end(); ++it) { MayDefNode &mayDef = *it; diff --git a/src/mapleall/mpl2mpl/src/constantfold.cpp b/src/mapleall/mpl2mpl/src/constantfold.cpp index 1c9b4bf295d4fc81f2a11cda8322a59c25d39908..149cefa0db73fdb8b50542bca326630dfd6a102c 100644 --- a/src/mapleall/mpl2mpl/src/constantfold.cpp +++ b/src/mapleall/mpl2mpl/src/constantfold.cpp @@ -1487,11 +1487,12 @@ std::pair ConstantFold::FoldExtractbits(ExtractbitsNode *node) std::pair ConstantFold::FoldIread(IreadNode *node) { CHECK_NULL_FATAL(node); - BaseNode *result = node; Opcode op = node->GetOpCode(); FieldID fieldID = node->GetFieldID(); std::pair p = DispatchFold(node->Opnd(0)); BaseNode *e = PairToExpr(node->Opnd(0)->GetPrimType(), p); + node->SetOpnd(e, 0); + BaseNode *result = node; if (op == OP_iaddrof && e->GetOpCode() == OP_addrof) { AddrofNode *addrofNode = static_cast(e); AddrofNode *newAddrof = addrofNode->CloneTree(mirModule->GetCurFuncCodeMPAllocator()); @@ -1504,18 +1505,14 @@ std::pair ConstantFold::FoldIread(IreadNode *node) { TyIdx typeId = msy->GetTyIdx(); CHECK_FATAL(GlobalTables::GetTypeTable().GetTypeTable().empty() == false, "container check"); MIRType *msyType = GlobalTables::GetTypeTable().GetTypeTable()[typeId]; - if (msyType->GetKind() == kTypeStruct || msyType->GetKind() == kTypeClass) { - FieldID newFieldId = fieldID + addrofNode->GetFieldID(); - MIRStructType *stty = static_cast(msyType); - // 0 for type itself - MIRType *fieldTy = newFieldId == 0 ? stty : stty->GetFieldType(newFieldId); - result = - mirModule->CurFuncCodeMemPool()->New(OP_dread, fieldTy->GetPrimType(), - addrofNode->GetStIdx(), newFieldId); - } - } else if (e != node->Opnd(0)) { - result = mirModule->CurFuncCodeMemPool()->New( - op, PrimType(node->GetPrimType()), node->GetTyIdx(), fieldID, static_cast(e)); + if (addrofNode->GetFieldID() != 0 && + (msyType->GetKind() == kTypeStruct || msyType->GetKind() == kTypeClass)) { + msyType = static_cast(msyType)->GetFieldType(addrofNode->GetFieldID()); + } + MIRPtrType *ptrType = static_cast(GlobalTables::GetTypeTable().GetTypeFromTyIdx(node->GetTyIdx())); + if (ptrType->GetPointedType() == msyType) { + result = mirModule->CurFuncCodeMemPool()->New(OP_dread, node->GetPrimType(), addrofNode->GetStIdx(), node->GetFieldID() + addrofNode->GetFieldID()); + } } return std::make_pair(result, 0); }