tree-optimization/105080 - make sure SCEV is available for ranger

Message ID 20220328131625.05BFC13215@imap2.suse-dmz.suse.de
State Committed
Commit 28c5df79300ab354cbc381aab200f7c2bd0331ad
Headers
Series tree-optimization/105080 - make sure SCEV is available for ranger |

Commit Message

Richard Biener March 28, 2022, 1:16 p.m. UTC
  When doing format diagnostics at -O0 we should make sure to make
SCEV available to avoid false positives due to ranges we otherwise
can trivially compute.

Bootstrap and regtest running on x86_64-unknown-linux-gnu.

OK if that succeeds?

Thanks,
Richard.

2022-03-28  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/105080
	* tree-ssa-strlen.cc (printf_strlen_execute): Always init
	loops and SCEV.

	* gcc.dg/pr105080.c: New testcase.
---
 gcc/testsuite/gcc.dg/pr105080.c | 11 +++++++++++
 gcc/tree-ssa-strlen.cc          | 16 ++++------------
 2 files changed, 15 insertions(+), 12 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/pr105080.c
  

Comments

Jakub Jelinek March 28, 2022, 1:21 p.m. UTC | #1
On Mon, Mar 28, 2022 at 03:16:24PM +0200, Richard Biener wrote:
> When doing format diagnostics at -O0 we should make sure to make
> SCEV available to avoid false positives due to ranges we otherwise
> can trivially compute.
> 
> Bootstrap and regtest running on x86_64-unknown-linux-gnu.
> 
> OK if that succeeds?

Doesn't this slow down -O0 compilation too much?

> 2022-03-28  Richard Biener  <rguenther@suse.de>
> 
> 	PR tree-optimization/105080
> 	* tree-ssa-strlen.cc (printf_strlen_execute): Always init
> 	loops and SCEV.
> 
> 	* gcc.dg/pr105080.c: New testcase.

	Jakub
  
Richard Biener March 28, 2022, 1:26 p.m. UTC | #2
On Mon, 28 Mar 2022, Jakub Jelinek wrote:

> On Mon, Mar 28, 2022 at 03:16:24PM +0200, Richard Biener wrote:
> > When doing format diagnostics at -O0 we should make sure to make
> > SCEV available to avoid false positives due to ranges we otherwise
> > can trivially compute.
> > 
> > Bootstrap and regtest running on x86_64-unknown-linux-gnu.
> > 
> > OK if that succeeds?
> 
> Doesn't this slow down -O0 compilation too much?

Not if you do not enable the warning.  If you enable it you
probably are set to pay for it.  Are you specifically
concerned about the cases where SCEV / niter analysis take
a lot of time or are you concerned about the constant overhead
of enabling loops/SCEV?

Richard.

> > 2022-03-28  Richard Biener  <rguenther@suse.de>
> > 
> > 	PR tree-optimization/105080
> > 	* tree-ssa-strlen.cc (printf_strlen_execute): Always init
> > 	loops and SCEV.
> > 
> > 	* gcc.dg/pr105080.c: New testcase.
> 
> 	Jakub
> 
>
  
Richard Biener March 28, 2022, 1:29 p.m. UTC | #3
On Mon, 28 Mar 2022, Richard Biener wrote:

> On Mon, 28 Mar 2022, Jakub Jelinek wrote:
> 
> > On Mon, Mar 28, 2022 at 03:16:24PM +0200, Richard Biener wrote:
> > > When doing format diagnostics at -O0 we should make sure to make
> > > SCEV available to avoid false positives due to ranges we otherwise
> > > can trivially compute.
> > > 
> > > Bootstrap and regtest running on x86_64-unknown-linux-gnu.
> > > 
> > > OK if that succeeds?
> > 
> > Doesn't this slow down -O0 compilation too much?
> 
> Not if you do not enable the warning.  If you enable it you
> probably are set to pay for it.  Are you specifically
> concerned about the cases where SCEV / niter analysis take
> a lot of time or are you concerned about the constant overhead
> of enabling loops/SCEV?

