tree.c (gimple_canonical_types_compatible_p): Do not compare function attributes.
[gcc.git] / gcc / lto / lto-symtab.c
index 2332225afd4760c8d8f64857f404e32f74df8ed5..995c7c98c75ecebc4c31ea6b7a4171c7caf0ef14 100644 (file)
@@ -1,5 +1,5 @@
 /* LTO symbol table.
-   Copyright (C) 2009-2014 Free Software Foundation, Inc.
+   Copyright (C) 2009-2015 Free Software Foundation, Inc.
    Contributed by CodeSourcery, Inc.
 
 This file is part of GCC.
@@ -22,19 +22,41 @@ along with GCC; see the file COPYING3.  If not see
 #include "system.h"
 #include "coretypes.h"
 #include "diagnostic-core.h"
+#include "hash-set.h"
+#include "machmode.h"
+#include "vec.h"
+#include "double-int.h"
+#include "input.h"
+#include "alias.h"
+#include "symtab.h"
+#include "options.h"
+#include "wide-int.h"
+#include "inchash.h"
 #include "tree.h"
+#include "fold-const.h"
+#include "predict.h"
+#include "tm.h"
+#include "hard-reg-set.h"
+#include "input.h"
+#include "function.h"
 #include "basic-block.h"
 #include "tree-ssa-alias.h"
 #include "internal-fn.h"
 #include "gimple-expr.h"
 #include "is-a.h"
 #include "gimple.h"
-#include "hashtab.h"
 #include "plugin-api.h"
+#include "hash-map.h"
+#include "ipa-ref.h"
+#include "cgraph.h"
 #include "lto-streamer.h"
 #include "ipa-utils.h"
+#include "alloc-pool.h"
+#include "symbol-summary.h"
+#include "ipa-prop.h"
 #include "ipa-inline.h"
 #include "builtins.h"
+#include "print-tree.h"
 
 /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
    all edges and removing the old node.  */
