From 31c7ea0165882eaf09040ea7edfc37cc38f0a032 Mon Sep 17 00:00:00 2001 From: Michael Matz Date: Mon, 19 Sep 2016 18:38:12 +0200 Subject: opt: Start optimizing dead code a bit If a condition is always zero/non-zero we can omit the then or else code. This is complicated a bit by having to deal with labels that might make such code reachable without us yet knowing during parsing. --- tests/tcctest.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'tests/tcctest.c') diff --git a/tests/tcctest.c b/tests/tcctest.c index d1f6c9b..8031af4 100644 --- a/tests/tcctest.c +++ b/tests/tcctest.c @@ -88,6 +88,7 @@ void struct_test(); void array_test(); void expr_ptr_test(); void bool_test(); +void optimize_out(); void expr2_test(); void constant_expr_test(); void expr_cmp_test(); @@ -695,6 +696,7 @@ int main(int argc, char **argv) array_test(); expr_ptr_test(); bool_test(); + optimize_out(); expr2_test(); constant_expr_test(); expr_cmp_test(); @@ -1165,6 +1167,31 @@ void bool_test() printf ("bits = 0x%x\n", calc_vm_flags (0x1)); } +extern int undefined_function(void); +extern int defined_function(void); + +void optimize_out(void) +{ + int i = 0 ? undefined_function() : defined_function(); + printf ("oo:%d\n", i); + int j = 1 ? defined_function() : undefined_function(); + printf ("oo:%d\n", j); + if (0) + printf("oo:%d\n", undefined_function()); + else + printf("oo:%d\n", defined_function()); + if (1) + printf("oo:%d\n", defined_function()); + else + printf("oo:%d\n", undefined_function()); +} + +int defined_function(void) +{ + static int i = 40; + return i++; +} + /* GCC accepts that */ static int tab_reinit[]; static int tab_reinit[10]; -- cgit v1.3.1