2013-05-30 Ed Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / gimple-streamer-in.c
blob03fbe91bbe2fbaf68c93f4101e23f04e8f8bd8f4
1 /* Routines for reading GIMPLE from a file stream.
3 Copyright (C) 2011-2013 Free Software Foundation, Inc.
4 Contributed by Diego Novillo <dnovillo@google.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "diagnostic.h"
26 #include "tree.h"
27 #include "tree-flow.h"
28 #include "data-streamer.h"
29 #include "tree-streamer.h"
30 #include "gimple-streamer.h"
31 #include "value-prof.h"
33 /* Read a PHI function for basic block BB in function FN. DATA_IN is
34 the file being read. IB is the input block to use for reading. */
36 static gimple
37 input_phi (struct lto_input_block *ib, basic_block bb, struct data_in *data_in,
38 struct function *fn)
40 unsigned HOST_WIDE_INT ix;
41 tree phi_result;
42 int i, len;
43 gimple result;
45 ix = streamer_read_uhwi (ib);
46 phi_result = (*SSANAMES (fn))[ix];
47 len = EDGE_COUNT (bb->preds);
48 result = create_phi_node (phi_result, bb);
50 /* We have to go through a lookup process here because the preds in the
51 reconstructed graph are generally in a different order than they
52 were in the original program. */
53 for (i = 0; i < len; i++)
55 tree def = stream_read_tree (ib, data_in);
56 int src_index = streamer_read_uhwi (ib);
57 bitpack_d bp = streamer_read_bitpack (ib);
58 location_t arg_loc = stream_input_location (&bp, data_in);
59 basic_block sbb = BASIC_BLOCK_FOR_FUNCTION (fn, src_index);
61 edge e = NULL;
62 int j;
64 for (j = 0; j < len; j++)
65 if (EDGE_PRED (bb, j)->src == sbb)
67 e = EDGE_PRED (bb, j);
68 break;
71 add_phi_arg (result, def, e, arg_loc);
74 return result;
78 /* Read a statement with tag TAG in function FN from block IB using
79 descriptors in DATA_IN. */
81 static gimple
82 input_gimple_stmt (struct lto_input_block *ib, struct data_in *data_in,
83 struct function *fn, enum LTO_tags tag)
85 gimple stmt;
86 enum gimple_code code;
87 unsigned HOST_WIDE_INT num_ops;
88 size_t i;
89 struct bitpack_d bp;
90 bool has_hist;
92 code = lto_tag_to_gimple_code (tag);
94 /* Read the tuple header. */
95 bp = streamer_read_bitpack (ib);
96 num_ops = bp_unpack_var_len_unsigned (&bp);
97 stmt = gimple_alloc (code, num_ops);
98 stmt->gsbase.no_warning = bp_unpack_value (&bp, 1);
99 if (is_gimple_assign (stmt))
100 stmt->gsbase.nontemporal_move = bp_unpack_value (&bp, 1);
101 stmt->gsbase.has_volatile_ops = bp_unpack_value (&bp, 1);
102 has_hist = bp_unpack_value (&bp, 1);
103 stmt->gsbase.subcode = bp_unpack_var_len_unsigned (&bp);
105 /* Read location information. */
106 gimple_set_location (stmt, stream_input_location (&bp, data_in));
108 /* Read lexical block reference. */
109 gimple_set_block (stmt, stream_read_tree (ib, data_in));
111 /* Read in all the operands. */
112 switch (code)
114 case GIMPLE_RESX:
115 gimple_resx_set_region (stmt, streamer_read_hwi (ib));
116 break;
118 case GIMPLE_EH_MUST_NOT_THROW:
119 gimple_eh_must_not_throw_set_fndecl (stmt, stream_read_tree (ib, data_in));
120 break;
122 case GIMPLE_EH_DISPATCH:
123 gimple_eh_dispatch_set_region (stmt, streamer_read_hwi (ib));
124 break;
126 case GIMPLE_ASM:
128 /* FIXME lto. Move most of this into a new gimple_asm_set_string(). */
129 tree str;
130 stmt->gimple_asm.ni = streamer_read_uhwi (ib);
131 stmt->gimple_asm.no = streamer_read_uhwi (ib);
132 stmt->gimple_asm.nc = streamer_read_uhwi (ib);
133 stmt->gimple_asm.nl = streamer_read_uhwi (ib);
134 str = streamer_read_string_cst (data_in, ib);
135 stmt->gimple_asm.string = TREE_STRING_POINTER (str);
137 /* Fallthru */
139 case GIMPLE_ASSIGN:
140 case GIMPLE_CALL:
141 case GIMPLE_RETURN:
142 case GIMPLE_SWITCH:
143 case GIMPLE_LABEL:
144 case GIMPLE_COND:
145 case GIMPLE_GOTO:
146 case GIMPLE_DEBUG:
147 for (i = 0; i < num_ops; i++)
149 tree *opp, op = stream_read_tree (ib, data_in);
150 gimple_set_op (stmt, i, op);
151 if (!op)
152 continue;
154 opp = gimple_op_ptr (stmt, i);
155 if (TREE_CODE (*opp) == ADDR_EXPR)
156 opp = &TREE_OPERAND (*opp, 0);
157 while (handled_component_p (*opp))
159 if (TREE_CODE (*opp) == COMPONENT_REF)
161 /* Fixup FIELD_DECLs in COMPONENT_REFs, they are not handled
162 by decl merging. */
163 tree field, type, tem;
164 tree closest_match = NULL_TREE;
165 field = TREE_OPERAND (*opp, 1);
166 type = DECL_CONTEXT (field);
167 for (tem = TYPE_FIELDS (type); tem; tem = TREE_CHAIN (tem))
169 if (TREE_CODE (tem) != FIELD_DECL)
170 continue;
171 if (tem == field)
172 break;
173 if (DECL_NONADDRESSABLE_P (tem)
174 == DECL_NONADDRESSABLE_P (field)
175 && gimple_compare_field_offset (tem, field))
177 if (types_compatible_p (TREE_TYPE (tem),
178 TREE_TYPE (field)))
179 break;
180 else
181 closest_match = tem;
184 /* In case of type mismatches across units we can fail
185 to unify some types and thus not find a proper
186 field-decl here. */
187 if (tem == NULL_TREE)
189 /* Thus, emit a ODR violation warning. */
190 if (warning_at (gimple_location (stmt), 0,
191 "use of type %<%E%> with two mismatching "
192 "declarations at field %<%E%>",
193 type, TREE_OPERAND (*opp, 1)))
195 if (TYPE_FIELDS (type))
196 inform (DECL_SOURCE_LOCATION (TYPE_FIELDS (type)),
197 "original type declared here");
198 inform (DECL_SOURCE_LOCATION (TREE_OPERAND (*opp, 1)),
199 "field in mismatching type declared here");
200 if (TYPE_NAME (TREE_TYPE (field))
201 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
202 == TYPE_DECL))
203 inform (DECL_SOURCE_LOCATION
204 (TYPE_NAME (TREE_TYPE (field))),
205 "type of field declared here");
206 if (closest_match
207 && TYPE_NAME (TREE_TYPE (closest_match))
208 && (TREE_CODE (TYPE_NAME
209 (TREE_TYPE (closest_match))) == TYPE_DECL))
210 inform (DECL_SOURCE_LOCATION
211 (TYPE_NAME (TREE_TYPE (closest_match))),
212 "type of mismatching field declared here");
214 /* And finally fixup the types. */
215 TREE_OPERAND (*opp, 0)
216 = build1 (VIEW_CONVERT_EXPR, type,
217 TREE_OPERAND (*opp, 0));
219 else
220 TREE_OPERAND (*opp, 1) = tem;
222 else if ((TREE_CODE (*opp) == ARRAY_REF
223 || TREE_CODE (*opp) == ARRAY_RANGE_REF)
224 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (*opp, 0)))
225 != ARRAY_TYPE))
227 /* And ARRAY_REFs to objects that had mismatched types
228 during symbol merging to avoid ICEs. */
229 TREE_OPERAND (*opp, 0)
230 = build1 (VIEW_CONVERT_EXPR,
231 build_array_type (TREE_TYPE (*opp), NULL_TREE),
232 TREE_OPERAND (*opp, 0));
235 opp = &TREE_OPERAND (*opp, 0);
237 /* At LTO output time we wrap all global decls in MEM_REFs to
238 allow seamless replacement with prevailing decls. Undo this
239 here if the prevailing decl allows for this.
240 ??? Maybe we should simply fold all stmts. */
241 if (TREE_CODE (*opp) == MEM_REF
242 && TREE_CODE (TREE_OPERAND (*opp, 0)) == ADDR_EXPR
243 && integer_zerop (TREE_OPERAND (*opp, 1))
244 && (TREE_THIS_VOLATILE (*opp)
245 == TREE_THIS_VOLATILE
246 (TREE_OPERAND (TREE_OPERAND (*opp, 0), 0)))
247 && !TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (TREE_OPERAND (*opp, 1)))
248 && (TREE_TYPE (*opp)
249 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (*opp, 1))))
250 && (TREE_TYPE (*opp)
251 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*opp, 0), 0))))
252 *opp = TREE_OPERAND (TREE_OPERAND (*opp, 0), 0);
254 if (is_gimple_call (stmt))
256 if (gimple_call_internal_p (stmt))
257 gimple_call_set_internal_fn
258 (stmt, streamer_read_enum (ib, internal_fn, IFN_LAST));
259 else
260 gimple_call_set_fntype (stmt, stream_read_tree (ib, data_in));
262 break;
264 case GIMPLE_NOP:
265 case GIMPLE_PREDICT:
266 break;
268 case GIMPLE_TRANSACTION:
269 gimple_transaction_set_label (stmt, stream_read_tree (ib, data_in));
270 break;
272 default:
273 internal_error ("bytecode stream: unknown GIMPLE statement tag %s",
274 lto_tag_name (tag));
277 /* Update the properties of symbols, SSA names and labels associated
278 with STMT. */
279 if (code == GIMPLE_ASSIGN || code == GIMPLE_CALL)
281 tree lhs = gimple_get_lhs (stmt);
282 if (lhs && TREE_CODE (lhs) == SSA_NAME)
283 SSA_NAME_DEF_STMT (lhs) = stmt;
285 else if (code == GIMPLE_LABEL)
286 gcc_assert (emit_label_in_global_context_p (gimple_label_label (stmt))
287 || DECL_CONTEXT (gimple_label_label (stmt)) == fn->decl);
288 else if (code == GIMPLE_ASM)
290 unsigned i;
292 for (i = 0; i < gimple_asm_noutputs (stmt); i++)
294 tree op = TREE_VALUE (gimple_asm_output_op (stmt, i));
295 if (TREE_CODE (op) == SSA_NAME)
296 SSA_NAME_DEF_STMT (op) = stmt;
300 /* Reset alias information. */
301 if (code == GIMPLE_CALL)
302 gimple_call_reset_alias_info (stmt);
304 /* Mark the statement modified so its operand vectors can be filled in. */
305 gimple_set_modified (stmt, true);
306 if (has_hist)
307 stream_in_histogram_value (ib, stmt);
309 return stmt;
313 /* Read a basic block with tag TAG from DATA_IN using input block IB.
314 FN is the function being processed. */
316 void
317 input_bb (struct lto_input_block *ib, enum LTO_tags tag,
318 struct data_in *data_in, struct function *fn,
319 int count_materialization_scale)
321 unsigned int index;
322 basic_block bb;
323 gimple_stmt_iterator bsi;
325 /* This routine assumes that CFUN is set to FN, as it needs to call
326 basic GIMPLE routines that use CFUN. */
327 gcc_assert (cfun == fn);
329 index = streamer_read_uhwi (ib);
330 bb = BASIC_BLOCK_FOR_FUNCTION (fn, index);
332 bb->count = apply_scale (streamer_read_gcov_count (ib),
333 count_materialization_scale);
334 bb->frequency = streamer_read_hwi (ib);
335 bb->flags = streamer_read_hwi (ib);
337 /* LTO_bb1 has statements. LTO_bb0 does not. */
338 if (tag == LTO_bb0)
339 return;
341 bsi = gsi_start_bb (bb);
342 tag = streamer_read_record_start (ib);
343 while (tag)
345 gimple stmt = input_gimple_stmt (ib, data_in, fn, tag);
346 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
348 /* After the statement, expect a 0 delimiter or the EH region
349 that the previous statement belongs to. */
350 tag = streamer_read_record_start (ib);
351 lto_tag_check_set (tag, 2, LTO_eh_region, LTO_null);
353 if (tag == LTO_eh_region)
355 HOST_WIDE_INT region = streamer_read_hwi (ib);
356 gcc_assert (region == (int) region);
357 add_stmt_to_eh_lp (stmt, region);
360 tag = streamer_read_record_start (ib);
363 tag = streamer_read_record_start (ib);
364 while (tag)
366 input_phi (ib, bb, data_in, fn);
367 tag = streamer_read_record_start (ib);