Skip to content

Commit 3f64f03

Browse files
committed
Expression rewriting may enable further constant propagation
The included regression test is an example the requires interleaving of simplification and constant propagation.
1 parent 4f41e6d commit 3f64f03

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
FUTURE
1+
KNOWNBUG
22
main.c
33
--constants --verify
44
^EXIT=0$
55
^SIGNAL=0$
66
^\[main.assertion.1\] file main.c line 11 function main, assertion tmp_if_expr: SUCCESS$
7-
^\[main.assertion.2\] file main.c line 12 function main, assertion tmp_if_expr\$1: FAILURE$
7+
^\[main.assertion.2\] file main.c line 12 function main, assertion tmp_if_expr\$0: FAILURE \(if reachable\)$
88
--
99
^warning: ignoring
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <assert.h>
2+
3+
int main()
4+
{
5+
int i = 1;
6+
int *p = &i;
7+
assert(*p == 1);
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
--constants --verify
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^\[main.assertion.1\] file main.c line 7 function main, assertion \*p == 1: SUCCESS$
7+
--
8+
^warning: ignoring

src/analyses/constant_propagator.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,8 +576,25 @@ bool constant_propagator_domaint::replace_constants_and_simplify(
576576
exprt &expr,
577577
const namespacet &ns)
578578
{
579-
bool did_not_change_anything = known_values.replace_const.replace(expr);
580-
did_not_change_anything &= simplify(expr, ns);
579+
bool did_not_change_anything = true;
580+
581+
// iterate constant propagation and simplification until we cannot
582+
// constant-propagate any further - a prime example is pointer dereferencing,
583+
// where constant propagation may have a value of the pointer, the simplifier
584+
// takes care of evaluating dereferencing, and we might then have the value of
585+
// the resulting symbol known to constant propagation and thus replace the
586+
// dereferenced expression by a constant
587+
while(!known_values.replace_const.replace(expr))
588+
{
589+
did_not_change_anything = false;
590+
simplify(expr, ns);
591+
}
592+
593+
// even if we haven't been able to constant-propagate anything, run the
594+
// simplifier on the expression
595+
if(did_not_change_anything)
596+
did_not_change_anything &= simplify(expr, ns);
597+
581598
return did_not_change_anything;
582599
}
583600

0 commit comments

Comments
 (0)