One concern might be that loop_optimizer_init (LOOPS_NORMAL)
changes the IL but the early pass just emits diagnostics so
when disabled/enabled (based purely on diagnostic options)
generated code could change.  Note SCEV currently has (undocumented)
contraints on loops so just AVOID_CFG_MANIPULATIONS doens't work
there without putting it extra safety checks to give up.

Richard.

> Richard.
> 
> > > 2022-03-28  Richard Biener  <rguenther@suse.de>
> > > 
> > > 	PR tree-optimization/105080
> > > 	* tree-ssa-strlen.cc (printf_strlen_execute): Always init
> > > 	loops and SCEV.
> > > 
> > > 	* gcc.dg/pr105080.c: New testcase.
> > 
> > 	Jakub
> > 
> > 
> 
>
  
Jakub Jelinek March 28, 2022, 1:38 p.m. UTC | #4
On Mon, Mar 28, 2022 at 03:26:11PM +0200, Richard Biener wrote:
> On Mon, 28 Mar 2022, Jakub Jelinek wrote:
> 
> > On Mon, Mar 28, 2022 at 03:16:24PM +0200, Richard Biener wrote:
> > > When doing format diagnostics at -O0 we should make sure to make
> > > SCEV available to avoid false positives due to ranges we otherwise
> > > can trivially compute.
> > > 
> > > Bootstrap and regtest running on x86_64-unknown-linux-gnu.
> > > 
> > > OK if that succeeds?
> > 
> > Doesn't this slow down -O0 compilation too much?
> 
> Not if you do not enable the warning.  If you enable it you
> probably are set to pay for it.  Are you specifically
> concerned about the cases where SCEV / niter analysis take
> a lot of time or are you concerned about the constant overhead
> of enabling loops/SCEV?

I was just worried about the worst case compile time for -O0.
I think we already have tons of -fcompare-debug=-Wall (or -Wextra)
dependent code generation (mainly in the C++ FE) so until we are able to
deal with it making this -W* dependent isn't the end of the world.

Guess the patch is ok.  If we find bad compile time issues with it,
one way would be to enable it lazily (don't enable it initially,
when about to emit first warning instead enable it and retry).

	Jakub
  

Patch

diff --git a/gcc/testsuite/gcc.dg/pr105080.c b/gcc/testsuite/gcc.dg/pr105080.c
new file mode 100644
index 00000000000..77ee7eeb396
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr105080.c
@@ -0,0 +1,11 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O0 -Wall" } */
+
+int main()
+{
+  char foo[3];
+  int i;
+
+  for (i = 0; i < 16; i++)
+    __builtin_snprintf(foo, sizeof(foo), "%d", i);  /* { dg-bogus "truncated" } */
+}
diff --git a/gcc/tree-ssa-strlen.cc b/gcc/tree-ssa-strlen.cc
index 112f0dce874..b2556366214 100644
--- a/gcc/tree-ssa-strlen.cc
+++ b/gcc/tree-ssa-strlen.cc
@@ -5858,13 +5858,8 @@  printf_strlen_execute (function *fun, bool warn_only)
   strlen_optimize = !warn_only;
 
   calculate_dominance_info (CDI_DOMINATORS);
-
-  bool use_scev = optimize > 0 && flag_printf_return_value;
-  if (use_scev)
-    {
-      loop_optimizer_init (LOOPS_NORMAL);
-      scev_initialize ();
-    }
+  loop_optimizer_init (LOOPS_NORMAL);
+  scev_initialize ();
 
   gcc_assert (!strlen_to_stridx);
   if (warn_stringop_overflow || warn_stringop_truncation)
@@ -5902,11 +5897,8 @@  printf_strlen_execute (function *fun, bool warn_only)
       strlen_to_stridx = NULL;
     }
 
-  if (use_scev)
-    {
-      scev_finalize ();
-      loop_optimizer_finalize ();
-    }
+  scev_finalize ();
+  loop_optimizer_finalize ();
 
   return walker.m_cleanup_cfg ? TODO_cleanup_cfg : 0;
 }