Bug 89998 - [7 regression] ICE: verify_gimple failed in printf-return-value
Summary: [7 regression] ICE: verify_gimple failed in printf-return-value
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 9.0
: P3 normal
Target Milestone: 7.5
Assignee: Jakub Jelinek
URL:
Keywords: ice-on-invalid-code
Depends on: 89996
Blocks: Wformat-overflow
  Show dependency treegraph
 
Reported: 2019-04-07 16:23 UTC by gandalf
Modified: 2019-08-30 13:34 UTC (History)
3 users (show)

See Also:
Host:
Target: avr
Build:
Known to work:
Known to fail:
Last reconfirmed: 2019-04-08 00:00:00


Attachments
gcc9-pr89998.patch (1.37 KB, patch)
2019-04-08 11:18 UTC, Jakub Jelinek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description gandalf 2019-04-07 16:23:20 UTC
I get an ICE on the following code with GCC 9.0.1 20190407 (experimental) compiled for AVR. Works in GCC 8.x. If the __flash keyword is taken out before _str[]=("xx"), then it compiles OK.


unsigned short sprintf(char *str, const char __flash *fmt, ...)
{
  __builtin_va_list args;

  __builtin_va_start(args, fmt);
  __builtin_va_end(args);
  return 0;
}

extern char *s;

int main()
{
  s+=sprintf(s, ({ static const char __flash _str[]=("xx"); _str; }));
}


# avr-gcc -v -O -mmcu=atmega1284p -c test3.c -o test3.o
Using built-in specs.
Reading specs from /usr/local/avr/lib/gcc/avr/9.0.1/device-specs/specs-atmega1284p
COLLECT_GCC=avr-gcc
Target: avr
Configured with: ../configure --target=avr --prefix=/usr/local/avr --disable-nls --enable-languages=c --disable-bootstrap --disable-libssp
Thread model: single
gcc version 9.0.1 20190407 (experimental) (GCC) 
COLLECT_GCC_OPTIONS='-v' '-O'  '-c' '-o' 'test3.o' '-specs=device-specs/specs-atmega1284p' '-mmcu=avr51'
 /usr/local/avr/libexec/gcc/avr/9.0.1/cc1 -quiet -v -imultilib avr51 -D__AVR_ATmega1284P__ -D__AVR_DEVICE_NAME__=atmega1284p test3.c -mn-flash=2 -mno-skip-bug -quiet -dumpbase test3.c -mmcu=avr51 -auxbase-strip test3.o -O -version -o /tmp/ccm4oBir.s
GNU C17 (GCC) version 9.0.1 20190407 (experimental) (avr)
        compiled by GNU C version 8.3.0, GMP version 6.1.2, MPFR version 4.0.2, MPC version 1.1.0, isl version none
GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
ignoring nonexistent directory "/usr/local/avr/lib/gcc/avr/9.0.1/../../../../avr/sys-include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/avr/lib/gcc/avr/9.0.1/include
 /usr/local/avr/lib/gcc/avr/9.0.1/include-fixed
 /usr/local/avr/lib/gcc/avr/9.0.1/../../../../avr/include
End of search list.
GNU C17 (GCC) version 9.0.1 20190407 (experimental) (avr)
        compiled by GNU C version 8.3.0, GMP version 6.1.2, MPFR version 4.0.2, MPC version 1.1.0, isl version none
GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
Compiler executable checksum: 082ea327cdf9723cb24010e987b4f891
test3.c: In function 'main':
test3.c:12:5: error: non-trivial conversion at assignment
   12 | int main()
      |     ^~~~
short unsigned int
int
_6 = 2;
during GIMPLE pass: printf-return-value
test3.c:12:5: internal compiler error: verify_gimple failed
0xc51c8b verify_gimple_in_cfg(function*, bool)
        ../../gcc/tree-cfg.c:5386
0xb5ff2f execute_function_todo
        ../../gcc/passes.c:1977
0xb60e6e execute_todo
        ../../gcc/passes.c:2031
Please submit a full bug report.
Comment 1 Andrew Pinski 2019-04-07 20:39:15 UTC
Dup.

*** This bug has been marked as a duplicate of bug 89996 ***
Comment 2 Andrew Pinski 2019-04-07 20:40:04 UTC
Maybe not exactly a dup but a related one.
Comment 3 Richard Biener 2019-04-08 08:09:42 UTC
There's another dup for this.  But I wonder why it works in GCC 8 (maybe it only works with -fno-checking after all).

So, can you try if GCC 8 fails the same way with -fchecking?
Comment 4 Jakub Jelinek 2019-04-08 08:34:55 UTC
This has nothing to do with PR89996 and is not AVR specific either.
Following can reproduce it on x86_64-linux:

unsigned int sprintf (char *str, const char *fmt, ...);

int
foo (char *s)
{
  return sprintf (s, "foo");
}

and s/unsigned int/unsigned short/ on AVR.
I'll handle it.
Comment 5 Jakub Jelinek 2019-04-08 08:57:17 UTC
I think this started with r213951, though that is just a guess, our bisect seeder gave weird answers.
Comment 6 Jakub Jelinek 2019-04-08 11:18:48 UTC
Created attachment 46097 [details]
gcc9-pr89998.patch

