* [PATCH] LoongArch: fix signed overflow in loongarch_emit_int_compare
@ 2022-11-04 6:39 Xi Ruoyao
2022-11-06 1:46 ` Lulu Cheng
0 siblings, 1 reply; 3+ messages in thread
From: Xi Ruoyao @ 2022-11-04 6:39 UTC (permalink / raw)
To: gcc-patches; +Cc: Lulu Cheng, Wang Xuerui, Chenghua Xu, Xi Ruoyao
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] LoongArch: fix signed overflow in loongarch_emit_int_compare
2022-11-04 6:39 [PATCH] LoongArch: fix signed overflow in loongarch_emit_int_compare Xi Ruoyao
@ 2022-11-06 1:46 ` Lulu Cheng
2022-11-06 8:00 ` Xi Ruoyao
0 siblings, 1 reply; 3+ messages in thread
From: Lulu Cheng @ 2022-11-06 1:46 UTC (permalink / raw)
To: Xi Ruoyao, gcc-patches; +Cc: Wang Xuerui, Chenghua Xu
在 2022/11/4 下午2:39, Xi Ruoyao 写道:
> 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;
I think it should be here:
if (!increment && !decrement)
continue;
+ if ((increment && rhs == HOST_WIDE_INT_MAX)
+ || (decrement && rhs == HOST_WIDE_INT_MIN))
+ break;
+
It is not necessary to continue when *code matches one of
mag_comparisons[i].
>
> 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];
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] LoongArch: fix signed overflow in loongarch_emit_int_compare
2022-11-06 1:46 ` Lulu Cheng
@ 2022-11-06 8:00 ` Xi Ruoyao
0 siblings, 0 replies; 3+ messages in thread
From: Xi Ruoyao @ 2022-11-06 8:00 UTC (permalink / raw)
To: Lulu Cheng, gcc-patches; +Cc: Wang Xuerui, Chenghua Xu
On Sun, 2022-11-06 at 09:46 +0800, Lulu Cheng wrote:
> I think it should be here:
>
> if (!increment && !decrement)
> continue;
>
> + if ((increment && rhs == HOST_WIDE_INT_MAX)
> + || (decrement && rhs == HOST_WIDE_INT_MIN))
> + break;
> +
>
> It is not necessary to continue when *code matches one of
> mag_comparisons[i].
Ah yes, I misread the code :(.
--
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-11-06 8:00 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-04 6:39 [PATCH] LoongArch: fix signed overflow in loongarch_emit_int_compare Xi Ruoyao
2022-11-06 1:46 ` Lulu Cheng
2022-11-06 8:00 ` Xi Ruoyao
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).