Fix const node polution in const param optimization

This commit is contained in:
drmortalwombat 2023-09-23 18:52:26 +02:00
parent cda66f3ec4
commit bba8931860
3 changed files with 16 additions and 6 deletions

View File

@ -967,6 +967,16 @@ Declaration* Declaration::BuildArrayPointer(void)
return this; return this;
} }
bool Declaration::IsNullConst(void) const
{
if (mType == DT_CONST_INTEGER || mType == DT_CONST_ADDRESS)
return mInteger == 0;
else if (mType == DT_CONST_FLOAT)
return mNumber == 0;
else
return false;
}
Declaration* Declaration::ConstCast(Declaration* ntype) Declaration* Declaration::ConstCast(Declaration* ntype)
{ {
if (ntype == mBase) if (ntype == mBase)

View File

@ -325,6 +325,7 @@ public:
Declaration* BuildArrayPointer(void); Declaration* BuildArrayPointer(void);
Declaration* DeduceAuto(Declaration* dec); Declaration* DeduceAuto(Declaration* dec);
Declaration* ConstCast(Declaration* ntype); Declaration* ConstCast(Declaration* ntype);
bool IsNullConst(void) const;
bool IsAuto(void) const; bool IsAuto(void) const;
DecType ValueType(void) const; DecType ValueType(void) const;

View File

@ -696,17 +696,15 @@ Declaration* GlobalOptimizer::Analyze(Expression* exp, Declaration* procDec, uin
Declaration* pdec = ldec->mBase->mParams; Declaration* pdec = ldec->mBase->mParams;
while (rex) while (rex)
{ {
Expression* pex = rex->mType == EX_LIST ? rex->mLeft : rex; Expression *& pex(rex->mType == EX_LIST ? rex->mLeft : rex);
if (pdec) if (pdec)
{ {
if ((pdec->mFlags & DTF_FPARAM_UNUSED) && !pex->HasSideEffects())// && pex->mType != EX_CONSTANT) if ((pdec->mFlags & DTF_FPARAM_UNUSED) && !pex->HasSideEffects() && (pex->mType != EX_CONSTANT || !pex->mDecValue->IsNullConst()))
{ {
if (pdec->mBase->IsSimpleType()) if (pdec->mBase->IsSimpleType())
{ {
pex->mType = EX_CONSTANT; pex = new Expression(pex->mLocation, EX_CONSTANT);
pex->mLeft = nullptr;
pex->mRight = nullptr;
switch (pdec->mBase->mType) switch (pdec->mBase->mType)
{ {
case DT_TYPE_INTEGER: case DT_TYPE_INTEGER:
@ -722,6 +720,7 @@ Declaration* GlobalOptimizer::Analyze(Expression* exp, Declaration* procDec, uin
pex->mDecValue = TheNullptrConstDeclaration; pex->mDecValue = TheNullptrConstDeclaration;
break; break;
} }
pex->mDecType = pdec->mBase;
} }
} }
if (!(pdec->mFlags & DTF_FPARAM_UNUSED) && !(pdec->mOptFlags & OPTF_VAR_NOCONST)) if (!(pdec->mFlags & DTF_FPARAM_UNUSED) && !(pdec->mOptFlags & OPTF_VAR_NOCONST))