Optimizer - Working constant propagation
AST's replace function needed to be recursive
This commit is contained in:
parent
223dd1763d
commit
12f9402c94
4 changed files with 16 additions and 8 deletions
12
c-opast.cc
12
c-opast.cc
|
@ -32,18 +32,24 @@ T_RootNode& A_Node::root( ) const noexcept
|
||||||
const_cast< A_Node* >( node ) );
|
const_cast< A_Node* >( node ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
void A_Node::replace(
|
bool A_Node::replace(
|
||||||
A_Node const& node ,
|
A_Node const& node ,
|
||||||
T_OwnPtr< A_Node > replacement
|
T_OwnPtr< A_Node >& replacement
|
||||||
) noexcept
|
) noexcept
|
||||||
{
|
{
|
||||||
const auto nc{ size( ) };
|
const auto nc{ size( ) };
|
||||||
for ( auto i = 0u ; i < nc ; i ++ ) {
|
for ( auto i = 0u ; i < nc ; i ++ ) {
|
||||||
if ( child( i ).get( ) == &node ) {
|
if ( child( i ).get( ) == &node ) {
|
||||||
child( i ) = std::move( replacement );
|
child( i ) = std::move( replacement );
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for ( auto i = 0u ; i < nc ; i ++ ) {
|
||||||
|
if ( child( i ) && child( i )->replace( node , replacement ) ) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*----------------------------------------------------------------------------*/
|
/*----------------------------------------------------------------------------*/
|
||||||
|
|
|
@ -107,8 +107,8 @@ class A_Node
|
||||||
const uint32_t index ) noexcept
|
const uint32_t index ) noexcept
|
||||||
{ return children_[ index ]; }
|
{ return children_[ index ]; }
|
||||||
|
|
||||||
void replace( A_Node const& node ,
|
bool replace( A_Node const& node ,
|
||||||
T_OwnPtr< A_Node > replacement ) noexcept;
|
T_OwnPtr< A_Node >& replacement ) noexcept;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Browser function to be used with T_Visitor
|
// Browser function to be used with T_Visitor
|
||||||
|
|
|
@ -1461,9 +1461,11 @@ void CPReplaceWithConstant_(
|
||||||
}
|
}
|
||||||
|
|
||||||
auto& p{ eid.parent( ) };
|
auto& p{ eid.parent( ) };
|
||||||
auto replacement{ NewOwned< T_ConstantExprNode >( p , value ) };
|
T_OwnPtr< A_Node > replacement{
|
||||||
|
NewOwned< T_ConstantExprNode >( p , value )
|
||||||
|
};
|
||||||
replacement->location( ) = eid.location( );
|
replacement->location( ) = eid.location( );
|
||||||
p.replace( eid , std::move( replacement ) );
|
p.replace( eid , replacement );
|
||||||
oData.logger( [&]() {
|
oData.logger( [&]() {
|
||||||
T_StringBuilder sb;
|
T_StringBuilder sb;
|
||||||
sb << "Propagated constant from " << var.name
|
sb << "Propagated constant from " << var.name
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
)
|
)
|
||||||
(optimizer on
|
(optimizer on
|
||||||
(constant-folding on
|
(constant-folding on
|
||||||
# (fixed-resolution on)
|
(fixed-resolution on)
|
||||||
)
|
)
|
||||||
(constant-propagation on)
|
(constant-propagation on)
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue