public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Eric Botcazou <ebotcazou@adacore.com>
To: gcc-patches@gcc.gnu.org
Subject: Small tweak to RTL expansion of some array accesses on RISC targets
Date: Thu, 29 Jun 2017 19:23:00 -0000	[thread overview]
Message-ID: <10060997.SGyfb6a1MB@polaris> (raw)

[-- Attachment #1: Type: text/plain, Size: 1909 bytes --]

I noticed that, when a variable-sized object declared on the stack turns out 
to be of fixed size, the optimizer can replace the call to __builtin_alloca by 
the declaration of fixed-size local array.  Now, even if the alignment of the 
object is explicitly preserved, the alignment of its type is not since the 
type of the local array is always unsigned char.

On RISC targets (STRICT_ALIGNMENT / SLOW_UNALIGNED_ACCESS to be precise), this 
causes any read larger than unsigned char to go through the bitfield expansion 
circuitry, because expand_expr_real_1 has:

	    /* If the field isn't aligned enough to fetch as a memref,
 	       fetch it as a bit field.  */
 	    || (mode1 != BLKmode
		&& (((TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
		      || (bitpos % GET_MODE_ALIGNMENT (mode) != 0)
		      || (MEM_P (op0)
			  && (MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
			      || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0))))
		     && modifier != EXPAND_MEMORY
		     && ((modifier == EXPAND_CONST_ADDRESS
			  || modifier == EXPAND_INITIALIZER)
			 ? STRICT_ALIGNMENT
			 : SLOW_UNALIGNED_ACCESS (mode1, MEM_ALIGN (op0))))

In other words, even if the alignment of the MEM is sufficient (and it is), 
the test on TYPE_ALIGN (TREE_TYPE (tem)) is true since TREE_TYPE (tem) is 
unsigned char.

I think that the test on TYPE_ALIGN (TREE_TYPE (tem)) is superfluous when op0 
is a MEM because the second part of the test is more precise and sufficient, 
so the attached patchlet uses a conditional expression to implement that.

Bootstrapped/regtested on SPARC/Solaris, SPARC64/Linux, PowerPC64/Linux and 
Aarch64/Linux, applied on the mainline as obvious.


2017-06-29  Eric Botcazou  <ebotcazou@adacore.com>

	* expr.c (expand_expr) <normal_inner_ref>: When testing for unaligned
	objects, take into account only the alignment of 'op0' and 'mode1' if
	'op0' is a MEM.

-- 
Eric Botcazou

[-- Attachment #2: p.diff --]
[-- Type: text/x-patch, Size: 976 bytes --]

Index: expr.c
===================================================================
--- expr.c	(revision 249619)
+++ expr.c	(working copy)
@@ -10631,11 +10631,11 @@ expand_expr_real_1 (tree exp, rtx target
 	    /* If the field isn't aligned enough to fetch as a memref,
 	       fetch it as a bit field.  */
 	    || (mode1 != BLKmode
-		&& (((TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
-		      || (bitpos % GET_MODE_ALIGNMENT (mode) != 0)
-		      || (MEM_P (op0)
-			  && (MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
-			      || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0))))
+		&& (((MEM_P (op0)
+		      ? MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
+		        || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0)
+		      : TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
+		        || (bitpos % GET_MODE_ALIGNMENT (mode) != 0))
 		     && modifier != EXPAND_MEMORY
 		     && ((modifier == EXPAND_CONST_ADDRESS
 			  || modifier == EXPAND_INITIALIZER)

                 reply	other threads:[~2017-06-29 19:23 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=10060997.SGyfb6a1MB@polaris \
    --to=ebotcazou@adacore.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).