diff options
author | Richard Biener <rguenther@suse.de> | 2023-10-20 15:08:49 +0200 |
---|---|---|
committer | Richard Biener <rguenther@suse.de> | 2024-06-21 11:21:15 +0200 |
commit | ce653fa683643d4e5a7146181954358f1060dfed (patch) | |
tree | 5f41f4a96f2b8d73177faf1be977d07d0eed8c26 | |
parent | 20fe647365a922c6dd7a7f283abb99b5588476e8 (diff) | |
download | gcc-ce653fa683643d4e5a7146181954358f1060dfed.zip gcc-ce653fa683643d4e5a7146181954358f1060dfed.tar.gz gcc-ce653fa683643d4e5a7146181954358f1060dfed.tar.bz2 |
tree-optimization/111445 - simple_iv simplification fault
The following fixes a missed check in the simple_iv attempt
to simplify (signed T)((unsigned T) base + step) where it
allows a truncating inner conversion leading to wrong code.
PR tree-optimization/111445
* tree-scalar-evolution.c (simple_iv_with_niters):
Add missing check for a sign-conversion.
* gcc.dg/torture/pr111445.c: New testcase.
(cherry picked from commit 9692309ed6b625f0fb358c0e230404b5603f69a6)
-rw-r--r-- | gcc/testsuite/gcc.dg/torture/pr111445.c | 29 | ||||
-rw-r--r-- | gcc/tree-scalar-evolution.c | 3 |
2 files changed, 31 insertions, 1 deletions
diff --git a/gcc/testsuite/gcc.dg/torture/pr111445.c b/gcc/testsuite/gcc.dg/torture/pr111445.c new file mode 100644 index 0000000..320e0b9 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr111445.c @@ -0,0 +1,29 @@ +/* { dg-do run } */ + +extern void abort (void); +short a, b; +unsigned char c = 255; +unsigned cnt; +void __attribute__((noipa)) +check (int x) +{ + if (x != 0) + abort (); + cnt++; +} +int main() +{ + int d; + unsigned char e; + d = 0; + for (; a >= 0; a--) { + int *f = &d; + *f = c; + } + e = 0; + for (; (unsigned char)(d - 255) + e <= 1; e++) + check (b); + if (cnt != 2) + abort (); + return 0; +} diff --git a/gcc/tree-scalar-evolution.c b/gcc/tree-scalar-evolution.c index ff052be..da85647 100644 --- a/gcc/tree-scalar-evolution.c +++ b/gcc/tree-scalar-evolution.c @@ -3281,7 +3281,8 @@ simple_iv_with_niters (class loop *wrto_loop, class loop *use_loop, type = TREE_TYPE (iv->base); e = TREE_OPERAND (iv->base, 0); - if (TREE_CODE (e) != PLUS_EXPR + if (!tree_nop_conversion_p (type, TREE_TYPE (e)) + || TREE_CODE (e) != PLUS_EXPR || TREE_CODE (TREE_OPERAND (e, 1)) != INTEGER_CST || !tree_int_cst_equal (iv->step, fold_convert (type, TREE_OPERAND (e, 1)))) |