From patchwork Wed Jun 12 02:41:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: HAO CHEN GUI X-Patchwork-Id: 1946620 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=AE+9DnpS; dkim-atps=neutral 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=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.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 (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VzVF81wfdz20KL for ; Wed, 12 Jun 2024 12:42:18 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EBC99385DDCD for ; Wed, 12 Jun 2024 02:42:16 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by sourceware.org (Postfix) with ESMTPS id DDC873858D34 for ; Wed, 12 Jun 2024 02:41:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DDC873858D34 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=linux.ibm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linux.ibm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org DDC873858D34 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=148.163.156.1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718160116; cv=none; b=mGOTK2oyRAcB+/xfcdLJERIP2m4frHsXWLnRO8JSHDGD6qGmWffg0fcNzcDqrwBir8dmrF/eFLiE09QV73shlU03kRBO7vEw0VJw0aLvclpdKBuWpcrP7s8LRMTPB1U/Mg1Rcf2tdqx8JvsTlQgpuwY2gMalYJRlkLxIA9Zbfwk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718160116; c=relaxed/simple; bh=ir3fgo8HgrByqQp6w2J1MhfcP/VxSzIfHPwbWVgBLPc=; h=DKIM-Signature:Message-ID:Date:To:From:Subject:MIME-Version; b=FHTyDOfUiUjHNF0A13xJfQ0JlTz38XD7m5MrZag4lPEWz7TMWMke6YWAtYbKppnRJeR80L+V5Wxl5IxdIEQ1mnoW0Su7YF+mh5sBAugdQkr2xQsNB59cTp6r8pgo2sxG0NOJIcTmNRDGhAkX9xv9AKwLLLwOen4sFidGHSofRXE= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0353729.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 45C2Pjg6025698; Wed, 12 Jun 2024 02:41:50 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h= message-id:date:to:cc:from:subject:content-type :content-transfer-encoding:mime-version; s=pp1; bh=aNG/XPnfx54nG +sQxTkj25RRQC9J0CJclL3xeDJiksM=; b=AE+9DnpSyMrdmefNXnmUsL33Wijdb nQ4OklNboScXv7AyoQElSZOq1DHwab6IWd9KGC2YckNdkYyGC5oTlqF5lcjLUo96 ST0DS5YNdvXngXlmJDwhGJZI1ZWnCTi4ykEEEmx5F4wRf+3Ed0PrxWYx7anz1PNd /hFlAS8SrfQo4sM+NWFRlJO6eKZkG8P8eT4J9E/txc9jUSvo7MIoDc+1Oa3Yp7Fk AQFnTlLATXWjhjk9OlsPdXGEAQ+MLYmSyJGsbcbUBk3jgrvIhvRXwWIdGhbglaMk 3EImat48MBw5DhSZmrsne3UOPqkhHGsI7maXpX/efd45ox3gotoxJ1JQw== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3ypydbrcq9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 12 Jun 2024 02:41:50 +0000 (GMT) Received: from m0353729.ppops.net (m0353729.ppops.net [127.0.0.1]) by pps.reinject (8.18.0.8/8.18.0.8) with ESMTP id 45C2fnEv017294; Wed, 12 Jun 2024 02:41:49 GMT Received: from ppma13.dal12v.mail.ibm.com (dd.9e.1632.ip4.static.sl-reverse.com [50.22.158.221]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3ypydbrcq6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 12 Jun 2024 02:41:49 +0000 (GMT) Received: from pps.filterd (ppma13.dal12v.mail.ibm.com [127.0.0.1]) by ppma13.dal12v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 45C0lECL023597; Wed, 12 Jun 2024 02:41:48 GMT Received: from smtprelay03.fra02v.mail.ibm.com ([9.218.2.224]) by ppma13.dal12v.mail.ibm.com (PPS) with ESMTPS id 3yn3umh956-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 12 Jun 2024 02:41:48 +0000 Received: from smtpav07.fra02v.mail.ibm.com (smtpav07.fra02v.mail.ibm.com [10.20.54.106]) by smtprelay03.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 45C2fhfi44499314 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 12 Jun 2024 02:41:45 GMT Received: from smtpav07.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5BD332004E; Wed, 12 Jun 2024 02:41:43 +0000 (GMT) Received: from smtpav07.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7A73B20040; Wed, 12 Jun 2024 02:41:41 +0000 (GMT) Received: from [9.200.103.244] (unknown [9.200.103.244]) by smtpav07.fra02v.mail.ibm.com (Postfix) with ESMTP; Wed, 12 Jun 2024 02:41:41 +0000 (GMT) Message-ID: Date: Wed, 12 Jun 2024 10:41:41 +0800 User-Agent: Mozilla Thunderbird Content-Language: en-US To: gcc-patches Cc: Segher Boessenkool , David , "Kewen.Lin" , Peter Bergner , Jakub Jelinek , Richard Sandiford From: HAO CHEN GUI Subject: [PATCH-1v3] fwprop: Replace rtx_cost with insn_cost in try_fwprop_subst_pattern [PR113325] X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: WmmmkGNkR_e_x-EdDwYbJpDbjLPVdTEw X-Proofpoint-GUID: SvD__jpgGzwxQcAsXqwT1iT414W_8eJc X-Proofpoint-UnRewURL: 0 URL was un-rewritten MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-06-11_13,2024-06-11_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 priorityscore=1501 lowpriorityscore=0 phishscore=0 impostorscore=0 suspectscore=0 mlxscore=0 bulkscore=0 spamscore=0 mlxlogscore=999 malwarescore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2405170001 definitions=main-2406120015 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, 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: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Hi, This patch replaces rtx_cost with insn_cost in forward propagation. In the PR, one constant vector should be propagated and replace a pseudo in a store insn if we know it's a duplicated constant vector. It reduces the insn cost but not rtx cost. In this case, the cost is determined by destination operand (memory or pseudo). Unfortunately, rtx cost can't help. The test case is added in the second rs6000 specific patch. Compared to previous version, the main changes are: 1. Invoke change_is_worthwhile to judge if the cost is reduced and the replacement is worthwhile. 2. Invalidate recog data before getting the insn cost for the new rtl as insn cost might call extract_constrain_insn_cached and extract_insn_cached to cache the recog data. The cache data is invalid for the new rtl and it causes ICE. 3. Check if the insn cost of new rtl is zero which means unknown cost. The replacement should be rejected at this situation. Previous version https://gcc.gnu.org/pipermail/gcc-patches/2024-May/651233.html The patch causes a regression cases on i386 as the pattern cost regulation has a bug. Please refer the patch and discussion here. https://gcc.gnu.org/pipermail/gcc-patches/2024-May/651363.html Bootstrapped and tested on powerpc64-linux BE and LE with no regressions. Is it OK for the trunk? ChangeLog fwprop: invoke change_is_worthwhile to judge if a replacement is worthwhile gcc/ * fwprop.cc (try_fwprop_subst_pattern): Invoke change_is_worthwhile to judge if a replacement is worthwhile. * rtl-ssa/changes.cc (rtl_ssa::changes_are_worthwhile): Invalidate recog data before getting the insn cost for the new rtl. Check if the insn cost of new rtl is unknown and fail the replacement. patch.diff diff --git a/gcc/fwprop.cc b/gcc/fwprop.cc index de543923b92..975de0eec7f 100644 --- a/gcc/fwprop.cc +++ b/gcc/fwprop.cc @@ -471,29 +471,19 @@ try_fwprop_subst_pattern (obstack_watermark &attempt, insn_change &use_change, redo_changes (0); } - /* ??? In theory, it should be better to use insn costs rather than - set_src_costs here. That would involve replacing this code with - change_is_worthwhile. */ bool ok = recog (attempt, use_change); - if (ok && !prop.changed_mem_p () && !use_insn->is_asm ()) - if (rtx use_set = single_set (use_rtl)) - { - bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (use_rtl)); - temporarily_undo_changes (0); - auto old_cost = set_src_cost (SET_SRC (use_set), - GET_MODE (SET_DEST (use_set)), speed); - redo_changes (0); - auto new_cost = set_src_cost (SET_SRC (use_set), - GET_MODE (SET_DEST (use_set)), speed); - if (new_cost > old_cost - || (new_cost == old_cost && !prop.likely_profitable_p ())) - { - if (dump_file) - fprintf (dump_file, "change not profitable" - " (cost %d -> cost %d)\n", old_cost, new_cost); - ok = false; - } - } + if (ok && !prop.changed_mem_p () && !use_insn->is_asm () + && single_set (use_rtl)) + { + if (!change_is_worthwhile (use_change, false) + || (!prop.likely_profitable_p () + && !change_is_worthwhile (use_change, true))) + { + if (dump_file) + fprintf (dump_file, "change not profitable"); + ok = false; + } + } if (!ok) { diff --git a/gcc/rtl-ssa/changes.cc b/gcc/rtl-ssa/changes.cc index 11639e81bb7..9bad6c2070c 100644 --- a/gcc/rtl-ssa/changes.cc +++ b/gcc/rtl-ssa/changes.cc @@ -185,7 +185,18 @@ rtl_ssa::changes_are_worthwhile (array_slice changes, * change->old_cost ()); if (!change->is_deletion ()) { + /* Invalidate recog data as insn_cost may call + extract_insn_cached. */ + INSN_CODE (change->rtl ()) = -1; change->new_cost = insn_cost (change->rtl (), for_speed); + /* If the cost is unknown, replacement is not worthwhile. */ + if (!change->new_cost) + { + if (dump_file && (dump_flags & TDF_DETAILS)) + fprintf (dump_file, + "Reject replacement due to unknown insn cost.\n"); + return false; + } new_cost += change->new_cost; if (for_speed) weighted_new_cost += (cfg_bb->count.to_sreal_scale (entry_count)