From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from server.nextmovesoftware.com (server.nextmovesoftware.com [162.254.253.69]) by sourceware.org (Postfix) with ESMTPS id 5A72B3858400 for ; Tue, 26 Jul 2022 12:13:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5A72B3858400 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=nextmovesoftware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=nextmovesoftware.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=nextmovesoftware.com; s=default; h=Content-Type:MIME-Version:Message-ID: Date:Subject:In-Reply-To:References:Cc:To:From:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=Ku0vZWeXHk2o8ojn1P+PbPzPTORCyVUdJa/JRH8xQZ4=; b=OmCIH13QAQk9pQvhGZ5R1/jo1A Zedqo5/LgumY+Hf2r/CpJCTfDBZ6ACMSCotVo/bOylr5zY4XDb8Zdhsab+khMvU+xJnLtjqszM8aY yqQVjTZt21dvf93aghr+LrEbBE/5uYD4gMjnWOfUZwwHCrS+6ocid7cyGyqJpcpG2tMUELTAgkjUE rmBqgdOW6Y9A57SRBqR8rkVHEi0tCUdBnqzBGO7aeoG4Gk88iPqBx+vwrV3OkQJG99/znpK7ZCnaG IG8PC75vuGWKAZbLGdh9tKHlYakM6TQoCxvo5M2G7uNdRyeKRFotGl4ZeJ9OHAQHvAd1b9ZecZbPF vz/IWoqw==; Received: from host86-167-159-17.range86-167.btcentralplus.com ([86.167.159.17]:63228 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1oGJQu-00024z-FU; Tue, 26 Jul 2022 08:13:04 -0400 From: "Roger Sayle" To: "'Segher Boessenkool'" Cc: References: <003601d89245$a86f8830$f94e9890$@nextmovesoftware.com> <20220707223833.GA25951@gate.crashing.org> In-Reply-To: <20220707223833.GA25951@gate.crashing.org> Subject: [PATCH] Add new target hook: simplify_modecc_const. Date: Tue, 26 Jul 2022 13:13:02 +0100 Message-ID: <00fa01d8a0e9$0efdfc60$2cf9f520$@nextmovesoftware.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_00FB_01D8A0F1.70C4FC70" X-Mailer: Microsoft Outlook 16.0 Thread-Index: Adig5j+5uPv68wE+QNqqfMmbUU+2tQ== Content-Language: en-gb X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - server.nextmovesoftware.com X-AntiAbuse: Original Domain - gcc.gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - nextmovesoftware.com X-Get-Message-Sender-Via: server.nextmovesoftware.com: authenticated_id: roger@nextmovesoftware.com X-Authenticated-Sender: server.nextmovesoftware.com: roger@nextmovesoftware.com X-Source: X-Source-Args: X-Source-Dir: X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_BARRACUDACENTRAL, SPF_HELO_NONE, SPF_PASS, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 26 Jul 2022 12:13:07 -0000 This is a multipart message in MIME format. ------=_NextPart_000_00FB_01D8A0F1.70C4FC70 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit This patch is a major revision of the patch I originally proposed here: https://gcc.gnu.org/pipermail/gcc-patches/2022-July/598040.html The primary motivation of this patch is to avoid incorrect optimization of MODE_CC comparisons in simplify_const_relational_operation when/if a backend represents the (known) contents of a MODE_CC register using a CONST_INT. In such cases, the RTL optimizers don't know the semantics of this integer value, so shouldn't change anything (i.e. should return NULL_RTX from simplify_const_relational_operation). The secondary motivation is that by introducing a new target hook, called simplify_modecc_const, the backend can (optionally) encode and interpret a target dependent encoding of MODE_CC registers. The worked example provided with this patch is to allow the i386 backend to explicitly model the carry flag (MODE_CCC) using 1 to indicate that the carry flag is set, and 0 to indicate the carry flag is clear. This allows the instructions stc (set carry flag), clc (clear carry flag) and cmc (complement carry flag) to be represented in RTL. However an even better example would be the rs6000 backend, where this patch/target hook would allow improved modelling of the condition register CR. The powerpc's comparison instructions set fields/bits in the CR register [where bit 0 indicates less than, bit 1 greater than, bit 2 equal to and bit3 overflow] analogous to x86's flags register [containing bits for carry, zero, overflow, parity etc.]. These fields can be manipulated directly using crset (aka creqv) and crclr (aka crxor) instructions and even transferred from general purpose registers using mtcr. However, without a patch like this, it's impossible to safely model/represent these instructions in rs6000.md. This patch has been tested on x86_64-pc-linux-gnu with make bootstrap and make -k check, both with and without --target_board=unix{-m32}, and both with and without a patch to add stc, clc and cmc support to the x86 backend. I'll resubmit the x86 target pieces again with that follow-up backend patch, so for now I'm only looking for approval of the middle-end infrastructure pieces. The x86 hunks below are provided as context/documentation for how this hook could/should be used (but I wouldn't object to pre-approval of those bits by Uros). Ok for mainline? 2022-07-26 Roger Sayle gcc/ChangeLog * target.def (simplify_modecc_const): New target hook. * doc/tm.texi (TARGET_SIMPLIFY_MODECC_CONST): Document here. * doc/tm.texi.in (TARGET_SIMPLIFY_MODECC_CONST): Locate @hook here. * hooks.cc (hook_rtx_mode_int_rtx_null): Define default hook here. * hooks.h (hook_rtx_mode_int_rtx_null): Prototype here. * simplify-rtx.c (simplify_const_relational_operation): Avoid mis-optimizing MODE_CC comparisons by calling new target hook. * config/i386.cc (ix86_simplify_modecc_const): Implement new target hook, supporting interpreting MODE_CCC values as the x86 carry flag. (TARGET_SIMPLIFY_MODECC_CONST): Define as ix86_simplify_modecc_const. Thanks in advance, Roger -- > -----Original Message----- > From: Segher Boessenkool > Sent: 07 July 2022 23:39 > To: Roger Sayle > Cc: gcc-patches@gcc.gnu.org > Subject: Re: [PATCH] Be careful with MODE_CC in > simplify_const_relational_operation. > > Hi! > > On Thu, Jul 07, 2022 at 10:08:04PM +0100, Roger Sayle wrote: > > I think it's fair to describe RTL's representation of condition flags > > using MODE_CC as a little counter-intuitive. > > "A little challenging", and you should see that as a good thing, as a puzzle to > crack :-) > > > For example, the i386 > > backend represents the carry flag (in adc instructions) using RTL of > > the form "(ltu:SI (reg:CCC) (const_int 0))", where great care needs to > > be taken not to treat this like a normal RTX expression, after all LTU > > (less-than-unsigned) against const0_rtx would normally always be > > false. > > A comparison of a MODE_CC thing against 0 means the result of a > *previous* comparison (or other cc setter) is looked at. Usually it simply looks > at some condition bits in a flags register. It does not do any actual comparison: > that has been done before (if at all even). > > > Hence, MODE_CC comparisons need to be treated with caution, and > > simplify_const_relational_operation returns early (to avoid > > problems) when GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC. > > Not just to avoid problems: there simply isn't enough information to do a > correct job. > > > However, consider the (currently) hypothetical situation, where the > > RTL optimizers determine that a previous instruction unconditionally > > sets or clears the carry flag, and this gets propagated by combine > > into the above expression, we'd end up with something that looks like > > (ltu:SI (const_int 1) (const_int 0)), which doesn't mean what it says. > > Fortunately, simplify_const_relational_operation is passed the > > original mode of the comparison (cmp_mode, the original mode of op0) > > which can be checked for MODE_CC, even when op0 is now VOIDmode > > (const_int) after the substitution. Defending against this is clearly > > the right thing to do. > > > > More controversially, rather than just abort > > simplification/optimization in this case, we can use the comparison > > operator to infer/select the semantics of the CC_MODE flag. > > Hopefully, whenever a backend uses LTU, it represents the (set) carry > > flag (and behaves like i386.md), in which case the result of the simplified > expression is the first operand. > > [If there's no standardization of semantics across backends, then we > > should always just return 0; but then miss potential optimizations]. > > On PowerPC, ltu means the result of an unsigned comparison (we have > instructions for that, cmpl[wd][i] mainly) was "smaller than". It does not mean > anything is unsigned smaller than zero. It also has nothing to do with carries, > which are done via a different register (the XER). > > > + /* Handle MODE_CC comparisons that have been simplified to > > + constants. */ > > + if (GET_MODE_CLASS (mode) == MODE_CC > > + && op1 == const0_rtx > > + && CONST_INT_P (op0)) > > + { > > + /* LTU represents the carry flag. */ > > + if (code == LTU) > > + return op0 == const0_rtx ? const0_rtx : const_true_rtx; > > + return 0; > > + } > > + > > /* We can't simplify MODE_CC values since we don't know what the > > actual comparison is. */ > > ^^^ > This comment is 100% true. We cannot simplify any MODE_CC comparison > without having more context. The combiner does have that context when it > tries to combine the CC setter with the CC consumer, for example. > > Do you have some piece of motivating example code? > > > Segher ------=_NextPart_000_00FB_01D8A0F1.70C4FC70 Content-Type: text/plain; name="patchcf3.txt" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="patchcf3.txt" diff --git a/gcc/target.def b/gcc/target.def=0A= index 2a7fa68..cd81b4e 100644=0A= --- a/gcc/target.def=0A= +++ b/gcc/target.def=0A= @@ -7107,6 +7107,20 @@ counters are incremented using atomic operations. = Targets not supporting\n\=0A= type.",=0A= HOST_WIDE_INT, (void), default_gcov_type_size)=0A= =0A= +DEFHOOK=0A= +(simplify_modecc_const,=0A= + "On targets that represent the result of a comparison using a\n\=0A= +@code{MODE_CC} flags register, allow the backend to represent a known\n\=0A= +result using a target-dependent integer constant. This helper = function\n\=0A= +of @code{simplify_const_relational_operation} attempts to simplify a\n\=0A= +comparison operator @var{code} that is defined by a @code{COMPARE} in = mode\n\=0A= +@var{mode}, which is known to be of class @code{MODE_CC}, and is = encoded\n\=0A= +as @var{val}, which must be a @code{CONST_INT}. The result must be\n\=0A= +@code{const_true_rtx}, @code{const0_rtx} or @code{NULL_RTX} if no\n\=0A= +simplification is possible.",=0A= + rtx, (machine_mode mode, int code, rtx val),=0A= + hook_rtx_mode_int_rtx_null)=0A= +=0A= /* This value represents whether the shadow call stack is implemented on=0A= the target platform. */=0A= DEFHOOKPOD=0A= diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi=0A= index b0ea398..5a1231d 100644=0A= --- a/gcc/doc/tm.texi=0A= +++ b/gcc/doc/tm.texi=0A= @@ -6631,6 +6631,18 @@ post-reload comparison elimination pass, or the = delay slot filler pass,=0A= then this value should be set appropriately.=0A= @end deftypevr=0A= =0A= +@deftypefn {Target Hook} rtx TARGET_SIMPLIFY_MODECC_CONST (machine_mode = @var{mode}, int @var{code}, rtx @var{val})=0A= +On targets that represent the result of a comparison using a=0A= +@code{MODE_CC} flags register, allow the backend to represent a known=0A= +result using a target-dependent integer constant. This helper function=0A= +of @code{simplify_const_relational_operation} attempts to simplify a=0A= +comparison operator @var{code} that is defined by a @code{COMPARE} in = mode=0A= +@var{mode}, which is known to be of class @code{MODE_CC}, and is encoded=0A= +as @var{val}, which must be a @code{CONST_INT}. The result must be=0A= +@code{const_true_rtx}, @code{const0_rtx} or @code{NULL_RTX} if no=0A= +simplification is possible.=0A= +@end deftypefn=0A= +=0A= @node Costs=0A= @section Describing Relative Costs of Operations=0A= @cindex costs of instructions=0A= diff --git a/gcc/doc/tm.texi.in b/gcc/doc/tm.texi.in=0A= index f869ddd..8cecde7 100644=0A= --- a/gcc/doc/tm.texi.in=0A= +++ b/gcc/doc/tm.texi.in=0A= @@ -4397,6 +4397,8 @@ like:=0A= =0A= @hook TARGET_FLAGS_REGNUM=0A= =0A= +@hook TARGET_SIMPLIFY_MODECC_CONST=0A= +=0A= @node Costs=0A= @section Describing Relative Costs of Operations=0A= @cindex costs of instructions=0A= diff --git a/gcc/hooks.cc b/gcc/hooks.cc=0A= index b29233f..90be303 100644=0A= --- a/gcc/hooks.cc=0A= +++ b/gcc/hooks.cc=0A= @@ -401,6 +401,14 @@ hook_rtx_tree_int_null (tree, int)=0A= return NULL;=0A= }=0A= =0A= +/* Generic hook that takes a machine mode, an int and an rtx and=0A= + returns NULL_RTX. */=0A= +rtx=0A= +hook_rtx_mode_int_rtx_null (machine_mode, int, rtx)=0A= +{=0A= + return NULL;=0A= +}=0A= +=0A= /* Generic hook that takes a machine mode and returns an unsigned int = 0. */=0A= unsigned int=0A= hook_uint_mode_0 (machine_mode)=0A= diff --git a/gcc/hooks.h b/gcc/hooks.h=0A= index 1056e1e..e978817 100644=0A= --- a/gcc/hooks.h=0A= +++ b/gcc/hooks.h=0A= @@ -123,6 +123,7 @@ extern bool default_can_output_mi_thunk_no_vcall = (const_tree, HOST_WIDE_INT,=0A= extern rtx hook_rtx_rtx_identity (rtx);=0A= extern rtx hook_rtx_rtx_null (rtx);=0A= extern rtx hook_rtx_tree_int_null (tree, int);=0A= +extern rtx hook_rtx_mode_int_rtx_null (machine_mode, int, rtx);=0A= =0A= extern char *hook_charptr_void_null (void);=0A= extern const char *hook_constcharptr_void_null (void);=0A= diff --git a/gcc/simplify-rtx.cc b/gcc/simplify-rtx.cc=0A= index fa20665..699e08a 100644=0A= --- a/gcc/simplify-rtx.cc=0A= +++ b/gcc/simplify-rtx.cc=0A= @@ -6012,6 +6012,13 @@ simplify_const_relational_operation (enum = rtx_code code,=0A= || (GET_MODE (op0) =3D=3D VOIDmode=0A= && GET_MODE (op1) =3D=3D VOIDmode));=0A= =0A= + /* Handle MODE_CC comparisons that have been simplified to=0A= + constants. */=0A= + if (GET_MODE_CLASS (mode) =3D=3D MODE_CC=0A= + && op1 =3D=3D const0_rtx=0A= + && CONST_INT_P (op0))=0A= + return targetm.simplify_modecc_const (mode, (int)code, op0);=0A= +=0A= /* If op0 is a compare, extract the comparison arguments from it. */=0A= if (GET_CODE (op0) =3D=3D COMPARE && op1 =3D=3D const0_rtx)=0A= {=0A= diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc=0A= index e03f86d..a73c265 100644=0A= --- a/gcc/config/i386/i386.cc=0A= +++ b/gcc/config/i386/i386.cc=0A= @@ -15995,6 +15995,31 @@ ix86_cc_modes_compatible (machine_mode m1, = machine_mode m2)=0A= }=0A= }=0A= =0A= +/* Simplify the results of comparisons with constant operands.=0A= + MODE is a MODE_CC, CODE is a comparison operator, and VAL is=0A= + a target (and mode) dependent CONST_INT. */=0A= +=0A= +rtx=0A= +ix86_simplify_modecc_const (machine_mode mode, int code, rtx val)=0A= +{=0A= + /* For CCCmode, const0_rtx represents the carry flag is clear,=0A= + otherwise the carry flag is set. */=0A= + switch ((enum rtx_code)code)=0A= + {=0A= + case LTU:=0A= + if (mode =3D=3D E_CCCmode)=0A= + return val =3D=3D const0_rtx ? const0_rtx : const_true_rtx;=0A= + break;=0A= + case GEU:=0A= + if (mode =3D=3D E_CCCmode)=0A= + return val =3D=3D const0_rtx ? const_true_rtx : const0_rtx;=0A= + break;=0A= + default:=0A= + break;=0A= + }=0A= + return NULL_RTX;=0A= +}=0A= +=0A= /* Return strategy to use for floating-point. We assume that fcomi is = always=0A= preferrable where available, since that is also true when looking at = size=0A= (2 bytes, vs. 3 for fnstsw+sahf and at least 5 for fnstsw+test). */=0A= @@ -24978,6 +25003,9 @@ static bool ix86_libc_has_fast_function (int = fcode ATTRIBUTE_UNUSED)=0A= #undef TARGET_GEN_MEMSET_SCRATCH_RTX=0A= #define TARGET_GEN_MEMSET_SCRATCH_RTX ix86_gen_scratch_sse_rtx=0A= =0A= +#undef TARGET_SIMPLIFY_MODECC_CONST=0A= +#define TARGET_SIMPLIFY_MODECC_CONST ix86_simplify_modecc_const=0A= +=0A= #if CHECKING_P=0A= #undef TARGET_RUN_TARGET_SELFTESTS=0A= #define TARGET_RUN_TARGET_SELFTESTS selftest::ix86_run_selftests=0A= ------=_NextPart_000_00FB_01D8A0F1.70C4FC70--