GCC Middle and Back End API Reference
rtl-iter.h
Go to the documentation of this file.
1/* RTL iterators
2 Copyright (C) 2014-2024 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20/* This structure describes the subrtxes of an rtx as follows:
21
22 - if the rtx has no subrtxes, START and COUNT are both 0.
23
24 - if all the subrtxes of an rtx are stored in a contiguous block
25 of XEXPs ("e"s), START is the index of the first XEXP and COUNT
26 is the number of them.
27
28 - otherwise START is arbitrary and COUNT is UCHAR_MAX.
29
30 rtx_all_subrtx_bounds applies to all codes. rtx_nonconst_subrtx_bounds
31 is like rtx_all_subrtx_bounds except that all constant rtxes are treated
32 as having no subrtxes. */
34 unsigned char start;
35 unsigned char count;
36};
39
40/* Return true if CODE has no subrtxes. */
41
42inline bool
44{
45 return rtx_all_subrtx_bounds[code].count == 0;
46}
47
48/* Used to iterate over subrtxes of an rtx. T abstracts the type of
49 access. */
50template <typename T>
52{
53 static const size_t LOCAL_ELEMS = 16;
54 typedef typename T::value_type value_type;
55 typedef typename T::rtx_type rtx_type;
56 typedef typename T::rtunion_type rtunion_type;
57
58public:
60 {
61 public:
62 array_type ();
63 ~array_type ();
65 vec <value_type, va_heap, vl_embed> *heap;
66 };
68 const rtx_subrtx_bound_info *);
69
70 value_type operator * () const;
71 bool at_end () const;
72 void next ();
73 void skip_subrtxes ();
75
76private:
77 /* The bounds to use for iterating over subrtxes. */
79
80 /* The storage used for the worklist. */
82
83 /* The current rtx. */
85
86 /* The base of the current worklist. */
88
89 /* The number of subrtxes in M_BASE. */
90 size_t m_end;
91
92 /* The following booleans shouldn't end up in registers or memory
93 but just direct control flow. */
94
95 /* True if the iteration is over. */
96 bool m_done;
97
98 /* True if we should skip the subrtxes of M_CURRENT. */
99 bool m_skip;
100
101 /* True if M_CURRENT has been replaced with a different rtx. */
103
104 static void free_array (array_type &);
105 static size_t add_subrtxes_to_queue (array_type &, value_type *, size_t,
106 rtx_type);
108 value_type);
109};
110
111template <typename T>
113
114template <typename T>
115inline generic_subrtx_iterator <T>::array_type::~array_type ()
116{
117 if (UNLIKELY (heap != 0))
118 free_array (*this);
119}
120
121/* Iterate over X and its subrtxes, in arbitrary order. Use ARRAY to
122 store the worklist. We use an external array in order to avoid
123 capturing the fields of this structure when taking the address of
124 the array. Use BOUNDS to find the bounds of simple "e"-string codes. */
125
126template <typename T>
130 : m_bounds (bounds),
131 m_array (array),
132 m_current (x),
133 m_base (m_array.stack),
134 m_end (0),
135 m_done (false),
136 m_skip (false),
138{
139}
140
141/* Return the current subrtx. */
142
143template <typename T>
144inline typename T::value_type
145generic_subrtx_iterator <T>::operator * () const
146{
147 return m_current;
148}
149
150/* Return true if the iteration has finished. */
151
152template <typename T>
153inline bool
155{
156 return m_done;
157}
158
159/* Move on to the next subrtx. */
160
161template <typename T>
162inline void
164{
165 if (m_substitute)
166 {
167 m_substitute = false;
168 m_skip = false;
169 return;
170 }
171 if (!m_skip)
172 {
173 /* Add the subrtxes of M_CURRENT. */
174 rtx_type x = T::get_rtx (m_current);
175 if (LIKELY (x != 0))
176 {
177 enum rtx_code code = GET_CODE (x);
178 ssize_t count = m_bounds[code].count;
179 if (count > 0)
180 {
181 /* Handle the simple case of a single "e" block that is known
182 to fit into the current array. */
183 if (LIKELY (m_end + count <= LOCAL_ELEMS + 1))
184 {
185 /* Set M_CURRENT to the first subrtx and queue the rest. */
186 ssize_t start = m_bounds[code].start;
187 rtunion_type *src = &x->u.fld[start];
188 if (UNLIKELY (count > 2))
189 m_base[m_end++] = T::get_value (src[2].rt_rtx);
190 if (count > 1)
191 m_base[m_end++] = T::get_value (src[1].rt_rtx);
192 m_current = T::get_value (src[0].rt_rtx);
193 return;
194 }
195 /* Handle cases which aren't simple "e" sequences or where
196 the sequence might overrun M_BASE. */
197 count = add_subrtxes_to_queue (m_array, m_base, m_end, x);
198 if (count > 0)
199 {
200 m_end += count;
201 if (m_end > LOCAL_ELEMS)
202 m_base = m_array.heap->address ();
203 m_current = m_base[--m_end];
204 return;
205 }
206 }
207 }
208 }
209 else
210 m_skip = false;
211 if (m_end == 0)
212 m_done = true;
213 else
214 m_current = m_base[--m_end];
215}
216
217/* Skip the subrtxes of the current rtx. */
218
219template <typename T>
220inline void
222{
223 m_skip = true;
224}
225
226/* Ignore the subrtxes of the current rtx and look at X instead. */
227
228template <typename T>
229inline void
231{
232 m_substitute = true;
233 m_current = x;
234}
235
236/* Iterators for const_rtx. */
238{
241 typedef const rtunion rtunion_type;
242 static rtx_type get_rtx (value_type x) { return x; }
243 static value_type get_value (rtx_type x) { return x; }
244};
246
247/* Iterators for non-constant rtx. */
249{
251 typedef rtx rtx_type;
253 static rtx_type get_rtx (value_type x) { return x; }
254 static value_type get_value (rtx_type x) { return x; }
255};
257
258/* Iterators for rtx *. */
260{
261 typedef rtx *value_type;
262 typedef rtx rtx_type;
264 static rtx_type get_rtx (value_type ptr) { return *ptr; }
265 static value_type get_value (rtx_type &x) { return &x; }
266};
268
269#define ALL_BOUNDS rtx_all_subrtx_bounds
270#define NONCONST_BOUNDS rtx_nonconst_subrtx_bounds
271
272/* Use ITER to iterate over const_rtx X and its recursive subrtxes,
273 using subrtx_iterator::array ARRAY as the storage for the worklist.
274 ARRAY can be reused for multiple consecutive iterations but shouldn't
275 be shared by two concurrent iterations. TYPE is ALL if all subrtxes
276 are of interest or NONCONST if it is safe to ignore subrtxes of
277 constants. */
278#define FOR_EACH_SUBRTX(ITER, ARRAY, X, TYPE) \
279 for (subrtx_iterator ITER (ARRAY, X, TYPE##_BOUNDS); !ITER.at_end (); \
280 ITER.next ())
281
282/* Like FOR_EACH_SUBRTX, but iterate over subrtxes of an rtx X. */
283#define FOR_EACH_SUBRTX_VAR(ITER, ARRAY, X, TYPE) \
284 for (subrtx_var_iterator ITER (ARRAY, X, TYPE##_BOUNDS); !ITER.at_end (); \
285 ITER.next ())
286
287/* Like FOR_EACH_SUBRTX, but iterate over subrtx pointers of rtx pointer X.
288 For example, if X is &PATTERN (insn) and the pattern is a SET, iterate
289 over &PATTERN (insn), &SET_DEST (PATTERN (insn)), etc. */
290#define FOR_EACH_SUBRTX_PTR(ITER, ARRAY, X, TYPE) \
291 for (subrtx_ptr_iterator ITER (ARRAY, X, TYPE##_BOUNDS); !ITER.at_end (); \
292 ITER.next ())
Definition rtl-iter.h:60
value_type stack[LOCAL_ELEMS]
Definition rtl-iter.h:64
~array_type()
Definition rtl-iter.h:115
vec< value_type, va_heap, vl_embed > * heap
Definition rtl-iter.h:65
array_type()
Definition rtl-iter.h:112
Definition rtl-iter.h:52
T::value_type value_type
Definition rtl-iter.h:54
bool at_end() const
Definition rtl-iter.h:154
void next()
Definition rtl-iter.h:163
static size_t add_subrtxes_to_queue(array_type &, value_type *, size_t, rtx_type)
Definition rtlanal.cc:127
bool m_skip
Definition rtl-iter.h:99
bool m_substitute
Definition rtl-iter.h:102
array_type & m_array
Definition rtl-iter.h:81
static const size_t LOCAL_ELEMS
Definition rtl-iter.h:53
bool m_done
Definition rtl-iter.h:96
generic_subrtx_iterator(array_type &, value_type, const rtx_subrtx_bound_info *)
Definition rtl-iter.h:128
const rtx_subrtx_bound_info * m_bounds
Definition rtl-iter.h:78
T::rtunion_type rtunion_type
Definition rtl-iter.h:56
value_type m_current
Definition rtl-iter.h:84
value_type operator*() const
Definition rtl-iter.h:145
void skip_subrtxes()
Definition rtl-iter.h:221
value_type * m_base
Definition rtl-iter.h:87
void substitute(value_type)
Definition rtl-iter.h:230
static void free_array(array_type &)
Definition rtlanal.cc:188
T::rtx_type rtx_type
Definition rtl-iter.h:55
static value_type * add_single_to_queue(array_type &, value_type *, size_t, value_type)
Definition rtlanal.cc:86
size_t m_end
Definition rtl-iter.h:90
static unsigned int count[debug_counter_number_of_counters]
Definition dbgcnt.cc:50
T * ggc_alloc(ALONE_CXX_MEM_STAT_INFO)
Definition ggc.h:184
rtx_subrtx_bound_info rtx_nonconst_subrtx_bounds[]
Definition rtlanal.cc:63
generic_subrtx_iterator< rtx_var_accessor > subrtx_var_iterator
Definition rtl-iter.h:256
generic_subrtx_iterator< rtx_ptr_accessor > subrtx_ptr_iterator
Definition rtl-iter.h:267
rtx_subrtx_bound_info rtx_all_subrtx_bounds[]
Definition rtlanal.cc:62
bool leaf_code_p(enum rtx_code code)
Definition rtl-iter.h:43
generic_subrtx_iterator< const_rtx_accessor > subrtx_iterator
Definition rtl-iter.h:245
rtx_code
Definition rtl.h:48
#define GET_CODE(RTX)
Definition rtl.h:725
Definition tree-ssa-loop-niter.cc:64
Definition rtl-iter.h:238
const_rtx rtx_type
Definition rtl-iter.h:240
static value_type get_value(rtx_type x)
Definition rtl-iter.h:243
static rtx_type get_rtx(value_type x)
Definition rtl-iter.h:242
const_rtx value_type
Definition rtl-iter.h:239
const rtunion rtunion_type
Definition rtl-iter.h:241
Definition rtl.h:311
Definition rtl-iter.h:260
rtx rtx_type
Definition rtl-iter.h:262
rtx * value_type
Definition rtl-iter.h:261
static value_type get_value(rtx_type &x)
Definition rtl-iter.h:265
rtunion rtunion_type
Definition rtl-iter.h:263
static rtx_type get_rtx(value_type ptr)
Definition rtl-iter.h:264
Definition rtl-iter.h:33
unsigned char start
Definition rtl-iter.h:34
unsigned char count
Definition rtl-iter.h:35
Definition rtl-iter.h:249
static value_type get_value(rtx_type x)
Definition rtl-iter.h:254
rtx value_type
Definition rtl-iter.h:250
static rtx_type get_rtx(value_type x)
Definition rtl-iter.h:253
rtx rtx_type
Definition rtl-iter.h:251
rtunion rtunion_type
Definition rtl-iter.h:252
#define false
Definition system.h:895
#define UNLIKELY(x)
Definition system.h:759
#define LIKELY(x)
Definition system.h:758
Definition rtl.h:204