]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/gimple.c
loop-unswitch.c (unswitch_single_loop): Use optimize_loop_for_speed_p.
[thirdparty/gcc.git] / gcc / gimple.c
CommitLineData
726a989a
RB
1/* Gimple IR support functions.
2
3 Copyright 2007, 2008 Free Software Foundation, Inc.
4 Contributed by Aldy Hernandez <aldyh@redhat.com>
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 3, or (at your option) any later
11version.
12
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
25#include "tm.h"
26#include "tree.h"
27#include "ggc.h"
28#include "errors.h"
29#include "hard-reg-set.h"
30#include "basic-block.h"
31#include "gimple.h"
32#include "diagnostic.h"
33#include "tree-flow.h"
34#include "value-prof.h"
35#include "flags.h"
36
37#define DEFGSCODE(SYM, NAME, STRUCT) NAME,
38const char *const gimple_code_name[] = {
39#include "gimple.def"
40};
41#undef DEFGSCODE
42
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)),
48const size_t gimple_ops_offset_[] = {
49#include "gimple.def"
50};
51#undef DEFGSCODE
52
53#ifdef GATHER_STATISTICS
54/* Gimple stats. */
55
56int gimple_alloc_counts[(int) gimple_alloc_kind_all];
57int gimple_alloc_sizes[(int) gimple_alloc_kind_all];
58
59/* Keep in sync with gimple.h:enum gimple_alloc_kind. */
60static const char * const gimple_alloc_kind_names[] = {
61 "assignments",
62 "phi nodes",
63 "conditionals",
64 "sequences",
65 "everything else"
66};
67
68#endif /* GATHER_STATISTICS */
69
70/* A cache of gimple_seq objects. Sequences are created and destroyed
71 fairly often during gimplification. */
72static GTY ((deletable)) struct gimple_seq_d *gimple_seq_cache;
73
74/* Private API manipulation functions shared only with some
75 other files. */
76extern void gimple_set_stored_syms (gimple, bitmap, bitmap_obstack *);
77extern void gimple_set_loaded_syms (gimple, bitmap, bitmap_obstack *);
78
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. */
82
83/* Set the code for statement G to CODE. */
84
85static inline void
86gimple_set_code (gimple g, enum gimple_code code)
87{
88 g->gsbase.code = code;
89}
90
91
92/* Return the GSS_* identifier for the given GIMPLE statement CODE. */
93
94static enum gimple_statement_structure_enum
95gss_for_code (enum gimple_code code)
96{
97 switch (code)
98 {
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_CHANGE_DYNAMIC_TYPE:
106 case GIMPLE_SWITCH: return GSS_WITH_OPS;
107 case GIMPLE_ASM: return GSS_ASM;
108 case GIMPLE_BIND: return GSS_BIND;
109 case GIMPLE_CATCH: return GSS_CATCH;
110 case GIMPLE_EH_FILTER: return GSS_EH_FILTER;
111 case GIMPLE_NOP: return GSS_BASE;
112 case GIMPLE_PHI: return GSS_PHI;
113 case GIMPLE_RESX: return GSS_RESX;
114 case GIMPLE_TRY: return GSS_TRY;
115 case GIMPLE_WITH_CLEANUP_EXPR: return GSS_WCE;
116 case GIMPLE_OMP_CRITICAL: return GSS_OMP_CRITICAL;
117 case GIMPLE_OMP_FOR: return GSS_OMP_FOR;
118 case GIMPLE_OMP_MASTER:
119 case GIMPLE_OMP_ORDERED:
120 case GIMPLE_OMP_SECTION: return GSS_OMP;
121 case GIMPLE_OMP_RETURN:
122 case GIMPLE_OMP_SECTIONS_SWITCH: return GSS_BASE;
123 case GIMPLE_OMP_CONTINUE: return GSS_OMP_CONTINUE;
124 case GIMPLE_OMP_PARALLEL: return GSS_OMP_PARALLEL;
125 case GIMPLE_OMP_TASK: return GSS_OMP_TASK;
126 case GIMPLE_OMP_SECTIONS: return GSS_OMP_SECTIONS;
127 case GIMPLE_OMP_SINGLE: return GSS_OMP_SINGLE;
128 case GIMPLE_OMP_ATOMIC_LOAD: return GSS_OMP_ATOMIC_LOAD;
129 case GIMPLE_OMP_ATOMIC_STORE: return GSS_OMP_ATOMIC_STORE;
130 case GIMPLE_PREDICT: return GSS_BASE;
131 default: gcc_unreachable ();
132 }
133}
134
135
136/* Return the number of bytes needed to hold a GIMPLE statement with
137 code CODE. */
138
139static size_t
140gimple_size (enum gimple_code code)
141{
142 enum gimple_statement_structure_enum gss = gss_for_code (code);
143
144 if (gss == GSS_WITH_OPS)
145 return sizeof (struct gimple_statement_with_ops);
146 else if (gss == GSS_WITH_MEM_OPS)
147 return sizeof (struct gimple_statement_with_memory_ops);
148
149 switch (code)
150 {
151 case GIMPLE_ASM:
152 return sizeof (struct gimple_statement_asm);
153 case GIMPLE_NOP:
154 return sizeof (struct gimple_statement_base);
155 case GIMPLE_BIND:
156 return sizeof (struct gimple_statement_bind);
157 case GIMPLE_CATCH:
158 return sizeof (struct gimple_statement_catch);
159 case GIMPLE_EH_FILTER:
160 return sizeof (struct gimple_statement_eh_filter);
161 case GIMPLE_TRY:
162 return sizeof (struct gimple_statement_try);
163 case GIMPLE_RESX:
164 return sizeof (struct gimple_statement_resx);
165 case GIMPLE_OMP_CRITICAL:
166 return sizeof (struct gimple_statement_omp_critical);
167 case GIMPLE_OMP_FOR:
168 return sizeof (struct gimple_statement_omp_for);
169 case GIMPLE_OMP_PARALLEL:
170 return sizeof (struct gimple_statement_omp_parallel);
171 case GIMPLE_OMP_TASK:
172 return sizeof (struct gimple_statement_omp_task);
173 case GIMPLE_OMP_SECTION:
174 case GIMPLE_OMP_MASTER:
175 case GIMPLE_OMP_ORDERED:
176 return sizeof (struct gimple_statement_omp);
177 case GIMPLE_OMP_RETURN:
178 return sizeof (struct gimple_statement_base);
179 case GIMPLE_OMP_CONTINUE:
180 return sizeof (struct gimple_statement_omp_continue);
181 case GIMPLE_OMP_SECTIONS:
182 return sizeof (struct gimple_statement_omp_sections);
183 case GIMPLE_OMP_SECTIONS_SWITCH:
184 return sizeof (struct gimple_statement_base);
185 case GIMPLE_OMP_SINGLE:
186 return sizeof (struct gimple_statement_omp_single);
187 case GIMPLE_OMP_ATOMIC_LOAD:
188 return sizeof (struct gimple_statement_omp_atomic_load);
189 case GIMPLE_OMP_ATOMIC_STORE:
190 return sizeof (struct gimple_statement_omp_atomic_store);
191 case GIMPLE_WITH_CLEANUP_EXPR:
192 return sizeof (struct gimple_statement_wce);
193 case GIMPLE_CHANGE_DYNAMIC_TYPE:
194 return sizeof (struct gimple_statement_with_ops);
195 case GIMPLE_PREDICT:
196 return sizeof (struct gimple_statement_base);
197 default:
198 break;
199 }
200
201 gcc_unreachable ();
202}
203
204
205/* Allocate memory for a GIMPLE statement with code CODE and NUM_OPS
206 operands. */
207
208#define gimple_alloc(c, n) gimple_alloc_stat (c, n MEM_STAT_INFO)
209static gimple
210gimple_alloc_stat (enum gimple_code code, unsigned num_ops MEM_STAT_DECL)
211{
212 size_t size;
213 gimple stmt;
214
215 size = gimple_size (code);
216 if (num_ops > 0)
217 size += sizeof (tree) * (num_ops - 1);
218
219#ifdef GATHER_STATISTICS
220 {
221 enum gimple_alloc_kind kind = gimple_alloc_kind (code);
222 gimple_alloc_counts[(int) kind]++;
223 gimple_alloc_sizes[(int) kind] += size;
224 }
225#endif
226
227 stmt = (gimple) ggc_alloc_cleared_stat (size PASS_MEM_STAT);
228 gimple_set_code (stmt, code);
229 gimple_set_num_ops (stmt, num_ops);
230
231 /* Do not call gimple_set_modified here as it has other side
232 effects and this tuple is still not completely built. */
233 stmt->gsbase.modified = 1;
234
235 return stmt;
236}
237
238/* Set SUBCODE to be the code of the expression computed by statement G. */
239
240static inline void
241gimple_set_subcode (gimple g, unsigned subcode)
242{
243 /* We only have 16 bits for the RHS code. Assert that we are not
244 overflowing it. */
245 gcc_assert (subcode < (1 << 16));
246 g->gsbase.subcode = subcode;
247}
248
249
250
251/* Build a tuple with operands. CODE is the statement to build (which
252 must be one of the GIMPLE_WITH_OPS tuples). SUBCODE is the sub-code
253 for the new tuple. NUM_OPS is the number of operands to allocate. */
254
255#define gimple_build_with_ops(c, s, n) \
256 gimple_build_with_ops_stat (c, s, n MEM_STAT_INFO)
257
258static gimple
259gimple_build_with_ops_stat (enum gimple_code code, enum tree_code subcode,
260 unsigned num_ops MEM_STAT_DECL)
261{
262 gimple s = gimple_alloc_stat (code, num_ops PASS_MEM_STAT);
263 gimple_set_subcode (s, subcode);
264
265 return s;
266}
267
268
269/* Build a GIMPLE_RETURN statement returning RETVAL. */
270
271gimple
272gimple_build_return (tree retval)
273{
274 gimple s = gimple_build_with_ops (GIMPLE_RETURN, 0, 1);
275 if (retval)
276 gimple_return_set_retval (s, retval);
277 return s;
278}
279
280/* Helper for gimple_build_call, gimple_build_call_vec and
281 gimple_build_call_from_tree. Build the basic components of a
282 GIMPLE_CALL statement to function FN with NARGS arguments. */
283
284static inline gimple
285gimple_build_call_1 (tree fn, unsigned nargs)
286{
287 gimple s = gimple_build_with_ops (GIMPLE_CALL, 0, nargs + 3);
7c9577be
RG
288 if (TREE_CODE (fn) == FUNCTION_DECL)
289 fn = build_fold_addr_expr (fn);
726a989a
RB
290 gimple_set_op (s, 1, fn);
291 return s;
292}
293
294
295/* Build a GIMPLE_CALL statement to function FN with the arguments
296 specified in vector ARGS. */
297
298gimple
299gimple_build_call_vec (tree fn, VEC(tree, heap) *args)
300{
301 unsigned i;
302 unsigned nargs = VEC_length (tree, args);
303 gimple call = gimple_build_call_1 (fn, nargs);
304
305 for (i = 0; i < nargs; i++)
306 gimple_call_set_arg (call, i, VEC_index (tree, args, i));
307
308 return call;
309}
310
311
312/* Build a GIMPLE_CALL statement to function FN. NARGS is the number of
313 arguments. The ... are the arguments. */
314
315gimple
316gimple_build_call (tree fn, unsigned nargs, ...)
317{
318 va_list ap;
319 gimple call;
320 unsigned i;
321
322 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL || is_gimple_call_addr (fn));
323
324 call = gimple_build_call_1 (fn, nargs);
325
326 va_start (ap, nargs);
327 for (i = 0; i < nargs; i++)
328 gimple_call_set_arg (call, i, va_arg (ap, tree));
329 va_end (ap);
330
331 return call;
332}
333
334
335/* Build a GIMPLE_CALL statement from CALL_EXPR T. Note that T is
336 assumed to be in GIMPLE form already. Minimal checking is done of
337 this fact. */
338
339gimple
340gimple_build_call_from_tree (tree t)
341{
342 unsigned i, nargs;
343 gimple call;
344 tree fndecl = get_callee_fndecl (t);
345
346 gcc_assert (TREE_CODE (t) == CALL_EXPR);
347
348 nargs = call_expr_nargs (t);
349 call = gimple_build_call_1 (fndecl ? fndecl : CALL_EXPR_FN (t), nargs);
350
351 for (i = 0; i < nargs; i++)
352 gimple_call_set_arg (call, i, CALL_EXPR_ARG (t, i));
353
354 gimple_set_block (call, TREE_BLOCK (t));
355
356 /* Carry all the CALL_EXPR flags to the new GIMPLE_CALL. */
357 gimple_call_set_chain (call, CALL_EXPR_STATIC_CHAIN (t));
358 gimple_call_set_tail (call, CALL_EXPR_TAILCALL (t));
359 gimple_call_set_cannot_inline (call, CALL_CANNOT_INLINE_P (t));
360 gimple_call_set_return_slot_opt (call, CALL_EXPR_RETURN_SLOT_OPT (t));
361 gimple_call_set_from_thunk (call, CALL_FROM_THUNK_P (t));
362 gimple_call_set_va_arg_pack (call, CALL_EXPR_VA_ARG_PACK (t));
363
364 return call;
365}
366
367
368/* Extract the operands and code for expression EXPR into *SUBCODE_P,
369 *OP1_P and *OP2_P respectively. */
370
371void
372extract_ops_from_tree (tree expr, enum tree_code *subcode_p, tree *op1_p,
373 tree *op2_p)
374{
82d6e6fc 375 enum gimple_rhs_class grhs_class;
726a989a
RB
376
377 *subcode_p = TREE_CODE (expr);
82d6e6fc 378 grhs_class = get_gimple_rhs_class (*subcode_p);
726a989a 379
82d6e6fc 380 if (grhs_class == GIMPLE_BINARY_RHS)
726a989a
RB
381 {
382 *op1_p = TREE_OPERAND (expr, 0);
383 *op2_p = TREE_OPERAND (expr, 1);
384 }
82d6e6fc 385 else if (grhs_class == GIMPLE_UNARY_RHS)
726a989a
RB
386 {
387 *op1_p = TREE_OPERAND (expr, 0);
388 *op2_p = NULL_TREE;
389 }
82d6e6fc 390 else if (grhs_class == GIMPLE_SINGLE_RHS)
726a989a
RB
391 {
392 *op1_p = expr;
393 *op2_p = NULL_TREE;
394 }
395 else
396 gcc_unreachable ();
397}
398
399
400/* Build a GIMPLE_ASSIGN statement.
401
402 LHS of the assignment.
403 RHS of the assignment which can be unary or binary. */
404
405gimple
406gimple_build_assign_stat (tree lhs, tree rhs MEM_STAT_DECL)
407{
408 enum tree_code subcode;
409 tree op1, op2;
410
411 extract_ops_from_tree (rhs, &subcode, &op1, &op2);
412 return gimple_build_assign_with_ops_stat (subcode, lhs, op1, op2
413 PASS_MEM_STAT);
414}
415
416
417/* Build a GIMPLE_ASSIGN statement with sub-code SUBCODE and operands
418 OP1 and OP2. If OP2 is NULL then SUBCODE must be of class
419 GIMPLE_UNARY_RHS or GIMPLE_SINGLE_RHS. */
420
421gimple
422gimple_build_assign_with_ops_stat (enum tree_code subcode, tree lhs, tree op1,
423 tree op2 MEM_STAT_DECL)
424{
425 unsigned num_ops;
426 gimple p;
427
428 /* Need 1 operand for LHS and 1 or 2 for the RHS (depending on the
429 code). */
430 num_ops = get_gimple_rhs_num_ops (subcode) + 1;
431
432 p = gimple_build_with_ops_stat (GIMPLE_ASSIGN, subcode, num_ops
433 PASS_MEM_STAT);
434 gimple_assign_set_lhs (p, lhs);
435 gimple_assign_set_rhs1 (p, op1);
436 if (op2)
437 {
438 gcc_assert (num_ops > 2);
439 gimple_assign_set_rhs2 (p, op2);
440 }
441
442 return p;
443}
444
445
446/* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
447
448 DST/SRC are the destination and source respectively. You can pass
449 ungimplified trees in DST or SRC, in which case they will be
450 converted to a gimple operand if necessary.
451
452 This function returns the newly created GIMPLE_ASSIGN tuple. */
453
454inline gimple
455gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
456{
457 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
458 gimplify_and_add (t, seq_p);
459 ggc_free (t);
460 return gimple_seq_last_stmt (*seq_p);
461}
462
463
464/* Build a GIMPLE_COND statement.
465
466 PRED is the condition used to compare LHS and the RHS.
467 T_LABEL is the label to jump to if the condition is true.
468 F_LABEL is the label to jump to otherwise. */
469
470gimple
471gimple_build_cond (enum tree_code pred_code, tree lhs, tree rhs,
472 tree t_label, tree f_label)
473{
474 gimple p;
475
476 gcc_assert (TREE_CODE_CLASS (pred_code) == tcc_comparison);
477 p = gimple_build_with_ops (GIMPLE_COND, pred_code, 4);
478 gimple_cond_set_lhs (p, lhs);
479 gimple_cond_set_rhs (p, rhs);
480 gimple_cond_set_true_label (p, t_label);
481 gimple_cond_set_false_label (p, f_label);
482 return p;
483}
484
485
486/* Extract operands for a GIMPLE_COND statement out of COND_EXPR tree COND. */
487
488void
489gimple_cond_get_ops_from_tree (tree cond, enum tree_code *code_p,
490 tree *lhs_p, tree *rhs_p)
491{
492 gcc_assert (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison
493 || TREE_CODE (cond) == TRUTH_NOT_EXPR
494 || is_gimple_min_invariant (cond)
495 || SSA_VAR_P (cond));
496
497 extract_ops_from_tree (cond, code_p, lhs_p, rhs_p);
498
499 /* Canonicalize conditionals of the form 'if (!VAL)'. */
500 if (*code_p == TRUTH_NOT_EXPR)
501 {
502 *code_p = EQ_EXPR;
503 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
504 *rhs_p = fold_convert (TREE_TYPE (*lhs_p), integer_zero_node);
505 }
506 /* Canonicalize conditionals of the form 'if (VAL)' */
507 else if (TREE_CODE_CLASS (*code_p) != tcc_comparison)
508 {
509 *code_p = NE_EXPR;
510 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
511 *rhs_p = fold_convert (TREE_TYPE (*lhs_p), integer_zero_node);
512 }
513}
514
515
516/* Build a GIMPLE_COND statement from the conditional expression tree
517 COND. T_LABEL and F_LABEL are as in gimple_build_cond. */
518
519gimple
520gimple_build_cond_from_tree (tree cond, tree t_label, tree f_label)
521{
522 enum tree_code code;
523 tree lhs, rhs;
524
525 gimple_cond_get_ops_from_tree (cond, &code, &lhs, &rhs);
526 return gimple_build_cond (code, lhs, rhs, t_label, f_label);
527}
528
529/* Set code, lhs, and rhs of a GIMPLE_COND from a suitable
530 boolean expression tree COND. */
531
532void
533gimple_cond_set_condition_from_tree (gimple stmt, tree cond)
534{
535 enum tree_code code;
536 tree lhs, rhs;
537
538 gimple_cond_get_ops_from_tree (cond, &code, &lhs, &rhs);
539 gimple_cond_set_condition (stmt, code, lhs, rhs);
540}
541
542/* Build a GIMPLE_LABEL statement for LABEL. */
543
544gimple
545gimple_build_label (tree label)
546{
547 gimple p = gimple_build_with_ops (GIMPLE_LABEL, 0, 1);
548 gimple_label_set_label (p, label);
549 return p;
550}
551
552/* Build a GIMPLE_GOTO statement to label DEST. */
553
554gimple
555gimple_build_goto (tree dest)
556{
557 gimple p = gimple_build_with_ops (GIMPLE_GOTO, 0, 1);
558 gimple_goto_set_dest (p, dest);
559 return p;
560}
561
562
563/* Build a GIMPLE_NOP statement. */
564
565gimple
566gimple_build_nop (void)
567{
568 return gimple_alloc (GIMPLE_NOP, 0);
569}
570
571
572/* Build a GIMPLE_BIND statement.
573 VARS are the variables in BODY.
574 BLOCK is the containing block. */
575
576gimple
577gimple_build_bind (tree vars, gimple_seq body, tree block)
578{
579 gimple p = gimple_alloc (GIMPLE_BIND, 0);
580 gimple_bind_set_vars (p, vars);
581 if (body)
582 gimple_bind_set_body (p, body);
583 if (block)
584 gimple_bind_set_block (p, block);
585 return p;
586}
587
588/* Helper function to set the simple fields of a asm stmt.
589
590 STRING is a pointer to a string that is the asm blocks assembly code.
591 NINPUT is the number of register inputs.
592 NOUTPUT is the number of register outputs.
593 NCLOBBERS is the number of clobbered registers.
594 */
595
596static inline gimple
597gimple_build_asm_1 (const char *string, unsigned ninputs, unsigned noutputs,
598 unsigned nclobbers)
599{
600 gimple p;
601 int size = strlen (string);
602
603 p = gimple_build_with_ops (GIMPLE_ASM, 0, ninputs + noutputs + nclobbers);
604
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);
609
610#ifdef GATHER_STATISTICS
611 gimple_alloc_sizes[(int) gimple_alloc_kind (GIMPLE_ASM)] += size;
612#endif
613
614 return p;
615}
616
617/* Build a GIMPLE_ASM statement.
618
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. */
626
627gimple
628gimple_build_asm_vec (const char *string, VEC(tree,gc)* inputs,
629 VEC(tree,gc)* outputs, VEC(tree,gc)* clobbers)
630{
631 gimple p;
632 unsigned i;
633
634 p = gimple_build_asm_1 (string,
635 VEC_length (tree, inputs),
636 VEC_length (tree, outputs),
637 VEC_length (tree, clobbers));
638
639 for (i = 0; i < VEC_length (tree, inputs); i++)
640 gimple_asm_set_input_op (p, i, VEC_index (tree, inputs, i));
641
642 for (i = 0; i < VEC_length (tree, outputs); i++)
643 gimple_asm_set_output_op (p, i, VEC_index (tree, outputs, i));
644
645 for (i = 0; i < VEC_length (tree, clobbers); i++)
646 gimple_asm_set_clobber_op (p, i, VEC_index (tree, clobbers, i));
647
648 return p;
649}
650
651/* Build a GIMPLE_ASM statement.
652
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. */
658
659gimple
660gimple_build_asm (const char *string, unsigned ninputs, unsigned noutputs,
661 unsigned nclobbers, ...)
662{
663 gimple p;
664 unsigned i;
665 va_list ap;
666
667 p = gimple_build_asm_1 (string, ninputs, noutputs, nclobbers);
668
669 va_start (ap, nclobbers);
670
671 for (i = 0; i < ninputs; i++)
672 gimple_asm_set_input_op (p, i, va_arg (ap, tree));
673
674 for (i = 0; i < noutputs; i++)
675 gimple_asm_set_output_op (p, i, va_arg (ap, tree));
676
677 for (i = 0; i < nclobbers; i++)
678 gimple_asm_set_clobber_op (p, i, va_arg (ap, tree));
679
680 va_end (ap);
681
682 return p;
683}
684
685/* Build a GIMPLE_CATCH statement.
686
687 TYPES are the catch types.
688 HANDLER is the exception handler. */
689
690gimple
691gimple_build_catch (tree types, gimple_seq handler)
692{
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);
697
698 return p;
699}
700
701/* Build a GIMPLE_EH_FILTER statement.
702
703 TYPES are the filter's types.
704 FAILURE is the filter's failure action. */
705
706gimple
707gimple_build_eh_filter (tree types, gimple_seq failure)
708{
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);
713
714 return p;
715}
716
717/* Build a GIMPLE_TRY statement.
718
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. */
723
724gimple
725gimple_build_try (gimple_seq eval, gimple_seq cleanup,
726 enum gimple_try_flags kind)
727{
728 gimple p;
729
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);
737
738 return p;
739}
740
741/* Construct a GIMPLE_WITH_CLEANUP_EXPR statement.
742
743 CLEANUP is the cleanup expression. */
744
745gimple
746gimple_build_wce (gimple_seq cleanup)
747{
748 gimple p = gimple_alloc (GIMPLE_WITH_CLEANUP_EXPR, 0);
749 if (cleanup)
750 gimple_wce_set_cleanup (p, cleanup);
751
752 return p;
753}
754
755
756/* Build a GIMPLE_RESX statement.
757
758 REGION is the region number from which this resx causes control flow to
759 leave. */
760
761gimple
762gimple_build_resx (int region)
763{
764 gimple p = gimple_alloc (GIMPLE_RESX, 0);
765 gimple_resx_set_region (p, region);
766 return p;
767}
768
769
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. */
774
775static inline gimple
776gimple_build_switch_1 (unsigned nlabels, tree index, tree default_label)
777{
778 /* nlabels + 1 default label + 1 index. */
779 gimple p = gimple_build_with_ops (GIMPLE_SWITCH, 0, nlabels + 1 + 1);
780 gimple_switch_set_index (p, index);
781 gimple_switch_set_default_label (p, default_label);
782 return p;
783}
784
785
786/* Build a GIMPLE_SWITCH statement.
787
788 INDEX is the switch's index.
789 NLABELS is the number of labels in the switch excluding the DEFAULT_LABEL.
790 ... are the labels excluding the default. */
791
792gimple
793gimple_build_switch (unsigned nlabels, tree index, tree default_label, ...)
794{
795 va_list al;
796 unsigned i;
797 gimple p;
798
799 p = gimple_build_switch_1 (nlabels, index, default_label);
800
801 /* Store the rest of the labels. */
802 va_start (al, default_label);
803 for (i = 1; i <= nlabels; i++)
804 gimple_switch_set_label (p, i, va_arg (al, tree));
805 va_end (al);
806
807 return p;
808}
809
810
811/* Build a GIMPLE_SWITCH statement.
812
813 INDEX is the switch's index.
814 DEFAULT_LABEL is the default label
815 ARGS is a vector of labels excluding the default. */
816
817gimple
818gimple_build_switch_vec (tree index, tree default_label, VEC(tree, heap) *args)
819{
820 unsigned i;
821 unsigned nlabels = VEC_length (tree, args);
822 gimple p = gimple_build_switch_1 (nlabels, index, default_label);
823
824 /* Put labels in labels[1 - (nlabels + 1)].
825 Default label is in labels[0]. */
826 for (i = 1; i <= nlabels; i++)
827 gimple_switch_set_label (p, i, VEC_index (tree, args, i - 1));
828
829 return p;
830}
831
832
833/* Build a GIMPLE_OMP_CRITICAL statement.
834
835 BODY is the sequence of statements for which only one thread can execute.
836 NAME is optional identifier for this critical block. */
837
838gimple
839gimple_build_omp_critical (gimple_seq body, tree name)
840{
841 gimple p = gimple_alloc (GIMPLE_OMP_CRITICAL, 0);
842 gimple_omp_critical_set_name (p, name);
843 if (body)
844 gimple_omp_set_body (p, body);
845
846 return p;
847}
848
849/* Build a GIMPLE_OMP_FOR statement.
850
851 BODY is sequence of statements inside the for loop.
852 CLAUSES, are any of the OMP loop construct's clauses: private, firstprivate,
853 lastprivate, reductions, ordered, schedule, and nowait.
854 COLLAPSE is the collapse count.
855 PRE_BODY is the sequence of statements that are loop invariant. */
856
857gimple
858gimple_build_omp_for (gimple_seq body, tree clauses, size_t collapse,
859 gimple_seq pre_body)
860{
861 gimple p = gimple_alloc (GIMPLE_OMP_FOR, 0);
862 if (body)
863 gimple_omp_set_body (p, body);
864 gimple_omp_for_set_clauses (p, clauses);
865 p->gimple_omp_for.collapse = collapse;
866 p->gimple_omp_for.iter = GGC_CNEWVEC (struct gimple_omp_for_iter, collapse);
867 if (pre_body)
868 gimple_omp_for_set_pre_body (p, pre_body);
869
870 return p;
871}
872
873
874/* Build a GIMPLE_OMP_PARALLEL statement.
875
876 BODY is sequence of statements which are executed in parallel.
877 CLAUSES, are the OMP parallel construct's clauses.
878 CHILD_FN is the function created for the parallel threads to execute.
879 DATA_ARG are the shared data argument(s). */
880
881gimple
882gimple_build_omp_parallel (gimple_seq body, tree clauses, tree child_fn,
883 tree data_arg)
884{
885 gimple p = gimple_alloc (GIMPLE_OMP_PARALLEL, 0);
886 if (body)
887 gimple_omp_set_body (p, body);
888 gimple_omp_parallel_set_clauses (p, clauses);
889 gimple_omp_parallel_set_child_fn (p, child_fn);
890 gimple_omp_parallel_set_data_arg (p, data_arg);
891
892 return p;
893}
894
895
896/* Build a GIMPLE_OMP_TASK statement.
897
898 BODY is sequence of statements which are executed by the explicit task.
899 CLAUSES, are the OMP parallel construct's clauses.
900 CHILD_FN is the function created for the parallel threads to execute.
901 DATA_ARG are the shared data argument(s).
902 COPY_FN is the optional function for firstprivate initialization.
903 ARG_SIZE and ARG_ALIGN are size and alignment of the data block. */
904
905gimple
906gimple_build_omp_task (gimple_seq body, tree clauses, tree child_fn,
907 tree data_arg, tree copy_fn, tree arg_size,
908 tree arg_align)
909{
910 gimple p = gimple_alloc (GIMPLE_OMP_TASK, 0);
911 if (body)
912 gimple_omp_set_body (p, body);
913 gimple_omp_task_set_clauses (p, clauses);
914 gimple_omp_task_set_child_fn (p, child_fn);
915 gimple_omp_task_set_data_arg (p, data_arg);
916 gimple_omp_task_set_copy_fn (p, copy_fn);
917 gimple_omp_task_set_arg_size (p, arg_size);
918 gimple_omp_task_set_arg_align (p, arg_align);
919
920 return p;
921}
922
923
924/* Build a GIMPLE_OMP_SECTION statement for a sections statement.
925
926 BODY is the sequence of statements in the section. */
927
928gimple
929gimple_build_omp_section (gimple_seq body)
930{
931 gimple p = gimple_alloc (GIMPLE_OMP_SECTION, 0);
932 if (body)
933 gimple_omp_set_body (p, body);
934
935 return p;
936}
937
938
939/* Build a GIMPLE_OMP_MASTER statement.
940
941 BODY is the sequence of statements to be executed by just the master. */
942
943gimple
944gimple_build_omp_master (gimple_seq body)
945{
946 gimple p = gimple_alloc (GIMPLE_OMP_MASTER, 0);
947 if (body)
948 gimple_omp_set_body (p, body);
949
950 return p;
951}
952
953
954/* Build a GIMPLE_OMP_CONTINUE statement.
955
956 CONTROL_DEF is the definition of the control variable.
957 CONTROL_USE is the use of the control variable. */
958
959gimple
960gimple_build_omp_continue (tree control_def, tree control_use)
961{
962 gimple p = gimple_alloc (GIMPLE_OMP_CONTINUE, 0);
963 gimple_omp_continue_set_control_def (p, control_def);
964 gimple_omp_continue_set_control_use (p, control_use);
965 return p;
966}
967
968/* Build a GIMPLE_OMP_ORDERED statement.
969
970 BODY is the sequence of statements inside a loop that will executed in
971 sequence. */
972
973gimple
974gimple_build_omp_ordered (gimple_seq body)
975{
976 gimple p = gimple_alloc (GIMPLE_OMP_ORDERED, 0);
977 if (body)
978 gimple_omp_set_body (p, body);
979
980 return p;
981}
982
983
984/* Build a GIMPLE_OMP_RETURN statement.
985 WAIT_P is true if this is a non-waiting return. */
986
987gimple
988gimple_build_omp_return (bool wait_p)
989{
990 gimple p = gimple_alloc (GIMPLE_OMP_RETURN, 0);
991 if (wait_p)
992 gimple_omp_return_set_nowait (p);
993
994 return p;
995}
996
997
998/* Build a GIMPLE_OMP_SECTIONS statement.
999
1000 BODY is a sequence of section statements.
1001 CLAUSES are any of the OMP sections contsruct's clauses: private,
1002 firstprivate, lastprivate, reduction, and nowait. */
1003
1004gimple
1005gimple_build_omp_sections (gimple_seq body, tree clauses)
1006{
1007 gimple p = gimple_alloc (GIMPLE_OMP_SECTIONS, 0);
1008 if (body)
1009 gimple_omp_set_body (p, body);
1010 gimple_omp_sections_set_clauses (p, clauses);
1011
1012 return p;
1013}
1014
1015
1016/* Build a GIMPLE_OMP_SECTIONS_SWITCH. */
1017
1018gimple
1019gimple_build_omp_sections_switch (void)
1020{
1021 return gimple_alloc (GIMPLE_OMP_SECTIONS_SWITCH, 0);
1022}
1023
1024
1025/* Build a GIMPLE_OMP_SINGLE statement.
1026
1027 BODY is the sequence of statements that will be executed once.
1028 CLAUSES are any of the OMP single construct's clauses: private, firstprivate,
1029 copyprivate, nowait. */
1030
1031gimple
1032gimple_build_omp_single (gimple_seq body, tree clauses)
1033{
1034 gimple p = gimple_alloc (GIMPLE_OMP_SINGLE, 0);
1035 if (body)
1036 gimple_omp_set_body (p, body);
1037 gimple_omp_single_set_clauses (p, clauses);
1038
1039 return p;
1040}
1041
1042
1043/* Build a GIMPLE_CHANGE_DYNAMIC_TYPE statement. TYPE is the new type
1044 for the location PTR. */
1045
1046gimple
1047gimple_build_cdt (tree type, tree ptr)
1048{
1049 gimple p = gimple_build_with_ops (GIMPLE_CHANGE_DYNAMIC_TYPE, 0, 2);
1050 gimple_cdt_set_new_type (p, type);
1051 gimple_cdt_set_location (p, ptr);
1052
1053 return p;
1054}
1055
1056
1057/* Build a GIMPLE_OMP_ATOMIC_LOAD statement. */
1058
1059gimple
1060gimple_build_omp_atomic_load (tree lhs, tree rhs)
1061{
1062 gimple p = gimple_alloc (GIMPLE_OMP_ATOMIC_LOAD, 0);
1063 gimple_omp_atomic_load_set_lhs (p, lhs);
1064 gimple_omp_atomic_load_set_rhs (p, rhs);
1065 return p;
1066}
1067
1068/* Build a GIMPLE_OMP_ATOMIC_STORE statement.
1069
1070 VAL is the value we are storing. */
1071
1072gimple
1073gimple_build_omp_atomic_store (tree val)
1074{
1075 gimple p = gimple_alloc (GIMPLE_OMP_ATOMIC_STORE, 0);
1076 gimple_omp_atomic_store_set_val (p, val);
1077 return p;
1078}
1079
1080/* Build a GIMPLE_PREDICT statement. PREDICT is one of the predictors from
1081 predict.def, OUTCOME is NOT_TAKEN or TAKEN. */
1082
1083gimple
1084gimple_build_predict (enum br_predictor predictor, enum prediction outcome)
1085{
1086 gimple p = gimple_alloc (GIMPLE_PREDICT, 0);
1087 /* Ensure all the predictors fit into the lower bits of the subcode. */
1088 gcc_assert (END_PREDICTORS <= GF_PREDICT_TAKEN);
1089 gimple_predict_set_predictor (p, predictor);
1090 gimple_predict_set_outcome (p, outcome);
1091 return p;
1092}
1093
1094/* Return which gimple structure is used by T. The enums here are defined
1095 in gsstruct.def. */
1096
1097enum gimple_statement_structure_enum
1098gimple_statement_structure (gimple gs)
1099{
1100 return gss_for_code (gimple_code (gs));
1101}
1102
1103#if defined ENABLE_GIMPLE_CHECKING && (GCC_VERSION >= 2007)
1104/* Complain of a gimple type mismatch and die. */
1105
1106void
1107gimple_check_failed (const_gimple gs, const char *file, int line,
1108 const char *function, enum gimple_code code,
1109 enum tree_code subcode)
1110{
1111 internal_error ("gimple check: expected %s(%s), have %s(%s) in %s, at %s:%d",
1112 gimple_code_name[code],
1113 tree_code_name[subcode],
1114 gimple_code_name[gimple_code (gs)],
1115 gs->gsbase.subcode > 0
1116 ? tree_code_name[gs->gsbase.subcode]
1117 : "",
1118 function, trim_filename (file), line);
1119}
1120
1121
1122/* Similar to gimple_check_failed, except that instead of specifying a
1123 dozen codes, use the knowledge that they're all sequential. */
1124
1125void
1126gimple_range_check_failed (const_gimple gs, const char *file, int line,
1127 const char *function, enum gimple_code c1,
1128 enum gimple_code c2)
1129{
1130 char *buffer;
1131 unsigned length = 0;
1132 enum gimple_code c;
1133
1134 for (c = c1; c <= c2; ++c)
1135 length += 4 + strlen (gimple_code_name[c]);
1136
1137 length += strlen ("expected ");
1138 buffer = XALLOCAVAR (char, length);
1139 length = 0;
1140
1141 for (c = c1; c <= c2; ++c)
1142 {
1143 const char *prefix = length ? " or " : "expected ";
1144
1145 strcpy (buffer + length, prefix);
1146 length += strlen (prefix);
1147 strcpy (buffer + length, gimple_code_name[c]);
1148 length += strlen (gimple_code_name[c]);
1149 }
1150
1151 internal_error ("gimple check: %s, have %s in %s, at %s:%d",
1152 buffer, gimple_code_name[gimple_code (gs)],
1153 function, trim_filename (file), line);
1154}
1155#endif /* ENABLE_GIMPLE_CHECKING */
1156
1157
1158/* Allocate a new GIMPLE sequence in GC memory and return it. If
1159 there are free sequences in GIMPLE_SEQ_CACHE return one of those
1160 instead. */
1161
1162gimple_seq
1163gimple_seq_alloc (void)
1164{
1165 gimple_seq seq = gimple_seq_cache;
1166 if (seq)
1167 {
1168 gimple_seq_cache = gimple_seq_cache->next_free;
1169 gcc_assert (gimple_seq_cache != seq);
1170 memset (seq, 0, sizeof (*seq));
1171 }
1172 else
1173 {
1174 seq = (gimple_seq) ggc_alloc_cleared (sizeof (*seq));
1175#ifdef GATHER_STATISTICS
1176 gimple_alloc_counts[(int) gimple_alloc_kind_seq]++;
1177 gimple_alloc_sizes[(int) gimple_alloc_kind_seq] += sizeof (*seq);
1178#endif
1179 }
1180
1181 return seq;
1182}
1183
1184/* Return SEQ to the free pool of GIMPLE sequences. */
1185
1186void
1187gimple_seq_free (gimple_seq seq)
1188{
1189 if (seq == NULL)
1190 return;
1191
1192 gcc_assert (gimple_seq_first (seq) == NULL);
1193 gcc_assert (gimple_seq_last (seq) == NULL);
1194
1195 /* If this triggers, it's a sign that the same list is being freed
1196 twice. */
1197 gcc_assert (seq != gimple_seq_cache || gimple_seq_cache == NULL);
1198
1199 /* Add SEQ to the pool of free sequences. */
1200 seq->next_free = gimple_seq_cache;
1201 gimple_seq_cache = seq;
1202}
1203
1204
1205/* Link gimple statement GS to the end of the sequence *SEQ_P. If
1206 *SEQ_P is NULL, a new sequence is allocated. */
1207
1208void
1209gimple_seq_add_stmt (gimple_seq *seq_p, gimple gs)
1210{
1211 gimple_stmt_iterator si;
1212
1213 if (gs == NULL)
1214 return;
1215
1216 if (*seq_p == NULL)
1217 *seq_p = gimple_seq_alloc ();
1218
1219 si = gsi_last (*seq_p);
1220 gsi_insert_after (&si, gs, GSI_NEW_STMT);
1221}
1222
1223
1224/* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
1225 NULL, a new sequence is allocated. */
1226
1227void
1228gimple_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
1229{
1230 gimple_stmt_iterator si;
1231
1232 if (src == NULL)
1233 return;
1234
1235 if (*dst_p == NULL)
1236 *dst_p = gimple_seq_alloc ();
1237
1238 si = gsi_last (*dst_p);
1239 gsi_insert_seq_after (&si, src, GSI_NEW_STMT);
1240}
1241
1242
1243/* Helper function of empty_body_p. Return true if STMT is an empty
1244 statement. */
1245
1246static bool
1247empty_stmt_p (gimple stmt)
1248{
1249 if (gimple_code (stmt) == GIMPLE_NOP)
1250 return true;
1251 if (gimple_code (stmt) == GIMPLE_BIND)
1252 return empty_body_p (gimple_bind_body (stmt));
1253 return false;
1254}
1255
1256
1257/* Return true if BODY contains nothing but empty statements. */
1258
1259bool
1260empty_body_p (gimple_seq body)
1261{
1262 gimple_stmt_iterator i;
1263
1264
1265 if (gimple_seq_empty_p (body))
1266 return true;
1267 for (i = gsi_start (body); !gsi_end_p (i); gsi_next (&i))
1268 if (!empty_stmt_p (gsi_stmt (i)))
1269 return false;
1270
1271 return true;
1272}
1273
1274
1275/* Perform a deep copy of sequence SRC and return the result. */
1276
1277gimple_seq
1278gimple_seq_copy (gimple_seq src)
1279{
1280 gimple_stmt_iterator gsi;
82d6e6fc 1281 gimple_seq new_seq = gimple_seq_alloc ();
726a989a
RB
1282 gimple stmt;
1283
1284 for (gsi = gsi_start (src); !gsi_end_p (gsi); gsi_next (&gsi))
1285 {
1286 stmt = gimple_copy (gsi_stmt (gsi));
82d6e6fc 1287 gimple_seq_add_stmt (&new_seq, stmt);
726a989a
RB
1288 }
1289
82d6e6fc 1290 return new_seq;
726a989a
RB
1291}
1292
1293
1294/* Walk all the statements in the sequence SEQ calling walk_gimple_stmt
1295 on each one. WI is as in walk_gimple_stmt.
1296
1297 If walk_gimple_stmt returns non-NULL, the walk is stopped, the
1298 value is stored in WI->CALLBACK_RESULT and the statement that
1299 produced the value is returned.
1300
1301 Otherwise, all the statements are walked and NULL returned. */
1302
1303gimple
1304walk_gimple_seq (gimple_seq seq, walk_stmt_fn callback_stmt,
1305 walk_tree_fn callback_op, struct walk_stmt_info *wi)
1306{
1307 gimple_stmt_iterator gsi;
1308
1309 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
1310 {
1311 tree ret = walk_gimple_stmt (&gsi, callback_stmt, callback_op, wi);
1312 if (ret)
1313 {
1314 /* If CALLBACK_STMT or CALLBACK_OP return a value, WI must exist
1315 to hold it. */
1316 gcc_assert (wi);
1317 wi->callback_result = ret;
1318 return gsi_stmt (gsi);
1319 }
1320 }
1321
1322 if (wi)
1323 wi->callback_result = NULL_TREE;
1324
1325 return NULL;
1326}
1327
1328
1329/* Helper function for walk_gimple_stmt. Walk operands of a GIMPLE_ASM. */
1330
1331static tree
1332walk_gimple_asm (gimple stmt, walk_tree_fn callback_op,
1333 struct walk_stmt_info *wi)
1334{
1335 tree ret;
1336 unsigned noutputs;
1337 const char **oconstraints;
1338 unsigned i;
1339 const char *constraint;
1340 bool allows_mem, allows_reg, is_inout;
1341
1342 noutputs = gimple_asm_noutputs (stmt);
1343 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
1344
1345 if (wi)
1346 wi->is_lhs = true;
1347
1348 for (i = 0; i < noutputs; i++)
1349 {
1350 tree op = gimple_asm_output_op (stmt, i);
1351 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
1352 oconstraints[i] = constraint;
1353 parse_output_constraint (&constraint, i, 0, 0, &allows_mem, &allows_reg,
1354 &is_inout);
1355 if (wi)
1356 wi->val_only = (allows_reg || !allows_mem);
1357 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1358 if (ret)
1359 return ret;
1360 }
1361
1362 for (i = 0; i < gimple_asm_ninputs (stmt); i++)
1363 {
1364 tree op = gimple_asm_input_op (stmt, i);
1365 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
1366 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1367 oconstraints, &allows_mem, &allows_reg);
1368 if (wi)
1369 wi->val_only = (allows_reg || !allows_mem);
1370
1371 /* Although input "m" is not really a LHS, we need a lvalue. */
1372 if (wi)
1373 wi->is_lhs = !wi->val_only;
1374 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1375 if (ret)
1376 return ret;
1377 }
1378
1379 if (wi)
1380 {
1381 wi->is_lhs = false;
1382 wi->val_only = true;
1383 }
1384
1385 return NULL_TREE;
1386}
1387
1388
1389/* Helper function of WALK_GIMPLE_STMT. Walk every tree operand in
1390 STMT. CALLBACK_OP and WI are as in WALK_GIMPLE_STMT.
1391
1392 CALLBACK_OP is called on each operand of STMT via walk_tree.
1393 Additional parameters to walk_tree must be stored in WI. For each operand
1394 OP, walk_tree is called as:
1395
1396 walk_tree (&OP, CALLBACK_OP, WI, WI->PSET)
1397
1398 If CALLBACK_OP returns non-NULL for an operand, the remaining
1399 operands are not scanned.
1400
1401 The return value is that returned by the last call to walk_tree, or
1402 NULL_TREE if no CALLBACK_OP is specified. */
1403
1404inline tree
1405walk_gimple_op (gimple stmt, walk_tree_fn callback_op,
1406 struct walk_stmt_info *wi)
1407{
1408 struct pointer_set_t *pset = (wi) ? wi->pset : NULL;
1409 unsigned i;
1410 tree ret = NULL_TREE;
1411
1412 switch (gimple_code (stmt))
1413 {
1414 case GIMPLE_ASSIGN:
1415 /* Walk the RHS operands. A formal temporary LHS may use a
1416 COMPONENT_REF RHS. */
1417 if (wi)
1418 wi->val_only = !is_gimple_formal_tmp_var (gimple_assign_lhs (stmt));
1419
1420 for (i = 1; i < gimple_num_ops (stmt); i++)
1421 {
1422 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi,
1423 pset);
1424 if (ret)
1425 return ret;
1426 }
1427
1428 /* Walk the LHS. If the RHS is appropriate for a memory, we
1429 may use a COMPONENT_REF on the LHS. */
1430 if (wi)
1431 {
1432 /* If the RHS has more than 1 operand, it is not appropriate
1433 for the memory. */
1434 wi->val_only = !is_gimple_mem_rhs (gimple_assign_rhs1 (stmt))
1435 || !gimple_assign_single_p (stmt);
1436 wi->is_lhs = true;
1437 }
1438
1439 ret = walk_tree (gimple_op_ptr (stmt, 0), callback_op, wi, pset);
1440 if (ret)
1441 return ret;
1442
1443 if (wi)
1444 {
1445 wi->val_only = true;
1446 wi->is_lhs = false;
1447 }
1448 break;
1449
1450 case GIMPLE_CALL:
1451 if (wi)
1452 wi->is_lhs = false;
1453
1454 ret = walk_tree (gimple_call_chain_ptr (stmt), callback_op, wi, pset);
1455 if (ret)
1456 return ret;
1457
1458 ret = walk_tree (gimple_call_fn_ptr (stmt), callback_op, wi, pset);
1459 if (ret)
1460 return ret;
1461
1462 for (i = 0; i < gimple_call_num_args (stmt); i++)
1463 {
1464 ret = walk_tree (gimple_call_arg_ptr (stmt, i), callback_op, wi,
1465 pset);
1466 if (ret)
1467 return ret;
1468 }
1469
1470 if (wi)
1471 wi->is_lhs = true;
1472
1473 ret = walk_tree (gimple_call_lhs_ptr (stmt), callback_op, wi, pset);
1474 if (ret)
1475 return ret;
1476
1477 if (wi)
1478 wi->is_lhs = false;
1479 break;
1480
1481 case GIMPLE_CATCH:
1482 ret = walk_tree (gimple_catch_types_ptr (stmt), callback_op, wi,
1483 pset);
1484 if (ret)
1485 return ret;
1486 break;
1487
1488 case GIMPLE_EH_FILTER:
1489 ret = walk_tree (gimple_eh_filter_types_ptr (stmt), callback_op, wi,
1490 pset);
1491 if (ret)
1492 return ret;
1493 break;
1494
1495 case GIMPLE_CHANGE_DYNAMIC_TYPE:
1496 ret = walk_tree (gimple_cdt_location_ptr (stmt), callback_op, wi, pset);
1497 if (ret)
1498 return ret;
1499
1500 ret = walk_tree (gimple_cdt_new_type_ptr (stmt), callback_op, wi, pset);
1501 if (ret)
1502 return ret;
1503 break;
1504
1505 case GIMPLE_ASM:
1506 ret = walk_gimple_asm (stmt, callback_op, wi);
1507 if (ret)
1508 return ret;
1509 break;
1510
1511 case GIMPLE_OMP_CONTINUE:
1512 ret = walk_tree (gimple_omp_continue_control_def_ptr (stmt),
1513 callback_op, wi, pset);
1514 if (ret)
1515 return ret;
1516
1517 ret = walk_tree (gimple_omp_continue_control_use_ptr (stmt),
1518 callback_op, wi, pset);
1519 if (ret)
1520 return ret;
1521 break;
1522
1523 case GIMPLE_OMP_CRITICAL:
1524 ret = walk_tree (gimple_omp_critical_name_ptr (stmt), callback_op, wi,
1525 pset);
1526 if (ret)
1527 return ret;
1528 break;
1529
1530 case GIMPLE_OMP_FOR:
1531 ret = walk_tree (gimple_omp_for_clauses_ptr (stmt), callback_op, wi,
1532 pset);
1533 if (ret)
1534 return ret;
1535 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
1536 {
1537 ret = walk_tree (gimple_omp_for_index_ptr (stmt, i), callback_op,
1538 wi, pset);
1539 if (ret)
1540 return ret;
1541 ret = walk_tree (gimple_omp_for_initial_ptr (stmt, i), callback_op,
1542 wi, pset);
1543 if (ret)
1544 return ret;
1545 ret = walk_tree (gimple_omp_for_final_ptr (stmt, i), callback_op,
1546 wi, pset);
1547 if (ret)
1548 return ret;
1549 ret = walk_tree (gimple_omp_for_incr_ptr (stmt, i), callback_op,
1550 wi, pset);
1551 }
1552 if (ret)
1553 return ret;
1554 break;
1555
1556 case GIMPLE_OMP_PARALLEL:
1557 ret = walk_tree (gimple_omp_parallel_clauses_ptr (stmt), callback_op,
1558 wi, pset);
1559 if (ret)
1560 return ret;
1561 ret = walk_tree (gimple_omp_parallel_child_fn_ptr (stmt), callback_op,
1562 wi, pset);
1563 if (ret)
1564 return ret;
1565 ret = walk_tree (gimple_omp_parallel_data_arg_ptr (stmt), callback_op,
1566 wi, pset);
1567 if (ret)
1568 return ret;
1569 break;
1570
1571 case GIMPLE_OMP_TASK:
1572 ret = walk_tree (gimple_omp_task_clauses_ptr (stmt), callback_op,
1573 wi, pset);
1574 if (ret)
1575 return ret;
1576 ret = walk_tree (gimple_omp_task_child_fn_ptr (stmt), callback_op,
1577 wi, pset);
1578 if (ret)
1579 return ret;
1580 ret = walk_tree (gimple_omp_task_data_arg_ptr (stmt), callback_op,
1581 wi, pset);
1582 if (ret)
1583 return ret;
1584 ret = walk_tree (gimple_omp_task_copy_fn_ptr (stmt), callback_op,
1585 wi, pset);
1586 if (ret)
1587 return ret;
1588 ret = walk_tree (gimple_omp_task_arg_size_ptr (stmt), callback_op,
1589 wi, pset);
1590 if (ret)
1591 return ret;
1592 ret = walk_tree (gimple_omp_task_arg_align_ptr (stmt), callback_op,
1593 wi, pset);
1594 if (ret)
1595 return ret;
1596 break;
1597
1598 case GIMPLE_OMP_SECTIONS:
1599 ret = walk_tree (gimple_omp_sections_clauses_ptr (stmt), callback_op,
1600 wi, pset);
1601 if (ret)
1602 return ret;
1603
1604 ret = walk_tree (gimple_omp_sections_control_ptr (stmt), callback_op,
1605 wi, pset);
1606 if (ret)
1607 return ret;
1608
1609 break;
1610
1611 case GIMPLE_OMP_SINGLE:
1612 ret = walk_tree (gimple_omp_single_clauses_ptr (stmt), callback_op, wi,
1613 pset);
1614 if (ret)
1615 return ret;
1616 break;
1617
1618 case GIMPLE_OMP_ATOMIC_LOAD:
1619 ret = walk_tree (gimple_omp_atomic_load_lhs_ptr (stmt), callback_op, wi,
1620 pset);
1621 if (ret)
1622 return ret;
1623
1624 ret = walk_tree (gimple_omp_atomic_load_rhs_ptr (stmt), callback_op, wi,
1625 pset);
1626 if (ret)
1627 return ret;
1628 break;
1629
1630 case GIMPLE_OMP_ATOMIC_STORE:
1631 ret = walk_tree (gimple_omp_atomic_store_val_ptr (stmt), callback_op,
1632 wi, pset);
1633 if (ret)
1634 return ret;
1635 break;
1636
1637 /* Tuples that do not have operands. */
1638 case GIMPLE_NOP:
1639 case GIMPLE_RESX:
1640 case GIMPLE_OMP_RETURN:
1641 case GIMPLE_PREDICT:
1642 break;
1643
1644 default:
1645 {
1646 enum gimple_statement_structure_enum gss;
1647 gss = gimple_statement_structure (stmt);
1648 if (gss == GSS_WITH_OPS || gss == GSS_WITH_MEM_OPS)
1649 for (i = 0; i < gimple_num_ops (stmt); i++)
1650 {
1651 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi, pset);
1652 if (ret)
1653 return ret;
1654 }
1655 }
1656 break;
1657 }
1658
1659 return NULL_TREE;
1660}
1661
1662
1663/* Walk the current statement in GSI (optionally using traversal state
1664 stored in WI). If WI is NULL, no state is kept during traversal.
1665 The callback CALLBACK_STMT is called. If CALLBACK_STMT indicates
1666 that it has handled all the operands of the statement, its return
1667 value is returned. Otherwise, the return value from CALLBACK_STMT
1668 is discarded and its operands are scanned.
1669
1670 If CALLBACK_STMT is NULL or it didn't handle the operands,
1671 CALLBACK_OP is called on each operand of the statement via
1672 walk_gimple_op. If walk_gimple_op returns non-NULL for any
1673 operand, the remaining operands are not scanned. In this case, the
1674 return value from CALLBACK_OP is returned.
1675
1676 In any other case, NULL_TREE is returned. */
1677
1678tree
1679walk_gimple_stmt (gimple_stmt_iterator *gsi, walk_stmt_fn callback_stmt,
1680 walk_tree_fn callback_op, struct walk_stmt_info *wi)
1681{
1682 gimple ret;
1683 tree tree_ret;
1684 gimple stmt = gsi_stmt (*gsi);
1685
1686 if (wi)
1687 wi->gsi = *gsi;
1688
1689 if (wi && wi->want_locations && gimple_has_location (stmt))
1690 input_location = gimple_location (stmt);
1691
1692 ret = NULL;
1693
1694 /* Invoke the statement callback. Return if the callback handled
1695 all of STMT operands by itself. */
1696 if (callback_stmt)
1697 {
1698 bool handled_ops = false;
1699 tree_ret = callback_stmt (gsi, &handled_ops, wi);
1700 if (handled_ops)
1701 return tree_ret;
1702
1703 /* If CALLBACK_STMT did not handle operands, it should not have
1704 a value to return. */
1705 gcc_assert (tree_ret == NULL);
1706
1707 /* Re-read stmt in case the callback changed it. */
1708 stmt = gsi_stmt (*gsi);
1709 }
1710
1711 /* If CALLBACK_OP is defined, invoke it on every operand of STMT. */
1712 if (callback_op)
1713 {
1714 tree_ret = walk_gimple_op (stmt, callback_op, wi);
1715 if (tree_ret)
1716 return tree_ret;
1717 }
1718
1719 /* If STMT can have statements inside (e.g. GIMPLE_BIND), walk them. */
1720 switch (gimple_code (stmt))
1721 {
1722 case GIMPLE_BIND:
1723 ret = walk_gimple_seq (gimple_bind_body (stmt), callback_stmt,
1724 callback_op, wi);
1725 if (ret)
1726 return wi->callback_result;
1727 break;
1728
1729 case GIMPLE_CATCH:
1730 ret = walk_gimple_seq (gimple_catch_handler (stmt), callback_stmt,
1731 callback_op, wi);
1732 if (ret)
1733 return wi->callback_result;
1734 break;
1735
1736 case GIMPLE_EH_FILTER:
1737 ret = walk_gimple_seq (gimple_eh_filter_failure (stmt), callback_stmt,
1738 callback_op, wi);
1739 if (ret)
1740 return wi->callback_result;
1741 break;
1742
1743 case GIMPLE_TRY:
1744 ret = walk_gimple_seq (gimple_try_eval (stmt), callback_stmt, callback_op,
1745 wi);
1746 if (ret)
1747 return wi->callback_result;
1748
1749 ret = walk_gimple_seq (gimple_try_cleanup (stmt), callback_stmt,
1750 callback_op, wi);
1751 if (ret)
1752 return wi->callback_result;
1753 break;
1754
1755 case GIMPLE_OMP_FOR:
1756 ret = walk_gimple_seq (gimple_omp_for_pre_body (stmt), callback_stmt,
1757 callback_op, wi);
1758 if (ret)
1759 return wi->callback_result;
1760
1761 /* FALL THROUGH. */
1762 case GIMPLE_OMP_CRITICAL:
1763 case GIMPLE_OMP_MASTER:
1764 case GIMPLE_OMP_ORDERED:
1765 case GIMPLE_OMP_SECTION:
1766 case GIMPLE_OMP_PARALLEL:
1767 case GIMPLE_OMP_TASK:
1768 case GIMPLE_OMP_SECTIONS:
1769 case GIMPLE_OMP_SINGLE:
1770 ret = walk_gimple_seq (gimple_omp_body (stmt), callback_stmt, callback_op,
1771 wi);
1772 if (ret)
1773 return wi->callback_result;
1774 break;
1775
1776 case GIMPLE_WITH_CLEANUP_EXPR:
1777 ret = walk_gimple_seq (gimple_wce_cleanup (stmt), callback_stmt,
1778 callback_op, wi);
1779 if (ret)
1780 return wi->callback_result;
1781 break;
1782
1783 default:
1784 gcc_assert (!gimple_has_substatements (stmt));
1785 break;
1786 }
1787
1788 return NULL;
1789}
1790
1791
1792/* Set sequence SEQ to be the GIMPLE body for function FN. */
1793
1794void
1795gimple_set_body (tree fndecl, gimple_seq seq)
1796{
1797 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1798 if (fn == NULL)
1799 {
1800 /* If FNDECL still does not have a function structure associated
1801 with it, then it does not make sense for it to receive a
1802 GIMPLE body. */
1803 gcc_assert (seq == NULL);
1804 }
1805 else
1806 fn->gimple_body = seq;
1807}
1808
1809
1810/* Return the body of GIMPLE statements for function FN. */
1811
1812gimple_seq
1813gimple_body (tree fndecl)
1814{
1815 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1816 return fn ? fn->gimple_body : NULL;
1817}
1818
1819
1820/* Detect flags from a GIMPLE_CALL. This is just like
1821 call_expr_flags, but for gimple tuples. */
1822
1823int
1824gimple_call_flags (const_gimple stmt)
1825{
1826 int flags;
1827 tree decl = gimple_call_fndecl (stmt);
1828 tree t;
1829
1830 if (decl)
1831 flags = flags_from_decl_or_type (decl);
1832 else
1833 {
1834 t = TREE_TYPE (gimple_call_fn (stmt));
1835 if (t && TREE_CODE (t) == POINTER_TYPE)
1836 flags = flags_from_decl_or_type (TREE_TYPE (t));
1837 else
1838 flags = 0;
1839 }
1840
1841 return flags;
1842}
1843
1844
1845/* Return true if GS is a copy assignment. */
1846
1847bool
1848gimple_assign_copy_p (gimple gs)
1849{
1850 return gimple_code (gs) == GIMPLE_ASSIGN
1851 && get_gimple_rhs_class (gimple_assign_rhs_code (gs))
1852 == GIMPLE_SINGLE_RHS
1853 && is_gimple_val (gimple_op (gs, 1));
1854}
1855
1856
1857/* Return true if GS is a SSA_NAME copy assignment. */
1858
1859bool
1860gimple_assign_ssa_name_copy_p (gimple gs)
1861{
1862 return (gimple_code (gs) == GIMPLE_ASSIGN
1863 && (get_gimple_rhs_class (gimple_assign_rhs_code (gs))
1864 == GIMPLE_SINGLE_RHS)
1865 && TREE_CODE (gimple_assign_lhs (gs)) == SSA_NAME
1866 && TREE_CODE (gimple_assign_rhs1 (gs)) == SSA_NAME);
1867}
1868
1869
1870/* Return true if GS is an assignment with a singleton RHS, i.e.,
1871 there is no operator associated with the assignment itself.
1872 Unlike gimple_assign_copy_p, this predicate returns true for
1873 any RHS operand, including those that perform an operation
1874 and do not have the semantics of a copy, such as COND_EXPR. */
1875
1876bool
1877gimple_assign_single_p (gimple gs)
1878{
1879 return (gimple_code (gs) == GIMPLE_ASSIGN
1880 && get_gimple_rhs_class (gimple_assign_rhs_code (gs))
1881 == GIMPLE_SINGLE_RHS);
1882}
1883
1884/* Return true if GS is an assignment with a unary RHS, but the
1885 operator has no effect on the assigned value. The logic is adapted
1886 from STRIP_NOPS. This predicate is intended to be used in tuplifying
1887 instances in which STRIP_NOPS was previously applied to the RHS of
1888 an assignment.
1889
1890 NOTE: In the use cases that led to the creation of this function
1891 and of gimple_assign_single_p, it is typical to test for either
1892 condition and to proceed in the same manner. In each case, the
1893 assigned value is represented by the single RHS operand of the
1894 assignment. I suspect there may be cases where gimple_assign_copy_p,
1895 gimple_assign_single_p, or equivalent logic is used where a similar
1896 treatment of unary NOPs is appropriate. */
1897
1898bool
1899gimple_assign_unary_nop_p (gimple gs)
1900{
1901 return (gimple_code (gs) == GIMPLE_ASSIGN
1a87cf0c 1902 && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (gs))
726a989a
RB
1903 || gimple_assign_rhs_code (gs) == NON_LVALUE_EXPR)
1904 && gimple_assign_rhs1 (gs) != error_mark_node
1905 && (TYPE_MODE (TREE_TYPE (gimple_assign_lhs (gs)))
1906 == TYPE_MODE (TREE_TYPE (gimple_assign_rhs1 (gs)))));
1907}
1908
1909/* Set BB to be the basic block holding G. */
1910
1911void
1912gimple_set_bb (gimple stmt, basic_block bb)
1913{
1914 stmt->gsbase.bb = bb;
1915
1916 /* If the statement is a label, add the label to block-to-labels map
1917 so that we can speed up edge creation for GIMPLE_GOTOs. */
1918 if (cfun->cfg && gimple_code (stmt) == GIMPLE_LABEL)
1919 {
1920 tree t;
1921 int uid;
1922
1923 t = gimple_label_label (stmt);
1924 uid = LABEL_DECL_UID (t);
1925 if (uid == -1)
1926 {
1927 unsigned old_len = VEC_length (basic_block, label_to_block_map);
1928 LABEL_DECL_UID (t) = uid = cfun->cfg->last_label_uid++;
1929 if (old_len <= (unsigned) uid)
1930 {
1931 unsigned new_len = 3 * uid / 2;
1932
1933 VEC_safe_grow_cleared (basic_block, gc, label_to_block_map,
1934 new_len);
1935 }
1936 }
1937
1938 VEC_replace (basic_block, label_to_block_map, uid, bb);
1939 }
1940}
1941
1942
1943/* Fold the expression computed by STMT. If the expression can be
1944 folded, return the folded result, otherwise return NULL. STMT is
1945 not modified. */
1946
1947tree
1948gimple_fold (const_gimple stmt)
1949{
1950 switch (gimple_code (stmt))
1951 {
1952 case GIMPLE_COND:
1953 return fold_binary (gimple_cond_code (stmt),
1954 boolean_type_node,
1955 gimple_cond_lhs (stmt),
1956 gimple_cond_rhs (stmt));
1957
1958 case GIMPLE_ASSIGN:
1959 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
1960 {
1961 case GIMPLE_UNARY_RHS:
1962 return fold_unary (gimple_assign_rhs_code (stmt),
1963 TREE_TYPE (gimple_assign_lhs (stmt)),
1964 gimple_assign_rhs1 (stmt));
1965 case GIMPLE_BINARY_RHS:
1966 return fold_binary (gimple_assign_rhs_code (stmt),
1967 TREE_TYPE (gimple_assign_lhs (stmt)),
1968 gimple_assign_rhs1 (stmt),
1969 gimple_assign_rhs2 (stmt));
1970 case GIMPLE_SINGLE_RHS:
1971 return fold (gimple_assign_rhs1 (stmt));
1972 default:;
1973 }
1974 break;
1975
1976 case GIMPLE_SWITCH:
1977 return gimple_switch_index (stmt);
1978
1979 case GIMPLE_CALL:
1980 return NULL_TREE;
1981
1982 default:
1983 break;
1984 }
1985
1986 gcc_unreachable ();
1987}
1988
1989
1990/* Modify the RHS of the assignment pointed-to by GSI using the
1991 operands in the expression tree EXPR.
1992
1993 NOTE: The statement pointed-to by GSI may be reallocated if it
1994 did not have enough operand slots.
1995
1996 This function is useful to convert an existing tree expression into
1997 the flat representation used for the RHS of a GIMPLE assignment.
1998 It will reallocate memory as needed to expand or shrink the number
1999 of operand slots needed to represent EXPR.
2000
2001 NOTE: If you find yourself building a tree and then calling this
2002 function, you are most certainly doing it the slow way. It is much
2003 better to build a new assignment or to use the function
2004 gimple_assign_set_rhs_with_ops, which does not require an
2005 expression tree to be built. */
2006
2007void
2008gimple_assign_set_rhs_from_tree (gimple_stmt_iterator *gsi, tree expr)
2009{
2010 enum tree_code subcode;
2011 tree op1, op2;
2012
2013 extract_ops_from_tree (expr, &subcode, &op1, &op2);
2014 gimple_assign_set_rhs_with_ops (gsi, subcode, op1, op2);
2015}
2016
2017
2018/* Set the RHS of assignment statement pointed-to by GSI to CODE with
2019 operands OP1 and OP2.
2020
2021 NOTE: The statement pointed-to by GSI may be reallocated if it
2022 did not have enough operand slots. */
2023
2024void
2025gimple_assign_set_rhs_with_ops (gimple_stmt_iterator *gsi, enum tree_code code,
2026 tree op1, tree op2)
2027{
2028 unsigned new_rhs_ops = get_gimple_rhs_num_ops (code);
2029 gimple stmt = gsi_stmt (*gsi);
2030
2031 /* If the new CODE needs more operands, allocate a new statement. */
2032 if (gimple_num_ops (stmt) < new_rhs_ops + 1)
2033 {
2034 tree lhs = gimple_assign_lhs (stmt);
2035 gimple new_stmt = gimple_alloc (gimple_code (stmt), new_rhs_ops + 1);
2036 memcpy (new_stmt, stmt, gimple_size (gimple_code (stmt)));
2037 gsi_replace (gsi, new_stmt, true);
2038 stmt = new_stmt;
2039
2040 /* The LHS needs to be reset as this also changes the SSA name
2041 on the LHS. */
2042 gimple_assign_set_lhs (stmt, lhs);
2043 }
2044
2045 gimple_set_num_ops (stmt, new_rhs_ops + 1);
2046 gimple_set_subcode (stmt, code);
2047 gimple_assign_set_rhs1 (stmt, op1);
2048 if (new_rhs_ops > 1)
2049 gimple_assign_set_rhs2 (stmt, op2);
2050}
2051
2052
2053/* Return the LHS of a statement that performs an assignment,
2054 either a GIMPLE_ASSIGN or a GIMPLE_CALL. Returns NULL_TREE
2055 for a call to a function that returns no value, or for a
2056 statement other than an assignment or a call. */
2057
2058tree
2059gimple_get_lhs (const_gimple stmt)
2060{
2061 enum tree_code code = gimple_code (stmt);
2062
2063 if (code == GIMPLE_ASSIGN)
2064 return gimple_assign_lhs (stmt);
2065 else if (code == GIMPLE_CALL)
2066 return gimple_call_lhs (stmt);
2067 else
2068 return NULL_TREE;
2069}
2070
2071
2072/* Set the LHS of a statement that performs an assignment,
2073 either a GIMPLE_ASSIGN or a GIMPLE_CALL. */
2074
2075void
2076gimple_set_lhs (gimple stmt, tree lhs)
2077{
2078 enum tree_code code = gimple_code (stmt);
2079
2080 if (code == GIMPLE_ASSIGN)
2081 gimple_assign_set_lhs (stmt, lhs);
2082 else if (code == GIMPLE_CALL)
2083 gimple_call_set_lhs (stmt, lhs);
2084 else
2085 gcc_unreachable();
2086}
2087
2088
2089/* Return a deep copy of statement STMT. All the operands from STMT
2090 are reallocated and copied using unshare_expr. The DEF, USE, VDEF
2091 and VUSE operand arrays are set to empty in the new copy. */
2092
2093gimple
2094gimple_copy (gimple stmt)
2095{
2096 enum gimple_code code = gimple_code (stmt);
2097 unsigned num_ops = gimple_num_ops (stmt);
2098 gimple copy = gimple_alloc (code, num_ops);
2099 unsigned i;
2100
2101 /* Shallow copy all the fields from STMT. */
2102 memcpy (copy, stmt, gimple_size (code));
2103
2104 /* If STMT has sub-statements, deep-copy them as well. */
2105 if (gimple_has_substatements (stmt))
2106 {
2107 gimple_seq new_seq;
2108 tree t;
2109
2110 switch (gimple_code (stmt))
2111 {
2112 case GIMPLE_BIND:
2113 new_seq = gimple_seq_copy (gimple_bind_body (stmt));
2114 gimple_bind_set_body (copy, new_seq);
2115 gimple_bind_set_vars (copy, unshare_expr (gimple_bind_vars (stmt)));
2116 gimple_bind_set_block (copy, gimple_bind_block (stmt));
2117 break;
2118
2119 case GIMPLE_CATCH:
2120 new_seq = gimple_seq_copy (gimple_catch_handler (stmt));
2121 gimple_catch_set_handler (copy, new_seq);
2122 t = unshare_expr (gimple_catch_types (stmt));
2123 gimple_catch_set_types (copy, t);
2124 break;
2125
2126 case GIMPLE_EH_FILTER:
2127 new_seq = gimple_seq_copy (gimple_eh_filter_failure (stmt));
2128 gimple_eh_filter_set_failure (copy, new_seq);
2129 t = unshare_expr (gimple_eh_filter_types (stmt));
2130 gimple_eh_filter_set_types (copy, t);
2131 break;
2132
2133 case GIMPLE_TRY:
2134 new_seq = gimple_seq_copy (gimple_try_eval (stmt));
2135 gimple_try_set_eval (copy, new_seq);
2136 new_seq = gimple_seq_copy (gimple_try_cleanup (stmt));
2137 gimple_try_set_cleanup (copy, new_seq);
2138 break;
2139
2140 case GIMPLE_OMP_FOR:
2141 new_seq = gimple_seq_copy (gimple_omp_for_pre_body (stmt));
2142 gimple_omp_for_set_pre_body (copy, new_seq);
2143 t = unshare_expr (gimple_omp_for_clauses (stmt));
2144 gimple_omp_for_set_clauses (copy, t);
2145 copy->gimple_omp_for.iter
2146 = GGC_NEWVEC (struct gimple_omp_for_iter,
2147 gimple_omp_for_collapse (stmt));
2148 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
2149 {
2150 gimple_omp_for_set_cond (copy, i,
2151 gimple_omp_for_cond (stmt, i));
2152 gimple_omp_for_set_index (copy, i,
2153 gimple_omp_for_index (stmt, i));
2154 t = unshare_expr (gimple_omp_for_initial (stmt, i));
2155 gimple_omp_for_set_initial (copy, i, t);
2156 t = unshare_expr (gimple_omp_for_final (stmt, i));
2157 gimple_omp_for_set_final (copy, i, t);
2158 t = unshare_expr (gimple_omp_for_incr (stmt, i));
2159 gimple_omp_for_set_incr (copy, i, t);
2160 }
2161 goto copy_omp_body;
2162
2163 case GIMPLE_OMP_PARALLEL:
2164 t = unshare_expr (gimple_omp_parallel_clauses (stmt));
2165 gimple_omp_parallel_set_clauses (copy, t);
2166 t = unshare_expr (gimple_omp_parallel_child_fn (stmt));
2167 gimple_omp_parallel_set_child_fn (copy, t);
2168 t = unshare_expr (gimple_omp_parallel_data_arg (stmt));
2169 gimple_omp_parallel_set_data_arg (copy, t);
2170 goto copy_omp_body;
2171
2172 case GIMPLE_OMP_TASK:
2173 t = unshare_expr (gimple_omp_task_clauses (stmt));
2174 gimple_omp_task_set_clauses (copy, t);
2175 t = unshare_expr (gimple_omp_task_child_fn (stmt));
2176 gimple_omp_task_set_child_fn (copy, t);
2177 t = unshare_expr (gimple_omp_task_data_arg (stmt));
2178 gimple_omp_task_set_data_arg (copy, t);
2179 t = unshare_expr (gimple_omp_task_copy_fn (stmt));
2180 gimple_omp_task_set_copy_fn (copy, t);
2181 t = unshare_expr (gimple_omp_task_arg_size (stmt));
2182 gimple_omp_task_set_arg_size (copy, t);
2183 t = unshare_expr (gimple_omp_task_arg_align (stmt));
2184 gimple_omp_task_set_arg_align (copy, t);
2185 goto copy_omp_body;
2186
2187 case GIMPLE_OMP_CRITICAL:
2188 t = unshare_expr (gimple_omp_critical_name (stmt));
2189 gimple_omp_critical_set_name (copy, t);
2190 goto copy_omp_body;
2191
2192 case GIMPLE_OMP_SECTIONS:
2193 t = unshare_expr (gimple_omp_sections_clauses (stmt));
2194 gimple_omp_sections_set_clauses (copy, t);
2195 t = unshare_expr (gimple_omp_sections_control (stmt));
2196 gimple_omp_sections_set_control (copy, t);
2197 /* FALLTHRU */
2198
2199 case GIMPLE_OMP_SINGLE:
2200 case GIMPLE_OMP_SECTION:
2201 case GIMPLE_OMP_MASTER:
2202 case GIMPLE_OMP_ORDERED:
2203 copy_omp_body:
2204 new_seq = gimple_seq_copy (gimple_omp_body (stmt));
2205 gimple_omp_set_body (copy, new_seq);
2206 break;
2207
2208 case GIMPLE_WITH_CLEANUP_EXPR:
2209 new_seq = gimple_seq_copy (gimple_wce_cleanup (stmt));
2210 gimple_wce_set_cleanup (copy, new_seq);
2211 break;
2212
2213 default:
2214 gcc_unreachable ();
2215 }
2216 }
2217
2218 /* Make copy of operands. */
2219 if (num_ops > 0)
2220 {
2221 for (i = 0; i < num_ops; i++)
2222 gimple_set_op (copy, i, unshare_expr (gimple_op (stmt, i)));
2223
2224 /* Clear out SSA operand vectors on COPY. Note that we cannot
2225 call the API functions for setting addresses_taken, stores
2226 and loads. These functions free the previous values, and we
2227 cannot do that on COPY as it will affect the original
2228 statement. */
2229 if (gimple_has_ops (stmt))
2230 {
2231 gimple_set_def_ops (copy, NULL);
2232 gimple_set_use_ops (copy, NULL);
2233 copy->gsops.opbase.addresses_taken = NULL;
2234 }
2235
2236 if (gimple_has_mem_ops (stmt))
2237 {
2238 gimple_set_vdef_ops (copy, NULL);
2239 gimple_set_vuse_ops (copy, NULL);
2240 copy->gsmem.membase.stores = NULL;
2241 copy->gsmem.membase.loads = NULL;
2242 }
2243
2244 update_stmt (copy);
2245 }
2246
2247 return copy;
2248}
2249
2250
2251/* Set the MODIFIED flag to MODIFIEDP, iff the gimple statement G has
2252 a MODIFIED field. */
2253
2254void
2255gimple_set_modified (gimple s, bool modifiedp)
2256{
2257 if (gimple_has_ops (s))
2258 {
2259 s->gsbase.modified = (unsigned) modifiedp;
2260
2261 if (modifiedp
2262 && cfun->gimple_df
2263 && is_gimple_call (s)
2264 && gimple_call_noreturn_p (s))
2265 VEC_safe_push (gimple, gc, MODIFIED_NORETURN_CALLS (cfun), s);
2266 }
2267}
2268
2269
2270/* Return true if statement S has side-effects. We consider a
2271 statement to have side effects if:
2272
2273 - It is a GIMPLE_CALL not marked with ECF_PURE or ECF_CONST.
2274 - Any of its operands are marked TREE_THIS_VOLATILE or TREE_SIDE_EFFECTS. */
2275
2276bool
2277gimple_has_side_effects (const_gimple s)
2278{
2279 unsigned i;
2280
2281 /* We don't have to scan the arguments to check for
2282 volatile arguments, though, at present, we still
2283 do a scan to check for TREE_SIDE_EFFECTS. */
2284 if (gimple_has_volatile_ops (s))
2285 return true;
2286
2287 if (is_gimple_call (s))
2288 {
2289 unsigned nargs = gimple_call_num_args (s);
2290
2291 if (!(gimple_call_flags (s) & (ECF_CONST | ECF_PURE)))
2292 return true;
2293 else if (gimple_call_flags (s) & ECF_LOOPING_CONST_OR_PURE)
2294 /* An infinite loop is considered a side effect. */
2295 return true;
2296
2297 if (gimple_call_lhs (s)
2298 && TREE_SIDE_EFFECTS (gimple_call_lhs (s)))
2299 {
2300 gcc_assert (gimple_has_volatile_ops (s));
2301 return true;
2302 }
2303
2304 if (TREE_SIDE_EFFECTS (gimple_call_fn (s)))
2305 return true;
2306
2307 for (i = 0; i < nargs; i++)
2308 if (TREE_SIDE_EFFECTS (gimple_call_arg (s, i)))
2309 {
2310 gcc_assert (gimple_has_volatile_ops (s));
2311 return true;
2312 }
2313
2314 return false;
2315 }
2316 else
2317 {
2318 for (i = 0; i < gimple_num_ops (s); i++)
2319 if (TREE_SIDE_EFFECTS (gimple_op (s, i)))
2320 {
2321 gcc_assert (gimple_has_volatile_ops (s));
2322 return true;
2323 }
2324 }
2325
2326 return false;
2327}
2328
2329/* Return true if the RHS of statement S has side effects.
2330 We may use it to determine if it is admissable to replace
2331 an assignment or call with a copy of a previously-computed
2332 value. In such cases, side-effects due the the LHS are
2333 preserved. */
2334
2335bool
2336gimple_rhs_has_side_effects (const_gimple s)
2337{
2338 unsigned i;
2339
2340 if (is_gimple_call (s))
2341 {
2342 unsigned nargs = gimple_call_num_args (s);
2343
2344 if (!(gimple_call_flags (s) & (ECF_CONST | ECF_PURE)))
2345 return true;
2346
2347 /* We cannot use gimple_has_volatile_ops here,
2348 because we must ignore a volatile LHS. */
2349 if (TREE_SIDE_EFFECTS (gimple_call_fn (s))
2350 || TREE_THIS_VOLATILE (gimple_call_fn (s)))
2351 {
2352 gcc_assert (gimple_has_volatile_ops (s));
2353 return true;
2354 }
2355
2356 for (i = 0; i < nargs; i++)
2357 if (TREE_SIDE_EFFECTS (gimple_call_arg (s, i))
2358 || TREE_THIS_VOLATILE (gimple_call_arg (s, i)))
2359 return true;
2360
2361 return false;
2362 }
2363 else if (is_gimple_assign (s))
2364 {
2365 /* Skip the first operand, the LHS. */
2366 for (i = 1; i < gimple_num_ops (s); i++)
2367 if (TREE_SIDE_EFFECTS (gimple_op (s, i))
2368 || TREE_THIS_VOLATILE (gimple_op (s, i)))
2369 {
2370 gcc_assert (gimple_has_volatile_ops (s));
2371 return true;
2372 }
2373 }
2374 else
2375 {
2376 /* For statements without an LHS, examine all arguments. */
2377 for (i = 0; i < gimple_num_ops (s); i++)
2378 if (TREE_SIDE_EFFECTS (gimple_op (s, i))
2379 || TREE_THIS_VOLATILE (gimple_op (s, i)))
2380 {
2381 gcc_assert (gimple_has_volatile_ops (s));
2382 return true;
2383 }
2384 }
2385
2386 return false;
2387}
2388
2389
2390/* Helper for gimple_could_trap_p and gimple_assign_rhs_could_trap_p.
2391 Return true if S can trap. If INCLUDE_LHS is true and S is a
2392 GIMPLE_ASSIGN, the LHS of the assignment is also checked.
2393 Otherwise, only the RHS of the assignment is checked. */
2394
2395static bool
2396gimple_could_trap_p_1 (gimple s, bool include_lhs)
2397{
2398 unsigned i, start;
2399 tree t, div = NULL_TREE;
2400 enum tree_code op;
2401
2402 start = (is_gimple_assign (s) && !include_lhs) ? 1 : 0;
2403
2404 for (i = start; i < gimple_num_ops (s); i++)
2405 if (tree_could_trap_p (gimple_op (s, i)))
2406 return true;
2407
2408 switch (gimple_code (s))
2409 {
2410 case GIMPLE_ASM:
2411 return gimple_asm_volatile_p (s);
2412
2413 case GIMPLE_CALL:
2414 t = gimple_call_fndecl (s);
2415 /* Assume that calls to weak functions may trap. */
2416 if (!t || !DECL_P (t) || DECL_WEAK (t))
2417 return true;
2418 return false;
2419
2420 case GIMPLE_ASSIGN:
2421 t = gimple_expr_type (s);
2422 op = gimple_assign_rhs_code (s);
2423 if (get_gimple_rhs_class (op) == GIMPLE_BINARY_RHS)
2424 div = gimple_assign_rhs2 (s);
2425 return (operation_could_trap_p (op, FLOAT_TYPE_P (t),
2426 (INTEGRAL_TYPE_P (t)
2427 && TYPE_OVERFLOW_TRAPS (t)),
2428 div));
2429
2430 default:
2431 break;
2432 }
2433
2434 return false;
2435
2436}
2437
2438
2439/* Return true if statement S can trap. */
2440
2441bool
2442gimple_could_trap_p (gimple s)
2443{
2444 return gimple_could_trap_p_1 (s, true);
2445}
2446
2447
2448/* Return true if RHS of a GIMPLE_ASSIGN S can trap. */
2449
2450bool
2451gimple_assign_rhs_could_trap_p (gimple s)
2452{
2453 gcc_assert (is_gimple_assign (s));
2454 return gimple_could_trap_p_1 (s, false);
2455}
2456
2457
2458/* Print debugging information for gimple stmts generated. */
2459
2460void
2461dump_gimple_statistics (void)
2462{
2463#ifdef GATHER_STATISTICS
2464 int i, total_tuples = 0, total_bytes = 0;
2465
2466 fprintf (stderr, "\nGIMPLE statements\n");
2467 fprintf (stderr, "Kind Stmts Bytes\n");
2468 fprintf (stderr, "---------------------------------------\n");
2469 for (i = 0; i < (int) gimple_alloc_kind_all; ++i)
2470 {
2471 fprintf (stderr, "%-20s %7d %10d\n", gimple_alloc_kind_names[i],
2472 gimple_alloc_counts[i], gimple_alloc_sizes[i]);
2473 total_tuples += gimple_alloc_counts[i];
2474 total_bytes += gimple_alloc_sizes[i];
2475 }
2476 fprintf (stderr, "---------------------------------------\n");
2477 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_tuples, total_bytes);
2478 fprintf (stderr, "---------------------------------------\n");
2479#else
2480 fprintf (stderr, "No gimple statistics\n");
2481#endif
2482}
2483
2484
2485/* Deep copy SYMS into the set of symbols stored by STMT. If SYMS is
2486 NULL or empty, the storage used is freed up. */
2487
2488void
2489gimple_set_stored_syms (gimple stmt, bitmap syms, bitmap_obstack *obs)
2490{
2491 gcc_assert (gimple_has_mem_ops (stmt));
2492
2493 if (syms == NULL || bitmap_empty_p (syms))
2494 BITMAP_FREE (stmt->gsmem.membase.stores);
2495 else
2496 {
2497 if (stmt->gsmem.membase.stores == NULL)
2498 stmt->gsmem.membase.stores = BITMAP_ALLOC (obs);
2499
2500 bitmap_copy (stmt->gsmem.membase.stores, syms);
2501 }
2502}
2503
2504
2505/* Deep copy SYMS into the set of symbols loaded by STMT. If SYMS is
2506 NULL or empty, the storage used is freed up. */
2507
2508void
2509gimple_set_loaded_syms (gimple stmt, bitmap syms, bitmap_obstack *obs)
2510{
2511 gcc_assert (gimple_has_mem_ops (stmt));
2512
2513 if (syms == NULL || bitmap_empty_p (syms))
2514 BITMAP_FREE (stmt->gsmem.membase.loads);
2515 else
2516 {
2517 if (stmt->gsmem.membase.loads == NULL)
2518 stmt->gsmem.membase.loads = BITMAP_ALLOC (obs);
2519
2520 bitmap_copy (stmt->gsmem.membase.loads, syms);
2521 }
2522}
2523
2524
2525/* Return the number of operands needed on the RHS of a GIMPLE
2526 assignment for an expression with tree code CODE. */
2527
2528unsigned
2529get_gimple_rhs_num_ops (enum tree_code code)
2530{
2531 enum gimple_rhs_class rhs_class = get_gimple_rhs_class (code);
2532
2533 if (rhs_class == GIMPLE_UNARY_RHS || rhs_class == GIMPLE_SINGLE_RHS)
2534 return 1;
2535 else if (rhs_class == GIMPLE_BINARY_RHS)
2536 return 2;
2537 else
2538 gcc_unreachable ();
2539}
2540
2541#define DEFTREECODE(SYM, STRING, TYPE, NARGS) \
2542 (unsigned char) \
2543 ((TYPE) == tcc_unary ? GIMPLE_UNARY_RHS \
2544 : ((TYPE) == tcc_binary \
2545 || (TYPE) == tcc_comparison) ? GIMPLE_BINARY_RHS \
2546 : ((TYPE) == tcc_constant \
2547 || (TYPE) == tcc_declaration \
2548 || (TYPE) == tcc_reference) ? GIMPLE_SINGLE_RHS \
2549 : ((SYM) == TRUTH_AND_EXPR \
2550 || (SYM) == TRUTH_OR_EXPR \
2551 || (SYM) == TRUTH_XOR_EXPR) ? GIMPLE_BINARY_RHS \
2552 : (SYM) == TRUTH_NOT_EXPR ? GIMPLE_UNARY_RHS \
2553 : ((SYM) == COND_EXPR \
2554 || (SYM) == CONSTRUCTOR \
2555 || (SYM) == OBJ_TYPE_REF \
2556 || (SYM) == ASSERT_EXPR \
2557 || (SYM) == ADDR_EXPR \
2558 || (SYM) == WITH_SIZE_EXPR \
2559 || (SYM) == EXC_PTR_EXPR \
2560 || (SYM) == SSA_NAME \
2561 || (SYM) == FILTER_EXPR \
2562 || (SYM) == POLYNOMIAL_CHREC \
2563 || (SYM) == DOT_PROD_EXPR \
2564 || (SYM) == VEC_COND_EXPR \
2565 || (SYM) == REALIGN_LOAD_EXPR) ? GIMPLE_SINGLE_RHS \
2566 : GIMPLE_INVALID_RHS),
2567#define END_OF_BASE_TREE_CODES (unsigned char) GIMPLE_INVALID_RHS,
2568
2569const unsigned char gimple_rhs_class_table[] = {
2570#include "all-tree.def"
2571};
2572
2573#undef DEFTREECODE
2574#undef END_OF_BASE_TREE_CODES
2575
2576/* For the definitive definition of GIMPLE, see doc/tree-ssa.texi. */
2577
2578/* Validation of GIMPLE expressions. */
2579
2580/* Return true if OP is an acceptable tree node to be used as a GIMPLE
2581 operand. */
2582
2583bool
2584is_gimple_operand (const_tree op)
2585{
2586 return op && get_gimple_rhs_class (TREE_CODE (op)) == GIMPLE_SINGLE_RHS;
2587}
2588
2589
2590/* Return true if T is a GIMPLE RHS for an assignment to a temporary. */
2591
2592bool
2593is_gimple_formal_tmp_rhs (tree t)
2594{
2595 if (is_gimple_lvalue (t) || is_gimple_val (t))
2596 return true;
2597
2598 return get_gimple_rhs_class (TREE_CODE (t)) != GIMPLE_INVALID_RHS;
2599}
2600
2601/* Returns true iff T is a valid RHS for an assignment to a renamed
2602 user -- or front-end generated artificial -- variable. */
2603
2604bool
2605is_gimple_reg_rhs (tree t)
2606{
2607 /* If the RHS of the MODIFY_EXPR may throw or make a nonlocal goto
2608 and the LHS is a user variable, then we need to introduce a formal
2609 temporary. This way the optimizers can determine that the user
2610 variable is only modified if evaluation of the RHS does not throw.
2611
2612 Don't force a temp of a non-renamable type; the copy could be
2613 arbitrarily expensive. Instead we will generate a VDEF for
2614 the assignment. */
2615
2616 if (is_gimple_reg_type (TREE_TYPE (t)) && tree_could_throw_p (t))
2617 return false;
2618
2619 return is_gimple_formal_tmp_rhs (t);
2620}
2621
2622/* Returns true iff T is a valid RHS for an assignment to an un-renamed
2623 LHS, or for a call argument. */
2624
2625bool
2626is_gimple_mem_rhs (tree t)
2627{
2628 /* If we're dealing with a renamable type, either source or dest must be
2629 a renamed variable. */
2630 if (is_gimple_reg_type (TREE_TYPE (t)))
2631 return is_gimple_val (t);
2632 else
2633 return is_gimple_formal_tmp_rhs (t);
2634}
2635
2636/* Return true if T is a valid LHS for a GIMPLE assignment expression. */
2637
2638bool
2639is_gimple_lvalue (tree t)
2640{
2641 return (is_gimple_addressable (t)
2642 || TREE_CODE (t) == WITH_SIZE_EXPR
2643 /* These are complex lvalues, but don't have addresses, so they
2644 go here. */
2645 || TREE_CODE (t) == BIT_FIELD_REF);
2646}
2647
2648/* Return true if T is a GIMPLE condition. */
2649
2650bool
2651is_gimple_condexpr (tree t)
2652{
2653 return (is_gimple_val (t) || (COMPARISON_CLASS_P (t)
2654 && !tree_could_trap_p (t)
2655 && is_gimple_val (TREE_OPERAND (t, 0))
2656 && is_gimple_val (TREE_OPERAND (t, 1))));
2657}
2658
2659/* Return true if T is something whose address can be taken. */
2660
2661bool
2662is_gimple_addressable (tree t)
2663{
2664 return (is_gimple_id (t) || handled_component_p (t) || INDIRECT_REF_P (t));
2665}
2666
2667/* Return true if T is a valid gimple constant. */
2668
2669bool
2670is_gimple_constant (const_tree t)
2671{
2672 switch (TREE_CODE (t))
2673 {
2674 case INTEGER_CST:
2675 case REAL_CST:
2676 case FIXED_CST:
2677 case STRING_CST:
2678 case COMPLEX_CST:
2679 case VECTOR_CST:
2680 return true;
2681
2682 /* Vector constant constructors are gimple invariant. */
2683 case CONSTRUCTOR:
2684 if (TREE_TYPE (t) && TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2685 return TREE_CONSTANT (t);
2686 else
2687 return false;
2688
2689 default:
2690 return false;
2691 }
2692}
2693
2694/* Return true if T is a gimple address. */
2695
2696bool
2697is_gimple_address (const_tree t)
2698{
2699 tree op;
2700
2701 if (TREE_CODE (t) != ADDR_EXPR)
2702 return false;
2703
2704 op = TREE_OPERAND (t, 0);
2705 while (handled_component_p (op))
2706 {
2707 if ((TREE_CODE (op) == ARRAY_REF
2708 || TREE_CODE (op) == ARRAY_RANGE_REF)
2709 && !is_gimple_val (TREE_OPERAND (op, 1)))
2710 return false;
2711
2712 op = TREE_OPERAND (op, 0);
2713 }
2714
2715 if (CONSTANT_CLASS_P (op) || INDIRECT_REF_P (op))
2716 return true;
2717
2718 switch (TREE_CODE (op))
2719 {
2720 case PARM_DECL:
2721 case RESULT_DECL:
2722 case LABEL_DECL:
2723 case FUNCTION_DECL:
2724 case VAR_DECL:
2725 case CONST_DECL:
2726 return true;
2727
2728 default:
2729 return false;
2730 }
2731}
2732
00fc2333
JH
2733/* Strip out all handled components that produce invariant
2734 offsets. */
726a989a 2735
00fc2333
JH
2736static const_tree
2737strip_invariant_refs (const_tree op)
726a989a 2738{
726a989a
RB
2739 while (handled_component_p (op))
2740 {
2741 switch (TREE_CODE (op))
2742 {
2743 case ARRAY_REF:
2744 case ARRAY_RANGE_REF:
2745 if (!is_gimple_constant (TREE_OPERAND (op, 1))
2746 || TREE_OPERAND (op, 2) != NULL_TREE
2747 || TREE_OPERAND (op, 3) != NULL_TREE)
00fc2333 2748 return NULL;
726a989a
RB
2749 break;
2750
2751 case COMPONENT_REF:
2752 if (TREE_OPERAND (op, 2) != NULL_TREE)
00fc2333 2753 return NULL;
726a989a
RB
2754 break;
2755
2756 default:;
2757 }
2758 op = TREE_OPERAND (op, 0);
2759 }
2760
00fc2333
JH
2761 return op;
2762}
2763
2764/* Return true if T is a gimple invariant address. */
2765
2766bool
2767is_gimple_invariant_address (const_tree t)
2768{
2769 const_tree op;
2770
2771 if (TREE_CODE (t) != ADDR_EXPR)
2772 return false;
2773
2774 op = strip_invariant_refs (TREE_OPERAND (t, 0));
2775
2776 return op && (CONSTANT_CLASS_P (op) || decl_address_invariant_p (op));
2777}
2778
2779/* Return true if T is a gimple invariant address at IPA level
2780 (so addresses of variables on stack are not allowed). */
2781
2782bool
2783is_gimple_ip_invariant_address (const_tree t)
2784{
2785 const_tree op;
2786
2787 if (TREE_CODE (t) != ADDR_EXPR)
2788 return false;
2789
2790 op = strip_invariant_refs (TREE_OPERAND (t, 0));
2791
2792 return op && (CONSTANT_CLASS_P (op) || decl_address_ip_invariant_p (op));
726a989a
RB
2793}
2794
2795/* Return true if T is a GIMPLE minimal invariant. It's a restricted
2796 form of function invariant. */
2797
2798bool
2799is_gimple_min_invariant (const_tree t)
2800{
2801 if (TREE_CODE (t) == ADDR_EXPR)
2802 return is_gimple_invariant_address (t);
2803
2804 return is_gimple_constant (t);
2805}
2806
00fc2333
JH
2807/* Return true if T is a GIMPLE interprocedural invariant. It's a restricted
2808 form of gimple minimal invariant. */
2809
2810bool
2811is_gimple_ip_invariant (const_tree t)
2812{
2813 if (TREE_CODE (t) == ADDR_EXPR)
2814 return is_gimple_ip_invariant_address (t);
2815
2816 return is_gimple_constant (t);
2817}
2818
726a989a
RB
2819/* Return true if T looks like a valid GIMPLE statement. */
2820
2821bool
2822is_gimple_stmt (tree t)
2823{
2824 const enum tree_code code = TREE_CODE (t);
2825
2826 switch (code)
2827 {
2828 case NOP_EXPR:
2829 /* The only valid NOP_EXPR is the empty statement. */
2830 return IS_EMPTY_STMT (t);
2831
2832 case BIND_EXPR:
2833 case COND_EXPR:
2834 /* These are only valid if they're void. */
2835 return TREE_TYPE (t) == NULL || VOID_TYPE_P (TREE_TYPE (t));
2836
2837 case SWITCH_EXPR:
2838 case GOTO_EXPR:
2839 case RETURN_EXPR:
2840 case LABEL_EXPR:
2841 case CASE_LABEL_EXPR:
2842 case TRY_CATCH_EXPR:
2843 case TRY_FINALLY_EXPR:
2844 case EH_FILTER_EXPR:
2845 case CATCH_EXPR:
2846 case CHANGE_DYNAMIC_TYPE_EXPR:
2847 case ASM_EXPR:
2848 case RESX_EXPR:
2849 case STATEMENT_LIST:
2850 case OMP_PARALLEL:
2851 case OMP_FOR:
2852 case OMP_SECTIONS:
2853 case OMP_SECTION:
2854 case OMP_SINGLE:
2855 case OMP_MASTER:
2856 case OMP_ORDERED:
2857 case OMP_CRITICAL:
2858 case OMP_TASK:
2859 /* These are always void. */
2860 return true;
2861
2862 case CALL_EXPR:
2863 case MODIFY_EXPR:
2864 case PREDICT_EXPR:
2865 /* These are valid regardless of their type. */
2866 return true;
2867
2868 default:
2869 return false;
2870 }
2871}
2872
2873/* Return true if T is a variable. */
2874
2875bool
2876is_gimple_variable (tree t)
2877{
2878 return (TREE_CODE (t) == VAR_DECL
2879 || TREE_CODE (t) == PARM_DECL
2880 || TREE_CODE (t) == RESULT_DECL
2881 || TREE_CODE (t) == SSA_NAME);
2882}
2883
2884/* Return true if T is a GIMPLE identifier (something with an address). */
2885
2886bool
2887is_gimple_id (tree t)
2888{
2889 return (is_gimple_variable (t)
2890 || TREE_CODE (t) == FUNCTION_DECL
2891 || TREE_CODE (t) == LABEL_DECL
2892 || TREE_CODE (t) == CONST_DECL
2893 /* Allow string constants, since they are addressable. */
2894 || TREE_CODE (t) == STRING_CST);
2895}
2896
2897/* Return true if TYPE is a suitable type for a scalar register variable. */
2898
2899bool
2900is_gimple_reg_type (tree type)
2901{
2902 /* In addition to aggregate types, we also exclude complex types if not
2903 optimizing because they can be subject to partial stores in GNU C by
2904 means of the __real__ and __imag__ operators and we cannot promote
2905 them to total stores (see gimplify_modify_expr_complex_part). */
2906 return !(AGGREGATE_TYPE_P (type)
2907 || (TREE_CODE (type) == COMPLEX_TYPE && !optimize));
2908
2909}
2910
2911/* Return true if T is a non-aggregate register variable. */
2912
2913bool
2914is_gimple_reg (tree t)
2915{
2916 if (TREE_CODE (t) == SSA_NAME)
2917 t = SSA_NAME_VAR (t);
2918
2919 if (MTAG_P (t))
2920 return false;
2921
2922 if (!is_gimple_variable (t))
2923 return false;
2924
2925 if (!is_gimple_reg_type (TREE_TYPE (t)))
2926 return false;
2927
2928 /* A volatile decl is not acceptable because we can't reuse it as
2929 needed. We need to copy it into a temp first. */
2930 if (TREE_THIS_VOLATILE (t))
2931 return false;
2932
2933 /* We define "registers" as things that can be renamed as needed,
2934 which with our infrastructure does not apply to memory. */
2935 if (needs_to_live_in_memory (t))
2936 return false;
2937
2938 /* Hard register variables are an interesting case. For those that
2939 are call-clobbered, we don't know where all the calls are, since
2940 we don't (want to) take into account which operations will turn
2941 into libcalls at the rtl level. For those that are call-saved,
2942 we don't currently model the fact that calls may in fact change
2943 global hard registers, nor do we examine ASM_CLOBBERS at the tree
2944 level, and so miss variable changes that might imply. All around,
2945 it seems safest to not do too much optimization with these at the
2946 tree level at all. We'll have to rely on the rtl optimizers to
2947 clean this up, as there we've got all the appropriate bits exposed. */
2948 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
2949 return false;
2950
2951 /* Complex and vector values must have been put into SSA-like form.
2952 That is, no assignments to the individual components. */
2953 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
2954 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2955 return DECL_GIMPLE_REG_P (t);
2956
2957 return true;
2958}
2959
2960
2961/* Returns true if T is a GIMPLE formal temporary variable. */
2962
2963bool
2964is_gimple_formal_tmp_var (tree t)
2965{
2966 if (TREE_CODE (t) == SSA_NAME)
2967 return true;
2968
2969 return TREE_CODE (t) == VAR_DECL && DECL_GIMPLE_FORMAL_TEMP_P (t);
2970}
2971
2972/* Returns true if T is a GIMPLE formal temporary register variable. */
2973
2974bool
2975is_gimple_formal_tmp_reg (tree t)
2976{
2977 /* The intent of this is to get hold of a value that won't change.
2978 An SSA_NAME qualifies no matter if its of a user variable or not. */
2979 if (TREE_CODE (t) == SSA_NAME)
2980 return true;
2981
2982 /* We don't know the lifetime characteristics of user variables. */
2983 if (!is_gimple_formal_tmp_var (t))
2984 return false;
2985
2986 /* Finally, it must be capable of being placed in a register. */
2987 return is_gimple_reg (t);
2988}
2989
2990/* Return true if T is a GIMPLE variable whose address is not needed. */
2991
2992bool
2993is_gimple_non_addressable (tree t)
2994{
2995 if (TREE_CODE (t) == SSA_NAME)
2996 t = SSA_NAME_VAR (t);
2997
2998 return (is_gimple_variable (t) && ! needs_to_live_in_memory (t));
2999}
3000
3001/* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant. */
3002
3003bool
3004is_gimple_val (tree t)
3005{
3006 /* Make loads from volatiles and memory vars explicit. */
3007 if (is_gimple_variable (t)
3008 && is_gimple_reg_type (TREE_TYPE (t))
3009 && !is_gimple_reg (t))
3010 return false;
3011
3012 /* FIXME make these decls. That can happen only when we expose the
3013 entire landing-pad construct at the tree level. */
3014 if (TREE_CODE (t) == EXC_PTR_EXPR || TREE_CODE (t) == FILTER_EXPR)
3015 return true;
3016
3017 return (is_gimple_variable (t) || is_gimple_min_invariant (t));
3018}
3019
3020/* Similarly, but accept hard registers as inputs to asm statements. */
3021
3022bool
3023is_gimple_asm_val (tree t)
3024{
3025 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
3026 return true;
3027
3028 return is_gimple_val (t);
3029}
3030
3031/* Return true if T is a GIMPLE minimal lvalue. */
3032
3033bool
3034is_gimple_min_lval (tree t)
3035{
3036 return (is_gimple_id (t) || TREE_CODE (t) == INDIRECT_REF);
3037}
3038
3039/* Return true if T is a typecast operation. */
3040
3041bool
3042is_gimple_cast (tree t)
3043{
3044 return (CONVERT_EXPR_P (t)
3045 || TREE_CODE (t) == FIX_TRUNC_EXPR);
3046}
3047
3048/* Return true if T is a valid function operand of a CALL_EXPR. */
3049
3050bool
3051is_gimple_call_addr (tree t)
3052{
3053 return (TREE_CODE (t) == OBJ_TYPE_REF || is_gimple_val (t));
3054}
3055
3056/* If T makes a function call, return the corresponding CALL_EXPR operand.
3057 Otherwise, return NULL_TREE. */
3058
3059tree
3060get_call_expr_in (tree t)
3061{
3062 if (TREE_CODE (t) == MODIFY_EXPR)
3063 t = TREE_OPERAND (t, 1);
3064 if (TREE_CODE (t) == WITH_SIZE_EXPR)
3065 t = TREE_OPERAND (t, 0);
3066 if (TREE_CODE (t) == CALL_EXPR)
3067 return t;
3068 return NULL_TREE;
3069}
3070
3071
3072/* Given a memory reference expression T, return its base address.
3073 The base address of a memory reference expression is the main
3074 object being referenced. For instance, the base address for
3075 'array[i].fld[j]' is 'array'. You can think of this as stripping
3076 away the offset part from a memory address.
3077
3078 This function calls handled_component_p to strip away all the inner
3079 parts of the memory reference until it reaches the base object. */
3080
3081tree
3082get_base_address (tree t)
3083{
3084 while (handled_component_p (t))
3085 t = TREE_OPERAND (t, 0);
3086
3087 if (SSA_VAR_P (t)
3088 || TREE_CODE (t) == STRING_CST
3089 || TREE_CODE (t) == CONSTRUCTOR
3090 || INDIRECT_REF_P (t))
3091 return t;
3092 else
3093 return NULL_TREE;
3094}
3095
3096void
3097recalculate_side_effects (tree t)
3098{
3099 enum tree_code code = TREE_CODE (t);
3100 int len = TREE_OPERAND_LENGTH (t);
3101 int i;
3102
3103 switch (TREE_CODE_CLASS (code))
3104 {
3105 case tcc_expression:
3106 switch (code)
3107 {
3108 case INIT_EXPR:
3109 case MODIFY_EXPR:
3110 case VA_ARG_EXPR:
3111 case PREDECREMENT_EXPR:
3112 case PREINCREMENT_EXPR:
3113 case POSTDECREMENT_EXPR:
3114 case POSTINCREMENT_EXPR:
3115 /* All of these have side-effects, no matter what their
3116 operands are. */
3117 return;
3118
3119 default:
3120 break;
3121 }
3122 /* Fall through. */
3123
3124 case tcc_comparison: /* a comparison expression */
3125 case tcc_unary: /* a unary arithmetic expression */
3126 case tcc_binary: /* a binary arithmetic expression */
3127 case tcc_reference: /* a reference */
3128 case tcc_vl_exp: /* a function call */
3129 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
3130 for (i = 0; i < len; ++i)
3131 {
3132 tree op = TREE_OPERAND (t, i);
3133 if (op && TREE_SIDE_EFFECTS (op))
3134 TREE_SIDE_EFFECTS (t) = 1;
3135 }
3136 break;
3137
3138 default:
3139 /* Can never be used with non-expressions. */
3140 gcc_unreachable ();
3141 }
3142}
3143
3144/* Canonicalize a tree T for use in a COND_EXPR as conditional. Returns
3145 a canonicalized tree that is valid for a COND_EXPR or NULL_TREE, if
3146 we failed to create one. */
3147
3148tree
3149canonicalize_cond_expr_cond (tree t)
3150{
3151 /* For (bool)x use x != 0. */
3152 if (TREE_CODE (t) == NOP_EXPR
3153 && TREE_TYPE (t) == boolean_type_node)
3154 {
3155 tree top0 = TREE_OPERAND (t, 0);
3156 t = build2 (NE_EXPR, TREE_TYPE (t),
3157 top0, build_int_cst (TREE_TYPE (top0), 0));
3158 }
3159 /* For !x use x == 0. */
3160 else if (TREE_CODE (t) == TRUTH_NOT_EXPR)
3161 {
3162 tree top0 = TREE_OPERAND (t, 0);
3163 t = build2 (EQ_EXPR, TREE_TYPE (t),
3164 top0, build_int_cst (TREE_TYPE (top0), 0));
3165 }
3166 /* For cmp ? 1 : 0 use cmp. */
3167 else if (TREE_CODE (t) == COND_EXPR
3168 && COMPARISON_CLASS_P (TREE_OPERAND (t, 0))
3169 && integer_onep (TREE_OPERAND (t, 1))
3170 && integer_zerop (TREE_OPERAND (t, 2)))
3171 {
3172 tree top0 = TREE_OPERAND (t, 0);
3173 t = build2 (TREE_CODE (top0), TREE_TYPE (t),
3174 TREE_OPERAND (top0, 0), TREE_OPERAND (top0, 1));
3175 }
3176
3177 if (is_gimple_condexpr (t))
3178 return t;
3179
3180 return NULL_TREE;
3181}
3182
3183#include "gt-gimple.h"