Untested fix.
Comment 7 Martin Sebor 2019-04-08 17:00:30 UTC
This is another instance of the mismatched built-in signature problem.  With -Wextra GCC 9 prints:

  warning: mismatch in return type of built-in function ‘sprintf’; expected ‘int’ [-Wbuiltin-declaration-mismatch]
    1 | unsigned int sprintf (char *str, const char *fmt, ...);

As we discussed, having the warning for the declaration disable treating the function as a built-in will avoid these bugs.
Comment 8 Jakub Jelinek 2019-04-09 10:26:44 UTC
Author: jakub
Date: Tue Apr  9 10:26:13 2019
New Revision: 270224

URL: https://gcc.gnu.org/viewcvs?rev=270224&root=gcc&view=rev
Log:
	PR tree-optimization/89998
	* gimple-ssa-sprintf.c (try_substitute_return_value): Use lhs type
	instead of integer_type_node if possible, don't add ranges if return
	type is not compatible with int.
	* gimple-fold.c (gimple_fold_builtin_sprintf,
	gimple_fold_builtin_snprintf): Use lhs type instead of hardcoded
	integer_type_node.

	* gcc.c-torture/compile/pr89998-1.c: New test.
	* gcc.c-torture/compile/pr89998-2.c: New test.

Added:
    trunk/gcc/testsuite/gcc.c-torture/compile/pr89998-1.c
    trunk/gcc/testsuite/gcc.c-torture/compile/pr89998-2.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/gimple-fold.c
    trunk/gcc/gimple-ssa-sprintf.c
    trunk/gcc/testsuite/ChangeLog
Comment 9 Jakub Jelinek 2019-04-09 11:29:27 UTC
Fixed for trunk.  As a workaround I'd suggest using a correct prototype or -fno-builtin-sprintf if you intentionally use a different one.
Comment 10 gandalf 2019-04-09 13:08:47 UTC
(In reply to Jakub Jelinek from comment #9)
> Fixed for trunk.  As a workaround I'd suggest using a correct prototype or
> -fno-builtin-sprintf if you intentionally use a different one.

Thanks. Using the correct prototype (dropping the 'unsigned') indeed works as a workaround.
Comment 11 Jakub Jelinek 2019-04-30 20:58:31 UTC
Author: jakub
Date: Tue Apr 30 20:58:00 2019
New Revision: 270745

URL: https://gcc.gnu.org/viewcvs?rev=270745&root=gcc&view=rev
Log:
	Backported from mainline
	2019-04-09  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/89998
	* gimple-ssa-sprintf.c (try_substitute_return_value): Use lhs type
	instead of integer_type_node if possible, don't add ranges if return
	type is not compatible with int.
	* gimple-fold.c (gimple_fold_builtin_sprintf,
	gimple_fold_builtin_snprintf): Use lhs type instead of hardcoded
	integer_type_node.

	* gcc.c-torture/compile/pr89998-1.c: New test.
	* gcc.c-torture/compile/pr89998-2.c: New test.

Added:
    branches/gcc-8-branch/gcc/testsuite/gcc.c-torture/compile/pr89998-1.c
    branches/gcc-8-branch/gcc/testsuite/gcc.c-torture/compile/pr89998-2.c
Modified:
    branches/gcc-8-branch/gcc/ChangeLog
    branches/gcc-8-branch/gcc/gimple-fold.c
    branches/gcc-8-branch/gcc/gimple-ssa-sprintf.c
    branches/gcc-8-branch/gcc/testsuite/ChangeLog
Comment 12 Jakub Jelinek 2019-05-01 07:17:13 UTC
Fixed for 8.4+ too.
Comment 13 Jakub Jelinek 2019-08-30 12:35:37 UTC
Author: jakub
Date: Fri Aug 30 12:35:05 2019
New Revision: 275146

URL: https://gcc.gnu.org/viewcvs?rev=275146&root=gcc&view=rev
Log:
	Backported from mainline
	2019-04-09  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/89998
	* gimple-ssa-sprintf.c (try_substitute_return_value): Use lhs type
	instead of integer_type_node if possible, don't add ranges if return
	type is not compatible with int.
	* gimple-fold.c (gimple_fold_builtin_sprintf,
	gimple_fold_builtin_snprintf): Use lhs type instead of hardcoded
	integer_type_node.

	* gcc.c-torture/compile/pr89998-1.c: New test.
	* gcc.c-torture/compile/pr89998-2.c: New test.

Added:
    branches/gcc-7-branch/gcc/testsuite/gcc.c-torture/compile/pr89998-1.c
    branches/gcc-7-branch/gcc/testsuite/gcc.c-torture/compile/pr89998-2.c
Modified:
    branches/gcc-7-branch/gcc/ChangeLog
    branches/gcc-7-branch/gcc/gimple-fold.c
    branches/gcc-7-branch/gcc/gimple-ssa-sprintf.c
    branches/gcc-7-branch/gcc/testsuite/ChangeLog
Comment 14 Jakub Jelinek 2019-08-30 13:34:02 UTC
Fixed.