Optimizer - DCE: find conditionals with constant inputs
This commit is contained in:
parent
027d6a2f8d
commit
08e486a57c
3 changed files with 45 additions and 2 deletions
2
TODO
2
TODO
|
@ -14,8 +14,6 @@ Scripting:
|
|||
* Aliases
|
||||
* Display errors in UI
|
||||
* Optimizer:
|
||||
* Constant propagation
|
||||
* Call arguments
|
||||
* Dead code elimination
|
||||
* Local variables
|
||||
* Unused arguments
|
||||
|
|
44
c-opopt.cc
44
c-opopt.cc
|
@ -1573,5 +1573,49 @@ bool opopt::RemoveDeadCode(
|
|||
T_OpsParserOutput& program ,
|
||||
T_OptData& oData ) noexcept
|
||||
{
|
||||
oData.buildUseDefineChains( program );
|
||||
M_LOGSTR_( "... Eliminating dead code" , 2 );
|
||||
|
||||
struct T_ConstCond_ {
|
||||
T_CondInstrNode* node;
|
||||
int64_t value;
|
||||
};
|
||||
T_AutoArray< T_ConstCond_ , 16 > constConds;
|
||||
oData.visitor.visit( program.root , [&]( A_Node& node , const bool exit ) {
|
||||
const auto nt{ node.type( ) };
|
||||
|
||||
if ( !exit && nt == A_Node::OP_COND ) {
|
||||
auto& cn{ dynamic_cast< T_CondInstrNode& >( node ) };
|
||||
auto const& ce{ cn.expression( ).expression( ) };
|
||||
if ( ce.type( ) == A_Node::EXPR_CONST ) {
|
||||
const int64_t ccv{ ( (T_ConstantExprNode const&) ce ).intValue( ) };
|
||||
constConds.add( T_ConstCond_{ &cn , ccv } );
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return !dynamic_cast< A_ExpressionNode* >( &node );
|
||||
} );
|
||||
|
||||
if ( constConds.size( ) ) {
|
||||
const auto ncc{ constConds.size( ) };
|
||||
for ( auto i = 0u ; i < ncc ; i ++ ) {
|
||||
auto const& cc{ constConds[ i ] };
|
||||
auto& cn{ *cc.node };
|
||||
T_InstrListNode* replacement;
|
||||
printf( "IF (CNST: %ld)!!!\n" , cc.value );
|
||||
if ( cn.hasCase( cc.value ) ) {
|
||||
printf( "Will replace with case\n" );
|
||||
replacement = &cn.getCase( cc.value ).instructions( );
|
||||
} else if ( cn.hasDefaultCase( ) ) {
|
||||
printf( "Will replace with default\n" );
|
||||
replacement = &cn.defaultCase( ).instructions( );
|
||||
} else {
|
||||
printf( "DELETE! DELETE! DELETE!\n" );
|
||||
replacement = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
(fixed-resolution on)
|
||||
)
|
||||
(constant-propagation on)
|
||||
(dead-code-elimination on)
|
||||
)
|
||||
)
|
||||
|
||||
|
|
Loading…
Reference in a new issue