[v2] LoongArch: fix signed overflow in loongarch_emit_int_compare

Message ID 20221109072645.790242-1-xry111@xry111.site
State Accepted
Headers
Series [v2] LoongArch: fix signed overflow in loongarch_emit_int_compare |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Xi Ruoyao Nov. 9, 2022, 7:26 a.m. UTC
  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.
---

v1 -> v2: break instead of continue if the inc/dec will overflow. So the
logic wouldn't be changed if signed overflow was defined to wrap.

Bootstrapped and regtested on loongarch64-linux-gnu.  Ok for trunk?

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

Comments

chenglulu Nov. 9, 2022, 7:32 a.m. UTC | #1
LGTM.

Thanks!

在 2022/11/9 下午3:26, 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.
> ---
>
> v1 -> v2: break instead of continue if the inc/dec will overflow. So the
> logic wouldn't be changed if signed overflow was defined to wrap.
>
> Bootstrapped and regtested on loongarch64-linux-gnu.  Ok for trunk?
>
>   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];
  

Patch

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];