2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / gimple-walk.c
blobb416ac82dd7d039cf1eb64d035c66fd8af2670a4
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 "input.h"
27 #include "alias.h"
28 #include "symtab.h"
29 #include "tree.h"
30 #include "predict.h"
31 #include "hard-reg-set.h"
32 #include "function.h"
33 #include "gimple-expr.h"
34 #include "is-a.h"
35 #include "tree-ssa-alias.h"
36 #include "basic-block.h"
37 #include "fold-const.h"
38 #include "gimple.h"
39 #include "gimple-iterator.h"
40 #include "gimple-walk.h"
41 #include "stmt.h"
43 /* Walk all the statements in the sequence *PSEQ calling walk_gimple_stmt
44 on each one. WI is as in walk_gimple_stmt.
46 If walk_gimple_stmt returns non-NULL, the walk is stopped, and the
47 value is stored in WI->CALLBACK_RESULT. Also, the statement that
48 produced the value is returned if this statement has not been
49 removed by a callback (wi->removed_stmt). If the statement has
50 been removed, NULL is returned.
52 Otherwise, all the statements are walked and NULL returned. */
54 gimple
55 walk_gimple_seq_mod (gimple_seq *pseq, walk_stmt_fn callback_stmt,
56 walk_tree_fn callback_op, struct walk_stmt_info *wi)
58 gimple_stmt_iterator gsi;
60 for (gsi = gsi_start (*pseq); !gsi_end_p (gsi); )
62 tree ret = walk_gimple_stmt (&gsi, callback_stmt, callback_op, wi);
63 if (ret)
65 /* If CALLBACK_STMT or CALLBACK_OP return a value, WI must exist
66 to hold it. */
67 gcc_assert (wi);
68 wi->callback_result = ret;
70 return wi->removed_stmt ? NULL : gsi_stmt (gsi);
73 if (!wi->removed_stmt)
74 gsi_next (&gsi);
77 if (wi)
78 wi->callback_result = NULL_TREE;
80 return NULL;
84 /* Like walk_gimple_seq_mod, but ensure that the head of SEQ isn't
85 changed by the callbacks. */
87 gimple
88 walk_gimple_seq (gimple_seq seq, walk_stmt_fn callback_stmt,
89 walk_tree_fn callback_op, struct walk_stmt_info *wi)
91 gimple_seq seq2 = seq;
92 gimple ret = walk_gimple_seq_mod (&seq2, callback_stmt, callback_op, wi);
93 gcc_assert (seq2 == seq);
94 return ret;
98 /* Helper function for walk_gimple_stmt. Walk operands of a GIMPLE_ASM. */
100 static tree
101 walk_gimple_asm (gasm *stmt, walk_tree_fn callback_op,
102 struct walk_stmt_info *wi)
104 tree ret, op;
105 unsigned noutputs;
106 const char **oconstraints;
107 unsigned i, n;
108 const char *constraint;
109 bool allows_mem, allows_reg, is_inout;
111 noutputs = gimple_asm_noutputs (stmt);
112 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
114 if (wi)
115 wi->is_lhs = true;
117 for (i = 0; i < noutputs; i++)
119 op = gimple_asm_output_op (stmt, i);
120 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
121 oconstraints[i] = constraint;
122 if (wi)
124 if (parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
125 &allows_reg, &is_inout))
126 wi->val_only = (allows_reg || !allows_mem);
128 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
129 if (ret)
130 return ret;
133 n = gimple_asm_ninputs (stmt);
134 for (i = 0; i < n; i++)
136 op = gimple_asm_input_op (stmt, i);
137 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
139 if (wi)
141 if (parse_input_constraint (&constraint, 0, 0, noutputs, 0,
142 oconstraints, &allows_mem, &allows_reg))
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;
149 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
150 if (ret)
151 return ret;
154 if (wi)
156 wi->is_lhs = false;
157 wi->val_only = true;
160 n = gimple_asm_nlabels (stmt);
161 for (i = 0; i < n; i++)
163 op = gimple_asm_label_op (stmt, i);
164 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
165 if (ret)
166 return ret;
169 return NULL_TREE;
173 /* Helper function of WALK_GIMPLE_STMT. Walk every tree operand in
174 STMT. CALLBACK_OP and WI are as in WALK_GIMPLE_STMT.
176 CALLBACK_OP is called on each operand of STMT via walk_tree.
177 Additional parameters to walk_tree must be stored in WI. For each operand
178 OP, walk_tree is called as:
180 walk_tree (&OP, CALLBACK_OP, WI, WI->PSET)
182 If CALLBACK_OP returns non-NULL for an operand, the remaining
183 operands are not scanned.
185 The return value is that returned by the last call to walk_tree, or
186 NULL_TREE if no CALLBACK_OP is specified. */
188 tree
189 walk_gimple_op (gimple stmt, walk_tree_fn callback_op,
190 struct walk_stmt_info *wi)
192 hash_set<tree> *pset = (wi) ? wi->pset : NULL;
193 unsigned i;
194 tree ret = NULL_TREE;
196 switch (gimple_code (stmt))
198 case GIMPLE_ASSIGN:
199 /* Walk the RHS operands. If the LHS is of a non-renamable type or
200 is a register variable, we may use a COMPONENT_REF on the RHS. */
201 if (wi)
203 tree lhs = gimple_assign_lhs (stmt);
204 wi->val_only
205 = (is_gimple_reg_type (TREE_TYPE (lhs)) && !is_gimple_reg (lhs))
206 || gimple_assign_rhs_class (stmt) != GIMPLE_SINGLE_RHS;
209 for (i = 1; i < gimple_num_ops (stmt); i++)
211 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi,
212 pset);
213 if (ret)
214 return ret;
217 /* Walk the LHS. If the RHS is appropriate for a memory, we
218 may use a COMPONENT_REF on the LHS. */
219 if (wi)
221 /* If the RHS is of a non-renamable type or is a register variable,
222 we may use a COMPONENT_REF on the LHS. */
223 tree rhs1 = gimple_assign_rhs1 (stmt);
224 wi->val_only
225 = (is_gimple_reg_type (TREE_TYPE (rhs1)) && !is_gimple_reg (rhs1))
226 || gimple_assign_rhs_class (stmt) != GIMPLE_SINGLE_RHS;
227 wi->is_lhs = true;
230 ret = walk_tree (gimple_op_ptr (stmt, 0), callback_op, wi, pset);
231 if (ret)
232 return ret;
234 if (wi)
236 wi->val_only = true;
237 wi->is_lhs = false;
239 break;
241 case GIMPLE_CALL:
242 if (wi)
244 wi->is_lhs = false;
245 wi->val_only = true;
248 ret = walk_tree (gimple_call_chain_ptr (as_a <gcall *> (stmt)),
249 callback_op, wi, pset);
250 if (ret)
251 return ret;
253 ret = walk_tree (gimple_call_fn_ptr (stmt), callback_op, wi, pset);
254 if (ret)
255 return ret;
257 for (i = 0; i < gimple_call_num_args (stmt); i++)
259 if (wi)
260 wi->val_only
261 = is_gimple_reg_type (TREE_TYPE (gimple_call_arg (stmt, i)));
262 ret = walk_tree (gimple_call_arg_ptr (stmt, i), callback_op, wi,
263 pset);
264 if (ret)
265 return ret;
268 if (gimple_call_lhs (stmt))
270 if (wi)
272 wi->is_lhs = true;
273 wi->val_only
274 = is_gimple_reg_type (TREE_TYPE (gimple_call_lhs (stmt)));
277 ret = walk_tree (gimple_call_lhs_ptr (stmt), callback_op, wi, pset);
278 if (ret)
279 return ret;
282 if (wi)
284 wi->is_lhs = false;
285 wi->val_only = true;
287 break;
289 case GIMPLE_CATCH:
290 ret = walk_tree (gimple_catch_types_ptr (as_a <gcatch *> (stmt)),
291 callback_op, wi, pset);
292 if (ret)
293 return ret;
294 break;
296 case GIMPLE_EH_FILTER:
297 ret = walk_tree (gimple_eh_filter_types_ptr (stmt), callback_op, wi,
298 pset);
299 if (ret)
300 return ret;
301 break;
303 case GIMPLE_ASM:
304 ret = walk_gimple_asm (as_a <gasm *> (stmt), callback_op, wi);
305 if (ret)
306 return ret;
307 break;
309 case GIMPLE_OMP_CONTINUE:
311 gomp_continue *cont_stmt = as_a <gomp_continue *> (stmt);
312 ret = walk_tree (gimple_omp_continue_control_def_ptr (cont_stmt),
313 callback_op, wi, pset);
314 if (ret)
315 return ret;
317 ret = walk_tree (gimple_omp_continue_control_use_ptr (cont_stmt),
318 callback_op, wi, pset);
319 if (ret)
320 return ret;
322 break;
324 case GIMPLE_OMP_CRITICAL:
326 gomp_critical *omp_stmt = as_a <gomp_critical *> (stmt);
327 ret = walk_tree (gimple_omp_critical_name_ptr (omp_stmt),
328 callback_op, wi, pset);
329 if (ret)
330 return ret;
332 break;
334 case GIMPLE_OMP_FOR:
335 ret = walk_tree (gimple_omp_for_clauses_ptr (stmt), callback_op, wi,
336 pset);
337 if (ret)
338 return ret;
339 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
341 ret = walk_tree (gimple_omp_for_index_ptr (stmt, i), callback_op,
342 wi, pset);
343 if (ret)
344 return ret;
345 ret = walk_tree (gimple_omp_for_initial_ptr (stmt, i), callback_op,
346 wi, pset);
347 if (ret)
348 return ret;
349 ret = walk_tree (gimple_omp_for_final_ptr (stmt, i), callback_op,
350 wi, pset);
351 if (ret)
352 return ret;
353 ret = walk_tree (gimple_omp_for_incr_ptr (stmt, i), callback_op,
354 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:
379 ret = walk_tree (gimple_omp_task_clauses_ptr (stmt), callback_op,
380 wi, pset);
381 if (ret)
382 return ret;
383 ret = walk_tree (gimple_omp_task_child_fn_ptr (stmt), callback_op,
384 wi, pset);
385 if (ret)
386 return ret;
387 ret = walk_tree (gimple_omp_task_data_arg_ptr (stmt), callback_op,
388 wi, pset);
389 if (ret)
390 return ret;
391 ret = walk_tree (gimple_omp_task_copy_fn_ptr (stmt), callback_op,
392 wi, pset);
393 if (ret)
394 return ret;
395 ret = walk_tree (gimple_omp_task_arg_size_ptr (stmt), callback_op,
396 wi, pset);
397 if (ret)
398 return ret;
399 ret = walk_tree (gimple_omp_task_arg_align_ptr (stmt), callback_op,
400 wi, pset);
401 if (ret)
402 return ret;
403 break;
405 case GIMPLE_OMP_SECTIONS:
406 ret = walk_tree (gimple_omp_sections_clauses_ptr (stmt), callback_op,
407 wi, pset);
408 if (ret)
409 return ret;
410 ret = walk_tree (gimple_omp_sections_control_ptr (stmt), callback_op,
411 wi, pset);
412 if (ret)
413 return ret;
415 break;
417 case GIMPLE_OMP_SINGLE:
418 ret = walk_tree (gimple_omp_single_clauses_ptr (stmt), callback_op, wi,
419 pset);
420 if (ret)
421 return ret;
422 break;
424 case GIMPLE_OMP_TARGET:
426 gomp_target *omp_stmt = as_a <gomp_target *> (stmt);
427 ret = walk_tree (gimple_omp_target_clauses_ptr (omp_stmt),
428 callback_op, wi, pset);
429 if (ret)
430 return ret;
431 ret = walk_tree (gimple_omp_target_child_fn_ptr (omp_stmt),
432 callback_op, wi, pset);
433 if (ret)
434 return ret;
435 ret = walk_tree (gimple_omp_target_data_arg_ptr (omp_stmt),
436 callback_op, wi, pset);
437 if (ret)
438 return ret;
440 break;
442 case GIMPLE_OMP_TEAMS:
443 ret = walk_tree (gimple_omp_teams_clauses_ptr (stmt), callback_op, wi,
444 pset);
445 if (ret)
446 return ret;
447 break;
449 case GIMPLE_OMP_ATOMIC_LOAD:
451 gomp_atomic_load *omp_stmt = as_a <gomp_atomic_load *> (stmt);
452 ret = walk_tree (gimple_omp_atomic_load_lhs_ptr (omp_stmt),
453 callback_op, wi, pset);
454 if (ret)
455 return ret;
456 ret = walk_tree (gimple_omp_atomic_load_rhs_ptr (omp_stmt),
457 callback_op, wi, pset);
458 if (ret)
459 return ret;
461 break;
463 case GIMPLE_OMP_ATOMIC_STORE:
465 gomp_atomic_store *omp_stmt = as_a <gomp_atomic_store *> (stmt);
466 ret = walk_tree (gimple_omp_atomic_store_val_ptr (omp_stmt),
467 callback_op, wi, pset);
468 if (ret)
469 return ret;
471 break;
473 case GIMPLE_TRANSACTION:
474 ret = walk_tree (gimple_transaction_label_ptr (
475 as_a <gtransaction *> (stmt)),
476 callback_op, wi, pset);
477 if (ret)
478 return ret;
479 break;
481 case GIMPLE_OMP_RETURN:
482 ret = walk_tree (gimple_omp_return_lhs_ptr (stmt), callback_op, wi,
483 pset);
484 if (ret)
485 return ret;
486 break;
488 /* Tuples that do not have operands. */
489 case GIMPLE_NOP:
490 case GIMPLE_RESX:
491 case GIMPLE_PREDICT:
492 break;
494 default:
496 enum gimple_statement_structure_enum gss;
497 gss = gimple_statement_structure (stmt);
498 if (gss == GSS_WITH_OPS || gss == GSS_WITH_MEM_OPS)
499 for (i = 0; i < gimple_num_ops (stmt); i++)
501 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi, pset);
502 if (ret)
503 return ret;
506 break;
509 return NULL_TREE;
513 /* Walk the current statement in GSI (optionally using traversal state
514 stored in WI). If WI is NULL, no state is kept during traversal.
515 The callback CALLBACK_STMT is called. If CALLBACK_STMT indicates
516 that it has handled all the operands of the statement, its return
517 value is returned. Otherwise, the return value from CALLBACK_STMT
518 is discarded and its operands are scanned.
520 If CALLBACK_STMT is NULL or it didn't handle the operands,
521 CALLBACK_OP is called on each operand of the statement via
522 walk_gimple_op. If walk_gimple_op returns non-NULL for any
523 operand, the remaining operands are not scanned. In this case, the
524 return value from CALLBACK_OP is returned.
526 In any other case, NULL_TREE is returned. */
528 tree
529 walk_gimple_stmt (gimple_stmt_iterator *gsi, walk_stmt_fn callback_stmt,
530 walk_tree_fn callback_op, struct walk_stmt_info *wi)
532 gimple ret;
533 tree tree_ret;
534 gimple stmt = gsi_stmt (*gsi);
536 if (wi)
538 wi->gsi = *gsi;
539 wi->removed_stmt = false;
541 if (wi->want_locations && gimple_has_location (stmt))
542 input_location = gimple_location (stmt);
545 ret = NULL;
547 /* Invoke the statement callback. Return if the callback handled
548 all of STMT operands by itself. */
549 if (callback_stmt)
551 bool handled_ops = false;
552 tree_ret = callback_stmt (gsi, &handled_ops, wi);
553 if (handled_ops)
554 return tree_ret;
556 /* If CALLBACK_STMT did not handle operands, it should not have
557 a value to return. */
558 gcc_assert (tree_ret == NULL);
560 if (wi && wi->removed_stmt)
561 return NULL;
563 /* Re-read stmt in case the callback changed it. */
564 stmt = gsi_stmt (*gsi);
567 /* If CALLBACK_OP is defined, invoke it on every operand of STMT. */
568 if (callback_op)
570 tree_ret = walk_gimple_op (stmt, callback_op, wi);
571 if (tree_ret)
572 return tree_ret;
575 /* If STMT can have statements inside (e.g. GIMPLE_BIND), walk them. */
576 switch (gimple_code (stmt))
578 case GIMPLE_BIND:
579 ret = walk_gimple_seq_mod (gimple_bind_body_ptr (as_a <gbind *> (stmt)),
580 callback_stmt, callback_op, wi);
581 if (ret)
582 return wi->callback_result;
583 break;
585 case GIMPLE_CATCH:
586 ret = walk_gimple_seq_mod (gimple_catch_handler_ptr (
587 as_a <gcatch *> (stmt)),
588 callback_stmt, callback_op, wi);
589 if (ret)
590 return wi->callback_result;
591 break;
593 case GIMPLE_EH_FILTER:
594 ret = walk_gimple_seq_mod (gimple_eh_filter_failure_ptr (stmt), callback_stmt,
595 callback_op, wi);
596 if (ret)
597 return wi->callback_result;
598 break;
600 case GIMPLE_EH_ELSE:
602 geh_else *eh_else_stmt = as_a <geh_else *> (stmt);
603 ret = walk_gimple_seq_mod (gimple_eh_else_n_body_ptr (eh_else_stmt),
604 callback_stmt, callback_op, wi);
605 if (ret)
606 return wi->callback_result;
607 ret = walk_gimple_seq_mod (gimple_eh_else_e_body_ptr (eh_else_stmt),
608 callback_stmt, callback_op, wi);
609 if (ret)
610 return wi->callback_result;
612 break;
614 case GIMPLE_TRY:
615 ret = walk_gimple_seq_mod (gimple_try_eval_ptr (stmt), callback_stmt, callback_op,
616 wi);
617 if (ret)
618 return wi->callback_result;
620 ret = walk_gimple_seq_mod (gimple_try_cleanup_ptr (stmt), callback_stmt,
621 callback_op, wi);
622 if (ret)
623 return wi->callback_result;
624 break;
626 case GIMPLE_OMP_FOR:
627 ret = walk_gimple_seq_mod (gimple_omp_for_pre_body_ptr (stmt), callback_stmt,
628 callback_op, wi);
629 if (ret)
630 return wi->callback_result;
632 /* FALL THROUGH. */
633 case GIMPLE_OMP_CRITICAL:
634 case GIMPLE_OMP_MASTER:
635 case GIMPLE_OMP_TASKGROUP:
636 case GIMPLE_OMP_ORDERED:
637 case GIMPLE_OMP_SECTION:
638 case GIMPLE_OMP_PARALLEL:
639 case GIMPLE_OMP_TASK:
640 case GIMPLE_OMP_SECTIONS:
641 case GIMPLE_OMP_SINGLE:
642 case GIMPLE_OMP_TARGET:
643 case GIMPLE_OMP_TEAMS:
644 ret = walk_gimple_seq_mod (gimple_omp_body_ptr (stmt), callback_stmt,
645 callback_op, wi);
646 if (ret)
647 return wi->callback_result;
648 break;
650 case GIMPLE_WITH_CLEANUP_EXPR:
651 ret = walk_gimple_seq_mod (gimple_wce_cleanup_ptr (stmt), callback_stmt,
652 callback_op, wi);
653 if (ret)
654 return wi->callback_result;
655 break;
657 case GIMPLE_TRANSACTION:
658 ret = walk_gimple_seq_mod (gimple_transaction_body_ptr (
659 as_a <gtransaction *> (stmt)),
660 callback_stmt, callback_op, wi);
661 if (ret)
662 return wi->callback_result;
663 break;
665 default:
666 gcc_assert (!gimple_has_substatements (stmt));
667 break;
670 return NULL;
673 /* From a tree operand OP return the base of a load or store operation
674 or NULL_TREE if OP is not a load or a store. */
676 static tree
677 get_base_loadstore (tree op)
679 while (handled_component_p (op))
680 op = TREE_OPERAND (op, 0);
681 if (DECL_P (op)
682 || INDIRECT_REF_P (op)
683 || TREE_CODE (op) == MEM_REF
684 || TREE_CODE (op) == TARGET_MEM_REF)
685 return op;
686 return NULL_TREE;
690 /* For the statement STMT call the callbacks VISIT_LOAD, VISIT_STORE and
691 VISIT_ADDR if non-NULL on loads, store and address-taken operands
692 passing the STMT, the base of the operand, the operand itself containing
693 the base and DATA to it. The base will be either a decl, an indirect
694 reference (including TARGET_MEM_REF) or the argument of an address
695 expression.
696 Returns the results of these callbacks or'ed. */
698 bool
699 walk_stmt_load_store_addr_ops (gimple stmt, void *data,
700 walk_stmt_load_store_addr_fn visit_load,
701 walk_stmt_load_store_addr_fn visit_store,
702 walk_stmt_load_store_addr_fn visit_addr)
704 bool ret = false;
705 unsigned i;
706 if (gimple_assign_single_p (stmt))
708 tree lhs, rhs, arg;
709 if (visit_store)
711 arg = gimple_assign_lhs (stmt);
712 lhs = get_base_loadstore (arg);
713 if (lhs)
714 ret |= visit_store (stmt, lhs, arg, data);
716 arg = gimple_assign_rhs1 (stmt);
717 rhs = arg;
718 while (handled_component_p (rhs))
719 rhs = TREE_OPERAND (rhs, 0);
720 if (visit_addr)
722 if (TREE_CODE (rhs) == ADDR_EXPR)
723 ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), arg, data);
724 else if (TREE_CODE (rhs) == TARGET_MEM_REF
725 && TREE_CODE (TMR_BASE (rhs)) == ADDR_EXPR)
726 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (rhs), 0), arg,
727 data);
728 else if (TREE_CODE (rhs) == OBJ_TYPE_REF
729 && TREE_CODE (OBJ_TYPE_REF_OBJECT (rhs)) == ADDR_EXPR)
730 ret |= visit_addr (stmt, TREE_OPERAND (OBJ_TYPE_REF_OBJECT (rhs),
731 0), arg, data);
732 else if (TREE_CODE (rhs) == CONSTRUCTOR)
734 unsigned int ix;
735 tree val;
737 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), ix, val)
738 if (TREE_CODE (val) == ADDR_EXPR)
739 ret |= visit_addr (stmt, TREE_OPERAND (val, 0), arg, data);
740 else if (TREE_CODE (val) == OBJ_TYPE_REF
741 && TREE_CODE (OBJ_TYPE_REF_OBJECT (val)) == ADDR_EXPR)
742 ret |= visit_addr (stmt,
743 TREE_OPERAND (OBJ_TYPE_REF_OBJECT (val),
744 0), arg, data);
746 lhs = gimple_assign_lhs (stmt);
747 if (TREE_CODE (lhs) == TARGET_MEM_REF
748 && TREE_CODE (TMR_BASE (lhs)) == ADDR_EXPR)
749 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (lhs), 0), lhs, data);
751 if (visit_load)
753 rhs = get_base_loadstore (rhs);
754 if (rhs)
755 ret |= visit_load (stmt, rhs, arg, data);
758 else if (visit_addr
759 && (is_gimple_assign (stmt)
760 || gimple_code (stmt) == GIMPLE_COND))
762 for (i = 0; i < gimple_num_ops (stmt); ++i)
764 tree op = gimple_op (stmt, i);
765 if (op == NULL_TREE)
767 else if (TREE_CODE (op) == ADDR_EXPR)
768 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
769 /* COND_EXPR and VCOND_EXPR rhs1 argument is a comparison
770 tree with two operands. */
771 else if (i == 1 && COMPARISON_CLASS_P (op))
773 if (TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
774 ret |= visit_addr (stmt, TREE_OPERAND (TREE_OPERAND (op, 0),
775 0), op, data);
776 if (TREE_CODE (TREE_OPERAND (op, 1)) == ADDR_EXPR)
777 ret |= visit_addr (stmt, TREE_OPERAND (TREE_OPERAND (op, 1),
778 0), op, data);
782 else if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
784 if (visit_store)
786 tree arg = gimple_call_lhs (call_stmt);
787 if (arg)
789 tree lhs = get_base_loadstore (arg);
790 if (lhs)
791 ret |= visit_store (stmt, lhs, arg, data);
794 if (visit_load || visit_addr)
795 for (i = 0; i < gimple_call_num_args (call_stmt); ++i)
797 tree arg = gimple_call_arg (call_stmt, i);
798 if (visit_addr
799 && TREE_CODE (arg) == ADDR_EXPR)
800 ret |= visit_addr (stmt, TREE_OPERAND (arg, 0), arg, data);
801 else if (visit_load)
803 tree rhs = get_base_loadstore (arg);
804 if (rhs)
805 ret |= visit_load (stmt, rhs, arg, data);
808 if (visit_addr
809 && gimple_call_chain (call_stmt)
810 && TREE_CODE (gimple_call_chain (call_stmt)) == ADDR_EXPR)
811 ret |= visit_addr (stmt, TREE_OPERAND (gimple_call_chain (call_stmt), 0),
812 gimple_call_chain (call_stmt), data);
813 if (visit_addr
814 && gimple_call_return_slot_opt_p (call_stmt)
815 && gimple_call_lhs (call_stmt) != NULL_TREE
816 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (call_stmt))))
817 ret |= visit_addr (stmt, gimple_call_lhs (call_stmt),
818 gimple_call_lhs (call_stmt), data);
820 else if (gasm *asm_stmt = dyn_cast <gasm *> (stmt))
822 unsigned noutputs;
823 const char *constraint;
824 const char **oconstraints;
825 bool allows_mem, allows_reg, is_inout;
826 noutputs = gimple_asm_noutputs (asm_stmt);
827 oconstraints = XALLOCAVEC (const char *, noutputs);
828 if (visit_store || visit_addr)
829 for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i)
831 tree link = gimple_asm_output_op (asm_stmt, i);
832 tree op = get_base_loadstore (TREE_VALUE (link));
833 if (op && visit_store)
834 ret |= visit_store (stmt, op, TREE_VALUE (link), data);
835 if (visit_addr)
837 constraint = TREE_STRING_POINTER
838 (TREE_VALUE (TREE_PURPOSE (link)));
839 oconstraints[i] = constraint;
840 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
841 &allows_reg, &is_inout);
842 if (op && !allows_reg && allows_mem)
843 ret |= visit_addr (stmt, op, TREE_VALUE (link), data);
846 if (visit_load || visit_addr)
847 for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
849 tree link = gimple_asm_input_op (asm_stmt, i);
850 tree op = TREE_VALUE (link);
851 if (visit_addr
852 && TREE_CODE (op) == ADDR_EXPR)
853 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
854 else if (visit_load || visit_addr)
856 op = get_base_loadstore (op);
857 if (op)
859 if (visit_load)
860 ret |= visit_load (stmt, op, TREE_VALUE (link), data);
861 if (visit_addr)
863 constraint = TREE_STRING_POINTER
864 (TREE_VALUE (TREE_PURPOSE (link)));
865 parse_input_constraint (&constraint, 0, 0, noutputs,
866 0, oconstraints,
867 &allows_mem, &allows_reg);
868 if (!allows_reg && allows_mem)
869 ret |= visit_addr (stmt, op, TREE_VALUE (link),
870 data);
876 else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
878 tree op = gimple_return_retval (return_stmt);
879 if (op)
881 if (visit_addr
882 && TREE_CODE (op) == ADDR_EXPR)
883 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
884 else if (visit_load)
886 tree base = get_base_loadstore (op);
887 if (base)
888 ret |= visit_load (stmt, base, op, data);
892 else if (visit_addr
893 && gimple_code (stmt) == GIMPLE_PHI)
895 for (i = 0; i < gimple_phi_num_args (stmt); ++i)
897 tree op = gimple_phi_arg_def (stmt, i);
898 if (TREE_CODE (op) == ADDR_EXPR)
899 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
902 else if (visit_addr
903 && gimple_code (stmt) == GIMPLE_GOTO)
905 tree op = gimple_goto_dest (stmt);
906 if (TREE_CODE (op) == ADDR_EXPR)
907 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
910 return ret;
913 /* Like walk_stmt_load_store_addr_ops but with NULL visit_addr. IPA-CP
914 should make a faster clone for this case. */
916 bool
917 walk_stmt_load_store_ops (gimple stmt, void *data,
918 walk_stmt_load_store_addr_fn visit_load,
919 walk_stmt_load_store_addr_fn visit_store)
921 return walk_stmt_load_store_addr_ops (stmt, data,
922 visit_load, visit_store, NULL);