public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "rguenth at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/105329] [12/13 Regression] Bogus restrict warning when assigning 1-char string literal to std::string since r12-3347-g8af8abfbbace49e6
Date: Mon, 02 May 2022 12:44:06 +0000	[thread overview]
Message-ID: <bug-105329-4-2XljI3SoWh@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-105329-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105329

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rguenth at gcc dot gnu.org
   Target Milestone|---                         |12.0

--- Comment #8 from Richard Biener <rguenth at gcc dot gnu.org> ---
Btw, requires -std=c++20 but -O2 is enough, -O3 not needed.

To quote again:

if (_22 >= &MEM <const char[2]> [(void *)"5" + 1B])
  goto <bb 10>; [50.00%]
else
  goto <bb 11>; [50.00%]

<bb 11> [local count: 44944954]:
if (_22 <= "5")
  goto <bb 12>; [50.00%]
else
  goto <bb 13>; [50.00%]

<bb 13> [local count: 22472477]:
_48 = _22 - "5";
if (_48 == 1)
  goto <bb 14>; [34.00%]
else
  goto <bb 15>; [66.00%]

the "simple" thing we fail to thread is

  if (_22 >= _1 + 1)
    ;
  else
    {
       if (_22 <= _1)
          ; // this must be true, _22 < _1 is true even
    }

in fact we fail to canonicalize _22 >= &MEM <const char[2]> [(void *)"5" + 1B]
to _22 > "5".  fold_comparison via maybe_canonicalize_comparison does such
thing but not for pointers or &MEM.

The following (too special) simplifies the above to

  <bb 9> [local count: 89889908]:
  if (_22 > &MEM <const char[2]> [(void *)"5"])
    goto <bb 10>; [50.00%]
  else
    goto <bb 11>; [50.00%]

  <bb 11> [local count: 44944954]:
  if (_22 <= "5")
    goto <bb 12>; [50.00%]
  else
    goto <bb 13>; [50.00%]

but we still won't simplify the second compare.

diff --git a/gcc/match.pd b/gcc/match.pd
index 6d691d302b3..cb16694a150 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -5397,6 +5397,18 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
        (if (known_eq (off, 0))
         { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))