@@ -46,9 +68,9 @@ lto_cgraph_replace_node (struct cgraph_node *node,
   struct cgraph_edge *e, *next;
   bool compatible_p;
 
-  if (cgraph_dump_file)
+  if (symtab->dump_file)
     {
-      fprintf (cgraph_dump_file, "Replacing cgraph node %s/%i by %s/%i"
+      fprintf (symtab->dump_file, "Replacing cgraph node %s/%i by %s/%i"
               " for symbol %s\n",
               node->name (), node->order,
               prevailing_node->name (),
@@ -67,6 +89,8 @@ lto_cgraph_replace_node (struct cgraph_node *node,
       gcc_assert (!prevailing_node->global.inlined_to);
       prevailing_node->mark_address_taken ();
     }
+  if (node->definition && prevailing_node->definition)
+    prevailing_node->merged = true;
 
   /* Redirect all incoming edges.  */
   compatible_p
@@ -75,7 +99,7 @@ lto_cgraph_replace_node (struct cgraph_node *node,
   for (e = node->callers; e; e = next)
     {
       next = e->next_caller;
-      cgraph_redirect_edge_callee (e, prevailing_node);
+      e->redirect_callee (prevailing_node);
       /* If there is a mismatch between the supposed callee return type and
         the real one do not attempt to inline this function.
         ???  We really need a way to match function signatures for ABI
@@ -86,6 +110,20 @@ lto_cgraph_replace_node (struct cgraph_node *node,
   /* Redirect incomming references.  */
   prevailing_node->clone_referring (node);
 
+  /* Fix instrumentation references.  */
+  if (node->instrumented_version)
+    {
+      gcc_assert (node->instrumentation_clone
+                 == prevailing_node->instrumentation_clone);
+      node->instrumented_version->instrumented_version = prevailing_node;
+      if (!prevailing_node->instrumented_version)
+       prevailing_node->instrumented_version = node->instrumented_version;
+      /* Need to reset node->instrumented_version to NULL,
+        otherwise node removal code would reset
+        node->instrumented_version->instrumented_version.  */
+      node->instrumented_version = NULL;
+    }
+
   ipa_merge_profiles (prevailing_node, node);
   lto_free_function_in_decl_state_for_node (node);
 
@@ -117,57 +155,98 @@ lto_varpool_replace_node (varpool_node *vnode,
       && vnode->decl != prevailing_node->decl)
     DECL_INITIAL (vnode->decl) = error_mark_node;
 
+  /* Check and report ODR violations on virtual tables.  */
+  if (DECL_VIRTUAL_P (vnode->decl) || DECL_VIRTUAL_P (prevailing_node->decl))
+    compare_virtual_tables (prevailing_node, vnode);
+
   if (vnode->tls_model != prevailing_node->tls_model)
     {
-      error_at (DECL_SOURCE_LOCATION (vnode->decl),
-               "%qD is defined as %s", vnode->decl, tls_model_names [vnode->tls_model]);
-      inform (DECL_SOURCE_LOCATION (prevailing_node->decl),
-             "previously defined here as %s",
-             tls_model_names [prevailing_node->tls_model]);
+      bool error = false;
+
+      /* Non-TLS and TLS never mix together.  Also emulated model is not
+        compatible with anything else.  */
+      if (prevailing_node->tls_model == TLS_MODEL_NONE
+         || prevailing_node->tls_model == TLS_MODEL_EMULATED
+         || vnode->tls_model == TLS_MODEL_NONE
+         || vnode->tls_model == TLS_MODEL_EMULATED)
+       error = true;
+      /* Linked is silently supporting transitions
+        GD -> IE, GD -> LE, LD -> LE, IE -> LE, LD -> IE.
+        Do the same transitions and error out on others.  */
+      else if ((prevailing_node->tls_model == TLS_MODEL_REAL
+               || prevailing_node->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
+              && (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
+                  || vnode->tls_model == TLS_MODEL_LOCAL_EXEC))
+       prevailing_node->tls_model = vnode->tls_model;
+      else if ((vnode->tls_model == TLS_MODEL_REAL
+               || vnode->tls_model == TLS_MODEL_LOCAL_DYNAMIC)
+              && (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
+                  || prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC))
+       ;
+      else if (prevailing_node->tls_model == TLS_MODEL_INITIAL_EXEC
+              && vnode->tls_model == TLS_MODEL_LOCAL_EXEC)
+       prevailing_node->tls_model = vnode->tls_model;
+      else if (vnode->tls_model == TLS_MODEL_INITIAL_EXEC
+              && prevailing_node->tls_model == TLS_MODEL_LOCAL_EXEC)
+       ;
+      else
+       error = true;
+      if (error)
+       {
+         error_at (DECL_SOURCE_LOCATION (vnode->decl),
+                   "%qD is defined with tls model %s", vnode->decl, tls_model_names [vnode->tls_model]);
+         inform (DECL_SOURCE_LOCATION (prevailing_node->decl),
+                 "previously defined here as %s",
+                 tls_model_names [prevailing_node->tls_model]);
+       }
     }
   /* Finally remove the replaced node.  */
   vnode->remove ();
 }
 
-/* Merge two variable or function symbol table entries PREVAILING and ENTRY.
-   Return false if the symbols are not fully compatible and a diagnostic
-   should be emitted.  */
+/* Return non-zero if we want to output waring about T1 and T2.
+   Return value is a bitmask of reasons of violation:
+   Bit 0 indicates that types are not compatible of memory layout.
+   Bot 1 indicates that types are not compatible because of C++ ODR rule.  */
 
-static bool
-lto_symtab_merge (symtab_node *prevailing, symtab_node *entry)
+static int
+warn_type_compatibility_p (tree prevailing_type, tree type)
 {
-  tree prevailing_decl = prevailing->decl;
-  tree decl = entry->decl;
-  tree prevailing_type, type;
-
-  if (prevailing_decl == decl)
-    return true;
-
-  /* Merge decl state in both directions, we may still end up using
-     the new decl.  */
-  TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
-  TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
-
-  /* The linker may ask us to combine two incompatible symbols.
-     Detect this case and notify the caller of required diagnostics.  */
-
-  if (TREE_CODE (decl) == FUNCTION_DECL)
+  int lev = 0;
+  /* C++ provide a robust way to check for type compatibility via the ODR
+     rule.  */
+  if (odr_or_derived_type_p (prevailing_type) && odr_type_p (type)
+      && !odr_types_equivalent_p (prevailing_type, type))
+    lev = 2;
+
+  /* Function types needs special care, because types_compatible_p never
+     thinks prototype is compatible to non-prototype.  */
+  if ((TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
+      && TREE_CODE (type) == TREE_CODE (prevailing_type))
     {
-      if (!types_compatible_p (TREE_TYPE (prevailing_decl),
-                              TREE_TYPE (decl)))
-       /* If we don't have a merged type yet...sigh.  The linker
-          wouldn't complain if the types were mismatched, so we
-          probably shouldn't either.  Just use the type from
-          whichever decl appears to be associated with the
-          definition.  If for some odd reason neither decl is, the
-          older one wins.  */
-       (void) 0;
-
-      return true;
+      lev |= warn_type_compatibility_p (TREE_TYPE (prevailing_type),
+                                       TREE_TYPE (type));
+      if (TREE_CODE (type) == METHOD_TYPE)
+       lev |= warn_type_compatibility_p (TYPE_METHOD_BASETYPE (prevailing_type),
+                                         TYPE_METHOD_BASETYPE (type));
+      if (prototype_p (prevailing_type) && prototype_p (type)
+         && TYPE_ARG_TYPES (prevailing_type) != TYPE_ARG_TYPES (type))
+       {
+         tree parm1, parm2;
+         for (parm1 = TYPE_ARG_TYPES (prevailing_type),
+              parm2 = TYPE_ARG_TYPES (type);
+              parm1 && parm2;
+              parm1 = TREE_CHAIN (prevailing_type),
+              parm2 = TREE_CHAIN (type))
+           lev |= warn_type_compatibility_p (TREE_VALUE (parm1),
+                                             TREE_VALUE (parm2));
+         if (parm1 || parm2)
+           lev = 3;
+       }
+      if (comp_type_attributes (prevailing_type, type) == 0)
+       lev = 3;
+      return lev;
     }
-
-  /* Now we exclusively deal with VAR_DECLs.  */
-
   /* Sharing a global symbol is a strong hint that two types are
      compatible.  We could use this information to complete
      incomplete pointed-to types more aggressively here, ignoring
@@ -180,19 +259,22 @@ lto_symtab_merge (symtab_node *prevailing, symtab_node *entry)
      ???  In principle we might want to only warn for structurally
      incompatible types here, but unless we have protective measures
      for TBAA in place that would hide useful information.  */
-  prevailing_type = TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl));
-  type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
+  prevailing_type = TYPE_MAIN_VARIANT (prevailing_type);
+  type = TYPE_MAIN_VARIANT (type);
 
   if (!types_compatible_p (prevailing_type, type))
     {
-      if (COMPLETE_TYPE_P (type))
-       return false;
+      if (TREE_CODE (prevailing_type) == FUNCTION_TYPE
+         || TREE_CODE (type) == METHOD_TYPE)
+       return 1 | lev;
+      if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (prevailing_type))
+       return 1 | lev;
 
       /* If type is incomplete then avoid warnings in the cases
         that TBAA handles just fine.  */
 
       if (TREE_CODE (prevailing_type) != TREE_CODE (type))
-       return false;
+       return 1 | lev;
 
       if (TREE_CODE (prevailing_type) == ARRAY_TYPE)
        {
@@ -206,10 +288,10 @@ lto_symtab_merge (symtab_node *prevailing, symtab_node *entry)
            }
 
          if (TREE_CODE (tem1) != TREE_CODE (tem2))
-           return false;
+           return 1 | lev;
 
          if (!types_compatible_p (tem1, tem2))
-           return false;
+           return 1 | lev;
        }
 
       /* Fallthru.  Compatible enough.  */
@@ -218,6 +300,43 @@ lto_symtab_merge (symtab_node *prevailing, symtab_node *entry)
   /* ???  We might want to emit a warning here if type qualification
      differences were spotted.  Do not do this unconditionally though.  */
 
+  return lev;
+}
+
+/* Merge two variable or function symbol table entries PREVAILING and ENTRY.
+   Return false if the symbols are not fully compatible and a diagnostic
+   should be emitted.  */
+
+static bool
+lto_symtab_merge (symtab_node *prevailing, symtab_node *entry)
+{
+  tree prevailing_decl = prevailing->decl;
+  tree decl = entry->decl;
+
+  if (prevailing_decl == decl)
+    return true;
+
+  /* Merge decl state in both directions, we may still end up using
+     the new decl.  */
+  TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
+  TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
+
+  /* The linker may ask us to combine two incompatible symbols.
+     Detect this case and notify the caller of required diagnostics.  */
+
+  if (TREE_CODE (decl) == FUNCTION_DECL)
+    {
+      if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl),
+                                    TREE_TYPE (decl)))
+       return false;
+
+      return true;
+    }
+
+  if (warn_type_compatibility_p (TREE_TYPE (prevailing_decl),
+                                TREE_TYPE (decl)))
+    return false;
+
   /* There is no point in comparing too many details of the decls here.
      The type compatibility checks or the completing of types has properly
      dealt with most issues.  */
@@ -310,7 +429,7 @@ lto_symtab_resolve_symbols (symtab_node *first)
            && (e->resolution == LDPR_PREVAILING_DEF_IRONLY
                || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
                || e->resolution == LDPR_PREVAILING_DEF))
-         fatal_error ("multiple prevailing defs for %qE",
+         fatal_error (input_location, "multiple prevailing defs for %qE",
                       DECL_NAME (prevailing->decl));
       return prevailing;
     }
@@ -399,7 +518,8 @@ lto_symtab_merge_decls_2 (symtab_node *first, bool diagnosed_p)
     if (TREE_PUBLIC (e->decl))
       {
        if (!lto_symtab_merge (prevailing, e)
-           && !diagnosed_p)
+           && !diagnosed_p
+           && !DECL_ARTIFICIAL (e->decl))
          mismatches.safe_push (e->decl);
       }
   if (mismatches.is_empty ())
