From 6c07253f5c1e420b5145bf018d47cd65940eb26f Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Thu, 1 Apr 2021 23:44:27 -0700 Subject: [PATCH] Set fieldid when creating MIRFloatConst and MIRDoubleConst --- src/mapleall/maple_ir/include/global_tables.h | 4 ++-- src/mapleall/maple_ir/src/bin_mpl_import.cpp | 4 ++-- src/mapleall/maple_ir/src/global_tables.cpp | 12 +++++++++-- src/mapleall/maple_ir/src/mir_parser.cpp | 4 ++-- src/mapleall/mpl2mpl/src/constantfold.cpp | 20 +++++++++---------- 5 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/mapleall/maple_ir/include/global_tables.h b/src/mapleall/maple_ir/include/global_tables.h index 6fdf9cd7f7..3563ed0952 100644 --- a/src/mapleall/maple_ir/include/global_tables.h +++ b/src/mapleall/maple_ir/include/global_tables.h @@ -503,8 +503,8 @@ class FPConstTable { FPConstTable &operator=(const FPConstTable &p) = delete; ~FPConstTable(); - MIRFloatConst *GetOrCreateFloatConst(float); // get the const from floatConstTable or create a new one - MIRDoubleConst *GetOrCreateDoubleConst(double); // get the const from doubleConstTable or create a new one + MIRFloatConst *GetOrCreateFloatConst(float fval, uint32 fieldID); // get the const from floatConstTable or create a new one + MIRDoubleConst *GetOrCreateDoubleConst(double fval, uint32 fieldID); // get the const from doubleConstTable or create a new one static std::unique_ptr Create() { auto p = std::unique_ptr(new FPConstTable()); diff --git a/src/mapleall/maple_ir/src/bin_mpl_import.cpp b/src/mapleall/maple_ir/src/bin_mpl_import.cpp index 00dd9679d4..2ac33c8b17 100644 --- a/src/mapleall/maple_ir/src/bin_mpl_import.cpp +++ b/src/mapleall/maple_ir/src/bin_mpl_import.cpp @@ -163,7 +163,7 @@ MIRConst *BinaryMplImport::ImportConst(MIRFunction *func) { } value; value.ivalue = ReadNum(); - return GlobalTables::GetFpConstTable().GetOrCreateFloatConst(value.fvalue); + return GlobalTables::GetFpConstTable().GetOrCreateFloatConst(value.fvalue, fieldID); } case kBinKindConstDouble: { union { @@ -172,7 +172,7 @@ MIRConst *BinaryMplImport::ImportConst(MIRFunction *func) { } value; value.ivalue = ReadNum(); - return GlobalTables::GetFpConstTable().GetOrCreateDoubleConst(value.dvalue); + return GlobalTables::GetFpConstTable().GetOrCreateDoubleConst(value.dvalue, fieldID); } case kBinKindConstAgg: { MIRAggConst *aggConst = mod.GetMemPool()->New(mod, *type, fieldID); diff --git a/src/mapleall/maple_ir/src/global_tables.cpp b/src/mapleall/maple_ir/src/global_tables.cpp index d8e20f082d..b00ba892bb 100644 --- a/src/mapleall/maple_ir/src/global_tables.cpp +++ b/src/mapleall/maple_ir/src/global_tables.cpp @@ -299,7 +299,11 @@ IntConstTable::~IntConstTable() { } } -MIRFloatConst *FPConstTable::GetOrCreateFloatConst(float floatVal) { +MIRFloatConst *FPConstTable::GetOrCreateFloatConst(float floatVal, uint32 fieldID) { + if (fieldID != 0) { + MIRFloatConst *fconst = new MIRFloatConst(floatVal, *GlobalTables::GetTypeTable().GetTypeFromTyIdx((TyIdx)PTY_f32), fieldID); + return fconst; + } if (std::isnan(floatVal)) { return nanFloatConst; } @@ -341,7 +345,11 @@ MIRFloatConst *FPConstTable::DoGetOrCreateFloatConstThreadSafe(float floatVal) { return floatConst; } -MIRDoubleConst *FPConstTable::GetOrCreateDoubleConst(double doubleVal) { +MIRDoubleConst *FPConstTable::GetOrCreateDoubleConst(double doubleVal, uint32 fieldID) { + if (fieldID != 0) { + MIRDoubleConst *dconst = new MIRDoubleConst(doubleVal, *GlobalTables::GetTypeTable().GetTypeFromTyIdx((TyIdx)PTY_f64), fieldID); + return dconst; + } if (std::isnan(doubleVal)) { return nanDoubleConst; } diff --git a/src/mapleall/maple_ir/src/mir_parser.cpp b/src/mapleall/maple_ir/src/mir_parser.cpp index 351cfa0d9a..a7c5b59801 100644 --- a/src/mapleall/maple_ir/src/mir_parser.cpp +++ b/src/mapleall/maple_ir/src/mir_parser.cpp @@ -2613,14 +2613,14 @@ bool MIRParser::ParseScalarValue(MIRConstPtr &stype, MIRType &type, uint32 field Error("constant value incompatible with single-precision float type at "); return false; } - MIRFloatConst *fConst = GlobalTables::GetFpConstTable().GetOrCreateFloatConst(lexer.GetTheFloatVal()); + MIRFloatConst *fConst = GlobalTables::GetFpConstTable().GetOrCreateFloatConst(lexer.GetTheFloatVal(), fieldID); stype = fConst; } else if (ptp == PTY_f64) { if (lexer.GetTokenKind() != TK_doubleconst && lexer.GetTokenKind() != TK_intconst) { Error("constant value incompatible with double-precision float type at "); return false; } - MIRDoubleConst *dconst = GlobalTables::GetFpConstTable().GetOrCreateDoubleConst(lexer.GetTheDoubleVal()); + MIRDoubleConst *dconst = GlobalTables::GetFpConstTable().GetOrCreateDoubleConst(lexer.GetTheDoubleVal(), fieldID); stype = dconst; } else { return false; diff --git a/src/mapleall/mpl2mpl/src/constantfold.cpp b/src/mapleall/mpl2mpl/src/constantfold.cpp index 42221c47c6..1c9b4bf295 100644 --- a/src/mapleall/mpl2mpl/src/constantfold.cpp +++ b/src/mapleall/mpl2mpl/src/constantfold.cpp @@ -743,9 +743,9 @@ ConstvalNode *ConstantFold::FoldFPConstBinary(Opcode opcode, PrimType resultType break; } if (resultType == PTY_f64) { - resultConst->SetConstVal(GlobalTables::GetFpConstTable().GetOrCreateDoubleConst(constValueDouble)); + resultConst->SetConstVal(GlobalTables::GetFpConstTable().GetOrCreateDoubleConst(constValueDouble, 0)); } else { - resultConst->SetConstVal(GlobalTables::GetFpConstTable().GetOrCreateFloatConst(constValueFloat)); + resultConst->SetConstVal(GlobalTables::GetFpConstTable().GetOrCreateFloatConst(constValueFloat, 0)); } return resultConst; } @@ -1029,9 +1029,9 @@ ConstvalNode *ConstantFold::FoldFPConstUnary(Opcode opcode, PrimType resultType, auto *resultConst = mirModule->CurFuncCodeMemPool()->New(); resultConst->SetPrimType(resultType); if (resultType == PTY_f32) { - resultConst->SetConstVal(GlobalTables::GetFpConstTable().GetOrCreateFloatConst(constValue)); + resultConst->SetConstVal(GlobalTables::GetFpConstTable().GetOrCreateFloatConst(constValue, 0)); } else { - resultConst->SetConstVal(GlobalTables::GetFpConstTable().GetOrCreateDoubleConst(constValue)); + resultConst->SetConstVal(GlobalTables::GetFpConstTable().GetOrCreateDoubleConst(constValue, 0)); } return resultConst; } @@ -1261,13 +1261,13 @@ MIRConst *ConstantFold::FoldRoundMIRConst(const MIRConst &cst, PrimType fromType int64 fromValue = constValue.GetValue(); float floatValue = round(static_cast(fromValue)); if (static_cast(floatValue) == fromValue) { - return GlobalTables::GetFpConstTable().GetOrCreateFloatConst(floatValue); + return GlobalTables::GetFpConstTable().GetOrCreateFloatConst(floatValue, 0); } } else { uint64 fromValue = static_cast(constValue.GetValue()); float floatValue = round(static_cast(fromValue)); if (static_cast(floatValue) == fromValue) { - return GlobalTables::GetFpConstTable().GetOrCreateFloatConst(floatValue); + return GlobalTables::GetFpConstTable().GetOrCreateFloatConst(floatValue, 0); } } } else if (toType == PTY_f64 && IsPrimitiveInteger(fromType)) { @@ -1276,13 +1276,13 @@ MIRConst *ConstantFold::FoldRoundMIRConst(const MIRConst &cst, PrimType fromType int64 fromValue = constValue.GetValue(); double doubleValue = round(static_cast(fromValue)); if (static_cast(doubleValue) == fromValue) { - return GlobalTables::GetFpConstTable().GetOrCreateDoubleConst(doubleValue); + return GlobalTables::GetFpConstTable().GetOrCreateDoubleConst(doubleValue, 0); } } else { uint64 fromValue = static_cast(constValue.GetValue()); double doubleValue = round(static_cast(fromValue)); if (static_cast(doubleValue) == fromValue) { - return GlobalTables::GetFpConstTable().GetOrCreateDoubleConst(doubleValue); + return GlobalTables::GetFpConstTable().GetOrCreateDoubleConst(doubleValue, 0); } } } @@ -1353,14 +1353,14 @@ MIRConst *ConstantFold::FoldTypeCvtMIRConst(const MIRConst &cst, PrimType fromTy const MIRDoubleConst *fromValue = safe_cast(cst); ASSERT_NOT_NULL(fromValue); float floutValue = static_cast(fromValue->GetValue()); - MIRFloatConst *toValue = GlobalTables::GetFpConstTable().GetOrCreateFloatConst(floutValue); + MIRFloatConst *toValue = GlobalTables::GetFpConstTable().GetOrCreateFloatConst(floutValue, 0); toConst = toValue; } else { ASSERT(GetPrimTypeBitSize(toType) == 64, "We suppot F32 and F64"); const MIRFloatConst *fromValue = safe_cast(cst); ASSERT_NOT_NULL(fromValue); double doubleValue = static_cast(fromValue->GetValue()); - MIRDoubleConst *toValue = GlobalTables::GetFpConstTable().GetOrCreateDoubleConst(doubleValue); + MIRDoubleConst *toValue = GlobalTables::GetFpConstTable().GetOrCreateDoubleConst(doubleValue, 0); toConst = toValue; } return toConst; -- Gitee