-
Notifications
You must be signed in to change notification settings - Fork 273
Move definition of can-be-constant-propagated to a central location #2221
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ Author: Daniel Kroening, [email protected] | |
|
||
#include <util/base_exceptions.h> | ||
#include <util/exception_utils.h> | ||
#include <util/expr_util.h> | ||
#include <util/invariant.h> | ||
#include <util/prefix.h> | ||
#include <util/std_expr.h> | ||
|
@@ -79,122 +80,6 @@ void goto_symex_statet::level1t::operator()(ssa_exprt &ssa_expr) | |
ssa_expr.set_level_1(it->second.second); | ||
} | ||
|
||
/// This function determines what expressions are to be propagated as | ||
/// "constants" | ||
bool goto_symex_statet::constant_propagation(const exprt &expr) const | ||
{ | ||
if(expr.is_constant()) | ||
return true; | ||
|
||
if(expr.id()==ID_address_of) | ||
{ | ||
const address_of_exprt &address_of_expr=to_address_of_expr(expr); | ||
|
||
return constant_propagation_reference(address_of_expr.object()); | ||
} | ||
else if(expr.id()==ID_typecast) | ||
{ | ||
const typecast_exprt &typecast_expr=to_typecast_expr(expr); | ||
|
||
return constant_propagation(typecast_expr.op()); | ||
} | ||
else if(expr.id()==ID_plus) | ||
{ | ||
forall_operands(it, expr) | ||
if(!constant_propagation(*it)) | ||
return false; | ||
|
||
return true; | ||
} | ||
else if(expr.id()==ID_mult) | ||
{ | ||
// propagate stuff with sizeof in it | ||
forall_operands(it, expr) | ||
{ | ||
if(it->find(ID_C_c_sizeof_type).is_not_nil()) | ||
return true; | ||
else if(!constant_propagation(*it)) | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
else if(expr.id()==ID_array) | ||
{ | ||
forall_operands(it, expr) | ||
if(!constant_propagation(*it)) | ||
return false; | ||
|
||
return true; | ||
} | ||
else if(expr.id()==ID_array_of) | ||
{ | ||
return constant_propagation(expr.op0()); | ||
} | ||
else if(expr.id()==ID_with) | ||
{ | ||
// this is bad | ||
/* | ||
forall_operands(it, expr) | ||
if(!constant_propagation(expr.op0())) | ||
return false; | ||
|
||
return true; | ||
*/ | ||
return false; | ||
} | ||
else if(expr.id()==ID_struct) | ||
{ | ||
forall_operands(it, expr) | ||
if(!constant_propagation(*it)) | ||
return false; | ||
|
||
return true; | ||
} | ||
else if(expr.id()==ID_union) | ||
{ | ||
forall_operands(it, expr) | ||
if(!constant_propagation(*it)) | ||
return false; | ||
|
||
return true; | ||
} | ||
// byte_update works, byte_extract may be out-of-bounds | ||
else if(expr.id()==ID_byte_update_big_endian || | ||
expr.id()==ID_byte_update_little_endian) | ||
{ | ||
forall_operands(it, expr) | ||
if(!constant_propagation(*it)) | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/// this function determines which reference-typed expressions are constant | ||
bool goto_symex_statet::constant_propagation_reference(const exprt &expr) const | ||
{ | ||
if(expr.id()==ID_symbol) | ||
return true; | ||
else if(expr.id()==ID_index) | ||
{ | ||
const index_exprt &index_expr=to_index_expr(expr); | ||
|
||
return constant_propagation_reference(index_expr.array()) && | ||
constant_propagation(index_expr.index()); | ||
} | ||
else if(expr.id()==ID_member) | ||
{ | ||
return constant_propagation_reference(to_member_expr(expr).compound()); | ||
} | ||
else if(expr.id()==ID_string_constant) | ||
return true; | ||
|
||
return false; | ||
} | ||
|
||
/// write to a variable | ||
static bool check_renaming(const exprt &expr); | ||
|
||
|
@@ -297,6 +182,41 @@ static void assert_l2_renaming(const exprt &expr) | |
#endif | ||
} | ||
|
||
class goto_symex_is_constantt : public is_constantt | ||
{ | ||
protected: | ||
bool is_constant(const exprt &expr) const override | ||
{ | ||
if(expr.id() == ID_mult) | ||
{ | ||
// propagate stuff with sizeof in it | ||
forall_operands(it, expr) | ||
{ | ||
if(it->find(ID_C_c_sizeof_type).is_not_nil()) | ||
return true; | ||
else if(!is_constant(*it)) | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
else if(expr.id() == ID_with) | ||
{ | ||
// this is bad | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is probably not your code, but it would be good to give an idea of 'what didn't work here' in place of a commented piece of code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, let me try to dig this up. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (But it might take a while to do so.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Throughout the back-end there are hints that the array theory used to do Strange Things. At one point I (tried to) patch a rewrite that converted with to ITE. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #3095 for further discussion. |
||
/* | ||
forall_operands(it, expr) | ||
if(!is_constant(expr.op0())) | ||
return false; | ||
|
||
return true; | ||
*/ | ||
return false; | ||
} | ||
|
||
return is_constantt::is_constant(expr); | ||
} | ||
}; | ||
|
||
void goto_symex_statet::assignment( | ||
ssa_exprt &lhs, // L0/L1 | ||
const exprt &rhs, // L2 | ||
|
@@ -341,7 +261,7 @@ void goto_symex_statet::assignment( | |
|
||
// for value propagation -- the RHS is L2 | ||
|
||
if(!is_shared && record_value && constant_propagation(rhs)) | ||
if(!is_shared && record_value && goto_symex_is_constantt()(rhs)) | ||
propagation.values[l1_identifier]=rhs; | ||
else | ||
propagation.remove(l1_identifier); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,10 +6,11 @@ Author: Daniel Kroening, [email protected] | |
\*******************************************************************/ | ||
|
||
|
||
#include "expr_util.h" | ||
|
||
#include <algorithm> | ||
#include <unordered_set> | ||
|
||
#include "expr.h" | ||
#include "expr_iterator.h" | ||
#include "fixedbv.h" | ||
|
@@ -222,3 +223,55 @@ const exprt &skip_typecast(const exprt &expr) | |
|
||
return skip_typecast(to_typecast_expr(expr).op()); | ||
} | ||
|
||
/// This function determines what expressions are to be propagated as | ||
/// "constants" | ||
bool is_constantt::is_constant(const exprt &expr) const | ||
{ | ||
if(expr.is_constant()) | ||
return true; | ||
|
||
if(expr.id() == ID_address_of) | ||
{ | ||
return is_constant_address_of(to_address_of_expr(expr).object()); | ||
} | ||
else if( | ||
expr.id() == ID_typecast || expr.id() == ID_array_of || | ||
expr.id() == ID_plus || expr.id() == ID_mult || expr.id() == ID_array || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about divide, floating-point ops, etc. Do we have any exprt's that cannot be constant folded if all arguments are constant? If we do; should we? I realise that constant folding and expression forwarding in symex is more involved than just "is it constant" but... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #3095 for further discussion. |
||
expr.id() == ID_with || expr.id() == ID_struct || expr.id() == ID_union || | ||
// byte_update works, byte_extract may be out-of-bounds | ||
expr.id() == ID_byte_update_big_endian || | ||
expr.id() == ID_byte_update_little_endian) | ||
{ | ||
return std::all_of( | ||
expr.operands().begin(), expr.operands().end(), [this](const exprt &e) { | ||
return is_constant(e); | ||
}); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/// this function determines which reference-typed expressions are constant | ||
bool is_constantt::is_constant_address_of(const exprt &expr) const | ||
{ | ||
if(expr.id() == ID_symbol) | ||
{ | ||
return true; | ||
} | ||
else if(expr.id() == ID_index) | ||
{ | ||
const index_exprt &index_expr = to_index_expr(expr); | ||
|
||
return is_constant_address_of(index_expr.array()) && | ||
is_constant(index_expr.index()); | ||
} | ||
else if(expr.id() == ID_member) | ||
{ | ||
return is_constant_address_of(to_member_expr(expr).compound()); | ||
} | ||
else if(expr.id() == ID_string_constant) | ||
return true; | ||
|
||
return false; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this only the case for multiply?
(Again, I realise that this is not new code.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#3095 for further discussion.