From patchwork Tue Feb 6 10:15:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 197302 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1440401dyb; Tue, 6 Feb 2024 02:16:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IGxdrZ2dcjjLoJWoo6u5IogDuL/dSXSam5yVndJtDWhG1IRxPswkqI6geRDdMOk2ujKQkUL X-Received: by 2002:ad4:5d49:0:b0:68c:8525:a349 with SMTP id jk9-20020ad45d49000000b0068c8525a349mr2464677qvb.29.1707214607054; Tue, 06 Feb 2024 02:16:47 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCXAIimJyxGF3LUXQLQjueM2xryVSasZubxpWwMSLzRhgOvpVReezOR4rOw+jSo9mjYGq4FytkZRXu1wsuOlbCV2uHiQcQ== Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id hf13-20020a0562140e8d00b0068c7f4c8c6esi2368290qvb.333.2024.02.06.02.16.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 02:16:47 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=fail header.i=@redhat.com header.s=mimecast20190719 header.b=X2fWz018; arc=fail (signature failed); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=fail (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 9FF8C3858C2A for ; Tue, 6 Feb 2024 10:16:45 +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.133.124]) by sourceware.org (Postfix) with ESMTPS id DFE893858D33 for ; Tue, 6 Feb 2024 10:15:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DFE893858D33 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 DFE893858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707214557; cv=none; b=uOJijMZYoM+viwbYwqaPiNyWRPZLCl6QibMYgVr6gYq9ovzzZd7fHJbsX59huIJ6KQ2yXx1DPM5UBCmNJgg03nF6Xa+qSVQx8h7YTylO7u8buAXCX7nZAGe7Ot2SoIQhX9gPHwT9ipe75CWvXrPgvj03V4let3otmSNcn/RMcQ8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707214557; c=relaxed/simple; bh=Uf4G5ElacizV9yeJAEsUW76b53ndijkW7UEqUURosGA=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=w7V3qbnvaMmSq2BEfUGL2RRVJzJzUcVhGRmVRgv1+yrXBXRwfp7elad3UJhc470JoOpQYPcIvAbTU1eFlvCU0r6XjoE0maEm2bAfhwO7B8vLBbhT0C1lkbZGuEwIsO9qz5GqZCmxdUqV0bFQ2b59OKAbssGOGxPngvu8U9CvvZQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707214554; 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: content-transfer-encoding:content-transfer-encoding; bh=BOcuEXE6QMQNHUzUCKEysCNIEKqEQ70cGxZ0YGKkG3Q=; b=X2fWz018Sh3Hf+ygC2czlx4S87mlD7FteWs8F5JWppwJGsvrHjQJ6Xm0SIag55/9rIRgs9 NjDddPZaZWAroZHDLkX/JXPqnCLaOkajWcqSJydkuIjBorjNY9vNO6poTW5vTg/RTznRxV IUYL9XE8rm5A0DAX3byOqOUDDvV8ZMg= 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-634-s7e5bt0pOTGJXQ1B2ZskSQ-1; Tue, 06 Feb 2024 05:15:51 -0500 X-MC-Unique: s7e5bt0pOTGJXQ1B2ZskSQ-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 0AF3338562C0; Tue, 6 Feb 2024 10:15:51 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.70]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C2B5C1C060AF; Tue, 6 Feb 2024 10:15:50 +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 416AFmiC3214863 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 6 Feb 2024 11:15:48 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 416AFlkx3214862; Tue, 6 Feb 2024 11:15:47 +0100 Date: Tue, 6 Feb 2024 11:15:47 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-ssa-math-opts: Fix up convert_{mult, plusminus}_to_widen [PR113759] 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.8 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_H5, 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: 1790144263749225407 X-GMAIL-MSGID: 1790144263749225407 Hi! On the following testcase we emit invalid stmt: error: type mismatch in ‘widen_mult_plus_expr’ 6 | foo (int c, int b) | ^~~ unsigned long int unsigned int unsigned long _31 = WIDEN_MULT_PLUS_EXPR ; The recent PR113560 r14-8680 changes tweaked convert_mult_to_widen, but didn't change convert_plusminus_to_widen for the TREE_TYPE (rhsN) != typeN cases, but looking at this, it was already before that change quite weird. Earlier in those functions it determines actual_precision and from_unsignedN and wants to use that precision and signedness for the operands and it used build_and_insert_cast for that (which emits a cast stmt, even for INTEGER_CSTs) and later on for INTEGER_CST arguments fold_converted them to typeN (which is unclear to me why, because it seems to have assumed that TREE_TYPE (rhsN) is typeN, for the actual_precision or from_unsignedN cases it would be wrong except that build_and_insert_cast forced a SSA_NAME and so it doesn't trigger anymore). Now, since r14-8680 it is possible that rhsN also has some other type from typeN and we again want to cast. The following patch changes this, so that for the differences in actual_precision and/or from_unsignedN we actually update typeN and then use it as the type to convert the arguments to if it isn't useless, for INTEGER_CSTs by just fold_converting, otherwise using build_and_insert_cast. And uses useless_type_conversion_p test so that we don't convert unless necessary. Plus by doing that effectively also doing the important part of the r14-8680 convert_mult_to_widen changes in convert_plusminus_to_widen. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-02-06 Jakub Jelinek PR tree-optimization/113759 * tree-ssa-math-opts.cc (convert_mult_to_widen): If actual_precision or from_unsignedN differs from properties of typeN, update typeN to build_nonstandard_integer_type. If TREE_TYPE (rhsN) is not uselessly convertible to typeN, convert it using fold_convert or build_and_insert_cast depending on if rhsN is INTEGER_CST or not. (convert_plusminus_to_widen): Likewise. * gcc.c-torture/compile/pr113759.c: New test. Jakub --- gcc/tree-ssa-math-opts.cc.jj 2024-02-02 11:26:43.730589763 +0100 +++ gcc/tree-ssa-math-opts.cc 2024-02-05 10:23:16.068489814 +0100 @@ -2865,25 +2865,25 @@ convert_mult_to_widen (gimple *stmt, gim if (2 * actual_precision > TYPE_PRECISION (type)) return false; if (actual_precision != TYPE_PRECISION (type1) - || from_unsigned1 != TYPE_UNSIGNED (type1) - || (TREE_TYPE (rhs1) != type1 - && TREE_CODE (rhs1) != INTEGER_CST)) - rhs1 = build_and_insert_cast (gsi, loc, - build_nonstandard_integer_type - (actual_precision, from_unsigned1), rhs1); + || from_unsigned1 != TYPE_UNSIGNED (type1)) + type1 = build_nonstandard_integer_type (actual_precision, from_unsigned1); + if (!useless_type_conversion_p (type1, TREE_TYPE (rhs1))) + { + if (TREE_CODE (rhs1) == INTEGER_CST) + rhs1 = fold_convert (type1, rhs1); + else + rhs1 = build_and_insert_cast (gsi, loc, type1, rhs1); + } if (actual_precision != TYPE_PRECISION (type2) - || from_unsigned2 != TYPE_UNSIGNED (type2) - || (TREE_TYPE (rhs2) != type2 - && TREE_CODE (rhs2) != INTEGER_CST)) - rhs2 = build_and_insert_cast (gsi, loc, - build_nonstandard_integer_type - (actual_precision, from_unsigned2), rhs2); - - /* Handle constants. */ - if (TREE_CODE (rhs1) == INTEGER_CST) - rhs1 = fold_convert (type1, rhs1); - if (TREE_CODE (rhs2) == INTEGER_CST) - rhs2 = fold_convert (type2, rhs2); + || from_unsigned2 != TYPE_UNSIGNED (type2)) + type2 = build_nonstandard_integer_type (actual_precision, from_unsigned2); + if (!useless_type_conversion_p (type2, TREE_TYPE (rhs2))) + { + if (TREE_CODE (rhs2) == INTEGER_CST) + rhs2 = fold_convert (type2, rhs2); + else + rhs2 = build_and_insert_cast (gsi, loc, type2, rhs2); + } gimple_assign_set_rhs1 (stmt, rhs1); gimple_assign_set_rhs2 (stmt, rhs2); @@ -3086,26 +3086,28 @@ convert_plusminus_to_widen (gimple_stmt_ actual_precision = GET_MODE_PRECISION (actual_mode); if (actual_precision != TYPE_PRECISION (type1) || from_unsigned1 != TYPE_UNSIGNED (type1)) - mult_rhs1 = build_and_insert_cast (gsi, loc, - build_nonstandard_integer_type - (actual_precision, from_unsigned1), - mult_rhs1); + type1 = build_nonstandard_integer_type (actual_precision, from_unsigned1); + if (!useless_type_conversion_p (type1, TREE_TYPE (mult_rhs1))) + { + if (TREE_CODE (mult_rhs1) == INTEGER_CST) + mult_rhs1 = fold_convert (type1, mult_rhs1); + else + mult_rhs1 = build_and_insert_cast (gsi, loc, type1, mult_rhs1); + } if (actual_precision != TYPE_PRECISION (type2) || from_unsigned2 != TYPE_UNSIGNED (type2)) - mult_rhs2 = build_and_insert_cast (gsi, loc, - build_nonstandard_integer_type - (actual_precision, from_unsigned2), - mult_rhs2); + type2 = build_nonstandard_integer_type (actual_precision, from_unsigned2); + if (!useless_type_conversion_p (type2, TREE_TYPE (mult_rhs2))) + { + if (TREE_CODE (mult_rhs2) == INTEGER_CST) + mult_rhs2 = fold_convert (type2, mult_rhs2); + else + mult_rhs2 = build_and_insert_cast (gsi, loc, type2, mult_rhs2); + } if (!useless_type_conversion_p (type, TREE_TYPE (add_rhs))) add_rhs = build_and_insert_cast (gsi, loc, type, add_rhs); - /* Handle constants. */ - if (TREE_CODE (mult_rhs1) == INTEGER_CST) - mult_rhs1 = fold_convert (type1, mult_rhs1); - if (TREE_CODE (mult_rhs2) == INTEGER_CST) - mult_rhs2 = fold_convert (type2, mult_rhs2); - gimple_assign_set_rhs_with_ops (gsi, wmult_code, mult_rhs1, mult_rhs2, add_rhs); update_stmt (gsi_stmt (*gsi)); --- gcc/testsuite/gcc.c-torture/compile/pr113759.c.jj 2024-02-05 10:34:35.017036427 +0100 +++ gcc/testsuite/gcc.c-torture/compile/pr113759.c 2024-02-05 10:34:24.682179591 +0100 @@ -0,0 +1,20 @@ +/* PR tree-optimization/113759 */ + +extern short t[]; + +int +foo (int c, int b) +{ + if (b < 0) + __builtin_unreachable (); + if (c <= 0) + __builtin_unreachable (); + int d; + for (; c >= 0; c--) + { + int a = b + c; + d = t[a]; + t[a] = 0; + } + return d; +}