From 01d958626441dd209f3880fec49cea17ddc92761 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Sun, 17 Dec 2017 10:30:00 +0100 Subject: [PATCH] Optimizer - Remove unused globals --- TODO | 1 + c-opopt.cc | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/TODO b/TODO index 8db62c2..cff506d 100644 --- a/TODO +++ b/TODO @@ -18,6 +18,7 @@ Scripting: * Unused arguments * Dead store: call arguments * Unused functions + * Unused inputs * Common subexpressions * Strength reduction * Loop-invariant code motion diff --git a/c-opopt.cc b/c-opopt.cc index 13e6b4f..9c1078c 100644 --- a/c-opopt.cc +++ b/c-opopt.cc @@ -1710,7 +1710,6 @@ bool RDCUnusedLocals_( ) noexcept { M_LOGSTR_( "...... Removing unused locals" , 3 ); - oData.buildUseDefineChains( program ); const auto nf{ program.root.nFunctions( ) }; bool changed{ false }; @@ -1734,7 +1733,7 @@ bool RDCUnusedLocals_( } oData.logger( [&](){ T_StringBuilder sb; - sb << "Removing used local variable " + sb << "Removing unused local variable " << ln << " at " << func.getLocalLocation( l ); return sb; @@ -1747,6 +1746,49 @@ bool RDCUnusedLocals_( return changed; } +bool RDCUnusedGlobals_( + T_OpsParserOutput& program , + T_OptData& oData + ) noexcept +{ + M_LOGSTR_( "...... Removing unused globals" , 3 ); + + T_AutoArray< T_String , 16 > remove; + for ( auto const& gn : program.types.keys( ) ) { + const auto gt{ *program.types.get( gn ) }; + if ( gt == E_DataType::BUILTIN || gt == E_DataType::INPUT ) { + continue; + } + const T_OptData::T_VarId vid{ gn }; + if ( !oData.varUDChains.contains( vid ) ) { + remove.add( gn ); + } + } + + const auto nr{ remove.size( ) }; + for ( auto i = 0u ; i < nr ; i ++ ) { + auto const& gn{ remove[ i ] }; + oData.logger( [&](){ + T_StringBuilder sb; + sb << "Removing unused global " << gn; + return sb; + } , 4 ); + program.types.remove( gn ); + } + return remove.size( ); +} + +bool RDCUnused_( + T_OpsParserOutput& program , + T_OptData& oData + ) noexcept +{ + oData.buildUseDefineChains( program ); + const bool locals{ RDCUnusedLocals_( program , oData ) }; + const bool globals{ RDCUnusedGlobals_( program , oData ) }; + return locals || globals; +} + } // namespace /*----------------------------------------------------------------------------*/ @@ -1770,7 +1812,7 @@ bool opopt::RemoveDeadCode( didStuffThisTime = RDCConditionals_( program , oData ) || RDCDeadStores_( program , oData ) - || RDCUnusedLocals_( program , oData ); + || RDCUnused_( program , oData ); didStuff = didStuff || didStuffThisTime; } while ( didStuffThisTime );