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