+(for cmp (ge le)
+     cmpp (gt lt)
+ (simplify
+  (cmp:c @0 addr@1)
+  (with
+    { tree m = TREE_OPERAND (@1, 0); }
+    (if (TREE_CODE (m) == MEM_REF
+        && integer_onep (TREE_OPERAND (m, 1)))
+     (cmpp @0 { build1 (ADDR_EXPR, TREE_TYPE (@1),
+                       build2 (MEM_REF, TREE_TYPE (m), TREE_OPERAND (m, 0),
+                               build_zero_cst (TREE_TYPE (TREE_OPERAND (m,
1))))); })))))
+
 /* Equality compare simplifications from fold_binary  */
 (for cmp (eq ne)


The ifcombine eventually arrives in

Breakpoint 5, tree_ssa_ifcombine_bb_1 (inner_cond_bb=<basic_block
0x7ffff463bf08 (11)>, outer_cond_bb=<basic_block 0x7ffff4740af8 (9)>,
then_bb=<basic_block 0x7ffff463bf70 (12)>, else_bb=<basic_block 0x7ffff47728f0
(13)>, phi_pred_bb=<basic_block 0x7ffff463bf08 (11)>) at
/home/rguenther/src/gcc-12-branch/gcc/tree-ssa-ifcombine.cc:646
646       if (phi_pred_bb != else_bb
<bb 9> [local count: 89889908]:
if (_22 > &MEM <const char[2]> [(void *)"5"])
  goto <bb 10>; [50.00%]
else
  goto <bb 11>; [50.00%]

$7 = void
<bb 11> [local count: 44944954]:
if (_22 <= "5")
  goto <bb 12>; [50.00%]
else
  goto <bb 13>; [50.00%]

$8 = void

but the CFG doesn't resemble any of the forms it handles and it does not
try to catch the case where the inner condition would simplify (basically
thread it without any code duplication).  So it doesn't really fit
ifcombine.

The old VRP pass sees

  <bb 12> [local count: 44944954]:
  _112 = ASSERT_EXPR <_22, _22 <= &MEM <const char[2]> [(void *)"5"]>;
  if (_112 <= "5")
    goto <bb 13>; [50.00%]
  else
    goto <bb 14>; [50.00%]

but concludes

xtract_range_from_stmt visiting:
_112 = ASSERT_EXPR <_22, _22 <= &MEM <const char[2]> [(void *)"5"]>;
Found new range for _112: char * VARYING

extract_range_from_stmt visiting:
if (_112 <= "5")

Visiting conditional with predicate: if (_112 <= "5")

With known ranges
        _112: char * VARYING

Predicate evaluates to: DON'T KNOW

it might reason that _22 <= &MEM <const char[2]> [(void *)"5"] means
_22 == &"5" and track the range of _22 as constant.

That said, we relied on jump threading for these kind of simplifications
but we are not good at this particular case.

  parent reply	other threads:[~2022-05-02 12:44 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-21  7:41 [Bug c++/105329] New: Bogus restrict warning when assigning 1-char string literal to std::string boris at kolpackov dot net
2022-04-21 11:15 ` [Bug tree-optimization/105329] " rguenth at gcc dot gnu.org
2022-04-21 20:50 ` amacleod at redhat dot com
2022-04-21 22:51 ` amacleod at redhat dot com
2022-04-22 10:31 ` aldyh at gcc dot gnu.org
2022-04-25 17:50 ` amacleod at redhat dot com
2022-04-25 22:25 ` mattias.ellert at physics dot uu.se
2022-04-26 15:04 ` [Bug tree-optimization/105329] [12 Regression] Bogus restrict warning when assigning 1-char string literal to std::string since r12-3347-g8af8abfbbace49e6 marxin at gcc dot gnu.org
2022-05-02 12:44 ` rguenth at gcc dot gnu.org [this message]
2022-05-02 12:45 ` [Bug tree-optimization/105329] [12/13 " rguenth at gcc dot gnu.org
2022-05-02 12:46 ` rguenth at gcc dot gnu.org
2022-05-02 12:57 ` rguenth at gcc dot gnu.org
2022-05-02 13:22 ` rguenth at gcc dot gnu.org
2022-05-02 13:33 ` jakub at gcc dot gnu.org
2022-05-02 13:58 ` jakub at gcc dot gnu.org
2022-05-02 16:04 ` jakub at gcc dot gnu.org
2022-05-02 19:43 ` jakub at gcc dot gnu.org
2022-05-02 22:08 ` redi at gcc dot gnu.org
2022-05-06  3:59 ` mattias.ellert at physics dot uu.se
2022-05-06  8:33 ` jakub at gcc dot gnu.org
2022-05-20  8:13 ` rguenth at gcc dot gnu.org
2022-07-26 12:05 ` jakub at gcc dot gnu.org
2022-09-12  9:37 ` cvs-commit at gcc dot gnu.org
2022-09-29 15:19 ` msebor at gcc dot gnu.org
2022-09-30 16:55 ` dan at stahlke dot org
2022-09-30 17:16 ` dan at stahlke dot org
2023-02-21 18:30 ` 49tbwddbqeazdawz at chyen dot cc
2023-02-21 19:28 ` redi at gcc dot gnu.org
2023-02-22 17:28 ` 49tbwddbqeazdawz at chyen dot cc
2023-02-22 17:52 ` redi at gcc dot gnu.org
2023-02-24 14:10 ` wielkiegie at gmail dot com
2023-05-08 12:24 ` [Bug tree-optimization/105329] [12/13/14 " rguenth at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-105329-4-2XljI3SoWh@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).