11/* ******************************************************************\
22
3- Module: Constant Propagation
3+ Module: Constant propagation
44
55Author: Peter Schrammel
66
@@ -18,6 +18,7 @@ Author: Peter Schrammel
1818#include < util/find_symbols.h>
1919#include < util/arith_tools.h>
2020#include < util/simplify_expr.h>
21+ #include < util/cprover_prefix.h>
2122
2223void constant_propagator_domaint::assign_rec (
2324 valuest &values,
@@ -32,7 +33,7 @@ void constant_propagator_domaint::assign_rec(
3233
3334 exprt tmp=rhs;
3435 values.replace_const (tmp);
35- tmp= simplify_expr (tmp, ns);
36+ simplify (tmp, ns);
3637
3738 if (tmp.is_constant ())
3839 values.set_to (s, tmp);
@@ -122,13 +123,13 @@ void constant_propagator_domaint::transform(
122123
123124 if (to==next)
124125 {
125- if (id==" __CPROVER_set_must " ||
126- id==" __CPROVER_get_must " ||
127- id==" __CPROVER_set_may " ||
128- id==" __CPROVER_get_may " ||
129- id==" __CPROVER_cleanup " ||
130- id==" __CPROVER_clear_may " ||
131- id==" __CPROVER_clear_must " )
126+ if (id==CPROVER_PREFIX " set_must " ||
127+ id==CPROVER_PREFIX " get_must " ||
128+ id==CPROVER_PREFIX " set_may " ||
129+ id==CPROVER_PREFIX " get_may " ||
130+ id==CPROVER_PREFIX " cleanup " ||
131+ id==CPROVER_PREFIX " clear_may " ||
132+ id==CPROVER_PREFIX " clear_must " )
132133 {
133134 // no effect on constants
134135 }
@@ -149,17 +150,19 @@ void constant_propagator_domaint::transform(
149150 const code_typet &code_type=to_code_type (symbol.type );
150151 const code_typet::parameterst ¶meters=code_type.parameters ();
151152
152- const code_function_callt::argumentst &arguments
153- = function_call.arguments ();
153+ const code_function_callt::argumentst &arguments=
154+ function_call.arguments ();
154155
155- unsigned n=std::min (arguments.size (), parameters.size ());
156-
157- for (unsigned i=0 ; i<n; i++)
156+ code_typet::parameterst::const_iterator p_it=parameters.begin ();
157+ for (const auto &arg : arguments)
158158 {
159- const symbol_exprt ¶meter_expr
160- =symbol_exprt (parameters[i].get_identifier (), arguments[i].type ());
159+ if (p_it==parameters.end ())
160+ break ;
161+
162+ const symbol_exprt parameter_expr (p_it->get_identifier (), arg.type ());
163+ assign_rec (values, parameter_expr, arg, ns);
161164
162- assign_rec (values, parameter_expr, arguments[i], ns) ;
165+ ++p_it ;
163166 }
164167 }
165168 }
@@ -183,18 +186,8 @@ void constant_propagator_domaint::transform(
183186
184187 const code_typet &type=to_code_type (symbol.type );
185188
186- typedef code_typet::parameterst parameterst;
187- const parameterst ¶meters=type.parameters ();
188-
189- for (parameterst::const_iterator it=parameters.begin ();
190- it!=parameters.end (); it++)
191- {
192- // normal parameter
193- const irep_idt par=it->get_identifier ();
194-
195- // this erases the parameter from the map
196- values.set_to_top (par);
197- }
189+ for (const auto ¶m : type.parameters ())
190+ values.set_to_top (param.get_identifier ());
198191 }
199192
200193 INVARIANT (from->is_goto () || !values.is_bottom ,
@@ -218,6 +211,7 @@ bool constant_propagator_domaint::two_way_propagate_rec(
218211
219212 bool change=false ;
220213
214+ // this seems to be buggy at present
221215#if 0
222216 if(expr.id()==ID_and)
223217 {
@@ -318,25 +312,20 @@ bool constant_propagator_domaint::valuest::is_constant_address_of(
318312// / Do not call this when iterating over replace_const.expr_map!
319313bool constant_propagator_domaint::valuest::set_to_top (const irep_idt &id)
320314{
321- replace_symbolt::expr_mapt::iterator r_it
322- = replace_const.expr_map .find (id);
315+ replace_symbolt::expr_mapt::size_type n_erased=
316+ replace_const.expr_map .erase (id);
323317
324- if (r_it!=replace_const.expr_map .end ())
325- {
326- assert (!is_bottom);
327- replace_const.expr_map .erase (r_it);
328- return true ;
329- }
318+ INVARIANT (n_erased==0 || !is_bottom, " bottom => 0 erased" );
330319
331- return false ;
320+ return n_erased> 0 ;
332321}
333322
334323
335324void constant_propagator_domaint::valuest::set_dirty_to_top (
336325 const dirtyt &dirty,
337326 const namespacet &ns)
338327{
339- typedef replace_symbol_extt ::expr_mapt expr_mapt;
328+ typedef replace_symbolt ::expr_mapt expr_mapt;
340329 expr_mapt &expr_map=replace_const.expr_map ;
341330
342331 for (expr_mapt::iterator it=expr_map.begin ();
@@ -375,12 +364,9 @@ void constant_propagator_domaint::valuest::output(
375364 return ;
376365 }
377366
378- for (replace_symbolt::expr_mapt::const_iterator
379- it=replace_const.expr_map .begin ();
380- it!=replace_const.expr_map .end ();
381- ++it)
367+ for (const auto &p : replace_const.expr_map )
382368 {
383- out << ' ' << it-> first << " =" << from_expr (ns, " " , it-> second ) << ' \n ' ;
369+ out << ' ' << p. first << " =" << from_expr (ns, " " , p. second ) << ' \n ' ;
384370 }
385371}
386372
@@ -396,10 +382,6 @@ void constant_propagator_domaint::output(
396382// / \return Return true if "this" has changed.
397383bool constant_propagator_domaint::valuest::merge (const valuest &src)
398384{
399- // dummy
400- const symbol_tablet symbol_table;
401- const namespacet ns (symbol_table);
402-
403385 // nothing to do
404386 if (src.is_bottom )
405387 return false ;
@@ -417,8 +399,8 @@ bool constant_propagator_domaint::valuest::merge(const valuest &src)
417399
418400 bool changed=false ;
419401
420- replace_symbol_extt ::expr_mapt &expr_map=replace_const.expr_map ;
421- const replace_symbol_extt ::expr_mapt &src_expr_map=src.replace_const .expr_map ;
402+ replace_symbolt ::expr_mapt &expr_map=replace_const.expr_map ;
403+ const replace_symbolt ::expr_mapt &src_expr_map=src.replace_const .expr_map ;
422404
423405 // handle top
424406 if (src_expr_map.empty ())
@@ -427,8 +409,6 @@ bool constant_propagator_domaint::valuest::merge(const valuest &src)
427409 changed=!expr_map.empty ();
428410
429411 set_to_top ();
430- assert (expr_map.empty ());
431- assert (!is_bottom);
432412
433413 return changed;
434414 }
@@ -475,29 +455,26 @@ bool constant_propagator_domaint::valuest::meet(const valuest &src)
475455 if (src.is_bottom || is_bottom)
476456 return false ;
477457
478- bool changed = false ;
458+ bool changed= false ;
479459
480- for (replace_symbolt::expr_mapt::const_iterator
481- it=src.replace_const .expr_map .begin ();
482- it!=src.replace_const .expr_map .end ();
483- ++it)
460+ for (const auto &m : src.replace_const .expr_map )
484461 {
485462 replace_symbolt::expr_mapt::iterator
486- c_it = replace_const.expr_map .find (it-> first );
463+ c_it= replace_const.expr_map .find (m. first );
487464
488465 if (c_it!=replace_const.expr_map .end ())
489466 {
490- if (c_it->second !=it-> second )
467+ if (c_it->second !=m. second )
491468 {
492469 set_to_bottom ();
493- changed = true ;
470+ changed= true ;
494471 break ;
495472 }
496473 }
497474 else
498475 {
499- set_to (it-> first , it-> second );
500- changed = true ;
476+ set_to (m. first , m. second );
477+ changed= true ;
501478 }
502479 }
503480
@@ -510,31 +487,7 @@ bool constant_propagator_domaint::merge(
510487 locationt from,
511488 locationt to)
512489{
513- const symbol_tablet symbol_table;
514- const namespacet ns (symbol_table);
515-
516- #if 0
517- if(to->is_skip())
518- {
519- std::cout << "This:\n";
520- values.output(std::cout, ns);
521- std::cout << "Other:\n";
522- other.values.output(std::cout, ns);
523- }
524- #endif
525-
526- bool b;
527- b=values.merge (other.values );
528-
529- #if 0
530- if(to->is_skip())
531- {
532- std::cout << "Merge result:\n";
533- values.output(std::cout, ns);
534- }
535- #endif
536-
537- return b;
490+ return values.merge (other.values );
538491}
539492
540493void constant_propagator_ait::replace (
@@ -563,21 +516,22 @@ void constant_propagator_ait::replace(
563516 if (it->is_goto () || it->is_assume () || it->is_assert ())
564517 {
565518 s_it->second .values .replace_const (it->guard );
566- it-> guard = simplify_expr (it->guard , ns);
519+ simplify (it->guard , ns);
567520 }
568521 else if (it->is_assign ())
569522 {
570523 exprt &rhs=to_code_assign (it->code ).rhs ();
571524 s_it->second .values .replace_const (rhs);
572- rhs= simplify_expr (rhs, ns);
525+ simplify (rhs, ns);
573526 if (rhs.id ()==ID_constant)
574527 rhs.add_source_location ()=it->code .op0 ().source_location ();
575528 }
576529 else if (it->is_function_call ())
577530 {
578531 s_it->second .values .replace_const (
579532 to_code_function_call (it->code ).function ());
580- simplify_expr (to_code_function_call (it->code ).function (), ns);
533+
534+ simplify (to_code_function_call (it->code ).function (), ns);
581535
582536 exprt::operandst &args=
583537 to_code_function_call (it->code ).arguments ();
@@ -586,7 +540,7 @@ void constant_propagator_ait::replace(
586540 o_it!=args.end (); ++o_it)
587541 {
588542 s_it->second .values .replace_const (*o_it);
589- *o_it= simplify_expr (*o_it, ns);
543+ simplify (*o_it, ns);
590544 }
591545 }
592546 else if (it->is_other ())
0 commit comments