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