@@ -408,24 +528,39 @@ lto_symtab_merge_decls_2 (symtab_node *first, bool diagnosed_p)
   /* Diagnose all mismatched re-declarations.  */
   FOR_EACH_VEC_ELT (mismatches, i, decl)
     {
-      if (!types_compatible_p (TREE_TYPE (prevailing->decl),
-                              TREE_TYPE (decl)))
-       diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
-                                  "type of %qD does not match original "
-                                  "declaration", decl);
-
+      int level = warn_type_compatibility_p (TREE_TYPE (prevailing->decl),
+                                            TREE_TYPE (decl));
+      if (level)
+       {
+         bool diag = false;
+         if (level > 1)
+           diag = warning_at (DECL_SOURCE_LOCATION (decl),
+                              OPT_Wodr,
+                              "%qD violates the C++ One Definition Rule ",
+                              decl);
+         if (!diag && (level & 1))
+           diag = warning_at (DECL_SOURCE_LOCATION (decl),
+                              OPT_Wlto_type_mismatch,
+                              "type of %qD does not match original "
+                              "declaration", decl);
+         if (diag)
+           warn_types_mismatch (TREE_TYPE (prevailing->decl),
+                                TREE_TYPE (decl));
+         diagnosed_p |= diag;
+       }
       else if ((DECL_USER_ALIGN (prevailing->decl)
                && DECL_USER_ALIGN (decl))
               && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
        {
-         diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
+         diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl),
+                                    OPT_Wlto_type_mismatch,
                                     "alignment of %qD is bigger than "
                                     "original declaration", decl);
        }
     }
   if (diagnosed_p)
     inform (DECL_SOURCE_LOCATION (prevailing->decl),
-           "previously declared here");
+           "%qD was previously declared here", prevailing->decl);
 
   mismatches.release ();
 }
