From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by sourceware.org (Postfix) with ESMTPS id C6B043858D37 for ; Tue, 21 Nov 2023 08:26:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C6B043858D37 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C6B043858D37 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:67c:2178:6::1d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700555196; cv=none; b=a3WekgpNiREkCVo8aIUK2nYQQ8Q/AZtqLyMbxPD1esyfCPIVevzXa/HZbUDSbEG5Ar1pfrvdAc2iUc+QMIF+d8HAA5L/1dF7yoxTWpi10710sxi4KxHhgpsOX/nk2P39KzHuZv1fEqnvvFQmEhSxRcA21gwndaaRYm/mVfgT9dc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700555196; c=relaxed/simple; bh=guG+Gf5Tt/t5XFFHIr9WWYrViNAdJcsHhzUvH8x82Mc=; h=DKIM-Signature:DKIM-Signature:Date:From:To:Subject:Message-ID: MIME-Version; b=xDAF72pW67g1iUWJZH2apo10XSSf2NE1vkDd2CguDdz7/+iwCBefJCc3ISa84gKPXna9LmJ2uWl58AlWrwjOAbii2uLL0z2zGyvhNeSopbUwyM67UWuWROAhGJaVmlT9YXmodNq8sPlkcj8PHhyDXIZHvjrsQBPrqYNYy5ye3Hc= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id B822E1F8AE; Tue, 21 Nov 2023 08:26:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1700555190; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Bx6rdXxgwPc0Ty1ZD6BQhqs8TjQxfupd3VG8URYBl4o=; b=K5Eduv0arX4qi//A8WWL/RWIaqJdqLuxReROwWjoqauAFuspVA3KjYEWWLGQbs9gUra9nh BjUY05dmrq+fykH0aS16uKnrMNwFja5XejS6LZyj6vmbCJWyQxQoYYhoLRe5WVKYvyGCEB lNTGCqMc/exoOOo2UHXU9jDxSNbcMwk= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1700555190; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Bx6rdXxgwPc0Ty1ZD6BQhqs8TjQxfupd3VG8URYBl4o=; b=ag5Y9JUYIqv4nCqobxkX0005DHtkFTICwCfPG3MbAc+fXUx4lsnlpCL8jPtop+qPEA5vI+ gT72L2RdV1QCerBQ== Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id BA5FE2C146; Tue, 21 Nov 2023 08:26:30 +0000 (UTC) Date: Tue, 21 Nov 2023 08:26:30 +0000 (UTC) From: Richard Biener To: Jakub Jelinek cc: gcc-patches@gcc.gnu.org Subject: Re: [PATCH] builtins: Fix fold_builtin_query clzg/ctzg side-effects handling [PR112639] In-Reply-To: Message-ID: References: User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spamd-Bar: +++++++++++++ Authentication-Results: smtp-out2.suse.de; dkim=none; dmarc=none; spf=softfail (smtp-out2.suse.de: 149.44.160.134 is neither permitted nor denied by domain of rguenther@suse.de) smtp.mailfrom=rguenther@suse.de X-Rspamd-Server: rspamd1 X-Spamd-Result: default: False [13.99 / 50.00]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_SPAM_SHORT(3.00)[1.000]; MIME_GOOD(-0.10)[text/plain]; DMARC_NA(1.20)[suse.de]; R_SPF_SOFTFAIL(4.60)[~all:c]; RWL_MAILSPIKE_GOOD(-1.00)[149.44.160.134:from]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; MX_GOOD(-0.01)[]; RCPT_COUNT_TWO(0.00)[2]; VIOLATED_DIRECT_SPF(3.50)[]; NEURAL_SPAM_LONG(3.50)[1.000]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_NO_TLS_LAST(0.10)[]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(2.20)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; BAYES_HAM(-3.00)[100.00%] X-Spam-Score: 13.99 X-Rspamd-Queue-Id: B822E1F8AE X-Spam-Status: No, score=-5.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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 List-Id: On Tue, 21 Nov 2023, Jakub Jelinek wrote: > 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? OK. > 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. > > --- 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 (); > +} > > Jakub > > -- Richard Biener SUSE Software Solutions Germany GmbH, Frankenstrasse 146, 90461 Nuernberg, Germany; GF: Ivo Totev, Andrew McDonald, Werner Knoblich; (HRB 36809, AG Nuernberg)