* xcoffout.h (xcoffout_source_line): Update prototype.
[official-gcc.git] / gcc / gimple.c
blobd3578da28dacc3ef7d871a2fe143385e29f5b315
1 /* Gimple IR support functions.
3 Copyright 2007, 2008, 2009 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 "ggc.h"
28 #include "hard-reg-set.h"
29 #include "basic-block.h"
30 #include "gimple.h"
31 #include "toplev.h"
32 #include "diagnostic.h"
33 #include "tree-flow.h"
34 #include "value-prof.h"
35 #include "flags.h"
37 #define DEFGSCODE(SYM, NAME, STRUCT) NAME,
38 const char *const gimple_code_name[] = {
39 #include "gimple.def"
41 #undef DEFGSCODE
43 /* All the tuples have their operand vector at the very bottom
44 of the structure. Therefore, the offset required to find the
45 operands vector the size of the structure minus the size of the 1
46 element tree array at the end (see gimple_ops). */
47 #define DEFGSCODE(SYM, NAME, STRUCT) (sizeof (STRUCT) - sizeof (tree)),
48 EXPORTED_CONST size_t gimple_ops_offset_[] = {
49 #include "gimple.def"
51 #undef DEFGSCODE
53 #ifdef GATHER_STATISTICS
54 /* Gimple stats. */
56 int gimple_alloc_counts[(int) gimple_alloc_kind_all];
57 int gimple_alloc_sizes[(int) gimple_alloc_kind_all];
59 /* Keep in sync with gimple.h:enum gimple_alloc_kind. */
60 static const char * const gimple_alloc_kind_names[] = {
61 "assignments",
62 "phi nodes",
63 "conditionals",
64 "sequences",
65 "everything else"
68 #endif /* GATHER_STATISTICS */
70 /* A cache of gimple_seq objects. Sequences are created and destroyed
71 fairly often during gimplification. */
72 static GTY ((deletable)) struct gimple_seq_d *gimple_seq_cache;
74 /* Private API manipulation functions shared only with some
75 other files. */
76 extern void gimple_set_stored_syms (gimple, bitmap, bitmap_obstack *);
77 extern void gimple_set_loaded_syms (gimple, bitmap, bitmap_obstack *);
79 /* Gimple tuple constructors.
80 Note: Any constructor taking a ``gimple_seq'' as a parameter, can
81 be passed a NULL to start with an empty sequence. */
83 /* Set the code for statement G to CODE. */
85 static inline void
86 gimple_set_code (gimple g, enum gimple_code code)
88 g->gsbase.code = code;
92 /* Return the GSS_* identifier for the given GIMPLE statement CODE. */
94 static enum gimple_statement_structure_enum
95 gss_for_code (enum gimple_code code)
97 switch (code)
99 case GIMPLE_ASSIGN:
100 case GIMPLE_CALL:
101 case GIMPLE_RETURN: return GSS_WITH_MEM_OPS;
102 case GIMPLE_COND:
103 case GIMPLE_GOTO:
104 case GIMPLE_LABEL:
105 case GIMPLE_SWITCH: return GSS_WITH_OPS;
106 case GIMPLE_ASM: return GSS_ASM;
107 case GIMPLE_BIND: return GSS_BIND;
108 case GIMPLE_CATCH: return GSS_CATCH;
109 case GIMPLE_EH_FILTER: return GSS_EH_FILTER;
110 case GIMPLE_NOP: return GSS_BASE;
111 case GIMPLE_PHI: return GSS_PHI;
112 case GIMPLE_RESX: return GSS_RESX;
113 case GIMPLE_TRY: return GSS_TRY;
114 case GIMPLE_WITH_CLEANUP_EXPR: return GSS_WCE;
115 case GIMPLE_OMP_CRITICAL: return GSS_OMP_CRITICAL;
116 case GIMPLE_OMP_FOR: return GSS_OMP_FOR;
117 case GIMPLE_OMP_MASTER:
118 case GIMPLE_OMP_ORDERED:
119 case GIMPLE_OMP_SECTION: return GSS_OMP;
120 case GIMPLE_OMP_RETURN:
121 case GIMPLE_OMP_SECTIONS_SWITCH: return GSS_BASE;
122 case GIMPLE_OMP_CONTINUE: return GSS_OMP_CONTINUE;
123 case GIMPLE_OMP_PARALLEL: return GSS_OMP_PARALLEL;
124 case GIMPLE_OMP_TASK: return GSS_OMP_TASK;
125 case GIMPLE_OMP_SECTIONS: return GSS_OMP_SECTIONS;
126 case GIMPLE_OMP_SINGLE: return GSS_OMP_SINGLE;
127 case GIMPLE_OMP_ATOMIC_LOAD: return GSS_OMP_ATOMIC_LOAD;
128 case GIMPLE_OMP_ATOMIC_STORE: return GSS_OMP_ATOMIC_STORE;
129 case GIMPLE_PREDICT: return GSS_BASE;
130 default: gcc_unreachable ();
135 /* Return the number of bytes needed to hold a GIMPLE statement with
136 code CODE. */
138 static size_t
139 gimple_size (enum gimple_code code)
141 enum gimple_statement_structure_enum gss = gss_for_code (code);
143 if (gss == GSS_WITH_OPS)
144 return sizeof (struct gimple_statement_with_ops);
145 else if (gss == GSS_WITH_MEM_OPS)
146 return sizeof (struct gimple_statement_with_memory_ops);
148 switch (code)
150 case GIMPLE_ASM:
151 return sizeof (struct gimple_statement_asm);
152 case GIMPLE_NOP:
153 return sizeof (struct gimple_statement_base);
154 case GIMPLE_BIND:
155 return sizeof (struct gimple_statement_bind);
156 case GIMPLE_CATCH:
157 return sizeof (struct gimple_statement_catch);
158 case GIMPLE_EH_FILTER:
159 return sizeof (struct gimple_statement_eh_filter);
160 case GIMPLE_TRY:
161 return sizeof (struct gimple_statement_try);
162 case GIMPLE_RESX:
163 return sizeof (struct gimple_statement_resx);
164 case GIMPLE_OMP_CRITICAL:
165 return sizeof (struct gimple_statement_omp_critical);
166 case GIMPLE_OMP_FOR:
167 return sizeof (struct gimple_statement_omp_for);
168 case GIMPLE_OMP_PARALLEL:
169 return sizeof (struct gimple_statement_omp_parallel);
170 case GIMPLE_OMP_TASK:
171 return sizeof (struct gimple_statement_omp_task);
172 case GIMPLE_OMP_SECTION:
173 case GIMPLE_OMP_MASTER:
174 case GIMPLE_OMP_ORDERED:
175 return sizeof (struct gimple_statement_omp);
176 case GIMPLE_OMP_RETURN:
177 return sizeof (struct gimple_statement_base);
178 case GIMPLE_OMP_CONTINUE:
179 return sizeof (struct gimple_statement_omp_continue);
180 case GIMPLE_OMP_SECTIONS:
181 return sizeof (struct gimple_statement_omp_sections);
182 case GIMPLE_OMP_SECTIONS_SWITCH:
183 return sizeof (struct gimple_statement_base);
184 case GIMPLE_OMP_SINGLE:
185 return sizeof (struct gimple_statement_omp_single);
186 case GIMPLE_OMP_ATOMIC_LOAD:
187 return sizeof (struct gimple_statement_omp_atomic_load);
188 case GIMPLE_OMP_ATOMIC_STORE:
189 return sizeof (struct gimple_statement_omp_atomic_store);
190 case GIMPLE_WITH_CLEANUP_EXPR:
191 return sizeof (struct gimple_statement_wce);
192 case GIMPLE_PREDICT:
193 return sizeof (struct gimple_statement_base);
194 default:
195 break;
198 gcc_unreachable ();
202 /* Allocate memory for a GIMPLE statement with code CODE and NUM_OPS
203 operands. */
205 #define gimple_alloc(c, n) gimple_alloc_stat (c, n MEM_STAT_INFO)
206 static gimple
207 gimple_alloc_stat (enum gimple_code code, unsigned num_ops MEM_STAT_DECL)
209 size_t size;
210 gimple stmt;
212 size = gimple_size (code);
213 if (num_ops > 0)
214 size += sizeof (tree) * (num_ops - 1);
216 #ifdef GATHER_STATISTICS
218 enum gimple_alloc_kind kind = gimple_alloc_kind (code);
219 gimple_alloc_counts[(int) kind]++;
220 gimple_alloc_sizes[(int) kind] += size;
222 #endif
224 stmt = (gimple) ggc_alloc_cleared_stat (size PASS_MEM_STAT);
225 gimple_set_code (stmt, code);
226 gimple_set_num_ops (stmt, num_ops);
228 /* Do not call gimple_set_modified here as it has other side
229 effects and this tuple is still not completely built. */
230 stmt->gsbase.modified = 1;
232 return stmt;
235 /* Set SUBCODE to be the code of the expression computed by statement G. */
237 static inline void
238 gimple_set_subcode (gimple g, unsigned subcode)
240 /* We only have 16 bits for the RHS code. Assert that we are not
241 overflowing it. */
242 gcc_assert (subcode < (1 << 16));
243 g->gsbase.subcode = subcode;
248 /* Build a tuple with operands. CODE is the statement to build (which
249 must be one of the GIMPLE_WITH_OPS tuples). SUBCODE is the sub-code
250 for the new tuple. NUM_OPS is the number of operands to allocate. */
252 #define gimple_build_with_ops(c, s, n) \
253 gimple_build_with_ops_stat (c, s, n MEM_STAT_INFO)
255 static gimple
256 gimple_build_with_ops_stat (enum gimple_code code, enum tree_code subcode,
257 unsigned num_ops MEM_STAT_DECL)
259 gimple s = gimple_alloc_stat (code, num_ops PASS_MEM_STAT);
260 gimple_set_subcode (s, subcode);
262 return s;
266 /* Build a GIMPLE_RETURN statement returning RETVAL. */
268 gimple
269 gimple_build_return (tree retval)
271 gimple s = gimple_build_with_ops (GIMPLE_RETURN, ERROR_MARK, 1);
272 if (retval)
273 gimple_return_set_retval (s, retval);
274 return s;
277 /* Helper for gimple_build_call, gimple_build_call_vec and
278 gimple_build_call_from_tree. Build the basic components of a
279 GIMPLE_CALL statement to function FN with NARGS arguments. */
281 static inline gimple
282 gimple_build_call_1 (tree fn, unsigned nargs)
284 gimple s = gimple_build_with_ops (GIMPLE_CALL, ERROR_MARK, nargs + 3);
285 if (TREE_CODE (fn) == FUNCTION_DECL)
286 fn = build_fold_addr_expr (fn);
287 gimple_set_op (s, 1, fn);
288 return s;
292 /* Build a GIMPLE_CALL statement to function FN with the arguments
293 specified in vector ARGS. */
295 gimple
296 gimple_build_call_vec (tree fn, VEC(tree, heap) *args)
298 unsigned i;
299 unsigned nargs = VEC_length (tree, args);
300 gimple call = gimple_build_call_1 (fn, nargs);
302 for (i = 0; i < nargs; i++)
303 gimple_call_set_arg (call, i, VEC_index (tree, args, i));
305 return call;
309 /* Build a GIMPLE_CALL statement to function FN. NARGS is the number of
310 arguments. The ... are the arguments. */
312 gimple
313 gimple_build_call (tree fn, unsigned nargs, ...)
315 va_list ap;
316 gimple call;
317 unsigned i;
319 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL || is_gimple_call_addr (fn));
321 call = gimple_build_call_1 (fn, nargs);
323 va_start (ap, nargs);
324 for (i = 0; i < nargs; i++)
325 gimple_call_set_arg (call, i, va_arg (ap, tree));
326 va_end (ap);
328 return call;
332 /* Build a GIMPLE_CALL statement from CALL_EXPR T. Note that T is
333 assumed to be in GIMPLE form already. Minimal checking is done of
334 this fact. */
336 gimple
337 gimple_build_call_from_tree (tree t)
339 unsigned i, nargs;
340 gimple call;
341 tree fndecl = get_callee_fndecl (t);
343 gcc_assert (TREE_CODE (t) == CALL_EXPR);
345 nargs = call_expr_nargs (t);
346 call = gimple_build_call_1 (fndecl ? fndecl : CALL_EXPR_FN (t), nargs);
348 for (i = 0; i < nargs; i++)
349 gimple_call_set_arg (call, i, CALL_EXPR_ARG (t, i));
351 gimple_set_block (call, TREE_BLOCK (t));
353 /* Carry all the CALL_EXPR flags to the new GIMPLE_CALL. */
354 gimple_call_set_chain (call, CALL_EXPR_STATIC_CHAIN (t));
355 gimple_call_set_tail (call, CALL_EXPR_TAILCALL (t));
356 gimple_call_set_cannot_inline (call, CALL_CANNOT_INLINE_P (t));
357 gimple_call_set_return_slot_opt (call, CALL_EXPR_RETURN_SLOT_OPT (t));
358 gimple_call_set_from_thunk (call, CALL_FROM_THUNK_P (t));
359 gimple_call_set_va_arg_pack (call, CALL_EXPR_VA_ARG_PACK (t));
361 return call;
365 /* Extract the operands and code for expression EXPR into *SUBCODE_P,
366 *OP1_P and *OP2_P respectively. */
368 void
369 extract_ops_from_tree (tree expr, enum tree_code *subcode_p, tree *op1_p,
370 tree *op2_p)
372 enum gimple_rhs_class grhs_class;
374 *subcode_p = TREE_CODE (expr);
375 grhs_class = get_gimple_rhs_class (*subcode_p);
377 if (grhs_class == GIMPLE_BINARY_RHS)
379 *op1_p = TREE_OPERAND (expr, 0);
380 *op2_p = TREE_OPERAND (expr, 1);
382 else if (grhs_class == GIMPLE_UNARY_RHS)
384 *op1_p = TREE_OPERAND (expr, 0);
385 *op2_p = NULL_TREE;
387 else if (grhs_class == GIMPLE_SINGLE_RHS)
389 *op1_p = expr;
390 *op2_p = NULL_TREE;
392 else
393 gcc_unreachable ();
397 /* Build a GIMPLE_ASSIGN statement.
399 LHS of the assignment.
400 RHS of the assignment which can be unary or binary. */
402 gimple
403 gimple_build_assign_stat (tree lhs, tree rhs MEM_STAT_DECL)
405 enum tree_code subcode;
406 tree op1, op2;
408 extract_ops_from_tree (rhs, &subcode, &op1, &op2);
409 return gimple_build_assign_with_ops_stat (subcode, lhs, op1, op2
410 PASS_MEM_STAT);
414 /* Build a GIMPLE_ASSIGN statement with sub-code SUBCODE and operands
415 OP1 and OP2. If OP2 is NULL then SUBCODE must be of class
416 GIMPLE_UNARY_RHS or GIMPLE_SINGLE_RHS. */
418 gimple
419 gimple_build_assign_with_ops_stat (enum tree_code subcode, tree lhs, tree op1,
420 tree op2 MEM_STAT_DECL)
422 unsigned num_ops;
423 gimple p;
425 /* Need 1 operand for LHS and 1 or 2 for the RHS (depending on the
426 code). */
427 num_ops = get_gimple_rhs_num_ops (subcode) + 1;
429 p = gimple_build_with_ops_stat (GIMPLE_ASSIGN, subcode, num_ops
430 PASS_MEM_STAT);
431 gimple_assign_set_lhs (p, lhs);
432 gimple_assign_set_rhs1 (p, op1);
433 if (op2)
435 gcc_assert (num_ops > 2);
436 gimple_assign_set_rhs2 (p, op2);
439 return p;
443 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
445 DST/SRC are the destination and source respectively. You can pass
446 ungimplified trees in DST or SRC, in which case they will be
447 converted to a gimple operand if necessary.
449 This function returns the newly created GIMPLE_ASSIGN tuple. */
451 inline gimple
452 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
454 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
455 gimplify_and_add (t, seq_p);
456 ggc_free (t);
457 return gimple_seq_last_stmt (*seq_p);
461 /* Build a GIMPLE_COND statement.
463 PRED is the condition used to compare LHS and the RHS.
464 T_LABEL is the label to jump to if the condition is true.
465 F_LABEL is the label to jump to otherwise. */
467 gimple
468 gimple_build_cond (enum tree_code pred_code, tree lhs, tree rhs,
469 tree t_label, tree f_label)
471 gimple p;
473 gcc_assert (TREE_CODE_CLASS (pred_code) == tcc_comparison);
474 p = gimple_build_with_ops (GIMPLE_COND, pred_code, 4);
475 gimple_cond_set_lhs (p, lhs);
476 gimple_cond_set_rhs (p, rhs);
477 gimple_cond_set_true_label (p, t_label);
478 gimple_cond_set_false_label (p, f_label);
479 return p;
483 /* Extract operands for a GIMPLE_COND statement out of COND_EXPR tree COND. */
485 void
486 gimple_cond_get_ops_from_tree (tree cond, enum tree_code *code_p,
487 tree *lhs_p, tree *rhs_p)
489 gcc_assert (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison
490 || TREE_CODE (cond) == TRUTH_NOT_EXPR
491 || is_gimple_min_invariant (cond)
492 || SSA_VAR_P (cond));
494 extract_ops_from_tree (cond, code_p, lhs_p, rhs_p);
496 /* Canonicalize conditionals of the form 'if (!VAL)'. */
497 if (*code_p == TRUTH_NOT_EXPR)
499 *code_p = EQ_EXPR;
500 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
501 *rhs_p = fold_convert (TREE_TYPE (*lhs_p), integer_zero_node);
503 /* Canonicalize conditionals of the form 'if (VAL)' */
504 else if (TREE_CODE_CLASS (*code_p) != tcc_comparison)
506 *code_p = NE_EXPR;
507 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
508 *rhs_p = fold_convert (TREE_TYPE (*lhs_p), integer_zero_node);
513 /* Build a GIMPLE_COND statement from the conditional expression tree
514 COND. T_LABEL and F_LABEL are as in gimple_build_cond. */
516 gimple
517 gimple_build_cond_from_tree (tree cond, tree t_label, tree f_label)
519 enum tree_code code;
520 tree lhs, rhs;
522 gimple_cond_get_ops_from_tree (cond, &code, &lhs, &rhs);
523 return gimple_build_cond (code, lhs, rhs, t_label, f_label);
526 /* Set code, lhs, and rhs of a GIMPLE_COND from a suitable
527 boolean expression tree COND. */
529 void
530 gimple_cond_set_condition_from_tree (gimple stmt, tree cond)
532 enum tree_code code;
533 tree lhs, rhs;
535 gimple_cond_get_ops_from_tree (cond, &code, &lhs, &rhs);
536 gimple_cond_set_condition (stmt, code, lhs, rhs);
539 /* Build a GIMPLE_LABEL statement for LABEL. */
541 gimple
542 gimple_build_label (tree label)
544 gimple p = gimple_build_with_ops (GIMPLE_LABEL, ERROR_MARK, 1);
545 gimple_label_set_label (p, label);
546 return p;
549 /* Build a GIMPLE_GOTO statement to label DEST. */
551 gimple
552 gimple_build_goto (tree dest)
554 gimple p = gimple_build_with_ops (GIMPLE_GOTO, ERROR_MARK, 1);
555 gimple_goto_set_dest (p, dest);
556 return p;
560 /* Build a GIMPLE_NOP statement. */
562 gimple
563 gimple_build_nop (void)
565 return gimple_alloc (GIMPLE_NOP, 0);
569 /* Build a GIMPLE_BIND statement.
570 VARS are the variables in BODY.
571 BLOCK is the containing block. */
573 gimple
574 gimple_build_bind (tree vars, gimple_seq body, tree block)
576 gimple p = gimple_alloc (GIMPLE_BIND, 0);
577 gimple_bind_set_vars (p, vars);
578 if (body)
579 gimple_bind_set_body (p, body);
580 if (block)
581 gimple_bind_set_block (p, block);
582 return p;
585 /* Helper function to set the simple fields of a asm stmt.
587 STRING is a pointer to a string that is the asm blocks assembly code.
588 NINPUT is the number of register inputs.
589 NOUTPUT is the number of register outputs.
590 NCLOBBERS is the number of clobbered registers.
593 static inline gimple
594 gimple_build_asm_1 (const char *string, unsigned ninputs, unsigned noutputs,
595 unsigned nclobbers)
597 gimple p;
598 int size = strlen (string);
600 p = gimple_build_with_ops (GIMPLE_ASM, ERROR_MARK,
601 ninputs + noutputs + nclobbers);
603 p->gimple_asm.ni = ninputs;
604 p->gimple_asm.no = noutputs;
605 p->gimple_asm.nc = nclobbers;
606 p->gimple_asm.string = ggc_alloc_string (string, size);
608 #ifdef GATHER_STATISTICS
609 gimple_alloc_sizes[(int) gimple_alloc_kind (GIMPLE_ASM)] += size;
610 #endif
612 return p;
615 /* Build a GIMPLE_ASM statement.
617 STRING is the assembly code.
618 NINPUT is the number of register inputs.
619 NOUTPUT is the number of register outputs.
620 NCLOBBERS is the number of clobbered registers.
621 INPUTS is a vector of the input register parameters.
622 OUTPUTS is a vector of the output register parameters.
623 CLOBBERS is a vector of the clobbered register parameters. */
625 gimple
626 gimple_build_asm_vec (const char *string, VEC(tree,gc)* inputs,
627 VEC(tree,gc)* outputs, VEC(tree,gc)* clobbers)
629 gimple p;
630 unsigned i;
632 p = gimple_build_asm_1 (string,
633 VEC_length (tree, inputs),
634 VEC_length (tree, outputs),
635 VEC_length (tree, clobbers));
637 for (i = 0; i < VEC_length (tree, inputs); i++)
638 gimple_asm_set_input_op (p, i, VEC_index (tree, inputs, i));
640 for (i = 0; i < VEC_length (tree, outputs); i++)
641 gimple_asm_set_output_op (p, i, VEC_index (tree, outputs, i));
643 for (i = 0; i < VEC_length (tree, clobbers); i++)
644 gimple_asm_set_clobber_op (p, i, VEC_index (tree, clobbers, i));
646 return p;
649 /* Build a GIMPLE_ASM statement.
651 STRING is the assembly code.
652 NINPUT is the number of register inputs.
653 NOUTPUT is the number of register outputs.
654 NCLOBBERS is the number of clobbered registers.
655 ... are trees for each input, output and clobbered register. */
657 gimple
658 gimple_build_asm (const char *string, unsigned ninputs, unsigned noutputs,
659 unsigned nclobbers, ...)
661 gimple p;
662 unsigned i;
663 va_list ap;
665 p = gimple_build_asm_1 (string, ninputs, noutputs, nclobbers);
667 va_start (ap, nclobbers);
669 for (i = 0; i < ninputs; i++)
670 gimple_asm_set_input_op (p, i, va_arg (ap, tree));
672 for (i = 0; i < noutputs; i++)
673 gimple_asm_set_output_op (p, i, va_arg (ap, tree));
675 for (i = 0; i < nclobbers; i++)
676 gimple_asm_set_clobber_op (p, i, va_arg (ap, tree));
678 va_end (ap);
680 return p;
683 /* Build a GIMPLE_CATCH statement.
685 TYPES are the catch types.
686 HANDLER is the exception handler. */
688 gimple
689 gimple_build_catch (tree types, gimple_seq handler)
691 gimple p = gimple_alloc (GIMPLE_CATCH, 0);
692 gimple_catch_set_types (p, types);
693 if (handler)
694 gimple_catch_set_handler (p, handler);
696 return p;
699 /* Build a GIMPLE_EH_FILTER statement.
701 TYPES are the filter's types.
702 FAILURE is the filter's failure action. */
704 gimple
705 gimple_build_eh_filter (tree types, gimple_seq failure)
707 gimple p = gimple_alloc (GIMPLE_EH_FILTER, 0);
708 gimple_eh_filter_set_types (p, types);
709 if (failure)
710 gimple_eh_filter_set_failure (p, failure);
712 return p;
715 /* Build a GIMPLE_TRY statement.
717 EVAL is the expression to evaluate.
718 CLEANUP is the cleanup expression.
719 KIND is either GIMPLE_TRY_CATCH or GIMPLE_TRY_FINALLY depending on
720 whether this is a try/catch or a try/finally respectively. */
722 gimple
723 gimple_build_try (gimple_seq eval, gimple_seq cleanup,
724 enum gimple_try_flags kind)
726 gimple p;
728 gcc_assert (kind == GIMPLE_TRY_CATCH || kind == GIMPLE_TRY_FINALLY);
729 p = gimple_alloc (GIMPLE_TRY, 0);
730 gimple_set_subcode (p, kind);
731 if (eval)
732 gimple_try_set_eval (p, eval);
733 if (cleanup)
734 gimple_try_set_cleanup (p, cleanup);
736 return p;
739 /* Construct a GIMPLE_WITH_CLEANUP_EXPR statement.
741 CLEANUP is the cleanup expression. */
743 gimple
744 gimple_build_wce (gimple_seq cleanup)
746 gimple p = gimple_alloc (GIMPLE_WITH_CLEANUP_EXPR, 0);
747 if (cleanup)
748 gimple_wce_set_cleanup (p, cleanup);
750 return p;
754 /* Build a GIMPLE_RESX statement.
756 REGION is the region number from which this resx causes control flow to
757 leave. */
759 gimple
760 gimple_build_resx (int region)
762 gimple p = gimple_alloc (GIMPLE_RESX, 0);
763 gimple_resx_set_region (p, region);
764 return p;
768 /* The helper for constructing a gimple switch statement.
769 INDEX is the switch's index.
770 NLABELS is the number of labels in the switch excluding the default.
771 DEFAULT_LABEL is the default label for the switch statement. */
773 static inline gimple
774 gimple_build_switch_1 (unsigned nlabels, tree index, tree default_label)
776 /* nlabels + 1 default label + 1 index. */
777 gimple p = gimple_build_with_ops (GIMPLE_SWITCH, ERROR_MARK,
778 nlabels + 1 + 1);
779 gimple_switch_set_index (p, index);
780 gimple_switch_set_default_label (p, default_label);
781 return p;
785 /* Build a GIMPLE_SWITCH statement.
787 INDEX is the switch's index.
788 NLABELS is the number of labels in the switch excluding the DEFAULT_LABEL.
789 ... are the labels excluding the default. */
791 gimple
792 gimple_build_switch (unsigned nlabels, tree index, tree default_label, ...)
794 va_list al;
795 unsigned i;
796 gimple p;
798 p = gimple_build_switch_1 (nlabels, index, default_label);
800 /* Store the rest of the labels. */
801 va_start (al, default_label);
802 for (i = 1; i <= nlabels; i++)
803 gimple_switch_set_label (p, i, va_arg (al, tree));
804 va_end (al);
806 return p;
810 /* Build a GIMPLE_SWITCH statement.
812 INDEX is the switch's index.
813 DEFAULT_LABEL is the default label
814 ARGS is a vector of labels excluding the default. */
816 gimple
817 gimple_build_switch_vec (tree index, tree default_label, VEC(tree, heap) *args)
819 unsigned i;
820 unsigned nlabels = VEC_length (tree, args);
821 gimple p = gimple_build_switch_1 (nlabels, index, default_label);
823 /* Put labels in labels[1 - (nlabels + 1)].
824 Default label is in labels[0]. */
825 for (i = 1; i <= nlabels; i++)
826 gimple_switch_set_label (p, i, VEC_index (tree, args, i - 1));
828 return p;
832 /* Build a GIMPLE_OMP_CRITICAL statement.
834 BODY is the sequence of statements for which only one thread can execute.
835 NAME is optional identifier for this critical block. */
837 gimple
838 gimple_build_omp_critical (gimple_seq body, tree name)
840 gimple p = gimple_alloc (GIMPLE_OMP_CRITICAL, 0);
841 gimple_omp_critical_set_name (p, name);
842 if (body)
843 gimple_omp_set_body (p, body);
845 return p;
848 /* Build a GIMPLE_OMP_FOR statement.
850 BODY is sequence of statements inside the for loop.
851 CLAUSES, are any of the OMP loop construct's clauses: private, firstprivate,
852 lastprivate, reductions, ordered, schedule, and nowait.
853 COLLAPSE is the collapse count.
854 PRE_BODY is the sequence of statements that are loop invariant. */
856 gimple
857 gimple_build_omp_for (gimple_seq body, tree clauses, size_t collapse,
858 gimple_seq pre_body)
860 gimple p = gimple_alloc (GIMPLE_OMP_FOR, 0);
861 if (body)
862 gimple_omp_set_body (p, body);
863 gimple_omp_for_set_clauses (p, clauses);
864 p->gimple_omp_for.collapse = collapse;
865 p->gimple_omp_for.iter = GGC_CNEWVEC (struct gimple_omp_for_iter, collapse);
866 if (pre_body)
867 gimple_omp_for_set_pre_body (p, pre_body);
869 return p;
873 /* Build a GIMPLE_OMP_PARALLEL statement.
875 BODY is sequence of statements which are executed in parallel.
876 CLAUSES, are the OMP parallel construct's clauses.
877 CHILD_FN is the function created for the parallel threads to execute.
878 DATA_ARG are the shared data argument(s). */
880 gimple
881 gimple_build_omp_parallel (gimple_seq body, tree clauses, tree child_fn,
882 tree data_arg)
884 gimple p = gimple_alloc (GIMPLE_OMP_PARALLEL, 0);
885 if (body)
886 gimple_omp_set_body (p, body);
887 gimple_omp_parallel_set_clauses (p, clauses);
888 gimple_omp_parallel_set_child_fn (p, child_fn);
889 gimple_omp_parallel_set_data_arg (p, data_arg);
891 return p;
895 /* Build a GIMPLE_OMP_TASK statement.
897 BODY is sequence of statements which are executed by the explicit task.
898 CLAUSES, are the OMP parallel construct's clauses.
899 CHILD_FN is the function created for the parallel threads to execute.
900 DATA_ARG are the shared data argument(s).
901 COPY_FN is the optional function for firstprivate initialization.
902 ARG_SIZE and ARG_ALIGN are size and alignment of the data block. */
904 gimple
905 gimple_build_omp_task (gimple_seq body, tree clauses, tree child_fn,
906 tree data_arg, tree copy_fn, tree arg_size,
907 tree arg_align)
909 gimple p = gimple_alloc (GIMPLE_OMP_TASK, 0);
910 if (body)
911 gimple_omp_set_body (p, body);
912 gimple_omp_task_set_clauses (p, clauses);
913 gimple_omp_task_set_child_fn (p, child_fn);
914 gimple_omp_task_set_data_arg (p, data_arg);
915 gimple_omp_task_set_copy_fn (p, copy_fn);
916 gimple_omp_task_set_arg_size (p, arg_size);
917 gimple_omp_task_set_arg_align (p, arg_align);
919 return p;
923 /* Build a GIMPLE_OMP_SECTION statement for a sections statement.
925 BODY is the sequence of statements in the section. */
927 gimple
928 gimple_build_omp_section (gimple_seq body)
930 gimple p = gimple_alloc (GIMPLE_OMP_SECTION, 0);
931 if (body)
932 gimple_omp_set_body (p, body);
934 return p;
938 /* Build a GIMPLE_OMP_MASTER statement.
940 BODY is the sequence of statements to be executed by just the master. */
942 gimple
943 gimple_build_omp_master (gimple_seq body)
945 gimple p = gimple_alloc (GIMPLE_OMP_MASTER, 0);
946 if (body)
947 gimple_omp_set_body (p, body);
949 return p;
953 /* Build a GIMPLE_OMP_CONTINUE statement.
955 CONTROL_DEF is the definition of the control variable.
956 CONTROL_USE is the use of the control variable. */
958 gimple
959 gimple_build_omp_continue (tree control_def, tree control_use)
961 gimple p = gimple_alloc (GIMPLE_OMP_CONTINUE, 0);
962 gimple_omp_continue_set_control_def (p, control_def);
963 gimple_omp_continue_set_control_use (p, control_use);
964 return p;
967 /* Build a GIMPLE_OMP_ORDERED statement.
969 BODY is the sequence of statements inside a loop that will executed in
970 sequence. */
972 gimple
973 gimple_build_omp_ordered (gimple_seq body)
975 gimple p = gimple_alloc (GIMPLE_OMP_ORDERED, 0);
976 if (body)
977 gimple_omp_set_body (p, body);
979 return p;
983 /* Build a GIMPLE_OMP_RETURN statement.
984 WAIT_P is true if this is a non-waiting return. */
986 gimple
987 gimple_build_omp_return (bool wait_p)
989 gimple p = gimple_alloc (GIMPLE_OMP_RETURN, 0);
990 if (wait_p)
991 gimple_omp_return_set_nowait (p);
993 return p;
997 /* Build a GIMPLE_OMP_SECTIONS statement.
999 BODY is a sequence of section statements.
1000 CLAUSES are any of the OMP sections contsruct's clauses: private,
1001 firstprivate, lastprivate, reduction, and nowait. */
1003 gimple
1004 gimple_build_omp_sections (gimple_seq body, tree clauses)
1006 gimple p = gimple_alloc (GIMPLE_OMP_SECTIONS, 0);
1007 if (body)
1008 gimple_omp_set_body (p, body);
1009 gimple_omp_sections_set_clauses (p, clauses);
1011 return p;
1015 /* Build a GIMPLE_OMP_SECTIONS_SWITCH. */
1017 gimple
1018 gimple_build_omp_sections_switch (void)
1020 return gimple_alloc (GIMPLE_OMP_SECTIONS_SWITCH, 0);
1024 /* Build a GIMPLE_OMP_SINGLE statement.
1026 BODY is the sequence of statements that will be executed once.
1027 CLAUSES are any of the OMP single construct's clauses: private, firstprivate,
1028 copyprivate, nowait. */
1030 gimple
1031 gimple_build_omp_single (gimple_seq body, tree clauses)
1033 gimple p = gimple_alloc (GIMPLE_OMP_SINGLE, 0);
1034 if (body)
1035 gimple_omp_set_body (p, body);
1036 gimple_omp_single_set_clauses (p, clauses);
1038 return p;
1042 /* Build a GIMPLE_OMP_ATOMIC_LOAD statement. */
1044 gimple
1045 gimple_build_omp_atomic_load (tree lhs, tree rhs)
1047 gimple p = gimple_alloc (GIMPLE_OMP_ATOMIC_LOAD, 0);
1048 gimple_omp_atomic_load_set_lhs (p, lhs);
1049 gimple_omp_atomic_load_set_rhs (p, rhs);
1050 return p;
1053 /* Build a GIMPLE_OMP_ATOMIC_STORE statement.
1055 VAL is the value we are storing. */
1057 gimple
1058 gimple_build_omp_atomic_store (tree val)
1060 gimple p = gimple_alloc (GIMPLE_OMP_ATOMIC_STORE, 0);
1061 gimple_omp_atomic_store_set_val (p, val);
1062 return p;
1065 /* Build a GIMPLE_PREDICT statement. PREDICT is one of the predictors from
1066 predict.def, OUTCOME is NOT_TAKEN or TAKEN. */
1068 gimple
1069 gimple_build_predict (enum br_predictor predictor, enum prediction outcome)
1071 gimple p = gimple_alloc (GIMPLE_PREDICT, 0);
1072 /* Ensure all the predictors fit into the lower bits of the subcode. */
1073 gcc_assert ((int) END_PREDICTORS <= GF_PREDICT_TAKEN);
1074 gimple_predict_set_predictor (p, predictor);
1075 gimple_predict_set_outcome (p, outcome);
1076 return p;
1079 /* Return which gimple structure is used by T. The enums here are defined
1080 in gsstruct.def. */
1082 enum gimple_statement_structure_enum
1083 gimple_statement_structure (gimple gs)
1085 return gss_for_code (gimple_code (gs));
1088 #if defined ENABLE_GIMPLE_CHECKING
1089 /* Complain of a gimple type mismatch and die. */
1091 void
1092 gimple_check_failed (const_gimple gs, const char *file, int line,
1093 const char *function, enum gimple_code code,
1094 enum tree_code subcode)
1096 internal_error ("gimple check: expected %s(%s), have %s(%s) in %s, at %s:%d",
1097 gimple_code_name[code],
1098 tree_code_name[subcode],
1099 gimple_code_name[gimple_code (gs)],
1100 gs->gsbase.subcode > 0
1101 ? tree_code_name[gs->gsbase.subcode]
1102 : "",
1103 function, trim_filename (file), line);
1105 #endif /* ENABLE_GIMPLE_CHECKING */
1108 /* Allocate a new GIMPLE sequence in GC memory and return it. If
1109 there are free sequences in GIMPLE_SEQ_CACHE return one of those
1110 instead. */
1112 gimple_seq
1113 gimple_seq_alloc (void)
1115 gimple_seq seq = gimple_seq_cache;
1116 if (seq)
1118 gimple_seq_cache = gimple_seq_cache->next_free;
1119 gcc_assert (gimple_seq_cache != seq);
1120 memset (seq, 0, sizeof (*seq));
1122 else
1124 seq = (gimple_seq) ggc_alloc_cleared (sizeof (*seq));
1125 #ifdef GATHER_STATISTICS
1126 gimple_alloc_counts[(int) gimple_alloc_kind_seq]++;
1127 gimple_alloc_sizes[(int) gimple_alloc_kind_seq] += sizeof (*seq);
1128 #endif
1131 return seq;
1134 /* Return SEQ to the free pool of GIMPLE sequences. */
1136 void
1137 gimple_seq_free (gimple_seq seq)
1139 if (seq == NULL)
1140 return;
1142 gcc_assert (gimple_seq_first (seq) == NULL);
1143 gcc_assert (gimple_seq_last (seq) == NULL);
1145 /* If this triggers, it's a sign that the same list is being freed
1146 twice. */
1147 gcc_assert (seq != gimple_seq_cache || gimple_seq_cache == NULL);
1149 /* Add SEQ to the pool of free sequences. */
1150 seq->next_free = gimple_seq_cache;
1151 gimple_seq_cache = seq;
1155 /* Link gimple statement GS to the end of the sequence *SEQ_P. If
1156 *SEQ_P is NULL, a new sequence is allocated. */
1158 void
1159 gimple_seq_add_stmt (gimple_seq *seq_p, gimple gs)
1161 gimple_stmt_iterator si;
1163 if (gs == NULL)
1164 return;
1166 if (*seq_p == NULL)
1167 *seq_p = gimple_seq_alloc ();
1169 si = gsi_last (*seq_p);
1170 gsi_insert_after (&si, gs, GSI_NEW_STMT);
1174 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
1175 NULL, a new sequence is allocated. */
1177 void
1178 gimple_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
1180 gimple_stmt_iterator si;
1182 if (src == NULL)
1183 return;
1185 if (*dst_p == NULL)
1186 *dst_p = gimple_seq_alloc ();
1188 si = gsi_last (*dst_p);
1189 gsi_insert_seq_after (&si, src, GSI_NEW_STMT);
1193 /* Helper function of empty_body_p. Return true if STMT is an empty
1194 statement. */
1196 static bool
1197 empty_stmt_p (gimple stmt)
1199 if (gimple_code (stmt) == GIMPLE_NOP)
1200 return true;
1201 if (gimple_code (stmt) == GIMPLE_BIND)
1202 return empty_body_p (gimple_bind_body (stmt));
1203 return false;
1207 /* Return true if BODY contains nothing but empty statements. */
1209 bool
1210 empty_body_p (gimple_seq body)
1212 gimple_stmt_iterator i;
1215 if (gimple_seq_empty_p (body))
1216 return true;
1217 for (i = gsi_start (body); !gsi_end_p (i); gsi_next (&i))
1218 if (!empty_stmt_p (gsi_stmt (i)))
1219 return false;
1221 return true;
1225 /* Perform a deep copy of sequence SRC and return the result. */
1227 gimple_seq
1228 gimple_seq_copy (gimple_seq src)
1230 gimple_stmt_iterator gsi;
1231 gimple_seq new_seq = gimple_seq_alloc ();
1232 gimple stmt;
1234 for (gsi = gsi_start (src); !gsi_end_p (gsi); gsi_next (&gsi))
1236 stmt = gimple_copy (gsi_stmt (gsi));
1237 gimple_seq_add_stmt (&new_seq, stmt);
1240 return new_seq;
1244 /* Walk all the statements in the sequence SEQ calling walk_gimple_stmt
1245 on each one. WI is as in walk_gimple_stmt.
1247 If walk_gimple_stmt returns non-NULL, the walk is stopped, the
1248 value is stored in WI->CALLBACK_RESULT and the statement that
1249 produced the value is returned.
1251 Otherwise, all the statements are walked and NULL returned. */
1253 gimple
1254 walk_gimple_seq (gimple_seq seq, walk_stmt_fn callback_stmt,
1255 walk_tree_fn callback_op, struct walk_stmt_info *wi)
1257 gimple_stmt_iterator gsi;
1259 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
1261 tree ret = walk_gimple_stmt (&gsi, callback_stmt, callback_op, wi);
1262 if (ret)
1264 /* If CALLBACK_STMT or CALLBACK_OP return a value, WI must exist
1265 to hold it. */
1266 gcc_assert (wi);
1267 wi->callback_result = ret;
1268 return gsi_stmt (gsi);
1272 if (wi)
1273 wi->callback_result = NULL_TREE;
1275 return NULL;
1279 /* Helper function for walk_gimple_stmt. Walk operands of a GIMPLE_ASM. */
1281 static tree
1282 walk_gimple_asm (gimple stmt, walk_tree_fn callback_op,
1283 struct walk_stmt_info *wi)
1285 tree ret;
1286 unsigned noutputs;
1287 const char **oconstraints;
1288 unsigned i;
1289 const char *constraint;
1290 bool allows_mem, allows_reg, is_inout;
1292 noutputs = gimple_asm_noutputs (stmt);
1293 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
1295 if (wi)
1296 wi->is_lhs = true;
1298 for (i = 0; i < noutputs; i++)
1300 tree op = gimple_asm_output_op (stmt, i);
1301 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
1302 oconstraints[i] = constraint;
1303 parse_output_constraint (&constraint, i, 0, 0, &allows_mem, &allows_reg,
1304 &is_inout);
1305 if (wi)
1306 wi->val_only = (allows_reg || !allows_mem);
1307 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1308 if (ret)
1309 return ret;
1312 for (i = 0; i < gimple_asm_ninputs (stmt); i++)
1314 tree op = gimple_asm_input_op (stmt, i);
1315 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
1316 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1317 oconstraints, &allows_mem, &allows_reg);
1318 if (wi)
1319 wi->val_only = (allows_reg || !allows_mem);
1321 /* Although input "m" is not really a LHS, we need a lvalue. */
1322 if (wi)
1323 wi->is_lhs = !wi->val_only;
1324 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1325 if (ret)
1326 return ret;
1329 if (wi)
1331 wi->is_lhs = false;
1332 wi->val_only = true;
1335 return NULL_TREE;
1339 /* Helper function of WALK_GIMPLE_STMT. Walk every tree operand in
1340 STMT. CALLBACK_OP and WI are as in WALK_GIMPLE_STMT.
1342 CALLBACK_OP is called on each operand of STMT via walk_tree.
1343 Additional parameters to walk_tree must be stored in WI. For each operand
1344 OP, walk_tree is called as:
1346 walk_tree (&OP, CALLBACK_OP, WI, WI->PSET)
1348 If CALLBACK_OP returns non-NULL for an operand, the remaining
1349 operands are not scanned.
1351 The return value is that returned by the last call to walk_tree, or
1352 NULL_TREE if no CALLBACK_OP is specified. */
1354 inline tree
1355 walk_gimple_op (gimple stmt, walk_tree_fn callback_op,
1356 struct walk_stmt_info *wi)
1358 struct pointer_set_t *pset = (wi) ? wi->pset : NULL;
1359 unsigned i;
1360 tree ret = NULL_TREE;
1362 switch (gimple_code (stmt))
1364 case GIMPLE_ASSIGN:
1365 /* Walk the RHS operands. A formal temporary LHS may use a
1366 COMPONENT_REF RHS. */
1367 if (wi)
1368 wi->val_only = !is_gimple_reg (gimple_assign_lhs (stmt))
1369 || !gimple_assign_single_p (stmt);
1371 for (i = 1; i < gimple_num_ops (stmt); i++)
1373 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi,
1374 pset);
1375 if (ret)
1376 return ret;
1379 /* Walk the LHS. If the RHS is appropriate for a memory, we
1380 may use a COMPONENT_REF on the LHS. */
1381 if (wi)
1383 /* If the RHS has more than 1 operand, it is not appropriate
1384 for the memory. */
1385 wi->val_only = !is_gimple_mem_rhs (gimple_assign_rhs1 (stmt))
1386 || !gimple_assign_single_p (stmt);
1387 wi->is_lhs = true;
1390 ret = walk_tree (gimple_op_ptr (stmt, 0), callback_op, wi, pset);
1391 if (ret)
1392 return ret;
1394 if (wi)
1396 wi->val_only = true;
1397 wi->is_lhs = false;
1399 break;
1401 case GIMPLE_CALL:
1402 if (wi)
1403 wi->is_lhs = false;
1405 ret = walk_tree (gimple_call_chain_ptr (stmt), callback_op, wi, pset);
1406 if (ret)
1407 return ret;
1409 ret = walk_tree (gimple_call_fn_ptr (stmt), callback_op, wi, pset);
1410 if (ret)
1411 return ret;
1413 for (i = 0; i < gimple_call_num_args (stmt); i++)
1415 ret = walk_tree (gimple_call_arg_ptr (stmt, i), callback_op, wi,
1416 pset);
1417 if (ret)
1418 return ret;
1421 if (wi)
1422 wi->is_lhs = true;
1424 ret = walk_tree (gimple_call_lhs_ptr (stmt), callback_op, wi, pset);
1425 if (ret)
1426 return ret;
1428 if (wi)
1429 wi->is_lhs = false;
1430 break;
1432 case GIMPLE_CATCH:
1433 ret = walk_tree (gimple_catch_types_ptr (stmt), callback_op, wi,
1434 pset);
1435 if (ret)
1436 return ret;
1437 break;
1439 case GIMPLE_EH_FILTER:
1440 ret = walk_tree (gimple_eh_filter_types_ptr (stmt), callback_op, wi,
1441 pset);
1442 if (ret)
1443 return ret;
1444 break;
1446 case GIMPLE_ASM:
1447 ret = walk_gimple_asm (stmt, callback_op, wi);
1448 if (ret)
1449 return ret;
1450 break;
1452 case GIMPLE_OMP_CONTINUE:
1453 ret = walk_tree (gimple_omp_continue_control_def_ptr (stmt),
1454 callback_op, wi, pset);
1455 if (ret)
1456 return ret;
1458 ret = walk_tree (gimple_omp_continue_control_use_ptr (stmt),
1459 callback_op, wi, pset);
1460 if (ret)
1461 return ret;
1462 break;
1464 case GIMPLE_OMP_CRITICAL:
1465 ret = walk_tree (gimple_omp_critical_name_ptr (stmt), callback_op, wi,
1466 pset);
1467 if (ret)
1468 return ret;
1469 break;
1471 case GIMPLE_OMP_FOR:
1472 ret = walk_tree (gimple_omp_for_clauses_ptr (stmt), callback_op, wi,
1473 pset);
1474 if (ret)
1475 return ret;
1476 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
1478 ret = walk_tree (gimple_omp_for_index_ptr (stmt, i), callback_op,
1479 wi, pset);
1480 if (ret)
1481 return ret;
1482 ret = walk_tree (gimple_omp_for_initial_ptr (stmt, i), callback_op,
1483 wi, pset);
1484 if (ret)
1485 return ret;
1486 ret = walk_tree (gimple_omp_for_final_ptr (stmt, i), callback_op,
1487 wi, pset);
1488 if (ret)
1489 return ret;
1490 ret = walk_tree (gimple_omp_for_incr_ptr (stmt, i), callback_op,
1491 wi, pset);
1493 if (ret)
1494 return ret;
1495 break;
1497 case GIMPLE_OMP_PARALLEL:
1498 ret = walk_tree (gimple_omp_parallel_clauses_ptr (stmt), callback_op,
1499 wi, pset);
1500 if (ret)
1501 return ret;
1502 ret = walk_tree (gimple_omp_parallel_child_fn_ptr (stmt), callback_op,
1503 wi, pset);
1504 if (ret)
1505 return ret;
1506 ret = walk_tree (gimple_omp_parallel_data_arg_ptr (stmt), callback_op,
1507 wi, pset);
1508 if (ret)
1509 return ret;
1510 break;
1512 case GIMPLE_OMP_TASK:
1513 ret = walk_tree (gimple_omp_task_clauses_ptr (stmt), callback_op,
1514 wi, pset);
1515 if (ret)
1516 return ret;
1517 ret = walk_tree (gimple_omp_task_child_fn_ptr (stmt), callback_op,
1518 wi, pset);
1519 if (ret)
1520 return ret;
1521 ret = walk_tree (gimple_omp_task_data_arg_ptr (stmt), callback_op,
1522 wi, pset);
1523 if (ret)
1524 return ret;
1525 ret = walk_tree (gimple_omp_task_copy_fn_ptr (stmt), callback_op,
1526 wi, pset);
1527 if (ret)
1528 return ret;
1529 ret = walk_tree (gimple_omp_task_arg_size_ptr (stmt), callback_op,
1530 wi, pset);
1531 if (ret)
1532 return ret;
1533 ret = walk_tree (gimple_omp_task_arg_align_ptr (stmt), callback_op,
1534 wi, pset);
1535 if (ret)
1536 return ret;
1537 break;
1539 case GIMPLE_OMP_SECTIONS:
1540 ret = walk_tree (gimple_omp_sections_clauses_ptr (stmt), callback_op,
1541 wi, pset);
1542 if (ret)
1543 return ret;
1545 ret = walk_tree (gimple_omp_sections_control_ptr (stmt), callback_op,
1546 wi, pset);
1547 if (ret)
1548 return ret;
1550 break;
1552 case GIMPLE_OMP_SINGLE:
1553 ret = walk_tree (gimple_omp_single_clauses_ptr (stmt), callback_op, wi,
1554 pset);
1555 if (ret)
1556 return ret;
1557 break;
1559 case GIMPLE_OMP_ATOMIC_LOAD:
1560 ret = walk_tree (gimple_omp_atomic_load_lhs_ptr (stmt), callback_op, wi,
1561 pset);
1562 if (ret)
1563 return ret;
1565 ret = walk_tree (gimple_omp_atomic_load_rhs_ptr (stmt), callback_op, wi,
1566 pset);
1567 if (ret)
1568 return ret;
1569 break;
1571 case GIMPLE_OMP_ATOMIC_STORE:
1572 ret = walk_tree (gimple_omp_atomic_store_val_ptr (stmt), callback_op,
1573 wi, pset);
1574 if (ret)
1575 return ret;
1576 break;
1578 /* Tuples that do not have operands. */
1579 case GIMPLE_NOP:
1580 case GIMPLE_RESX:
1581 case GIMPLE_OMP_RETURN:
1582 case GIMPLE_PREDICT:
1583 break;
1585 default:
1587 enum gimple_statement_structure_enum gss;
1588 gss = gimple_statement_structure (stmt);
1589 if (gss == GSS_WITH_OPS || gss == GSS_WITH_MEM_OPS)
1590 for (i = 0; i < gimple_num_ops (stmt); i++)
1592 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi, pset);
1593 if (ret)
1594 return ret;
1597 break;
1600 return NULL_TREE;
1604 /* Walk the current statement in GSI (optionally using traversal state
1605 stored in WI). If WI is NULL, no state is kept during traversal.
1606 The callback CALLBACK_STMT is called. If CALLBACK_STMT indicates
1607 that it has handled all the operands of the statement, its return
1608 value is returned. Otherwise, the return value from CALLBACK_STMT
1609 is discarded and its operands are scanned.
1611 If CALLBACK_STMT is NULL or it didn't handle the operands,
1612 CALLBACK_OP is called on each operand of the statement via
1613 walk_gimple_op. If walk_gimple_op returns non-NULL for any
1614 operand, the remaining operands are not scanned. In this case, the
1615 return value from CALLBACK_OP is returned.
1617 In any other case, NULL_TREE is returned. */
1619 tree
1620 walk_gimple_stmt (gimple_stmt_iterator *gsi, walk_stmt_fn callback_stmt,
1621 walk_tree_fn callback_op, struct walk_stmt_info *wi)
1623 gimple ret;
1624 tree tree_ret;
1625 gimple stmt = gsi_stmt (*gsi);
1627 if (wi)
1628 wi->gsi = *gsi;
1630 if (wi && wi->want_locations && gimple_has_location (stmt))
1631 input_location = gimple_location (stmt);
1633 ret = NULL;
1635 /* Invoke the statement callback. Return if the callback handled
1636 all of STMT operands by itself. */
1637 if (callback_stmt)
1639 bool handled_ops = false;
1640 tree_ret = callback_stmt (gsi, &handled_ops, wi);
1641 if (handled_ops)
1642 return tree_ret;
1644 /* If CALLBACK_STMT did not handle operands, it should not have
1645 a value to return. */
1646 gcc_assert (tree_ret == NULL);
1648 /* Re-read stmt in case the callback changed it. */
1649 stmt = gsi_stmt (*gsi);
1652 /* If CALLBACK_OP is defined, invoke it on every operand of STMT. */
1653 if (callback_op)
1655 tree_ret = walk_gimple_op (stmt, callback_op, wi);
1656 if (tree_ret)
1657 return tree_ret;
1660 /* If STMT can have statements inside (e.g. GIMPLE_BIND), walk them. */
1661 switch (gimple_code (stmt))
1663 case GIMPLE_BIND:
1664 ret = walk_gimple_seq (gimple_bind_body (stmt), callback_stmt,
1665 callback_op, wi);
1666 if (ret)
1667 return wi->callback_result;
1668 break;
1670 case GIMPLE_CATCH:
1671 ret = walk_gimple_seq (gimple_catch_handler (stmt), callback_stmt,
1672 callback_op, wi);
1673 if (ret)
1674 return wi->callback_result;
1675 break;
1677 case GIMPLE_EH_FILTER:
1678 ret = walk_gimple_seq (gimple_eh_filter_failure (stmt), callback_stmt,
1679 callback_op, wi);
1680 if (ret)
1681 return wi->callback_result;
1682 break;
1684 case GIMPLE_TRY:
1685 ret = walk_gimple_seq (gimple_try_eval (stmt), callback_stmt, callback_op,
1686 wi);
1687 if (ret)
1688 return wi->callback_result;
1690 ret = walk_gimple_seq (gimple_try_cleanup (stmt), callback_stmt,
1691 callback_op, wi);
1692 if (ret)
1693 return wi->callback_result;
1694 break;
1696 case GIMPLE_OMP_FOR:
1697 ret = walk_gimple_seq (gimple_omp_for_pre_body (stmt), callback_stmt,
1698 callback_op, wi);
1699 if (ret)
1700 return wi->callback_result;
1702 /* FALL THROUGH. */
1703 case GIMPLE_OMP_CRITICAL:
1704 case GIMPLE_OMP_MASTER:
1705 case GIMPLE_OMP_ORDERED:
1706 case GIMPLE_OMP_SECTION:
1707 case GIMPLE_OMP_PARALLEL:
1708 case GIMPLE_OMP_TASK:
1709 case GIMPLE_OMP_SECTIONS:
1710 case GIMPLE_OMP_SINGLE:
1711 ret = walk_gimple_seq (gimple_omp_body (stmt), callback_stmt, callback_op,
1712 wi);
1713 if (ret)
1714 return wi->callback_result;
1715 break;
1717 case GIMPLE_WITH_CLEANUP_EXPR:
1718 ret = walk_gimple_seq (gimple_wce_cleanup (stmt), callback_stmt,
1719 callback_op, wi);
1720 if (ret)
1721 return wi->callback_result;
1722 break;
1724 default:
1725 gcc_assert (!gimple_has_substatements (stmt));
1726 break;
1729 return NULL;
1733 /* Set sequence SEQ to be the GIMPLE body for function FN. */
1735 void
1736 gimple_set_body (tree fndecl, gimple_seq seq)
1738 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1739 if (fn == NULL)
1741 /* If FNDECL still does not have a function structure associated
1742 with it, then it does not make sense for it to receive a
1743 GIMPLE body. */
1744 gcc_assert (seq == NULL);
1746 else
1747 fn->gimple_body = seq;
1751 /* Return the body of GIMPLE statements for function FN. */
1753 gimple_seq
1754 gimple_body (tree fndecl)
1756 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1757 return fn ? fn->gimple_body : NULL;
1760 /* Return true when FNDECL has Gimple body either in unlowered
1761 or CFG form. */
1762 bool
1763 gimple_has_body_p (tree fndecl)
1765 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1766 return (gimple_body (fndecl) || (fn && fn->cfg));
1769 /* Detect flags from a GIMPLE_CALL. This is just like
1770 call_expr_flags, but for gimple tuples. */
1773 gimple_call_flags (const_gimple stmt)
1775 int flags;
1776 tree decl = gimple_call_fndecl (stmt);
1777 tree t;
1779 if (decl)
1780 flags = flags_from_decl_or_type (decl);
1781 else
1783 t = TREE_TYPE (gimple_call_fn (stmt));
1784 if (t && TREE_CODE (t) == POINTER_TYPE)
1785 flags = flags_from_decl_or_type (TREE_TYPE (t));
1786 else
1787 flags = 0;
1790 return flags;
1794 /* Return true if GS is a copy assignment. */
1796 bool
1797 gimple_assign_copy_p (gimple gs)
1799 return gimple_code (gs) == GIMPLE_ASSIGN
1800 && get_gimple_rhs_class (gimple_assign_rhs_code (gs))
1801 == GIMPLE_SINGLE_RHS
1802 && is_gimple_val (gimple_op (gs, 1));
1806 /* Return true if GS is a SSA_NAME copy assignment. */
1808 bool
1809 gimple_assign_ssa_name_copy_p (gimple gs)
1811 return (gimple_code (gs) == GIMPLE_ASSIGN
1812 && (get_gimple_rhs_class (gimple_assign_rhs_code (gs))
1813 == GIMPLE_SINGLE_RHS)
1814 && TREE_CODE (gimple_assign_lhs (gs)) == SSA_NAME
1815 && TREE_CODE (gimple_assign_rhs1 (gs)) == SSA_NAME);
1819 /* Return true if GS is an assignment with a singleton RHS, i.e.,
1820 there is no operator associated with the assignment itself.
1821 Unlike gimple_assign_copy_p, this predicate returns true for
1822 any RHS operand, including those that perform an operation
1823 and do not have the semantics of a copy, such as COND_EXPR. */
1825 bool
1826 gimple_assign_single_p (gimple gs)
1828 return (gimple_code (gs) == GIMPLE_ASSIGN
1829 && get_gimple_rhs_class (gimple_assign_rhs_code (gs))
1830 == GIMPLE_SINGLE_RHS);
1833 /* Return true if GS is an assignment with a unary RHS, but the
1834 operator has no effect on the assigned value. The logic is adapted
1835 from STRIP_NOPS. This predicate is intended to be used in tuplifying
1836 instances in which STRIP_NOPS was previously applied to the RHS of
1837 an assignment.
1839 NOTE: In the use cases that led to the creation of this function
1840 and of gimple_assign_single_p, it is typical to test for either
1841 condition and to proceed in the same manner. In each case, the
1842 assigned value is represented by the single RHS operand of the
1843 assignment. I suspect there may be cases where gimple_assign_copy_p,
1844 gimple_assign_single_p, or equivalent logic is used where a similar
1845 treatment of unary NOPs is appropriate. */
1847 bool
1848 gimple_assign_unary_nop_p (gimple gs)
1850 return (gimple_code (gs) == GIMPLE_ASSIGN
1851 && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (gs))
1852 || gimple_assign_rhs_code (gs) == NON_LVALUE_EXPR)
1853 && gimple_assign_rhs1 (gs) != error_mark_node
1854 && (TYPE_MODE (TREE_TYPE (gimple_assign_lhs (gs)))
1855 == TYPE_MODE (TREE_TYPE (gimple_assign_rhs1 (gs)))));
1858 /* Set BB to be the basic block holding G. */
1860 void
1861 gimple_set_bb (gimple stmt, basic_block bb)
1863 stmt->gsbase.bb = bb;
1865 /* If the statement is a label, add the label to block-to-labels map
1866 so that we can speed up edge creation for GIMPLE_GOTOs. */
1867 if (cfun->cfg && gimple_code (stmt) == GIMPLE_LABEL)
1869 tree t;
1870 int uid;
1872 t = gimple_label_label (stmt);
1873 uid = LABEL_DECL_UID (t);
1874 if (uid == -1)
1876 unsigned old_len = VEC_length (basic_block, label_to_block_map);
1877 LABEL_DECL_UID (t) = uid = cfun->cfg->last_label_uid++;
1878 if (old_len <= (unsigned) uid)
1880 unsigned new_len = 3 * uid / 2 + 1;
1882 VEC_safe_grow_cleared (basic_block, gc, label_to_block_map,
1883 new_len);
1887 VEC_replace (basic_block, label_to_block_map, uid, bb);
1892 /* Fold the expression computed by STMT. If the expression can be
1893 folded, return the folded result, otherwise return NULL. STMT is
1894 not modified. */
1896 tree
1897 gimple_fold (const_gimple stmt)
1899 switch (gimple_code (stmt))
1901 case GIMPLE_COND:
1902 return fold_binary (gimple_cond_code (stmt),
1903 boolean_type_node,
1904 gimple_cond_lhs (stmt),
1905 gimple_cond_rhs (stmt));
1907 case GIMPLE_ASSIGN:
1908 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
1910 case GIMPLE_UNARY_RHS:
1911 return fold_unary (gimple_assign_rhs_code (stmt),
1912 TREE_TYPE (gimple_assign_lhs (stmt)),
1913 gimple_assign_rhs1 (stmt));
1914 case GIMPLE_BINARY_RHS:
1915 return fold_binary (gimple_assign_rhs_code (stmt),
1916 TREE_TYPE (gimple_assign_lhs (stmt)),
1917 gimple_assign_rhs1 (stmt),
1918 gimple_assign_rhs2 (stmt));
1919 case GIMPLE_SINGLE_RHS:
1920 return fold (gimple_assign_rhs1 (stmt));
1921 default:;
1923 break;
1925 case GIMPLE_SWITCH:
1926 return gimple_switch_index (stmt);
1928 case GIMPLE_CALL:
1929 return NULL_TREE;
1931 default:
1932 break;
1935 gcc_unreachable ();
1939 /* Modify the RHS of the assignment pointed-to by GSI using the
1940 operands in the expression tree EXPR.
1942 NOTE: The statement pointed-to by GSI may be reallocated if it
1943 did not have enough operand slots.
1945 This function is useful to convert an existing tree expression into
1946 the flat representation used for the RHS of a GIMPLE assignment.
1947 It will reallocate memory as needed to expand or shrink the number
1948 of operand slots needed to represent EXPR.
1950 NOTE: If you find yourself building a tree and then calling this
1951 function, you are most certainly doing it the slow way. It is much
1952 better to build a new assignment or to use the function
1953 gimple_assign_set_rhs_with_ops, which does not require an
1954 expression tree to be built. */
1956 void
1957 gimple_assign_set_rhs_from_tree (gimple_stmt_iterator *gsi, tree expr)
1959 enum tree_code subcode;
1960 tree op1, op2;
1962 extract_ops_from_tree (expr, &subcode, &op1, &op2);
1963 gimple_assign_set_rhs_with_ops (gsi, subcode, op1, op2);
1967 /* Set the RHS of assignment statement pointed-to by GSI to CODE with
1968 operands OP1 and OP2.
1970 NOTE: The statement pointed-to by GSI may be reallocated if it
1971 did not have enough operand slots. */
1973 void
1974 gimple_assign_set_rhs_with_ops (gimple_stmt_iterator *gsi, enum tree_code code,
1975 tree op1, tree op2)
1977 unsigned new_rhs_ops = get_gimple_rhs_num_ops (code);
1978 gimple stmt = gsi_stmt (*gsi);
1980 /* If the new CODE needs more operands, allocate a new statement. */
1981 if (gimple_num_ops (stmt) < new_rhs_ops + 1)
1983 tree lhs = gimple_assign_lhs (stmt);
1984 gimple new_stmt = gimple_alloc (gimple_code (stmt), new_rhs_ops + 1);
1985 memcpy (new_stmt, stmt, gimple_size (gimple_code (stmt)));
1986 gsi_replace (gsi, new_stmt, true);
1987 stmt = new_stmt;
1989 /* The LHS needs to be reset as this also changes the SSA name
1990 on the LHS. */
1991 gimple_assign_set_lhs (stmt, lhs);
1994 gimple_set_num_ops (stmt, new_rhs_ops + 1);
1995 gimple_set_subcode (stmt, code);
1996 gimple_assign_set_rhs1 (stmt, op1);
1997 if (new_rhs_ops > 1)
1998 gimple_assign_set_rhs2 (stmt, op2);
2002 /* Return the LHS of a statement that performs an assignment,
2003 either a GIMPLE_ASSIGN or a GIMPLE_CALL. Returns NULL_TREE
2004 for a call to a function that returns no value, or for a
2005 statement other than an assignment or a call. */
2007 tree
2008 gimple_get_lhs (const_gimple stmt)
2010 enum gimple_code code = gimple_code (stmt);
2012 if (code == GIMPLE_ASSIGN)
2013 return gimple_assign_lhs (stmt);
2014 else if (code == GIMPLE_CALL)
2015 return gimple_call_lhs (stmt);
2016 else
2017 return NULL_TREE;
2021 /* Set the LHS of a statement that performs an assignment,
2022 either a GIMPLE_ASSIGN or a GIMPLE_CALL. */
2024 void
2025 gimple_set_lhs (gimple stmt, tree lhs)
2027 enum gimple_code code = gimple_code (stmt);
2029 if (code == GIMPLE_ASSIGN)
2030 gimple_assign_set_lhs (stmt, lhs);
2031 else if (code == GIMPLE_CALL)
2032 gimple_call_set_lhs (stmt, lhs);
2033 else
2034 gcc_unreachable();
2038 /* Return a deep copy of statement STMT. All the operands from STMT
2039 are reallocated and copied using unshare_expr. The DEF, USE, VDEF
2040 and VUSE operand arrays are set to empty in the new copy. */
2042 gimple
2043 gimple_copy (gimple stmt)
2045 enum gimple_code code = gimple_code (stmt);
2046 unsigned num_ops = gimple_num_ops (stmt);
2047 gimple copy = gimple_alloc (code, num_ops);
2048 unsigned i;
2050 /* Shallow copy all the fields from STMT. */
2051 memcpy (copy, stmt, gimple_size (code));
2053 /* If STMT has sub-statements, deep-copy them as well. */
2054 if (gimple_has_substatements (stmt))
2056 gimple_seq new_seq;
2057 tree t;
2059 switch (gimple_code (stmt))
2061 case GIMPLE_BIND:
2062 new_seq = gimple_seq_copy (gimple_bind_body (stmt));
2063 gimple_bind_set_body (copy, new_seq);
2064 gimple_bind_set_vars (copy, unshare_expr (gimple_bind_vars (stmt)));
2065 gimple_bind_set_block (copy, gimple_bind_block (stmt));
2066 break;
2068 case GIMPLE_CATCH:
2069 new_seq = gimple_seq_copy (gimple_catch_handler (stmt));
2070 gimple_catch_set_handler (copy, new_seq);
2071 t = unshare_expr (gimple_catch_types (stmt));
2072 gimple_catch_set_types (copy, t);
2073 break;
2075 case GIMPLE_EH_FILTER:
2076 new_seq = gimple_seq_copy (gimple_eh_filter_failure (stmt));
2077 gimple_eh_filter_set_failure (copy, new_seq);
2078 t = unshare_expr (gimple_eh_filter_types (stmt));
2079 gimple_eh_filter_set_types (copy, t);
2080 break;
2082 case GIMPLE_TRY:
2083 new_seq = gimple_seq_copy (gimple_try_eval (stmt));
2084 gimple_try_set_eval (copy, new_seq);
2085 new_seq = gimple_seq_copy (gimple_try_cleanup (stmt));
2086 gimple_try_set_cleanup (copy, new_seq);
2087 break;
2089 case GIMPLE_OMP_FOR:
2090 new_seq = gimple_seq_copy (gimple_omp_for_pre_body (stmt));
2091 gimple_omp_for_set_pre_body (copy, new_seq);
2092 t = unshare_expr (gimple_omp_for_clauses (stmt));
2093 gimple_omp_for_set_clauses (copy, t);
2094 copy->gimple_omp_for.iter
2095 = GGC_NEWVEC (struct gimple_omp_for_iter,
2096 gimple_omp_for_collapse (stmt));
2097 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
2099 gimple_omp_for_set_cond (copy, i,
2100 gimple_omp_for_cond (stmt, i));
2101 gimple_omp_for_set_index (copy, i,
2102 gimple_omp_for_index (stmt, i));
2103 t = unshare_expr (gimple_omp_for_initial (stmt, i));
2104 gimple_omp_for_set_initial (copy, i, t);
2105 t = unshare_expr (gimple_omp_for_final (stmt, i));
2106 gimple_omp_for_set_final (copy, i, t);
2107 t = unshare_expr (gimple_omp_for_incr (stmt, i));
2108 gimple_omp_for_set_incr (copy, i, t);
2110 goto copy_omp_body;
2112 case GIMPLE_OMP_PARALLEL:
2113 t = unshare_expr (gimple_omp_parallel_clauses (stmt));
2114 gimple_omp_parallel_set_clauses (copy, t);
2115 t = unshare_expr (gimple_omp_parallel_child_fn (stmt));
2116 gimple_omp_parallel_set_child_fn (copy, t);
2117 t = unshare_expr (gimple_omp_parallel_data_arg (stmt));
2118 gimple_omp_parallel_set_data_arg (copy, t);
2119 goto copy_omp_body;
2121 case GIMPLE_OMP_TASK:
2122 t = unshare_expr (gimple_omp_task_clauses (stmt));
2123 gimple_omp_task_set_clauses (copy, t);
2124 t = unshare_expr (gimple_omp_task_child_fn (stmt));
2125 gimple_omp_task_set_child_fn (copy, t);
2126 t = unshare_expr (gimple_omp_task_data_arg (stmt));
2127 gimple_omp_task_set_data_arg (copy, t);
2128 t = unshare_expr (gimple_omp_task_copy_fn (stmt));
2129 gimple_omp_task_set_copy_fn (copy, t);
2130 t = unshare_expr (gimple_omp_task_arg_size (stmt));
2131 gimple_omp_task_set_arg_size (copy, t);
2132 t = unshare_expr (gimple_omp_task_arg_align (stmt));
2133 gimple_omp_task_set_arg_align (copy, t);
2134 goto copy_omp_body;
2136 case GIMPLE_OMP_CRITICAL:
2137 t = unshare_expr (gimple_omp_critical_name (stmt));
2138 gimple_omp_critical_set_name (copy, t);
2139 goto copy_omp_body;
2141 case GIMPLE_OMP_SECTIONS:
2142 t = unshare_expr (gimple_omp_sections_clauses (stmt));
2143 gimple_omp_sections_set_clauses (copy, t);
2144 t = unshare_expr (gimple_omp_sections_control (stmt));
2145 gimple_omp_sections_set_control (copy, t);
2146 /* FALLTHRU */
2148 case GIMPLE_OMP_SINGLE:
2149 case GIMPLE_OMP_SECTION:
2150 case GIMPLE_OMP_MASTER:
2151 case GIMPLE_OMP_ORDERED:
2152 copy_omp_body:
2153 new_seq = gimple_seq_copy (gimple_omp_body (stmt));
2154 gimple_omp_set_body (copy, new_seq);
2155 break;
2157 case GIMPLE_WITH_CLEANUP_EXPR:
2158 new_seq = gimple_seq_copy (gimple_wce_cleanup (stmt));
2159 gimple_wce_set_cleanup (copy, new_seq);
2160 break;
2162 default:
2163 gcc_unreachable ();
2167 /* Make copy of operands. */
2168 if (num_ops > 0)
2170 for (i = 0; i < num_ops; i++)
2171 gimple_set_op (copy, i, unshare_expr (gimple_op (stmt, i)));
2173 /* Clear out SSA operand vectors on COPY. */
2174 if (gimple_has_ops (stmt))
2176 gimple_set_def_ops (copy, NULL);
2177 gimple_set_use_ops (copy, NULL);
2180 if (gimple_has_mem_ops (stmt))
2182 gimple_set_vdef (copy, gimple_vdef (stmt));
2183 gimple_set_vuse (copy, gimple_vuse (stmt));
2186 /* SSA operands need to be updated. */
2187 gimple_set_modified (copy, true);
2190 return copy;
2194 /* Set the MODIFIED flag to MODIFIEDP, iff the gimple statement G has
2195 a MODIFIED field. */
2197 void
2198 gimple_set_modified (gimple s, bool modifiedp)
2200 if (gimple_has_ops (s))
2202 s->gsbase.modified = (unsigned) modifiedp;
2204 if (modifiedp
2205 && cfun->gimple_df
2206 && is_gimple_call (s)
2207 && gimple_call_noreturn_p (s))
2208 VEC_safe_push (gimple, gc, MODIFIED_NORETURN_CALLS (cfun), s);
2213 /* Return true if statement S has side-effects. We consider a
2214 statement to have side effects if:
2216 - It is a GIMPLE_CALL not marked with ECF_PURE or ECF_CONST.
2217 - Any of its operands are marked TREE_THIS_VOLATILE or TREE_SIDE_EFFECTS. */
2219 bool
2220 gimple_has_side_effects (const_gimple s)
2222 unsigned i;
2224 /* We don't have to scan the arguments to check for
2225 volatile arguments, though, at present, we still
2226 do a scan to check for TREE_SIDE_EFFECTS. */
2227 if (gimple_has_volatile_ops (s))
2228 return true;
2230 if (is_gimple_call (s))
2232 unsigned nargs = gimple_call_num_args (s);
2234 if (!(gimple_call_flags (s) & (ECF_CONST | ECF_PURE)))
2235 return true;
2236 else if (gimple_call_flags (s) & ECF_LOOPING_CONST_OR_PURE)
2237 /* An infinite loop is considered a side effect. */
2238 return true;
2240 if (gimple_call_lhs (s)
2241 && TREE_SIDE_EFFECTS (gimple_call_lhs (s)))
2243 gcc_assert (gimple_has_volatile_ops (s));
2244 return true;
2247 if (TREE_SIDE_EFFECTS (gimple_call_fn (s)))
2248 return true;
2250 for (i = 0; i < nargs; i++)
2251 if (TREE_SIDE_EFFECTS (gimple_call_arg (s, i)))
2253 gcc_assert (gimple_has_volatile_ops (s));
2254 return true;
2257 return false;
2259 else
2261 for (i = 0; i < gimple_num_ops (s); i++)
2262 if (TREE_SIDE_EFFECTS (gimple_op (s, i)))
2264 gcc_assert (gimple_has_volatile_ops (s));
2265 return true;
2269 return false;
2272 /* Return true if the RHS of statement S has side effects.
2273 We may use it to determine if it is admissable to replace
2274 an assignment or call with a copy of a previously-computed
2275 value. In such cases, side-effects due the the LHS are
2276 preserved. */
2278 bool
2279 gimple_rhs_has_side_effects (const_gimple s)
2281 unsigned i;
2283 if (is_gimple_call (s))
2285 unsigned nargs = gimple_call_num_args (s);
2287 if (!(gimple_call_flags (s) & (ECF_CONST | ECF_PURE)))
2288 return true;
2290 /* We cannot use gimple_has_volatile_ops here,
2291 because we must ignore a volatile LHS. */
2292 if (TREE_SIDE_EFFECTS (gimple_call_fn (s))
2293 || TREE_THIS_VOLATILE (gimple_call_fn (s)))
2295 gcc_assert (gimple_has_volatile_ops (s));
2296 return true;
2299 for (i = 0; i < nargs; i++)
2300 if (TREE_SIDE_EFFECTS (gimple_call_arg (s, i))
2301 || TREE_THIS_VOLATILE (gimple_call_arg (s, i)))
2302 return true;
2304 return false;
2306 else if (is_gimple_assign (s))
2308 /* Skip the first operand, the LHS. */
2309 for (i = 1; i < gimple_num_ops (s); i++)
2310 if (TREE_SIDE_EFFECTS (gimple_op (s, i))
2311 || TREE_THIS_VOLATILE (gimple_op (s, i)))
2313 gcc_assert (gimple_has_volatile_ops (s));
2314 return true;
2317 else
2319 /* For statements without an LHS, examine all arguments. */
2320 for (i = 0; i < gimple_num_ops (s); i++)
2321 if (TREE_SIDE_EFFECTS (gimple_op (s, i))
2322 || TREE_THIS_VOLATILE (gimple_op (s, i)))
2324 gcc_assert (gimple_has_volatile_ops (s));
2325 return true;
2329 return false;
2333 /* Helper for gimple_could_trap_p and gimple_assign_rhs_could_trap_p.
2334 Return true if S can trap. If INCLUDE_LHS is true and S is a
2335 GIMPLE_ASSIGN, the LHS of the assignment is also checked.
2336 Otherwise, only the RHS of the assignment is checked. */
2338 static bool
2339 gimple_could_trap_p_1 (gimple s, bool include_lhs)
2341 unsigned i, start;
2342 tree t, div = NULL_TREE;
2343 enum tree_code op;
2345 start = (is_gimple_assign (s) && !include_lhs) ? 1 : 0;
2347 for (i = start; i < gimple_num_ops (s); i++)
2348 if (tree_could_trap_p (gimple_op (s, i)))
2349 return true;
2351 switch (gimple_code (s))
2353 case GIMPLE_ASM:
2354 return gimple_asm_volatile_p (s);
2356 case GIMPLE_CALL:
2357 t = gimple_call_fndecl (s);
2358 /* Assume that calls to weak functions may trap. */
2359 if (!t || !DECL_P (t) || DECL_WEAK (t))
2360 return true;
2361 return false;
2363 case GIMPLE_ASSIGN:
2364 t = gimple_expr_type (s);
2365 op = gimple_assign_rhs_code (s);
2366 if (get_gimple_rhs_class (op) == GIMPLE_BINARY_RHS)
2367 div = gimple_assign_rhs2 (s);
2368 return (operation_could_trap_p (op, FLOAT_TYPE_P (t),
2369 (INTEGRAL_TYPE_P (t)
2370 && TYPE_OVERFLOW_TRAPS (t)),
2371 div));
2373 default:
2374 break;
2377 return false;
2382 /* Return true if statement S can trap. */
2384 bool
2385 gimple_could_trap_p (gimple s)
2387 return gimple_could_trap_p_1 (s, true);
2391 /* Return true if RHS of a GIMPLE_ASSIGN S can trap. */
2393 bool
2394 gimple_assign_rhs_could_trap_p (gimple s)
2396 gcc_assert (is_gimple_assign (s));
2397 return gimple_could_trap_p_1 (s, false);
2401 /* Print debugging information for gimple stmts generated. */
2403 void
2404 dump_gimple_statistics (void)
2406 #ifdef GATHER_STATISTICS
2407 int i, total_tuples = 0, total_bytes = 0;
2409 fprintf (stderr, "\nGIMPLE statements\n");
2410 fprintf (stderr, "Kind Stmts Bytes\n");
2411 fprintf (stderr, "---------------------------------------\n");
2412 for (i = 0; i < (int) gimple_alloc_kind_all; ++i)
2414 fprintf (stderr, "%-20s %7d %10d\n", gimple_alloc_kind_names[i],
2415 gimple_alloc_counts[i], gimple_alloc_sizes[i]);
2416 total_tuples += gimple_alloc_counts[i];
2417 total_bytes += gimple_alloc_sizes[i];
2419 fprintf (stderr, "---------------------------------------\n");
2420 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_tuples, total_bytes);
2421 fprintf (stderr, "---------------------------------------\n");
2422 #else
2423 fprintf (stderr, "No gimple statistics\n");
2424 #endif
2428 /* Return the number of operands needed on the RHS of a GIMPLE
2429 assignment for an expression with tree code CODE. */
2431 unsigned
2432 get_gimple_rhs_num_ops (enum tree_code code)
2434 enum gimple_rhs_class rhs_class = get_gimple_rhs_class (code);
2436 if (rhs_class == GIMPLE_UNARY_RHS || rhs_class == GIMPLE_SINGLE_RHS)
2437 return 1;
2438 else if (rhs_class == GIMPLE_BINARY_RHS)
2439 return 2;
2440 else
2441 gcc_unreachable ();
2444 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) \
2445 (unsigned char) \
2446 ((TYPE) == tcc_unary ? GIMPLE_UNARY_RHS \
2447 : ((TYPE) == tcc_binary \
2448 || (TYPE) == tcc_comparison) ? GIMPLE_BINARY_RHS \
2449 : ((TYPE) == tcc_constant \
2450 || (TYPE) == tcc_declaration \
2451 || (TYPE) == tcc_reference) ? GIMPLE_SINGLE_RHS \
2452 : ((SYM) == TRUTH_AND_EXPR \
2453 || (SYM) == TRUTH_OR_EXPR \
2454 || (SYM) == TRUTH_XOR_EXPR) ? GIMPLE_BINARY_RHS \
2455 : (SYM) == TRUTH_NOT_EXPR ? GIMPLE_UNARY_RHS \
2456 : ((SYM) == COND_EXPR \
2457 || (SYM) == CONSTRUCTOR \
2458 || (SYM) == OBJ_TYPE_REF \
2459 || (SYM) == ASSERT_EXPR \
2460 || (SYM) == ADDR_EXPR \
2461 || (SYM) == WITH_SIZE_EXPR \
2462 || (SYM) == EXC_PTR_EXPR \
2463 || (SYM) == SSA_NAME \
2464 || (SYM) == FILTER_EXPR \
2465 || (SYM) == POLYNOMIAL_CHREC \
2466 || (SYM) == DOT_PROD_EXPR \
2467 || (SYM) == VEC_COND_EXPR \
2468 || (SYM) == REALIGN_LOAD_EXPR) ? GIMPLE_SINGLE_RHS \
2469 : GIMPLE_INVALID_RHS),
2470 #define END_OF_BASE_TREE_CODES (unsigned char) GIMPLE_INVALID_RHS,
2472 const unsigned char gimple_rhs_class_table[] = {
2473 #include "all-tree.def"
2476 #undef DEFTREECODE
2477 #undef END_OF_BASE_TREE_CODES
2479 /* For the definitive definition of GIMPLE, see doc/tree-ssa.texi. */
2481 /* Validation of GIMPLE expressions. */
2483 /* Return true if OP is an acceptable tree node to be used as a GIMPLE
2484 operand. */
2486 bool
2487 is_gimple_operand (const_tree op)
2489 return op && get_gimple_rhs_class (TREE_CODE (op)) == GIMPLE_SINGLE_RHS;
2492 /* Returns true iff T is a valid RHS for an assignment to a renamed
2493 user -- or front-end generated artificial -- variable. */
2495 bool
2496 is_gimple_reg_rhs (tree t)
2498 return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
2501 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
2502 LHS, or for a call argument. */
2504 bool
2505 is_gimple_mem_rhs (tree t)
2507 /* If we're dealing with a renamable type, either source or dest must be
2508 a renamed variable. */
2509 if (is_gimple_reg_type (TREE_TYPE (t)))
2510 return is_gimple_val (t);
2511 else
2512 return is_gimple_val (t) || is_gimple_lvalue (t);
2515 /* Return true if T is a valid LHS for a GIMPLE assignment expression. */
2517 bool
2518 is_gimple_lvalue (tree t)
2520 return (is_gimple_addressable (t)
2521 || TREE_CODE (t) == WITH_SIZE_EXPR
2522 /* These are complex lvalues, but don't have addresses, so they
2523 go here. */
2524 || TREE_CODE (t) == BIT_FIELD_REF);
2527 /* Return true if T is a GIMPLE condition. */
2529 bool
2530 is_gimple_condexpr (tree t)
2532 return (is_gimple_val (t) || (COMPARISON_CLASS_P (t)
2533 && !tree_could_trap_p (t)
2534 && is_gimple_val (TREE_OPERAND (t, 0))
2535 && is_gimple_val (TREE_OPERAND (t, 1))));
2538 /* Return true if T is something whose address can be taken. */
2540 bool
2541 is_gimple_addressable (tree t)
2543 return (is_gimple_id (t) || handled_component_p (t) || INDIRECT_REF_P (t));
2546 /* Return true if T is a valid gimple constant. */
2548 bool
2549 is_gimple_constant (const_tree t)
2551 switch (TREE_CODE (t))
2553 case INTEGER_CST:
2554 case REAL_CST:
2555 case FIXED_CST:
2556 case STRING_CST:
2557 case COMPLEX_CST:
2558 case VECTOR_CST:
2559 return true;
2561 /* Vector constant constructors are gimple invariant. */
2562 case CONSTRUCTOR:
2563 if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2564 return TREE_CONSTANT (t);
2565 else
2566 return false;
2568 default:
2569 return false;
2573 /* Return true if T is a gimple address. */
2575 bool
2576 is_gimple_address (const_tree t)
2578 tree op;
2580 if (TREE_CODE (t) != ADDR_EXPR)
2581 return false;
2583 op = TREE_OPERAND (t, 0);
2584 while (handled_component_p (op))
2586 if ((TREE_CODE (op) == ARRAY_REF
2587 || TREE_CODE (op) == ARRAY_RANGE_REF)
2588 && !is_gimple_val (TREE_OPERAND (op, 1)))
2589 return false;
2591 op = TREE_OPERAND (op, 0);
2594 if (CONSTANT_CLASS_P (op) || INDIRECT_REF_P (op))
2595 return true;
2597 switch (TREE_CODE (op))
2599 case PARM_DECL:
2600 case RESULT_DECL:
2601 case LABEL_DECL:
2602 case FUNCTION_DECL:
2603 case VAR_DECL:
2604 case CONST_DECL:
2605 return true;
2607 default:
2608 return false;
2612 /* Strip out all handled components that produce invariant
2613 offsets. */
2615 static const_tree
2616 strip_invariant_refs (const_tree op)
2618 while (handled_component_p (op))
2620 switch (TREE_CODE (op))
2622 case ARRAY_REF:
2623 case ARRAY_RANGE_REF:
2624 if (!is_gimple_constant (TREE_OPERAND (op, 1))
2625 || TREE_OPERAND (op, 2) != NULL_TREE
2626 || TREE_OPERAND (op, 3) != NULL_TREE)
2627 return NULL;
2628 break;
2630 case COMPONENT_REF:
2631 if (TREE_OPERAND (op, 2) != NULL_TREE)
2632 return NULL;
2633 break;
2635 default:;
2637 op = TREE_OPERAND (op, 0);
2640 return op;
2643 /* Return true if T is a gimple invariant address. */
2645 bool
2646 is_gimple_invariant_address (const_tree t)
2648 const_tree op;
2650 if (TREE_CODE (t) != ADDR_EXPR)
2651 return false;
2653 op = strip_invariant_refs (TREE_OPERAND (t, 0));
2655 return op && (CONSTANT_CLASS_P (op) || decl_address_invariant_p (op));
2658 /* Return true if T is a gimple invariant address at IPA level
2659 (so addresses of variables on stack are not allowed). */
2661 bool
2662 is_gimple_ip_invariant_address (const_tree t)
2664 const_tree op;
2666 if (TREE_CODE (t) != ADDR_EXPR)
2667 return false;
2669 op = strip_invariant_refs (TREE_OPERAND (t, 0));
2671 return op && (CONSTANT_CLASS_P (op) || decl_address_ip_invariant_p (op));
2674 /* Return true if T is a GIMPLE minimal invariant. It's a restricted
2675 form of function invariant. */
2677 bool
2678 is_gimple_min_invariant (const_tree t)
2680 if (TREE_CODE (t) == ADDR_EXPR)
2681 return is_gimple_invariant_address (t);
2683 return is_gimple_constant (t);
2686 /* Return true if T is a GIMPLE interprocedural invariant. It's a restricted
2687 form of gimple minimal invariant. */
2689 bool
2690 is_gimple_ip_invariant (const_tree t)
2692 if (TREE_CODE (t) == ADDR_EXPR)
2693 return is_gimple_ip_invariant_address (t);
2695 return is_gimple_constant (t);
2698 /* Return true if T looks like a valid GIMPLE statement. */
2700 bool
2701 is_gimple_stmt (tree t)
2703 const enum tree_code code = TREE_CODE (t);
2705 switch (code)
2707 case NOP_EXPR:
2708 /* The only valid NOP_EXPR is the empty statement. */
2709 return IS_EMPTY_STMT (t);
2711 case BIND_EXPR:
2712 case COND_EXPR:
2713 /* These are only valid if they're void. */
2714 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
2716 case SWITCH_EXPR:
2717 case GOTO_EXPR:
2718 case RETURN_EXPR:
2719 case LABEL_EXPR:
2720 case CASE_LABEL_EXPR:
2721 case TRY_CATCH_EXPR:
2722 case TRY_FINALLY_EXPR:
2723 case EH_FILTER_EXPR:
2724 case CATCH_EXPR:
2725 case ASM_EXPR:
2726 case RESX_EXPR:
2727 case STATEMENT_LIST:
2728 case OMP_PARALLEL:
2729 case OMP_FOR:
2730 case OMP_SECTIONS:
2731 case OMP_SECTION:
2732 case OMP_SINGLE:
2733 case OMP_MASTER:
2734 case OMP_ORDERED:
2735 case OMP_CRITICAL:
2736 case OMP_TASK:
2737 /* These are always void. */
2738 return true;
2740 case CALL_EXPR:
2741 case MODIFY_EXPR:
2742 case PREDICT_EXPR:
2743 /* These are valid regardless of their type. */
2744 return true;
2746 default:
2747 return false;
2751 /* Return true if T is a variable. */
2753 bool
2754 is_gimple_variable (tree t)
2756 return (TREE_CODE (t) == VAR_DECL
2757 || TREE_CODE (t) == PARM_DECL
2758 || TREE_CODE (t) == RESULT_DECL
2759 || TREE_CODE (t) == SSA_NAME);
2762 /* Return true if T is a GIMPLE identifier (something with an address). */
2764 bool
2765 is_gimple_id (tree t)
2767 return (is_gimple_variable (t)
2768 || TREE_CODE (t) == FUNCTION_DECL
2769 || TREE_CODE (t) == LABEL_DECL
2770 || TREE_CODE (t) == CONST_DECL
2771 /* Allow string constants, since they are addressable. */
2772 || TREE_CODE (t) == STRING_CST);
2775 /* Return true if TYPE is a suitable type for a scalar register variable. */
2777 bool
2778 is_gimple_reg_type (tree type)
2780 return !AGGREGATE_TYPE_P (type);
2783 /* Return true if T is a non-aggregate register variable. */
2785 bool
2786 is_gimple_reg (tree t)
2788 if (TREE_CODE (t) == SSA_NAME)
2789 t = SSA_NAME_VAR (t);
2791 if (!is_gimple_variable (t))
2792 return false;
2794 if (!is_gimple_reg_type (TREE_TYPE (t)))
2795 return false;
2797 /* A volatile decl is not acceptable because we can't reuse it as
2798 needed. We need to copy it into a temp first. */
2799 if (TREE_THIS_VOLATILE (t))
2800 return false;
2802 /* We define "registers" as things that can be renamed as needed,
2803 which with our infrastructure does not apply to memory. */
2804 if (needs_to_live_in_memory (t))
2805 return false;
2807 /* Hard register variables are an interesting case. For those that
2808 are call-clobbered, we don't know where all the calls are, since
2809 we don't (want to) take into account which operations will turn
2810 into libcalls at the rtl level. For those that are call-saved,
2811 we don't currently model the fact that calls may in fact change
2812 global hard registers, nor do we examine ASM_CLOBBERS at the tree
2813 level, and so miss variable changes that might imply. All around,
2814 it seems safest to not do too much optimization with these at the
2815 tree level at all. We'll have to rely on the rtl optimizers to
2816 clean this up, as there we've got all the appropriate bits exposed. */
2817 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
2818 return false;
2820 /* Complex and vector values must have been put into SSA-like form.
2821 That is, no assignments to the individual components. */
2822 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
2823 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2824 return DECL_GIMPLE_REG_P (t);
2826 return true;
2830 /* Return true if T is a GIMPLE variable whose address is not needed. */
2832 bool
2833 is_gimple_non_addressable (tree t)
2835 if (TREE_CODE (t) == SSA_NAME)
2836 t = SSA_NAME_VAR (t);
2838 return (is_gimple_variable (t) && ! needs_to_live_in_memory (t));
2841 /* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant. */
2843 bool
2844 is_gimple_val (tree t)
2846 /* Make loads from volatiles and memory vars explicit. */
2847 if (is_gimple_variable (t)
2848 && is_gimple_reg_type (TREE_TYPE (t))
2849 && !is_gimple_reg (t))
2850 return false;
2852 /* FIXME make these decls. That can happen only when we expose the
2853 entire landing-pad construct at the tree level. */
2854 if (TREE_CODE (t) == EXC_PTR_EXPR || TREE_CODE (t) == FILTER_EXPR)
2855 return true;
2857 return (is_gimple_variable (t) || is_gimple_min_invariant (t));
2860 /* Similarly, but accept hard registers as inputs to asm statements. */
2862 bool
2863 is_gimple_asm_val (tree t)
2865 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
2866 return true;
2868 return is_gimple_val (t);
2871 /* Return true if T is a GIMPLE minimal lvalue. */
2873 bool
2874 is_gimple_min_lval (tree t)
2876 if (!(t = CONST_CAST_TREE (strip_invariant_refs (t))))
2877 return false;
2878 return (is_gimple_id (t) || TREE_CODE (t) == INDIRECT_REF);
2881 /* Return true if T is a typecast operation. */
2883 bool
2884 is_gimple_cast (tree t)
2886 return (CONVERT_EXPR_P (t)
2887 || TREE_CODE (t) == FIX_TRUNC_EXPR);
2890 /* Return true if T is a valid function operand of a CALL_EXPR. */
2892 bool
2893 is_gimple_call_addr (tree t)
2895 return (TREE_CODE (t) == OBJ_TYPE_REF || is_gimple_val (t));
2898 /* If T makes a function call, return the corresponding CALL_EXPR operand.
2899 Otherwise, return NULL_TREE. */
2901 tree
2902 get_call_expr_in (tree t)
2904 if (TREE_CODE (t) == MODIFY_EXPR)
2905 t = TREE_OPERAND (t, 1);
2906 if (TREE_CODE (t) == WITH_SIZE_EXPR)
2907 t = TREE_OPERAND (t, 0);
2908 if (TREE_CODE (t) == CALL_EXPR)
2909 return t;
2910 return NULL_TREE;
2914 /* Given a memory reference expression T, return its base address.
2915 The base address of a memory reference expression is the main
2916 object being referenced. For instance, the base address for
2917 'array[i].fld[j]' is 'array'. You can think of this as stripping
2918 away the offset part from a memory address.
2920 This function calls handled_component_p to strip away all the inner
2921 parts of the memory reference until it reaches the base object. */
2923 tree
2924 get_base_address (tree t)
2926 while (handled_component_p (t))
2927 t = TREE_OPERAND (t, 0);
2929 if (SSA_VAR_P (t)
2930 || TREE_CODE (t) == STRING_CST
2931 || TREE_CODE (t) == CONSTRUCTOR
2932 || INDIRECT_REF_P (t))
2933 return t;
2934 else
2935 return NULL_TREE;
2938 void
2939 recalculate_side_effects (tree t)
2941 enum tree_code code = TREE_CODE (t);
2942 int len = TREE_OPERAND_LENGTH (t);
2943 int i;
2945 switch (TREE_CODE_CLASS (code))
2947 case tcc_expression:
2948 switch (code)
2950 case INIT_EXPR:
2951 case MODIFY_EXPR:
2952 case VA_ARG_EXPR:
2953 case PREDECREMENT_EXPR:
2954 case PREINCREMENT_EXPR:
2955 case POSTDECREMENT_EXPR:
2956 case POSTINCREMENT_EXPR:
2957 /* All of these have side-effects, no matter what their
2958 operands are. */
2959 return;
2961 default:
2962 break;
2964 /* Fall through. */
2966 case tcc_comparison: /* a comparison expression */
2967 case tcc_unary: /* a unary arithmetic expression */
2968 case tcc_binary: /* a binary arithmetic expression */
2969 case tcc_reference: /* a reference */
2970 case tcc_vl_exp: /* a function call */
2971 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
2972 for (i = 0; i < len; ++i)
2974 tree op = TREE_OPERAND (t, i);
2975 if (op && TREE_SIDE_EFFECTS (op))
2976 TREE_SIDE_EFFECTS (t) = 1;
2978 break;
2980 case tcc_constant:
2981 /* No side-effects. */
2982 return;
2984 default:
2985 gcc_unreachable ();
2989 /* Canonicalize a tree T for use in a COND_EXPR as conditional. Returns
2990 a canonicalized tree that is valid for a COND_EXPR or NULL_TREE, if
2991 we failed to create one. */
2993 tree
2994 canonicalize_cond_expr_cond (tree t)
2996 /* For (bool)x use x != 0. */
2997 if (TREE_CODE (t) == NOP_EXPR
2998 && TREE_TYPE (t) == boolean_type_node)
3000 tree top0 = TREE_OPERAND (t, 0);
3001 t = build2 (NE_EXPR, TREE_TYPE (t),
3002 top0, build_int_cst (TREE_TYPE (top0), 0));
3004 /* For !x use x == 0. */
3005 else if (TREE_CODE (t) == TRUTH_NOT_EXPR)
3007 tree top0 = TREE_OPERAND (t, 0);
3008 t = build2 (EQ_EXPR, TREE_TYPE (t),
3009 top0, build_int_cst (TREE_TYPE (top0), 0));
3011 /* For cmp ? 1 : 0 use cmp. */
3012 else if (TREE_CODE (t) == COND_EXPR
3013 && COMPARISON_CLASS_P (TREE_OPERAND (t, 0))
3014 && integer_onep (TREE_OPERAND (t, 1))
3015 && integer_zerop (TREE_OPERAND (t, 2)))
3017 tree top0 = TREE_OPERAND (t, 0);
3018 t = build2 (TREE_CODE (top0), TREE_TYPE (t),
3019 TREE_OPERAND (top0, 0), TREE_OPERAND (top0, 1));
3022 if (is_gimple_condexpr (t))
3023 return t;
3025 return NULL_TREE;
3028 /* Build a GIMPLE_CALL identical to STMT but skipping the arguments in
3029 the positions marked by the set ARGS_TO_SKIP. */
3031 gimple
3032 gimple_call_copy_skip_args (gimple stmt, bitmap args_to_skip)
3034 int i;
3035 tree fn = gimple_call_fn (stmt);
3036 int nargs = gimple_call_num_args (stmt);
3037 VEC(tree, heap) *vargs = VEC_alloc (tree, heap, nargs);
3038 gimple new_stmt;
3040 for (i = 0; i < nargs; i++)
3041 if (!bitmap_bit_p (args_to_skip, i))
3042 VEC_quick_push (tree, vargs, gimple_call_arg (stmt, i));
3044 new_stmt = gimple_build_call_vec (fn, vargs);
3045 VEC_free (tree, heap, vargs);
3046 if (gimple_call_lhs (stmt))
3047 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
3049 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
3050 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
3052 gimple_set_block (new_stmt, gimple_block (stmt));
3053 if (gimple_has_location (stmt))
3054 gimple_set_location (new_stmt, gimple_location (stmt));
3056 /* Carry all the flags to the new GIMPLE_CALL. */
3057 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
3058 gimple_call_set_tail (new_stmt, gimple_call_tail_p (stmt));
3059 gimple_call_set_cannot_inline (new_stmt, gimple_call_cannot_inline_p (stmt));
3060 gimple_call_set_return_slot_opt (new_stmt, gimple_call_return_slot_opt_p (stmt));
3061 gimple_call_set_from_thunk (new_stmt, gimple_call_from_thunk_p (stmt));
3062 gimple_call_set_va_arg_pack (new_stmt, gimple_call_va_arg_pack_p (stmt));
3064 gimple_set_modified (new_stmt, true);
3066 return new_stmt;
3070 /* Data structure used to count the number of dereferences to PTR
3071 inside an expression. */
3072 struct count_ptr_d
3074 tree ptr;
3075 unsigned num_stores;
3076 unsigned num_loads;
3079 /* Helper for count_uses_and_derefs. Called by walk_tree to look for
3080 (ALIGN/MISALIGNED_)INDIRECT_REF nodes for the pointer passed in DATA. */
3082 static tree
3083 count_ptr_derefs (tree *tp, int *walk_subtrees, void *data)
3085 struct walk_stmt_info *wi_p = (struct walk_stmt_info *) data;
3086 struct count_ptr_d *count_p = (struct count_ptr_d *) wi_p->info;
3088 /* Do not walk inside ADDR_EXPR nodes. In the expression &ptr->fld,
3089 pointer 'ptr' is *not* dereferenced, it is simply used to compute
3090 the address of 'fld' as 'ptr + offsetof(fld)'. */
3091 if (TREE_CODE (*tp) == ADDR_EXPR)
3093 *walk_subtrees = 0;
3094 return NULL_TREE;
3097 if (INDIRECT_REF_P (*tp) && TREE_OPERAND (*tp, 0) == count_p->ptr)
3099 if (wi_p->is_lhs)
3100 count_p->num_stores++;
3101 else
3102 count_p->num_loads++;
3105 return NULL_TREE;
3108 /* Count the number of direct and indirect uses for pointer PTR in
3109 statement STMT. The number of direct uses is stored in
3110 *NUM_USES_P. Indirect references are counted separately depending
3111 on whether they are store or load operations. The counts are
3112 stored in *NUM_STORES_P and *NUM_LOADS_P. */
3114 void
3115 count_uses_and_derefs (tree ptr, gimple stmt, unsigned *num_uses_p,
3116 unsigned *num_loads_p, unsigned *num_stores_p)
3118 ssa_op_iter i;
3119 tree use;
3121 *num_uses_p = 0;
3122 *num_loads_p = 0;
3123 *num_stores_p = 0;
3125 /* Find out the total number of uses of PTR in STMT. */
3126 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
3127 if (use == ptr)
3128 (*num_uses_p)++;
3130 /* Now count the number of indirect references to PTR. This is
3131 truly awful, but we don't have much choice. There are no parent
3132 pointers inside INDIRECT_REFs, so an expression like
3133 '*x_1 = foo (x_1, *x_1)' needs to be traversed piece by piece to
3134 find all the indirect and direct uses of x_1 inside. The only
3135 shortcut we can take is the fact that GIMPLE only allows
3136 INDIRECT_REFs inside the expressions below. */
3137 if (is_gimple_assign (stmt)
3138 || gimple_code (stmt) == GIMPLE_RETURN
3139 || gimple_code (stmt) == GIMPLE_ASM
3140 || is_gimple_call (stmt))
3142 struct walk_stmt_info wi;
3143 struct count_ptr_d count;
3145 count.ptr = ptr;
3146 count.num_stores = 0;
3147 count.num_loads = 0;
3149 memset (&wi, 0, sizeof (wi));
3150 wi.info = &count;
3151 walk_gimple_op (stmt, count_ptr_derefs, &wi);
3153 *num_stores_p = count.num_stores;
3154 *num_loads_p = count.num_loads;
3157 gcc_assert (*num_uses_p >= *num_loads_p + *num_stores_p);
3160 /* From a tree operand OP return the base of a load or store operation
3161 or NULL_TREE if OP is not a load or a store. */
3163 static tree
3164 get_base_loadstore (tree op)
3166 while (handled_component_p (op))
3167 op = TREE_OPERAND (op, 0);
3168 if (DECL_P (op)
3169 || INDIRECT_REF_P (op)
3170 || TREE_CODE (op) == TARGET_MEM_REF)
3171 return op;
3172 return NULL_TREE;
3175 /* For the statement STMT call the callbacks VISIT_LOAD, VISIT_STORE and
3176 VISIT_ADDR if non-NULL on loads, store and address-taken operands
3177 passing the STMT, the base of the operand and DATA to it. The base
3178 will be either a decl, an indirect reference (including TARGET_MEM_REF)
3179 or the argument of an address expression.
3180 Returns the results of these callbacks or'ed. */
3182 bool
3183 walk_stmt_load_store_addr_ops (gimple stmt, void *data,
3184 bool (*visit_load)(gimple, tree, void *),
3185 bool (*visit_store)(gimple, tree, void *),
3186 bool (*visit_addr)(gimple, tree, void *))
3188 bool ret = false;
3189 unsigned i;
3190 if (gimple_assign_single_p (stmt))
3192 tree lhs, rhs;
3193 if (visit_store)
3195 lhs = get_base_loadstore (gimple_assign_lhs (stmt));
3196 if (lhs)
3197 ret |= visit_store (stmt, lhs, data);
3199 rhs = gimple_assign_rhs1 (stmt);
3200 while (handled_component_p (rhs))
3201 rhs = TREE_OPERAND (rhs, 0);
3202 if (visit_addr)
3204 if (TREE_CODE (rhs) == ADDR_EXPR)
3205 ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), data);
3206 else if (TREE_CODE (rhs) == TARGET_MEM_REF
3207 && TMR_BASE (rhs) != NULL_TREE
3208 && TREE_CODE (TMR_BASE (rhs)) == ADDR_EXPR)
3209 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (rhs), 0), data);
3210 else if (TREE_CODE (rhs) == OBJ_TYPE_REF
3211 && TREE_CODE (OBJ_TYPE_REF_OBJECT (rhs)) == ADDR_EXPR)
3212 ret |= visit_addr (stmt, TREE_OPERAND (OBJ_TYPE_REF_OBJECT (rhs),
3213 0), data);
3214 lhs = gimple_assign_lhs (stmt);
3215 if (TREE_CODE (lhs) == TARGET_MEM_REF
3216 && TMR_BASE (lhs) != NULL_TREE
3217 && TREE_CODE (TMR_BASE (lhs)) == ADDR_EXPR)
3218 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (lhs), 0), data);
3220 if (visit_load)
3222 rhs = get_base_loadstore (rhs);
3223 if (rhs)
3224 ret |= visit_load (stmt, rhs, data);
3227 else if (visit_addr
3228 && (is_gimple_assign (stmt)
3229 || gimple_code (stmt) == GIMPLE_COND))
3231 for (i = 0; i < gimple_num_ops (stmt); ++i)
3232 if (gimple_op (stmt, i)
3233 && TREE_CODE (gimple_op (stmt, i)) == ADDR_EXPR)
3234 ret |= visit_addr (stmt, TREE_OPERAND (gimple_op (stmt, i), 0), data);
3236 else if (is_gimple_call (stmt))
3238 if (visit_store)
3240 tree lhs = gimple_call_lhs (stmt);
3241 if (lhs)
3243 lhs = get_base_loadstore (lhs);
3244 if (lhs)
3245 ret |= visit_store (stmt, lhs, data);
3248 if (visit_load || visit_addr)
3249 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3251 tree rhs = gimple_call_arg (stmt, i);
3252 if (visit_addr
3253 && TREE_CODE (rhs) == ADDR_EXPR)
3254 ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), data);
3255 else if (visit_load)
3257 rhs = get_base_loadstore (rhs);
3258 if (rhs)
3259 ret |= visit_load (stmt, rhs, data);
3262 if (visit_addr
3263 && gimple_call_chain (stmt)
3264 && TREE_CODE (gimple_call_chain (stmt)) == ADDR_EXPR)
3265 ret |= visit_addr (stmt, TREE_OPERAND (gimple_call_chain (stmt), 0),
3266 data);
3268 else if (gimple_code (stmt) == GIMPLE_ASM)
3270 unsigned noutputs;
3271 const char *constraint;
3272 const char **oconstraints;
3273 bool allows_mem, allows_reg, is_inout;
3274 noutputs = gimple_asm_noutputs (stmt);
3275 oconstraints = XALLOCAVEC (const char *, noutputs);
3276 if (visit_store || visit_addr)
3277 for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
3279 tree link = gimple_asm_output_op (stmt, i);
3280 tree op = get_base_loadstore (TREE_VALUE (link));
3281 if (op && visit_store)
3282 ret |= visit_store (stmt, op, data);
3283 if (visit_addr)
3285 constraint = TREE_STRING_POINTER
3286 (TREE_VALUE (TREE_PURPOSE (link)));
3287 oconstraints[i] = constraint;
3288 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
3289 &allows_reg, &is_inout);
3290 if (op && !allows_reg && allows_mem)
3291 ret |= visit_addr (stmt, op, data);
3294 if (visit_load || visit_addr)
3295 for (i = 0; i < gimple_asm_ninputs (stmt); ++i)
3297 tree link = gimple_asm_input_op (stmt, i);
3298 tree op = TREE_VALUE (link);
3299 if (visit_addr
3300 && TREE_CODE (op) == ADDR_EXPR)
3301 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
3302 else if (visit_load || visit_addr)
3304 op = get_base_loadstore (op);
3305 if (op)
3307 if (visit_load)
3308 ret |= visit_load (stmt, op, data);
3309 if (visit_addr)
3311 constraint = TREE_STRING_POINTER
3312 (TREE_VALUE (TREE_PURPOSE (link)));
3313 parse_input_constraint (&constraint, 0, 0, noutputs,
3314 0, oconstraints,
3315 &allows_mem, &allows_reg);
3316 if (!allows_reg && allows_mem)
3317 ret |= visit_addr (stmt, op, data);
3323 else if (gimple_code (stmt) == GIMPLE_RETURN)
3325 tree op = gimple_return_retval (stmt);
3326 if (op)
3328 if (visit_addr
3329 && TREE_CODE (op) == ADDR_EXPR)
3330 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
3331 else if (visit_load)
3333 op = get_base_loadstore (op);
3334 if (op)
3335 ret |= visit_load (stmt, op, data);
3339 else if (visit_addr
3340 && gimple_code (stmt) == GIMPLE_PHI)
3342 for (i = 0; i < gimple_phi_num_args (stmt); ++i)
3344 tree op = PHI_ARG_DEF (stmt, i);
3345 if (TREE_CODE (op) == ADDR_EXPR)
3346 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
3350 return ret;
3353 /* Like walk_stmt_load_store_addr_ops but with NULL visit_addr. IPA-CP
3354 should make a faster clone for this case. */
3356 bool
3357 walk_stmt_load_store_ops (gimple stmt, void *data,
3358 bool (*visit_load)(gimple, tree, void *),
3359 bool (*visit_store)(gimple, tree, void *))
3361 return walk_stmt_load_store_addr_ops (stmt, data,
3362 visit_load, visit_store, NULL);
3365 /* Helper for gimple_ior_addresses_taken_1. */
3367 static bool
3368 gimple_ior_addresses_taken_1 (gimple stmt ATTRIBUTE_UNUSED,
3369 tree addr, void *data)
3371 bitmap addresses_taken = (bitmap)data;
3372 while (handled_component_p (addr))
3373 addr = TREE_OPERAND (addr, 0);
3374 if (DECL_P (addr))
3376 bitmap_set_bit (addresses_taken, DECL_UID (addr));
3377 return true;
3379 return false;
3382 /* Set the bit for the uid of all decls that have their address taken
3383 in STMT in the ADDRESSES_TAKEN bitmap. Returns true if there
3384 were any in this stmt. */
3386 bool
3387 gimple_ior_addresses_taken (bitmap addresses_taken, gimple stmt)
3389 return walk_stmt_load_store_addr_ops (stmt, addresses_taken, NULL, NULL,
3390 gimple_ior_addresses_taken_1);
3393 #include "gt-gimple.h"