@@ -439,13 +574,13 @@ lto_symtab_merge_decls_1 (symtab_node *first)
   symtab_node *prevailing;
   bool diagnosed_p = false;
 
-  if (cgraph_dump_file)
+  if (symtab->dump_file)
     {
-      fprintf (cgraph_dump_file, "Merging nodes for %s. Candidates:\n",
+      fprintf (symtab->dump_file, "Merging nodes for %s. Candidates:\n",
               first->asm_name ());
       for (e = first; e; e = e->next_sharing_asm_name)
        if (TREE_PUBLIC (e->decl))
-         e->dump (cgraph_dump_file);
+         e->dump (symtab->dump_file);
     }
 
   /* Compute the symbol resolutions.  This is a no-op when using the
@@ -493,7 +628,7 @@ lto_symtab_merge_decls_1 (symtab_node *first)
        }
     }
 
-  symtab_prevail_in_asm_name_hash (prevailing);
+  symtab->symtab_prevail_in_asm_name_hash (prevailing);
 
   /* Diagnose mismatched objects.  */
   for (e = prevailing->next_sharing_asm_name;
@@ -535,11 +670,11 @@ lto_symtab_merge_decls_1 (symtab_node *first)
      mismatches.  */
   lto_symtab_merge_decls_2 (prevailing, diagnosed_p);
 
-  if (cgraph_dump_file)
+  if (symtab->dump_file)
     {
-      fprintf (cgraph_dump_file, "After resolution:\n");
+      fprintf (symtab->dump_file, "After resolution:\n");
       for (e = prevailing; e; e = e->next_sharing_asm_name)
-       e->dump (cgraph_dump_file);
+       e->dump (symtab->dump_file);
     }
 }
 
@@ -551,7 +686,7 @@ lto_symtab_merge_decls (void)
   symtab_node *node;
 
   /* Populate assembler name hash.   */
-  symtab_initialize_asm_name_hash ();
+  symtab->symtab_initialize_asm_name_hash ();
 
   FOR_EACH_SYMBOL (node)
     if (!node->previous_sharing_asm_name
@@ -595,7 +730,7 @@ lto_symtab_merge_symbols (void)
 
   if (!flag_ltrans)
     {
-      symtab_initialize_asm_name_hash ();
+      symtab->symtab_initialize_asm_name_hash ();
 
       /* Do the actual merging.  
          At this point we invalidate hash translating decls into symtab nodes
@@ -617,7 +752,7 @@ lto_symtab_merge_symbols (void)
 
          if (!node->analyzed && node->alias_target)
            {
-             symtab_node *tgt = symtab_node_for_asm (node->alias_target);
+             symtab_node *tgt = symtab_node::get_for_asmname (node->alias_target);
              gcc_assert (node->weakref);
              if (tgt)
                node->resolve_alias (tgt);
@@ -649,7 +784,7 @@ lto_symtab_merge_symbols (void)
 
              /* Abstract functions may have duplicated cgraph nodes attached;
                 remove them.  */
-             else if (cnode && DECL_ABSTRACT (cnode->decl)
+             else if (cnode && DECL_ABSTRACT_P (cnode->decl)
                       && (cnode2 = cgraph_node::get (node->decl))
                       && cnode2 != cnode)
                cnode2->remove ();
@@ -671,8 +806,8 @@ lto_symtab_prevailing_decl (tree decl)
   if ((!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl)) || is_builtin_fn (decl))
     return decl;
 
-  /* DECL_ABSTRACTs are their own prevailng decl.  */
-  if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
+  /* DECL_ABSTRACT_Ps are their own prevailing decl.  */
+  if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT_P (decl))
     return decl;
 
   /* Likewise builtins are their own prevailing decl.  This preserves
@@ -684,7 +819,7 @@ lto_symtab_prevailing_decl (tree decl)
   gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
 
   /* Walk through the list of candidates and return the one we merged to.  */
-  ret = symtab_node_for_asm (DECL_ASSEMBLER_NAME (decl));
+  ret = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl));
   if (!ret)
     return decl;