Fix looping flag discovery in ipa-pure-const
authorJan Hubicka <jh@suse.cz>
Sat, 20 Nov 2021 23:35:22 +0000 (00:35 +0100)
committerJan Hubicka <jh@suse.cz>
Sat, 20 Nov 2021 23:35:22 +0000 (00:35 +0100)
The testcase shows situation where there is non-trivial cycle in the callgraph
involving a noreturn call.  This cycle is important for const function discovery
but not important for pure.  IPA pure const uses same strongly connected
components for both propagations which makes it to get suboptimal result
(does not detect the pure flag). However local pure const gets the situation
right becaue it processes functions in right order.  This hits rarely
executed code in propagate_pure_const that merge results with previously
known state that has long standing bug in it that makes it to throw away
the looping flag.

Bootstrapped/regtested x86_64-linux.

gcc/ChangeLog:

2021-11-21  Jan Hubicka  <hubicka@ucw.cz>

PR ipa/103052
* ipa-pure-const.c (propagate_pure_const): Fix merging of loping flag.

gcc/testsuite/ChangeLog:

2021-11-21  Jan Hubicka  <hubicka@ucw.cz>

PR ipa/103052
* gcc.c-torture/execute/pr103052.c: New test.

gcc/ipa-pure-const.c
gcc/testsuite/gcc.c-torture/execute/pr103052.c [new file with mode: 0644]

index a332940..fea8b08 100644 (file)
@@ -1782,9 +1782,9 @@ propagate_pure_const (void)
          if (w_l->state_previously_known != IPA_NEITHER
              && this_state > w_l->state_previously_known)
            {
-              this_state = w_l->state_previously_known;
              if (this_state == IPA_NEITHER)
-               this_looping = w_l->looping_previously_known;
+               this_looping = w_l->looping_previously_known;
+             this_state = w_l->state_previously_known;
            }
          if (!this_looping && self_recursive_p (w))
            this_looping = true;
diff --git a/gcc/testsuite/gcc.c-torture/execute/pr103052.c b/gcc/testsuite/gcc.c-torture/execute/pr103052.c
new file mode 100644 (file)
index 0000000..bef8674
--- /dev/null
@@ -0,0 +1,35 @@
+static void js_error(void);
+static int top;
+static void js_throw(void)
+{
+       __builtin_exit(0);
+}
+
+// LOCATION A -- if js_pop is here, the bug is present
+static void js_pop(void)
+{
+       if (++top > 100)
+               js_error();
+}
+
+static void jsC_error(const char *v)
+{
+       if (v[0] == 0)
+               js_error();
+       js_throw();
+}
+static void checkfutureword(const char *exp)
+{
+       if (!__builtin_strcmp(exp, "const"))
+               jsC_error("boom");
+}
+static void js_error(void) {
+       checkfutureword("foo");
+       checkfutureword("bar");
+       js_pop();
+}
+int main(void)
+{
+       checkfutureword("const");
+       __builtin_abort ();
+}