From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2a07:de40:b251:101:10:150:64:1]) by sourceware.org (Postfix) with ESMTPS id 3ECE3385842D for ; Fri, 15 Dec 2023 07:46:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3ECE3385842D 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 3ECE3385842D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a07:de40:b251:101:10:150:64:1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702626420; cv=none; b=QcdCQbUgGx6cjIMFiyYRa/NzY7RxYY9ZdLEJNx/xeZEReZrqaNIeEGLuII5iw6rihqhpU0cs+8QBY0JAY0h+G/bSnzYGfe+EWdEUEYEC3H9O0cqwMlqgJspLce6AZ5zhykk0YFZu7MiHg/ZRdJ7hidTOlNuwT/rvLPFSTUW4GC0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702626420; c=relaxed/simple; bh=uYXeghAgLJQK1hoLSGuaBkyuqwoipTHtkC2z/BQrjUE=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:Message-ID:MIME-Version; b=NCOeE5V71CGQQMc4Ht6PEB1hV4st0v2iIQYecLmfAeVzcl68UZr2V7BHQCbDPqsgA6ZjHygWaR1Yi6qgXHA5ARCSamTTz1d2o7nvN1U+qrSpH0LQyz/oCMYrGESBvIY619AUkFB/uzqdGkddTPoCFdw8e+XPHbgz6Q0UA6x7QKA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from [10.168.4.150] (unknown [10.168.4.150]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 021962253E; Fri, 15 Dec 2023 07:46:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1702626418; 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=oMECzk/Jx7MEJSqNz7cdFoXay8sszBtIe5LyAUajtJQ=; b=v1/8g/eLNCI48m29p1RCfhy9Ug8lPfIRNvKtrJXlvJ1zVZu2eXMgzzMkYBQP1d40PFhy6G Kuecoe7Da+RKnR/SBvGnSWC8uA7v2oO63DqTU5Uj7rCx8O8taN5H7xLLpiq3N7HBkP9wVF uq+FEIjeVs4bphHDQvN23rrMMAw2rxQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1702626418; 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=oMECzk/Jx7MEJSqNz7cdFoXay8sszBtIe5LyAUajtJQ=; b=/XDGcLParx4lBxwZKeCHnMd/WCkp4ZMnqSJ2/WvbUfdcr1RkM2EQvOwkpGyoJKm93mDR49 bICnXBbOIbVla7AA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1702626418; 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=oMECzk/Jx7MEJSqNz7cdFoXay8sszBtIe5LyAUajtJQ=; b=v1/8g/eLNCI48m29p1RCfhy9Ug8lPfIRNvKtrJXlvJ1zVZu2eXMgzzMkYBQP1d40PFhy6G Kuecoe7Da+RKnR/SBvGnSWC8uA7v2oO63DqTU5Uj7rCx8O8taN5H7xLLpiq3N7HBkP9wVF uq+FEIjeVs4bphHDQvN23rrMMAw2rxQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1702626418; 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=oMECzk/Jx7MEJSqNz7cdFoXay8sszBtIe5LyAUajtJQ=; b=/XDGcLParx4lBxwZKeCHnMd/WCkp4ZMnqSJ2/WvbUfdcr1RkM2EQvOwkpGyoJKm93mDR49 bICnXBbOIbVla7AA== Date: Fri, 15 Dec 2023 08:45:52 +0100 (CET) From: Richard Biener To: Jakub Jelinek cc: gcc-patches@gcc.gnu.org Subject: Re: [PATCH] match.pd: Optimize sign-extension followed by truncation [PR113024] In-Reply-To: Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Level: X-Spam-Score: -3.10 X-Spam-Flag: NO X-Spamd-Result: default: False [-3.10 / 50.00]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; RCPT_COUNT_TWO(0.00)[2]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:email]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_COUNT_ZERO(0.00)[0]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; BAYES_HAM(-3.00)[100.00%] X-Spam-Level: Authentication-Results: smtp-out1.suse.de; none X-Spam-Score: -3.10 X-Spam-Status: No, score=-5.3 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 Thu, 14 Dec 2023, Jakub Jelinek wrote: > Hi! > > While looking at a bitint ICE, I've noticed we don't optimize > in f1 and f5 functions below the 2 casts into just one at GIMPLE, > even when optimize it in convert_to_integer if it appears in the same > stmt. The large match.pd simplification of two conversions in a row > has many complex rules and as the testcase shows, everything else from > the narrowest -> widest -> prec_in_between all integer conversions > is already handled, either because the inside_unsignedp == inter_unsignedp > rule kicks in, or the > && ((inter_unsignedp && inter_prec > inside_prec) > == (final_unsignedp && final_prec > inter_prec)) > one, but there is no reason why sign extension to from narrowest to > widest type followed by truncation to something in between can't be > done just as sign extension from narrowest to the final type. After all, > if the widest type is signed rather than unsigned, regardless of the final > type signedness we already handle it that way. > And since PR93044 we also handle it if the final precision is not wider > than the inside precision. > > Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? OK. Richard. > 2023-12-14 Jakub Jelinek > > PR tree-optimization/113024 > * match.pd (two conversions in a row): Simplify scalar integer > sign-extension followed by truncation. > > * gcc.dg/tree-ssa/pr113024.c: New test. > > --- gcc/match.pd.jj 2023-12-14 11:59:28.000000000 +0100 > +++ gcc/match.pd 2023-12-14 18:25:00.457961975 +0100 > @@ -4754,11 +4754,14 @@ (define_operator_list SYNC_FETCH_AND_AND > /* If we have a sign-extension of a zero-extended value, we can > replace that by a single zero-extension. Likewise if the > final conversion does not change precision we can drop the > - intermediate conversion. */ > + intermediate conversion. Similarly truncation of a sign-extension > + can be replaced by a single sign-extension. */ > (if (inside_int && inter_int && final_int > && ((inside_prec < inter_prec && inter_prec < final_prec > && inside_unsignedp && !inter_unsignedp) > - || final_prec == inter_prec)) > + || final_prec == inter_prec > + || (inside_prec < inter_prec && inter_prec > final_prec > + && !inside_unsignedp && inter_unsignedp))) > (ocvt @0)) > > /* Two conversions in a row are not needed unless: > --- gcc/testsuite/gcc.dg/tree-ssa/pr113024.c.jj 2023-12-14 18:35:30.652225327 +0100 > +++ gcc/testsuite/gcc.dg/tree-ssa/pr113024.c 2023-12-14 18:37:42.056403418 +0100 > @@ -0,0 +1,22 @@ > +/* PR tree-optimization/113024 */ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-forwprop1" } */ > +/* Make sure we have just a single cast per function rather than 2 casts in some cases. */ > +/* { dg-final { scan-tree-dump-times " = \\\(\[a-z \]*\\\) \[xy_\]" 16 "forwprop1" { target { ilp32 || lp64 } } } } */ > + > +unsigned int f1 (signed char x) { unsigned long long y = x; return y; } > +unsigned int f2 (unsigned char x) { unsigned long long y = x; return y; } > +unsigned int f3 (signed char x) { long long y = x; return y; } > +unsigned int f4 (unsigned char x) { long long y = x; return y; } > +int f5 (signed char x) { unsigned long long y = x; return y; } > +int f6 (unsigned char x) { unsigned long long y = x; return y; } > +int f7 (signed char x) { long long y = x; return y; } > +int f8 (unsigned char x) { long long y = x; return y; } > +unsigned int f9 (signed char x) { return (unsigned long long) x; } > +unsigned int f10 (unsigned char x) { return (unsigned long long) x; } > +unsigned int f11 (signed char x) { return (long long) x; } > +unsigned int f12 (unsigned char x) { return (long long) x; } > +int f13 (signed char x) { return (unsigned long long) x; } > +int f14 (unsigned char x) { return (unsigned long long) x; } > +int f15 (signed char x) { return (long long) x; } > +int f16 (unsigned char x) { return (long long) x; } > > Jakub > > -- Richard Biener SUSE Software Solutions Germany GmbH, Frankenstrasse 146, 90461 Nuernberg, Germany; GF: Ivo Totev, Andrew McDonald, Werner Knoblich; (HRB 36809, AG Nuernberg)