From: Xi Ruoyao Date: Thu, 3 Nov 2022 17:35:25 +0000 (+0800) Subject: LoongArch: fix signed overflow in loongarch_emit_int_compare X-Git-Tag: basepoints/gcc-14~3363 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d699d32f47833cfab959a810dad48774c021c677;p=thirdparty%2Fgcc.git 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 --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc index f54c233f90c6..8d5d8d965dd7 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];