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