From patchwork Thu Dec 14 22:14:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 178932 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp8885811dys; Thu, 14 Dec 2023 14:15:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IH1Yk3R5cDVpjg33W5Z7DYXIw55+9Wpc2wEHY+VAjcCRiQGlZqSZCSUQdqTRV6jwKqSrzJC X-Received: by 2002:a05:620a:480e:b0:77f:74d:8bb0 with SMTP id eb14-20020a05620a480e00b0077f074d8bb0mr11959625qkb.42.1702592102122; Thu, 14 Dec 2023 14:15:02 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1702592102; cv=pass; d=google.com; s=arc-20160816; b=W9yBSmlr6wWn/KRlwFDwx+UBsLiFyMF/aUlt0LSpQh/hp1ZA5hNNxFekgHfna/pts/ kqPgMatRmjNKEtDp/mrF3YTIZkjwapQfvXoTLoa0Zz6iI96Vecu1cCrmmoeB7amW/uv9 TocFtwrC1NS/nhn5LGXyk8B87jn8Gr5bE1nM1cSOsgRoEj+BR8XtqBb/v09IshNIXuRh EBjaWn3xh8rKmgPalAOqCCQ57iOMp+ARfD02kiUGRf8vDfhJSgW8p7PvkR+xas7fEZCl lxapOdp+ebtDfrlSDpObLQ8djmKWX63jBjSgrN3FAlETSSk46DRZd3xMej1oYLZkJIWZ ZtpA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:reply-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=NNwMh1xinuVIymWyVDT4I2bBIrGfZ3uUSe8LOQ5iHvk=; fh=FCjeRajqaQYHMkQtfIia8KT5yBac53mYOLLyJhYG/AY=; b=JGoWkPF4DS6lIyHeegE/Vp8UmxJsBGv6UeMmLZuexJ7sqgUD3QIm1hhvFMVIpurb3/ 2vFKsf/TjBBMCCHYF15hQcDo+9rmtvgxIEI1g0Qe1bGuh3X7d/4I4ADhSCHIOFIcu9tJ JFSRM4XMT3XHnyVeya/K7Exwk66GCHElJ6rWQ7FZ71yRRViBJDIUZ+6eAvLC909DH+X3 cHZrGfQ+bJLZgZMdDeqF63DszIO/c9CJmTw6dApPt1Ibtn/57wB5B5xON0hHSHx/+h+w NZpreIjIJomfLfvRcqwhLQt/KUzMiQDLDKEk/UtQKck/6WWk6AERD6p1o8hzqXdCjCNQ ZDjQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=dazqBcXd; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id h22-20020a05620a245600b0077d883cb7b1si12693844qkn.238.2023.12.14.14.15.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 14:15:02 -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=@redhat.com header.s=mimecast20190719 header.b=dazqBcXd; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C9C7038618FC for ; Thu, 14 Dec 2023 22:15:01 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 232313858D20 for ; Thu, 14 Dec 2023 22:14:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 232313858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 232313858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702592078; cv=none; b=ctOK9vbegYDtS+liSHlJPv91/RB8nN49ffI0wFiEyANNU4FbF+OQfJmeh4Fo6om/YQ52LLLfzgN2LRzXaUTcTwSmKLE1RQ+LFGvCgNaGX7KqDVq6mI8VM+b9jzR88gtcdjkYJJkGbruHaIlnwHIFCTj5d8Zbt+bAAZtyspVX0u4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702592078; c=relaxed/simple; bh=VtjzwCfz1SkJpOn1UPxuz3n0HeUA5sc6izzgVtBLU98=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=VEKIWerIXMI7otXKDprYWAXvHSavbzEoJuWvEcFrUy3eEEeuhDxcScAZ5LBheZGCUY9WvqQ2Rn574nDT/pZET9b1OkIpI8LK9XyCmYgyFt2im7r1WikVd3VJLG1qdebNKn++UdPTg73vYaQLIllmBjnrCD0yb+ipS9EOUMUyAEY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702592075; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=NNwMh1xinuVIymWyVDT4I2bBIrGfZ3uUSe8LOQ5iHvk=; b=dazqBcXdpHYc8OevfYVKyDA8wBvhTFS8VmraDqf5tcWa8q1aA+eAIYQPcigzfYX7myuulE N4UXGkwFzlPZ2UTS7PwOAQs+yoOVsZFZB1WVWNULJ9aPvlKBaI4QVIC+op14xiZ2/zP94M XoR+175vgCKoMqb2gKwykemRGVr4aw0= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-511-N0uVonYRP8ScSFY0Erol7w-1; Thu, 14 Dec 2023 17:14:32 -0500 X-MC-Unique: N0uVonYRP8ScSFY0Erol7w-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id ABA4638562C0; Thu, 14 Dec 2023 22:14:32 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.92]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6E14D1C060B1; Thu, 14 Dec 2023 22:14:32 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 3BEMETsA3914761 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 14 Dec 2023 23:14:30 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 3BEMETXI3914760; Thu, 14 Dec 2023 23:14:29 +0100 Date: Thu, 14 Dec 2023 23:14:28 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] lower-bitint: Fix .{ADD,SUB,MUL}_OVERFLOW with _BitInt large/huge INTEGER_CST arguments [PR113003] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785297216011321862 X-GMAIL-MSGID: 1785297216011321862 Hi! As shown in the testcase, .{ADD,SUB,MUL}_OVERFLOW calls are another exception to the middle/large/huge _BitInt discovery through SSA_NAMEs next to stores of INTEGER_CSTs to memory and their conversions to floating point. The calls can have normal COMPLEX_TYPE with INTEGER_TYPE elts return type (or BITINT_TYPE with small precision) and one of the arguments can be SSA_NAME with an INTEGER_TYPE or small BITINT_TYPE as well; still, when there is an INTEGER_CST argument with large/huge BITINT_TYPE, we need to lower it that way. Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-12-14 Jakub Jelinek PR tree-optimization/113003 * gimple-lower-bitint.cc (arith_overflow_arg_kind): New function. (gimple_lower_bitint): Use it to catch .{ADD,SUB,MUL}_OVERFLOW calls with large/huge INTEGER_CST arguments. * gcc.dg/bitint-54.c: New test. Jakub --- gcc/gimple-lower-bitint.cc.jj 2023-12-13 11:36:21.636633517 +0100 +++ gcc/gimple-lower-bitint.cc 2023-12-14 13:28:17.660737468 +0100 @@ -5641,6 +5641,37 @@ build_bitint_stmt_ssa_conflicts (gimple def (live, lhs, graph); } +/* If STMT is .{ADD,SUB,MUL}_OVERFLOW with INTEGER_CST arguments, + return the largest bitint_prec_kind of them, otherwise return + bitint_prec_small. */ + +static bitint_prec_kind +arith_overflow_arg_kind (gimple *stmt) +{ + bitint_prec_kind ret = bitint_prec_small; + if (is_gimple_call (stmt) && gimple_call_internal_p (stmt)) + switch (gimple_call_internal_fn (stmt)) + { + case IFN_ADD_OVERFLOW: + case IFN_SUB_OVERFLOW: + case IFN_MUL_OVERFLOW: + for (int i = 0; i < 2; ++i) + { + tree a = gimple_call_arg (stmt, i); + if (TREE_CODE (a) == INTEGER_CST + && TREE_CODE (TREE_TYPE (a)) == BITINT_TYPE) + { + bitint_prec_kind kind = bitint_precision_kind (TREE_TYPE (a)); + ret = MAX (ret, kind); + } + } + break; + default: + break; + } + return ret; +} + /* Entry point for _BitInt(N) operation lowering during optimization. */ static unsigned int @@ -5657,7 +5688,12 @@ gimple_lower_bitint (void) continue; tree type = TREE_TYPE (s); if (TREE_CODE (type) == COMPLEX_TYPE) - type = TREE_TYPE (type); + { + if (arith_overflow_arg_kind (SSA_NAME_DEF_STMT (s)) + != bitint_prec_small) + break; + type = TREE_TYPE (type); + } if (TREE_CODE (type) == BITINT_TYPE && bitint_precision_kind (type) != bitint_prec_small) break; @@ -5745,7 +5781,12 @@ gimple_lower_bitint (void) continue; tree type = TREE_TYPE (s); if (TREE_CODE (type) == COMPLEX_TYPE) - type = TREE_TYPE (type); + { + if (arith_overflow_arg_kind (SSA_NAME_DEF_STMT (s)) + >= bitint_prec_large) + has_large_huge = true; + type = TREE_TYPE (type); + } if (TREE_CODE (type) == BITINT_TYPE && bitint_precision_kind (type) >= bitint_prec_large) { @@ -6245,8 +6286,7 @@ gimple_lower_bitint (void) { bitint_prec_kind this_kind = bitint_precision_kind (TREE_TYPE (t)); - if (this_kind > kind) - kind = this_kind; + kind = MAX (kind, this_kind); } if (is_gimple_assign (stmt) && gimple_store_p (stmt)) { @@ -6255,8 +6295,7 @@ gimple_lower_bitint (void) { bitint_prec_kind this_kind = bitint_precision_kind (TREE_TYPE (t)); - if (this_kind > kind) - kind = this_kind; + kind = MAX (kind, this_kind); } } if (is_gimple_assign (stmt) @@ -6268,21 +6307,22 @@ gimple_lower_bitint (void) { bitint_prec_kind this_kind = bitint_precision_kind (TREE_TYPE (t)); - if (this_kind > kind) - kind = this_kind; + kind = MAX (kind, this_kind); } } if (is_gimple_call (stmt)) { t = gimple_call_lhs (stmt); - if (t - && TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE - && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) == BITINT_TYPE) + if (t && TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE) { - bitint_prec_kind this_kind - = bitint_precision_kind (TREE_TYPE (TREE_TYPE (t))); - if (this_kind > kind) - kind = this_kind; + bitint_prec_kind this_kind = arith_overflow_arg_kind (stmt); + kind = MAX (kind, this_kind); + if (TREE_CODE (TREE_TYPE (TREE_TYPE (t))) == BITINT_TYPE) + { + this_kind + = bitint_precision_kind (TREE_TYPE (TREE_TYPE (t))); + kind = MAX (kind, this_kind); + } } } if (kind == bitint_prec_small) --- gcc/testsuite/gcc.dg/bitint-54.c.jj 2023-12-14 13:47:46.683512224 +0100 +++ gcc/testsuite/gcc.dg/bitint-54.c 2023-12-14 13:47:20.191879500 +0100 @@ -0,0 +1,21 @@ +/* PR tree-optimization/113003 */ +/* { dg-do compile { target bitint } } */ +/* { dg-options "-std=c23 -O2" } */ + +#if __BITINT_MAXWIDTH__ >= 131 +int +foo (_BitInt(7) x) +{ + return __builtin_mul_overflow_p (x, 1046555807606105294475452482332716433408wb, 0); +} + +#ifdef __SIZEOF_INT128__ +int +bar (unsigned __int128 x) +{ + return __builtin_sub_overflow_p (340282366920938463463374607431768211457uwb, x, 0); +} +#endif +#else +int i; +#endif