functions.h revision 1.1 1 1.1 mrg // Function-related RTL SSA classes -*- C++ -*-
2 1.1 mrg // Copyright (C) 2020-2022 Free Software Foundation, Inc.
3 1.1 mrg //
4 1.1 mrg // This file is part of GCC.
5 1.1 mrg //
6 1.1 mrg // GCC is free software; you can redistribute it and/or modify it under
7 1.1 mrg // the terms of the GNU General Public License as published by the Free
8 1.1 mrg // Software Foundation; either version 3, or (at your option) any later
9 1.1 mrg // version.
10 1.1 mrg //
11 1.1 mrg // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 1.1 mrg // WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 1.1 mrg // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 1.1 mrg // for more details.
15 1.1 mrg //
16 1.1 mrg // You should have received a copy of the GNU General Public License
17 1.1 mrg // along with GCC; see the file COPYING3. If not see
18 1.1 mrg // <http://www.gnu.org/licenses/>.
19 1.1 mrg
20 1.1 mrg namespace rtl_ssa {
21 1.1 mrg
22 1.1 mrg // SSA-related information about a function. It contains three levels
23 1.1 mrg // of information, each in reverse postorder:
24 1.1 mrg //
25 1.1 mrg // - a list of extended basic blocks
26 1.1 mrg // - a list of basic blocks
27 1.1 mrg // - a list of instructions
28 1.1 mrg //
29 1.1 mrg // It also maintains a list of definitions of memory, and a list of
30 1.1 mrg // definitions of each register.
31 1.1 mrg //
32 1.1 mrg // See doc/rtl.texi for more details about the way this information
33 1.1 mrg // is organized and how changes to it are made.
34 1.1 mrg class function_info
35 1.1 mrg {
36 1.1 mrg // The default obstack alignment takes long double into account.
37 1.1 mrg // Since we have no use for that here, and since we allocate many
38 1.1 mrg // relatively small objects, it's better to specify an alignment
39 1.1 mrg // explicitly. The allocation routines assert that the alignment
40 1.1 mrg // is enough for the objects being allocated.
41 1.1 mrg //
42 1.1 mrg // Because various structures use pointer_mux, we need at least 2 bytes
43 1.1 mrg // of alignment.
44 1.1 mrg static const size_t obstack_alignment = sizeof (void *);
45 1.1 mrg
46 1.1 mrg public:
47 1.1 mrg // Construct SSA form for function FN.
48 1.1 mrg function_info (function *fn);
49 1.1 mrg ~function_info ();
50 1.1 mrg
51 1.1 mrg // Return a list of all the extended basic blocks in the function, in reverse
52 1.1 mrg // postorder. The list includes the entry and exit blocks.
53 1.1 mrg iterator_range<ebb_iterator> ebbs () const;
54 1.1 mrg
55 1.1 mrg // Like ebbs (), but in the reverse order.
56 1.1 mrg iterator_range<reverse_ebb_iterator> reverse_ebbs () const;
57 1.1 mrg
58 1.1 mrg // Return a list of all the basic blocks in the function, in reverse
59 1.1 mrg // postorder. The list includes the entry and exit blocks.
60 1.1 mrg iterator_range<bb_iterator> bbs () const;
61 1.1 mrg
62 1.1 mrg // Like bbs (), but in the reverse order.
63 1.1 mrg iterator_range<reverse_bb_iterator> reverse_bbs () const;
64 1.1 mrg
65 1.1 mrg // Return the SSA information for the basic block with index INDEX.
66 1.1 mrg bb_info *bb (unsigned int index) const { return m_bbs[index]; }
67 1.1 mrg
68 1.1 mrg // Return the SSA information for CFG_BB.
69 1.1 mrg bb_info *bb (basic_block cfg_bb) const { return m_bbs[cfg_bb->index]; }
70 1.1 mrg
71 1.1 mrg // Return a list of all the instructions in the function, in reverse
72 1.1 mrg // postorder. The list includes both real and artificial instructions.
73 1.1 mrg //
74 1.1 mrg // Iterations over the list will pick up any new instructions that are
75 1.1 mrg // inserted after the iterator's current instruction.
76 1.1 mrg iterator_range<any_insn_iterator> all_insns () const;
77 1.1 mrg
78 1.1 mrg // Like all_insns (), but in the reverse order.
79 1.1 mrg //
80 1.1 mrg // Iterations over the list will pick up any new instructions that are
81 1.1 mrg // inserted before the iterator's current instruction.
82 1.1 mrg iterator_range<reverse_any_insn_iterator> reverse_all_insns () const;
83 1.1 mrg
84 1.1 mrg // Like all_insns (), but without the debug instructions.
85 1.1 mrg iterator_range<nondebug_insn_iterator> nondebug_insns () const;
86 1.1 mrg
87 1.1 mrg // Like reverse_all_insns (), but without the debug instructions.
88 1.1 mrg iterator_range<reverse_nondebug_insn_iterator>
89 1.1 mrg reverse_nondebug_insns () const;
90 1.1 mrg
91 1.1 mrg // Return the first and last instructions in insns ().
92 1.1 mrg insn_info *first_insn () const { return m_first_insn; }
93 1.1 mrg insn_info *last_insn () const { return m_last_insn; }
94 1.1 mrg
95 1.1 mrg // Return a list of all definitions of memory, in reverse postorder.
96 1.1 mrg // This includes both real stores by instructions and artificial
97 1.1 mrg // definitions by things like phi nodes.
98 1.1 mrg iterator_range<def_iterator> mem_defs () const;
99 1.1 mrg
100 1.1 mrg // Return a list of all definitions of register REGNO, in reverse postorder.
101 1.1 mrg // This includes both real stores by instructions and artificial
102 1.1 mrg // definitions by things like phi nodes.
103 1.1 mrg iterator_range<def_iterator> reg_defs (unsigned int regno) const;
104 1.1 mrg
105 1.1 mrg // Check if all uses of register REGNO are either unconditionally undefined
106 1.1 mrg // or use the same single dominating definition. Return the definition
107 1.1 mrg // if so, otherwise return null.
108 1.1 mrg set_info *single_dominating_def (unsigned int regno) const;
109 1.1 mrg
110 1.1 mrg // Look for a definition of RESOURCE at INSN. Return the result of the
111 1.1 mrg // search as a def_lookup; see the comments there for more details.
112 1.1 mrg def_lookup find_def (resource_info resource, insn_info *insn);
113 1.1 mrg
114 1.1 mrg // Return an RAII object that owns all temporary RTL SSA memory
115 1.1 mrg // allocated during a change attempt. The object should remain in
116 1.1 mrg // scope until the change has been aborted or successfully completed.
117 1.1 mrg obstack_watermark new_change_attempt () { return &m_temp_obstack; }
118 1.1 mrg
119 1.1 mrg // Make a best attempt to check whether the values used by USES are
120 1.1 mrg // available on entry to BB, without solving a full dataflow problem.
121 1.1 mrg // If all the values are already live on entry to BB or can be made
122 1.1 mrg // available there, return a use_array that describes the uses as
123 1.1 mrg // if they occured at the start of BB. These uses are purely temporary,
124 1.1 mrg // and will not become permanent unless applied using change_insns.
125 1.1 mrg //
126 1.1 mrg // If the operation fails, return an invalid use_array.
127 1.1 mrg //
128 1.1 mrg // WATERMARK is a watermark returned by new_change_attempt ().
129 1.1 mrg // WILL_BE_DEBUG_USES is true if the returned use_array will be
130 1.1 mrg // used only for debug instructions.
131 1.1 mrg use_array make_uses_available (obstack_watermark &watermark,
132 1.1 mrg use_array uses, bb_info *bb,
133 1.1 mrg bool will_be_debug_uses);
134 1.1 mrg
135 1.1 mrg // If CHANGE doesn't already clobber REGNO, try to add such a clobber,
136 1.1 mrg // limiting the movement range in order to make the clobber valid.
137 1.1 mrg // When determining whether REGNO is live, ignore accesses made by an
138 1.1 mrg // instruction I if IGNORE (I) is true. The caller then assumes the
139 1.1 mrg // responsibility of ensuring that CHANGE and I are placed in a valid order.
140 1.1 mrg //
141 1.1 mrg // Return true on success. Leave CHANGE unmodified when returning false.
142 1.1 mrg //
143 1.1 mrg // WATERMARK is a watermark returned by new_change_attempt ().
144 1.1 mrg template<typename IgnorePredicate>
145 1.1 mrg bool add_regno_clobber (obstack_watermark &watermark, insn_change &change,
146 1.1 mrg unsigned int regno, IgnorePredicate ignore);
147 1.1 mrg
148 1.1 mrg // Return true if change_insns will be able to perform the changes
149 1.1 mrg // described by CHANGES.
150 1.1 mrg bool verify_insn_changes (array_slice<insn_change *const> changes);
151 1.1 mrg
152 1.1 mrg // Perform all the changes in CHANGES, keeping the instructions in the
153 1.1 mrg // order specified by the CHANGES array. On return, the SSA information
154 1.1 mrg // remains up-to-date. The same is true for instruction-level DF
155 1.1 mrg // information, although the block-level DF information might be
156 1.1 mrg // marked dirty.
157 1.1 mrg void change_insns (array_slice<insn_change *> changes);
158 1.1 mrg
159 1.1 mrg // Like change_insns, but for a single change CHANGE.
160 1.1 mrg void change_insn (insn_change &change);
161 1.1 mrg
162 1.1 mrg // If the changes that have been made to instructions require updates
163 1.1 mrg // to the CFG, perform those updates now. Return true if something changed.
164 1.1 mrg // If it did:
165 1.1 mrg //
166 1.1 mrg // - The SSA information is now invalid and needs to be recomputed.
167 1.1 mrg //
168 1.1 mrg // - Dominance information is no longer available (in either direction).
169 1.1 mrg //
170 1.1 mrg // - The caller will need to call cleanup_cfg at some point.
171 1.1 mrg //
172 1.1 mrg // ??? We could probably update the SSA information for simple updates,
173 1.1 mrg // but currently nothing would benefit. These late CFG changes are
174 1.1 mrg // relatively rare anyway, since gimple optimisers should remove most
175 1.1 mrg // unnecessary control flow.
176 1.1 mrg bool perform_pending_updates ();
177 1.1 mrg
178 1.1 mrg // Print the contents of the function to PP.
179 1.1 mrg void print (pretty_printer *pp) const;
180 1.1 mrg
181 1.1 mrg private:
182 1.1 mrg class bb_phi_info;
183 1.1 mrg class build_info;
184 1.1 mrg class bb_walker;
185 1.1 mrg
186 1.1 mrg // Return an RAII object that owns all objects allocated by
187 1.1 mrg // allocate_temp during its lifetime.
188 1.1 mrg obstack_watermark temp_watermark () { return &m_temp_obstack; }
189 1.1 mrg
190 1.1 mrg template<typename T, typename... Ts>
191 1.1 mrg T *allocate (Ts... args);
192 1.1 mrg
193 1.1 mrg template<typename T, typename... Ts>
194 1.1 mrg T *allocate_temp (Ts... args);
195 1.1 mrg
196 1.1 mrg access_array temp_access_array (access_array accesses);
197 1.1 mrg
198 1.1 mrg clobber_group *need_clobber_group (clobber_info *);
199 1.1 mrg def_node *need_def_node (def_info *);
200 1.1 mrg def_splay_tree need_def_splay_tree (def_info *);
201 1.1 mrg
202 1.1 mrg use_info *make_use_available (use_info *, bb_info *, bool);
203 1.1 mrg def_array insert_temp_clobber (obstack_watermark &, insn_info *,
204 1.1 mrg unsigned int, def_array);
205 1.1 mrg
206 1.1 mrg void insert_def_before (def_info *, def_info *);
207 1.1 mrg void insert_def_after (def_info *, def_info *);
208 1.1 mrg void remove_def_from_list (def_info *);
209 1.1 mrg
210 1.1 mrg void add_clobber (clobber_info *, clobber_group *);
211 1.1 mrg void remove_clobber (clobber_info *, clobber_group *);
212 1.1 mrg void prepend_clobber_to_group (clobber_info *, clobber_group *);
213 1.1 mrg void append_clobber_to_group (clobber_info *, clobber_group *);
214 1.1 mrg void merge_clobber_groups (clobber_info *, clobber_info *,
215 1.1 mrg def_info *);
216 1.1 mrg clobber_info *split_clobber_group (clobber_group *, insn_info *);
217 1.1 mrg
218 1.1 mrg void append_def (def_info *);
219 1.1 mrg void add_def (def_info *);
220 1.1 mrg void remove_def (def_info *);
221 1.1 mrg
222 1.1 mrg void need_use_splay_tree (set_info *);
223 1.1 mrg
224 1.1 mrg static void insert_use_before (use_info *, use_info *);
225 1.1 mrg static void insert_use_after (use_info *, use_info *);
226 1.1 mrg
227 1.1 mrg void add_use (use_info *);
228 1.1 mrg void remove_use (use_info *);
229 1.1 mrg
230 1.1 mrg insn_info::order_node *need_order_node (insn_info *);
231 1.1 mrg
232 1.1 mrg void add_insn_after (insn_info *, insn_info *);
233 1.1 mrg void append_insn (insn_info *);
234 1.1 mrg void remove_insn (insn_info *);
235 1.1 mrg
236 1.1 mrg insn_info *append_artificial_insn (bb_info *, rtx_insn * = nullptr);
237 1.1 mrg
238 1.1 mrg void start_insn_accesses ();
239 1.1 mrg void finish_insn_accesses (insn_info *);
240 1.1 mrg
241 1.1 mrg use_info *create_reg_use (build_info &, insn_info *, resource_info);
242 1.1 mrg void record_use (build_info &, insn_info *, rtx_obj_reference);
243 1.1 mrg void record_call_clobbers (build_info &, insn_info *, rtx_call_insn *);
244 1.1 mrg void record_def (build_info &, insn_info *, rtx_obj_reference);
245 1.1 mrg void add_insn_to_block (build_info &, rtx_insn *);
246 1.1 mrg
247 1.1 mrg void add_reg_unused_notes (insn_info *);
248 1.1 mrg
249 1.1 mrg void add_live_out_use (bb_info *, set_info *);
250 1.1 mrg set_info *live_out_value (bb_info *, set_info *);
251 1.1 mrg
252 1.1 mrg void append_phi (ebb_info *, phi_info *);
253 1.1 mrg void remove_phi (phi_info *);
254 1.1 mrg void delete_phi (phi_info *);
255 1.1 mrg void replace_phi (phi_info *, set_info *);
256 1.1 mrg phi_info *create_phi (ebb_info *, resource_info, access_info **,
257 1.1 mrg unsigned int);
258 1.1 mrg phi_info *create_degenerate_phi (ebb_info *, set_info *);
259 1.1 mrg
260 1.1 mrg bb_info *create_bb_info (basic_block);
261 1.1 mrg void append_bb (bb_info *);
262 1.1 mrg
263 1.1 mrg insn_info *add_placeholder_after (insn_info *);
264 1.1 mrg void possibly_queue_changes (insn_change &);
265 1.1 mrg void finalize_new_accesses (insn_change &);
266 1.1 mrg void apply_changes_to_insn (insn_change &);
267 1.1 mrg
268 1.1 mrg void init_function_data ();
269 1.1 mrg void calculate_potential_phi_regs (build_info &);
270 1.1 mrg void place_phis (build_info &);
271 1.1 mrg void create_ebbs (build_info &);
272 1.1 mrg void add_entry_block_defs (build_info &);
273 1.1 mrg void calculate_ebb_live_in_for_debug (build_info &);
274 1.1 mrg void add_phi_nodes (build_info &);
275 1.1 mrg void add_artificial_accesses (build_info &, df_ref_flags);
276 1.1 mrg void add_block_contents (build_info &);
277 1.1 mrg void record_block_live_out (build_info &);
278 1.1 mrg void start_block (build_info &, bb_info *);
279 1.1 mrg void end_block (build_info &, bb_info *);
280 1.1 mrg void populate_phi_inputs (build_info &);
281 1.1 mrg void process_all_blocks ();
282 1.1 mrg
283 1.1 mrg void simplify_phi_setup (phi_info *, set_info **, bitmap);
284 1.1 mrg void simplify_phi_propagate (phi_info *, set_info **, bitmap, bitmap);
285 1.1 mrg void simplify_phis ();
286 1.1 mrg
287 1.1 mrg // The function that this object describes.
288 1.1 mrg function *m_fn;
289 1.1 mrg
290 1.1 mrg // The lowest (negative) in-use artificial insn uid minus one.
291 1.1 mrg int m_next_artificial_uid;
292 1.1 mrg
293 1.1 mrg // The highest in-use phi uid plus one.
294 1.1 mrg unsigned int m_next_phi_uid;
295 1.1 mrg
296 1.1 mrg // The highest in-use register number plus one.
297 1.1 mrg unsigned int m_num_regs;
298 1.1 mrg
299 1.1 mrg // M_DEFS[R] is the first definition of register R - 1 in a reverse
300 1.1 mrg // postorder traversal of the function, or null if the function has
301 1.1 mrg // no definition of R. Applying last () gives the last definition of R.
302 1.1 mrg //
303 1.1 mrg // M_DEFS[0] is for memory; MEM_REGNO + 1 == 0.
304 1.1 mrg auto_vec<def_info *> m_defs;
305 1.1 mrg
306 1.1 mrg // M_BBS[BI] gives the SSA information about the block with index BI.
307 1.1 mrg auto_vec<bb_info *> m_bbs;
308 1.1 mrg
309 1.1 mrg // An obstack used to allocate the main RTL SSA information.
310 1.1 mrg obstack m_obstack;
311 1.1 mrg
312 1.1 mrg // An obstack used for temporary work, such as while building up a list
313 1.1 mrg // of possible instruction changes.
314 1.1 mrg obstack m_temp_obstack;
315 1.1 mrg
316 1.1 mrg // The start of each obstack, so that all memory in them can be freed.
317 1.1 mrg char *m_obstack_start;
318 1.1 mrg char *m_temp_obstack_start;
319 1.1 mrg
320 1.1 mrg // The entry and exit blocks.
321 1.1 mrg bb_info *m_first_bb;
322 1.1 mrg bb_info *m_last_bb;
323 1.1 mrg
324 1.1 mrg // The first and last instructions in a reverse postorder traversal
325 1.1 mrg // of the function.
326 1.1 mrg insn_info *m_first_insn;
327 1.1 mrg insn_info *m_last_insn;
328 1.1 mrg
329 1.1 mrg // The last nondebug instruction in the list of instructions.
330 1.1 mrg // This is only different from m_last_insn when building the initial
331 1.1 mrg // SSA information; after that, the last instruction is always a
332 1.1 mrg // BB end instruction.
333 1.1 mrg insn_info *m_last_nondebug_insn;
334 1.1 mrg
335 1.1 mrg // Temporary working state when building up lists of definitions and uses.
336 1.1 mrg // Keeping them around should reduce the number of unnecessary reallocations.
337 1.1 mrg auto_vec<access_info *> m_temp_defs;
338 1.1 mrg auto_vec<access_info *> m_temp_uses;
339 1.1 mrg
340 1.1 mrg // A list of phis that are no longer in use. Their uids are still unique
341 1.1 mrg // and so can be recycled.
342 1.1 mrg phi_info *m_free_phis;
343 1.1 mrg
344 1.1 mrg // A list of instructions that have been changed in ways that need
345 1.1 mrg // further processing later, such as removing dead instructions or
346 1.1 mrg // altering the CFG.
347 1.1 mrg auto_vec<insn_info *> m_queued_insn_updates;
348 1.1 mrg
349 1.1 mrg // The INSN_UIDs of all instructions in M_QUEUED_INSN_UPDATES.
350 1.1 mrg auto_bitmap m_queued_insn_update_uids;
351 1.1 mrg
352 1.1 mrg // A basic_block is in this bitmap if we need to call purge_dead_edges
353 1.1 mrg // on it. As with M_QUEUED_INSN_UPDATES, these updates are queued until
354 1.1 mrg // a convenient point.
355 1.1 mrg auto_bitmap m_need_to_purge_dead_edges;
356 1.1 mrg };
357 1.1 mrg
358 1.1 mrg void pp_function (pretty_printer *, const function_info *);
359 1.1 mrg }
360 1.1 mrg
361 1.1 mrg void dump (FILE *, const rtl_ssa::function_info *);
362 1.1 mrg
363 1.1 mrg void DEBUG_FUNCTION debug (const rtl_ssa::function_info *);
364