Import from savannah.gnu.org:
[official-gcc.git] / gcc / tree-streamer.c
blob8d532d9c250818781ed32f535d96d70ccb588a29
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 "streamer-hooks.h"
27 #include "tree-streamer.h"
29 /* Check that all the TS_* structures handled by the streamer_write_* and
30 streamer_read_* routines are exactly ALL the structures defined in
31 treestruct.def. */
33 void
34 streamer_check_handled_ts_structures (void)
36 bool handled_p[LAST_TS_ENUM];
37 unsigned i;
39 memset (&handled_p, 0, sizeof (handled_p));
41 /* These are the TS_* structures that are either handled or
42 explicitly ignored by the streamer routines. */
43 handled_p[TS_BASE] = true;
44 handled_p[TS_TYPED] = true;
45 handled_p[TS_COMMON] = true;
46 handled_p[TS_INT_CST] = true;
47 handled_p[TS_REAL_CST] = true;
48 handled_p[TS_FIXED_CST] = true;
49 handled_p[TS_VECTOR] = true;
50 handled_p[TS_STRING] = true;
51 handled_p[TS_COMPLEX] = true;
52 handled_p[TS_IDENTIFIER] = true;
53 handled_p[TS_DECL_MINIMAL] = true;
54 handled_p[TS_DECL_COMMON] = true;
55 handled_p[TS_DECL_WRTL] = true;
56 handled_p[TS_DECL_NON_COMMON] = true;
57 handled_p[TS_DECL_WITH_VIS] = true;
58 handled_p[TS_FIELD_DECL] = true;
59 handled_p[TS_VAR_DECL] = true;
60 handled_p[TS_PARM_DECL] = true;
61 handled_p[TS_LABEL_DECL] = true;
62 handled_p[TS_RESULT_DECL] = true;
63 handled_p[TS_CONST_DECL] = true;
64 handled_p[TS_TYPE_DECL] = true;
65 handled_p[TS_FUNCTION_DECL] = true;
66 handled_p[TS_TYPE_COMMON] = true;
67 handled_p[TS_TYPE_WITH_LANG_SPECIFIC] = true;
68 handled_p[TS_TYPE_NON_COMMON] = true;
69 handled_p[TS_LIST] = true;
70 handled_p[TS_VEC] = true;
71 handled_p[TS_EXP] = true;
72 handled_p[TS_SSA_NAME] = true;
73 handled_p[TS_BLOCK] = true;
74 handled_p[TS_BINFO] = true;
75 handled_p[TS_STATEMENT_LIST] = true;
76 handled_p[TS_CONSTRUCTOR] = true;
77 handled_p[TS_OMP_CLAUSE] = true;
78 handled_p[TS_OPTIMIZATION] = true;
79 handled_p[TS_TARGET_OPTION] = true;
80 handled_p[TS_TRANSLATION_UNIT_DECL] = true;
82 /* Anything not marked above will trigger the following assertion.
83 If this assertion triggers, it means that there is a new TS_*
84 structure that should be handled by the streamer. */
85 for (i = 0; i < LAST_TS_ENUM; i++)
86 gcc_assert (handled_p[i]);
90 /* Helper for streamer_tree_cache_insert_1. Add T to CACHE->NODES at
91 slot IX. */
93 static void
94 streamer_tree_cache_add_to_node_array (struct streamer_tree_cache_d *cache,
95 unsigned ix, tree t, hashval_t hash)
97 /* Make sure we're either replacing an old element or
98 appending consecutively. */
99 gcc_assert (ix <= cache->nodes.length ());
101 if (ix == cache->nodes.length ())
103 cache->nodes.safe_push (t);
104 if (cache->hashes.exists ())
105 cache->hashes.safe_push (hash);
107 else
109 cache->nodes[ix] = t;
110 if (cache->hashes.exists ())
111 cache->hashes[ix] = hash;
116 /* Helper for streamer_tree_cache_insert and streamer_tree_cache_insert_at.
117 CACHE, T, and IX_P are as in streamer_tree_cache_insert.
119 If INSERT_AT_NEXT_SLOT_P is true, T is inserted at the next available
120 slot in the cache. Otherwise, T is inserted at the position indicated
121 in *IX_P.
123 If T already existed in CACHE, return true. Otherwise,
124 return false. */
126 static bool
127 streamer_tree_cache_insert_1 (struct streamer_tree_cache_d *cache,
128 tree t, hashval_t hash, unsigned *ix_p,
129 bool insert_at_next_slot_p)
131 unsigned *slot;
132 unsigned ix;
133 bool existed_p;
135 gcc_assert (t);
137 slot = cache->node_map->insert (t, &existed_p);
138 if (!existed_p)
140 /* Determine the next slot to use in the cache. */
141 if (insert_at_next_slot_p)
142 ix = cache->nodes.length ();
143 else
144 ix = *ix_p;
145 *slot = ix;
147 streamer_tree_cache_add_to_node_array (cache, ix, t, hash);
149 else
151 ix = *slot;
153 if (!insert_at_next_slot_p && ix != *ix_p)
155 /* If the caller wants to insert T at a specific slot
156 location, and ENTRY->TO does not match *IX_P, add T to
157 the requested location slot. */
158 ix = *ix_p;
159 streamer_tree_cache_add_to_node_array (cache, ix, t, hash);
160 *slot = ix;
164 if (ix_p)
165 *ix_p = ix;
167 return existed_p;
171 /* Insert tree node T in CACHE. If T already existed in the cache
172 return true. Otherwise, return false.
174 If IX_P is non-null, update it with the index into the cache where
175 T has been stored. */
177 bool
178 streamer_tree_cache_insert (struct streamer_tree_cache_d *cache, tree t,
179 hashval_t hash, unsigned *ix_p)
181 return streamer_tree_cache_insert_1 (cache, t, hash, ix_p, true);
185 /* Replace the tree node with T in CACHE at slot IX. */
187 void
188 streamer_tree_cache_replace_tree (struct streamer_tree_cache_d *cache,
189 tree t, unsigned ix)
191 hashval_t hash = 0;
192 if (cache->hashes.exists ())
193 hash = streamer_tree_cache_get_hash (cache, ix);
194 if (!cache->node_map)
195 streamer_tree_cache_add_to_node_array (cache, ix, t, hash);
196 else
197 streamer_tree_cache_insert_1 (cache, t, hash, &ix, false);
201 /* Appends tree node T to CACHE, even if T already existed in it. */
203 void
204 streamer_tree_cache_append (struct streamer_tree_cache_d *cache,
205 tree t, hashval_t hash)
207 unsigned ix = cache->nodes.length ();
208 if (!cache->node_map)
209 streamer_tree_cache_add_to_node_array (cache, ix, t, hash);
210 else
211 streamer_tree_cache_insert_1 (cache, t, hash, &ix, false);
214 /* Return true if tree node T exists in CACHE, otherwise false. If IX_P is
215 not NULL, write to *IX_P the index into the cache where T is stored
216 ((unsigned)-1 if T is not found). */
218 bool
219 streamer_tree_cache_lookup (struct streamer_tree_cache_d *cache, tree t,
220 unsigned *ix_p)
222 unsigned *slot;
223 bool retval;
224 unsigned ix;
226 gcc_assert (t);
228 slot = cache->node_map->contains (t);
229 if (slot == NULL)
231 retval = false;
232 ix = -1;
234 else
236 retval = true;
237 ix = *slot;
240 if (ix_p)
241 *ix_p = ix;
243 return retval;
247 /* Record NODE in CACHE. */
249 static void
250 record_common_node (struct streamer_tree_cache_d *cache, tree node)
252 /* If we recursively end up at nodes we do not want to preload simply don't.
253 ??? We'd want to verify that this doesn't happen, or alternatively
254 do not recurse at all. */
255 if (node == char_type_node)
256 return;
258 gcc_checking_assert (node != boolean_type_node
259 && node != boolean_true_node
260 && node != boolean_false_node);
262 /* We have to make sure to fill exactly the same number of
263 elements for all frontends. That can include NULL trees.
264 As our hash table can't deal with zero entries we'll simply stream
265 a random other tree. A NULL tree never will be looked up so it
266 doesn't matter which tree we replace it with, just to be sure
267 use error_mark_node. */
268 if (!node)
269 node = error_mark_node;
271 /* ??? FIXME, devise a better hash value. But the hash needs to be equal
272 for all frontend and lto1 invocations. So just use the position
273 in the cache as hash value. */
274 streamer_tree_cache_append (cache, node, cache->nodes.length ());
276 if (POINTER_TYPE_P (node)
277 || TREE_CODE (node) == COMPLEX_TYPE
278 || TREE_CODE (node) == ARRAY_TYPE)
279 record_common_node (cache, TREE_TYPE (node));
280 else if (TREE_CODE (node) == RECORD_TYPE)
282 /* The FIELD_DECLs of structures should be shared, so that every
283 COMPONENT_REF uses the same tree node when referencing a field.
284 Pointer equality between FIELD_DECLs is used by the alias
285 machinery to compute overlapping component references (see
286 nonoverlapping_component_refs_p and
287 nonoverlapping_component_refs_of_decl_p). */
288 for (tree f = TYPE_FIELDS (node); f; f = TREE_CHAIN (f))
289 record_common_node (cache, f);
294 /* Preload common nodes into CACHE and make sure they are merged
295 properly according to the gimple type table. */
297 static void
298 preload_common_nodes (struct streamer_tree_cache_d *cache)
300 unsigned i;
302 for (i = 0; i < itk_none; i++)
303 /* Skip itk_char. char_type_node is dependent on -f[un]signed-char. */
304 if (i != itk_char)
305 record_common_node (cache, integer_types[i]);
307 for (i = 0; i < stk_type_kind_last; i++)
308 record_common_node (cache, sizetype_tab[i]);
310 for (i = 0; i < TI_MAX; i++)
311 /* Skip boolean type and constants, they are frontend dependent. */
312 if (i != TI_BOOLEAN_TYPE
313 && i != TI_BOOLEAN_FALSE
314 && i != TI_BOOLEAN_TRUE)
315 record_common_node (cache, global_trees[i]);
319 /* Create a cache of pickled nodes. */
321 struct streamer_tree_cache_d *
322 streamer_tree_cache_create (bool with_hashes, bool with_map)
324 struct streamer_tree_cache_d *cache;
326 cache = XCNEW (struct streamer_tree_cache_d);
328 if (with_map)
329 cache->node_map = new pointer_map<unsigned>;
330 cache->nodes.create (165);
331 if (with_hashes)
332 cache->hashes.create (165);
334 /* Load all the well-known tree nodes that are always created by
335 the compiler on startup. This prevents writing them out
336 unnecessarily. */
337 preload_common_nodes (cache);
339 return cache;
343 /* Delete the streamer cache C. */
345 void
346 streamer_tree_cache_delete (struct streamer_tree_cache_d *c)
348 if (c == NULL)
349 return;
351 if (c->node_map)
352 delete c->node_map;
353 c->nodes.release ();
354 c->hashes.release ();
355 free (c);