ggcplug.c: Shuffle includes to include gcc-plugin.h earlier.
[gcc.git] / gcc / gimple-streamer-in.c
1 /* Routines for reading GIMPLE from a file stream.
2
3 Copyright (C) 2011-2014 Free Software Foundation, Inc.
4 Contributed by Diego Novillo <dnovillo@google.com>
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "diagnostic.h"
26 #include "tree.h"
27 #include "predict.h"
28 #include "vec.h"
29 #include "hashtab.h"
30 #include "hash-set.h"
31 #include "machmode.h"
32 #include "tm.h"
33 #include "hard-reg-set.h"
34 #include "input.h"
35 #include "function.h"
36 #include "dominance.h"
37 #include "cfg.h"
38 #include "basic-block.h"
39 #include "tree-ssa-alias.h"
40 #include "internal-fn.h"
41 #include "tree-eh.h"
42 #include "gimple-expr.h"
43 #include "is-a.h"
44 #include "gimple.h"
45 #include "gimple-iterator.h"
46 #include "gimple-ssa.h"
47 #include "tree-phinodes.h"
48 #include "stringpool.h"
49 #include "tree-ssanames.h"
50 #include "data-streamer.h"
51 #include "tree-streamer.h"
52 #include "gimple-streamer.h"
53 #include "value-prof.h"
54
55 /* Read a PHI function for basic block BB in function FN. DATA_IN is
56 the file being read. IB is the input block to use for reading. */
57
58 static gimple
59 input_phi (struct lto_input_block *ib, basic_block bb, struct data_in *data_in,
60 struct function *fn)
61 {
62 unsigned HOST_WIDE_INT ix;
63 tree phi_result;
64 int i, len;
65 gimple result;
66
67 ix = streamer_read_uhwi (ib);
68 phi_result = (*SSANAMES (fn))[ix];
69 len = EDGE_COUNT (bb->preds);
70 result = create_phi_node (phi_result, bb);
71
72 /* We have to go through a lookup process here because the preds in the
73 reconstructed graph are generally in a different order than they
74 were in the original program. */
75 for (i = 0; i < len; i++)
76 {
77 tree def = stream_read_tree (ib, data_in);
78 int src_index = streamer_read_uhwi (ib);
79 bitpack_d bp = streamer_read_bitpack (ib);
80 location_t arg_loc = stream_input_location (&bp, data_in);
81 basic_block sbb = BASIC_BLOCK_FOR_FN (fn, src_index);
82
83 edge e = NULL;
84 int j;
85
86 for (j = 0; j < len; j++)
87 if (EDGE_PRED (bb, j)->src == sbb)
88 {
89 e = EDGE_PRED (bb, j);
90 break;
91 }
92
93 add_phi_arg (result, def, e, arg_loc);
94 }
95
96 return result;
97 }
98
99
100 /* Read a statement with tag TAG in function FN from block IB using
101 descriptors in DATA_IN. */
102
103 static gimple
104 input_gimple_stmt (struct lto_input_block *ib, struct data_in *data_in,
105 enum LTO_tags tag)
106 {
107 gimple stmt;
108 enum gimple_code code;
109 unsigned HOST_WIDE_INT num_ops;
110 size_t i;
111 struct bitpack_d bp;
112 bool has_hist;
113
114 code = lto_tag_to_gimple_code (tag);
115
116 /* Read the tuple header. */
117 bp = streamer_read_bitpack (ib);
118 num_ops = bp_unpack_var_len_unsigned (&bp);
119 stmt = gimple_alloc (code, num_ops);
120 stmt->no_warning = bp_unpack_value (&bp, 1);
121 if (is_gimple_assign (stmt))
122 stmt->nontemporal_move = bp_unpack_value (&bp, 1);
123 stmt->has_volatile_ops = bp_unpack_value (&bp, 1);
124 has_hist = bp_unpack_value (&bp, 1);
125 stmt->subcode = bp_unpack_var_len_unsigned (&bp);
126
127 /* Read location information. */
128 gimple_set_location (stmt, stream_input_location (&bp, data_in));
129
130 /* Read lexical block reference. */
131 gimple_set_block (stmt, stream_read_tree (ib, data_in));
132
133 /* Read in all the operands. */
134 switch (code)
135 {
136 case GIMPLE_RESX:
137 gimple_resx_set_region (stmt, streamer_read_hwi (ib));
138 break;
139
140 case GIMPLE_EH_MUST_NOT_THROW:
141 gimple_eh_must_not_throw_set_fndecl (stmt, stream_read_tree (ib, data_in));
142 break;
143
144 case GIMPLE_EH_DISPATCH:
145 gimple_eh_dispatch_set_region (stmt, streamer_read_hwi (ib));
146 break;
147
148 case GIMPLE_ASM:
149 {
150 /* FIXME lto. Move most of this into a new gimple_asm_set_string(). */
151 gimple_statement_asm *asm_stmt = as_a <gimple_statement_asm *> (stmt);
152 tree str;
153 asm_stmt->ni = streamer_read_uhwi (ib);
154 asm_stmt->no = streamer_read_uhwi (ib);
155 asm_stmt->nc = streamer_read_uhwi (ib);
156 asm_stmt->nl = streamer_read_uhwi (ib);
157 str = streamer_read_string_cst (data_in, ib);
158 asm_stmt->string = TREE_STRING_POINTER (str);
159 }
160 /* Fallthru */
161
162 case GIMPLE_ASSIGN:
163 case GIMPLE_CALL:
164 case GIMPLE_RETURN:
165 case GIMPLE_SWITCH:
166 case GIMPLE_LABEL:
167 case GIMPLE_COND:
168 case GIMPLE_GOTO:
169 case GIMPLE_DEBUG:
170 for (i = 0; i < num_ops; i++)
171 {
172 tree *opp, op = stream_read_tree (ib, data_in);
173 gimple_set_op (stmt, i, op);
174 if (!op)
175 continue;
176
177 opp = gimple_op_ptr (stmt, i);
178 if (TREE_CODE (*opp) == ADDR_EXPR)
179 opp = &TREE_OPERAND (*opp, 0);
180 while (handled_component_p (*opp))
181 opp = &TREE_OPERAND (*opp, 0);
182 /* At LTO output time we wrap all global decls in MEM_REFs to
183 allow seamless replacement with prevailing decls. Undo this
184 here if the prevailing decl allows for this.
185 ??? Maybe we should simply fold all stmts. */
186 if (TREE_CODE (*opp) == MEM_REF
187 && TREE_CODE (TREE_OPERAND (*opp, 0)) == ADDR_EXPR
188 && integer_zerop (TREE_OPERAND (*opp, 1))
189 && (TREE_THIS_VOLATILE (*opp)
190 == TREE_THIS_VOLATILE
191 (TREE_OPERAND (TREE_OPERAND (*opp, 0), 0)))
192 && !TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (TREE_OPERAND (*opp, 1)))
193 && (TREE_TYPE (*opp)
194 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (*opp, 1))))
195 && (TREE_TYPE (*opp)
196 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (*opp, 0), 0))))
197 *opp = TREE_OPERAND (TREE_OPERAND (*opp, 0), 0);
198 }
199 if (is_gimple_call (stmt))
200 {
201 if (gimple_call_internal_p (stmt))
202 gimple_call_set_internal_fn
203 (stmt, streamer_read_enum (ib, internal_fn, IFN_LAST));
204 else
205 gimple_call_set_fntype (stmt, stream_read_tree (ib, data_in));
206 }
207 break;
208
209 case GIMPLE_NOP:
210 case GIMPLE_PREDICT:
211 break;
212
213 case GIMPLE_TRANSACTION:
214 gimple_transaction_set_label (stmt, stream_read_tree (ib, data_in));
215 break;
216
217 default:
218 internal_error ("bytecode stream: unknown GIMPLE statement tag %s",
219 lto_tag_name (tag));
220 }
221
222 /* Update the properties of symbols, SSA names and labels associated
223 with STMT. */
224 if (code == GIMPLE_ASSIGN || code == GIMPLE_CALL)
225 {
226 tree lhs = gimple_get_lhs (stmt);
227 if (lhs && TREE_CODE (lhs) == SSA_NAME)
228 SSA_NAME_DEF_STMT (lhs) = stmt;
229 }
230 else if (code == GIMPLE_ASM)
231 {
232 unsigned i;
233
234 for (i = 0; i < gimple_asm_noutputs (stmt); i++)
235 {
236 tree op = TREE_VALUE (gimple_asm_output_op (stmt, i));
237 if (TREE_CODE (op) == SSA_NAME)
238 SSA_NAME_DEF_STMT (op) = stmt;
239 }
240 }
241
242 /* Reset alias information. */
243 if (code == GIMPLE_CALL)
244 gimple_call_reset_alias_info (stmt);
245
246 /* Mark the statement modified so its operand vectors can be filled in. */
247 gimple_set_modified (stmt, true);
248 if (has_hist)
249 stream_in_histogram_value (ib, stmt);
250
251 return stmt;
252 }
253
254
255 /* Read a basic block with tag TAG from DATA_IN using input block IB.
256 FN is the function being processed. */
257
258 void
259 input_bb (struct lto_input_block *ib, enum LTO_tags tag,
260 struct data_in *data_in, struct function *fn,
261 int count_materialization_scale)
262 {
263 unsigned int index;
264 basic_block bb;
265 gimple_stmt_iterator bsi;
266
267 /* This routine assumes that CFUN is set to FN, as it needs to call
268 basic GIMPLE routines that use CFUN. */
269 gcc_assert (cfun == fn);
270
271 index = streamer_read_uhwi (ib);
272 bb = BASIC_BLOCK_FOR_FN (fn, index);
273
274 bb->count = apply_scale (streamer_read_gcov_count (ib),
275 count_materialization_scale);
276 bb->frequency = streamer_read_hwi (ib);
277 bb->flags = streamer_read_hwi (ib);
278
279 /* LTO_bb1 has statements. LTO_bb0 does not. */
280 if (tag == LTO_bb0)
281 return;
282
283 bsi = gsi_start_bb (bb);
284 tag = streamer_read_record_start (ib);
285 while (tag)
286 {
287 gimple stmt = input_gimple_stmt (ib, data_in, tag);
288 gsi_insert_after (&bsi, stmt, GSI_NEW_STMT);
289
290 /* After the statement, expect a 0 delimiter or the EH region
291 that the previous statement belongs to. */
292 tag = streamer_read_record_start (ib);
293 lto_tag_check_set (tag, 2, LTO_eh_region, LTO_null);
294
295 if (tag == LTO_eh_region)
296 {
297 HOST_WIDE_INT region = streamer_read_hwi (ib);
298 gcc_assert (region == (int) region);
299 add_stmt_to_eh_lp (stmt, region);
300 }
301
302 tag = streamer_read_record_start (ib);
303 }
304
305 tag = streamer_read_record_start (ib);
306 while (tag)
307 {
308 input_phi (ib, bb, data_in, fn);
309 tag = streamer_read_record_start (ib);
310 }
311 }