From patchwork Mon Dec 18 16:52:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: xndcn X-Patchwork-Id: 180570 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp1380419dyi; Mon, 18 Dec 2023 08:56:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IGeLr8vYdY1cV/Y1gLokRoFsPayUazBw+c+fA4a/jbgGiWL2esyhEF19krm9fhcK8gDLMgo X-Received: by 2002:a05:6122:138e:b0:4b6:e29f:5bbd with SMTP id m14-20020a056122138e00b004b6e29f5bbdmr10545vkp.25.1702918565221; Mon, 18 Dec 2023 08:56:05 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1702918565; cv=pass; d=google.com; s=arc-20160816; b=fzUW5Oq+tFBPcpoMWrwHT041kC95ESmvK7GKDo9JSX9jsF+aveKuhW1HS2+OdZ6ryO thAoFsC4ov6+Jbf8ORLvwEMwNh4b75L4RAXf6LoY1zzL6uGGoxAZjM9mMwTyC5nDxKmE tcCmd0g42V2MaGJgdd5b3jrgmcge6fIszv6Orsvdam+whB/m5ECBd+a+wbBLrOfOpCUZ btY6mDvh3Cj2sxBAg+pAUJv8qFCT1hg4CuD8rE+tsphR3vdxxLjZNncsFCJMquiDbsPm oEbLJCQvjoiqXpBfbvfP+YgYqX9HKMkXG1jFTfCXfm/oXiAfzWR6vAFZqw0U0J2nOFMb fh6A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=Q0qoCEjIZhv0tGoYguZkHjEDCnzlvc5e1tqBeGxnkWM=; fh=76mgaJSCU3dyOhKAj28QMcJGn2RmynbpDAfLvngqz3s=; b=J57lir5A2+tyhnYG7RQmP7JG9eNjSIVH04Iwxq77qvl/fPhvrFFHenqS+dCRpsRs5S 1yXFzfPcHXZOS5zkO+hirVF5zm0COiq4xx7yWKT8krzWqUDaGgNOCeg6U/wzZzE7nj9X hELKpBoqHhAIug2biLPUJwwa4qxwSyFg6dSKJs2CqGFs0PaFG4JVLDzY0ciubH7y+VFL 1sMLdGaPeCxSb0IqnQotfVE3f/DHNed9sd2B3eAVtQMNgrbNDX12N+eo9AX1aEm4vqMD xR+Oolzmx/WVhjjGE0mcr4sq06bJsUYXcP0YeM981Wg6AAoYL3MhkTA6QtdfK41MtcFr f/rg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=VCk9CKRb; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id z6-20020a05620a260600b00774067d659csi25773558qko.92.2023.12.18.08.56.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Dec 2023 08:56:05 -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=@gmail.com header.s=20230601 header.b=VCk9CKRb; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E8DD83857719 for ; Mon, 18 Dec 2023 16:56:04 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by sourceware.org (Postfix) with ESMTPS id 7000D38582AE for ; Mon, 18 Dec 2023 16:55:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7000D38582AE Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 7000D38582AE Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::630 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702918538; cv=none; b=V958Jj7ca6hk+kdQnptwq6T+bUuSEGFKN1s1syY8dLE8aE5ZvIQk9l+JTBdzK5wKwZeVf++65AIT7Jc0e9imzLxuukPuW7twrCGGe2hDdNku6rmqL611k2L3AuQhIvUSriBgNoG4MJ6X1XFhDgaRPwEnH9MhKbcLAstK4n1K4OM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702918538; c=relaxed/simple; bh=iy4x7MMA/DJoeWjFcu0D1IAVp5hOnitg8RtaLwWDn0E=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=SDNuoIx5ySDZhPS21guz+mxpjsh43gKzBWG8iOSjTfrknLdiLmYQhF7jCWhHQlbH0dffy4VS/m3Df0Bq26AcmDtPGJu5gTKZy84yuXw4qxsXf9OyK2nA2jeftW6yG+g4kbNy4wjNvOxPPjzYr9T3XtLdDc+IzRy1o9F+TmV/ktA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-1d3b60c5054so5584485ad.2 for ; Mon, 18 Dec 2023 08:55:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702918535; x=1703523335; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Q0qoCEjIZhv0tGoYguZkHjEDCnzlvc5e1tqBeGxnkWM=; b=VCk9CKRbaQqitGWZ7SIhWjMf8u5Q8UJi2hog9V9MAEbdVAiVFAuTejDpKtInYTB+rf FgwwnUuoqsUcY7Lanfz00CrBZjld9b3mQrhkxyS2uYwdTJyODGeAkPScrxwu6JnC9TUB XNZp1MzsEHWltfJ0XdQDhlPoo+39c08fYKGdCGcHqtWEjeMsZ47Xfow+c+JeOlsGt77E sP2B5njFL1c3qtNWurDl3KpnS5JPfn1xfNKJ+oXMLmLMkCiYFsht2VfUC0kybaN8Hhhm QVHOi5LyvyBJveul6aSk70zY0fVLN1MfTshtW6ZDZ6fHS3iMuDjZiA+nEMtM6007pi0q V+zA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702918535; x=1703523335; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Q0qoCEjIZhv0tGoYguZkHjEDCnzlvc5e1tqBeGxnkWM=; b=duUij5w03hOY1vVyEYWS9xRcOitWQ1N7PyACmxVHLNAXwoBNdJy73xN2cd5ofNFFGw /CWlGEg/ykIWDYk84SC2KJiPcY87nxVaC3ufgmhcla12MBUC7age52n++VIfmXfjWajN XYndnilzLeV/C+iOWkPcRgD0ILN4pGsQld3+zn6b62SOQZpY4GUhw9GF8JE7VC6+8HQg AohXEVTVaQs1MOQ7GIht66kBOqvwdDwZZoI8ak+4nTKXShlxpzgyjMZwZMnCVikwt5Mt LEKzAXHhkokTGSKYTVrGN7bHPuntC38q9coWf1TzBiGapHMLthW/e59waULSgqrREs+5 RfBw== X-Gm-Message-State: AOJu0YzX+jNIuWuEghKPIiSKvY+61KcWFxuEvy3ROz9FJLQjjg0LMcr+ 7AmCLawyCsc1WZyVl7yOB2Mtb23Xihd3Xg== X-Received: by 2002:a17:902:ecc5:b0:1d3:d0f4:fc71 with SMTP id a5-20020a170902ecc500b001d3d0f4fc71mr451913plh.132.1702918534583; Mon, 18 Dec 2023 08:55:34 -0800 (PST) Received: from localhost.localdomain ([222.131.165.216]) by smtp.googlemail.com with ESMTPSA id l6-20020a170902f68600b001d3be09f374sm1974876plg.275.2023.12.18.08.55.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Dec 2023 08:55:34 -0800 (PST) From: xndcn To: gcc-patches@gcc.gnu.org Cc: jakub@redhat.com, xndcn Subject: [PATCH] gimple-fold.cc: enable ATOMIC_COMPARE_EXCHANGE opt for floating type or types contain padding Date: Tue, 19 Dec 2023 00:52:21 +0800 Message-Id: <20231218165221.44037-2-xndchn@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231218165221.44037-1-xndchn@gmail.com> References: <20231218165221.44037-1-xndchn@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, 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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785639537215285255 X-GMAIL-MSGID: 1785639537215285255 gcc/ChangeLog: * gimple-fold.cc (optimize_atomic_compare_exchange_p): enable for SCALAR_FLOAT_TYPE_P type of expected var, or if TYPE_PRECISION is different from mode's bitsize (fold_builtin_atomic_compare_exchange): if TYPE_PRECISION is different from mode's bitsize, try to keep track all the bits and mix it with VIEW_CONVERT_EXPR(expected). Signed-off-by: xndcn --- gcc/gimple-fold.cc | 77 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 71 insertions(+), 6 deletions(-) diff --git a/gcc/gimple-fold.cc b/gcc/gimple-fold.cc index cb4b57250..321ff4f41 100644 --- a/gcc/gimple-fold.cc +++ b/gcc/gimple-fold.cc @@ -5306,12 +5306,7 @@ optimize_atomic_compare_exchange_p (gimple *stmt) || !auto_var_in_fn_p (TREE_OPERAND (expected, 0), current_function_decl) || TREE_THIS_VOLATILE (etype) || VECTOR_TYPE_P (etype) - || TREE_CODE (etype) == COMPLEX_TYPE - /* Don't optimize floating point expected vars, VIEW_CONVERT_EXPRs - might not preserve all the bits. See PR71716. */ - || SCALAR_FLOAT_TYPE_P (etype) - || maybe_ne (TYPE_PRECISION (etype), - GET_MODE_BITSIZE (TYPE_MODE (etype)))) + || TREE_CODE (etype) == COMPLEX_TYPE) return false; tree weak = gimple_call_arg (stmt, 3); @@ -5350,8 +5345,10 @@ fold_builtin_atomic_compare_exchange (gimple_stmt_iterator *gsi) tree itype = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (parmt))); tree ctype = build_complex_type (itype); tree expected = TREE_OPERAND (gimple_call_arg (stmt, 1), 0); + tree etype = TREE_TYPE (expected); bool throws = false; edge e = NULL; + tree allbits = NULL_TREE; gimple *g = gimple_build_assign (make_ssa_name (TREE_TYPE (expected)), expected); gsi_insert_before (gsi, g, GSI_SAME_STMT); @@ -5362,6 +5359,67 @@ fold_builtin_atomic_compare_exchange (gimple_stmt_iterator *gsi) build1 (VIEW_CONVERT_EXPR, itype, gimple_assign_lhs (g))); gsi_insert_before (gsi, g, GSI_SAME_STMT); + + // VIEW_CONVERT_EXPRs might not preserve all the bits. See PR71716. + // so we have to keep track all bits here. + if (maybe_ne (TYPE_PRECISION (etype), + GET_MODE_BITSIZE (TYPE_MODE (etype)))) + { + gimple_stmt_iterator cgsi + = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun))); + allbits = create_tmp_var (itype); + // allbits is initialized to 0, which can be ignored first time + gimple *init_stmt + = gimple_build_assign (allbits, build_int_cst (itype, 0)); + gsi_insert_before (&cgsi, init_stmt, GSI_SAME_STMT); + tree maskbits = create_tmp_var (itype); + // maskbits is initialized to full 1 (0xFFF...) + init_stmt = gimple_build_assign (maskbits, build1 (BIT_NOT_EXPR, + itype, allbits)); + gsi_insert_before (&cgsi, init_stmt, GSI_SAME_STMT); + + // g = g & maskbits + g = gimple_build_assign (make_ssa_name (itype), + build2 (BIT_AND_EXPR, itype, + gimple_assign_lhs (g), maskbits)); + gsi_insert_before (gsi, g, GSI_SAME_STMT); + + gimple *def_mask = gimple_build_assign ( + make_ssa_name (itype), + build2 (LSHIFT_EXPR, itype, build_int_cst (itype, 1), + build_int_cst (itype, TYPE_PRECISION (etype)))); + gsi_insert_before (gsi, def_mask, GSI_SAME_STMT); + def_mask = gimple_build_assign (make_ssa_name (itype), + build2 (MINUS_EXPR, itype, + gimple_assign_lhs (def_mask), + build_int_cst (itype, 1))); + gsi_insert_before (gsi, def_mask, GSI_SAME_STMT); + // maskbits = (1 << TYPE_PRECISION (etype)) - 1 + def_mask = gimple_build_assign (maskbits, SSA_NAME, + gimple_assign_lhs (def_mask)); + gsi_insert_before (gsi, def_mask, GSI_SAME_STMT); + + // paddingbits = (~maskbits) & allbits + def_mask + = gimple_build_assign (make_ssa_name (itype), + build1 (BIT_NOT_EXPR, itype, + gimple_assign_lhs (def_mask))); + gsi_insert_before (gsi, def_mask, GSI_SAME_STMT); + def_mask + = gimple_build_assign (make_ssa_name (itype), + build2 (BIT_AND_EXPR, itype, allbits, + gimple_assign_lhs (def_mask))); + gsi_insert_before (gsi, def_mask, GSI_SAME_STMT); + + // g = g | paddingbits, i.e., + // g = (VIEW_CONVERT_EXPR(expected) & maskbits) + // | (allbits &(~maskbits)) + g = gimple_build_assign (make_ssa_name (itype), + build2 (BIT_IOR_EXPR, itype, + gimple_assign_lhs (g), + gimple_assign_lhs (def_mask))); + gsi_insert_before (gsi, g, GSI_SAME_STMT); + } } int flag = (integer_onep (gimple_call_arg (stmt, 3)) ? 256 : 0) + int_size_in_bytes (itype); @@ -5410,6 +5468,13 @@ fold_builtin_atomic_compare_exchange (gimple_stmt_iterator *gsi) gsi_insert_after (gsi, g, GSI_NEW_STMT); if (!useless_type_conversion_p (TREE_TYPE (expected), itype)) { + // save all bits here + if (maybe_ne (TYPE_PRECISION (etype), + GET_MODE_BITSIZE (TYPE_MODE (etype)))) + { + g = gimple_build_assign (allbits, SSA_NAME, gimple_assign_lhs (g)); + gsi_insert_after (gsi, g, GSI_NEW_STMT); + } g = gimple_build_assign (make_ssa_name (TREE_TYPE (expected)), VIEW_CONVERT_EXPR, build1 (VIEW_CONVERT_EXPR, TREE_TYPE (expected),