2015-06-23 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / gimple-streamer-in.c
blobf1ec3de2b6ae36cba5c2e81aaa9058017df6f3dd
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 "plugin-api.h"
49 #include "ipa-ref.h"
50 #include "cgraph.h"
51 #include "data-streamer.h"
52 #include "tree-streamer.h"
53 #include "gimple-streamer.h"
54 #include "value-prof.h"
56 /* Read a PHI function for basic block BB in function FN. DATA_IN is
57 the file being read. IB is the input block to use for reading. */
59 static gphi *
60 input_phi (struct lto_input_block *ib, basic_block bb, struct data_in *data_in,
61 struct function *fn)
63 unsigned HOST_WIDE_INT ix;
64 tree phi_result;
65 int i, len;
66 gphi *result;
68 ix = streamer_read_uhwi (ib);
69 phi_result = (*SSANAMES (fn))[ix];
70 len = EDGE_COUNT (bb->preds);
71 result = create_phi_node (phi_result, bb);
73 /* We have to go through a lookup process here because the preds in the
74 reconstructed graph are generally in a different order than they
75 were in the original program. */
76 for (i = 0; i < len; i++)
78 tree def = stream_read_tree (ib, data_in);
79 int src_index = streamer_read_uhwi (ib);
80 bitpack_d bp = streamer_read_bitpack (ib);
81 /* Do not cache a location - we do not have API to get pointer to the
82 location in PHI statement and we may trigger reallocation. */
83 location_t arg_loc = stream_input_location_now (&bp, data_in);
84 basic_block sbb = BASIC_BLOCK_FOR_FN (fn, src_index);
86 edge e = NULL;
87 int j;
89 for (j = 0; j < len; j++)
90 if (EDGE_PRED (bb, j)->src == sbb)
92 e = EDGE_PRED (bb, j);
93 break;
96 add_phi_arg (result, def, e, arg_loc);
99 return result;
103 /* Read a statement with tag TAG in function FN from block IB using
104 descriptors in DATA_IN. */
106 static gimple
107 input_gimple_stmt (struct lto_input_block *ib, struct data_in *data_in,
108 enum LTO_tags tag)
110 gimple stmt;
111 enum gimple_code code;
112 unsigned HOST_WIDE_INT num_ops;
113 size_t i;
114 struct bitpack_d bp;
115 bool has_hist;
117 code = lto_tag_to_gimple_code (tag);
119 /* Read the tuple header. */
120 bp = streamer_read_bitpack (ib);
121 num_ops = bp_unpack_var_len_unsigned (&bp);
122 stmt = gimple_alloc (code, num_ops);
123 stmt->no_warning = bp_unpack_value (&bp, 1);
124 if (is_gimple_assign (stmt))
125 stmt->nontemporal_move = bp_unpack_value (&bp, 1);
126 stmt->has_volatile_ops = bp_unpack_value (&bp, 1);
127 has_hist = bp_unpack_value (&bp, 1);
128 stmt->subcode = bp_unpack_var_len_unsigned (&bp);
130 /* Read location information. Caching here makes no sense until streamer
131 cache can handle the following gimple_set_block. */
132 gimple_set_location (stmt, stream_input_location_now (&bp, data_in));
134 /* Read lexical block reference. */
135 gimple_set_block (stmt, stream_read_tree (ib, data_in));
137 /* Read in all the operands. */
138 switch (code)
140 case GIMPLE_RESX:
141 gimple_resx_set_region (as_a <gresx *> (stmt),
142 streamer_read_hwi (ib));
143 break;
145 case GIMPLE_EH_MUST_NOT_THROW:
146 gimple_eh_must_not_throw_set_fndecl (
147 as_a <geh_mnt *> (stmt),
148 stream_read_tree (ib, data_in));
149 break;
151 case GIMPLE_EH_DISPATCH:
152 gimple_eh_dispatch_set_region (as_a <geh_dispatch *> (stmt),
153 streamer_read_hwi (ib));
154 break;
156 case GIMPLE_ASM:
158 /* FIXME lto. Move most of this into a new gimple_asm_set_string(). */
159 gasm *asm_stmt = as_a <gasm *> (stmt);
160 tree str;
161 asm_stmt->ni = streamer_read_uhwi (ib);
162 asm_stmt->no = streamer_read_uhwi (ib);
163 asm_stmt->nc = streamer_read_uhwi (ib);
164 asm_stmt->nl = streamer_read_uhwi (ib);
165 str = streamer_read_string_cst (data_in, ib);
166 asm_stmt->string = TREE_STRING_POINTER (str);
168 /* Fallthru */
170 case GIMPLE_ASSIGN:
171 case GIMPLE_CALL:
172 case GIMPLE_RETURN:
173 case GIMPLE_SWITCH:
174 case GIMPLE_LABEL:
175 case GIMPLE_COND:
176 case GIMPLE_GOTO:
177 case GIMPLE_DEBUG:
178 for (i = 0; i < num_ops; i++)
180 tree *opp, op = stream_read_tree (ib, data_in);
181 gimple_set_op (stmt, i, op);
182 if (!op)
183 continue;
185 opp = gimple_op_ptr (stmt, i);
186 if (TREE_CODE (*opp) == ADDR_EXPR)
187 opp = &TREE_OPERAND (*opp, 0);
188 while (handled_component_p (*opp))
189 opp = &TREE_OPERAND (*opp, 0);
190 /* At LTO output time we wrap all global decls in MEM_REFs to
191 allow seamless replacement with prevailing decls. Undo this
192 here if the prevailing decl allows for this.
193 ??? Maybe we should simply fold all stmts. */
194 if (TREE_CODE (*opp) == MEM_REF
195 && TREE_CODE (TREE_OPERAND (*opp, 0)) == ADDR_EXPR
196 && integer_zerop (TREE_OPERAND (*opp, 1))
197 && (TREE_THIS_VOLATILE (*opp)
198 == TREE_THIS_VOLATILE
199 (TREE_OPERAND (TREE_OPERAND (*opp, 0), 0)))
200 && !TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (TREE_OPERAND (*opp, 1)))
201 && (TREE_TYPE (*opp)
202 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (*opp, 1))))
203 && (TREE_TYPE (*opp)
204 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*opp, 0), 0))))
205 *opp = TREE_OPERAND (TREE_OPERAND (*opp, 0), 0);
207 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
209 if (gimple_call_internal_p (call_stmt))
210 gimple_call_set_internal_fn
211 (call_stmt, streamer_read_enum (ib, internal_fn, IFN_LAST));
212 else
213 gimple_call_set_fntype (call_stmt, stream_read_tree (ib, data_in));
215 break;
217 case GIMPLE_NOP:
218 case GIMPLE_PREDICT:
219 break;
221 case GIMPLE_TRANSACTION:
222 gimple_transaction_set_label (as_a <gtransaction *> (stmt),
223 stream_read_tree (ib, data_in));
224 break;
226 default:
227 internal_error ("bytecode stream: unknown GIMPLE statement tag %s",
228 lto_tag_name (tag));
231 /* Update the properties of symbols, SSA names and labels associated
232 with STMT. */
233 if (code == GIMPLE_ASSIGN || code == GIMPLE_CALL)
235 tree lhs = gimple_get_lhs (stmt);
236 if (lhs && TREE_CODE (lhs) == SSA_NAME)
237 SSA_NAME_DEF_STMT (lhs) = stmt;
239 else if (code == GIMPLE_ASM)
241 gasm *asm_stmt = as_a <gasm *> (stmt);
242 unsigned i;
244 for (i = 0; i < gimple_asm_noutputs (asm_stmt); i++)
246 tree op = TREE_VALUE (gimple_asm_output_op (asm_stmt, i));
247 if (TREE_CODE (op) == SSA_NAME)
248 SSA_NAME_DEF_STMT (op) = stmt;
252 /* Reset alias information. */
253 if (code == GIMPLE_CALL)
254 gimple_call_reset_alias_info (as_a <gcall *> (stmt));
256 /* Mark the statement modified so its operand vectors can be filled in. */
257 gimple_set_modified (stmt, true);
258 if (has_hist)
259 stream_in_histogram_value (ib, stmt);
261 return stmt;
265 /* Read a basic block with tag TAG from DATA_IN using input block IB.
266 FN is the function being processed. */
268 void
269 input_bb (struct lto_input_block *ib, enum LTO_tags tag,
270 struct data_in *data_in, struct function *fn,
271 int count_materialization_scale)
273 unsigned int index;
274 basic_block bb;
275 gimple_stmt_iterator bsi;
277 /* This routine assumes that CFUN is set to FN, as it needs to call
278 basic GIMPLE routines that use CFUN. */
279 gcc_assert (cfun == fn);
281 index = streamer_read_uhwi (ib);
282 bb = BASIC_BLOCK_FOR_FN (fn, index);
284 bb->count = apply_scale (streamer_read_gcov_count (ib),
285 count_materialization_scale);
286 bb->frequency = streamer_read_hwi (ib);
287 bb->flags = streamer_read_hwi (ib);
289 /* LTO_bb1 has statements. LTO_bb0 does not. */
290 if (tag == LTO_bb0)
291 return;
293 bsi = gsi_start_bb (bb);
294 tag = streamer_read_record_start (ib);
295 while (tag)
297 gimple stmt = input_gimple_stmt (ib, data_in, tag);
298 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
300 /* After the statement, expect a 0 delimiter or the EH region
301 that the previous statement belongs to. */
302 tag = streamer_read_record_start (ib);
303 lto_tag_check_set (tag, 2, LTO_eh_region, LTO_null);
305 if (tag == LTO_eh_region)
307 HOST_WIDE_INT region = streamer_read_hwi (ib);
308 gcc_assert (region == (int) region);
309 add_stmt_to_eh_lp (stmt, region);
312 tag = streamer_read_record_start (ib);
315 tag = streamer_read_record_start (ib);
316 while (tag)
318 input_phi (ib, bb, data_in, fn);
319 tag = streamer_read_record_start (ib);