2013-12-05 Marek Polacek <polacek@redhat.com>
[official-gcc.git] / gcc / tree-streamer.c
blobaa06b0bbfe694498b3cb306a67f556b407681e4a
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 "basic-block.h"
28 #include "tree-ssa-alias.h"
29 #include "internal-fn.h"
30 #include "gimple-expr.h"
31 #include "is-a.h"
32 #include "gimple.h"
33 #include "streamer-hooks.h"
34 #include "tree-streamer.h"
36 /* Check that all the TS_* structures handled by the streamer_write_* and
37 streamer_read_* routines are exactly ALL the structures defined in
38 treestruct.def. */
40 void
41 streamer_check_handled_ts_structures (void)
43 bool handled_p[LAST_TS_ENUM];
44 unsigned i;
46 memset (&handled_p, 0, sizeof (handled_p));
48 /* These are the TS_* structures that are either handled or
49 explicitly ignored by the streamer routines. */
50 handled_p[TS_BASE] = true;
51 handled_p[TS_TYPED] = true;
52 handled_p[TS_COMMON] = true;
53 handled_p[TS_INT_CST] = true;
54 handled_p[TS_REAL_CST] = true;
55 handled_p[TS_FIXED_CST] = true;
56 handled_p[TS_VECTOR] = true;
57 handled_p[TS_STRING] = true;
58 handled_p[TS_COMPLEX] = true;
59 handled_p[TS_IDENTIFIER] = true;
60 handled_p[TS_DECL_MINIMAL] = true;
61 handled_p[TS_DECL_COMMON] = true;
62 handled_p[TS_DECL_WRTL] = true;
63 handled_p[TS_DECL_NON_COMMON] = true;
64 handled_p[TS_DECL_WITH_VIS] = true;
65 handled_p[TS_FIELD_DECL] = true;
66 handled_p[TS_VAR_DECL] = true;
67 handled_p[TS_PARM_DECL] = true;
68 handled_p[TS_LABEL_DECL] = true;
69 handled_p[TS_RESULT_DECL] = true;
70 handled_p[TS_CONST_DECL] = true;
71 handled_p[TS_TYPE_DECL] = true;
72 handled_p[TS_FUNCTION_DECL] = true;
73 handled_p[TS_TYPE_COMMON] = true;
74 handled_p[TS_TYPE_WITH_LANG_SPECIFIC] = true;
75 handled_p[TS_TYPE_NON_COMMON] = true;
76 handled_p[TS_LIST] = true;
77 handled_p[TS_VEC] = true;
78 handled_p[TS_EXP] = true;
79 handled_p[TS_SSA_NAME] = true;
80 handled_p[TS_BLOCK] = true;
81 handled_p[TS_BINFO] = true;
82 handled_p[TS_STATEMENT_LIST] = true;
83 handled_p[TS_CONSTRUCTOR] = true;
84 handled_p[TS_OMP_CLAUSE] = true;
85 handled_p[TS_OPTIMIZATION] = true;
86 handled_p[TS_TARGET_OPTION] = true;
87 handled_p[TS_TRANSLATION_UNIT_DECL] = true;
89 /* Anything not marked above will trigger the following assertion.
90 If this assertion triggers, it means that there is a new TS_*
91 structure that should be handled by the streamer. */
92 for (i = 0; i < LAST_TS_ENUM; i++)
93 gcc_assert (handled_p[i]);
97 /* Helper for streamer_tree_cache_insert_1. Add T to CACHE->NODES at
98 slot IX. */
100 static void
101 streamer_tree_cache_add_to_node_array (struct streamer_tree_cache_d *cache,
102 unsigned ix, tree t, hashval_t hash)
104 /* Make sure we're either replacing an old element or
105 appending consecutively. */
106 gcc_assert (ix <= cache->nodes.length ());
108 if (ix == cache->nodes.length ())
110 cache->nodes.safe_push (t);
111 if (cache->hashes.exists ())
112 cache->hashes.safe_push (hash);
114 else
116 cache->nodes[ix] = t;
117 if (cache->hashes.exists ())
118 cache->hashes[ix] = hash;
123 /* Helper for streamer_tree_cache_insert and streamer_tree_cache_insert_at.
124 CACHE, T, and IX_P are as in streamer_tree_cache_insert.
126 If INSERT_AT_NEXT_SLOT_P is true, T is inserted at the next available
127 slot in the cache. Otherwise, T is inserted at the position indicated
128 in *IX_P.
130 If T already existed in CACHE, return true. Otherwise,
131 return false. */
133 static bool
134 streamer_tree_cache_insert_1 (struct streamer_tree_cache_d *cache,
135 tree t, hashval_t hash, unsigned *ix_p,
136 bool insert_at_next_slot_p)
138 unsigned *slot;
139 unsigned ix;
140 bool existed_p;
142 gcc_assert (t);
144 slot = cache->node_map->insert (t, &existed_p);
145 if (!existed_p)
147 /* Determine the next slot to use in the cache. */
148 if (insert_at_next_slot_p)
149 ix = cache->nodes.length ();
150 else
151 ix = *ix_p;
152 *slot = ix;
154 streamer_tree_cache_add_to_node_array (cache, ix, t, hash);
156 else
158 ix = *slot;
160 if (!insert_at_next_slot_p && ix != *ix_p)
162 /* If the caller wants to insert T at a specific slot
163 location, and ENTRY->TO does not match *IX_P, add T to
164 the requested location slot. */
165 ix = *ix_p;
166 streamer_tree_cache_add_to_node_array (cache, ix, t, hash);
167 *slot = ix;
171 if (ix_p)
172 *ix_p = ix;
174 return existed_p;
178 /* Insert tree node T in CACHE. If T already existed in the cache
179 return true. Otherwise, return false.
181 If IX_P is non-null, update it with the index into the cache where
182 T has been stored. */
184 bool
185 streamer_tree_cache_insert (struct streamer_tree_cache_d *cache, tree t,
186 hashval_t hash, unsigned *ix_p)
188 return streamer_tree_cache_insert_1 (cache, t, hash, ix_p, true);
192 /* Replace the tree node with T in CACHE at slot IX. */
194 void
195 streamer_tree_cache_replace_tree (struct streamer_tree_cache_d *cache,
196 tree t, unsigned ix)
198 hashval_t hash = 0;
199 if (cache->hashes.exists ())
200 hash = streamer_tree_cache_get_hash (cache, ix);
201 if (!cache->node_map)
202 streamer_tree_cache_add_to_node_array (cache, ix, t, hash);
203 else
204 streamer_tree_cache_insert_1 (cache, t, hash, &ix, false);
208 /* Appends tree node T to CACHE, even if T already existed in it. */
210 void
211 streamer_tree_cache_append (struct streamer_tree_cache_d *cache,
212 tree t, hashval_t hash)
214 unsigned ix = cache->nodes.length ();
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);
221 /* Return true if tree node T exists in CACHE, otherwise false. If IX_P is
222 not NULL, write to *IX_P the index into the cache where T is stored
223 ((unsigned)-1 if T is not found). */
225 bool
226 streamer_tree_cache_lookup (struct streamer_tree_cache_d *cache, tree t,
227 unsigned *ix_p)
229 unsigned *slot;
230 bool retval;
231 unsigned ix;
233 gcc_assert (t);
235 slot = cache->node_map->contains (t);
236 if (slot == NULL)
238 retval = false;
239 ix = -1;
241 else
243 retval = true;
244 ix = *slot;
247 if (ix_p)
248 *ix_p = ix;
250 return retval;
254 /* Record NODE in CACHE. */
256 static void
257 record_common_node (struct streamer_tree_cache_d *cache, tree node)
259 /* If we recursively end up at nodes we do not want to preload simply don't.
260 ??? We'd want to verify that this doesn't happen, or alternatively
261 do not recurse at all. */
262 if (node == char_type_node)
263 return;
265 gcc_checking_assert (node != boolean_type_node
266 && node != boolean_true_node
267 && node != boolean_false_node);
269 /* We have to make sure to fill exactly the same number of
270 elements for all frontends. That can include NULL trees.
271 As our hash table can't deal with zero entries we'll simply stream
272 a random other tree. A NULL tree never will be looked up so it
273 doesn't matter which tree we replace it with, just to be sure
274 use error_mark_node. */
275 if (!node)
276 node = error_mark_node;
278 /* ??? FIXME, devise a better hash value. But the hash needs to be equal
279 for all frontend and lto1 invocations. So just use the position
280 in the cache as hash value. */
281 streamer_tree_cache_append (cache, node, cache->nodes.length ());
283 if (POINTER_TYPE_P (node)
284 || TREE_CODE (node) == COMPLEX_TYPE
285 || TREE_CODE (node) == ARRAY_TYPE)
286 record_common_node (cache, TREE_TYPE (node));
287 else if (TREE_CODE (node) == RECORD_TYPE)
289 /* The FIELD_DECLs of structures should be shared, so that every
290 COMPONENT_REF uses the same tree node when referencing a field.
291 Pointer equality between FIELD_DECLs is used by the alias
292 machinery to compute overlapping component references (see
293 nonoverlapping_component_refs_p and
294 nonoverlapping_component_refs_of_decl_p). */
295 for (tree f = TYPE_FIELDS (node); f; f = TREE_CHAIN (f))
296 record_common_node (cache, f);
301 /* Preload common nodes into CACHE and make sure they are merged
302 properly according to the gimple type table. */
304 static void
305 preload_common_nodes (struct streamer_tree_cache_d *cache)
307 unsigned i;
309 for (i = 0; i < itk_none; i++)
310 /* Skip itk_char. char_type_node is dependent on -f[un]signed-char. */
311 if (i != itk_char)
312 record_common_node (cache, integer_types[i]);
314 for (i = 0; i < stk_type_kind_last; i++)
315 record_common_node (cache, sizetype_tab[i]);
317 for (i = 0; i < TI_MAX; i++)
318 /* Skip boolean type and constants, they are frontend dependent. */
319 if (i != TI_BOOLEAN_TYPE
320 && i != TI_BOOLEAN_FALSE
321 && i != TI_BOOLEAN_TRUE)
322 record_common_node (cache, global_trees[i]);
326 /* Create a cache of pickled nodes. */
328 struct streamer_tree_cache_d *
329 streamer_tree_cache_create (bool with_hashes, bool with_map)
331 struct streamer_tree_cache_d *cache;
333 cache = XCNEW (struct streamer_tree_cache_d);
335 if (with_map)
336 cache->node_map = new pointer_map<unsigned>;
337 cache->nodes.create (165);
338 if (with_hashes)
339 cache->hashes.create (165);
341 /* Load all the well-known tree nodes that are always created by
342 the compiler on startup. This prevents writing them out
343 unnecessarily. */
344 preload_common_nodes (cache);
346 return cache;
350 /* Delete the streamer cache C. */
352 void
353 streamer_tree_cache_delete (struct streamer_tree_cache_d *c)
355 if (c == NULL)
356 return;
358 if (c->node_map)
359 delete c->node_map;
360 c->nodes.release ();
361 c->hashes.release ();
362 free (c);