1 /* Gimple walk support.
2 
3    Copyright (C) 2007-2016 Free Software Foundation, Inc.
4    Contributed by Aldy Hernandez <aldyh@redhat.com>
5 
6 This file is part of GCC.
7 
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.
12 
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.
17 
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/>.  */
21 
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 "gimple-iterator.h"
29 #include "gimple-walk.h"
30 #include "stmt.h"
31 
32 /* Walk all the statements in the sequence *PSEQ calling walk_gimple_stmt
33    on each one.  WI is as in walk_gimple_stmt.
34 
35    If walk_gimple_stmt returns non-NULL, the walk is stopped, and the
36    value is stored in WI->CALLBACK_RESULT.  Also, the statement that
37    produced the value is returned if this statement has not been
38    removed by a callback (wi->removed_stmt).  If the statement has
39    been removed, NULL is returned.
40 
41    Otherwise, all the statements are walked and NULL returned.  */
42 
43 gimple *
walk_gimple_seq_mod(gimple_seq * pseq,walk_stmt_fn callback_stmt,walk_tree_fn callback_op,struct walk_stmt_info * wi)44 walk_gimple_seq_mod (gimple_seq *pseq, walk_stmt_fn callback_stmt,
45 		     walk_tree_fn callback_op, struct walk_stmt_info *wi)
46 {
47   gimple_stmt_iterator gsi;
48 
49   for (gsi = gsi_start (*pseq); !gsi_end_p (gsi); )
50     {
51       tree ret = walk_gimple_stmt (&gsi, callback_stmt, callback_op, wi);
52       if (ret)
53 	{
54 	  /* If CALLBACK_STMT or CALLBACK_OP return a value, WI must exist
55 	     to hold it.  */
56 	  gcc_assert (wi);
57 	  wi->callback_result = ret;
58 
59 	  return wi->removed_stmt ? NULL : gsi_stmt (gsi);
60 	}
61 
62       if (!wi->removed_stmt)
63 	gsi_next (&gsi);
64     }
65 
66   if (wi)
67     wi->callback_result = NULL_TREE;
68 
69   return NULL;
70 }
71 
72 
73 /* Like walk_gimple_seq_mod, but ensure that the head of SEQ isn't
74    changed by the callbacks.  */
75 
76 gimple *
walk_gimple_seq(gimple_seq seq,walk_stmt_fn callback_stmt,walk_tree_fn callback_op,struct walk_stmt_info * wi)77 walk_gimple_seq (gimple_seq seq, walk_stmt_fn callback_stmt,
78 		 walk_tree_fn callback_op, struct walk_stmt_info *wi)
79 {
80   gimple_seq seq2 = seq;
81   gimple *ret = walk_gimple_seq_mod (&seq2, callback_stmt, callback_op, wi);
82   gcc_assert (seq2 == seq);
83   return ret;
84 }
85 
86 
87 /* Helper function for walk_gimple_stmt.  Walk operands of a GIMPLE_ASM.  */
88 
89 static tree
walk_gimple_asm(gasm * stmt,walk_tree_fn callback_op,struct walk_stmt_info * wi)90 walk_gimple_asm (gasm *stmt, walk_tree_fn callback_op,
91 		 struct walk_stmt_info *wi)
92 {
93   tree ret, op;
94   unsigned noutputs;
95   const char **oconstraints;
96   unsigned i, n;
97   const char *constraint;
98   bool allows_mem, allows_reg, is_inout;
99 
100   noutputs = gimple_asm_noutputs (stmt);
101   oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
102 
103   if (wi)
104     wi->is_lhs = true;
105 
106   for (i = 0; i < noutputs; i++)
107     {
108       op = gimple_asm_output_op (stmt, i);
109       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
110       oconstraints[i] = constraint;
111       if (wi)
112 	{
113 	  if (parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
114 				       &allows_reg, &is_inout))
115 	    wi->val_only = (allows_reg || !allows_mem);
116 	}
117       ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
118       if (ret)
119 	return ret;
120     }
121 
122   n = gimple_asm_ninputs (stmt);
123   for (i = 0; i < n; i++)
124     {
125       op = gimple_asm_input_op (stmt, i);
126       constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
127 
128       if (wi)
129 	{
130 	  if (parse_input_constraint (&constraint, 0, 0, noutputs, 0,
131 				      oconstraints, &allows_mem, &allows_reg))
132 	    {
133 	      wi->val_only = (allows_reg || !allows_mem);
134 	      /* Although input "m" is not really a LHS, we need a lvalue.  */
135 	      wi->is_lhs = !wi->val_only;
136 	    }
137 	}
138       ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
139       if (ret)
140 	return ret;
141     }
142 
143   if (wi)
144     {
145       wi->is_lhs = false;
146       wi->val_only = true;
147     }
148 
149   n = gimple_asm_nlabels (stmt);
150   for (i = 0; i < n; i++)
151     {
152       op = gimple_asm_label_op (stmt, i);
153       ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
154       if (ret)
155 	return ret;
156     }
157 
158   return NULL_TREE;
159 }
160 
161 
162 /* Helper function of WALK_GIMPLE_STMT.  Walk every tree operand in
163    STMT.  CALLBACK_OP and WI are as in WALK_GIMPLE_STMT.
164 
165    CALLBACK_OP is called on each operand of STMT via walk_tree.
166    Additional parameters to walk_tree must be stored in WI.  For each operand
167    OP, walk_tree is called as:
168 
169 	walk_tree (&OP, CALLBACK_OP, WI, WI->PSET)
170 
171    If CALLBACK_OP returns non-NULL for an operand, the remaining
172    operands are not scanned.
173 
174    The return value is that returned by the last call to walk_tree, or
175    NULL_TREE if no CALLBACK_OP is specified.  */
176 
177 tree
walk_gimple_op(gimple * stmt,walk_tree_fn callback_op,struct walk_stmt_info * wi)178 walk_gimple_op (gimple *stmt, walk_tree_fn callback_op,
179 		struct walk_stmt_info *wi)
180 {
181   hash_set<tree> *pset = (wi) ? wi->pset : NULL;
182   unsigned i;
183   tree ret = NULL_TREE;
184 
185   switch (gimple_code (stmt))
186     {
187     case GIMPLE_ASSIGN:
188       /* Walk the RHS operands.  If the LHS is of a non-renamable type or
189          is a register variable, we may use a COMPONENT_REF on the RHS.  */
190       if (wi)
191 	{
192 	  tree lhs = gimple_assign_lhs (stmt);
193 	  wi->val_only
194 	    = (is_gimple_reg_type (TREE_TYPE (lhs)) && !is_gimple_reg (lhs))
195 	      || gimple_assign_rhs_class (stmt) != GIMPLE_SINGLE_RHS;
196 	}
197 
198       for (i = 1; i < gimple_num_ops (stmt); i++)
199 	{
200 	  ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi,
201 			   pset);
202 	  if (ret)
203 	    return ret;
204 	}
205 
206       /* Walk the LHS.  If the RHS is appropriate for a memory, we
207 	 may use a COMPONENT_REF on the LHS.  */
208       if (wi)
209 	{
210           /* If the RHS is of a non-renamable type or is a register variable,
211 	     we may use a COMPONENT_REF on the LHS.  */
212 	  tree rhs1 = gimple_assign_rhs1 (stmt);
213 	  wi->val_only
214 	    = (is_gimple_reg_type (TREE_TYPE (rhs1)) && !is_gimple_reg (rhs1))
215 	      || gimple_assign_rhs_class (stmt) != GIMPLE_SINGLE_RHS;
216 	  wi->is_lhs = true;
217 	}
218 
219       ret = walk_tree (gimple_op_ptr (stmt, 0), callback_op, wi, pset);
220       if (ret)
221 	return ret;
222 
223       if (wi)
224 	{
225 	  wi->val_only = true;
226 	  wi->is_lhs = false;
227 	}
228       break;
229 
230     case GIMPLE_CALL:
231       if (wi)
232 	{
233 	  wi->is_lhs = false;
234 	  wi->val_only = true;
235 	}
236 
237       ret = walk_tree (gimple_call_chain_ptr (as_a <gcall *> (stmt)),
238 		       callback_op, wi, pset);
239       if (ret)
240         return ret;
241 
242       ret = walk_tree (gimple_call_fn_ptr (stmt), callback_op, wi, pset);
243       if (ret)
244         return ret;
245 
246       for (i = 0; i < gimple_call_num_args (stmt); i++)
247 	{
248 	  if (wi)
249 	    wi->val_only
250 	      = is_gimple_reg_type (TREE_TYPE (gimple_call_arg (stmt, i)));
251 	  ret = walk_tree (gimple_call_arg_ptr (stmt, i), callback_op, wi,
252 			   pset);
253 	  if (ret)
254 	    return ret;
255 	}
256 
257       if (gimple_call_lhs (stmt))
258 	{
259 	  if (wi)
260 	    {
261 	      wi->is_lhs = true;
262 	      wi->val_only
263 		= is_gimple_reg_type (TREE_TYPE (gimple_call_lhs (stmt)));
264 	    }
265 
266 	  ret = walk_tree (gimple_call_lhs_ptr (stmt), callback_op, wi, pset);
267 	  if (ret)
268 	    return ret;
269 	}
270 
271       if (wi)
272 	{
273 	  wi->is_lhs = false;
274 	  wi->val_only = true;
275 	}
276       break;
277 
278     case GIMPLE_CATCH:
279       ret = walk_tree (gimple_catch_types_ptr (as_a <gcatch *> (stmt)),
280 		       callback_op, wi, pset);
281       if (ret)
282 	return ret;
283       break;
284 
285     case GIMPLE_EH_FILTER:
286       ret = walk_tree (gimple_eh_filter_types_ptr (stmt), callback_op, wi,
287 		       pset);
288       if (ret)
289 	return ret;
290       break;
291 
292     case GIMPLE_ASM:
293       ret = walk_gimple_asm (as_a <gasm *> (stmt), callback_op, wi);
294       if (ret)
295 	return ret;
296       break;
297 
298     case GIMPLE_OMP_CONTINUE:
299       {
300 	gomp_continue *cont_stmt = as_a <gomp_continue *> (stmt);
301 	ret = walk_tree (gimple_omp_continue_control_def_ptr (cont_stmt),
302 			 callback_op, wi, pset);
303 	if (ret)
304 	  return ret;
305 
306 	ret = walk_tree (gimple_omp_continue_control_use_ptr (cont_stmt),
307 			 callback_op, wi, pset);
308 	if (ret)
309 	  return ret;
310       }
311       break;
312 
313     case GIMPLE_OMP_CRITICAL:
314       {
315 	gomp_critical *omp_stmt = as_a <gomp_critical *> (stmt);
316 	ret = walk_tree (gimple_omp_critical_name_ptr (omp_stmt),
317 			 callback_op, wi, pset);
318 	if (ret)
319 	  return ret;
320 	ret = walk_tree (gimple_omp_critical_clauses_ptr (omp_stmt),
321 			 callback_op, wi, pset);
322 	if (ret)
323 	  return ret;
324       }
325       break;
326 
327     case GIMPLE_OMP_ORDERED:
328       {
329 	gomp_ordered *omp_stmt = as_a <gomp_ordered *> (stmt);
330 	ret = walk_tree (gimple_omp_ordered_clauses_ptr (omp_stmt),
331 			 callback_op, wi, pset);
332 	if (ret)
333 	  return ret;
334       }
335       break;
336 
337     case GIMPLE_OMP_FOR:
338       ret = walk_tree (gimple_omp_for_clauses_ptr (stmt), callback_op, wi,
339 		       pset);
340       if (ret)
341 	return ret;
342       for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
343 	{
344 	  ret = walk_tree (gimple_omp_for_index_ptr (stmt, i), callback_op,
345 			   wi, pset);
346 	  if (ret)
347 	    return ret;
348 	  ret = walk_tree (gimple_omp_for_initial_ptr (stmt, i), callback_op,
349 			   wi, pset);
350 	  if (ret)
351 	    return ret;
352 	  ret = walk_tree (gimple_omp_for_final_ptr (stmt, i), callback_op,
353 			   wi, pset);
354 	  if (ret)
355 	    return ret;
356 	  ret = walk_tree (gimple_omp_for_incr_ptr (stmt, i), callback_op,
357 			   wi, pset);
358 	  if (ret)
359 	    return ret;
360 	}
361       break;
362 
363     case GIMPLE_OMP_PARALLEL:
364       {
365 	gomp_parallel *omp_par_stmt = as_a <gomp_parallel *> (stmt);
366 	ret = walk_tree (gimple_omp_parallel_clauses_ptr (omp_par_stmt),
367 			 callback_op, wi, pset);
368 	if (ret)
369 	  return ret;
370 	ret = walk_tree (gimple_omp_parallel_child_fn_ptr (omp_par_stmt),
371 			 callback_op, wi, pset);
372 	if (ret)
373 	  return ret;
374 	ret = walk_tree (gimple_omp_parallel_data_arg_ptr (omp_par_stmt),
375 			 callback_op, wi, pset);
376 	if (ret)
377 	  return ret;
378       }
379       break;
380 
381     case GIMPLE_OMP_TASK:
382       ret = walk_tree (gimple_omp_task_clauses_ptr (stmt), callback_op,
383 		       wi, pset);
384       if (ret)
385 	return ret;
386       ret = walk_tree (gimple_omp_task_child_fn_ptr (stmt), callback_op,
387 		       wi, pset);
388       if (ret)
389 	return ret;
390       ret = walk_tree (gimple_omp_task_data_arg_ptr (stmt), callback_op,
391 		       wi, pset);
392       if (ret)
393 	return ret;
394       ret = walk_tree (gimple_omp_task_copy_fn_ptr (stmt), callback_op,
395 		       wi, pset);
396       if (ret)
397 	return ret;
398       ret = walk_tree (gimple_omp_task_arg_size_ptr (stmt), callback_op,
399 		       wi, pset);
400       if (ret)
401 	return ret;
402       ret = walk_tree (gimple_omp_task_arg_align_ptr (stmt), callback_op,
403 		       wi, pset);
404       if (ret)
405 	return ret;
406       break;
407 
408     case GIMPLE_OMP_SECTIONS:
409       ret = walk_tree (gimple_omp_sections_clauses_ptr (stmt), callback_op,
410 		       wi, pset);
411       if (ret)
412 	return ret;
413       ret = walk_tree (gimple_omp_sections_control_ptr (stmt), callback_op,
414 		       wi, pset);
415       if (ret)
416 	return ret;
417 
418       break;
419 
420     case GIMPLE_OMP_SINGLE:
421       ret = walk_tree (gimple_omp_single_clauses_ptr (stmt), callback_op, wi,
422 		       pset);
423       if (ret)
424 	return ret;
425       break;
426 
427     case GIMPLE_OMP_TARGET:
428       {
429 	gomp_target *omp_stmt = as_a <gomp_target *> (stmt);
430 	ret = walk_tree (gimple_omp_target_clauses_ptr (omp_stmt),
431 			 callback_op, wi, pset);
432 	if (ret)
433 	  return ret;
434 	ret = walk_tree (gimple_omp_target_child_fn_ptr (omp_stmt),
435 			 callback_op, wi, pset);
436 	if (ret)
437 	  return ret;
438 	ret = walk_tree (gimple_omp_target_data_arg_ptr (omp_stmt),
439 			 callback_op, wi, pset);
440 	if (ret)
441 	  return ret;
442       }
443       break;
444 
445     case GIMPLE_OMP_TEAMS:
446       ret = walk_tree (gimple_omp_teams_clauses_ptr (stmt), callback_op, wi,
447 		       pset);
448       if (ret)
449 	return ret;
450       break;
451 
452     case GIMPLE_OMP_ATOMIC_LOAD:
453       {
454 	gomp_atomic_load *omp_stmt = as_a <gomp_atomic_load *> (stmt);
455 	ret = walk_tree (gimple_omp_atomic_load_lhs_ptr (omp_stmt),
456 			 callback_op, wi, pset);
457 	if (ret)
458 	  return ret;
459 	ret = walk_tree (gimple_omp_atomic_load_rhs_ptr (omp_stmt),
460 			 callback_op, wi, pset);
461 	if (ret)
462 	  return ret;
463       }
464       break;
465 
466     case GIMPLE_OMP_ATOMIC_STORE:
467       {
468 	gomp_atomic_store *omp_stmt = as_a <gomp_atomic_store *> (stmt);
469 	ret = walk_tree (gimple_omp_atomic_store_val_ptr (omp_stmt),
470 			 callback_op, wi, pset);
471 	if (ret)
472 	  return ret;
473       }
474       break;
475 
476     case GIMPLE_TRANSACTION:
477       {
478 	gtransaction *txn = as_a <gtransaction *> (stmt);
479 
480 	ret = walk_tree (gimple_transaction_label_norm_ptr (txn),
481 			 callback_op, wi, pset);
482 	if (ret)
483 	  return ret;
484 	ret = walk_tree (gimple_transaction_label_uninst_ptr (txn),
485 			 callback_op, wi, pset);
486 	if (ret)
487 	  return ret;
488 	ret = walk_tree (gimple_transaction_label_over_ptr (txn),
489 			 callback_op, wi, pset);
490 	if (ret)
491 	  return ret;
492       }
493       break;
494 
495     case GIMPLE_OMP_RETURN:
496       ret = walk_tree (gimple_omp_return_lhs_ptr (stmt), callback_op, wi,
497 		       pset);
498       if (ret)
499 	return ret;
500       break;
501 
502       /* Tuples that do not have operands.  */
503     case GIMPLE_NOP:
504     case GIMPLE_RESX:
505     case GIMPLE_PREDICT:
506       break;
507 
508     default:
509       {
510 	enum gimple_statement_structure_enum gss;
511 	gss = gimple_statement_structure (stmt);
512 	if (gss == GSS_WITH_OPS || gss == GSS_WITH_MEM_OPS)
513 	  for (i = 0; i < gimple_num_ops (stmt); i++)
514 	    {
515 	      ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi, pset);
516 	      if (ret)
517 		return ret;
518 	    }
519       }
520       break;
521     }
522 
523   return NULL_TREE;
524 }
525 
526 
527 /* Walk the current statement in GSI (optionally using traversal state
528    stored in WI).  If WI is NULL, no state is kept during traversal.
529    The callback CALLBACK_STMT is called.  If CALLBACK_STMT indicates
530    that it has handled all the operands of the statement, its return
531    value is returned.  Otherwise, the return value from CALLBACK_STMT
532    is discarded and its operands are scanned.
533 
534    If CALLBACK_STMT is NULL or it didn't handle the operands,
535    CALLBACK_OP is called on each operand of the statement via
536    walk_gimple_op.  If walk_gimple_op returns non-NULL for any
537    operand, the remaining operands are not scanned.  In this case, the
538    return value from CALLBACK_OP is returned.
539 
540    In any other case, NULL_TREE is returned.  */
541 
542 tree
walk_gimple_stmt(gimple_stmt_iterator * gsi,walk_stmt_fn callback_stmt,walk_tree_fn callback_op,struct walk_stmt_info * wi)543 walk_gimple_stmt (gimple_stmt_iterator *gsi, walk_stmt_fn callback_stmt,
544 		  walk_tree_fn callback_op, struct walk_stmt_info *wi)
545 {
546   gimple *ret;
547   tree tree_ret;
548   gimple *stmt = gsi_stmt (*gsi);
549 
550   if (wi)
551     {
552       wi->gsi = *gsi;
553       wi->removed_stmt = false;
554 
555       if (wi->want_locations && gimple_has_location (stmt))
556 	input_location = gimple_location (stmt);
557     }
558 
559   ret = NULL;
560 
561   /* Invoke the statement callback.  Return if the callback handled
562      all of STMT operands by itself.  */
563   if (callback_stmt)
564     {
565       bool handled_ops = false;
566       tree_ret = callback_stmt (gsi, &handled_ops, wi);
567       if (handled_ops)
568 	return tree_ret;
569 
570       /* If CALLBACK_STMT did not handle operands, it should not have
571 	 a value to return.  */
572       gcc_assert (tree_ret == NULL);
573 
574       if (wi && wi->removed_stmt)
575 	return NULL;
576 
577       /* Re-read stmt in case the callback changed it.  */
578       stmt = gsi_stmt (*gsi);
579     }
580 
581   /* If CALLBACK_OP is defined, invoke it on every operand of STMT.  */
582   if (callback_op)
583     {
584       tree_ret = walk_gimple_op (stmt, callback_op, wi);
585       if (tree_ret)
586 	return tree_ret;
587     }
588 
589   /* If STMT can have statements inside (e.g. GIMPLE_BIND), walk them.  */
590   switch (gimple_code (stmt))
591     {
592     case GIMPLE_BIND:
593       ret = walk_gimple_seq_mod (gimple_bind_body_ptr (as_a <gbind *> (stmt)),
594 				 callback_stmt, callback_op, wi);
595       if (ret)
596 	return wi->callback_result;
597       break;
598 
599     case GIMPLE_CATCH:
600       ret = walk_gimple_seq_mod (gimple_catch_handler_ptr (
601 				   as_a <gcatch *> (stmt)),
602 				 callback_stmt, callback_op, wi);
603       if (ret)
604 	return wi->callback_result;
605       break;
606 
607     case GIMPLE_EH_FILTER:
608       ret = walk_gimple_seq_mod (gimple_eh_filter_failure_ptr (stmt), callback_stmt,
609 		             callback_op, wi);
610       if (ret)
611 	return wi->callback_result;
612       break;
613 
614     case GIMPLE_EH_ELSE:
615       {
616 	geh_else *eh_else_stmt = as_a <geh_else *> (stmt);
617 	ret = walk_gimple_seq_mod (gimple_eh_else_n_body_ptr (eh_else_stmt),
618 				   callback_stmt, callback_op, wi);
619 	if (ret)
620 	  return wi->callback_result;
621 	ret = walk_gimple_seq_mod (gimple_eh_else_e_body_ptr (eh_else_stmt),
622 				   callback_stmt, callback_op, wi);
623 	if (ret)
624 	  return wi->callback_result;
625       }
626       break;
627 
628     case GIMPLE_TRY:
629       ret = walk_gimple_seq_mod (gimple_try_eval_ptr (stmt), callback_stmt, callback_op,
630 	                     wi);
631       if (ret)
632 	return wi->callback_result;
633 
634       ret = walk_gimple_seq_mod (gimple_try_cleanup_ptr (stmt), callback_stmt,
635 	                     callback_op, wi);
636       if (ret)
637 	return wi->callback_result;
638       break;
639 
640     case GIMPLE_OMP_FOR:
641       ret = walk_gimple_seq_mod (gimple_omp_for_pre_body_ptr (stmt), callback_stmt,
642 		             callback_op, wi);
643       if (ret)
644 	return wi->callback_result;
645 
646       /* FALL THROUGH.  */
647     case GIMPLE_OMP_CRITICAL:
648     case GIMPLE_OMP_MASTER:
649     case GIMPLE_OMP_TASKGROUP:
650     case GIMPLE_OMP_ORDERED:
651     case GIMPLE_OMP_SECTION:
652     case GIMPLE_OMP_PARALLEL:
653     case GIMPLE_OMP_TASK:
654     case GIMPLE_OMP_SECTIONS:
655     case GIMPLE_OMP_SINGLE:
656     case GIMPLE_OMP_TARGET:
657     case GIMPLE_OMP_TEAMS:
658     case GIMPLE_OMP_GRID_BODY:
659       ret = walk_gimple_seq_mod (gimple_omp_body_ptr (stmt), callback_stmt,
660 			     callback_op, wi);
661       if (ret)
662 	return wi->callback_result;
663       break;
664 
665     case GIMPLE_WITH_CLEANUP_EXPR:
666       ret = walk_gimple_seq_mod (gimple_wce_cleanup_ptr (stmt), callback_stmt,
667 			     callback_op, wi);
668       if (ret)
669 	return wi->callback_result;
670       break;
671 
672     case GIMPLE_TRANSACTION:
673       ret = walk_gimple_seq_mod (gimple_transaction_body_ptr (
674 				   as_a <gtransaction *> (stmt)),
675 			     callback_stmt, callback_op, wi);
676       if (ret)
677 	return wi->callback_result;
678       break;
679 
680     default:
681       gcc_assert (!gimple_has_substatements (stmt));
682       break;
683     }
684 
685   return NULL;
686 }
687 
688 /* From a tree operand OP return the base of a load or store operation
689    or NULL_TREE if OP is not a load or a store.  */
690 
691 static tree
get_base_loadstore(tree op)692 get_base_loadstore (tree op)
693 {
694   while (handled_component_p (op))
695     op = TREE_OPERAND (op, 0);
696   if (DECL_P (op)
697       || INDIRECT_REF_P (op)
698       || TREE_CODE (op) == MEM_REF
699       || TREE_CODE (op) == TARGET_MEM_REF)
700     return op;
701   return NULL_TREE;
702 }
703 
704 
705 /* For the statement STMT call the callbacks VISIT_LOAD, VISIT_STORE and
706    VISIT_ADDR if non-NULL on loads, store and address-taken operands
707    passing the STMT, the base of the operand, the operand itself containing
708    the base and DATA to it.  The base will be either a decl, an indirect
709    reference (including TARGET_MEM_REF) or the argument of an address
710    expression.
711    Returns the results of these callbacks or'ed.  */
712 
713 bool
walk_stmt_load_store_addr_ops(gimple * stmt,void * data,walk_stmt_load_store_addr_fn visit_load,walk_stmt_load_store_addr_fn visit_store,walk_stmt_load_store_addr_fn visit_addr)714 walk_stmt_load_store_addr_ops (gimple *stmt, void *data,
715 			       walk_stmt_load_store_addr_fn visit_load,
716 			       walk_stmt_load_store_addr_fn visit_store,
717 			       walk_stmt_load_store_addr_fn visit_addr)
718 {
719   bool ret = false;
720   unsigned i;
721   if (gimple_assign_single_p (stmt))
722     {
723       tree lhs, rhs, arg;
724       if (visit_store)
725 	{
726 	  arg = gimple_assign_lhs (stmt);
727 	  lhs = get_base_loadstore (arg);
728 	  if (lhs)
729 	    ret |= visit_store (stmt, lhs, arg, data);
730 	}
731       arg = gimple_assign_rhs1 (stmt);
732       rhs = arg;
733       while (handled_component_p (rhs))
734 	rhs = TREE_OPERAND (rhs, 0);
735       if (visit_addr)
736 	{
737 	  if (TREE_CODE (rhs) == ADDR_EXPR)
738 	    ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), arg, data);
739 	  else if (TREE_CODE (rhs) == TARGET_MEM_REF
740 		   && TREE_CODE (TMR_BASE (rhs)) == ADDR_EXPR)
741 	    ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (rhs), 0), arg,
742 			       data);
743 	  else if (TREE_CODE (rhs) == OBJ_TYPE_REF
744 		   && TREE_CODE (OBJ_TYPE_REF_OBJECT (rhs)) == ADDR_EXPR)
745 	    ret |= visit_addr (stmt, TREE_OPERAND (OBJ_TYPE_REF_OBJECT (rhs),
746 						   0), arg, data);
747 	  else if (TREE_CODE (rhs) == CONSTRUCTOR)
748 	    {
749 	      unsigned int ix;
750 	      tree val;
751 
752 	      FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), ix, val)
753 		if (TREE_CODE (val) == ADDR_EXPR)
754 		  ret |= visit_addr (stmt, TREE_OPERAND (val, 0), arg, data);
755 		else if (TREE_CODE (val) == OBJ_TYPE_REF
756 			 && TREE_CODE (OBJ_TYPE_REF_OBJECT (val)) == ADDR_EXPR)
757 		  ret |= visit_addr (stmt,
758 				     TREE_OPERAND (OBJ_TYPE_REF_OBJECT (val),
759 						   0), arg, data);
760 	    }
761           lhs = gimple_assign_lhs (stmt);
762 	  if (TREE_CODE (lhs) == TARGET_MEM_REF
763               && TREE_CODE (TMR_BASE (lhs)) == ADDR_EXPR)
764 	    ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (lhs), 0), lhs, data);
765 	}
766       if (visit_load)
767 	{
768 	  rhs = get_base_loadstore (rhs);
769 	  if (rhs)
770 	    ret |= visit_load (stmt, rhs, arg, data);
771 	}
772     }
773   else if (visit_addr
774 	   && (is_gimple_assign (stmt)
775 	       || gimple_code (stmt) == GIMPLE_COND))
776     {
777       for (i = 0; i < gimple_num_ops (stmt); ++i)
778 	{
779 	  tree op = gimple_op (stmt, i);
780 	  if (op == NULL_TREE)
781 	    ;
782 	  else if (TREE_CODE (op) == ADDR_EXPR)
783 	    ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
784 	  /* COND_EXPR and VCOND_EXPR rhs1 argument is a comparison
785 	     tree with two operands.  */
786 	  else if (i == 1 && COMPARISON_CLASS_P (op))
787 	    {
788 	      if (TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
789 		ret |= visit_addr (stmt, TREE_OPERAND (TREE_OPERAND (op, 0),
790 						       0), op, data);
791 	      if (TREE_CODE (TREE_OPERAND (op, 1)) == ADDR_EXPR)
792 		ret |= visit_addr (stmt, TREE_OPERAND (TREE_OPERAND (op, 1),
793 						       0), op, data);
794 	    }
795 	}
796     }
797   else if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
798     {
799       if (visit_store)
800 	{
801 	  tree arg = gimple_call_lhs (call_stmt);
802 	  if (arg)
803 	    {
804 	      tree lhs = get_base_loadstore (arg);
805 	      if (lhs)
806 		ret |= visit_store (stmt, lhs, arg, data);
807 	    }
808 	}
809       if (visit_load || visit_addr)
810 	for (i = 0; i < gimple_call_num_args (call_stmt); ++i)
811 	  {
812 	    tree arg = gimple_call_arg (call_stmt, i);
813 	    if (visit_addr
814 		&& TREE_CODE (arg) == ADDR_EXPR)
815 	      ret |= visit_addr (stmt, TREE_OPERAND (arg, 0), arg, data);
816 	    else if (visit_load)
817 	      {
818 		tree rhs = get_base_loadstore (arg);
819 		if (rhs)
820 		  ret |= visit_load (stmt, rhs, arg, data);
821 	      }
822 	  }
823       if (visit_addr
824 	  && gimple_call_chain (call_stmt)
825 	  && TREE_CODE (gimple_call_chain (call_stmt)) == ADDR_EXPR)
826 	ret |= visit_addr (stmt, TREE_OPERAND (gimple_call_chain (call_stmt), 0),
827 			   gimple_call_chain (call_stmt), data);
828       if (visit_addr
829 	  && gimple_call_return_slot_opt_p (call_stmt)
830 	  && gimple_call_lhs (call_stmt) != NULL_TREE
831 	  && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (call_stmt))))
832 	ret |= visit_addr (stmt, gimple_call_lhs (call_stmt),
833 			   gimple_call_lhs (call_stmt), data);
834     }
835   else if (gasm *asm_stmt = dyn_cast <gasm *> (stmt))
836     {
837       unsigned noutputs;
838       const char *constraint;
839       const char **oconstraints;
840       bool allows_mem, allows_reg, is_inout;
841       noutputs = gimple_asm_noutputs (asm_stmt);
842       oconstraints = XALLOCAVEC (const char *, noutputs);
843       if (visit_store || visit_addr)
844 	for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i)
845 	  {
846 	    tree link = gimple_asm_output_op (asm_stmt, i);
847 	    tree op = get_base_loadstore (TREE_VALUE (link));
848 	    if (op && visit_store)
849 	      ret |= visit_store (stmt, op, TREE_VALUE (link), data);
850 	    if (visit_addr)
851 	      {
852 		constraint = TREE_STRING_POINTER
853 		    (TREE_VALUE (TREE_PURPOSE (link)));
854 		oconstraints[i] = constraint;
855 		parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
856 					 &allows_reg, &is_inout);
857 		if (op && !allows_reg && allows_mem)
858 		  ret |= visit_addr (stmt, op, TREE_VALUE (link), data);
859 	      }
860 	  }
861       if (visit_load || visit_addr)
862 	for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
863 	  {
864 	    tree link = gimple_asm_input_op (asm_stmt, i);
865 	    tree op = TREE_VALUE (link);
866 	    if (visit_addr
867 		&& TREE_CODE (op) == ADDR_EXPR)
868 	      ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
869 	    else if (visit_load || visit_addr)
870 	      {
871 		op = get_base_loadstore (op);
872 		if (op)
873 		  {
874 		    if (visit_load)
875 		      ret |= visit_load (stmt, op, TREE_VALUE (link), data);
876 		    if (visit_addr)
877 		      {
878 			constraint = TREE_STRING_POINTER
879 			    (TREE_VALUE (TREE_PURPOSE (link)));
880 			parse_input_constraint (&constraint, 0, 0, noutputs,
881 						0, oconstraints,
882 						&allows_mem, &allows_reg);
883 			if (!allows_reg && allows_mem)
884 			  ret |= visit_addr (stmt, op, TREE_VALUE (link),
885 					     data);
886 		      }
887 		  }
888 	      }
889 	  }
890     }
891   else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
892     {
893       tree op = gimple_return_retval (return_stmt);
894       if (op)
895 	{
896 	  if (visit_addr
897 	      && TREE_CODE (op) == ADDR_EXPR)
898 	    ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
899 	  else if (visit_load)
900 	    {
901 	      tree base = get_base_loadstore (op);
902 	      if (base)
903 		ret |= visit_load (stmt, base, op, data);
904 	    }
905 	}
906     }
907   else if (visit_addr
908 	   && gimple_code (stmt) == GIMPLE_PHI)
909     {
910       for (i = 0; i < gimple_phi_num_args (stmt); ++i)
911 	{
912 	  tree op = gimple_phi_arg_def (stmt, i);
913 	  if (TREE_CODE (op) == ADDR_EXPR)
914 	    ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
915 	}
916     }
917   else if (visit_addr
918 	   && gimple_code (stmt) == GIMPLE_GOTO)
919     {
920       tree op = gimple_goto_dest (stmt);
921       if (TREE_CODE (op) == ADDR_EXPR)
922 	ret |= visit_addr (stmt, TREE_OPERAND (op, 0), op, data);
923     }
924 
925   return ret;
926 }
927 
928 /* Like walk_stmt_load_store_addr_ops but with NULL visit_addr.  IPA-CP
929    should make a faster clone for this case.  */
930 
931 bool
walk_stmt_load_store_ops(gimple * stmt,void * data,walk_stmt_load_store_addr_fn visit_load,walk_stmt_load_store_addr_fn visit_store)932 walk_stmt_load_store_ops (gimple *stmt, void *data,
933 			  walk_stmt_load_store_addr_fn visit_load,
934 			  walk_stmt_load_store_addr_fn visit_store)
935 {
936   return walk_stmt_load_store_addr_ops (stmt, data,
937 					visit_load, visit_store, NULL);
938 }
939