/cp
[official-gcc.git] / gcc / gimple-streamer-in.c
blob6081c5b12b7d57ddff3f06c1e17838b5a2719447
1 /* Routines for reading GIMPLE from a file stream.
3 Copyright (C) 2011-2015 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 "alias.h"
27 #include "symtab.h"
28 #include "options.h"
29 #include "tree.h"
30 #include "fold-const.h"
31 #include "predict.h"
32 #include "tm.h"
33 #include "hard-reg-set.h"
34 #include "function.h"
35 #include "dominance.h"
36 #include "cfg.h"
37 #include "basic-block.h"
38 #include "tree-ssa-alias.h"
39 #include "internal-fn.h"
40 #include "tree-eh.h"
41 #include "gimple-expr.h"
42 #include "gimple.h"
43 #include "gimple-iterator.h"
44 #include "gimple-ssa.h"
45 #include "tree-phinodes.h"
46 #include "stringpool.h"
47 #include "tree-ssanames.h"
48 #include "cgraph.h"
49 #include "data-streamer.h"
50 #include "tree-streamer.h"
51 #include "gimple-streamer.h"
52 #include "value-prof.h"
54 /* Read a PHI function for basic block BB in function FN. DATA_IN is
55 the file being read. IB is the input block to use for reading. */
57 static gphi *
58 input_phi (struct lto_input_block *ib, basic_block bb, struct data_in *data_in,
59 struct function *fn)
61 unsigned HOST_WIDE_INT ix;
62 tree phi_result;
63 int i, len;
64 gphi *result;
66 ix = streamer_read_uhwi (ib);
67 phi_result = (*SSANAMES (fn))[ix];
68 len = EDGE_COUNT (bb->preds);
69 result = create_phi_node (phi_result, bb);
71 /* We have to go through a lookup process here because the preds in the
72 reconstructed graph are generally in a different order than they
73 were in the original program. */
74 for (i = 0; i < len; i++)
76 tree def = stream_read_tree (ib, data_in);
77 int src_index = streamer_read_uhwi (ib);
78 bitpack_d bp = streamer_read_bitpack (ib);
79 /* Do not cache a location - we do not have API to get pointer to the
80 location in PHI statement and we may trigger reallocation. */
81 location_t arg_loc = stream_input_location_now (&bp, data_in);
82 basic_block sbb = BASIC_BLOCK_FOR_FN (fn, src_index);
84 edge e = NULL;
85 int j;
87 for (j = 0; j < len; j++)
88 if (EDGE_PRED (bb, j)->src == sbb)
90 e = EDGE_PRED (bb, j);
91 break;
94 add_phi_arg (result, def, e, arg_loc);
97 return result;
101 /* Read a statement with tag TAG in function FN from block IB using
102 descriptors in DATA_IN. */
104 static gimple
105 input_gimple_stmt (struct lto_input_block *ib, struct data_in *data_in,
106 enum LTO_tags tag)
108 gimple stmt;
109 enum gimple_code code;
110 unsigned HOST_WIDE_INT num_ops;
111 size_t i;
112 struct bitpack_d bp;
113 bool has_hist;
115 code = lto_tag_to_gimple_code (tag);
117 /* Read the tuple header. */
118 bp = streamer_read_bitpack (ib);
119 num_ops = bp_unpack_var_len_unsigned (&bp);
120 stmt = gimple_alloc (code, num_ops);
121 stmt->no_warning = bp_unpack_value (&bp, 1);
122 if (is_gimple_assign (stmt))
123 stmt->nontemporal_move = bp_unpack_value (&bp, 1);
124 stmt->has_volatile_ops = bp_unpack_value (&bp, 1);
125 has_hist = bp_unpack_value (&bp, 1);
126 stmt->subcode = bp_unpack_var_len_unsigned (&bp);
128 /* Read location information. Caching here makes no sense until streamer
129 cache can handle the following gimple_set_block. */
130 gimple_set_location (stmt, stream_input_location_now (&bp, data_in));
132 /* Read lexical block reference. */
133 gimple_set_block (stmt, stream_read_tree (ib, data_in));
135 /* Read in all the operands. */
136 switch (code)
138 case GIMPLE_RESX:
139 gimple_resx_set_region (as_a <gresx *> (stmt),
140 streamer_read_hwi (ib));
141 break;
143 case GIMPLE_EH_MUST_NOT_THROW:
144 gimple_eh_must_not_throw_set_fndecl (
145 as_a <geh_mnt *> (stmt),
146 stream_read_tree (ib, data_in));
147 break;
149 case GIMPLE_EH_DISPATCH:
150 gimple_eh_dispatch_set_region (as_a <geh_dispatch *> (stmt),
151 streamer_read_hwi (ib));
152 break;
154 case GIMPLE_ASM:
156 /* FIXME lto. Move most of this into a new gimple_asm_set_string(). */
157 gasm *asm_stmt = as_a <gasm *> (stmt);
158 tree str;
159 asm_stmt->ni = streamer_read_uhwi (ib);
160 asm_stmt->no = streamer_read_uhwi (ib);
161 asm_stmt->nc = streamer_read_uhwi (ib);
162 asm_stmt->nl = streamer_read_uhwi (ib);
163 str = streamer_read_string_cst (data_in, ib);
164 asm_stmt->string = TREE_STRING_POINTER (str);
166 /* Fallthru */
168 case GIMPLE_ASSIGN:
169 case GIMPLE_CALL:
170 case GIMPLE_RETURN:
171 case GIMPLE_SWITCH:
172 case GIMPLE_LABEL:
173 case GIMPLE_COND:
174 case GIMPLE_GOTO:
175 case GIMPLE_DEBUG:
176 for (i = 0; i < num_ops; i++)
178 tree *opp, op = stream_read_tree (ib, data_in);
179 gimple_set_op (stmt, i, op);
180 if (!op)
181 continue;
183 opp = gimple_op_ptr (stmt, i);
184 if (TREE_CODE (*opp) == ADDR_EXPR)
185 opp = &TREE_OPERAND (*opp, 0);
186 while (handled_component_p (*opp))
187 opp = &TREE_OPERAND (*opp, 0);
188 /* At LTO output time we wrap all global decls in MEM_REFs to
189 allow seamless replacement with prevailing decls. Undo this
190 here if the prevailing decl allows for this.
191 ??? Maybe we should simply fold all stmts. */
192 if (TREE_CODE (*opp) == MEM_REF
193 && TREE_CODE (TREE_OPERAND (*opp, 0)) == ADDR_EXPR
194 && integer_zerop (TREE_OPERAND (*opp, 1))
195 && (TREE_THIS_VOLATILE (*opp)
196 == TREE_THIS_VOLATILE
197 (TREE_OPERAND (TREE_OPERAND (*opp, 0), 0)))
198 && !TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (TREE_OPERAND (*opp, 1)))
199 && (TREE_TYPE (*opp)
200 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (*opp, 1))))
201 && (TREE_TYPE (*opp)
202 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*opp, 0), 0))))
203 *opp = TREE_OPERAND (TREE_OPERAND (*opp, 0), 0);
205 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
207 if (gimple_call_internal_p (call_stmt))
208 gimple_call_set_internal_fn
209 (call_stmt, streamer_read_enum (ib, internal_fn, IFN_LAST));
210 else
211 gimple_call_set_fntype (call_stmt, stream_read_tree (ib, data_in));
213 break;
215 case GIMPLE_NOP:
216 case GIMPLE_PREDICT:
217 break;
219 case GIMPLE_TRANSACTION:
220 gimple_transaction_set_label (as_a <gtransaction *> (stmt),
221 stream_read_tree (ib, data_in));
222 break;
224 default:
225 internal_error ("bytecode stream: unknown GIMPLE statement tag %s",
226 lto_tag_name (tag));
229 /* Update the properties of symbols, SSA names and labels associated
230 with STMT. */
231 if (code == GIMPLE_ASSIGN || code == GIMPLE_CALL)
233 tree lhs = gimple_get_lhs (stmt);
234 if (lhs && TREE_CODE (lhs) == SSA_NAME)
235 SSA_NAME_DEF_STMT (lhs) = stmt;
237 else if (code == GIMPLE_ASM)
239 gasm *asm_stmt = as_a <gasm *> (stmt);
240 unsigned i;
242 for (i = 0; i < gimple_asm_noutputs (asm_stmt); i++)
244 tree op = TREE_VALUE (gimple_asm_output_op (asm_stmt, i));
245 if (TREE_CODE (op) == SSA_NAME)
246 SSA_NAME_DEF_STMT (op) = stmt;
250 /* Reset alias information. */
251 if (code == GIMPLE_CALL)
252 gimple_call_reset_alias_info (as_a <gcall *> (stmt));
254 /* Mark the statement modified so its operand vectors can be filled in. */
255 gimple_set_modified (stmt, true);
256 if (has_hist)
257 stream_in_histogram_value (ib, stmt);
259 return stmt;
263 /* Read a basic block with tag TAG from DATA_IN using input block IB.
264 FN is the function being processed. */
266 void
267 input_bb (struct lto_input_block *ib, enum LTO_tags tag,
268 struct data_in *data_in, struct function *fn,
269 int count_materialization_scale)
271 unsigned int index;
272 basic_block bb;
273 gimple_stmt_iterator bsi;
275 /* This routine assumes that CFUN is set to FN, as it needs to call
276 basic GIMPLE routines that use CFUN. */
277 gcc_assert (cfun == fn);
279 index = streamer_read_uhwi (ib);
280 bb = BASIC_BLOCK_FOR_FN (fn, index);
282 bb->count = apply_scale (streamer_read_gcov_count (ib),
283 count_materialization_scale);
284 bb->frequency = streamer_read_hwi (ib);
285 bb->flags = streamer_read_hwi (ib);
287 /* LTO_bb1 has statements. LTO_bb0 does not. */
288 if (tag == LTO_bb0)
289 return;
291 bsi = gsi_start_bb (bb);
292 tag = streamer_read_record_start (ib);
293 while (tag)
295 gimple stmt = input_gimple_stmt (ib, data_in, tag);
296 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
298 /* After the statement, expect a 0 delimiter or the EH region
299 that the previous statement belongs to. */
300 tag = streamer_read_record_start (ib);
301 lto_tag_check_set (tag, 2, LTO_eh_region, LTO_null);
303 if (tag == LTO_eh_region)
305 HOST_WIDE_INT region = streamer_read_hwi (ib);
306 gcc_assert (region == (int) region);
307 add_stmt_to_eh_lp (stmt, region);
310 tag = streamer_read_record_start (ib);
313 tag = streamer_read_record_start (ib);
314 while (tag)
316 input_phi (ib, bb, data_in, fn);
317 tag = streamer_read_record_start (ib);