annotate gcc/rtl-iter.h @ 136:4627f235cf2a

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