2017-12-01 07:30:03 +01:00
|
|
|
#include "externals.hh"
|
|
|
|
|
|
|
|
#include "c-opopt.hh"
|
2017-12-02 10:07:14 +01:00
|
|
|
#include "c-ops.hh"
|
2017-12-01 07:30:03 +01:00
|
|
|
#include "c-sync.hh"
|
|
|
|
|
|
|
|
using namespace ebcl;
|
|
|
|
using namespace opast;
|
|
|
|
using namespace opopt;
|
|
|
|
|
|
|
|
|
2017-12-03 18:35:56 +01:00
|
|
|
/*= T_OptData ================================================================*/
|
|
|
|
|
2017-12-03 13:05:54 +01:00
|
|
|
#define M_LOGSTR_( S , L ) \
|
2017-12-03 18:35:56 +01:00
|
|
|
logger( [](){ return T_StringBuilder{ S }; } , L )
|
2017-12-03 13:05:54 +01:00
|
|
|
|
2017-12-03 23:15:44 +01:00
|
|
|
uint32_t opopt::ComputeHash(
|
|
|
|
T_OptData::T_VarId const& id ) noexcept
|
|
|
|
{
|
|
|
|
const uint32_t nh{ ComputeHash( id.name ) };
|
|
|
|
const uint32_t oh{ id.type != T_OptData::E_UDVarType::GLOBAL
|
|
|
|
? ComputeHash( id.name )
|
|
|
|
: 0
|
|
|
|
};
|
|
|
|
return ( uint32_t( id.type ) << 8 )
|
|
|
|
^ nh
|
|
|
|
^ ( ( oh << 29 ) | ( oh >> 3 ) );
|
|
|
|
}
|
|
|
|
|
2017-12-03 13:05:54 +01:00
|
|
|
|
2017-12-03 18:35:56 +01:00
|
|
|
/*= T_OptData - INPUT DECLARATIONS ===========================================*/
|
2017-12-02 10:40:47 +01:00
|
|
|
|
|
|
|
void T_OptData::findInputDecls(
|
|
|
|
T_OpsParserOutput& program ) noexcept
|
|
|
|
{
|
|
|
|
if ( inputDecls ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
inputDecls = T_KeyValueTable< T_String , T_Array< T_InputDecl > >{ };
|
|
|
|
visitor.visit( program.root , [this]( A_Node& node , const bool exit ) {
|
|
|
|
if ( exit && node.type( ) == A_Node::OP_INPUT ) {
|
|
|
|
auto& input{ (T_InputInstrNode&) node };
|
|
|
|
auto* da{ inputDecls->get( input.id( ) ) };
|
|
|
|
if ( !da ) {
|
|
|
|
inputDecls->add( input.id( ) , T_Array< T_InputDecl >{ } );
|
|
|
|
da = inputDecls->get( input.id( ) );
|
|
|
|
}
|
|
|
|
da->add( T_InputDecl{ input.location( ) , input.defValue( ) } );
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-03 18:35:56 +01:00
|
|
|
/*= T_OptData - INSTRUCTION NUMBERING ========================================*/
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool ODNIVisitor_(
|
|
|
|
A_Node& node ,
|
|
|
|
const bool exit ,
|
|
|
|
T_OptData& oData ,
|
|
|
|
const uint32_t fnIndex ) noexcept
|
|
|
|
{
|
|
|
|
if ( dynamic_cast< A_ExpressionNode* >( &node ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto* const iptr{
|
|
|
|
dynamic_cast< A_InstructionNode* >( &node ) };
|
|
|
|
if ( iptr && !exit ) {
|
|
|
|
auto const& il{ dynamic_cast< T_InstrListNode& >( iptr->parent( ) ) };
|
2017-12-03 23:15:44 +01:00
|
|
|
const auto hash{ ebcl::ComputeHash( (uint64_t)iptr ) };
|
2017-12-03 18:35:56 +01:00
|
|
|
oData.instrIndex.add( hash );
|
|
|
|
oData.instructions.add( T_OptData::T_InstrPos{
|
|
|
|
oData.instructions.size( ) , iptr ,
|
|
|
|
iptr == &il.node( il.size( ) - 1 ) ,
|
|
|
|
fnIndex } );
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace <anon>
|
|
|
|
|
|
|
|
void T_OptData::numberInstructions(
|
|
|
|
T_OpsParserOutput& program ) noexcept
|
|
|
|
{
|
|
|
|
instructions.clear( );
|
|
|
|
instrIndex.clear( );
|
|
|
|
|
|
|
|
const auto nf{ program.root.nFunctions( ) };
|
|
|
|
for ( auto i = 0u ; i < nf ; i ++ ) {
|
|
|
|
visitor.visit( program.root.function( i ) ,
|
|
|
|
[&]( A_Node& node , const bool exit ) {
|
|
|
|
return ODNIVisitor_( node , exit , *this , i );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t T_OptData::indexOf(
|
|
|
|
opast::A_InstructionNode const& instr ) noexcept
|
|
|
|
{
|
2017-12-03 23:15:44 +01:00
|
|
|
const auto hash{ ebcl::ComputeHash( (uint64_t)&instr ) };
|
2017-12-03 18:35:56 +01:00
|
|
|
uint32_t existing{ instrIndex.first( hash ) };
|
|
|
|
while ( existing != T_HashIndex::INVALID_INDEX ) {
|
|
|
|
if ( &instr == instructions[ existing ].node ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
existing = instrIndex.next( existing );
|
|
|
|
}
|
|
|
|
assert( existing != T_HashIndex::INVALID_INDEX );
|
|
|
|
return existing;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-03 23:15:44 +01:00
|
|
|
/*= T_OptData - CONTROL FLOW GRAPH CONSTRUCTION ==============================*/
|
2017-12-03 18:35:56 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
#warning Remove this later
|
2017-12-03 23:15:44 +01:00
|
|
|
#define LL1 2
|
|
|
|
#define LL2 2
|
2017-12-03 18:35:56 +01:00
|
|
|
|
2017-12-03 23:15:44 +01:00
|
|
|
// CFG type shortcuts
|
2017-12-03 18:35:56 +01:00
|
|
|
using T_CFN_ = T_OptData::T_CtrlFlowNode;
|
|
|
|
using P_CFN_ = T_OptData::P_CtrlFlowNode;
|
|
|
|
using RP_CFN_ = T_OptData::RP_CtrlFlowNode;
|
|
|
|
|
2017-12-03 23:15:44 +01:00
|
|
|
// Helpers to create or re-use CFG nodes
|
2017-12-03 18:35:56 +01:00
|
|
|
T_OptData::P_CtrlFlowNode BCFGNewNode_(
|
|
|
|
T_Array< P_CFN_ >& pool ) noexcept
|
|
|
|
{
|
|
|
|
if ( pool.empty( ) ) {
|
|
|
|
return NewOwned< T_CFN_ >( );
|
|
|
|
}
|
|
|
|
|
|
|
|
auto r{ std::move( pool.last( ) ) };
|
|
|
|
pool.removeLast( );
|
|
|
|
r->instructions.clear( );
|
|
|
|
r->inbound.clear( );
|
|
|
|
r->outbound.clear( );
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define M_NEWNODE_() BCFGNewNode_( old )
|
|
|
|
#define M_ADDNEW_() \
|
|
|
|
ctrlFlowGraph[ ctrlFlowGraph.add( M_NEWNODE_( ) ) ].get( )
|
|
|
|
|
|
|
|
} // namespace <anon>
|
|
|
|
|
2017-12-03 23:15:44 +01:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
2017-12-03 18:35:56 +01:00
|
|
|
void T_OptData::buildControlFlowGraph(
|
|
|
|
T_OpsParserOutput& program ) noexcept
|
|
|
|
{
|
|
|
|
// Keep the old array, we'll reuse its contents
|
|
|
|
T_Array< P_CtrlFlowNode > old{ std::move( ctrlFlowGraph ) };
|
|
|
|
M_LOGSTR_( "Building control flow graph" , LL1 );
|
|
|
|
|
|
|
|
// Create special nodes
|
|
|
|
M_ADDNEW_( );
|
|
|
|
const RP_CFN_ nMainLoop { M_ADDNEW_( ) };
|
|
|
|
const RP_CFN_ nExit { M_ADDNEW_( ) };
|
|
|
|
nMainLoop->outbound.add( nExit );
|
|
|
|
nExit->inbound.add( nMainLoop );
|
|
|
|
|
|
|
|
// Data structure to handle conditionals
|
|
|
|
struct T_StackEntry_ {
|
|
|
|
RP_CFN_ condBlock;
|
|
|
|
bool hasDefault{ false };
|
|
|
|
T_AutoArray< RP_CFN_ , 8 > caseBlocks;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Data structure for call sites
|
|
|
|
struct T_CallSite_ {
|
|
|
|
T_String name;
|
|
|
|
RP_CFN_ callBlock;
|
|
|
|
RP_CFN_ retBlock;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Generate control flow graph for each function
|
|
|
|
T_AutoArray< T_StackEntry_ , 8 > stack;
|
|
|
|
T_Array< T_CallSite_ > callSites;
|
|
|
|
RP_CFN_ cNode{ nullptr };
|
|
|
|
visitor.visit( program.root , [&]( auto& node , const bool exit ) {
|
|
|
|
const auto nt{ node.type( ) };
|
|
|
|
|
|
|
|
// Handle start/end of functions
|
|
|
|
if ( nt == A_Node::DECL_FN || nt == A_Node::DECL_INIT
|
|
|
|
|| nt == A_Node::DECL_FRAME ) {
|
|
|
|
auto& n{ dynamic_cast< A_FuncNode& >( node ) };
|
|
|
|
auto const& fn{ n.name( ) };
|
|
|
|
if ( exit ) {
|
|
|
|
assert( stack.empty( ) );
|
|
|
|
logger( [&](){
|
|
|
|
T_StringBuilder sb;
|
|
|
|
sb << "Function ended; last block had "
|
|
|
|
<< ( cNode->instructions
|
|
|
|
? cNode->instructions->count
|
|
|
|
: 0 )
|
|
|
|
<< " instructions";
|
|
|
|
return sb;
|
|
|
|
} , LL1 );
|
|
|
|
auto* frec{ cfgFunctions.get( fn ) };
|
|
|
|
assert( frec );
|
|
|
|
frec->count = ctrlFlowGraph.size( ) - frec->first;
|
|
|
|
cNode = nullptr;
|
|
|
|
} else {
|
|
|
|
logger( [&](){
|
|
|
|
T_StringBuilder sb;
|
|
|
|
sb << "Starting function '" << fn << "' at "
|
|
|
|
<< ctrlFlowGraph.size( );
|
|
|
|
return sb;
|
|
|
|
} , LL1 );
|
|
|
|
cfgFunctions.add( fn , T_BasicBlock{ ctrlFlowGraph.size( ) } );
|
|
|
|
cNode = M_ADDNEW_( );
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All instructions: continue the current basic block
|
|
|
|
auto* const iptr{ dynamic_cast< A_InstructionNode* >( &node ) };
|
|
|
|
if ( iptr && !exit ) {
|
|
|
|
assert( cNode );
|
|
|
|
if ( cNode->instructions ) {
|
|
|
|
cNode->instructions->count ++;
|
|
|
|
} else {
|
|
|
|
cNode->instructions = T_BasicBlock{ indexOf( *iptr ) };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle conditionals
|
|
|
|
if ( nt == A_Node::OP_COND ) {
|
|
|
|
if ( exit ) {
|
|
|
|
auto& se{ stack.last( ) };
|
|
|
|
cNode = M_ADDNEW_( );
|
|
|
|
|
|
|
|
// Connect each case block to both the condition
|
|
|
|
// and the next block
|
|
|
|
const auto ncb{ se.caseBlocks.size( ) };
|
|
|
|
for ( auto i = 0u ; i < ncb ; i ++ ) {
|
|
|
|
auto* cb{ se.caseBlocks[ i ] };
|
|
|
|
cb->inbound.add( se.condBlock );
|
|
|
|
se.condBlock->outbound.add( cb );
|
|
|
|
cb->outbound.add( cNode );
|
|
|
|
cNode->inbound.add( cb );
|
|
|
|
}
|
|
|
|
if ( !se.hasDefault ) {
|
|
|
|
cNode->inbound.add( se.condBlock );
|
|
|
|
se.condBlock->outbound.add( cNode );
|
|
|
|
}
|
|
|
|
|
|
|
|
stack.removeLast( );
|
|
|
|
logger( [&](){
|
|
|
|
T_StringBuilder sb;
|
|
|
|
sb << "Exiting conditional instruction, stack size "
|
|
|
|
<< stack.size( );
|
|
|
|
return sb;
|
|
|
|
} , LL2 );
|
|
|
|
} else {
|
|
|
|
auto& se{ stack.addNew( ) };
|
|
|
|
se.condBlock = cNode;
|
|
|
|
cNode = nullptr;
|
|
|
|
logger( [&](){
|
|
|
|
T_StringBuilder sb;
|
|
|
|
sb << "Entering conditional instruction, stack size "
|
|
|
|
<< stack.size( )
|
|
|
|
<< ", block had "
|
|
|
|
<< ( se.condBlock->instructions
|
|
|
|
? se.condBlock->instructions->count
|
|
|
|
: 0 )
|
|
|
|
<< " instructions";
|
|
|
|
return sb;
|
|
|
|
} , LL2 );
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calls also break the flow
|
|
|
|
if ( nt == A_Node::OP_CALL && !exit ) {
|
|
|
|
T_CallInstrNode& ci{ *dynamic_cast< T_CallInstrNode* >( iptr ) };
|
|
|
|
logger( [&](){
|
|
|
|
T_StringBuilder sb;
|
|
|
|
sb << "Call to " << ci.id( ) << ", block had "
|
|
|
|
<< ( cNode->instructions
|
|
|
|
? cNode->instructions->count
|
|
|
|
: 0 )
|
|
|
|
<< " instructions";
|
|
|
|
return sb;
|
|
|
|
} , LL2 );
|
|
|
|
|
|
|
|
auto& cs{ callSites.addNew( ) };
|
|
|
|
cs.name = ci.id( );
|
|
|
|
cs.callBlock = cNode;
|
|
|
|
cNode = cs.retBlock = M_ADDNEW_( );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Condition case nodes: create new basic block, add to stack's list
|
|
|
|
if ( nt == A_Node::TN_CASE || nt == A_Node::TN_DEFAULT ) {
|
|
|
|
if ( exit ) {
|
|
|
|
logger( [&](){
|
|
|
|
T_StringBuilder sb;
|
|
|
|
sb << "Case block added ("
|
|
|
|
<< ( cNode->instructions
|
|
|
|
? cNode->instructions->count
|
|
|
|
: 0 )
|
|
|
|
<< " instructions)";
|
|
|
|
return sb;
|
|
|
|
} , LL2 );
|
|
|
|
cNode = nullptr;
|
|
|
|
} else {
|
|
|
|
stack.last( ).hasDefault = stack.last( ).hasDefault
|
|
|
|
|| ( nt == A_Node::TN_DEFAULT );
|
|
|
|
cNode = M_ADDNEW_( );
|
|
|
|
stack.last( ).caseBlocks.add( cNode );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return !dynamic_cast< A_ExpressionNode* >( &node );
|
|
|
|
} );
|
|
|
|
assert( cfgFunctions.contains( "*init*" ) && cfgFunctions.contains( "*frame*" ) );
|
|
|
|
|
|
|
|
// Add fake call sites for *init* and *frame*
|
|
|
|
{
|
|
|
|
auto& cs{ callSites.addNew( ) };
|
|
|
|
cs.callBlock = ctrlFlowGraph[ CFG_ENTER ].get( );
|
|
|
|
cs.retBlock = ctrlFlowGraph[ CFG_MAINLOOP ].get( );
|
|
|
|
cs.name = "*init*";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto& cs{ callSites.addNew( ) };
|
|
|
|
cs.callBlock = ctrlFlowGraph[ CFG_MAINLOOP ].get( );
|
|
|
|
cs.retBlock = ctrlFlowGraph[ CFG_MAINLOOP ].get( );
|
|
|
|
cs.name = "*frame*";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle calls
|
|
|
|
for ( auto const& cs : callSites ) {
|
|
|
|
auto const* frec{ cfgFunctions.get( cs.name ) };
|
|
|
|
assert( frec );
|
|
|
|
{
|
|
|
|
auto& entry{ ctrlFlowGraph[ frec->first ] };
|
|
|
|
entry->inbound.add( cs.callBlock );
|
|
|
|
cs.callBlock->outbound.add( entry.get( ) );
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto& exit{ ctrlFlowGraph[ frec->first + frec->count - 1 ] };
|
|
|
|
exit->outbound.add( cs.retBlock );
|
|
|
|
cs.retBlock->inbound.add( exit.get( ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-03 23:15:44 +01:00
|
|
|
#undef M_ADDNEW_
|
|
|
|
#undef M_NEWNODE_
|
|
|
|
|
|
|
|
|
|
|
|
/*= T_OptData - USE/DEFINE CHAINS ============================================*/
|
|
|
|
|
|
|
|
#warning Remove this later
|
|
|
|
#undef LL1
|
|
|
|
#undef LL2
|
|
|
|
#define LL1 1
|
|
|
|
#define LL2 1
|
|
|
|
|
|
|
|
void T_OptData::buildUseDefineChains(
|
|
|
|
T_OpsParserOutput& program ) noexcept
|
|
|
|
{
|
|
|
|
M_LOGSTR_( "Building use/define chains" , LL1 );
|
|
|
|
varUDChains.clear( );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We need to locate all variable uses. For each use, we will go
|
|
|
|
* backwards in the graph, until one of the following becomes true:
|
|
|
|
* - we find an instruction that sets the variable;
|
|
|
|
* - we find the initial block due to cycling back;
|
|
|
|
* - we reached the CFG_ENTER node.
|
|
|
|
* In the latter case, it's an error - the variable is never set.
|
|
|
|
*
|
|
|
|
* FIXME: Nope, actually that won't work. We need to find (set)
|
|
|
|
* instuctions, calls, and also resource initialisations as well
|
|
|
|
* when visiting the tree; *then* we can associate records from
|
|
|
|
* both lists.
|
|
|
|
*
|
|
|
|
* Note: for function arguments, most of this can be avoided by simply
|
|
|
|
* looking at the call sites.
|
|
|
|
*/
|
|
|
|
visitor.visit( program.root , [&]( auto& n , const bool exit ) {
|
|
|
|
if ( n.type( ) != A_Node::EXPR_ID || exit ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find instruction and function index
|
|
|
|
auto& nId{ dynamic_cast< T_IdentifierExprNode& >( n ) };
|
|
|
|
A_FuncNode* func{ nullptr };
|
|
|
|
T_Optional< uint32_t > instrId;
|
|
|
|
A_Node* pn{ &nId };
|
|
|
|
while ( pn ) {
|
|
|
|
auto* const asInstr{ dynamic_cast< A_InstructionNode* >( pn ) };
|
|
|
|
func = dynamic_cast< A_FuncNode* >( pn );
|
|
|
|
if ( !instrId && asInstr ) {
|
|
|
|
instrId = indexOf( *asInstr );
|
|
|
|
} else if ( func ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pn = &pn->parent( );
|
|
|
|
}
|
|
|
|
assert( func && instrId );
|
|
|
|
|
|
|
|
// Generate the identifier
|
|
|
|
const T_VarId varId{ [&]() {
|
|
|
|
auto const& n{ nId.id( ) };
|
|
|
|
if ( func->hasLocal( nId.id( ) ) ) {
|
|
|
|
return T_VarId{ n , func->name( ) ,
|
|
|
|
func->isArgument( nId.id( ) ) };
|
|
|
|
}
|
|
|
|
return T_VarId{ n };
|
|
|
|
} () };
|
|
|
|
|
|
|
|
// Access or create the record
|
|
|
|
auto* const varRec{ [&]() {
|
|
|
|
auto* const x{ varUDChains.get( varId ) };
|
|
|
|
if ( x ) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
varUDChains.add( T_VarUseDefine{ varId } );
|
|
|
|
return varUDChains.get( varId );
|
|
|
|
} () };
|
|
|
|
assert( varRec );
|
|
|
|
|
|
|
|
// Add use record
|
|
|
|
auto& useRec{ varRec->uses.addNew( ) };
|
|
|
|
useRec.node = *instrId;
|
|
|
|
useRec.fnIndex = program.root.functionIndex( func->name( ) );
|
|
|
|
|
|
|
|
logger( [&](){
|
|
|
|
T_StringBuilder sb;
|
|
|
|
sb << "use " << varId.name << " at " << nId.location( ) << " (";
|
|
|
|
if ( varId.type == E_UDVarType::GLOBAL ) {
|
|
|
|
sb << "global";
|
|
|
|
} else {
|
|
|
|
if ( varId.type == E_UDVarType::LOCAL ) {
|
|
|
|
sb << "local";
|
|
|
|
} else {
|
|
|
|
sb << "argument";
|
|
|
|
}
|
|
|
|
sb << " of " << varId.owner;
|
|
|
|
}
|
|
|
|
sb << ')';
|
|
|
|
return sb;
|
|
|
|
} , LL2 );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2017-12-03 18:35:56 +01:00
|
|
|
|
|
|
|
/*============================================================================*/
|
|
|
|
|
|
|
|
#undef M_LOGSTR_
|
|
|
|
#define M_LOGSTR_( S , L ) \
|
|
|
|
oData.logger( [](){ return T_StringBuilder{ S }; } , L )
|
|
|
|
|
|
|
|
|
2017-12-01 07:30:03 +01:00
|
|
|
/*= CONSTANT FOLDING =========================================================*/
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct T_ConstantFolder_
|
|
|
|
{
|
2017-12-02 10:07:14 +01:00
|
|
|
T_ConstantFolder_( T_OptData& data ) noexcept
|
|
|
|
: oData{ data }
|
|
|
|
{}
|
2017-12-01 07:30:03 +01:00
|
|
|
|
|
|
|
// Result
|
|
|
|
bool didFold{ false };
|
|
|
|
|
|
|
|
bool operator()( A_Node& node , bool exit ) noexcept;
|
|
|
|
|
|
|
|
private:
|
2017-12-02 10:07:14 +01:00
|
|
|
T_OptData& oData;
|
|
|
|
|
2017-12-01 22:10:36 +01:00
|
|
|
template<
|
|
|
|
typename T
|
|
|
|
> void handleParentNode(
|
2017-12-01 07:30:03 +01:00
|
|
|
A_Node& node ,
|
2017-12-01 22:10:36 +01:00
|
|
|
std::function< A_ExpressionNode&( T& ) > get ,
|
|
|
|
std::function< void( T& , P_ExpressionNode ) > set ) noexcept;
|
2017-12-01 07:30:03 +01:00
|
|
|
|
|
|
|
P_ExpressionNode checkExpression(
|
2017-12-01 22:10:36 +01:00
|
|
|
A_ExpressionNode& node ) noexcept;
|
|
|
|
|
|
|
|
// Handle identifiers. If the size is fixed and the identifier is
|
|
|
|
// either width or height, replace it with the appropriate value.
|
|
|
|
P_ExpressionNode doIdExpr(
|
|
|
|
T_IdentifierExprNode& node ) noexcept;
|
|
|
|
|
2017-12-02 10:40:47 +01:00
|
|
|
// Handle reads from inputs. If there's a curve and it is a constant,
|
|
|
|
// or if there's no curve and only one default value, then the
|
|
|
|
// expression is constant.
|
|
|
|
P_ExpressionNode doInputExpr(
|
|
|
|
T_InputExprNode& node ) noexcept;
|
|
|
|
|
2017-12-01 22:10:36 +01:00
|
|
|
// Transform an unary operator applied to a constant into a constant.
|
|
|
|
P_ExpressionNode doUnaryOp(
|
2017-12-02 10:07:14 +01:00
|
|
|
T_UnaryOperatorNode& node ,
|
2017-12-01 22:10:36 +01:00
|
|
|
double value ) const noexcept;
|
|
|
|
|
|
|
|
// Transform a binary operator applied to a constant into a constant.
|
|
|
|
P_ExpressionNode doBinaryOp(
|
2017-12-02 10:07:14 +01:00
|
|
|
T_BinaryOperatorNode& node ,
|
2017-12-01 22:10:36 +01:00
|
|
|
double left ,
|
|
|
|
double right ) const noexcept;
|
2017-12-01 07:30:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
bool T_ConstantFolder_::operator()(
|
|
|
|
A_Node& node ,
|
|
|
|
const bool exit ) noexcept
|
|
|
|
{
|
|
|
|
if ( exit ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( node.type( ) ) {
|
|
|
|
|
|
|
|
case A_Node::TN_ARG:
|
2017-12-01 22:10:36 +01:00
|
|
|
handleParentNode< T_ArgumentNode >(
|
|
|
|
node ,
|
|
|
|
[]( auto& n ) -> A_ExpressionNode& { return n.expression( ); } ,
|
|
|
|
[]( auto& n , P_ExpressionNode e ) { n.expression( std::move( e ) ); }
|
|
|
|
);
|
2017-12-01 07:30:03 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
case A_Node::TN_CONDITION:
|
2017-12-01 22:10:36 +01:00
|
|
|
handleParentNode< T_CondInstrNode::T_Expression >( node ,
|
|
|
|
[]( auto& n ) -> A_ExpressionNode& { return n.expression( ); } ,
|
|
|
|
[]( auto& n , P_ExpressionNode e ) { n.expression( std::move( e ) ); }
|
|
|
|
);
|
2017-12-01 07:30:03 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
case A_Node::OP_SET:
|
2017-12-01 22:10:36 +01:00
|
|
|
handleParentNode< T_SetInstrNode >( node ,
|
|
|
|
[]( auto& n ) -> A_ExpressionNode& { return n.expression( ); } ,
|
|
|
|
[]( auto& n , P_ExpressionNode e ) { n.setExpression( std::move( e ) ); } );
|
2017-12-01 07:30:03 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-01 22:10:36 +01:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
template<
|
|
|
|
typename T
|
|
|
|
> void T_ConstantFolder_::handleParentNode(
|
|
|
|
A_Node& n ,
|
|
|
|
std::function< A_ExpressionNode&( T& ) > get ,
|
|
|
|
std::function< void( T& , P_ExpressionNode ) > set ) noexcept
|
2017-12-01 07:30:03 +01:00
|
|
|
{
|
2017-12-01 22:10:36 +01:00
|
|
|
auto& node{ (T&) n };
|
2017-12-02 12:18:31 +01:00
|
|
|
auto& child{ get( node ) };
|
|
|
|
auto r{ checkExpression( child ) };
|
2017-12-01 07:30:03 +01:00
|
|
|
if ( r ) {
|
2017-12-02 12:18:31 +01:00
|
|
|
oData.logger( [&]() {
|
|
|
|
T_StringBuilder sb;
|
|
|
|
sb << "substituting node at " << child.location( );
|
|
|
|
return sb;
|
2017-12-03 13:05:54 +01:00
|
|
|
} , 3 );
|
2017-12-01 22:10:36 +01:00
|
|
|
r->location( ) = node.location( );
|
2017-12-01 07:30:03 +01:00
|
|
|
set( node , std::move( r ) );
|
|
|
|
didFold = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
P_ExpressionNode T_ConstantFolder_::checkExpression(
|
2017-12-01 22:10:36 +01:00
|
|
|
A_ExpressionNode& node ) noexcept
|
2017-12-01 07:30:03 +01:00
|
|
|
{
|
2017-12-01 22:10:36 +01:00
|
|
|
// Already a constant
|
|
|
|
if ( node.type( ) == A_Node::EXPR_CONST ) {
|
|
|
|
return {};
|
|
|
|
}
|
2017-12-01 07:30:03 +01:00
|
|
|
|
2017-12-01 22:10:36 +01:00
|
|
|
// Replace $width/$height with value if fixedSize
|
2017-12-01 07:30:03 +01:00
|
|
|
if ( node.type( ) == A_Node::EXPR_ID ) {
|
2017-12-01 22:10:36 +01:00
|
|
|
return doIdExpr( (T_IdentifierExprNode&) node );
|
|
|
|
}
|
2017-12-01 07:30:03 +01:00
|
|
|
|
2017-12-01 22:10:36 +01:00
|
|
|
// Replace inputs with value if no curve/constant curve
|
|
|
|
if ( node.type( ) == A_Node::EXPR_INPUT ) {
|
2017-12-02 10:40:47 +01:00
|
|
|
return doInputExpr( (T_InputExprNode&) node );
|
2017-12-01 22:10:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replace UnOp( Cnst ) with result
|
|
|
|
auto* const asUnary{ dynamic_cast< T_UnaryOperatorNode* >( &node ) };
|
|
|
|
if ( asUnary ) {
|
|
|
|
handleParentNode< T_UnaryOperatorNode >( *asUnary ,
|
|
|
|
[]( auto& n ) -> A_ExpressionNode& { return n.argument( ); } ,
|
|
|
|
[]( auto& n , P_ExpressionNode e ) { n.setArgument( std::move( e ) ); } );
|
|
|
|
if ( asUnary->argument( ).type( ) == A_Node::EXPR_CONST ) {
|
|
|
|
auto const& cn{ (T_ConstantExprNode const&) asUnary->argument( ) };
|
2017-12-02 10:07:14 +01:00
|
|
|
return doUnaryOp( *asUnary , cn.floatValue( ) );
|
2017-12-01 07:30:03 +01:00
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2017-12-01 22:10:36 +01:00
|
|
|
// Replace BinOp( Cnst , Cnst ) with result
|
|
|
|
auto* const asBinary{ dynamic_cast< T_BinaryOperatorNode* >( &node ) };
|
|
|
|
assert( asBinary && "Missing support for some expr subtype" );
|
|
|
|
handleParentNode< T_BinaryOperatorNode >( *asBinary ,
|
|
|
|
[]( auto& n ) -> A_ExpressionNode& { return n.left( ); } ,
|
|
|
|
[]( auto& n , P_ExpressionNode e ) { n.setLeft( std::move( e ) ); } );
|
|
|
|
handleParentNode< T_BinaryOperatorNode >( *asBinary ,
|
|
|
|
[]( auto& n ) -> A_ExpressionNode& { return n.right( ); } ,
|
|
|
|
[]( auto& n , P_ExpressionNode e ) { n.setRight( std::move( e ) ); } );
|
|
|
|
|
|
|
|
if ( asBinary->left( ).type( ) == A_Node::EXPR_CONST
|
|
|
|
&& asBinary->right( ).type( ) == A_Node::EXPR_CONST ) {
|
|
|
|
auto const& l{ (T_ConstantExprNode const&) asBinary->left( ) };
|
|
|
|
auto const& r{ (T_ConstantExprNode const&) asBinary->right( ) };
|
2017-12-02 10:07:14 +01:00
|
|
|
return doBinaryOp( *asBinary , l.floatValue( ) , r.floatValue( ) );
|
2017-12-01 22:10:36 +01:00
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
2017-12-02 10:40:47 +01:00
|
|
|
P_ExpressionNode T_ConstantFolder_::doInputExpr(
|
|
|
|
T_InputExprNode& node ) noexcept
|
|
|
|
{
|
|
|
|
if ( !oData.curves ) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const* const curve{ oData.curves->curves.get( node.id( ) ) };
|
|
|
|
if ( curve ) {
|
|
|
|
// Curve present, check if it's constant
|
|
|
|
const auto cval{ curve->isConstant( ) };
|
|
|
|
if ( !cval ) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return NewOwned< T_ConstantExprNode >( node.parent( ) , *cval );
|
|
|
|
}
|
|
|
|
|
|
|
|
assert( oData.inputDecls );
|
|
|
|
auto const* const dva{ oData.inputDecls->get( node.id( ) ) };
|
|
|
|
assert( dva );
|
|
|
|
if ( dva->size( ) == 1 ) {
|
|
|
|
// If there's only one default value, that's a constant.
|
|
|
|
return NewOwned< T_ConstantExprNode >( node.parent( ) ,
|
|
|
|
(*dva)[ 0 ].value );
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2017-12-01 22:10:36 +01:00
|
|
|
P_ExpressionNode T_ConstantFolder_::doIdExpr(
|
|
|
|
T_IdentifierExprNode& node ) noexcept
|
|
|
|
{
|
2017-12-02 10:07:14 +01:00
|
|
|
if ( !oData.fixedSize ) {
|
2017-12-01 22:10:36 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( node.id( ) == "width" ) {
|
2017-12-03 13:05:54 +01:00
|
|
|
M_LOGSTR_( "replacing $width with fixed width" , 3 );
|
2017-12-01 22:10:36 +01:00
|
|
|
return NewOwned< T_ConstantExprNode >( node.parent( ) ,
|
2017-12-02 10:07:14 +01:00
|
|
|
double( oData.fixedSize->first ) );
|
2017-12-01 22:10:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( node.id( ) == "height" ) {
|
2017-12-03 13:05:54 +01:00
|
|
|
M_LOGSTR_( "replacing $height with fixed height" , 3 );
|
2017-12-01 22:10:36 +01:00
|
|
|
return NewOwned< T_ConstantExprNode >( node.parent( ) ,
|
2017-12-02 10:07:14 +01:00
|
|
|
float( oData.fixedSize->second ) );
|
2017-12-01 22:10:36 +01:00
|
|
|
}
|
|
|
|
|
2017-12-01 07:30:03 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2017-12-01 22:10:36 +01:00
|
|
|
P_ExpressionNode T_ConstantFolder_::doUnaryOp(
|
2017-12-02 10:07:14 +01:00
|
|
|
T_UnaryOperatorNode& node ,
|
2017-12-01 22:10:36 +01:00
|
|
|
const double value ) const noexcept
|
|
|
|
{
|
2017-12-02 10:07:14 +01:00
|
|
|
const double rVal{ [this]( auto& node , const auto value ) {
|
|
|
|
switch ( node.op( ) ) {
|
|
|
|
|
2017-12-01 22:10:36 +01:00
|
|
|
case T_UnaryOperatorNode::NEG:
|
|
|
|
return -value;
|
2017-12-02 10:07:14 +01:00
|
|
|
|
2017-12-01 22:10:36 +01:00
|
|
|
case T_UnaryOperatorNode::NOT:
|
|
|
|
return value ? 0. : 1.;
|
2017-12-02 10:07:14 +01:00
|
|
|
|
2017-12-01 22:10:36 +01:00
|
|
|
case T_UnaryOperatorNode::INV:
|
2017-12-02 10:07:14 +01:00
|
|
|
if ( value == 0 ) {
|
|
|
|
oData.errors.addNew( "math - 1/x, x=0" , node.location( ) );
|
|
|
|
return 0.;
|
|
|
|
}
|
2017-12-01 22:10:36 +01:00
|
|
|
return 1. / value;
|
2017-12-02 10:07:14 +01:00
|
|
|
|
2017-12-01 22:10:36 +01:00
|
|
|
case T_UnaryOperatorNode::COS:
|
|
|
|
return cos( value );
|
2017-12-02 10:07:14 +01:00
|
|
|
|
2017-12-01 22:10:36 +01:00
|
|
|
case T_UnaryOperatorNode::SIN:
|
|
|
|
return sin( value );
|
2017-12-02 10:07:14 +01:00
|
|
|
|
2017-12-01 22:10:36 +01:00
|
|
|
case T_UnaryOperatorNode::TAN:
|
2017-12-02 10:07:14 +01:00
|
|
|
if ( fabs( value - M_PI / 2 ) <= 1e-6 ) {
|
|
|
|
oData.errors.addNew( "math - tan(x), x=~PI/2" ,
|
|
|
|
node.location( ) , E_SRDErrorType::WARNING );
|
|
|
|
}
|
2017-12-01 22:10:36 +01:00
|
|
|
return tan( value );
|
2017-12-02 10:07:14 +01:00
|
|
|
|
2017-12-01 22:10:36 +01:00
|
|
|
case T_UnaryOperatorNode::SQRT:
|
2017-12-02 10:07:14 +01:00
|
|
|
if ( value < 0 ) {
|
|
|
|
oData.errors.addNew( "math - sqrt(x), x<0" , node.location( ) );
|
|
|
|
return 0.;
|
|
|
|
}
|
2017-12-01 22:10:36 +01:00
|
|
|
return sqrt( value );
|
2017-12-02 10:07:14 +01:00
|
|
|
|
2017-12-01 22:10:36 +01:00
|
|
|
case T_UnaryOperatorNode::LN:
|
2017-12-02 10:07:14 +01:00
|
|
|
if ( value <= 0 ) {
|
|
|
|
oData.errors.addNew( "math - ln(x), x<=0" , node.location( ) );
|
|
|
|
return 0.;
|
|
|
|
}
|
2017-12-01 22:10:36 +01:00
|
|
|
return log( value );
|
2017-12-02 10:07:14 +01:00
|
|
|
|
2017-12-01 22:10:36 +01:00
|
|
|
case T_UnaryOperatorNode::EXP:
|
|
|
|
return exp( value );
|
|
|
|
}
|
2017-12-02 10:07:14 +01:00
|
|
|
|
|
|
|
fprintf( stderr , "invalid operator %d\n" , int( node.op( ) ) );
|
2017-12-01 22:10:36 +01:00
|
|
|
std::abort( );
|
2017-12-02 10:07:14 +01:00
|
|
|
}( node , value ) };
|
2017-12-01 22:10:36 +01:00
|
|
|
|
2017-12-02 10:07:14 +01:00
|
|
|
return NewOwned< T_ConstantExprNode >( node.parent( ) , rVal );
|
2017-12-01 22:10:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
P_ExpressionNode T_ConstantFolder_::doBinaryOp(
|
2017-12-02 10:07:14 +01:00
|
|
|
T_BinaryOperatorNode& node ,
|
2017-12-01 22:10:36 +01:00
|
|
|
const double left ,
|
|
|
|
const double right ) const noexcept
|
|
|
|
{
|
2017-12-02 10:07:14 +01:00
|
|
|
const double rVal{ [this]( auto& node , const auto l , const auto r ) {
|
|
|
|
switch ( node.op( ) ) {
|
|
|
|
|
2017-12-01 22:10:36 +01:00
|
|
|
case T_BinaryOperatorNode::ADD:
|
|
|
|
return l + r;
|
2017-12-02 10:07:14 +01:00
|
|
|
|
2017-12-01 22:10:36 +01:00
|
|
|
case T_BinaryOperatorNode::SUB:
|
|
|
|
return l - r;
|
2017-12-02 10:07:14 +01:00
|
|
|
|
2017-12-01 22:10:36 +01:00
|
|
|
case T_BinaryOperatorNode::MUL:
|
|
|
|
return l * r;
|
2017-12-02 10:07:14 +01:00
|
|
|
|
2017-12-01 22:10:36 +01:00
|
|
|
case T_BinaryOperatorNode::DIV:
|
2017-12-02 10:07:14 +01:00
|
|
|
if ( r == 0 ) {
|
|
|
|
oData.errors.addNew( "math - l/r, r=0" , node.location( ) );
|
|
|
|
return 0.;
|
|
|
|
}
|
2017-12-01 22:10:36 +01:00
|
|
|
return l / r;
|
2017-12-02 10:07:14 +01:00
|
|
|
|
2017-12-01 22:10:36 +01:00
|
|
|
case T_BinaryOperatorNode::POW:
|
2017-12-02 10:07:14 +01:00
|
|
|
if ( l == 0 && r == 0 ) {
|
|
|
|
oData.errors.addNew( "math - l^r, l=r=0" , node.location( ) );
|
|
|
|
return 0.;
|
|
|
|
}
|
|
|
|
if ( l == 0 && r < 0 ) {
|
|
|
|
oData.errors.addNew( "math - l^r, l=0, r<0" , node.location( ) );
|
|
|
|
return 0.;
|
|
|
|
}
|
|
|
|
if ( l < 0 && fmod( r , 1. ) != 0. ) {
|
|
|
|
oData.errors.addNew( "math - l^r, l<0, r not integer" , node.location( ) );
|
|
|
|
return 0.;
|
|
|
|
}
|
2017-12-01 22:10:36 +01:00
|
|
|
return pow( l , r );
|
2017-12-02 10:07:14 +01:00
|
|
|
|
|
|
|
case T_BinaryOperatorNode::CMP_EQ: return ( l == r ) ? 1. : 0.;
|
|
|
|
case T_BinaryOperatorNode::CMP_NE: return ( l != r ) ? 1. : 0.;
|
|
|
|
case T_BinaryOperatorNode::CMP_GT: return ( l > r ) ? 1. : 0.;
|
|
|
|
case T_BinaryOperatorNode::CMP_GE: return ( l >= r ) ? 1. : 0.;
|
|
|
|
case T_BinaryOperatorNode::CMP_LT: return ( l < r ) ? 1. : 0.;
|
|
|
|
case T_BinaryOperatorNode::CMP_LE: return ( l <= r ) ? 1. : 0.;
|
2017-12-01 22:10:36 +01:00
|
|
|
}
|
2017-12-02 10:07:14 +01:00
|
|
|
fprintf( stderr , "invalid operator %d\n" , int( node.op( ) ) );
|
2017-12-01 22:10:36 +01:00
|
|
|
std::abort( );
|
2017-12-02 10:07:14 +01:00
|
|
|
}( node , left , right ) };
|
2017-12-01 22:10:36 +01:00
|
|
|
|
2017-12-02 10:07:14 +01:00
|
|
|
return NewOwned< T_ConstantExprNode >( node.parent( ) , rVal );
|
2017-12-01 22:10:36 +01:00
|
|
|
}
|
|
|
|
|
2017-12-01 07:30:03 +01:00
|
|
|
} // namespace <anon>
|
|
|
|
|
2017-12-01 22:10:36 +01:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
2017-12-01 07:30:03 +01:00
|
|
|
|
|
|
|
bool opopt::FoldConstants(
|
2017-12-02 10:07:14 +01:00
|
|
|
T_OpsParserOutput& program ,
|
|
|
|
T_OptData& oData ) noexcept
|
2017-12-01 07:30:03 +01:00
|
|
|
{
|
2017-12-02 10:07:14 +01:00
|
|
|
T_ConstantFolder_ folder{ oData };
|
2017-12-03 13:05:54 +01:00
|
|
|
M_LOGSTR_( "... Folding constants" , 2 );
|
2017-12-02 10:40:47 +01:00
|
|
|
if ( oData.curves ) {
|
|
|
|
oData.findInputDecls( program );
|
|
|
|
}
|
2017-12-02 12:18:31 +01:00
|
|
|
oData.visitor.visit( program.root , [&]( auto& n , auto x ) {
|
|
|
|
return folder( n , x );
|
|
|
|
} );
|
|
|
|
oData.logger( [&]() {
|
2017-12-03 13:05:54 +01:00
|
|
|
T_StringBuilder sb{ "...... " };
|
|
|
|
sb << ( folder.didFold
|
|
|
|
? "Some constants were folded"
|
|
|
|
: "No constants were folded" );
|
|
|
|
return sb;
|
|
|
|
} , 2 );
|
2017-12-01 07:30:03 +01:00
|
|
|
return folder.didFold;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-03 14:59:39 +01:00
|
|
|
/*= CONSTANT PROPAGATION =====================================================*/
|
|
|
|
|
|
|
|
bool opopt::PropagateConstants(
|
|
|
|
T_OpsParserOutput& program ,
|
2017-12-03 18:35:56 +01:00
|
|
|
T_OptData& oData ) noexcept
|
2017-12-03 14:59:39 +01:00
|
|
|
{
|
|
|
|
// We need to follow the general execution flow of the program. This is
|
|
|
|
// not as straightforward as it seems.
|
|
|
|
// - Handling locals is rather easy as they "die" at the end of
|
|
|
|
// the function in which they are defined.
|
|
|
|
// - Handling variables in init and functions that are called only
|
|
|
|
// from init is not too hard: once a variable is set to a constant, it
|
|
|
|
// can be substituted until the next set instruction.
|
|
|
|
// - Other variables need additional checks before propagating.
|
|
|
|
// For example, if a variable is set to a constant during init, but is
|
|
|
|
// updated at the end of the frame function, the value cannot be
|
|
|
|
// propagated.
|
2017-12-03 18:35:56 +01:00
|
|
|
|
|
|
|
oData.numberInstructions( program );
|
|
|
|
oData.buildControlFlowGraph( program );
|
2017-12-03 23:15:44 +01:00
|
|
|
oData.buildUseDefineChains( program );
|
2017-12-03 18:35:56 +01:00
|
|
|
M_LOGSTR_( "... Propagating constants" , 2 );
|
|
|
|
|
2017-12-03 14:59:39 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-01 07:30:03 +01:00
|
|
|
/*= DEAD CODE REMOVAL ========================================================*/
|
|
|
|
|
2017-12-02 10:07:14 +01:00
|
|
|
bool opopt::RemoveDeadCode(
|
|
|
|
T_OpsParserOutput& program ,
|
|
|
|
T_OptData& oData ) noexcept
|
2017-12-01 07:30:03 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|