static enum loongarch_symbol_type
loongarch_classify_symbol (const_rtx x)
{
+ enum loongarch_symbol_type pcrel =
+ TARGET_CMODEL_EXTREME ? SYMBOL_PCREL64 : SYMBOL_PCREL;
+
if (!SYMBOL_REF_P (x))
- return SYMBOL_PCREL;
+ return pcrel;
if (SYMBOL_REF_TLS_MODEL (x))
return SYMBOL_TLS;
if (!loongarch_symbol_binds_local_p (x))
return SYMBOL_GOT_DISP;
- return SYMBOL_PCREL;
+ tree t = SYMBOL_REF_DECL (x);
+ if (!t)
+ return pcrel;
+
+ t = lookup_attribute ("model", DECL_ATTRIBUTES (t));
+ if (!t)
+ return pcrel;
+
+ t = TREE_VALUE (TREE_VALUE (t));
+
+ /* loongarch_handle_model_attribute should reject other values. */
+ gcc_assert (TREE_CODE (t) == STRING_CST);
+
+ const char *model = TREE_STRING_POINTER (t);
+ if (strcmp (model, "normal") == 0)
+ return SYMBOL_PCREL;
+ if (strcmp (model, "extreme") == 0)
+ return SYMBOL_PCREL64;
+
+ /* loongarch_handle_model_attribute should reject unknown model
+ name. */
+ gcc_unreachable ();
}
/* Classify the base of symbolic expression X, given that X appears in
case SYMBOL_TLSGD:
case SYMBOL_TLSLDM:
case SYMBOL_PCREL:
+ case SYMBOL_PCREL64:
/* GAS rejects offsets outside the range [-2^31, 2^31-1]. */
return sext_hwi (INTVAL (offset), 32) == INTVAL (offset);
case SYMBOL_TLSLDM:
return 3;
+ case SYMBOL_PCREL64:
+ return 5;
+
case SYMBOL_TLS:
/* We don't treat a bare TLS symbol as a constant. */
return 0;
return true;
}
-/* Should a symbol of type SYMBOL_TYPE should be split in two? */
+/* Should a symbol of type SYMBOL_TYPE should be split in two or more? */
bool
loongarch_split_symbol_type (enum loongarch_symbol_type symbol_type)
switch (symbol_type)
{
case SYMBOL_PCREL:
+ case SYMBOL_PCREL64:
case SYMBOL_GOT_DISP:
case SYMBOL_TLS_IE:
case SYMBOL_TLS_LE:
return x;
}
+static bool
+loongarch_symbol_extreme_p (enum loongarch_symbol_type type)
+{
+ switch (type)
+ {
+ case SYMBOL_PCREL:
+ return false;
+ case SYMBOL_PCREL64:
+ return true;
+ default:
+ return TARGET_CMODEL_EXTREME;
+ }
+}
+
/* If MODE is MAX_MACHINE_MODE, ADDR appears as a move operand, otherwise
it appears in a MEM of that mode. Return true if ADDR is a legitimate
constant in that context and can be split into high and low parts.
high = gen_rtx_HIGH (Pmode, copy_rtx (addr));
high = loongarch_force_temporary (temp, high);
- if (TARGET_CMODEL_EXTREME && can_create_pseudo_p ())
+ if (loongarch_symbol_extreme_p (symbol_type) && can_create_pseudo_p ())
{
gcc_assert (TARGET_EXPLICIT_RELOCS);
if (low_out)
switch (symbol_type)
{
- case SYMBOL_PCREL:
- {
- if (TARGET_CMODEL_EXTREME && can_create_pseudo_p ())
+ case SYMBOL_PCREL64:
+ if (can_create_pseudo_p ())
+ {
*low_out = gen_rtx_PLUS (Pmode, high, temp1);
- else
- *low_out = gen_rtx_LO_SUM (Pmode, high, addr);
- break;
- }
+ break;
+ }
+ /* fall through */
+ case SYMBOL_PCREL:
+ *low_out = gen_rtx_LO_SUM (Pmode, high, addr);
+ break;
case SYMBOL_GOT_DISP:
/* SYMBOL_GOT_DISP symbols are loaded from the GOT. */
bool hi_reloc)
{
const char *reloc;
+ enum loongarch_symbol_type symbol_type =
+ loongarch_classify_symbolic_expression (op);
- if (TARGET_CMODEL_EXTREME)
+ if (loongarch_symbol_extreme_p (symbol_type))
gcc_assert (TARGET_EXPLICIT_RELOCS);
- switch (loongarch_classify_symbolic_expression (op))
+ switch (symbol_type)
{
- case SYMBOL_PCREL:
+ case SYMBOL_PCREL64:
if (hi64_part)
{
- if (TARGET_CMODEL_EXTREME)
- reloc = hi_reloc ? "%pc64_hi12" : "%pc64_lo20";
- else
- gcc_unreachable ();
+ reloc = hi_reloc ? "%pc64_hi12" : "%pc64_lo20";
+ break;
}
- else
- reloc = hi_reloc ? "%pc_hi20" : "%pc_lo12";
+ /* fall through */
+ case SYMBOL_PCREL:
+ reloc = hi_reloc ? "%pc_hi20" : "%pc_lo12";
break;
case SYMBOL_GOT_DISP:
return crtl->outgoing_args_size;
}
+static tree
+loongarch_handle_model_attribute (tree *node, tree name, tree arg, int,
+ bool *no_add_attrs)
+{
+ tree decl = *node;
+ if (TREE_CODE (decl) == VAR_DECL)
+ {
+ if (DECL_THREAD_LOCAL_P (decl))
+ {
+ error_at (DECL_SOURCE_LOCATION (decl),
+ "%qE attribute cannot be specified for thread-local "
+ "variables", name);
+ *no_add_attrs = true;
+ return NULL_TREE;
+ }
+ if (DECL_CONTEXT (decl)
+ && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
+ && !TREE_STATIC (decl))
+ {
+ error_at (DECL_SOURCE_LOCATION (decl),
+ "%qE attribute cannot be specified for local "
+ "variables", name);
+ *no_add_attrs = true;
+ return NULL_TREE;
+ }
+ if (DECL_REGISTER (decl))
+ {
+ error_at (DECL_SOURCE_LOCATION (decl),
+ "%qE attribute cannot be specified for register "
+ "variables", name);
+ *no_add_attrs = true;
+ return NULL_TREE;
+ }
+ if (!TARGET_EXPLICIT_RELOCS)
+ {
+ error_at (DECL_SOURCE_LOCATION (decl),
+ "%qE attribute requires %s", name, "-mexplicit-relocs");
+ *no_add_attrs = true;
+ return NULL_TREE;
+ }
+
+ arg = TREE_VALUE (arg);
+ if (TREE_CODE (arg) != STRING_CST)
+ {
+ error_at (DECL_SOURCE_LOCATION (decl),
+ "invalid argument of %qE attribute", name);
+ *no_add_attrs = true;
+ return NULL_TREE;
+ }
+
+ const char *model = TREE_STRING_POINTER (arg);
+ if (strcmp (model, "normal") != 0
+ && strcmp (model, "extreme") != 0)
+ {
+ error_at (DECL_SOURCE_LOCATION (decl),
+ "invalid argument of %qE attribute", name);
+ *no_add_attrs = true;
+ return NULL_TREE;
+ }
+
+ if (lookup_attribute ("model", DECL_ATTRIBUTES (decl)))
+ {
+ error_at (DECL_SOURCE_LOCATION (decl),
+ "multiple %qE attribute", name);
+ *no_add_attrs = true;
+ return NULL_TREE;
+ }
+ }
+ else
+ {
+ warning (OPT_Wattributes, "%qE attribute ignored", name);
+ *no_add_attrs = true;
+ }
+ return NULL_TREE;
+}
+
+static const struct attribute_spec loongarch_attribute_table[] =
+{
+ /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
+ affects_type_identity, handler, exclude } */
+ { "model", 1, 1, true, false, false, false,
+ loongarch_handle_model_attribute, NULL },
+ /* The last attribute spec is set to be NULL. */
+ {}
+};
+
+bool
+loongarch_use_anchors_for_symbol_p (const_rtx symbol)
+{
+ tree decl = SYMBOL_REF_DECL (symbol);
+
+ /* The section anchor optimization may break custom address model. */
+ if (decl && lookup_attribute ("model", DECL_ATTRIBUTES (decl)))
+ return false;
+
+ return default_use_anchors_for_symbol_p (symbol);
+}
+
/* Initialize the GCC target structure. */
#undef TARGET_ASM_ALIGNED_HI_OP
#define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
#undef TARGET_HAVE_SPECULATION_SAFE_VALUE
#define TARGET_HAVE_SPECULATION_SAFE_VALUE speculation_safe_value_not_needed
+#undef TARGET_ATTRIBUTE_TABLE
+#define TARGET_ATTRIBUTE_TABLE loongarch_attribute_table
+
+#undef TARGET_USE_ANCHORS_FOR_SYMBOL_P
+#define TARGET_USE_ANCHORS_FOR_SYMBOL_P loongarch_use_anchors_for_symbol_p
+
struct gcc_target targetm = TARGET_INITIALIZER;
#include "gt-loongarch.h"