2015-07-02 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / gimple-walk.c
blob05ea179e09ffa7efd92361f725d5a528422a66cf
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 "alias.h"
27 #include "symtab.h"
28 #include "tree.h"
29 #include "predict.h"
30 #include "hard-reg-set.h"
31 #include "function.h"
32 #include "gimple-expr.h"
33 #include "tree-ssa-alias.h"
34 #include "basic-block.h"
35 #include "fold-const.h"
36 #include "gimple.h"
37 #include "gimple-iterator.h"
38 #include "gimple-walk.h"
39 #include "stmt.h"
41 /* Walk all the statements in the sequence *PSEQ calling walk_gimple_stmt
42 on each one. WI is as in walk_gimple_stmt.
44 If walk_gimple_stmt returns non-NULL, the walk is stopped, and the
45 value is stored in WI->CALLBACK_RESULT. Also, the statement that
46 produced the value is returned if this statement has not been
47 removed by a callback (wi->removed_stmt). If the statement has
48 been removed, NULL is returned.
50 Otherwise, all the statements are walked and NULL returned. */
52 gimple
53 walk_gimple_seq_mod (gimple_seq *pseq, walk_stmt_fn callback_stmt,
54 walk_tree_fn callback_op, struct walk_stmt_info *wi)
56 gimple_stmt_iterator gsi;
58 for (gsi = gsi_start (*pseq); !gsi_end_p (gsi); )
60 tree ret = walk_gimple_stmt (&gsi, callback_stmt, callback_op, wi);
61 if (ret)
63 /* If CALLBACK_STMT or CALLBACK_OP return a value, WI must exist
64 to hold it. */
65 gcc_assert (wi);
66 wi->callback_result = ret;
68 return wi->removed_stmt ? NULL : gsi_stmt (gsi);
71 if (!wi->removed_stmt)
72 gsi_next (&gsi);
75 if (wi)
76 wi->callback_result = NULL_TREE;
78 return NULL;
82 /* Like walk_gimple_seq_mod, but ensure that the head of SEQ isn't
83 changed by the callbacks. */
85 gimple
86 walk_gimple_seq (gimple_seq seq, walk_stmt_fn callback_stmt,
87 walk_tree_fn callback_op, struct walk_stmt_info *wi)
89 gimple_seq seq2 = seq;
90 gimple ret = walk_gimple_seq_mod (&seq2, callback_stmt, callback_op, wi);
91 gcc_assert (seq2 == seq);
92 return ret;
96 /* Helper function for walk_gimple_stmt. Walk operands of a GIMPLE_ASM. */
98 static tree
99 walk_gimple_asm (gasm *stmt, walk_tree_fn callback_op,
100 struct walk_stmt_info *wi)
102 tree ret, op;
103 unsigned noutputs;
104 const char **oconstraints;
105 unsigned i, n;
106 const char *constraint;
107 bool allows_mem, allows_reg, is_inout;
109 noutputs = gimple_asm_noutputs (stmt);
110 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
112 if (wi)
113 wi->is_lhs = true;
115 for (i = 0; i < noutputs; i++)
117 op = gimple_asm_output_op (stmt, i);
118 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
119 oconstraints[i] = constraint;
120 if (wi)
122 if (parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
123 &allows_reg, &is_inout))
124 wi->val_only = (allows_reg || !allows_mem);
126 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
127 if (ret)
128 return ret;
131 n = gimple_asm_ninputs (stmt);
132 for (i = 0; i < n; i++)
134 op = gimple_asm_input_op (stmt, i);
135 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
137 if (wi)
139 if (parse_input_constraint (&constraint, 0, 0, noutputs, 0,
140 oconstraints, &allows_mem, &allows_reg))
142 wi->val_only = (allows_reg || !allows_mem);
143 /* Although input "m" is not really a LHS, we need a lvalue. */
144 wi->is_lhs = !wi->val_only;
147 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
148 if (ret)
149 return ret;
152 if (wi)
154 wi->is_lhs = false;
155 wi->val_only = true;
158 n = gimple_asm_nlabels (stmt);
159 for (i = 0; i < n; i++)
161 op = gimple_asm_label_op (stmt, i);
162 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
163 if (ret)
164 return ret;
167 return NULL_TREE;
171 /* Helper function of WALK_GIMPLE_STMT. Walk every tree operand in
172 STMT. CALLBACK_OP and WI are as in WALK_GIMPLE_STMT.
174 CALLBACK_OP is called on each operand of STMT via walk_tree.
175 Additional parameters to walk_tree must be stored in WI. For each operand
176 OP, walk_tree is called as:
178 walk_tree (&OP, CALLBACK_OP, WI, WI->PSET)
180 If CALLBACK_OP returns non-NULL for an operand, the remaining
181 operands are not scanned.
183 The return value is that returned by the last call to walk_tree, or
184 NULL_TREE if no CALLBACK_OP is specified. */
186 tree
187 walk_gimple_op (gimple stmt, walk_tree_fn callback_op,
188 struct walk_stmt_info *wi)
190 hash_set<tree> *pset = (wi) ? wi->pset : NULL;
191 unsigned i;
192 tree ret = NULL_TREE;
194 switch (gimple_code (stmt))
196 case GIMPLE_ASSIGN:
197 /* Walk the RHS operands. If the LHS is of a non-renamable type or
198 is a register variable, we may use a COMPONENT_REF on the RHS. */
199 if (wi)
201 tree lhs = gimple_assign_lhs (stmt);
202 wi->val_only
203 = (is_gimple_reg_type (TREE_TYPE (lhs)) && !is_gimple_reg (lhs))
204 || gimple_assign_rhs_class (stmt) != GIMPLE_SINGLE_RHS;
207 for (i = 1; i < gimple_num_ops (stmt); i++)
209 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi,
210 pset);
211 if (ret)
212 return ret;
215 /* Walk the LHS. If the RHS is appropriate for a memory, we
216 may use a COMPONENT_REF on the LHS. */
217 if (wi)
219 /* If the RHS is of a non-renamable type or is a register variable,
220 we may use a COMPONENT_REF on the LHS. */
221 tree rhs1 = gimple_assign_rhs1 (stmt);
222 wi->val_only
223 = (is_gimple_reg_type (TREE_TYPE (rhs1)) && !is_gimple_reg (rhs1))
224 || gimple_assign_rhs_class (stmt) != GIMPLE_SINGLE_RHS;
225 wi->is_lhs = true;
228 ret = walk_tree (gimple_op_ptr (stmt, 0), callback_op, wi, pset);
229 if (ret)
230 return ret;
232 if (wi)
234 wi->val_only = true;
235 wi->is_lhs = false;
237 break;
239 case GIMPLE_CALL:
240 if (wi)
242 wi->is_lhs = false;
243 wi->val_only = true;
246 ret = walk_tree (gimple_call_chain_ptr (as_a <gcall *> (stmt)),
247 callback_op, wi, pset);
248 if (ret)
249 return ret;
251 ret = walk_tree (gimple_call_fn_ptr (stmt), callback_op, wi, pset);
252 if (ret)
253 return ret;
255 for (i = 0; i < gimple_call_num_args (stmt); i++)
257 if (wi)
258 wi->val_only
259 = is_gimple_reg_type (TREE_TYPE (gimple_call_arg (stmt, i)));
260 ret = walk_tree (gimple_call_arg_ptr (stmt, i), callback_op, wi,
261 pset);
262 if (ret)
263 return ret;
266 if (gimple_call_lhs (stmt))
268 if (wi)
270 wi->is_lhs = true;
271 wi->val_only
272 = is_gimple_reg_type (TREE_TYPE (gimple_call_lhs (stmt)));
275 ret = walk_tree (gimple_call_lhs_ptr (stmt), callback_op, wi, pset);
276 if (ret)
277 return ret;
280 if (wi)
282 wi->is_lhs = false;
283 wi->val_only = true;
285 break;
287 case GIMPLE_CATCH:
288 ret = walk_tree (gimple_catch_types_ptr (as_a <gcatch *> (stmt)),
289 callback_op, wi, pset);
290 if (ret)
291 return ret;
292 break;
294 case GIMPLE_EH_FILTER:
295 ret = walk_tree (gimple_eh_filter_types_ptr (stmt), callback_op, wi,
296 pset);
297 if (ret)
298 return ret;
299 break;
301 case GIMPLE_ASM:
302 ret = walk_gimple_asm (as_a <gasm *> (stmt), callback_op, wi);
303 if (ret)
304 return ret;
305 break;
307 case GIMPLE_OMP_CONTINUE:
309 gomp_continue *cont_stmt = as_a <gomp_continue *> (stmt);
310 ret = walk_tree (gimple_omp_continue_control_def_ptr (cont_stmt),
311 callback_op, wi, pset);
312 if (ret)
313 return ret;
315 ret = walk_tree (gimple_omp_continue_control_use_ptr (cont_stmt),
316 callback_op, wi, pset);
317 if (ret)
318 return ret;
320 break;
322 case GIMPLE_OMP_CRITICAL:
324 gomp_critical *omp_stmt = as_a <gomp_critical *> (stmt);
325 ret = walk_tree (gimple_omp_critical_name_ptr (omp_stmt),
326 callback_op, wi, pset);
327 if (ret)
328 return ret;
330 break;
332 case GIMPLE_OMP_FOR:
333 ret = walk_tree (gimple_omp_for_clauses_ptr (stmt), callback_op, wi,
334 pset);
335 if (ret)
336 return ret;
337 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
339 ret = walk_tree (gimple_omp_for_index_ptr (stmt, i), callback_op,
340 wi, pset);
341 if (ret)
342 return ret;
343 ret = walk_tree (gimple_omp_for_initial_ptr (stmt, i), callback_op,
344 wi, pset);
345 if (ret)
346 return ret;
347 ret = walk_tree (gimple_omp_for_final_ptr (stmt, i), callback_op,
348 wi, pset);
349 if (ret)
350 return ret;
351 ret = walk_tree (gimple_omp_for_incr_ptr (stmt, i), callback_op,
352 wi, pset);
353 if (ret)
354 return ret;
356 break;
358 case GIMPLE_OMP_PARALLEL:
360 gomp_parallel *omp_par_stmt = as_a <gomp_parallel *> (stmt);
361 ret = walk_tree (gimple_omp_parallel_clauses_ptr (omp_par_stmt),
362 callback_op, wi, pset);
363 if (ret)
364 return ret;
365 ret = walk_tree (gimple_omp_parallel_child_fn_ptr (omp_par_stmt),
366 callback_op, wi, pset);
367 if (ret)
368 return ret;
369 ret = walk_tree (gimple_omp_parallel_data_arg_ptr (omp_par_stmt),
370 callback_op, wi, pset);
371 if (ret)
372 return ret;
374 break;
376 case GIMPLE_OMP_TASK:
377 ret = walk_tree (gimple_omp_task_clauses_ptr (stmt), callback_op,
378 wi, pset);
379 if (ret)
380 return ret;
381 ret = walk_tree (gimple_omp_task_child_fn_ptr (stmt), callback_op,
382 wi, pset);
383 if (ret)
384 return ret;
385 ret = walk_tree (gimple_omp_task_data_arg_ptr (stmt), callback_op,
386 wi, pset);
387 if (ret)
388 return ret;
389 ret = walk_tree (gimple_omp_task_copy_fn_ptr (stmt), callback_op,
390 wi, pset);
391 if (ret)
392 return ret;
393 ret = walk_tree (gimple_omp_task_arg_size_ptr (stmt), callback_op,
394 wi, pset);
395 if (ret)
396 return ret;
397 ret = walk_tree (gimple_omp_task_arg_align_ptr (stmt), callback_op,
398 wi, pset);
399 if (ret)
400 return ret;
401 break;
403 case GIMPLE_OMP_SECTIONS:
404 ret = walk_tree (gimple_omp_sections_clauses_ptr (stmt), callback_op,
405 wi, pset);
406 if (ret)
407 return ret;
408 ret = walk_tree (gimple_omp_sections_control_ptr (stmt), callback_op,
409 wi, pset);
410 if (ret)
411 return ret;
413 break;
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;
422 case GIMPLE_OMP_TARGET:
424 gomp_target *omp_stmt = as_a <gomp_target *> (stmt);
425 ret = walk_tree (gimple_omp_target_clauses_ptr (omp_stmt),
426 callback_op, wi, pset);
427 if (ret)
428 return ret;
429 ret = walk_tree (gimple_omp_target_child_fn_ptr (omp_stmt),
430 callback_op, wi, pset);
431 if (ret)
432 return ret;
433 ret = walk_tree (gimple_omp_target_data_arg_ptr (omp_stmt),
434 callback_op, wi, pset);
435 if (ret)
436 return ret;
438 break;
440 case GIMPLE_OMP_TEAMS:
441 ret = walk_tree (gimple_omp_teams_clauses_ptr (stmt), callback_op, wi,
442 pset);
443 if (ret)
444 return ret;
445 break;
447 case GIMPLE_OMP_ATOMIC_LOAD:
449 gomp_atomic_load *omp_stmt = as_a <gomp_atomic_load *> (stmt);
450 ret = walk_tree (gimple_omp_atomic_load_lhs_ptr (omp_stmt),
451 callback_op, wi, pset);
452 if (ret)
453 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:
463 gomp_atomic_store *omp_stmt = as_a <gomp_atomic_store *> (stmt);
464 ret = walk_tree (gimple_omp_atomic_store_val_ptr (omp_stmt),
465 callback_op, wi, pset);
466 if (ret)
467 return ret;
469 break;
471 case GIMPLE_TRANSACTION:
472 ret = walk_tree (gimple_transaction_label_ptr (
473 as_a <gtransaction *> (stmt)),
474 callback_op, wi, pset);
475 if (ret)
476 return ret;
477 break;
479 case GIMPLE_OMP_RETURN:
480 ret = walk_tree (gimple_omp_return_lhs_ptr (stmt), callback_op, wi,
481 pset);
482 if (ret)
483 return ret;
484 break;
486 /* Tuples that do not have operands. */
487 case GIMPLE_NOP:
488 case GIMPLE_RESX:
489 case GIMPLE_PREDICT:
490 break;
492 default:
494 enum gimple_statement_structure_enum gss;
495 gss = gimple_statement_structure (stmt);
496 if (gss == GSS_WITH_OPS || gss == GSS_WITH_MEM_OPS)
497 for (i = 0; i < gimple_num_ops (stmt); i++)
499 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi, pset);
500 if (ret)
501 return ret;
504 break;
507 return NULL_TREE;
511 /* Walk the current statement in GSI (optionally using traversal state
512 stored in WI). If WI is NULL, no state is kept during traversal.
513 The callback CALLBACK_STMT is called. If CALLBACK_STMT indicates
514 that it has handled all the operands of the statement, its return
515 value is returned. Otherwise, the return value from CALLBACK_STMT
516 is discarded and its operands are scanned.
518 If CALLBACK_STMT is NULL or it didn't handle the operands,
519 CALLBACK_OP is called on each operand of the statement via
520 walk_gimple_op. If walk_gimple_op returns non-NULL for any
521 operand, the remaining operands are not scanned. In this case, the
522 return value from CALLBACK_OP is returned.
524 In any other case, NULL_TREE is returned. */
526 tree
527 walk_gimple_stmt (gimple_stmt_iterator *gsi, walk_stmt_fn callback_stmt,
528 walk_tree_fn callback_op, struct walk_stmt_info *wi)
530 gimple ret;
531 tree tree_ret;
532 gimple stmt = gsi_stmt (*gsi);
534 if (wi)
536 wi->gsi = *gsi;
537 wi->removed_stmt = false;
539 if (wi->want_locations && gimple_has_location (stmt))
540 input_location = gimple_location (stmt);
543 ret = NULL;
545 /* Invoke the statement callback. Return if the callback handled
546 all of STMT operands by itself. */
547 if (callback_stmt)
549 bool handled_ops = false;
550 tree_ret = callback_stmt (gsi, &handled_ops, wi);
551 if (handled_ops)
552 return tree_ret;
554 /* If CALLBACK_STMT did not handle operands, it should not have
555 a value to return. */
556 gcc_assert (tree_ret == NULL);
558 if (wi && wi->removed_stmt)
559 return NULL;
561 /* Re-read stmt in case the callback changed it. */
562 stmt = gsi_stmt (*gsi);
565 /* If CALLBACK_OP is defined, invoke it on every operand of STMT. */
566 if (callback_op)
568 tree_ret = walk_gimple_op (stmt, callback_op, wi);
569 if (tree_ret)
570 return tree_ret;
573 /* If STMT can have statements inside (e.g. GIMPLE_BIND), walk them. */
574 switch (gimple_code (stmt))
576 case GIMPLE_BIND:
577 ret = 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:
613 ret = walk_gimple_seq_mod (gimple_try_eval_ptr (stmt), callback_stmt, callback_op,
614 wi);
615 if (ret)
616 return wi->callback_result;
618 ret = walk_gimple_seq_mod (gimple_try_cleanup_ptr (stmt), callback_stmt,
619 callback_op, wi);
620 if (ret)
621 return wi->callback_result;
622 break;
624 case GIMPLE_OMP_FOR:
625 ret = walk_gimple_seq_mod (gimple_omp_for_pre_body_ptr (stmt), callback_stmt,
626 callback_op, wi);
627 if (ret)
628 return wi->callback_result;
630 /* FALL THROUGH. */
631 case GIMPLE_OMP_CRITICAL:
632 case GIMPLE_OMP_MASTER:
633 case GIMPLE_OMP_TASKGROUP:
634 case GIMPLE_OMP_ORDERED:
635 case GIMPLE_OMP_SECTION:
636 case GIMPLE_OMP_PARALLEL:
637 case GIMPLE_OMP_TASK:
638 case GIMPLE_OMP_SECTIONS:
639 case GIMPLE_OMP_SINGLE:
640 case GIMPLE_OMP_TARGET:
641 case GIMPLE_OMP_TEAMS:
642 ret = walk_gimple_seq_mod (gimple_omp_body_ptr (stmt), callback_stmt,
643 callback_op, wi);
644 if (ret)
645 return wi->callback_result;
646 break;
648 case GIMPLE_WITH_CLEANUP_EXPR:
649 ret = walk_gimple_seq_mod (gimple_wce_cleanup_ptr (stmt), callback_stmt,
650 callback_op, wi);
651 if (ret)
652 return wi->callback_result;
653 break;
655 case GIMPLE_TRANSACTION:
656 ret = walk_gimple_seq_mod (gimple_transaction_body_ptr (
657 as_a <gtransaction *> (stmt)),
658 callback_stmt, callback_op, wi);
659 if (ret)
660 return wi->callback_result;
661 break;
663 default:
664 gcc_assert (!gimple_has_substatements (stmt));
665 break;
668 return NULL;
671 /* From a tree operand OP return the base of a load or store operation
672 or NULL_TREE if OP is not a load or a store. */
674 static tree
675 get_base_loadstore (tree op)
677 while (handled_component_p (op))
678 op = TREE_OPERAND (op, 0);
679 if (DECL_P (op)
680 || INDIRECT_REF_P (op)
681 || TREE_CODE (op) == MEM_REF
682 || TREE_CODE (op) == TARGET_MEM_REF)
683 return op;
684 return NULL_TREE;
688 /* For the statement STMT call the callbacks VISIT_LOAD, VISIT_STORE and
689 VISIT_ADDR if non-NULL on loads, store and address-taken operands
690 passing the STMT, the base of the operand, the operand itself containing
691 the base and DATA to it. The base will be either a decl, an indirect
692 reference (including TARGET_MEM_REF) or the argument of an address
693 expression.
694 Returns the results of these callbacks or'ed. */
696 bool
697 walk_stmt_load_store_addr_ops (gimple stmt, void *data,
698 walk_stmt_load_store_addr_fn visit_load,
699 walk_stmt_load_store_addr_fn visit_store,
700 walk_stmt_load_store_addr_fn visit_addr)
702 bool ret = false;
703 unsigned i;
704 if (gimple_assign_single_p (stmt))
706 tree lhs, rhs, arg;
707 if (visit_store)
709 arg = gimple_assign_lhs (stmt);
710 lhs = get_base_loadstore (arg);
711 if (lhs)
712 ret |= visit_store (stmt, lhs, arg, data);
714 arg = gimple_assign_rhs1 (stmt);
715 rhs = arg;
716 while (handled_component_p (rhs))
717 rhs = TREE_OPERAND (rhs, 0);
718 if (visit_addr)
720 if (TREE_CODE (rhs) == ADDR_EXPR)
721 ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), arg, data);
722 else if (TREE_CODE (rhs) == TARGET_MEM_REF
723 && TREE_CODE (TMR_BASE (rhs)) == ADDR_EXPR)
724 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (rhs), 0), arg,
725 data);
726 else if (TREE_CODE (rhs) == OBJ_TYPE_REF
727 && TREE_CODE (OBJ_TYPE_REF_OBJECT (rhs)) == ADDR_EXPR)
728 ret |= visit_addr (stmt, TREE_OPERAND (OBJ_TYPE_REF_OBJECT (rhs),
729 0), arg, data);
730 else if (TREE_CODE (rhs) == CONSTRUCTOR)
732 unsigned int ix;
733 tree val;
735 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), ix, val)
736 if (TREE_CODE (val) == ADDR_EXPR)
737 ret |= visit_addr (stmt, TREE_OPERAND (val, 0), arg, data);
738 else if (TREE_CODE (val) == OBJ_TYPE_REF
739 && TREE_CODE (OBJ_TYPE_REF_OBJECT (val)) == ADDR_EXPR)
740 ret |= visit_addr (stmt,
741 TREE_OPERAND (OBJ_TYPE_REF_OBJECT (val),
742 0), arg, data);
744 lhs = gimple_assign_lhs (stmt);
745 if (TREE_CODE (lhs) == TARGET_MEM_REF
746 && TREE_CODE (TMR_BASE (lhs)) == ADDR_EXPR)
747 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (lhs), 0), lhs, data);
749 if (visit_load)
751 rhs = get_base_loadstore (rhs);
752 if (rhs)
753 ret |= visit_load (stmt, rhs, arg, data);
756 else if (visit_addr
757 && (is_gimple_assign (stmt)
758 || gimple_code (stmt) == GIMPLE_COND))
760 for (i = 0; i < gimple_num_ops (stmt); ++i)
762 tree op = gimple_op (stmt, i);
763 if (op == NULL_TREE)
765 else if (TREE_CODE (op) == ADDR_EXPR)
766 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
767 /* COND_EXPR and VCOND_EXPR rhs1 argument is a comparison
768 tree with two operands. */
769 else if (i == 1 && COMPARISON_CLASS_P (op))
771 if (TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
772 ret |= visit_addr (stmt, TREE_OPERAND (TREE_OPERAND (op, 0),
773 0), op, data);
774 if (TREE_CODE (TREE_OPERAND (op, 1)) == ADDR_EXPR)
775 ret |= visit_addr (stmt, TREE_OPERAND (TREE_OPERAND (op, 1),
776 0), op, data);
780 else if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
782 if (visit_store)
784 tree arg = gimple_call_lhs (call_stmt);
785 if (arg)
787 tree lhs = get_base_loadstore (arg);
788 if (lhs)
789 ret |= visit_store (stmt, lhs, arg, data);
792 if (visit_load || visit_addr)
793 for (i = 0; i < gimple_call_num_args (call_stmt); ++i)
795 tree arg = gimple_call_arg (call_stmt, i);
796 if (visit_addr
797 && TREE_CODE (arg) == ADDR_EXPR)
798 ret |= visit_addr (stmt, TREE_OPERAND (arg, 0), arg, data);
799 else if (visit_load)
801 tree rhs = get_base_loadstore (arg);
802 if (rhs)
803 ret |= visit_load (stmt, rhs, arg, data);
806 if (visit_addr
807 && gimple_call_chain (call_stmt)
808 && TREE_CODE (gimple_call_chain (call_stmt)) == ADDR_EXPR)
809 ret |= visit_addr (stmt, TREE_OPERAND (gimple_call_chain (call_stmt), 0),
810 gimple_call_chain (call_stmt), data);
811 if (visit_addr
812 && gimple_call_return_slot_opt_p (call_stmt)
813 && gimple_call_lhs (call_stmt) != NULL_TREE
814 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (call_stmt))))
815 ret |= visit_addr (stmt, gimple_call_lhs (call_stmt),
816 gimple_call_lhs (call_stmt), data);
818 else if (gasm *asm_stmt = dyn_cast <gasm *> (stmt))
820 unsigned noutputs;
821 const char *constraint;
822 const char **oconstraints;
823 bool allows_mem, allows_reg, is_inout;
824 noutputs = gimple_asm_noutputs (asm_stmt);
825 oconstraints = XALLOCAVEC (const char *, noutputs);
826 if (visit_store || visit_addr)
827 for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i)
829 tree link = gimple_asm_output_op (asm_stmt, i);
830 tree op = get_base_loadstore (TREE_VALUE (link));
831 if (op && visit_store)
832 ret |= visit_store (stmt, op, TREE_VALUE (link), data);
833 if (visit_addr)
835 constraint = TREE_STRING_POINTER
836 (TREE_VALUE (TREE_PURPOSE (link)));
837 oconstraints[i] = constraint;
838 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
839 &allows_reg, &is_inout);
840 if (op && !allows_reg && allows_mem)
841 ret |= visit_addr (stmt, op, TREE_VALUE (link), data);
844 if (visit_load || visit_addr)
845 for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
847 tree link = gimple_asm_input_op (asm_stmt, i);
848 tree op = TREE_VALUE (link);
849 if (visit_addr
850 && TREE_CODE (op) == ADDR_EXPR)
851 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
852 else if (visit_load || visit_addr)
854 op = get_base_loadstore (op);
855 if (op)
857 if (visit_load)
858 ret |= visit_load (stmt, op, TREE_VALUE (link), data);
859 if (visit_addr)
861 constraint = TREE_STRING_POINTER
862 (TREE_VALUE (TREE_PURPOSE (link)));
863 parse_input_constraint (&constraint, 0, 0, noutputs,
864 0, oconstraints,
865 &allows_mem, &allows_reg);
866 if (!allows_reg && allows_mem)
867 ret |= visit_addr (stmt, op, TREE_VALUE (link),
868 data);
874 else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
876 tree op = gimple_return_retval (return_stmt);
877 if (op)
879 if (visit_addr
880 && TREE_CODE (op) == ADDR_EXPR)
881 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
882 else if (visit_load)
884 tree base = get_base_loadstore (op);
885 if (base)
886 ret |= visit_load (stmt, base, op, data);
890 else if (visit_addr
891 && gimple_code (stmt) == GIMPLE_PHI)
893 for (i = 0; i < gimple_phi_num_args (stmt); ++i)
895 tree op = gimple_phi_arg_def (stmt, i);
896 if (TREE_CODE (op) == ADDR_EXPR)
897 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
900 else if (visit_addr
901 && gimple_code (stmt) == GIMPLE_GOTO)
903 tree op = gimple_goto_dest (stmt);
904 if (TREE_CODE (op) == ADDR_EXPR)
905 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
908 return ret;
911 /* Like walk_stmt_load_store_addr_ops but with NULL visit_addr. IPA-CP
912 should make a faster clone for this case. */
914 bool
915 walk_stmt_load_store_ops (gimple stmt, void *data,
916 walk_stmt_load_store_addr_fn visit_load,
917 walk_stmt_load_store_addr_fn visit_store)
919 return walk_stmt_load_store_addr_ops (stmt, data,
920 visit_load, visit_store, NULL);