From patchwork Wed Aug 30 07:56:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 137148 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a7d1:0:b0:3f2:4152:657d with SMTP id p17csp4389041vqm; Wed, 30 Aug 2023 00:57:27 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHVPkUJBVhbHmC8xwqXXU5tmnyVX209f82GH0EAp3d7KRYIqNa7BWPJXkMKZE24TTPZKmna X-Received: by 2002:a17:906:7499:b0:99b:f58d:1c49 with SMTP id e25-20020a170906749900b0099bf58d1c49mr1269938ejl.53.1693382247482; Wed, 30 Aug 2023 00:57:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693382247; cv=none; d=google.com; s=arc-20160816; b=TjWN5vbWRNbQX52OqemZs8ic6CEm9sNPkYuQEKIJglRPFCzMUejm6kcTxm5Gbvq+FZ pnI0SQAaGAszIOxtsEyW32tpL8QdpU9QUBWuN3uyUFWOy/RavYSuNoqq2enLfBKQ42J3 09umgRAbbX99ADr6+ISRluyMg53IcIkmZ04tUMVPnRabOBgI8O8HD1e8EVACss4TOqLW h26NopTKzdCsT76qGGMGKkG07vfXYxsECHH1VvnyR3OVOT4hFNorgNxvcGT+UVcMPZMY cQ+XgclT95TBrgw+BHuMw2kkVyr6bJba2r6mOxBTd+QnlHswfVYdm7iQgPEVXZiRpZco On+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=dxx3w3kiiRhkv0BZCPYHNuqxYQr9tASLENiKTnNEXe8=; fh=FCjeRajqaQYHMkQtfIia8KT5yBac53mYOLLyJhYG/AY=; b=gl+NpEE97ndCi/294+tUZKgUmcpAE0fIAGSvbUCJ09u5qBTosjX7BzMMBVOm0ppJGA e6xzIyjtCywSnJz38utAwViOi4nD1nnaFTWQNBJKSDifbw/IaqEBNwXbDCGlYhIXbnAp BEswpAHNeTtX3MDjU5s3UFrHaicjSWQsnlmoBKgwnJeTVVZH+MvhJrJA21prk7CMMCee /O3zxg2ZOJrRjuwBix3oWaO01j6VeZkC59hJU/EkzzEUNcissryKpeVyOaYbKqePchak H5dr5z+EHXYicbLNXD6N5x98yde0nLrNDqtGaWrFA7EWlYuoveW2oL3KNuBr1yKW5Lr+ cIGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=cXC6rRiH; 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=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id bl3-20020a170906c24300b0099bd5bedfcbsi6919818ejb.422.2023.08.30.00.57.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Aug 2023 00:57:27 -0700 (PDT) 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=pass header.i=@gcc.gnu.org header.s=default header.b=cXC6rRiH; 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=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 089893858438 for ; Wed, 30 Aug 2023 07:57:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 089893858438 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1693382238; bh=dxx3w3kiiRhkv0BZCPYHNuqxYQr9tASLENiKTnNEXe8=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=cXC6rRiHa3uXkU01uJOq1kUUGkGAZn/hZQoIhRRLCXp8TYYzskZ7+vzSgOPROEwRT lYL9fhqLjtMAwR+rRmdmo+j7kdFW83J+Csy/5XIWRrTjYOaJua/698QUFwTbyjnLNe zc0hPkbsJZygQWCEB2aRthiz+eYVCYtlbmps4X/A= 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 914303858C41 for ; Wed, 30 Aug 2023 07:56:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 914303858C41 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-20-PoArkwB-OYatoP_lrpaV1Q-1; Wed, 30 Aug 2023 03:56:31 -0400 X-MC-Unique: PoArkwB-OYatoP_lrpaV1Q-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 89E78810C2B; Wed, 30 Aug 2023 07:56:31 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.16]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4FF0140C6F4C; Wed, 30 Aug 2023 07:56:31 +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 37U7uTae2048299 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 30 Aug 2023 09:56:29 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 37U7uS0g2048298; Wed, 30 Aug 2023 09:56:28 +0200 Date: Wed, 30 Aug 2023 09:56:28 +0200 To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] store-merging: Fix up >= 64 bit insertion [PR111015] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.4 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_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1775639983716393707 X-GMAIL-MSGID: 1775639983716393707 Hi! The following testcase shows that we mishandle bit insertion for info->bitsize >= 64. The problem is in using unsigned HOST_WIDE_INT shift + subtraction + build_int_cst to compute mask, the shift invokes UB at compile time for info->bitsize 64 and larger and e.g. on the testcase with info->bitsize happens to compute mask of 0x3f rather than 0x3f'ffffffff'ffffffff. The patch fixes that by using wide_int wi::mask + wide_int_to_tree, so it handles masks in any precision (up to WIDE_INT_MAX_PRECISION ;) ). Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk and backports? 2023-08-30 Jakub Jelinek PR tree-optimization/111015 * gimple-ssa-store-merging.cc (imm_store_chain_info::output_merged_store): Use wi::mask and wide_int_to_tree instead of unsigned HOST_WIDE_INT shift and build_int_cst to build BIT_AND_EXPR mask. * gcc.dg/pr111015.c: New test. Jakub --- gcc/gimple-ssa-store-merging.cc.jj 2023-07-11 13:40:39.049448058 +0200 +++ gcc/gimple-ssa-store-merging.cc 2023-08-29 16:13:12.808434272 +0200 @@ -4687,12 +4687,13 @@ imm_store_chain_info::output_merged_stor } else if ((BYTES_BIG_ENDIAN ? start_gap : end_gap) > 0) { - const unsigned HOST_WIDE_INT imask - = (HOST_WIDE_INT_1U << info->bitsize) - 1; + wide_int imask + = wi::mask (info->bitsize, false, + TYPE_PRECISION (TREE_TYPE (tem))); tem = gimple_build (&seq, loc, BIT_AND_EXPR, TREE_TYPE (tem), tem, - build_int_cst (TREE_TYPE (tem), - imask)); + wide_int_to_tree (TREE_TYPE (tem), + imask)); } const HOST_WIDE_INT shift = (BYTES_BIG_ENDIAN ? end_gap : start_gap); --- gcc/testsuite/gcc.dg/pr111015.c.jj 2023-08-29 16:06:38.526938204 +0200 +++ gcc/testsuite/gcc.dg/pr111015.c 2023-08-29 16:19:03.702536015 +0200 @@ -0,0 +1,28 @@ +/* PR tree-optimization/111015 */ +/* { dg-do run { target int128 } } */ +/* { dg-options "-O2" } */ + +struct S { unsigned a : 4, b : 4; unsigned __int128 c : 70; } d; + +__attribute__((noipa)) void +foo (unsigned __int128 x, unsigned char y, unsigned char z) +{ + d.a = y; + d.b = z; + d.c = x; +} + +int +main () +{ + foo (-1, 12, 5); + if (d.a != 12 + || d.b != 5 + || d.c != (-1ULL | (((unsigned __int128) 0x3f) << 64))) + __builtin_abort (); + foo (0x123456789abcdef0ULL | (((unsigned __int128) 26) << 64), 7, 11); + if (d.a != 7 + || d.b != 11 + || d.c != (0x123456789abcdef0ULL | (((unsigned __int128) 26) << 64))) + __builtin_abort (); +}