Skip to content

Commit 350aa96

Browse files
author
martin
committed
Addressing constant domain review comments by Daniel Poetzl.
1 parent 08b2332 commit 350aa96

7 files changed

+123
-199
lines changed

src/analyses/Makefile

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ SRC = ai.cpp \
2222
locals.cpp \
2323
natural_loops.cpp \
2424
reaching_definitions.cpp \
25-
replace_symbol_ext.cpp \
2625
static_analysis.cpp \
2726
uncaught_exceptions_analysis.cpp \
2827
uninitialized_domain.cpp \

src/analyses/constant_propagator.cpp

+45-91
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*******************************************************************\
22
3-
Module: Constant Propagation
3+
Module: Constant propagation
44
55
Author: 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

2223
void 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 &parameters=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 &parameter_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 &parameters=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 &param : 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!
319313
bool 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

335324
void 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.
397383
bool 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

540493
void 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())

src/analyses/constant_propagator.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ Author: Peter Schrammel
1212
#ifndef CPROVER_ANALYSES_CONSTANT_PROPAGATOR_H
1313
#define CPROVER_ANALYSES_CONSTANT_PROPAGATOR_H
1414

15-
#include <iostream>
15+
#include <iosfwd>
16+
#include <util/replace_symbol.h>
1617

1718
#include "ai.h"
1819
#include "dirty.h"
1920

20-
#include "replace_symbol_ext.h"
21-
2221
class constant_propagator_domaint:public ai_domain_baset
2322
{
2423
public:
@@ -63,7 +62,7 @@ class constant_propagator_domaint:public ai_domain_baset
6362
valuest():is_bottom(true) {}
6463

6564
// maps variables to constants
66-
replace_symbol_extt replace_const;
65+
replace_symbolt replace_const;
6766
bool is_bottom;
6867

6968
bool merge(const valuest &src);

src/analyses/replace_symbol_ext.cpp

-64
This file was deleted.

0 commit comments

Comments
 (0)