From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by sourceware.org (Postfix) with ESMTP id 59174385801A for ; Wed, 10 Jan 2024 17:57:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 59174385801A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=arm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 59174385801A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704909433; cv=none; b=R18X0YWDmUJ6rRyEp2oVsIJI3q+2DKvcEn3lxa8n5xYrNVlVERlLlZXePm46XrKmbE6tiA9kZ/8xb5cpVB7Z4PKuxYt/W+o3lbfB4Lm3wRoNWwn5RnmgbNI/gS8Nh/qcKbWMrauo8a2m36D1S8Y8i0CH1/WR2HOrh/2QByFKxq4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704909433; c=relaxed/simple; bh=/RIXq0WdvRbuxjFZHRps02mBljsLlo4527hRDD9MebM=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=n6PGVhyaz9XQZrUHbUxThn5Op1MUO36wzxDmmba4bqjgWehf+IehStYnGcCXaK9nOdIfPwVM8OTDBPEonTgsjo8kXp6kzv0lLZMtiUEBVesT69FO3Pz+IPTWZcLjeAyN9wkRaboklIlJwRCQt7RGx5KFMcz3lfh0GzDjkM4eKBs= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id BCBC8DA7; Wed, 10 Jan 2024 09:57:56 -0800 (PST) Received: from localhost (e121540-lin.manchester.arm.com [10.32.110.72]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 492893F7B4; Wed, 10 Jan 2024 09:57:10 -0800 (PST) From: Richard Sandiford To: Wilco Dijkstra Mail-Followup-To: Wilco Dijkstra ,GCC Patches , Richard Earnshaw , Kyrylo Tkachov , richard.sandiford@arm.com Cc: GCC Patches , Richard Earnshaw , Kyrylo Tkachov Subject: Re: [PATCH] AArch64: Reassociate CONST in address expressions [PR112573] References: Date: Wed, 10 Jan 2024 17:57:09 +0000 In-Reply-To: (Wilco Dijkstra's message of "Wed, 10 Jan 2024 15:54:42 +0000") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-21.4 required=5.0 tests=BAYES_00,GIT_PATCH_0,KAM_DMARC_NONE,KAM_DMARC_STATUS,KAM_LAZY_DOMAIN_SECURITY,KAM_SHORT,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 List-Id: Wilco Dijkstra writes: > GCC tends to optimistically create CONST of globals with an immediate offset. > However it is almost always better to CSE addresses of globals and add immediate > offsets separately (the offset could be merged later in single-use cases). > Splitting CONST expressions with an index in aarch64_legitimize_address fixes part > of PR112573. > > Passes regress & bootstrap, OK for commit? > > gcc/ChangeLog: > PR target/112573 > * config/aarch64/aarch64.cc (aarch64_legitimize_address): Reassociate badly > formed CONST expressions. > > gcc/testsuite/ChangeLog: > PR target/112573 > * gcc.target/aarch64/pr112573.c: Add new test. > > --- > > diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc > index 0909b319d16b9a1587314bcfda0a8112b42a663f..9fbc8b62455f48baec533d3dd5e2d9ea995d5a8f 100644 > --- a/gcc/config/aarch64/aarch64.cc > +++ b/gcc/config/aarch64/aarch64.cc > @@ -12608,6 +12608,20 @@ aarch64_legitimize_address (rtx x, rtx /* orig_x */, machine_mode mode) > not to split a CONST for some forms of address expression, otherwise > it will generate sub-optimal code. */ > > + /* First split X + CONST (base, offset) into (base + X) + offset. */ > + if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST) > + { > + poly_int64 offset; > + rtx base = strip_offset_and_salt (XEXP (x, 1), &offset); This should be just strip_offset, so that we don't lose the salt during optimisation. > + > + if (offset.is_constant ()) I'm not sure this is really required. Logically the same thing would apply to SVE, although admittedly: /* { dg-do compile } */ /* { dg-options "-O2 -fno-section-anchors" } */ #include char a[2048]; void f1 (svint8_t x, int y) { *(svint8_t *)((a + y) + svcntb() * 3) = x; *(svint8_t *)((a + y) + svcntb() * 2) = x; *(svint8_t *)((a + y) + svcntb() * 1) = x; *(svint8_t *)((a + y) + 0) = x; } /* { dg-final { scan-assembler-times "strb" 4 } } */ /* { dg-final { scan-assembler-times "adrp" 1 } } */ doesn't get arranged into the same form for other reasons (and already produces somewhat decent code). The patch is OK from my POV without the offset.is_constant check and with s/strip_offset_and_salt/strip_offset/. Please say if there's a reason to keep the offset check though. Thanks, Richard > + { > + base = expand_binop (Pmode, add_optab, base, XEXP (x, 0), > + NULL_RTX, true, OPTAB_DIRECT); > + x = plus_constant (Pmode, base, offset); > + } > + } > + > if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1))) > { > rtx base = XEXP (x, 0); > diff --git a/gcc/testsuite/gcc.target/aarch64/pr112573.c b/gcc/testsuite/gcc.target/aarch64/pr112573.c > new file mode 100644 > index 0000000000000000000000000000000000000000..be04c0ca86ad9f33975a85f497549955d6d1236d > --- /dev/null > +++ b/gcc/testsuite/gcc.target/aarch64/pr112573.c > @@ -0,0 +1,15 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fno-section-anchors" } */ > + > +char a[100]; > + > +void f1 (int x, int y) > +{ > + *((a + y) + 3) = x; > + *((a + y) + 2) = x; > + *((a + y) + 1) = x; > + *((a + y) + 0) = x; > +} > + > +/* { dg-final { scan-assembler-times "strb" 4 } } */ > +/* { dg-final { scan-assembler-times "adrp" 1 } } */