2015-10-18 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / gimple-walk.c
blobb045c67d3575f33595672fd416e7765f77c4cf31
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 "backend.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "hard-reg-set.h"
29 #include "alias.h"
30 #include "fold-const.h"
31 #include "gimple-iterator.h"
32 #include "gimple-walk.h"
33 #include "stmt.h"
35 /* Walk all the statements in the sequence *PSEQ calling walk_gimple_stmt
36 on each one. WI is as in walk_gimple_stmt.
38 If walk_gimple_stmt returns non-NULL, the walk is stopped, and the
39 value is stored in WI->CALLBACK_RESULT. Also, the statement that
40 produced the value is returned if this statement has not been
41 removed by a callback (wi->removed_stmt). If the statement has
42 been removed, NULL is returned.
44 Otherwise, all the statements are walked and NULL returned. */
46 gimple *
47 walk_gimple_seq_mod (gimple_seq *pseq, walk_stmt_fn callback_stmt,
48 walk_tree_fn callback_op, struct walk_stmt_info *wi)
50 gimple_stmt_iterator gsi;
52 for (gsi = gsi_start (*pseq); !gsi_end_p (gsi); )
54 tree ret = walk_gimple_stmt (&gsi, callback_stmt, callback_op, wi);
55 if (ret)
57 /* If CALLBACK_STMT or CALLBACK_OP return a value, WI must exist
58 to hold it. */
59 gcc_assert (wi);
60 wi->callback_result = ret;
62 return wi->removed_stmt ? NULL : gsi_stmt (gsi);
65 if (!wi->removed_stmt)
66 gsi_next (&gsi);
69 if (wi)
70 wi->callback_result = NULL_TREE;
72 return NULL;
76 /* Like walk_gimple_seq_mod, but ensure that the head of SEQ isn't
77 changed by the callbacks. */
79 gimple *
80 walk_gimple_seq (gimple_seq seq, walk_stmt_fn callback_stmt,
81 walk_tree_fn callback_op, struct walk_stmt_info *wi)
83 gimple_seq seq2 = seq;
84 gimple *ret = walk_gimple_seq_mod (&seq2, callback_stmt, callback_op, wi);
85 gcc_assert (seq2 == seq);
86 return ret;
90 /* Helper function for walk_gimple_stmt. Walk operands of a GIMPLE_ASM. */
92 static tree
93 walk_gimple_asm (gasm *stmt, walk_tree_fn callback_op,
94 struct walk_stmt_info *wi)
96 tree ret, op;
97 unsigned noutputs;
98 const char **oconstraints;
99 unsigned i, n;
100 const char *constraint;
101 bool allows_mem, allows_reg, is_inout;
103 noutputs = gimple_asm_noutputs (stmt);
104 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
106 if (wi)
107 wi->is_lhs = true;
109 for (i = 0; i < noutputs; i++)
111 op = gimple_asm_output_op (stmt, i);
112 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
113 oconstraints[i] = constraint;
114 if (wi)
116 if (parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
117 &allows_reg, &is_inout))
118 wi->val_only = (allows_reg || !allows_mem);
120 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
121 if (ret)
122 return ret;
125 n = gimple_asm_ninputs (stmt);
126 for (i = 0; i < n; i++)
128 op = gimple_asm_input_op (stmt, i);
129 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
131 if (wi)
133 if (parse_input_constraint (&constraint, 0, 0, noutputs, 0,
134 oconstraints, &allows_mem, &allows_reg))
136 wi->val_only = (allows_reg || !allows_mem);
137 /* Although input "m" is not really a LHS, we need a lvalue. */
138 wi->is_lhs = !wi->val_only;
141 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
142 if (ret)
143 return ret;
146 if (wi)
148 wi->is_lhs = false;
149 wi->val_only = true;
152 n = gimple_asm_nlabels (stmt);
153 for (i = 0; i < n; i++)
155 op = gimple_asm_label_op (stmt, i);
156 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
157 if (ret)
158 return ret;
161 return NULL_TREE;
165 /* Helper function of WALK_GIMPLE_STMT. Walk every tree operand in
166 STMT. CALLBACK_OP and WI are as in WALK_GIMPLE_STMT.
168 CALLBACK_OP is called on each operand of STMT via walk_tree.
169 Additional parameters to walk_tree must be stored in WI. For each operand
170 OP, walk_tree is called as:
172 walk_tree (&OP, CALLBACK_OP, WI, WI->PSET)
174 If CALLBACK_OP returns non-NULL for an operand, the remaining
175 operands are not scanned.
177 The return value is that returned by the last call to walk_tree, or
178 NULL_TREE if no CALLBACK_OP is specified. */
180 tree
181 walk_gimple_op (gimple *stmt, walk_tree_fn callback_op,
182 struct walk_stmt_info *wi)
184 hash_set<tree> *pset = (wi) ? wi->pset : NULL;
185 unsigned i;
186 tree ret = NULL_TREE;
188 switch (gimple_code (stmt))
190 case GIMPLE_ASSIGN:
191 /* Walk the RHS operands. If the LHS is of a non-renamable type or
192 is a register variable, we may use a COMPONENT_REF on the RHS. */
193 if (wi)
195 tree lhs = gimple_assign_lhs (stmt);
196 wi->val_only
197 = (is_gimple_reg_type (TREE_TYPE (lhs)) && !is_gimple_reg (lhs))
198 || gimple_assign_rhs_class (stmt) != GIMPLE_SINGLE_RHS;
201 for (i = 1; i < gimple_num_ops (stmt); i++)
203 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi,
204 pset);
205 if (ret)
206 return ret;
209 /* Walk the LHS. If the RHS is appropriate for a memory, we
210 may use a COMPONENT_REF on the LHS. */
211 if (wi)
213 /* If the RHS is of a non-renamable type or is a register variable,
214 we may use a COMPONENT_REF on the LHS. */
215 tree rhs1 = gimple_assign_rhs1 (stmt);
216 wi->val_only
217 = (is_gimple_reg_type (TREE_TYPE (rhs1)) && !is_gimple_reg (rhs1))
218 || gimple_assign_rhs_class (stmt) != GIMPLE_SINGLE_RHS;
219 wi->is_lhs = true;
222 ret = walk_tree (gimple_op_ptr (stmt, 0), callback_op, wi, pset);
223 if (ret)
224 return ret;
226 if (wi)
228 wi->val_only = true;
229 wi->is_lhs = false;
231 break;
233 case GIMPLE_CALL:
234 if (wi)
236 wi->is_lhs = false;
237 wi->val_only = true;
240 ret = walk_tree (gimple_call_chain_ptr (as_a <gcall *> (stmt)),
241 callback_op, wi, pset);
242 if (ret)
243 return ret;
245 ret = walk_tree (gimple_call_fn_ptr (stmt), callback_op, wi, pset);
246 if (ret)
247 return ret;
249 for (i = 0; i < gimple_call_num_args (stmt); i++)
251 if (wi)
252 wi->val_only
253 = is_gimple_reg_type (TREE_TYPE (gimple_call_arg (stmt, i)));
254 ret = walk_tree (gimple_call_arg_ptr (stmt, i), callback_op, wi,
255 pset);
256 if (ret)
257 return ret;
260 if (gimple_call_lhs (stmt))
262 if (wi)
264 wi->is_lhs = true;
265 wi->val_only
266 = is_gimple_reg_type (TREE_TYPE (gimple_call_lhs (stmt)));
269 ret = walk_tree (gimple_call_lhs_ptr (stmt), callback_op, wi, pset);
270 if (ret)
271 return ret;
274 if (wi)
276 wi->is_lhs = false;
277 wi->val_only = true;
279 break;
281 case GIMPLE_CATCH:
282 ret = walk_tree (gimple_catch_types_ptr (as_a <gcatch *> (stmt)),
283 callback_op, wi, pset);
284 if (ret)
285 return ret;
286 break;
288 case GIMPLE_EH_FILTER:
289 ret = walk_tree (gimple_eh_filter_types_ptr (stmt), callback_op, wi,
290 pset);
291 if (ret)
292 return ret;
293 break;
295 case GIMPLE_ASM:
296 ret = walk_gimple_asm (as_a <gasm *> (stmt), callback_op, wi);
297 if (ret)
298 return ret;
299 break;
301 case GIMPLE_OMP_CONTINUE:
303 gomp_continue *cont_stmt = as_a <gomp_continue *> (stmt);
304 ret = walk_tree (gimple_omp_continue_control_def_ptr (cont_stmt),
305 callback_op, wi, pset);
306 if (ret)
307 return ret;
309 ret = walk_tree (gimple_omp_continue_control_use_ptr (cont_stmt),
310 callback_op, wi, pset);
311 if (ret)
312 return ret;
314 break;
316 case GIMPLE_OMP_CRITICAL:
318 gomp_critical *omp_stmt = as_a <gomp_critical *> (stmt);
319 ret = walk_tree (gimple_omp_critical_name_ptr (omp_stmt),
320 callback_op, wi, pset);
321 if (ret)
322 return ret;
323 ret = walk_tree (gimple_omp_critical_clauses_ptr (omp_stmt),
324 callback_op, wi, pset);
325 if (ret)
326 return ret;
328 break;
330 case GIMPLE_OMP_ORDERED:
332 gomp_ordered *omp_stmt = as_a <gomp_ordered *> (stmt);
333 ret = walk_tree (gimple_omp_ordered_clauses_ptr (omp_stmt),
334 callback_op, wi, pset);
335 if (ret)
336 return ret;
338 break;
340 case GIMPLE_OMP_FOR:
341 ret = walk_tree (gimple_omp_for_clauses_ptr (stmt), callback_op, wi,
342 pset);
343 if (ret)
344 return ret;
345 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
347 ret = walk_tree (gimple_omp_for_index_ptr (stmt, i), callback_op,
348 wi, pset);
349 if (ret)
350 return ret;
351 ret = walk_tree (gimple_omp_for_initial_ptr (stmt, i), callback_op,
352 wi, pset);
353 if (ret)
354 return ret;
355 ret = walk_tree (gimple_omp_for_final_ptr (stmt, i), callback_op,
356 wi, pset);
357 if (ret)
358 return ret;
359 ret = walk_tree (gimple_omp_for_incr_ptr (stmt, i), callback_op,
360 wi, pset);
361 if (ret)
362 return ret;
364 break;
366 case GIMPLE_OMP_PARALLEL:
368 gomp_parallel *omp_par_stmt = as_a <gomp_parallel *> (stmt);
369 ret = walk_tree (gimple_omp_parallel_clauses_ptr (omp_par_stmt),
370 callback_op, wi, pset);
371 if (ret)
372 return ret;
373 ret = walk_tree (gimple_omp_parallel_child_fn_ptr (omp_par_stmt),
374 callback_op, wi, pset);
375 if (ret)
376 return ret;
377 ret = walk_tree (gimple_omp_parallel_data_arg_ptr (omp_par_stmt),
378 callback_op, wi, pset);
379 if (ret)
380 return ret;
382 break;
384 case GIMPLE_OMP_TASK:
385 ret = walk_tree (gimple_omp_task_clauses_ptr (stmt), callback_op,
386 wi, pset);
387 if (ret)
388 return ret;
389 ret = walk_tree (gimple_omp_task_child_fn_ptr (stmt), callback_op,
390 wi, pset);
391 if (ret)
392 return ret;
393 ret = walk_tree (gimple_omp_task_data_arg_ptr (stmt), callback_op,
394 wi, pset);
395 if (ret)
396 return ret;
397 ret = walk_tree (gimple_omp_task_copy_fn_ptr (stmt), callback_op,
398 wi, pset);
399 if (ret)
400 return ret;
401 ret = walk_tree (gimple_omp_task_arg_size_ptr (stmt), callback_op,
402 wi, pset);
403 if (ret)
404 return ret;
405 ret = walk_tree (gimple_omp_task_arg_align_ptr (stmt), callback_op,
406 wi, pset);
407 if (ret)
408 return ret;
409 break;
411 case GIMPLE_OMP_SECTIONS:
412 ret = walk_tree (gimple_omp_sections_clauses_ptr (stmt), callback_op,
413 wi, pset);
414 if (ret)
415 return ret;
416 ret = walk_tree (gimple_omp_sections_control_ptr (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 (stmt), callback_op, wi,
425 pset);
426 if (ret)
427 return ret;
428 break;
430 case GIMPLE_OMP_TARGET:
432 gomp_target *omp_stmt = as_a <gomp_target *> (stmt);
433 ret = walk_tree (gimple_omp_target_clauses_ptr (omp_stmt),
434 callback_op, wi, pset);
435 if (ret)
436 return ret;
437 ret = walk_tree (gimple_omp_target_child_fn_ptr (omp_stmt),
438 callback_op, wi, pset);
439 if (ret)
440 return ret;
441 ret = walk_tree (gimple_omp_target_data_arg_ptr (omp_stmt),
442 callback_op, wi, pset);
443 if (ret)
444 return ret;
446 break;
448 case GIMPLE_OMP_TEAMS:
449 ret = walk_tree (gimple_omp_teams_clauses_ptr (stmt), callback_op, wi,
450 pset);
451 if (ret)
452 return ret;
453 break;
455 case GIMPLE_OMP_ATOMIC_LOAD:
457 gomp_atomic_load *omp_stmt = as_a <gomp_atomic_load *> (stmt);
458 ret = walk_tree (gimple_omp_atomic_load_lhs_ptr (omp_stmt),
459 callback_op, wi, pset);
460 if (ret)
461 return ret;
462 ret = walk_tree (gimple_omp_atomic_load_rhs_ptr (omp_stmt),
463 callback_op, wi, pset);
464 if (ret)
465 return ret;
467 break;
469 case GIMPLE_OMP_ATOMIC_STORE:
471 gomp_atomic_store *omp_stmt = as_a <gomp_atomic_store *> (stmt);
472 ret = walk_tree (gimple_omp_atomic_store_val_ptr (omp_stmt),
473 callback_op, wi, pset);
474 if (ret)
475 return ret;
477 break;
479 case GIMPLE_TRANSACTION:
480 ret = walk_tree (gimple_transaction_label_ptr (
481 as_a <gtransaction *> (stmt)),
482 callback_op, wi, pset);
483 if (ret)
484 return ret;
485 break;
487 case GIMPLE_OMP_RETURN:
488 ret = walk_tree (gimple_omp_return_lhs_ptr (stmt), callback_op, wi,
489 pset);
490 if (ret)
491 return ret;
492 break;
494 /* Tuples that do not have operands. */
495 case GIMPLE_NOP:
496 case GIMPLE_RESX:
497 case GIMPLE_PREDICT:
498 break;
500 default:
502 enum gimple_statement_structure_enum gss;
503 gss = gimple_statement_structure (stmt);
504 if (gss == GSS_WITH_OPS || gss == GSS_WITH_MEM_OPS)
505 for (i = 0; i < gimple_num_ops (stmt); i++)
507 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi, pset);
508 if (ret)
509 return ret;
512 break;
515 return NULL_TREE;
519 /* Walk the current statement in GSI (optionally using traversal state
520 stored in WI). If WI is NULL, no state is kept during traversal.
521 The callback CALLBACK_STMT is called. If CALLBACK_STMT indicates
522 that it has handled all the operands of the statement, its return
523 value is returned. Otherwise, the return value from CALLBACK_STMT
524 is discarded and its operands are scanned.
526 If CALLBACK_STMT is NULL or it didn't handle the operands,
527 CALLBACK_OP is called on each operand of the statement via
528 walk_gimple_op. If walk_gimple_op returns non-NULL for any
529 operand, the remaining operands are not scanned. In this case, the
530 return value from CALLBACK_OP is returned.
532 In any other case, NULL_TREE is returned. */
534 tree
535 walk_gimple_stmt (gimple_stmt_iterator *gsi, walk_stmt_fn callback_stmt,
536 walk_tree_fn callback_op, struct walk_stmt_info *wi)
538 gimple *ret;
539 tree tree_ret;
540 gimple *stmt = gsi_stmt (*gsi);
542 if (wi)
544 wi->gsi = *gsi;
545 wi->removed_stmt = false;
547 if (wi->want_locations && gimple_has_location (stmt))
548 input_location = gimple_location (stmt);
551 ret = NULL;
553 /* Invoke the statement callback. Return if the callback handled
554 all of STMT operands by itself. */
555 if (callback_stmt)
557 bool handled_ops = false;
558 tree_ret = callback_stmt (gsi, &handled_ops, wi);
559 if (handled_ops)
560 return tree_ret;
562 /* If CALLBACK_STMT did not handle operands, it should not have
563 a value to return. */
564 gcc_assert (tree_ret == NULL);
566 if (wi && wi->removed_stmt)
567 return NULL;
569 /* Re-read stmt in case the callback changed it. */
570 stmt = gsi_stmt (*gsi);
573 /* If CALLBACK_OP is defined, invoke it on every operand of STMT. */
574 if (callback_op)
576 tree_ret = walk_gimple_op (stmt, callback_op, wi);
577 if (tree_ret)
578 return tree_ret;
581 /* If STMT can have statements inside (e.g. GIMPLE_BIND), walk them. */
582 switch (gimple_code (stmt))
584 case GIMPLE_BIND:
585 ret = walk_gimple_seq_mod (gimple_bind_body_ptr (as_a <gbind *> (stmt)),
586 callback_stmt, callback_op, wi);
587 if (ret)
588 return wi->callback_result;
589 break;
591 case GIMPLE_CATCH:
592 ret = walk_gimple_seq_mod (gimple_catch_handler_ptr (
593 as_a <gcatch *> (stmt)),
594 callback_stmt, callback_op, wi);
595 if (ret)
596 return wi->callback_result;
597 break;
599 case GIMPLE_EH_FILTER:
600 ret = walk_gimple_seq_mod (gimple_eh_filter_failure_ptr (stmt), callback_stmt,
601 callback_op, wi);
602 if (ret)
603 return wi->callback_result;
604 break;
606 case GIMPLE_EH_ELSE:
608 geh_else *eh_else_stmt = as_a <geh_else *> (stmt);
609 ret = walk_gimple_seq_mod (gimple_eh_else_n_body_ptr (eh_else_stmt),
610 callback_stmt, callback_op, wi);
611 if (ret)
612 return wi->callback_result;
613 ret = walk_gimple_seq_mod (gimple_eh_else_e_body_ptr (eh_else_stmt),
614 callback_stmt, callback_op, wi);
615 if (ret)
616 return wi->callback_result;
618 break;
620 case GIMPLE_TRY:
621 ret = walk_gimple_seq_mod (gimple_try_eval_ptr (stmt), callback_stmt, callback_op,
622 wi);
623 if (ret)
624 return wi->callback_result;
626 ret = walk_gimple_seq_mod (gimple_try_cleanup_ptr (stmt), callback_stmt,
627 callback_op, wi);
628 if (ret)
629 return wi->callback_result;
630 break;
632 case GIMPLE_OMP_FOR:
633 ret = walk_gimple_seq_mod (gimple_omp_for_pre_body_ptr (stmt), callback_stmt,
634 callback_op, wi);
635 if (ret)
636 return wi->callback_result;
638 /* FALL THROUGH. */
639 case GIMPLE_OMP_CRITICAL:
640 case GIMPLE_OMP_MASTER:
641 case GIMPLE_OMP_TASKGROUP:
642 case GIMPLE_OMP_ORDERED:
643 case GIMPLE_OMP_SECTION:
644 case GIMPLE_OMP_PARALLEL:
645 case GIMPLE_OMP_TASK:
646 case GIMPLE_OMP_SECTIONS:
647 case GIMPLE_OMP_SINGLE:
648 case GIMPLE_OMP_TARGET:
649 case GIMPLE_OMP_TEAMS:
650 ret = walk_gimple_seq_mod (gimple_omp_body_ptr (stmt), callback_stmt,
651 callback_op, wi);
652 if (ret)
653 return wi->callback_result;
654 break;
656 case GIMPLE_WITH_CLEANUP_EXPR:
657 ret = walk_gimple_seq_mod (gimple_wce_cleanup_ptr (stmt), callback_stmt,
658 callback_op, wi);
659 if (ret)
660 return wi->callback_result;
661 break;
663 case GIMPLE_TRANSACTION:
664 ret = walk_gimple_seq_mod (gimple_transaction_body_ptr (
665 as_a <gtransaction *> (stmt)),
666 callback_stmt, callback_op, wi);
667 if (ret)
668 return wi->callback_result;
669 break;
671 default:
672 gcc_assert (!gimple_has_substatements (stmt));
673 break;
676 return NULL;
679 /* From a tree operand OP return the base of a load or store operation
680 or NULL_TREE if OP is not a load or a store. */
682 static tree
683 get_base_loadstore (tree op)
685 while (handled_component_p (op))
686 op = TREE_OPERAND (op, 0);
687 if (DECL_P (op)
688 || INDIRECT_REF_P (op)
689 || TREE_CODE (op) == MEM_REF
690 || TREE_CODE (op) == TARGET_MEM_REF)
691 return op;
692 return NULL_TREE;
696 /* For the statement STMT call the callbacks VISIT_LOAD, VISIT_STORE and
697 VISIT_ADDR if non-NULL on loads, store and address-taken operands
698 passing the STMT, the base of the operand, the operand itself containing
699 the base and DATA to it. The base will be either a decl, an indirect
700 reference (including TARGET_MEM_REF) or the argument of an address
701 expression.
702 Returns the results of these callbacks or'ed. */
704 bool
705 walk_stmt_load_store_addr_ops (gimple *stmt, void *data,
706 walk_stmt_load_store_addr_fn visit_load,
707 walk_stmt_load_store_addr_fn visit_store,
708 walk_stmt_load_store_addr_fn visit_addr)
710 bool ret = false;
711 unsigned i;
712 if (gimple_assign_single_p (stmt))
714 tree lhs, rhs, arg;
715 if (visit_store)
717 arg = gimple_assign_lhs (stmt);
718 lhs = get_base_loadstore (arg);
719 if (lhs)
720 ret |= visit_store (stmt, lhs, arg, data);
722 arg = gimple_assign_rhs1 (stmt);
723 rhs = arg;
724 while (handled_component_p (rhs))
725 rhs = TREE_OPERAND (rhs, 0);
726 if (visit_addr)
728 if (TREE_CODE (rhs) == ADDR_EXPR)
729 ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), arg, data);
730 else if (TREE_CODE (rhs) == TARGET_MEM_REF
731 && TREE_CODE (TMR_BASE (rhs)) == ADDR_EXPR)
732 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (rhs), 0), arg,
733 data);
734 else if (TREE_CODE (rhs) == OBJ_TYPE_REF
735 && TREE_CODE (OBJ_TYPE_REF_OBJECT (rhs)) == ADDR_EXPR)
736 ret |= visit_addr (stmt, TREE_OPERAND (OBJ_TYPE_REF_OBJECT (rhs),
737 0), arg, data);
738 else if (TREE_CODE (rhs) == CONSTRUCTOR)
740 unsigned int ix;
741 tree val;
743 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), ix, val)
744 if (TREE_CODE (val) == ADDR_EXPR)
745 ret |= visit_addr (stmt, TREE_OPERAND (val, 0), arg, data);
746 else if (TREE_CODE (val) == OBJ_TYPE_REF
747 && TREE_CODE (OBJ_TYPE_REF_OBJECT (val)) == ADDR_EXPR)
748 ret |= visit_addr (stmt,
749 TREE_OPERAND (OBJ_TYPE_REF_OBJECT (val),
750 0), arg, data);
752 lhs = gimple_assign_lhs (stmt);
753 if (TREE_CODE (lhs) == TARGET_MEM_REF
754 && TREE_CODE (TMR_BASE (lhs)) == ADDR_EXPR)
755 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (lhs), 0), lhs, data);
757 if (visit_load)
759 rhs = get_base_loadstore (rhs);
760 if (rhs)
761 ret |= visit_load (stmt, rhs, arg, data);
764 else if (visit_addr
765 && (is_gimple_assign (stmt)
766 || gimple_code (stmt) == GIMPLE_COND))
768 for (i = 0; i < gimple_num_ops (stmt); ++i)
770 tree op = gimple_op (stmt, i);
771 if (op == NULL_TREE)
773 else if (TREE_CODE (op) == ADDR_EXPR)
774 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
775 /* COND_EXPR and VCOND_EXPR rhs1 argument is a comparison
776 tree with two operands. */
777 else if (i == 1 && COMPARISON_CLASS_P (op))
779 if (TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
780 ret |= visit_addr (stmt, TREE_OPERAND (TREE_OPERAND (op, 0),
781 0), op, data);
782 if (TREE_CODE (TREE_OPERAND (op, 1)) == ADDR_EXPR)
783 ret |= visit_addr (stmt, TREE_OPERAND (TREE_OPERAND (op, 1),
784 0), op, data);
788 else if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
790 if (visit_store)
792 tree arg = gimple_call_lhs (call_stmt);
793 if (arg)
795 tree lhs = get_base_loadstore (arg);
796 if (lhs)
797 ret |= visit_store (stmt, lhs, arg, data);
800 if (visit_load || visit_addr)
801 for (i = 0; i < gimple_call_num_args (call_stmt); ++i)
803 tree arg = gimple_call_arg (call_stmt, i);
804 if (visit_addr
805 && TREE_CODE (arg) == ADDR_EXPR)
806 ret |= visit_addr (stmt, TREE_OPERAND (arg, 0), arg, data);
807 else if (visit_load)
809 tree rhs = get_base_loadstore (arg);
810 if (rhs)
811 ret |= visit_load (stmt, rhs, arg, data);
814 if (visit_addr
815 && gimple_call_chain (call_stmt)
816 && TREE_CODE (gimple_call_chain (call_stmt)) == ADDR_EXPR)
817 ret |= visit_addr (stmt, TREE_OPERAND (gimple_call_chain (call_stmt), 0),
818 gimple_call_chain (call_stmt), data);
819 if (visit_addr
820 && gimple_call_return_slot_opt_p (call_stmt)
821 && gimple_call_lhs (call_stmt) != NULL_TREE
822 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (call_stmt))))
823 ret |= visit_addr (stmt, gimple_call_lhs (call_stmt),
824 gimple_call_lhs (call_stmt), data);
826 else if (gasm *asm_stmt = dyn_cast <gasm *> (stmt))
828 unsigned noutputs;
829 const char *constraint;
830 const char **oconstraints;
831 bool allows_mem, allows_reg, is_inout;
832 noutputs = gimple_asm_noutputs (asm_stmt);
833 oconstraints = XALLOCAVEC (const char *, noutputs);
834 if (visit_store || visit_addr)
835 for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i)
837 tree link = gimple_asm_output_op (asm_stmt, i);
838 tree op = get_base_loadstore (TREE_VALUE (link));
839 if (op && visit_store)
840 ret |= visit_store (stmt, op, TREE_VALUE (link), data);
841 if (visit_addr)
843 constraint = TREE_STRING_POINTER
844 (TREE_VALUE (TREE_PURPOSE (link)));
845 oconstraints[i] = constraint;
846 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
847 &allows_reg, &is_inout);
848 if (op && !allows_reg && allows_mem)
849 ret |= visit_addr (stmt, op, TREE_VALUE (link), data);
852 if (visit_load || visit_addr)
853 for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
855 tree link = gimple_asm_input_op (asm_stmt, i);
856 tree op = TREE_VALUE (link);
857 if (visit_addr
858 && TREE_CODE (op) == ADDR_EXPR)
859 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
860 else if (visit_load || visit_addr)
862 op = get_base_loadstore (op);
863 if (op)
865 if (visit_load)
866 ret |= visit_load (stmt, op, TREE_VALUE (link), data);
867 if (visit_addr)
869 constraint = TREE_STRING_POINTER
870 (TREE_VALUE (TREE_PURPOSE (link)));
871 parse_input_constraint (&constraint, 0, 0, noutputs,
872 0, oconstraints,
873 &allows_mem, &allows_reg);
874 if (!allows_reg && allows_mem)
875 ret |= visit_addr (stmt, op, TREE_VALUE (link),
876 data);
882 else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
884 tree op = gimple_return_retval (return_stmt);
885 if (op)
887 if (visit_addr
888 && TREE_CODE (op) == ADDR_EXPR)
889 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
890 else if (visit_load)
892 tree base = get_base_loadstore (op);
893 if (base)
894 ret |= visit_load (stmt, base, op, data);
898 else if (visit_addr
899 && gimple_code (stmt) == GIMPLE_PHI)
901 for (i = 0; i < gimple_phi_num_args (stmt); ++i)
903 tree op = gimple_phi_arg_def (stmt, i);
904 if (TREE_CODE (op) == ADDR_EXPR)
905 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
908 else if (visit_addr
909 && gimple_code (stmt) == GIMPLE_GOTO)
911 tree op = gimple_goto_dest (stmt);
912 if (TREE_CODE (op) == ADDR_EXPR)
913 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
916 return ret;
919 /* Like walk_stmt_load_store_addr_ops but with NULL visit_addr. IPA-CP
920 should make a faster clone for this case. */
922 bool
923 walk_stmt_load_store_ops (gimple *stmt, void *data,
924 walk_stmt_load_store_addr_fn visit_load,
925 walk_stmt_load_store_addr_fn visit_store)
927 return walk_stmt_load_store_addr_ops (stmt, data,
928 visit_load, visit_store, NULL);