From patchwork Fri Feb 9 09:28:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1896892 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=ES99cx2n; 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 4TWT7j66hYz23hn for ; Fri, 9 Feb 2024 20:29:05 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CCC55385829E for ; Fri, 9 Feb 2024 09:29:03 +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 409333858438 for ; Fri, 9 Feb 2024 09:28:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 409333858438 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 409333858438 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=1707470918; cv=none; b=hu09FUypWmdKPr93rFCIBYUNrqbIAfRmpqXlX0t3wNP5OXsFWoSr80L86JywAVnREkUzbOgWSL5Qnm9vQ8V9ObPaR37AY/c6mqv0D3iJCEZc3ihVf2PgyBy0bZf/DE3vlf5LPzRJoFkrxvHQG8CfSqtkFAS1AUZQo8xKfn4LKX4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707470918; c=relaxed/simple; bh=SJsgmHQMEF61c3Zy3BCFkr1gP90wuHZ1c00xM1I9uyI=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=L7uW/jVKMBfHXPq28r2C/Djm0WA3qARSssRzSmWb279HuUc9UJ3whPhphjjSQMwrwOv9pMGS6ogWsj8VoD5o4Drl4+iMNuQ53J/hOliyEoCoQTkOR14LV059e43/Saghq2hFkLDPB945xsLQUYFXYoWGBn9HUeS0fjOZOROv1Yw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707470915; 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=uYyviMbv0eUdJw6mxKmK0S9+KCFReeNSBMceM/zEaRc=; b=ES99cx2nG41gCgnT/lBRIjyFoUb7W2aMkI6e42pphWKDXqHTbvdOMbiqXf1AQw5PTyKAMK w+xxc0CepaOxfj7xAQvyTAemk6DuQbG2txg8bQM2Fcop1Pn9WMYaipK6zRVaQ5VVbyZD1G IH1fms7+9C7SPkJdMqo4HmNOyHYU/Ew= 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-660-dyoUeqpkMmOFAeQ_Ypt90Q-1; Fri, 09 Feb 2024 04:28:32 -0500 X-MC-Unique: dyoUeqpkMmOFAeQ_Ypt90Q-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (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 7D85B1C05135; Fri, 9 Feb 2024 09:28:32 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.70]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 40CBBAD68; Fri, 9 Feb 2024 09:28: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 4199STI0640062 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 9 Feb 2024 10:28:29 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 4199SSOn640061; Fri, 9 Feb 2024 10:28:28 +0100 Date: Fri, 9 Feb 2024 10:28:28 +0100 From: Jakub Jelinek To: Richard Biener , Jeff Law , Vladimir Makarov Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] expand: Fix asm goto expansion [PR113415] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 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, KAM_SHORT, 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Hi! The asm goto expansion ICEs on the following testcase (which normally is rejected later), because expand_asm_stmt emits the code to copy the large var out of the out operand to its memory location into after_rtl_seq ... after_rtl_end sequence and because it is asm goto, it duplicates the sequence on each successor edge of the asm goto. The problem is that with -mstringop-strategy=byte_loop that sequence contains loops, so CODE_LABELs, JUMP_INSNs, with other strategies could contain CALL_INSNs etc. But the copying is done using a loop doing emit_insn (copy_insn (PATTERN (curr))); which does the right thing solely for INSNs, it will do the wrong thing for JUMP_INSNs, CALL_INSNs, CODE_LABELs (with RTL checking even ICE on them), BARRIERs and the like. The following patch partially fixes it (with the hope that such stuff only occurs in asms that really can't be accepted; if one uses say "=rm" or "=g" constraint then the operand uses the memory directly and nothing is copied) by using the duplicate_insn_chain function which is used e.g. in RTL loop unrolling and which can handle JUMP_INSNs, CALL_INSNs, BARRIERs etc. As it is meant to operate on sequences inside of basic blocks, it doesn't handle CODE_LABELs (well, it skips them), so if we need a solution that will be correct at runtime here for those cases, we'd need to do further work (e.g. still use duplicate_insn_chain, but if we notice any CODE_LABELs, walk the sequence again, add copies of the CODE_LABELs and then remap references to the old CODE_LABELs in the copied sequence to the new ones). Because as is now, if the code in one of the sequence copies (where the CODE_LABELs have been left out) decides to jump to such a CODE_LABEL, it will jump to the CODE_LABEL which has been in the original sequence (which the code emits on the last edge, after all, duplicating the sequence EDGE_COUNT times and throwing away the original was wasteful, compared to doing that just EDGE_COUNT - 1 times and using the original. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? Or do need to handle even CODE_LABELs? 2024-02-09 Jakub Jelinek PR middle-end/113415 * cfgexpand.cc (expand_asm_stmt): For asm goto, use duplicate_insn_chain to duplicate after_rtl_seq sequence instead of hand written loop with emit_insn of copy_insn and emit original after_rtl_seq on the last edge. * gcc.target/i386/pr113415.c: New test. Jakub --- gcc/cfgexpand.cc.jj 2024-01-24 13:11:21.011469855 +0100 +++ gcc/cfgexpand.cc 2024-02-08 18:22:04.699621085 +0100 @@ -3671,16 +3671,21 @@ expand_asm_stmt (gasm *stmt) { edge e; edge_iterator ei; - + unsigned int cnt = EDGE_COUNT (gimple_bb (stmt)->succs); + FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs) { - start_sequence (); - for (rtx_insn *curr = after_rtl_seq; - curr != NULL_RTX; - curr = NEXT_INSN (curr)) - emit_insn (copy_insn (PATTERN (curr))); - rtx_insn *copy = get_insns (); - end_sequence (); + rtx_insn *copy; + if (--cnt == 0) + copy = after_rtl_seq; + else + { + start_sequence (); + duplicate_insn_chain (after_rtl_seq, after_rtl_end, + NULL, NULL); + copy = get_insns (); + end_sequence (); + } insert_insn_on_edge (copy, e); } } --- gcc/testsuite/gcc.target/i386/pr113415.c.jj 2024-02-08 18:26:27.622966847 +0100 +++ gcc/testsuite/gcc.target/i386/pr113415.c 2024-02-08 18:26:11.336193222 +0100 @@ -0,0 +1,11 @@ +/* PR middle-end/113415 */ +/* { dg-do compile } */ +/* { dg-options "-mstringop-strategy=byte_loop" } */ + +void +foo (void) +{ + unsigned long arr[64]; +lab: + __asm__ goto ("" : "=r" (arr) : : : lab); /* { dg-error "impossible constraint in 'asm'" } */ +}