public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-3844] LoongArch: fix signed overflow in loongarch_emit_int_compare
@ 2022-11-09  9:11 Xi Ruoyao
  0 siblings, 0 replies; only message in thread
From: Xi Ruoyao @ 2022-11-09  9:11 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:d699d32f47833cfab959a810dad48774c021c677

commit r13-3844-gd699d32f47833cfab959a810dad48774c021c677
Author: Xi Ruoyao <xry111@xry111.site>
Date:   Fri Nov 4 01:35:25 2022 +0800

    LoongArch: fix signed overflow in loongarch_emit_int_compare
    
    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.

Diff:
---
 gcc/config/loongarch/loongarch.cc | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc
index f54c233f90c..8d5d8d965dd 100644
--- a/gcc/config/loongarch/loongarch.cc
+++ b/gcc/config/loongarch/loongarch.cc
@@ -4178,10 +4178,13 @@ loongarch_emit_int_compare (enum rtx_code *code, rtx *op0, rtx *op1)
 	      if (!increment && !decrement)
 		continue;
 
+	      if ((increment && rhs == HOST_WIDE_INT_MAX)
+		  || (decrement && rhs == HOST_WIDE_INT_MIN))
+		break;
+
 	      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] only message in thread

only message in thread, other threads:[~2022-11-09  9:11 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-09  9:11 [gcc r13-3844] LoongArch: fix signed overflow in loongarch_emit_int_compare 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).