2015-05-05 Yvan Roux <yvan.roux@linaro.org>
[official-gcc.git] / gcc / gimple-walk.c
blob53462b50201e29120d22f4b7d25d3532c2538caf
1 /* Gimple walk support.
3 Copyright (C) 2007-2015 Free Software Foundation, Inc.
4 Contributed by Aldy Hernandez <aldyh@redhat.com>
6 This file is part of GCC.
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.
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.
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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "hash-set.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "inchash.h"
33 #include "tree.h"
34 #include "predict.h"
35 #include "hard-reg-set.h"
36 #include "function.h"
37 #include "gimple-expr.h"
38 #include "is-a.h"
39 #include "tree-ssa-alias.h"
40 #include "basic-block.h"
41 #include "fold-const.h"
42 #include "gimple.h"
43 #include "gimple-iterator.h"
44 #include "gimple-walk.h"
45 #include "stmt.h"
47 /* Walk all the statements in the sequence *PSEQ calling walk_gimple_stmt
48 on each one. WI is as in walk_gimple_stmt.
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.
56 Otherwise, all the statements are walked and NULL returned. */
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)
62 gimple_stmt_iterator gsi;
64 for (gsi = gsi_start (*pseq); !gsi_end_p (gsi); )
66 tree ret = walk_gimple_stmt (&gsi, callback_stmt, callback_op, wi);
67 if (ret)
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;
74 return wi->removed_stmt ? NULL : gsi_stmt (gsi);
77 if (!wi->removed_stmt)
78 gsi_next (&gsi);
81 if (wi)
82 wi->callback_result = NULL_TREE;
84 return NULL;
88 /* Like walk_gimple_seq_mod, but ensure that the head of SEQ isn't
89 changed by the callbacks. */
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)
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;
102 /* Helper function for walk_gimple_stmt. Walk operands of a GIMPLE_ASM. */
104 static tree
105 walk_gimple_asm (gasm *stmt, walk_tree_fn callback_op,
106 struct walk_stmt_info *wi)
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;
115 noutputs = gimple_asm_noutputs (stmt);
116 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
118 if (wi)
119 wi->is_lhs = true;
121 for (i = 0; i < noutputs; i++)
123 op = gimple_asm_output_op (stmt, i);
124 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
125 oconstraints[i] = constraint;
126 if (wi)
128 if (parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
129 &allows_reg, &is_inout))
130 wi->val_only = (allows_reg || !allows_mem);
132 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
133 if (ret)
134 return ret;
137 n = gimple_asm_ninputs (stmt);
138 for (i = 0; i < n; i++)
140 op = gimple_asm_input_op (stmt, i);
141 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
143 if (wi)
145 if (parse_input_constraint (&constraint, 0, 0, noutputs, 0,
146 oconstraints, &allows_mem, &allows_reg))
148 wi->val_only = (allows_reg || !allows_mem);
149 /* Although input "m" is not really a LHS, we need a lvalue. */
150 wi->is_lhs = !wi->val_only;
153 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
154 if (ret)
155 return ret;
158 if (wi)
160 wi->is_lhs = false;
161 wi->val_only = true;
164 n = gimple_asm_nlabels (stmt);
165 for (i = 0; i < n; i++)
167 op = gimple_asm_label_op (stmt, i);
168 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
169 if (ret)
170 return ret;
173 return NULL_TREE;
177 /* Helper function of WALK_GIMPLE_STMT. Walk every tree operand in
178 STMT. CALLBACK_OP and WI are as in WALK_GIMPLE_STMT.
180 CALLBACK_OP is called on each operand of STMT via walk_tree.
181 Additional parameters to walk_tree must be stored in WI. For each operand
182 OP, walk_tree is called as:
184 walk_tree (&OP, CALLBACK_OP, WI, WI->PSET)
186 If CALLBACK_OP returns non-NULL for an operand, the remaining
187 operands are not scanned.
189 The return value is that returned by the last call to walk_tree, or
190 NULL_TREE if no CALLBACK_OP is specified. */
192 tree
193 walk_gimple_op (gimple stmt, walk_tree_fn callback_op,
194 struct walk_stmt_info *wi)
196 hash_set<tree> *pset = (wi) ? wi->pset : NULL;
197 unsigned i;
198 tree ret = NULL_TREE;
200 switch (gimple_code (stmt))
202 case GIMPLE_ASSIGN:
203 /* Walk the RHS operands. If the LHS is of a non-renamable type or
204 is a register variable, we may use a COMPONENT_REF on the RHS. */
205 if (wi)
207 tree lhs = gimple_assign_lhs (stmt);
208 wi->val_only
209 = (is_gimple_reg_type (TREE_TYPE (lhs)) && !is_gimple_reg (lhs))
210 || gimple_assign_rhs_class (stmt) != GIMPLE_SINGLE_RHS;
213 for (i = 1; i < gimple_num_ops (stmt); i++)
215 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi,
216 pset);
217 if (ret)
218 return ret;
221 /* Walk the LHS. If the RHS is appropriate for a memory, we
222 may use a COMPONENT_REF on the LHS. */
223 if (wi)
225 /* If the RHS is of a non-renamable type or is a register variable,
226 we may use a COMPONENT_REF on the LHS. */
227 tree rhs1 = gimple_assign_rhs1 (stmt);
228 wi->val_only
229 = (is_gimple_reg_type (TREE_TYPE (rhs1)) && !is_gimple_reg (rhs1))
230 || gimple_assign_rhs_class (stmt) != GIMPLE_SINGLE_RHS;
231 wi->is_lhs = true;
234 ret = walk_tree (gimple_op_ptr (stmt, 0), callback_op, wi, pset);
235 if (ret)
236 return ret;
238 if (wi)
240 wi->val_only = true;
241 wi->is_lhs = false;
243 break;
245 case GIMPLE_CALL:
246 if (wi)
248 wi->is_lhs = false;
249 wi->val_only = true;
252 ret = walk_tree (gimple_call_chain_ptr (as_a <gcall *> (stmt)),
253 callback_op, wi, pset);
254 if (ret)
255 return ret;
257 ret = walk_tree (gimple_call_fn_ptr (stmt), callback_op, wi, pset);
258 if (ret)
259 return ret;
261 for (i = 0; i < gimple_call_num_args (stmt); i++)
263 if (wi)
264 wi->val_only
265 = is_gimple_reg_type (TREE_TYPE (gimple_call_arg (stmt, i)));
266 ret = walk_tree (gimple_call_arg_ptr (stmt, i), callback_op, wi,
267 pset);
268 if (ret)
269 return ret;
272 if (gimple_call_lhs (stmt))
274 if (wi)
276 wi->is_lhs = true;
277 wi->val_only
278 = is_gimple_reg_type (TREE_TYPE (gimple_call_lhs (stmt)));
281 ret = walk_tree (gimple_call_lhs_ptr (stmt), callback_op, wi, pset);
282 if (ret)
283 return ret;
286 if (wi)
288 wi->is_lhs = false;
289 wi->val_only = true;
291 break;
293 case GIMPLE_CATCH:
294 ret = walk_tree (gimple_catch_types_ptr (as_a <gcatch *> (stmt)),
295 callback_op, wi, pset);
296 if (ret)
297 return ret;
298 break;
300 case GIMPLE_EH_FILTER:
301 ret = walk_tree (gimple_eh_filter_types_ptr (stmt), callback_op, wi,
302 pset);
303 if (ret)
304 return ret;
305 break;
307 case GIMPLE_ASM:
308 ret = walk_gimple_asm (as_a <gasm *> (stmt), callback_op, wi);
309 if (ret)
310 return ret;
311 break;
313 case GIMPLE_OMP_CONTINUE:
315 gomp_continue *cont_stmt = as_a <gomp_continue *> (stmt);
316 ret = walk_tree (gimple_omp_continue_control_def_ptr (cont_stmt),
317 callback_op, wi, pset);
318 if (ret)
319 return ret;
321 ret = walk_tree (gimple_omp_continue_control_use_ptr (cont_stmt),
322 callback_op, wi, pset);
323 if (ret)
324 return ret;
326 break;
328 case GIMPLE_OMP_CRITICAL:
330 gomp_critical *omp_stmt = as_a <gomp_critical *> (stmt);
331 ret = walk_tree (gimple_omp_critical_name_ptr (omp_stmt),
332 callback_op, wi, pset);
333 if (ret)
334 return ret;
336 break;
338 case GIMPLE_OMP_FOR:
339 ret = walk_tree (gimple_omp_for_clauses_ptr (stmt), callback_op, wi,
340 pset);
341 if (ret)
342 return ret;
343 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
345 ret = walk_tree (gimple_omp_for_index_ptr (stmt, i), callback_op,
346 wi, pset);
347 if (ret)
348 return ret;
349 ret = walk_tree (gimple_omp_for_initial_ptr (stmt, i), callback_op,
350 wi, pset);
351 if (ret)
352 return ret;
353 ret = walk_tree (gimple_omp_for_final_ptr (stmt, i), callback_op,
354 wi, pset);
355 if (ret)
356 return ret;
357 ret = walk_tree (gimple_omp_for_incr_ptr (stmt, i), callback_op,
358 wi, pset);
359 if (ret)
360 return ret;
362 break;
364 case GIMPLE_OMP_PARALLEL:
366 gomp_parallel *omp_par_stmt = as_a <gomp_parallel *> (stmt);
367 ret = walk_tree (gimple_omp_parallel_clauses_ptr (omp_par_stmt),
368 callback_op, wi, pset);
369 if (ret)
370 return ret;
371 ret = walk_tree (gimple_omp_parallel_child_fn_ptr (omp_par_stmt),
372 callback_op, wi, pset);
373 if (ret)
374 return ret;
375 ret = walk_tree (gimple_omp_parallel_data_arg_ptr (omp_par_stmt),
376 callback_op, wi, pset);
377 if (ret)
378 return ret;
380 break;
382 case GIMPLE_OMP_TASK:
383 ret = walk_tree (gimple_omp_task_clauses_ptr (stmt), callback_op,
384 wi, pset);
385 if (ret)
386 return ret;
387 ret = walk_tree (gimple_omp_task_child_fn_ptr (stmt), callback_op,
388 wi, pset);
389 if (ret)
390 return ret;
391 ret = walk_tree (gimple_omp_task_data_arg_ptr (stmt), callback_op,
392 wi, pset);
393 if (ret)
394 return ret;
395 ret = walk_tree (gimple_omp_task_copy_fn_ptr (stmt), callback_op,
396 wi, pset);
397 if (ret)
398 return ret;
399 ret = walk_tree (gimple_omp_task_arg_size_ptr (stmt), callback_op,
400 wi, pset);
401 if (ret)
402 return ret;
403 ret = walk_tree (gimple_omp_task_arg_align_ptr (stmt), callback_op,
404 wi, pset);
405 if (ret)
406 return ret;
407 break;
409 case GIMPLE_OMP_SECTIONS:
410 ret = walk_tree (gimple_omp_sections_clauses_ptr (stmt), callback_op,
411 wi, pset);
412 if (ret)
413 return ret;
414 ret = walk_tree (gimple_omp_sections_control_ptr (stmt), callback_op,
415 wi, pset);
416 if (ret)
417 return ret;
419 break;
421 case GIMPLE_OMP_SINGLE:
422 ret = walk_tree (gimple_omp_single_clauses_ptr (stmt), callback_op, wi,
423 pset);
424 if (ret)
425 return ret;
426 break;
428 case GIMPLE_OMP_TARGET:
430 gomp_target *omp_stmt = as_a <gomp_target *> (stmt);
431 ret = walk_tree (gimple_omp_target_clauses_ptr (omp_stmt),
432 callback_op, wi, pset);
433 if (ret)
434 return ret;
435 ret = walk_tree (gimple_omp_target_child_fn_ptr (omp_stmt),
436 callback_op, wi, pset);
437 if (ret)
438 return ret;
439 ret = walk_tree (gimple_omp_target_data_arg_ptr (omp_stmt),
440 callback_op, wi, pset);
441 if (ret)
442 return ret;
444 break;
446 case GIMPLE_OMP_TEAMS:
447 ret = walk_tree (gimple_omp_teams_clauses_ptr (stmt), callback_op, wi,
448 pset);
449 if (ret)
450 return ret;
451 break;
453 case GIMPLE_OMP_ATOMIC_LOAD:
455 gomp_atomic_load *omp_stmt = as_a <gomp_atomic_load *> (stmt);
456 ret = walk_tree (gimple_omp_atomic_load_lhs_ptr (omp_stmt),
457 callback_op, wi, pset);
458 if (ret)
459 return ret;
460 ret = walk_tree (gimple_omp_atomic_load_rhs_ptr (omp_stmt),
461 callback_op, wi, pset);
462 if (ret)
463 return ret;
465 break;
467 case GIMPLE_OMP_ATOMIC_STORE:
469 gomp_atomic_store *omp_stmt = as_a <gomp_atomic_store *> (stmt);
470 ret = walk_tree (gimple_omp_atomic_store_val_ptr (omp_stmt),
471 callback_op, wi, pset);
472 if (ret)
473 return ret;
475 break;
477 case GIMPLE_TRANSACTION:
478 ret = walk_tree (gimple_transaction_label_ptr (
479 as_a <gtransaction *> (stmt)),
480 callback_op, wi, pset);
481 if (ret)
482 return ret;
483 break;
485 case GIMPLE_OMP_RETURN:
486 ret = walk_tree (gimple_omp_return_lhs_ptr (stmt), callback_op, wi,
487 pset);
488 if (ret)
489 return ret;
490 break;
492 /* Tuples that do not have operands. */
493 case GIMPLE_NOP:
494 case GIMPLE_RESX:
495 case GIMPLE_PREDICT:
496 break;
498 default:
500 enum gimple_statement_structure_enum gss;
501 gss = gimple_statement_structure (stmt);
502 if (gss == GSS_WITH_OPS || gss == GSS_WITH_MEM_OPS)
503 for (i = 0; i < gimple_num_ops (stmt); i++)
505 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi, pset);
506 if (ret)
507 return ret;
510 break;
513 return NULL_TREE;
517 /* Walk the current statement in GSI (optionally using traversal state
518 stored in WI). If WI is NULL, no state is kept during traversal.
519 The callback CALLBACK_STMT is called. If CALLBACK_STMT indicates
520 that it has handled all the operands of the statement, its return
521 value is returned. Otherwise, the return value from CALLBACK_STMT
522 is discarded and its operands are scanned.
524 If CALLBACK_STMT is NULL or it didn't handle the operands,
525 CALLBACK_OP is called on each operand of the statement via
526 walk_gimple_op. If walk_gimple_op returns non-NULL for any
527 operand, the remaining operands are not scanned. In this case, the
528 return value from CALLBACK_OP is returned.
530 In any other case, NULL_TREE is returned. */
532 tree
533 walk_gimple_stmt (gimple_stmt_iterator *gsi, walk_stmt_fn callback_stmt,
534 walk_tree_fn callback_op, struct walk_stmt_info *wi)
536 gimple ret;
537 tree tree_ret;
538 gimple stmt = gsi_stmt (*gsi);
540 if (wi)
542 wi->gsi = *gsi;
543 wi->removed_stmt = false;
545 if (wi->want_locations && gimple_has_location (stmt))
546 input_location = gimple_location (stmt);
549 ret = NULL;
551 /* Invoke the statement callback. Return if the callback handled
552 all of STMT operands by itself. */
553 if (callback_stmt)
555 bool handled_ops = false;
556 tree_ret = callback_stmt (gsi, &handled_ops, wi);
557 if (handled_ops)
558 return tree_ret;
560 /* If CALLBACK_STMT did not handle operands, it should not have
561 a value to return. */
562 gcc_assert (tree_ret == NULL);
564 if (wi && wi->removed_stmt)
565 return NULL;
567 /* Re-read stmt in case the callback changed it. */
568 stmt = gsi_stmt (*gsi);
571 /* If CALLBACK_OP is defined, invoke it on every operand of STMT. */
572 if (callback_op)
574 tree_ret = walk_gimple_op (stmt, callback_op, wi);
575 if (tree_ret)
576 return tree_ret;
579 /* If STMT can have statements inside (e.g. GIMPLE_BIND), walk them. */
580 switch (gimple_code (stmt))
582 case GIMPLE_BIND:
583 ret = walk_gimple_seq_mod (gimple_bind_body_ptr (as_a <gbind *> (stmt)),
584 callback_stmt, callback_op, wi);
585 if (ret)
586 return wi->callback_result;
587 break;
589 case GIMPLE_CATCH:
590 ret = walk_gimple_seq_mod (gimple_catch_handler_ptr (
591 as_a <gcatch *> (stmt)),
592 callback_stmt, callback_op, wi);
593 if (ret)
594 return wi->callback_result;
595 break;
597 case GIMPLE_EH_FILTER:
598 ret = walk_gimple_seq_mod (gimple_eh_filter_failure_ptr (stmt), callback_stmt,
599 callback_op, wi);
600 if (ret)
601 return wi->callback_result;
602 break;
604 case GIMPLE_EH_ELSE:
606 geh_else *eh_else_stmt = as_a <geh_else *> (stmt);
607 ret = walk_gimple_seq_mod (gimple_eh_else_n_body_ptr (eh_else_stmt),
608 callback_stmt, callback_op, wi);
609 if (ret)
610 return wi->callback_result;
611 ret = walk_gimple_seq_mod (gimple_eh_else_e_body_ptr (eh_else_stmt),
612 callback_stmt, callback_op, wi);
613 if (ret)
614 return wi->callback_result;
616 break;
618 case GIMPLE_TRY:
619 ret = walk_gimple_seq_mod (gimple_try_eval_ptr (stmt), callback_stmt, callback_op,
620 wi);
621 if (ret)
622 return wi->callback_result;
624 ret = walk_gimple_seq_mod (gimple_try_cleanup_ptr (stmt), callback_stmt,
625 callback_op, wi);
626 if (ret)
627 return wi->callback_result;
628 break;
630 case GIMPLE_OMP_FOR:
631 ret = walk_gimple_seq_mod (gimple_omp_for_pre_body_ptr (stmt), callback_stmt,
632 callback_op, wi);
633 if (ret)
634 return wi->callback_result;
636 /* FALL THROUGH. */
637 case GIMPLE_OMP_CRITICAL:
638 case GIMPLE_OMP_MASTER:
639 case GIMPLE_OMP_TASKGROUP:
640 case GIMPLE_OMP_ORDERED:
641 case GIMPLE_OMP_SECTION:
642 case GIMPLE_OMP_PARALLEL:
643 case GIMPLE_OMP_TASK:
644 case GIMPLE_OMP_SECTIONS:
645 case GIMPLE_OMP_SINGLE:
646 case GIMPLE_OMP_TARGET:
647 case GIMPLE_OMP_TEAMS:
648 ret = walk_gimple_seq_mod (gimple_omp_body_ptr (stmt), callback_stmt,
649 callback_op, wi);
650 if (ret)
651 return wi->callback_result;
652 break;
654 case GIMPLE_WITH_CLEANUP_EXPR:
655 ret = walk_gimple_seq_mod (gimple_wce_cleanup_ptr (stmt), callback_stmt,
656 callback_op, wi);
657 if (ret)
658 return wi->callback_result;
659 break;
661 case GIMPLE_TRANSACTION:
662 ret = walk_gimple_seq_mod (gimple_transaction_body_ptr (
663 as_a <gtransaction *> (stmt)),
664 callback_stmt, callback_op, wi);
665 if (ret)
666 return wi->callback_result;
667 break;
669 default:
670 gcc_assert (!gimple_has_substatements (stmt));
671 break;
674 return NULL;
677 /* From a tree operand OP return the base of a load or store operation
678 or NULL_TREE if OP is not a load or a store. */
680 static tree
681 get_base_loadstore (tree op)
683 while (handled_component_p (op))
684 op = TREE_OPERAND (op, 0);
685 if (DECL_P (op)
686 || INDIRECT_REF_P (op)
687 || TREE_CODE (op) == MEM_REF
688 || TREE_CODE (op) == TARGET_MEM_REF)
689 return op;
690 return NULL_TREE;
694 /* For the statement STMT call the callbacks VISIT_LOAD, VISIT_STORE and
695 VISIT_ADDR if non-NULL on loads, store and address-taken operands
696 passing the STMT, the base of the operand, the operand itself containing
697 the base and DATA to it. The base will be either a decl, an indirect
698 reference (including TARGET_MEM_REF) or the argument of an address
699 expression.
700 Returns the results of these callbacks or'ed. */
702 bool
703 walk_stmt_load_store_addr_ops (gimple stmt, void *data,
704 walk_stmt_load_store_addr_fn visit_load,
705 walk_stmt_load_store_addr_fn visit_store,
706 walk_stmt_load_store_addr_fn visit_addr)
708 bool ret = false;
709 unsigned i;
710 if (gimple_assign_single_p (stmt))
712 tree lhs, rhs, arg;
713 if (visit_store)
715 arg = gimple_assign_lhs (stmt);
716 lhs = get_base_loadstore (arg);
717 if (lhs)
718 ret |= visit_store (stmt, lhs, arg, data);
720 arg = gimple_assign_rhs1 (stmt);
721 rhs = arg;
722 while (handled_component_p (rhs))
723 rhs = TREE_OPERAND (rhs, 0);
724 if (visit_addr)
726 if (TREE_CODE (rhs) == ADDR_EXPR)
727 ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), arg, data);
728 else if (TREE_CODE (rhs) == TARGET_MEM_REF
729 && TREE_CODE (TMR_BASE (rhs)) == ADDR_EXPR)
730 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (rhs), 0), arg,
731 data);
732 else if (TREE_CODE (rhs) == OBJ_TYPE_REF
733 && TREE_CODE (OBJ_TYPE_REF_OBJECT (rhs)) == ADDR_EXPR)
734 ret |= visit_addr (stmt, TREE_OPERAND (OBJ_TYPE_REF_OBJECT (rhs),
735 0), arg, data);
736 else if (TREE_CODE (rhs) == CONSTRUCTOR)
738 unsigned int ix;
739 tree val;
741 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), ix, val)
742 if (TREE_CODE (val) == ADDR_EXPR)
743 ret |= visit_addr (stmt, TREE_OPERAND (val, 0), arg, data);
744 else if (TREE_CODE (val) == OBJ_TYPE_REF
745 && TREE_CODE (OBJ_TYPE_REF_OBJECT (val)) == ADDR_EXPR)
746 ret |= visit_addr (stmt,
747 TREE_OPERAND (OBJ_TYPE_REF_OBJECT (val),
748 0), arg, data);
750 lhs = gimple_assign_lhs (stmt);
751 if (TREE_CODE (lhs) == TARGET_MEM_REF
752 && TREE_CODE (TMR_BASE (lhs)) == ADDR_EXPR)
753 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (lhs), 0), lhs, data);
755 if (visit_load)
757 rhs = get_base_loadstore (rhs);
758 if (rhs)
759 ret |= visit_load (stmt, rhs, arg, data);
762 else if (visit_addr
763 && (is_gimple_assign (stmt)
764 || gimple_code (stmt) == GIMPLE_COND))
766 for (i = 0; i < gimple_num_ops (stmt); ++i)
768 tree op = gimple_op (stmt, i);
769 if (op == NULL_TREE)
771 else if (TREE_CODE (op) == ADDR_EXPR)
772 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
773 /* COND_EXPR and VCOND_EXPR rhs1 argument is a comparison
774 tree with two operands. */
775 else if (i == 1 && COMPARISON_CLASS_P (op))
777 if (TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
778 ret |= visit_addr (stmt, TREE_OPERAND (TREE_OPERAND (op, 0),
779 0), op, data);
780 if (TREE_CODE (TREE_OPERAND (op, 1)) == ADDR_EXPR)
781 ret |= visit_addr (stmt, TREE_OPERAND (TREE_OPERAND (op, 1),
782 0), op, data);
786 else if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
788 if (visit_store)
790 tree arg = gimple_call_lhs (call_stmt);
791 if (arg)
793 tree lhs = get_base_loadstore (arg);
794 if (lhs)
795 ret |= visit_store (stmt, lhs, arg, data);
798 if (visit_load || visit_addr)
799 for (i = 0; i < gimple_call_num_args (call_stmt); ++i)
801 tree arg = gimple_call_arg (call_stmt, i);
802 if (visit_addr
803 && TREE_CODE (arg) == ADDR_EXPR)
804 ret |= visit_addr (stmt, TREE_OPERAND (arg, 0), arg, data);
805 else if (visit_load)
807 tree rhs = get_base_loadstore (arg);
808 if (rhs)
809 ret |= visit_load (stmt, rhs, arg, data);
812 if (visit_addr
813 && gimple_call_chain (call_stmt)
814 && TREE_CODE (gimple_call_chain (call_stmt)) == ADDR_EXPR)
815 ret |= visit_addr (stmt, TREE_OPERAND (gimple_call_chain (call_stmt), 0),
816 gimple_call_chain (call_stmt), data);
817 if (visit_addr
818 && gimple_call_return_slot_opt_p (call_stmt)
819 && gimple_call_lhs (call_stmt) != NULL_TREE
820 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (call_stmt))))
821 ret |= visit_addr (stmt, gimple_call_lhs (call_stmt),
822 gimple_call_lhs (call_stmt), data);
824 else if (gasm *asm_stmt = dyn_cast <gasm *> (stmt))
826 unsigned noutputs;
827 const char *constraint;
828 const char **oconstraints;
829 bool allows_mem, allows_reg, is_inout;
830 noutputs = gimple_asm_noutputs (asm_stmt);
831 oconstraints = XALLOCAVEC (const char *, noutputs);
832 if (visit_store || visit_addr)
833 for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i)
835 tree link = gimple_asm_output_op (asm_stmt, i);
836 tree op = get_base_loadstore (TREE_VALUE (link));
837 if (op && visit_store)
838 ret |= visit_store (stmt, op, TREE_VALUE (link), data);
839 if (visit_addr)
841 constraint = TREE_STRING_POINTER
842 (TREE_VALUE (TREE_PURPOSE (link)));
843 oconstraints[i] = constraint;
844 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
845 &allows_reg, &is_inout);
846 if (op && !allows_reg && allows_mem)
847 ret |= visit_addr (stmt, op, TREE_VALUE (link), data);
850 if (visit_load || visit_addr)
851 for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
853 tree link = gimple_asm_input_op (asm_stmt, i);
854 tree op = TREE_VALUE (link);
855 if (visit_addr
856 && TREE_CODE (op) == ADDR_EXPR)
857 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
858 else if (visit_load || visit_addr)
860 op = get_base_loadstore (op);
861 if (op)
863 if (visit_load)
864 ret |= visit_load (stmt, op, TREE_VALUE (link), data);
865 if (visit_addr)
867 constraint = TREE_STRING_POINTER
868 (TREE_VALUE (TREE_PURPOSE (link)));
869 parse_input_constraint (&constraint, 0, 0, noutputs,
870 0, oconstraints,
871 &allows_mem, &allows_reg);
872 if (!allows_reg && allows_mem)
873 ret |= visit_addr (stmt, op, TREE_VALUE (link),
874 data);
880 else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
882 tree op = gimple_return_retval (return_stmt);
883 if (op)
885 if (visit_addr
886 && TREE_CODE (op) == ADDR_EXPR)
887 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
888 else if (visit_load)
890 tree base = get_base_loadstore (op);
891 if (base)
892 ret |= visit_load (stmt, base, op, data);
896 else if (visit_addr
897 && gimple_code (stmt) == GIMPLE_PHI)
899 for (i = 0; i < gimple_phi_num_args (stmt); ++i)
901 tree op = gimple_phi_arg_def (stmt, i);
902 if (TREE_CODE (op) == ADDR_EXPR)
903 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
906 else if (visit_addr
907 && gimple_code (stmt) == GIMPLE_GOTO)
909 tree op = gimple_goto_dest (stmt);
910 if (TREE_CODE (op) == ADDR_EXPR)
911 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
914 return ret;
917 /* Like walk_stmt_load_store_addr_ops but with NULL visit_addr. IPA-CP
918 should make a faster clone for this case. */
920 bool
921 walk_stmt_load_store_ops (gimple stmt, void *data,
922 walk_stmt_load_store_addr_fn visit_load,
923 walk_stmt_load_store_addr_fn visit_store)
925 return walk_stmt_load_store_addr_ops (stmt, data,
926 visit_load, visit_store, NULL);