From patchwork Tue Nov 21 08:15:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 167534 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2b07:b0:403:3b70:6f57 with SMTP id io7csp471039vqb; Tue, 21 Nov 2023 00:17:40 -0800 (PST) X-Google-Smtp-Source: AGHT+IGmWDJ4+xXN5tz+MQNqTYtFwYENeLIHgXsNRQ3y/5uwcwdPQlqRpHpoH53Hguzfsev/VoHW X-Received: by 2002:a05:6214:c28:b0:672:5ea5:fbec with SMTP id a8-20020a0562140c2800b006725ea5fbecmr11820980qvd.2.1700554660441; Tue, 21 Nov 2023 00:17:40 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1700554660; cv=pass; d=google.com; s=arc-20160816; b=ECp5ffugc7moOCaCw1vgMDGfBGTOOjI2qt04VCGe1wOKE2FuYu9dwvWZXyYE/Xs6Li lASHJr7z8viTNJiv+BuRGZk2KlAoihr0jKGPhduo5dLqV+dKgslIegKnv7amMQeQOZiD HibUDsx+aFoa2iByOzfhIau+bDv9HQChJBSg2C27SsDtUmOFfqsc0nGfmnTq8zqp2SAn 3Wu3aqxV50HPvA8guMABWNXz5rmpYhD6RG/ukP0smRC7pwcXZzV3Jwbub5l5l+SJfwQs AjYr1RUyJ5JNt4ABEUmIj87VeOplV9YRNkbQ7+v9+0QbOrJvyjrX2A3yMXssjrvLD0Kt NUhg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:reply-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition :mime-version:message-id:subject:cc:to:from:date:resent-to :resent-message-id:resent-date:resent-from:dkim-signature:arc-filter :dmarc-filter:delivered-to; bh=OfHEQO1jSFxrRnk/wQasm5/nPJasLQg+UV/ONe3nysU=; fh=FCjeRajqaQYHMkQtfIia8KT5yBac53mYOLLyJhYG/AY=; b=oh2ndoDiK0hWlhBziYcu4xRWPVqtOygf4YYMISTLr3j91I3npG5MDig/Y/enPTVUKY IQko9s/hWAG6EaSxhYjx88F/2pvZhsMQYWjm3QOVfagsRNed8HDdIeHKF1Y/s3HIt6dA Me7+s3no4jeHCGD6kD8xIRmjefT5j8v5jTzfsxSrenqHqhBwmmSkiBxlYoCZdgqC2PTE 4ecXh5o3TmjRnY/ybhkf43JHhYsLmENyt8a8DrzSGQ4UUr4TKu0MaZVdLjDDjI+R3jc+ gSRlGlbhXNGDylg/Y3fpN3PeDDKSwx2t/pdBBp+uro4hGiE67Zv4XDDbUI0v34jkpbZ4 /n0g== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ItA4Vegg; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id d1-20020a056214184100b00679e07d3d3dsi2524400qvy.59.2023.11.21.00.17.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Nov 2023 00:17:40 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b=ItA4Vegg; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 382DA3858402 for ; Tue, 21 Nov 2023 08:17:40 +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.129.124]) by sourceware.org (Postfix) with ESMTPS id D4E333858D35 for ; Tue, 21 Nov 2023 08:17:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D4E333858D35 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 D4E333858D35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700554632; cv=none; b=aHW+FIw2kd4FEWBTpqR65dimoDhqqmgn1pAiwzZ1MBtcUpgINrfFG2dh93Wn1FExawclX0v2rt1Uwdzj15Q/oRJdgb/p4cYoxuRW0eUkWqD3ECAAMFogbvt/Bfw9lgDyZSIgPnNJ8tpgpTNkppVPzn0GnBc2PI9MMFd77O+UDi0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700554632; c=relaxed/simple; bh=+TLpQvmCEOOf1vJuAz+4YH3kzzk5raea/0T0KnC93LI=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=uErpwdi8H3BI/a9icy/2ryIt/c7rRZxvU9YgXJFPhe4oiQK9zAJSpmvb9cFgaKS2l2vY7lm29bMNIcZ4E0slHpXe4piesKG3JFl3Mghjoqhx4j+coZCH8V3IPICps7MW0SQUAkDbGKVwLePPsPohiKhO5SgKxGqrCqFY6COrxeo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700554627; 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:resent-to:resent-from:resent-message-id; bh=OfHEQO1jSFxrRnk/wQasm5/nPJasLQg+UV/ONe3nysU=; b=ItA4VeggCTTAgDIcC5zUM4oknV9K/WYv5Ia5UqUPZVw1GhvEQg99gcpnXySLNLCMvbA+iE YGfa0lZmxtFwOkfmE3os/1hzvqlzsNz7VmyWz7EXROHVpOxrRnZAeyzHYlKNI+yl5OkYfc yFlsooIZv6jiXyCSqYoiph492NkxfY8= 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-97-BJHruDiXO8ig7C43ZdNcRQ-1; Tue, 21 Nov 2023 03:17:04 -0500 X-MC-Unique: BJHruDiXO8ig7C43ZdNcRQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (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 DEECB81408D; Tue, 21 Nov 2023 08:17:03 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.194.53]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A23AB1121306; Tue, 21 Nov 2023 08:17:03 +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 3AL8H08q4108943 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 21 Nov 2023 09:17:01 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 3AL8H0Sm4108942; Tue, 21 Nov 2023 09:17:00 +0100 Resent-From: Jakub Jelinek Resent-Date: Tue, 21 Nov 2023 09:17:00 +0100 Resent-Message-ID: Resent-To: Richard Biener , gcc-patches@gcc.gnu.org Date: Tue, 21 Nov 2023 09:15:21 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] builtins: Fix fold_builtin_query clzg/ctzg side-effects handling [PR112639] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 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, 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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783160803767288704 X-GMAIL-MSGID: 1783160803767288704 Hi! As the testcase shows, I've missed one spot where initially the code thinks it could use 2 argument IFN_CLZ/IFN_CTZ form, but then verifies it can't because it doesn't have the right target value and turns it into the arg0 ? arg1 : .C[LT]Z (arg0) form. That form evaluates the argument twice though and so needs save_expr, which I've missed to call in that case. In other cases where it is known from the beginning that it will be needed (e.g. the __builtin_clzg case on types smaller than unsigned int where we'll need to add an addend to the clz value) or the unsigned __int128 expansion called save_expr before. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-11-21 Jakub Jelinek PR middle-end/112639 * builtins.cc (fold_builtin_bit_query): If arg0 has side-effects, arg1 is specified but cleared, call save_expr on arg0. * gcc.dg/torture/pr112639.c: New test. Jakub --- gcc/builtins.cc.jj 2023-11-20 10:36:18.642625716 +0100 +++ gcc/builtins.cc 2023-11-20 15:25:59.665718971 +0100 @@ -9819,6 +9819,8 @@ fold_builtin_bit_query (location_t loc, if (!direct_internal_fn_supported_p (ifn, arg0_type, OPTIMIZE_FOR_BOTH)) arg2 = NULL_TREE; + if (arg2 == NULL_TREE) + arg0 = save_expr (arg0); } if (fcodei == END_BUILTINS || arg2) call = build_call_expr_internal_loc (loc, ifn, integer_type_node, --- gcc/testsuite/gcc.dg/torture/pr112639.c.jj 2023-11-20 15:32:24.994391656 +0100 +++ gcc/testsuite/gcc.dg/torture/pr112639.c 2023-11-20 15:34:14.395882993 +0100 @@ -0,0 +1,34 @@ +/* PR middle-end/112639 */ +/* { dg-do run } */ + +unsigned long long b = 0; + +int +foo (void) +{ + return __builtin_clzg (b++, __SIZEOF_LONG_LONG__ * __CHAR_BIT__); +} + +int +bar (void) +{ + return __builtin_ctzg (b++, __SIZEOF_LONG_LONG__ * __CHAR_BIT__); +} + +int +main () +{ + if (foo () != __SIZEOF_LONG_LONG__ * __CHAR_BIT__ || b != 1) + __builtin_abort (); + if (foo () != __SIZEOF_LONG_LONG__ * __CHAR_BIT__ - 1 || b != 2) + __builtin_abort (); + if (foo () != __SIZEOF_LONG_LONG__ * __CHAR_BIT__ - 2 || b != 3) + __builtin_abort (); + b = 0; + if (bar () != __SIZEOF_LONG_LONG__ * __CHAR_BIT__ || b != 1) + __builtin_abort (); + if (bar () != 0 || b != 2) + __builtin_abort (); + if (bar () != 1 || b != 3) + __builtin_abort (); +}