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