2009-08-22 Steven K. kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / gimple.c
blobaa30a2e31c0cd2090a67cc6abba10bf2104f1573
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));
360 gimple_set_no_warning (call, TREE_NO_WARNING (t));
362 return call;
366 /* Extract the operands and code for expression EXPR into *SUBCODE_P,
367 *OP1_P and *OP2_P respectively. */
369 void
370 extract_ops_from_tree (tree expr, enum tree_code *subcode_p, tree *op1_p,
371 tree *op2_p)
373 enum gimple_rhs_class grhs_class;
375 *subcode_p = TREE_CODE (expr);
376 grhs_class = get_gimple_rhs_class (*subcode_p);
378 if (grhs_class == GIMPLE_BINARY_RHS)
380 *op1_p = TREE_OPERAND (expr, 0);
381 *op2_p = TREE_OPERAND (expr, 1);
383 else if (grhs_class == GIMPLE_UNARY_RHS)
385 *op1_p = TREE_OPERAND (expr, 0);
386 *op2_p = NULL_TREE;
388 else if (grhs_class == GIMPLE_SINGLE_RHS)
390 *op1_p = expr;
391 *op2_p = NULL_TREE;
393 else
394 gcc_unreachable ();
398 /* Build a GIMPLE_ASSIGN statement.
400 LHS of the assignment.
401 RHS of the assignment which can be unary or binary. */
403 gimple
404 gimple_build_assign_stat (tree lhs, tree rhs MEM_STAT_DECL)
406 enum tree_code subcode;
407 tree op1, op2;
409 extract_ops_from_tree (rhs, &subcode, &op1, &op2);
410 return gimple_build_assign_with_ops_stat (subcode, lhs, op1, op2
411 PASS_MEM_STAT);
415 /* Build a GIMPLE_ASSIGN statement with sub-code SUBCODE and operands
416 OP1 and OP2. If OP2 is NULL then SUBCODE must be of class
417 GIMPLE_UNARY_RHS or GIMPLE_SINGLE_RHS. */
419 gimple
420 gimple_build_assign_with_ops_stat (enum tree_code subcode, tree lhs, tree op1,
421 tree op2 MEM_STAT_DECL)
423 unsigned num_ops;
424 gimple p;
426 /* Need 1 operand for LHS and 1 or 2 for the RHS (depending on the
427 code). */
428 num_ops = get_gimple_rhs_num_ops (subcode) + 1;
430 p = gimple_build_with_ops_stat (GIMPLE_ASSIGN, subcode, num_ops
431 PASS_MEM_STAT);
432 gimple_assign_set_lhs (p, lhs);
433 gimple_assign_set_rhs1 (p, op1);
434 if (op2)
436 gcc_assert (num_ops > 2);
437 gimple_assign_set_rhs2 (p, op2);
440 return p;
444 /* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
446 DST/SRC are the destination and source respectively. You can pass
447 ungimplified trees in DST or SRC, in which case they will be
448 converted to a gimple operand if necessary.
450 This function returns the newly created GIMPLE_ASSIGN tuple. */
452 gimple
453 gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
455 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
456 gimplify_and_add (t, seq_p);
457 ggc_free (t);
458 return gimple_seq_last_stmt (*seq_p);
462 /* Build a GIMPLE_COND statement.
464 PRED is the condition used to compare LHS and the RHS.
465 T_LABEL is the label to jump to if the condition is true.
466 F_LABEL is the label to jump to otherwise. */
468 gimple
469 gimple_build_cond (enum tree_code pred_code, tree lhs, tree rhs,
470 tree t_label, tree f_label)
472 gimple p;
474 gcc_assert (TREE_CODE_CLASS (pred_code) == tcc_comparison);
475 p = gimple_build_with_ops (GIMPLE_COND, pred_code, 4);
476 gimple_cond_set_lhs (p, lhs);
477 gimple_cond_set_rhs (p, rhs);
478 gimple_cond_set_true_label (p, t_label);
479 gimple_cond_set_false_label (p, f_label);
480 return p;
484 /* Extract operands for a GIMPLE_COND statement out of COND_EXPR tree COND. */
486 void
487 gimple_cond_get_ops_from_tree (tree cond, enum tree_code *code_p,
488 tree *lhs_p, tree *rhs_p)
490 location_t loc = EXPR_LOCATION (cond);
491 gcc_assert (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison
492 || TREE_CODE (cond) == TRUTH_NOT_EXPR
493 || is_gimple_min_invariant (cond)
494 || SSA_VAR_P (cond));
496 extract_ops_from_tree (cond, code_p, lhs_p, rhs_p);
498 /* Canonicalize conditionals of the form 'if (!VAL)'. */
499 if (*code_p == TRUTH_NOT_EXPR)
501 *code_p = EQ_EXPR;
502 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
503 *rhs_p = fold_convert_loc (loc, TREE_TYPE (*lhs_p), integer_zero_node);
505 /* Canonicalize conditionals of the form 'if (VAL)' */
506 else if (TREE_CODE_CLASS (*code_p) != tcc_comparison)
508 *code_p = NE_EXPR;
509 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
510 *rhs_p = fold_convert_loc (loc, TREE_TYPE (*lhs_p), integer_zero_node);
515 /* Build a GIMPLE_COND statement from the conditional expression tree
516 COND. T_LABEL and F_LABEL are as in gimple_build_cond. */
518 gimple
519 gimple_build_cond_from_tree (tree cond, tree t_label, tree f_label)
521 enum tree_code code;
522 tree lhs, rhs;
524 gimple_cond_get_ops_from_tree (cond, &code, &lhs, &rhs);
525 return gimple_build_cond (code, lhs, rhs, t_label, f_label);
528 /* Set code, lhs, and rhs of a GIMPLE_COND from a suitable
529 boolean expression tree COND. */
531 void
532 gimple_cond_set_condition_from_tree (gimple stmt, tree cond)
534 enum tree_code code;
535 tree lhs, rhs;
537 gimple_cond_get_ops_from_tree (cond, &code, &lhs, &rhs);
538 gimple_cond_set_condition (stmt, code, lhs, rhs);
541 /* Build a GIMPLE_LABEL statement for LABEL. */
543 gimple
544 gimple_build_label (tree label)
546 gimple p = gimple_build_with_ops (GIMPLE_LABEL, ERROR_MARK, 1);
547 gimple_label_set_label (p, label);
548 return p;
551 /* Build a GIMPLE_GOTO statement to label DEST. */
553 gimple
554 gimple_build_goto (tree dest)
556 gimple p = gimple_build_with_ops (GIMPLE_GOTO, ERROR_MARK, 1);
557 gimple_goto_set_dest (p, dest);
558 return p;
562 /* Build a GIMPLE_NOP statement. */
564 gimple
565 gimple_build_nop (void)
567 return gimple_alloc (GIMPLE_NOP, 0);
571 /* Build a GIMPLE_BIND statement.
572 VARS are the variables in BODY.
573 BLOCK is the containing block. */
575 gimple
576 gimple_build_bind (tree vars, gimple_seq body, tree block)
578 gimple p = gimple_alloc (GIMPLE_BIND, 0);
579 gimple_bind_set_vars (p, vars);
580 if (body)
581 gimple_bind_set_body (p, body);
582 if (block)
583 gimple_bind_set_block (p, block);
584 return p;
587 /* Helper function to set the simple fields of a asm stmt.
589 STRING is a pointer to a string that is the asm blocks assembly code.
590 NINPUT is the number of register inputs.
591 NOUTPUT is the number of register outputs.
592 NCLOBBERS is the number of clobbered registers.
595 static inline gimple
596 gimple_build_asm_1 (const char *string, unsigned ninputs, unsigned noutputs,
597 unsigned nclobbers)
599 gimple p;
600 int size = strlen (string);
602 p = gimple_build_with_ops (GIMPLE_ASM, ERROR_MARK,
603 ninputs + noutputs + nclobbers);
605 p->gimple_asm.ni = ninputs;
606 p->gimple_asm.no = noutputs;
607 p->gimple_asm.nc = nclobbers;
608 p->gimple_asm.string = ggc_alloc_string (string, size);
610 #ifdef GATHER_STATISTICS
611 gimple_alloc_sizes[(int) gimple_alloc_kind (GIMPLE_ASM)] += size;
612 #endif
614 return p;
617 /* Build a GIMPLE_ASM statement.
619 STRING is the assembly code.
620 NINPUT is the number of register inputs.
621 NOUTPUT is the number of register outputs.
622 NCLOBBERS is the number of clobbered registers.
623 INPUTS is a vector of the input register parameters.
624 OUTPUTS is a vector of the output register parameters.
625 CLOBBERS is a vector of the clobbered register parameters. */
627 gimple
628 gimple_build_asm_vec (const char *string, VEC(tree,gc)* inputs,
629 VEC(tree,gc)* outputs, VEC(tree,gc)* clobbers)
631 gimple p;
632 unsigned i;
634 p = gimple_build_asm_1 (string,
635 VEC_length (tree, inputs),
636 VEC_length (tree, outputs),
637 VEC_length (tree, clobbers));
639 for (i = 0; i < VEC_length (tree, inputs); i++)
640 gimple_asm_set_input_op (p, i, VEC_index (tree, inputs, i));
642 for (i = 0; i < VEC_length (tree, outputs); i++)
643 gimple_asm_set_output_op (p, i, VEC_index (tree, outputs, i));
645 for (i = 0; i < VEC_length (tree, clobbers); i++)
646 gimple_asm_set_clobber_op (p, i, VEC_index (tree, clobbers, i));
648 return p;
651 /* Build a GIMPLE_ASM statement.
653 STRING is the assembly code.
654 NINPUT is the number of register inputs.
655 NOUTPUT is the number of register outputs.
656 NCLOBBERS is the number of clobbered registers.
657 ... are trees for each input, output and clobbered register. */
659 gimple
660 gimple_build_asm (const char *string, unsigned ninputs, unsigned noutputs,
661 unsigned nclobbers, ...)
663 gimple p;
664 unsigned i;
665 va_list ap;
667 p = gimple_build_asm_1 (string, ninputs, noutputs, nclobbers);
669 va_start (ap, nclobbers);
671 for (i = 0; i < ninputs; i++)
672 gimple_asm_set_input_op (p, i, va_arg (ap, tree));
674 for (i = 0; i < noutputs; i++)
675 gimple_asm_set_output_op (p, i, va_arg (ap, tree));
677 for (i = 0; i < nclobbers; i++)
678 gimple_asm_set_clobber_op (p, i, va_arg (ap, tree));
680 va_end (ap);
682 return p;
685 /* Build a GIMPLE_CATCH statement.
687 TYPES are the catch types.
688 HANDLER is the exception handler. */
690 gimple
691 gimple_build_catch (tree types, gimple_seq handler)
693 gimple p = gimple_alloc (GIMPLE_CATCH, 0);
694 gimple_catch_set_types (p, types);
695 if (handler)
696 gimple_catch_set_handler (p, handler);
698 return p;
701 /* Build a GIMPLE_EH_FILTER statement.
703 TYPES are the filter's types.
704 FAILURE is the filter's failure action. */
706 gimple
707 gimple_build_eh_filter (tree types, gimple_seq failure)
709 gimple p = gimple_alloc (GIMPLE_EH_FILTER, 0);
710 gimple_eh_filter_set_types (p, types);
711 if (failure)
712 gimple_eh_filter_set_failure (p, failure);
714 return p;
717 /* Build a GIMPLE_TRY statement.
719 EVAL is the expression to evaluate.
720 CLEANUP is the cleanup expression.
721 KIND is either GIMPLE_TRY_CATCH or GIMPLE_TRY_FINALLY depending on
722 whether this is a try/catch or a try/finally respectively. */
724 gimple
725 gimple_build_try (gimple_seq eval, gimple_seq cleanup,
726 enum gimple_try_flags kind)
728 gimple p;
730 gcc_assert (kind == GIMPLE_TRY_CATCH || kind == GIMPLE_TRY_FINALLY);
731 p = gimple_alloc (GIMPLE_TRY, 0);
732 gimple_set_subcode (p, kind);
733 if (eval)
734 gimple_try_set_eval (p, eval);
735 if (cleanup)
736 gimple_try_set_cleanup (p, cleanup);
738 return p;
741 /* Construct a GIMPLE_WITH_CLEANUP_EXPR statement.
743 CLEANUP is the cleanup expression. */
745 gimple
746 gimple_build_wce (gimple_seq cleanup)
748 gimple p = gimple_alloc (GIMPLE_WITH_CLEANUP_EXPR, 0);
749 if (cleanup)
750 gimple_wce_set_cleanup (p, cleanup);
752 return p;
756 /* Build a GIMPLE_RESX statement.
758 REGION is the region number from which this resx causes control flow to
759 leave. */
761 gimple
762 gimple_build_resx (int region)
764 gimple p = gimple_alloc (GIMPLE_RESX, 0);
765 gimple_resx_set_region (p, region);
766 return p;
770 /* The helper for constructing a gimple switch statement.
771 INDEX is the switch's index.
772 NLABELS is the number of labels in the switch excluding the default.
773 DEFAULT_LABEL is the default label for the switch statement. */
775 static inline gimple
776 gimple_build_switch_1 (unsigned nlabels, tree index, tree default_label)
778 /* nlabels + 1 default label + 1 index. */
779 gimple p = gimple_build_with_ops (GIMPLE_SWITCH, ERROR_MARK,
780 nlabels + 1 + 1);
781 gimple_switch_set_index (p, index);
782 gimple_switch_set_default_label (p, default_label);
783 return p;
787 /* Build a GIMPLE_SWITCH statement.
789 INDEX is the switch's index.
790 NLABELS is the number of labels in the switch excluding the DEFAULT_LABEL.
791 ... are the labels excluding the default. */
793 gimple
794 gimple_build_switch (unsigned nlabels, tree index, tree default_label, ...)
796 va_list al;
797 unsigned i;
798 gimple p;
800 p = gimple_build_switch_1 (nlabels, index, default_label);
802 /* Store the rest of the labels. */
803 va_start (al, default_label);
804 for (i = 1; i <= nlabels; i++)
805 gimple_switch_set_label (p, i, va_arg (al, tree));
806 va_end (al);
808 return p;
812 /* Build a GIMPLE_SWITCH statement.
814 INDEX is the switch's index.
815 DEFAULT_LABEL is the default label
816 ARGS is a vector of labels excluding the default. */
818 gimple
819 gimple_build_switch_vec (tree index, tree default_label, VEC(tree, heap) *args)
821 unsigned i;
822 unsigned nlabels = VEC_length (tree, args);
823 gimple p = gimple_build_switch_1 (nlabels, index, default_label);
825 /* Put labels in labels[1 - (nlabels + 1)].
826 Default label is in labels[0]. */
827 for (i = 1; i <= nlabels; i++)
828 gimple_switch_set_label (p, i, VEC_index (tree, args, i - 1));
830 return p;
834 /* Build a GIMPLE_OMP_CRITICAL statement.
836 BODY is the sequence of statements for which only one thread can execute.
837 NAME is optional identifier for this critical block. */
839 gimple
840 gimple_build_omp_critical (gimple_seq body, tree name)
842 gimple p = gimple_alloc (GIMPLE_OMP_CRITICAL, 0);
843 gimple_omp_critical_set_name (p, name);
844 if (body)
845 gimple_omp_set_body (p, body);
847 return p;
850 /* Build a GIMPLE_OMP_FOR statement.
852 BODY is sequence of statements inside the for loop.
853 CLAUSES, are any of the OMP loop construct's clauses: private, firstprivate,
854 lastprivate, reductions, ordered, schedule, and nowait.
855 COLLAPSE is the collapse count.
856 PRE_BODY is the sequence of statements that are loop invariant. */
858 gimple
859 gimple_build_omp_for (gimple_seq body, tree clauses, size_t collapse,
860 gimple_seq pre_body)
862 gimple p = gimple_alloc (GIMPLE_OMP_FOR, 0);
863 if (body)
864 gimple_omp_set_body (p, body);
865 gimple_omp_for_set_clauses (p, clauses);
866 p->gimple_omp_for.collapse = collapse;
867 p->gimple_omp_for.iter = GGC_CNEWVEC (struct gimple_omp_for_iter, collapse);
868 if (pre_body)
869 gimple_omp_for_set_pre_body (p, pre_body);
871 return p;
875 /* Build a GIMPLE_OMP_PARALLEL statement.
877 BODY is sequence of statements which are executed in parallel.
878 CLAUSES, are the OMP parallel construct's clauses.
879 CHILD_FN is the function created for the parallel threads to execute.
880 DATA_ARG are the shared data argument(s). */
882 gimple
883 gimple_build_omp_parallel (gimple_seq body, tree clauses, tree child_fn,
884 tree data_arg)
886 gimple p = gimple_alloc (GIMPLE_OMP_PARALLEL, 0);
887 if (body)
888 gimple_omp_set_body (p, body);
889 gimple_omp_parallel_set_clauses (p, clauses);
890 gimple_omp_parallel_set_child_fn (p, child_fn);
891 gimple_omp_parallel_set_data_arg (p, data_arg);
893 return p;
897 /* Build a GIMPLE_OMP_TASK statement.
899 BODY is sequence of statements which are executed by the explicit task.
900 CLAUSES, are the OMP parallel construct's clauses.
901 CHILD_FN is the function created for the parallel threads to execute.
902 DATA_ARG are the shared data argument(s).
903 COPY_FN is the optional function for firstprivate initialization.
904 ARG_SIZE and ARG_ALIGN are size and alignment of the data block. */
906 gimple
907 gimple_build_omp_task (gimple_seq body, tree clauses, tree child_fn,
908 tree data_arg, tree copy_fn, tree arg_size,
909 tree arg_align)
911 gimple p = gimple_alloc (GIMPLE_OMP_TASK, 0);
912 if (body)
913 gimple_omp_set_body (p, body);
914 gimple_omp_task_set_clauses (p, clauses);
915 gimple_omp_task_set_child_fn (p, child_fn);
916 gimple_omp_task_set_data_arg (p, data_arg);
917 gimple_omp_task_set_copy_fn (p, copy_fn);
918 gimple_omp_task_set_arg_size (p, arg_size);
919 gimple_omp_task_set_arg_align (p, arg_align);
921 return p;
925 /* Build a GIMPLE_OMP_SECTION statement for a sections statement.
927 BODY is the sequence of statements in the section. */
929 gimple
930 gimple_build_omp_section (gimple_seq body)
932 gimple p = gimple_alloc (GIMPLE_OMP_SECTION, 0);
933 if (body)
934 gimple_omp_set_body (p, body);
936 return p;
940 /* Build a GIMPLE_OMP_MASTER statement.
942 BODY is the sequence of statements to be executed by just the master. */
944 gimple
945 gimple_build_omp_master (gimple_seq body)
947 gimple p = gimple_alloc (GIMPLE_OMP_MASTER, 0);
948 if (body)
949 gimple_omp_set_body (p, body);
951 return p;
955 /* Build a GIMPLE_OMP_CONTINUE statement.
957 CONTROL_DEF is the definition of the control variable.
958 CONTROL_USE is the use of the control variable. */
960 gimple
961 gimple_build_omp_continue (tree control_def, tree control_use)
963 gimple p = gimple_alloc (GIMPLE_OMP_CONTINUE, 0);
964 gimple_omp_continue_set_control_def (p, control_def);
965 gimple_omp_continue_set_control_use (p, control_use);
966 return p;
969 /* Build a GIMPLE_OMP_ORDERED statement.
971 BODY is the sequence of statements inside a loop that will executed in
972 sequence. */
974 gimple
975 gimple_build_omp_ordered (gimple_seq body)
977 gimple p = gimple_alloc (GIMPLE_OMP_ORDERED, 0);
978 if (body)
979 gimple_omp_set_body (p, body);
981 return p;
985 /* Build a GIMPLE_OMP_RETURN statement.
986 WAIT_P is true if this is a non-waiting return. */
988 gimple
989 gimple_build_omp_return (bool wait_p)
991 gimple p = gimple_alloc (GIMPLE_OMP_RETURN, 0);
992 if (wait_p)
993 gimple_omp_return_set_nowait (p);
995 return p;
999 /* Build a GIMPLE_OMP_SECTIONS statement.
1001 BODY is a sequence of section statements.
1002 CLAUSES are any of the OMP sections contsruct's clauses: private,
1003 firstprivate, lastprivate, reduction, and nowait. */
1005 gimple
1006 gimple_build_omp_sections (gimple_seq body, tree clauses)
1008 gimple p = gimple_alloc (GIMPLE_OMP_SECTIONS, 0);
1009 if (body)
1010 gimple_omp_set_body (p, body);
1011 gimple_omp_sections_set_clauses (p, clauses);
1013 return p;
1017 /* Build a GIMPLE_OMP_SECTIONS_SWITCH. */
1019 gimple
1020 gimple_build_omp_sections_switch (void)
1022 return gimple_alloc (GIMPLE_OMP_SECTIONS_SWITCH, 0);
1026 /* Build a GIMPLE_OMP_SINGLE statement.
1028 BODY is the sequence of statements that will be executed once.
1029 CLAUSES are any of the OMP single construct's clauses: private, firstprivate,
1030 copyprivate, nowait. */
1032 gimple
1033 gimple_build_omp_single (gimple_seq body, tree clauses)
1035 gimple p = gimple_alloc (GIMPLE_OMP_SINGLE, 0);
1036 if (body)
1037 gimple_omp_set_body (p, body);
1038 gimple_omp_single_set_clauses (p, clauses);
1040 return p;
1044 /* Build a GIMPLE_OMP_ATOMIC_LOAD statement. */
1046 gimple
1047 gimple_build_omp_atomic_load (tree lhs, tree rhs)
1049 gimple p = gimple_alloc (GIMPLE_OMP_ATOMIC_LOAD, 0);
1050 gimple_omp_atomic_load_set_lhs (p, lhs);
1051 gimple_omp_atomic_load_set_rhs (p, rhs);
1052 return p;
1055 /* Build a GIMPLE_OMP_ATOMIC_STORE statement.
1057 VAL is the value we are storing. */
1059 gimple
1060 gimple_build_omp_atomic_store (tree val)
1062 gimple p = gimple_alloc (GIMPLE_OMP_ATOMIC_STORE, 0);
1063 gimple_omp_atomic_store_set_val (p, val);
1064 return p;
1067 /* Build a GIMPLE_PREDICT statement. PREDICT is one of the predictors from
1068 predict.def, OUTCOME is NOT_TAKEN or TAKEN. */
1070 gimple
1071 gimple_build_predict (enum br_predictor predictor, enum prediction outcome)
1073 gimple p = gimple_alloc (GIMPLE_PREDICT, 0);
1074 /* Ensure all the predictors fit into the lower bits of the subcode. */
1075 gcc_assert ((int) END_PREDICTORS <= GF_PREDICT_TAKEN);
1076 gimple_predict_set_predictor (p, predictor);
1077 gimple_predict_set_outcome (p, outcome);
1078 return p;
1081 /* Return which gimple structure is used by T. The enums here are defined
1082 in gsstruct.def. */
1084 enum gimple_statement_structure_enum
1085 gimple_statement_structure (gimple gs)
1087 return gss_for_code (gimple_code (gs));
1090 #if defined ENABLE_GIMPLE_CHECKING
1091 /* Complain of a gimple type mismatch and die. */
1093 void
1094 gimple_check_failed (const_gimple gs, const char *file, int line,
1095 const char *function, enum gimple_code code,
1096 enum tree_code subcode)
1098 internal_error ("gimple check: expected %s(%s), have %s(%s) in %s, at %s:%d",
1099 gimple_code_name[code],
1100 tree_code_name[subcode],
1101 gimple_code_name[gimple_code (gs)],
1102 gs->gsbase.subcode > 0
1103 ? tree_code_name[gs->gsbase.subcode]
1104 : "",
1105 function, trim_filename (file), line);
1107 #endif /* ENABLE_GIMPLE_CHECKING */
1110 /* Allocate a new GIMPLE sequence in GC memory and return it. If
1111 there are free sequences in GIMPLE_SEQ_CACHE return one of those
1112 instead. */
1114 gimple_seq
1115 gimple_seq_alloc (void)
1117 gimple_seq seq = gimple_seq_cache;
1118 if (seq)
1120 gimple_seq_cache = gimple_seq_cache->next_free;
1121 gcc_assert (gimple_seq_cache != seq);
1122 memset (seq, 0, sizeof (*seq));
1124 else
1126 seq = (gimple_seq) ggc_alloc_cleared (sizeof (*seq));
1127 #ifdef GATHER_STATISTICS
1128 gimple_alloc_counts[(int) gimple_alloc_kind_seq]++;
1129 gimple_alloc_sizes[(int) gimple_alloc_kind_seq] += sizeof (*seq);
1130 #endif
1133 return seq;
1136 /* Return SEQ to the free pool of GIMPLE sequences. */
1138 void
1139 gimple_seq_free (gimple_seq seq)
1141 if (seq == NULL)
1142 return;
1144 gcc_assert (gimple_seq_first (seq) == NULL);
1145 gcc_assert (gimple_seq_last (seq) == NULL);
1147 /* If this triggers, it's a sign that the same list is being freed
1148 twice. */
1149 gcc_assert (seq != gimple_seq_cache || gimple_seq_cache == NULL);
1151 /* Add SEQ to the pool of free sequences. */
1152 seq->next_free = gimple_seq_cache;
1153 gimple_seq_cache = seq;
1157 /* Link gimple statement GS to the end of the sequence *SEQ_P. If
1158 *SEQ_P is NULL, a new sequence is allocated. */
1160 void
1161 gimple_seq_add_stmt (gimple_seq *seq_p, gimple gs)
1163 gimple_stmt_iterator si;
1165 if (gs == NULL)
1166 return;
1168 if (*seq_p == NULL)
1169 *seq_p = gimple_seq_alloc ();
1171 si = gsi_last (*seq_p);
1172 gsi_insert_after (&si, gs, GSI_NEW_STMT);
1176 /* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
1177 NULL, a new sequence is allocated. */
1179 void
1180 gimple_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
1182 gimple_stmt_iterator si;
1184 if (src == NULL)
1185 return;
1187 if (*dst_p == NULL)
1188 *dst_p = gimple_seq_alloc ();
1190 si = gsi_last (*dst_p);
1191 gsi_insert_seq_after (&si, src, GSI_NEW_STMT);
1195 /* Helper function of empty_body_p. Return true if STMT is an empty
1196 statement. */
1198 static bool
1199 empty_stmt_p (gimple stmt)
1201 if (gimple_code (stmt) == GIMPLE_NOP)
1202 return true;
1203 if (gimple_code (stmt) == GIMPLE_BIND)
1204 return empty_body_p (gimple_bind_body (stmt));
1205 return false;
1209 /* Return true if BODY contains nothing but empty statements. */
1211 bool
1212 empty_body_p (gimple_seq body)
1214 gimple_stmt_iterator i;
1217 if (gimple_seq_empty_p (body))
1218 return true;
1219 for (i = gsi_start (body); !gsi_end_p (i); gsi_next (&i))
1220 if (!empty_stmt_p (gsi_stmt (i)))
1221 return false;
1223 return true;
1227 /* Perform a deep copy of sequence SRC and return the result. */
1229 gimple_seq
1230 gimple_seq_copy (gimple_seq src)
1232 gimple_stmt_iterator gsi;
1233 gimple_seq new_seq = gimple_seq_alloc ();
1234 gimple stmt;
1236 for (gsi = gsi_start (src); !gsi_end_p (gsi); gsi_next (&gsi))
1238 stmt = gimple_copy (gsi_stmt (gsi));
1239 gimple_seq_add_stmt (&new_seq, stmt);
1242 return new_seq;
1246 /* Walk all the statements in the sequence SEQ calling walk_gimple_stmt
1247 on each one. WI is as in walk_gimple_stmt.
1249 If walk_gimple_stmt returns non-NULL, the walk is stopped, the
1250 value is stored in WI->CALLBACK_RESULT and the statement that
1251 produced the value is returned.
1253 Otherwise, all the statements are walked and NULL returned. */
1255 gimple
1256 walk_gimple_seq (gimple_seq seq, walk_stmt_fn callback_stmt,
1257 walk_tree_fn callback_op, struct walk_stmt_info *wi)
1259 gimple_stmt_iterator gsi;
1261 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
1263 tree ret = walk_gimple_stmt (&gsi, callback_stmt, callback_op, wi);
1264 if (ret)
1266 /* If CALLBACK_STMT or CALLBACK_OP return a value, WI must exist
1267 to hold it. */
1268 gcc_assert (wi);
1269 wi->callback_result = ret;
1270 return gsi_stmt (gsi);
1274 if (wi)
1275 wi->callback_result = NULL_TREE;
1277 return NULL;
1281 /* Helper function for walk_gimple_stmt. Walk operands of a GIMPLE_ASM. */
1283 static tree
1284 walk_gimple_asm (gimple stmt, walk_tree_fn callback_op,
1285 struct walk_stmt_info *wi)
1287 tree ret;
1288 unsigned noutputs;
1289 const char **oconstraints;
1290 unsigned i;
1291 const char *constraint;
1292 bool allows_mem, allows_reg, is_inout;
1294 noutputs = gimple_asm_noutputs (stmt);
1295 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
1297 if (wi)
1298 wi->is_lhs = true;
1300 for (i = 0; i < noutputs; i++)
1302 tree op = gimple_asm_output_op (stmt, i);
1303 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
1304 oconstraints[i] = constraint;
1305 parse_output_constraint (&constraint, i, 0, 0, &allows_mem, &allows_reg,
1306 &is_inout);
1307 if (wi)
1308 wi->val_only = (allows_reg || !allows_mem);
1309 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1310 if (ret)
1311 return ret;
1314 for (i = 0; i < gimple_asm_ninputs (stmt); i++)
1316 tree op = gimple_asm_input_op (stmt, i);
1317 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
1318 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1319 oconstraints, &allows_mem, &allows_reg);
1320 if (wi)
1321 wi->val_only = (allows_reg || !allows_mem);
1323 /* Although input "m" is not really a LHS, we need a lvalue. */
1324 if (wi)
1325 wi->is_lhs = !wi->val_only;
1326 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1327 if (ret)
1328 return ret;
1331 if (wi)
1333 wi->is_lhs = false;
1334 wi->val_only = true;
1337 return NULL_TREE;
1341 /* Helper function of WALK_GIMPLE_STMT. Walk every tree operand in
1342 STMT. CALLBACK_OP and WI are as in WALK_GIMPLE_STMT.
1344 CALLBACK_OP is called on each operand of STMT via walk_tree.
1345 Additional parameters to walk_tree must be stored in WI. For each operand
1346 OP, walk_tree is called as:
1348 walk_tree (&OP, CALLBACK_OP, WI, WI->PSET)
1350 If CALLBACK_OP returns non-NULL for an operand, the remaining
1351 operands are not scanned.
1353 The return value is that returned by the last call to walk_tree, or
1354 NULL_TREE if no CALLBACK_OP is specified. */
1356 inline tree
1357 walk_gimple_op (gimple stmt, walk_tree_fn callback_op,
1358 struct walk_stmt_info *wi)
1360 struct pointer_set_t *pset = (wi) ? wi->pset : NULL;
1361 unsigned i;
1362 tree ret = NULL_TREE;
1364 switch (gimple_code (stmt))
1366 case GIMPLE_ASSIGN:
1367 /* Walk the RHS operands. A formal temporary LHS may use a
1368 COMPONENT_REF RHS. */
1369 if (wi)
1370 wi->val_only = !is_gimple_reg (gimple_assign_lhs (stmt))
1371 || !gimple_assign_single_p (stmt);
1373 for (i = 1; i < gimple_num_ops (stmt); i++)
1375 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi,
1376 pset);
1377 if (ret)
1378 return ret;
1381 /* Walk the LHS. If the RHS is appropriate for a memory, we
1382 may use a COMPONENT_REF on the LHS. */
1383 if (wi)
1385 /* If the RHS has more than 1 operand, it is not appropriate
1386 for the memory. */
1387 wi->val_only = !is_gimple_mem_rhs (gimple_assign_rhs1 (stmt))
1388 || !gimple_assign_single_p (stmt);
1389 wi->is_lhs = true;
1392 ret = walk_tree (gimple_op_ptr (stmt, 0), callback_op, wi, pset);
1393 if (ret)
1394 return ret;
1396 if (wi)
1398 wi->val_only = true;
1399 wi->is_lhs = false;
1401 break;
1403 case GIMPLE_CALL:
1404 if (wi)
1405 wi->is_lhs = false;
1407 ret = walk_tree (gimple_call_chain_ptr (stmt), callback_op, wi, pset);
1408 if (ret)
1409 return ret;
1411 ret = walk_tree (gimple_call_fn_ptr (stmt), callback_op, wi, pset);
1412 if (ret)
1413 return ret;
1415 for (i = 0; i < gimple_call_num_args (stmt); i++)
1417 ret = walk_tree (gimple_call_arg_ptr (stmt, i), callback_op, wi,
1418 pset);
1419 if (ret)
1420 return ret;
1423 if (wi)
1424 wi->is_lhs = true;
1426 ret = walk_tree (gimple_call_lhs_ptr (stmt), callback_op, wi, pset);
1427 if (ret)
1428 return ret;
1430 if (wi)
1431 wi->is_lhs = false;
1432 break;
1434 case GIMPLE_CATCH:
1435 ret = walk_tree (gimple_catch_types_ptr (stmt), callback_op, wi,
1436 pset);
1437 if (ret)
1438 return ret;
1439 break;
1441 case GIMPLE_EH_FILTER:
1442 ret = walk_tree (gimple_eh_filter_types_ptr (stmt), callback_op, wi,
1443 pset);
1444 if (ret)
1445 return ret;
1446 break;
1448 case GIMPLE_ASM:
1449 ret = walk_gimple_asm (stmt, callback_op, wi);
1450 if (ret)
1451 return ret;
1452 break;
1454 case GIMPLE_OMP_CONTINUE:
1455 ret = walk_tree (gimple_omp_continue_control_def_ptr (stmt),
1456 callback_op, wi, pset);
1457 if (ret)
1458 return ret;
1460 ret = walk_tree (gimple_omp_continue_control_use_ptr (stmt),
1461 callback_op, wi, pset);
1462 if (ret)
1463 return ret;
1464 break;
1466 case GIMPLE_OMP_CRITICAL:
1467 ret = walk_tree (gimple_omp_critical_name_ptr (stmt), callback_op, wi,
1468 pset);
1469 if (ret)
1470 return ret;
1471 break;
1473 case GIMPLE_OMP_FOR:
1474 ret = walk_tree (gimple_omp_for_clauses_ptr (stmt), callback_op, wi,
1475 pset);
1476 if (ret)
1477 return ret;
1478 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
1480 ret = walk_tree (gimple_omp_for_index_ptr (stmt, i), callback_op,
1481 wi, pset);
1482 if (ret)
1483 return ret;
1484 ret = walk_tree (gimple_omp_for_initial_ptr (stmt, i), callback_op,
1485 wi, pset);
1486 if (ret)
1487 return ret;
1488 ret = walk_tree (gimple_omp_for_final_ptr (stmt, i), callback_op,
1489 wi, pset);
1490 if (ret)
1491 return ret;
1492 ret = walk_tree (gimple_omp_for_incr_ptr (stmt, i), callback_op,
1493 wi, pset);
1495 if (ret)
1496 return ret;
1497 break;
1499 case GIMPLE_OMP_PARALLEL:
1500 ret = walk_tree (gimple_omp_parallel_clauses_ptr (stmt), callback_op,
1501 wi, pset);
1502 if (ret)
1503 return ret;
1504 ret = walk_tree (gimple_omp_parallel_child_fn_ptr (stmt), callback_op,
1505 wi, pset);
1506 if (ret)
1507 return ret;
1508 ret = walk_tree (gimple_omp_parallel_data_arg_ptr (stmt), callback_op,
1509 wi, pset);
1510 if (ret)
1511 return ret;
1512 break;
1514 case GIMPLE_OMP_TASK:
1515 ret = walk_tree (gimple_omp_task_clauses_ptr (stmt), callback_op,
1516 wi, pset);
1517 if (ret)
1518 return ret;
1519 ret = walk_tree (gimple_omp_task_child_fn_ptr (stmt), callback_op,
1520 wi, pset);
1521 if (ret)
1522 return ret;
1523 ret = walk_tree (gimple_omp_task_data_arg_ptr (stmt), callback_op,
1524 wi, pset);
1525 if (ret)
1526 return ret;
1527 ret = walk_tree (gimple_omp_task_copy_fn_ptr (stmt), callback_op,
1528 wi, pset);
1529 if (ret)
1530 return ret;
1531 ret = walk_tree (gimple_omp_task_arg_size_ptr (stmt), callback_op,
1532 wi, pset);
1533 if (ret)
1534 return ret;
1535 ret = walk_tree (gimple_omp_task_arg_align_ptr (stmt), callback_op,
1536 wi, pset);
1537 if (ret)
1538 return ret;
1539 break;
1541 case GIMPLE_OMP_SECTIONS:
1542 ret = walk_tree (gimple_omp_sections_clauses_ptr (stmt), callback_op,
1543 wi, pset);
1544 if (ret)
1545 return ret;
1547 ret = walk_tree (gimple_omp_sections_control_ptr (stmt), callback_op,
1548 wi, pset);
1549 if (ret)
1550 return ret;
1552 break;
1554 case GIMPLE_OMP_SINGLE:
1555 ret = walk_tree (gimple_omp_single_clauses_ptr (stmt), callback_op, wi,
1556 pset);
1557 if (ret)
1558 return ret;
1559 break;
1561 case GIMPLE_OMP_ATOMIC_LOAD:
1562 ret = walk_tree (gimple_omp_atomic_load_lhs_ptr (stmt), callback_op, wi,
1563 pset);
1564 if (ret)
1565 return ret;
1567 ret = walk_tree (gimple_omp_atomic_load_rhs_ptr (stmt), callback_op, wi,
1568 pset);
1569 if (ret)
1570 return ret;
1571 break;
1573 case GIMPLE_OMP_ATOMIC_STORE:
1574 ret = walk_tree (gimple_omp_atomic_store_val_ptr (stmt), callback_op,
1575 wi, pset);
1576 if (ret)
1577 return ret;
1578 break;
1580 /* Tuples that do not have operands. */
1581 case GIMPLE_NOP:
1582 case GIMPLE_RESX:
1583 case GIMPLE_OMP_RETURN:
1584 case GIMPLE_PREDICT:
1585 break;
1587 default:
1589 enum gimple_statement_structure_enum gss;
1590 gss = gimple_statement_structure (stmt);
1591 if (gss == GSS_WITH_OPS || gss == GSS_WITH_MEM_OPS)
1592 for (i = 0; i < gimple_num_ops (stmt); i++)
1594 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi, pset);
1595 if (ret)
1596 return ret;
1599 break;
1602 return NULL_TREE;
1606 /* Walk the current statement in GSI (optionally using traversal state
1607 stored in WI). If WI is NULL, no state is kept during traversal.
1608 The callback CALLBACK_STMT is called. If CALLBACK_STMT indicates
1609 that it has handled all the operands of the statement, its return
1610 value is returned. Otherwise, the return value from CALLBACK_STMT
1611 is discarded and its operands are scanned.
1613 If CALLBACK_STMT is NULL or it didn't handle the operands,
1614 CALLBACK_OP is called on each operand of the statement via
1615 walk_gimple_op. If walk_gimple_op returns non-NULL for any
1616 operand, the remaining operands are not scanned. In this case, the
1617 return value from CALLBACK_OP is returned.
1619 In any other case, NULL_TREE is returned. */
1621 tree
1622 walk_gimple_stmt (gimple_stmt_iterator *gsi, walk_stmt_fn callback_stmt,
1623 walk_tree_fn callback_op, struct walk_stmt_info *wi)
1625 gimple ret;
1626 tree tree_ret;
1627 gimple stmt = gsi_stmt (*gsi);
1629 if (wi)
1630 wi->gsi = *gsi;
1632 if (wi && wi->want_locations && gimple_has_location (stmt))
1633 input_location = gimple_location (stmt);
1635 ret = NULL;
1637 /* Invoke the statement callback. Return if the callback handled
1638 all of STMT operands by itself. */
1639 if (callback_stmt)
1641 bool handled_ops = false;
1642 tree_ret = callback_stmt (gsi, &handled_ops, wi);
1643 if (handled_ops)
1644 return tree_ret;
1646 /* If CALLBACK_STMT did not handle operands, it should not have
1647 a value to return. */
1648 gcc_assert (tree_ret == NULL);
1650 /* Re-read stmt in case the callback changed it. */
1651 stmt = gsi_stmt (*gsi);
1654 /* If CALLBACK_OP is defined, invoke it on every operand of STMT. */
1655 if (callback_op)
1657 tree_ret = walk_gimple_op (stmt, callback_op, wi);
1658 if (tree_ret)
1659 return tree_ret;
1662 /* If STMT can have statements inside (e.g. GIMPLE_BIND), walk them. */
1663 switch (gimple_code (stmt))
1665 case GIMPLE_BIND:
1666 ret = walk_gimple_seq (gimple_bind_body (stmt), callback_stmt,
1667 callback_op, wi);
1668 if (ret)
1669 return wi->callback_result;
1670 break;
1672 case GIMPLE_CATCH:
1673 ret = walk_gimple_seq (gimple_catch_handler (stmt), callback_stmt,
1674 callback_op, wi);
1675 if (ret)
1676 return wi->callback_result;
1677 break;
1679 case GIMPLE_EH_FILTER:
1680 ret = walk_gimple_seq (gimple_eh_filter_failure (stmt), callback_stmt,
1681 callback_op, wi);
1682 if (ret)
1683 return wi->callback_result;
1684 break;
1686 case GIMPLE_TRY:
1687 ret = walk_gimple_seq (gimple_try_eval (stmt), callback_stmt, callback_op,
1688 wi);
1689 if (ret)
1690 return wi->callback_result;
1692 ret = walk_gimple_seq (gimple_try_cleanup (stmt), callback_stmt,
1693 callback_op, wi);
1694 if (ret)
1695 return wi->callback_result;
1696 break;
1698 case GIMPLE_OMP_FOR:
1699 ret = walk_gimple_seq (gimple_omp_for_pre_body (stmt), callback_stmt,
1700 callback_op, wi);
1701 if (ret)
1702 return wi->callback_result;
1704 /* FALL THROUGH. */
1705 case GIMPLE_OMP_CRITICAL:
1706 case GIMPLE_OMP_MASTER:
1707 case GIMPLE_OMP_ORDERED:
1708 case GIMPLE_OMP_SECTION:
1709 case GIMPLE_OMP_PARALLEL:
1710 case GIMPLE_OMP_TASK:
1711 case GIMPLE_OMP_SECTIONS:
1712 case GIMPLE_OMP_SINGLE:
1713 ret = walk_gimple_seq (gimple_omp_body (stmt), callback_stmt, callback_op,
1714 wi);
1715 if (ret)
1716 return wi->callback_result;
1717 break;
1719 case GIMPLE_WITH_CLEANUP_EXPR:
1720 ret = walk_gimple_seq (gimple_wce_cleanup (stmt), callback_stmt,
1721 callback_op, wi);
1722 if (ret)
1723 return wi->callback_result;
1724 break;
1726 default:
1727 gcc_assert (!gimple_has_substatements (stmt));
1728 break;
1731 return NULL;
1735 /* Set sequence SEQ to be the GIMPLE body for function FN. */
1737 void
1738 gimple_set_body (tree fndecl, gimple_seq seq)
1740 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1741 if (fn == NULL)
1743 /* If FNDECL still does not have a function structure associated
1744 with it, then it does not make sense for it to receive a
1745 GIMPLE body. */
1746 gcc_assert (seq == NULL);
1748 else
1749 fn->gimple_body = seq;
1753 /* Return the body of GIMPLE statements for function FN. */
1755 gimple_seq
1756 gimple_body (tree fndecl)
1758 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1759 return fn ? fn->gimple_body : NULL;
1762 /* Return true when FNDECL has Gimple body either in unlowered
1763 or CFG form. */
1764 bool
1765 gimple_has_body_p (tree fndecl)
1767 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1768 return (gimple_body (fndecl) || (fn && fn->cfg));
1771 /* Detect flags from a GIMPLE_CALL. This is just like
1772 call_expr_flags, but for gimple tuples. */
1775 gimple_call_flags (const_gimple stmt)
1777 int flags;
1778 tree decl = gimple_call_fndecl (stmt);
1779 tree t;
1781 if (decl)
1782 flags = flags_from_decl_or_type (decl);
1783 else
1785 t = TREE_TYPE (gimple_call_fn (stmt));
1786 if (t && TREE_CODE (t) == POINTER_TYPE)
1787 flags = flags_from_decl_or_type (TREE_TYPE (t));
1788 else
1789 flags = 0;
1792 return flags;
1796 /* Return true if GS is a copy assignment. */
1798 bool
1799 gimple_assign_copy_p (gimple gs)
1801 return gimple_code (gs) == GIMPLE_ASSIGN
1802 && get_gimple_rhs_class (gimple_assign_rhs_code (gs))
1803 == GIMPLE_SINGLE_RHS
1804 && is_gimple_val (gimple_op (gs, 1));
1808 /* Return true if GS is a SSA_NAME copy assignment. */
1810 bool
1811 gimple_assign_ssa_name_copy_p (gimple gs)
1813 return (gimple_code (gs) == GIMPLE_ASSIGN
1814 && (get_gimple_rhs_class (gimple_assign_rhs_code (gs))
1815 == GIMPLE_SINGLE_RHS)
1816 && TREE_CODE (gimple_assign_lhs (gs)) == SSA_NAME
1817 && TREE_CODE (gimple_assign_rhs1 (gs)) == SSA_NAME);
1821 /* Return true if GS is an assignment with a singleton RHS, i.e.,
1822 there is no operator associated with the assignment itself.
1823 Unlike gimple_assign_copy_p, this predicate returns true for
1824 any RHS operand, including those that perform an operation
1825 and do not have the semantics of a copy, such as COND_EXPR. */
1827 bool
1828 gimple_assign_single_p (gimple gs)
1830 return (gimple_code (gs) == GIMPLE_ASSIGN
1831 && get_gimple_rhs_class (gimple_assign_rhs_code (gs))
1832 == GIMPLE_SINGLE_RHS);
1835 /* Return true if GS is an assignment with a unary RHS, but the
1836 operator has no effect on the assigned value. The logic is adapted
1837 from STRIP_NOPS. This predicate is intended to be used in tuplifying
1838 instances in which STRIP_NOPS was previously applied to the RHS of
1839 an assignment.
1841 NOTE: In the use cases that led to the creation of this function
1842 and of gimple_assign_single_p, it is typical to test for either
1843 condition and to proceed in the same manner. In each case, the
1844 assigned value is represented by the single RHS operand of the
1845 assignment. I suspect there may be cases where gimple_assign_copy_p,
1846 gimple_assign_single_p, or equivalent logic is used where a similar
1847 treatment of unary NOPs is appropriate. */
1849 bool
1850 gimple_assign_unary_nop_p (gimple gs)
1852 return (gimple_code (gs) == GIMPLE_ASSIGN
1853 && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (gs))
1854 || gimple_assign_rhs_code (gs) == NON_LVALUE_EXPR)
1855 && gimple_assign_rhs1 (gs) != error_mark_node
1856 && (TYPE_MODE (TREE_TYPE (gimple_assign_lhs (gs)))
1857 == TYPE_MODE (TREE_TYPE (gimple_assign_rhs1 (gs)))));
1860 /* Set BB to be the basic block holding G. */
1862 void
1863 gimple_set_bb (gimple stmt, basic_block bb)
1865 stmt->gsbase.bb = bb;
1867 /* If the statement is a label, add the label to block-to-labels map
1868 so that we can speed up edge creation for GIMPLE_GOTOs. */
1869 if (cfun->cfg && gimple_code (stmt) == GIMPLE_LABEL)
1871 tree t;
1872 int uid;
1874 t = gimple_label_label (stmt);
1875 uid = LABEL_DECL_UID (t);
1876 if (uid == -1)
1878 unsigned old_len = VEC_length (basic_block, label_to_block_map);
1879 LABEL_DECL_UID (t) = uid = cfun->cfg->last_label_uid++;
1880 if (old_len <= (unsigned) uid)
1882 unsigned new_len = 3 * uid / 2 + 1;
1884 VEC_safe_grow_cleared (basic_block, gc, label_to_block_map,
1885 new_len);
1889 VEC_replace (basic_block, label_to_block_map, uid, bb);
1894 /* Fold the expression computed by STMT. If the expression can be
1895 folded, return the folded result, otherwise return NULL. STMT is
1896 not modified. */
1898 tree
1899 gimple_fold (const_gimple stmt)
1901 location_t loc = gimple_location (stmt);
1902 switch (gimple_code (stmt))
1904 case GIMPLE_COND:
1905 return fold_binary_loc (loc, gimple_cond_code (stmt),
1906 boolean_type_node,
1907 gimple_cond_lhs (stmt),
1908 gimple_cond_rhs (stmt));
1910 case GIMPLE_ASSIGN:
1911 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
1913 case GIMPLE_UNARY_RHS:
1914 return fold_unary_loc (loc, gimple_assign_rhs_code (stmt),
1915 TREE_TYPE (gimple_assign_lhs (stmt)),
1916 gimple_assign_rhs1 (stmt));
1917 case GIMPLE_BINARY_RHS:
1918 return fold_binary_loc (loc, gimple_assign_rhs_code (stmt),
1919 TREE_TYPE (gimple_assign_lhs (stmt)),
1920 gimple_assign_rhs1 (stmt),
1921 gimple_assign_rhs2 (stmt));
1922 case GIMPLE_SINGLE_RHS:
1923 return fold (gimple_assign_rhs1 (stmt));
1924 default:;
1926 break;
1928 case GIMPLE_SWITCH:
1929 return gimple_switch_index (stmt);
1931 case GIMPLE_CALL:
1932 return NULL_TREE;
1934 default:
1935 break;
1938 gcc_unreachable ();
1942 /* Modify the RHS of the assignment pointed-to by GSI using the
1943 operands in the expression tree EXPR.
1945 NOTE: The statement pointed-to by GSI may be reallocated if it
1946 did not have enough operand slots.
1948 This function is useful to convert an existing tree expression into
1949 the flat representation used for the RHS of a GIMPLE assignment.
1950 It will reallocate memory as needed to expand or shrink the number
1951 of operand slots needed to represent EXPR.
1953 NOTE: If you find yourself building a tree and then calling this
1954 function, you are most certainly doing it the slow way. It is much
1955 better to build a new assignment or to use the function
1956 gimple_assign_set_rhs_with_ops, which does not require an
1957 expression tree to be built. */
1959 void
1960 gimple_assign_set_rhs_from_tree (gimple_stmt_iterator *gsi, tree expr)
1962 enum tree_code subcode;
1963 tree op1, op2;
1965 extract_ops_from_tree (expr, &subcode, &op1, &op2);
1966 gimple_assign_set_rhs_with_ops (gsi, subcode, op1, op2);
1970 /* Set the RHS of assignment statement pointed-to by GSI to CODE with
1971 operands OP1 and OP2.
1973 NOTE: The statement pointed-to by GSI may be reallocated if it
1974 did not have enough operand slots. */
1976 void
1977 gimple_assign_set_rhs_with_ops (gimple_stmt_iterator *gsi, enum tree_code code,
1978 tree op1, tree op2)
1980 unsigned new_rhs_ops = get_gimple_rhs_num_ops (code);
1981 gimple stmt = gsi_stmt (*gsi);
1983 /* If the new CODE needs more operands, allocate a new statement. */
1984 if (gimple_num_ops (stmt) < new_rhs_ops + 1)
1986 tree lhs = gimple_assign_lhs (stmt);
1987 gimple new_stmt = gimple_alloc (gimple_code (stmt), new_rhs_ops + 1);
1988 memcpy (new_stmt, stmt, gimple_size (gimple_code (stmt)));
1989 gsi_replace (gsi, new_stmt, true);
1990 stmt = new_stmt;
1992 /* The LHS needs to be reset as this also changes the SSA name
1993 on the LHS. */
1994 gimple_assign_set_lhs (stmt, lhs);
1997 gimple_set_num_ops (stmt, new_rhs_ops + 1);
1998 gimple_set_subcode (stmt, code);
1999 gimple_assign_set_rhs1 (stmt, op1);
2000 if (new_rhs_ops > 1)
2001 gimple_assign_set_rhs2 (stmt, op2);
2005 /* Return the LHS of a statement that performs an assignment,
2006 either a GIMPLE_ASSIGN or a GIMPLE_CALL. Returns NULL_TREE
2007 for a call to a function that returns no value, or for a
2008 statement other than an assignment or a call. */
2010 tree
2011 gimple_get_lhs (const_gimple stmt)
2013 enum gimple_code code = gimple_code (stmt);
2015 if (code == GIMPLE_ASSIGN)
2016 return gimple_assign_lhs (stmt);
2017 else if (code == GIMPLE_CALL)
2018 return gimple_call_lhs (stmt);
2019 else
2020 return NULL_TREE;
2024 /* Set the LHS of a statement that performs an assignment,
2025 either a GIMPLE_ASSIGN or a GIMPLE_CALL. */
2027 void
2028 gimple_set_lhs (gimple stmt, tree lhs)
2030 enum gimple_code code = gimple_code (stmt);
2032 if (code == GIMPLE_ASSIGN)
2033 gimple_assign_set_lhs (stmt, lhs);
2034 else if (code == GIMPLE_CALL)
2035 gimple_call_set_lhs (stmt, lhs);
2036 else
2037 gcc_unreachable();
2041 /* Return a deep copy of statement STMT. All the operands from STMT
2042 are reallocated and copied using unshare_expr. The DEF, USE, VDEF
2043 and VUSE operand arrays are set to empty in the new copy. */
2045 gimple
2046 gimple_copy (gimple stmt)
2048 enum gimple_code code = gimple_code (stmt);
2049 unsigned num_ops = gimple_num_ops (stmt);
2050 gimple copy = gimple_alloc (code, num_ops);
2051 unsigned i;
2053 /* Shallow copy all the fields from STMT. */
2054 memcpy (copy, stmt, gimple_size (code));
2056 /* If STMT has sub-statements, deep-copy them as well. */
2057 if (gimple_has_substatements (stmt))
2059 gimple_seq new_seq;
2060 tree t;
2062 switch (gimple_code (stmt))
2064 case GIMPLE_BIND:
2065 new_seq = gimple_seq_copy (gimple_bind_body (stmt));
2066 gimple_bind_set_body (copy, new_seq);
2067 gimple_bind_set_vars (copy, unshare_expr (gimple_bind_vars (stmt)));
2068 gimple_bind_set_block (copy, gimple_bind_block (stmt));
2069 break;
2071 case GIMPLE_CATCH:
2072 new_seq = gimple_seq_copy (gimple_catch_handler (stmt));
2073 gimple_catch_set_handler (copy, new_seq);
2074 t = unshare_expr (gimple_catch_types (stmt));
2075 gimple_catch_set_types (copy, t);
2076 break;
2078 case GIMPLE_EH_FILTER:
2079 new_seq = gimple_seq_copy (gimple_eh_filter_failure (stmt));
2080 gimple_eh_filter_set_failure (copy, new_seq);
2081 t = unshare_expr (gimple_eh_filter_types (stmt));
2082 gimple_eh_filter_set_types (copy, t);
2083 break;
2085 case GIMPLE_TRY:
2086 new_seq = gimple_seq_copy (gimple_try_eval (stmt));
2087 gimple_try_set_eval (copy, new_seq);
2088 new_seq = gimple_seq_copy (gimple_try_cleanup (stmt));
2089 gimple_try_set_cleanup (copy, new_seq);
2090 break;
2092 case GIMPLE_OMP_FOR:
2093 new_seq = gimple_seq_copy (gimple_omp_for_pre_body (stmt));
2094 gimple_omp_for_set_pre_body (copy, new_seq);
2095 t = unshare_expr (gimple_omp_for_clauses (stmt));
2096 gimple_omp_for_set_clauses (copy, t);
2097 copy->gimple_omp_for.iter
2098 = GGC_NEWVEC (struct gimple_omp_for_iter,
2099 gimple_omp_for_collapse (stmt));
2100 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
2102 gimple_omp_for_set_cond (copy, i,
2103 gimple_omp_for_cond (stmt, i));
2104 gimple_omp_for_set_index (copy, i,
2105 gimple_omp_for_index (stmt, i));
2106 t = unshare_expr (gimple_omp_for_initial (stmt, i));
2107 gimple_omp_for_set_initial (copy, i, t);
2108 t = unshare_expr (gimple_omp_for_final (stmt, i));
2109 gimple_omp_for_set_final (copy, i, t);
2110 t = unshare_expr (gimple_omp_for_incr (stmt, i));
2111 gimple_omp_for_set_incr (copy, i, t);
2113 goto copy_omp_body;
2115 case GIMPLE_OMP_PARALLEL:
2116 t = unshare_expr (gimple_omp_parallel_clauses (stmt));
2117 gimple_omp_parallel_set_clauses (copy, t);
2118 t = unshare_expr (gimple_omp_parallel_child_fn (stmt));
2119 gimple_omp_parallel_set_child_fn (copy, t);
2120 t = unshare_expr (gimple_omp_parallel_data_arg (stmt));
2121 gimple_omp_parallel_set_data_arg (copy, t);
2122 goto copy_omp_body;
2124 case GIMPLE_OMP_TASK:
2125 t = unshare_expr (gimple_omp_task_clauses (stmt));
2126 gimple_omp_task_set_clauses (copy, t);
2127 t = unshare_expr (gimple_omp_task_child_fn (stmt));
2128 gimple_omp_task_set_child_fn (copy, t);
2129 t = unshare_expr (gimple_omp_task_data_arg (stmt));
2130 gimple_omp_task_set_data_arg (copy, t);
2131 t = unshare_expr (gimple_omp_task_copy_fn (stmt));
2132 gimple_omp_task_set_copy_fn (copy, t);
2133 t = unshare_expr (gimple_omp_task_arg_size (stmt));
2134 gimple_omp_task_set_arg_size (copy, t);
2135 t = unshare_expr (gimple_omp_task_arg_align (stmt));
2136 gimple_omp_task_set_arg_align (copy, t);
2137 goto copy_omp_body;
2139 case GIMPLE_OMP_CRITICAL:
2140 t = unshare_expr (gimple_omp_critical_name (stmt));
2141 gimple_omp_critical_set_name (copy, t);
2142 goto copy_omp_body;
2144 case GIMPLE_OMP_SECTIONS:
2145 t = unshare_expr (gimple_omp_sections_clauses (stmt));
2146 gimple_omp_sections_set_clauses (copy, t);
2147 t = unshare_expr (gimple_omp_sections_control (stmt));
2148 gimple_omp_sections_set_control (copy, t);
2149 /* FALLTHRU */
2151 case GIMPLE_OMP_SINGLE:
2152 case GIMPLE_OMP_SECTION:
2153 case GIMPLE_OMP_MASTER:
2154 case GIMPLE_OMP_ORDERED:
2155 copy_omp_body:
2156 new_seq = gimple_seq_copy (gimple_omp_body (stmt));
2157 gimple_omp_set_body (copy, new_seq);
2158 break;
2160 case GIMPLE_WITH_CLEANUP_EXPR:
2161 new_seq = gimple_seq_copy (gimple_wce_cleanup (stmt));
2162 gimple_wce_set_cleanup (copy, new_seq);
2163 break;
2165 default:
2166 gcc_unreachable ();
2170 /* Make copy of operands. */
2171 if (num_ops > 0)
2173 for (i = 0; i < num_ops; i++)
2174 gimple_set_op (copy, i, unshare_expr (gimple_op (stmt, i)));
2176 /* Clear out SSA operand vectors on COPY. */
2177 if (gimple_has_ops (stmt))
2179 gimple_set_def_ops (copy, NULL);
2180 gimple_set_use_ops (copy, NULL);
2183 if (gimple_has_mem_ops (stmt))
2185 gimple_set_vdef (copy, gimple_vdef (stmt));
2186 gimple_set_vuse (copy, gimple_vuse (stmt));
2189 /* SSA operands need to be updated. */
2190 gimple_set_modified (copy, true);
2193 return copy;
2197 /* Set the MODIFIED flag to MODIFIEDP, iff the gimple statement G has
2198 a MODIFIED field. */
2200 void
2201 gimple_set_modified (gimple s, bool modifiedp)
2203 if (gimple_has_ops (s))
2205 s->gsbase.modified = (unsigned) modifiedp;
2207 if (modifiedp
2208 && cfun->gimple_df
2209 && is_gimple_call (s)
2210 && gimple_call_noreturn_p (s))
2211 VEC_safe_push (gimple, gc, MODIFIED_NORETURN_CALLS (cfun), s);
2216 /* Return true if statement S has side-effects. We consider a
2217 statement to have side effects if:
2219 - It is a GIMPLE_CALL not marked with ECF_PURE or ECF_CONST.
2220 - Any of its operands are marked TREE_THIS_VOLATILE or TREE_SIDE_EFFECTS. */
2222 bool
2223 gimple_has_side_effects (const_gimple s)
2225 unsigned i;
2227 /* We don't have to scan the arguments to check for
2228 volatile arguments, though, at present, we still
2229 do a scan to check for TREE_SIDE_EFFECTS. */
2230 if (gimple_has_volatile_ops (s))
2231 return true;
2233 if (is_gimple_call (s))
2235 unsigned nargs = gimple_call_num_args (s);
2237 if (!(gimple_call_flags (s) & (ECF_CONST | ECF_PURE)))
2238 return true;
2239 else if (gimple_call_flags (s) & ECF_LOOPING_CONST_OR_PURE)
2240 /* An infinite loop is considered a side effect. */
2241 return true;
2243 if (gimple_call_lhs (s)
2244 && TREE_SIDE_EFFECTS (gimple_call_lhs (s)))
2246 gcc_assert (gimple_has_volatile_ops (s));
2247 return true;
2250 if (TREE_SIDE_EFFECTS (gimple_call_fn (s)))
2251 return true;
2253 for (i = 0; i < nargs; i++)
2254 if (TREE_SIDE_EFFECTS (gimple_call_arg (s, i)))
2256 gcc_assert (gimple_has_volatile_ops (s));
2257 return true;
2260 return false;
2262 else
2264 for (i = 0; i < gimple_num_ops (s); i++)
2265 if (TREE_SIDE_EFFECTS (gimple_op (s, i)))
2267 gcc_assert (gimple_has_volatile_ops (s));
2268 return true;
2272 return false;
2275 /* Return true if the RHS of statement S has side effects.
2276 We may use it to determine if it is admissable to replace
2277 an assignment or call with a copy of a previously-computed
2278 value. In such cases, side-effects due the the LHS are
2279 preserved. */
2281 bool
2282 gimple_rhs_has_side_effects (const_gimple s)
2284 unsigned i;
2286 if (is_gimple_call (s))
2288 unsigned nargs = gimple_call_num_args (s);
2290 if (!(gimple_call_flags (s) & (ECF_CONST | ECF_PURE)))
2291 return true;
2293 /* We cannot use gimple_has_volatile_ops here,
2294 because we must ignore a volatile LHS. */
2295 if (TREE_SIDE_EFFECTS (gimple_call_fn (s))
2296 || TREE_THIS_VOLATILE (gimple_call_fn (s)))
2298 gcc_assert (gimple_has_volatile_ops (s));
2299 return true;
2302 for (i = 0; i < nargs; i++)
2303 if (TREE_SIDE_EFFECTS (gimple_call_arg (s, i))
2304 || TREE_THIS_VOLATILE (gimple_call_arg (s, i)))
2305 return true;
2307 return false;
2309 else if (is_gimple_assign (s))
2311 /* Skip the first operand, the LHS. */
2312 for (i = 1; i < gimple_num_ops (s); i++)
2313 if (TREE_SIDE_EFFECTS (gimple_op (s, i))
2314 || TREE_THIS_VOLATILE (gimple_op (s, i)))
2316 gcc_assert (gimple_has_volatile_ops (s));
2317 return true;
2320 else
2322 /* For statements without an LHS, examine all arguments. */
2323 for (i = 0; i < gimple_num_ops (s); i++)
2324 if (TREE_SIDE_EFFECTS (gimple_op (s, i))
2325 || TREE_THIS_VOLATILE (gimple_op (s, i)))
2327 gcc_assert (gimple_has_volatile_ops (s));
2328 return true;
2332 return false;
2336 /* Helper for gimple_could_trap_p and gimple_assign_rhs_could_trap_p.
2337 Return true if S can trap. If INCLUDE_LHS is true and S is a
2338 GIMPLE_ASSIGN, the LHS of the assignment is also checked.
2339 Otherwise, only the RHS of the assignment is checked. */
2341 static bool
2342 gimple_could_trap_p_1 (gimple s, bool include_lhs)
2344 unsigned i, start;
2345 tree t, div = NULL_TREE;
2346 enum tree_code op;
2348 start = (is_gimple_assign (s) && !include_lhs) ? 1 : 0;
2350 for (i = start; i < gimple_num_ops (s); i++)
2351 if (tree_could_trap_p (gimple_op (s, i)))
2352 return true;
2354 switch (gimple_code (s))
2356 case GIMPLE_ASM:
2357 return gimple_asm_volatile_p (s);
2359 case GIMPLE_CALL:
2360 t = gimple_call_fndecl (s);
2361 /* Assume that calls to weak functions may trap. */
2362 if (!t || !DECL_P (t) || DECL_WEAK (t))
2363 return true;
2364 return false;
2366 case GIMPLE_ASSIGN:
2367 t = gimple_expr_type (s);
2368 op = gimple_assign_rhs_code (s);
2369 if (get_gimple_rhs_class (op) == GIMPLE_BINARY_RHS)
2370 div = gimple_assign_rhs2 (s);
2371 return (operation_could_trap_p (op, FLOAT_TYPE_P (t),
2372 (INTEGRAL_TYPE_P (t)
2373 && TYPE_OVERFLOW_TRAPS (t)),
2374 div));
2376 default:
2377 break;
2380 return false;
2385 /* Return true if statement S can trap. */
2387 bool
2388 gimple_could_trap_p (gimple s)
2390 return gimple_could_trap_p_1 (s, true);
2394 /* Return true if RHS of a GIMPLE_ASSIGN S can trap. */
2396 bool
2397 gimple_assign_rhs_could_trap_p (gimple s)
2399 gcc_assert (is_gimple_assign (s));
2400 return gimple_could_trap_p_1 (s, false);
2404 /* Print debugging information for gimple stmts generated. */
2406 void
2407 dump_gimple_statistics (void)
2409 #ifdef GATHER_STATISTICS
2410 int i, total_tuples = 0, total_bytes = 0;
2412 fprintf (stderr, "\nGIMPLE statements\n");
2413 fprintf (stderr, "Kind Stmts Bytes\n");
2414 fprintf (stderr, "---------------------------------------\n");
2415 for (i = 0; i < (int) gimple_alloc_kind_all; ++i)
2417 fprintf (stderr, "%-20s %7d %10d\n", gimple_alloc_kind_names[i],
2418 gimple_alloc_counts[i], gimple_alloc_sizes[i]);
2419 total_tuples += gimple_alloc_counts[i];
2420 total_bytes += gimple_alloc_sizes[i];
2422 fprintf (stderr, "---------------------------------------\n");
2423 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_tuples, total_bytes);
2424 fprintf (stderr, "---------------------------------------\n");
2425 #else
2426 fprintf (stderr, "No gimple statistics\n");
2427 #endif
2431 /* Return the number of operands needed on the RHS of a GIMPLE
2432 assignment for an expression with tree code CODE. */
2434 unsigned
2435 get_gimple_rhs_num_ops (enum tree_code code)
2437 enum gimple_rhs_class rhs_class = get_gimple_rhs_class (code);
2439 if (rhs_class == GIMPLE_UNARY_RHS || rhs_class == GIMPLE_SINGLE_RHS)
2440 return 1;
2441 else if (rhs_class == GIMPLE_BINARY_RHS)
2442 return 2;
2443 else
2444 gcc_unreachable ();
2447 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) \
2448 (unsigned char) \
2449 ((TYPE) == tcc_unary ? GIMPLE_UNARY_RHS \
2450 : ((TYPE) == tcc_binary \
2451 || (TYPE) == tcc_comparison) ? GIMPLE_BINARY_RHS \
2452 : ((TYPE) == tcc_constant \
2453 || (TYPE) == tcc_declaration \
2454 || (TYPE) == tcc_reference) ? GIMPLE_SINGLE_RHS \
2455 : ((SYM) == TRUTH_AND_EXPR \
2456 || (SYM) == TRUTH_OR_EXPR \
2457 || (SYM) == TRUTH_XOR_EXPR) ? GIMPLE_BINARY_RHS \
2458 : (SYM) == TRUTH_NOT_EXPR ? GIMPLE_UNARY_RHS \
2459 : ((SYM) == COND_EXPR \
2460 || (SYM) == CONSTRUCTOR \
2461 || (SYM) == OBJ_TYPE_REF \
2462 || (SYM) == ASSERT_EXPR \
2463 || (SYM) == ADDR_EXPR \
2464 || (SYM) == WITH_SIZE_EXPR \
2465 || (SYM) == EXC_PTR_EXPR \
2466 || (SYM) == SSA_NAME \
2467 || (SYM) == FILTER_EXPR \
2468 || (SYM) == POLYNOMIAL_CHREC \
2469 || (SYM) == DOT_PROD_EXPR \
2470 || (SYM) == VEC_COND_EXPR \
2471 || (SYM) == REALIGN_LOAD_EXPR) ? GIMPLE_SINGLE_RHS \
2472 : GIMPLE_INVALID_RHS),
2473 #define END_OF_BASE_TREE_CODES (unsigned char) GIMPLE_INVALID_RHS,
2475 const unsigned char gimple_rhs_class_table[] = {
2476 #include "all-tree.def"
2479 #undef DEFTREECODE
2480 #undef END_OF_BASE_TREE_CODES
2482 /* For the definitive definition of GIMPLE, see doc/tree-ssa.texi. */
2484 /* Validation of GIMPLE expressions. */
2486 /* Return true if OP is an acceptable tree node to be used as a GIMPLE
2487 operand. */
2489 bool
2490 is_gimple_operand (const_tree op)
2492 return op && get_gimple_rhs_class (TREE_CODE (op)) == GIMPLE_SINGLE_RHS;
2495 /* Returns true iff T is a valid RHS for an assignment to a renamed
2496 user -- or front-end generated artificial -- variable. */
2498 bool
2499 is_gimple_reg_rhs (tree t)
2501 return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
2504 /* Returns true iff T is a valid RHS for an assignment to an un-renamed
2505 LHS, or for a call argument. */
2507 bool
2508 is_gimple_mem_rhs (tree t)
2510 /* If we're dealing with a renamable type, either source or dest must be
2511 a renamed variable. */
2512 if (is_gimple_reg_type (TREE_TYPE (t)))
2513 return is_gimple_val (t);
2514 else
2515 return is_gimple_val (t) || is_gimple_lvalue (t);
2518 /* Return true if T is a valid LHS for a GIMPLE assignment expression. */
2520 bool
2521 is_gimple_lvalue (tree t)
2523 return (is_gimple_addressable (t)
2524 || TREE_CODE (t) == WITH_SIZE_EXPR
2525 /* These are complex lvalues, but don't have addresses, so they
2526 go here. */
2527 || TREE_CODE (t) == BIT_FIELD_REF);
2530 /* Return true if T is a GIMPLE condition. */
2532 bool
2533 is_gimple_condexpr (tree t)
2535 return (is_gimple_val (t) || (COMPARISON_CLASS_P (t)
2536 && !tree_could_trap_p (t)
2537 && is_gimple_val (TREE_OPERAND (t, 0))
2538 && is_gimple_val (TREE_OPERAND (t, 1))));
2541 /* Return true if T is something whose address can be taken. */
2543 bool
2544 is_gimple_addressable (tree t)
2546 return (is_gimple_id (t) || handled_component_p (t) || INDIRECT_REF_P (t));
2549 /* Return true if T is a valid gimple constant. */
2551 bool
2552 is_gimple_constant (const_tree t)
2554 switch (TREE_CODE (t))
2556 case INTEGER_CST:
2557 case REAL_CST:
2558 case FIXED_CST:
2559 case STRING_CST:
2560 case COMPLEX_CST:
2561 case VECTOR_CST:
2562 return true;
2564 /* Vector constant constructors are gimple invariant. */
2565 case CONSTRUCTOR:
2566 if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2567 return TREE_CONSTANT (t);
2568 else
2569 return false;
2571 default:
2572 return false;
2576 /* Return true if T is a gimple address. */
2578 bool
2579 is_gimple_address (const_tree t)
2581 tree op;
2583 if (TREE_CODE (t) != ADDR_EXPR)
2584 return false;
2586 op = TREE_OPERAND (t, 0);
2587 while (handled_component_p (op))
2589 if ((TREE_CODE (op) == ARRAY_REF
2590 || TREE_CODE (op) == ARRAY_RANGE_REF)
2591 && !is_gimple_val (TREE_OPERAND (op, 1)))
2592 return false;
2594 op = TREE_OPERAND (op, 0);
2597 if (CONSTANT_CLASS_P (op) || INDIRECT_REF_P (op))
2598 return true;
2600 switch (TREE_CODE (op))
2602 case PARM_DECL:
2603 case RESULT_DECL:
2604 case LABEL_DECL:
2605 case FUNCTION_DECL:
2606 case VAR_DECL:
2607 case CONST_DECL:
2608 return true;
2610 default:
2611 return false;
2615 /* Strip out all handled components that produce invariant
2616 offsets. */
2618 static const_tree
2619 strip_invariant_refs (const_tree op)
2621 while (handled_component_p (op))
2623 switch (TREE_CODE (op))
2625 case ARRAY_REF:
2626 case ARRAY_RANGE_REF:
2627 if (!is_gimple_constant (TREE_OPERAND (op, 1))
2628 || TREE_OPERAND (op, 2) != NULL_TREE
2629 || TREE_OPERAND (op, 3) != NULL_TREE)
2630 return NULL;
2631 break;
2633 case COMPONENT_REF:
2634 if (TREE_OPERAND (op, 2) != NULL_TREE)
2635 return NULL;
2636 break;
2638 default:;
2640 op = TREE_OPERAND (op, 0);
2643 return op;
2646 /* Return true if T is a gimple invariant address. */
2648 bool
2649 is_gimple_invariant_address (const_tree t)
2651 const_tree op;
2653 if (TREE_CODE (t) != ADDR_EXPR)
2654 return false;
2656 op = strip_invariant_refs (TREE_OPERAND (t, 0));
2658 return op && (CONSTANT_CLASS_P (op) || decl_address_invariant_p (op));
2661 /* Return true if T is a gimple invariant address at IPA level
2662 (so addresses of variables on stack are not allowed). */
2664 bool
2665 is_gimple_ip_invariant_address (const_tree t)
2667 const_tree op;
2669 if (TREE_CODE (t) != ADDR_EXPR)
2670 return false;
2672 op = strip_invariant_refs (TREE_OPERAND (t, 0));
2674 return op && (CONSTANT_CLASS_P (op) || decl_address_ip_invariant_p (op));
2677 /* Return true if T is a GIMPLE minimal invariant. It's a restricted
2678 form of function invariant. */
2680 bool
2681 is_gimple_min_invariant (const_tree t)
2683 if (TREE_CODE (t) == ADDR_EXPR)
2684 return is_gimple_invariant_address (t);
2686 return is_gimple_constant (t);
2689 /* Return true if T is a GIMPLE interprocedural invariant. It's a restricted
2690 form of gimple minimal invariant. */
2692 bool
2693 is_gimple_ip_invariant (const_tree t)
2695 if (TREE_CODE (t) == ADDR_EXPR)
2696 return is_gimple_ip_invariant_address (t);
2698 return is_gimple_constant (t);
2701 /* Return true if T looks like a valid GIMPLE statement. */
2703 bool
2704 is_gimple_stmt (tree t)
2706 const enum tree_code code = TREE_CODE (t);
2708 switch (code)
2710 case NOP_EXPR:
2711 /* The only valid NOP_EXPR is the empty statement. */
2712 return IS_EMPTY_STMT (t);
2714 case BIND_EXPR:
2715 case COND_EXPR:
2716 /* These are only valid if they're void. */
2717 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
2719 case SWITCH_EXPR:
2720 case GOTO_EXPR:
2721 case RETURN_EXPR:
2722 case LABEL_EXPR:
2723 case CASE_LABEL_EXPR:
2724 case TRY_CATCH_EXPR:
2725 case TRY_FINALLY_EXPR:
2726 case EH_FILTER_EXPR:
2727 case CATCH_EXPR:
2728 case ASM_EXPR:
2729 case RESX_EXPR:
2730 case STATEMENT_LIST:
2731 case OMP_PARALLEL:
2732 case OMP_FOR:
2733 case OMP_SECTIONS:
2734 case OMP_SECTION:
2735 case OMP_SINGLE:
2736 case OMP_MASTER:
2737 case OMP_ORDERED:
2738 case OMP_CRITICAL:
2739 case OMP_TASK:
2740 /* These are always void. */
2741 return true;
2743 case CALL_EXPR:
2744 case MODIFY_EXPR:
2745 case PREDICT_EXPR:
2746 /* These are valid regardless of their type. */
2747 return true;
2749 default:
2750 return false;
2754 /* Return true if T is a variable. */
2756 bool
2757 is_gimple_variable (tree t)
2759 return (TREE_CODE (t) == VAR_DECL
2760 || TREE_CODE (t) == PARM_DECL
2761 || TREE_CODE (t) == RESULT_DECL
2762 || TREE_CODE (t) == SSA_NAME);
2765 /* Return true if T is a GIMPLE identifier (something with an address). */
2767 bool
2768 is_gimple_id (tree t)
2770 return (is_gimple_variable (t)
2771 || TREE_CODE (t) == FUNCTION_DECL
2772 || TREE_CODE (t) == LABEL_DECL
2773 || TREE_CODE (t) == CONST_DECL
2774 /* Allow string constants, since they are addressable. */
2775 || TREE_CODE (t) == STRING_CST);
2778 /* Return true if TYPE is a suitable type for a scalar register variable. */
2780 bool
2781 is_gimple_reg_type (tree type)
2783 return !AGGREGATE_TYPE_P (type);
2786 /* Return true if T is a non-aggregate register variable. */
2788 bool
2789 is_gimple_reg (tree t)
2791 if (TREE_CODE (t) == SSA_NAME)
2792 t = SSA_NAME_VAR (t);
2794 if (!is_gimple_variable (t))
2795 return false;
2797 if (!is_gimple_reg_type (TREE_TYPE (t)))
2798 return false;
2800 /* A volatile decl is not acceptable because we can't reuse it as
2801 needed. We need to copy it into a temp first. */
2802 if (TREE_THIS_VOLATILE (t))
2803 return false;
2805 /* We define "registers" as things that can be renamed as needed,
2806 which with our infrastructure does not apply to memory. */
2807 if (needs_to_live_in_memory (t))
2808 return false;
2810 /* Hard register variables are an interesting case. For those that
2811 are call-clobbered, we don't know where all the calls are, since
2812 we don't (want to) take into account which operations will turn
2813 into libcalls at the rtl level. For those that are call-saved,
2814 we don't currently model the fact that calls may in fact change
2815 global hard registers, nor do we examine ASM_CLOBBERS at the tree
2816 level, and so miss variable changes that might imply. All around,
2817 it seems safest to not do too much optimization with these at the
2818 tree level at all. We'll have to rely on the rtl optimizers to
2819 clean this up, as there we've got all the appropriate bits exposed. */
2820 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
2821 return false;
2823 /* Complex and vector values must have been put into SSA-like form.
2824 That is, no assignments to the individual components. */
2825 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
2826 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2827 return DECL_GIMPLE_REG_P (t);
2829 return true;
2833 /* Return true if T is a GIMPLE variable whose address is not needed. */
2835 bool
2836 is_gimple_non_addressable (tree t)
2838 if (TREE_CODE (t) == SSA_NAME)
2839 t = SSA_NAME_VAR (t);
2841 return (is_gimple_variable (t) && ! needs_to_live_in_memory (t));
2844 /* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant. */
2846 bool
2847 is_gimple_val (tree t)
2849 /* Make loads from volatiles and memory vars explicit. */
2850 if (is_gimple_variable (t)
2851 && is_gimple_reg_type (TREE_TYPE (t))
2852 && !is_gimple_reg (t))
2853 return false;
2855 /* FIXME make these decls. That can happen only when we expose the
2856 entire landing-pad construct at the tree level. */
2857 if (TREE_CODE (t) == EXC_PTR_EXPR || TREE_CODE (t) == FILTER_EXPR)
2858 return true;
2860 return (is_gimple_variable (t) || is_gimple_min_invariant (t));
2863 /* Similarly, but accept hard registers as inputs to asm statements. */
2865 bool
2866 is_gimple_asm_val (tree t)
2868 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
2869 return true;
2871 return is_gimple_val (t);
2874 /* Return true if T is a GIMPLE minimal lvalue. */
2876 bool
2877 is_gimple_min_lval (tree t)
2879 if (!(t = CONST_CAST_TREE (strip_invariant_refs (t))))
2880 return false;
2881 return (is_gimple_id (t) || TREE_CODE (t) == INDIRECT_REF);
2884 /* Return true if T is a typecast operation. */
2886 bool
2887 is_gimple_cast (tree t)
2889 return (CONVERT_EXPR_P (t)
2890 || TREE_CODE (t) == FIX_TRUNC_EXPR);
2893 /* Return true if T is a valid function operand of a CALL_EXPR. */
2895 bool
2896 is_gimple_call_addr (tree t)
2898 return (TREE_CODE (t) == OBJ_TYPE_REF || is_gimple_val (t));
2901 /* If T makes a function call, return the corresponding CALL_EXPR operand.
2902 Otherwise, return NULL_TREE. */
2904 tree
2905 get_call_expr_in (tree t)
2907 if (TREE_CODE (t) == MODIFY_EXPR)
2908 t = TREE_OPERAND (t, 1);
2909 if (TREE_CODE (t) == WITH_SIZE_EXPR)
2910 t = TREE_OPERAND (t, 0);
2911 if (TREE_CODE (t) == CALL_EXPR)
2912 return t;
2913 return NULL_TREE;
2917 /* Given a memory reference expression T, return its base address.
2918 The base address of a memory reference expression is the main
2919 object being referenced. For instance, the base address for
2920 'array[i].fld[j]' is 'array'. You can think of this as stripping
2921 away the offset part from a memory address.
2923 This function calls handled_component_p to strip away all the inner
2924 parts of the memory reference until it reaches the base object. */
2926 tree
2927 get_base_address (tree t)
2929 while (handled_component_p (t))
2930 t = TREE_OPERAND (t, 0);
2932 if (SSA_VAR_P (t)
2933 || TREE_CODE (t) == STRING_CST
2934 || TREE_CODE (t) == CONSTRUCTOR
2935 || INDIRECT_REF_P (t))
2936 return t;
2937 else
2938 return NULL_TREE;
2941 void
2942 recalculate_side_effects (tree t)
2944 enum tree_code code = TREE_CODE (t);
2945 int len = TREE_OPERAND_LENGTH (t);
2946 int i;
2948 switch (TREE_CODE_CLASS (code))
2950 case tcc_expression:
2951 switch (code)
2953 case INIT_EXPR:
2954 case MODIFY_EXPR:
2955 case VA_ARG_EXPR:
2956 case PREDECREMENT_EXPR:
2957 case PREINCREMENT_EXPR:
2958 case POSTDECREMENT_EXPR:
2959 case POSTINCREMENT_EXPR:
2960 /* All of these have side-effects, no matter what their
2961 operands are. */
2962 return;
2964 default:
2965 break;
2967 /* Fall through. */
2969 case tcc_comparison: /* a comparison expression */
2970 case tcc_unary: /* a unary arithmetic expression */
2971 case tcc_binary: /* a binary arithmetic expression */
2972 case tcc_reference: /* a reference */
2973 case tcc_vl_exp: /* a function call */
2974 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
2975 for (i = 0; i < len; ++i)
2977 tree op = TREE_OPERAND (t, i);
2978 if (op && TREE_SIDE_EFFECTS (op))
2979 TREE_SIDE_EFFECTS (t) = 1;
2981 break;
2983 case tcc_constant:
2984 /* No side-effects. */
2985 return;
2987 default:
2988 gcc_unreachable ();
2992 /* Canonicalize a tree T for use in a COND_EXPR as conditional. Returns
2993 a canonicalized tree that is valid for a COND_EXPR or NULL_TREE, if
2994 we failed to create one. */
2996 tree
2997 canonicalize_cond_expr_cond (tree t)
2999 /* For (bool)x use x != 0. */
3000 if (TREE_CODE (t) == NOP_EXPR
3001 && TREE_TYPE (t) == boolean_type_node)
3003 tree top0 = TREE_OPERAND (t, 0);
3004 t = build2 (NE_EXPR, TREE_TYPE (t),
3005 top0, build_int_cst (TREE_TYPE (top0), 0));
3007 /* For !x use x == 0. */
3008 else if (TREE_CODE (t) == TRUTH_NOT_EXPR)
3010 tree top0 = TREE_OPERAND (t, 0);
3011 t = build2 (EQ_EXPR, TREE_TYPE (t),
3012 top0, build_int_cst (TREE_TYPE (top0), 0));
3014 /* For cmp ? 1 : 0 use cmp. */
3015 else if (TREE_CODE (t) == COND_EXPR
3016 && COMPARISON_CLASS_P (TREE_OPERAND (t, 0))
3017 && integer_onep (TREE_OPERAND (t, 1))
3018 && integer_zerop (TREE_OPERAND (t, 2)))
3020 tree top0 = TREE_OPERAND (t, 0);
3021 t = build2 (TREE_CODE (top0), TREE_TYPE (t),
3022 TREE_OPERAND (top0, 0), TREE_OPERAND (top0, 1));
3025 if (is_gimple_condexpr (t))
3026 return t;
3028 return NULL_TREE;
3031 /* Build a GIMPLE_CALL identical to STMT but skipping the arguments in
3032 the positions marked by the set ARGS_TO_SKIP. */
3034 gimple
3035 gimple_call_copy_skip_args (gimple stmt, bitmap args_to_skip)
3037 int i;
3038 tree fn = gimple_call_fn (stmt);
3039 int nargs = gimple_call_num_args (stmt);
3040 VEC(tree, heap) *vargs = VEC_alloc (tree, heap, nargs);
3041 gimple new_stmt;
3043 for (i = 0; i < nargs; i++)
3044 if (!bitmap_bit_p (args_to_skip, i))
3045 VEC_quick_push (tree, vargs, gimple_call_arg (stmt, i));
3047 new_stmt = gimple_build_call_vec (fn, vargs);
3048 VEC_free (tree, heap, vargs);
3049 if (gimple_call_lhs (stmt))
3050 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
3052 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
3053 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
3055 gimple_set_block (new_stmt, gimple_block (stmt));
3056 if (gimple_has_location (stmt))
3057 gimple_set_location (new_stmt, gimple_location (stmt));
3059 /* Carry all the flags to the new GIMPLE_CALL. */
3060 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
3061 gimple_call_set_tail (new_stmt, gimple_call_tail_p (stmt));
3062 gimple_call_set_cannot_inline (new_stmt, gimple_call_cannot_inline_p (stmt));
3063 gimple_call_set_return_slot_opt (new_stmt, gimple_call_return_slot_opt_p (stmt));
3064 gimple_call_set_from_thunk (new_stmt, gimple_call_from_thunk_p (stmt));
3065 gimple_call_set_va_arg_pack (new_stmt, gimple_call_va_arg_pack_p (stmt));
3067 gimple_set_modified (new_stmt, true);
3069 return new_stmt;
3073 /* Data structure used to count the number of dereferences to PTR
3074 inside an expression. */
3075 struct count_ptr_d
3077 tree ptr;
3078 unsigned num_stores;
3079 unsigned num_loads;
3082 /* Helper for count_uses_and_derefs. Called by walk_tree to look for
3083 (ALIGN/MISALIGNED_)INDIRECT_REF nodes for the pointer passed in DATA. */
3085 static tree
3086 count_ptr_derefs (tree *tp, int *walk_subtrees, void *data)
3088 struct walk_stmt_info *wi_p = (struct walk_stmt_info *) data;
3089 struct count_ptr_d *count_p = (struct count_ptr_d *) wi_p->info;
3091 /* Do not walk inside ADDR_EXPR nodes. In the expression &ptr->fld,
3092 pointer 'ptr' is *not* dereferenced, it is simply used to compute
3093 the address of 'fld' as 'ptr + offsetof(fld)'. */
3094 if (TREE_CODE (*tp) == ADDR_EXPR)
3096 *walk_subtrees = 0;
3097 return NULL_TREE;
3100 if (INDIRECT_REF_P (*tp) && TREE_OPERAND (*tp, 0) == count_p->ptr)
3102 if (wi_p->is_lhs)
3103 count_p->num_stores++;
3104 else
3105 count_p->num_loads++;
3108 return NULL_TREE;
3111 /* Count the number of direct and indirect uses for pointer PTR in
3112 statement STMT. The number of direct uses is stored in
3113 *NUM_USES_P. Indirect references are counted separately depending
3114 on whether they are store or load operations. The counts are
3115 stored in *NUM_STORES_P and *NUM_LOADS_P. */
3117 void
3118 count_uses_and_derefs (tree ptr, gimple stmt, unsigned *num_uses_p,
3119 unsigned *num_loads_p, unsigned *num_stores_p)
3121 ssa_op_iter i;
3122 tree use;
3124 *num_uses_p = 0;
3125 *num_loads_p = 0;
3126 *num_stores_p = 0;
3128 /* Find out the total number of uses of PTR in STMT. */
3129 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
3130 if (use == ptr)
3131 (*num_uses_p)++;
3133 /* Now count the number of indirect references to PTR. This is
3134 truly awful, but we don't have much choice. There are no parent
3135 pointers inside INDIRECT_REFs, so an expression like
3136 '*x_1 = foo (x_1, *x_1)' needs to be traversed piece by piece to
3137 find all the indirect and direct uses of x_1 inside. The only
3138 shortcut we can take is the fact that GIMPLE only allows
3139 INDIRECT_REFs inside the expressions below. */
3140 if (is_gimple_assign (stmt)
3141 || gimple_code (stmt) == GIMPLE_RETURN
3142 || gimple_code (stmt) == GIMPLE_ASM
3143 || is_gimple_call (stmt))
3145 struct walk_stmt_info wi;
3146 struct count_ptr_d count;
3148 count.ptr = ptr;
3149 count.num_stores = 0;
3150 count.num_loads = 0;
3152 memset (&wi, 0, sizeof (wi));
3153 wi.info = &count;
3154 walk_gimple_op (stmt, count_ptr_derefs, &wi);
3156 *num_stores_p = count.num_stores;
3157 *num_loads_p = count.num_loads;
3160 gcc_assert (*num_uses_p >= *num_loads_p + *num_stores_p);
3163 /* From a tree operand OP return the base of a load or store operation
3164 or NULL_TREE if OP is not a load or a store. */
3166 static tree
3167 get_base_loadstore (tree op)
3169 while (handled_component_p (op))
3170 op = TREE_OPERAND (op, 0);
3171 if (DECL_P (op)
3172 || INDIRECT_REF_P (op)
3173 || TREE_CODE (op) == TARGET_MEM_REF)
3174 return op;
3175 return NULL_TREE;
3178 /* For the statement STMT call the callbacks VISIT_LOAD, VISIT_STORE and
3179 VISIT_ADDR if non-NULL on loads, store and address-taken operands
3180 passing the STMT, the base of the operand and DATA to it. The base
3181 will be either a decl, an indirect reference (including TARGET_MEM_REF)
3182 or the argument of an address expression.
3183 Returns the results of these callbacks or'ed. */
3185 bool
3186 walk_stmt_load_store_addr_ops (gimple stmt, void *data,
3187 bool (*visit_load)(gimple, tree, void *),
3188 bool (*visit_store)(gimple, tree, void *),
3189 bool (*visit_addr)(gimple, tree, void *))
3191 bool ret = false;
3192 unsigned i;
3193 if (gimple_assign_single_p (stmt))
3195 tree lhs, rhs;
3196 if (visit_store)
3198 lhs = get_base_loadstore (gimple_assign_lhs (stmt));
3199 if (lhs)
3200 ret |= visit_store (stmt, lhs, data);
3202 rhs = gimple_assign_rhs1 (stmt);
3203 while (handled_component_p (rhs))
3204 rhs = TREE_OPERAND (rhs, 0);
3205 if (visit_addr)
3207 if (TREE_CODE (rhs) == ADDR_EXPR)
3208 ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), data);
3209 else if (TREE_CODE (rhs) == TARGET_MEM_REF
3210 && TMR_BASE (rhs) != NULL_TREE
3211 && TREE_CODE (TMR_BASE (rhs)) == ADDR_EXPR)
3212 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (rhs), 0), data);
3213 else if (TREE_CODE (rhs) == OBJ_TYPE_REF
3214 && TREE_CODE (OBJ_TYPE_REF_OBJECT (rhs)) == ADDR_EXPR)
3215 ret |= visit_addr (stmt, TREE_OPERAND (OBJ_TYPE_REF_OBJECT (rhs),
3216 0), data);
3217 lhs = gimple_assign_lhs (stmt);
3218 if (TREE_CODE (lhs) == TARGET_MEM_REF
3219 && TMR_BASE (lhs) != NULL_TREE
3220 && TREE_CODE (TMR_BASE (lhs)) == ADDR_EXPR)
3221 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (lhs), 0), data);
3223 if (visit_load)
3225 rhs = get_base_loadstore (rhs);
3226 if (rhs)
3227 ret |= visit_load (stmt, rhs, data);
3230 else if (visit_addr
3231 && (is_gimple_assign (stmt)
3232 || gimple_code (stmt) == GIMPLE_COND))
3234 for (i = 0; i < gimple_num_ops (stmt); ++i)
3235 if (gimple_op (stmt, i)
3236 && TREE_CODE (gimple_op (stmt, i)) == ADDR_EXPR)
3237 ret |= visit_addr (stmt, TREE_OPERAND (gimple_op (stmt, i), 0), data);
3239 else if (is_gimple_call (stmt))
3241 if (visit_store)
3243 tree lhs = gimple_call_lhs (stmt);
3244 if (lhs)
3246 lhs = get_base_loadstore (lhs);
3247 if (lhs)
3248 ret |= visit_store (stmt, lhs, data);
3251 if (visit_load || visit_addr)
3252 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3254 tree rhs = gimple_call_arg (stmt, i);
3255 if (visit_addr
3256 && TREE_CODE (rhs) == ADDR_EXPR)
3257 ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), data);
3258 else if (visit_load)
3260 rhs = get_base_loadstore (rhs);
3261 if (rhs)
3262 ret |= visit_load (stmt, rhs, data);
3265 if (visit_addr
3266 && gimple_call_chain (stmt)
3267 && TREE_CODE (gimple_call_chain (stmt)) == ADDR_EXPR)
3268 ret |= visit_addr (stmt, TREE_OPERAND (gimple_call_chain (stmt), 0),
3269 data);
3270 if (visit_addr
3271 && gimple_call_return_slot_opt_p (stmt)
3272 && gimple_call_lhs (stmt) != NULL_TREE
3273 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
3274 ret |= visit_addr (stmt, gimple_call_lhs (stmt), data);
3276 else if (gimple_code (stmt) == GIMPLE_ASM)
3278 unsigned noutputs;
3279 const char *constraint;
3280 const char **oconstraints;
3281 bool allows_mem, allows_reg, is_inout;
3282 noutputs = gimple_asm_noutputs (stmt);
3283 oconstraints = XALLOCAVEC (const char *, noutputs);
3284 if (visit_store || visit_addr)
3285 for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
3287 tree link = gimple_asm_output_op (stmt, i);
3288 tree op = get_base_loadstore (TREE_VALUE (link));
3289 if (op && visit_store)
3290 ret |= visit_store (stmt, op, data);
3291 if (visit_addr)
3293 constraint = TREE_STRING_POINTER
3294 (TREE_VALUE (TREE_PURPOSE (link)));
3295 oconstraints[i] = constraint;
3296 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
3297 &allows_reg, &is_inout);
3298 if (op && !allows_reg && allows_mem)
3299 ret |= visit_addr (stmt, op, data);
3302 if (visit_load || visit_addr)
3303 for (i = 0; i < gimple_asm_ninputs (stmt); ++i)
3305 tree link = gimple_asm_input_op (stmt, i);
3306 tree op = TREE_VALUE (link);
3307 if (visit_addr
3308 && TREE_CODE (op) == ADDR_EXPR)
3309 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
3310 else if (visit_load || visit_addr)
3312 op = get_base_loadstore (op);
3313 if (op)
3315 if (visit_load)
3316 ret |= visit_load (stmt, op, data);
3317 if (visit_addr)
3319 constraint = TREE_STRING_POINTER
3320 (TREE_VALUE (TREE_PURPOSE (link)));
3321 parse_input_constraint (&constraint, 0, 0, noutputs,
3322 0, oconstraints,
3323 &allows_mem, &allows_reg);
3324 if (!allows_reg && allows_mem)
3325 ret |= visit_addr (stmt, op, data);
3331 else if (gimple_code (stmt) == GIMPLE_RETURN)
3333 tree op = gimple_return_retval (stmt);
3334 if (op)
3336 if (visit_addr
3337 && TREE_CODE (op) == ADDR_EXPR)
3338 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
3339 else if (visit_load)
3341 op = get_base_loadstore (op);
3342 if (op)
3343 ret |= visit_load (stmt, op, data);
3347 else if (visit_addr
3348 && gimple_code (stmt) == GIMPLE_PHI)
3350 for (i = 0; i < gimple_phi_num_args (stmt); ++i)
3352 tree op = PHI_ARG_DEF (stmt, i);
3353 if (TREE_CODE (op) == ADDR_EXPR)
3354 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
3358 return ret;
3361 /* Like walk_stmt_load_store_addr_ops but with NULL visit_addr. IPA-CP
3362 should make a faster clone for this case. */
3364 bool
3365 walk_stmt_load_store_ops (gimple stmt, void *data,
3366 bool (*visit_load)(gimple, tree, void *),
3367 bool (*visit_store)(gimple, tree, void *))
3369 return walk_stmt_load_store_addr_ops (stmt, data,
3370 visit_load, visit_store, NULL);
3373 /* Helper for gimple_ior_addresses_taken_1. */
3375 static bool
3376 gimple_ior_addresses_taken_1 (gimple stmt ATTRIBUTE_UNUSED,
3377 tree addr, void *data)
3379 bitmap addresses_taken = (bitmap)data;
3380 while (handled_component_p (addr))
3381 addr = TREE_OPERAND (addr, 0);
3382 if (DECL_P (addr))
3384 bitmap_set_bit (addresses_taken, DECL_UID (addr));
3385 return true;
3387 return false;
3390 /* Set the bit for the uid of all decls that have their address taken
3391 in STMT in the ADDRESSES_TAKEN bitmap. Returns true if there
3392 were any in this stmt. */
3394 bool
3395 gimple_ior_addresses_taken (bitmap addresses_taken, gimple stmt)
3397 return walk_stmt_load_store_addr_ops (stmt, addresses_taken, NULL, NULL,
3398 gimple_ior_addresses_taken_1);
3401 #include "gt-gimple.h"