symtab.c (insert_to_assembler_name_hash): Do not insert register vars.
[gcc.git] / gcc / lto / lto.c
1 /* Top-level LTO routines.
2 Copyright 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
3 Contributed by CodeSourcery, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "opts.h"
25 #include "toplev.h"
26 #include "tree.h"
27 #include "tree-flow.h"
28 #include "diagnostic-core.h"
29 #include "tm.h"
30 #include "cgraph.h"
31 #include "ggc.h"
32 #include "tree-ssa-operands.h"
33 #include "tree-pass.h"
34 #include "langhooks.h"
35 #include "vec.h"
36 #include "bitmap.h"
37 #include "pointer-set.h"
38 #include "ipa-prop.h"
39 #include "common.h"
40 #include "debug.h"
41 #include "gimple.h"
42 #include "lto.h"
43 #include "lto-tree.h"
44 #include "lto-streamer.h"
45 #include "tree-streamer.h"
46 #include "splay-tree.h"
47 #include "lto-partition.h"
48
49 static GTY(()) tree first_personality_decl;
50
51 /* Returns a hash code for P. */
52
53 static hashval_t
54 hash_name (const void *p)
55 {
56 const struct lto_section_slot *ds = (const struct lto_section_slot *) p;
57 return (hashval_t) htab_hash_string (ds->name);
58 }
59
60
61 /* Returns nonzero if P1 and P2 are equal. */
62
63 static int
64 eq_name (const void *p1, const void *p2)
65 {
66 const struct lto_section_slot *s1 =
67 (const struct lto_section_slot *) p1;
68 const struct lto_section_slot *s2 =
69 (const struct lto_section_slot *) p2;
70
71 return strcmp (s1->name, s2->name) == 0;
72 }
73
74 /* Free lto_section_slot */
75
76 static void
77 free_with_string (void *arg)
78 {
79 struct lto_section_slot *s = (struct lto_section_slot *)arg;
80
81 free (CONST_CAST (char *, s->name));
82 free (arg);
83 }
84
85 /* Create section hash table */
86
87 htab_t
88 lto_obj_create_section_hash_table (void)
89 {
90 return htab_create (37, hash_name, eq_name, free_with_string);
91 }
92
93 /* Delete an allocated integer KEY in the splay tree. */
94
95 static void
96 lto_splay_tree_delete_id (splay_tree_key key)
97 {
98 free ((void *) key);
99 }
100
101 /* Compare splay tree node ids A and B. */
102
103 static int
104 lto_splay_tree_compare_ids (splay_tree_key a, splay_tree_key b)
105 {
106 unsigned HOST_WIDE_INT ai;
107 unsigned HOST_WIDE_INT bi;
108
109 ai = *(unsigned HOST_WIDE_INT *) a;
110 bi = *(unsigned HOST_WIDE_INT *) b;
111
112 if (ai < bi)
113 return -1;
114 else if (ai > bi)
115 return 1;
116 return 0;
117 }
118
119 /* Look up splay tree node by ID in splay tree T. */
120
121 static splay_tree_node
122 lto_splay_tree_lookup (splay_tree t, unsigned HOST_WIDE_INT id)
123 {
124 return splay_tree_lookup (t, (splay_tree_key) &id);
125 }
126
127 /* Check if KEY has ID. */
128
129 static bool
130 lto_splay_tree_id_equal_p (splay_tree_key key, unsigned HOST_WIDE_INT id)
131 {
132 return *(unsigned HOST_WIDE_INT *) key == id;
133 }
134
135 /* Insert a splay tree node into tree T with ID as key and FILE_DATA as value.
136 The ID is allocated separately because we need HOST_WIDE_INTs which may
137 be wider than a splay_tree_key. */
138
139 static void
140 lto_splay_tree_insert (splay_tree t, unsigned HOST_WIDE_INT id,
141 struct lto_file_decl_data *file_data)
142 {
143 unsigned HOST_WIDE_INT *idp = XCNEW (unsigned HOST_WIDE_INT);
144 *idp = id;
145 splay_tree_insert (t, (splay_tree_key) idp, (splay_tree_value) file_data);
146 }
147
148 /* Create a splay tree. */
149
150 static splay_tree
151 lto_splay_tree_new (void)
152 {
153 return splay_tree_new (lto_splay_tree_compare_ids,
154 lto_splay_tree_delete_id,
155 NULL);
156 }
157
158 /* Return true when NODE has a clone that is analyzed (i.e. we need
159 to load its body even if the node itself is not needed). */
160
161 static bool
162 has_analyzed_clone_p (struct cgraph_node *node)
163 {
164 struct cgraph_node *orig = node;
165 node = node->clones;
166 if (node)
167 while (node != orig)
168 {
169 if (node->analyzed)
170 return true;
171 if (node->clones)
172 node = node->clones;
173 else if (node->next_sibling_clone)
174 node = node->next_sibling_clone;
175 else
176 {
177 while (node != orig && !node->next_sibling_clone)
178 node = node->clone_of;
179 if (node != orig)
180 node = node->next_sibling_clone;
181 }
182 }
183 return false;
184 }
185
186 /* Read the function body for the function associated with NODE. */
187
188 static void
189 lto_materialize_function (struct cgraph_node *node)
190 {
191 tree decl;
192 struct lto_file_decl_data *file_data;
193 const char *data, *name;
194 size_t len;
195
196 decl = node->symbol.decl;
197 /* Read in functions with body (analyzed nodes)
198 and also functions that are needed to produce virtual clones. */
199 if (cgraph_function_with_gimple_body_p (node) || has_analyzed_clone_p (node))
200 {
201 /* Clones don't need to be read. */
202 if (node->clone_of)
203 return;
204
205 /* Load the function body only if not operating in WPA mode. In
206 WPA mode, the body of the function is not needed. */
207 if (!flag_wpa)
208 {
209 file_data = node->symbol.lto_file_data;
210 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
211
212 /* We may have renamed the declaration, e.g., a static function. */
213 name = lto_get_decl_name_mapping (file_data, name);
214
215 data = lto_get_section_data (file_data, LTO_section_function_body,
216 name, &len);
217 if (!data)
218 fatal_error ("%s: section %s is missing",
219 file_data->file_name,
220 name);
221
222 gcc_assert (DECL_STRUCT_FUNCTION (decl) == NULL);
223
224 allocate_struct_function (decl, false);
225 announce_function (decl);
226 lto_input_function_body (file_data, decl, data);
227 if (DECL_FUNCTION_PERSONALITY (decl) && !first_personality_decl)
228 first_personality_decl = DECL_FUNCTION_PERSONALITY (decl);
229 lto_stats.num_function_bodies++;
230 lto_free_section_data (file_data, LTO_section_function_body, name,
231 data, len);
232 ggc_collect ();
233 }
234 }
235
236 /* Let the middle end know about the function. */
237 rest_of_decl_compilation (decl, 1, 0);
238 }
239
240
241 /* Decode the content of memory pointed to by DATA in the in decl
242 state object STATE. DATA_IN points to a data_in structure for
243 decoding. Return the address after the decoded object in the
244 input. */
245
246 static const uint32_t *
247 lto_read_in_decl_state (struct data_in *data_in, const uint32_t *data,
248 struct lto_in_decl_state *state)
249 {
250 uint32_t ix;
251 tree decl;
252 uint32_t i, j;
253
254 ix = *data++;
255 decl = streamer_tree_cache_get (data_in->reader_cache, ix);
256 if (TREE_CODE (decl) != FUNCTION_DECL)
257 {
258 gcc_assert (decl == void_type_node);
259 decl = NULL_TREE;
260 }
261 state->fn_decl = decl;
262
263 for (i = 0; i < LTO_N_DECL_STREAMS; i++)
264 {
265 uint32_t size = *data++;
266 tree *decls = ggc_alloc_vec_tree (size);
267
268 for (j = 0; j < size; j++)
269 decls[j] = streamer_tree_cache_get (data_in->reader_cache, data[j]);
270
271 state->streams[i].size = size;
272 state->streams[i].trees = decls;
273 data += size;
274 }
275
276 return data;
277 }
278
279
280
281 /* Global type table. FIXME, it should be possible to re-use some
282 of the type hashing routines in tree.c (type_hash_canon, type_hash_lookup,
283 etc), but those assume that types were built with the various
284 build_*_type routines which is not the case with the streamer. */
285 static GTY((if_marked ("ggc_marked_p"), param_is (union tree_node)))
286 htab_t gimple_types;
287 static GTY((if_marked ("tree_int_map_marked_p"), param_is (struct tree_int_map)))
288 htab_t type_hash_cache;
289
290 static hashval_t gimple_type_hash (const void *);
291
292 /* Structure used to maintain a cache of some type pairs compared by
293 gimple_types_compatible_p when comparing aggregate types. There are
294 three possible values for SAME_P:
295
296 -2: The pair (T1, T2) has just been inserted in the table.
297 0: T1 and T2 are different types.
298 1: T1 and T2 are the same type. */
299
300 struct type_pair_d
301 {
302 unsigned int uid1;
303 unsigned int uid2;
304 signed char same_p;
305 };
306 typedef struct type_pair_d *type_pair_t;
307 DEF_VEC_P(type_pair_t);
308 DEF_VEC_ALLOC_P(type_pair_t,heap);
309
310 #define GIMPLE_TYPE_PAIR_SIZE 16381
311 struct type_pair_d *type_pair_cache;
312
313
314 /* Lookup the pair of types T1 and T2 in *VISITED_P. Insert a new
315 entry if none existed. */
316
317 static inline type_pair_t
318 lookup_type_pair (tree t1, tree t2)
319 {
320 unsigned int index;
321 unsigned int uid1, uid2;
322
323 if (TYPE_UID (t1) < TYPE_UID (t2))
324 {
325 uid1 = TYPE_UID (t1);
326 uid2 = TYPE_UID (t2);
327 }
328 else
329 {
330 uid1 = TYPE_UID (t2);
331 uid2 = TYPE_UID (t1);
332 }
333 gcc_checking_assert (uid1 != uid2);
334
335 /* iterative_hash_hashval_t imply an function calls.
336 We know that UIDS are in limited range. */
337 index = ((((unsigned HOST_WIDE_INT)uid1 << HOST_BITS_PER_WIDE_INT / 2) + uid2)
338 % GIMPLE_TYPE_PAIR_SIZE);
339 if (type_pair_cache [index].uid1 == uid1
340 && type_pair_cache [index].uid2 == uid2)
341 return &type_pair_cache[index];
342
343 type_pair_cache [index].uid1 = uid1;
344 type_pair_cache [index].uid2 = uid2;
345 type_pair_cache [index].same_p = -2;
346
347 return &type_pair_cache[index];
348 }
349
350 /* Per pointer state for the SCC finding. The on_sccstack flag
351 is not strictly required, it is true when there is no hash value
352 recorded for the type and false otherwise. But querying that
353 is slower. */
354
355 struct sccs
356 {
357 unsigned int dfsnum;
358 unsigned int low;
359 bool on_sccstack;
360 union {
361 hashval_t hash;
362 signed char same_p;
363 } u;
364 };
365
366 static unsigned int next_dfs_num;
367 static unsigned int gtc_next_dfs_num;
368
369 /* GIMPLE type merging cache. A direct-mapped cache based on TYPE_UID. */
370
371 typedef struct GTY(()) gimple_type_leader_entry_s {
372 tree type;
373 tree leader;
374 } gimple_type_leader_entry;
375
376 #define GIMPLE_TYPE_LEADER_SIZE 16381
377 static GTY((length("GIMPLE_TYPE_LEADER_SIZE")))
378 gimple_type_leader_entry *gimple_type_leader;
379
380 /* Lookup an existing leader for T and return it or NULL_TREE, if
381 there is none in the cache. */
382
383 static inline tree
384 gimple_lookup_type_leader (tree t)
385 {
386 gimple_type_leader_entry *leader;
387
388 leader = &gimple_type_leader[TYPE_UID (t) % GIMPLE_TYPE_LEADER_SIZE];
389 if (leader->type != t)
390 return NULL_TREE;
391
392 return leader->leader;
393 }
394
395
396 /* Return true if T1 and T2 have the same name. If FOR_COMPLETION_P is
397 true then if any type has no name return false, otherwise return
398 true if both types have no names. */
399
400 static bool
401 compare_type_names_p (tree t1, tree t2)
402 {
403 tree name1 = TYPE_NAME (t1);
404 tree name2 = TYPE_NAME (t2);
405
406 if ((name1 != NULL_TREE) != (name2 != NULL_TREE))
407 return false;
408
409 if (name1 == NULL_TREE)
410 return true;
411
412 /* Either both should be a TYPE_DECL or both an IDENTIFIER_NODE. */
413 if (TREE_CODE (name1) != TREE_CODE (name2))
414 return false;
415
416 if (TREE_CODE (name1) == TYPE_DECL)
417 name1 = DECL_NAME (name1);
418 gcc_checking_assert (!name1 || TREE_CODE (name1) == IDENTIFIER_NODE);
419
420 if (TREE_CODE (name2) == TYPE_DECL)
421 name2 = DECL_NAME (name2);
422 gcc_checking_assert (!name2 || TREE_CODE (name2) == IDENTIFIER_NODE);
423
424 /* Identifiers can be compared with pointer equality rather
425 than a string comparison. */
426 if (name1 == name2)
427 return true;
428
429 return false;
430 }
431
432 static bool
433 gimple_types_compatible_p_1 (tree, tree, type_pair_t,
434 VEC(type_pair_t, heap) **,
435 struct pointer_map_t *, struct obstack *);
436
437 /* DFS visit the edge from the callers type pair with state *STATE to
438 the pair T1, T2 while operating in FOR_MERGING_P mode.
439 Update the merging status if it is not part of the SCC containing the
440 callers pair and return it.
441 SCCSTACK, SCCSTATE and SCCSTATE_OBSTACK are state for the DFS walk done. */
442
443 static bool
444 gtc_visit (tree t1, tree t2,
445 struct sccs *state,
446 VEC(type_pair_t, heap) **sccstack,
447 struct pointer_map_t *sccstate,
448 struct obstack *sccstate_obstack)
449 {
450 struct sccs *cstate = NULL;
451 type_pair_t p;
452 void **slot;
453 tree leader1, leader2;
454
455 /* Check first for the obvious case of pointer identity. */
456 if (t1 == t2)
457 return true;
458
459 /* Check that we have two types to compare. */
460 if (t1 == NULL_TREE || t2 == NULL_TREE)
461 return false;
462
463 /* Can't be the same type if the types don't have the same code. */
464 if (TREE_CODE (t1) != TREE_CODE (t2))
465 return false;
466
467 /* Can't be the same type if they have different CV qualifiers. */
468 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
469 return false;
470
471 if (TREE_ADDRESSABLE (t1) != TREE_ADDRESSABLE (t2))
472 return false;
473
474 /* Void types and nullptr types are always the same. */
475 if (TREE_CODE (t1) == VOID_TYPE
476 || TREE_CODE (t1) == NULLPTR_TYPE)
477 return true;
478
479 /* Can't be the same type if they have different alignment or mode. */
480 if (TYPE_ALIGN (t1) != TYPE_ALIGN (t2)
481 || TYPE_MODE (t1) != TYPE_MODE (t2))
482 return false;
483
484 /* Do some simple checks before doing three hashtable queries. */
485 if (INTEGRAL_TYPE_P (t1)
486 || SCALAR_FLOAT_TYPE_P (t1)
487 || FIXED_POINT_TYPE_P (t1)
488 || TREE_CODE (t1) == VECTOR_TYPE
489 || TREE_CODE (t1) == COMPLEX_TYPE
490 || TREE_CODE (t1) == OFFSET_TYPE
491 || POINTER_TYPE_P (t1))
492 {
493 /* Can't be the same type if they have different sign or precision. */
494 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2)
495 || TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
496 return false;
497
498 if (TREE_CODE (t1) == INTEGER_TYPE
499 && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
500 return false;
501
502 /* That's all we need to check for float and fixed-point types. */
503 if (SCALAR_FLOAT_TYPE_P (t1)
504 || FIXED_POINT_TYPE_P (t1))
505 return true;
506
507 /* For other types fall through to more complex checks. */
508 }
509
510 /* If the types have been previously registered and found equal
511 they still are. */
512 leader1 = gimple_lookup_type_leader (t1);
513 leader2 = gimple_lookup_type_leader (t2);
514 if (leader1 == t2
515 || t1 == leader2
516 || (leader1 && leader1 == leader2))
517 return true;
518
519 /* If the hash values of t1 and t2 are different the types can't
520 possibly be the same. This helps keeping the type-pair hashtable
521 small, only tracking comparisons for hash collisions. */
522 if (gimple_type_hash (t1) != gimple_type_hash (t2))
523 return false;
524
525 /* Allocate a new cache entry for this comparison. */
526 p = lookup_type_pair (t1, t2);
527 if (p->same_p == 0 || p->same_p == 1)
528 {
529 /* We have already decided whether T1 and T2 are the
530 same, return the cached result. */
531 return p->same_p == 1;
532 }
533
534 if ((slot = pointer_map_contains (sccstate, p)) != NULL)
535 cstate = (struct sccs *)*slot;
536 /* Not yet visited. DFS recurse. */
537 if (!cstate)
538 {
539 gimple_types_compatible_p_1 (t1, t2, p,
540 sccstack, sccstate, sccstate_obstack);
541 cstate = (struct sccs *)* pointer_map_contains (sccstate, p);
542 state->low = MIN (state->low, cstate->low);
543 }
544 /* If the type is still on the SCC stack adjust the parents low. */
545 if (cstate->dfsnum < state->dfsnum
546 && cstate->on_sccstack)
547 state->low = MIN (cstate->dfsnum, state->low);
548
549 /* Return the current lattice value. We start with an equality
550 assumption so types part of a SCC will be optimistically
551 treated equal unless proven otherwise. */
552 return cstate->u.same_p;
553 }
554
555 /* Worker for gimple_types_compatible.
556 SCCSTACK, SCCSTATE and SCCSTATE_OBSTACK are state for the DFS walk done. */
557
558 static bool
559 gimple_types_compatible_p_1 (tree t1, tree t2, type_pair_t p,
560 VEC(type_pair_t, heap) **sccstack,
561 struct pointer_map_t *sccstate,
562 struct obstack *sccstate_obstack)
563 {
564 struct sccs *state;
565
566 gcc_assert (p->same_p == -2);
567
568 state = XOBNEW (sccstate_obstack, struct sccs);
569 *pointer_map_insert (sccstate, p) = state;
570
571 VEC_safe_push (type_pair_t, heap, *sccstack, p);
572 state->dfsnum = gtc_next_dfs_num++;
573 state->low = state->dfsnum;
574 state->on_sccstack = true;
575 /* Start with an equality assumption. As we DFS recurse into child
576 SCCs this assumption may get revisited. */
577 state->u.same_p = 1;
578
579 /* The struct tags shall compare equal. */
580 if (!compare_type_names_p (t1, t2))
581 goto different_types;
582
583 /* We may not merge typedef types to the same type in different
584 contexts. */
585 if (TYPE_NAME (t1)
586 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
587 && DECL_CONTEXT (TYPE_NAME (t1))
588 && TYPE_P (DECL_CONTEXT (TYPE_NAME (t1))))
589 {
590 if (!gtc_visit (DECL_CONTEXT (TYPE_NAME (t1)),
591 DECL_CONTEXT (TYPE_NAME (t2)),
592 state, sccstack, sccstate, sccstate_obstack))
593 goto different_types;
594 }
595
596 /* If their attributes are not the same they can't be the same type. */
597 if (!attribute_list_equal (TYPE_ATTRIBUTES (t1), TYPE_ATTRIBUTES (t2)))
598 goto different_types;
599
600 /* Do type-specific comparisons. */
601 switch (TREE_CODE (t1))
602 {
603 case VECTOR_TYPE:
604 case COMPLEX_TYPE:
605 if (!gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2),
606 state, sccstack, sccstate, sccstate_obstack))
607 goto different_types;
608 goto same_types;
609
610 case ARRAY_TYPE:
611 /* Array types are the same if the element types are the same and
612 the number of elements are the same. */
613 if (!gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2),
614 state, sccstack, sccstate, sccstate_obstack)
615 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)
616 || TYPE_NONALIASED_COMPONENT (t1) != TYPE_NONALIASED_COMPONENT (t2))
617 goto different_types;
618 else
619 {
620 tree i1 = TYPE_DOMAIN (t1);
621 tree i2 = TYPE_DOMAIN (t2);
622
623 /* For an incomplete external array, the type domain can be
624 NULL_TREE. Check this condition also. */
625 if (i1 == NULL_TREE && i2 == NULL_TREE)
626 goto same_types;
627 else if (i1 == NULL_TREE || i2 == NULL_TREE)
628 goto different_types;
629 else
630 {
631 tree min1 = TYPE_MIN_VALUE (i1);
632 tree min2 = TYPE_MIN_VALUE (i2);
633 tree max1 = TYPE_MAX_VALUE (i1);
634 tree max2 = TYPE_MAX_VALUE (i2);
635
636 /* The minimum/maximum values have to be the same. */
637 if ((min1 == min2
638 || (min1 && min2
639 && ((TREE_CODE (min1) == PLACEHOLDER_EXPR
640 && TREE_CODE (min2) == PLACEHOLDER_EXPR)
641 || operand_equal_p (min1, min2, 0))))
642 && (max1 == max2
643 || (max1 && max2
644 && ((TREE_CODE (max1) == PLACEHOLDER_EXPR
645 && TREE_CODE (max2) == PLACEHOLDER_EXPR)
646 || operand_equal_p (max1, max2, 0)))))
647 goto same_types;
648 else
649 goto different_types;
650 }
651 }
652
653 case METHOD_TYPE:
654 /* Method types should belong to the same class. */
655 if (!gtc_visit (TYPE_METHOD_BASETYPE (t1), TYPE_METHOD_BASETYPE (t2),
656 state, sccstack, sccstate, sccstate_obstack))
657 goto different_types;
658
659 /* Fallthru */
660
661 case FUNCTION_TYPE:
662 /* Function types are the same if the return type and arguments types
663 are the same. */
664 if (!gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2),
665 state, sccstack, sccstate, sccstate_obstack))
666 goto different_types;
667
668 if (!comp_type_attributes (t1, t2))
669 goto different_types;
670
671 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
672 goto same_types;
673 else
674 {
675 tree parms1, parms2;
676
677 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
678 parms1 && parms2;
679 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
680 {
681 if (!gtc_visit (TREE_VALUE (parms1), TREE_VALUE (parms2),
682 state, sccstack, sccstate, sccstate_obstack))
683 goto different_types;
684 }
685
686 if (parms1 || parms2)
687 goto different_types;
688
689 goto same_types;
690 }
691
692 case OFFSET_TYPE:
693 {
694 if (!gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2),
695 state, sccstack, sccstate, sccstate_obstack)
696 || !gtc_visit (TYPE_OFFSET_BASETYPE (t1),
697 TYPE_OFFSET_BASETYPE (t2),
698 state, sccstack, sccstate, sccstate_obstack))
699 goto different_types;
700
701 goto same_types;
702 }
703
704 case POINTER_TYPE:
705 case REFERENCE_TYPE:
706 {
707 /* If the two pointers have different ref-all attributes,
708 they can't be the same type. */
709 if (TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
710 goto different_types;
711
712 /* Otherwise, pointer and reference types are the same if the
713 pointed-to types are the same. */
714 if (gtc_visit (TREE_TYPE (t1), TREE_TYPE (t2),
715 state, sccstack, sccstate, sccstate_obstack))
716 goto same_types;
717
718 goto different_types;
719 }
720
721 case INTEGER_TYPE:
722 case BOOLEAN_TYPE:
723 {
724 tree min1 = TYPE_MIN_VALUE (t1);
725 tree max1 = TYPE_MAX_VALUE (t1);
726 tree min2 = TYPE_MIN_VALUE (t2);
727 tree max2 = TYPE_MAX_VALUE (t2);
728 bool min_equal_p = false;
729 bool max_equal_p = false;
730
731 /* If either type has a minimum value, the other type must
732 have the same. */
733 if (min1 == NULL_TREE && min2 == NULL_TREE)
734 min_equal_p = true;
735 else if (min1 && min2 && operand_equal_p (min1, min2, 0))
736 min_equal_p = true;
737
738 /* Likewise, if either type has a maximum value, the other
739 type must have the same. */
740 if (max1 == NULL_TREE && max2 == NULL_TREE)
741 max_equal_p = true;
742 else if (max1 && max2 && operand_equal_p (max1, max2, 0))
743 max_equal_p = true;
744
745 if (!min_equal_p || !max_equal_p)
746 goto different_types;
747
748 goto same_types;
749 }
750
751 case ENUMERAL_TYPE:
752 {
753 /* FIXME lto, we cannot check bounds on enumeral types because
754 different front ends will produce different values.
755 In C, enumeral types are integers, while in C++ each element
756 will have its own symbolic value. We should decide how enums
757 are to be represented in GIMPLE and have each front end lower
758 to that. */
759 tree v1, v2;
760
761 /* For enumeral types, all the values must be the same. */
762 if (TYPE_VALUES (t1) == TYPE_VALUES (t2))
763 goto same_types;
764
765 for (v1 = TYPE_VALUES (t1), v2 = TYPE_VALUES (t2);
766 v1 && v2;
767 v1 = TREE_CHAIN (v1), v2 = TREE_CHAIN (v2))
768 {
769 tree c1 = TREE_VALUE (v1);
770 tree c2 = TREE_VALUE (v2);
771
772 if (TREE_CODE (c1) == CONST_DECL)
773 c1 = DECL_INITIAL (c1);
774
775 if (TREE_CODE (c2) == CONST_DECL)
776 c2 = DECL_INITIAL (c2);
777
778 if (tree_int_cst_equal (c1, c2) != 1)
779 goto different_types;
780
781 if (TREE_PURPOSE (v1) != TREE_PURPOSE (v2))
782 goto different_types;
783 }
784
785 /* If one enumeration has more values than the other, they
786 are not the same. */
787 if (v1 || v2)
788 goto different_types;
789
790 goto same_types;
791 }
792
793 case RECORD_TYPE:
794 case UNION_TYPE:
795 case QUAL_UNION_TYPE:
796 {
797 tree f1, f2;
798
799 /* For aggregate types, all the fields must be the same. */
800 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
801 f1 && f2;
802 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
803 {
804 /* Different field kinds are not compatible. */
805 if (TREE_CODE (f1) != TREE_CODE (f2))
806 goto different_types;
807 /* Field decls must have the same name and offset. */
808 if (TREE_CODE (f1) == FIELD_DECL
809 && (DECL_NONADDRESSABLE_P (f1) != DECL_NONADDRESSABLE_P (f2)
810 || !gimple_compare_field_offset (f1, f2)))
811 goto different_types;
812 /* All entities should have the same name and type. */
813 if (DECL_NAME (f1) != DECL_NAME (f2)
814 || !gtc_visit (TREE_TYPE (f1), TREE_TYPE (f2),
815 state, sccstack, sccstate, sccstate_obstack))
816 goto different_types;
817 }
818
819 /* If one aggregate has more fields than the other, they
820 are not the same. */
821 if (f1 || f2)
822 goto different_types;
823
824 goto same_types;
825 }
826
827 default:
828 gcc_unreachable ();
829 }
830
831 /* Common exit path for types that are not compatible. */
832 different_types:
833 state->u.same_p = 0;
834 goto pop;
835
836 /* Common exit path for types that are compatible. */
837 same_types:
838 gcc_assert (state->u.same_p == 1);
839
840 pop:
841 if (state->low == state->dfsnum)
842 {
843 type_pair_t x;
844
845 /* Pop off the SCC and set its cache values to the final
846 comparison result. */
847 do
848 {
849 struct sccs *cstate;
850 x = VEC_pop (type_pair_t, *sccstack);
851 cstate = (struct sccs *)*pointer_map_contains (sccstate, x);
852 cstate->on_sccstack = false;
853 x->same_p = state->u.same_p;
854 }
855 while (x != p);
856 }
857
858 return state->u.same_p;
859 }
860
861 /* Return true iff T1 and T2 are structurally identical. When
862 FOR_MERGING_P is true the an incomplete type and a complete type
863 are considered different, otherwise they are considered compatible. */
864
865 static bool
866 gimple_types_compatible_p (tree t1, tree t2)
867 {
868 VEC(type_pair_t, heap) *sccstack = NULL;
869 struct pointer_map_t *sccstate;
870 struct obstack sccstate_obstack;
871 type_pair_t p = NULL;
872 bool res;
873 tree leader1, leader2;
874
875 /* Before starting to set up the SCC machinery handle simple cases. */
876
877 /* Check first for the obvious case of pointer identity. */
878 if (t1 == t2)
879 return true;
880
881 /* Check that we have two types to compare. */
882 if (t1 == NULL_TREE || t2 == NULL_TREE)
883 return false;
884
885 /* Can't be the same type if the types don't have the same code. */
886 if (TREE_CODE (t1) != TREE_CODE (t2))
887 return false;
888
889 /* Can't be the same type if they have different CV qualifiers. */
890 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
891 return false;
892
893 if (TREE_ADDRESSABLE (t1) != TREE_ADDRESSABLE (t2))
894 return false;
895
896 /* Void types and nullptr types are always the same. */
897 if (TREE_CODE (t1) == VOID_TYPE
898 || TREE_CODE (t1) == NULLPTR_TYPE)
899 return true;
900
901 /* Can't be the same type if they have different alignment or mode. */
902 if (TYPE_ALIGN (t1) != TYPE_ALIGN (t2)
903 || TYPE_MODE (t1) != TYPE_MODE (t2))
904 return false;
905
906 /* Do some simple checks before doing three hashtable queries. */
907 if (INTEGRAL_TYPE_P (t1)
908 || SCALAR_FLOAT_TYPE_P (t1)
909 || FIXED_POINT_TYPE_P (t1)
910 || TREE_CODE (t1) == VECTOR_TYPE
911 || TREE_CODE (t1) == COMPLEX_TYPE
912 || TREE_CODE (t1) == OFFSET_TYPE
913 || POINTER_TYPE_P (t1))
914 {
915 /* Can't be the same type if they have different sign or precision. */
916 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2)
917 || TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
918 return false;
919
920 if (TREE_CODE (t1) == INTEGER_TYPE
921 && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
922 return false;
923
924 /* That's all we need to check for float and fixed-point types. */
925 if (SCALAR_FLOAT_TYPE_P (t1)
926 || FIXED_POINT_TYPE_P (t1))
927 return true;
928
929 /* For other types fall through to more complex checks. */
930 }
931
932 /* If the types have been previously registered and found equal
933 they still are. */
934 leader1 = gimple_lookup_type_leader (t1);
935 leader2 = gimple_lookup_type_leader (t2);
936 if (leader1 == t2
937 || t1 == leader2
938 || (leader1 && leader1 == leader2))
939 return true;
940
941 /* If the hash values of t1 and t2 are different the types can't
942 possibly be the same. This helps keeping the type-pair hashtable
943 small, only tracking comparisons for hash collisions. */
944 if (gimple_type_hash (t1) != gimple_type_hash (t2))
945 return false;
946
947 /* If we've visited this type pair before (in the case of aggregates
948 with self-referential types), and we made a decision, return it. */
949 p = lookup_type_pair (t1, t2);
950 if (p->same_p == 0 || p->same_p == 1)
951 {
952 /* We have already decided whether T1 and T2 are the
953 same, return the cached result. */
954 return p->same_p == 1;
955 }
956
957 /* Now set up the SCC machinery for the comparison. */
958 gtc_next_dfs_num = 1;
959 sccstate = pointer_map_create ();
960 gcc_obstack_init (&sccstate_obstack);
961 res = gimple_types_compatible_p_1 (t1, t2, p,
962 &sccstack, sccstate, &sccstate_obstack);
963 VEC_free (type_pair_t, heap, sccstack);
964 pointer_map_destroy (sccstate);
965 obstack_free (&sccstate_obstack, NULL);
966
967 return res;
968 }
969
970 static hashval_t
971 iterative_hash_gimple_type (tree, hashval_t, VEC(tree, heap) **,
972 struct pointer_map_t *, struct obstack *);
973
974 /* DFS visit the edge from the callers type with state *STATE to T.
975 Update the callers type hash V with the hash for T if it is not part
976 of the SCC containing the callers type and return it.
977 SCCSTACK, SCCSTATE and SCCSTATE_OBSTACK are state for the DFS walk done. */
978
979 static hashval_t
980 visit (tree t, struct sccs *state, hashval_t v,
981 VEC (tree, heap) **sccstack,
982 struct pointer_map_t *sccstate,
983 struct obstack *sccstate_obstack)
984 {
985 struct sccs *cstate = NULL;
986 struct tree_int_map m;
987 void **slot;
988
989 /* If there is a hash value recorded for this type then it can't
990 possibly be part of our parent SCC. Simply mix in its hash. */
991 m.base.from = t;
992 if ((slot = htab_find_slot (type_hash_cache, &m, NO_INSERT))
993 && *slot)
994 return iterative_hash_hashval_t (((struct tree_int_map *) *slot)->to, v);
995
996 if ((slot = pointer_map_contains (sccstate, t)) != NULL)
997 cstate = (struct sccs *)*slot;
998 if (!cstate)
999 {
1000 hashval_t tem;
1001 /* Not yet visited. DFS recurse. */
1002 tem = iterative_hash_gimple_type (t, v,
1003 sccstack, sccstate, sccstate_obstack);
1004 if (!cstate)
1005 cstate = (struct sccs *)* pointer_map_contains (sccstate, t);
1006 state->low = MIN (state->low, cstate->low);
1007 /* If the type is no longer on the SCC stack and thus is not part
1008 of the parents SCC mix in its hash value. Otherwise we will
1009 ignore the type for hashing purposes and return the unaltered
1010 hash value. */
1011 if (!cstate->on_sccstack)
1012 return tem;
1013 }
1014 if (cstate->dfsnum < state->dfsnum
1015 && cstate->on_sccstack)
1016 state->low = MIN (cstate->dfsnum, state->low);
1017
1018 /* We are part of our parents SCC, skip this type during hashing
1019 and return the unaltered hash value. */
1020 return v;
1021 }
1022
1023 /* Hash NAME with the previous hash value V and return it. */
1024
1025 static hashval_t
1026 iterative_hash_name (tree name, hashval_t v)
1027 {
1028 if (!name)
1029 return v;
1030 v = iterative_hash_hashval_t (TREE_CODE (name), v);
1031 if (TREE_CODE (name) == TYPE_DECL)
1032 name = DECL_NAME (name);
1033 if (!name)
1034 return v;
1035 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
1036 return iterative_hash_object (IDENTIFIER_HASH_VALUE (name), v);
1037 }
1038
1039 /* A type, hashvalue pair for sorting SCC members. */
1040
1041 struct type_hash_pair {
1042 tree type;
1043 hashval_t hash;
1044 };
1045
1046 /* Compare two type, hashvalue pairs. */
1047
1048 static int
1049 type_hash_pair_compare (const void *p1_, const void *p2_)
1050 {
1051 const struct type_hash_pair *p1 = (const struct type_hash_pair *) p1_;
1052 const struct type_hash_pair *p2 = (const struct type_hash_pair *) p2_;
1053 if (p1->hash < p2->hash)
1054 return -1;
1055 else if (p1->hash > p2->hash)
1056 return 1;
1057 return 0;
1058 }
1059
1060 /* Returning a hash value for gimple type TYPE combined with VAL.
1061 SCCSTACK, SCCSTATE and SCCSTATE_OBSTACK are state for the DFS walk done.
1062
1063 To hash a type we end up hashing in types that are reachable.
1064 Through pointers we can end up with cycles which messes up the
1065 required property that we need to compute the same hash value
1066 for structurally equivalent types. To avoid this we have to
1067 hash all types in a cycle (the SCC) in a commutative way. The
1068 easiest way is to not mix in the hashes of the SCC members at
1069 all. To make this work we have to delay setting the hash
1070 values of the SCC until it is complete. */
1071
1072 static hashval_t
1073 iterative_hash_gimple_type (tree type, hashval_t val,
1074 VEC(tree, heap) **sccstack,
1075 struct pointer_map_t *sccstate,
1076 struct obstack *sccstate_obstack)
1077 {
1078 hashval_t v;
1079 void **slot;
1080 struct sccs *state;
1081
1082 /* Not visited during this DFS walk. */
1083 gcc_checking_assert (!pointer_map_contains (sccstate, type));
1084 state = XOBNEW (sccstate_obstack, struct sccs);
1085 *pointer_map_insert (sccstate, type) = state;
1086
1087 VEC_safe_push (tree, heap, *sccstack, type);
1088 state->dfsnum = next_dfs_num++;
1089 state->low = state->dfsnum;
1090 state->on_sccstack = true;
1091
1092 /* Combine a few common features of types so that types are grouped into
1093 smaller sets; when searching for existing matching types to merge,
1094 only existing types having the same features as the new type will be
1095 checked. */
1096 v = iterative_hash_name (TYPE_NAME (type), 0);
1097 if (TYPE_NAME (type)
1098 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1099 && DECL_CONTEXT (TYPE_NAME (type))
1100 && TYPE_P (DECL_CONTEXT (TYPE_NAME (type))))
1101 v = visit (DECL_CONTEXT (TYPE_NAME (type)), state, v,
1102 sccstack, sccstate, sccstate_obstack);
1103 v = iterative_hash_hashval_t (TREE_CODE (type), v);
1104 v = iterative_hash_hashval_t (TYPE_QUALS (type), v);
1105 v = iterative_hash_hashval_t (TREE_ADDRESSABLE (type), v);
1106
1107 /* Do not hash the types size as this will cause differences in
1108 hash values for the complete vs. the incomplete type variant. */
1109
1110 /* Incorporate common features of numerical types. */
1111 if (INTEGRAL_TYPE_P (type)
1112 || SCALAR_FLOAT_TYPE_P (type)
1113 || FIXED_POINT_TYPE_P (type))
1114 {
1115 v = iterative_hash_hashval_t (TYPE_PRECISION (type), v);
1116 v = iterative_hash_hashval_t (TYPE_MODE (type), v);
1117 v = iterative_hash_hashval_t (TYPE_UNSIGNED (type), v);
1118 }
1119
1120 /* For pointer and reference types, fold in information about the type
1121 pointed to. */
1122 if (POINTER_TYPE_P (type))
1123 v = visit (TREE_TYPE (type), state, v,
1124 sccstack, sccstate, sccstate_obstack);
1125
1126 /* For integer types hash the types min/max values and the string flag. */
1127 if (TREE_CODE (type) == INTEGER_TYPE)
1128 {
1129 /* OMP lowering can introduce error_mark_node in place of
1130 random local decls in types. */
1131 if (TYPE_MIN_VALUE (type) != error_mark_node)
1132 v = iterative_hash_expr (TYPE_MIN_VALUE (type), v);
1133 if (TYPE_MAX_VALUE (type) != error_mark_node)
1134 v = iterative_hash_expr (TYPE_MAX_VALUE (type), v);
1135 v = iterative_hash_hashval_t (TYPE_STRING_FLAG (type), v);
1136 }
1137
1138 /* For array types hash the domain and the string flag. */
1139 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
1140 {
1141 v = iterative_hash_hashval_t (TYPE_STRING_FLAG (type), v);
1142 v = visit (TYPE_DOMAIN (type), state, v,
1143 sccstack, sccstate, sccstate_obstack);
1144 }
1145
1146 /* Recurse for aggregates with a single element type. */
1147 if (TREE_CODE (type) == ARRAY_TYPE
1148 || TREE_CODE (type) == COMPLEX_TYPE
1149 || TREE_CODE (type) == VECTOR_TYPE)
1150 v = visit (TREE_TYPE (type), state, v,
1151 sccstack, sccstate, sccstate_obstack);
1152
1153 /* Incorporate function return and argument types. */
1154 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
1155 {
1156 unsigned na;
1157 tree p;
1158
1159 /* For method types also incorporate their parent class. */
1160 if (TREE_CODE (type) == METHOD_TYPE)
1161 v = visit (TYPE_METHOD_BASETYPE (type), state, v,
1162 sccstack, sccstate, sccstate_obstack);
1163
1164 /* Check result and argument types. */
1165 v = visit (TREE_TYPE (type), state, v,
1166 sccstack, sccstate, sccstate_obstack);
1167 for (p = TYPE_ARG_TYPES (type), na = 0; p; p = TREE_CHAIN (p))
1168 {
1169 v = visit (TREE_VALUE (p), state, v,
1170 sccstack, sccstate, sccstate_obstack);
1171 na++;
1172 }
1173
1174 v = iterative_hash_hashval_t (na, v);
1175 }
1176
1177 if (RECORD_OR_UNION_TYPE_P (type))
1178 {
1179 unsigned nf;
1180 tree f;
1181
1182 for (f = TYPE_FIELDS (type), nf = 0; f; f = TREE_CHAIN (f))
1183 {
1184 v = iterative_hash_name (DECL_NAME (f), v);
1185 v = visit (TREE_TYPE (f), state, v,
1186 sccstack, sccstate, sccstate_obstack);
1187 nf++;
1188 }
1189
1190 v = iterative_hash_hashval_t (nf, v);
1191 }
1192
1193 /* Record hash for us. */
1194 state->u.hash = v;
1195
1196 /* See if we found an SCC. */
1197 if (state->low == state->dfsnum)
1198 {
1199 tree x;
1200 struct tree_int_map *m;
1201
1202 /* Pop off the SCC and set its hash values. */
1203 x = VEC_pop (tree, *sccstack);
1204 /* Optimize SCC size one. */
1205 if (x == type)
1206 {
1207 state->on_sccstack = false;
1208 m = ggc_alloc_cleared_tree_int_map ();
1209 m->base.from = x;
1210 m->to = v;
1211 slot = htab_find_slot (type_hash_cache, m, INSERT);
1212 gcc_assert (!*slot);
1213 *slot = (void *) m;
1214 }
1215 else
1216 {
1217 struct sccs *cstate;
1218 unsigned first, i, size, j;
1219 struct type_hash_pair *pairs;
1220 /* Pop off the SCC and build an array of type, hash pairs. */
1221 first = VEC_length (tree, *sccstack) - 1;
1222 while (VEC_index (tree, *sccstack, first) != type)
1223 --first;
1224 size = VEC_length (tree, *sccstack) - first + 1;
1225 pairs = XALLOCAVEC (struct type_hash_pair, size);
1226 i = 0;
1227 cstate = (struct sccs *)*pointer_map_contains (sccstate, x);
1228 cstate->on_sccstack = false;
1229 pairs[i].type = x;
1230 pairs[i].hash = cstate->u.hash;
1231 do
1232 {
1233 x = VEC_pop (tree, *sccstack);
1234 cstate = (struct sccs *)*pointer_map_contains (sccstate, x);
1235 cstate->on_sccstack = false;
1236 ++i;
1237 pairs[i].type = x;
1238 pairs[i].hash = cstate->u.hash;
1239 }
1240 while (x != type);
1241 gcc_assert (i + 1 == size);
1242 /* Sort the arrays of type, hash pairs so that when we mix in
1243 all members of the SCC the hash value becomes independent on
1244 the order we visited the SCC. Disregard hashes equal to
1245 the hash of the type we mix into because we cannot guarantee
1246 a stable sort for those across different TUs. */
1247 qsort (pairs, size, sizeof (struct type_hash_pair),
1248 type_hash_pair_compare);
1249 for (i = 0; i < size; ++i)
1250 {
1251 hashval_t hash;
1252 m = ggc_alloc_cleared_tree_int_map ();
1253 m->base.from = pairs[i].type;
1254 hash = pairs[i].hash;
1255 /* Skip same hashes. */
1256 for (j = i + 1; j < size && pairs[j].hash == pairs[i].hash; ++j)
1257 ;
1258 for (; j < size; ++j)
1259 hash = iterative_hash_hashval_t (pairs[j].hash, hash);
1260 for (j = 0; pairs[j].hash != pairs[i].hash; ++j)
1261 hash = iterative_hash_hashval_t (pairs[j].hash, hash);
1262 m->to = hash;
1263 if (pairs[i].type == type)
1264 v = hash;
1265 slot = htab_find_slot (type_hash_cache, m, INSERT);
1266 gcc_assert (!*slot);
1267 *slot = (void *) m;
1268 }
1269 }
1270 }
1271
1272 return iterative_hash_hashval_t (v, val);
1273 }
1274
1275 /* Returns a hash value for P (assumed to be a type). The hash value
1276 is computed using some distinguishing features of the type. Note
1277 that we cannot use pointer hashing here as we may be dealing with
1278 two distinct instances of the same type.
1279
1280 This function should produce the same hash value for two compatible
1281 types according to gimple_types_compatible_p. */
1282
1283 static hashval_t
1284 gimple_type_hash (const void *p)
1285 {
1286 const_tree t = (const_tree) p;
1287 VEC(tree, heap) *sccstack = NULL;
1288 struct pointer_map_t *sccstate;
1289 struct obstack sccstate_obstack;
1290 hashval_t val;
1291 void **slot;
1292 struct tree_int_map m;
1293
1294 m.base.from = CONST_CAST_TREE (t);
1295 if ((slot = htab_find_slot (type_hash_cache, &m, NO_INSERT))
1296 && *slot)
1297 return iterative_hash_hashval_t (((struct tree_int_map *) *slot)->to, 0);
1298
1299 /* Perform a DFS walk and pre-hash all reachable types. */
1300 next_dfs_num = 1;
1301 sccstate = pointer_map_create ();
1302 gcc_obstack_init (&sccstate_obstack);
1303 val = iterative_hash_gimple_type (CONST_CAST_TREE (t), 0,
1304 &sccstack, sccstate, &sccstate_obstack);
1305 VEC_free (tree, heap, sccstack);
1306 pointer_map_destroy (sccstate);
1307 obstack_free (&sccstate_obstack, NULL);
1308
1309 return val;
1310 }
1311
1312 /* Returns nonzero if P1 and P2 are equal. */
1313
1314 static int
1315 gimple_type_eq (const void *p1, const void *p2)
1316 {
1317 const_tree t1 = (const_tree) p1;
1318 const_tree t2 = (const_tree) p2;
1319 return gimple_types_compatible_p (CONST_CAST_TREE (t1),
1320 CONST_CAST_TREE (t2));
1321 }
1322
1323
1324 /* Worker for gimple_register_type.
1325 Register type T in the global type table gimple_types.
1326 When REGISTERING_MV is false first recurse for the main variant of T. */
1327
1328 static tree
1329 gimple_register_type_1 (tree t, bool registering_mv)
1330 {
1331 void **slot;
1332 gimple_type_leader_entry *leader;
1333
1334 /* If we registered this type before return the cached result. */
1335 leader = &gimple_type_leader[TYPE_UID (t) % GIMPLE_TYPE_LEADER_SIZE];
1336 if (leader->type == t)
1337 return leader->leader;
1338
1339 /* Always register the main variant first. This is important so we
1340 pick up the non-typedef variants as canonical, otherwise we'll end
1341 up taking typedef ids for structure tags during comparison.
1342 It also makes sure that main variants will be merged to main variants.
1343 As we are operating on a possibly partially fixed up type graph
1344 do not bother to recurse more than once, otherwise we may end up
1345 walking in circles.
1346 If we are registering a main variant it will either remain its
1347 own main variant or it will be merged to something else in which
1348 case we do not care for the main variant leader. */
1349 if (!registering_mv
1350 && TYPE_MAIN_VARIANT (t) != t)
1351 gimple_register_type_1 (TYPE_MAIN_VARIANT (t), true);
1352
1353 /* See if we already have an equivalent type registered. */
1354 slot = htab_find_slot (gimple_types, t, INSERT);
1355 if (*slot
1356 && *(tree *)slot != t)
1357 {
1358 tree new_type = (tree) *((tree *) slot);
1359 leader->type = t;
1360 leader->leader = new_type;
1361 return new_type;
1362 }
1363
1364 /* If not, insert it to the cache and the hash. */
1365 leader->type = t;
1366 leader->leader = t;
1367 *slot = (void *) t;
1368 return t;
1369 }
1370
1371 /* Register type T in the global type table gimple_types.
1372 If another type T', compatible with T, already existed in
1373 gimple_types then return T', otherwise return T. This is used by
1374 LTO to merge identical types read from different TUs. */
1375
1376 static tree
1377 gimple_register_type (tree t)
1378 {
1379 gcc_assert (TYPE_P (t));
1380 return gimple_register_type_1 (t, false);
1381 }
1382
1383 #define GIMPLE_REGISTER_TYPE(tt) \
1384 (TREE_VISITED (tt) ? gimple_register_type (tt) : tt)
1385
1386
1387
1388 /* A hashtable of trees that potentially refer to variables or functions
1389 that must be replaced with their prevailing variant. */
1390 static GTY((if_marked ("ggc_marked_p"), param_is (union tree_node))) htab_t
1391 tree_with_vars;
1392
1393 /* Remember that T is a tree that (potentially) refers to a variable
1394 or function decl that may be replaced with its prevailing variant. */
1395 static void
1396 remember_with_vars (tree t)
1397 {
1398 *(tree *) htab_find_slot (tree_with_vars, t, INSERT) = t;
1399 }
1400
1401 #define LTO_FIXUP_TREE(tt) \
1402 do \
1403 { \
1404 if (tt) \
1405 { \
1406 if (TYPE_P (tt)) \
1407 (tt) = GIMPLE_REGISTER_TYPE (tt); \
1408 if (VAR_OR_FUNCTION_DECL_P (tt) && TREE_PUBLIC (tt)) \
1409 remember_with_vars (t); \
1410 } \
1411 } while (0)
1412
1413 static void lto_fixup_types (tree);
1414
1415 /* Fix up fields of a tree_typed T. */
1416
1417 static void
1418 lto_ft_typed (tree t)
1419 {
1420 LTO_FIXUP_TREE (TREE_TYPE (t));
1421 }
1422
1423 /* Fix up fields of a tree_common T. */
1424
1425 static void
1426 lto_ft_common (tree t)
1427 {
1428 lto_ft_typed (t);
1429 LTO_FIXUP_TREE (TREE_CHAIN (t));
1430 }
1431
1432 /* Fix up fields of a decl_minimal T. */
1433
1434 static void
1435 lto_ft_decl_minimal (tree t)
1436 {
1437 lto_ft_common (t);
1438 LTO_FIXUP_TREE (DECL_NAME (t));
1439 LTO_FIXUP_TREE (DECL_CONTEXT (t));
1440 }
1441
1442 /* Fix up fields of a decl_common T. */
1443
1444 static void
1445 lto_ft_decl_common (tree t)
1446 {
1447 lto_ft_decl_minimal (t);
1448 LTO_FIXUP_TREE (DECL_SIZE (t));
1449 LTO_FIXUP_TREE (DECL_SIZE_UNIT (t));
1450 LTO_FIXUP_TREE (DECL_INITIAL (t));
1451 LTO_FIXUP_TREE (DECL_ATTRIBUTES (t));
1452 LTO_FIXUP_TREE (DECL_ABSTRACT_ORIGIN (t));
1453 }
1454
1455 /* Fix up fields of a decl_with_vis T. */
1456
1457 static void
1458 lto_ft_decl_with_vis (tree t)
1459 {
1460 lto_ft_decl_common (t);
1461
1462 /* Accessor macro has side-effects, use field-name here. */
1463 LTO_FIXUP_TREE (t->decl_with_vis.assembler_name);
1464 LTO_FIXUP_TREE (DECL_SECTION_NAME (t));
1465 }
1466
1467 /* Fix up fields of a decl_non_common T. */
1468
1469 static void
1470 lto_ft_decl_non_common (tree t)
1471 {
1472 lto_ft_decl_with_vis (t);
1473 LTO_FIXUP_TREE (DECL_ARGUMENT_FLD (t));
1474 LTO_FIXUP_TREE (DECL_RESULT_FLD (t));
1475 LTO_FIXUP_TREE (DECL_VINDEX (t));
1476 /* The C frontends may create exact duplicates for DECL_ORIGINAL_TYPE
1477 like for 'typedef enum foo foo'. We have no way of avoiding to
1478 merge them and dwarf2out.c cannot deal with this,
1479 so fix this up by clearing DECL_ORIGINAL_TYPE in this case. */
1480 if (TREE_CODE (t) == TYPE_DECL
1481 && DECL_ORIGINAL_TYPE (t) == TREE_TYPE (t))
1482 DECL_ORIGINAL_TYPE (t) = NULL_TREE;
1483 }
1484
1485 /* Fix up fields of a decl_non_common T. */
1486
1487 static void
1488 lto_ft_function (tree t)
1489 {
1490 lto_ft_decl_non_common (t);
1491 LTO_FIXUP_TREE (DECL_FUNCTION_PERSONALITY (t));
1492 }
1493
1494 /* Fix up fields of a field_decl T. */
1495
1496 static void
1497 lto_ft_field_decl (tree t)
1498 {
1499 lto_ft_decl_common (t);
1500 LTO_FIXUP_TREE (DECL_FIELD_OFFSET (t));
1501 LTO_FIXUP_TREE (DECL_BIT_FIELD_TYPE (t));
1502 LTO_FIXUP_TREE (DECL_QUALIFIER (t));
1503 LTO_FIXUP_TREE (DECL_FIELD_BIT_OFFSET (t));
1504 LTO_FIXUP_TREE (DECL_FCONTEXT (t));
1505 }
1506
1507 /* Fix up fields of a type T. */
1508
1509 static void
1510 lto_ft_type (tree t)
1511 {
1512 lto_ft_common (t);
1513 LTO_FIXUP_TREE (TYPE_CACHED_VALUES (t));
1514 LTO_FIXUP_TREE (TYPE_SIZE (t));
1515 LTO_FIXUP_TREE (TYPE_SIZE_UNIT (t));
1516 LTO_FIXUP_TREE (TYPE_ATTRIBUTES (t));
1517 LTO_FIXUP_TREE (TYPE_NAME (t));
1518
1519 /* Accessors are for derived node types only. */
1520 if (!POINTER_TYPE_P (t))
1521 LTO_FIXUP_TREE (TYPE_MINVAL (t));
1522 LTO_FIXUP_TREE (TYPE_MAXVAL (t));
1523
1524 /* Accessor is for derived node types only. */
1525 LTO_FIXUP_TREE (t->type_non_common.binfo);
1526
1527 LTO_FIXUP_TREE (TYPE_CONTEXT (t));
1528 }
1529
1530 /* Fix up fields of a BINFO T. */
1531
1532 static void
1533 lto_ft_binfo (tree t)
1534 {
1535 unsigned HOST_WIDE_INT i, n;
1536 tree base, saved_base;
1537
1538 lto_ft_common (t);
1539 LTO_FIXUP_TREE (BINFO_VTABLE (t));
1540 LTO_FIXUP_TREE (BINFO_OFFSET (t));
1541 LTO_FIXUP_TREE (BINFO_VIRTUALS (t));
1542 LTO_FIXUP_TREE (BINFO_VPTR_FIELD (t));
1543 n = VEC_length (tree, BINFO_BASE_ACCESSES (t));
1544 for (i = 0; i < n; i++)
1545 {
1546 saved_base = base = BINFO_BASE_ACCESS (t, i);
1547 LTO_FIXUP_TREE (base);
1548 if (base != saved_base)
1549 VEC_replace (tree, BINFO_BASE_ACCESSES (t), i, base);
1550 }
1551 LTO_FIXUP_TREE (BINFO_INHERITANCE_CHAIN (t));
1552 LTO_FIXUP_TREE (BINFO_SUBVTT_INDEX (t));
1553 LTO_FIXUP_TREE (BINFO_VPTR_INDEX (t));
1554 n = BINFO_N_BASE_BINFOS (t);
1555 for (i = 0; i < n; i++)
1556 {
1557 saved_base = base = BINFO_BASE_BINFO (t, i);
1558 LTO_FIXUP_TREE (base);
1559 if (base != saved_base)
1560 VEC_replace (tree, BINFO_BASE_BINFOS (t), i, base);
1561 }
1562 }
1563
1564 /* Fix up fields of a CONSTRUCTOR T. */
1565
1566 static void
1567 lto_ft_constructor (tree t)
1568 {
1569 unsigned HOST_WIDE_INT idx;
1570 constructor_elt *ce;
1571
1572 lto_ft_typed (t);
1573
1574 for (idx = 0;
1575 VEC_iterate(constructor_elt, CONSTRUCTOR_ELTS (t), idx, ce);
1576 idx++)
1577 {
1578 LTO_FIXUP_TREE (ce->index);
1579 LTO_FIXUP_TREE (ce->value);
1580 }
1581 }
1582
1583 /* Fix up fields of an expression tree T. */
1584
1585 static void
1586 lto_ft_expr (tree t)
1587 {
1588 int i;
1589 lto_ft_typed (t);
1590 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
1591 LTO_FIXUP_TREE (TREE_OPERAND (t, i));
1592 }
1593
1594 /* Given a tree T fixup fields of T by replacing types with their merged
1595 variant and other entities by an equal entity from an earlier compilation
1596 unit, or an entity being canonical in a different way. This includes
1597 for instance integer or string constants. */
1598
1599 static void
1600 lto_fixup_types (tree t)
1601 {
1602 switch (TREE_CODE (t))
1603 {
1604 case IDENTIFIER_NODE:
1605 break;
1606
1607 case TREE_LIST:
1608 LTO_FIXUP_TREE (TREE_VALUE (t));
1609 LTO_FIXUP_TREE (TREE_PURPOSE (t));
1610 LTO_FIXUP_TREE (TREE_CHAIN (t));
1611 break;
1612
1613 case FIELD_DECL:
1614 lto_ft_field_decl (t);
1615 break;
1616
1617 case LABEL_DECL:
1618 case CONST_DECL:
1619 case PARM_DECL:
1620 case RESULT_DECL:
1621 case IMPORTED_DECL:
1622 lto_ft_decl_common (t);
1623 break;
1624
1625 case VAR_DECL:
1626 lto_ft_decl_with_vis (t);
1627 break;
1628
1629 case TYPE_DECL:
1630 lto_ft_decl_non_common (t);
1631 break;
1632
1633 case FUNCTION_DECL:
1634 lto_ft_function (t);
1635 break;
1636
1637 case TREE_BINFO:
1638 lto_ft_binfo (t);
1639 break;
1640
1641 case PLACEHOLDER_EXPR:
1642 lto_ft_common (t);
1643 break;
1644
1645 case BLOCK:
1646 case TRANSLATION_UNIT_DECL:
1647 case OPTIMIZATION_NODE:
1648 case TARGET_OPTION_NODE:
1649 break;
1650
1651 default:
1652 if (TYPE_P (t))
1653 lto_ft_type (t);
1654 else if (TREE_CODE (t) == CONSTRUCTOR)
1655 lto_ft_constructor (t);
1656 else if (CONSTANT_CLASS_P (t))
1657 LTO_FIXUP_TREE (TREE_TYPE (t));
1658 else if (EXPR_P (t))
1659 {
1660 lto_ft_expr (t);
1661 }
1662 else
1663 {
1664 remember_with_vars (t);
1665 }
1666 }
1667 }
1668
1669
1670 /* Return the resolution for the decl with index INDEX from DATA_IN. */
1671
1672 static enum ld_plugin_symbol_resolution
1673 get_resolution (struct data_in *data_in, unsigned index)
1674 {
1675 if (data_in->globals_resolution)
1676 {
1677 ld_plugin_symbol_resolution_t ret;
1678 /* We can have references to not emitted functions in
1679 DECL_FUNCTION_PERSONALITY at least. So we can and have
1680 to indeed return LDPR_UNKNOWN in some cases. */
1681 if (VEC_length (ld_plugin_symbol_resolution_t,
1682 data_in->globals_resolution) <= index)
1683 return LDPR_UNKNOWN;
1684 ret = VEC_index (ld_plugin_symbol_resolution_t,
1685 data_in->globals_resolution,
1686 index);
1687 return ret;
1688 }
1689 else
1690 /* Delay resolution finding until decl merging. */
1691 return LDPR_UNKNOWN;
1692 }
1693
1694
1695 /* Register DECL with the global symbol table and change its
1696 name if necessary to avoid name clashes for static globals across
1697 different files. */
1698
1699 static void
1700 lto_register_var_decl_in_symtab (struct data_in *data_in, tree decl)
1701 {
1702 tree context;
1703
1704 /* Variable has file scope, not local. Need to ensure static variables
1705 between different files don't clash unexpectedly. */
1706 if (!TREE_PUBLIC (decl)
1707 && !((context = decl_function_context (decl))
1708 && auto_var_in_fn_p (decl, context)))
1709 {
1710 /* ??? We normally pre-mangle names before we serialize them
1711 out. Here, in lto1, we do not know the language, and
1712 thus cannot do the mangling again. Instead, we just
1713 append a suffix to the mangled name. The resulting name,
1714 however, is not a properly-formed mangled name, and will
1715 confuse any attempt to unmangle it. */
1716 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1717 char *label;
1718
1719 ASM_FORMAT_PRIVATE_NAME (label, name, DECL_UID (decl));
1720 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (label));
1721 rest_of_decl_compilation (decl, 1, 0);
1722 VEC_safe_push (tree, gc, lto_global_var_decls, decl);
1723 }
1724
1725 /* If this variable has already been declared, queue the
1726 declaration for merging. */
1727 if (TREE_PUBLIC (decl))
1728 {
1729 unsigned ix;
1730 if (!streamer_tree_cache_lookup (data_in->reader_cache, decl, &ix))
1731 gcc_unreachable ();
1732 lto_symtab_register_decl (decl, get_resolution (data_in, ix),
1733 data_in->file_data);
1734 }
1735 }
1736
1737
1738 /* Register DECL with the global symbol table and change its
1739 name if necessary to avoid name clashes for static globals across
1740 different files. DATA_IN contains descriptors and tables for the
1741 file being read. */
1742
1743 static void
1744 lto_register_function_decl_in_symtab (struct data_in *data_in, tree decl)
1745 {
1746 /* Need to ensure static entities between different files
1747 don't clash unexpectedly. */
1748 if (!TREE_PUBLIC (decl))
1749 {
1750 /* We must not use the DECL_ASSEMBLER_NAME macro here, as it
1751 may set the assembler name where it was previously empty. */
1752 tree old_assembler_name = decl->decl_with_vis.assembler_name;
1753
1754 /* FIXME lto: We normally pre-mangle names before we serialize
1755 them out. Here, in lto1, we do not know the language, and
1756 thus cannot do the mangling again. Instead, we just append a
1757 suffix to the mangled name. The resulting name, however, is
1758 not a properly-formed mangled name, and will confuse any
1759 attempt to unmangle it. */
1760 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1761 char *label;
1762
1763 ASM_FORMAT_PRIVATE_NAME (label, name, DECL_UID (decl));
1764 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (label));
1765
1766 /* We may arrive here with the old assembler name not set
1767 if the function body is not needed, e.g., it has been
1768 inlined away and does not appear in the cgraph. */
1769 if (old_assembler_name)
1770 {
1771 tree new_assembler_name = DECL_ASSEMBLER_NAME (decl);
1772
1773 /* Make the original assembler name available for later use.
1774 We may have used it to indicate the section within its
1775 object file where the function body may be found.
1776 FIXME lto: Find a better way to maintain the function decl
1777 to body section mapping so we don't need this hack. */
1778 lto_record_renamed_decl (data_in->file_data,
1779 IDENTIFIER_POINTER (old_assembler_name),
1780 IDENTIFIER_POINTER (new_assembler_name));
1781 }
1782 }
1783
1784 /* If this variable has already been declared, queue the
1785 declaration for merging. */
1786 if (TREE_PUBLIC (decl) && !DECL_ABSTRACT (decl))
1787 {
1788 unsigned ix;
1789 if (!streamer_tree_cache_lookup (data_in->reader_cache, decl, &ix))
1790 gcc_unreachable ();
1791 lto_symtab_register_decl (decl, get_resolution (data_in, ix),
1792 data_in->file_data);
1793 }
1794 }
1795
1796
1797 /* Given a streamer cache structure DATA_IN (holding a sequence of trees
1798 for one compilation unit) go over all trees starting at index FROM until the
1799 end of the sequence and replace fields of those trees, and the trees
1800 themself with their canonical variants as per gimple_register_type. */
1801
1802 static void
1803 uniquify_nodes (struct data_in *data_in, unsigned from)
1804 {
1805 struct streamer_tree_cache_d *cache = data_in->reader_cache;
1806 unsigned len = VEC_length (tree, cache->nodes);
1807 unsigned i;
1808
1809 /* Go backwards because children streamed for the first time come
1810 as part of their parents, and hence are created after them. */
1811
1812 /* First register all the types in the cache. This makes sure to
1813 have the original structure in the type cycles when registering
1814 them and computing hashes. */
1815 for (i = len; i-- > from;)
1816 {
1817 tree t = VEC_index (tree, cache->nodes, i);
1818 if (t && TYPE_P (t))
1819 {
1820 tree newt = gimple_register_type (t);
1821 /* Mark non-prevailing types so we fix them up. No need
1822 to reset that flag afterwards - nothing that refers
1823 to those types is left and they are collected. */
1824 if (newt != t)
1825 TREE_VISITED (t) = 1;
1826 }
1827 }
1828
1829 /* Second fixup all trees in the new cache entries. */
1830 for (i = len; i-- > from;)
1831 {
1832 tree t = VEC_index (tree, cache->nodes, i);
1833 tree oldt = t;
1834 if (!t)
1835 continue;
1836
1837 /* First fixup the fields of T. */
1838 lto_fixup_types (t);
1839
1840 if (!TYPE_P (t))
1841 continue;
1842
1843 /* Now try to find a canonical variant of T itself. */
1844 t = GIMPLE_REGISTER_TYPE (t);
1845
1846 if (t == oldt)
1847 {
1848 /* The following re-creates proper variant lists while fixing up
1849 the variant leaders. We do not stream TYPE_NEXT_VARIANT so the
1850 variant list state before fixup is broken. */
1851 tree tem, mv;
1852
1853 #ifdef ENABLE_CHECKING
1854 /* Remove us from our main variant list if we are not the
1855 variant leader. */
1856 if (TYPE_MAIN_VARIANT (t) != t)
1857 {
1858 tem = TYPE_MAIN_VARIANT (t);
1859 while (tem && TYPE_NEXT_VARIANT (tem) != t)
1860 tem = TYPE_NEXT_VARIANT (tem);
1861 gcc_assert (!tem && !TYPE_NEXT_VARIANT (t));
1862 }
1863 #endif
1864
1865 /* Query our new main variant. */
1866 mv = GIMPLE_REGISTER_TYPE (TYPE_MAIN_VARIANT (t));
1867
1868 /* If we were the variant leader and we get replaced ourselves drop
1869 all variants from our list. */
1870 if (TYPE_MAIN_VARIANT (t) == t
1871 && mv != t)
1872 {
1873 tem = t;
1874 while (tem)
1875 {
1876 tree tem2 = TYPE_NEXT_VARIANT (tem);
1877 TYPE_NEXT_VARIANT (tem) = NULL_TREE;
1878 tem = tem2;
1879 }
1880 }
1881
1882 /* If we are not our own variant leader link us into our new leaders
1883 variant list. */
1884 if (mv != t)
1885 {
1886 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv);
1887 TYPE_NEXT_VARIANT (mv) = t;
1888 if (RECORD_OR_UNION_TYPE_P (t))
1889 TYPE_BINFO (t) = TYPE_BINFO (mv);
1890 /* Preserve the invariant that type variants share their
1891 TYPE_FIELDS. */
1892 if (RECORD_OR_UNION_TYPE_P (t)
1893 && TYPE_FIELDS (mv) != TYPE_FIELDS (t))
1894 {
1895 tree f1, f2;
1896 for (f1 = TYPE_FIELDS (mv), f2 = TYPE_FIELDS (t);
1897 f1 && f2; f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1898 {
1899 unsigned ix;
1900 gcc_assert (f1 != f2
1901 && DECL_NAME (f1) == DECL_NAME (f2));
1902 if (!streamer_tree_cache_lookup (cache, f2, &ix))
1903 gcc_unreachable ();
1904 /* If we're going to replace an element which we'd
1905 still visit in the next iterations, we wouldn't
1906 handle it, so do it here. We do have to handle it
1907 even though the field_decl itself will be removed,
1908 as it could refer to e.g. integer_cst which we
1909 wouldn't reach via any other way, hence they
1910 (and their type) would stay uncollected. */
1911 /* ??? We should rather make sure to replace all
1912 references to f2 with f1. That means handling
1913 COMPONENT_REFs and CONSTRUCTOR elements in
1914 lto_fixup_types and special-case the field-decl
1915 operand handling. */
1916 /* ??? Not sure the above is all relevant in this
1917 path canonicalizing TYPE_FIELDS to that of the
1918 main variant. */
1919 if (ix < i)
1920 lto_fixup_types (f2);
1921 streamer_tree_cache_insert_at (cache, f1, ix);
1922 }
1923 TYPE_FIELDS (t) = TYPE_FIELDS (mv);
1924 }
1925 }
1926
1927 /* Finally adjust our main variant and fix it up. */
1928 TYPE_MAIN_VARIANT (t) = mv;
1929
1930 /* The following reconstructs the pointer chains
1931 of the new pointed-to type if we are a main variant. We do
1932 not stream those so they are broken before fixup. */
1933 if (TREE_CODE (t) == POINTER_TYPE
1934 && TYPE_MAIN_VARIANT (t) == t)
1935 {
1936 TYPE_NEXT_PTR_TO (t) = TYPE_POINTER_TO (TREE_TYPE (t));
1937 TYPE_POINTER_TO (TREE_TYPE (t)) = t;
1938 }
1939 else if (TREE_CODE (t) == REFERENCE_TYPE
1940 && TYPE_MAIN_VARIANT (t) == t)
1941 {
1942 TYPE_NEXT_REF_TO (t) = TYPE_REFERENCE_TO (TREE_TYPE (t));
1943 TYPE_REFERENCE_TO (TREE_TYPE (t)) = t;
1944 }
1945 }
1946
1947 else
1948 {
1949 if (RECORD_OR_UNION_TYPE_P (t))
1950 {
1951 tree f1, f2;
1952 if (TYPE_FIELDS (t) != TYPE_FIELDS (oldt))
1953 for (f1 = TYPE_FIELDS (t), f2 = TYPE_FIELDS (oldt);
1954 f1 && f2; f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1955 {
1956 unsigned ix;
1957 gcc_assert (f1 != f2 && DECL_NAME (f1) == DECL_NAME (f2));
1958 if (!streamer_tree_cache_lookup (cache, f2, &ix))
1959 gcc_unreachable ();
1960 /* If we're going to replace an element which we'd
1961 still visit in the next iterations, we wouldn't
1962 handle it, so do it here. We do have to handle it
1963 even though the field_decl itself will be removed,
1964 as it could refer to e.g. integer_cst which we
1965 wouldn't reach via any other way, hence they
1966 (and their type) would stay uncollected. */
1967 /* ??? We should rather make sure to replace all
1968 references to f2 with f1. That means handling
1969 COMPONENT_REFs and CONSTRUCTOR elements in
1970 lto_fixup_types and special-case the field-decl
1971 operand handling. */
1972 if (ix < i)
1973 lto_fixup_types (f2);
1974 streamer_tree_cache_insert_at (cache, f1, ix);
1975 }
1976 }
1977
1978 /* If we found a tree that is equal to oldt replace it in the
1979 cache, so that further users (in the various LTO sections)
1980 make use of it. */
1981 streamer_tree_cache_insert_at (cache, t, i);
1982 }
1983 }
1984
1985 /* Finally compute the canonical type of all TREE_TYPEs and register
1986 VAR_DECL and FUNCTION_DECL nodes in the symbol table.
1987 From this point there are no longer any types with
1988 TYPE_STRUCTURAL_EQUALITY_P and its type-based alias problems.
1989 This step requires the TYPE_POINTER_TO lists being present, so
1990 make sure it is done last. */
1991 for (i = len; i-- > from;)
1992 {
1993 tree t = VEC_index (tree, cache->nodes, i);
1994 if (t == NULL_TREE)
1995 continue;
1996
1997 if (TREE_CODE (t) == VAR_DECL)
1998 lto_register_var_decl_in_symtab (data_in, t);
1999 else if (TREE_CODE (t) == FUNCTION_DECL && !DECL_BUILT_IN (t))
2000 lto_register_function_decl_in_symtab (data_in, t);
2001 else if (!flag_wpa
2002 && TREE_CODE (t) == TYPE_DECL)
2003 debug_hooks->type_decl (t, !DECL_FILE_SCOPE_P (t));
2004 else if (TYPE_P (t) && !TYPE_CANONICAL (t))
2005 TYPE_CANONICAL (t) = gimple_register_canonical_type (t);
2006 }
2007 }
2008
2009
2010 /* Read all the symbols from buffer DATA, using descriptors in DECL_DATA.
2011 RESOLUTIONS is the set of symbols picked by the linker (read from the
2012 resolution file when the linker plugin is being used). */
2013
2014 static void
2015 lto_read_decls (struct lto_file_decl_data *decl_data, const void *data,
2016 VEC(ld_plugin_symbol_resolution_t,heap) *resolutions)
2017 {
2018 const struct lto_decl_header *header = (const struct lto_decl_header *) data;
2019 const int decl_offset = sizeof (struct lto_decl_header);
2020 const int main_offset = decl_offset + header->decl_state_size;
2021 const int string_offset = main_offset + header->main_size;
2022 struct lto_input_block ib_main;
2023 struct data_in *data_in;
2024 unsigned int i;
2025 const uint32_t *data_ptr, *data_end;
2026 uint32_t num_decl_states;
2027
2028 LTO_INIT_INPUT_BLOCK (ib_main, (const char *) data + main_offset, 0,
2029 header->main_size);
2030
2031 data_in = lto_data_in_create (decl_data, (const char *) data + string_offset,
2032 header->string_size, resolutions);
2033
2034 /* We do not uniquify the pre-loaded cache entries, those are middle-end
2035 internal types that should not be merged. */
2036
2037 /* Read the global declarations and types. */
2038 while (ib_main.p < ib_main.len)
2039 {
2040 tree t;
2041 unsigned from = VEC_length (tree, data_in->reader_cache->nodes);
2042 t = stream_read_tree (&ib_main, data_in);
2043 gcc_assert (t && ib_main.p <= ib_main.len);
2044 uniquify_nodes (data_in, from);
2045 }
2046
2047 /* Read in lto_in_decl_state objects. */
2048 data_ptr = (const uint32_t *) ((const char*) data + decl_offset);
2049 data_end =
2050 (const uint32_t *) ((const char*) data_ptr + header->decl_state_size);
2051 num_decl_states = *data_ptr++;
2052
2053 gcc_assert (num_decl_states > 0);
2054 decl_data->global_decl_state = lto_new_in_decl_state ();
2055 data_ptr = lto_read_in_decl_state (data_in, data_ptr,
2056 decl_data->global_decl_state);
2057
2058 /* Read in per-function decl states and enter them in hash table. */
2059 decl_data->function_decl_states =
2060 htab_create_ggc (37, lto_hash_in_decl_state, lto_eq_in_decl_state, NULL);
2061
2062 for (i = 1; i < num_decl_states; i++)
2063 {
2064 struct lto_in_decl_state *state = lto_new_in_decl_state ();
2065 void **slot;
2066
2067 data_ptr = lto_read_in_decl_state (data_in, data_ptr, state);
2068 slot = htab_find_slot (decl_data->function_decl_states, state, INSERT);
2069 gcc_assert (*slot == NULL);
2070 *slot = state;
2071 }
2072
2073 if (data_ptr != data_end)
2074 internal_error ("bytecode stream: garbage at the end of symbols section");
2075
2076 /* Set the current decl state to be the global state. */
2077 decl_data->current_decl_state = decl_data->global_decl_state;
2078
2079 lto_data_in_delete (data_in);
2080 }
2081
2082 /* Custom version of strtoll, which is not portable. */
2083
2084 static HOST_WIDEST_INT
2085 lto_parse_hex (const char *p)
2086 {
2087 HOST_WIDEST_INT ret = 0;
2088
2089 for (; *p != '\0'; ++p)
2090 {
2091 char c = *p;
2092 unsigned char part;
2093 ret <<= 4;
2094 if (c >= '0' && c <= '9')
2095 part = c - '0';
2096 else if (c >= 'a' && c <= 'f')
2097 part = c - 'a' + 10;
2098 else if (c >= 'A' && c <= 'F')
2099 part = c - 'A' + 10;
2100 else
2101 internal_error ("could not parse hex number");
2102 ret |= part;
2103 }
2104
2105 return ret;
2106 }
2107
2108 /* Read resolution for file named FILE_NAME. The resolution is read from
2109 RESOLUTION. */
2110
2111 static void
2112 lto_resolution_read (splay_tree file_ids, FILE *resolution, lto_file *file)
2113 {
2114 /* We require that objects in the resolution file are in the same
2115 order as the lto1 command line. */
2116 unsigned int name_len;
2117 char *obj_name;
2118 unsigned int num_symbols;
2119 unsigned int i;
2120 struct lto_file_decl_data *file_data;
2121 splay_tree_node nd = NULL;
2122
2123 if (!resolution)
2124 return;
2125
2126 name_len = strlen (file->filename);
2127 obj_name = XNEWVEC (char, name_len + 1);
2128 fscanf (resolution, " "); /* Read white space. */
2129
2130 fread (obj_name, sizeof (char), name_len, resolution);
2131 obj_name[name_len] = '\0';
2132 if (filename_cmp (obj_name, file->filename) != 0)
2133 internal_error ("unexpected file name %s in linker resolution file. "
2134 "Expected %s", obj_name, file->filename);
2135 if (file->offset != 0)
2136 {
2137 int t;
2138 char offset_p[17];
2139 HOST_WIDEST_INT offset;
2140 t = fscanf (resolution, "@0x%16s", offset_p);
2141 if (t != 1)
2142 internal_error ("could not parse file offset");
2143 offset = lto_parse_hex (offset_p);
2144 if (offset != file->offset)
2145 internal_error ("unexpected offset");
2146 }
2147
2148 free (obj_name);
2149
2150 fscanf (resolution, "%u", &num_symbols);
2151
2152 for (i = 0; i < num_symbols; i++)
2153 {
2154 int t;
2155 unsigned index;
2156 unsigned HOST_WIDE_INT id;
2157 char r_str[27];
2158 enum ld_plugin_symbol_resolution r = (enum ld_plugin_symbol_resolution) 0;
2159 unsigned int j;
2160 unsigned int lto_resolution_str_len =
2161 sizeof (lto_resolution_str) / sizeof (char *);
2162 res_pair rp;
2163
2164 t = fscanf (resolution, "%u " HOST_WIDE_INT_PRINT_HEX_PURE " %26s %*[^\n]\n",
2165 &index, &id, r_str);
2166 if (t != 3)
2167 internal_error ("invalid line in the resolution file");
2168
2169 for (j = 0; j < lto_resolution_str_len; j++)
2170 {
2171 if (strcmp (lto_resolution_str[j], r_str) == 0)
2172 {
2173 r = (enum ld_plugin_symbol_resolution) j;
2174 break;
2175 }
2176 }
2177 if (j == lto_resolution_str_len)
2178 internal_error ("invalid resolution in the resolution file");
2179
2180 if (!(nd && lto_splay_tree_id_equal_p (nd->key, id)))
2181 {
2182 nd = lto_splay_tree_lookup (file_ids, id);
2183 if (nd == NULL)
2184 internal_error ("resolution sub id " HOST_WIDE_INT_PRINT_HEX_PURE
2185 " not in object file", id);
2186 }
2187
2188 file_data = (struct lto_file_decl_data *)nd->value;
2189 /* The indexes are very sparse. To save memory save them in a compact
2190 format that is only unpacked later when the subfile is processed. */
2191 rp.res = r;
2192 rp.index = index;
2193 VEC_safe_push (res_pair, heap, file_data->respairs, rp);
2194 if (file_data->max_index < index)
2195 file_data->max_index = index;
2196 }
2197 }
2198
2199 /* List of file_decl_datas */
2200 struct file_data_list
2201 {
2202 struct lto_file_decl_data *first, *last;
2203 };
2204
2205 /* Is the name for a id'ed LTO section? */
2206
2207 static int
2208 lto_section_with_id (const char *name, unsigned HOST_WIDE_INT *id)
2209 {
2210 const char *s;
2211
2212 if (strncmp (name, LTO_SECTION_NAME_PREFIX, strlen (LTO_SECTION_NAME_PREFIX)))
2213 return 0;
2214 s = strrchr (name, '.');
2215 return s && sscanf (s, "." HOST_WIDE_INT_PRINT_HEX_PURE, id) == 1;
2216 }
2217
2218 /* Create file_data of each sub file id */
2219
2220 static int
2221 create_subid_section_table (struct lto_section_slot *ls, splay_tree file_ids,
2222 struct file_data_list *list)
2223 {
2224 struct lto_section_slot s_slot, *new_slot;
2225 unsigned HOST_WIDE_INT id;
2226 splay_tree_node nd;
2227 void **hash_slot;
2228 char *new_name;
2229 struct lto_file_decl_data *file_data;
2230
2231 if (!lto_section_with_id (ls->name, &id))
2232 return 1;
2233
2234 /* Find hash table of sub module id */
2235 nd = lto_splay_tree_lookup (file_ids, id);
2236 if (nd != NULL)
2237 {
2238 file_data = (struct lto_file_decl_data *)nd->value;
2239 }
2240 else
2241 {
2242 file_data = ggc_alloc_lto_file_decl_data ();
2243 memset(file_data, 0, sizeof (struct lto_file_decl_data));
2244 file_data->id = id;
2245 file_data->section_hash_table = lto_obj_create_section_hash_table ();;
2246 lto_splay_tree_insert (file_ids, id, file_data);
2247
2248 /* Maintain list in linker order */
2249 if (!list->first)
2250 list->first = file_data;
2251 if (list->last)
2252 list->last->next = file_data;
2253 list->last = file_data;
2254 }
2255
2256 /* Copy section into sub module hash table */
2257 new_name = XDUPVEC (char, ls->name, strlen (ls->name) + 1);
2258 s_slot.name = new_name;
2259 hash_slot = htab_find_slot (file_data->section_hash_table, &s_slot, INSERT);
2260 gcc_assert (*hash_slot == NULL);
2261
2262 new_slot = XDUP (struct lto_section_slot, ls);
2263 new_slot->name = new_name;
2264 *hash_slot = new_slot;
2265 return 1;
2266 }
2267
2268 /* Read declarations and other initializations for a FILE_DATA. */
2269
2270 static void
2271 lto_file_finalize (struct lto_file_decl_data *file_data, lto_file *file)
2272 {
2273 const char *data;
2274 size_t len;
2275 VEC(ld_plugin_symbol_resolution_t,heap) *resolutions = NULL;
2276 int i;
2277 res_pair *rp;
2278
2279 /* Create vector for fast access of resolution. We do this lazily
2280 to save memory. */
2281 VEC_safe_grow_cleared (ld_plugin_symbol_resolution_t, heap,
2282 resolutions,
2283 file_data->max_index + 1);
2284 for (i = 0; VEC_iterate (res_pair, file_data->respairs, i, rp); i++)
2285 VEC_replace (ld_plugin_symbol_resolution_t, resolutions, rp->index, rp->res);
2286 VEC_free (res_pair, heap, file_data->respairs);
2287
2288 file_data->renaming_hash_table = lto_create_renaming_table ();
2289 file_data->file_name = file->filename;
2290 data = lto_get_section_data (file_data, LTO_section_decls, NULL, &len);
2291 if (data == NULL)
2292 {
2293 internal_error ("cannot read LTO decls from %s", file_data->file_name);
2294 return;
2295 }
2296 /* Frees resolutions */
2297 lto_read_decls (file_data, data, resolutions);
2298 lto_free_section_data (file_data, LTO_section_decls, NULL, data, len);
2299 }
2300
2301 /* Finalize FILE_DATA in FILE and increase COUNT. */
2302
2303 static int
2304 lto_create_files_from_ids (lto_file *file, struct lto_file_decl_data *file_data,
2305 int *count)
2306 {
2307 lto_file_finalize (file_data, file);
2308 if (cgraph_dump_file)
2309 fprintf (cgraph_dump_file, "Creating file %s with sub id " HOST_WIDE_INT_PRINT_HEX "\n",
2310 file_data->file_name, file_data->id);
2311 (*count)++;
2312 return 0;
2313 }
2314
2315 /* Generate a TREE representation for all types and external decls
2316 entities in FILE.
2317
2318 Read all of the globals out of the file. Then read the cgraph
2319 and process the .o index into the cgraph nodes so that it can open
2320 the .o file to load the functions and ipa information. */
2321
2322 static struct lto_file_decl_data *
2323 lto_file_read (lto_file *file, FILE *resolution_file, int *count)
2324 {
2325 struct lto_file_decl_data *file_data = NULL;
2326 splay_tree file_ids;
2327 htab_t section_hash_table;
2328 struct lto_section_slot *section;
2329 struct file_data_list file_list;
2330 struct lto_section_list section_list;
2331
2332 memset (&section_list, 0, sizeof (struct lto_section_list));
2333 section_hash_table = lto_obj_build_section_table (file, &section_list);
2334
2335 /* Find all sub modules in the object and put their sections into new hash
2336 tables in a splay tree. */
2337 file_ids = lto_splay_tree_new ();
2338 memset (&file_list, 0, sizeof (struct file_data_list));
2339 for (section = section_list.first; section != NULL; section = section->next)
2340 create_subid_section_table (section, file_ids, &file_list);
2341
2342 /* Add resolutions to file ids */
2343 lto_resolution_read (file_ids, resolution_file, file);
2344
2345 /* Finalize each lto file for each submodule in the merged object */
2346 for (file_data = file_list.first; file_data != NULL; file_data = file_data->next)
2347 lto_create_files_from_ids (file, file_data, count);
2348
2349 splay_tree_delete (file_ids);
2350 htab_delete (section_hash_table);
2351
2352 return file_list.first;
2353 }
2354
2355 #if HAVE_MMAP_FILE && HAVE_SYSCONF && defined _SC_PAGE_SIZE
2356 #define LTO_MMAP_IO 1
2357 #endif
2358
2359 #if LTO_MMAP_IO
2360 /* Page size of machine is used for mmap and munmap calls. */
2361 static size_t page_mask;
2362 #endif
2363
2364 /* Get the section data of length LEN from FILENAME starting at
2365 OFFSET. The data segment must be freed by the caller when the
2366 caller is finished. Returns NULL if all was not well. */
2367
2368 static char *
2369 lto_read_section_data (struct lto_file_decl_data *file_data,
2370 intptr_t offset, size_t len)
2371 {
2372 char *result;
2373 static int fd = -1;
2374 static char *fd_name;
2375 #if LTO_MMAP_IO
2376 intptr_t computed_len;
2377 intptr_t computed_offset;
2378 intptr_t diff;
2379 #endif
2380
2381 /* Keep a single-entry file-descriptor cache. The last file we
2382 touched will get closed at exit.
2383 ??? Eventually we want to add a more sophisticated larger cache
2384 or rather fix function body streaming to not stream them in
2385 practically random order. */
2386 if (fd != -1
2387 && filename_cmp (fd_name, file_data->file_name) != 0)
2388 {
2389 free (fd_name);
2390 close (fd);
2391 fd = -1;
2392 }
2393 if (fd == -1)
2394 {
2395 fd = open (file_data->file_name, O_RDONLY|O_BINARY);
2396 if (fd == -1)
2397 {
2398 fatal_error ("Cannot open %s", file_data->file_name);
2399 return NULL;
2400 }
2401 fd_name = xstrdup (file_data->file_name);
2402 }
2403
2404 #if LTO_MMAP_IO
2405 if (!page_mask)
2406 {
2407 size_t page_size = sysconf (_SC_PAGE_SIZE);
2408 page_mask = ~(page_size - 1);
2409 }
2410
2411 computed_offset = offset & page_mask;
2412 diff = offset - computed_offset;
2413 computed_len = len + diff;
2414
2415 result = (char *) mmap (NULL, computed_len, PROT_READ, MAP_PRIVATE,
2416 fd, computed_offset);
2417 if (result == MAP_FAILED)
2418 {
2419 fatal_error ("Cannot map %s", file_data->file_name);
2420 return NULL;
2421 }
2422
2423 return result + diff;
2424 #else
2425 result = (char *) xmalloc (len);
2426 if (lseek (fd, offset, SEEK_SET) != offset
2427 || read (fd, result, len) != (ssize_t) len)
2428 {
2429 free (result);
2430 fatal_error ("Cannot read %s", file_data->file_name);
2431 result = NULL;
2432 }
2433 #ifdef __MINGW32__
2434 /* Native windows doesn't supports delayed unlink on opened file. So
2435 we close file here again. This produces higher I/O load, but at least
2436 it prevents to have dangling file handles preventing unlink. */
2437 free (fd_name);
2438 fd_name = NULL;
2439 close (fd);
2440 fd = -1;
2441 #endif
2442 return result;
2443 #endif
2444 }
2445
2446
2447 /* Get the section data from FILE_DATA of SECTION_TYPE with NAME.
2448 NAME will be NULL unless the section type is for a function
2449 body. */
2450
2451 static const char *
2452 get_section_data (struct lto_file_decl_data *file_data,
2453 enum lto_section_type section_type,
2454 const char *name,
2455 size_t *len)
2456 {
2457 htab_t section_hash_table = file_data->section_hash_table;
2458 struct lto_section_slot *f_slot;
2459 struct lto_section_slot s_slot;
2460 const char *section_name = lto_get_section_name (section_type, name, file_data);
2461 char *data = NULL;
2462
2463 *len = 0;
2464 s_slot.name = section_name;
2465 f_slot = (struct lto_section_slot *) htab_find (section_hash_table, &s_slot);
2466 if (f_slot)
2467 {
2468 data = lto_read_section_data (file_data, f_slot->start, f_slot->len);
2469 *len = f_slot->len;
2470 }
2471
2472 free (CONST_CAST (char *, section_name));
2473 return data;
2474 }
2475
2476
2477 /* Free the section data from FILE_DATA of SECTION_TYPE with NAME that
2478 starts at OFFSET and has LEN bytes. */
2479
2480 static void
2481 free_section_data (struct lto_file_decl_data *file_data ATTRIBUTE_UNUSED,
2482 enum lto_section_type section_type ATTRIBUTE_UNUSED,
2483 const char *name ATTRIBUTE_UNUSED,
2484 const char *offset, size_t len ATTRIBUTE_UNUSED)
2485 {
2486 #if LTO_MMAP_IO
2487 intptr_t computed_len;
2488 intptr_t computed_offset;
2489 intptr_t diff;
2490 #endif
2491
2492 #if LTO_MMAP_IO
2493 computed_offset = ((intptr_t) offset) & page_mask;
2494 diff = (intptr_t) offset - computed_offset;
2495 computed_len = len + diff;
2496
2497 munmap ((caddr_t) computed_offset, computed_len);
2498 #else
2499 free (CONST_CAST(char *, offset));
2500 #endif
2501 }
2502
2503 static lto_file *current_lto_file;
2504
2505 /* Helper for qsort; compare partitions and return one with smaller size.
2506 We sort from greatest to smallest so parallel build doesn't stale on the
2507 longest compilation being executed too late. */
2508
2509 static int
2510 cmp_partitions_size (const void *a, const void *b)
2511 {
2512 const struct ltrans_partition_def *pa
2513 = *(struct ltrans_partition_def *const *)a;
2514 const struct ltrans_partition_def *pb
2515 = *(struct ltrans_partition_def *const *)b;
2516 return pb->insns - pa->insns;
2517 }
2518
2519 /* Helper for qsort; compare partitions and return one with smaller order. */
2520
2521 static int
2522 cmp_partitions_order (const void *a, const void *b)
2523 {
2524 const struct ltrans_partition_def *pa
2525 = *(struct ltrans_partition_def *const *)a;
2526 const struct ltrans_partition_def *pb
2527 = *(struct ltrans_partition_def *const *)b;
2528 int ordera = -1, orderb = -1;
2529
2530 if (lto_symtab_encoder_size (pa->encoder))
2531 ordera = lto_symtab_encoder_deref (pa->encoder, 0)->symbol.order;
2532 if (lto_symtab_encoder_size (pb->encoder))
2533 orderb = lto_symtab_encoder_deref (pb->encoder, 0)->symbol.order;
2534 return orderb - ordera;
2535 }
2536
2537 /* Write all output files in WPA mode and the file with the list of
2538 LTRANS units. */
2539
2540 static void
2541 lto_wpa_write_files (void)
2542 {
2543 unsigned i, n_sets;
2544 lto_file *file;
2545 ltrans_partition part;
2546 FILE *ltrans_output_list_stream;
2547 char *temp_filename;
2548 size_t blen;
2549
2550 /* Open the LTRANS output list. */
2551 if (!ltrans_output_list)
2552 fatal_error ("no LTRANS output list filename provided");
2553 ltrans_output_list_stream = fopen (ltrans_output_list, "w");
2554 if (ltrans_output_list_stream == NULL)
2555 fatal_error ("opening LTRANS output list %s: %m", ltrans_output_list);
2556
2557 timevar_push (TV_WHOPR_WPA);
2558
2559 FOR_EACH_VEC_ELT (ltrans_partition, ltrans_partitions, i, part)
2560 lto_stats.num_output_symtab_nodes += lto_symtab_encoder_size (part->encoder);
2561
2562 /* Find out statics that need to be promoted
2563 to globals with hidden visibility because they are accessed from multiple
2564 partitions. */
2565 lto_promote_cross_file_statics ();
2566
2567 timevar_pop (TV_WHOPR_WPA);
2568
2569 timevar_push (TV_WHOPR_WPA_IO);
2570
2571 /* Generate a prefix for the LTRANS unit files. */
2572 blen = strlen (ltrans_output_list);
2573 temp_filename = (char *) xmalloc (blen + sizeof ("2147483648.o"));
2574 strcpy (temp_filename, ltrans_output_list);
2575 if (blen > sizeof (".out")
2576 && strcmp (temp_filename + blen - sizeof (".out") + 1,
2577 ".out") == 0)
2578 temp_filename[blen - sizeof (".out") + 1] = '\0';
2579 blen = strlen (temp_filename);
2580
2581 n_sets = VEC_length (ltrans_partition, ltrans_partitions);
2582
2583 /* Sort partitions by size so small ones are compiled last.
2584 FIXME: Even when not reordering we may want to output one list for parallel make
2585 and other for final link command. */
2586 VEC_qsort (ltrans_partition, ltrans_partitions,
2587 flag_toplevel_reorder ? cmp_partitions_size : cmp_partitions_order);
2588 for (i = 0; i < n_sets; i++)
2589 {
2590 size_t len;
2591 ltrans_partition part = VEC_index (ltrans_partition, ltrans_partitions, i);
2592
2593 /* Write all the nodes in SET. */
2594 sprintf (temp_filename + blen, "%u.o", i);
2595 file = lto_obj_file_open (temp_filename, true);
2596 if (!file)
2597 fatal_error ("lto_obj_file_open() failed");
2598
2599 if (!quiet_flag)
2600 fprintf (stderr, " %s (%s %i insns)", temp_filename, part->name, part->insns);
2601 if (cgraph_dump_file)
2602 {
2603 lto_symtab_encoder_iterator lsei;
2604
2605 fprintf (cgraph_dump_file, "Writing partition %s to file %s, %i insns\n",
2606 part->name, temp_filename, part->insns);
2607 fprintf (cgraph_dump_file, " Symbols in partition: ");
2608 for (lsei = lsei_start_in_partition (part->encoder); !lsei_end_p (lsei);
2609 lsei_next_in_partition (&lsei))
2610 {
2611 symtab_node node = lsei_node (lsei);
2612 fprintf (cgraph_dump_file, "%s ", symtab_node_asm_name (node));
2613 }
2614 fprintf (cgraph_dump_file, "\n Symbols in boundary: ");
2615 for (lsei = lsei_start (part->encoder); !lsei_end_p (lsei);
2616 lsei_next (&lsei))
2617 {
2618 symtab_node node = lsei_node (lsei);
2619 if (!lto_symtab_encoder_in_partition_p (part->encoder, node))
2620 {
2621 fprintf (cgraph_dump_file, "%s ", symtab_node_asm_name (node));
2622 if (symtab_function_p (node)
2623 && lto_symtab_encoder_encode_body_p (part->encoder, cgraph (node)))
2624 fprintf (cgraph_dump_file, "(body included)");
2625 else if (symtab_variable_p (node)
2626 && lto_symtab_encoder_encode_initializer_p (part->encoder, varpool (node)))
2627 fprintf (cgraph_dump_file, "(initializer included)");
2628 }
2629 }
2630 fprintf (cgraph_dump_file, "\n");
2631 }
2632 gcc_checking_assert (lto_symtab_encoder_size (part->encoder) || !i);
2633
2634 lto_set_current_out_file (file);
2635
2636 ipa_write_optimization_summaries (part->encoder);
2637
2638 lto_set_current_out_file (NULL);
2639 lto_obj_file_close (file);
2640 part->encoder = NULL;
2641
2642 len = strlen (temp_filename);
2643 if (fwrite (temp_filename, 1, len, ltrans_output_list_stream) < len
2644 || fwrite ("\n", 1, 1, ltrans_output_list_stream) < 1)
2645 fatal_error ("writing to LTRANS output list %s: %m",
2646 ltrans_output_list);
2647 }
2648
2649 lto_stats.num_output_files += n_sets;
2650
2651 /* Close the LTRANS output list. */
2652 if (fclose (ltrans_output_list_stream))
2653 fatal_error ("closing LTRANS output list %s: %m", ltrans_output_list);
2654
2655 free_ltrans_partitions();
2656
2657 timevar_pop (TV_WHOPR_WPA_IO);
2658 }
2659
2660
2661 /* If TT is a variable or function decl replace it with its
2662 prevailing variant. */
2663 #define LTO_SET_PREVAIL(tt) \
2664 do {\
2665 if ((tt) && VAR_OR_FUNCTION_DECL_P (tt)) \
2666 tt = lto_symtab_prevailing_decl (tt); \
2667 } while (0)
2668
2669 /* Ensure that TT isn't a replacable var of function decl. */
2670 #define LTO_NO_PREVAIL(tt) \
2671 gcc_assert (!(tt) || !VAR_OR_FUNCTION_DECL_P (tt))
2672
2673 /* Given a tree T replace all fields referring to variables or functions
2674 with their prevailing variant. */
2675 static void
2676 lto_fixup_prevailing_decls (tree t)
2677 {
2678 enum tree_code code = TREE_CODE (t);
2679 LTO_NO_PREVAIL (TREE_TYPE (t));
2680 if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
2681 LTO_NO_PREVAIL (TREE_CHAIN (t));
2682 if (DECL_P (t))
2683 {
2684 LTO_NO_PREVAIL (DECL_NAME (t));
2685 LTO_SET_PREVAIL (DECL_CONTEXT (t));
2686 if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
2687 {
2688 LTO_SET_PREVAIL (DECL_SIZE (t));
2689 LTO_SET_PREVAIL (DECL_SIZE_UNIT (t));
2690 LTO_SET_PREVAIL (DECL_INITIAL (t));
2691 LTO_NO_PREVAIL (DECL_ATTRIBUTES (t));
2692 LTO_SET_PREVAIL (DECL_ABSTRACT_ORIGIN (t));
2693 }
2694 if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
2695 {
2696 LTO_NO_PREVAIL (t->decl_with_vis.assembler_name);
2697 LTO_NO_PREVAIL (DECL_SECTION_NAME (t));
2698 }
2699 if (CODE_CONTAINS_STRUCT (code, TS_DECL_NON_COMMON))
2700 {
2701 LTO_NO_PREVAIL (DECL_ARGUMENT_FLD (t));
2702 LTO_NO_PREVAIL (DECL_RESULT_FLD (t));
2703 LTO_NO_PREVAIL (DECL_VINDEX (t));
2704 }
2705 if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
2706 LTO_SET_PREVAIL (DECL_FUNCTION_PERSONALITY (t));
2707 if (CODE_CONTAINS_STRUCT (code, TS_FIELD_DECL))
2708 {
2709 LTO_NO_PREVAIL (DECL_FIELD_OFFSET (t));
2710 LTO_NO_PREVAIL (DECL_BIT_FIELD_TYPE (t));
2711 LTO_NO_PREVAIL (DECL_QUALIFIER (t));
2712 LTO_NO_PREVAIL (DECL_FIELD_BIT_OFFSET (t));
2713 LTO_NO_PREVAIL (DECL_FCONTEXT (t));
2714 }
2715 }
2716 else if (TYPE_P (t))
2717 {
2718 LTO_NO_PREVAIL (TYPE_CACHED_VALUES (t));
2719 LTO_SET_PREVAIL (TYPE_SIZE (t));
2720 LTO_SET_PREVAIL (TYPE_SIZE_UNIT (t));
2721 LTO_NO_PREVAIL (TYPE_ATTRIBUTES (t));
2722 LTO_NO_PREVAIL (TYPE_NAME (t));
2723
2724 LTO_SET_PREVAIL (TYPE_MINVAL (t));
2725 LTO_SET_PREVAIL (TYPE_MAXVAL (t));
2726 LTO_SET_PREVAIL (t->type_non_common.binfo);
2727
2728 LTO_SET_PREVAIL (TYPE_CONTEXT (t));
2729
2730 LTO_NO_PREVAIL (TYPE_CANONICAL (t));
2731 LTO_NO_PREVAIL (TYPE_MAIN_VARIANT (t));
2732 LTO_NO_PREVAIL (TYPE_NEXT_VARIANT (t));
2733 }
2734 else if (EXPR_P (t))
2735 {
2736 int i;
2737 LTO_NO_PREVAIL (t->exp.block);
2738 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
2739 LTO_SET_PREVAIL (TREE_OPERAND (t, i));
2740 }
2741 else
2742 {
2743 switch (code)
2744 {
2745 case TREE_LIST:
2746 LTO_SET_PREVAIL (TREE_VALUE (t));
2747 LTO_SET_PREVAIL (TREE_PURPOSE (t));
2748 break;
2749 default:
2750 gcc_unreachable ();
2751 }
2752 }
2753 }
2754 #undef LTO_SET_PREVAIL
2755 #undef LTO_NO_PREVAIL
2756
2757 /* Helper function of lto_fixup_decls. Walks the var and fn streams in STATE,
2758 replaces var and function decls with the corresponding prevailing def. */
2759
2760 static void
2761 lto_fixup_state (struct lto_in_decl_state *state)
2762 {
2763 unsigned i, si;
2764 struct lto_tree_ref_table *table;
2765
2766 /* Although we only want to replace FUNCTION_DECLs and VAR_DECLs,
2767 we still need to walk from all DECLs to find the reachable
2768 FUNCTION_DECLs and VAR_DECLs. */
2769 for (si = 0; si < LTO_N_DECL_STREAMS; si++)
2770 {
2771 table = &state->streams[si];
2772 for (i = 0; i < table->size; i++)
2773 {
2774 tree *tp = table->trees + i;
2775 if (VAR_OR_FUNCTION_DECL_P (*tp))
2776 *tp = lto_symtab_prevailing_decl (*tp);
2777 }
2778 }
2779 }
2780
2781 /* A callback of htab_traverse. Just extracts a state from SLOT
2782 and calls lto_fixup_state. */
2783
2784 static int
2785 lto_fixup_state_aux (void **slot, void *aux ATTRIBUTE_UNUSED)
2786 {
2787 struct lto_in_decl_state *state = (struct lto_in_decl_state *) *slot;
2788 lto_fixup_state (state);
2789 return 1;
2790 }
2791
2792 /* Fix the decls from all FILES. Replaces each decl with the corresponding
2793 prevailing one. */
2794
2795 static void
2796 lto_fixup_decls (struct lto_file_decl_data **files)
2797 {
2798 unsigned int i;
2799 htab_iterator hi;
2800 tree t;
2801
2802 FOR_EACH_HTAB_ELEMENT (tree_with_vars, t, tree, hi)
2803 lto_fixup_prevailing_decls (t);
2804
2805 for (i = 0; files[i]; i++)
2806 {
2807 struct lto_file_decl_data *file = files[i];
2808 struct lto_in_decl_state *state = file->global_decl_state;
2809 lto_fixup_state (state);
2810
2811 htab_traverse (file->function_decl_states, lto_fixup_state_aux, NULL);
2812 }
2813 }
2814
2815 static GTY((length ("lto_stats.num_input_files + 1"))) struct lto_file_decl_data **all_file_decl_data;
2816
2817 /* Turn file datas for sub files into a single array, so that they look
2818 like separate files for further passes. */
2819
2820 static void
2821 lto_flatten_files (struct lto_file_decl_data **orig, int count, int last_file_ix)
2822 {
2823 struct lto_file_decl_data *n, *next;
2824 int i, k;
2825
2826 lto_stats.num_input_files = count;
2827 all_file_decl_data
2828 = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (count + 1);
2829 /* Set the hooks so that all of the ipa passes can read in their data. */
2830 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2831 for (i = 0, k = 0; i < last_file_ix; i++)
2832 {
2833 for (n = orig[i]; n != NULL; n = next)
2834 {
2835 all_file_decl_data[k++] = n;
2836 next = n->next;
2837 n->next = NULL;
2838 }
2839 }
2840 all_file_decl_data[k] = NULL;
2841 gcc_assert (k == count);
2842 }
2843
2844 /* Input file data before flattening (i.e. splitting them to subfiles to support
2845 incremental linking. */
2846 static int real_file_count;
2847 static GTY((length ("real_file_count + 1"))) struct lto_file_decl_data **real_file_decl_data;
2848
2849 /* Read all the symbols from the input files FNAMES. NFILES is the
2850 number of files requested in the command line. Instantiate a
2851 global call graph by aggregating all the sub-graphs found in each
2852 file. */
2853
2854 static void
2855 read_cgraph_and_symbols (unsigned nfiles, const char **fnames)
2856 {
2857 unsigned int i, last_file_ix;
2858 FILE *resolution;
2859 struct cgraph_node *node;
2860 int count = 0;
2861 struct lto_file_decl_data **decl_data;
2862
2863 init_cgraph ();
2864
2865 timevar_push (TV_IPA_LTO_DECL_IN);
2866
2867 real_file_decl_data
2868 = decl_data = ggc_alloc_cleared_vec_lto_file_decl_data_ptr (nfiles + 1);
2869 real_file_count = nfiles;
2870
2871 /* Read the resolution file. */
2872 resolution = NULL;
2873 if (resolution_file_name)
2874 {
2875 int t;
2876 unsigned num_objects;
2877
2878 resolution = fopen (resolution_file_name, "r");
2879 if (resolution == NULL)
2880 fatal_error ("could not open symbol resolution file: %m");
2881
2882 t = fscanf (resolution, "%u", &num_objects);
2883 gcc_assert (t == 1);
2884
2885 /* True, since the plugin splits the archives. */
2886 gcc_assert (num_objects == nfiles);
2887 }
2888
2889 tree_with_vars = htab_create_ggc (101, htab_hash_pointer, htab_eq_pointer,
2890 NULL);
2891 type_hash_cache = htab_create_ggc (512, tree_int_map_hash,
2892 tree_int_map_eq, NULL);
2893 type_pair_cache = XCNEWVEC (struct type_pair_d, GIMPLE_TYPE_PAIR_SIZE);
2894 gimple_type_leader = ggc_alloc_cleared_vec_gimple_type_leader_entry_s
2895 (GIMPLE_TYPE_LEADER_SIZE);
2896 gimple_types = htab_create_ggc (16381, gimple_type_hash, gimple_type_eq, 0);
2897
2898 if (!quiet_flag)
2899 fprintf (stderr, "Reading object files:");
2900
2901 /* Read all of the object files specified on the command line. */
2902 for (i = 0, last_file_ix = 0; i < nfiles; ++i)
2903 {
2904 struct lto_file_decl_data *file_data = NULL;
2905 if (!quiet_flag)
2906 {
2907 fprintf (stderr, " %s", fnames[i]);
2908 fflush (stderr);
2909 }
2910
2911 current_lto_file = lto_obj_file_open (fnames[i], false);
2912 if (!current_lto_file)
2913 break;
2914
2915 file_data = lto_file_read (current_lto_file, resolution, &count);
2916 if (!file_data)
2917 {
2918 lto_obj_file_close (current_lto_file);
2919 current_lto_file = NULL;
2920 break;
2921 }
2922
2923 decl_data[last_file_ix++] = file_data;
2924
2925 lto_obj_file_close (current_lto_file);
2926 current_lto_file = NULL;
2927 ggc_collect ();
2928 }
2929
2930 lto_flatten_files (decl_data, count, last_file_ix);
2931 lto_stats.num_input_files = count;
2932 ggc_free(decl_data);
2933 real_file_decl_data = NULL;
2934
2935 if (resolution_file_name)
2936 fclose (resolution);
2937
2938 /* Set the hooks so that all of the ipa passes can read in their data. */
2939 lto_set_in_hooks (all_file_decl_data, get_section_data, free_section_data);
2940
2941 timevar_pop (TV_IPA_LTO_DECL_IN);
2942
2943 if (!quiet_flag)
2944 fprintf (stderr, "\nReading the callgraph\n");
2945
2946 timevar_push (TV_IPA_LTO_CGRAPH_IO);
2947 /* Read the symtab. */
2948 input_symtab ();
2949 timevar_pop (TV_IPA_LTO_CGRAPH_IO);
2950
2951 if (!quiet_flag)
2952 fprintf (stderr, "Merging declarations\n");
2953
2954 timevar_push (TV_IPA_LTO_DECL_MERGE);
2955 /* Merge global decls. */
2956 lto_symtab_merge_decls ();
2957
2958 /* If there were errors during symbol merging bail out, we have no
2959 good way to recover here. */
2960 if (seen_error ())
2961 fatal_error ("errors during merging of translation units");
2962
2963 /* Fixup all decls and types and free the type hash tables. */
2964 lto_fixup_decls (all_file_decl_data);
2965 htab_delete (tree_with_vars);
2966 tree_with_vars = NULL;
2967 htab_delete (gimple_types);
2968 gimple_types = NULL;
2969 htab_delete (type_hash_cache);
2970 type_hash_cache = NULL;
2971 free (type_pair_cache);
2972 type_pair_cache = NULL;
2973 gimple_type_leader = NULL;
2974 free_gimple_type_tables ();
2975 ggc_collect ();
2976
2977 timevar_pop (TV_IPA_LTO_DECL_MERGE);
2978 /* Each pass will set the appropriate timer. */
2979
2980 if (!quiet_flag)
2981 fprintf (stderr, "Reading summaries\n");
2982
2983 /* Read the IPA summary data. */
2984 if (flag_ltrans)
2985 ipa_read_optimization_summaries ();
2986 else
2987 ipa_read_summaries ();
2988
2989 /* Finally merge the cgraph according to the decl merging decisions. */
2990 timevar_push (TV_IPA_LTO_CGRAPH_MERGE);
2991 if (cgraph_dump_file)
2992 {
2993 fprintf (cgraph_dump_file, "Before merging:\n");
2994 dump_cgraph (cgraph_dump_file);
2995 dump_varpool (cgraph_dump_file);
2996 }
2997 lto_symtab_merge_cgraph_nodes ();
2998 ggc_collect ();
2999
3000 /* FIXME: ipa_transforms_to_apply holds list of passes that have optimization
3001 summaries computed and needs to apply changes. At the moment WHOPR only
3002 supports inlining, so we can push it here by hand. In future we need to stream
3003 this field into ltrans compilation. */
3004 if (flag_ltrans)
3005 FOR_EACH_DEFINED_FUNCTION (node)
3006 VEC_safe_push (ipa_opt_pass, heap,
3007 node->ipa_transforms_to_apply,
3008 (ipa_opt_pass)&pass_ipa_inline);
3009
3010 timevar_pop (TV_IPA_LTO_CGRAPH_MERGE);
3011
3012 timevar_push (TV_IPA_LTO_DECL_INIT_IO);
3013
3014 /* Indicate that the cgraph is built and ready. */
3015 cgraph_function_flags_ready = true;
3016
3017 timevar_pop (TV_IPA_LTO_DECL_INIT_IO);
3018 ggc_free (all_file_decl_data);
3019 all_file_decl_data = NULL;
3020 }
3021
3022
3023 /* Materialize all the bodies for all the nodes in the callgraph. */
3024
3025 static void
3026 materialize_cgraph (void)
3027 {
3028 tree decl;
3029 struct cgraph_node *node;
3030 unsigned i;
3031 timevar_id_t lto_timer;
3032
3033 if (!quiet_flag)
3034 fprintf (stderr,
3035 flag_wpa ? "Materializing decls:" : "Reading function bodies:");
3036
3037 /* Now that we have input the cgraph, we need to clear all of the aux
3038 nodes and read the functions if we are not running in WPA mode. */
3039 timevar_push (TV_IPA_LTO_GIMPLE_IN);
3040
3041 FOR_EACH_FUNCTION (node)
3042 {
3043 if (node->symbol.lto_file_data)
3044 {
3045 lto_materialize_function (node);
3046 lto_stats.num_input_cgraph_nodes++;
3047 }
3048 }
3049
3050 timevar_pop (TV_IPA_LTO_GIMPLE_IN);
3051
3052 /* Start the appropriate timer depending on the mode that we are
3053 operating in. */
3054 lto_timer = (flag_wpa) ? TV_WHOPR_WPA
3055 : (flag_ltrans) ? TV_WHOPR_LTRANS
3056 : TV_LTO;
3057 timevar_push (lto_timer);
3058
3059 current_function_decl = NULL;
3060 set_cfun (NULL);
3061
3062 /* Inform the middle end about the global variables we have seen. */
3063 FOR_EACH_VEC_ELT (tree, lto_global_var_decls, i, decl)
3064 rest_of_decl_compilation (decl, 1, 0);
3065
3066 if (!quiet_flag)
3067 fprintf (stderr, "\n");
3068
3069 timevar_pop (lto_timer);
3070 }
3071
3072
3073 /* Show various memory usage statistics related to LTO. */
3074 static void
3075 print_lto_report_1 (void)
3076 {
3077 const char *pfx = (flag_lto) ? "LTO" : (flag_wpa) ? "WPA" : "LTRANS";
3078 fprintf (stderr, "%s statistics\n", pfx);
3079
3080 if (gimple_types)
3081 fprintf (stderr, "[%s] GIMPLE type table: size %ld, %ld elements, "
3082 "%ld searches, %ld collisions (ratio: %f)\n", pfx,
3083 (long) htab_size (gimple_types),
3084 (long) htab_elements (gimple_types),
3085 (long) gimple_types->searches,
3086 (long) gimple_types->collisions,
3087 htab_collisions (gimple_types));
3088 else
3089 fprintf (stderr, "[%s] GIMPLE type table is empty\n", pfx);
3090 if (type_hash_cache)
3091 fprintf (stderr, "[%s] GIMPLE type hash table: size %ld, %ld elements, "
3092 "%ld searches, %ld collisions (ratio: %f)\n", pfx,
3093 (long) htab_size (type_hash_cache),
3094 (long) htab_elements (type_hash_cache),
3095 (long) type_hash_cache->searches,
3096 (long) type_hash_cache->collisions,
3097 htab_collisions (type_hash_cache));
3098 else
3099 fprintf (stderr, "[%s] GIMPLE type hash table is empty\n", pfx);
3100
3101 print_gimple_types_stats (pfx);
3102 print_lto_report (pfx);
3103 }
3104
3105 /* Perform whole program analysis (WPA) on the callgraph and write out the
3106 optimization plan. */
3107
3108 static void
3109 do_whole_program_analysis (void)
3110 {
3111 symtab_node node;
3112
3113 timevar_start (TV_PHASE_OPT_GEN);
3114
3115 /* Note that since we are in WPA mode, materialize_cgraph will not
3116 actually read in all the function bodies. It only materializes
3117 the decls and cgraph nodes so that analysis can be performed. */
3118 materialize_cgraph ();
3119
3120 /* Reading in the cgraph uses different timers, start timing WPA now. */
3121 timevar_push (TV_WHOPR_WPA);
3122
3123 if (pre_ipa_mem_report)
3124 {
3125 fprintf (stderr, "Memory consumption before IPA\n");
3126 dump_memory_report (false);
3127 }
3128
3129 cgraph_function_flags_ready = true;
3130
3131 if (cgraph_dump_file)
3132 {
3133 dump_cgraph (cgraph_dump_file);
3134 dump_varpool (cgraph_dump_file);
3135 }
3136 bitmap_obstack_initialize (NULL);
3137 cgraph_state = CGRAPH_STATE_IPA_SSA;
3138
3139 execute_ipa_pass_list (all_regular_ipa_passes);
3140
3141 if (cgraph_dump_file)
3142 {
3143 fprintf (cgraph_dump_file, "Optimized ");
3144 dump_cgraph (cgraph_dump_file);
3145 dump_varpool (cgraph_dump_file);
3146 }
3147 #ifdef ENABLE_CHECKING
3148 verify_cgraph ();
3149 #endif
3150 bitmap_obstack_release (NULL);
3151
3152 /* We are about to launch the final LTRANS phase, stop the WPA timer. */
3153 timevar_pop (TV_WHOPR_WPA);
3154
3155 timevar_push (TV_WHOPR_PARTITIONING);
3156 if (flag_lto_partition_1to1)
3157 lto_1_to_1_map ();
3158 else if (flag_lto_partition_max)
3159 lto_max_map ();
3160 else
3161 lto_balanced_map ();
3162
3163 /* AUX pointers are used by partitioning code to bookkeep number of
3164 partitions symbol is in. This is no longer needed. */
3165 FOR_EACH_SYMBOL (node)
3166 node->symbol.aux = NULL;
3167
3168 lto_stats.num_cgraph_partitions += VEC_length (ltrans_partition,
3169 ltrans_partitions);
3170 timevar_pop (TV_WHOPR_PARTITIONING);
3171
3172 timevar_stop (TV_PHASE_OPT_GEN);
3173 timevar_start (TV_PHASE_STREAM_OUT);
3174
3175 if (!quiet_flag)
3176 {
3177 fprintf (stderr, "\nStreaming out");
3178 fflush (stderr);
3179 }
3180 lto_wpa_write_files ();
3181 if (!quiet_flag)
3182 fprintf (stderr, "\n");
3183
3184 timevar_stop (TV_PHASE_STREAM_OUT);
3185
3186 ggc_collect ();
3187 if (post_ipa_mem_report)
3188 {
3189 fprintf (stderr, "Memory consumption after IPA\n");
3190 dump_memory_report (false);
3191 }
3192
3193 /* Show the LTO report before launching LTRANS. */
3194 if (flag_lto_report)
3195 print_lto_report_1 ();
3196 if (mem_report_wpa)
3197 dump_memory_report (true);
3198 }
3199
3200
3201 static GTY(()) tree lto_eh_personality_decl;
3202
3203 /* Return the LTO personality function decl. */
3204
3205 tree
3206 lto_eh_personality (void)
3207 {
3208 if (!lto_eh_personality_decl)
3209 {
3210 /* Use the first personality DECL for our personality if we don't
3211 support multiple ones. This ensures that we don't artificially
3212 create the need for them in a single-language program. */
3213 if (first_personality_decl && !dwarf2out_do_cfi_asm ())
3214 lto_eh_personality_decl = first_personality_decl;
3215 else
3216 lto_eh_personality_decl = lhd_gcc_personality ();
3217 }
3218
3219 return lto_eh_personality_decl;
3220 }
3221
3222 /* Set the process name based on the LTO mode. */
3223
3224 static void
3225 lto_process_name (void)
3226 {
3227 if (flag_lto)
3228 setproctitle ("lto1-lto");
3229 if (flag_wpa)
3230 setproctitle ("lto1-wpa");
3231 if (flag_ltrans)
3232 setproctitle ("lto1-ltrans");
3233 }
3234
3235
3236 /* Initialize the LTO front end. */
3237
3238 static void
3239 lto_init (void)
3240 {
3241 lto_process_name ();
3242 lto_streamer_hooks_init ();
3243 lto_reader_init ();
3244 lto_set_in_hooks (NULL, get_section_data, free_section_data);
3245 memset (&lto_stats, 0, sizeof (lto_stats));
3246 bitmap_obstack_initialize (NULL);
3247 gimple_register_cfg_hooks ();
3248 }
3249
3250
3251 /* Main entry point for the GIMPLE front end. This front end has
3252 three main personalities:
3253
3254 - LTO (-flto). All the object files on the command line are
3255 loaded in memory and processed as a single translation unit.
3256 This is the traditional link-time optimization behavior.
3257
3258 - WPA (-fwpa). Only the callgraph and summary information for
3259 files in the command file are loaded. A single callgraph
3260 (without function bodies) is instantiated for the whole set of
3261 files. IPA passes are only allowed to analyze the call graph
3262 and make transformation decisions. The callgraph is
3263 partitioned, each partition is written to a new object file
3264 together with the transformation decisions.
3265
3266 - LTRANS (-fltrans). Similar to -flto but it prevents the IPA
3267 summary files from running again. Since WPA computed summary
3268 information and decided what transformations to apply, LTRANS
3269 simply applies them. */
3270
3271 void
3272 lto_main (void)
3273 {
3274 /* LTO is called as a front end, even though it is not a front end.
3275 Because it is called as a front end, TV_PHASE_PARSING and
3276 TV_PARSE_GLOBAL are active, and we need to turn them off while
3277 doing LTO. Later we turn them back on so they are active up in
3278 toplev.c. */
3279 timevar_pop (TV_PARSE_GLOBAL);
3280 timevar_stop (TV_PHASE_PARSING);
3281
3282 timevar_start (TV_PHASE_SETUP);
3283
3284 /* Initialize the LTO front end. */
3285 lto_init ();
3286
3287 timevar_stop (TV_PHASE_SETUP);
3288 timevar_start (TV_PHASE_STREAM_IN);
3289
3290 /* Read all the symbols and call graph from all the files in the
3291 command line. */
3292 read_cgraph_and_symbols (num_in_fnames, in_fnames);
3293
3294 timevar_stop (TV_PHASE_STREAM_IN);
3295
3296 if (!seen_error ())
3297 {
3298 /* If WPA is enabled analyze the whole call graph and create an
3299 optimization plan. Otherwise, read in all the function
3300 bodies and continue with optimization. */
3301 if (flag_wpa)
3302 do_whole_program_analysis ();
3303 else
3304 {
3305 timevar_start (TV_PHASE_OPT_GEN);
3306
3307 materialize_cgraph ();
3308
3309 /* Let the middle end know that we have read and merged all of
3310 the input files. */
3311 compile ();
3312
3313 timevar_stop (TV_PHASE_OPT_GEN);
3314
3315 /* FIXME lto, if the processes spawned by WPA fail, we miss
3316 the chance to print WPA's report, so WPA will call
3317 print_lto_report before launching LTRANS. If LTRANS was
3318 launched directly by the driver we would not need to do
3319 this. */
3320 if (flag_lto_report)
3321 print_lto_report_1 ();
3322 }
3323 }
3324
3325 /* Here we make LTO pretend to be a parser. */
3326 timevar_start (TV_PHASE_PARSING);
3327 timevar_push (TV_PARSE_GLOBAL);
3328 }
3329
3330 #include "gt-lto-lto.h"