b874c9fdd3d3135e1b48bf620ba3ae4e80dfc888
[platform/upstream/linaro-gcc.git] / gcc / gimple.c
1 /* Gimple IR support functions.
2
3    Copyright (C) 2007-2016 Free Software Foundation, Inc.
4    Contributed by Aldy Hernandez <aldyh@redhat.com>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "ssa.h"
29 #include "cgraph.h"
30 #include "diagnostic.h"
31 #include "alias.h"
32 #include "fold-const.h"
33 #include "calls.h"
34 #include "stor-layout.h"
35 #include "internal-fn.h"
36 #include "tree-eh.h"
37 #include "gimple-iterator.h"
38 #include "gimple-walk.h"
39 #include "gimplify.h"
40 #include "target.h"
41 #include "builtins.h"
42
43
44 /* All the tuples have their operand vector (if present) at the very bottom
45    of the structure.  Therefore, the offset required to find the
46    operands vector the size of the structure minus the size of the 1
47    element tree array at the end (see gimple_ops).  */
48 #define DEFGSSTRUCT(SYM, STRUCT, HAS_TREE_OP) \
49         (HAS_TREE_OP ? sizeof (struct STRUCT) - sizeof (tree) : 0),
50 EXPORTED_CONST size_t gimple_ops_offset_[] = {
51 #include "gsstruct.def"
52 };
53 #undef DEFGSSTRUCT
54
55 #define DEFGSSTRUCT(SYM, STRUCT, HAS_TREE_OP) sizeof (struct STRUCT),
56 static const size_t gsstruct_code_size[] = {
57 #include "gsstruct.def"
58 };
59 #undef DEFGSSTRUCT
60
61 #define DEFGSCODE(SYM, NAME, GSSCODE)   NAME,
62 const char *const gimple_code_name[] = {
63 #include "gimple.def"
64 };
65 #undef DEFGSCODE
66
67 #define DEFGSCODE(SYM, NAME, GSSCODE)   GSSCODE,
68 EXPORTED_CONST enum gimple_statement_structure_enum gss_for_code_[] = {
69 #include "gimple.def"
70 };
71 #undef DEFGSCODE
72
73 /* Gimple stats.  */
74
75 int gimple_alloc_counts[(int) gimple_alloc_kind_all];
76 int gimple_alloc_sizes[(int) gimple_alloc_kind_all];
77
78 /* Keep in sync with gimple.h:enum gimple_alloc_kind.  */
79 static const char * const gimple_alloc_kind_names[] = {
80     "assignments",
81     "phi nodes",
82     "conditionals",
83     "everything else"
84 };
85
86 /* Static gimple tuple members.  */
87 const enum gimple_code gassign::code_;
88 const enum gimple_code gcall::code_;
89 const enum gimple_code gcond::code_;
90
91
92 /* Gimple tuple constructors.
93    Note: Any constructor taking a ``gimple_seq'' as a parameter, can
94    be passed a NULL to start with an empty sequence.  */
95
96 /* Set the code for statement G to CODE.  */
97
98 static inline void
99 gimple_set_code (gimple *g, enum gimple_code code)
100 {
101   g->code = code;
102 }
103
104 /* Return the number of bytes needed to hold a GIMPLE statement with
105    code CODE.  */
106
107 static inline size_t
108 gimple_size (enum gimple_code code)
109 {
110   return gsstruct_code_size[gss_for_code (code)];
111 }
112
113 /* Allocate memory for a GIMPLE statement with code CODE and NUM_OPS
114    operands.  */
115
116 gimple *
117 gimple_alloc_stat (enum gimple_code code, unsigned num_ops MEM_STAT_DECL)
118 {
119   size_t size;
120   gimple *stmt;
121
122   size = gimple_size (code);
123   if (num_ops > 0)
124     size += sizeof (tree) * (num_ops - 1);
125
126   if (GATHER_STATISTICS)
127     {
128       enum gimple_alloc_kind kind = gimple_alloc_kind (code);
129       gimple_alloc_counts[(int) kind]++;
130       gimple_alloc_sizes[(int) kind] += size;
131     }
132
133   stmt = ggc_alloc_cleared_gimple_statement_stat (size PASS_MEM_STAT);
134   gimple_set_code (stmt, code);
135   gimple_set_num_ops (stmt, num_ops);
136
137   /* Do not call gimple_set_modified here as it has other side
138      effects and this tuple is still not completely built.  */
139   stmt->modified = 1;
140   gimple_init_singleton (stmt);
141
142   return stmt;
143 }
144
145 /* Set SUBCODE to be the code of the expression computed by statement G.  */
146
147 static inline void
148 gimple_set_subcode (gimple *g, unsigned subcode)
149 {
150   /* We only have 16 bits for the RHS code.  Assert that we are not
151      overflowing it.  */
152   gcc_assert (subcode < (1 << 16));
153   g->subcode = subcode;
154 }
155
156
157
158 /* Build a tuple with operands.  CODE is the statement to build (which
159    must be one of the GIMPLE_WITH_OPS tuples).  SUBCODE is the subcode
160    for the new tuple.  NUM_OPS is the number of operands to allocate.  */
161
162 #define gimple_build_with_ops(c, s, n) \
163   gimple_build_with_ops_stat (c, s, n MEM_STAT_INFO)
164
165 static gimple *
166 gimple_build_with_ops_stat (enum gimple_code code, unsigned subcode,
167                             unsigned num_ops MEM_STAT_DECL)
168 {
169   gimple *s = gimple_alloc_stat (code, num_ops PASS_MEM_STAT);
170   gimple_set_subcode (s, subcode);
171
172   return s;
173 }
174
175
176 /* Build a GIMPLE_RETURN statement returning RETVAL.  */
177
178 greturn *
179 gimple_build_return (tree retval)
180 {
181   greturn *s
182     = as_a <greturn *> (gimple_build_with_ops (GIMPLE_RETURN, ERROR_MARK,
183                                                2));
184   if (retval)
185     gimple_return_set_retval (s, retval);
186   return s;
187 }
188
189 /* Reset alias information on call S.  */
190
191 void
192 gimple_call_reset_alias_info (gcall *s)
193 {
194   if (gimple_call_flags (s) & ECF_CONST)
195     memset (gimple_call_use_set (s), 0, sizeof (struct pt_solution));
196   else
197     pt_solution_reset (gimple_call_use_set (s));
198   if (gimple_call_flags (s) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
199     memset (gimple_call_clobber_set (s), 0, sizeof (struct pt_solution));
200   else
201     pt_solution_reset (gimple_call_clobber_set (s));
202 }
203
204 /* Helper for gimple_build_call, gimple_build_call_valist,
205    gimple_build_call_vec and gimple_build_call_from_tree.  Build the basic
206    components of a GIMPLE_CALL statement to function FN with NARGS
207    arguments.  */
208
209 static inline gcall *
210 gimple_build_call_1 (tree fn, unsigned nargs)
211 {
212   gcall *s
213     = as_a <gcall *> (gimple_build_with_ops (GIMPLE_CALL, ERROR_MARK,
214                                              nargs + 3));
215   if (TREE_CODE (fn) == FUNCTION_DECL)
216     fn = build_fold_addr_expr (fn);
217   gimple_set_op (s, 1, fn);
218   gimple_call_set_fntype (s, TREE_TYPE (TREE_TYPE (fn)));
219   gimple_call_reset_alias_info (s);
220   return s;
221 }
222
223
224 /* Build a GIMPLE_CALL statement to function FN with the arguments
225    specified in vector ARGS.  */
226
227 gcall *
228 gimple_build_call_vec (tree fn, vec<tree> args)
229 {
230   unsigned i;
231   unsigned nargs = args.length ();
232   gcall *call = gimple_build_call_1 (fn, nargs);
233
234   for (i = 0; i < nargs; i++)
235     gimple_call_set_arg (call, i, args[i]);
236
237   return call;
238 }
239
240
241 /* Build a GIMPLE_CALL statement to function FN.  NARGS is the number of
242    arguments.  The ... are the arguments.  */
243
244 gcall *
245 gimple_build_call (tree fn, unsigned nargs, ...)
246 {
247   va_list ap;
248   gcall *call;
249   unsigned i;
250
251   gcc_assert (TREE_CODE (fn) == FUNCTION_DECL || is_gimple_call_addr (fn));
252
253   call = gimple_build_call_1 (fn, nargs);
254
255   va_start (ap, nargs);
256   for (i = 0; i < nargs; i++)
257     gimple_call_set_arg (call, i, va_arg (ap, tree));
258   va_end (ap);
259
260   return call;
261 }
262
263
264 /* Build a GIMPLE_CALL statement to function FN.  NARGS is the number of
265    arguments.  AP contains the arguments.  */
266
267 gcall *
268 gimple_build_call_valist (tree fn, unsigned nargs, va_list ap)
269 {
270   gcall *call;
271   unsigned i;
272
273   gcc_assert (TREE_CODE (fn) == FUNCTION_DECL || is_gimple_call_addr (fn));
274
275   call = gimple_build_call_1 (fn, nargs);
276
277   for (i = 0; i < nargs; i++)
278     gimple_call_set_arg (call, i, va_arg (ap, tree));
279
280   return call;
281 }
282
283
284 /* Helper for gimple_build_call_internal and gimple_build_call_internal_vec.
285    Build the basic components of a GIMPLE_CALL statement to internal
286    function FN with NARGS arguments.  */
287
288 static inline gcall *
289 gimple_build_call_internal_1 (enum internal_fn fn, unsigned nargs)
290 {
291   gcall *s
292     = as_a <gcall *> (gimple_build_with_ops (GIMPLE_CALL, ERROR_MARK,
293                                              nargs + 3));
294   s->subcode |= GF_CALL_INTERNAL;
295   gimple_call_set_internal_fn (s, fn);
296   gimple_call_reset_alias_info (s);
297   return s;
298 }
299
300
301 /* Build a GIMPLE_CALL statement to internal function FN.  NARGS is
302    the number of arguments.  The ... are the arguments.  */
303
304 gcall *
305 gimple_build_call_internal (enum internal_fn fn, unsigned nargs, ...)
306 {
307   va_list ap;
308   gcall *call;
309   unsigned i;
310
311   call = gimple_build_call_internal_1 (fn, nargs);
312   va_start (ap, nargs);
313   for (i = 0; i < nargs; i++)
314     gimple_call_set_arg (call, i, va_arg (ap, tree));
315   va_end (ap);
316
317   return call;
318 }
319
320
321 /* Build a GIMPLE_CALL statement to internal function FN with the arguments
322    specified in vector ARGS.  */
323
324 gcall *
325 gimple_build_call_internal_vec (enum internal_fn fn, vec<tree> args)
326 {
327   unsigned i, nargs;
328   gcall *call;
329
330   nargs = args.length ();
331   call = gimple_build_call_internal_1 (fn, nargs);
332   for (i = 0; i < nargs; i++)
333     gimple_call_set_arg (call, i, args[i]);
334
335   return call;
336 }
337
338
339 /* Build a GIMPLE_CALL statement from CALL_EXPR T.  Note that T is
340    assumed to be in GIMPLE form already.  Minimal checking is done of
341    this fact.  */
342
343 gcall *
344 gimple_build_call_from_tree (tree t)
345 {
346   unsigned i, nargs;
347   gcall *call;
348   tree fndecl = get_callee_fndecl (t);
349
350   gcc_assert (TREE_CODE (t) == CALL_EXPR);
351
352   nargs = call_expr_nargs (t);
353   call = gimple_build_call_1 (fndecl ? fndecl : CALL_EXPR_FN (t), nargs);
354
355   for (i = 0; i < nargs; i++)
356     gimple_call_set_arg (call, i, CALL_EXPR_ARG (t, i));
357
358   gimple_set_block (call, TREE_BLOCK (t));
359
360   /* Carry all the CALL_EXPR flags to the new GIMPLE_CALL.  */
361   gimple_call_set_chain (call, CALL_EXPR_STATIC_CHAIN (t));
362   gimple_call_set_tail (call, CALL_EXPR_TAILCALL (t));
363   gimple_call_set_return_slot_opt (call, CALL_EXPR_RETURN_SLOT_OPT (t));
364   if (fndecl
365       && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
366       && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_ALLOCA
367           || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_ALLOCA_WITH_ALIGN))
368     gimple_call_set_alloca_for_var (call, CALL_ALLOCA_FOR_VAR_P (t));
369   else
370     gimple_call_set_from_thunk (call, CALL_FROM_THUNK_P (t));
371   gimple_call_set_va_arg_pack (call, CALL_EXPR_VA_ARG_PACK (t));
372   gimple_call_set_nothrow (call, TREE_NOTHROW (t));
373   gimple_set_no_warning (call, TREE_NO_WARNING (t));
374   gimple_call_set_with_bounds (call, CALL_WITH_BOUNDS_P (t));
375
376   return call;
377 }
378
379
380 /* Build a GIMPLE_ASSIGN statement.
381
382    LHS of the assignment.
383    RHS of the assignment which can be unary or binary.  */
384
385 gassign *
386 gimple_build_assign (tree lhs, tree rhs MEM_STAT_DECL)
387 {
388   enum tree_code subcode;
389   tree op1, op2, op3;
390
391   extract_ops_from_tree (rhs, &subcode, &op1, &op2, &op3);
392   return gimple_build_assign (lhs, subcode, op1, op2, op3 PASS_MEM_STAT);
393 }
394
395
396 /* Build a GIMPLE_ASSIGN statement with subcode SUBCODE and operands
397    OP1, OP2 and OP3.  */
398
399 static inline gassign *
400 gimple_build_assign_1 (tree lhs, enum tree_code subcode, tree op1,
401                        tree op2, tree op3 MEM_STAT_DECL)
402 {
403   unsigned num_ops;
404   gassign *p;
405
406   /* Need 1 operand for LHS and 1 or 2 for the RHS (depending on the
407      code).  */
408   num_ops = get_gimple_rhs_num_ops (subcode) + 1;
409
410   p = as_a <gassign *> (
411         gimple_build_with_ops_stat (GIMPLE_ASSIGN, (unsigned)subcode, num_ops
412                                     PASS_MEM_STAT));
413   gimple_assign_set_lhs (p, lhs);
414   gimple_assign_set_rhs1 (p, op1);
415   if (op2)
416     {
417       gcc_assert (num_ops > 2);
418       gimple_assign_set_rhs2 (p, op2);
419     }
420
421   if (op3)
422     {
423       gcc_assert (num_ops > 3);
424       gimple_assign_set_rhs3 (p, op3);
425     }
426
427   return p;
428 }
429
430 /* Build a GIMPLE_ASSIGN statement with subcode SUBCODE and operands
431    OP1, OP2 and OP3.  */
432
433 gassign *
434 gimple_build_assign (tree lhs, enum tree_code subcode, tree op1,
435                      tree op2, tree op3 MEM_STAT_DECL)
436 {
437   return gimple_build_assign_1 (lhs, subcode, op1, op2, op3 PASS_MEM_STAT);
438 }
439
440 /* Build a GIMPLE_ASSIGN statement with subcode SUBCODE and operands
441    OP1 and OP2.  */
442
443 gassign *
444 gimple_build_assign (tree lhs, enum tree_code subcode, tree op1,
445                      tree op2 MEM_STAT_DECL)
446 {
447   return gimple_build_assign_1 (lhs, subcode, op1, op2, NULL_TREE
448                                 PASS_MEM_STAT);
449 }
450
451 /* Build a GIMPLE_ASSIGN statement with subcode SUBCODE and operand OP1.  */
452
453 gassign *
454 gimple_build_assign (tree lhs, enum tree_code subcode, tree op1 MEM_STAT_DECL)
455 {
456   return gimple_build_assign_1 (lhs, subcode, op1, NULL_TREE, NULL_TREE
457                                 PASS_MEM_STAT);
458 }
459
460
461 /* Build a GIMPLE_COND statement.
462
463    PRED is the condition used to compare LHS and the RHS.
464    T_LABEL is the label to jump to if the condition is true.
465    F_LABEL is the label to jump to otherwise.  */
466
467 gcond *
468 gimple_build_cond (enum tree_code pred_code, tree lhs, tree rhs,
469                    tree t_label, tree f_label)
470 {
471   gcond *p;
472
473   gcc_assert (TREE_CODE_CLASS (pred_code) == tcc_comparison);
474   p = as_a <gcond *> (gimple_build_with_ops (GIMPLE_COND, pred_code, 4));
475   gimple_cond_set_lhs (p, lhs);
476   gimple_cond_set_rhs (p, rhs);
477   gimple_cond_set_true_label (p, t_label);
478   gimple_cond_set_false_label (p, f_label);
479   return p;
480 }
481
482 /* Build a GIMPLE_COND statement from the conditional expression tree
483    COND.  T_LABEL and F_LABEL are as in gimple_build_cond.  */
484
485 gcond *
486 gimple_build_cond_from_tree (tree cond, tree t_label, tree f_label)
487 {
488   enum tree_code code;
489   tree lhs, rhs;
490
491   gimple_cond_get_ops_from_tree (cond, &code, &lhs, &rhs);
492   return gimple_build_cond (code, lhs, rhs, t_label, f_label);
493 }
494
495 /* Set code, lhs, and rhs of a GIMPLE_COND from a suitable
496    boolean expression tree COND.  */
497
498 void
499 gimple_cond_set_condition_from_tree (gcond *stmt, tree cond)
500 {
501   enum tree_code code;
502   tree lhs, rhs;
503
504   gimple_cond_get_ops_from_tree (cond, &code, &lhs, &rhs);
505   gimple_cond_set_condition (stmt, code, lhs, rhs);
506 }
507
508 /* Build a GIMPLE_LABEL statement for LABEL.  */
509
510 glabel *
511 gimple_build_label (tree label)
512 {
513   glabel *p
514     = as_a <glabel *> (gimple_build_with_ops (GIMPLE_LABEL, ERROR_MARK, 1));
515   gimple_label_set_label (p, label);
516   return p;
517 }
518
519 /* Build a GIMPLE_GOTO statement to label DEST.  */
520
521 ggoto *
522 gimple_build_goto (tree dest)
523 {
524   ggoto *p
525     = as_a <ggoto *> (gimple_build_with_ops (GIMPLE_GOTO, ERROR_MARK, 1));
526   gimple_goto_set_dest (p, dest);
527   return p;
528 }
529
530
531 /* Build a GIMPLE_NOP statement.  */
532
533 gimple *
534 gimple_build_nop (void)
535 {
536   return gimple_alloc (GIMPLE_NOP, 0);
537 }
538
539
540 /* Build a GIMPLE_BIND statement.
541    VARS are the variables in BODY.
542    BLOCK is the containing block.  */
543
544 gbind *
545 gimple_build_bind (tree vars, gimple_seq body, tree block)
546 {
547   gbind *p = as_a <gbind *> (gimple_alloc (GIMPLE_BIND, 0));
548   gimple_bind_set_vars (p, vars);
549   if (body)
550     gimple_bind_set_body (p, body);
551   if (block)
552     gimple_bind_set_block (p, block);
553   return p;
554 }
555
556 /* Helper function to set the simple fields of a asm stmt.
557
558    STRING is a pointer to a string that is the asm blocks assembly code.
559    NINPUT is the number of register inputs.
560    NOUTPUT is the number of register outputs.
561    NCLOBBERS is the number of clobbered registers.
562    */
563
564 static inline gasm *
565 gimple_build_asm_1 (const char *string, unsigned ninputs, unsigned noutputs,
566                     unsigned nclobbers, unsigned nlabels)
567 {
568   gasm *p;
569   int size = strlen (string);
570
571   /* ASMs with labels cannot have outputs.  This should have been
572      enforced by the front end.  */
573   gcc_assert (nlabels == 0 || noutputs == 0);
574
575   p = as_a <gasm *> (
576         gimple_build_with_ops (GIMPLE_ASM, ERROR_MARK,
577                                ninputs + noutputs + nclobbers + nlabels));
578
579   p->ni = ninputs;
580   p->no = noutputs;
581   p->nc = nclobbers;
582   p->nl = nlabels;
583   p->string = ggc_alloc_string (string, size);
584
585   if (GATHER_STATISTICS)
586     gimple_alloc_sizes[(int) gimple_alloc_kind (GIMPLE_ASM)] += size;
587
588   return p;
589 }
590
591 /* Build a GIMPLE_ASM statement.
592
593    STRING is the assembly code.
594    NINPUT is the number of register inputs.
595    NOUTPUT is the number of register outputs.
596    NCLOBBERS is the number of clobbered registers.
597    INPUTS is a vector of the input register parameters.
598    OUTPUTS is a vector of the output register parameters.
599    CLOBBERS is a vector of the clobbered register parameters.
600    LABELS is a vector of destination labels.  */
601
602 gasm *
603 gimple_build_asm_vec (const char *string, vec<tree, va_gc> *inputs,
604                       vec<tree, va_gc> *outputs, vec<tree, va_gc> *clobbers,
605                       vec<tree, va_gc> *labels)
606 {
607   gasm *p;
608   unsigned i;
609
610   p = gimple_build_asm_1 (string,
611                           vec_safe_length (inputs),
612                           vec_safe_length (outputs),
613                           vec_safe_length (clobbers),
614                           vec_safe_length (labels));
615
616   for (i = 0; i < vec_safe_length (inputs); i++)
617     gimple_asm_set_input_op (p, i, (*inputs)[i]);
618
619   for (i = 0; i < vec_safe_length (outputs); i++)
620     gimple_asm_set_output_op (p, i, (*outputs)[i]);
621
622   for (i = 0; i < vec_safe_length (clobbers); i++)
623     gimple_asm_set_clobber_op (p, i, (*clobbers)[i]);
624
625   for (i = 0; i < vec_safe_length (labels); i++)
626     gimple_asm_set_label_op (p, i, (*labels)[i]);
627
628   return p;
629 }
630
631 /* Build a GIMPLE_CATCH statement.
632
633   TYPES are the catch types.
634   HANDLER is the exception handler.  */
635
636 gcatch *
637 gimple_build_catch (tree types, gimple_seq handler)
638 {
639   gcatch *p = as_a <gcatch *> (gimple_alloc (GIMPLE_CATCH, 0));
640   gimple_catch_set_types (p, types);
641   if (handler)
642     gimple_catch_set_handler (p, handler);
643
644   return p;
645 }
646
647 /* Build a GIMPLE_EH_FILTER statement.
648
649    TYPES are the filter's types.
650    FAILURE is the filter's failure action.  */
651
652 geh_filter *
653 gimple_build_eh_filter (tree types, gimple_seq failure)
654 {
655   geh_filter *p = as_a <geh_filter *> (gimple_alloc (GIMPLE_EH_FILTER, 0));
656   gimple_eh_filter_set_types (p, types);
657   if (failure)
658     gimple_eh_filter_set_failure (p, failure);
659
660   return p;
661 }
662
663 /* Build a GIMPLE_EH_MUST_NOT_THROW statement.  */
664
665 geh_mnt *
666 gimple_build_eh_must_not_throw (tree decl)
667 {
668   geh_mnt *p = as_a <geh_mnt *> (gimple_alloc (GIMPLE_EH_MUST_NOT_THROW, 0));
669
670   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
671   gcc_assert (flags_from_decl_or_type (decl) & ECF_NORETURN);
672   gimple_eh_must_not_throw_set_fndecl (p, decl);
673
674   return p;
675 }
676
677 /* Build a GIMPLE_EH_ELSE statement.  */
678
679 geh_else *
680 gimple_build_eh_else (gimple_seq n_body, gimple_seq e_body)
681 {
682   geh_else *p = as_a <geh_else *> (gimple_alloc (GIMPLE_EH_ELSE, 0));
683   gimple_eh_else_set_n_body (p, n_body);
684   gimple_eh_else_set_e_body (p, e_body);
685   return p;
686 }
687
688 /* Build a GIMPLE_TRY statement.
689
690    EVAL is the expression to evaluate.
691    CLEANUP is the cleanup expression.
692    KIND is either GIMPLE_TRY_CATCH or GIMPLE_TRY_FINALLY depending on
693    whether this is a try/catch or a try/finally respectively.  */
694
695 gtry *
696 gimple_build_try (gimple_seq eval, gimple_seq cleanup,
697                   enum gimple_try_flags kind)
698 {
699   gtry *p;
700
701   gcc_assert (kind == GIMPLE_TRY_CATCH || kind == GIMPLE_TRY_FINALLY);
702   p = as_a <gtry *> (gimple_alloc (GIMPLE_TRY, 0));
703   gimple_set_subcode (p, kind);
704   if (eval)
705     gimple_try_set_eval (p, eval);
706   if (cleanup)
707     gimple_try_set_cleanup (p, cleanup);
708
709   return p;
710 }
711
712 /* Construct a GIMPLE_WITH_CLEANUP_EXPR statement.
713
714    CLEANUP is the cleanup expression.  */
715
716 gimple *
717 gimple_build_wce (gimple_seq cleanup)
718 {
719   gimple *p = gimple_alloc (GIMPLE_WITH_CLEANUP_EXPR, 0);
720   if (cleanup)
721     gimple_wce_set_cleanup (p, cleanup);
722
723   return p;
724 }
725
726
727 /* Build a GIMPLE_RESX statement.  */
728
729 gresx *
730 gimple_build_resx (int region)
731 {
732   gresx *p
733     = as_a <gresx *> (gimple_build_with_ops (GIMPLE_RESX, ERROR_MARK, 0));
734   p->region = region;
735   return p;
736 }
737
738
739 /* The helper for constructing a gimple switch statement.
740    INDEX is the switch's index.
741    NLABELS is the number of labels in the switch excluding the default.
742    DEFAULT_LABEL is the default label for the switch statement.  */
743
744 gswitch *
745 gimple_build_switch_nlabels (unsigned nlabels, tree index, tree default_label)
746 {
747   /* nlabels + 1 default label + 1 index.  */
748   gcc_checking_assert (default_label);
749   gswitch *p = as_a <gswitch *> (gimple_build_with_ops (GIMPLE_SWITCH,
750                                                         ERROR_MARK,
751                                                         1 + 1 + nlabels));
752   gimple_switch_set_index (p, index);
753   gimple_switch_set_default_label (p, default_label);
754   return p;
755 }
756
757 /* Build a GIMPLE_SWITCH statement.
758
759    INDEX is the switch's index.
760    DEFAULT_LABEL is the default label
761    ARGS is a vector of labels excluding the default.  */
762
763 gswitch *
764 gimple_build_switch (tree index, tree default_label, vec<tree> args)
765 {
766   unsigned i, nlabels = args.length ();
767
768   gswitch *p = gimple_build_switch_nlabels (nlabels, index, default_label);
769
770   /* Copy the labels from the vector to the switch statement.  */
771   for (i = 0; i < nlabels; i++)
772     gimple_switch_set_label (p, i + 1, args[i]);
773
774   return p;
775 }
776
777 /* Build a GIMPLE_EH_DISPATCH statement.  */
778
779 geh_dispatch *
780 gimple_build_eh_dispatch (int region)
781 {
782   geh_dispatch *p
783     = as_a <geh_dispatch *> (
784         gimple_build_with_ops (GIMPLE_EH_DISPATCH, ERROR_MARK, 0));
785   p->region = region;
786   return p;
787 }
788
789 /* Build a new GIMPLE_DEBUG_BIND statement.
790
791    VAR is bound to VALUE; block and location are taken from STMT.  */
792
793 gdebug *
794 gimple_build_debug_bind_stat (tree var, tree value, gimple *stmt MEM_STAT_DECL)
795 {
796   gdebug *p
797     = as_a <gdebug *> (gimple_build_with_ops_stat (GIMPLE_DEBUG,
798                                                    (unsigned)GIMPLE_DEBUG_BIND, 2
799                                                    PASS_MEM_STAT));
800   gimple_debug_bind_set_var (p, var);
801   gimple_debug_bind_set_value (p, value);
802   if (stmt)
803     gimple_set_location (p, gimple_location (stmt));
804
805   return p;
806 }
807
808
809 /* Build a new GIMPLE_DEBUG_SOURCE_BIND statement.
810
811    VAR is bound to VALUE; block and location are taken from STMT.  */
812
813 gdebug *
814 gimple_build_debug_source_bind_stat (tree var, tree value,
815                                      gimple *stmt MEM_STAT_DECL)
816 {
817   gdebug *p
818     = as_a <gdebug *> (
819         gimple_build_with_ops_stat (GIMPLE_DEBUG,
820                                     (unsigned)GIMPLE_DEBUG_SOURCE_BIND, 2
821                                     PASS_MEM_STAT));
822
823   gimple_debug_source_bind_set_var (p, var);
824   gimple_debug_source_bind_set_value (p, value);
825   if (stmt)
826     gimple_set_location (p, gimple_location (stmt));
827
828   return p;
829 }
830
831
832 /* Build a GIMPLE_OMP_CRITICAL statement.
833
834    BODY is the sequence of statements for which only one thread can execute.
835    NAME is optional identifier for this critical block.
836    CLAUSES are clauses for this critical block.  */
837
838 gomp_critical *
839 gimple_build_omp_critical (gimple_seq body, tree name, tree clauses)
840 {
841   gomp_critical *p
842     = as_a <gomp_critical *> (gimple_alloc (GIMPLE_OMP_CRITICAL, 0));
843   gimple_omp_critical_set_name (p, name);
844   gimple_omp_critical_set_clauses (p, clauses);
845   if (body)
846     gimple_omp_set_body (p, body);
847
848   return p;
849 }
850
851 /* Build a GIMPLE_OMP_FOR statement.
852
853    BODY is sequence of statements inside the for loop.
854    KIND is the `for' variant.
855    CLAUSES, are any of the construct's clauses.
856    COLLAPSE is the collapse count.
857    PRE_BODY is the sequence of statements that are loop invariant.  */
858
859 gomp_for *
860 gimple_build_omp_for (gimple_seq body, int kind, tree clauses, size_t collapse,
861                       gimple_seq pre_body)
862 {
863   gomp_for *p = as_a <gomp_for *> (gimple_alloc (GIMPLE_OMP_FOR, 0));
864   if (body)
865     gimple_omp_set_body (p, body);
866   gimple_omp_for_set_clauses (p, clauses);
867   gimple_omp_for_set_kind (p, kind);
868   p->collapse = collapse;
869   p->iter =  ggc_cleared_vec_alloc<gimple_omp_for_iter> (collapse);
870
871   if (pre_body)
872     gimple_omp_for_set_pre_body (p, pre_body);
873
874   return p;
875 }
876
877
878 /* Build a GIMPLE_OMP_PARALLEL statement.
879
880    BODY is sequence of statements which are executed in parallel.
881    CLAUSES, are the OMP parallel construct's clauses.
882    CHILD_FN is the function created for the parallel threads to execute.
883    DATA_ARG are the shared data argument(s).  */
884
885 gomp_parallel *
886 gimple_build_omp_parallel (gimple_seq body, tree clauses, tree child_fn,
887                            tree data_arg)
888 {
889   gomp_parallel *p
890     = as_a <gomp_parallel *> (gimple_alloc (GIMPLE_OMP_PARALLEL, 0));
891   if (body)
892     gimple_omp_set_body (p, body);
893   gimple_omp_parallel_set_clauses (p, clauses);
894   gimple_omp_parallel_set_child_fn (p, child_fn);
895   gimple_omp_parallel_set_data_arg (p, data_arg);
896
897   return p;
898 }
899
900
901 /* Build a GIMPLE_OMP_TASK statement.
902
903    BODY is sequence of statements which are executed by the explicit task.
904    CLAUSES, are the OMP parallel construct's clauses.
905    CHILD_FN is the function created for the parallel threads to execute.
906    DATA_ARG are the shared data argument(s).
907    COPY_FN is the optional function for firstprivate initialization.
908    ARG_SIZE and ARG_ALIGN are size and alignment of the data block.  */
909
910 gomp_task *
911 gimple_build_omp_task (gimple_seq body, tree clauses, tree child_fn,
912                        tree data_arg, tree copy_fn, tree arg_size,
913                        tree arg_align)
914 {
915   gomp_task *p = as_a <gomp_task *> (gimple_alloc (GIMPLE_OMP_TASK, 0));
916   if (body)
917     gimple_omp_set_body (p, body);
918   gimple_omp_task_set_clauses (p, clauses);
919   gimple_omp_task_set_child_fn (p, child_fn);
920   gimple_omp_task_set_data_arg (p, data_arg);
921   gimple_omp_task_set_copy_fn (p, copy_fn);
922   gimple_omp_task_set_arg_size (p, arg_size);
923   gimple_omp_task_set_arg_align (p, arg_align);
924
925   return p;
926 }
927
928
929 /* Build a GIMPLE_OMP_SECTION statement for a sections statement.
930
931    BODY is the sequence of statements in the section.  */
932
933 gimple *
934 gimple_build_omp_section (gimple_seq body)
935 {
936   gimple *p = gimple_alloc (GIMPLE_OMP_SECTION, 0);
937   if (body)
938     gimple_omp_set_body (p, body);
939
940   return p;
941 }
942
943
944 /* Build a GIMPLE_OMP_MASTER statement.
945
946    BODY is the sequence of statements to be executed by just the master.  */
947
948 gimple *
949 gimple_build_omp_master (gimple_seq body)
950 {
951   gimple *p = gimple_alloc (GIMPLE_OMP_MASTER, 0);
952   if (body)
953     gimple_omp_set_body (p, body);
954
955   return p;
956 }
957
958 /* Build a GIMPLE_OMP_GRID_BODY statement.
959
960    BODY is the sequence of statements to be executed by the kernel.  */
961
962 gimple *
963 gimple_build_omp_grid_body (gimple_seq body)
964 {
965   gimple *p = gimple_alloc (GIMPLE_OMP_GRID_BODY, 0);
966   if (body)
967     gimple_omp_set_body (p, body);
968
969   return p;
970 }
971
972 /* Build a GIMPLE_OMP_TASKGROUP statement.
973
974    BODY is the sequence of statements to be executed by the taskgroup
975    construct.  */
976
977 gimple *
978 gimple_build_omp_taskgroup (gimple_seq body)
979 {
980   gimple *p = gimple_alloc (GIMPLE_OMP_TASKGROUP, 0);
981   if (body)
982     gimple_omp_set_body (p, body);
983
984   return p;
985 }
986
987
988 /* Build a GIMPLE_OMP_CONTINUE statement.
989
990    CONTROL_DEF is the definition of the control variable.
991    CONTROL_USE is the use of the control variable.  */
992
993 gomp_continue *
994 gimple_build_omp_continue (tree control_def, tree control_use)
995 {
996   gomp_continue *p
997     = as_a <gomp_continue *> (gimple_alloc (GIMPLE_OMP_CONTINUE, 0));
998   gimple_omp_continue_set_control_def (p, control_def);
999   gimple_omp_continue_set_control_use (p, control_use);
1000   return p;
1001 }
1002
1003 /* Build a GIMPLE_OMP_ORDERED statement.
1004
1005    BODY is the sequence of statements inside a loop that will executed in
1006    sequence.
1007    CLAUSES are clauses for this statement.  */
1008
1009 gomp_ordered *
1010 gimple_build_omp_ordered (gimple_seq body, tree clauses)
1011 {
1012   gomp_ordered *p
1013     = as_a <gomp_ordered *> (gimple_alloc (GIMPLE_OMP_ORDERED, 0));
1014   gimple_omp_ordered_set_clauses (p, clauses);
1015   if (body)
1016     gimple_omp_set_body (p, body);
1017
1018   return p;
1019 }
1020
1021
1022 /* Build a GIMPLE_OMP_RETURN statement.
1023    WAIT_P is true if this is a non-waiting return.  */
1024
1025 gimple *
1026 gimple_build_omp_return (bool wait_p)
1027 {
1028   gimple *p = gimple_alloc (GIMPLE_OMP_RETURN, 0);
1029   if (wait_p)
1030     gimple_omp_return_set_nowait (p);
1031
1032   return p;
1033 }
1034
1035
1036 /* Build a GIMPLE_OMP_SECTIONS statement.
1037
1038    BODY is a sequence of section statements.
1039    CLAUSES are any of the OMP sections contsruct's clauses: private,
1040    firstprivate, lastprivate, reduction, and nowait.  */
1041
1042 gomp_sections *
1043 gimple_build_omp_sections (gimple_seq body, tree clauses)
1044 {
1045   gomp_sections *p
1046     = as_a <gomp_sections *> (gimple_alloc (GIMPLE_OMP_SECTIONS, 0));
1047   if (body)
1048     gimple_omp_set_body (p, body);
1049   gimple_omp_sections_set_clauses (p, clauses);
1050
1051   return p;
1052 }
1053
1054
1055 /* Build a GIMPLE_OMP_SECTIONS_SWITCH.  */
1056
1057 gimple *
1058 gimple_build_omp_sections_switch (void)
1059 {
1060   return gimple_alloc (GIMPLE_OMP_SECTIONS_SWITCH, 0);
1061 }
1062
1063
1064 /* Build a GIMPLE_OMP_SINGLE statement.
1065
1066    BODY is the sequence of statements that will be executed once.
1067    CLAUSES are any of the OMP single construct's clauses: private, firstprivate,
1068    copyprivate, nowait.  */
1069
1070 gomp_single *
1071 gimple_build_omp_single (gimple_seq body, tree clauses)
1072 {
1073   gomp_single *p
1074     = as_a <gomp_single *> (gimple_alloc (GIMPLE_OMP_SINGLE, 0));
1075   if (body)
1076     gimple_omp_set_body (p, body);
1077   gimple_omp_single_set_clauses (p, clauses);
1078
1079   return p;
1080 }
1081
1082
1083 /* Build a GIMPLE_OMP_TARGET statement.
1084
1085    BODY is the sequence of statements that will be executed.
1086    KIND is the kind of the region.
1087    CLAUSES are any of the construct's clauses.  */
1088
1089 gomp_target *
1090 gimple_build_omp_target (gimple_seq body, int kind, tree clauses)
1091 {
1092   gomp_target *p
1093     = as_a <gomp_target *> (gimple_alloc (GIMPLE_OMP_TARGET, 0));
1094   if (body)
1095     gimple_omp_set_body (p, body);
1096   gimple_omp_target_set_clauses (p, clauses);
1097   gimple_omp_target_set_kind (p, kind);
1098
1099   return p;
1100 }
1101
1102
1103 /* Build a GIMPLE_OMP_TEAMS statement.
1104
1105    BODY is the sequence of statements that will be executed.
1106    CLAUSES are any of the OMP teams construct's clauses.  */
1107
1108 gomp_teams *
1109 gimple_build_omp_teams (gimple_seq body, tree clauses)
1110 {
1111   gomp_teams *p = as_a <gomp_teams *> (gimple_alloc (GIMPLE_OMP_TEAMS, 0));
1112   if (body)
1113     gimple_omp_set_body (p, body);
1114   gimple_omp_teams_set_clauses (p, clauses);
1115
1116   return p;
1117 }
1118
1119
1120 /* Build a GIMPLE_OMP_ATOMIC_LOAD statement.  */
1121
1122 gomp_atomic_load *
1123 gimple_build_omp_atomic_load (tree lhs, tree rhs)
1124 {
1125   gomp_atomic_load *p
1126     = as_a <gomp_atomic_load *> (gimple_alloc (GIMPLE_OMP_ATOMIC_LOAD, 0));
1127   gimple_omp_atomic_load_set_lhs (p, lhs);
1128   gimple_omp_atomic_load_set_rhs (p, rhs);
1129   return p;
1130 }
1131
1132 /* Build a GIMPLE_OMP_ATOMIC_STORE statement.
1133
1134    VAL is the value we are storing.  */
1135
1136 gomp_atomic_store *
1137 gimple_build_omp_atomic_store (tree val)
1138 {
1139   gomp_atomic_store *p
1140     = as_a <gomp_atomic_store *> (gimple_alloc (GIMPLE_OMP_ATOMIC_STORE, 0));
1141   gimple_omp_atomic_store_set_val (p, val);
1142   return p;
1143 }
1144
1145 /* Build a GIMPLE_TRANSACTION statement.  */
1146
1147 gtransaction *
1148 gimple_build_transaction (gimple_seq body)
1149 {
1150   gtransaction *p
1151     = as_a <gtransaction *> (gimple_alloc (GIMPLE_TRANSACTION, 0));
1152   gimple_transaction_set_body (p, body);
1153   gimple_transaction_set_label_norm (p, 0);
1154   gimple_transaction_set_label_uninst (p, 0);
1155   gimple_transaction_set_label_over (p, 0);
1156   return p;
1157 }
1158
1159 #if defined ENABLE_GIMPLE_CHECKING
1160 /* Complain of a gimple type mismatch and die.  */
1161
1162 void
1163 gimple_check_failed (const gimple *gs, const char *file, int line,
1164                      const char *function, enum gimple_code code,
1165                      enum tree_code subcode)
1166 {
1167   internal_error ("gimple check: expected %s(%s), have %s(%s) in %s, at %s:%d",
1168                   gimple_code_name[code],
1169                   get_tree_code_name (subcode),
1170                   gimple_code_name[gimple_code (gs)],
1171                   gs->subcode > 0
1172                     ? get_tree_code_name ((enum tree_code) gs->subcode)
1173                     : "",
1174                   function, trim_filename (file), line);
1175 }
1176 #endif /* ENABLE_GIMPLE_CHECKING */
1177
1178
1179 /* Link gimple statement GS to the end of the sequence *SEQ_P.  If
1180    *SEQ_P is NULL, a new sequence is allocated.  */
1181
1182 void
1183 gimple_seq_add_stmt (gimple_seq *seq_p, gimple *gs)
1184 {
1185   gimple_stmt_iterator si;
1186   if (gs == NULL)
1187     return;
1188
1189   si = gsi_last (*seq_p);
1190   gsi_insert_after (&si, gs, GSI_NEW_STMT);
1191 }
1192
1193 /* Link gimple statement GS to the end of the sequence *SEQ_P.  If
1194    *SEQ_P is NULL, a new sequence is allocated.  This function is
1195    similar to gimple_seq_add_stmt, but does not scan the operands.
1196    During gimplification, we need to manipulate statement sequences
1197    before the def/use vectors have been constructed.  */
1198
1199 void
1200 gimple_seq_add_stmt_without_update (gimple_seq *seq_p, gimple *gs)
1201 {
1202   gimple_stmt_iterator si;
1203
1204   if (gs == NULL)
1205     return;
1206
1207   si = gsi_last (*seq_p);
1208   gsi_insert_after_without_update (&si, gs, GSI_NEW_STMT);
1209 }
1210
1211 /* Append sequence SRC to the end of sequence *DST_P.  If *DST_P is
1212    NULL, a new sequence is allocated.  */
1213
1214 void
1215 gimple_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
1216 {
1217   gimple_stmt_iterator si;
1218   if (src == NULL)
1219     return;
1220
1221   si = gsi_last (*dst_p);
1222   gsi_insert_seq_after (&si, src, GSI_NEW_STMT);
1223 }
1224
1225 /* Append sequence SRC to the end of sequence *DST_P.  If *DST_P is
1226    NULL, a new sequence is allocated.  This function is
1227    similar to gimple_seq_add_seq, but does not scan the operands.  */
1228
1229 void
1230 gimple_seq_add_seq_without_update (gimple_seq *dst_p, gimple_seq src)
1231 {
1232   gimple_stmt_iterator si;
1233   if (src == NULL)
1234     return;
1235
1236   si = gsi_last (*dst_p);
1237   gsi_insert_seq_after_without_update (&si, src, GSI_NEW_STMT);
1238 }
1239
1240 /* Determine whether to assign a location to the statement GS.  */
1241
1242 static bool
1243 should_carry_location_p (gimple *gs)
1244 {
1245   /* Don't emit a line note for a label.  We particularly don't want to
1246      emit one for the break label, since it doesn't actually correspond
1247      to the beginning of the loop/switch.  */
1248   if (gimple_code (gs) == GIMPLE_LABEL)
1249     return false;
1250
1251   return true;
1252 }
1253
1254 /* Set the location for gimple statement GS to LOCATION.  */
1255
1256 static void
1257 annotate_one_with_location (gimple *gs, location_t location)
1258 {
1259   if (!gimple_has_location (gs)
1260       && !gimple_do_not_emit_location_p (gs)
1261       && should_carry_location_p (gs))
1262     gimple_set_location (gs, location);
1263 }
1264
1265 /* Set LOCATION for all the statements after iterator GSI in sequence
1266    SEQ.  If GSI is pointing to the end of the sequence, start with the
1267    first statement in SEQ.  */
1268
1269 void
1270 annotate_all_with_location_after (gimple_seq seq, gimple_stmt_iterator gsi,
1271                                   location_t location)
1272 {
1273   if (gsi_end_p (gsi))
1274     gsi = gsi_start (seq);
1275   else
1276     gsi_next (&gsi);
1277
1278   for (; !gsi_end_p (gsi); gsi_next (&gsi))
1279     annotate_one_with_location (gsi_stmt (gsi), location);
1280 }
1281
1282 /* Set the location for all the statements in a sequence STMT_P to LOCATION.  */
1283
1284 void
1285 annotate_all_with_location (gimple_seq stmt_p, location_t location)
1286 {
1287   gimple_stmt_iterator i;
1288
1289   if (gimple_seq_empty_p (stmt_p))
1290     return;
1291
1292   for (i = gsi_start (stmt_p); !gsi_end_p (i); gsi_next (&i))
1293     {
1294       gimple *gs = gsi_stmt (i);
1295       annotate_one_with_location (gs, location);
1296     }
1297 }
1298
1299 /* Helper function of empty_body_p.  Return true if STMT is an empty
1300    statement.  */
1301
1302 static bool
1303 empty_stmt_p (gimple *stmt)
1304 {
1305   if (gimple_code (stmt) == GIMPLE_NOP)
1306     return true;
1307   if (gbind *bind_stmt = dyn_cast <gbind *> (stmt))
1308     return empty_body_p (gimple_bind_body (bind_stmt));
1309   return false;
1310 }
1311
1312
1313 /* Return true if BODY contains nothing but empty statements.  */
1314
1315 bool
1316 empty_body_p (gimple_seq body)
1317 {
1318   gimple_stmt_iterator i;
1319
1320   if (gimple_seq_empty_p (body))
1321     return true;
1322   for (i = gsi_start (body); !gsi_end_p (i); gsi_next (&i))
1323     if (!empty_stmt_p (gsi_stmt (i))
1324         && !is_gimple_debug (gsi_stmt (i)))
1325       return false;
1326
1327   return true;
1328 }
1329
1330
1331 /* Perform a deep copy of sequence SRC and return the result.  */
1332
1333 gimple_seq
1334 gimple_seq_copy (gimple_seq src)
1335 {
1336   gimple_stmt_iterator gsi;
1337   gimple_seq new_seq = NULL;
1338   gimple *stmt;
1339
1340   for (gsi = gsi_start (src); !gsi_end_p (gsi); gsi_next (&gsi))
1341     {
1342       stmt = gimple_copy (gsi_stmt (gsi));
1343       gimple_seq_add_stmt (&new_seq, stmt);
1344     }
1345
1346   return new_seq;
1347 }
1348
1349
1350
1351 /* Return true if calls C1 and C2 are known to go to the same function.  */
1352
1353 bool
1354 gimple_call_same_target_p (const gimple *c1, const gimple *c2)
1355 {
1356   if (gimple_call_internal_p (c1))
1357     return (gimple_call_internal_p (c2)
1358             && gimple_call_internal_fn (c1) == gimple_call_internal_fn (c2)
1359             && (!gimple_call_internal_unique_p (as_a <const gcall *> (c1))
1360                 || c1 == c2));
1361   else
1362     return (gimple_call_fn (c1) == gimple_call_fn (c2)
1363             || (gimple_call_fndecl (c1)
1364                 && gimple_call_fndecl (c1) == gimple_call_fndecl (c2)));
1365 }
1366
1367 /* Detect flags from a GIMPLE_CALL.  This is just like
1368    call_expr_flags, but for gimple tuples.  */
1369
1370 int
1371 gimple_call_flags (const gimple *stmt)
1372 {
1373   int flags;
1374   tree decl = gimple_call_fndecl (stmt);
1375
1376   if (decl)
1377     flags = flags_from_decl_or_type (decl);
1378   else if (gimple_call_internal_p (stmt))
1379     flags = internal_fn_flags (gimple_call_internal_fn (stmt));
1380   else
1381     flags = flags_from_decl_or_type (gimple_call_fntype (stmt));
1382
1383   if (stmt->subcode & GF_CALL_NOTHROW)
1384     flags |= ECF_NOTHROW;
1385
1386   return flags;
1387 }
1388
1389 /* Return the "fn spec" string for call STMT.  */
1390
1391 static const_tree
1392 gimple_call_fnspec (const gcall *stmt)
1393 {
1394   tree type, attr;
1395
1396   if (gimple_call_internal_p (stmt))
1397     return internal_fn_fnspec (gimple_call_internal_fn (stmt));
1398
1399   type = gimple_call_fntype (stmt);
1400   if (!type)
1401     return NULL_TREE;
1402
1403   attr = lookup_attribute ("fn spec", TYPE_ATTRIBUTES (type));
1404   if (!attr)
1405     return NULL_TREE;
1406
1407   return TREE_VALUE (TREE_VALUE (attr));
1408 }
1409
1410 /* Detects argument flags for argument number ARG on call STMT.  */
1411
1412 int
1413 gimple_call_arg_flags (const gcall *stmt, unsigned arg)
1414 {
1415   const_tree attr = gimple_call_fnspec (stmt);
1416
1417   if (!attr || 1 + arg >= (unsigned) TREE_STRING_LENGTH (attr))
1418     return 0;
1419
1420   switch (TREE_STRING_POINTER (attr)[1 + arg])
1421     {
1422     case 'x':
1423     case 'X':
1424       return EAF_UNUSED;
1425
1426     case 'R':
1427       return EAF_DIRECT | EAF_NOCLOBBER | EAF_NOESCAPE;
1428
1429     case 'r':
1430       return EAF_NOCLOBBER | EAF_NOESCAPE;
1431
1432     case 'W':
1433       return EAF_DIRECT | EAF_NOESCAPE;
1434
1435     case 'w':
1436       return EAF_NOESCAPE;
1437
1438     case '.':
1439     default:
1440       return 0;
1441     }
1442 }
1443
1444 /* Detects return flags for the call STMT.  */
1445
1446 int
1447 gimple_call_return_flags (const gcall *stmt)
1448 {
1449   const_tree attr;
1450
1451   if (gimple_call_flags (stmt) & ECF_MALLOC)
1452     return ERF_NOALIAS;
1453
1454   attr = gimple_call_fnspec (stmt);
1455   if (!attr || TREE_STRING_LENGTH (attr) < 1)
1456     return 0;
1457
1458   switch (TREE_STRING_POINTER (attr)[0])
1459     {
1460     case '1':
1461     case '2':
1462     case '3':
1463     case '4':
1464       return ERF_RETURNS_ARG | (TREE_STRING_POINTER (attr)[0] - '1');
1465
1466     case 'm':
1467       return ERF_NOALIAS;
1468
1469     case '.':
1470     default:
1471       return 0;
1472     }
1473 }
1474
1475
1476 /* Return true if GS is a copy assignment.  */
1477
1478 bool
1479 gimple_assign_copy_p (gimple *gs)
1480 {
1481   return (gimple_assign_single_p (gs)
1482           && is_gimple_val (gimple_op (gs, 1)));
1483 }
1484
1485
1486 /* Return true if GS is a SSA_NAME copy assignment.  */
1487
1488 bool
1489 gimple_assign_ssa_name_copy_p (gimple *gs)
1490 {
1491   return (gimple_assign_single_p (gs)
1492           && TREE_CODE (gimple_assign_lhs (gs)) == SSA_NAME
1493           && TREE_CODE (gimple_assign_rhs1 (gs)) == SSA_NAME);
1494 }
1495
1496
1497 /* Return true if GS is an assignment with a unary RHS, but the
1498    operator has no effect on the assigned value.  The logic is adapted
1499    from STRIP_NOPS.  This predicate is intended to be used in tuplifying
1500    instances in which STRIP_NOPS was previously applied to the RHS of
1501    an assignment.
1502
1503    NOTE: In the use cases that led to the creation of this function
1504    and of gimple_assign_single_p, it is typical to test for either
1505    condition and to proceed in the same manner.  In each case, the
1506    assigned value is represented by the single RHS operand of the
1507    assignment.  I suspect there may be cases where gimple_assign_copy_p,
1508    gimple_assign_single_p, or equivalent logic is used where a similar
1509    treatment of unary NOPs is appropriate.  */
1510
1511 bool
1512 gimple_assign_unary_nop_p (gimple *gs)
1513 {
1514   return (is_gimple_assign (gs)
1515           && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (gs))
1516               || gimple_assign_rhs_code (gs) == NON_LVALUE_EXPR)
1517           && gimple_assign_rhs1 (gs) != error_mark_node
1518           && (TYPE_MODE (TREE_TYPE (gimple_assign_lhs (gs)))
1519               == TYPE_MODE (TREE_TYPE (gimple_assign_rhs1 (gs)))));
1520 }
1521
1522 /* Set BB to be the basic block holding G.  */
1523
1524 void
1525 gimple_set_bb (gimple *stmt, basic_block bb)
1526 {
1527   stmt->bb = bb;
1528
1529   if (gimple_code (stmt) != GIMPLE_LABEL)
1530     return;
1531
1532   /* If the statement is a label, add the label to block-to-labels map
1533      so that we can speed up edge creation for GIMPLE_GOTOs.  */
1534   if (cfun->cfg)
1535     {
1536       tree t;
1537       int uid;
1538
1539       t = gimple_label_label (as_a <glabel *> (stmt));
1540       uid = LABEL_DECL_UID (t);
1541       if (uid == -1)
1542         {
1543           unsigned old_len =
1544             vec_safe_length (label_to_block_map_for_fn (cfun));
1545           LABEL_DECL_UID (t) = uid = cfun->cfg->last_label_uid++;
1546           if (old_len <= (unsigned) uid)
1547             {
1548               unsigned new_len = 3 * uid / 2 + 1;
1549
1550               vec_safe_grow_cleared (label_to_block_map_for_fn (cfun),
1551                                      new_len);
1552             }
1553         }
1554
1555       (*label_to_block_map_for_fn (cfun))[uid] = bb;
1556     }
1557 }
1558
1559
1560 /* Modify the RHS of the assignment pointed-to by GSI using the
1561    operands in the expression tree EXPR.
1562
1563    NOTE: The statement pointed-to by GSI may be reallocated if it
1564    did not have enough operand slots.
1565
1566    This function is useful to convert an existing tree expression into
1567    the flat representation used for the RHS of a GIMPLE assignment.
1568    It will reallocate memory as needed to expand or shrink the number
1569    of operand slots needed to represent EXPR.
1570
1571    NOTE: If you find yourself building a tree and then calling this
1572    function, you are most certainly doing it the slow way.  It is much
1573    better to build a new assignment or to use the function
1574    gimple_assign_set_rhs_with_ops, which does not require an
1575    expression tree to be built.  */
1576
1577 void
1578 gimple_assign_set_rhs_from_tree (gimple_stmt_iterator *gsi, tree expr)
1579 {
1580   enum tree_code subcode;
1581   tree op1, op2, op3;
1582
1583   extract_ops_from_tree (expr, &subcode, &op1, &op2, &op3);
1584   gimple_assign_set_rhs_with_ops (gsi, subcode, op1, op2, op3);
1585 }
1586
1587
1588 /* Set the RHS of assignment statement pointed-to by GSI to CODE with
1589    operands OP1, OP2 and OP3.
1590
1591    NOTE: The statement pointed-to by GSI may be reallocated if it
1592    did not have enough operand slots.  */
1593
1594 void
1595 gimple_assign_set_rhs_with_ops (gimple_stmt_iterator *gsi, enum tree_code code,
1596                                 tree op1, tree op2, tree op3)
1597 {
1598   unsigned new_rhs_ops = get_gimple_rhs_num_ops (code);
1599   gimple *stmt = gsi_stmt (*gsi);
1600
1601   /* If the new CODE needs more operands, allocate a new statement.  */
1602   if (gimple_num_ops (stmt) < new_rhs_ops + 1)
1603     {
1604       tree lhs = gimple_assign_lhs (stmt);
1605       gimple *new_stmt = gimple_alloc (gimple_code (stmt), new_rhs_ops + 1);
1606       memcpy (new_stmt, stmt, gimple_size (gimple_code (stmt)));
1607       gimple_init_singleton (new_stmt);
1608       gsi_replace (gsi, new_stmt, true);
1609       stmt = new_stmt;
1610
1611       /* The LHS needs to be reset as this also changes the SSA name
1612          on the LHS.  */
1613       gimple_assign_set_lhs (stmt, lhs);
1614     }
1615
1616   gimple_set_num_ops (stmt, new_rhs_ops + 1);
1617   gimple_set_subcode (stmt, code);
1618   gimple_assign_set_rhs1 (stmt, op1);
1619   if (new_rhs_ops > 1)
1620     gimple_assign_set_rhs2 (stmt, op2);
1621   if (new_rhs_ops > 2)
1622     gimple_assign_set_rhs3 (stmt, op3);
1623 }
1624
1625
1626 /* Return the LHS of a statement that performs an assignment,
1627    either a GIMPLE_ASSIGN or a GIMPLE_CALL.  Returns NULL_TREE
1628    for a call to a function that returns no value, or for a
1629    statement other than an assignment or a call.  */
1630
1631 tree
1632 gimple_get_lhs (const gimple *stmt)
1633 {
1634   enum gimple_code code = gimple_code (stmt);
1635
1636   if (code == GIMPLE_ASSIGN)
1637     return gimple_assign_lhs (stmt);
1638   else if (code == GIMPLE_CALL)
1639     return gimple_call_lhs (stmt);
1640   else
1641     return NULL_TREE;
1642 }
1643
1644
1645 /* Set the LHS of a statement that performs an assignment,
1646    either a GIMPLE_ASSIGN or a GIMPLE_CALL.  */
1647
1648 void
1649 gimple_set_lhs (gimple *stmt, tree lhs)
1650 {
1651   enum gimple_code code = gimple_code (stmt);
1652
1653   if (code == GIMPLE_ASSIGN)
1654     gimple_assign_set_lhs (stmt, lhs);
1655   else if (code == GIMPLE_CALL)
1656     gimple_call_set_lhs (stmt, lhs);
1657   else
1658     gcc_unreachable ();
1659 }
1660
1661
1662 /* Return a deep copy of statement STMT.  All the operands from STMT
1663    are reallocated and copied using unshare_expr.  The DEF, USE, VDEF
1664    and VUSE operand arrays are set to empty in the new copy.  The new
1665    copy isn't part of any sequence.  */
1666
1667 gimple *
1668 gimple_copy (gimple *stmt)
1669 {
1670   enum gimple_code code = gimple_code (stmt);
1671   unsigned num_ops = gimple_num_ops (stmt);
1672   gimple *copy = gimple_alloc (code, num_ops);
1673   unsigned i;
1674
1675   /* Shallow copy all the fields from STMT.  */
1676   memcpy (copy, stmt, gimple_size (code));
1677   gimple_init_singleton (copy);
1678
1679   /* If STMT has sub-statements, deep-copy them as well.  */
1680   if (gimple_has_substatements (stmt))
1681     {
1682       gimple_seq new_seq;
1683       tree t;
1684
1685       switch (gimple_code (stmt))
1686         {
1687         case GIMPLE_BIND:
1688           {
1689             gbind *bind_stmt = as_a <gbind *> (stmt);
1690             gbind *bind_copy = as_a <gbind *> (copy);
1691             new_seq = gimple_seq_copy (gimple_bind_body (bind_stmt));
1692             gimple_bind_set_body (bind_copy, new_seq);
1693             gimple_bind_set_vars (bind_copy,
1694                                   unshare_expr (gimple_bind_vars (bind_stmt)));
1695             gimple_bind_set_block (bind_copy, gimple_bind_block (bind_stmt));
1696           }
1697           break;
1698
1699         case GIMPLE_CATCH:
1700           {
1701             gcatch *catch_stmt = as_a <gcatch *> (stmt);
1702             gcatch *catch_copy = as_a <gcatch *> (copy);
1703             new_seq = gimple_seq_copy (gimple_catch_handler (catch_stmt));
1704             gimple_catch_set_handler (catch_copy, new_seq);
1705             t = unshare_expr (gimple_catch_types (catch_stmt));
1706             gimple_catch_set_types (catch_copy, t);
1707           }
1708           break;
1709
1710         case GIMPLE_EH_FILTER:
1711           {
1712             geh_filter *eh_filter_stmt = as_a <geh_filter *> (stmt);
1713             geh_filter *eh_filter_copy = as_a <geh_filter *> (copy);
1714             new_seq
1715               = gimple_seq_copy (gimple_eh_filter_failure (eh_filter_stmt));
1716             gimple_eh_filter_set_failure (eh_filter_copy, new_seq);
1717             t = unshare_expr (gimple_eh_filter_types (eh_filter_stmt));
1718             gimple_eh_filter_set_types (eh_filter_copy, t);
1719           }
1720           break;
1721
1722         case GIMPLE_EH_ELSE:
1723           {
1724             geh_else *eh_else_stmt = as_a <geh_else *> (stmt);
1725             geh_else *eh_else_copy = as_a <geh_else *> (copy);
1726             new_seq = gimple_seq_copy (gimple_eh_else_n_body (eh_else_stmt));
1727             gimple_eh_else_set_n_body (eh_else_copy, new_seq);
1728             new_seq = gimple_seq_copy (gimple_eh_else_e_body (eh_else_stmt));
1729             gimple_eh_else_set_e_body (eh_else_copy, new_seq);
1730           }
1731           break;
1732
1733         case GIMPLE_TRY:
1734           {
1735             gtry *try_stmt = as_a <gtry *> (stmt);
1736             gtry *try_copy = as_a <gtry *> (copy);
1737             new_seq = gimple_seq_copy (gimple_try_eval (try_stmt));
1738             gimple_try_set_eval (try_copy, new_seq);
1739             new_seq = gimple_seq_copy (gimple_try_cleanup (try_stmt));
1740             gimple_try_set_cleanup (try_copy, new_seq);
1741           }
1742           break;
1743
1744         case GIMPLE_OMP_FOR:
1745           new_seq = gimple_seq_copy (gimple_omp_for_pre_body (stmt));
1746           gimple_omp_for_set_pre_body (copy, new_seq);
1747           t = unshare_expr (gimple_omp_for_clauses (stmt));
1748           gimple_omp_for_set_clauses (copy, t);
1749           {
1750             gomp_for *omp_for_copy = as_a <gomp_for *> (copy);
1751             omp_for_copy->iter = ggc_vec_alloc<gimple_omp_for_iter>
1752               ( gimple_omp_for_collapse (stmt));
1753           }
1754           for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
1755             {
1756               gimple_omp_for_set_cond (copy, i,
1757                                        gimple_omp_for_cond (stmt, i));
1758               gimple_omp_for_set_index (copy, i,
1759                                         gimple_omp_for_index (stmt, i));
1760               t = unshare_expr (gimple_omp_for_initial (stmt, i));
1761               gimple_omp_for_set_initial (copy, i, t);
1762               t = unshare_expr (gimple_omp_for_final (stmt, i));
1763               gimple_omp_for_set_final (copy, i, t);
1764               t = unshare_expr (gimple_omp_for_incr (stmt, i));
1765               gimple_omp_for_set_incr (copy, i, t);
1766             }
1767           goto copy_omp_body;
1768
1769         case GIMPLE_OMP_PARALLEL:
1770           {
1771             gomp_parallel *omp_par_stmt = as_a <gomp_parallel *> (stmt);
1772             gomp_parallel *omp_par_copy = as_a <gomp_parallel *> (copy);
1773             t = unshare_expr (gimple_omp_parallel_clauses (omp_par_stmt));
1774             gimple_omp_parallel_set_clauses (omp_par_copy, t);
1775             t = unshare_expr (gimple_omp_parallel_child_fn (omp_par_stmt));
1776             gimple_omp_parallel_set_child_fn (omp_par_copy, t);
1777             t = unshare_expr (gimple_omp_parallel_data_arg (omp_par_stmt));
1778             gimple_omp_parallel_set_data_arg (omp_par_copy, t);
1779           }
1780           goto copy_omp_body;
1781
1782         case GIMPLE_OMP_TASK:
1783           t = unshare_expr (gimple_omp_task_clauses (stmt));
1784           gimple_omp_task_set_clauses (copy, t);
1785           t = unshare_expr (gimple_omp_task_child_fn (stmt));
1786           gimple_omp_task_set_child_fn (copy, t);
1787           t = unshare_expr (gimple_omp_task_data_arg (stmt));
1788           gimple_omp_task_set_data_arg (copy, t);
1789           t = unshare_expr (gimple_omp_task_copy_fn (stmt));
1790           gimple_omp_task_set_copy_fn (copy, t);
1791           t = unshare_expr (gimple_omp_task_arg_size (stmt));
1792           gimple_omp_task_set_arg_size (copy, t);
1793           t = unshare_expr (gimple_omp_task_arg_align (stmt));
1794           gimple_omp_task_set_arg_align (copy, t);
1795           goto copy_omp_body;
1796
1797         case GIMPLE_OMP_CRITICAL:
1798           t = unshare_expr (gimple_omp_critical_name
1799                                 (as_a <gomp_critical *> (stmt)));
1800           gimple_omp_critical_set_name (as_a <gomp_critical *> (copy), t);
1801           t = unshare_expr (gimple_omp_critical_clauses
1802                                 (as_a <gomp_critical *> (stmt)));
1803           gimple_omp_critical_set_clauses (as_a <gomp_critical *> (copy), t);
1804           goto copy_omp_body;
1805
1806         case GIMPLE_OMP_ORDERED:
1807           t = unshare_expr (gimple_omp_ordered_clauses
1808                                 (as_a <gomp_ordered *> (stmt)));
1809           gimple_omp_ordered_set_clauses (as_a <gomp_ordered *> (copy), t);
1810           goto copy_omp_body;
1811
1812         case GIMPLE_OMP_SECTIONS:
1813           t = unshare_expr (gimple_omp_sections_clauses (stmt));
1814           gimple_omp_sections_set_clauses (copy, t);
1815           t = unshare_expr (gimple_omp_sections_control (stmt));
1816           gimple_omp_sections_set_control (copy, t);
1817           /* FALLTHRU  */
1818
1819         case GIMPLE_OMP_SINGLE:
1820         case GIMPLE_OMP_TARGET:
1821         case GIMPLE_OMP_TEAMS:
1822         case GIMPLE_OMP_SECTION:
1823         case GIMPLE_OMP_MASTER:
1824         case GIMPLE_OMP_TASKGROUP:
1825         case GIMPLE_OMP_GRID_BODY:
1826         copy_omp_body:
1827           new_seq = gimple_seq_copy (gimple_omp_body (stmt));
1828           gimple_omp_set_body (copy, new_seq);
1829           break;
1830
1831         case GIMPLE_TRANSACTION:
1832           new_seq = gimple_seq_copy (gimple_transaction_body (
1833                                        as_a <gtransaction *> (stmt)));
1834           gimple_transaction_set_body (as_a <gtransaction *> (copy),
1835                                        new_seq);
1836           break;
1837
1838         case GIMPLE_WITH_CLEANUP_EXPR:
1839           new_seq = gimple_seq_copy (gimple_wce_cleanup (stmt));
1840           gimple_wce_set_cleanup (copy, new_seq);
1841           break;
1842
1843         default:
1844           gcc_unreachable ();
1845         }
1846     }
1847
1848   /* Make copy of operands.  */
1849   for (i = 0; i < num_ops; i++)
1850     gimple_set_op (copy, i, unshare_expr (gimple_op (stmt, i)));
1851
1852   if (gimple_has_mem_ops (stmt))
1853     {
1854       gimple_set_vdef (copy, gimple_vdef (stmt));
1855       gimple_set_vuse (copy, gimple_vuse (stmt));
1856     }
1857
1858   /* Clear out SSA operand vectors on COPY.  */
1859   if (gimple_has_ops (stmt))
1860     {
1861       gimple_set_use_ops (copy, NULL);
1862
1863       /* SSA operands need to be updated.  */
1864       gimple_set_modified (copy, true);
1865     }
1866
1867   return copy;
1868 }
1869
1870
1871 /* Return true if statement S has side-effects.  We consider a
1872    statement to have side effects if:
1873
1874    - It is a GIMPLE_CALL not marked with ECF_PURE or ECF_CONST.
1875    - Any of its operands are marked TREE_THIS_VOLATILE or TREE_SIDE_EFFECTS.  */
1876
1877 bool
1878 gimple_has_side_effects (const gimple *s)
1879 {
1880   if (is_gimple_debug (s))
1881     return false;
1882
1883   /* We don't have to scan the arguments to check for
1884      volatile arguments, though, at present, we still
1885      do a scan to check for TREE_SIDE_EFFECTS.  */
1886   if (gimple_has_volatile_ops (s))
1887     return true;
1888
1889   if (gimple_code (s) == GIMPLE_ASM
1890       && gimple_asm_volatile_p (as_a <const gasm *> (s)))
1891     return true;
1892
1893   if (is_gimple_call (s))
1894     {
1895       int flags = gimple_call_flags (s);
1896
1897       /* An infinite loop is considered a side effect.  */
1898       if (!(flags & (ECF_CONST | ECF_PURE))
1899           || (flags & ECF_LOOPING_CONST_OR_PURE))
1900         return true;
1901
1902       return false;
1903     }
1904
1905   return false;
1906 }
1907
1908 /* Helper for gimple_could_trap_p and gimple_assign_rhs_could_trap_p.
1909    Return true if S can trap.  When INCLUDE_MEM is true, check whether
1910    the memory operations could trap.  When INCLUDE_STORES is true and
1911    S is a GIMPLE_ASSIGN, the LHS of the assignment is also checked.  */
1912
1913 bool
1914 gimple_could_trap_p_1 (gimple *s, bool include_mem, bool include_stores)
1915 {
1916   tree t, div = NULL_TREE;
1917   enum tree_code op;
1918
1919   if (include_mem)
1920     {
1921       unsigned i, start = (is_gimple_assign (s) && !include_stores) ? 1 : 0;
1922
1923       for (i = start; i < gimple_num_ops (s); i++)
1924         if (tree_could_trap_p (gimple_op (s, i)))
1925           return true;
1926     }
1927
1928   switch (gimple_code (s))
1929     {
1930     case GIMPLE_ASM:
1931       return gimple_asm_volatile_p (as_a <gasm *> (s));
1932
1933     case GIMPLE_CALL:
1934       t = gimple_call_fndecl (s);
1935       /* Assume that calls to weak functions may trap.  */
1936       if (!t || !DECL_P (t) || DECL_WEAK (t))
1937         return true;
1938       return false;
1939
1940     case GIMPLE_ASSIGN:
1941       t = gimple_expr_type (s);
1942       op = gimple_assign_rhs_code (s);
1943       if (get_gimple_rhs_class (op) == GIMPLE_BINARY_RHS)
1944         div = gimple_assign_rhs2 (s);
1945       return (operation_could_trap_p (op, FLOAT_TYPE_P (t),
1946                                       (INTEGRAL_TYPE_P (t)
1947                                        && TYPE_OVERFLOW_TRAPS (t)),
1948                                       div));
1949
1950     case GIMPLE_COND:
1951       t = TREE_TYPE (gimple_cond_lhs (s));
1952       return operation_could_trap_p (gimple_cond_code (s),
1953                                      FLOAT_TYPE_P (t), false, NULL_TREE);
1954
1955     default:
1956       break;
1957     }
1958
1959   return false;
1960 }
1961
1962 /* Return true if statement S can trap.  */
1963
1964 bool
1965 gimple_could_trap_p (gimple *s)
1966 {
1967   return gimple_could_trap_p_1 (s, true, true);
1968 }
1969
1970 /* Return true if RHS of a GIMPLE_ASSIGN S can trap.  */
1971
1972 bool
1973 gimple_assign_rhs_could_trap_p (gimple *s)
1974 {
1975   gcc_assert (is_gimple_assign (s));
1976   return gimple_could_trap_p_1 (s, true, false);
1977 }
1978
1979
1980 /* Print debugging information for gimple stmts generated.  */
1981
1982 void
1983 dump_gimple_statistics (void)
1984 {
1985   int i, total_tuples = 0, total_bytes = 0;
1986
1987   if (! GATHER_STATISTICS)
1988     {
1989       fprintf (stderr, "No gimple statistics\n");
1990       return;
1991     }
1992
1993   fprintf (stderr, "\nGIMPLE statements\n");
1994   fprintf (stderr, "Kind                   Stmts      Bytes\n");
1995   fprintf (stderr, "---------------------------------------\n");
1996   for (i = 0; i < (int) gimple_alloc_kind_all; ++i)
1997     {
1998       fprintf (stderr, "%-20s %7d %10d\n", gimple_alloc_kind_names[i],
1999           gimple_alloc_counts[i], gimple_alloc_sizes[i]);
2000       total_tuples += gimple_alloc_counts[i];
2001       total_bytes += gimple_alloc_sizes[i];
2002     }
2003   fprintf (stderr, "---------------------------------------\n");
2004   fprintf (stderr, "%-20s %7d %10d\n", "Total", total_tuples, total_bytes);
2005   fprintf (stderr, "---------------------------------------\n");
2006 }
2007
2008
2009 /* Return the number of operands needed on the RHS of a GIMPLE
2010    assignment for an expression with tree code CODE.  */
2011
2012 unsigned
2013 get_gimple_rhs_num_ops (enum tree_code code)
2014 {
2015   enum gimple_rhs_class rhs_class = get_gimple_rhs_class (code);
2016
2017   if (rhs_class == GIMPLE_UNARY_RHS || rhs_class == GIMPLE_SINGLE_RHS)
2018     return 1;
2019   else if (rhs_class == GIMPLE_BINARY_RHS)
2020     return 2;
2021   else if (rhs_class == GIMPLE_TERNARY_RHS)
2022     return 3;
2023   else
2024     gcc_unreachable ();
2025 }
2026
2027 #define DEFTREECODE(SYM, STRING, TYPE, NARGS)                               \
2028   (unsigned char)                                                           \
2029   ((TYPE) == tcc_unary ? GIMPLE_UNARY_RHS                                   \
2030    : ((TYPE) == tcc_binary                                                  \
2031       || (TYPE) == tcc_comparison) ? GIMPLE_BINARY_RHS                      \
2032    : ((TYPE) == tcc_constant                                                \
2033       || (TYPE) == tcc_declaration                                          \
2034       || (TYPE) == tcc_reference) ? GIMPLE_SINGLE_RHS                       \
2035    : ((SYM) == TRUTH_AND_EXPR                                               \
2036       || (SYM) == TRUTH_OR_EXPR                                             \
2037       || (SYM) == TRUTH_XOR_EXPR) ? GIMPLE_BINARY_RHS                       \
2038    : (SYM) == TRUTH_NOT_EXPR ? GIMPLE_UNARY_RHS                             \
2039    : ((SYM) == COND_EXPR                                                    \
2040       || (SYM) == WIDEN_MULT_PLUS_EXPR                                      \
2041       || (SYM) == WIDEN_MULT_MINUS_EXPR                                     \
2042       || (SYM) == DOT_PROD_EXPR                                             \
2043       || (SYM) == SAD_EXPR                                                  \
2044       || (SYM) == REALIGN_LOAD_EXPR                                         \
2045       || (SYM) == VEC_COND_EXPR                                             \
2046       || (SYM) == VEC_PERM_EXPR                                             \
2047       || (SYM) == FMA_EXPR) ? GIMPLE_TERNARY_RHS                            \
2048    : ((SYM) == CONSTRUCTOR                                                  \
2049       || (SYM) == OBJ_TYPE_REF                                              \
2050       || (SYM) == ASSERT_EXPR                                               \
2051       || (SYM) == ADDR_EXPR                                                 \
2052       || (SYM) == WITH_SIZE_EXPR                                            \
2053       || (SYM) == SSA_NAME) ? GIMPLE_SINGLE_RHS                             \
2054    : GIMPLE_INVALID_RHS),
2055 #define END_OF_BASE_TREE_CODES (unsigned char) GIMPLE_INVALID_RHS,
2056
2057 const unsigned char gimple_rhs_class_table[] = {
2058 #include "all-tree.def"
2059 };
2060
2061 #undef DEFTREECODE
2062 #undef END_OF_BASE_TREE_CODES
2063
2064 /* Canonicalize a tree T for use in a COND_EXPR as conditional.  Returns
2065    a canonicalized tree that is valid for a COND_EXPR or NULL_TREE, if
2066    we failed to create one.  */
2067
2068 tree
2069 canonicalize_cond_expr_cond (tree t)
2070 {
2071   /* Strip conversions around boolean operations.  */
2072   if (CONVERT_EXPR_P (t)
2073       && (truth_value_p (TREE_CODE (TREE_OPERAND (t, 0)))
2074           || TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
2075              == BOOLEAN_TYPE))
2076     t = TREE_OPERAND (t, 0);
2077
2078   /* For !x use x == 0.  */
2079   if (TREE_CODE (t) == TRUTH_NOT_EXPR)
2080     {
2081       tree top0 = TREE_OPERAND (t, 0);
2082       t = build2 (EQ_EXPR, TREE_TYPE (t),
2083                   top0, build_int_cst (TREE_TYPE (top0), 0));
2084     }
2085   /* For cmp ? 1 : 0 use cmp.  */
2086   else if (TREE_CODE (t) == COND_EXPR
2087            && COMPARISON_CLASS_P (TREE_OPERAND (t, 0))
2088            && integer_onep (TREE_OPERAND (t, 1))
2089            && integer_zerop (TREE_OPERAND (t, 2)))
2090     {
2091       tree top0 = TREE_OPERAND (t, 0);
2092       t = build2 (TREE_CODE (top0), TREE_TYPE (t),
2093                   TREE_OPERAND (top0, 0), TREE_OPERAND (top0, 1));
2094     }
2095   /* For x ^ y use x != y.  */
2096   else if (TREE_CODE (t) == BIT_XOR_EXPR)
2097     t = build2 (NE_EXPR, TREE_TYPE (t),
2098                 TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
2099   
2100   if (is_gimple_condexpr (t))
2101     return t;
2102
2103   return NULL_TREE;
2104 }
2105
2106 /* Build a GIMPLE_CALL identical to STMT but skipping the arguments in
2107    the positions marked by the set ARGS_TO_SKIP.  */
2108
2109 gcall *
2110 gimple_call_copy_skip_args (gcall *stmt, bitmap args_to_skip)
2111 {
2112   int i;
2113   int nargs = gimple_call_num_args (stmt);
2114   auto_vec<tree> vargs (nargs);
2115   gcall *new_stmt;
2116
2117   for (i = 0; i < nargs; i++)
2118     if (!bitmap_bit_p (args_to_skip, i))
2119       vargs.quick_push (gimple_call_arg (stmt, i));
2120
2121   if (gimple_call_internal_p (stmt))
2122     new_stmt = gimple_build_call_internal_vec (gimple_call_internal_fn (stmt),
2123                                                vargs);
2124   else
2125     new_stmt = gimple_build_call_vec (gimple_call_fn (stmt), vargs);
2126
2127   if (gimple_call_lhs (stmt))
2128     gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
2129
2130   gimple_set_vuse (new_stmt, gimple_vuse (stmt));
2131   gimple_set_vdef (new_stmt, gimple_vdef (stmt));
2132
2133   if (gimple_has_location (stmt))
2134     gimple_set_location (new_stmt, gimple_location (stmt));
2135   gimple_call_copy_flags (new_stmt, stmt);
2136   gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
2137
2138   gimple_set_modified (new_stmt, true);
2139
2140   return new_stmt;
2141 }
2142
2143
2144
2145 /* Return true if the field decls F1 and F2 are at the same offset.
2146
2147    This is intended to be used on GIMPLE types only.  */
2148
2149 bool
2150 gimple_compare_field_offset (tree f1, tree f2)
2151 {
2152   if (DECL_OFFSET_ALIGN (f1) == DECL_OFFSET_ALIGN (f2))
2153     {
2154       tree offset1 = DECL_FIELD_OFFSET (f1);
2155       tree offset2 = DECL_FIELD_OFFSET (f2);
2156       return ((offset1 == offset2
2157                /* Once gimplification is done, self-referential offsets are
2158                   instantiated as operand #2 of the COMPONENT_REF built for
2159                   each access and reset.  Therefore, they are not relevant
2160                   anymore and fields are interchangeable provided that they
2161                   represent the same access.  */
2162                || (TREE_CODE (offset1) == PLACEHOLDER_EXPR
2163                    && TREE_CODE (offset2) == PLACEHOLDER_EXPR
2164                    && (DECL_SIZE (f1) == DECL_SIZE (f2)
2165                        || (TREE_CODE (DECL_SIZE (f1)) == PLACEHOLDER_EXPR
2166                            && TREE_CODE (DECL_SIZE (f2)) == PLACEHOLDER_EXPR)
2167                        || operand_equal_p (DECL_SIZE (f1), DECL_SIZE (f2), 0))
2168                    && DECL_ALIGN (f1) == DECL_ALIGN (f2))
2169                || operand_equal_p (offset1, offset2, 0))
2170               && tree_int_cst_equal (DECL_FIELD_BIT_OFFSET (f1),
2171                                      DECL_FIELD_BIT_OFFSET (f2)));
2172     }
2173
2174   /* Fortran and C do not always agree on what DECL_OFFSET_ALIGN
2175      should be, so handle differing ones specially by decomposing
2176      the offset into a byte and bit offset manually.  */
2177   if (tree_fits_shwi_p (DECL_FIELD_OFFSET (f1))
2178       && tree_fits_shwi_p (DECL_FIELD_OFFSET (f2)))
2179     {
2180       unsigned HOST_WIDE_INT byte_offset1, byte_offset2;
2181       unsigned HOST_WIDE_INT bit_offset1, bit_offset2;
2182       bit_offset1 = TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (f1));
2183       byte_offset1 = (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (f1))
2184                       + bit_offset1 / BITS_PER_UNIT);
2185       bit_offset2 = TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (f2));
2186       byte_offset2 = (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (f2))
2187                       + bit_offset2 / BITS_PER_UNIT);
2188       if (byte_offset1 != byte_offset2)
2189         return false;
2190       return bit_offset1 % BITS_PER_UNIT == bit_offset2 % BITS_PER_UNIT;
2191     }
2192
2193   return false;
2194 }
2195
2196
2197 /* Return a type the same as TYPE except unsigned or
2198    signed according to UNSIGNEDP.  */
2199
2200 static tree
2201 gimple_signed_or_unsigned_type (bool unsignedp, tree type)
2202 {
2203   tree type1;
2204   int i;
2205
2206   type1 = TYPE_MAIN_VARIANT (type);
2207   if (type1 == signed_char_type_node
2208       || type1 == char_type_node
2209       || type1 == unsigned_char_type_node)
2210     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2211   if (type1 == integer_type_node || type1 == unsigned_type_node)
2212     return unsignedp ? unsigned_type_node : integer_type_node;
2213   if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
2214     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2215   if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
2216     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2217   if (type1 == long_long_integer_type_node
2218       || type1 == long_long_unsigned_type_node)
2219     return unsignedp
2220            ? long_long_unsigned_type_node
2221            : long_long_integer_type_node;
2222
2223   for (i = 0; i < NUM_INT_N_ENTS; i ++)
2224     if (int_n_enabled_p[i]
2225         && (type1 == int_n_trees[i].unsigned_type
2226             || type1 == int_n_trees[i].signed_type))
2227         return unsignedp
2228           ? int_n_trees[i].unsigned_type
2229           : int_n_trees[i].signed_type;
2230
2231 #if HOST_BITS_PER_WIDE_INT >= 64
2232   if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
2233     return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2234 #endif
2235   if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
2236     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2237   if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
2238     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2239   if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
2240     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2241   if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
2242     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2243
2244 #define GIMPLE_FIXED_TYPES(NAME)            \
2245   if (type1 == short_ ## NAME ## _type_node \
2246       || type1 == unsigned_short_ ## NAME ## _type_node) \
2247     return unsignedp ? unsigned_short_ ## NAME ## _type_node \
2248                      : short_ ## NAME ## _type_node; \
2249   if (type1 == NAME ## _type_node \
2250       || type1 == unsigned_ ## NAME ## _type_node) \
2251     return unsignedp ? unsigned_ ## NAME ## _type_node \
2252                      : NAME ## _type_node; \
2253   if (type1 == long_ ## NAME ## _type_node \
2254       || type1 == unsigned_long_ ## NAME ## _type_node) \
2255     return unsignedp ? unsigned_long_ ## NAME ## _type_node \
2256                      : long_ ## NAME ## _type_node; \
2257   if (type1 == long_long_ ## NAME ## _type_node \
2258       || type1 == unsigned_long_long_ ## NAME ## _type_node) \
2259     return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
2260                      : long_long_ ## NAME ## _type_node;
2261
2262 #define GIMPLE_FIXED_MODE_TYPES(NAME) \
2263   if (type1 == NAME ## _type_node \
2264       || type1 == u ## NAME ## _type_node) \
2265     return unsignedp ? u ## NAME ## _type_node \
2266                      : NAME ## _type_node;
2267
2268 #define GIMPLE_FIXED_TYPES_SAT(NAME) \
2269   if (type1 == sat_ ## short_ ## NAME ## _type_node \
2270       || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
2271     return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
2272                      : sat_ ## short_ ## NAME ## _type_node; \
2273   if (type1 == sat_ ## NAME ## _type_node \
2274       || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
2275     return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
2276                      : sat_ ## NAME ## _type_node; \
2277   if (type1 == sat_ ## long_ ## NAME ## _type_node \
2278       || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
2279     return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
2280                      : sat_ ## long_ ## NAME ## _type_node; \
2281   if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
2282       || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
2283     return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
2284                      : sat_ ## long_long_ ## NAME ## _type_node;
2285
2286 #define GIMPLE_FIXED_MODE_TYPES_SAT(NAME)       \
2287   if (type1 == sat_ ## NAME ## _type_node \
2288       || type1 == sat_ ## u ## NAME ## _type_node) \
2289     return unsignedp ? sat_ ## u ## NAME ## _type_node \
2290                      : sat_ ## NAME ## _type_node;
2291
2292   GIMPLE_FIXED_TYPES (fract);
2293   GIMPLE_FIXED_TYPES_SAT (fract);
2294   GIMPLE_FIXED_TYPES (accum);
2295   GIMPLE_FIXED_TYPES_SAT (accum);
2296
2297   GIMPLE_FIXED_MODE_TYPES (qq);
2298   GIMPLE_FIXED_MODE_TYPES (hq);
2299   GIMPLE_FIXED_MODE_TYPES (sq);
2300   GIMPLE_FIXED_MODE_TYPES (dq);
2301   GIMPLE_FIXED_MODE_TYPES (tq);
2302   GIMPLE_FIXED_MODE_TYPES_SAT (qq);
2303   GIMPLE_FIXED_MODE_TYPES_SAT (hq);
2304   GIMPLE_FIXED_MODE_TYPES_SAT (sq);
2305   GIMPLE_FIXED_MODE_TYPES_SAT (dq);
2306   GIMPLE_FIXED_MODE_TYPES_SAT (tq);
2307   GIMPLE_FIXED_MODE_TYPES (ha);
2308   GIMPLE_FIXED_MODE_TYPES (sa);
2309   GIMPLE_FIXED_MODE_TYPES (da);
2310   GIMPLE_FIXED_MODE_TYPES (ta);
2311   GIMPLE_FIXED_MODE_TYPES_SAT (ha);
2312   GIMPLE_FIXED_MODE_TYPES_SAT (sa);
2313   GIMPLE_FIXED_MODE_TYPES_SAT (da);
2314   GIMPLE_FIXED_MODE_TYPES_SAT (ta);
2315
2316   /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
2317      the precision; they have precision set to match their range, but
2318      may use a wider mode to match an ABI.  If we change modes, we may
2319      wind up with bad conversions.  For INTEGER_TYPEs in C, must check
2320      the precision as well, so as to yield correct results for
2321      bit-field types.  C++ does not have these separate bit-field
2322      types, and producing a signed or unsigned variant of an
2323      ENUMERAL_TYPE may cause other problems as well.  */
2324   if (!INTEGRAL_TYPE_P (type)
2325       || TYPE_UNSIGNED (type) == unsignedp)
2326     return type;
2327
2328 #define TYPE_OK(node)                                                       \
2329   (TYPE_MODE (type) == TYPE_MODE (node)                                     \
2330    && TYPE_PRECISION (type) == TYPE_PRECISION (node))
2331   if (TYPE_OK (signed_char_type_node))
2332     return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2333   if (TYPE_OK (integer_type_node))
2334     return unsignedp ? unsigned_type_node : integer_type_node;
2335   if (TYPE_OK (short_integer_type_node))
2336     return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2337   if (TYPE_OK (long_integer_type_node))
2338     return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2339   if (TYPE_OK (long_long_integer_type_node))
2340     return (unsignedp
2341             ? long_long_unsigned_type_node
2342             : long_long_integer_type_node);
2343
2344   for (i = 0; i < NUM_INT_N_ENTS; i ++)
2345     if (int_n_enabled_p[i]
2346         && TYPE_MODE (type) == int_n_data[i].m
2347         && TYPE_PRECISION (type) == int_n_data[i].bitsize)
2348         return unsignedp
2349           ? int_n_trees[i].unsigned_type
2350           : int_n_trees[i].signed_type;
2351
2352 #if HOST_BITS_PER_WIDE_INT >= 64
2353   if (TYPE_OK (intTI_type_node))
2354     return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2355 #endif
2356   if (TYPE_OK (intDI_type_node))
2357     return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2358   if (TYPE_OK (intSI_type_node))
2359     return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2360   if (TYPE_OK (intHI_type_node))
2361     return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2362   if (TYPE_OK (intQI_type_node))
2363     return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2364
2365 #undef GIMPLE_FIXED_TYPES
2366 #undef GIMPLE_FIXED_MODE_TYPES
2367 #undef GIMPLE_FIXED_TYPES_SAT
2368 #undef GIMPLE_FIXED_MODE_TYPES_SAT
2369 #undef TYPE_OK
2370
2371   return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
2372 }
2373
2374
2375 /* Return an unsigned type the same as TYPE in other respects.  */
2376
2377 tree
2378 gimple_unsigned_type (tree type)
2379 {
2380   return gimple_signed_or_unsigned_type (true, type);
2381 }
2382
2383
2384 /* Return a signed type the same as TYPE in other respects.  */
2385
2386 tree
2387 gimple_signed_type (tree type)
2388 {
2389   return gimple_signed_or_unsigned_type (false, type);
2390 }
2391
2392
2393 /* Return the typed-based alias set for T, which may be an expression
2394    or a type.  Return -1 if we don't do anything special.  */
2395
2396 alias_set_type
2397 gimple_get_alias_set (tree t)
2398 {
2399   tree u;
2400
2401   /* Permit type-punning when accessing a union, provided the access
2402      is directly through the union.  For example, this code does not
2403      permit taking the address of a union member and then storing
2404      through it.  Even the type-punning allowed here is a GCC
2405      extension, albeit a common and useful one; the C standard says
2406      that such accesses have implementation-defined behavior.  */
2407   for (u = t;
2408        TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
2409        u = TREE_OPERAND (u, 0))
2410     if (TREE_CODE (u) == COMPONENT_REF
2411         && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
2412       return 0;
2413
2414   /* That's all the expressions we handle specially.  */
2415   if (!TYPE_P (t))
2416     return -1;
2417
2418   /* For convenience, follow the C standard when dealing with
2419      character types.  Any object may be accessed via an lvalue that
2420      has character type.  */
2421   if (t == char_type_node
2422       || t == signed_char_type_node
2423       || t == unsigned_char_type_node)
2424     return 0;
2425
2426   /* Allow aliasing between signed and unsigned variants of the same
2427      type.  We treat the signed variant as canonical.  */
2428   if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
2429     {
2430       tree t1 = gimple_signed_type (t);
2431
2432       /* t1 == t can happen for boolean nodes which are always unsigned.  */
2433       if (t1 != t)
2434         return get_alias_set (t1);
2435     }
2436
2437   return -1;
2438 }
2439
2440
2441 /* Helper for gimple_ior_addresses_taken_1.  */
2442
2443 static bool
2444 gimple_ior_addresses_taken_1 (gimple *, tree addr, tree, void *data)
2445 {
2446   bitmap addresses_taken = (bitmap)data;
2447   addr = get_base_address (addr);
2448   if (addr
2449       && DECL_P (addr))
2450     {
2451       bitmap_set_bit (addresses_taken, DECL_UID (addr));
2452       return true;
2453     }
2454   return false;
2455 }
2456
2457 /* Set the bit for the uid of all decls that have their address taken
2458    in STMT in the ADDRESSES_TAKEN bitmap.  Returns true if there
2459    were any in this stmt.  */
2460
2461 bool
2462 gimple_ior_addresses_taken (bitmap addresses_taken, gimple *stmt)
2463 {
2464   return walk_stmt_load_store_addr_ops (stmt, addresses_taken, NULL, NULL,
2465                                         gimple_ior_addresses_taken_1);
2466 }
2467
2468
2469 /* Return true when STMTs arguments and return value match those of FNDECL,
2470    a decl of a builtin function.  */
2471
2472 bool
2473 gimple_builtin_call_types_compatible_p (const gimple *stmt, tree fndecl)
2474 {
2475   gcc_checking_assert (DECL_BUILT_IN_CLASS (fndecl) != NOT_BUILT_IN);
2476
2477   tree ret = gimple_call_lhs (stmt);
2478   if (ret
2479       && !useless_type_conversion_p (TREE_TYPE (ret),
2480                                      TREE_TYPE (TREE_TYPE (fndecl))))
2481     return false;
2482
2483   tree targs = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2484   unsigned nargs = gimple_call_num_args (stmt);
2485   for (unsigned i = 0; i < nargs; ++i)
2486     {
2487       /* Variadic args follow.  */
2488       if (!targs)
2489         return true;
2490       tree arg = gimple_call_arg (stmt, i);
2491       if (!useless_type_conversion_p (TREE_VALUE (targs), TREE_TYPE (arg)))
2492         return false;
2493       targs = TREE_CHAIN (targs);
2494     }
2495   if (targs && !VOID_TYPE_P (TREE_VALUE (targs)))
2496     return false;
2497   return true;
2498 }
2499
2500 /* Return true when STMT is builtins call.  */
2501
2502 bool
2503 gimple_call_builtin_p (const gimple *stmt)
2504 {
2505   tree fndecl;
2506   if (is_gimple_call (stmt)
2507       && (fndecl = gimple_call_fndecl (stmt)) != NULL_TREE
2508       && DECL_BUILT_IN_CLASS (fndecl) != NOT_BUILT_IN)
2509     return gimple_builtin_call_types_compatible_p (stmt, fndecl);
2510   return false;
2511 }
2512
2513 /* Return true when STMT is builtins call to CLASS.  */
2514
2515 bool
2516 gimple_call_builtin_p (const gimple *stmt, enum built_in_class klass)
2517 {
2518   tree fndecl;
2519   if (is_gimple_call (stmt)
2520       && (fndecl = gimple_call_fndecl (stmt)) != NULL_TREE
2521       && DECL_BUILT_IN_CLASS (fndecl) == klass)
2522     return gimple_builtin_call_types_compatible_p (stmt, fndecl);
2523   return false;
2524 }
2525
2526 /* Return true when STMT is builtins call to CODE of CLASS.  */
2527
2528 bool
2529 gimple_call_builtin_p (const gimple *stmt, enum built_in_function code)
2530 {
2531   tree fndecl;
2532   if (is_gimple_call (stmt)
2533       && (fndecl = gimple_call_fndecl (stmt)) != NULL_TREE
2534       && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL 
2535       && DECL_FUNCTION_CODE (fndecl) == code)
2536     return gimple_builtin_call_types_compatible_p (stmt, fndecl);
2537   return false;
2538 }
2539
2540 /* If CALL is a call to a combined_fn (i.e. an internal function or
2541    a normal built-in function), return its code, otherwise return
2542    CFN_LAST.  */
2543
2544 combined_fn
2545 gimple_call_combined_fn (const gimple *stmt)
2546 {
2547   if (const gcall *call = dyn_cast <const gcall *> (stmt))
2548     {
2549       if (gimple_call_internal_p (call))
2550         return as_combined_fn (gimple_call_internal_fn (call));
2551
2552       tree fndecl = gimple_call_fndecl (stmt);
2553       if (fndecl
2554           && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
2555           && gimple_builtin_call_types_compatible_p (stmt, fndecl))
2556         return as_combined_fn (DECL_FUNCTION_CODE (fndecl));
2557     }
2558   return CFN_LAST;
2559 }
2560
2561 /* Return true if STMT clobbers memory.  STMT is required to be a
2562    GIMPLE_ASM.  */
2563
2564 bool
2565 gimple_asm_clobbers_memory_p (const gasm *stmt)
2566 {
2567   unsigned i;
2568
2569   for (i = 0; i < gimple_asm_nclobbers (stmt); i++)
2570     {
2571       tree op = gimple_asm_clobber_op (stmt, i);
2572       if (strcmp (TREE_STRING_POINTER (TREE_VALUE (op)), "memory") == 0)
2573         return true;
2574     }
2575
2576   return false;
2577 }
2578
2579 /* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE.  */
2580
2581 void
2582 dump_decl_set (FILE *file, bitmap set)
2583 {
2584   if (set)
2585     {
2586       bitmap_iterator bi;
2587       unsigned i;
2588
2589       fprintf (file, "{ ");
2590
2591       EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
2592         {
2593           fprintf (file, "D.%u", i);
2594           fprintf (file, " ");
2595         }
2596
2597       fprintf (file, "}");
2598     }
2599   else
2600     fprintf (file, "NIL");
2601 }
2602
2603 /* Return true when CALL is a call stmt that definitely doesn't
2604    free any memory or makes it unavailable otherwise.  */
2605 bool
2606 nonfreeing_call_p (gimple *call)
2607 {
2608   if (gimple_call_builtin_p (call, BUILT_IN_NORMAL)
2609       && gimple_call_flags (call) & ECF_LEAF)
2610     switch (DECL_FUNCTION_CODE (gimple_call_fndecl (call)))
2611       {
2612         /* Just in case these become ECF_LEAF in the future.  */
2613         case BUILT_IN_FREE:
2614         case BUILT_IN_TM_FREE:
2615         case BUILT_IN_REALLOC:
2616         case BUILT_IN_STACK_RESTORE:
2617           return false;
2618         default:
2619           return true;
2620       }
2621   else if (gimple_call_internal_p (call))
2622     switch (gimple_call_internal_fn (call))
2623       {
2624       case IFN_ABNORMAL_DISPATCHER:
2625         return true;
2626       default:
2627         if (gimple_call_flags (call) & ECF_LEAF)
2628           return true;
2629         return false;
2630       }
2631
2632   tree fndecl = gimple_call_fndecl (call);
2633   if (!fndecl)
2634     return false;
2635   struct cgraph_node *n = cgraph_node::get (fndecl);
2636   if (!n)
2637     return false;
2638   enum availability availability;
2639   n = n->function_symbol (&availability);
2640   if (!n || availability <= AVAIL_INTERPOSABLE)
2641     return false;
2642   return n->nonfreeing_fn;
2643 }
2644
2645 /* Return true when CALL is a call stmt that definitely need not
2646    be considered to be a memory barrier.  */
2647 bool
2648 nonbarrier_call_p (gimple *call)
2649 {
2650   if (gimple_call_flags (call) & (ECF_PURE | ECF_CONST))
2651     return true;
2652   /* Should extend this to have a nonbarrier_fn flag, just as above in
2653      the nonfreeing case.  */
2654   return false;
2655 }
2656
2657 /* Callback for walk_stmt_load_store_ops.
2658  
2659    Return TRUE if OP will dereference the tree stored in DATA, FALSE
2660    otherwise.
2661
2662    This routine only makes a superficial check for a dereference.  Thus
2663    it must only be used if it is safe to return a false negative.  */
2664 static bool
2665 check_loadstore (gimple *, tree op, tree, void *data)
2666 {
2667   if (TREE_CODE (op) == MEM_REF || TREE_CODE (op) == TARGET_MEM_REF)
2668     {
2669       /* Some address spaces may legitimately dereference zero.  */
2670       addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (op));
2671       if (targetm.addr_space.zero_address_valid (as))
2672         return false;
2673
2674       return operand_equal_p (TREE_OPERAND (op, 0), (tree)data, 0);
2675     }
2676   return false;
2677 }
2678
2679
2680 /* Return true if OP can be inferred to be non-NULL after STMT executes,
2681    either by using a pointer dereference or attributes.  */
2682 bool
2683 infer_nonnull_range (gimple *stmt, tree op)
2684 {
2685   return infer_nonnull_range_by_dereference (stmt, op)
2686     || infer_nonnull_range_by_attribute (stmt, op);
2687 }
2688
2689 /* Return true if OP can be inferred to be non-NULL after STMT
2690    executes by using a pointer dereference.  */
2691 bool
2692 infer_nonnull_range_by_dereference (gimple *stmt, tree op)
2693 {
2694   /* We can only assume that a pointer dereference will yield
2695      non-NULL if -fdelete-null-pointer-checks is enabled.  */
2696   if (!flag_delete_null_pointer_checks
2697       || !POINTER_TYPE_P (TREE_TYPE (op))
2698       || gimple_code (stmt) == GIMPLE_ASM)
2699     return false;
2700
2701   if (walk_stmt_load_store_ops (stmt, (void *)op,
2702                                 check_loadstore, check_loadstore))
2703     return true;
2704
2705   return false;
2706 }
2707
2708 /* Return true if OP can be inferred to be a non-NULL after STMT
2709    executes by using attributes.  */
2710 bool
2711 infer_nonnull_range_by_attribute (gimple *stmt, tree op)
2712 {
2713   /* We can only assume that a pointer dereference will yield
2714      non-NULL if -fdelete-null-pointer-checks is enabled.  */
2715   if (!flag_delete_null_pointer_checks
2716       || !POINTER_TYPE_P (TREE_TYPE (op))
2717       || gimple_code (stmt) == GIMPLE_ASM)
2718     return false;
2719
2720   if (is_gimple_call (stmt) && !gimple_call_internal_p (stmt))
2721     {
2722       tree fntype = gimple_call_fntype (stmt);
2723       tree attrs = TYPE_ATTRIBUTES (fntype);
2724       for (; attrs; attrs = TREE_CHAIN (attrs))
2725         {
2726           attrs = lookup_attribute ("nonnull", attrs);
2727
2728           /* If "nonnull" wasn't specified, we know nothing about
2729              the argument.  */
2730           if (attrs == NULL_TREE)
2731             return false;
2732
2733           /* If "nonnull" applies to all the arguments, then ARG
2734              is non-null if it's in the argument list.  */
2735           if (TREE_VALUE (attrs) == NULL_TREE)
2736             {
2737               for (unsigned int i = 0; i < gimple_call_num_args (stmt); i++)
2738                 {
2739                   if (POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (stmt, i)))
2740                       && operand_equal_p (op, gimple_call_arg (stmt, i), 0))
2741                     return true;
2742                 }
2743               return false;
2744             }
2745
2746           /* Now see if op appears in the nonnull list.  */
2747           for (tree t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
2748             {
2749               unsigned int idx = TREE_INT_CST_LOW (TREE_VALUE (t)) - 1;
2750               if (idx < gimple_call_num_args (stmt))
2751                 {
2752                   tree arg = gimple_call_arg (stmt, idx);
2753                   if (operand_equal_p (op, arg, 0))
2754                     return true;
2755                 }
2756             }
2757         }
2758     }
2759
2760   /* If this function is marked as returning non-null, then we can
2761      infer OP is non-null if it is used in the return statement.  */
2762   if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
2763     if (gimple_return_retval (return_stmt)
2764         && operand_equal_p (gimple_return_retval (return_stmt), op, 0)
2765         && lookup_attribute ("returns_nonnull",
2766                              TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))))
2767       return true;
2768
2769   return false;
2770 }
2771
2772 /* Compare two case labels.  Because the front end should already have
2773    made sure that case ranges do not overlap, it is enough to only compare
2774    the CASE_LOW values of each case label.  */
2775
2776 static int
2777 compare_case_labels (const void *p1, const void *p2)
2778 {
2779   const_tree const case1 = *(const_tree const*)p1;
2780   const_tree const case2 = *(const_tree const*)p2;
2781
2782   /* The 'default' case label always goes first.  */
2783   if (!CASE_LOW (case1))
2784     return -1;
2785   else if (!CASE_LOW (case2))
2786     return 1;
2787   else
2788     return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
2789 }
2790
2791 /* Sort the case labels in LABEL_VEC in place in ascending order.  */
2792
2793 void
2794 sort_case_labels (vec<tree> label_vec)
2795 {
2796   label_vec.qsort (compare_case_labels);
2797 }
2798 \f
2799 /* Prepare a vector of case labels to be used in a GIMPLE_SWITCH statement.
2800
2801    LABELS is a vector that contains all case labels to look at.
2802
2803    INDEX_TYPE is the type of the switch index expression.  Case labels
2804    in LABELS are discarded if their values are not in the value range
2805    covered by INDEX_TYPE.  The remaining case label values are folded
2806    to INDEX_TYPE.
2807
2808    If a default case exists in LABELS, it is removed from LABELS and
2809    returned in DEFAULT_CASEP.  If no default case exists, but the
2810    case labels already cover the whole range of INDEX_TYPE, a default
2811    case is returned pointing to one of the existing case labels.
2812    Otherwise DEFAULT_CASEP is set to NULL_TREE.
2813
2814    DEFAULT_CASEP may be NULL, in which case the above comment doesn't
2815    apply and no action is taken regardless of whether a default case is
2816    found or not.  */
2817
2818 void
2819 preprocess_case_label_vec_for_gimple (vec<tree> labels,
2820                                       tree index_type,
2821                                       tree *default_casep)
2822 {
2823   tree min_value, max_value;
2824   tree default_case = NULL_TREE;
2825   size_t i, len;
2826
2827   i = 0;
2828   min_value = TYPE_MIN_VALUE (index_type);
2829   max_value = TYPE_MAX_VALUE (index_type);
2830   while (i < labels.length ())
2831     {
2832       tree elt = labels[i];
2833       tree low = CASE_LOW (elt);
2834       tree high = CASE_HIGH (elt);
2835       bool remove_element = FALSE;
2836
2837       if (low)
2838         {
2839           gcc_checking_assert (TREE_CODE (low) == INTEGER_CST);
2840           gcc_checking_assert (!high || TREE_CODE (high) == INTEGER_CST);
2841
2842           /* This is a non-default case label, i.e. it has a value.
2843
2844              See if the case label is reachable within the range of
2845              the index type.  Remove out-of-range case values.  Turn
2846              case ranges into a canonical form (high > low strictly)
2847              and convert the case label values to the index type.
2848
2849              NB: The type of gimple_switch_index() may be the promoted
2850              type, but the case labels retain the original type.  */
2851
2852           if (high)
2853             {
2854               /* This is a case range.  Discard empty ranges.
2855                  If the bounds or the range are equal, turn this
2856                  into a simple (one-value) case.  */
2857               int cmp = tree_int_cst_compare (high, low);
2858               if (cmp < 0)
2859                 remove_element = TRUE;
2860               else if (cmp == 0)
2861                 high = NULL_TREE;
2862             }
2863
2864           if (! high)
2865             {
2866               /* If the simple case value is unreachable, ignore it.  */
2867               if ((TREE_CODE (min_value) == INTEGER_CST
2868                    && tree_int_cst_compare (low, min_value) < 0)
2869                   || (TREE_CODE (max_value) == INTEGER_CST
2870                       && tree_int_cst_compare (low, max_value) > 0))
2871                 remove_element = TRUE;
2872               else
2873                 low = fold_convert (index_type, low);
2874             }
2875           else
2876             {
2877               /* If the entire case range is unreachable, ignore it.  */
2878               if ((TREE_CODE (min_value) == INTEGER_CST
2879                    && tree_int_cst_compare (high, min_value) < 0)
2880                   || (TREE_CODE (max_value) == INTEGER_CST
2881                       && tree_int_cst_compare (low, max_value) > 0))
2882                 remove_element = TRUE;
2883               else
2884                 {
2885                   /* If the lower bound is less than the index type's
2886                      minimum value, truncate the range bounds.  */
2887                   if (TREE_CODE (min_value) == INTEGER_CST
2888                       && tree_int_cst_compare (low, min_value) < 0)
2889                     low = min_value;
2890                   low = fold_convert (index_type, low);
2891
2892                   /* If the upper bound is greater than the index type's
2893                      maximum value, truncate the range bounds.  */
2894                   if (TREE_CODE (max_value) == INTEGER_CST
2895                       && tree_int_cst_compare (high, max_value) > 0)
2896                     high = max_value;
2897                   high = fold_convert (index_type, high);
2898
2899                   /* We may have folded a case range to a one-value case.  */
2900                   if (tree_int_cst_equal (low, high))
2901                     high = NULL_TREE;
2902                 }
2903             }
2904
2905           CASE_LOW (elt) = low;
2906           CASE_HIGH (elt) = high;
2907         }
2908       else
2909         {
2910           gcc_assert (!default_case);
2911           default_case = elt;
2912           /* The default case must be passed separately to the
2913              gimple_build_switch routine.  But if DEFAULT_CASEP
2914              is NULL, we do not remove the default case (it would
2915              be completely lost).  */
2916           if (default_casep)
2917             remove_element = TRUE;
2918         }
2919
2920       if (remove_element)
2921         labels.ordered_remove (i);
2922       else
2923         i++;
2924     }
2925   len = i;
2926
2927   if (!labels.is_empty ())
2928     sort_case_labels (labels);
2929
2930   if (default_casep && !default_case)
2931     {
2932       /* If the switch has no default label, add one, so that we jump
2933          around the switch body.  If the labels already cover the whole
2934          range of the switch index_type, add the default label pointing
2935          to one of the existing labels.  */
2936       if (len
2937           && TYPE_MIN_VALUE (index_type)
2938           && TYPE_MAX_VALUE (index_type)
2939           && tree_int_cst_equal (CASE_LOW (labels[0]),
2940                                  TYPE_MIN_VALUE (index_type)))
2941         {
2942           tree low, high = CASE_HIGH (labels[len - 1]);
2943           if (!high)
2944             high = CASE_LOW (labels[len - 1]);
2945           if (tree_int_cst_equal (high, TYPE_MAX_VALUE (index_type)))
2946             {
2947               for (i = 1; i < len; i++)
2948                 {
2949                   high = CASE_LOW (labels[i]);
2950                   low = CASE_HIGH (labels[i - 1]);
2951                   if (!low)
2952                     low = CASE_LOW (labels[i - 1]);
2953                   if (wi::add (low, 1) != high)
2954                     break;
2955                 }
2956               if (i == len)
2957                 {
2958                   tree label = CASE_LABEL (labels[0]);
2959                   default_case = build_case_label (NULL_TREE, NULL_TREE,
2960                                                    label);
2961                 }
2962             }
2963         }
2964     }
2965
2966   if (default_casep)
2967     *default_casep = default_case;
2968 }
2969
2970 /* Set the location of all statements in SEQ to LOC.  */
2971
2972 void
2973 gimple_seq_set_location (gimple_seq seq, location_t loc)
2974 {
2975   for (gimple_stmt_iterator i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
2976     gimple_set_location (gsi_stmt (i), loc);
2977 }
2978
2979 /* Release SSA_NAMEs in SEQ as well as the GIMPLE statements.  */
2980
2981 void
2982 gimple_seq_discard (gimple_seq seq)
2983 {
2984   gimple_stmt_iterator gsi;
2985
2986   for (gsi = gsi_start (seq); !gsi_end_p (gsi); )
2987     {
2988       gimple *stmt = gsi_stmt (gsi);
2989       gsi_remove (&gsi, true);
2990       release_defs (stmt);
2991       ggc_free (stmt);
2992     }
2993 }
2994
2995 /* See if STMT now calls function that takes no parameters and if so, drop
2996    call arguments.  This is used when devirtualization machinery redirects
2997    to __builtiln_unreacahble or __cxa_pure_virutal.  */
2998
2999 void
3000 maybe_remove_unused_call_args (struct function *fn, gimple *stmt)
3001 {
3002   tree decl = gimple_call_fndecl (stmt);
3003   if (TYPE_ARG_TYPES (TREE_TYPE (decl))
3004       && TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl))) == void_type_node
3005       && gimple_call_num_args (stmt))
3006     {
3007       gimple_set_num_ops (stmt, 3);
3008       update_stmt_fn (fn, stmt);
3009     }
3010 }
3011
3012 /* Return false if STMT will likely expand to real function call.  */
3013
3014 bool
3015 gimple_inexpensive_call_p (gimple *stmt)
3016 {
3017   if (gimple_call_internal_p (stmt))
3018     return true;
3019   tree decl = gimple_call_fndecl (stmt);
3020   if (decl && is_inexpensive_builtin (decl))
3021     return true;
3022   return false;
3023 }