Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

variable folding #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/Jitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ namespace Jitter

void Compile();

bool VariableFolding(StatementList&);
bool ConstantFolding(StatementList&);
bool ConstantPropagation(StatementList&);
bool CopyPropagation(StatementList&);
Expand Down
78 changes: 78 additions & 0 deletions src/Jitter_Optimize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ void CJitter::Compile()
while(1)
{
bool dirty = false;
dirty |= VariableFolding(versionedStatements.statements);
dirty |= ConstantPropagation(versionedStatements.statements);
dirty |= ConstantFolding(versionedStatements.statements);
dirty |= CopyPropagation(versionedStatements.statements);
Expand Down Expand Up @@ -827,6 +828,83 @@ bool CJitter::FoldConstant12832Operation(STATEMENT& statement)
return changed;
}

bool CJitter::VariableFolding(StatementList& statements)
{
auto changed = false;
for(auto& statement : statements)
{
if(!statement.src2) continue;

bool src1cst = statement.src1.get()->GetSymbol().get()->IsConstant();
bool src2cst = statement.src2.get()->GetSymbol().get()->IsConstant();

if(src1cst || src2cst || !statement.src1.get()->Equals(statement.src2.get())) continue;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not very good with logical expressions, so I just want to make sure I understand properly :)

This function transforms statements that have the same non-constant src1 and src2 operands?

Copy link
Contributor Author

@Zer0xFF Zer0xFF Jun 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, (checking if they're constant is easier then checking if they're !(rel || reg || tmp)).


switch(statement.op)
{
case OP_AND64:
case OP_MD_OR:
{
statement.op = OP_MOV;
statement.src2.reset();
changed = true;
}
break;
case OP_XOR:
{
statement.op = OP_MOV;
statement.src1 = MakeSymbolRef(MakeSymbol(SYM_CONSTANT, 0));
statement.src2.reset();
changed = true;
}
break;
case OP_FP_SUB:
{
float constant = 0;
statement.op = OP_FP_LDCST;
statement.src1 = MakeSymbolRef(MakeSymbol(SYM_CONSTANT, *reinterpret_cast<uint32*>(&constant)));
statement.src2.reset();
changed = true;
}
break;
case OP_CMP64:
case OP_FP_CMP:
{
SymbolPtr result;
switch(statement.jmpCondition)
{
case CONDITION_EQ:
case CONDITION_LE:
case CONDITION_GE:
result = MakeSymbol(SYM_CONSTANT, 1);
break;
default:
result = MakeSymbol(SYM_CONSTANT, 0);
break;

}
statement.op = OP_MOV;
statement.src1 = MakeSymbolRef(result);
statement.src2.reset();
changed = true;
}
break;
case OP_FP_DIV:
{
float constant = 1;
statement.op = OP_FP_LDCST;
statement.src1 = MakeSymbolRef(MakeSymbol(SYM_CONSTANT, *reinterpret_cast<uint32*>(&constant)));
statement.src2.reset();
changed = true;
};
break;
default:
break;
}
}
return changed;
}

bool CJitter::ConstantFolding(StatementList& statements)
{
bool changed = false;
Expand Down