2014-10-30 Hristian Kirtchev <kirtchev@adacore.com>
[official-gcc.git] / gcc / gimple-walk.c
blobbfa3532c128e260e04d86eddca9d1f64afeeaa2f
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 (gimple 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 (stmt), 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 (stmt), callback_op, wi,
289 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 (stmt, callback_op, wi);
303 if (ret)
304 return ret;
305 break;
307 case GIMPLE_OMP_CONTINUE:
308 ret = walk_tree (gimple_omp_continue_control_def_ptr (stmt),
309 callback_op, wi, pset);
310 if (ret)
311 return ret;
313 ret = walk_tree (gimple_omp_continue_control_use_ptr (stmt),
314 callback_op, wi, pset);
315 if (ret)
316 return ret;
317 break;
319 case GIMPLE_OMP_CRITICAL:
320 ret = walk_tree (gimple_omp_critical_name_ptr (stmt), callback_op, wi,
321 pset);
322 if (ret)
323 return ret;
324 break;
326 case GIMPLE_OMP_FOR:
327 ret = walk_tree (gimple_omp_for_clauses_ptr (stmt), callback_op, wi,
328 pset);
329 if (ret)
330 return ret;
331 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
333 ret = walk_tree (gimple_omp_for_index_ptr (stmt, i), callback_op,
334 wi, pset);
335 if (ret)
336 return ret;
337 ret = walk_tree (gimple_omp_for_initial_ptr (stmt, i), callback_op,
338 wi, pset);
339 if (ret)
340 return ret;
341 ret = walk_tree (gimple_omp_for_final_ptr (stmt, i), callback_op,
342 wi, pset);
343 if (ret)
344 return ret;
345 ret = walk_tree (gimple_omp_for_incr_ptr (stmt, i), callback_op,
346 wi, pset);
348 if (ret)
349 return ret;
350 break;
352 case GIMPLE_OMP_PARALLEL:
353 ret = walk_tree (gimple_omp_parallel_clauses_ptr (stmt), callback_op,
354 wi, pset);
355 if (ret)
356 return ret;
357 ret = walk_tree (gimple_omp_parallel_child_fn_ptr (stmt), callback_op,
358 wi, pset);
359 if (ret)
360 return ret;
361 ret = walk_tree (gimple_omp_parallel_data_arg_ptr (stmt), callback_op,
362 wi, pset);
363 if (ret)
364 return ret;
365 break;
367 case GIMPLE_OMP_TASK:
368 ret = walk_tree (gimple_omp_task_clauses_ptr (stmt), callback_op,
369 wi, pset);
370 if (ret)
371 return ret;
372 ret = walk_tree (gimple_omp_task_child_fn_ptr (stmt), callback_op,
373 wi, pset);
374 if (ret)
375 return ret;
376 ret = walk_tree (gimple_omp_task_data_arg_ptr (stmt), callback_op,
377 wi, pset);
378 if (ret)
379 return ret;
380 ret = walk_tree (gimple_omp_task_copy_fn_ptr (stmt), callback_op,
381 wi, pset);
382 if (ret)
383 return ret;
384 ret = walk_tree (gimple_omp_task_arg_size_ptr (stmt), callback_op,
385 wi, pset);
386 if (ret)
387 return ret;
388 ret = walk_tree (gimple_omp_task_arg_align_ptr (stmt), callback_op,
389 wi, pset);
390 if (ret)
391 return ret;
392 break;
394 case GIMPLE_OMP_SECTIONS:
395 ret = walk_tree (gimple_omp_sections_clauses_ptr (stmt), callback_op,
396 wi, pset);
397 if (ret)
398 return ret;
400 ret = walk_tree (gimple_omp_sections_control_ptr (stmt), callback_op,
401 wi, pset);
402 if (ret)
403 return ret;
405 break;
407 case GIMPLE_OMP_SINGLE:
408 ret = walk_tree (gimple_omp_single_clauses_ptr (stmt), callback_op, wi,
409 pset);
410 if (ret)
411 return ret;
412 break;
414 case GIMPLE_OMP_TARGET:
415 ret = walk_tree (gimple_omp_target_clauses_ptr (stmt), callback_op, wi,
416 pset);
417 if (ret)
418 return ret;
419 break;
421 case GIMPLE_OMP_TEAMS:
422 ret = walk_tree (gimple_omp_teams_clauses_ptr (stmt), callback_op, wi,
423 pset);
424 if (ret)
425 return ret;
426 break;
428 case GIMPLE_OMP_ATOMIC_LOAD:
429 ret = walk_tree (gimple_omp_atomic_load_lhs_ptr (stmt), callback_op, wi,
430 pset);
431 if (ret)
432 return ret;
434 ret = walk_tree (gimple_omp_atomic_load_rhs_ptr (stmt), callback_op, wi,
435 pset);
436 if (ret)
437 return ret;
438 break;
440 case GIMPLE_OMP_ATOMIC_STORE:
441 ret = walk_tree (gimple_omp_atomic_store_val_ptr (stmt), callback_op,
442 wi, pset);
443 if (ret)
444 return ret;
445 break;
447 case GIMPLE_TRANSACTION:
448 ret = walk_tree (gimple_transaction_label_ptr (stmt), callback_op,
449 wi, pset);
450 if (ret)
451 return ret;
452 break;
454 case GIMPLE_OMP_RETURN:
455 ret = walk_tree (gimple_omp_return_lhs_ptr (stmt), callback_op, wi,
456 pset);
457 if (ret)
458 return ret;
459 break;
461 /* Tuples that do not have operands. */
462 case GIMPLE_NOP:
463 case GIMPLE_RESX:
464 case GIMPLE_PREDICT:
465 break;
467 default:
469 enum gimple_statement_structure_enum gss;
470 gss = gimple_statement_structure (stmt);
471 if (gss == GSS_WITH_OPS || gss == GSS_WITH_MEM_OPS)
472 for (i = 0; i < gimple_num_ops (stmt); i++)
474 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi, pset);
475 if (ret)
476 return ret;
479 break;
482 return NULL_TREE;
486 /* Walk the current statement in GSI (optionally using traversal state
487 stored in WI). If WI is NULL, no state is kept during traversal.
488 The callback CALLBACK_STMT is called. If CALLBACK_STMT indicates
489 that it has handled all the operands of the statement, its return
490 value is returned. Otherwise, the return value from CALLBACK_STMT
491 is discarded and its operands are scanned.
493 If CALLBACK_STMT is NULL or it didn't handle the operands,
494 CALLBACK_OP is called on each operand of the statement via
495 walk_gimple_op. If walk_gimple_op returns non-NULL for any
496 operand, the remaining operands are not scanned. In this case, the
497 return value from CALLBACK_OP is returned.
499 In any other case, NULL_TREE is returned. */
501 tree
502 walk_gimple_stmt (gimple_stmt_iterator *gsi, walk_stmt_fn callback_stmt,
503 walk_tree_fn callback_op, struct walk_stmt_info *wi)
505 gimple ret;
506 tree tree_ret;
507 gimple stmt = gsi_stmt (*gsi);
509 if (wi)
511 wi->gsi = *gsi;
512 wi->removed_stmt = false;
514 if (wi->want_locations && gimple_has_location (stmt))
515 input_location = gimple_location (stmt);
518 ret = NULL;
520 /* Invoke the statement callback. Return if the callback handled
521 all of STMT operands by itself. */
522 if (callback_stmt)
524 bool handled_ops = false;
525 tree_ret = callback_stmt (gsi, &handled_ops, wi);
526 if (handled_ops)
527 return tree_ret;
529 /* If CALLBACK_STMT did not handle operands, it should not have
530 a value to return. */
531 gcc_assert (tree_ret == NULL);
533 if (wi && wi->removed_stmt)
534 return NULL;
536 /* Re-read stmt in case the callback changed it. */
537 stmt = gsi_stmt (*gsi);
540 /* If CALLBACK_OP is defined, invoke it on every operand of STMT. */
541 if (callback_op)
543 tree_ret = walk_gimple_op (stmt, callback_op, wi);
544 if (tree_ret)
545 return tree_ret;
548 /* If STMT can have statements inside (e.g. GIMPLE_BIND), walk them. */
549 switch (gimple_code (stmt))
551 case GIMPLE_BIND:
552 ret = walk_gimple_seq_mod (gimple_bind_body_ptr (stmt), callback_stmt,
553 callback_op, wi);
554 if (ret)
555 return wi->callback_result;
556 break;
558 case GIMPLE_CATCH:
559 ret = walk_gimple_seq_mod (gimple_catch_handler_ptr (stmt), callback_stmt,
560 callback_op, wi);
561 if (ret)
562 return wi->callback_result;
563 break;
565 case GIMPLE_EH_FILTER:
566 ret = walk_gimple_seq_mod (gimple_eh_filter_failure_ptr (stmt), callback_stmt,
567 callback_op, wi);
568 if (ret)
569 return wi->callback_result;
570 break;
572 case GIMPLE_EH_ELSE:
573 ret = walk_gimple_seq_mod (gimple_eh_else_n_body_ptr (stmt),
574 callback_stmt, callback_op, wi);
575 if (ret)
576 return wi->callback_result;
577 ret = walk_gimple_seq_mod (gimple_eh_else_e_body_ptr (stmt),
578 callback_stmt, callback_op, wi);
579 if (ret)
580 return wi->callback_result;
581 break;
583 case GIMPLE_TRY:
584 ret = walk_gimple_seq_mod (gimple_try_eval_ptr (stmt), callback_stmt, callback_op,
585 wi);
586 if (ret)
587 return wi->callback_result;
589 ret = walk_gimple_seq_mod (gimple_try_cleanup_ptr (stmt), callback_stmt,
590 callback_op, wi);
591 if (ret)
592 return wi->callback_result;
593 break;
595 case GIMPLE_OMP_FOR:
596 ret = walk_gimple_seq_mod (gimple_omp_for_pre_body_ptr (stmt), callback_stmt,
597 callback_op, wi);
598 if (ret)
599 return wi->callback_result;
601 /* FALL THROUGH. */
602 case GIMPLE_OMP_CRITICAL:
603 case GIMPLE_OMP_MASTER:
604 case GIMPLE_OMP_TASKGROUP:
605 case GIMPLE_OMP_ORDERED:
606 case GIMPLE_OMP_SECTION:
607 case GIMPLE_OMP_PARALLEL:
608 case GIMPLE_OMP_TASK:
609 case GIMPLE_OMP_SECTIONS:
610 case GIMPLE_OMP_SINGLE:
611 case GIMPLE_OMP_TARGET:
612 case GIMPLE_OMP_TEAMS:
613 ret = walk_gimple_seq_mod (gimple_omp_body_ptr (stmt), callback_stmt,
614 callback_op, wi);
615 if (ret)
616 return wi->callback_result;
617 break;
619 case GIMPLE_WITH_CLEANUP_EXPR:
620 ret = walk_gimple_seq_mod (gimple_wce_cleanup_ptr (stmt), callback_stmt,
621 callback_op, wi);
622 if (ret)
623 return wi->callback_result;
624 break;
626 case GIMPLE_TRANSACTION:
627 ret = walk_gimple_seq_mod (gimple_transaction_body_ptr (stmt),
628 callback_stmt, callback_op, wi);
629 if (ret)
630 return wi->callback_result;
631 break;
633 default:
634 gcc_assert (!gimple_has_substatements (stmt));
635 break;
638 return NULL;
641 /* From a tree operand OP return the base of a load or store operation
642 or NULL_TREE if OP is not a load or a store. */
644 static tree
645 get_base_loadstore (tree op)
647 while (handled_component_p (op))
648 op = TREE_OPERAND (op, 0);
649 if (DECL_P (op)
650 || INDIRECT_REF_P (op)
651 || TREE_CODE (op) == MEM_REF
652 || TREE_CODE (op) == TARGET_MEM_REF)
653 return op;
654 return NULL_TREE;
658 /* For the statement STMT call the callbacks VISIT_LOAD, VISIT_STORE and
659 VISIT_ADDR if non-NULL on loads, store and address-taken operands
660 passing the STMT, the base of the operand, the operand itself containing
661 the base and DATA to it. The base will be either a decl, an indirect
662 reference (including TARGET_MEM_REF) or the argument of an address
663 expression.
664 Returns the results of these callbacks or'ed. */
666 bool
667 walk_stmt_load_store_addr_ops (gimple stmt, void *data,
668 walk_stmt_load_store_addr_fn visit_load,
669 walk_stmt_load_store_addr_fn visit_store,
670 walk_stmt_load_store_addr_fn visit_addr)
672 bool ret = false;
673 unsigned i;
674 if (gimple_assign_single_p (stmt))
676 tree lhs, rhs, arg;
677 if (visit_store)
679 arg = gimple_assign_lhs (stmt);
680 lhs = get_base_loadstore (arg);
681 if (lhs)
682 ret |= visit_store (stmt, lhs, arg, data);
684 arg = gimple_assign_rhs1 (stmt);
685 rhs = arg;
686 while (handled_component_p (rhs))
687 rhs = TREE_OPERAND (rhs, 0);
688 if (visit_addr)
690 if (TREE_CODE (rhs) == ADDR_EXPR)
691 ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), arg, data);
692 else if (TREE_CODE (rhs) == TARGET_MEM_REF
693 && TREE_CODE (TMR_BASE (rhs)) == ADDR_EXPR)
694 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (rhs), 0), arg,
695 data);
696 else if (TREE_CODE (rhs) == OBJ_TYPE_REF
697 && TREE_CODE (OBJ_TYPE_REF_OBJECT (rhs)) == ADDR_EXPR)
698 ret |= visit_addr (stmt, TREE_OPERAND (OBJ_TYPE_REF_OBJECT (rhs),
699 0), arg, data);
700 else if (TREE_CODE (rhs) == CONSTRUCTOR)
702 unsigned int ix;
703 tree val;
705 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), ix, val)
706 if (TREE_CODE (val) == ADDR_EXPR)
707 ret |= visit_addr (stmt, TREE_OPERAND (val, 0), arg, data);
708 else if (TREE_CODE (val) == OBJ_TYPE_REF
709 && TREE_CODE (OBJ_TYPE_REF_OBJECT (val)) == ADDR_EXPR)
710 ret |= visit_addr (stmt,
711 TREE_OPERAND (OBJ_TYPE_REF_OBJECT (val),
712 0), arg, data);
714 lhs = gimple_assign_lhs (stmt);
715 if (TREE_CODE (lhs) == TARGET_MEM_REF
716 && TREE_CODE (TMR_BASE (lhs)) == ADDR_EXPR)
717 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (lhs), 0), lhs, data);
719 if (visit_load)
721 rhs = get_base_loadstore (rhs);
722 if (rhs)
723 ret |= visit_load (stmt, rhs, arg, data);
726 else if (visit_addr
727 && (is_gimple_assign (stmt)
728 || gimple_code (stmt) == GIMPLE_COND))
730 for (i = 0; i < gimple_num_ops (stmt); ++i)
732 tree op = gimple_op (stmt, i);
733 if (op == NULL_TREE)
735 else if (TREE_CODE (op) == ADDR_EXPR)
736 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
737 /* COND_EXPR and VCOND_EXPR rhs1 argument is a comparison
738 tree with two operands. */
739 else if (i == 1 && COMPARISON_CLASS_P (op))
741 if (TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
742 ret |= visit_addr (stmt, TREE_OPERAND (TREE_OPERAND (op, 0),
743 0), op, data);
744 if (TREE_CODE (TREE_OPERAND (op, 1)) == ADDR_EXPR)
745 ret |= visit_addr (stmt, TREE_OPERAND (TREE_OPERAND (op, 1),
746 0), op, data);
750 else if (is_gimple_call (stmt))
752 if (visit_store)
754 tree arg = gimple_call_lhs (stmt);
755 if (arg)
757 tree lhs = get_base_loadstore (arg);
758 if (lhs)
759 ret |= visit_store (stmt, lhs, arg, data);
762 if (visit_load || visit_addr)
763 for (i = 0; i < gimple_call_num_args (stmt); ++i)
765 tree arg = gimple_call_arg (stmt, i);
766 if (visit_addr
767 && TREE_CODE (arg) == ADDR_EXPR)
768 ret |= visit_addr (stmt, TREE_OPERAND (arg, 0), arg, data);
769 else if (visit_load)
771 tree rhs = get_base_loadstore (arg);
772 if (rhs)
773 ret |= visit_load (stmt, rhs, arg, data);
776 if (visit_addr
777 && gimple_call_chain (stmt)
778 && TREE_CODE (gimple_call_chain (stmt)) == ADDR_EXPR)
779 ret |= visit_addr (stmt, TREE_OPERAND (gimple_call_chain (stmt), 0),
780 gimple_call_chain (stmt), data);
781 if (visit_addr
782 && gimple_call_return_slot_opt_p (stmt)
783 && gimple_call_lhs (stmt) != NULL_TREE
784 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
785 ret |= visit_addr (stmt, gimple_call_lhs (stmt),
786 gimple_call_lhs (stmt), data);
788 else if (gimple_code (stmt) == GIMPLE_ASM)
790 unsigned noutputs;
791 const char *constraint;
792 const char **oconstraints;
793 bool allows_mem, allows_reg, is_inout;
794 noutputs = gimple_asm_noutputs (stmt);
795 oconstraints = XALLOCAVEC (const char *, noutputs);
796 if (visit_store || visit_addr)
797 for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
799 tree link = gimple_asm_output_op (stmt, i);
800 tree op = get_base_loadstore (TREE_VALUE (link));
801 if (op && visit_store)
802 ret |= visit_store (stmt, op, TREE_VALUE (link), data);
803 if (visit_addr)
805 constraint = TREE_STRING_POINTER
806 (TREE_VALUE (TREE_PURPOSE (link)));
807 oconstraints[i] = constraint;
808 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
809 &allows_reg, &is_inout);
810 if (op && !allows_reg && allows_mem)
811 ret |= visit_addr (stmt, op, TREE_VALUE (link), data);
814 if (visit_load || visit_addr)
815 for (i = 0; i < gimple_asm_ninputs (stmt); ++i)
817 tree link = gimple_asm_input_op (stmt, i);
818 tree op = TREE_VALUE (link);
819 if (visit_addr
820 && TREE_CODE (op) == ADDR_EXPR)
821 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
822 else if (visit_load || visit_addr)
824 op = get_base_loadstore (op);
825 if (op)
827 if (visit_load)
828 ret |= visit_load (stmt, op, TREE_VALUE (link), data);
829 if (visit_addr)
831 constraint = TREE_STRING_POINTER
832 (TREE_VALUE (TREE_PURPOSE (link)));
833 parse_input_constraint (&constraint, 0, 0, noutputs,
834 0, oconstraints,
835 &allows_mem, &allows_reg);
836 if (!allows_reg && allows_mem)
837 ret |= visit_addr (stmt, op, TREE_VALUE (link),
838 data);
844 else if (gimple_code (stmt) == GIMPLE_RETURN)
846 tree op = gimple_return_retval (stmt);
847 if (op)
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)
854 tree base = get_base_loadstore (op);
855 if (base)
856 ret |= visit_load (stmt, base, op, data);
860 else if (visit_addr
861 && gimple_code (stmt) == GIMPLE_PHI)
863 for (i = 0; i < gimple_phi_num_args (stmt); ++i)
865 tree op = gimple_phi_arg_def (stmt, i);
866 if (TREE_CODE (op) == ADDR_EXPR)
867 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
870 else if (visit_addr
871 && gimple_code (stmt) == GIMPLE_GOTO)
873 tree op = gimple_goto_dest (stmt);
874 if (TREE_CODE (op) == ADDR_EXPR)
875 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
878 return ret;
881 /* Like walk_stmt_load_store_addr_ops but with NULL visit_addr. IPA-CP
882 should make a faster clone for this case. */
884 bool
885 walk_stmt_load_store_ops (gimple stmt, void *data,
886 walk_stmt_load_store_addr_fn visit_load,
887 walk_stmt_load_store_addr_fn visit_store)
889 return walk_stmt_load_store_addr_ops (stmt, data,
890 visit_load, visit_store, NULL);