Convert remaining gimple_omp_single_ accessors to be typesafe
[official-gcc.git] / gcc / gimple-walk.c
blob5882fe6eb117c1ad473191865c89149f0bdd1a10
1 /* Gimple walk support.
3 Copyright (C) 2007-2014 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 "tree.h"
27 #include "stmt.h"
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"
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"
41 #include "gimple.h"
42 #include "gimple-iterator.h"
43 #include "gimple-walk.h"
44 #include "gimple-walk.h"
45 #include "demangle.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 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;
135 n = gimple_asm_ninputs (stmt);
136 for (i = 0; i < n; i++)
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)
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;
148 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
149 if (ret)
150 return ret;
153 if (wi)
155 wi->is_lhs = false;
156 wi->val_only = true;
159 n = gimple_asm_nlabels (stmt);
160 for (i = 0; i < n; i++)
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;
168 return NULL_TREE;
172 /* Helper function of WALK_GIMPLE_STMT. Walk every tree operand in
173 STMT. CALLBACK_OP and WI are as in WALK_GIMPLE_STMT.
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:
179 walk_tree (&OP, CALLBACK_OP, WI, WI->PSET)
181 If CALLBACK_OP returns non-NULL for an operand, the remaining
182 operands are not scanned.
184 The return value is that returned by the last call to walk_tree, or
185 NULL_TREE if no CALLBACK_OP is specified. */
187 tree
188 walk_gimple_op (gimple stmt, walk_tree_fn callback_op,
189 struct walk_stmt_info *wi)
191 hash_set<tree> *pset = (wi) ? wi->pset : NULL;
192 unsigned i;
193 tree ret = NULL_TREE;
195 switch (gimple_code (stmt))
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)
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;
208 for (i = 1; i < gimple_num_ops (stmt); i++)
210 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi,
211 pset);
212 if (ret)
213 return ret;
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)
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;
229 ret = walk_tree (gimple_op_ptr (stmt, 0), callback_op, wi, pset);
230 if (ret)
231 return ret;
233 if (wi)
235 wi->val_only = true;
236 wi->is_lhs = false;
238 break;
240 case GIMPLE_CALL:
241 if (wi)
243 wi->is_lhs = false;
244 wi->val_only = true;
247 ret = walk_tree (gimple_call_chain_ptr (as_a <gcall *> (stmt)),
248 callback_op, wi, pset);
249 if (ret)
250 return ret;
252 ret = walk_tree (gimple_call_fn_ptr (stmt), callback_op, wi, pset);
253 if (ret)
254 return ret;
256 for (i = 0; i < gimple_call_num_args (stmt); i++)
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;
267 if (gimple_call_lhs (stmt))
269 if (wi)
271 wi->is_lhs = true;
272 wi->val_only
273 = is_gimple_reg_type (TREE_TYPE (gimple_call_lhs (stmt)));
276 ret = walk_tree (gimple_call_lhs_ptr (stmt), callback_op, wi, pset);
277 if (ret)
278 return ret;
281 if (wi)
283 wi->is_lhs = false;
284 wi->val_only = true;
286 break;
288 case GIMPLE_CATCH:
289 ret = walk_tree (gimple_catch_types_ptr (as_a <gcatch *> (stmt)),
290 callback_op, wi, pset);
291 if (ret)
292 return ret;
293 break;
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;
302 case GIMPLE_ASM:
303 ret = walk_gimple_asm (as_a <gasm *> (stmt), callback_op, wi);
304 if (ret)
305 return ret;
306 break;
308 case GIMPLE_OMP_CONTINUE:
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;
316 ret = walk_tree (gimple_omp_continue_control_use_ptr (cont_stmt),
317 callback_op, wi, pset);
318 if (ret)
319 return ret;
321 break;
323 case GIMPLE_OMP_CRITICAL:
324 ret = walk_tree (gimple_omp_critical_name_ptr (
325 as_a <gomp_critical *> (stmt)),
326 callback_op, wi, pset);
327 if (ret)
328 return ret;
329 break;
331 case GIMPLE_OMP_FOR:
333 gomp_for *omp_for_stmt = as_a <gomp_for *> (stmt);
334 ret = walk_tree (gimple_omp_for_clauses_ptr (omp_for_stmt), callback_op, wi,
335 pset);
336 if (ret)
337 return ret;
338 for (i = 0; i < gimple_omp_for_collapse (omp_for_stmt); i++)
340 ret = walk_tree (gimple_omp_for_index_ptr (omp_for_stmt, i),
341 callback_op, wi, pset);
342 if (ret)
343 return ret;
344 ret = walk_tree (gimple_omp_for_initial_ptr (omp_for_stmt, i),
345 callback_op, wi, pset);
346 if (ret)
347 return ret;
348 ret = walk_tree (gimple_omp_for_final_ptr (omp_for_stmt, i),
349 callback_op, wi, pset);
350 if (ret)
351 return ret;
352 ret = walk_tree (gimple_omp_for_incr_ptr (omp_for_stmt, i),
353 callback_op, wi, pset);
355 if (ret)
356 return ret;
358 break;
360 case GIMPLE_OMP_PARALLEL:
362 gomp_parallel *omp_par_stmt = as_a <gomp_parallel *> (stmt);
363 ret = walk_tree (gimple_omp_parallel_clauses_ptr (omp_par_stmt),
364 callback_op, wi, pset);
365 if (ret)
366 return ret;
367 ret = walk_tree (gimple_omp_parallel_child_fn_ptr (omp_par_stmt),
368 callback_op, wi, pset);
369 if (ret)
370 return ret;
371 ret = walk_tree (gimple_omp_parallel_data_arg_ptr (omp_par_stmt),
372 callback_op, wi, pset);
373 if (ret)
374 return ret;
376 break;
378 case GIMPLE_OMP_TASK:
380 gomp_task *omp_task = as_a <gomp_task *> (stmt);
381 ret = walk_tree (gimple_omp_task_clauses_ptr (omp_task), callback_op,
382 wi, pset);
383 if (ret)
384 return ret;
385 ret = walk_tree (gimple_omp_task_child_fn_ptr (omp_task), callback_op,
386 wi, pset);
387 if (ret)
388 return ret;
389 ret = walk_tree (gimple_omp_task_data_arg_ptr (omp_task), callback_op,
390 wi, pset);
391 if (ret)
392 return ret;
393 ret = walk_tree (gimple_omp_task_copy_fn_ptr (omp_task), callback_op,
394 wi, pset);
395 if (ret)
396 return ret;
397 ret = walk_tree (gimple_omp_task_arg_size_ptr (omp_task), callback_op,
398 wi, pset);
399 if (ret)
400 return ret;
401 ret = walk_tree (gimple_omp_task_arg_align_ptr (omp_task), callback_op,
402 wi, pset);
403 if (ret)
404 return ret;
406 break;
408 case GIMPLE_OMP_SECTIONS:
410 gomp_sections *omp_sections_stmt = as_a <gomp_sections *> (stmt);
411 ret = walk_tree (gimple_omp_sections_clauses_ptr (omp_sections_stmt), callback_op,
412 wi, pset);
413 if (ret)
414 return ret;
416 ret = walk_tree (gimple_omp_sections_control_ptr (omp_sections_stmt), callback_op,
417 wi, pset);
418 if (ret)
419 return ret;
421 break;
423 case GIMPLE_OMP_SINGLE:
424 ret = walk_tree (gimple_omp_single_clauses_ptr (
425 as_a <gomp_single *> (stmt)),
426 callback_op, wi, pset);
427 if (ret)
428 return ret;
429 break;
431 case GIMPLE_OMP_TARGET:
432 ret = walk_tree (gimple_omp_target_clauses_ptr (stmt), callback_op, wi,
433 pset);
434 if (ret)
435 return ret;
436 break;
438 case GIMPLE_OMP_TEAMS:
439 ret = walk_tree (gimple_omp_teams_clauses_ptr (
440 as_a <gomp_teams *> (stmt)),
441 callback_op, wi, pset);
442 if (ret)
443 return ret;
444 break;
446 case GIMPLE_OMP_ATOMIC_LOAD:
448 gomp_atomic_load *omp_stmt = as_a <gomp_atomic_load *> (stmt);
449 ret = walk_tree (gimple_omp_atomic_load_lhs_ptr (omp_stmt),
450 callback_op, wi, pset);
451 if (ret)
452 return ret;
454 ret = walk_tree (gimple_omp_atomic_load_rhs_ptr (omp_stmt),
455 callback_op, wi, pset);
456 if (ret)
457 return ret;
459 break;
461 case GIMPLE_OMP_ATOMIC_STORE:
462 ret = walk_tree (gimple_omp_atomic_store_val_ptr (
463 as_a <gomp_atomic_store *> (stmt)),
464 callback_op, wi, pset);
465 if (ret)
466 return ret;
467 break;
469 case GIMPLE_TRANSACTION:
470 ret = walk_tree (gimple_transaction_label_ptr (
471 as_a <gtransaction *> (stmt)),
472 callback_op, wi, pset);
473 if (ret)
474 return ret;
475 break;
477 case GIMPLE_OMP_RETURN:
478 ret = walk_tree (gimple_omp_return_lhs_ptr (
479 as_a <gomp_return *> (stmt)),
480 callback_op, wi, pset);
481 if (ret)
482 return ret;
483 break;
485 /* Tuples that do not have operands. */
486 case GIMPLE_NOP:
487 case GIMPLE_RESX:
488 case GIMPLE_PREDICT:
489 break;
491 default:
493 enum gimple_statement_structure_enum gss;
494 gss = gimple_statement_structure (stmt);
495 if (gss == GSS_WITH_OPS || gss == GSS_WITH_MEM_OPS)
496 for (i = 0; i < gimple_num_ops (stmt); i++)
498 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi, pset);
499 if (ret)
500 return ret;
503 break;
506 return NULL_TREE;
510 /* Walk the current statement in GSI (optionally using traversal state
511 stored in WI). If WI is NULL, no state is kept during traversal.
512 The callback CALLBACK_STMT is called. If CALLBACK_STMT indicates
513 that it has handled all the operands of the statement, its return
514 value is returned. Otherwise, the return value from CALLBACK_STMT
515 is discarded and its operands are scanned.
517 If CALLBACK_STMT is NULL or it didn't handle the operands,
518 CALLBACK_OP is called on each operand of the statement via
519 walk_gimple_op. If walk_gimple_op returns non-NULL for any
520 operand, the remaining operands are not scanned. In this case, the
521 return value from CALLBACK_OP is returned.
523 In any other case, NULL_TREE is returned. */
525 tree
526 walk_gimple_stmt (gimple_stmt_iterator *gsi, walk_stmt_fn callback_stmt,
527 walk_tree_fn callback_op, struct walk_stmt_info *wi)
529 gimple ret;
530 tree tree_ret;
531 gimple stmt = gsi_stmt (*gsi);
533 if (wi)
535 wi->gsi = *gsi;
536 wi->removed_stmt = false;
538 if (wi->want_locations && gimple_has_location (stmt))
539 input_location = gimple_location (stmt);
542 ret = NULL;
544 /* Invoke the statement callback. Return if the callback handled
545 all of STMT operands by itself. */
546 if (callback_stmt)
548 bool handled_ops = false;
549 tree_ret = callback_stmt (gsi, &handled_ops, wi);
550 if (handled_ops)
551 return tree_ret;
553 /* If CALLBACK_STMT did not handle operands, it should not have
554 a value to return. */
555 gcc_assert (tree_ret == NULL);
557 if (wi && wi->removed_stmt)
558 return NULL;
560 /* Re-read stmt in case the callback changed it. */
561 stmt = gsi_stmt (*gsi);
564 /* If CALLBACK_OP is defined, invoke it on every operand of STMT. */
565 if (callback_op)
567 tree_ret = walk_gimple_op (stmt, callback_op, wi);
568 if (tree_ret)
569 return tree_ret;
572 /* If STMT can have statements inside (e.g. GIMPLE_BIND), walk them. */
573 switch (gimple_code (stmt))
575 case GIMPLE_BIND:
576 ret =
577 walk_gimple_seq_mod (gimple_bind_body_ptr (as_a <gbind *> (stmt)),
578 callback_stmt, callback_op, wi);
579 if (ret)
580 return wi->callback_result;
581 break;
583 case GIMPLE_CATCH:
584 ret = walk_gimple_seq_mod (gimple_catch_handler_ptr (
585 as_a <gcatch *> (stmt)),
586 callback_stmt, callback_op, wi);
587 if (ret)
588 return wi->callback_result;
589 break;
591 case GIMPLE_EH_FILTER:
592 ret = walk_gimple_seq_mod (gimple_eh_filter_failure_ptr (stmt), callback_stmt,
593 callback_op, wi);
594 if (ret)
595 return wi->callback_result;
596 break;
598 case GIMPLE_EH_ELSE:
600 geh_else *eh_else_stmt = as_a <geh_else *> (stmt);
601 ret = walk_gimple_seq_mod (gimple_eh_else_n_body_ptr (eh_else_stmt),
602 callback_stmt, callback_op, wi);
603 if (ret)
604 return wi->callback_result;
605 ret = walk_gimple_seq_mod (gimple_eh_else_e_body_ptr (eh_else_stmt),
606 callback_stmt, callback_op, wi);
607 if (ret)
608 return wi->callback_result;
610 break;
612 case GIMPLE_TRY:
614 gtry *try_stmt = as_a <gtry *> (stmt);
615 ret = walk_gimple_seq_mod (gimple_try_eval_ptr (try_stmt),
616 callback_stmt, callback_op,
617 wi);
618 if (ret)
619 return wi->callback_result;
621 ret = walk_gimple_seq_mod (gimple_try_cleanup_ptr (try_stmt),
622 callback_stmt,
623 callback_op, wi);
624 if (ret)
625 return wi->callback_result;
627 break;
629 case GIMPLE_OMP_FOR:
630 ret = walk_gimple_seq_mod (gimple_omp_for_pre_body_ptr (
631 as_a <gomp_for *> (stmt)),
632 callback_stmt, 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 (as_a <gwce *> (stmt)),
656 callback_stmt,
657 callback_op, wi);
658 if (ret)
659 return wi->callback_result;
660 break;
662 case GIMPLE_TRANSACTION:
663 ret = walk_gimple_seq_mod (gimple_transaction_body_ptr (
664 as_a <gtransaction *> (stmt)),
665 callback_stmt, callback_op, wi);
666 if (ret)
667 return wi->callback_result;
668 break;
670 default:
671 gcc_assert (!gimple_has_substatements (stmt));
672 break;
675 return NULL;
678 /* From a tree operand OP return the base of a load or store operation
679 or NULL_TREE if OP is not a load or a store. */
681 static tree
682 get_base_loadstore (tree op)
684 while (handled_component_p (op))
685 op = TREE_OPERAND (op, 0);
686 if (DECL_P (op)
687 || INDIRECT_REF_P (op)
688 || TREE_CODE (op) == MEM_REF
689 || TREE_CODE (op) == TARGET_MEM_REF)
690 return op;
691 return NULL_TREE;
695 /* For the statement STMT call the callbacks VISIT_LOAD, VISIT_STORE and
696 VISIT_ADDR if non-NULL on loads, store and address-taken operands
697 passing the STMT, the base of the operand, the operand itself containing
698 the base and DATA to it. The base will be either a decl, an indirect
699 reference (including TARGET_MEM_REF) or the argument of an address
700 expression.
701 Returns the results of these callbacks or'ed. */
703 bool
704 walk_stmt_load_store_addr_ops (gimple stmt, void *data,
705 walk_stmt_load_store_addr_fn visit_load,
706 walk_stmt_load_store_addr_fn visit_store,
707 walk_stmt_load_store_addr_fn visit_addr)
709 bool ret = false;
710 unsigned i;
711 if (gimple_assign_single_p (stmt))
713 tree lhs, rhs, arg;
714 if (visit_store)
716 arg = gimple_assign_lhs (stmt);
717 lhs = get_base_loadstore (arg);
718 if (lhs)
719 ret |= visit_store (stmt, lhs, arg, data);
721 arg = gimple_assign_rhs1 (stmt);
722 rhs = arg;
723 while (handled_component_p (rhs))
724 rhs = TREE_OPERAND (rhs, 0);
725 if (visit_addr)
727 if (TREE_CODE (rhs) == ADDR_EXPR)
728 ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), arg, data);
729 else if (TREE_CODE (rhs) == TARGET_MEM_REF
730 && TREE_CODE (TMR_BASE (rhs)) == ADDR_EXPR)
731 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (rhs), 0), arg,
732 data);
733 else if (TREE_CODE (rhs) == OBJ_TYPE_REF
734 && TREE_CODE (OBJ_TYPE_REF_OBJECT (rhs)) == ADDR_EXPR)
735 ret |= visit_addr (stmt, TREE_OPERAND (OBJ_TYPE_REF_OBJECT (rhs),
736 0), arg, data);
737 else if (TREE_CODE (rhs) == CONSTRUCTOR)
739 unsigned int ix;
740 tree val;
742 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), ix, val)
743 if (TREE_CODE (val) == ADDR_EXPR)
744 ret |= visit_addr (stmt, TREE_OPERAND (val, 0), arg, data);
745 else if (TREE_CODE (val) == OBJ_TYPE_REF
746 && TREE_CODE (OBJ_TYPE_REF_OBJECT (val)) == ADDR_EXPR)
747 ret |= visit_addr (stmt,
748 TREE_OPERAND (OBJ_TYPE_REF_OBJECT (val),
749 0), arg, data);
751 lhs = gimple_assign_lhs (stmt);
752 if (TREE_CODE (lhs) == TARGET_MEM_REF
753 && TREE_CODE (TMR_BASE (lhs)) == ADDR_EXPR)
754 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (lhs), 0), lhs, data);
756 if (visit_load)
758 rhs = get_base_loadstore (rhs);
759 if (rhs)
760 ret |= visit_load (stmt, rhs, arg, data);
763 else if (visit_addr
764 && (is_gimple_assign (stmt)
765 || gimple_code (stmt) == GIMPLE_COND))
767 for (i = 0; i < gimple_num_ops (stmt); ++i)
769 tree op = gimple_op (stmt, i);
770 if (op == NULL_TREE)
772 else if (TREE_CODE (op) == ADDR_EXPR)
773 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
774 /* COND_EXPR and VCOND_EXPR rhs1 argument is a comparison
775 tree with two operands. */
776 else if (i == 1 && COMPARISON_CLASS_P (op))
778 if (TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
779 ret |= visit_addr (stmt, TREE_OPERAND (TREE_OPERAND (op, 0),
780 0), op, data);
781 if (TREE_CODE (TREE_OPERAND (op, 1)) == ADDR_EXPR)
782 ret |= visit_addr (stmt, TREE_OPERAND (TREE_OPERAND (op, 1),
783 0), op, data);
787 else if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
789 if (visit_store)
791 tree arg = gimple_call_lhs (call_stmt);
792 if (arg)
794 tree lhs = get_base_loadstore (arg);
795 if (lhs)
796 ret |= visit_store (stmt, lhs, arg, data);
799 if (visit_load || visit_addr)
800 for (i = 0; i < gimple_call_num_args (call_stmt); ++i)
802 tree arg = gimple_call_arg (call_stmt, i);
803 if (visit_addr
804 && TREE_CODE (arg) == ADDR_EXPR)
805 ret |= visit_addr (stmt, TREE_OPERAND (arg, 0), arg, data);
806 else if (visit_load)
808 tree rhs = get_base_loadstore (arg);
809 if (rhs)
810 ret |= visit_load (stmt, rhs, arg, data);
813 if (visit_addr
814 && gimple_call_chain (call_stmt)
815 && TREE_CODE (gimple_call_chain (call_stmt)) == ADDR_EXPR)
816 ret |= visit_addr (stmt, TREE_OPERAND (gimple_call_chain (call_stmt), 0),
817 gimple_call_chain (call_stmt), data);
818 if (visit_addr
819 && gimple_call_return_slot_opt_p (call_stmt)
820 && gimple_call_lhs (call_stmt) != NULL_TREE
821 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (call_stmt))))
822 ret |= visit_addr (stmt, gimple_call_lhs (call_stmt),
823 gimple_call_lhs (call_stmt), data);
825 else if (gasm *asm_stmt = dyn_cast <gasm *> (stmt))
827 unsigned noutputs;
828 const char *constraint;
829 const char **oconstraints;
830 bool allows_mem, allows_reg, is_inout;
831 noutputs = gimple_asm_noutputs (asm_stmt);
832 oconstraints = XALLOCAVEC (const char *, noutputs);
833 if (visit_store || visit_addr)
834 for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i)
836 tree link = gimple_asm_output_op (asm_stmt, i);
837 tree op = get_base_loadstore (TREE_VALUE (link));
838 if (op && visit_store)
839 ret |= visit_store (stmt, op, TREE_VALUE (link), data);
840 if (visit_addr)
842 constraint = TREE_STRING_POINTER
843 (TREE_VALUE (TREE_PURPOSE (link)));
844 oconstraints[i] = constraint;
845 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
846 &allows_reg, &is_inout);
847 if (op && !allows_reg && allows_mem)
848 ret |= visit_addr (stmt, op, TREE_VALUE (link), data);
851 if (visit_load || visit_addr)
852 for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
854 tree link = gimple_asm_input_op (asm_stmt, i);
855 tree op = TREE_VALUE (link);
856 if (visit_addr
857 && TREE_CODE (op) == ADDR_EXPR)
858 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
859 else if (visit_load || visit_addr)
861 op = get_base_loadstore (op);
862 if (op)
864 if (visit_load)
865 ret |= visit_load (stmt, op, TREE_VALUE (link), data);
866 if (visit_addr)
868 constraint = TREE_STRING_POINTER
869 (TREE_VALUE (TREE_PURPOSE (link)));
870 parse_input_constraint (&constraint, 0, 0, noutputs,
871 0, oconstraints,
872 &allows_mem, &allows_reg);
873 if (!allows_reg && allows_mem)
874 ret |= visit_addr (stmt, op, TREE_VALUE (link),
875 data);
881 else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
883 tree op = gimple_return_retval (return_stmt);
884 if (op)
886 if (visit_addr
887 && TREE_CODE (op) == ADDR_EXPR)
888 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
889 else if (visit_load)
891 tree base = get_base_loadstore (op);
892 if (base)
893 ret |= visit_load (stmt, base, op, data);
897 else if (visit_addr
898 && gimple_code (stmt) == GIMPLE_PHI)
900 for (i = 0; i < gimple_phi_num_args (stmt); ++i)
902 tree op = gimple_phi_arg_def (stmt, i);
903 if (TREE_CODE (op) == ADDR_EXPR)
904 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
907 else if (visit_addr
908 && gimple_code (stmt) == GIMPLE_GOTO)
910 tree op = gimple_goto_dest (as_a <ggoto *> (stmt));
911 if (TREE_CODE (op) == ADDR_EXPR)
912 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
915 return ret;
918 /* Like walk_stmt_load_store_addr_ops but with NULL visit_addr. IPA-CP
919 should make a faster clone for this case. */
921 bool
922 walk_stmt_load_store_ops (gimple stmt, void *data,
923 walk_stmt_load_store_addr_fn visit_load,
924 walk_stmt_load_store_addr_fn visit_store)
926 return walk_stmt_load_store_addr_ops (stmt, data,
927 visit_load, visit_store, NULL);