xref: /dflybsd-src/contrib/gcc-4.7/gcc/lto-symtab.c (revision 04febcfb30580676d3e95f58a16c5137ee478b32)
1*e4b17023SJohn Marino /* LTO symbol table.
2*e4b17023SJohn Marino    Copyright 2009, 2010 Free Software Foundation, Inc.
3*e4b17023SJohn Marino    Contributed by CodeSourcery, Inc.
4*e4b17023SJohn Marino 
5*e4b17023SJohn Marino This file is part of GCC.
6*e4b17023SJohn Marino 
7*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
8*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
9*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
10*e4b17023SJohn Marino version.
11*e4b17023SJohn Marino 
12*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
14*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15*e4b17023SJohn Marino for more details.
16*e4b17023SJohn Marino 
17*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
18*e4b17023SJohn Marino along with GCC; see the file COPYING3.  If not see
19*e4b17023SJohn Marino <http://www.gnu.org/licenses/>.  */
20*e4b17023SJohn Marino 
21*e4b17023SJohn Marino #include "config.h"
22*e4b17023SJohn Marino #include "system.h"
23*e4b17023SJohn Marino #include "coretypes.h"
24*e4b17023SJohn Marino #include "diagnostic-core.h"
25*e4b17023SJohn Marino #include "tree.h"
26*e4b17023SJohn Marino #include "gimple.h"
27*e4b17023SJohn Marino #include "ggc.h"
28*e4b17023SJohn Marino #include "hashtab.h"
29*e4b17023SJohn Marino #include "plugin-api.h"
30*e4b17023SJohn Marino #include "lto-streamer.h"
31*e4b17023SJohn Marino 
32*e4b17023SJohn Marino /* Vector to keep track of external variables we've seen so far.  */
VEC(tree,gc)33*e4b17023SJohn Marino VEC(tree,gc) *lto_global_var_decls;
34*e4b17023SJohn Marino 
35*e4b17023SJohn Marino /* Symbol table entry.  */
36*e4b17023SJohn Marino 
37*e4b17023SJohn Marino struct GTY(()) lto_symtab_entry_def
38*e4b17023SJohn Marino {
39*e4b17023SJohn Marino   /* The symbol table entry key, an IDENTIFIER.  */
40*e4b17023SJohn Marino   tree id;
41*e4b17023SJohn Marino   /* The symbol table entry, a DECL.  */
42*e4b17023SJohn Marino   tree decl;
43*e4b17023SJohn Marino   /* The cgraph node if decl is a function decl.  Filled in during the
44*e4b17023SJohn Marino      merging process.  */
45*e4b17023SJohn Marino   struct cgraph_node *node;
46*e4b17023SJohn Marino   /* The varpool node if decl is a variable decl.  Filled in during the
47*e4b17023SJohn Marino      merging process.  */
48*e4b17023SJohn Marino   struct varpool_node *vnode;
49*e4b17023SJohn Marino   /* LTO file-data and symbol resolution for this decl.  */
50*e4b17023SJohn Marino   struct lto_file_decl_data * GTY((skip (""))) file_data;
51*e4b17023SJohn Marino   enum ld_plugin_symbol_resolution resolution;
52*e4b17023SJohn Marino   /* True when resolution was guessed and not read from the file.  */
53*e4b17023SJohn Marino   bool guessed;
54*e4b17023SJohn Marino   /* Pointer to the next entry with the same key.  Before decl merging
55*e4b17023SJohn Marino      this links all symbols from the different TUs.  After decl merging
56*e4b17023SJohn Marino      this links merged but incompatible decls, thus all prevailing ones
57*e4b17023SJohn Marino      remaining.  */
58*e4b17023SJohn Marino   struct lto_symtab_entry_def *next;
59*e4b17023SJohn Marino };
60*e4b17023SJohn Marino typedef struct lto_symtab_entry_def *lto_symtab_entry_t;
61*e4b17023SJohn Marino 
62*e4b17023SJohn Marino /* A poor man's symbol table. This hashes identifier to prevailing DECL
63*e4b17023SJohn Marino    if there is one. */
64*e4b17023SJohn Marino 
65*e4b17023SJohn Marino static GTY ((if_marked ("lto_symtab_entry_marked_p"),
66*e4b17023SJohn Marino 	     param_is (struct lto_symtab_entry_def)))
67*e4b17023SJohn Marino   htab_t lto_symtab_identifiers;
68*e4b17023SJohn Marino 
69*e4b17023SJohn Marino /* Free symtab hashtable.  */
70*e4b17023SJohn Marino 
71*e4b17023SJohn Marino void
lto_symtab_free(void)72*e4b17023SJohn Marino lto_symtab_free (void)
73*e4b17023SJohn Marino {
74*e4b17023SJohn Marino   htab_delete (lto_symtab_identifiers);
75*e4b17023SJohn Marino   lto_symtab_identifiers = NULL;
76*e4b17023SJohn Marino }
77*e4b17023SJohn Marino 
78*e4b17023SJohn Marino /* Return the hash value of an lto_symtab_entry_t object pointed to by P.  */
79*e4b17023SJohn Marino 
80*e4b17023SJohn Marino static hashval_t
lto_symtab_entry_hash(const void * p)81*e4b17023SJohn Marino lto_symtab_entry_hash (const void *p)
82*e4b17023SJohn Marino {
83*e4b17023SJohn Marino   const struct lto_symtab_entry_def *base =
84*e4b17023SJohn Marino     (const struct lto_symtab_entry_def *) p;
85*e4b17023SJohn Marino   return IDENTIFIER_HASH_VALUE (base->id);
86*e4b17023SJohn Marino }
87*e4b17023SJohn Marino 
88*e4b17023SJohn Marino /* Return non-zero if P1 and P2 points to lto_symtab_entry_def structs
89*e4b17023SJohn Marino    corresponding to the same symbol.  */
90*e4b17023SJohn Marino 
91*e4b17023SJohn Marino static int
lto_symtab_entry_eq(const void * p1,const void * p2)92*e4b17023SJohn Marino lto_symtab_entry_eq (const void *p1, const void *p2)
93*e4b17023SJohn Marino {
94*e4b17023SJohn Marino   const struct lto_symtab_entry_def *base1 =
95*e4b17023SJohn Marino      (const struct lto_symtab_entry_def *) p1;
96*e4b17023SJohn Marino   const struct lto_symtab_entry_def *base2 =
97*e4b17023SJohn Marino      (const struct lto_symtab_entry_def *) p2;
98*e4b17023SJohn Marino   return (base1->id == base2->id);
99*e4b17023SJohn Marino }
100*e4b17023SJohn Marino 
101*e4b17023SJohn Marino /* Returns non-zero if P points to an lto_symtab_entry_def struct that needs
102*e4b17023SJohn Marino    to be marked for GC.  */
103*e4b17023SJohn Marino 
104*e4b17023SJohn Marino static int
lto_symtab_entry_marked_p(const void * p)105*e4b17023SJohn Marino lto_symtab_entry_marked_p (const void *p)
106*e4b17023SJohn Marino {
107*e4b17023SJohn Marino   const struct lto_symtab_entry_def *base =
108*e4b17023SJohn Marino      (const struct lto_symtab_entry_def *) p;
109*e4b17023SJohn Marino 
110*e4b17023SJohn Marino   /* Keep this only if the common IDENTIFIER_NODE of the symtab chain
111*e4b17023SJohn Marino      is marked which it will be if at least one of the DECLs in the
112*e4b17023SJohn Marino      chain is marked.  */
113*e4b17023SJohn Marino   return ggc_marked_p (base->id);
114*e4b17023SJohn Marino }
115*e4b17023SJohn Marino 
116*e4b17023SJohn Marino /* Lazily initialize resolution hash tables.  */
117*e4b17023SJohn Marino 
118*e4b17023SJohn Marino static void
lto_symtab_maybe_init_hash_table(void)119*e4b17023SJohn Marino lto_symtab_maybe_init_hash_table (void)
120*e4b17023SJohn Marino {
121*e4b17023SJohn Marino   if (lto_symtab_identifiers)
122*e4b17023SJohn Marino     return;
123*e4b17023SJohn Marino 
124*e4b17023SJohn Marino   lto_symtab_identifiers =
125*e4b17023SJohn Marino     htab_create_ggc (1021, lto_symtab_entry_hash,
126*e4b17023SJohn Marino 		     lto_symtab_entry_eq, NULL);
127*e4b17023SJohn Marino }
128*e4b17023SJohn Marino 
129*e4b17023SJohn Marino /* Registers DECL with the LTO symbol table as having resolution RESOLUTION
130*e4b17023SJohn Marino    and read from FILE_DATA. */
131*e4b17023SJohn Marino 
132*e4b17023SJohn Marino void
lto_symtab_register_decl(tree decl,ld_plugin_symbol_resolution_t resolution,struct lto_file_decl_data * file_data)133*e4b17023SJohn Marino lto_symtab_register_decl (tree decl,
134*e4b17023SJohn Marino 			  ld_plugin_symbol_resolution_t resolution,
135*e4b17023SJohn Marino 			  struct lto_file_decl_data *file_data)
136*e4b17023SJohn Marino {
137*e4b17023SJohn Marino   lto_symtab_entry_t new_entry;
138*e4b17023SJohn Marino   void **slot;
139*e4b17023SJohn Marino 
140*e4b17023SJohn Marino   /* Check that declarations reaching this function do not have
141*e4b17023SJohn Marino      properties inconsistent with having external linkage.  If any of
142*e4b17023SJohn Marino      these asertions fail, then the object file reader has failed to
143*e4b17023SJohn Marino      detect these cases and issue appropriate error messages.  */
144*e4b17023SJohn Marino   gcc_assert (decl
145*e4b17023SJohn Marino 	      && TREE_PUBLIC (decl)
146*e4b17023SJohn Marino 	      && (TREE_CODE (decl) == VAR_DECL
147*e4b17023SJohn Marino 		  || TREE_CODE (decl) == FUNCTION_DECL)
148*e4b17023SJohn Marino 	      && DECL_ASSEMBLER_NAME_SET_P (decl));
149*e4b17023SJohn Marino   if (TREE_CODE (decl) == VAR_DECL
150*e4b17023SJohn Marino       && DECL_INITIAL (decl))
151*e4b17023SJohn Marino     gcc_assert (!DECL_EXTERNAL (decl)
152*e4b17023SJohn Marino 		|| (TREE_STATIC (decl) && TREE_READONLY (decl)));
153*e4b17023SJohn Marino   if (TREE_CODE (decl) == FUNCTION_DECL)
154*e4b17023SJohn Marino     gcc_assert (!DECL_ABSTRACT (decl));
155*e4b17023SJohn Marino 
156*e4b17023SJohn Marino   new_entry = ggc_alloc_cleared_lto_symtab_entry_def ();
157*e4b17023SJohn Marino   new_entry->id = (*targetm.asm_out.mangle_assembler_name)
158*e4b17023SJohn Marino 		  (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
159*e4b17023SJohn Marino   new_entry->decl = decl;
160*e4b17023SJohn Marino   new_entry->resolution = resolution;
161*e4b17023SJohn Marino   new_entry->file_data = file_data;
162*e4b17023SJohn Marino 
163*e4b17023SJohn Marino   lto_symtab_maybe_init_hash_table ();
164*e4b17023SJohn Marino   slot = htab_find_slot (lto_symtab_identifiers, new_entry, INSERT);
165*e4b17023SJohn Marino   new_entry->next = (lto_symtab_entry_t) *slot;
166*e4b17023SJohn Marino   *slot = new_entry;
167*e4b17023SJohn Marino }
168*e4b17023SJohn Marino 
169*e4b17023SJohn Marino /* Get the lto_symtab_entry_def struct associated with ID
170*e4b17023SJohn Marino    if there is one.  */
171*e4b17023SJohn Marino 
172*e4b17023SJohn Marino static lto_symtab_entry_t
lto_symtab_get(tree id)173*e4b17023SJohn Marino lto_symtab_get (tree id)
174*e4b17023SJohn Marino {
175*e4b17023SJohn Marino   struct lto_symtab_entry_def temp;
176*e4b17023SJohn Marino   void **slot;
177*e4b17023SJohn Marino 
178*e4b17023SJohn Marino   lto_symtab_maybe_init_hash_table ();
179*e4b17023SJohn Marino   temp.id = id;
180*e4b17023SJohn Marino   slot = htab_find_slot (lto_symtab_identifiers, &temp, NO_INSERT);
181*e4b17023SJohn Marino   return slot ? (lto_symtab_entry_t) *slot : NULL;
182*e4b17023SJohn Marino }
183*e4b17023SJohn Marino 
184*e4b17023SJohn Marino /* Get the linker resolution for DECL.  */
185*e4b17023SJohn Marino 
186*e4b17023SJohn Marino enum ld_plugin_symbol_resolution
lto_symtab_get_resolution(tree decl)187*e4b17023SJohn Marino lto_symtab_get_resolution (tree decl)
188*e4b17023SJohn Marino {
189*e4b17023SJohn Marino   lto_symtab_entry_t e;
190*e4b17023SJohn Marino 
191*e4b17023SJohn Marino   gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
192*e4b17023SJohn Marino 
193*e4b17023SJohn Marino   e = lto_symtab_get ((*targetm.asm_out.mangle_assembler_name)
194*e4b17023SJohn Marino 		      (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
195*e4b17023SJohn Marino   while (e && e->decl != decl)
196*e4b17023SJohn Marino     e = e->next;
197*e4b17023SJohn Marino   if (!e)
198*e4b17023SJohn Marino     return LDPR_UNKNOWN;
199*e4b17023SJohn Marino 
200*e4b17023SJohn Marino   return e->resolution;
201*e4b17023SJohn Marino }
202*e4b17023SJohn Marino 
203*e4b17023SJohn Marino 
204*e4b17023SJohn Marino /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
205*e4b17023SJohn Marino    all edges and removing the old node.  */
206*e4b17023SJohn Marino 
207*e4b17023SJohn Marino static void
lto_cgraph_replace_node(struct cgraph_node * node,struct cgraph_node * prevailing_node)208*e4b17023SJohn Marino lto_cgraph_replace_node (struct cgraph_node *node,
209*e4b17023SJohn Marino 			 struct cgraph_node *prevailing_node)
210*e4b17023SJohn Marino {
211*e4b17023SJohn Marino   struct cgraph_edge *e, *next;
212*e4b17023SJohn Marino   bool compatible_p;
213*e4b17023SJohn Marino 
214*e4b17023SJohn Marino   if (cgraph_dump_file)
215*e4b17023SJohn Marino     {
216*e4b17023SJohn Marino       fprintf (cgraph_dump_file, "Replacing cgraph node %s/%i by %s/%i"
217*e4b17023SJohn Marino  	       " for symbol %s\n",
218*e4b17023SJohn Marino 	       xstrdup (cgraph_node_name (node)), node->uid,
219*e4b17023SJohn Marino 	       xstrdup (cgraph_node_name (prevailing_node)),
220*e4b17023SJohn Marino 	       prevailing_node->uid,
221*e4b17023SJohn Marino 	       IDENTIFIER_POINTER ((*targetm.asm_out.mangle_assembler_name)
222*e4b17023SJohn Marino 		 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)))));
223*e4b17023SJohn Marino     }
224*e4b17023SJohn Marino 
225*e4b17023SJohn Marino   /* Merge node flags.  */
226*e4b17023SJohn Marino   if (node->needed)
227*e4b17023SJohn Marino     cgraph_mark_needed_node (prevailing_node);
228*e4b17023SJohn Marino   if (node->reachable)
229*e4b17023SJohn Marino     cgraph_mark_reachable_node (prevailing_node);
230*e4b17023SJohn Marino   if (node->address_taken)
231*e4b17023SJohn Marino     {
232*e4b17023SJohn Marino       gcc_assert (!prevailing_node->global.inlined_to);
233*e4b17023SJohn Marino       cgraph_mark_address_taken_node (prevailing_node);
234*e4b17023SJohn Marino     }
235*e4b17023SJohn Marino 
236*e4b17023SJohn Marino   /* Redirect all incoming edges.  */
237*e4b17023SJohn Marino   compatible_p
238*e4b17023SJohn Marino     = types_compatible_p (TREE_TYPE (TREE_TYPE (prevailing_node->decl)),
239*e4b17023SJohn Marino 			  TREE_TYPE (TREE_TYPE (node->decl)));
240*e4b17023SJohn Marino   for (e = node->callers; e; e = next)
241*e4b17023SJohn Marino     {
242*e4b17023SJohn Marino       next = e->next_caller;
243*e4b17023SJohn Marino       cgraph_redirect_edge_callee (e, prevailing_node);
244*e4b17023SJohn Marino       /* If there is a mismatch between the supposed callee return type and
245*e4b17023SJohn Marino 	 the real one do not attempt to inline this function.
246*e4b17023SJohn Marino 	 ???  We really need a way to match function signatures for ABI
247*e4b17023SJohn Marino 	 compatibility and perform related promotions at inlining time.  */
248*e4b17023SJohn Marino       if (!compatible_p)
249*e4b17023SJohn Marino 	e->call_stmt_cannot_inline_p = 1;
250*e4b17023SJohn Marino     }
251*e4b17023SJohn Marino   /* Redirect incomming references.  */
252*e4b17023SJohn Marino   ipa_clone_refering (prevailing_node, NULL, &node->ref_list);
253*e4b17023SJohn Marino 
254*e4b17023SJohn Marino   /* Finally remove the replaced node.  */
255*e4b17023SJohn Marino   cgraph_remove_node (node);
256*e4b17023SJohn Marino }
257*e4b17023SJohn Marino 
258*e4b17023SJohn Marino /* Replace the cgraph node NODE with PREVAILING_NODE in the cgraph, merging
259*e4b17023SJohn Marino    all edges and removing the old node.  */
260*e4b17023SJohn Marino 
261*e4b17023SJohn Marino static void
lto_varpool_replace_node(struct varpool_node * vnode,struct varpool_node * prevailing_node)262*e4b17023SJohn Marino lto_varpool_replace_node (struct varpool_node *vnode,
263*e4b17023SJohn Marino 			  struct varpool_node *prevailing_node)
264*e4b17023SJohn Marino {
265*e4b17023SJohn Marino   /* Merge node flags.  */
266*e4b17023SJohn Marino   if (vnode->needed)
267*e4b17023SJohn Marino     {
268*e4b17023SJohn Marino       gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
269*e4b17023SJohn Marino       varpool_mark_needed_node (prevailing_node);
270*e4b17023SJohn Marino     }
271*e4b17023SJohn Marino   gcc_assert (!vnode->finalized || prevailing_node->finalized);
272*e4b17023SJohn Marino   gcc_assert (!vnode->analyzed || prevailing_node->analyzed);
273*e4b17023SJohn Marino 
274*e4b17023SJohn Marino   ipa_clone_refering (NULL, prevailing_node, &vnode->ref_list);
275*e4b17023SJohn Marino 
276*e4b17023SJohn Marino   /* Be sure we can garbage collect the initializer.  */
277*e4b17023SJohn Marino   if (DECL_INITIAL (vnode->decl))
278*e4b17023SJohn Marino     DECL_INITIAL (vnode->decl) = error_mark_node;
279*e4b17023SJohn Marino   /* Finally remove the replaced node.  */
280*e4b17023SJohn Marino   varpool_remove_node (vnode);
281*e4b17023SJohn Marino }
282*e4b17023SJohn Marino 
283*e4b17023SJohn Marino /* Merge two variable or function symbol table entries PREVAILING and ENTRY.
284*e4b17023SJohn Marino    Return false if the symbols are not fully compatible and a diagnostic
285*e4b17023SJohn Marino    should be emitted.  */
286*e4b17023SJohn Marino 
287*e4b17023SJohn Marino static bool
lto_symtab_merge(lto_symtab_entry_t prevailing,lto_symtab_entry_t entry)288*e4b17023SJohn Marino lto_symtab_merge (lto_symtab_entry_t prevailing, lto_symtab_entry_t entry)
289*e4b17023SJohn Marino {
290*e4b17023SJohn Marino   tree prevailing_decl = prevailing->decl;
291*e4b17023SJohn Marino   tree decl = entry->decl;
292*e4b17023SJohn Marino   tree prevailing_type, type;
293*e4b17023SJohn Marino 
294*e4b17023SJohn Marino   /* Merge decl state in both directions, we may still end up using
295*e4b17023SJohn Marino      the new decl.  */
296*e4b17023SJohn Marino   TREE_ADDRESSABLE (prevailing_decl) |= TREE_ADDRESSABLE (decl);
297*e4b17023SJohn Marino   TREE_ADDRESSABLE (decl) |= TREE_ADDRESSABLE (prevailing_decl);
298*e4b17023SJohn Marino 
299*e4b17023SJohn Marino   /* The linker may ask us to combine two incompatible symbols.
300*e4b17023SJohn Marino      Detect this case and notify the caller of required diagnostics.  */
301*e4b17023SJohn Marino 
302*e4b17023SJohn Marino   if (TREE_CODE (decl) == FUNCTION_DECL)
303*e4b17023SJohn Marino     {
304*e4b17023SJohn Marino       if (!types_compatible_p (TREE_TYPE (prevailing_decl),
305*e4b17023SJohn Marino 			       TREE_TYPE (decl)))
306*e4b17023SJohn Marino 	/* If we don't have a merged type yet...sigh.  The linker
307*e4b17023SJohn Marino 	   wouldn't complain if the types were mismatched, so we
308*e4b17023SJohn Marino 	   probably shouldn't either.  Just use the type from
309*e4b17023SJohn Marino 	   whichever decl appears to be associated with the
310*e4b17023SJohn Marino 	   definition.  If for some odd reason neither decl is, the
311*e4b17023SJohn Marino 	   older one wins.  */
312*e4b17023SJohn Marino 	(void) 0;
313*e4b17023SJohn Marino 
314*e4b17023SJohn Marino       return true;
315*e4b17023SJohn Marino     }
316*e4b17023SJohn Marino 
317*e4b17023SJohn Marino   /* Now we exclusively deal with VAR_DECLs.  */
318*e4b17023SJohn Marino 
319*e4b17023SJohn Marino   /* Sharing a global symbol is a strong hint that two types are
320*e4b17023SJohn Marino      compatible.  We could use this information to complete
321*e4b17023SJohn Marino      incomplete pointed-to types more aggressively here, ignoring
322*e4b17023SJohn Marino      mismatches in both field and tag names.  It's difficult though
323*e4b17023SJohn Marino      to guarantee that this does not have side-effects on merging
324*e4b17023SJohn Marino      more compatible types from other translation units though.  */
325*e4b17023SJohn Marino 
326*e4b17023SJohn Marino   /* We can tolerate differences in type qualification, the
327*e4b17023SJohn Marino      qualification of the prevailing definition will prevail.
328*e4b17023SJohn Marino      ???  In principle we might want to only warn for structurally
329*e4b17023SJohn Marino      incompatible types here, but unless we have protective measures
330*e4b17023SJohn Marino      for TBAA in place that would hide useful information.  */
331*e4b17023SJohn Marino   prevailing_type = TYPE_MAIN_VARIANT (TREE_TYPE (prevailing_decl));
332*e4b17023SJohn Marino   type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
333*e4b17023SJohn Marino 
334*e4b17023SJohn Marino   if (!types_compatible_p (prevailing_type, type))
335*e4b17023SJohn Marino     {
336*e4b17023SJohn Marino       if (COMPLETE_TYPE_P (type))
337*e4b17023SJohn Marino 	return false;
338*e4b17023SJohn Marino 
339*e4b17023SJohn Marino       /* If type is incomplete then avoid warnings in the cases
340*e4b17023SJohn Marino 	 that TBAA handles just fine.  */
341*e4b17023SJohn Marino 
342*e4b17023SJohn Marino       if (TREE_CODE (prevailing_type) != TREE_CODE (type))
343*e4b17023SJohn Marino 	return false;
344*e4b17023SJohn Marino 
345*e4b17023SJohn Marino       if (TREE_CODE (prevailing_type) == ARRAY_TYPE)
346*e4b17023SJohn Marino 	{
347*e4b17023SJohn Marino 	  tree tem1 = TREE_TYPE (prevailing_type);
348*e4b17023SJohn Marino 	  tree tem2 = TREE_TYPE (type);
349*e4b17023SJohn Marino 	  while (TREE_CODE (tem1) == ARRAY_TYPE
350*e4b17023SJohn Marino 		 && TREE_CODE (tem2) == ARRAY_TYPE)
351*e4b17023SJohn Marino 	    {
352*e4b17023SJohn Marino 	      tem1 = TREE_TYPE (tem1);
353*e4b17023SJohn Marino 	      tem2 = TREE_TYPE (tem2);
354*e4b17023SJohn Marino 	    }
355*e4b17023SJohn Marino 
356*e4b17023SJohn Marino 	  if (TREE_CODE (tem1) != TREE_CODE (tem2))
357*e4b17023SJohn Marino 	    return false;
358*e4b17023SJohn Marino 
359*e4b17023SJohn Marino 	  if (!types_compatible_p (tem1, tem2))
360*e4b17023SJohn Marino 	    return false;
361*e4b17023SJohn Marino 	}
362*e4b17023SJohn Marino 
363*e4b17023SJohn Marino       /* Fallthru.  Compatible enough.  */
364*e4b17023SJohn Marino     }
365*e4b17023SJohn Marino 
366*e4b17023SJohn Marino   /* ???  We might want to emit a warning here if type qualification
367*e4b17023SJohn Marino      differences were spotted.  Do not do this unconditionally though.  */
368*e4b17023SJohn Marino 
369*e4b17023SJohn Marino   /* There is no point in comparing too many details of the decls here.
370*e4b17023SJohn Marino      The type compatibility checks or the completing of types has properly
371*e4b17023SJohn Marino      dealt with most issues.  */
372*e4b17023SJohn Marino 
373*e4b17023SJohn Marino   /* The following should all not invoke fatal errors as in non-LTO
374*e4b17023SJohn Marino      mode the linker wouldn't complain either.  Just emit warnings.  */
375*e4b17023SJohn Marino 
376*e4b17023SJohn Marino   /* Report a warning if user-specified alignments do not match.  */
377*e4b17023SJohn Marino   if ((DECL_USER_ALIGN (prevailing_decl) && DECL_USER_ALIGN (decl))
378*e4b17023SJohn Marino       && DECL_ALIGN (prevailing_decl) < DECL_ALIGN (decl))
379*e4b17023SJohn Marino     return false;
380*e4b17023SJohn Marino 
381*e4b17023SJohn Marino   return true;
382*e4b17023SJohn Marino }
383*e4b17023SJohn Marino 
384*e4b17023SJohn Marino /* Return true if the symtab entry E can be replaced by another symtab
385*e4b17023SJohn Marino    entry.  */
386*e4b17023SJohn Marino 
387*e4b17023SJohn Marino static bool
lto_symtab_resolve_replaceable_p(lto_symtab_entry_t e)388*e4b17023SJohn Marino lto_symtab_resolve_replaceable_p (lto_symtab_entry_t e)
389*e4b17023SJohn Marino {
390*e4b17023SJohn Marino   if (DECL_EXTERNAL (e->decl)
391*e4b17023SJohn Marino       || DECL_COMDAT (e->decl)
392*e4b17023SJohn Marino       || DECL_ONE_ONLY (e->decl)
393*e4b17023SJohn Marino       || DECL_WEAK (e->decl))
394*e4b17023SJohn Marino     return true;
395*e4b17023SJohn Marino 
396*e4b17023SJohn Marino   if (TREE_CODE (e->decl) == VAR_DECL)
397*e4b17023SJohn Marino     return (DECL_COMMON (e->decl)
398*e4b17023SJohn Marino 	    || (!flag_no_common && !DECL_INITIAL (e->decl)));
399*e4b17023SJohn Marino 
400*e4b17023SJohn Marino   return false;
401*e4b17023SJohn Marino }
402*e4b17023SJohn Marino 
403*e4b17023SJohn Marino /* Return true if the symtab entry E can be the prevailing one.  */
404*e4b17023SJohn Marino 
405*e4b17023SJohn Marino static bool
lto_symtab_resolve_can_prevail_p(lto_symtab_entry_t e)406*e4b17023SJohn Marino lto_symtab_resolve_can_prevail_p (lto_symtab_entry_t e)
407*e4b17023SJohn Marino {
408*e4b17023SJohn Marino   /* The C++ frontend ends up neither setting TREE_STATIC nor
409*e4b17023SJohn Marino      DECL_EXTERNAL on virtual methods but only TREE_PUBLIC.
410*e4b17023SJohn Marino      So do not reject !TREE_STATIC here but only DECL_EXTERNAL.  */
411*e4b17023SJohn Marino   if (DECL_EXTERNAL (e->decl))
412*e4b17023SJohn Marino     return false;
413*e4b17023SJohn Marino 
414*e4b17023SJohn Marino   /* For functions we need a non-discarded body.  */
415*e4b17023SJohn Marino   if (TREE_CODE (e->decl) == FUNCTION_DECL)
416*e4b17023SJohn Marino     return (e->node && e->node->analyzed);
417*e4b17023SJohn Marino 
418*e4b17023SJohn Marino   else if (TREE_CODE (e->decl) == VAR_DECL)
419*e4b17023SJohn Marino     {
420*e4b17023SJohn Marino       if (!e->vnode)
421*e4b17023SJohn Marino 	return false;
422*e4b17023SJohn Marino       return e->vnode->finalized;
423*e4b17023SJohn Marino     }
424*e4b17023SJohn Marino 
425*e4b17023SJohn Marino   gcc_unreachable ();
426*e4b17023SJohn Marino }
427*e4b17023SJohn Marino 
428*e4b17023SJohn Marino /* Resolve the symbol with the candidates in the chain *SLOT and store
429*e4b17023SJohn Marino    their resolutions.  */
430*e4b17023SJohn Marino 
431*e4b17023SJohn Marino static void
lto_symtab_resolve_symbols(void ** slot)432*e4b17023SJohn Marino lto_symtab_resolve_symbols (void **slot)
433*e4b17023SJohn Marino {
434*e4b17023SJohn Marino   lto_symtab_entry_t e;
435*e4b17023SJohn Marino   lto_symtab_entry_t prevailing = NULL;
436*e4b17023SJohn Marino 
437*e4b17023SJohn Marino   /* Always set e->node so that edges are updated to reflect decl merging. */
438*e4b17023SJohn Marino   for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
439*e4b17023SJohn Marino     {
440*e4b17023SJohn Marino       if (TREE_CODE (e->decl) == FUNCTION_DECL)
441*e4b17023SJohn Marino 	e->node = cgraph_get_node (e->decl);
442*e4b17023SJohn Marino       else if (TREE_CODE (e->decl) == VAR_DECL)
443*e4b17023SJohn Marino 	e->vnode = varpool_get_node (e->decl);
444*e4b17023SJohn Marino       if (e->resolution == LDPR_PREVAILING_DEF_IRONLY
445*e4b17023SJohn Marino 	  || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
446*e4b17023SJohn Marino 	  || e->resolution == LDPR_PREVAILING_DEF)
447*e4b17023SJohn Marino 	prevailing = e;
448*e4b17023SJohn Marino     }
449*e4b17023SJohn Marino 
450*e4b17023SJohn Marino   /* If the chain is already resolved there is nothing else to do.  */
451*e4b17023SJohn Marino   if (prevailing)
452*e4b17023SJohn Marino     return;
453*e4b17023SJohn Marino 
454*e4b17023SJohn Marino   /* Find the single non-replaceable prevailing symbol and
455*e4b17023SJohn Marino      diagnose ODR violations.  */
456*e4b17023SJohn Marino   for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
457*e4b17023SJohn Marino     {
458*e4b17023SJohn Marino       if (!lto_symtab_resolve_can_prevail_p (e))
459*e4b17023SJohn Marino 	{
460*e4b17023SJohn Marino 	  e->resolution = LDPR_RESOLVED_IR;
461*e4b17023SJohn Marino           e->guessed = true;
462*e4b17023SJohn Marino 	  continue;
463*e4b17023SJohn Marino 	}
464*e4b17023SJohn Marino 
465*e4b17023SJohn Marino       /* Set a default resolution - the final prevailing one will get
466*e4b17023SJohn Marino          adjusted later.  */
467*e4b17023SJohn Marino       e->resolution = LDPR_PREEMPTED_IR;
468*e4b17023SJohn Marino       e->guessed = true;
469*e4b17023SJohn Marino       if (!lto_symtab_resolve_replaceable_p (e))
470*e4b17023SJohn Marino 	{
471*e4b17023SJohn Marino 	  if (prevailing)
472*e4b17023SJohn Marino 	    {
473*e4b17023SJohn Marino 	      error_at (DECL_SOURCE_LOCATION (e->decl),
474*e4b17023SJohn Marino 			"%qD has already been defined", e->decl);
475*e4b17023SJohn Marino 	      inform (DECL_SOURCE_LOCATION (prevailing->decl),
476*e4b17023SJohn Marino 		      "previously defined here");
477*e4b17023SJohn Marino 	    }
478*e4b17023SJohn Marino 	  prevailing = e;
479*e4b17023SJohn Marino 	}
480*e4b17023SJohn Marino     }
481*e4b17023SJohn Marino   if (prevailing)
482*e4b17023SJohn Marino     goto found;
483*e4b17023SJohn Marino 
484*e4b17023SJohn Marino   /* Do a second round choosing one from the replaceable prevailing decls.  */
485*e4b17023SJohn Marino   for (e = (lto_symtab_entry_t) *slot; e; e = e->next)
486*e4b17023SJohn Marino     {
487*e4b17023SJohn Marino       if (e->resolution != LDPR_PREEMPTED_IR)
488*e4b17023SJohn Marino 	continue;
489*e4b17023SJohn Marino 
490*e4b17023SJohn Marino       /* Choose the first function that can prevail as prevailing.  */
491*e4b17023SJohn Marino       if (TREE_CODE (e->decl) == FUNCTION_DECL)
492*e4b17023SJohn Marino 	{
493*e4b17023SJohn Marino 	  prevailing = e;
494*e4b17023SJohn Marino 	  break;
495*e4b17023SJohn Marino 	}
496*e4b17023SJohn Marino 
497*e4b17023SJohn Marino       /* From variables that can prevail choose the largest one.  */
498*e4b17023SJohn Marino       if (!prevailing
499*e4b17023SJohn Marino 	  || tree_int_cst_lt (DECL_SIZE (prevailing->decl),
500*e4b17023SJohn Marino 			      DECL_SIZE (e->decl)))
501*e4b17023SJohn Marino 	prevailing = e;
502*e4b17023SJohn Marino     }
503*e4b17023SJohn Marino 
504*e4b17023SJohn Marino   if (!prevailing)
505*e4b17023SJohn Marino     return;
506*e4b17023SJohn Marino 
507*e4b17023SJohn Marino found:
508*e4b17023SJohn Marino   /* If current lto files represent the whole program,
509*e4b17023SJohn Marino     it is correct to use LDPR_PREVALING_DEF_IRONLY.
510*e4b17023SJohn Marino     If current lto files are part of whole program, internal
511*e4b17023SJohn Marino     resolver doesn't know if it is LDPR_PREVAILING_DEF
512*e4b17023SJohn Marino     or LDPR_PREVAILING_DEF_IRONLY.  Use IRONLY conforms to
513*e4b17023SJohn Marino     using -fwhole-program.  Otherwise, it doesn't
514*e4b17023SJohn Marino     matter using either LDPR_PREVAILING_DEF or
515*e4b17023SJohn Marino     LDPR_PREVAILING_DEF_IRONLY
516*e4b17023SJohn Marino 
517*e4b17023SJohn Marino     FIXME: above workaround due to gold plugin makes some
518*e4b17023SJohn Marino     variables IRONLY, which are indeed PREVAILING_DEF in
519*e4b17023SJohn Marino     resolution file.  These variables still need manual
520*e4b17023SJohn Marino     externally_visible attribute.  */
521*e4b17023SJohn Marino     prevailing->resolution = LDPR_PREVAILING_DEF_IRONLY;
522*e4b17023SJohn Marino     prevailing->guessed = true;
523*e4b17023SJohn Marino }
524*e4b17023SJohn Marino 
525*e4b17023SJohn Marino /* Merge all decls in the symbol table chain to the prevailing decl and
526*e4b17023SJohn Marino    issue diagnostics about type mismatches.  If DIAGNOSED_P is true
527*e4b17023SJohn Marino    do not issue further diagnostics.*/
528*e4b17023SJohn Marino 
529*e4b17023SJohn Marino static void
lto_symtab_merge_decls_2(void ** slot,bool diagnosed_p)530*e4b17023SJohn Marino lto_symtab_merge_decls_2 (void **slot, bool diagnosed_p)
531*e4b17023SJohn Marino {
532*e4b17023SJohn Marino   lto_symtab_entry_t prevailing, e;
533*e4b17023SJohn Marino   VEC(tree, heap) *mismatches = NULL;
534*e4b17023SJohn Marino   unsigned i;
535*e4b17023SJohn Marino   tree decl;
536*e4b17023SJohn Marino 
537*e4b17023SJohn Marino   /* Nothing to do for a single entry.  */
538*e4b17023SJohn Marino   prevailing = (lto_symtab_entry_t) *slot;
539*e4b17023SJohn Marino   if (!prevailing->next)
540*e4b17023SJohn Marino     return;
541*e4b17023SJohn Marino 
542*e4b17023SJohn Marino   /* Try to merge each entry with the prevailing one.  */
543*e4b17023SJohn Marino   for (e = prevailing->next; e; e = e->next)
544*e4b17023SJohn Marino     {
545*e4b17023SJohn Marino       if (!lto_symtab_merge (prevailing, e)
546*e4b17023SJohn Marino 	  && !diagnosed_p)
547*e4b17023SJohn Marino 	VEC_safe_push (tree, heap, mismatches, e->decl);
548*e4b17023SJohn Marino     }
549*e4b17023SJohn Marino   if (VEC_empty (tree, mismatches))
550*e4b17023SJohn Marino     return;
551*e4b17023SJohn Marino 
552*e4b17023SJohn Marino   /* Diagnose all mismatched re-declarations.  */
553*e4b17023SJohn Marino   FOR_EACH_VEC_ELT (tree, mismatches, i, decl)
554*e4b17023SJohn Marino     {
555*e4b17023SJohn Marino       if (!types_compatible_p (TREE_TYPE (prevailing->decl), TREE_TYPE (decl)))
556*e4b17023SJohn Marino 	diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
557*e4b17023SJohn Marino 				   "type of %qD does not match original "
558*e4b17023SJohn Marino 				   "declaration", decl);
559*e4b17023SJohn Marino 
560*e4b17023SJohn Marino       else if ((DECL_USER_ALIGN (prevailing->decl) && DECL_USER_ALIGN (decl))
561*e4b17023SJohn Marino 	       && DECL_ALIGN (prevailing->decl) < DECL_ALIGN (decl))
562*e4b17023SJohn Marino 	{
563*e4b17023SJohn Marino 	  diagnosed_p |= warning_at (DECL_SOURCE_LOCATION (decl), 0,
564*e4b17023SJohn Marino 				     "alignment of %qD is bigger than "
565*e4b17023SJohn Marino 				     "original declaration", decl);
566*e4b17023SJohn Marino 	}
567*e4b17023SJohn Marino     }
568*e4b17023SJohn Marino   if (diagnosed_p)
569*e4b17023SJohn Marino     inform (DECL_SOURCE_LOCATION (prevailing->decl),
570*e4b17023SJohn Marino 	    "previously declared here");
571*e4b17023SJohn Marino 
572*e4b17023SJohn Marino   VEC_free (tree, heap, mismatches);
573*e4b17023SJohn Marino }
574*e4b17023SJohn Marino 
575*e4b17023SJohn Marino /* Helper to process the decl chain for the symbol table entry *SLOT.  */
576*e4b17023SJohn Marino 
577*e4b17023SJohn Marino static int
lto_symtab_merge_decls_1(void ** slot,void * data ATTRIBUTE_UNUSED)578*e4b17023SJohn Marino lto_symtab_merge_decls_1 (void **slot, void *data ATTRIBUTE_UNUSED)
579*e4b17023SJohn Marino {
580*e4b17023SJohn Marino   lto_symtab_entry_t e, prevailing;
581*e4b17023SJohn Marino   bool diagnosed_p = false;
582*e4b17023SJohn Marino 
583*e4b17023SJohn Marino   /* Compute the symbol resolutions.  This is a no-op when using the
584*e4b17023SJohn Marino      linker plugin.  */
585*e4b17023SJohn Marino   lto_symtab_resolve_symbols (slot);
586*e4b17023SJohn Marino 
587*e4b17023SJohn Marino   /* Find the prevailing decl.  */
588*e4b17023SJohn Marino   for (prevailing = (lto_symtab_entry_t) *slot;
589*e4b17023SJohn Marino        prevailing
590*e4b17023SJohn Marino        && prevailing->resolution != LDPR_PREVAILING_DEF_IRONLY
591*e4b17023SJohn Marino        && prevailing->resolution != LDPR_PREVAILING_DEF_IRONLY_EXP
592*e4b17023SJohn Marino        && prevailing->resolution != LDPR_PREVAILING_DEF;
593*e4b17023SJohn Marino        prevailing = prevailing->next)
594*e4b17023SJohn Marino     ;
595*e4b17023SJohn Marino 
596*e4b17023SJohn Marino   /* Assert it's the only one.  */
597*e4b17023SJohn Marino   if (prevailing)
598*e4b17023SJohn Marino     for (e = prevailing->next; e; e = e->next)
599*e4b17023SJohn Marino       {
600*e4b17023SJohn Marino 	if (e->resolution == LDPR_PREVAILING_DEF_IRONLY
601*e4b17023SJohn Marino 	    || e->resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
602*e4b17023SJohn Marino 	    || e->resolution == LDPR_PREVAILING_DEF)
603*e4b17023SJohn Marino 	  fatal_error ("multiple prevailing defs for %qE",
604*e4b17023SJohn Marino 		       DECL_NAME (prevailing->decl));
605*e4b17023SJohn Marino       }
606*e4b17023SJohn Marino 
607*e4b17023SJohn Marino   /* If there's not a prevailing symbol yet it's an external reference.
608*e4b17023SJohn Marino      Happens a lot during ltrans.  Choose the first symbol with a
609*e4b17023SJohn Marino      cgraph or a varpool node.  */
610*e4b17023SJohn Marino   if (!prevailing)
611*e4b17023SJohn Marino     {
612*e4b17023SJohn Marino       prevailing = (lto_symtab_entry_t) *slot;
613*e4b17023SJohn Marino       /* For functions choose one with a cgraph node.  */
614*e4b17023SJohn Marino       if (TREE_CODE (prevailing->decl) == FUNCTION_DECL)
615*e4b17023SJohn Marino 	while (!prevailing->node
616*e4b17023SJohn Marino 	       && prevailing->next)
617*e4b17023SJohn Marino 	  prevailing = prevailing->next;
618*e4b17023SJohn Marino       /* For variables chose with a priority variant with vnode
619*e4b17023SJohn Marino 	 attached (i.e. from unit where external declaration of
620*e4b17023SJohn Marino 	 variable is actually used).
621*e4b17023SJohn Marino 	 When there are multiple variants, chose one with size.
622*e4b17023SJohn Marino 	 This is needed for C++ typeinfos, for example in
623*e4b17023SJohn Marino 	 lto/20081204-1 there are typeifos in both units, just
624*e4b17023SJohn Marino 	 one of them do have size.  */
625*e4b17023SJohn Marino       if (TREE_CODE (prevailing->decl) == VAR_DECL)
626*e4b17023SJohn Marino 	{
627*e4b17023SJohn Marino 	  for (e = prevailing->next; e; e = e->next)
628*e4b17023SJohn Marino 	    if ((!prevailing->vnode && e->vnode)
629*e4b17023SJohn Marino 		|| ((prevailing->vnode != NULL) == (e->vnode != NULL)
630*e4b17023SJohn Marino 		    && !COMPLETE_TYPE_P (TREE_TYPE (prevailing->decl))
631*e4b17023SJohn Marino 		    && COMPLETE_TYPE_P (TREE_TYPE (e->decl))))
632*e4b17023SJohn Marino 	      prevailing = e;
633*e4b17023SJohn Marino 	}
634*e4b17023SJohn Marino     }
635*e4b17023SJohn Marino 
636*e4b17023SJohn Marino   /* Move it first in the list.  */
637*e4b17023SJohn Marino   if ((lto_symtab_entry_t) *slot != prevailing)
638*e4b17023SJohn Marino     {
639*e4b17023SJohn Marino       for (e = (lto_symtab_entry_t) *slot; e->next != prevailing; e = e->next)
640*e4b17023SJohn Marino 	;
641*e4b17023SJohn Marino       e->next = prevailing->next;
642*e4b17023SJohn Marino       prevailing->next = (lto_symtab_entry_t) *slot;
643*e4b17023SJohn Marino       *slot = (void *) prevailing;
644*e4b17023SJohn Marino     }
645*e4b17023SJohn Marino 
646*e4b17023SJohn Marino   /* Record the prevailing variable.  */
647*e4b17023SJohn Marino   if (TREE_CODE (prevailing->decl) == VAR_DECL)
648*e4b17023SJohn Marino     VEC_safe_push (tree, gc, lto_global_var_decls, prevailing->decl);
649*e4b17023SJohn Marino 
650*e4b17023SJohn Marino   /* Diagnose mismatched objects.  */
651*e4b17023SJohn Marino   for (e = prevailing->next; e; e = e->next)
652*e4b17023SJohn Marino     {
653*e4b17023SJohn Marino       if (TREE_CODE (prevailing->decl) == TREE_CODE (e->decl))
654*e4b17023SJohn Marino 	continue;
655*e4b17023SJohn Marino 
656*e4b17023SJohn Marino       switch (TREE_CODE (prevailing->decl))
657*e4b17023SJohn Marino 	{
658*e4b17023SJohn Marino 	case VAR_DECL:
659*e4b17023SJohn Marino 	  gcc_assert (TREE_CODE (e->decl) == FUNCTION_DECL);
660*e4b17023SJohn Marino 	  error_at (DECL_SOURCE_LOCATION (e->decl),
661*e4b17023SJohn Marino 		    "variable %qD redeclared as function", prevailing->decl);
662*e4b17023SJohn Marino 	  break;
663*e4b17023SJohn Marino 
664*e4b17023SJohn Marino 	case FUNCTION_DECL:
665*e4b17023SJohn Marino 	  gcc_assert (TREE_CODE (e->decl) == VAR_DECL);
666*e4b17023SJohn Marino 	  error_at (DECL_SOURCE_LOCATION (e->decl),
667*e4b17023SJohn Marino 		    "function %qD redeclared as variable", prevailing->decl);
668*e4b17023SJohn Marino 	  break;
669*e4b17023SJohn Marino 
670*e4b17023SJohn Marino 	default:
671*e4b17023SJohn Marino 	  gcc_unreachable ();
672*e4b17023SJohn Marino 	}
673*e4b17023SJohn Marino 
674*e4b17023SJohn Marino       diagnosed_p = true;
675*e4b17023SJohn Marino     }
676*e4b17023SJohn Marino   if (diagnosed_p)
677*e4b17023SJohn Marino       inform (DECL_SOURCE_LOCATION (prevailing->decl),
678*e4b17023SJohn Marino 	      "previously declared here");
679*e4b17023SJohn Marino 
680*e4b17023SJohn Marino   /* Merge the chain to the single prevailing decl and diagnose
681*e4b17023SJohn Marino      mismatches.  */
682*e4b17023SJohn Marino   lto_symtab_merge_decls_2 (slot, diagnosed_p);
683*e4b17023SJohn Marino 
684*e4b17023SJohn Marino   /* Store resolution decision into the callgraph.
685*e4b17023SJohn Marino      In LTRANS don't overwrite information we stored into callgraph at
686*e4b17023SJohn Marino      WPA stage.
687*e4b17023SJohn Marino 
688*e4b17023SJohn Marino      Do not bother to store guessed decisions.  Generic code knows how
689*e4b17023SJohn Marino      to handle UNKNOWN relocation well.
690*e4b17023SJohn Marino 
691*e4b17023SJohn Marino      The problem with storing guessed decision is whether to use
692*e4b17023SJohn Marino      PREVAILING_DEF, PREVAILING_DEF_IRONLY, PREVAILING_DEF_IRONLY_EXP.
693*e4b17023SJohn Marino      First one would disable some whole program optimizations, while
694*e4b17023SJohn Marino      ther second would imply to many whole program assumptions.  */
695*e4b17023SJohn Marino   if (prevailing->node && !flag_ltrans && !prevailing->guessed)
696*e4b17023SJohn Marino     prevailing->node->resolution = prevailing->resolution;
697*e4b17023SJohn Marino   else if (prevailing->vnode && !flag_ltrans && !prevailing->guessed)
698*e4b17023SJohn Marino     prevailing->vnode->resolution = prevailing->resolution;
699*e4b17023SJohn Marino   return 1;
700*e4b17023SJohn Marino }
701*e4b17023SJohn Marino 
702*e4b17023SJohn Marino /* Resolve and merge all symbol table chains to a prevailing decl.  */
703*e4b17023SJohn Marino 
704*e4b17023SJohn Marino void
lto_symtab_merge_decls(void)705*e4b17023SJohn Marino lto_symtab_merge_decls (void)
706*e4b17023SJohn Marino {
707*e4b17023SJohn Marino   lto_symtab_maybe_init_hash_table ();
708*e4b17023SJohn Marino   htab_traverse (lto_symtab_identifiers, lto_symtab_merge_decls_1, NULL);
709*e4b17023SJohn Marino }
710*e4b17023SJohn Marino 
711*e4b17023SJohn Marino /* Helper to process the decl chain for the symbol table entry *SLOT.  */
712*e4b17023SJohn Marino 
713*e4b17023SJohn Marino static int
lto_symtab_merge_cgraph_nodes_1(void ** slot,void * data ATTRIBUTE_UNUSED)714*e4b17023SJohn Marino lto_symtab_merge_cgraph_nodes_1 (void **slot, void *data ATTRIBUTE_UNUSED)
715*e4b17023SJohn Marino {
716*e4b17023SJohn Marino   lto_symtab_entry_t e, prevailing = (lto_symtab_entry_t) *slot;
717*e4b17023SJohn Marino 
718*e4b17023SJohn Marino   if (!prevailing->next)
719*e4b17023SJohn Marino     return 1;
720*e4b17023SJohn Marino 
721*e4b17023SJohn Marino   /* Replace the cgraph node of each entry with the prevailing one.  */
722*e4b17023SJohn Marino   for (e = prevailing->next; e; e = e->next)
723*e4b17023SJohn Marino     {
724*e4b17023SJohn Marino       if (e->node != NULL)
725*e4b17023SJohn Marino 	{
726*e4b17023SJohn Marino 	  /* In case we prevail funcion by an alias, we can run into case
727*e4b17023SJohn Marino 	     that the alias has no cgraph node attached, since it was
728*e4b17023SJohn Marino 	     previously unused.  Create the node.  */
729*e4b17023SJohn Marino 	  if (!prevailing->node)
730*e4b17023SJohn Marino 	    {
731*e4b17023SJohn Marino 	      prevailing->node = cgraph_create_node (prevailing->decl);
732*e4b17023SJohn Marino 	      prevailing->node->alias = true;
733*e4b17023SJohn Marino 	    }
734*e4b17023SJohn Marino 	  lto_cgraph_replace_node (e->node, prevailing->node);
735*e4b17023SJohn Marino 	}
736*e4b17023SJohn Marino       if (e->vnode != NULL)
737*e4b17023SJohn Marino 	{
738*e4b17023SJohn Marino 	  if (!prevailing->vnode)
739*e4b17023SJohn Marino 	    {
740*e4b17023SJohn Marino 	      prevailing->vnode = varpool_node (prevailing->decl);
741*e4b17023SJohn Marino 	      prevailing->vnode->alias = true;
742*e4b17023SJohn Marino 	    }
743*e4b17023SJohn Marino 	  lto_varpool_replace_node (e->vnode, prevailing->vnode);
744*e4b17023SJohn Marino 	}
745*e4b17023SJohn Marino     }
746*e4b17023SJohn Marino 
747*e4b17023SJohn Marino   /* Drop all but the prevailing decl from the symtab.  */
748*e4b17023SJohn Marino   prevailing->next = NULL;
749*e4b17023SJohn Marino 
750*e4b17023SJohn Marino   return 1;
751*e4b17023SJohn Marino }
752*e4b17023SJohn Marino 
753*e4b17023SJohn Marino /* Merge cgraph nodes according to the symbol merging done by
754*e4b17023SJohn Marino    lto_symtab_merge_decls.  */
755*e4b17023SJohn Marino 
756*e4b17023SJohn Marino void
lto_symtab_merge_cgraph_nodes(void)757*e4b17023SJohn Marino lto_symtab_merge_cgraph_nodes (void)
758*e4b17023SJohn Marino {
759*e4b17023SJohn Marino   struct cgraph_node *node;
760*e4b17023SJohn Marino   struct varpool_node *vnode;
761*e4b17023SJohn Marino   lto_symtab_maybe_init_hash_table ();
762*e4b17023SJohn Marino   htab_traverse (lto_symtab_identifiers, lto_symtab_merge_cgraph_nodes_1, NULL);
763*e4b17023SJohn Marino 
764*e4b17023SJohn Marino   for (node = cgraph_nodes; node; node = node->next)
765*e4b17023SJohn Marino     if ((node->thunk.thunk_p || node->alias)
766*e4b17023SJohn Marino 	&& node->thunk.alias)
767*e4b17023SJohn Marino       node->thunk.alias = lto_symtab_prevailing_decl (node->thunk.alias);
768*e4b17023SJohn Marino   for (vnode = varpool_nodes; vnode; vnode = vnode->next)
769*e4b17023SJohn Marino     if (vnode->alias_of)
770*e4b17023SJohn Marino       vnode->alias_of = lto_symtab_prevailing_decl (vnode->alias_of);
771*e4b17023SJohn Marino }
772*e4b17023SJohn Marino 
773*e4b17023SJohn Marino /* Given the decl DECL, return the prevailing decl with the same name. */
774*e4b17023SJohn Marino 
775*e4b17023SJohn Marino tree
lto_symtab_prevailing_decl(tree decl)776*e4b17023SJohn Marino lto_symtab_prevailing_decl (tree decl)
777*e4b17023SJohn Marino {
778*e4b17023SJohn Marino   lto_symtab_entry_t ret;
779*e4b17023SJohn Marino 
780*e4b17023SJohn Marino   /* Builtins and local symbols are their own prevailing decl.  */
781*e4b17023SJohn Marino   if (!TREE_PUBLIC (decl) || is_builtin_fn (decl))
782*e4b17023SJohn Marino     return decl;
783*e4b17023SJohn Marino 
784*e4b17023SJohn Marino   /* DECL_ABSTRACTs are their own prevailng decl.  */
785*e4b17023SJohn Marino   if (TREE_CODE (decl) == FUNCTION_DECL && DECL_ABSTRACT (decl))
786*e4b17023SJohn Marino     return decl;
787*e4b17023SJohn Marino 
788*e4b17023SJohn Marino   /* Ensure DECL_ASSEMBLER_NAME will not set assembler name.  */
789*e4b17023SJohn Marino   gcc_assert (DECL_ASSEMBLER_NAME_SET_P (decl));
790*e4b17023SJohn Marino 
791*e4b17023SJohn Marino   /* Walk through the list of candidates and return the one we merged to.  */
792*e4b17023SJohn Marino   ret = lto_symtab_get ((*targetm.asm_out.mangle_assembler_name)
793*e4b17023SJohn Marino 			(IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
794*e4b17023SJohn Marino   if (!ret)
795*e4b17023SJohn Marino     return NULL_TREE;
796*e4b17023SJohn Marino 
797*e4b17023SJohn Marino   return ret->decl;
798*e4b17023SJohn Marino }
799*e4b17023SJohn Marino 
800*e4b17023SJohn Marino #include "gt-lto-symtab.h"
801