From: Xi Ruoyao <xry111@xry111.site>
To: gcc-patches@gcc.gnu.org
Cc: Lulu Cheng <chenglulu@loongson.cn>, Wang Xuerui <i@xen0n.name>,
Chenghua Xu <xuchenghua@loongson.cn>,
Xi Ruoyao <xry111@xry111.site>
Subject: [PATCH] LoongArch: fix signed overflow in loongarch_emit_int_compare
Date: Fri, 4 Nov 2022 14:39:41 +0800 [thread overview]
Message-ID: <20221104063942.1594844-1-xry111@xry111.site> (raw)
Signed overflow is an undefined behavior, so we need to prevent it from
happening, instead of "checking" the result.
gcc/ChangeLog:
* config/loongarch/loongarch.cc (loongarch_emit_int_compare):
Avoid signed overflow.
---
Bootstrapped and regtested on loongarch64-linux-gnu. OK for trunk?
gcc/config/loongarch/loongarch.cc | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc
index f54c233f90c..032fb1474c7 100644
--- a/gcc/config/loongarch/loongarch.cc
+++ b/gcc/config/loongarch/loongarch.cc
@@ -4175,13 +4175,14 @@ loongarch_emit_int_compare (enum rtx_code *code, rtx *op0, rtx *op1)
HOST_WIDE_INT new_rhs;
bool increment = *code == mag_comparisons[i][0];
bool decrement = *code == mag_comparisons[i][1];
- if (!increment && !decrement)
+ if ((!increment && !decrement)
+ || (increment && rhs == HOST_WIDE_INT_MAX)
+ || (decrement && rhs == HOST_WIDE_INT_MIN))
continue;
new_rhs = rhs + (increment ? 1 : -1);
if (loongarch_integer_cost (new_rhs)
- < loongarch_integer_cost (rhs)
- && (rhs < 0) == (new_rhs < 0))
+ < loongarch_integer_cost (rhs))
{
*op1 = GEN_INT (new_rhs);
*code = mag_comparisons[i][increment];
--
2.38.1
next reply other threads:[~2022-11-04 6:40 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-04 6:39 Xi Ruoyao [this message]
2022-11-06 1:46 ` Lulu Cheng
2022-11-06 8:00 ` Xi Ruoyao
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=20221104063942.1594844-1-xry111@xry111.site \
--to=xry111@xry111.site \
--cc=chenglulu@loongson.cn \
--cc=gcc-patches@gcc.gnu.org \
--cc=i@xen0n.name \
--cc=xuchenghua@loongson.cn \
/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).