1 /* Routines for emitting GIMPLE to a file stream.
2 
3    Copyright (C) 2011-2018 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 "backend.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "gimple-ssa.h"
29 #include "gimple-streamer.h"
30 #include "tree-eh.h"
31 #include "gimple-iterator.h"
32 #include "cgraph.h"
33 #include "value-prof.h"
34 
35 /* Output PHI function PHI to the main stream in OB.  */
36 
37 static void
38 output_phi (struct output_block *ob, gphi *phi)
39 {
40   unsigned i, len = gimple_phi_num_args (phi);
41 
42   streamer_write_record_start (ob, lto_gimple_code_to_tag (GIMPLE_PHI));
43   streamer_write_uhwi (ob, SSA_NAME_VERSION (PHI_RESULT (phi)));
44 
45   for (i = 0; i < len; i++)
46     {
47       stream_write_tree (ob, gimple_phi_arg_def (phi, i), true);
48       streamer_write_uhwi (ob, gimple_phi_arg_edge (phi, i)->src->index);
49       bitpack_d bp = bitpack_create (ob->main_stream);
50       stream_output_location (ob, &bp, gimple_phi_arg_location (phi, i));
51       streamer_write_bitpack (&bp);
52     }
53 }
54 
55 
56 /* Emit statement STMT on the main stream of output block OB.  */
57 
58 static void
59 output_gimple_stmt (struct output_block *ob, gimple *stmt)
60 {
61   unsigned i;
62   enum gimple_code code;
63   enum LTO_tags tag;
64   struct bitpack_d bp;
65   histogram_value hist;
66 
67   /* Emit identifying tag.  */
68   code = gimple_code (stmt);
69   tag = lto_gimple_code_to_tag (code);
70   streamer_write_record_start (ob, tag);
71 
72   /* Emit the tuple header.  */
73   bp = bitpack_create (ob->main_stream);
74   bp_pack_var_len_unsigned (&bp, gimple_num_ops (stmt));
75   bp_pack_value (&bp, gimple_no_warning_p (stmt), 1);
76   if (is_gimple_assign (stmt))
77     bp_pack_value (&bp,
78 		   gimple_assign_nontemporal_move_p (
79 		     as_a <gassign *> (stmt)),
80 		   1);
81   bp_pack_value (&bp, gimple_has_volatile_ops (stmt), 1);
82   hist = gimple_histogram_value (cfun, stmt);
83   bp_pack_value (&bp, hist != NULL, 1);
84   bp_pack_var_len_unsigned (&bp, stmt->subcode);
85 
86   /* Emit location information for the statement.  */
87   stream_output_location (ob, &bp, LOCATION_LOCUS (gimple_location (stmt)));
88   streamer_write_bitpack (&bp);
89 
90   /* Emit the lexical block holding STMT.  */
91   stream_write_tree (ob, gimple_block (stmt), true);
92 
93   /* Emit the operands.  */
94   switch (gimple_code (stmt))
95     {
96     case GIMPLE_RESX:
97       streamer_write_hwi (ob, gimple_resx_region (as_a <gresx *> (stmt)));
98       break;
99 
100     case GIMPLE_EH_MUST_NOT_THROW:
101       stream_write_tree (ob,
102 			 gimple_eh_must_not_throw_fndecl (
103 			   as_a <geh_mnt *> (stmt)),
104 			 true);
105       break;
106 
107     case GIMPLE_EH_DISPATCH:
108       streamer_write_hwi (ob,
109 			  gimple_eh_dispatch_region (
110 			    as_a <geh_dispatch *> (stmt)));
111       break;
112 
113     case GIMPLE_ASM:
114       {
115 	gasm *asm_stmt = as_a <gasm *> (stmt);
116 	streamer_write_uhwi (ob, gimple_asm_ninputs (asm_stmt));
117 	streamer_write_uhwi (ob, gimple_asm_noutputs (asm_stmt));
118 	streamer_write_uhwi (ob, gimple_asm_nclobbers (asm_stmt));
119 	streamer_write_uhwi (ob, gimple_asm_nlabels (asm_stmt));
120 	streamer_write_string (ob, ob->main_stream,
121 			       gimple_asm_string (asm_stmt), true);
122       }
123       /* Fallthru  */
124 
125     case GIMPLE_ASSIGN:
126     case GIMPLE_CALL:
127     case GIMPLE_RETURN:
128     case GIMPLE_SWITCH:
129     case GIMPLE_LABEL:
130     case GIMPLE_COND:
131     case GIMPLE_GOTO:
132     case GIMPLE_DEBUG:
133       for (i = 0; i < gimple_num_ops (stmt); i++)
134 	{
135 	  tree op = gimple_op (stmt, i);
136 	  tree *basep = NULL;
137 	  /* Wrap all uses of non-automatic variables inside MEM_REFs
138 	     so that we do not have to deal with type mismatches on
139 	     merged symbols during IL read in.  The first operand
140 	     of GIMPLE_DEBUG must be a decl, not MEM_REF, though.  */
141 	  if (op && (i || !is_gimple_debug (stmt)))
142 	    {
143 	      basep = &op;
144 	      if (TREE_CODE (*basep) == ADDR_EXPR)
145 		basep = &TREE_OPERAND (*basep, 0);
146 	      while (handled_component_p (*basep))
147 		basep = &TREE_OPERAND (*basep, 0);
148 	      if (VAR_P (*basep)
149 		  && !auto_var_in_fn_p (*basep, current_function_decl)
150 		  && !DECL_REGISTER (*basep))
151 		{
152 		  bool volatilep = TREE_THIS_VOLATILE (*basep);
153 		  tree ptrtype = build_pointer_type (TREE_TYPE (*basep));
154 		  *basep = build2 (MEM_REF, TREE_TYPE (*basep),
155 				   build1 (ADDR_EXPR, ptrtype, *basep),
156 				   build_int_cst (ptrtype, 0));
157 		  TREE_THIS_VOLATILE (*basep) = volatilep;
158 		}
159 	      else
160 		basep = NULL;
161 	    }
162 	  stream_write_tree (ob, op, true);
163 	  /* Restore the original base if we wrapped it inside a MEM_REF.  */
164 	  if (basep)
165 	    *basep = TREE_OPERAND (TREE_OPERAND (*basep, 0), 0);
166 	}
167       if (is_gimple_call (stmt))
168 	{
169 	  if (gimple_call_internal_p (stmt))
170 	    streamer_write_enum (ob->main_stream, internal_fn,
171 				 IFN_LAST, gimple_call_internal_fn (stmt));
172 	  else
173 	    stream_write_tree (ob, gimple_call_fntype (stmt), true);
174 	}
175       break;
176 
177     case GIMPLE_NOP:
178     case GIMPLE_PREDICT:
179       break;
180 
181     case GIMPLE_TRANSACTION:
182       {
183 	gtransaction *txn = as_a <gtransaction *> (stmt);
184 	gcc_assert (gimple_transaction_body (txn) == NULL);
185 	stream_write_tree (ob, gimple_transaction_label_norm (txn), true);
186 	stream_write_tree (ob, gimple_transaction_label_uninst (txn), true);
187 	stream_write_tree (ob, gimple_transaction_label_over (txn), true);
188       }
189       break;
190 
191     default:
192       gcc_unreachable ();
193     }
194   if (hist)
195     stream_out_histogram_value (ob, hist);
196 }
197 
198 
199 /* Output a basic block BB to the main stream in OB for this FN.  */
200 
201 void
202 output_bb (struct output_block *ob, basic_block bb, struct function *fn)
203 {
204   gimple_stmt_iterator bsi = gsi_start_bb (bb);
205 
206   streamer_write_record_start (ob,
207 			       (!gsi_end_p (bsi)) || phi_nodes (bb)
208 			        ? LTO_bb1
209 				: LTO_bb0);
210 
211   streamer_write_uhwi (ob, bb->index);
212   bb->count.stream_out (ob);
213   streamer_write_hwi (ob, bb->flags);
214 
215   if (!gsi_end_p (bsi) || phi_nodes (bb))
216     {
217       /* Output the statements.  The list of statements is terminated
218 	 with a zero.  */
219       for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
220 	{
221 	  int region;
222 	  gimple *stmt = gsi_stmt (bsi);
223 
224 	  output_gimple_stmt (ob, stmt);
225 
226 	  /* Emit the EH region holding STMT.  */
227 	  region = lookup_stmt_eh_lp_fn (fn, stmt);
228 	  if (region != 0)
229 	    {
230 	      streamer_write_record_start (ob, LTO_eh_region);
231 	      streamer_write_hwi (ob, region);
232 	    }
233 	  else
234 	    streamer_write_record_start (ob, LTO_null);
235 	}
236 
237       streamer_write_record_start (ob, LTO_null);
238 
239       for (gphi_iterator psi = gsi_start_phis (bb);
240 	   !gsi_end_p (psi);
241 	   gsi_next (&psi))
242 	{
243 	  gphi *phi = psi.phi ();
244 
245 	  /* Only emit PHIs for gimple registers.  PHI nodes for .MEM
246 	     will be filled in on reading when the SSA form is
247 	     updated.  */
248 	  if (!virtual_operand_p (gimple_phi_result (phi)))
249 	    output_phi (ob, phi);
250 	}
251 
252       streamer_write_record_start (ob, LTO_null);
253     }
254 }
255