public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Biener <richard.guenther@gmail.com>
To: "bin.cheng" <bin.cheng@linux.alibaba.com>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH PR100740]Fix overflow check in simplifying exit cond comparing two IVs.
Date: Wed, 2 Jun 2021 09:27:08 +0200	[thread overview]
Message-ID: <CAFiYyc0S_3xZsR9q7Lc3ECaWQ-_=6j=i54JYAnM_oRJxEPb+CQ@mail.gmail.com> (raw)
In-Reply-To: <b348cd70-f131-4efc-b980-95ae68bd4a8e.bin.cheng@linux.alibaba.com>

On Tue, Jun 1, 2021 at 4:00 PM bin.cheng via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> Hi,
> As described in patch summary, this fixes the wrong code issue by adding overflow-ness
> check for iv1.step - iv2.step.
>
> Bootstrap and test on x86_64.  Any comments?

+         bool wrap_p = TYPE_OVERFLOW_WRAPS (step_type);
+         if (wrap_p)
+           {
+             tree t = fold_binary_to_constant (GE_EXPR, step_type,
+                                               iv0->step, iv1->step);
+             wrap_p = integer_zerop (t);
+           }

I think we can't use TYPE_OVERFLOW_WRAPS/TYPE_OVERFLOW_UNDEFINED since
that's only relevant for expressions written by the user - we're
computing iv0.step - iv1.step
which can even overflow when TYPE_OVERFLOW_UNDEFINED (in fact we may not
even generate this expression then!).  So I think we have to do sth like

   /* If the iv0->step - iv1->step wraps, fail.  */
   if (!operand_equal_p (iv0->step, iv1->step)
       && (TREE_CODE (iv0->step) != INTEGER_CST || TREE_CODE
(iv1->step) != INTEGER_CST)
       && !wi::gt (wi::to_widest (iv0->step), wi::to_widest (iv1->step))
     return false;

which only handles equality and all integer constant steps. You could
also use ranges
like

 wide_int min0, max0, min1, max1;
  if (!operand_equal_p (iv->step, iv1->step)
      && (determine_value_range (iv0->step, &min0, &max0) != VR_RANGE
             || determine_value_range (iv1->step, &min1, &max1) != VR_RANGE
             || !wi::ge (min0, max1)))
   return false;

Note I'm not sure why

       iv0->step = step;
       if (!POINTER_TYPE_P (type))
        iv0->no_overflow = false;

here the no_overflow reset does not happen for pointer types?  Or
rather why does
it happen at all?  Don't we strictly make the step less in absolute value?

> Thanks,
> bin

  reply	other threads:[~2021-06-02  7:27 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-01 11:15 bin.cheng
2021-06-02  7:27 ` Richard Biener [this message]
2021-06-06 10:01   ` Bin.Cheng
2021-06-07 14:35     ` Richard Biener
2021-07-01 12:19       ` Richard Biener
2021-07-02  1:37         ` Bin.Cheng
2021-07-02  7:57           ` Richard Biener

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='CAFiYyc0S_3xZsR9q7Lc3ECaWQ-_=6j=i54JYAnM_oRJxEPb+CQ@mail.gmail.com' \
    --to=richard.guenther@gmail.com \
    --cc=bin.cheng@linux.alibaba.com \
    --cc=gcc-patches@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).