From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) by sourceware.org (Postfix) with ESMTP id C75A5385F01D for ; Thu, 2 Apr 2020 10:29:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org C75A5385F01D Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-373-PE_h_OYlOo-EB3MKOkAzbg-1; Thu, 02 Apr 2020 06:29:47 -0400 X-MC-Unique: PE_h_OYlOo-EB3MKOkAzbg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B964F1005516; Thu, 2 Apr 2020 10:29:45 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-113-52.ams2.redhat.com [10.36.113.52]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D6AC25E039; Thu, 2 Apr 2020 10:29:44 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id 032ATgZw022317; Thu, 2 Apr 2020 12:29:42 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id 032ATddV022316; Thu, 2 Apr 2020 12:29:39 +0200 Date: Thu, 2 Apr 2020 12:29:39 +0200 From: Jakub Jelinek To: Richard Biener , Jeff Law , Alexandre Oliva Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] cselib: Reuse VALUEs on sp adjustments [PR92264] Message-ID: <20200402102939.GQ2212@tucnak> Reply-To: Jakub Jelinek MIME-Version: 1.0 User-Agent: Mutt/1.11.3 (2019-02-01) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Content-Disposition: inline X-Spam-Status: No, score=-20.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Thu, 02 Apr 2020 10:29:54 -0000 Hi! As discussed in the PR, if !ACCUMULATE_OUTGOING_ARGS on large functions we can have hundreds of thousands of stack pointer adjustments and cselib creates a new VALUE after each sp adjustment, which form extremely deep VALUE chains, which is very harmful e.g. for find_base_term. E.g. if we have sp -=3D 4 sp -=3D 4 sp +=3D 4 sp +=3D 4 sp -=3D 4 sp +=3D 4 that means 7 VALUEs, one for the sp at beginning (val1), than val2 =3D val1= - 4, then val3 =3D val2 - 4, then val4 =3D val3 + 4, then val5 =3D val4 + 4, = then val6 =3D val5 - 4, then val7 =3D val6 + 4. This patch tweaks cselib, so that it is smarter about sp adjustments. When cselib_lookup (stack_pointer_rtx, Pmode, 1, VOIDmode) and we know nothing about sp yet (this happens at the start of the function, for non-var-tracking also after cselib_reset_table and for var-tracking after processing fp_setter insn where we forget about former sp values because that is now hfp related while everything after it is sp related), we look it up normally, but in addition to what we have been doing before we mark the VALUE as SP_DERIVED_VALUE_P. Further lookups of sp + offset are then special cased, so that it is canonicalized to that SP_DERIVED_VALUE_P VALUE + CONST_INT (if possible). So, for the above, we get val1 with SP_DERIVED_VALUE_P set, then val2 =3D val1 - 4, val3 =3D v= al1 - 8 (note, no longer val2 - 4!), then we get val2 again, val1 again, val2 again, val1 again. In the find_base_term visited_vals.length () > 100 find_base_term statistics during combined x86_64-linux and i686-linux bootstrap+regtest cycle, without the patch I see: =09=09=09find_base_term > 100 =09=09=09returning NULL=09returning non-NULL 32-bit compilations=094229178=09=09407 64-bit compilations=09217523=09=090 with largest visited_vals.length () when returning non-NULL being 206. With the patch the same numbers are: 32-bit compilations=091249588=09=09135 64-bit compilations=093510=09=090 with largest visited_vals.length () when returning non-NULL being 173. This shows significant reduction of the deep VALUE chains. On powerpc64{,le}-linux, these stats didn't change at all, we have =09=09=091008=09=090 for all of -m32, -m64 and little-endian -m64, just the gcc.dg/pr85180.c and gcc.dg/pr87985.c testcases which are unrelated to sp. My earlier version of the patch, which contained just the rtl.h and cselib.= c changes, regressed some tests: gcc.dg/guality/{pr36728-{1,3},pr68860-{1,2}}.c gcc.target/i386/{pr88416,sse-{13,23,24,25,26}}.c The problem with the former tests was worse debug info, where with -m32 where arg7 was passed in a stack slot we though a push later on might have invalidated it, when it couldn't. This is something I've solved with the var-tracking.c (vt_initialize) changes. In those problematic functions, we create a cfa_base VALUE (argp) and want to record that at the start of the function the argp VALUE is sp + off and also record that current sp VALUE is argp's VALUE - off. The second permanent equivalence didn't make it after the patch though, because cselib_add_permanent_equiv will cselib_lookup the value of the expression it wants to add as the equivalenc= e and if it is the same VALUE as we are calling it on, it doesn't do anything= ; and due to the cselib changes for sp based accesses that is exactly what happened. By reversing the order of the cselib_add_permanent_equiv calls w= e get both equivalences though and thus are able to canonicalize the sp based accesses in var-tracking to the cfa_base value + offset. The i386 FAILs were all ICEs, where we had pushf instruction pushing flags and then pop pseudo reading that value again. With the cselib changes, cselib during RTL DSE is able to see through the sp adjustment and wanted to replace_read what was done pushf, by moving the flags register into a pseudo and replace the memory read in the pop with that pseudo. That is wrong for two reasons: one is that the backend doesn't have an instruction to move the flags hard register into some other register, but replace_read has been validating just the mem -> pseudo replacement and not the insns emitted by copy_to_mode_reg. And the second issue is that it is obviously wrong to replace a stack pop which contains stack post-increment by a copy of pseudo into destination. dse.c has some code to handle RTX_AUTOINC, but only uses it when actually removing stores and only when there is REG_INC note (stack RTX_AUTOINC does not have those), in check_for_inc_dec* where it emits the reg adjustment(s) before the insn that is going to be deleted. replace_read doesn't remove the insn, so if it e.g. contained REG_INC note, it would be kept there and we might have the RTX_AUTOINC not just in *loc, but other spots. So, the dse.c changes try to validate the added insns and punt on all RTX_AUTOINC in *loc. Furthermore, it seems that with the cselib.c changes on the gfortran.dg/pr87360.f90 and gcc.target/i386/pr88416.c testcases check_for_inc_dec{,_1} happily throws stack pointer autoinc on the floor, which is also wrong. While we could perhaps do the for_each_inc_dec call regardless of whether we have REG_INC note or not, we aren't prepared to handle e.g. REG_ARGS_SIZE distribution and thus could end up with wrong unwind info or ICEs during dwarf2cfi.c. So the patch also punts on those, after all, if we'd in theory managed to try to optimize such pushes before, we'd create wrong-code. On x86_64-linux and i686-linux, the patch has some minor debug info coverag= e differences, but it doesn't appear very significant to me. https://github.com/pmachata/dwlocstat tool gives (where before is vanilla trunk + the rtl.h patch but not {cselib,var-tracking,dse}.c --enable-checking=3Dyes,rtl,extra bootstrapped, then {cselib,var-tracking,d= se}.c hunks applied and make cc1plus, while after is trunk with the whole patch applied). 64-bit cc1plus before cov%=09samples=09cumul 0..10=091232756/48%=091232756/48% 11..20=0931089/1%=091263845/49% 21..30=0939172/1%=091303017/51% 31..40=0938853/1%=091341870/52% 41..50=0947473/1%=091389343/54% 51..60=0945171/1%=091434514/56% 61..70=0969393/2%=091503907/59% 71..80=0961988/2%=091565895/61% 81..90=09104528/4%=091670423/65% 91..100=09875402/34%=092545825/100% after cov%=09samples=09cumul 0..10=091233238/48%=091233238/48% 11..20=0931086/1%=091264324/49% 21..30=0939157/1%=091303481/51% 31..40=0938819/1%=091342300/52% 41..50=0947447/1%=091389747/54% 51..60=0945151/1%=091434898/56% 61..70=0969379/2%=091504277/59% 71..80=0961946/2%=091566223/61% 81..90=09104508/4%=091670731/65% 91..100=09875094/34%=092545825/100% 32-bit cc1plus before cov%=09samples=09cumul 0..10=091231221/48%=091231221/48% 11..20=0930992/1%=091262213/49% 21..30=0936422/1%=091298635/51% 31..40=0935793/1%=091334428/52% 41..50=0947102/1%=091381530/54% 51..60=0941201/1%=091422731/56% 61..70=0965467/2%=091488198/58% 71..80=0959560/2%=091547758/61% 81..90=09104076/4%=091651834/65% 91..100=09881879/34%=092533713/100% after cov%=09samples=09cumul 0..10=091230469/48%=091230469/48% 11..20=0930390/1%=091260859/49% 21..30=0936362/1%=091297221/51% 31..40=0936042/1%=091333263/52% 41..50=0947619/1%=091380882/54% 51..60=0941674/1%=091422556/56% 61..70=0965849/2%=091488405/58% 71..80=0959857/2%=091548262/61% 81..90=09104178/4%=091652440/65% 91..100=09881273/34%=092533713/100% Bootstrapped/regtested on {x86_64,i686,powerpc64{,-le}}-linux, ok for trunk= ? For the PR in question, my proposal would be to also lower -param=3Dmax-find-base-term-values=3D default from 2000 to 200 after this, at least in the above 4 bootstraps/regtests there is nothing that would ever result in find_base_term returning non-NULL with more than 200 VALUEs being processed= . 2020-04-02 Jakub Jelinek =09PR rtl-optimization/92264 =09* rtl.h (struct rtx_def): Mention that call bit is used as =09SP_DERIVED_VALUE_P in cselib.c. =09* cselib.c (SP_DERIVED_VALUE_P): Define. =09(PRESERVED_VALUE_P, SP_BASED_VALUE_P): Move definitions earlier. =09(cselib_hasher::equal): Handle equality between SP_DERIVED_VALUE_P =09val_rtx and sp based expression where offsets cancel each other. =09(preserve_constants_and_equivs): Formatting fix. =09(cselib_reset_table): Add reverse op loc to SP_DERIVED_VALUE_P =09locs list for cfa_base_preserved_val if needed. Formatting fix. =09(autoinc_split): If the to be returned value is a REG, MEM or =09VALUE which has SP_DERIVED_VALUE_P + CONST_INT as one of its =09locs, return the SP_DERIVED_VALUE_P VALUE and adjust *off. =09(rtx_equal_for_cselib_1): Call autoinc_split even if both =09expressions are PLUS in Pmode with CONST_INT second operands. =09Handle SP_DERIVED_VALUE_P cases. =09(cselib_hash_plus_const_int): New function. =09(cselib_hash_rtx): Use it for PLUS in Pmode with CONST_INT =09second operand, as well as for PRE_DEC etc. that ought to be =09hashed the same way. =09(cselib_subst_to_values): Substitute PLUS with Pmode and =09CONST_INT operand if the first operand is a VALUE which has =09SP_DERIVED_VALUE_P + CONST_INT as one of its locs for the =09SP_DERIVED_VALUE_P + adjusted offset. =09(cselib_lookup_1): When creating a new VALUE for stack_pointer_rtx, =09set SP_DERIVED_VALUE_P on it. Set PRESERVED_VALUE_P when adding =09SP_DERIVED_VALUE_P PRESERVED_VALUE_P subseted VALUE location. =09* var-tracking.c (vt_initialize): Call cselib_add_permanent_equiv =09on the sp value before calling cselib_add_permanent_equiv on the =09cfa_base value. =09* dse.c (check_for_inc_dec_1, check_for_inc_dec): Punt on RTX_AUTOINC =09in the insn without REG_INC note. =09(replace_read): Punt on RTX_AUTOINC in the *loc being replaced. =09Punt on invalid insns added by copy_to_mode_reg. Formatting fixes. --- gcc/rtl.h.jj=092020-04-01 09:43:45.346628694 +0200 +++ gcc/rtl.h=092020-04-01 17:46:15.160396309 +0200 @@ -331,6 +331,7 @@ struct GTY((desc("0"), tag("0"), 1 in a MEM if it cannot trap. 1 in a CALL_INSN logically equivalent to ECF_LOOPING_CONST_OR_PURE and DECL_LOOPING_CONST_OR_PURE_P. + 1 in a VALUE is SP_DERIVED_VALUE_P in cselib.c. Dumped as "/c" in RTL dumps. */ unsigned int call : 1; /* 1 in a REG, MEM, or CONCAT if the value is set at most once, anywhere= . --- gcc/cselib.c.jj=092020-04-01 09:43:45.305629293 +0200 +++ gcc/cselib.c=092020-04-01 17:46:15.161396295 +0200 @@ -58,6 +58,16 @@ static void cselib_invalidate_regno (uns static void cselib_invalidate_mem (rtx); static void cselib_record_set (rtx, cselib_val *, cselib_val *); static void cselib_record_sets (rtx_insn *); +static rtx autoinc_split (rtx, rtx *, machine_mode); + +#define PRESERVED_VALUE_P(RTX) \ + (RTL_FLAG_CHECK1 ("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging) + +#define SP_BASED_VALUE_P(RTX) \ + (RTL_FLAG_CHECK1 ("SP_BASED_VALUE_P", (RTX), VALUE)->jump) + +#define SP_DERIVED_VALUE_P(RTX) \ + (RTL_FLAG_CHECK1 ("SP_DERIVED_VALUE_P", (RTX), VALUE)->call) =20 struct expand_value_data { @@ -122,6 +132,13 @@ cselib_hasher::equal (const cselib_val * if (GET_CODE (x) =3D=3D VALUE) return x =3D=3D v->val_rtx; =20 + if (SP_DERIVED_VALUE_P (v->val_rtx) && GET_MODE (x) =3D=3D Pmode) + { + rtx xoff =3D NULL; + if (autoinc_split (x, &xoff, memmode) =3D=3D v->val_rtx && xoff =3D= =3D NULL_RTX) +=09return true; + } + /* We don't guarantee that distinct rtx's have different hash values, so we need to do a comparison. */ for (l =3D v->locs; l; l =3D l->next) @@ -256,12 +273,6 @@ void (*cselib_discard_hook) (cselib_val void (*cselib_record_sets_hook) (rtx_insn *insn, struct cselib_set *sets, =09=09=09=09 int n_sets); =20 -#define PRESERVED_VALUE_P(RTX) \ - (RTL_FLAG_CHECK1 ("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging) - -#define SP_BASED_VALUE_P(RTX) \ - (RTL_FLAG_CHECK1 ("SP_BASED_VALUE_P", (RTX), VALUE)->jump) - =0C =20 /* Allocate a struct elt_list and fill in its two elements with the @@ -494,7 +505,7 @@ preserve_constants_and_equivs (cselib_va }; cselib_val **slot =09=3D cselib_preserved_hash_table->find_slot_with_hash (&lookup, -=09=09=09=09=09=09=09 v->hash, INSERT); +=09=09=09=09=09=09=09 v->hash, INSERT); gcc_assert (!*slot); *slot =3D v; } @@ -532,6 +543,28 @@ cselib_reset_table (unsigned int num) max_value_regs =09=3D hard_regno_nregs (regno, =09=09=09 GET_MODE (cfa_base_preserved_val->locs->loc)); + + /* If cfa_base is sp + const_int, need to preserve also the +=09 SP_DERIVED_VALUE_P value. */ + for (struct elt_loc_list *l =3D cfa_base_preserved_val->locs; +=09 l; l =3D l->next) +=09if (GET_CODE (l->loc) =3D=3D PLUS +=09 && GET_CODE (XEXP (l->loc, 0)) =3D=3D VALUE +=09 && SP_DERIVED_VALUE_P (XEXP (l->loc, 0)) +=09 && CONST_INT_P (XEXP (l->loc, 1))) +=09 { +=09 if (! invariant_or_equiv_p (CSELIB_VAL_PTR (XEXP (l->loc, 0)))) +=09 { +=09=09rtx val =3D cfa_base_preserved_val->val_rtx; +=09=09rtx_insn *save_cselib_current_insn =3D cselib_current_insn; +=09=09cselib_current_insn =3D l->setting_insn; +=09=09new_elt_loc_list (CSELIB_VAL_PTR (XEXP (l->loc, 0)), +=09=09=09=09 plus_constant (Pmode, val, +=09=09=09=09=09=09 -UINTVAL (XEXP (l->loc, 1)))); +=09=09cselib_current_insn =3D save_cselib_current_insn; +=09 } +=09 break; +=09 } } else { @@ -541,8 +574,7 @@ cselib_reset_table (unsigned int num) } =20 if (cselib_preserve_constants) - cselib_hash_table->traverse - (NULL); + cselib_hash_table->traverse (N= ULL); else { cselib_hash_table->empty (); @@ -799,33 +831,66 @@ autoinc_split (rtx x, rtx *off, machine_ { case PLUS: *off =3D XEXP (x, 1); - return XEXP (x, 0); + x =3D XEXP (x, 0); + break; =20 case PRE_DEC: if (memmode =3D=3D VOIDmode) =09return x; =20 *off =3D gen_int_mode (-GET_MODE_SIZE (memmode), GET_MODE (x)); - return XEXP (x, 0); + x =3D XEXP (x, 0); + break; =20 case PRE_INC: if (memmode =3D=3D VOIDmode) =09return x; =20 *off =3D gen_int_mode (GET_MODE_SIZE (memmode), GET_MODE (x)); - return XEXP (x, 0); + x =3D XEXP (x, 0); + break; =20 case PRE_MODIFY: - return XEXP (x, 1); + x =3D XEXP (x, 1); + break; =20 case POST_DEC: case POST_INC: case POST_MODIFY: - return XEXP (x, 0); + x =3D XEXP (x, 0); + break; =20 default: - return x; + break; } + + if (GET_MODE (x) =3D=3D Pmode + && (REG_P (x) || MEM_P (x) || GET_CODE (x) =3D=3D VALUE) + && (*off =3D=3D NULL_RTX || CONST_INT_P (*off))) + { + cselib_val *e; + if (GET_CODE (x) =3D=3D VALUE) +=09e =3D CSELIB_VAL_PTR (x); + else +=09e =3D cselib_lookup (x, GET_MODE (x), 0, memmode); + if (e) +=09for (struct elt_loc_list *l =3D e->locs; l; l =3D l->next) +=09 if (GET_CODE (l->loc) =3D=3D PLUS +=09 && GET_CODE (XEXP (l->loc, 0)) =3D=3D VALUE +=09 && SP_DERIVED_VALUE_P (XEXP (l->loc, 0)) +=09 && CONST_INT_P (XEXP (l->loc, 1))) +=09 { +=09 if (*off =3D=3D NULL_RTX) +=09=09*off =3D XEXP (l->loc, 1); +=09 else +=09=09*off =3D plus_constant (Pmode, *off, +=09=09=09=09 INTVAL (XEXP (l->loc, 1))); +=09 if (*off =3D=3D const0_rtx) +=09=09*off =3D NULL_RTX; +=09 return XEXP (l->loc, 0); +=09 } + } + return x; } =20 /* Return nonzero if we can prove that X and Y contain the same value, @@ -868,6 +933,16 @@ rtx_equal_for_cselib_1 (rtx x, rtx y, ma if (GET_CODE (y) =3D=3D VALUE) =09return e =3D=3D canonical_cselib_val (CSELIB_VAL_PTR (y)); =20 + if ((SP_DERIVED_VALUE_P (x) +=09 || SP_DERIVED_VALUE_P (e->val_rtx)) +=09 && GET_MODE (y) =3D=3D Pmode) +=09{ +=09 rtx yoff =3D NULL; +=09 rtx yr =3D autoinc_split (y, &yoff, memmode); +=09 if ((yr =3D=3D x || yr =3D=3D e->val_rtx) && yoff =3D=3D NULL_RTX) +=09 return 1; +=09} + if (depth =3D=3D 128) =09return 0; =20 @@ -891,6 +966,16 @@ rtx_equal_for_cselib_1 (rtx x, rtx y, ma cselib_val *e =3D canonical_cselib_val (CSELIB_VAL_PTR (y)); struct elt_loc_list *l; =20 + if ((SP_DERIVED_VALUE_P (y) +=09 || SP_DERIVED_VALUE_P (e->val_rtx)) +=09 && GET_MODE (x) =3D=3D Pmode) +=09{ +=09 rtx xoff =3D NULL; +=09 rtx xr =3D autoinc_split (x, &xoff, memmode); +=09 if ((xr =3D=3D y || xr =3D=3D e->val_rtx) && xoff =3D=3D NULL_RTX) +=09 return 1; +=09} + if (depth =3D=3D 128) =09return 0; =20 @@ -910,7 +995,11 @@ rtx_equal_for_cselib_1 (rtx x, rtx y, ma if (GET_MODE (x) !=3D GET_MODE (y)) return 0; =20 - if (GET_CODE (x) !=3D GET_CODE (y)) + if (GET_CODE (x) !=3D GET_CODE (y) + || (GET_CODE (x) =3D=3D PLUS +=09 && GET_MODE (x) =3D=3D Pmode +=09 && CONST_INT_P (XEXP (x, 1)) +=09 && CONST_INT_P (XEXP (y, 1)))) { rtx xorig =3D x, yorig =3D y; rtx xoff =3D NULL, yoff =3D NULL; @@ -918,17 +1007,20 @@ rtx_equal_for_cselib_1 (rtx x, rtx y, ma x =3D autoinc_split (x, &xoff, memmode); y =3D autoinc_split (y, &yoff, memmode); =20 - if (!xoff !=3D !yoff) -=09return 0; - - if (xoff && !rtx_equal_for_cselib_1 (xoff, yoff, memmode, depth)) -=09return 0; - /* Don't recurse if nothing changed. */ if (x !=3D xorig || y !=3D yorig) -=09return rtx_equal_for_cselib_1 (x, y, memmode, depth); +=09{ +=09 if (!xoff !=3D !yoff) +=09 return 0; =20 - return 0; +=09 if (xoff && !rtx_equal_for_cselib_1 (xoff, yoff, memmode, depth)) +=09 return 0; + +=09 return rtx_equal_for_cselib_1 (x, y, memmode, depth); +=09} + + if (GET_CODE (xorig) !=3D GET_CODE (yorig)) +=09return 0; } =20 /* These won't be handled correctly by the code below. */ @@ -1042,6 +1134,41 @@ rtx_equal_for_cselib_1 (rtx x, rtx y, ma return 1; } =20 +/* Helper function for cselib_hash_rtx. Arguments like for cselib_hash_rt= x, + except that it hashes (plus:P x c). */ + +static unsigned int +cselib_hash_plus_const_int (rtx x, HOST_WIDE_INT c, int create, +=09=09=09 machine_mode memmode) +{ + cselib_val *e =3D cselib_lookup (x, GET_MODE (x), create, memmode); + if (! e) + return 0; + + if (! SP_DERIVED_VALUE_P (e->val_rtx)) + for (struct elt_loc_list *l =3D e->locs; l; l =3D l->next) + if (GET_CODE (l->loc) =3D=3D PLUS +=09 && GET_CODE (XEXP (l->loc, 0)) =3D=3D VALUE +=09 && SP_DERIVED_VALUE_P (XEXP (l->loc, 0)) +=09 && CONST_INT_P (XEXP (l->loc, 1))) +=09{ +=09 e =3D CSELIB_VAL_PTR (XEXP (l->loc, 0)); +=09 c =3D trunc_int_for_mode (c + UINTVAL (XEXP (l->loc, 1)), Pmode); +=09 break; +=09} + if (c =3D=3D 0) + return e->hash; + + unsigned hash =3D (unsigned) PLUS + (unsigned) GET_MODE (x); + hash +=3D e->hash; + unsigned int tem_hash =3D (unsigned) CONST_INT + (unsigned) VOIDmode; + tem_hash +=3D ((unsigned) CONST_INT << 7) + (unsigned HOST_WIDE_INT) c; + if (tem_hash =3D=3D 0) + tem_hash =3D (unsigned int) CONST_INT; + hash +=3D tem_hash; + return hash ? hash : 1 + (unsigned int) PLUS; +} + /* Hash an rtx. Return 0 if we couldn't hash the rtx. For registers and memory locations, we look up their cselib_val structu= re and return its VALUE element. @@ -1209,10 +1336,21 @@ cselib_hash_rtx (rtx x, int create, mach =09offset =3D -offset; /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes =09 like (mem:MEMMODE (plus (reg) (const_int I))). */ - hash +=3D (unsigned) PLUS - (unsigned)code -=09+ cselib_hash_rtx (XEXP (x, 0), create, memmode) -=09+ cselib_hash_rtx (gen_int_mode (offset, GET_MODE (x)), -=09=09=09 create, memmode); + if (GET_MODE (x) =3D=3D Pmode +=09 && (REG_P (XEXP (x, 0)) +=09 || MEM_P (XEXP (x, 0)) +=09 || GET_CODE (XEXP (x, 0)) =3D=3D VALUE)) +=09{ +=09 HOST_WIDE_INT c; +=09 if (offset.is_constant (&c)) +=09 return cselib_hash_plus_const_int (XEXP (x, 0), +=09=09=09=09=09 trunc_int_for_mode (c, Pmode), +=09=09=09=09=09 create, memmode); +=09} + hash =3D ((unsigned) PLUS + (unsigned) GET_MODE (x) +=09 + cselib_hash_rtx (XEXP (x, 0), create, memmode) +=09 + cselib_hash_rtx (gen_int_mode (offset, GET_MODE (x)), +=09=09=09=09 create, memmode)); return hash ? hash : 1 + (unsigned) PLUS; =20 case PRE_MODIFY: @@ -1237,6 +1375,16 @@ cselib_hash_rtx (rtx x, int create, mach =20 break; =20 + case PLUS: + if (GET_MODE (x) =3D=3D Pmode +=09 && (REG_P (XEXP (x, 0)) +=09 || MEM_P (XEXP (x, 0)) +=09 || GET_CODE (XEXP (x, 0)) =3D=3D VALUE) +=09 && CONST_INT_P (XEXP (x, 1))) +=09return cselib_hash_plus_const_int (XEXP (x, 0), INTVAL (XEXP (x, 1)), +=09=09=09=09=09 create, memmode); + break; + default: break; } @@ -1927,6 +2075,26 @@ cselib_subst_to_values (rtx x, machine_m gcc_assert (memmode !=3D VOIDmode); return cselib_subst_to_values (XEXP (x, 0), memmode); =20 + case PLUS: + if (GET_MODE (x) =3D=3D Pmode && CONST_INT_P (XEXP (x, 1))) +=09{ +=09 rtx t =3D cselib_subst_to_values (XEXP (x, 0), memmode); +=09 if (GET_CODE (t) =3D=3D VALUE) +=09 for (struct elt_loc_list *l =3D CSELIB_VAL_PTR (t)->locs; +=09=09 l; l =3D l->next) +=09 if (GET_CODE (l->loc) =3D=3D PLUS +=09=09 && GET_CODE (XEXP (l->loc, 0)) =3D=3D VALUE +=09=09 && SP_DERIVED_VALUE_P (XEXP (l->loc, 0)) +=09=09 && CONST_INT_P (XEXP (l->loc, 1))) +=09=09return plus_constant (Pmode, l->loc, INTVAL (XEXP (x, 1))); +=09 if (t !=3D XEXP (x, 0)) +=09 { +=09 copy =3D shallow_copy_rtx (x); +=09 XEXP (copy, 0) =3D t; +=09 } +=09 return copy; +=09} + default: break; } @@ -2030,6 +2198,8 @@ cselib_lookup_1 (rtx x, machine_mode mod =09} =20 e =3D new_cselib_val (next_uid, GET_MODE (x), x); + if (GET_MODE (x) =3D=3D Pmode && x =3D=3D stack_pointer_rtx) +=09SP_DERIVED_VALUE_P (e->val_rtx) =3D 1; new_elt_loc_list (e, x); =20 scalar_int_mode int_mode; @@ -2107,7 +2277,14 @@ cselib_lookup_1 (rtx x, machine_mode mod the hash table is inconsistent until we do so, and cselib_subst_to_values will need to do lookups. */ *slot =3D e; - new_elt_loc_list (e, cselib_subst_to_values (x, memmode)); + rtx v =3D cselib_subst_to_values (x, memmode); + + /* If cselib_preserve_constants, we might get a SP_DERIVED_VALUE_P + VALUE that isn't in the hash tables anymore. */ + if (GET_CODE (v) =3D=3D VALUE && SP_DERIVED_VALUE_P (v) && PRESERVED_VAL= UE_P (v)) + PRESERVED_VALUE_P (e->val_rtx) =3D 1; + + new_elt_loc_list (e, v); return e; } =20 --- gcc/var-tracking.c.jj=092020-03-26 09:15:29.825500110 +0100 +++ gcc/var-tracking.c=092020-04-01 19:09:30.145421270 +0200 @@ -10045,19 +10045,20 @@ vt_initialize (void) preserve_value (val); if (reg !=3D hard_frame_pointer_rtx && fixed_regs[REGNO (reg)]) =09cselib_preserve_cfa_base_value (val, REGNO (reg)); - expr =3D plus_constant (GET_MODE (stack_pointer_rtx), -=09=09=09 stack_pointer_rtx, -ofst); - cselib_add_permanent_equiv (val, expr, get_insns ()); - if (ofst) =09{ -=09 val =3D cselib_lookup_from_insn (stack_pointer_rtx, -=09=09=09=09=09 GET_MODE (stack_pointer_rtx), 1, -=09=09=09=09=09 VOIDmode, get_insns ()); -=09 preserve_value (val); +=09 cselib_val *valsp +=09 =3D cselib_lookup_from_insn (stack_pointer_rtx, +=09=09=09=09 GET_MODE (stack_pointer_rtx), 1, +=09=09=09=09 VOIDmode, get_insns ()); +=09 preserve_value (valsp); =09 expr =3D plus_constant (GET_MODE (reg), reg, ofst); -=09 cselib_add_permanent_equiv (val, expr, get_insns ()); +=09 cselib_add_permanent_equiv (valsp, expr, get_insns ()); =09} + + expr =3D plus_constant (GET_MODE (stack_pointer_rtx), +=09=09=09 stack_pointer_rtx, -ofst); + cselib_add_permanent_equiv (val, expr, get_insns ()); } =20 /* In order to factor out the adjustments made to the stack pointer or t= o --- gcc/dse.c.jj=092020-03-03 11:04:46.365821937 +0100 +++ gcc/dse.c=092020-04-02 10:48:08.042260842 +0200 @@ -846,6 +846,17 @@ check_for_inc_dec_1 (insn_info_t insn_in if (note) return for_each_inc_dec (PATTERN (insn), emit_inc_dec_insn_before, =09=09=09 insn_info) =3D=3D 0; + + /* Punt on stack pushes, those don't have REG_INC notes and we are + unprepared to deal with distribution of REG_ARGS_SIZE notes etc. */ + subrtx_iterator::array_type array; + FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST) + { + const_rtx x =3D *iter; + if (GET_RTX_CLASS (GET_CODE (x)) =3D=3D RTX_AUTOINC) +=09return false; + } + return true; } =20 @@ -866,6 +877,17 @@ check_for_inc_dec (rtx_insn *insn) if (note) return for_each_inc_dec (PATTERN (insn), emit_inc_dec_insn_before, =09=09=09 &insn_info) =3D=3D 0; + + /* Punt on stack pushes, those don't have REG_INC notes and we are + unprepared to deal with distribution of REG_ARGS_SIZE notes etc. */ + subrtx_iterator::array_type array; + FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST) + { + const_rtx x =3D *iter; + if (GET_RTX_CLASS (GET_CODE (x)) =3D=3D RTX_AUTOINC) +=09return false; + } + return true; } =20 @@ -1980,16 +2002,30 @@ replace_read (store_info *store_info, in =09 point. This does occasionally happen, see PR 37922. */ bitmap regs_set =3D BITMAP_ALLOC (®_obstack); =20 - for (this_insn =3D insns; this_insn !=3D NULL_RTX; this_insn =3D NEX= T_INSN (this_insn)) -=09note_stores (this_insn, look_for_hardregs, regs_set); + for (this_insn =3D insns; +=09 this_insn !=3D NULL_RTX; this_insn =3D NEXT_INSN (this_insn)) +=09{ +=09 if (insn_invalid_p (this_insn, false)) +=09 { +=09 if (dump_file && (dump_flags & TDF_DETAILS)) +=09=09{ +=09=09 fprintf (dump_file, " -- replacing the loaded MEM with "); +=09=09 print_simple_rtl (dump_file, read_reg); +=09=09 fprintf (dump_file, " led to an invalid instruction\n"); +=09=09} +=09 BITMAP_FREE (regs_set); +=09 return false; +=09 } +=09 note_stores (this_insn, look_for_hardregs, regs_set); +=09} =20 bitmap_and_into (regs_set, regs_live); if (!bitmap_empty_p (regs_set)) =09{ =09 if (dump_file && (dump_flags & TDF_DETAILS)) =09 { -=09 fprintf (dump_file, -=09=09 "abandoning replacement because sequence clobbers live hardre= gs:"); +=09 fprintf (dump_file, "abandoning replacement because sequence " +=09=09=09=09 "clobbers live hardregs:"); =09 df_print_regset (dump_file, regs_set); =09 } =20 @@ -1999,6 +2035,19 @@ replace_read (store_info *store_info, in BITMAP_FREE (regs_set); } =20 + subrtx_iterator::array_type array; + FOR_EACH_SUBRTX (iter, array, *loc, NONCONST) + { + const_rtx x =3D *iter; + if (GET_RTX_CLASS (GET_CODE (x)) =3D=3D RTX_AUTOINC) +=09{ +=09 if (dump_file && (dump_flags & TDF_DETAILS)) +=09 fprintf (dump_file, " -- replacing the MEM failed due to address " +=09=09=09=09"side-effects\n"); +=09 return false; +=09} + } + if (validate_change (read_insn->insn, loc, read_reg, 0)) { deferred_change *change =3D deferred_change_pool.allocate (); =09Jakub