From patchwork Fri Nov 4 06:39:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xi Ruoyao X-Patchwork-Id: 1699339 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=TMpPO6Ob; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4N3WH84T9Sz23lK for ; Fri, 4 Nov 2022 17:41:06 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A916438582B5 for ; Fri, 4 Nov 2022 06:41:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A916438582B5 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667544063; bh=HMjEuLI9KVWsFpK2F4ty8p9hxWq+fncnlYgWCeC38Pk=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=TMpPO6Ob5MCwUaqbl3HBYA/scomK9pGAmR6umZ66kx0qaxd121FvMljSVyaDLzRhV 9IObGcSzgKO91yKUHfiC0EIRgGcEr8tpmCmCwaZlaZc3sXe1vrV65079wnnfXB/Gj5 Pxn3ZA/GNSXYxeW3NOh4WtjV1t4CWCgX+CGuES90= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from xry111.site (xry111.site [IPv6:2001:470:683e::1]) by sourceware.org (Postfix) with ESMTPS id B9E3F3858427 for ; Fri, 4 Nov 2022 06:40:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B9E3F3858427 Received: from xry111-x57s1.. (unknown [IPv6:240e:358:11ed:a500:dc73:854d:832e:4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@xry111.site) by xry111.site (Postfix) with ESMTPSA id 938CF667E5; Fri, 4 Nov 2022 02:40:37 -0400 (EDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] LoongArch: fix signed overflow in loongarch_emit_int_compare Date: Fri, 4 Nov 2022 14:39:41 +0800 Message-Id: <20221104063942.1594844-1-xry111@xry111.site> X-Mailer: git-send-email 2.38.1 MIME-Version: 1.0 X-Spam-Status: No, score=-6.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FROM_SUSPICIOUS_NTLD, FROM_SUSPICIOUS_NTLD_FP, GIT_PATCH_0, LIKELY_SPAM_FROM, PDS_OTHER_BAD_TLD, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Xi Ruoyao via Gcc-patches From: Xi Ruoyao Reply-To: Xi Ruoyao Cc: Chenghua Xu , Lulu Cheng , Wang Xuerui Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" 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];