2013-11-21 Edward Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / tree-streamer.c
blob8534f4a9b8b4c6abe70a387dd17ed386c650fc6f
1 /* Miscellaneous utilities for tree streaming. Things that are used
2 in both input and output are here.
4 Copyright (C) 2011-2013 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 "tree.h"
27 #include "gimple.h"
28 #include "streamer-hooks.h"
29 #include "tree-streamer.h"
31 /* Check that all the TS_* structures handled by the streamer_write_* and
32 streamer_read_* routines are exactly ALL the structures defined in
33 treestruct.def. */
35 void
36 streamer_check_handled_ts_structures (void)
38 bool handled_p[LAST_TS_ENUM];
39 unsigned i;
41 memset (&handled_p, 0, sizeof (handled_p));
43 /* These are the TS_* structures that are either handled or
44 explicitly ignored by the streamer routines. */
45 handled_p[TS_BASE] = true;
46 handled_p[TS_TYPED] = true;
47 handled_p[TS_COMMON] = true;
48 handled_p[TS_INT_CST] = true;
49 handled_p[TS_REAL_CST] = true;
50 handled_p[TS_FIXED_CST] = true;
51 handled_p[TS_VECTOR] = true;
52 handled_p[TS_STRING] = true;
53 handled_p[TS_COMPLEX] = true;
54 handled_p[TS_IDENTIFIER] = true;
55 handled_p[TS_DECL_MINIMAL] = true;
56 handled_p[TS_DECL_COMMON] = true;
57 handled_p[TS_DECL_WRTL] = true;
58 handled_p[TS_DECL_NON_COMMON] = true;
59 handled_p[TS_DECL_WITH_VIS] = true;
60 handled_p[TS_FIELD_DECL] = true;
61 handled_p[TS_VAR_DECL] = true;
62 handled_p[TS_PARM_DECL] = true;
63 handled_p[TS_LABEL_DECL] = true;
64 handled_p[TS_RESULT_DECL] = true;
65 handled_p[TS_CONST_DECL] = true;
66 handled_p[TS_TYPE_DECL] = true;
67 handled_p[TS_FUNCTION_DECL] = true;
68 handled_p[TS_TYPE_COMMON] = true;
69 handled_p[TS_TYPE_WITH_LANG_SPECIFIC] = true;
70 handled_p[TS_TYPE_NON_COMMON] = true;
71 handled_p[TS_LIST] = true;
72 handled_p[TS_VEC] = true;
73 handled_p[TS_EXP] = true;
74 handled_p[TS_SSA_NAME] = true;
75 handled_p[TS_BLOCK] = true;
76 handled_p[TS_BINFO] = true;
77 handled_p[TS_STATEMENT_LIST] = true;
78 handled_p[TS_CONSTRUCTOR] = true;
79 handled_p[TS_OMP_CLAUSE] = true;
80 handled_p[TS_OPTIMIZATION] = true;
81 handled_p[TS_TARGET_OPTION] = true;
82 handled_p[TS_TRANSLATION_UNIT_DECL] = true;
84 /* Anything not marked above will trigger the following assertion.
85 If this assertion triggers, it means that there is a new TS_*
86 structure that should be handled by the streamer. */
87 for (i = 0; i < LAST_TS_ENUM; i++)
88 gcc_assert (handled_p[i]);
92 /* Helper for streamer_tree_cache_insert_1. Add T to CACHE->NODES at
93 slot IX. */
95 static void
96 streamer_tree_cache_add_to_node_array (struct streamer_tree_cache_d *cache,
97 unsigned ix, tree t, hashval_t hash)
99 /* Make sure we're either replacing an old element or
100 appending consecutively. */
101 gcc_assert (ix <= cache->nodes.length ());
103 if (ix == cache->nodes.length ())
105 cache->nodes.safe_push (t);
106 if (cache->hashes.exists ())
107 cache->hashes.safe_push (hash);
109 else
111 cache->nodes[ix] = t;
112 if (cache->hashes.exists ())
113 cache->hashes[ix] = hash;
118 /* Helper for streamer_tree_cache_insert and streamer_tree_cache_insert_at.
119 CACHE, T, and IX_P are as in streamer_tree_cache_insert.
121 If INSERT_AT_NEXT_SLOT_P is true, T is inserted at the next available
122 slot in the cache. Otherwise, T is inserted at the position indicated
123 in *IX_P.
125 If T already existed in CACHE, return true. Otherwise,
126 return false. */
128 static bool
129 streamer_tree_cache_insert_1 (struct streamer_tree_cache_d *cache,
130 tree t, hashval_t hash, unsigned *ix_p,
131 bool insert_at_next_slot_p)
133 unsigned *slot;
134 unsigned ix;
135 bool existed_p;
137 gcc_assert (t);
139 slot = cache->node_map->insert (t, &existed_p);
140 if (!existed_p)
142 /* Determine the next slot to use in the cache. */
143 if (insert_at_next_slot_p)
144 ix = cache->nodes.length ();
145 else
146 ix = *ix_p;
147 *slot = ix;
149 streamer_tree_cache_add_to_node_array (cache, ix, t, hash);
151 else
153 ix = *slot;
155 if (!insert_at_next_slot_p && ix != *ix_p)
157 /* If the caller wants to insert T at a specific slot
158 location, and ENTRY->TO does not match *IX_P, add T to
159 the requested location slot. */
160 ix = *ix_p;
161 streamer_tree_cache_add_to_node_array (cache, ix, t, hash);
162 *slot = ix;
166 if (ix_p)
167 *ix_p = ix;
169 return existed_p;
173 /* Insert tree node T in CACHE. If T already existed in the cache
174 return true. Otherwise, return false.
176 If IX_P is non-null, update it with the index into the cache where
177 T has been stored. */
179 bool
180 streamer_tree_cache_insert (struct streamer_tree_cache_d *cache, tree t,
181 hashval_t hash, unsigned *ix_p)
183 return streamer_tree_cache_insert_1 (cache, t, hash, ix_p, true);
187 /* Replace the tree node with T in CACHE at slot IX. */
189 void
190 streamer_tree_cache_replace_tree (struct streamer_tree_cache_d *cache,
191 tree t, unsigned ix)
193 hashval_t hash = 0;
194 if (cache->hashes.exists ())
195 hash = streamer_tree_cache_get_hash (cache, ix);
196 if (!cache->node_map)
197 streamer_tree_cache_add_to_node_array (cache, ix, t, hash);
198 else
199 streamer_tree_cache_insert_1 (cache, t, hash, &ix, false);
203 /* Appends tree node T to CACHE, even if T already existed in it. */
205 void
206 streamer_tree_cache_append (struct streamer_tree_cache_d *cache,
207 tree t, hashval_t hash)
209 unsigned ix = cache->nodes.length ();
210 if (!cache->node_map)
211 streamer_tree_cache_add_to_node_array (cache, ix, t, hash);
212 else
213 streamer_tree_cache_insert_1 (cache, t, hash, &ix, false);
216 /* Return true if tree node T exists in CACHE, otherwise false. If IX_P is
217 not NULL, write to *IX_P the index into the cache where T is stored
218 ((unsigned)-1 if T is not found). */
220 bool
221 streamer_tree_cache_lookup (struct streamer_tree_cache_d *cache, tree t,
222 unsigned *ix_p)
224 unsigned *slot;
225 bool retval;
226 unsigned ix;
228 gcc_assert (t);
230 slot = cache->node_map->contains (t);
231 if (slot == NULL)
233 retval = false;
234 ix = -1;
236 else
238 retval = true;
239 ix = *slot;
242 if (ix_p)
243 *ix_p = ix;
245 return retval;
249 /* Record NODE in CACHE. */
251 static void
252 record_common_node (struct streamer_tree_cache_d *cache, tree node)
254 /* If we recursively end up at nodes we do not want to preload simply don't.
255 ??? We'd want to verify that this doesn't happen, or alternatively
256 do not recurse at all. */
257 if (node == char_type_node)
258 return;
260 gcc_checking_assert (node != boolean_type_node
261 && node != boolean_true_node
262 && node != boolean_false_node);
264 /* We have to make sure to fill exactly the same number of
265 elements for all frontends. That can include NULL trees.
266 As our hash table can't deal with zero entries we'll simply stream
267 a random other tree. A NULL tree never will be looked up so it
268 doesn't matter which tree we replace it with, just to be sure
269 use error_mark_node. */
270 if (!node)
271 node = error_mark_node;
273 /* ??? FIXME, devise a better hash value. But the hash needs to be equal
274 for all frontend and lto1 invocations. So just use the position
275 in the cache as hash value. */
276 streamer_tree_cache_append (cache, node, cache->nodes.length ());
278 if (POINTER_TYPE_P (node)
279 || TREE_CODE (node) == COMPLEX_TYPE
280 || TREE_CODE (node) == ARRAY_TYPE)
281 record_common_node (cache, TREE_TYPE (node));
282 else if (TREE_CODE (node) == RECORD_TYPE)
284 /* The FIELD_DECLs of structures should be shared, so that every
285 COMPONENT_REF uses the same tree node when referencing a field.
286 Pointer equality between FIELD_DECLs is used by the alias
287 machinery to compute overlapping component references (see
288 nonoverlapping_component_refs_p and
289 nonoverlapping_component_refs_of_decl_p). */
290 for (tree f = TYPE_FIELDS (node); f; f = TREE_CHAIN (f))
291 record_common_node (cache, f);
296 /* Preload common nodes into CACHE and make sure they are merged
297 properly according to the gimple type table. */
299 static void
300 preload_common_nodes (struct streamer_tree_cache_d *cache)
302 unsigned i;
304 for (i = 0; i < itk_none; i++)
305 /* Skip itk_char. char_type_node is dependent on -f[un]signed-char. */
306 if (i != itk_char)
307 record_common_node (cache, integer_types[i]);
309 for (i = 0; i < stk_type_kind_last; i++)
310 record_common_node (cache, sizetype_tab[i]);
312 for (i = 0; i < TI_MAX; i++)
313 /* Skip boolean type and constants, they are frontend dependent. */
314 if (i != TI_BOOLEAN_TYPE
315 && i != TI_BOOLEAN_FALSE
316 && i != TI_BOOLEAN_TRUE)
317 record_common_node (cache, global_trees[i]);
321 /* Create a cache of pickled nodes. */
323 struct streamer_tree_cache_d *
324 streamer_tree_cache_create (bool with_hashes, bool with_map)
326 struct streamer_tree_cache_d *cache;
328 cache = XCNEW (struct streamer_tree_cache_d);
330 if (with_map)
331 cache->node_map = new pointer_map<unsigned>;
332 cache->nodes.create (165);
333 if (with_hashes)
334 cache->hashes.create (165);
336 /* Load all the well-known tree nodes that are always created by
337 the compiler on startup. This prevents writing them out
338 unnecessarily. */
339 preload_common_nodes (cache);
341 return cache;
345 /* Delete the streamer cache C. */
347 void
348 streamer_tree_cache_delete (struct streamer_tree_cache_d *c)
350 if (c == NULL)
351 return;
353 if (c->node_map)
354 delete c->node_map;
355 c->nodes.release ();
356 c->hashes.release ();
357 free (c);