From patchwork Wed Nov 9 07:26:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xi Ruoyao X-Patchwork-Id: 17369 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp195428wru; Tue, 8 Nov 2022 23:27:43 -0800 (PST) X-Google-Smtp-Source: AMsMyM6CncJppbg9W0W6P8W1VUJ7VOAjM+lxuI6NoxHG7JWyU+UnCZ44w2Qs9LWp7CMDatNsmuBY X-Received: by 2002:a17:906:6a26:b0:7ad:975c:9785 with SMTP id qw38-20020a1709066a2600b007ad975c9785mr60169441ejc.25.1667978863386; Tue, 08 Nov 2022 23:27:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667978863; cv=none; d=google.com; s=arc-20160816; b=CJ/VrvUhOIGdODCNFtsODIKYQU+uTCADels9AnVPqgxyVso5uAbdo4kRsmF5lA7km2 1+yxDjir9oBISq9pgKV8o0Pt78ysAxehY2S3J+F4X74eBNCxNkPCkBF12YJgeba0Q+Qi W8L1w5a59mNrHU4hCNG3y5k8xg5dWVA0a2YULFj+L8MkWv8EDQe8ZiNvji4Ye6uXh+UW zp8yQ13LzCyunatTW9vQYGbZfVTHEHDwy1V5loa2mjWpflR2I2ZiCMRx7wc6mMlb36Sw uS97tq5CxOZ8xHJxvhO8tt165PVG3GgOk0VkHCJPf555geFwEEs9J6lFuc+Dqm+RSOGO uP6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to: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:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=b7R7cSgWI5QPx81T7BvJZixNGiS/nfHxVczvFLSI258=; b=hQ+g9qzcLTH9Hfa8XPmtbExms7Seu69g1K5b/xe3/8kVLKjBzpFNdMtq5y6IwLhCw8 Iu07Uv6Uw5xjnIX1hJJZ4ZS9f2VD+3B4j1ohXObuSE8VAsxuDLk/UsP3jVWXiiE/zHdU Z6Rzjf9DlTgciXI//0Kn6JNp2MJ3ilUSakuIhd/a6hm52qJHRbGMH9v+ODk8ZvOccsg3 rW4di43el1kJoQ+Vl07VdPN41bkwbdOY0HRL+7jijIa30KP0Y12pkevDIVhNssICsuWl ZBxaIKFYgaPNDaLPoop50so9SgcT46UNcUvHUmrSgn7hwF5qKxrVxiCL44P4muO31sTB FedA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=wZgCkfbA; 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 mp33-20020a1709071b2100b007acbac0871csi15060721ejc.420.2022.11.08.23.27.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Nov 2022 23:27:43 -0800 (PST) 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=wZgCkfbA; 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 1FEEB38582BD for ; Wed, 9 Nov 2022 07:27:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1FEEB38582BD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667978862; bh=b7R7cSgWI5QPx81T7BvJZixNGiS/nfHxVczvFLSI258=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=wZgCkfbAUN0oqMEEdCQebIp1GIA/ByF+A0a0AGLOwMqe0SlzmCy2KQGh1hLBbqc8v d9YryCPeSWtMnZIOMdDbQT7zuZAj97PKmuP4vwxjv2uH4iKCgg6A30wVzpyn1X9j/V AEnvBt9RqSngt1ZVhs+/Ww04JI/3TtmdJ7/hzqRU= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from xry111.site (xry111.site [89.208.246.23]) by sourceware.org (Postfix) with ESMTPS id D61313858039 for ; Wed, 9 Nov 2022 07:26:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D61313858039 Received: from xry111-x57s1.. (unknown [IPv6:240e:358:113a:1300:dc73:854d:832e:2]) (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 E991B667D5; Wed, 9 Nov 2022 02:26:53 -0500 (EST) To: gcc-patches@gcc.gnu.org Cc: Lulu Cheng , Wang Xuerui , Chenghua Xu , Xi Ruoyao Subject: [PATCH v2] LoongArch: fix signed overflow in loongarch_emit_int_compare Date: Wed, 9 Nov 2022 15:26:45 +0800 Message-Id: <20221109072645.790242-1-xry111@xry111.site> X-Mailer: git-send-email 2.38.1 MIME-Version: 1.0 X-Spam-Status: No, score=-6.3 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 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?1749002604491480994?= X-GMAIL-MSGID: =?utf-8?q?1749002604491480994?= 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];