From patchwork Wed Jan 17 09:58:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 84239 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 402543858294 for ; Wed, 17 Jan 2024 09:59:30 +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 CB9F43858430 for ; Wed, 17 Jan 2024 09:58:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CB9F43858430 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 CB9F43858430 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=1705485530; cv=none; b=JgqAMMZcD7r8zAvJkyoCzwwkT2zpTCQmLSkSapk4iVZ23Zd8UaDArQvL03GUavndkDQWig5OGbXKEx+9AdShwgeaQaVbltUNuaDnqb7UnNR8ZFS9tUa2DjLsq8RMzXS9/v+dUOripYcmxGhWk5pjQrI0833er/AwDazwvIb8NTU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705485530; c=relaxed/simple; bh=xdwSq+1ewLu/bhKVfD2qj1zuMLlVnd32BGLPgFxlZNQ=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=oPEK28HuHXixggDbjEL+6kHyGAT8zNBpC782fU3nGVuy2FOBaf++scZMbqrfKtEeT7VBnc2gD23OEvQXXjcZuhNW+cSRaaP57RufkcrsqCwbpssMd92KwRiFufYiU5Y9/9tj2XiAxENIRIxCfTRNKnE9Zjbj21h2dFJKVHDu0Ro= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705485528; 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=pqfIBu87cw4z0T5GZ0PGwtbszpeBGbYwxGBIcKpvDj4=; b=U81x0PxfNXhli4dRr7d6TKHu9cpUSlyj2S63Jyv5hDDFMS0yMZRKnRXa/l0KfG/rDbYh1e IsGgZsg07lXDu9j6ScP2cuxJHB5DDBtsytU7D9nFrbnBtW9lK5TPEy+DOGDEJnifTLSac1 tEPi1DyoFRVyCvz96z9ae4Q4mu8UEfc= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-674-r-F6tBBbPlCcyknEft0FmQ-1; Wed, 17 Jan 2024 04:58:45 -0500 X-MC-Unique: r-F6tBBbPlCcyknEft0FmQ-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (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 18318185A782; Wed, 17 Jan 2024 09:58:45 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.70]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D07D7492BE6; Wed, 17 Jan 2024 09:58:44 +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 40H9wgiR2307502 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 17 Jan 2024 10:58:42 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 40H9wfq72307501; Wed, 17 Jan 2024 10:58:41 +0100 Date: Wed, 17 Jan 2024 10:58:41 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] lower-bitint: Avoid overlap between destinations and sources in libgcc calls [PR113421] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-4.3 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_H3, 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+patchwork=sourceware.org@gcc.gnu.org Hi! The following testcase is miscompiled because the bitint lowering emits a .MULBITINT (&a, 1024, &a, 1024, &x, 1024); call. The bug is in the overlap between the destination and source, that is something the libgcc routines don't handle, they use the source arrays during the entire algorithms which computes the destination array(s). For the mapping of SSA_NAMEs to VAR_DECLs the code already supports that correctly, but the checking whether a load from memory can be used directly without a temporary even when earlier we decided to merge the multiplication/division/modulo etc. with a store didn't. The following patch implements that. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-01-17 Jakub Jelinek PR tree-optimization/113421 * gimple-lower-bitint.cc (stmt_needs_operand_addr): Adjust function comment. (bitint_dom_walker::before_dom_children): Add g temporary to simplify formatting. Start at vop rather than cvop even if stmt is a store and needs_operand_addr. * gcc.dg/torture/bitint-50.c: New test. Jakub --- gcc/gimple-lower-bitint.cc.jj 2024-01-16 12:32:56.617721208 +0100 +++ gcc/gimple-lower-bitint.cc 2024-01-16 17:33:04.046476302 +0100 @@ -5455,7 +5455,8 @@ vuse_eq (ao_ref *, tree vuse1, void *dat /* Return true if STMT uses a library function and needs to take address of its inputs. We need to avoid bit-fields in those - cases. */ + cases. Similarly, we need to avoid overlap between destination + and source limb arrays. */ bool stmt_needs_operand_addr (gimple *stmt) @@ -5574,7 +5575,8 @@ bitint_dom_walker::before_dom_children ( else if (!bitmap_bit_p (m_loads, SSA_NAME_VERSION (s))) continue; - tree rhs1 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (s)); + gimple *g = SSA_NAME_DEF_STMT (s); + tree rhs1 = gimple_assign_rhs1 (g); if (needs_operand_addr && TREE_CODE (rhs1) == COMPONENT_REF && DECL_BIT_FIELD_TYPE (TREE_OPERAND (rhs1, 1))) @@ -5596,15 +5598,14 @@ bitint_dom_walker::before_dom_children ( ao_ref ref; ao_ref_init (&ref, rhs1); - tree lvop = gimple_vuse (SSA_NAME_DEF_STMT (s)); + tree lvop = gimple_vuse (g); unsigned limit = 64; tree vuse = cvop; if (vop != cvop && is_gimple_assign (stmt) && gimple_store_p (stmt) - && !operand_equal_p (lhs, - gimple_assign_rhs1 (SSA_NAME_DEF_STMT (s)), - 0)) + && (needs_operand_addr + || !operand_equal_p (lhs, gimple_assign_rhs1 (g), 0))) vuse = vop; if (vuse != lvop && walk_non_aliased_vuses (&ref, vuse, false, vuse_eq, --- gcc/testsuite/gcc.dg/torture/bitint-50.c.jj 2024-01-16 17:35:16.084622119 +0100 +++ gcc/testsuite/gcc.dg/torture/bitint-50.c 2024-01-16 17:35:06.701753879 +0100 @@ -0,0 +1,31 @@ +/* PR tree-optimization/113421 */ +/* { dg-do run { target bitint } } */ +/* { dg-options "-std=c23 -pedantic-errors" } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "-flto" } { "" } } */ + +#if __BITINT_MAXWIDTH__ >= 1024 +unsigned _BitInt(1024) a = -5wb; + +__attribute__((noipa)) void +foo (unsigned _BitInt(1024) x) +{ + a *= x; +} +#else +int a = 30; + +void +foo (int) +{ +} +#endif + +int +main () +{ + foo (-6wb); + if (a != 30wb) + __builtin_abort (); + return 0; +}