diff --git a/Makefile b/Makefile index 565853aadf4390fac8641ec26edc8e62d323a3ca..93e1c4f54164b3fd760ad15c06f48221edb9ea78 100644 --- a/Makefile +++ b/Makefile @@ -36,5 +36,5 @@ define build_gn mkdir -p ${INSTALL_DIR}; \ $(GN) gen ${INSTALL_DIR} --args='$(1)' --export-compile-commands; \ cd ${INSTALL_DIR}; \ - $(NINJA) -v $(2) | tee ${MAPLE_ROOT}/build/logs/$(shell date +"%Y-%m-%d-%H-%M-%S").log; + $(NINJA) -v $(2); endef diff --git a/build/build.sh b/build/build.sh index 1322c2d1b8759904b0d2e56ac73b1a2dea8746f8..6574dca3e1dfe1c2169e10e3e4bda74d9c6f4bb4 100644 --- a/build/build.sh +++ b/build/build.sh @@ -17,9 +17,14 @@ set -e source build/envsetup.sh make clean option=$@ -if [ "$option" = "DEBUG" ]; then - make BUILD_TYPE=DEBUG +logfile_name=$(date +"%Y-%m-%d-%H-%M-%S") +logfile_path=${MAPLE_ROOT}/build/logs +date_str=$(date "+%Y-%m-%d %H:%M:%S") +echo "${date_str} INFO special log start" | tee ${logfile_path}/${logfile_name}.log +if [ "x${option}" = "xDEBUG" ]; then + make BUILD_TYPE=DEBUG | tee -a ${logfile_path}/${logfile_name}.log else - make + make | tee -a ${logfile_path}/${logfile_name}.log fi - +date_str=$(date "+%Y-%m-%d %H:%M:%S") +echo "${date_str} INFO special log end" | tee -a ${logfile_path}/${logfile_name}.log diff --git a/src/bin/jbc2mpl b/src/bin/jbc2mpl index d1697c9aa6a858786ec095967180d2775efa3145..533b0e70294bee769f0a157593c237a1066d25fd 100755 Binary files a/src/bin/jbc2mpl and b/src/bin/jbc2mpl differ diff --git a/src/bin/maple b/src/bin/maple index 86eb9e91fdea0e4af51895e60864a0ba994b70b8..7b1df1d335758d9eca85649f2d95cf52f6e53235 100755 Binary files a/src/bin/maple and b/src/bin/maple differ diff --git a/src/deplibs/libmplphase.a b/src/deplibs/libmplphase.a index 96518c8332a69e435583ca579f84fad3c9255382..cba8da88dd9ef3d7a38d2e78429b8d180d53155d 100644 Binary files a/src/deplibs/libmplphase.a and b/src/deplibs/libmplphase.a differ diff --git a/src/maple_ir/include/mir_module.h b/src/maple_ir/include/mir_module.h index 24ed8f9f0717408c08a00324caf07ece02f20fa2..faedf4e8cc50a2ac260eeeb5171818e491843650 100644 --- a/src/maple_ir/include/mir_module.h +++ b/src/maple_ir/include/mir_module.h @@ -288,6 +288,10 @@ class MIRModule { void SetPuIdxFieldSet(PUIdx puIdx, MapleSet *fieldIDSet) { puIdxFieldInitializedMap[puIdx] = fieldIDSet; } + const auto &GetRealCaller() const { + return realCaller; + } + auto &GetRealCaller() { return realCaller; } @@ -335,6 +339,12 @@ class MIRModule { bool IsInIPA() const { return inIPA; } + bool IsWithMe() const { + return withMe; + } + void SetWithMe(bool isWithMe) { + withMe = isWithMe; + } void SetInIPA(bool isInIPA) { inIPA = isInIPA; } @@ -496,6 +506,7 @@ class MIRModule { // for cg in mplt BinaryMplt *binMplt = nullptr; bool inIPA = false; + bool withMe = true; MIRInfoVector fileInfo; // store info provided under fileInfo keyword MapleVector fileInfoIsString; // tells if an entry has string value MIRDataVector fileData; diff --git a/src/maple_ir/include/mir_nodes.h b/src/maple_ir/include/mir_nodes.h index bf343d09c816d2b1abd2cc6f053bbeb61decc3fd..958b34fc3439a3e3e48249ddd2ac3972fb5dbfdb 100644 --- a/src/maple_ir/include/mir_nodes.h +++ b/src/maple_ir/include/mir_nodes.h @@ -2164,9 +2164,9 @@ class BlockNode : public StmtNode { void InsertFirst(StmtNode *stmt); // Insert stmt as the first void InsertLast(StmtNode *stmt); // Insert stmt as the last void ReplaceStmtWithBlock(StmtNode &stmtNode, BlockNode &blk); - void ReplaceStmt1WithStmt2(StmtNode *stmtNode1, StmtNode *stmtNode2); + void ReplaceStmt1WithStmt2(const StmtNode *stmtNode1, StmtNode *stmtNode2); void RemoveStmt(StmtNode *stmtNode2); - void InsertBefore(StmtNode *stmtNode1, StmtNode *stmtNode2); // Insert ss2 before ss1 in current block. + void InsertBefore(const StmtNode *stmtNode1, StmtNode *stmtNode2); // Insert ss2 before ss1 in current block. void InsertAfter(StmtNode *stmtNode1, StmtNode *stmtNode2); // Insert ss2 after ss1 in current block. // insert all the stmts in inblock to the current block after stmt1 void InsertBlockAfter(BlockNode &inblock, StmtNode *stmt1); diff --git a/src/maple_ir/include/mir_type.h b/src/maple_ir/include/mir_type.h index 04c0de96166bd8ee4934360dcfd0894a04529867..e6f57ce5f1f2a8fff9a478e7dce154cdbe2b8c39 100644 --- a/src/maple_ir/include/mir_type.h +++ b/src/maple_ir/include/mir_type.h @@ -805,7 +805,7 @@ class MIRStructType : public MIRType { isUsed = flag; } - GStrIdx GetFieldGStrIdx(FieldID id) { + GStrIdx GetFieldGStrIdx(FieldID id) const { const FieldPair &fieldPair = TraverseToField(id); return fieldPair.first; } diff --git a/src/maple_ir/src/mir_function.cpp b/src/maple_ir/src/mir_function.cpp index 96a108940b19d27eb438d3ded3189d74c2591651..b2abce61eeba6f611c4723aedc12bdbc5f31e65d 100644 --- a/src/maple_ir/src/mir_function.cpp +++ b/src/maple_ir/src/mir_function.cpp @@ -36,6 +36,9 @@ enum FuncProp : uint32_t { namespace maple { void MIRFunction::Init() { symTab = module->GetMemPool()->New(module->GetMPAllocator()); + if (!module->IsWithMe()) { + pregTab = module->GetMemPool()->New(module, &module->GetMPAllocator()); + } return; } diff --git a/src/maple_ir/src/mir_nodes.cpp b/src/maple_ir/src/mir_nodes.cpp index 0dc21f67fcde91373c3707f566609fd585223b8d..5ea835ce80c978816401993c90e16577fa7c6687 100644 --- a/src/maple_ir/src/mir_nodes.cpp +++ b/src/maple_ir/src/mir_nodes.cpp @@ -160,7 +160,7 @@ void BlockNode::ReplaceStmtWithBlock(StmtNode &stmtNode, BlockNode &blk) { stmtNode.SetNext(blk.GetLast()->GetNext()); } -void BlockNode::ReplaceStmt1WithStmt2(StmtNode *stmtNode1, StmtNode *stmtNode2) { +void BlockNode::ReplaceStmt1WithStmt2(const StmtNode *stmtNode1, StmtNode *stmtNode2) { if (stmtNode2 == stmtNode1) { // do nothing } else if (stmtNode2 == nullptr) { @@ -180,7 +180,7 @@ void BlockNode::RemoveStmt(StmtNode *stmtNode1) { } /// Insert stmtNode2 before stmtNode1 in current block. -void BlockNode::InsertBefore(StmtNode *stmtNode1, StmtNode *stmtNode2) { +void BlockNode::InsertBefore(const StmtNode *stmtNode1, StmtNode *stmtNode2) { stmtNodeList.insert(stmtNode1, stmtNode2); } diff --git a/src/mpl2mpl/include/coderelayout.h b/src/mpl2mpl/include/coderelayout.h index a4aecf9fc746bf37dbe5d3f27cf784730a50e5a3..5549359addd166b726bf8d1fa916d36a3b1f0708 100644 --- a/src/mpl2mpl/include/coderelayout.h +++ b/src/mpl2mpl/include/coderelayout.h @@ -1,5 +1,5 @@ /* - * Copyright (c) [2019] Huawei Technologies Co.,Ltd.All rights reserved. + * Copyright (c) [2019-2020] Huawei Technologies Co.,Ltd.All rights reserved. * * OpenArkCompiler is licensed under the Mulan PSL v1. * You can use this software according to the terms and conditions of the Mulan PSL v1. @@ -18,10 +18,11 @@ #include "phase_impl.h" #include "module_phase.h" #include "file_layout.h" + namespace maple { class CodeReLayout : public FuncOptimizeImpl { public: - explicit CodeReLayout(MIRModule *mod, KlassHierarchy *kh, bool dump); + CodeReLayout(MIRModule *mod, KlassHierarchy *kh, bool dump); ~CodeReLayout() = default; FuncOptimizeImpl *Clone() override { @@ -32,24 +33,24 @@ class CodeReLayout : public FuncOptimizeImpl { void Finish() override; private: - const std::string kExeFuncTag = "executedFuncStart"; - const std::string kProfileStartTag = "#profile_start"; - const std::string kProfileSummaryTag = "#profile_summary"; + const std::string exeFuncTag = "executedFuncStart"; + const std::string profileStartTag = "#profile_start"; + const std::string profileSummaryTag = "#profile_summary"; std::unordered_map str2SymMap; uint32 layoutCount[static_cast(LayoutType::kLayoutTypeCount)] = {}; - std::string StaticFieldFilename(const std::string &mplFile); + std::string StaticFieldFilename(const std::string &mplFile) const; void GenLayoutSym(); void AddStaticFieldRecord(); CallNode *CreateRecordFieldStaticCall(BaseNode *node, const std::string &name); - void FindDreadRecur(StmtNode *stmt, BaseNode *node); - void InsertProfileBeforeDread(StmtNode *stmt, BaseNode *opnd); + void FindDreadRecur(const StmtNode *stmt, BaseNode *node); + void InsertProfileBeforeDread(const StmtNode *stmt, BaseNode *opnd); MIRSymbol *GetorCreateStaticFieldSym(const std::string &fieldName); MIRSymbol *GenStrSym(const std::string &str); }; class DoCodeReLayout : public ModulePhase { public: - DoCodeReLayout(ModulePhaseID id) : ModulePhase(id) {} + explicit DoCodeReLayout(ModulePhaseID id) : ModulePhase(id) {} ~DoCodeReLayout() = default; diff --git a/src/mpl2mpl/include/constantfold.h b/src/mpl2mpl/include/constantfold.h index e2cf04cc42996ded3c5060336cc1100806b10505..bac3d4cc9b022562930dde02ebd9d383f0d9bacc 100644 --- a/src/mpl2mpl/include/constantfold.h +++ b/src/mpl2mpl/include/constantfold.h @@ -45,18 +45,18 @@ class ConstantFold : public FuncOptimizeImpl { void ProcessFunc(MIRFunction *func); virtual ~ConstantFold() = default; - MIRConst *FoldFloorMIRConst(MIRConst*, PrimType, PrimType); - MIRConst *FoldRoundMIRConst(MIRConst*, PrimType, PrimType); - MIRConst *FoldTypeCvtMIRConst(MIRConst*, PrimType, PrimType); - MIRConst *FoldSignExtendMIRConst(Opcode, PrimType, uint8, MIRConst*); - MIRConst *FoldConstComparisonMIRConst(Opcode, PrimType, PrimType, MIRConst&, MIRConst&); + MIRConst *FoldFloorMIRConst(const MIRConst*, PrimType, PrimType) const; + MIRConst *FoldRoundMIRConst(const MIRConst*, PrimType, PrimType) const; + MIRConst *FoldTypeCvtMIRConst(const MIRConst*, PrimType, PrimType) const; + MIRConst *FoldSignExtendMIRConst(Opcode, PrimType, uint8, const MIRConst*) const; + MIRConst *FoldConstComparisonMIRConst(Opcode, PrimType, PrimType, const MIRConst&, const MIRConst&); private: MIRModule *mirModule; StmtNode *SimplifyBinary(BinaryStmtNode *node); StmtNode *SimplifyBlock(BlockNode *node); StmtNode *SimplifyCondGoto(CondGotoNode *node); - StmtNode *SimplifyCondGotoSelect(CondGotoNode *node); + StmtNode *SimplifyCondGotoSelect(CondGotoNode *node) const; StmtNode *SimplifyDassign(DassignNode *node); StmtNode *SimplifyIassign(IassignNode *node); StmtNode *SimplifyNary(NaryStmtNode *node); @@ -71,43 +71,47 @@ class ConstantFold : public FuncOptimizeImpl { std::pair FoldCompare(CompareNode *node); std::pair FoldDepositbits(DepositbitsNode *node); std::pair FoldExtractbits(ExtractbitsNode *node); - ConstvalNode *FoldSignExtend(Opcode opcode, PrimType resultType, uint8 size, ConstvalNode *cst); + ConstvalNode *FoldSignExtend(Opcode opcode, PrimType resultType, uint8 size, const ConstvalNode *cst) const; std::pair FoldIread(IreadNode *node); - std::pair FoldSizeoftype(SizeoftypeNode *node); + std::pair FoldSizeoftype(SizeoftypeNode *node) const; std::pair FoldRetype(RetypeNode *node); std::pair FoldGcmallocjarray(JarrayMallocNode *node); std::pair FoldUnary(UnaryNode *node); std::pair FoldTernary(TernaryNode *node); std::pair FoldTypeCvt(TypeCvtNode *node); - ConstvalNode *FoldCeil(ConstvalNode *cst, PrimType fromType, PrimType toType); - ConstvalNode *FoldFloor(ConstvalNode *cst, PrimType fromType, PrimType toType); - ConstvalNode *FoldRound(ConstvalNode *cst, PrimType fromType, PrimType toType); - ConstvalNode *FoldTrunk(ConstvalNode *cst, PrimType fromType, PrimType toType); - ConstvalNode *FoldTypeCvt(ConstvalNode *cst, PrimType fromType, PrimType toType); - ConstvalNode *FoldConstComparison(Opcode opcode, PrimType resultType, PrimType opndType, ConstvalNode &const0, - ConstvalNode &const1); - ConstvalNode *FoldConstBinary(Opcode opcode, PrimType resultType, ConstvalNode &const0, ConstvalNode &const1); - ConstvalNode *FoldIntConstComparison(Opcode opcode, PrimType resultType, ConstvalNode &const0, ConstvalNode &const1); - MIRIntConst *FoldIntConstComparisonMIRConst(Opcode, PrimType, const MIRIntConst&, const MIRIntConst&); - ConstvalNode *FoldIntConstBinary(Opcode opcode, PrimType resultType, ConstvalNode &const0, ConstvalNode &const1); - ConstvalNode *FoldFPConstComparison(Opcode opcode, PrimType resultType, PrimType opndType, ConstvalNode &const0, - ConstvalNode &const1); - MIRIntConst *FoldFPConstComparisonMIRConst(Opcode opcode, PrimType resultType, PrimType opndType, MIRConst &const0, - MIRConst &const1); - ConstvalNode *FoldFPConstBinary(Opcode opcode, PrimType resultType, ConstvalNode &const0, ConstvalNode &const1); - ConstvalNode *FoldConstUnary(Opcode opcode, PrimType resultType, ConstvalNode *c); - ConstvalNode *FoldIntConstUnary(Opcode opcode, PrimType resultType, ConstvalNode *c); + ConstvalNode *FoldCeil(const ConstvalNode *cst, PrimType fromType, PrimType toType) const; + ConstvalNode *FoldFloor(const ConstvalNode *cst, PrimType fromType, PrimType toType) const; + ConstvalNode *FoldRound(const ConstvalNode *cst, PrimType fromType, PrimType toType) const; + ConstvalNode *FoldTrunk(const ConstvalNode *cst, PrimType fromType, PrimType toType) const; + ConstvalNode *FoldTypeCvt(const ConstvalNode *cst, PrimType fromType, PrimType toType) const; + ConstvalNode *FoldConstComparison(Opcode opcode, PrimType resultType, PrimType opndType, const ConstvalNode &const0, + const ConstvalNode &const1) const; + ConstvalNode *FoldConstBinary(Opcode opcode, PrimType resultType, const ConstvalNode &const0, + const ConstvalNode &const1) const; + ConstvalNode *FoldIntConstComparison(Opcode opcode, PrimType resultType, const ConstvalNode &const0, + const ConstvalNode &const1) const; + MIRIntConst *FoldIntConstComparisonMIRConst(Opcode, PrimType, const MIRIntConst&, const MIRIntConst&) const; + ConstvalNode *FoldIntConstBinary(Opcode opcode, PrimType resultType, const ConstvalNode &const0, + const ConstvalNode &const1) const; + ConstvalNode *FoldFPConstComparison(Opcode opcode, PrimType resultType, PrimType opndType, const ConstvalNode &const0, + const ConstvalNode &const1) const; + MIRIntConst *FoldFPConstComparisonMIRConst(Opcode opcode, PrimType resultType, PrimType opndType, + const MIRConst &const0, const MIRConst &const1) const; + ConstvalNode *FoldFPConstBinary(Opcode opcode, PrimType resultType, const ConstvalNode &const0, + const ConstvalNode &const1) const; + ConstvalNode *FoldConstUnary(Opcode opcode, PrimType resultType, ConstvalNode *c) const; + ConstvalNode *FoldIntConstUnary(Opcode opcode, PrimType resultType, const ConstvalNode *c) const; template - ConstvalNode *FoldFPConstUnary(Opcode opcode, PrimType resultType, ConstvalNode *c); - BaseNode *NegateTree(BaseNode *node); - BaseNode *Negate(BaseNode *node); - BaseNode *Negate(UnaryNode *node); - BaseNode *Negate(ConstvalNode *node); - BinaryNode *NewBinaryNode(BinaryNode *old, Opcode op, PrimType primeType, BaseNode *l, BaseNode *r); - UnaryNode *NewUnaryNode(UnaryNode *old, Opcode op, PrimType primeType, BaseNode *e); + ConstvalNode *FoldFPConstUnary(Opcode opcode, PrimType resultType, ConstvalNode *c) const; + BaseNode *NegateTree(BaseNode *node) const; + BaseNode *Negate(BaseNode *node) const; + BaseNode *Negate(UnaryNode *node) const; + BaseNode *Negate(const ConstvalNode *node) const; + BinaryNode *NewBinaryNode(BinaryNode *old, Opcode op, PrimType primeType, BaseNode *l, BaseNode *r) const; + UnaryNode *NewUnaryNode(UnaryNode *old, Opcode op, PrimType primeType, BaseNode *e) const; std::pair DispatchFold(BaseNode *node); - BaseNode *PairToExpr(PrimType resultType, const std::pair &p); - BaseNode *SimplifyDoubleCompare(CompareNode *node); + BaseNode *PairToExpr(PrimType resultType, const std::pair &p) const; + BaseNode *SimplifyDoubleCompare(CompareNode *node) const; }; class DoConstantFold : public ModulePhase { diff --git a/src/mpl2mpl/src/coderelayout.cpp b/src/mpl2mpl/src/coderelayout.cpp index 3a0ef825fabaa918bfc2cd51766befe7969b0f98..d9edfa737553788967e7736f9b259cdb6357f0b8 100644 --- a/src/mpl2mpl/src/coderelayout.cpp +++ b/src/mpl2mpl/src/coderelayout.cpp @@ -17,6 +17,7 @@ #include #include #include "profile.h" + // This phase layout the function according the profile. // First parse the profile,find the corresponding dex file's // function type info,the profile file give three function type @@ -66,10 +67,11 @@ CallNode *CodeReLayout::CreateRecordFieldStaticCall(BaseNode *node, const std::s return builder->CreateStmtCall(callee->GetPuidx(), args); } -std::string CodeReLayout::StaticFieldFilename(const std::string &mplFile) { +std::string CodeReLayout::StaticFieldFilename(const std::string &mplFile) const { size_t pos = mplFile.rfind(".mpl"); size_t postfixSize = 4; - CHECK_FATAL(pos != std::string::npos && pos == mplFile.length() - postfixSize, "Not compiling .mpl file?"); + CHECK_FATAL(pos != std::string::npos, "Not found .mpl postfix"); + CHECK_FATAL(pos == mplFile.length() - postfixSize, "Not compiling .mpl file?"); std::string smryFileName = mplFile.substr(0, pos) + ".staticfields"; return smryFileName; } @@ -93,7 +95,7 @@ void CodeReLayout::AddStaticFieldRecord() { } } -void CodeReLayout::FindDreadRecur(StmtNode *stmt, BaseNode *node) { +void CodeReLayout::FindDreadRecur(const StmtNode *stmt, BaseNode *node) { if (node == nullptr) { return; } @@ -137,8 +139,8 @@ void CodeReLayout::FindDreadRecur(StmtNode *stmt, BaseNode *node) { } } -void CodeReLayout::InsertProfileBeforeDread(StmtNode *stmt, BaseNode *opnd) { - if (!opnd || (opnd->GetOpCode() != OP_dread && opnd->GetOpCode() != OP_addrof)) { +void CodeReLayout::InsertProfileBeforeDread(const StmtNode *stmt, BaseNode *opnd) { + if (opnd == nullptr || (opnd->GetOpCode() != OP_dread && opnd->GetOpCode() != OP_addrof)) { return; } DreadNode *dreadNode = static_cast(opnd); @@ -234,22 +236,22 @@ void CodeReLayout::GenLayoutSym() { MIRAggConst *funcLayoutConst = GetMIRModule().GetMemPool()->New(GetMIRModule(), arrayType); uint32 funcIdx = 0; MIRConst *fieldConst = nullptr; - MIRFunction *vMethod = nullptr; + MIRFunction *method = nullptr; for (uint32 i = 0; i < static_cast(LayoutType::kLayoutTypeCount); ++i) { if (funcIdx < GetMIRModule().GetFunctionList().size()) { - vMethod = GetMIRModule().GetFunction(funcIdx); + method = GetMIRModule().GetFunction(funcIdx); } else { std::cerr << "no method for codelayout type " << GetLayoutTypeString(i) << "\n"; return; } - while (vMethod->IsAbstract() || vMethod->GetBody() == nullptr) { + while (method->IsAbstract() || method->GetBody() == nullptr) { // find the function not Abstract if (trace) { LogInfo::MapleLogger() << "encounter valid method " << funcIdx << "\n"; } funcIdx++; if (funcIdx < GetMIRModule().GetFunctionList().size()) { - vMethod = GetMIRModule().GetFunction(funcIdx); + method = GetMIRModule().GetFunction(funcIdx); } else { std::cerr << "no method for codelayout" << GetLayoutTypeString(i) << "\n"; return; @@ -257,9 +259,9 @@ void CodeReLayout::GenLayoutSym() { } if (trace) { LogInfo::MapleLogger() << "Start of category " << i << " in funcIdx " << funcIdx << " " - << vMethod->GetName() << "\n"; + << method->GetName() << "\n"; } - AddroffuncNode *addroffuncExpr = builder->CreateExprAddroffunc(vMethod->GetPuidx(), GetMIRModule().GetMemPool()); + AddroffuncNode *addroffuncExpr = builder->CreateExprAddroffunc(method->GetPuidx(), GetMIRModule().GetMemPool()); fieldConst = GetMIRModule().GetMemPool()->New(addroffuncExpr->GetPUIdx(), *GlobalTables::GetTypeTable().GetVoidPtr()); diff --git a/src/mpl2mpl/src/constantfold.cpp b/src/mpl2mpl/src/constantfold.cpp index b03dda04a148f57428f3d84be83742b6929eb2ca..34c1b713630be672c2902ddafc5a14479ea01f28 100644 --- a/src/mpl2mpl/src/constantfold.cpp +++ b/src/mpl2mpl/src/constantfold.cpp @@ -41,7 +41,8 @@ namespace maple { // A. Analyze expression type // B. Analysis operator type // C. Replace the expression with the result of the operation -BinaryNode *ConstantFold::NewBinaryNode(BinaryNode *old, Opcode op, PrimType primType, BaseNode *lhs, BaseNode *rhs) { +BinaryNode *ConstantFold::NewBinaryNode(BinaryNode *old, Opcode op, PrimType primType, + BaseNode *lhs, BaseNode *rhs) const { CHECK_NULL_FATAL(old); BinaryNode *result = nullptr; if (old->GetOpCode() == op && old->GetPrimType() == primType && old->Opnd(0) == lhs && old->Opnd(1) == rhs) { @@ -52,7 +53,7 @@ BinaryNode *ConstantFold::NewBinaryNode(BinaryNode *old, Opcode op, PrimType pri return result; } -UnaryNode *ConstantFold::NewUnaryNode(UnaryNode *old, Opcode op, PrimType primType, BaseNode *e) { +UnaryNode *ConstantFold::NewUnaryNode(UnaryNode *old, Opcode op, PrimType primType, BaseNode *e) const { CHECK_NULL_FATAL(old); UnaryNode *result = nullptr; if (old->GetOpCode() == op && old->GetPrimType() == primType && old->Opnd(0) == e) { @@ -63,7 +64,7 @@ UnaryNode *ConstantFold::NewUnaryNode(UnaryNode *old, Opcode op, PrimType primTy return result; } -BaseNode *ConstantFold::PairToExpr(PrimType resultType, const std::pair &p) { +BaseNode *ConstantFold::PairToExpr(PrimType resultType, const std::pair &p) const { CHECK_NULL_FATAL(p.first); BaseNode *result = p.first; if (p.second == 0) { @@ -229,12 +230,12 @@ std::pair ConstantFold::DispatchFold(BaseNode *node) { } } -BaseNode *ConstantFold::Negate(BaseNode *node) { +BaseNode *ConstantFold::Negate(BaseNode *node) const { CHECK_NULL_FATAL(node); return mirModule->CurFuncCodeMemPool()->New(OP_neg, PrimType(node->GetPrimType()), node); } -BaseNode *ConstantFold::Negate(UnaryNode *node) { +BaseNode *ConstantFold::Negate(UnaryNode *node) const { CHECK_NULL_FATAL(node); BaseNode *result = nullptr; if (node->GetOpCode() == OP_neg) { @@ -246,7 +247,7 @@ BaseNode *ConstantFold::Negate(UnaryNode *node) { return result; } -BaseNode *ConstantFold::Negate(ConstvalNode *node) { +BaseNode *ConstantFold::Negate(const ConstvalNode *node) const { CHECK_NULL_FATAL(node); ConstvalNode *copy = node->CloneTree(mirModule->GetCurFuncCodeMPAllocator()); CHECK_NULL_FATAL(copy); @@ -254,7 +255,7 @@ BaseNode *ConstantFold::Negate(ConstvalNode *node) { return copy; } -BaseNode *ConstantFold::NegateTree(BaseNode *node) { +BaseNode *ConstantFold::NegateTree(BaseNode *node) const { CHECK_NULL_FATAL(node); if (node->IsUnaryNode()) { return Negate(static_cast(node)); @@ -267,7 +268,7 @@ BaseNode *ConstantFold::NegateTree(BaseNode *node) { MIRIntConst *ConstantFold::FoldIntConstComparisonMIRConst(Opcode opcode, PrimType resultType, const MIRIntConst &intConst0, - const MIRIntConst &intConst1) { + const MIRIntConst &intConst1) const { int64 result = 0; bool greater = (intConst0.GetValue() > intConst1.GetValue()); bool equal = (intConst0.GetValue() == intConst1.GetValue()); @@ -309,6 +310,7 @@ MIRIntConst *ConstantFold::FoldIntConstComparisonMIRConst(Opcode opcode, PrimTyp } default: ASSERT(false, "Unknown opcode for FoldIntConstComparison"); + break; } // determine the type MIRType &type = *GlobalTables::GetTypeTable().GetPrimType(resultType); @@ -324,9 +326,9 @@ MIRIntConst *ConstantFold::FoldIntConstComparisonMIRConst(Opcode opcode, PrimTyp } ConstvalNode *ConstantFold::FoldIntConstComparison(Opcode opcode, PrimType resultType, - ConstvalNode &const0, ConstvalNode &const1) { - MIRIntConst *intConst0 = safe_cast(const0.GetConstVal()); - MIRIntConst *intConst1 = safe_cast(const1.GetConstVal()); + const ConstvalNode &const0, const ConstvalNode &const1) const { + const MIRIntConst *intConst0 = safe_cast(const0.GetConstVal()); + const MIRIntConst *intConst1 = safe_cast(const1.GetConstVal()); CHECK_NULL_FATAL(intConst0); CHECK_NULL_FATAL(intConst1); MIRIntConst *constValue = FoldIntConstComparisonMIRConst(opcode, resultType, *intConst0, *intConst1); @@ -337,10 +339,10 @@ ConstvalNode *ConstantFold::FoldIntConstComparison(Opcode opcode, PrimType resul return resultConst; } -ConstvalNode *ConstantFold::FoldIntConstBinary(Opcode opcode, PrimType resultType, ConstvalNode &const0, - ConstvalNode &const1) { - MIRIntConst *intConst0 = safe_cast(const0.GetConstVal()); - MIRIntConst *intConst1 = safe_cast(const1.GetConstVal()); +ConstvalNode *ConstantFold::FoldIntConstBinary(Opcode opcode, PrimType resultType, const ConstvalNode &const0, + const ConstvalNode &const1) const { + const MIRIntConst *intConst0 = safe_cast(const0.GetConstVal()); + const MIRIntConst *intConst1 = safe_cast(const1.GetConstVal()); CHECK_NULL_FATAL(intConst0); CHECK_NULL_FATAL(intConst1); int64 intValueOfConst0 = intConst0->GetValue(); @@ -516,6 +518,7 @@ ConstvalNode *ConstantFold::FoldIntConstBinary(Opcode opcode, PrimType resultTyp } default: ASSERT(false, "Unknown opcode for FoldIntConstBinary"); + break; } // determine the type MIRType &type = *GlobalTables::GetTypeTable().GetPrimType(resultType); @@ -536,13 +539,13 @@ ConstvalNode *ConstantFold::FoldIntConstBinary(Opcode opcode, PrimType resultTyp return resultConst; } -ConstvalNode *ConstantFold::FoldFPConstBinary(Opcode opcode, PrimType resultType, ConstvalNode &const0, - ConstvalNode &const1) { +ConstvalNode *ConstantFold::FoldFPConstBinary(Opcode opcode, PrimType resultType, const ConstvalNode &const0, + const ConstvalNode &const1) const { ASSERT(const0.GetPrimType() == const1.GetPrimType(), "The types of the operands must match"); - MIRDoubleConst *doubleConst0 = nullptr; - MIRDoubleConst *doubleConst1 = nullptr; - MIRFloatConst *floatConst0 = nullptr; - MIRFloatConst *floatConst1 = nullptr; + const MIRDoubleConst *doubleConst0 = nullptr; + const MIRDoubleConst *doubleConst1 = nullptr; + const MIRFloatConst *floatConst0 = nullptr; + const MIRFloatConst *floatConst1 = nullptr; bool useDouble = (const0.GetPrimType() == PTY_f64); ConstvalNode *resultConst = mirModule->CurFuncCodeMemPool()->New(); resultConst->SetPrimType(resultType); @@ -630,6 +633,7 @@ ConstvalNode *ConstantFold::FoldFPConstBinary(Opcode opcode, PrimType resultType } default: ASSERT(false, "Unknown opcode for FoldFPConstBinary"); + break; } if (resultType == PTY_f64) { resultConst->SetConstVal(GlobalTables::GetFpConstTable().GetOrCreateDoubleConst(constValueDouble)); @@ -640,11 +644,11 @@ ConstvalNode *ConstantFold::FoldFPConstBinary(Opcode opcode, PrimType resultType } MIRIntConst *ConstantFold::FoldFPConstComparisonMIRConst(Opcode opcode, PrimType resultType, PrimType opndType, - MIRConst &const0, MIRConst &const1) { - MIRDoubleConst *doubleConst0 = nullptr; - MIRDoubleConst *doubleConst1 = nullptr; - MIRFloatConst *floatConst0 = nullptr; - MIRFloatConst *floatConst1 = nullptr; + const MIRConst &const0, const MIRConst &const1) const { + const MIRDoubleConst *doubleConst0 = nullptr; + const MIRDoubleConst *doubleConst1 = nullptr; + const MIRFloatConst *floatConst0 = nullptr; + const MIRFloatConst *floatConst1 = nullptr; bool useDouble = (opndType == PTY_f64); if (useDouble) { doubleConst0 = safe_cast(&const0); @@ -740,13 +744,14 @@ MIRIntConst *ConstantFold::FoldFPConstComparisonMIRConst(Opcode opcode, PrimType } default: ASSERT(false, "Unknown opcode for FoldFPConstComparison"); + break; } MIRIntConst *resultConst = GlobalTables::GetIntConstTable().GetOrCreateIntConst(constValue, type); return resultConst; } ConstvalNode *ConstantFold::FoldFPConstComparison(Opcode opcode, PrimType resultType, PrimType opndType, - ConstvalNode &const0, ConstvalNode &const1) { + const ConstvalNode &const0, const ConstvalNode &const1) const { ASSERT(const0.GetPrimType() == const1.GetPrimType(), "The types of the operands must match"); ConstvalNode *resultConst = mirModule->CurFuncCodeMemPool()->New(); resultConst->SetPrimType(resultType); @@ -756,7 +761,7 @@ ConstvalNode *ConstantFold::FoldFPConstComparison(Opcode opcode, PrimType result } MIRConst *ConstantFold::FoldConstComparisonMIRConst(Opcode opcode, PrimType resultType, PrimType opndType, - MIRConst &const0, MIRConst &const1) { + const MIRConst &const0, const MIRConst &const1) { MIRConst *returnValue = nullptr; if (IsPrimitiveInteger(opndType) || IsPrimitiveDynInteger(opndType)) { returnValue = FoldIntConstComparisonMIRConst(opcode, resultType, *safe_cast(&const0), @@ -770,7 +775,7 @@ MIRConst *ConstantFold::FoldConstComparisonMIRConst(Opcode opcode, PrimType resu } ConstvalNode *ConstantFold::FoldConstComparison(Opcode opcode, PrimType resultType, PrimType opndType, - ConstvalNode &const0, ConstvalNode &const1) { + const ConstvalNode &const0, const ConstvalNode &const1) const { ConstvalNode *returnValue = nullptr; if (IsPrimitiveInteger(opndType) || IsPrimitiveDynInteger(opndType)) { returnValue = FoldIntConstComparison(opcode, resultType, const0, const1); @@ -782,8 +787,8 @@ ConstvalNode *ConstantFold::FoldConstComparison(Opcode opcode, PrimType resultTy return returnValue; } -ConstvalNode *ConstantFold::FoldConstBinary(Opcode opcode, PrimType resultType, ConstvalNode &const0, - ConstvalNode &const1) { +ConstvalNode *ConstantFold::FoldConstBinary(Opcode opcode, PrimType resultType, const ConstvalNode &const0, + const ConstvalNode &const1) const { ConstvalNode *returnValue = nullptr; if (IsPrimitiveInteger(resultType) || IsPrimitiveDynInteger(resultType)) { returnValue = FoldIntConstBinary(opcode, resultType, const0, const1); @@ -795,9 +800,9 @@ ConstvalNode *ConstantFold::FoldConstBinary(Opcode opcode, PrimType resultType, return returnValue; } -ConstvalNode *ConstantFold::FoldIntConstUnary(Opcode opcode, PrimType resultType, ConstvalNode *c) { +ConstvalNode *ConstantFold::FoldIntConstUnary(Opcode opcode, PrimType resultType, const ConstvalNode *c) const { CHECK_NULL_FATAL(c); - MIRIntConst *cst = safe_cast(c->GetConstVal()); + const MIRIntConst *cst = safe_cast(c->GetConstVal()); uint32 result32 = 0; uint64 result64 = 0; bool useResult64 = (GetPrimTypeSize(resultType) == kByteSizeOfBit64); @@ -853,6 +858,7 @@ ConstvalNode *ConstantFold::FoldIntConstUnary(Opcode opcode, PrimType resultType } default: ASSERT(false, "Unknown opcode for FoldIntConstUnary"); + break; } // determine the type MIRType &type = *GlobalTables::GetTypeTable().GetPrimType(resultType); @@ -874,7 +880,7 @@ ConstvalNode *ConstantFold::FoldIntConstUnary(Opcode opcode, PrimType resultType } template -ConstvalNode *ConstantFold::FoldFPConstUnary(Opcode opcode, PrimType resultType, ConstvalNode *c) { +ConstvalNode *ConstantFold::FoldFPConstUnary(Opcode opcode, PrimType resultType, ConstvalNode *c) const { CHECK_NULL_FATAL(c); ConstvalNode *resultConst = c; typename T::value_type constValue = 0.0; @@ -906,6 +912,7 @@ ConstvalNode *ConstantFold::FoldFPConstUnary(Opcode opcode, PrimType resultType, } default: ASSERT(false, "Unknown opcode for FoldFPConstUnary"); + break; } resultConst = mirModule->CurFuncCodeMemPool()->New(); resultConst->SetPrimType(resultType); @@ -917,7 +924,7 @@ ConstvalNode *ConstantFold::FoldFPConstUnary(Opcode opcode, PrimType resultType, return resultConst; } -ConstvalNode *ConstantFold::FoldConstUnary(Opcode opcode, PrimType resultType, ConstvalNode *c) { +ConstvalNode *ConstantFold::FoldConstUnary(Opcode opcode, PrimType resultType, ConstvalNode *c) const { ConstvalNode *returnValue = nullptr; if (IsPrimitiveInteger(resultType) || IsPrimitiveDynInteger(resultType)) { returnValue = FoldIntConstUnary(opcode, resultType, c); @@ -932,7 +939,7 @@ ConstvalNode *ConstantFold::FoldConstUnary(Opcode opcode, PrimType resultType, C return returnValue; } -std::pair ConstantFold::FoldSizeoftype(SizeoftypeNode *node) { +std::pair ConstantFold::FoldSizeoftype(SizeoftypeNode *node) const { CHECK_NULL_FATAL(node); BaseNode *result = node; MIRType *argType = GlobalTables::GetTypeTable().GetTypeFromTyIdx(node->GetTyIdx()); @@ -1003,18 +1010,18 @@ std::pair ConstantFold::FoldUnary(UnaryNode *node) { return std::make_pair(result, sum); } -ConstvalNode *ConstantFold::FoldCeil(ConstvalNode *cst, PrimType fromType, PrimType toType) { +ConstvalNode *ConstantFold::FoldCeil(const ConstvalNode *cst, PrimType fromType, PrimType toType) const { ConstvalNode *resultConst = mirModule->CurFuncCodeMemPool()->New(); resultConst->SetPrimType(toType); MIRType &resultType = *GlobalTables::GetTypeTable().GetPrimType(toType); CHECK_NULL_FATAL(cst); if (fromType == PTY_f32) { - MIRFloatConst *constValue = safe_cast(cst->GetConstVal()); + const MIRFloatConst *constValue = safe_cast(cst->GetConstVal()); float floutValue = ceil(constValue->GetValue()); resultConst->SetConstVal(GlobalTables::GetIntConstTable().GetOrCreateIntConst( static_cast(floutValue), resultType)); } else { - MIRDoubleConst *constValue = safe_cast(cst->GetConstVal()); + const MIRDoubleConst *constValue = safe_cast(cst->GetConstVal()); double doubleValue = ceil(constValue->GetValue()); resultConst->SetConstVal(GlobalTables::GetIntConstTable().GetOrCreateIntConst( static_cast(doubleValue), resultType)); @@ -1022,46 +1029,46 @@ ConstvalNode *ConstantFold::FoldCeil(ConstvalNode *cst, PrimType fromType, PrimT return resultConst; } -MIRConst *ConstantFold::FoldFloorMIRConst(MIRConst *cst, PrimType fromType, PrimType toType) { +MIRConst *ConstantFold::FoldFloorMIRConst(const MIRConst *cst, PrimType fromType, PrimType toType) const { MIRType &resultType = *GlobalTables::GetTypeTable().GetPrimType(toType); CHECK_NULL_FATAL(cst); if (fromType == PTY_f32) { - MIRFloatConst *constValue = safe_cast(cst); + const MIRFloatConst *constValue = safe_cast(cst); float floutValue = floor(constValue->GetValue()); return GlobalTables::GetIntConstTable().GetOrCreateIntConst(static_cast(floutValue), resultType); } else { - MIRDoubleConst *constValue = safe_cast(cst); + const MIRDoubleConst *constValue = safe_cast(cst); double doubleValue = floor(constValue->GetValue()); return GlobalTables::GetIntConstTable().GetOrCreateIntConst(static_cast(doubleValue), resultType); } } -ConstvalNode *ConstantFold::FoldFloor(ConstvalNode *cst, PrimType fromType, PrimType toType) { +ConstvalNode *ConstantFold::FoldFloor(const ConstvalNode *cst, PrimType fromType, PrimType toType) const { ConstvalNode *resultConst = mirModule->CurFuncCodeMemPool()->New(); resultConst->SetPrimType(toType); resultConst->SetConstVal(FoldFloorMIRConst(cst->GetConstVal(), fromType, toType)); return resultConst; } -MIRConst *ConstantFold::FoldRoundMIRConst(MIRConst *cst, PrimType fromType, PrimType toType) { +MIRConst *ConstantFold::FoldRoundMIRConst(const MIRConst *cst, PrimType fromType, PrimType toType) const { MIRType &resultType = *GlobalTables::GetTypeTable().GetPrimType(toType); if (fromType == PTY_f32) { - MIRFloatConst *constValue = safe_cast(cst); - float floutValue = round(constValue->GetValue()); - return GlobalTables::GetIntConstTable().GetOrCreateIntConst(static_cast(floutValue), resultType); + const MIRFloatConst *constValue = safe_cast(cst); + float floatValue = round(constValue->GetValue()); + return GlobalTables::GetIntConstTable().GetOrCreateIntConst(static_cast(floatValue), resultType); } else if (fromType == PTY_f64) { - MIRDoubleConst *constValue = safe_cast(cst); + const MIRDoubleConst *constValue = safe_cast(cst); double doubleValue = round(constValue->GetValue()); return GlobalTables::GetIntConstTable().GetOrCreateIntConst(static_cast(doubleValue), resultType); } else if (toType == PTY_f32 && IsPrimitiveInteger(fromType)) { - MIRIntConst *constValue = safe_cast(cst); + const MIRIntConst *constValue = safe_cast(cst); int64 fromValue = constValue->GetValue(); - float floutValue = round(static_cast(fromValue)); - if (static_cast(floutValue) == fromValue) { - return GlobalTables::GetFpConstTable().GetOrCreateFloatConst(floutValue); + float floatValue = round(static_cast(fromValue)); + if (static_cast(floatValue) == fromValue) { + return GlobalTables::GetFpConstTable().GetOrCreateFloatConst(floatValue); } } else if (toType == PTY_f64 && IsPrimitiveInteger(fromType)) { - MIRIntConst *constValue = safe_cast(cst); + const MIRIntConst *constValue = safe_cast(cst); int64 fromValue = constValue->GetValue(); double doubleValue = round(static_cast(fromValue)); if (static_cast(doubleValue) == fromValue) { @@ -1071,7 +1078,7 @@ MIRConst *ConstantFold::FoldRoundMIRConst(MIRConst *cst, PrimType fromType, Prim return nullptr; } -ConstvalNode *ConstantFold::FoldRound(ConstvalNode *cst, PrimType fromType, PrimType toType) { +ConstvalNode *ConstantFold::FoldRound(const ConstvalNode *cst, PrimType fromType, PrimType toType) const { ConstvalNode *resultConst = mirModule->CurFuncCodeMemPool()->New(); resultConst->SetPrimType(toType); CHECK_NULL_FATAL(cst); @@ -1079,18 +1086,18 @@ ConstvalNode *ConstantFold::FoldRound(ConstvalNode *cst, PrimType fromType, Prim return resultConst; } -ConstvalNode *ConstantFold::FoldTrunk(ConstvalNode *cst, PrimType fromType, PrimType toType) { +ConstvalNode *ConstantFold::FoldTrunk(const ConstvalNode *cst, PrimType fromType, PrimType toType) const { ConstvalNode *resultConst = mirModule->CurFuncCodeMemPool()->New(); resultConst->SetPrimType(toType); MIRType &resultType = *GlobalTables::GetTypeTable().GetPrimType(toType); CHECK_NULL_FATAL(cst); if (fromType == PTY_f32) { - MIRFloatConst *constValue = safe_cast(cst->GetConstVal()); + const MIRFloatConst *constValue = safe_cast(cst->GetConstVal()); float floutValue = trunc(constValue->GetValue()); resultConst->SetConstVal(GlobalTables::GetIntConstTable().GetOrCreateIntConst( static_cast(floutValue), resultType)); } else { - MIRDoubleConst *constValue = safe_cast(cst->GetConstVal()); + const MIRDoubleConst *constValue = safe_cast(cst->GetConstVal()); double doubleValue = trunc(constValue->GetValue()); resultConst->SetConstVal(GlobalTables::GetIntConstTable().GetOrCreateIntConst( static_cast(doubleValue), resultType)); @@ -1098,7 +1105,7 @@ ConstvalNode *ConstantFold::FoldTrunk(ConstvalNode *cst, PrimType fromType, Prim return resultConst; } -MIRConst *ConstantFold::FoldTypeCvtMIRConst(MIRConst *cst, PrimType fromType, PrimType toType) { +MIRConst *ConstantFold::FoldTypeCvtMIRConst(const MIRConst *cst, PrimType fromType, PrimType toType) const { if (IsPrimitiveDynType(fromType) || IsPrimitiveDynType(toType)) { // do not fold return nullptr; @@ -1114,7 +1121,7 @@ MIRConst *ConstantFold::FoldTypeCvtMIRConst(MIRConst *cst, PrimType fromType, Pr } toConst = FoldSignExtendMIRConst(op, toType, fromSize, cst); } else { - MIRIntConst *c = safe_cast(cst); + const MIRIntConst *c = safe_cast(cst); MIRType &type = *GlobalTables::GetTypeTable().GetPrimType(toType); toConst = GlobalTables::GetIntConstTable().GetOrCreateIntConst(c->GetValue(), type); } @@ -1124,13 +1131,13 @@ MIRConst *ConstantFold::FoldTypeCvtMIRConst(MIRConst *cst, PrimType fromType, Pr MIRConst *toConst = nullptr; if (GetPrimTypeBitSize(toType) < GetPrimTypeBitSize(fromType)) { ASSERT(GetPrimTypeBitSize(toType) == 32, "We suppot F32 and F64"); - MIRDoubleConst *fromValue = safe_cast(cst); + const MIRDoubleConst *fromValue = safe_cast(cst); float floutValue = static_cast(fromValue->GetValue()); MIRFloatConst *toValue = GlobalTables::GetFpConstTable().GetOrCreateFloatConst(floutValue); toConst = toValue; } else { ASSERT(GetPrimTypeBitSize(toType) == 64, "We suppot F32 and F64"); - MIRFloatConst *fromValue = safe_cast(cst); + const MIRFloatConst *fromValue = safe_cast(cst); double doubleValue = static_cast(fromValue->GetValue()); MIRDoubleConst *toValue = GlobalTables::GetFpConstTable().GetOrCreateDoubleConst(doubleValue); toConst = toValue; @@ -1147,7 +1154,7 @@ MIRConst *ConstantFold::FoldTypeCvtMIRConst(MIRConst *cst, PrimType fromType, Pr return nullptr; } -ConstvalNode *ConstantFold::FoldTypeCvt(ConstvalNode *cst, PrimType fromType, PrimType toType) { +ConstvalNode *ConstantFold::FoldTypeCvt(const ConstvalNode *cst, PrimType fromType, PrimType toType) const { CHECK_NULL_FATAL(cst); MIRConst *toConstValue = FoldTypeCvtMIRConst(cst->GetConstVal(), fromType, toType); if (toConstValue == nullptr) { @@ -1189,6 +1196,7 @@ std::pair ConstantFold::FoldTypeCvt(TypeCvtNode *node) { default: result = nullptr; ASSERT(false, "Unexpected opcode in TypeCvtNodeConstFold"); + break; } } if (result == nullptr) { @@ -1204,8 +1212,9 @@ std::pair ConstantFold::FoldTypeCvt(TypeCvtNode *node) { return std::make_pair(result, 0); } -MIRConst *ConstantFold::FoldSignExtendMIRConst(Opcode opcode, PrimType resultType, uint8 size, MIRConst *cst) { - MIRIntConst *c = safe_cast(cst); +MIRConst *ConstantFold::FoldSignExtendMIRConst(Opcode opcode, PrimType resultType, uint8 size, + const MIRConst *cst) const { + const MIRIntConst *c = safe_cast(cst); uint64 result64 = 0; if (opcode == OP_sext) { result64 = (c->GetValue() << (64u - size)) >> (64u - size); @@ -1217,7 +1226,8 @@ MIRConst *ConstantFold::FoldSignExtendMIRConst(Opcode opcode, PrimType resultTyp return constValue; } -ConstvalNode *ConstantFold::FoldSignExtend(Opcode opcode, PrimType resultType, uint8 size, ConstvalNode *cst) { +ConstvalNode *ConstantFold::FoldSignExtend(Opcode opcode, PrimType resultType, uint8 size, + const ConstvalNode *cst) const { ConstvalNode *resultConst = mirModule->CurFuncCodeMemPool()->New(); CHECK_NULL_FATAL(cst); MIRConst *toConst = FoldSignExtendMIRConst(opcode, resultType, size, cst->GetConstVal()); @@ -1235,7 +1245,7 @@ std::pair ConstantFold::FoldExtractbits(ExtractbitsNode *node) Opcode opcode = node->GetOpCode(); std::pair p = DispatchFold(node->Opnd(0)); ConstvalNode *cst = safe_cast(p.first); - if (cst && (opcode == OP_sext || opcode == OP_zext)) { + if (cst != nullptr && (opcode == OP_sext || opcode == OP_zext)) { result = FoldSignExtend(opcode, node->GetPrimType(), size, cst); } else { BaseNode *e = PairToExpr(node->Opnd(0)->GetPrimType(), p); @@ -1286,7 +1296,7 @@ std::pair ConstantFold::FoldIread(IreadNode *node) { std::pair ConstantFold::FoldBinary(BinaryNode *node) { CHECK_NULL_FATAL(node); BaseNode *result = nullptr; - int64 sum; + int64 sum = 0; Opcode op = node->GetOpCode(); PrimType primType = node->GetPrimType(); PrimType lPrimTypes = node->Opnd(0)->GetPrimType(); @@ -1454,7 +1464,7 @@ std::pair ConstantFold::FoldBinary(BinaryNode *node) { return std::make_pair(result, sum); } -BaseNode *ConstantFold::SimplifyDoubleCompare(CompareNode *node) { +BaseNode *ConstantFold::SimplifyDoubleCompare(CompareNode *node) const { // For cases on gitlab issue 636. // See arm manual B.cond(P2993) and FCMP(P1091) CHECK_NULL_FATAL(node); @@ -1565,37 +1575,38 @@ BaseNode *ConstantFold::Fold(BaseNode *node) { std::pair ConstantFold::FoldDepositbits(DepositbitsNode *node) { CHECK_NULL_FATAL(node); BaseNode *result = nullptr; - uint8 bOffset = node->GetBitsOffset(); - uint8 bSize = node->GetBitsSize(); - std::pair lp = DispatchFold(node->Opnd(0)); - std::pair rp = DispatchFold(node->Opnd(1)); - ConstvalNode *lConst = safe_cast(lp.first); - ConstvalNode *rConst = safe_cast(rp.first); - if (lConst != nullptr && rConst != nullptr) { - MIRIntConst *intConst0 = safe_cast(lConst->GetConstVal()); - MIRIntConst *intConst1 = safe_cast(rConst->GetConstVal()); + uint8 bitsOffset = node->GetBitsOffset(); + uint8 bitsSize = node->GetBitsSize(); + std::pair leftPair = DispatchFold(node->Opnd(0)); + std::pair rightPair = DispatchFold(node->Opnd(1)); + ConstvalNode *leftConst = safe_cast(leftPair.first); + ConstvalNode *rightConst = safe_cast(rightPair.first); + if (leftConst != nullptr && rightConst != nullptr) { + MIRIntConst *intConst0 = safe_cast(leftConst->GetConstVal()); + MIRIntConst *intConst1 = safe_cast(rightConst->GetConstVal()); ConstvalNode *resultConst = mirModule->CurFuncCodeMemPool()->New(); resultConst->SetPrimType(node->GetPrimType()); MIRType &type = *GlobalTables::GetTypeTable().GetPrimType(node->GetPrimType()); MIRIntConst *constValue = GlobalTables::GetIntConstTable().GetOrCreateIntConst(0, type); uint64 op0ExtractVal = 0; uint64 op1ExtractVal = 0; - uint64 mask0 = (1LLU << (bSize + bOffset)) - 1; - uint64 mask1 = (1LLU << bOffset) - 1; + uint64 mask0 = (1LLU << (bitsSize + bitsOffset)) - 1; + uint64 mask1 = (1LLU << bitsOffset) - 1; uint64 op0Mask = ~(mask0 ^ mask1); op0ExtractVal = (static_cast(intConst0->GetValue()) & op0Mask); - op1ExtractVal = (static_cast(intConst1->GetValue()) << bOffset) & ((1ULL << (bSize + bOffset)) - 1); + op1ExtractVal = (static_cast(intConst1->GetValue()) << bitsOffset) & + ((1ULL << (bitsSize + bitsOffset)) - 1); constValue = GlobalTables::GetIntConstTable().GetOrCreateIntConst( op0ExtractVal | op1ExtractVal, constValue->GetType(), constValue->GetFieldId()); resultConst->SetConstVal(constValue); result = resultConst; } else { - BaseNode *l = PairToExpr(node->Opnd(0)->GetPrimType(), lp); - BaseNode *r = PairToExpr(node->Opnd(1)->GetPrimType(), rp); + BaseNode *l = PairToExpr(node->Opnd(0)->GetPrimType(), leftPair); + BaseNode *r = PairToExpr(node->Opnd(1)->GetPrimType(), rightPair); if (l != node->Opnd(0) || r != node->Opnd(1)) { result = mirModule->CurFuncCodeMemPool()->New(Opcode(node->GetOpCode()), PrimType(node->GetPrimType()), - bOffset, bSize, l, r); + bitsOffset, bitsSize, l, r); } else { result = node; } @@ -1761,7 +1772,7 @@ StmtNode *ConstantFold::SimplifyCondGoto(CondGotoNode *node) { return node; } -StmtNode *ConstantFold::SimplifyCondGotoSelect(CondGotoNode *node) { +StmtNode *ConstantFold::SimplifyCondGotoSelect(CondGotoNode *node) const { CHECK_NULL_FATAL(node); TernaryNode *sel = static_cast(node->Opnd(0)); if (sel == nullptr || sel->GetOpCode() != OP_select) {