2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / tree-streamer.c
blobc1203fd7233b1800948c224183858748360ed5ed
1 /* Miscellaneous utilities for tree streaming. Things that are used
2 in both input and output are here.
4 Copyright (C) 2011-2015 Free Software Foundation, Inc.
5 Contributed by Diego Novillo <dnovillo@google.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "input.h"
27 #include "alias.h"
28 #include "symtab.h"
29 #include "options.h"
30 #include "tree.h"
31 #include "fold-const.h"
32 #include "predict.h"
33 #include "tm.h"
34 #include "hard-reg-set.h"
35 #include "input.h"
36 #include "function.h"
37 #include "basic-block.h"
38 #include "tree-ssa-alias.h"
39 #include "internal-fn.h"
40 #include "gimple-expr.h"
41 #include "is-a.h"
42 #include "gimple.h"
43 #include "streamer-hooks.h"
44 #include "plugin-api.h"
45 #include "ipa-ref.h"
46 #include "cgraph.h"
47 #include "tree-streamer.h"
49 /* Table indexed by machine_mode, used for 2 different purposes.
50 During streaming out we record there non-zero value for all modes
51 that were streamed out.
52 During streaming in, we translate the on the disk mode using this
53 table. For normal LTO it is set to identity, for ACCEL_COMPILER
54 depending on the mode_table content. */
55 unsigned char streamer_mode_table[1 << 8];
57 /* Check that all the TS_* structures handled by the streamer_write_* and
58 streamer_read_* routines are exactly ALL the structures defined in
59 treestruct.def. */
61 void
62 streamer_check_handled_ts_structures (void)
64 bool handled_p[LAST_TS_ENUM];
65 unsigned i;
67 memset (&handled_p, 0, sizeof (handled_p));
69 /* These are the TS_* structures that are either handled or
70 explicitly ignored by the streamer routines. */
71 handled_p[TS_BASE] = true;
72 handled_p[TS_TYPED] = true;
73 handled_p[TS_COMMON] = true;
74 handled_p[TS_INT_CST] = true;
75 handled_p[TS_REAL_CST] = true;
76 handled_p[TS_FIXED_CST] = true;
77 handled_p[TS_VECTOR] = true;
78 handled_p[TS_STRING] = true;
79 handled_p[TS_COMPLEX] = true;
80 handled_p[TS_IDENTIFIER] = true;
81 handled_p[TS_DECL_MINIMAL] = true;
82 handled_p[TS_DECL_COMMON] = true;
83 handled_p[TS_DECL_WRTL] = true;
84 handled_p[TS_DECL_NON_COMMON] = true;
85 handled_p[TS_DECL_WITH_VIS] = true;
86 handled_p[TS_FIELD_DECL] = true;
87 handled_p[TS_VAR_DECL] = true;
88 handled_p[TS_PARM_DECL] = true;
89 handled_p[TS_LABEL_DECL] = true;
90 handled_p[TS_RESULT_DECL] = true;
91 handled_p[TS_CONST_DECL] = true;
92 handled_p[TS_TYPE_DECL] = true;
93 handled_p[TS_FUNCTION_DECL] = true;
94 handled_p[TS_TYPE_COMMON] = true;
95 handled_p[TS_TYPE_WITH_LANG_SPECIFIC] = true;
96 handled_p[TS_TYPE_NON_COMMON] = true;
97 handled_p[TS_LIST] = true;
98 handled_p[TS_VEC] = true;
99 handled_p[TS_EXP] = true;
100 handled_p[TS_SSA_NAME] = true;
101 handled_p[TS_BLOCK] = true;
102 handled_p[TS_BINFO] = true;
103 handled_p[TS_STATEMENT_LIST] = true;
104 handled_p[TS_CONSTRUCTOR] = true;
105 handled_p[TS_OMP_CLAUSE] = true;
106 handled_p[TS_OPTIMIZATION] = true;
107 handled_p[TS_TARGET_OPTION] = true;
108 handled_p[TS_TRANSLATION_UNIT_DECL] = true;
110 /* Anything not marked above will trigger the following assertion.
111 If this assertion triggers, it means that there is a new TS_*
112 structure that should be handled by the streamer. */
113 for (i = 0; i < LAST_TS_ENUM; i++)
114 gcc_assert (handled_p[i]);
118 /* Helper for streamer_tree_cache_insert_1. Add T to CACHE->NODES at
119 slot IX. */
121 static void
122 streamer_tree_cache_add_to_node_array (struct streamer_tree_cache_d *cache,
123 unsigned ix, tree t, hashval_t hash)
125 /* We're either replacing an old element or appending consecutively. */
126 if (cache->nodes.exists ())
128 if (cache->nodes.length () == ix)
129 cache->nodes.safe_push (t);
130 else
131 cache->nodes[ix] = t;
133 if (cache->hashes.exists ())
135 if (cache->hashes.length () == ix)
136 cache->hashes.safe_push (hash);
137 else
138 cache->hashes[ix] = hash;
143 /* Helper for streamer_tree_cache_insert and streamer_tree_cache_insert_at.
144 CACHE, T, and IX_P are as in streamer_tree_cache_insert.
146 If INSERT_AT_NEXT_SLOT_P is true, T is inserted at the next available
147 slot in the cache. Otherwise, T is inserted at the position indicated
148 in *IX_P.
150 If T already existed in CACHE, return true. Otherwise,
151 return false. */
153 static bool
154 streamer_tree_cache_insert_1 (struct streamer_tree_cache_d *cache,
155 tree t, hashval_t hash, unsigned *ix_p,
156 bool insert_at_next_slot_p)
158 bool existed_p;
160 gcc_assert (t);
162 unsigned int &ix = cache->node_map->get_or_insert (t, &existed_p);
163 if (!existed_p)
165 /* Determine the next slot to use in the cache. */
166 if (insert_at_next_slot_p)
167 ix = cache->next_idx++;
168 else
169 ix = *ix_p;
171 streamer_tree_cache_add_to_node_array (cache, ix, t, hash);
173 else
175 if (!insert_at_next_slot_p && ix != *ix_p)
177 /* If the caller wants to insert T at a specific slot
178 location, and ENTRY->TO does not match *IX_P, add T to
179 the requested location slot. */
180 ix = *ix_p;
181 streamer_tree_cache_add_to_node_array (cache, ix, t, hash);
185 if (ix_p)
186 *ix_p = ix;
188 return existed_p;
192 /* Insert tree node T in CACHE. If T already existed in the cache
193 return true. Otherwise, return false.
195 If IX_P is non-null, update it with the index into the cache where
196 T has been stored. */
198 bool
199 streamer_tree_cache_insert (struct streamer_tree_cache_d *cache, tree t,
200 hashval_t hash, unsigned *ix_p)
202 return streamer_tree_cache_insert_1 (cache, t, hash, ix_p, true);
206 /* Replace the tree node with T in CACHE at slot IX. */
208 void
209 streamer_tree_cache_replace_tree (struct streamer_tree_cache_d *cache,
210 tree t, unsigned ix)
212 hashval_t hash = 0;
213 if (cache->hashes.exists ())
214 hash = streamer_tree_cache_get_hash (cache, ix);
215 if (!cache->node_map)
216 streamer_tree_cache_add_to_node_array (cache, ix, t, hash);
217 else
218 streamer_tree_cache_insert_1 (cache, t, hash, &ix, false);
222 /* Appends tree node T to CACHE, even if T already existed in it. */
224 void
225 streamer_tree_cache_append (struct streamer_tree_cache_d *cache,
226 tree t, hashval_t hash)
228 unsigned ix = cache->next_idx++;
229 if (!cache->node_map)
230 streamer_tree_cache_add_to_node_array (cache, ix, t, hash);
231 else
232 streamer_tree_cache_insert_1 (cache, t, hash, &ix, false);
235 /* Return true if tree node T exists in CACHE, otherwise false. If IX_P is
236 not NULL, write to *IX_P the index into the cache where T is stored
237 ((unsigned)-1 if T is not found). */
239 bool
240 streamer_tree_cache_lookup (struct streamer_tree_cache_d *cache, tree t,
241 unsigned *ix_p)
243 unsigned *slot;
244 bool retval;
245 unsigned ix;
247 gcc_assert (t);
249 slot = cache->node_map->get (t);
250 if (slot == NULL)
252 retval = false;
253 ix = -1;
255 else
257 retval = true;
258 ix = *slot;
261 if (ix_p)
262 *ix_p = ix;
264 return retval;
268 /* Record NODE in CACHE. */
270 static void
271 record_common_node (struct streamer_tree_cache_d *cache, tree node)
273 /* If we recursively end up at nodes we do not want to preload simply don't.
274 ??? We'd want to verify that this doesn't happen, or alternatively
275 do not recurse at all. */
276 if (node == char_type_node)
277 return;
279 gcc_checking_assert (node != boolean_type_node
280 && node != boolean_true_node
281 && node != boolean_false_node);
283 /* We have to make sure to fill exactly the same number of
284 elements for all frontends. That can include NULL trees.
285 As our hash table can't deal with zero entries we'll simply stream
286 a random other tree. A NULL tree never will be looked up so it
287 doesn't matter which tree we replace it with, just to be sure
288 use error_mark_node. */
289 if (!node)
290 node = error_mark_node;
292 /* ??? FIXME, devise a better hash value. But the hash needs to be equal
293 for all frontend and lto1 invocations. So just use the position
294 in the cache as hash value. */
295 streamer_tree_cache_append (cache, node, cache->nodes.length ());
297 if (POINTER_TYPE_P (node)
298 || TREE_CODE (node) == COMPLEX_TYPE
299 || TREE_CODE (node) == ARRAY_TYPE)
300 record_common_node (cache, TREE_TYPE (node));
301 else if (TREE_CODE (node) == RECORD_TYPE)
303 /* The FIELD_DECLs of structures should be shared, so that every
304 COMPONENT_REF uses the same tree node when referencing a field.
305 Pointer equality between FIELD_DECLs is used by the alias
306 machinery to compute overlapping component references (see
307 nonoverlapping_component_refs_p and
308 nonoverlapping_component_refs_of_decl_p). */
309 for (tree f = TYPE_FIELDS (node); f; f = TREE_CHAIN (f))
310 record_common_node (cache, f);
315 /* Preload common nodes into CACHE and make sure they are merged
316 properly according to the gimple type table. */
318 static void
319 preload_common_nodes (struct streamer_tree_cache_d *cache)
321 unsigned i;
323 for (i = 0; i < itk_none; i++)
324 /* Skip itk_char. char_type_node is dependent on -f[un]signed-char. */
325 if (i != itk_char)
326 record_common_node (cache, integer_types[i]);
328 for (i = 0; i < stk_type_kind_last; i++)
329 record_common_node (cache, sizetype_tab[i]);
331 for (i = 0; i < TI_MAX; i++)
332 /* Skip boolean type and constants, they are frontend dependent. */
333 if (i != TI_BOOLEAN_TYPE
334 && i != TI_BOOLEAN_FALSE
335 && i != TI_BOOLEAN_TRUE
336 /* MAIN_IDENTIFIER is not always initialized by Fortran FE. */
337 && i != TI_MAIN_IDENTIFIER
338 /* PID_TYPE is initialized only by C family front-ends. */
339 && i != TI_PID_TYPE
340 /* Skip optimization and target option nodes; they depend on flags. */
341 && i != TI_OPTIMIZATION_DEFAULT
342 && i != TI_OPTIMIZATION_CURRENT
343 && i != TI_TARGET_OPTION_DEFAULT
344 && i != TI_TARGET_OPTION_CURRENT
345 && i != TI_CURRENT_TARGET_PRAGMA
346 && i != TI_CURRENT_OPTIMIZE_PRAGMA
347 /* Skip va_list* related nodes if offloading. For native LTO
348 we want them to be merged for the stdarg pass, for offloading
349 they might not be identical between host and offloading target. */
350 && (!lto_stream_offload_p
351 || (i != TI_VA_LIST_TYPE
352 && i != TI_VA_LIST_GPR_COUNTER_FIELD
353 && i != TI_VA_LIST_FPR_COUNTER_FIELD)))
354 record_common_node (cache, global_trees[i]);
358 /* Create a cache of pickled nodes. */
360 struct streamer_tree_cache_d *
361 streamer_tree_cache_create (bool with_hashes, bool with_map, bool with_vec)
363 struct streamer_tree_cache_d *cache;
365 cache = XCNEW (struct streamer_tree_cache_d);
367 if (with_map)
368 cache->node_map = new hash_map<tree, unsigned> (251);
369 cache->next_idx = 0;
370 if (with_vec)
371 cache->nodes.create (165);
372 if (with_hashes)
373 cache->hashes.create (165);
375 /* Load all the well-known tree nodes that are always created by
376 the compiler on startup. This prevents writing them out
377 unnecessarily. */
378 preload_common_nodes (cache);
380 return cache;
384 /* Delete the streamer cache C. */
386 void
387 streamer_tree_cache_delete (struct streamer_tree_cache_d *c)
389 if (c == NULL)
390 return;
392 delete c->node_map;
393 c->node_map = NULL;
394 c->nodes.release ();
395 c->hashes.release ();
396 free (c);