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: 15382 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp208833wru; Thu, 3 Nov 2022 23:41:28 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6ulh59aKyYznncUD4fDoxWiCne0d57hUhBk4ymmUqSw1PvNMDHZHCF4xX7/kTGxUuWAqQM X-Received: by 2002:a17:907:7214:b0:791:a4cf:5bb7 with SMTP id dr20-20020a170907721400b00791a4cf5bb7mr31700727ejc.576.1667544088291; Thu, 03 Nov 2022 23:41:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667544088; cv=none; d=google.com; s=arc-20160816; b=TxAiucsoAgRjwY5WgvUbshS9an+jXn+NI3l9ZperjBmlcpNlrtuwrED70k1npkFlk/ LSwuma7KoeufnP3F438fzkhJrpOgIAxfDMarlmQa5Hwg5sL/Q8qLd3LMUFqGGpNOhjsG HCNbHxE12QIA33JSdlm0QNSYDH6QCHm88A4t1a+Z1iSlmw+s4us4vmJRDcApeO2fOKps Uj3Tu2w1DaUmO1S+0ko9TBkiiZWzIxD/KAW7zfDgb6e3fqb0liCbsOqgtEPGkNGbTkjH 5LL0k6gSNKE/6Pa1DvBTQyN2jt+lccaMMwA7nRsEtjIFrfFYiDYDheIG+UIaljNHvx3k D5OA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=HMjEuLI9KVWsFpK2F4ty8p9hxWq+fncnlYgWCeC38Pk=; b=AN5C4Vw8F4VP+/qFy2atbVJct3t9z9tHlISG+Crmai+l6gtmI27gLQO7NlXoQKGhkR Q4tfJZXQknrnwTIdFj4cfDoMMmS1HdAE/rwD+EEw80rOhu1Ns3orwFnUZOCV6Lkwu9NS gneMpzdPmHnuodEoTTLfl5JoAPfFK2C2X1jBQJDK9+KR+tcfz9A0xljCZD2EesBsNCnL lg0ZMBSZBIuHc0LXbwrGXbbq6KeAMUtRRDNvBqyWVbYwg+xArbN+d5Tk6fI9YVdHXh/I b4X/KQAWQEcy/mDuYTcDzrRpjuPpkVmEC8umTcPixOZcS6ZCruVY0IfMFDoDOAms4PZf DKew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=C0ZfDLSH; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id cr15-20020a170906d54f00b0078356aaeb61si4242271ejc.286.2022.11.03.23.41.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Nov 2022 23:41:28 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=C0ZfDLSH; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0DBDF385800D for ; Fri, 4 Nov 2022 06:41:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0DBDF385800D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667544087; 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=C0ZfDLSH5uPsLrVeaK/N8Yq2hGqbwHUjFuSTTrEIkg/56C/iYHAiDJGb730/IR5u6 JeECVX7WsCWa1Knug7eo8l6RqEfxB5P9SubAuV3bhMQrQByLcMMBSkfSMxp+PVl5TL Fy7t/+v/l6YZJCh4tzHlFGuzPLdGibTXYSDRk02k= 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748546710030271215?= X-GMAIL-MSGID: =?utf-8?q?1748546710030271215?= 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];