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