From 79498ad8ba53725b8429deeb9eb81c66bc0c496d Mon Sep 17 00:00:00 2001
From: Chung-Ju Wu <jasonwucj@gmail.com>
Date: Sun, 22 Apr 2018 08:19:38 +0000
Subject: [PATCH] [NDS32] Implement DATA_ALIGNMENT, LOCAL_ALIGNMENT and
 TARGET_CONSTANT_ALIGNMENT.

gcc/
	* config/nds32/nds32-protos.h (nds32_data_alignment,
	nds32_local_alignment): Declare.
	* config/nds32/nds32.c (nds32_data_alignment, nds32_constant_alignment,
	nds32_local_alignment): New functions.
	(TARGET_CONSTANT_ALIGNMENT): Define.
	* config/nds32/nds32.h (DATA_ALIGNMENT, LOCAL_ALIGNMENT): Define.

From-SVN: r259548
---
 gcc/ChangeLog                   |  9 +++++
 gcc/config/nds32/nds32-protos.h |  2 ++
 gcc/config/nds32/nds32.c        | 60 +++++++++++++++++++++++++++++++++
 gcc/config/nds32/nds32.h        |  6 ++++
 4 files changed, 77 insertions(+)

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index f71edd546160..7c6fb7631c05 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,12 @@
+2018-04-22  Chung-Ju Wu  <jasonwucj@gmail.com>
+
+	* config/nds32/nds32-protos.h (nds32_data_alignment,
+	nds32_local_alignment): Declare.
+	* config/nds32/nds32.c (nds32_data_alignment, nds32_constant_alignment,
+	nds32_local_alignment): New functions.
+	(TARGET_CONSTANT_ALIGNMENT): Define.
+	* config/nds32/nds32.h (DATA_ALIGNMENT, LOCAL_ALIGNMENT): Define.
+
 2018-04-22  Chung-Ju Wu  <jasonwucj@gmail.com>
 
 	* config/nds32/nds32.c
diff --git a/gcc/config/nds32/nds32-protos.h b/gcc/config/nds32/nds32-protos.h
index b7522f1ed7da..2dce97ef91b8 100644
--- a/gcc/config/nds32/nds32-protos.h
+++ b/gcc/config/nds32/nds32-protos.h
@@ -223,6 +223,8 @@ extern int nds32_can_use_return_insn (void);
 /* Auxiliary functions to decide output alignment or not.  */
 
 extern int nds32_target_alignment (rtx_insn *);
+extern unsigned int nds32_data_alignment (tree, unsigned int);
+extern unsigned int nds32_local_alignment (tree, unsigned int);
 
 /* Auxiliary functions to expand builtin functions.  */
 
diff --git a/gcc/config/nds32/nds32.c b/gcc/config/nds32/nds32.c
index 76a72a8b12eb..a0012c0ccf3e 100644
--- a/gcc/config/nds32/nds32.c
+++ b/gcc/config/nds32/nds32.c
@@ -4812,6 +4812,63 @@ nds32_target_alignment (rtx_insn *label)
     return 2;
 }
 
+/* Return alignment for data.  */
+unsigned int
+nds32_data_alignment (tree data,
+		      unsigned int basic_align)
+{
+  if ((basic_align < BITS_PER_WORD)
+      && (TREE_CODE (data) == ARRAY_TYPE
+	 || TREE_CODE (data) == UNION_TYPE
+	 || TREE_CODE (data) == RECORD_TYPE))
+    return BITS_PER_WORD;
+  else
+    return basic_align;
+}
+
+/* Return alignment for constant value.  */
+static HOST_WIDE_INT
+nds32_constant_alignment (const_tree constant,
+			  HOST_WIDE_INT basic_align)
+{
+  /* Make string literal and constant for constructor to word align.  */
+  if (((TREE_CODE (constant) == STRING_CST
+	|| TREE_CODE (constant) == CONSTRUCTOR
+	|| TREE_CODE (constant) == UNION_TYPE
+	|| TREE_CODE (constant) == RECORD_TYPE
+	|| TREE_CODE (constant) == ARRAY_TYPE)
+       && basic_align < BITS_PER_WORD))
+    return BITS_PER_WORD;
+  else
+    return basic_align;
+}
+
+/* Return alignment for local variable.  */
+unsigned int
+nds32_local_alignment (tree local ATTRIBUTE_UNUSED,
+		       unsigned int basic_align)
+{
+  bool at_least_align_to_word = false;
+  /* Make local array, struct and union at least align to word for make
+     sure it can unroll memcpy when initialize by constant.  */
+  switch (TREE_CODE (local))
+    {
+    case ARRAY_TYPE:
+    case RECORD_TYPE:
+    case UNION_TYPE:
+      at_least_align_to_word = true;
+      break;
+    default:
+      at_least_align_to_word = false;
+      break;
+    }
+  if (at_least_align_to_word
+      && (basic_align < BITS_PER_WORD))
+    return BITS_PER_WORD;
+  else
+    return basic_align;
+}
+
 bool
 nds32_split_double_word_load_store_p(rtx *operands, bool load_p)
 {
@@ -4865,6 +4922,9 @@ nds32_use_blocks_for_constant_p (machine_mode mode,
 #undef TARGET_EXPAND_TO_RTL_HOOK
 #define TARGET_EXPAND_TO_RTL_HOOK nds32_expand_to_rtl_hook
 
+#undef TARGET_CONSTANT_ALIGNMENT
+#define TARGET_CONSTANT_ALIGNMENT nds32_constant_alignment
+
 
 /* Layout of Source Language Data Types.  */
 
diff --git a/gcc/config/nds32/nds32.h b/gcc/config/nds32/nds32.h
index 9d673d54e804..6c2f1f8f13ed 100644
--- a/gcc/config/nds32/nds32.h
+++ b/gcc/config/nds32/nds32.h
@@ -688,6 +688,12 @@ enum nds32_builtins
 
 #define BIGGEST_ALIGNMENT 64
 
+#define DATA_ALIGNMENT(constant, basic_align) \
+  nds32_data_alignment (constant, basic_align)
+
+#define LOCAL_ALIGNMENT(type, basic_align) \
+  nds32_local_alignment (type, basic_align)
+
 #define EMPTY_FIELD_BOUNDARY 32
 
 #define STRUCTURE_SIZE_BOUNDARY 8
-- 
GitLab