]>
Commit | Line | Data |
---|---|---|
5be5c238 AM |
1 | /* Gimple walk support. |
2 | ||
aeee4812 | 3 | Copyright (C) 2007-2023 Free Software Foundation, Inc. |
5be5c238 AM |
4 | Contributed by Aldy Hernandez <aldyh@redhat.com> |
5 | ||
6 | This file is part of GCC. | |
7 | ||
8 | GCC is free software; you can redistribute it and/or modify it under | |
9 | the terms of the GNU General Public License as published by the Free | |
10 | Software Foundation; either version 3, or (at your option) any later | |
11 | version. | |
12 | ||
13 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with GCC; see the file COPYING3. If not see | |
20 | <http://www.gnu.org/licenses/>. */ | |
21 | ||
22 | #include "config.h" | |
23 | #include "system.h" | |
24 | #include "coretypes.h" | |
c7131fb2 | 25 | #include "backend.h" |
5be5c238 | 26 | #include "tree.h" |
c7131fb2 | 27 | #include "gimple.h" |
5be5c238 AM |
28 | #include "gimple-iterator.h" |
29 | #include "gimple-walk.h" | |
917c68f5 | 30 | #include "stmt.h" |
5be5c238 AM |
31 | |
32 | /* Walk all the statements in the sequence *PSEQ calling walk_gimple_stmt | |
33 | on each one. WI is as in walk_gimple_stmt. | |
34 | ||
35 | If walk_gimple_stmt returns non-NULL, the walk is stopped, and the | |
36 | value is stored in WI->CALLBACK_RESULT. Also, the statement that | |
37 | produced the value is returned if this statement has not been | |
38 | removed by a callback (wi->removed_stmt). If the statement has | |
39 | been removed, NULL is returned. | |
40 | ||
41 | Otherwise, all the statements are walked and NULL returned. */ | |
42 | ||
355fe088 | 43 | gimple * |
5be5c238 AM |
44 | walk_gimple_seq_mod (gimple_seq *pseq, walk_stmt_fn callback_stmt, |
45 | walk_tree_fn callback_op, struct walk_stmt_info *wi) | |
46 | { | |
47 | gimple_stmt_iterator gsi; | |
48 | ||
49 | for (gsi = gsi_start (*pseq); !gsi_end_p (gsi); ) | |
50 | { | |
51 | tree ret = walk_gimple_stmt (&gsi, callback_stmt, callback_op, wi); | |
52 | if (ret) | |
53 | { | |
54 | /* If CALLBACK_STMT or CALLBACK_OP return a value, WI must exist | |
55 | to hold it. */ | |
56 | gcc_assert (wi); | |
57 | wi->callback_result = ret; | |
58 | ||
59 | return wi->removed_stmt ? NULL : gsi_stmt (gsi); | |
60 | } | |
61 | ||
62 | if (!wi->removed_stmt) | |
63 | gsi_next (&gsi); | |
64 | } | |
65 | ||
66 | if (wi) | |
67 | wi->callback_result = NULL_TREE; | |
68 | ||
69 | return NULL; | |
70 | } | |
71 | ||
72 | ||
73 | /* Like walk_gimple_seq_mod, but ensure that the head of SEQ isn't | |
74 | changed by the callbacks. */ | |
75 | ||
355fe088 | 76 | gimple * |
5be5c238 AM |
77 | walk_gimple_seq (gimple_seq seq, walk_stmt_fn callback_stmt, |
78 | walk_tree_fn callback_op, struct walk_stmt_info *wi) | |
79 | { | |
80 | gimple_seq seq2 = seq; | |
355fe088 | 81 | gimple *ret = walk_gimple_seq_mod (&seq2, callback_stmt, callback_op, wi); |
5be5c238 AM |
82 | gcc_assert (seq2 == seq); |
83 | return ret; | |
84 | } | |
85 | ||
86 | ||
87 | /* Helper function for walk_gimple_stmt. Walk operands of a GIMPLE_ASM. */ | |
88 | ||
89 | static tree | |
538dd0b7 | 90 | walk_gimple_asm (gasm *stmt, walk_tree_fn callback_op, |
5be5c238 AM |
91 | struct walk_stmt_info *wi) |
92 | { | |
93 | tree ret, op; | |
94 | unsigned noutputs; | |
95 | const char **oconstraints; | |
96 | unsigned i, n; | |
97 | const char *constraint; | |
98 | bool allows_mem, allows_reg, is_inout; | |
99 | ||
100 | noutputs = gimple_asm_noutputs (stmt); | |
101 | oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *)); | |
102 | ||
5be5c238 AM |
103 | for (i = 0; i < noutputs; i++) |
104 | { | |
105 | op = gimple_asm_output_op (stmt, i); | |
106 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op))); | |
107 | oconstraints[i] = constraint; | |
5be5c238 | 108 | if (wi) |
917c68f5 BRF |
109 | { |
110 | if (parse_output_constraint (&constraint, i, 0, 0, &allows_mem, | |
111 | &allows_reg, &is_inout)) | |
112 | wi->val_only = (allows_reg || !allows_mem); | |
113 | } | |
d247f8e2 RB |
114 | if (wi) |
115 | wi->is_lhs = true; | |
5be5c238 AM |
116 | ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL); |
117 | if (ret) | |
118 | return ret; | |
119 | } | |
120 | ||
121 | n = gimple_asm_ninputs (stmt); | |
122 | for (i = 0; i < n; i++) | |
123 | { | |
124 | op = gimple_asm_input_op (stmt, i); | |
125 | constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op))); | |
917c68f5 | 126 | |
5be5c238 AM |
127 | if (wi) |
128 | { | |
917c68f5 BRF |
129 | if (parse_input_constraint (&constraint, 0, 0, noutputs, 0, |
130 | oconstraints, &allows_mem, &allows_reg)) | |
131 | { | |
132 | wi->val_only = (allows_reg || !allows_mem); | |
133 | /* Although input "m" is not really a LHS, we need a lvalue. */ | |
134 | wi->is_lhs = !wi->val_only; | |
135 | } | |
5be5c238 AM |
136 | } |
137 | ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL); | |
138 | if (ret) | |
139 | return ret; | |
140 | } | |
141 | ||
142 | if (wi) | |
143 | { | |
144 | wi->is_lhs = false; | |
145 | wi->val_only = true; | |
146 | } | |
147 | ||
148 | n = gimple_asm_nlabels (stmt); | |
149 | for (i = 0; i < n; i++) | |
150 | { | |
151 | op = gimple_asm_label_op (stmt, i); | |
152 | ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL); | |
153 | if (ret) | |
154 | return ret; | |
155 | } | |
156 | ||
157 | return NULL_TREE; | |
158 | } | |
159 | ||
160 | ||
161 | /* Helper function of WALK_GIMPLE_STMT. Walk every tree operand in | |
162 | STMT. CALLBACK_OP and WI are as in WALK_GIMPLE_STMT. | |
163 | ||
164 | CALLBACK_OP is called on each operand of STMT via walk_tree. | |
165 | Additional parameters to walk_tree must be stored in WI. For each operand | |
166 | OP, walk_tree is called as: | |
167 | ||
168 | walk_tree (&OP, CALLBACK_OP, WI, WI->PSET) | |
169 | ||
170 | If CALLBACK_OP returns non-NULL for an operand, the remaining | |
171 | operands are not scanned. | |
172 | ||
173 | The return value is that returned by the last call to walk_tree, or | |
174 | NULL_TREE if no CALLBACK_OP is specified. */ | |
175 | ||
176 | tree | |
355fe088 | 177 | walk_gimple_op (gimple *stmt, walk_tree_fn callback_op, |
5be5c238 AM |
178 | struct walk_stmt_info *wi) |
179 | { | |
6e2830c3 | 180 | hash_set<tree> *pset = (wi) ? wi->pset : NULL; |
5be5c238 AM |
181 | unsigned i; |
182 | tree ret = NULL_TREE; | |
183 | ||
d247f8e2 RB |
184 | if (wi) |
185 | wi->stmt = stmt; | |
186 | ||
5be5c238 AM |
187 | switch (gimple_code (stmt)) |
188 | { | |
189 | case GIMPLE_ASSIGN: | |
190 | /* Walk the RHS operands. If the LHS is of a non-renamable type or | |
191 | is a register variable, we may use a COMPONENT_REF on the RHS. */ | |
192 | if (wi) | |
193 | { | |
194 | tree lhs = gimple_assign_lhs (stmt); | |
195 | wi->val_only | |
196 | = (is_gimple_reg_type (TREE_TYPE (lhs)) && !is_gimple_reg (lhs)) | |
197 | || gimple_assign_rhs_class (stmt) != GIMPLE_SINGLE_RHS; | |
198 | } | |
199 | ||
200 | for (i = 1; i < gimple_num_ops (stmt); i++) | |
201 | { | |
202 | ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi, | |
203 | pset); | |
204 | if (ret) | |
205 | return ret; | |
206 | } | |
207 | ||
208 | /* Walk the LHS. If the RHS is appropriate for a memory, we | |
209 | may use a COMPONENT_REF on the LHS. */ | |
210 | if (wi) | |
211 | { | |
212 | /* If the RHS is of a non-renamable type or is a register variable, | |
213 | we may use a COMPONENT_REF on the LHS. */ | |
214 | tree rhs1 = gimple_assign_rhs1 (stmt); | |
215 | wi->val_only | |
216 | = (is_gimple_reg_type (TREE_TYPE (rhs1)) && !is_gimple_reg (rhs1)) | |
217 | || gimple_assign_rhs_class (stmt) != GIMPLE_SINGLE_RHS; | |
218 | wi->is_lhs = true; | |
219 | } | |
220 | ||
221 | ret = walk_tree (gimple_op_ptr (stmt, 0), callback_op, wi, pset); | |
222 | if (ret) | |
223 | return ret; | |
224 | ||
225 | if (wi) | |
226 | { | |
227 | wi->val_only = true; | |
228 | wi->is_lhs = false; | |
229 | } | |
230 | break; | |
231 | ||
232 | case GIMPLE_CALL: | |
233 | if (wi) | |
234 | { | |
235 | wi->is_lhs = false; | |
236 | wi->val_only = true; | |
237 | } | |
238 | ||
538dd0b7 DM |
239 | ret = walk_tree (gimple_call_chain_ptr (as_a <gcall *> (stmt)), |
240 | callback_op, wi, pset); | |
5be5c238 AM |
241 | if (ret) |
242 | return ret; | |
243 | ||
244 | ret = walk_tree (gimple_call_fn_ptr (stmt), callback_op, wi, pset); | |
245 | if (ret) | |
246 | return ret; | |
247 | ||
248 | for (i = 0; i < gimple_call_num_args (stmt); i++) | |
249 | { | |
250 | if (wi) | |
251 | wi->val_only | |
252 | = is_gimple_reg_type (TREE_TYPE (gimple_call_arg (stmt, i))); | |
253 | ret = walk_tree (gimple_call_arg_ptr (stmt, i), callback_op, wi, | |
254 | pset); | |
255 | if (ret) | |
256 | return ret; | |
257 | } | |
258 | ||
259 | if (gimple_call_lhs (stmt)) | |
260 | { | |
261 | if (wi) | |
262 | { | |
263 | wi->is_lhs = true; | |
264 | wi->val_only | |
265 | = is_gimple_reg_type (TREE_TYPE (gimple_call_lhs (stmt))); | |
266 | } | |
267 | ||
268 | ret = walk_tree (gimple_call_lhs_ptr (stmt), callback_op, wi, pset); | |
269 | if (ret) | |
270 | return ret; | |
271 | } | |
272 | ||
273 | if (wi) | |
274 | { | |
275 | wi->is_lhs = false; | |
276 | wi->val_only = true; | |
277 | } | |
278 | break; | |
279 | ||
280 | case GIMPLE_CATCH: | |
538dd0b7 DM |
281 | ret = walk_tree (gimple_catch_types_ptr (as_a <gcatch *> (stmt)), |
282 | callback_op, wi, pset); | |
5be5c238 AM |
283 | if (ret) |
284 | return ret; | |
285 | break; | |
286 | ||
287 | case GIMPLE_EH_FILTER: | |
288 | ret = walk_tree (gimple_eh_filter_types_ptr (stmt), callback_op, wi, | |
289 | pset); | |
290 | if (ret) | |
291 | return ret; | |
292 | break; | |
293 | ||
294 | case GIMPLE_ASM: | |
538dd0b7 | 295 | ret = walk_gimple_asm (as_a <gasm *> (stmt), callback_op, wi); |
5be5c238 AM |
296 | if (ret) |
297 | return ret; | |
298 | break; | |
299 | ||
300 | case GIMPLE_OMP_CONTINUE: | |
538dd0b7 DM |
301 | { |
302 | gomp_continue *cont_stmt = as_a <gomp_continue *> (stmt); | |
303 | ret = walk_tree (gimple_omp_continue_control_def_ptr (cont_stmt), | |
304 | callback_op, wi, pset); | |
305 | if (ret) | |
306 | return ret; | |
307 | ||
308 | ret = walk_tree (gimple_omp_continue_control_use_ptr (cont_stmt), | |
309 | callback_op, wi, pset); | |
310 | if (ret) | |
311 | return ret; | |
312 | } | |
5be5c238 AM |
313 | break; |
314 | ||
315 | case GIMPLE_OMP_CRITICAL: | |
8e25a61a TS |
316 | { |
317 | gomp_critical *omp_stmt = as_a <gomp_critical *> (stmt); | |
318 | ret = walk_tree (gimple_omp_critical_name_ptr (omp_stmt), | |
319 | callback_op, wi, pset); | |
320 | if (ret) | |
321 | return ret; | |
d9a6bd32 JJ |
322 | ret = walk_tree (gimple_omp_critical_clauses_ptr (omp_stmt), |
323 | callback_op, wi, pset); | |
324 | if (ret) | |
325 | return ret; | |
326 | } | |
327 | break; | |
328 | ||
329 | case GIMPLE_OMP_ORDERED: | |
330 | { | |
331 | gomp_ordered *omp_stmt = as_a <gomp_ordered *> (stmt); | |
332 | ret = walk_tree (gimple_omp_ordered_clauses_ptr (omp_stmt), | |
333 | callback_op, wi, pset); | |
334 | if (ret) | |
335 | return ret; | |
8e25a61a | 336 | } |
5be5c238 AM |
337 | break; |
338 | ||
bf38f7e9 JJ |
339 | case GIMPLE_OMP_SCAN: |
340 | { | |
341 | gomp_scan *scan_stmt = as_a <gomp_scan *> (stmt); | |
342 | ret = walk_tree (gimple_omp_scan_clauses_ptr (scan_stmt), | |
343 | callback_op, wi, pset); | |
344 | if (ret) | |
345 | return ret; | |
346 | } | |
347 | break; | |
348 | ||
5be5c238 AM |
349 | case GIMPLE_OMP_FOR: |
350 | ret = walk_tree (gimple_omp_for_clauses_ptr (stmt), callback_op, wi, | |
351 | pset); | |
352 | if (ret) | |
353 | return ret; | |
354 | for (i = 0; i < gimple_omp_for_collapse (stmt); i++) | |
355 | { | |
356 | ret = walk_tree (gimple_omp_for_index_ptr (stmt, i), callback_op, | |
357 | wi, pset); | |
358 | if (ret) | |
359 | return ret; | |
360 | ret = walk_tree (gimple_omp_for_initial_ptr (stmt, i), callback_op, | |
361 | wi, pset); | |
362 | if (ret) | |
363 | return ret; | |
364 | ret = walk_tree (gimple_omp_for_final_ptr (stmt, i), callback_op, | |
365 | wi, pset); | |
366 | if (ret) | |
367 | return ret; | |
368 | ret = walk_tree (gimple_omp_for_incr_ptr (stmt, i), callback_op, | |
369 | wi, pset); | |
8e25a61a TS |
370 | if (ret) |
371 | return ret; | |
5be5c238 | 372 | } |
5be5c238 AM |
373 | break; |
374 | ||
375 | case GIMPLE_OMP_PARALLEL: | |
538dd0b7 DM |
376 | { |
377 | gomp_parallel *omp_par_stmt = as_a <gomp_parallel *> (stmt); | |
378 | ret = walk_tree (gimple_omp_parallel_clauses_ptr (omp_par_stmt), | |
379 | callback_op, wi, pset); | |
380 | if (ret) | |
381 | return ret; | |
382 | ret = walk_tree (gimple_omp_parallel_child_fn_ptr (omp_par_stmt), | |
383 | callback_op, wi, pset); | |
384 | if (ret) | |
385 | return ret; | |
386 | ret = walk_tree (gimple_omp_parallel_data_arg_ptr (omp_par_stmt), | |
387 | callback_op, wi, pset); | |
388 | if (ret) | |
389 | return ret; | |
390 | } | |
5be5c238 AM |
391 | break; |
392 | ||
393 | case GIMPLE_OMP_TASK: | |
394 | ret = walk_tree (gimple_omp_task_clauses_ptr (stmt), callback_op, | |
395 | wi, pset); | |
396 | if (ret) | |
397 | return ret; | |
398 | ret = walk_tree (gimple_omp_task_child_fn_ptr (stmt), callback_op, | |
399 | wi, pset); | |
400 | if (ret) | |
401 | return ret; | |
402 | ret = walk_tree (gimple_omp_task_data_arg_ptr (stmt), callback_op, | |
403 | wi, pset); | |
404 | if (ret) | |
405 | return ret; | |
406 | ret = walk_tree (gimple_omp_task_copy_fn_ptr (stmt), callback_op, | |
407 | wi, pset); | |
408 | if (ret) | |
409 | return ret; | |
410 | ret = walk_tree (gimple_omp_task_arg_size_ptr (stmt), callback_op, | |
411 | wi, pset); | |
412 | if (ret) | |
413 | return ret; | |
414 | ret = walk_tree (gimple_omp_task_arg_align_ptr (stmt), callback_op, | |
415 | wi, pset); | |
416 | if (ret) | |
417 | return ret; | |
418 | break; | |
419 | ||
420 | case GIMPLE_OMP_SECTIONS: | |
421 | ret = walk_tree (gimple_omp_sections_clauses_ptr (stmt), callback_op, | |
422 | wi, pset); | |
423 | if (ret) | |
424 | return ret; | |
5be5c238 AM |
425 | ret = walk_tree (gimple_omp_sections_control_ptr (stmt), callback_op, |
426 | wi, pset); | |
427 | if (ret) | |
428 | return ret; | |
429 | ||
430 | break; | |
431 | ||
432 | case GIMPLE_OMP_SINGLE: | |
433 | ret = walk_tree (gimple_omp_single_clauses_ptr (stmt), callback_op, wi, | |
434 | pset); | |
435 | if (ret) | |
436 | return ret; | |
437 | break; | |
438 | ||
439 | case GIMPLE_OMP_TARGET: | |
8e25a61a TS |
440 | { |
441 | gomp_target *omp_stmt = as_a <gomp_target *> (stmt); | |
442 | ret = walk_tree (gimple_omp_target_clauses_ptr (omp_stmt), | |
443 | callback_op, wi, pset); | |
444 | if (ret) | |
445 | return ret; | |
446 | ret = walk_tree (gimple_omp_target_child_fn_ptr (omp_stmt), | |
447 | callback_op, wi, pset); | |
448 | if (ret) | |
449 | return ret; | |
450 | ret = walk_tree (gimple_omp_target_data_arg_ptr (omp_stmt), | |
451 | callback_op, wi, pset); | |
452 | if (ret) | |
453 | return ret; | |
454 | } | |
5be5c238 AM |
455 | break; |
456 | ||
457 | case GIMPLE_OMP_TEAMS: | |
458 | ret = walk_tree (gimple_omp_teams_clauses_ptr (stmt), callback_op, wi, | |
459 | pset); | |
460 | if (ret) | |
461 | return ret; | |
462 | break; | |
463 | ||
464 | case GIMPLE_OMP_ATOMIC_LOAD: | |
538dd0b7 DM |
465 | { |
466 | gomp_atomic_load *omp_stmt = as_a <gomp_atomic_load *> (stmt); | |
467 | ret = walk_tree (gimple_omp_atomic_load_lhs_ptr (omp_stmt), | |
468 | callback_op, wi, pset); | |
469 | if (ret) | |
470 | return ret; | |
538dd0b7 DM |
471 | ret = walk_tree (gimple_omp_atomic_load_rhs_ptr (omp_stmt), |
472 | callback_op, wi, pset); | |
473 | if (ret) | |
474 | return ret; | |
475 | } | |
5be5c238 AM |
476 | break; |
477 | ||
478 | case GIMPLE_OMP_ATOMIC_STORE: | |
8e25a61a TS |
479 | { |
480 | gomp_atomic_store *omp_stmt = as_a <gomp_atomic_store *> (stmt); | |
481 | ret = walk_tree (gimple_omp_atomic_store_val_ptr (omp_stmt), | |
482 | callback_op, wi, pset); | |
483 | if (ret) | |
484 | return ret; | |
485 | } | |
5be5c238 AM |
486 | break; |
487 | ||
4dda30e9 JJ |
488 | case GIMPLE_ASSUME: |
489 | ret = walk_tree (gimple_assume_guard_ptr (stmt), callback_op, wi, pset); | |
490 | if (ret) | |
491 | return ret; | |
492 | break; | |
493 | ||
5be5c238 | 494 | case GIMPLE_TRANSACTION: |
7c11b0fe RH |
495 | { |
496 | gtransaction *txn = as_a <gtransaction *> (stmt); | |
497 | ||
498 | ret = walk_tree (gimple_transaction_label_norm_ptr (txn), | |
499 | callback_op, wi, pset); | |
500 | if (ret) | |
501 | return ret; | |
502 | ret = walk_tree (gimple_transaction_label_uninst_ptr (txn), | |
503 | callback_op, wi, pset); | |
504 | if (ret) | |
505 | return ret; | |
506 | ret = walk_tree (gimple_transaction_label_over_ptr (txn), | |
507 | callback_op, wi, pset); | |
508 | if (ret) | |
509 | return ret; | |
510 | } | |
5be5c238 AM |
511 | break; |
512 | ||
513 | case GIMPLE_OMP_RETURN: | |
514 | ret = walk_tree (gimple_omp_return_lhs_ptr (stmt), callback_op, wi, | |
515 | pset); | |
516 | if (ret) | |
517 | return ret; | |
518 | break; | |
519 | ||
520 | /* Tuples that do not have operands. */ | |
521 | case GIMPLE_NOP: | |
522 | case GIMPLE_RESX: | |
523 | case GIMPLE_PREDICT: | |
524 | break; | |
525 | ||
f80c4eac RB |
526 | case GIMPLE_PHI: |
527 | /* PHIs are not GSS_WITH_OPS so we need to handle them explicitely. */ | |
528 | { | |
529 | gphi *phi = as_a <gphi *> (stmt); | |
530 | if (wi) | |
531 | { | |
532 | wi->val_only = true; | |
533 | wi->is_lhs = true; | |
534 | } | |
535 | ret = walk_tree (gimple_phi_result_ptr (phi), callback_op, wi, pset); | |
536 | if (wi) | |
537 | wi->is_lhs = false; | |
538 | if (ret) | |
539 | return ret; | |
540 | for (unsigned i = 0; i < gimple_phi_num_args (phi); ++i) | |
541 | { | |
542 | ret = walk_tree (gimple_phi_arg_def_ptr (phi, i), | |
543 | callback_op, wi, pset); | |
544 | if (ret) | |
545 | return ret; | |
546 | } | |
547 | break; | |
548 | } | |
549 | ||
5be5c238 AM |
550 | default: |
551 | { | |
552 | enum gimple_statement_structure_enum gss; | |
553 | gss = gimple_statement_structure (stmt); | |
554 | if (gss == GSS_WITH_OPS || gss == GSS_WITH_MEM_OPS) | |
555 | for (i = 0; i < gimple_num_ops (stmt); i++) | |
556 | { | |
557 | ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi, pset); | |
558 | if (ret) | |
559 | return ret; | |
560 | } | |
561 | } | |
562 | break; | |
563 | } | |
564 | ||
565 | return NULL_TREE; | |
566 | } | |
567 | ||
568 | ||
569 | /* Walk the current statement in GSI (optionally using traversal state | |
570 | stored in WI). If WI is NULL, no state is kept during traversal. | |
571 | The callback CALLBACK_STMT is called. If CALLBACK_STMT indicates | |
572 | that it has handled all the operands of the statement, its return | |
573 | value is returned. Otherwise, the return value from CALLBACK_STMT | |
574 | is discarded and its operands are scanned. | |
575 | ||
576 | If CALLBACK_STMT is NULL or it didn't handle the operands, | |
577 | CALLBACK_OP is called on each operand of the statement via | |
578 | walk_gimple_op. If walk_gimple_op returns non-NULL for any | |
579 | operand, the remaining operands are not scanned. In this case, the | |
580 | return value from CALLBACK_OP is returned. | |
581 | ||
582 | In any other case, NULL_TREE is returned. */ | |
583 | ||
584 | tree | |
585 | walk_gimple_stmt (gimple_stmt_iterator *gsi, walk_stmt_fn callback_stmt, | |
586 | walk_tree_fn callback_op, struct walk_stmt_info *wi) | |
587 | { | |
355fe088 | 588 | gimple *ret; |
5be5c238 | 589 | tree tree_ret; |
355fe088 | 590 | gimple *stmt = gsi_stmt (*gsi); |
5be5c238 AM |
591 | |
592 | if (wi) | |
593 | { | |
594 | wi->gsi = *gsi; | |
595 | wi->removed_stmt = false; | |
596 | ||
597 | if (wi->want_locations && gimple_has_location (stmt)) | |
598 | input_location = gimple_location (stmt); | |
599 | } | |
600 | ||
601 | ret = NULL; | |
602 | ||
603 | /* Invoke the statement callback. Return if the callback handled | |
604 | all of STMT operands by itself. */ | |
605 | if (callback_stmt) | |
606 | { | |
607 | bool handled_ops = false; | |
608 | tree_ret = callback_stmt (gsi, &handled_ops, wi); | |
609 | if (handled_ops) | |
610 | return tree_ret; | |
611 | ||
612 | /* If CALLBACK_STMT did not handle operands, it should not have | |
613 | a value to return. */ | |
614 | gcc_assert (tree_ret == NULL); | |
615 | ||
616 | if (wi && wi->removed_stmt) | |
617 | return NULL; | |
618 | ||
619 | /* Re-read stmt in case the callback changed it. */ | |
620 | stmt = gsi_stmt (*gsi); | |
621 | } | |
622 | ||
623 | /* If CALLBACK_OP is defined, invoke it on every operand of STMT. */ | |
624 | if (callback_op) | |
625 | { | |
626 | tree_ret = walk_gimple_op (stmt, callback_op, wi); | |
627 | if (tree_ret) | |
628 | return tree_ret; | |
629 | } | |
630 | ||
631 | /* If STMT can have statements inside (e.g. GIMPLE_BIND), walk them. */ | |
632 | switch (gimple_code (stmt)) | |
633 | { | |
634 | case GIMPLE_BIND: | |
538dd0b7 DM |
635 | ret = walk_gimple_seq_mod (gimple_bind_body_ptr (as_a <gbind *> (stmt)), |
636 | callback_stmt, callback_op, wi); | |
5be5c238 AM |
637 | if (ret) |
638 | return wi->callback_result; | |
639 | break; | |
640 | ||
641 | case GIMPLE_CATCH: | |
538dd0b7 DM |
642 | ret = walk_gimple_seq_mod (gimple_catch_handler_ptr ( |
643 | as_a <gcatch *> (stmt)), | |
644 | callback_stmt, callback_op, wi); | |
5be5c238 AM |
645 | if (ret) |
646 | return wi->callback_result; | |
647 | break; | |
648 | ||
649 | case GIMPLE_EH_FILTER: | |
650 | ret = walk_gimple_seq_mod (gimple_eh_filter_failure_ptr (stmt), callback_stmt, | |
651 | callback_op, wi); | |
652 | if (ret) | |
653 | return wi->callback_result; | |
654 | break; | |
655 | ||
656 | case GIMPLE_EH_ELSE: | |
538dd0b7 DM |
657 | { |
658 | geh_else *eh_else_stmt = as_a <geh_else *> (stmt); | |
659 | ret = walk_gimple_seq_mod (gimple_eh_else_n_body_ptr (eh_else_stmt), | |
660 | callback_stmt, callback_op, wi); | |
661 | if (ret) | |
662 | return wi->callback_result; | |
663 | ret = walk_gimple_seq_mod (gimple_eh_else_e_body_ptr (eh_else_stmt), | |
664 | callback_stmt, callback_op, wi); | |
665 | if (ret) | |
666 | return wi->callback_result; | |
667 | } | |
5be5c238 AM |
668 | break; |
669 | ||
670 | case GIMPLE_TRY: | |
671 | ret = walk_gimple_seq_mod (gimple_try_eval_ptr (stmt), callback_stmt, callback_op, | |
672 | wi); | |
673 | if (ret) | |
674 | return wi->callback_result; | |
675 | ||
676 | ret = walk_gimple_seq_mod (gimple_try_cleanup_ptr (stmt), callback_stmt, | |
677 | callback_op, wi); | |
678 | if (ret) | |
679 | return wi->callback_result; | |
680 | break; | |
681 | ||
682 | case GIMPLE_OMP_FOR: | |
683 | ret = walk_gimple_seq_mod (gimple_omp_for_pre_body_ptr (stmt), callback_stmt, | |
684 | callback_op, wi); | |
685 | if (ret) | |
686 | return wi->callback_result; | |
687 | ||
688 | /* FALL THROUGH. */ | |
689 | case GIMPLE_OMP_CRITICAL: | |
690 | case GIMPLE_OMP_MASTER: | |
d0befed7 | 691 | case GIMPLE_OMP_MASKED: |
5be5c238 AM |
692 | case GIMPLE_OMP_TASKGROUP: |
693 | case GIMPLE_OMP_ORDERED: | |
bf38f7e9 | 694 | case GIMPLE_OMP_SCAN: |
5be5c238 AM |
695 | case GIMPLE_OMP_SECTION: |
696 | case GIMPLE_OMP_PARALLEL: | |
697 | case GIMPLE_OMP_TASK: | |
e45483c7 | 698 | case GIMPLE_OMP_SCOPE: |
5be5c238 AM |
699 | case GIMPLE_OMP_SECTIONS: |
700 | case GIMPLE_OMP_SINGLE: | |
701 | case GIMPLE_OMP_TARGET: | |
702 | case GIMPLE_OMP_TEAMS: | |
703 | ret = walk_gimple_seq_mod (gimple_omp_body_ptr (stmt), callback_stmt, | |
704 | callback_op, wi); | |
705 | if (ret) | |
706 | return wi->callback_result; | |
707 | break; | |
708 | ||
709 | case GIMPLE_WITH_CLEANUP_EXPR: | |
710 | ret = walk_gimple_seq_mod (gimple_wce_cleanup_ptr (stmt), callback_stmt, | |
711 | callback_op, wi); | |
712 | if (ret) | |
713 | return wi->callback_result; | |
714 | break; | |
715 | ||
4dda30e9 JJ |
716 | case GIMPLE_ASSUME: |
717 | ret = walk_gimple_seq_mod (gimple_assume_body_ptr (stmt), | |
718 | callback_stmt, callback_op, wi); | |
719 | if (ret) | |
720 | return wi->callback_result; | |
721 | break; | |
722 | ||
5be5c238 | 723 | case GIMPLE_TRANSACTION: |
538dd0b7 DM |
724 | ret = walk_gimple_seq_mod (gimple_transaction_body_ptr ( |
725 | as_a <gtransaction *> (stmt)), | |
5be5c238 AM |
726 | callback_stmt, callback_op, wi); |
727 | if (ret) | |
728 | return wi->callback_result; | |
729 | break; | |
730 | ||
731 | default: | |
732 | gcc_assert (!gimple_has_substatements (stmt)); | |
733 | break; | |
734 | } | |
735 | ||
736 | return NULL; | |
737 | } | |
738 | ||
739 | /* From a tree operand OP return the base of a load or store operation | |
740 | or NULL_TREE if OP is not a load or a store. */ | |
741 | ||
742 | static tree | |
743 | get_base_loadstore (tree op) | |
744 | { | |
745 | while (handled_component_p (op)) | |
746 | op = TREE_OPERAND (op, 0); | |
747 | if (DECL_P (op) | |
748 | || INDIRECT_REF_P (op) | |
749 | || TREE_CODE (op) == MEM_REF | |
750 | || TREE_CODE (op) == TARGET_MEM_REF) | |
751 | return op; | |
752 | return NULL_TREE; | |
753 | } | |
754 | ||
755 | ||
756 | /* For the statement STMT call the callbacks VISIT_LOAD, VISIT_STORE and | |
757 | VISIT_ADDR if non-NULL on loads, store and address-taken operands | |
9f1363cd JJ |
758 | passing the STMT, the base of the operand, the operand itself containing |
759 | the base and DATA to it. The base will be either a decl, an indirect | |
760 | reference (including TARGET_MEM_REF) or the argument of an address | |
761 | expression. | |
5be5c238 AM |
762 | Returns the results of these callbacks or'ed. */ |
763 | ||
764 | bool | |
355fe088 | 765 | walk_stmt_load_store_addr_ops (gimple *stmt, void *data, |
9f1363cd JJ |
766 | walk_stmt_load_store_addr_fn visit_load, |
767 | walk_stmt_load_store_addr_fn visit_store, | |
768 | walk_stmt_load_store_addr_fn visit_addr) | |
5be5c238 AM |
769 | { |
770 | bool ret = false; | |
771 | unsigned i; | |
772 | if (gimple_assign_single_p (stmt)) | |
773 | { | |
9f1363cd | 774 | tree lhs, rhs, arg; |
5be5c238 AM |
775 | if (visit_store) |
776 | { | |
9f1363cd JJ |
777 | arg = gimple_assign_lhs (stmt); |
778 | lhs = get_base_loadstore (arg); | |
5be5c238 | 779 | if (lhs) |
9f1363cd | 780 | ret |= visit_store (stmt, lhs, arg, data); |
5be5c238 | 781 | } |
9f1363cd JJ |
782 | arg = gimple_assign_rhs1 (stmt); |
783 | rhs = arg; | |
5be5c238 AM |
784 | while (handled_component_p (rhs)) |
785 | rhs = TREE_OPERAND (rhs, 0); | |
786 | if (visit_addr) | |
787 | { | |
788 | if (TREE_CODE (rhs) == ADDR_EXPR) | |
9f1363cd | 789 | ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), arg, data); |
5be5c238 AM |
790 | else if (TREE_CODE (rhs) == OBJ_TYPE_REF |
791 | && TREE_CODE (OBJ_TYPE_REF_OBJECT (rhs)) == ADDR_EXPR) | |
792 | ret |= visit_addr (stmt, TREE_OPERAND (OBJ_TYPE_REF_OBJECT (rhs), | |
9f1363cd | 793 | 0), arg, data); |
5be5c238 AM |
794 | else if (TREE_CODE (rhs) == CONSTRUCTOR) |
795 | { | |
796 | unsigned int ix; | |
797 | tree val; | |
798 | ||
799 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), ix, val) | |
800 | if (TREE_CODE (val) == ADDR_EXPR) | |
9f1363cd | 801 | ret |= visit_addr (stmt, TREE_OPERAND (val, 0), arg, data); |
5be5c238 AM |
802 | else if (TREE_CODE (val) == OBJ_TYPE_REF |
803 | && TREE_CODE (OBJ_TYPE_REF_OBJECT (val)) == ADDR_EXPR) | |
804 | ret |= visit_addr (stmt, | |
805 | TREE_OPERAND (OBJ_TYPE_REF_OBJECT (val), | |
9f1363cd | 806 | 0), arg, data); |
5be5c238 | 807 | } |
5be5c238 AM |
808 | } |
809 | if (visit_load) | |
810 | { | |
811 | rhs = get_base_loadstore (rhs); | |
812 | if (rhs) | |
9f1363cd | 813 | ret |= visit_load (stmt, rhs, arg, data); |
5be5c238 AM |
814 | } |
815 | } | |
816 | else if (visit_addr | |
817 | && (is_gimple_assign (stmt) | |
818 | || gimple_code (stmt) == GIMPLE_COND)) | |
819 | { | |
820 | for (i = 0; i < gimple_num_ops (stmt); ++i) | |
821 | { | |
822 | tree op = gimple_op (stmt, i); | |
823 | if (op == NULL_TREE) | |
824 | ; | |
825 | else if (TREE_CODE (op) == ADDR_EXPR) | |
9f1363cd | 826 | ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data); |
5be5c238 AM |
827 | /* COND_EXPR and VCOND_EXPR rhs1 argument is a comparison |
828 | tree with two operands. */ | |
829 | else if (i == 1 && COMPARISON_CLASS_P (op)) | |
830 | { | |
831 | if (TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR) | |
832 | ret |= visit_addr (stmt, TREE_OPERAND (TREE_OPERAND (op, 0), | |
9f1363cd | 833 | 0), op, data); |
5be5c238 AM |
834 | if (TREE_CODE (TREE_OPERAND (op, 1)) == ADDR_EXPR) |
835 | ret |= visit_addr (stmt, TREE_OPERAND (TREE_OPERAND (op, 1), | |
9f1363cd | 836 | 0), op, data); |
5be5c238 AM |
837 | } |
838 | } | |
839 | } | |
538dd0b7 | 840 | else if (gcall *call_stmt = dyn_cast <gcall *> (stmt)) |
5be5c238 AM |
841 | { |
842 | if (visit_store) | |
843 | { | |
538dd0b7 | 844 | tree arg = gimple_call_lhs (call_stmt); |
9f1363cd | 845 | if (arg) |
5be5c238 | 846 | { |
9f1363cd | 847 | tree lhs = get_base_loadstore (arg); |
5be5c238 | 848 | if (lhs) |
9f1363cd | 849 | ret |= visit_store (stmt, lhs, arg, data); |
5be5c238 AM |
850 | } |
851 | } | |
852 | if (visit_load || visit_addr) | |
538dd0b7 | 853 | for (i = 0; i < gimple_call_num_args (call_stmt); ++i) |
5be5c238 | 854 | { |
538dd0b7 | 855 | tree arg = gimple_call_arg (call_stmt, i); |
5be5c238 | 856 | if (visit_addr |
9f1363cd JJ |
857 | && TREE_CODE (arg) == ADDR_EXPR) |
858 | ret |= visit_addr (stmt, TREE_OPERAND (arg, 0), arg, data); | |
5be5c238 AM |
859 | else if (visit_load) |
860 | { | |
9f1363cd | 861 | tree rhs = get_base_loadstore (arg); |
5be5c238 | 862 | if (rhs) |
9f1363cd | 863 | ret |= visit_load (stmt, rhs, arg, data); |
5be5c238 AM |
864 | } |
865 | } | |
866 | if (visit_addr | |
538dd0b7 DM |
867 | && gimple_call_chain (call_stmt) |
868 | && TREE_CODE (gimple_call_chain (call_stmt)) == ADDR_EXPR) | |
869 | ret |= visit_addr (stmt, TREE_OPERAND (gimple_call_chain (call_stmt), 0), | |
870 | gimple_call_chain (call_stmt), data); | |
5be5c238 | 871 | if (visit_addr |
538dd0b7 DM |
872 | && gimple_call_return_slot_opt_p (call_stmt) |
873 | && gimple_call_lhs (call_stmt) != NULL_TREE | |
874 | && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (call_stmt)))) | |
875 | ret |= visit_addr (stmt, gimple_call_lhs (call_stmt), | |
876 | gimple_call_lhs (call_stmt), data); | |
5be5c238 | 877 | } |
538dd0b7 | 878 | else if (gasm *asm_stmt = dyn_cast <gasm *> (stmt)) |
5be5c238 AM |
879 | { |
880 | unsigned noutputs; | |
881 | const char *constraint; | |
882 | const char **oconstraints; | |
883 | bool allows_mem, allows_reg, is_inout; | |
538dd0b7 | 884 | noutputs = gimple_asm_noutputs (asm_stmt); |
5be5c238 AM |
885 | oconstraints = XALLOCAVEC (const char *, noutputs); |
886 | if (visit_store || visit_addr) | |
538dd0b7 | 887 | for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i) |
5be5c238 | 888 | { |
538dd0b7 | 889 | tree link = gimple_asm_output_op (asm_stmt, i); |
5be5c238 AM |
890 | tree op = get_base_loadstore (TREE_VALUE (link)); |
891 | if (op && visit_store) | |
9f1363cd | 892 | ret |= visit_store (stmt, op, TREE_VALUE (link), data); |
5be5c238 AM |
893 | if (visit_addr) |
894 | { | |
895 | constraint = TREE_STRING_POINTER | |
896 | (TREE_VALUE (TREE_PURPOSE (link))); | |
897 | oconstraints[i] = constraint; | |
898 | parse_output_constraint (&constraint, i, 0, 0, &allows_mem, | |
899 | &allows_reg, &is_inout); | |
900 | if (op && !allows_reg && allows_mem) | |
9f1363cd | 901 | ret |= visit_addr (stmt, op, TREE_VALUE (link), data); |
5be5c238 AM |
902 | } |
903 | } | |
904 | if (visit_load || visit_addr) | |
538dd0b7 | 905 | for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i) |
5be5c238 | 906 | { |
538dd0b7 | 907 | tree link = gimple_asm_input_op (asm_stmt, i); |
5be5c238 AM |
908 | tree op = TREE_VALUE (link); |
909 | if (visit_addr | |
910 | && TREE_CODE (op) == ADDR_EXPR) | |
9f1363cd | 911 | ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data); |
5be5c238 AM |
912 | else if (visit_load || visit_addr) |
913 | { | |
914 | op = get_base_loadstore (op); | |
915 | if (op) | |
916 | { | |
917 | if (visit_load) | |
9f1363cd | 918 | ret |= visit_load (stmt, op, TREE_VALUE (link), data); |
5be5c238 AM |
919 | if (visit_addr) |
920 | { | |
921 | constraint = TREE_STRING_POINTER | |
922 | (TREE_VALUE (TREE_PURPOSE (link))); | |
923 | parse_input_constraint (&constraint, 0, 0, noutputs, | |
924 | 0, oconstraints, | |
925 | &allows_mem, &allows_reg); | |
926 | if (!allows_reg && allows_mem) | |
9f1363cd JJ |
927 | ret |= visit_addr (stmt, op, TREE_VALUE (link), |
928 | data); | |
5be5c238 AM |
929 | } |
930 | } | |
931 | } | |
932 | } | |
933 | } | |
538dd0b7 | 934 | else if (greturn *return_stmt = dyn_cast <greturn *> (stmt)) |
5be5c238 | 935 | { |
538dd0b7 | 936 | tree op = gimple_return_retval (return_stmt); |
5be5c238 AM |
937 | if (op) |
938 | { | |
939 | if (visit_addr | |
940 | && TREE_CODE (op) == ADDR_EXPR) | |
9f1363cd | 941 | ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data); |
5be5c238 AM |
942 | else if (visit_load) |
943 | { | |
9f1363cd JJ |
944 | tree base = get_base_loadstore (op); |
945 | if (base) | |
946 | ret |= visit_load (stmt, base, op, data); | |
5be5c238 AM |
947 | } |
948 | } | |
949 | } | |
950 | else if (visit_addr | |
951 | && gimple_code (stmt) == GIMPLE_PHI) | |
952 | { | |
953 | for (i = 0; i < gimple_phi_num_args (stmt); ++i) | |
954 | { | |
955 | tree op = gimple_phi_arg_def (stmt, i); | |
956 | if (TREE_CODE (op) == ADDR_EXPR) | |
9f1363cd | 957 | ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data); |
5be5c238 AM |
958 | } |
959 | } | |
960 | else if (visit_addr | |
961 | && gimple_code (stmt) == GIMPLE_GOTO) | |
962 | { | |
963 | tree op = gimple_goto_dest (stmt); | |
964 | if (TREE_CODE (op) == ADDR_EXPR) | |
9f1363cd | 965 | ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data); |
5be5c238 AM |
966 | } |
967 | ||
968 | return ret; | |
969 | } | |
970 | ||
971 | /* Like walk_stmt_load_store_addr_ops but with NULL visit_addr. IPA-CP | |
972 | should make a faster clone for this case. */ | |
973 | ||
974 | bool | |
355fe088 | 975 | walk_stmt_load_store_ops (gimple *stmt, void *data, |
9f1363cd JJ |
976 | walk_stmt_load_store_addr_fn visit_load, |
977 | walk_stmt_load_store_addr_fn visit_store) | |
5be5c238 AM |
978 | { |
979 | return walk_stmt_load_store_addr_ops (stmt, data, | |
980 | visit_load, visit_store, NULL); | |
981 | } |