]> gcc.gnu.org Git - gcc.git/blame - gcc/gimple.c
reflect: Use hand-coded .eh_frame section rather than CFI directives.
[gcc.git] / gcc / gimple.c
CommitLineData
726a989a
RB
1/* Gimple IR support functions.
2
d1e082c2 3 Copyright (C) 2007-2013 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"
25#include "tm.h"
d7f09764 26#include "target.h"
726a989a
RB
27#include "tree.h"
28#include "ggc.h"
726a989a
RB
29#include "hard-reg-set.h"
30#include "basic-block.h"
31#include "gimple.h"
32#include "diagnostic.h"
ff2a63a7 33#include "tree-flow.h"
726a989a
RB
34#include "value-prof.h"
35#include "flags.h"
d7f09764 36#include "alias.h"
4537ec0c 37#include "demangle.h"
0f443ad0 38#include "langhooks.h"
726a989a 39
b8f4e58f 40/* Global canonical type table. */
4490cae6
RG
41static GTY((if_marked ("ggc_marked_p"), param_is (union tree_node)))
42 htab_t gimple_canonical_types;
a844a60b
RG
43static GTY((if_marked ("tree_int_map_marked_p"), param_is (struct tree_int_map)))
44 htab_t canonical_type_hash_cache;
d7f09764 45
f2c4a81c 46/* All the tuples have their operand vector (if present) at the very bottom
726a989a
RB
47 of the structure. Therefore, the offset required to find the
48 operands vector the size of the structure minus the size of the 1
49 element tree array at the end (see gimple_ops). */
f2c4a81c
RH
50#define DEFGSSTRUCT(SYM, STRUCT, HAS_TREE_OP) \
51 (HAS_TREE_OP ? sizeof (struct STRUCT) - sizeof (tree) : 0),
6bc7bc14 52EXPORTED_CONST size_t gimple_ops_offset_[] = {
f2c4a81c
RH
53#include "gsstruct.def"
54};
55#undef DEFGSSTRUCT
56
c3284718 57#define DEFGSSTRUCT(SYM, STRUCT, HAS_TREE_OP) sizeof (struct STRUCT),
f2c4a81c
RH
58static const size_t gsstruct_code_size[] = {
59#include "gsstruct.def"
60};
61#undef DEFGSSTRUCT
62
63#define DEFGSCODE(SYM, NAME, GSSCODE) NAME,
64const char *const gimple_code_name[] = {
65#include "gimple.def"
66};
67#undef DEFGSCODE
68
69#define DEFGSCODE(SYM, NAME, GSSCODE) GSSCODE,
70EXPORTED_CONST enum gimple_statement_structure_enum gss_for_code_[] = {
726a989a
RB
71#include "gimple.def"
72};
73#undef DEFGSCODE
74
726a989a
RB
75/* Gimple stats. */
76
77int gimple_alloc_counts[(int) gimple_alloc_kind_all];
78int gimple_alloc_sizes[(int) gimple_alloc_kind_all];
79
80/* Keep in sync with gimple.h:enum gimple_alloc_kind. */
81static const char * const gimple_alloc_kind_names[] = {
82 "assignments",
83 "phi nodes",
84 "conditionals",
726a989a
RB
85 "everything else"
86};
87
726a989a
RB
88/* Private API manipulation functions shared only with some
89 other files. */
90extern void gimple_set_stored_syms (gimple, bitmap, bitmap_obstack *);
91extern void gimple_set_loaded_syms (gimple, bitmap, bitmap_obstack *);
92
93/* Gimple tuple constructors.
94 Note: Any constructor taking a ``gimple_seq'' as a parameter, can
95 be passed a NULL to start with an empty sequence. */
96
97/* Set the code for statement G to CODE. */
98
99static inline void
100gimple_set_code (gimple g, enum gimple_code code)
101{
102 g->gsbase.code = code;
103}
104
726a989a
RB
105/* Return the number of bytes needed to hold a GIMPLE statement with
106 code CODE. */
107
f2c4a81c 108static inline size_t
726a989a
RB
109gimple_size (enum gimple_code code)
110{
f2c4a81c 111 return gsstruct_code_size[gss_for_code (code)];
726a989a
RB
112}
113
726a989a
RB
114/* Allocate memory for a GIMPLE statement with code CODE and NUM_OPS
115 operands. */
116
d7f09764 117gimple
726a989a
RB
118gimple_alloc_stat (enum gimple_code code, unsigned num_ops MEM_STAT_DECL)
119{
120 size_t size;
121 gimple stmt;
122
123 size = gimple_size (code);
124 if (num_ops > 0)
125 size += sizeof (tree) * (num_ops - 1);
126
7aa6d18a
SB
127 if (GATHER_STATISTICS)
128 {
129 enum gimple_alloc_kind kind = gimple_alloc_kind (code);
130 gimple_alloc_counts[(int) kind]++;
131 gimple_alloc_sizes[(int) kind] += size;
132 }
726a989a 133
a9429e29 134 stmt = ggc_alloc_cleared_gimple_statement_d_stat (size PASS_MEM_STAT);
726a989a
RB
135 gimple_set_code (stmt, code);
136 gimple_set_num_ops (stmt, num_ops);
137
138 /* Do not call gimple_set_modified here as it has other side
139 effects and this tuple is still not completely built. */
140 stmt->gsbase.modified = 1;
355a7673 141 gimple_init_singleton (stmt);
726a989a
RB
142
143 return stmt;
144}
145
146/* Set SUBCODE to be the code of the expression computed by statement G. */
147
148static inline void
149gimple_set_subcode (gimple g, unsigned subcode)
150{
151 /* We only have 16 bits for the RHS code. Assert that we are not
152 overflowing it. */
153 gcc_assert (subcode < (1 << 16));
154 g->gsbase.subcode = subcode;
155}
156
157
158
159/* Build a tuple with operands. CODE is the statement to build (which
160 must be one of the GIMPLE_WITH_OPS tuples). SUBCODE is the sub-code
b8698a0f 161 for the new tuple. NUM_OPS is the number of operands to allocate. */
726a989a
RB
162
163#define gimple_build_with_ops(c, s, n) \
164 gimple_build_with_ops_stat (c, s, n MEM_STAT_INFO)
165
166static gimple
b5b8b0ac 167gimple_build_with_ops_stat (enum gimple_code code, unsigned subcode,
726a989a
RB
168 unsigned num_ops MEM_STAT_DECL)
169{
170 gimple s = gimple_alloc_stat (code, num_ops PASS_MEM_STAT);
171 gimple_set_subcode (s, subcode);
172
173 return s;
174}
175
176
177/* Build a GIMPLE_RETURN statement returning RETVAL. */
178
179gimple
180gimple_build_return (tree retval)
181{
bbbbb16a 182 gimple s = gimple_build_with_ops (GIMPLE_RETURN, ERROR_MARK, 1);
726a989a
RB
183 if (retval)
184 gimple_return_set_retval (s, retval);
185 return s;
186}
187
d086d311
RG
188/* Reset alias information on call S. */
189
190void
191gimple_call_reset_alias_info (gimple s)
192{
193 if (gimple_call_flags (s) & ECF_CONST)
194 memset (gimple_call_use_set (s), 0, sizeof (struct pt_solution));
195 else
196 pt_solution_reset (gimple_call_use_set (s));
197 if (gimple_call_flags (s) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
198 memset (gimple_call_clobber_set (s), 0, sizeof (struct pt_solution));
199 else
200 pt_solution_reset (gimple_call_clobber_set (s));
201}
202
21860814
JJ
203/* Helper for gimple_build_call, gimple_build_call_valist,
204 gimple_build_call_vec and gimple_build_call_from_tree. Build the basic
205 components of a GIMPLE_CALL statement to function FN with NARGS
206 arguments. */
726a989a
RB
207
208static inline gimple
209gimple_build_call_1 (tree fn, unsigned nargs)
210{
bbbbb16a 211 gimple s = gimple_build_with_ops (GIMPLE_CALL, ERROR_MARK, nargs + 3);
7c9577be
RG
212 if (TREE_CODE (fn) == FUNCTION_DECL)
213 fn = build_fold_addr_expr (fn);
726a989a 214 gimple_set_op (s, 1, fn);
f20ca725 215 gimple_call_set_fntype (s, TREE_TYPE (TREE_TYPE (fn)));
d086d311 216 gimple_call_reset_alias_info (s);
726a989a
RB
217 return s;
218}
219
220
221/* Build a GIMPLE_CALL statement to function FN with the arguments
222 specified in vector ARGS. */
223
224gimple
9771b263 225gimple_build_call_vec (tree fn, vec<tree> args)
726a989a
RB
226{
227 unsigned i;
9771b263 228 unsigned nargs = args.length ();
726a989a
RB
229 gimple call = gimple_build_call_1 (fn, nargs);
230
231 for (i = 0; i < nargs; i++)
9771b263 232 gimple_call_set_arg (call, i, args[i]);
726a989a
RB
233
234 return call;
235}
236
237
238/* Build a GIMPLE_CALL statement to function FN. NARGS is the number of
239 arguments. The ... are the arguments. */
240
241gimple
242gimple_build_call (tree fn, unsigned nargs, ...)
243{
244 va_list ap;
245 gimple call;
246 unsigned i;
247
248 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL || is_gimple_call_addr (fn));
249
250 call = gimple_build_call_1 (fn, nargs);
251
252 va_start (ap, nargs);
253 for (i = 0; i < nargs; i++)
254 gimple_call_set_arg (call, i, va_arg (ap, tree));
255 va_end (ap);
256
257 return call;
258}
259
260
21860814
JJ
261/* Build a GIMPLE_CALL statement to function FN. NARGS is the number of
262 arguments. AP contains the arguments. */
263
264gimple
265gimple_build_call_valist (tree fn, unsigned nargs, va_list ap)
266{
267 gimple call;
268 unsigned i;
269
270 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL || is_gimple_call_addr (fn));
271
272 call = gimple_build_call_1 (fn, nargs);
273
274 for (i = 0; i < nargs; i++)
275 gimple_call_set_arg (call, i, va_arg (ap, tree));
276
277 return call;
278}
279
280
25583c4f
RS
281/* Helper for gimple_build_call_internal and gimple_build_call_internal_vec.
282 Build the basic components of a GIMPLE_CALL statement to internal
283 function FN with NARGS arguments. */
284
285static inline gimple
286gimple_build_call_internal_1 (enum internal_fn fn, unsigned nargs)
287{
288 gimple s = gimple_build_with_ops (GIMPLE_CALL, ERROR_MARK, nargs + 3);
289 s->gsbase.subcode |= GF_CALL_INTERNAL;
290 gimple_call_set_internal_fn (s, fn);
291 gimple_call_reset_alias_info (s);
292 return s;
293}
294
295
296/* Build a GIMPLE_CALL statement to internal function FN. NARGS is
297 the number of arguments. The ... are the arguments. */
298
299gimple
300gimple_build_call_internal (enum internal_fn fn, unsigned nargs, ...)
301{
302 va_list ap;
303 gimple call;
304 unsigned i;
305
306 call = gimple_build_call_internal_1 (fn, nargs);
307 va_start (ap, nargs);
308 for (i = 0; i < nargs; i++)
309 gimple_call_set_arg (call, i, va_arg (ap, tree));
310 va_end (ap);
311
312 return call;
313}
314
315
316/* Build a GIMPLE_CALL statement to internal function FN with the arguments
317 specified in vector ARGS. */
318
319gimple
9771b263 320gimple_build_call_internal_vec (enum internal_fn fn, vec<tree> args)
25583c4f
RS
321{
322 unsigned i, nargs;
323 gimple call;
324
9771b263 325 nargs = args.length ();
25583c4f
RS
326 call = gimple_build_call_internal_1 (fn, nargs);
327 for (i = 0; i < nargs; i++)
9771b263 328 gimple_call_set_arg (call, i, args[i]);
25583c4f
RS
329
330 return call;
331}
332
333
726a989a
RB
334/* Build a GIMPLE_CALL statement from CALL_EXPR T. Note that T is
335 assumed to be in GIMPLE form already. Minimal checking is done of
336 this fact. */
337
338gimple
339gimple_build_call_from_tree (tree t)
340{
341 unsigned i, nargs;
342 gimple call;
343 tree fndecl = get_callee_fndecl (t);
344
345 gcc_assert (TREE_CODE (t) == CALL_EXPR);
346
347 nargs = call_expr_nargs (t);
348 call = gimple_build_call_1 (fndecl ? fndecl : CALL_EXPR_FN (t), nargs);
349
350 for (i = 0; i < nargs; i++)
351 gimple_call_set_arg (call, i, CALL_EXPR_ARG (t, i));
352
353 gimple_set_block (call, TREE_BLOCK (t));
354
355 /* Carry all the CALL_EXPR flags to the new GIMPLE_CALL. */
356 gimple_call_set_chain (call, CALL_EXPR_STATIC_CHAIN (t));
357 gimple_call_set_tail (call, CALL_EXPR_TAILCALL (t));
726a989a 358 gimple_call_set_return_slot_opt (call, CALL_EXPR_RETURN_SLOT_OPT (t));
63d2a353
MM
359 if (fndecl
360 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
13e49da9
TV
361 && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_ALLOCA
362 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_ALLOCA_WITH_ALIGN))
63d2a353
MM
363 gimple_call_set_alloca_for_var (call, CALL_ALLOCA_FOR_VAR_P (t));
364 else
365 gimple_call_set_from_thunk (call, CALL_FROM_THUNK_P (t));
726a989a 366 gimple_call_set_va_arg_pack (call, CALL_EXPR_VA_ARG_PACK (t));
9bb1a81b 367 gimple_call_set_nothrow (call, TREE_NOTHROW (t));
d665b6e5 368 gimple_set_no_warning (call, TREE_NO_WARNING (t));
726a989a
RB
369
370 return call;
371}
372
373
374/* Extract the operands and code for expression EXPR into *SUBCODE_P,
0354c0c7 375 *OP1_P, *OP2_P and *OP3_P respectively. */
726a989a
RB
376
377void
0354c0c7
BS
378extract_ops_from_tree_1 (tree expr, enum tree_code *subcode_p, tree *op1_p,
379 tree *op2_p, tree *op3_p)
726a989a 380{
82d6e6fc 381 enum gimple_rhs_class grhs_class;
726a989a
RB
382
383 *subcode_p = TREE_CODE (expr);
82d6e6fc 384 grhs_class = get_gimple_rhs_class (*subcode_p);
726a989a 385
0354c0c7 386 if (grhs_class == GIMPLE_TERNARY_RHS)
726a989a
RB
387 {
388 *op1_p = TREE_OPERAND (expr, 0);
389 *op2_p = TREE_OPERAND (expr, 1);
0354c0c7
BS
390 *op3_p = TREE_OPERAND (expr, 2);
391 }
392 else if (grhs_class == GIMPLE_BINARY_RHS)
393 {
394 *op1_p = TREE_OPERAND (expr, 0);
395 *op2_p = TREE_OPERAND (expr, 1);
396 *op3_p = NULL_TREE;
726a989a 397 }
82d6e6fc 398 else if (grhs_class == GIMPLE_UNARY_RHS)
726a989a
RB
399 {
400 *op1_p = TREE_OPERAND (expr, 0);
401 *op2_p = NULL_TREE;
0354c0c7 402 *op3_p = NULL_TREE;
726a989a 403 }
82d6e6fc 404 else if (grhs_class == GIMPLE_SINGLE_RHS)
726a989a
RB
405 {
406 *op1_p = expr;
407 *op2_p = NULL_TREE;
0354c0c7 408 *op3_p = NULL_TREE;
726a989a
RB
409 }
410 else
411 gcc_unreachable ();
412}
413
414
415/* Build a GIMPLE_ASSIGN statement.
416
417 LHS of the assignment.
418 RHS of the assignment which can be unary or binary. */
419
420gimple
421gimple_build_assign_stat (tree lhs, tree rhs MEM_STAT_DECL)
422{
423 enum tree_code subcode;
0354c0c7 424 tree op1, op2, op3;
726a989a 425
0354c0c7 426 extract_ops_from_tree_1 (rhs, &subcode, &op1, &op2, &op3);
73804b12
RG
427 return gimple_build_assign_with_ops (subcode, lhs, op1, op2, op3
428 PASS_MEM_STAT);
726a989a
RB
429}
430
431
432/* Build a GIMPLE_ASSIGN statement with sub-code SUBCODE and operands
433 OP1 and OP2. If OP2 is NULL then SUBCODE must be of class
434 GIMPLE_UNARY_RHS or GIMPLE_SINGLE_RHS. */
435
436gimple
73804b12
RG
437gimple_build_assign_with_ops (enum tree_code subcode, tree lhs, tree op1,
438 tree op2, tree op3 MEM_STAT_DECL)
726a989a
RB
439{
440 unsigned num_ops;
441 gimple p;
442
443 /* Need 1 operand for LHS and 1 or 2 for the RHS (depending on the
444 code). */
445 num_ops = get_gimple_rhs_num_ops (subcode) + 1;
b8698a0f 446
b5b8b0ac 447 p = gimple_build_with_ops_stat (GIMPLE_ASSIGN, (unsigned)subcode, num_ops
726a989a
RB
448 PASS_MEM_STAT);
449 gimple_assign_set_lhs (p, lhs);
450 gimple_assign_set_rhs1 (p, op1);
451 if (op2)
452 {
453 gcc_assert (num_ops > 2);
454 gimple_assign_set_rhs2 (p, op2);
455 }
456
0354c0c7
BS
457 if (op3)
458 {
459 gcc_assert (num_ops > 3);
460 gimple_assign_set_rhs3 (p, op3);
461 }
462
726a989a
RB
463 return p;
464}
465
73804b12
RG
466gimple
467gimple_build_assign_with_ops (enum tree_code subcode, tree lhs, tree op1,
468 tree op2 MEM_STAT_DECL)
469{
470 return gimple_build_assign_with_ops (subcode, lhs, op1, op2, NULL_TREE
471 PASS_MEM_STAT);
472}
473
726a989a
RB
474
475/* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
476
477 DST/SRC are the destination and source respectively. You can pass
478 ungimplified trees in DST or SRC, in which case they will be
479 converted to a gimple operand if necessary.
480
481 This function returns the newly created GIMPLE_ASSIGN tuple. */
482
5fd8300b 483gimple
726a989a 484gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
b8698a0f 485{
726a989a
RB
486 tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
487 gimplify_and_add (t, seq_p);
488 ggc_free (t);
489 return gimple_seq_last_stmt (*seq_p);
490}
491
492
493/* Build a GIMPLE_COND statement.
494
495 PRED is the condition used to compare LHS and the RHS.
496 T_LABEL is the label to jump to if the condition is true.
497 F_LABEL is the label to jump to otherwise. */
498
499gimple
500gimple_build_cond (enum tree_code pred_code, tree lhs, tree rhs,
501 tree t_label, tree f_label)
502{
503 gimple p;
504
505 gcc_assert (TREE_CODE_CLASS (pred_code) == tcc_comparison);
506 p = gimple_build_with_ops (GIMPLE_COND, pred_code, 4);
507 gimple_cond_set_lhs (p, lhs);
508 gimple_cond_set_rhs (p, rhs);
509 gimple_cond_set_true_label (p, t_label);
510 gimple_cond_set_false_label (p, f_label);
511 return p;
512}
513
514
515/* Extract operands for a GIMPLE_COND statement out of COND_EXPR tree COND. */
516
517void
518gimple_cond_get_ops_from_tree (tree cond, enum tree_code *code_p,
519 tree *lhs_p, tree *rhs_p)
520{
521 gcc_assert (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison
522 || TREE_CODE (cond) == TRUTH_NOT_EXPR
523 || is_gimple_min_invariant (cond)
524 || SSA_VAR_P (cond));
525
526 extract_ops_from_tree (cond, code_p, lhs_p, rhs_p);
527
528 /* Canonicalize conditionals of the form 'if (!VAL)'. */
529 if (*code_p == TRUTH_NOT_EXPR)
530 {
531 *code_p = EQ_EXPR;
532 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
e8160c9a 533 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
726a989a
RB
534 }
535 /* Canonicalize conditionals of the form 'if (VAL)' */
536 else if (TREE_CODE_CLASS (*code_p) != tcc_comparison)
537 {
538 *code_p = NE_EXPR;
539 gcc_assert (*lhs_p && *rhs_p == NULL_TREE);
e8160c9a 540 *rhs_p = build_zero_cst (TREE_TYPE (*lhs_p));
726a989a
RB
541 }
542}
543
544
545/* Build a GIMPLE_COND statement from the conditional expression tree
546 COND. T_LABEL and F_LABEL are as in gimple_build_cond. */
547
548gimple
549gimple_build_cond_from_tree (tree cond, tree t_label, tree f_label)
550{
551 enum tree_code code;
552 tree lhs, rhs;
553
554 gimple_cond_get_ops_from_tree (cond, &code, &lhs, &rhs);
555 return gimple_build_cond (code, lhs, rhs, t_label, f_label);
556}
557
558/* Set code, lhs, and rhs of a GIMPLE_COND from a suitable
559 boolean expression tree COND. */
560
561void
562gimple_cond_set_condition_from_tree (gimple stmt, tree cond)
563{
564 enum tree_code code;
565 tree lhs, rhs;
566
567 gimple_cond_get_ops_from_tree (cond, &code, &lhs, &rhs);
568 gimple_cond_set_condition (stmt, code, lhs, rhs);
569}
570
571/* Build a GIMPLE_LABEL statement for LABEL. */
572
573gimple
574gimple_build_label (tree label)
575{
bbbbb16a 576 gimple p = gimple_build_with_ops (GIMPLE_LABEL, ERROR_MARK, 1);
726a989a
RB
577 gimple_label_set_label (p, label);
578 return p;
579}
580
581/* Build a GIMPLE_GOTO statement to label DEST. */
582
583gimple
584gimple_build_goto (tree dest)
585{
bbbbb16a 586 gimple p = gimple_build_with_ops (GIMPLE_GOTO, ERROR_MARK, 1);
726a989a
RB
587 gimple_goto_set_dest (p, dest);
588 return p;
589}
590
591
592/* Build a GIMPLE_NOP statement. */
593
b8698a0f 594gimple
726a989a
RB
595gimple_build_nop (void)
596{
597 return gimple_alloc (GIMPLE_NOP, 0);
598}
599
600
601/* Build a GIMPLE_BIND statement.
602 VARS are the variables in BODY.
603 BLOCK is the containing block. */
604
605gimple
606gimple_build_bind (tree vars, gimple_seq body, tree block)
607{
608 gimple p = gimple_alloc (GIMPLE_BIND, 0);
609 gimple_bind_set_vars (p, vars);
610 if (body)
611 gimple_bind_set_body (p, body);
612 if (block)
613 gimple_bind_set_block (p, block);
614 return p;
615}
616
617/* Helper function to set the simple fields of a asm stmt.
618
619 STRING is a pointer to a string that is the asm blocks assembly code.
620 NINPUT is the number of register inputs.
621 NOUTPUT is the number of register outputs.
622 NCLOBBERS is the number of clobbered registers.
623 */
624
625static inline gimple
b8698a0f 626gimple_build_asm_1 (const char *string, unsigned ninputs, unsigned noutputs,
1c384bf1 627 unsigned nclobbers, unsigned nlabels)
726a989a
RB
628{
629 gimple p;
630 int size = strlen (string);
631
1c384bf1
RH
632 /* ASMs with labels cannot have outputs. This should have been
633 enforced by the front end. */
634 gcc_assert (nlabels == 0 || noutputs == 0);
635
bbbbb16a 636 p = gimple_build_with_ops (GIMPLE_ASM, ERROR_MARK,
1c384bf1 637 ninputs + noutputs + nclobbers + nlabels);
726a989a
RB
638
639 p->gimple_asm.ni = ninputs;
640 p->gimple_asm.no = noutputs;
641 p->gimple_asm.nc = nclobbers;
1c384bf1 642 p->gimple_asm.nl = nlabels;
726a989a
RB
643 p->gimple_asm.string = ggc_alloc_string (string, size);
644
7aa6d18a
SB
645 if (GATHER_STATISTICS)
646 gimple_alloc_sizes[(int) gimple_alloc_kind (GIMPLE_ASM)] += size;
b8698a0f 647
726a989a
RB
648 return p;
649}
650
651/* Build a GIMPLE_ASM statement.
652
653 STRING is the assembly code.
654 NINPUT is the number of register inputs.
655 NOUTPUT is the number of register outputs.
656 NCLOBBERS is the number of clobbered registers.
657 INPUTS is a vector of the input register parameters.
658 OUTPUTS is a vector of the output register parameters.
1c384bf1
RH
659 CLOBBERS is a vector of the clobbered register parameters.
660 LABELS is a vector of destination labels. */
726a989a
RB
661
662gimple
9771b263
DN
663gimple_build_asm_vec (const char *string, vec<tree, va_gc> *inputs,
664 vec<tree, va_gc> *outputs, vec<tree, va_gc> *clobbers,
665 vec<tree, va_gc> *labels)
726a989a
RB
666{
667 gimple p;
668 unsigned i;
669
670 p = gimple_build_asm_1 (string,
9771b263
DN
671 vec_safe_length (inputs),
672 vec_safe_length (outputs),
673 vec_safe_length (clobbers),
674 vec_safe_length (labels));
b8698a0f 675
9771b263
DN
676 for (i = 0; i < vec_safe_length (inputs); i++)
677 gimple_asm_set_input_op (p, i, (*inputs)[i]);
726a989a 678
9771b263
DN
679 for (i = 0; i < vec_safe_length (outputs); i++)
680 gimple_asm_set_output_op (p, i, (*outputs)[i]);
726a989a 681
9771b263
DN
682 for (i = 0; i < vec_safe_length (clobbers); i++)
683 gimple_asm_set_clobber_op (p, i, (*clobbers)[i]);
b8698a0f 684
9771b263
DN
685 for (i = 0; i < vec_safe_length (labels); i++)
686 gimple_asm_set_label_op (p, i, (*labels)[i]);
b8698a0f 687
726a989a
RB
688 return p;
689}
690
691/* Build a GIMPLE_CATCH statement.
692
693 TYPES are the catch types.
694 HANDLER is the exception handler. */
695
696gimple
697gimple_build_catch (tree types, gimple_seq handler)
698{
699 gimple p = gimple_alloc (GIMPLE_CATCH, 0);
700 gimple_catch_set_types (p, types);
701 if (handler)
702 gimple_catch_set_handler (p, handler);
703
704 return p;
705}
706
707/* Build a GIMPLE_EH_FILTER statement.
708
709 TYPES are the filter's types.
710 FAILURE is the filter's failure action. */
711
712gimple
713gimple_build_eh_filter (tree types, gimple_seq failure)
714{
715 gimple p = gimple_alloc (GIMPLE_EH_FILTER, 0);
716 gimple_eh_filter_set_types (p, types);
717 if (failure)
718 gimple_eh_filter_set_failure (p, failure);
719
720 return p;
721}
722
1d65f45c
RH
723/* Build a GIMPLE_EH_MUST_NOT_THROW statement. */
724
725gimple
726gimple_build_eh_must_not_throw (tree decl)
727{
786f715d 728 gimple p = gimple_alloc (GIMPLE_EH_MUST_NOT_THROW, 0);
1d65f45c
RH
729
730 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
731 gcc_assert (flags_from_decl_or_type (decl) & ECF_NORETURN);
d7f09764 732 gimple_eh_must_not_throw_set_fndecl (p, decl);
1d65f45c
RH
733
734 return p;
735}
736
0a35513e
AH
737/* Build a GIMPLE_EH_ELSE statement. */
738
739gimple
740gimple_build_eh_else (gimple_seq n_body, gimple_seq e_body)
741{
742 gimple p = gimple_alloc (GIMPLE_EH_ELSE, 0);
743 gimple_eh_else_set_n_body (p, n_body);
744 gimple_eh_else_set_e_body (p, e_body);
745 return p;
746}
747
726a989a
RB
748/* Build a GIMPLE_TRY statement.
749
750 EVAL is the expression to evaluate.
751 CLEANUP is the cleanup expression.
752 KIND is either GIMPLE_TRY_CATCH or GIMPLE_TRY_FINALLY depending on
753 whether this is a try/catch or a try/finally respectively. */
754
755gimple
756gimple_build_try (gimple_seq eval, gimple_seq cleanup,
757 enum gimple_try_flags kind)
758{
759 gimple p;
760
761 gcc_assert (kind == GIMPLE_TRY_CATCH || kind == GIMPLE_TRY_FINALLY);
762 p = gimple_alloc (GIMPLE_TRY, 0);
763 gimple_set_subcode (p, kind);
764 if (eval)
765 gimple_try_set_eval (p, eval);
766 if (cleanup)
767 gimple_try_set_cleanup (p, cleanup);
768
769 return p;
770}
771
772/* Construct a GIMPLE_WITH_CLEANUP_EXPR statement.
773
774 CLEANUP is the cleanup expression. */
775
776gimple
777gimple_build_wce (gimple_seq cleanup)
778{
779 gimple p = gimple_alloc (GIMPLE_WITH_CLEANUP_EXPR, 0);
780 if (cleanup)
781 gimple_wce_set_cleanup (p, cleanup);
782
783 return p;
784}
785
786
1d65f45c 787/* Build a GIMPLE_RESX statement. */
726a989a
RB
788
789gimple
790gimple_build_resx (int region)
791{
1d65f45c
RH
792 gimple p = gimple_build_with_ops (GIMPLE_RESX, ERROR_MARK, 0);
793 p->gimple_eh_ctrl.region = region;
726a989a
RB
794 return p;
795}
796
797
798/* The helper for constructing a gimple switch statement.
799 INDEX is the switch's index.
800 NLABELS is the number of labels in the switch excluding the default.
801 DEFAULT_LABEL is the default label for the switch statement. */
802
b8698a0f 803gimple
1d65f45c 804gimple_build_switch_nlabels (unsigned nlabels, tree index, tree default_label)
726a989a
RB
805{
806 /* nlabels + 1 default label + 1 index. */
fd8d363e 807 gcc_checking_assert (default_label);
bbbbb16a 808 gimple p = gimple_build_with_ops (GIMPLE_SWITCH, ERROR_MARK,
fd8d363e 809 1 + 1 + nlabels);
726a989a 810 gimple_switch_set_index (p, index);
fd8d363e 811 gimple_switch_set_default_label (p, default_label);
726a989a
RB
812 return p;
813}
814
726a989a
RB
815/* Build a GIMPLE_SWITCH statement.
816
817 INDEX is the switch's index.
818 DEFAULT_LABEL is the default label
819 ARGS is a vector of labels excluding the default. */
820
821gimple
9771b263 822gimple_build_switch (tree index, tree default_label, vec<tree> args)
726a989a 823{
9771b263 824 unsigned i, nlabels = args.length ();
fd8d363e 825
1d65f45c 826 gimple p = gimple_build_switch_nlabels (nlabels, index, default_label);
726a989a 827
1d65f45c 828 /* Copy the labels from the vector to the switch statement. */
1d65f45c 829 for (i = 0; i < nlabels; i++)
9771b263 830 gimple_switch_set_label (p, i + 1, args[i]);
726a989a
RB
831
832 return p;
833}
834
1d65f45c
RH
835/* Build a GIMPLE_EH_DISPATCH statement. */
836
837gimple
838gimple_build_eh_dispatch (int region)
839{
840 gimple p = gimple_build_with_ops (GIMPLE_EH_DISPATCH, ERROR_MARK, 0);
841 p->gimple_eh_ctrl.region = region;
842 return p;
843}
726a989a 844
b5b8b0ac
AO
845/* Build a new GIMPLE_DEBUG_BIND statement.
846
847 VAR is bound to VALUE; block and location are taken from STMT. */
848
849gimple
850gimple_build_debug_bind_stat (tree var, tree value, gimple stmt MEM_STAT_DECL)
851{
852 gimple p = gimple_build_with_ops_stat (GIMPLE_DEBUG,
853 (unsigned)GIMPLE_DEBUG_BIND, 2
854 PASS_MEM_STAT);
855
856 gimple_debug_bind_set_var (p, var);
857 gimple_debug_bind_set_value (p, value);
858 if (stmt)
5368224f 859 gimple_set_location (p, gimple_location (stmt));
b5b8b0ac
AO
860
861 return p;
862}
863
864
ddb555ed
JJ
865/* Build a new GIMPLE_DEBUG_SOURCE_BIND statement.
866
867 VAR is bound to VALUE; block and location are taken from STMT. */
868
869gimple
870gimple_build_debug_source_bind_stat (tree var, tree value,
871 gimple stmt MEM_STAT_DECL)
872{
873 gimple p = gimple_build_with_ops_stat (GIMPLE_DEBUG,
874 (unsigned)GIMPLE_DEBUG_SOURCE_BIND, 2
875 PASS_MEM_STAT);
876
877 gimple_debug_source_bind_set_var (p, var);
878 gimple_debug_source_bind_set_value (p, value);
879 if (stmt)
5368224f 880 gimple_set_location (p, gimple_location (stmt));
ddb555ed
JJ
881
882 return p;
883}
884
885
726a989a
RB
886/* Build a GIMPLE_OMP_CRITICAL statement.
887
888 BODY is the sequence of statements for which only one thread can execute.
889 NAME is optional identifier for this critical block. */
890
b8698a0f 891gimple
726a989a
RB
892gimple_build_omp_critical (gimple_seq body, tree name)
893{
894 gimple p = gimple_alloc (GIMPLE_OMP_CRITICAL, 0);
895 gimple_omp_critical_set_name (p, name);
896 if (body)
897 gimple_omp_set_body (p, body);
898
899 return p;
900}
901
902/* Build a GIMPLE_OMP_FOR statement.
903
904 BODY is sequence of statements inside the for loop.
74bf76ed 905 KIND is the `for' variant.
b8698a0f 906 CLAUSES, are any of the OMP loop construct's clauses: private, firstprivate,
726a989a
RB
907 lastprivate, reductions, ordered, schedule, and nowait.
908 COLLAPSE is the collapse count.
909 PRE_BODY is the sequence of statements that are loop invariant. */
910
911gimple
74bf76ed 912gimple_build_omp_for (gimple_seq body, int kind, tree clauses, size_t collapse,
726a989a
RB
913 gimple_seq pre_body)
914{
915 gimple p = gimple_alloc (GIMPLE_OMP_FOR, 0);
916 if (body)
917 gimple_omp_set_body (p, body);
918 gimple_omp_for_set_clauses (p, clauses);
74bf76ed 919 gimple_omp_for_set_kind (p, kind);
726a989a 920 p->gimple_omp_for.collapse = collapse;
a9429e29
LB
921 p->gimple_omp_for.iter
922 = ggc_alloc_cleared_vec_gimple_omp_for_iter (collapse);
726a989a
RB
923 if (pre_body)
924 gimple_omp_for_set_pre_body (p, pre_body);
925
926 return p;
927}
928
929
930/* Build a GIMPLE_OMP_PARALLEL statement.
931
932 BODY is sequence of statements which are executed in parallel.
933 CLAUSES, are the OMP parallel construct's clauses.
934 CHILD_FN is the function created for the parallel threads to execute.
935 DATA_ARG are the shared data argument(s). */
936
b8698a0f
L
937gimple
938gimple_build_omp_parallel (gimple_seq body, tree clauses, tree child_fn,
726a989a
RB
939 tree data_arg)
940{
941 gimple p = gimple_alloc (GIMPLE_OMP_PARALLEL, 0);
942 if (body)
943 gimple_omp_set_body (p, body);
944 gimple_omp_parallel_set_clauses (p, clauses);
945 gimple_omp_parallel_set_child_fn (p, child_fn);
946 gimple_omp_parallel_set_data_arg (p, data_arg);
947
948 return p;
949}
950
951
952/* Build a GIMPLE_OMP_TASK statement.
953
954 BODY is sequence of statements which are executed by the explicit task.
955 CLAUSES, are the OMP parallel construct's clauses.
956 CHILD_FN is the function created for the parallel threads to execute.
957 DATA_ARG are the shared data argument(s).
958 COPY_FN is the optional function for firstprivate initialization.
959 ARG_SIZE and ARG_ALIGN are size and alignment of the data block. */
960
b8698a0f 961gimple
726a989a
RB
962gimple_build_omp_task (gimple_seq body, tree clauses, tree child_fn,
963 tree data_arg, tree copy_fn, tree arg_size,
964 tree arg_align)
965{
966 gimple p = gimple_alloc (GIMPLE_OMP_TASK, 0);
967 if (body)
968 gimple_omp_set_body (p, body);
969 gimple_omp_task_set_clauses (p, clauses);
970 gimple_omp_task_set_child_fn (p, child_fn);
971 gimple_omp_task_set_data_arg (p, data_arg);
972 gimple_omp_task_set_copy_fn (p, copy_fn);
973 gimple_omp_task_set_arg_size (p, arg_size);
974 gimple_omp_task_set_arg_align (p, arg_align);
975
976 return p;
977}
978
979
980/* Build a GIMPLE_OMP_SECTION statement for a sections statement.
981
982 BODY is the sequence of statements in the section. */
983
984gimple
985gimple_build_omp_section (gimple_seq body)
986{
987 gimple p = gimple_alloc (GIMPLE_OMP_SECTION, 0);
988 if (body)
989 gimple_omp_set_body (p, body);
990
991 return p;
992}
993
994
995/* Build a GIMPLE_OMP_MASTER statement.
996
997 BODY is the sequence of statements to be executed by just the master. */
998
b8698a0f 999gimple
726a989a
RB
1000gimple_build_omp_master (gimple_seq body)
1001{
1002 gimple p = gimple_alloc (GIMPLE_OMP_MASTER, 0);
1003 if (body)
1004 gimple_omp_set_body (p, body);
1005
1006 return p;
1007}
1008
1009
1010/* Build a GIMPLE_OMP_CONTINUE statement.
1011
1012 CONTROL_DEF is the definition of the control variable.
1013 CONTROL_USE is the use of the control variable. */
1014
b8698a0f 1015gimple
726a989a
RB
1016gimple_build_omp_continue (tree control_def, tree control_use)
1017{
1018 gimple p = gimple_alloc (GIMPLE_OMP_CONTINUE, 0);
1019 gimple_omp_continue_set_control_def (p, control_def);
1020 gimple_omp_continue_set_control_use (p, control_use);
1021 return p;
1022}
1023
1024/* Build a GIMPLE_OMP_ORDERED statement.
1025
1026 BODY is the sequence of statements inside a loop that will executed in
1027 sequence. */
1028
b8698a0f 1029gimple
726a989a
RB
1030gimple_build_omp_ordered (gimple_seq body)
1031{
1032 gimple p = gimple_alloc (GIMPLE_OMP_ORDERED, 0);
1033 if (body)
1034 gimple_omp_set_body (p, body);
1035
1036 return p;
1037}
1038
1039
1040/* Build a GIMPLE_OMP_RETURN statement.
1041 WAIT_P is true if this is a non-waiting return. */
1042
b8698a0f 1043gimple
726a989a
RB
1044gimple_build_omp_return (bool wait_p)
1045{
1046 gimple p = gimple_alloc (GIMPLE_OMP_RETURN, 0);
1047 if (wait_p)
1048 gimple_omp_return_set_nowait (p);
1049
1050 return p;
1051}
1052
1053
1054/* Build a GIMPLE_OMP_SECTIONS statement.
1055
1056 BODY is a sequence of section statements.
1057 CLAUSES are any of the OMP sections contsruct's clauses: private,
1058 firstprivate, lastprivate, reduction, and nowait. */
1059
b8698a0f 1060gimple
726a989a
RB
1061gimple_build_omp_sections (gimple_seq body, tree clauses)
1062{
1063 gimple p = gimple_alloc (GIMPLE_OMP_SECTIONS, 0);
1064 if (body)
1065 gimple_omp_set_body (p, body);
1066 gimple_omp_sections_set_clauses (p, clauses);
1067
1068 return p;
1069}
1070
1071
1072/* Build a GIMPLE_OMP_SECTIONS_SWITCH. */
1073
1074gimple
1075gimple_build_omp_sections_switch (void)
1076{
1077 return gimple_alloc (GIMPLE_OMP_SECTIONS_SWITCH, 0);
1078}
1079
1080
1081/* Build a GIMPLE_OMP_SINGLE statement.
1082
1083 BODY is the sequence of statements that will be executed once.
1084 CLAUSES are any of the OMP single construct's clauses: private, firstprivate,
1085 copyprivate, nowait. */
1086
b8698a0f 1087gimple
726a989a
RB
1088gimple_build_omp_single (gimple_seq body, tree clauses)
1089{
1090 gimple p = gimple_alloc (GIMPLE_OMP_SINGLE, 0);
1091 if (body)
1092 gimple_omp_set_body (p, body);
1093 gimple_omp_single_set_clauses (p, clauses);
1094
1095 return p;
1096}
1097
1098
726a989a
RB
1099/* Build a GIMPLE_OMP_ATOMIC_LOAD statement. */
1100
1101gimple
1102gimple_build_omp_atomic_load (tree lhs, tree rhs)
1103{
1104 gimple p = gimple_alloc (GIMPLE_OMP_ATOMIC_LOAD, 0);
1105 gimple_omp_atomic_load_set_lhs (p, lhs);
1106 gimple_omp_atomic_load_set_rhs (p, rhs);
1107 return p;
1108}
1109
1110/* Build a GIMPLE_OMP_ATOMIC_STORE statement.
1111
1112 VAL is the value we are storing. */
1113
1114gimple
1115gimple_build_omp_atomic_store (tree val)
1116{
1117 gimple p = gimple_alloc (GIMPLE_OMP_ATOMIC_STORE, 0);
1118 gimple_omp_atomic_store_set_val (p, val);
1119 return p;
1120}
1121
0a35513e
AH
1122/* Build a GIMPLE_TRANSACTION statement. */
1123
1124gimple
1125gimple_build_transaction (gimple_seq body, tree label)
1126{
1127 gimple p = gimple_alloc (GIMPLE_TRANSACTION, 0);
1128 gimple_transaction_set_body (p, body);
1129 gimple_transaction_set_label (p, label);
1130 return p;
1131}
1132
726a989a
RB
1133/* Build a GIMPLE_PREDICT statement. PREDICT is one of the predictors from
1134 predict.def, OUTCOME is NOT_TAKEN or TAKEN. */
1135
1136gimple
1137gimple_build_predict (enum br_predictor predictor, enum prediction outcome)
1138{
1139 gimple p = gimple_alloc (GIMPLE_PREDICT, 0);
1140 /* Ensure all the predictors fit into the lower bits of the subcode. */
e0c68ce9 1141 gcc_assert ((int) END_PREDICTORS <= GF_PREDICT_TAKEN);
726a989a
RB
1142 gimple_predict_set_predictor (p, predictor);
1143 gimple_predict_set_outcome (p, outcome);
1144 return p;
1145}
1146
cea094ed 1147#if defined ENABLE_GIMPLE_CHECKING
726a989a
RB
1148/* Complain of a gimple type mismatch and die. */
1149
1150void
1151gimple_check_failed (const_gimple gs, const char *file, int line,
1152 const char *function, enum gimple_code code,
1153 enum tree_code subcode)
1154{
1155 internal_error ("gimple check: expected %s(%s), have %s(%s) in %s, at %s:%d",
1156 gimple_code_name[code],
1157 tree_code_name[subcode],
1158 gimple_code_name[gimple_code (gs)],
1159 gs->gsbase.subcode > 0
1160 ? tree_code_name[gs->gsbase.subcode]
1161 : "",
1162 function, trim_filename (file), line);
1163}
726a989a
RB
1164#endif /* ENABLE_GIMPLE_CHECKING */
1165
1166
726a989a
RB
1167/* Link gimple statement GS to the end of the sequence *SEQ_P. If
1168 *SEQ_P is NULL, a new sequence is allocated. */
1169
1170void
1171gimple_seq_add_stmt (gimple_seq *seq_p, gimple gs)
1172{
1173 gimple_stmt_iterator si;
726a989a
RB
1174 if (gs == NULL)
1175 return;
1176
726a989a
RB
1177 si = gsi_last (*seq_p);
1178 gsi_insert_after (&si, gs, GSI_NEW_STMT);
1179}
1180
1181
1182/* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
1183 NULL, a new sequence is allocated. */
1184
1185void
1186gimple_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
1187{
1188 gimple_stmt_iterator si;
726a989a
RB
1189 if (src == NULL)
1190 return;
1191
726a989a
RB
1192 si = gsi_last (*dst_p);
1193 gsi_insert_seq_after (&si, src, GSI_NEW_STMT);
1194}
1195
1196
1197/* Helper function of empty_body_p. Return true if STMT is an empty
1198 statement. */
1199
1200static bool
1201empty_stmt_p (gimple stmt)
1202{
1203 if (gimple_code (stmt) == GIMPLE_NOP)
1204 return true;
1205 if (gimple_code (stmt) == GIMPLE_BIND)
1206 return empty_body_p (gimple_bind_body (stmt));
1207 return false;
1208}
1209
1210
1211/* Return true if BODY contains nothing but empty statements. */
1212
1213bool
1214empty_body_p (gimple_seq body)
1215{
1216 gimple_stmt_iterator i;
1217
726a989a
RB
1218 if (gimple_seq_empty_p (body))
1219 return true;
1220 for (i = gsi_start (body); !gsi_end_p (i); gsi_next (&i))
b5b8b0ac
AO
1221 if (!empty_stmt_p (gsi_stmt (i))
1222 && !is_gimple_debug (gsi_stmt (i)))
726a989a
RB
1223 return false;
1224
1225 return true;
1226}
1227
1228
1229/* Perform a deep copy of sequence SRC and return the result. */
1230
1231gimple_seq
1232gimple_seq_copy (gimple_seq src)
1233{
1234 gimple_stmt_iterator gsi;
355a7673 1235 gimple_seq new_seq = NULL;
726a989a
RB
1236 gimple stmt;
1237
1238 for (gsi = gsi_start (src); !gsi_end_p (gsi); gsi_next (&gsi))
1239 {
1240 stmt = gimple_copy (gsi_stmt (gsi));
82d6e6fc 1241 gimple_seq_add_stmt (&new_seq, stmt);
726a989a
RB
1242 }
1243
82d6e6fc 1244 return new_seq;
726a989a
RB
1245}
1246
1247
355a7673 1248/* Walk all the statements in the sequence *PSEQ calling walk_gimple_stmt
726a989a 1249 on each one. WI is as in walk_gimple_stmt.
b8698a0f 1250
0a35513e
AH
1251 If walk_gimple_stmt returns non-NULL, the walk is stopped, and the
1252 value is stored in WI->CALLBACK_RESULT. Also, the statement that
1253 produced the value is returned if this statement has not been
1254 removed by a callback (wi->removed_stmt). If the statement has
1255 been removed, NULL is returned.
726a989a
RB
1256
1257 Otherwise, all the statements are walked and NULL returned. */
1258
1259gimple
355a7673
MM
1260walk_gimple_seq_mod (gimple_seq *pseq, walk_stmt_fn callback_stmt,
1261 walk_tree_fn callback_op, struct walk_stmt_info *wi)
726a989a
RB
1262{
1263 gimple_stmt_iterator gsi;
1264
355a7673 1265 for (gsi = gsi_start (*pseq); !gsi_end_p (gsi); )
726a989a
RB
1266 {
1267 tree ret = walk_gimple_stmt (&gsi, callback_stmt, callback_op, wi);
1268 if (ret)
1269 {
1270 /* If CALLBACK_STMT or CALLBACK_OP return a value, WI must exist
1271 to hold it. */
1272 gcc_assert (wi);
1273 wi->callback_result = ret;
0a35513e
AH
1274
1275 return wi->removed_stmt ? NULL : gsi_stmt (gsi);
726a989a 1276 }
0a35513e
AH
1277
1278 if (!wi->removed_stmt)
1279 gsi_next (&gsi);
726a989a
RB
1280 }
1281
1282 if (wi)
1283 wi->callback_result = NULL_TREE;
1284
1285 return NULL;
1286}
1287
1288
355a7673
MM
1289/* Like walk_gimple_seq_mod, but ensure that the head of SEQ isn't
1290 changed by the callbacks. */
1291
1292gimple
1293walk_gimple_seq (gimple_seq seq, walk_stmt_fn callback_stmt,
1294 walk_tree_fn callback_op, struct walk_stmt_info *wi)
1295{
1296 gimple_seq seq2 = seq;
1297 gimple ret = walk_gimple_seq_mod (&seq2, callback_stmt, callback_op, wi);
1298 gcc_assert (seq2 == seq);
1299 return ret;
1300}
1301
1302
726a989a
RB
1303/* Helper function for walk_gimple_stmt. Walk operands of a GIMPLE_ASM. */
1304
1305static tree
1306walk_gimple_asm (gimple stmt, walk_tree_fn callback_op,
1307 struct walk_stmt_info *wi)
1308{
1c384bf1 1309 tree ret, op;
726a989a
RB
1310 unsigned noutputs;
1311 const char **oconstraints;
1c384bf1 1312 unsigned i, n;
726a989a
RB
1313 const char *constraint;
1314 bool allows_mem, allows_reg, is_inout;
1315
1316 noutputs = gimple_asm_noutputs (stmt);
1317 oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
1318
1319 if (wi)
1320 wi->is_lhs = true;
1321
1322 for (i = 0; i < noutputs; i++)
1323 {
1c384bf1 1324 op = gimple_asm_output_op (stmt, i);
726a989a
RB
1325 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
1326 oconstraints[i] = constraint;
1327 parse_output_constraint (&constraint, i, 0, 0, &allows_mem, &allows_reg,
1328 &is_inout);
1329 if (wi)
1330 wi->val_only = (allows_reg || !allows_mem);
1331 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1332 if (ret)
1333 return ret;
1334 }
1335
1c384bf1
RH
1336 n = gimple_asm_ninputs (stmt);
1337 for (i = 0; i < n; i++)
726a989a 1338 {
1c384bf1 1339 op = gimple_asm_input_op (stmt, i);
726a989a
RB
1340 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
1341 parse_input_constraint (&constraint, 0, 0, noutputs, 0,
1342 oconstraints, &allows_mem, &allows_reg);
1343 if (wi)
1c384bf1
RH
1344 {
1345 wi->val_only = (allows_reg || !allows_mem);
1346 /* Although input "m" is not really a LHS, we need a lvalue. */
1347 wi->is_lhs = !wi->val_only;
1348 }
726a989a
RB
1349 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1350 if (ret)
1351 return ret;
1352 }
1353
1354 if (wi)
1355 {
1356 wi->is_lhs = false;
1357 wi->val_only = true;
1358 }
1359
1c384bf1
RH
1360 n = gimple_asm_nlabels (stmt);
1361 for (i = 0; i < n; i++)
1362 {
1363 op = gimple_asm_label_op (stmt, i);
1364 ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
1365 if (ret)
1366 return ret;
1367 }
1368
726a989a
RB
1369 return NULL_TREE;
1370}
1371
1372
1373/* Helper function of WALK_GIMPLE_STMT. Walk every tree operand in
1374 STMT. CALLBACK_OP and WI are as in WALK_GIMPLE_STMT.
1375
1376 CALLBACK_OP is called on each operand of STMT via walk_tree.
1377 Additional parameters to walk_tree must be stored in WI. For each operand
1378 OP, walk_tree is called as:
1379
1380 walk_tree (&OP, CALLBACK_OP, WI, WI->PSET)
1381
1382 If CALLBACK_OP returns non-NULL for an operand, the remaining
1383 operands are not scanned.
1384
1385 The return value is that returned by the last call to walk_tree, or
1386 NULL_TREE if no CALLBACK_OP is specified. */
1387
6a4d4e8a 1388tree
726a989a
RB
1389walk_gimple_op (gimple stmt, walk_tree_fn callback_op,
1390 struct walk_stmt_info *wi)
1391{
1392 struct pointer_set_t *pset = (wi) ? wi->pset : NULL;
1393 unsigned i;
1394 tree ret = NULL_TREE;
1395
1396 switch (gimple_code (stmt))
1397 {
1398 case GIMPLE_ASSIGN:
cb3d597d
EB
1399 /* Walk the RHS operands. If the LHS is of a non-renamable type or
1400 is a register variable, we may use a COMPONENT_REF on the RHS. */
726a989a 1401 if (wi)
cb3d597d
EB
1402 {
1403 tree lhs = gimple_assign_lhs (stmt);
1404 wi->val_only
1405 = (is_gimple_reg_type (TREE_TYPE (lhs)) && !is_gimple_reg (lhs))
b9af73fc 1406 || gimple_assign_rhs_class (stmt) != GIMPLE_SINGLE_RHS;
cb3d597d 1407 }
726a989a
RB
1408
1409 for (i = 1; i < gimple_num_ops (stmt); i++)
1410 {
1411 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi,
1412 pset);
1413 if (ret)
1414 return ret;
1415 }
1416
1417 /* Walk the LHS. If the RHS is appropriate for a memory, we
1418 may use a COMPONENT_REF on the LHS. */
1419 if (wi)
1420 {
216820a4
RG
1421 /* If the RHS is of a non-renamable type or is a register variable,
1422 we may use a COMPONENT_REF on the LHS. */
b9af73fc 1423 tree rhs1 = gimple_assign_rhs1 (stmt);
216820a4
RG
1424 wi->val_only
1425 = (is_gimple_reg_type (TREE_TYPE (rhs1)) && !is_gimple_reg (rhs1))
1426 || gimple_assign_rhs_class (stmt) != GIMPLE_SINGLE_RHS;
726a989a
RB
1427 wi->is_lhs = true;
1428 }
1429
1430 ret = walk_tree (gimple_op_ptr (stmt, 0), callback_op, wi, pset);
1431 if (ret)
1432 return ret;
1433
1434 if (wi)
1435 {
1436 wi->val_only = true;
1437 wi->is_lhs = false;
1438 }
1439 break;
1440
1441 case GIMPLE_CALL:
1442 if (wi)
523968bf
RG
1443 {
1444 wi->is_lhs = false;
1445 wi->val_only = true;
1446 }
726a989a
RB
1447
1448 ret = walk_tree (gimple_call_chain_ptr (stmt), callback_op, wi, pset);
1449 if (ret)
1450 return ret;
1451
1452 ret = walk_tree (gimple_call_fn_ptr (stmt), callback_op, wi, pset);
1453 if (ret)
1454 return ret;
1455
1456 for (i = 0; i < gimple_call_num_args (stmt); i++)
1457 {
523968bf 1458 if (wi)
4d931f41
EB
1459 wi->val_only
1460 = is_gimple_reg_type (TREE_TYPE (gimple_call_arg (stmt, i)));
726a989a
RB
1461 ret = walk_tree (gimple_call_arg_ptr (stmt, i), callback_op, wi,
1462 pset);
1463 if (ret)
1464 return ret;
1465 }
1466
523968bf
RG
1467 if (gimple_call_lhs (stmt))
1468 {
1469 if (wi)
1470 {
1471 wi->is_lhs = true;
4d931f41
EB
1472 wi->val_only
1473 = is_gimple_reg_type (TREE_TYPE (gimple_call_lhs (stmt)));
523968bf 1474 }
726a989a 1475
523968bf
RG
1476 ret = walk_tree (gimple_call_lhs_ptr (stmt), callback_op, wi, pset);
1477 if (ret)
1478 return ret;
1479 }
726a989a
RB
1480
1481 if (wi)
523968bf
RG
1482 {
1483 wi->is_lhs = false;
1484 wi->val_only = true;
1485 }
726a989a
RB
1486 break;
1487
1488 case GIMPLE_CATCH:
1489 ret = walk_tree (gimple_catch_types_ptr (stmt), callback_op, wi,
1490 pset);
1491 if (ret)
1492 return ret;
1493 break;
1494
1495 case GIMPLE_EH_FILTER:
1496 ret = walk_tree (gimple_eh_filter_types_ptr (stmt), callback_op, wi,
1497 pset);
1498 if (ret)
1499 return ret;
1500 break;
1501
726a989a
RB
1502 case GIMPLE_ASM:
1503 ret = walk_gimple_asm (stmt, callback_op, wi);
1504 if (ret)
1505 return ret;
1506 break;
1507
1508 case GIMPLE_OMP_CONTINUE:
1509 ret = walk_tree (gimple_omp_continue_control_def_ptr (stmt),
1510 callback_op, wi, pset);
1511 if (ret)
1512 return ret;
1513
1514 ret = walk_tree (gimple_omp_continue_control_use_ptr (stmt),
1515 callback_op, wi, pset);
1516 if (ret)
1517 return ret;
1518 break;
1519
1520 case GIMPLE_OMP_CRITICAL:
1521 ret = walk_tree (gimple_omp_critical_name_ptr (stmt), callback_op, wi,
1522 pset);
1523 if (ret)
1524 return ret;
1525 break;
1526
1527 case GIMPLE_OMP_FOR:
1528 ret = walk_tree (gimple_omp_for_clauses_ptr (stmt), callback_op, wi,
1529 pset);
1530 if (ret)
1531 return ret;
1532 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
1533 {
1534 ret = walk_tree (gimple_omp_for_index_ptr (stmt, i), callback_op,
1535 wi, pset);
1536 if (ret)
1537 return ret;
1538 ret = walk_tree (gimple_omp_for_initial_ptr (stmt, i), callback_op,
1539 wi, pset);
1540 if (ret)
1541 return ret;
1542 ret = walk_tree (gimple_omp_for_final_ptr (stmt, i), callback_op,
1543 wi, pset);
1544 if (ret)
1545 return ret;
1546 ret = walk_tree (gimple_omp_for_incr_ptr (stmt, i), callback_op,
1547 wi, pset);
1548 }
1549 if (ret)
1550 return ret;
1551 break;
1552
1553 case GIMPLE_OMP_PARALLEL:
1554 ret = walk_tree (gimple_omp_parallel_clauses_ptr (stmt), callback_op,
1555 wi, pset);
1556 if (ret)
1557 return ret;
1558 ret = walk_tree (gimple_omp_parallel_child_fn_ptr (stmt), callback_op,
1559 wi, pset);
1560 if (ret)
1561 return ret;
1562 ret = walk_tree (gimple_omp_parallel_data_arg_ptr (stmt), callback_op,
1563 wi, pset);
1564 if (ret)
1565 return ret;
1566 break;
1567
1568 case GIMPLE_OMP_TASK:
1569 ret = walk_tree (gimple_omp_task_clauses_ptr (stmt), callback_op,
1570 wi, pset);
1571 if (ret)
1572 return ret;
1573 ret = walk_tree (gimple_omp_task_child_fn_ptr (stmt), callback_op,
1574 wi, pset);
1575 if (ret)
1576 return ret;
1577 ret = walk_tree (gimple_omp_task_data_arg_ptr (stmt), callback_op,
1578 wi, pset);
1579 if (ret)
1580 return ret;
1581 ret = walk_tree (gimple_omp_task_copy_fn_ptr (stmt), callback_op,
1582 wi, pset);
1583 if (ret)
1584 return ret;
1585 ret = walk_tree (gimple_omp_task_arg_size_ptr (stmt), callback_op,
1586 wi, pset);
1587 if (ret)
1588 return ret;
1589 ret = walk_tree (gimple_omp_task_arg_align_ptr (stmt), callback_op,
1590 wi, pset);
1591 if (ret)
1592 return ret;
1593 break;
1594
1595 case GIMPLE_OMP_SECTIONS:
1596 ret = walk_tree (gimple_omp_sections_clauses_ptr (stmt), callback_op,
1597 wi, pset);
1598 if (ret)
1599 return ret;
1600
1601 ret = walk_tree (gimple_omp_sections_control_ptr (stmt), callback_op,
1602 wi, pset);
1603 if (ret)
1604 return ret;
1605
1606 break;
1607
1608 case GIMPLE_OMP_SINGLE:
1609 ret = walk_tree (gimple_omp_single_clauses_ptr (stmt), callback_op, wi,
1610 pset);
1611 if (ret)
1612 return ret;
1613 break;
1614
1615 case GIMPLE_OMP_ATOMIC_LOAD:
1616 ret = walk_tree (gimple_omp_atomic_load_lhs_ptr (stmt), callback_op, wi,
1617 pset);
1618 if (ret)
1619 return ret;
1620
1621 ret = walk_tree (gimple_omp_atomic_load_rhs_ptr (stmt), callback_op, wi,
1622 pset);
1623 if (ret)
1624 return ret;
1625 break;
1626
1627 case GIMPLE_OMP_ATOMIC_STORE:
1628 ret = walk_tree (gimple_omp_atomic_store_val_ptr (stmt), callback_op,
1629 wi, pset);
1630 if (ret)
1631 return ret;
1632 break;
1633
0a35513e
AH
1634 case GIMPLE_TRANSACTION:
1635 ret = walk_tree (gimple_transaction_label_ptr (stmt), callback_op,
1636 wi, pset);
1637 if (ret)
1638 return ret;
1639 break;
1640
726a989a
RB
1641 /* Tuples that do not have operands. */
1642 case GIMPLE_NOP:
1643 case GIMPLE_RESX:
1644 case GIMPLE_OMP_RETURN:
1645 case GIMPLE_PREDICT:
1646 break;
1647
1648 default:
1649 {
1650 enum gimple_statement_structure_enum gss;
1651 gss = gimple_statement_structure (stmt);
1652 if (gss == GSS_WITH_OPS || gss == GSS_WITH_MEM_OPS)
1653 for (i = 0; i < gimple_num_ops (stmt); i++)
1654 {
1655 ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi, pset);
1656 if (ret)
1657 return ret;
1658 }
1659 }
1660 break;
1661 }
1662
1663 return NULL_TREE;
1664}
1665
1666
1667/* Walk the current statement in GSI (optionally using traversal state
1668 stored in WI). If WI is NULL, no state is kept during traversal.
1669 The callback CALLBACK_STMT is called. If CALLBACK_STMT indicates
1670 that it has handled all the operands of the statement, its return
1671 value is returned. Otherwise, the return value from CALLBACK_STMT
1672 is discarded and its operands are scanned.
1673
1674 If CALLBACK_STMT is NULL or it didn't handle the operands,
1675 CALLBACK_OP is called on each operand of the statement via
1676 walk_gimple_op. If walk_gimple_op returns non-NULL for any
1677 operand, the remaining operands are not scanned. In this case, the
1678 return value from CALLBACK_OP is returned.
1679
1680 In any other case, NULL_TREE is returned. */
1681
1682tree
1683walk_gimple_stmt (gimple_stmt_iterator *gsi, walk_stmt_fn callback_stmt,
1684 walk_tree_fn callback_op, struct walk_stmt_info *wi)
1685{
1686 gimple ret;
1687 tree tree_ret;
1688 gimple stmt = gsi_stmt (*gsi);
1689
1690 if (wi)
0a35513e
AH
1691 {
1692 wi->gsi = *gsi;
1693 wi->removed_stmt = false;
726a989a 1694
0a35513e
AH
1695 if (wi->want_locations && gimple_has_location (stmt))
1696 input_location = gimple_location (stmt);
1697 }
726a989a
RB
1698
1699 ret = NULL;
1700
1701 /* Invoke the statement callback. Return if the callback handled
1702 all of STMT operands by itself. */
1703 if (callback_stmt)
1704 {
1705 bool handled_ops = false;
1706 tree_ret = callback_stmt (gsi, &handled_ops, wi);
1707 if (handled_ops)
1708 return tree_ret;
1709
1710 /* If CALLBACK_STMT did not handle operands, it should not have
1711 a value to return. */
1712 gcc_assert (tree_ret == NULL);
1713
0a35513e
AH
1714 if (wi && wi->removed_stmt)
1715 return NULL;
1716
726a989a
RB
1717 /* Re-read stmt in case the callback changed it. */
1718 stmt = gsi_stmt (*gsi);
1719 }
1720
1721 /* If CALLBACK_OP is defined, invoke it on every operand of STMT. */
1722 if (callback_op)
1723 {
1724 tree_ret = walk_gimple_op (stmt, callback_op, wi);
1725 if (tree_ret)
1726 return tree_ret;
1727 }
1728
1729 /* If STMT can have statements inside (e.g. GIMPLE_BIND), walk them. */
1730 switch (gimple_code (stmt))
1731 {
1732 case GIMPLE_BIND:
355a7673
MM
1733 ret = walk_gimple_seq_mod (gimple_bind_body_ptr (stmt), callback_stmt,
1734 callback_op, wi);
726a989a
RB
1735 if (ret)
1736 return wi->callback_result;
1737 break;
1738
1739 case GIMPLE_CATCH:
355a7673
MM
1740 ret = walk_gimple_seq_mod (gimple_catch_handler_ptr (stmt), callback_stmt,
1741 callback_op, wi);
726a989a
RB
1742 if (ret)
1743 return wi->callback_result;
1744 break;
1745
1746 case GIMPLE_EH_FILTER:
355a7673 1747 ret = walk_gimple_seq_mod (gimple_eh_filter_failure_ptr (stmt), callback_stmt,
726a989a
RB
1748 callback_op, wi);
1749 if (ret)
1750 return wi->callback_result;
1751 break;
1752
0a35513e 1753 case GIMPLE_EH_ELSE:
355a7673 1754 ret = walk_gimple_seq_mod (gimple_eh_else_n_body_ptr (stmt),
0a35513e
AH
1755 callback_stmt, callback_op, wi);
1756 if (ret)
1757 return wi->callback_result;
355a7673 1758 ret = walk_gimple_seq_mod (gimple_eh_else_e_body_ptr (stmt),
0a35513e
AH
1759 callback_stmt, callback_op, wi);
1760 if (ret)
1761 return wi->callback_result;
1762 break;
1763
726a989a 1764 case GIMPLE_TRY:
355a7673 1765 ret = walk_gimple_seq_mod (gimple_try_eval_ptr (stmt), callback_stmt, callback_op,
726a989a
RB
1766 wi);
1767 if (ret)
1768 return wi->callback_result;
1769
355a7673 1770 ret = walk_gimple_seq_mod (gimple_try_cleanup_ptr (stmt), callback_stmt,
726a989a
RB
1771 callback_op, wi);
1772 if (ret)
1773 return wi->callback_result;
1774 break;
1775
1776 case GIMPLE_OMP_FOR:
355a7673 1777 ret = walk_gimple_seq_mod (gimple_omp_for_pre_body_ptr (stmt), callback_stmt,
726a989a
RB
1778 callback_op, wi);
1779 if (ret)
1780 return wi->callback_result;
1781
1782 /* FALL THROUGH. */
1783 case GIMPLE_OMP_CRITICAL:
1784 case GIMPLE_OMP_MASTER:
1785 case GIMPLE_OMP_ORDERED:
1786 case GIMPLE_OMP_SECTION:
1787 case GIMPLE_OMP_PARALLEL:
1788 case GIMPLE_OMP_TASK:
1789 case GIMPLE_OMP_SECTIONS:
1790 case GIMPLE_OMP_SINGLE:
355a7673 1791 ret = walk_gimple_seq_mod (gimple_omp_body_ptr (stmt), callback_stmt,
0a35513e 1792 callback_op, wi);
726a989a
RB
1793 if (ret)
1794 return wi->callback_result;
1795 break;
1796
1797 case GIMPLE_WITH_CLEANUP_EXPR:
355a7673 1798 ret = walk_gimple_seq_mod (gimple_wce_cleanup_ptr (stmt), callback_stmt,
726a989a
RB
1799 callback_op, wi);
1800 if (ret)
1801 return wi->callback_result;
1802 break;
1803
0a35513e 1804 case GIMPLE_TRANSACTION:
355a7673 1805 ret = walk_gimple_seq_mod (gimple_transaction_body_ptr (stmt),
0a35513e
AH
1806 callback_stmt, callback_op, wi);
1807 if (ret)
1808 return wi->callback_result;
1809 break;
1810
726a989a
RB
1811 default:
1812 gcc_assert (!gimple_has_substatements (stmt));
1813 break;
1814 }
1815
1816 return NULL;
1817}
1818
1819
1820/* Set sequence SEQ to be the GIMPLE body for function FN. */
1821
1822void
1823gimple_set_body (tree fndecl, gimple_seq seq)
1824{
1825 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1826 if (fn == NULL)
1827 {
1828 /* If FNDECL still does not have a function structure associated
1829 with it, then it does not make sense for it to receive a
1830 GIMPLE body. */
1831 gcc_assert (seq == NULL);
1832 }
1833 else
1834 fn->gimple_body = seq;
1835}
1836
1837
abbd64b9
JS
1838/* Return the body of GIMPLE statements for function FN. After the
1839 CFG pass, the function body doesn't exist anymore because it has
1840 been split up into basic blocks. In this case, it returns
1841 NULL. */
726a989a
RB
1842
1843gimple_seq
1844gimple_body (tree fndecl)
1845{
1846 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1847 return fn ? fn->gimple_body : NULL;
1848}
1849
39ecc018
JH
1850/* Return true when FNDECL has Gimple body either in unlowered
1851 or CFG form. */
1852bool
1853gimple_has_body_p (tree fndecl)
1854{
1855 struct function *fn = DECL_STRUCT_FUNCTION (fndecl);
1856 return (gimple_body (fndecl) || (fn && fn->cfg));
1857}
726a989a 1858
25583c4f
RS
1859/* Return true if calls C1 and C2 are known to go to the same function. */
1860
1861bool
1862gimple_call_same_target_p (const_gimple c1, const_gimple c2)
1863{
1864 if (gimple_call_internal_p (c1))
1865 return (gimple_call_internal_p (c2)
1866 && gimple_call_internal_fn (c1) == gimple_call_internal_fn (c2));
1867 else
1868 return (gimple_call_fn (c1) == gimple_call_fn (c2)
1869 || (gimple_call_fndecl (c1)
1870 && gimple_call_fndecl (c1) == gimple_call_fndecl (c2)));
1871}
1872
726a989a
RB
1873/* Detect flags from a GIMPLE_CALL. This is just like
1874 call_expr_flags, but for gimple tuples. */
1875
1876int
1877gimple_call_flags (const_gimple stmt)
1878{
1879 int flags;
1880 tree decl = gimple_call_fndecl (stmt);
726a989a
RB
1881
1882 if (decl)
1883 flags = flags_from_decl_or_type (decl);
25583c4f
RS
1884 else if (gimple_call_internal_p (stmt))
1885 flags = internal_fn_flags (gimple_call_internal_fn (stmt));
726a989a 1886 else
97e03fa1 1887 flags = flags_from_decl_or_type (gimple_call_fntype (stmt));
726a989a 1888
9bb1a81b
JM
1889 if (stmt->gsbase.subcode & GF_CALL_NOTHROW)
1890 flags |= ECF_NOTHROW;
1891
726a989a
RB
1892 return flags;
1893}
1894
25583c4f
RS
1895/* Return the "fn spec" string for call STMT. */
1896
1897static tree
1898gimple_call_fnspec (const_gimple stmt)
1899{
1900 tree type, attr;
1901
1902 type = gimple_call_fntype (stmt);
1903 if (!type)
1904 return NULL_TREE;
1905
1906 attr = lookup_attribute ("fn spec", TYPE_ATTRIBUTES (type));
1907 if (!attr)
1908 return NULL_TREE;
1909
1910 return TREE_VALUE (TREE_VALUE (attr));
1911}
1912
0b7b376d
RG
1913/* Detects argument flags for argument number ARG on call STMT. */
1914
1915int
1916gimple_call_arg_flags (const_gimple stmt, unsigned arg)
1917{
25583c4f 1918 tree attr = gimple_call_fnspec (stmt);
0b7b376d 1919
25583c4f 1920 if (!attr || 1 + arg >= (unsigned) TREE_STRING_LENGTH (attr))
0b7b376d
RG
1921 return 0;
1922
1923 switch (TREE_STRING_POINTER (attr)[1 + arg])
1924 {
1925 case 'x':
1926 case 'X':
1927 return EAF_UNUSED;
1928
1929 case 'R':
1930 return EAF_DIRECT | EAF_NOCLOBBER | EAF_NOESCAPE;
1931
1932 case 'r':
1933 return EAF_NOCLOBBER | EAF_NOESCAPE;
1934
1935 case 'W':
1936 return EAF_DIRECT | EAF_NOESCAPE;
1937
1938 case 'w':
1939 return EAF_NOESCAPE;
1940
1941 case '.':
1942 default:
1943 return 0;
1944 }
1945}
1946
1947/* Detects return flags for the call STMT. */
1948
1949int
1950gimple_call_return_flags (const_gimple stmt)
1951{
25583c4f 1952 tree attr;
0b7b376d
RG
1953
1954 if (gimple_call_flags (stmt) & ECF_MALLOC)
1955 return ERF_NOALIAS;
1956
25583c4f
RS
1957 attr = gimple_call_fnspec (stmt);
1958 if (!attr || TREE_STRING_LENGTH (attr) < 1)
0b7b376d
RG
1959 return 0;
1960
1961 switch (TREE_STRING_POINTER (attr)[0])
1962 {
1963 case '1':
1964 case '2':
1965 case '3':
1966 case '4':
1967 return ERF_RETURNS_ARG | (TREE_STRING_POINTER (attr)[0] - '1');
1968
1969 case 'm':
1970 return ERF_NOALIAS;
1971
1972 case '.':
1973 default:
1974 return 0;
1975 }
1976}
726a989a 1977
3dbe9454 1978
726a989a
RB
1979/* Return true if GS is a copy assignment. */
1980
1981bool
1982gimple_assign_copy_p (gimple gs)
1983{
3dbe9454
RG
1984 return (gimple_assign_single_p (gs)
1985 && is_gimple_val (gimple_op (gs, 1)));
726a989a
RB
1986}
1987
1988
1989/* Return true if GS is a SSA_NAME copy assignment. */
1990
1991bool
1992gimple_assign_ssa_name_copy_p (gimple gs)
1993{
3dbe9454 1994 return (gimple_assign_single_p (gs)
726a989a
RB
1995 && TREE_CODE (gimple_assign_lhs (gs)) == SSA_NAME
1996 && TREE_CODE (gimple_assign_rhs1 (gs)) == SSA_NAME);
1997}
1998
1999
726a989a
RB
2000/* Return true if GS is an assignment with a unary RHS, but the
2001 operator has no effect on the assigned value. The logic is adapted
2002 from STRIP_NOPS. This predicate is intended to be used in tuplifying
2003 instances in which STRIP_NOPS was previously applied to the RHS of
2004 an assignment.
2005
2006 NOTE: In the use cases that led to the creation of this function
2007 and of gimple_assign_single_p, it is typical to test for either
2008 condition and to proceed in the same manner. In each case, the
2009 assigned value is represented by the single RHS operand of the
2010 assignment. I suspect there may be cases where gimple_assign_copy_p,
2011 gimple_assign_single_p, or equivalent logic is used where a similar
2012 treatment of unary NOPs is appropriate. */
b8698a0f 2013
726a989a
RB
2014bool
2015gimple_assign_unary_nop_p (gimple gs)
2016{
3dbe9454 2017 return (is_gimple_assign (gs)
1a87cf0c 2018 && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (gs))
726a989a
RB
2019 || gimple_assign_rhs_code (gs) == NON_LVALUE_EXPR)
2020 && gimple_assign_rhs1 (gs) != error_mark_node
2021 && (TYPE_MODE (TREE_TYPE (gimple_assign_lhs (gs)))
2022 == TYPE_MODE (TREE_TYPE (gimple_assign_rhs1 (gs)))));
2023}
2024
2025/* Set BB to be the basic block holding G. */
2026
2027void
2028gimple_set_bb (gimple stmt, basic_block bb)
2029{
2030 stmt->gsbase.bb = bb;
2031
2032 /* If the statement is a label, add the label to block-to-labels map
2033 so that we can speed up edge creation for GIMPLE_GOTOs. */
2034 if (cfun->cfg && gimple_code (stmt) == GIMPLE_LABEL)
2035 {
2036 tree t;
2037 int uid;
2038
2039 t = gimple_label_label (stmt);
2040 uid = LABEL_DECL_UID (t);
2041 if (uid == -1)
2042 {
9771b263 2043 unsigned old_len = vec_safe_length (label_to_block_map);
726a989a
RB
2044 LABEL_DECL_UID (t) = uid = cfun->cfg->last_label_uid++;
2045 if (old_len <= (unsigned) uid)
2046 {
5006671f 2047 unsigned new_len = 3 * uid / 2 + 1;
726a989a 2048
9771b263 2049 vec_safe_grow_cleared (label_to_block_map, new_len);
726a989a
RB
2050 }
2051 }
2052
9771b263 2053 (*label_to_block_map)[uid] = bb;
726a989a
RB
2054 }
2055}
2056
2057
726a989a
RB
2058/* Modify the RHS of the assignment pointed-to by GSI using the
2059 operands in the expression tree EXPR.
2060
2061 NOTE: The statement pointed-to by GSI may be reallocated if it
2062 did not have enough operand slots.
2063
2064 This function is useful to convert an existing tree expression into
2065 the flat representation used for the RHS of a GIMPLE assignment.
2066 It will reallocate memory as needed to expand or shrink the number
2067 of operand slots needed to represent EXPR.
2068
2069 NOTE: If you find yourself building a tree and then calling this
2070 function, you are most certainly doing it the slow way. It is much
2071 better to build a new assignment or to use the function
2072 gimple_assign_set_rhs_with_ops, which does not require an
2073 expression tree to be built. */
2074
2075void
2076gimple_assign_set_rhs_from_tree (gimple_stmt_iterator *gsi, tree expr)
2077{
2078 enum tree_code subcode;
0354c0c7 2079 tree op1, op2, op3;
726a989a 2080
0354c0c7
BS
2081 extract_ops_from_tree_1 (expr, &subcode, &op1, &op2, &op3);
2082 gimple_assign_set_rhs_with_ops_1 (gsi, subcode, op1, op2, op3);
726a989a
RB
2083}
2084
2085
2086/* Set the RHS of assignment statement pointed-to by GSI to CODE with
0354c0c7 2087 operands OP1, OP2 and OP3.
726a989a
RB
2088
2089 NOTE: The statement pointed-to by GSI may be reallocated if it
2090 did not have enough operand slots. */
2091
2092void
0354c0c7
BS
2093gimple_assign_set_rhs_with_ops_1 (gimple_stmt_iterator *gsi, enum tree_code code,
2094 tree op1, tree op2, tree op3)
726a989a
RB
2095{
2096 unsigned new_rhs_ops = get_gimple_rhs_num_ops (code);
2097 gimple stmt = gsi_stmt (*gsi);
2098
2099 /* If the new CODE needs more operands, allocate a new statement. */
2100 if (gimple_num_ops (stmt) < new_rhs_ops + 1)
2101 {
2102 tree lhs = gimple_assign_lhs (stmt);
2103 gimple new_stmt = gimple_alloc (gimple_code (stmt), new_rhs_ops + 1);
2104 memcpy (new_stmt, stmt, gimple_size (gimple_code (stmt)));
355a7673 2105 gimple_init_singleton (new_stmt);
726a989a
RB
2106 gsi_replace (gsi, new_stmt, true);
2107 stmt = new_stmt;
2108
2109 /* The LHS needs to be reset as this also changes the SSA name
2110 on the LHS. */
2111 gimple_assign_set_lhs (stmt, lhs);
2112 }
2113
2114 gimple_set_num_ops (stmt, new_rhs_ops + 1);
2115 gimple_set_subcode (stmt, code);
2116 gimple_assign_set_rhs1 (stmt, op1);
2117 if (new_rhs_ops > 1)
2118 gimple_assign_set_rhs2 (stmt, op2);
0354c0c7
BS
2119 if (new_rhs_ops > 2)
2120 gimple_assign_set_rhs3 (stmt, op3);
726a989a
RB
2121}
2122
2123
2124/* Return the LHS of a statement that performs an assignment,
2125 either a GIMPLE_ASSIGN or a GIMPLE_CALL. Returns NULL_TREE
2126 for a call to a function that returns no value, or for a
2127 statement other than an assignment or a call. */
2128
2129tree
2130gimple_get_lhs (const_gimple stmt)
2131{
e0c68ce9 2132 enum gimple_code code = gimple_code (stmt);
726a989a
RB
2133
2134 if (code == GIMPLE_ASSIGN)
2135 return gimple_assign_lhs (stmt);
2136 else if (code == GIMPLE_CALL)
2137 return gimple_call_lhs (stmt);
2138 else
2139 return NULL_TREE;
2140}
2141
2142
2143/* Set the LHS of a statement that performs an assignment,
2144 either a GIMPLE_ASSIGN or a GIMPLE_CALL. */
2145
2146void
2147gimple_set_lhs (gimple stmt, tree lhs)
2148{
e0c68ce9 2149 enum gimple_code code = gimple_code (stmt);
726a989a
RB
2150
2151 if (code == GIMPLE_ASSIGN)
2152 gimple_assign_set_lhs (stmt, lhs);
2153 else if (code == GIMPLE_CALL)
2154 gimple_call_set_lhs (stmt, lhs);
2155 else
c3284718 2156 gcc_unreachable ();
726a989a
RB
2157}
2158
2159
2160/* Return a deep copy of statement STMT. All the operands from STMT
2161 are reallocated and copied using unshare_expr. The DEF, USE, VDEF
355a7673
MM
2162 and VUSE operand arrays are set to empty in the new copy. The new
2163 copy isn't part of any sequence. */
726a989a
RB
2164
2165gimple
2166gimple_copy (gimple stmt)
2167{
2168 enum gimple_code code = gimple_code (stmt);
2169 unsigned num_ops = gimple_num_ops (stmt);
2170 gimple copy = gimple_alloc (code, num_ops);
2171 unsigned i;
2172
2173 /* Shallow copy all the fields from STMT. */
2174 memcpy (copy, stmt, gimple_size (code));
355a7673 2175 gimple_init_singleton (copy);
726a989a
RB
2176
2177 /* If STMT has sub-statements, deep-copy them as well. */
2178 if (gimple_has_substatements (stmt))
2179 {
2180 gimple_seq new_seq;
2181 tree t;
2182
2183 switch (gimple_code (stmt))
2184 {
2185 case GIMPLE_BIND:
2186 new_seq = gimple_seq_copy (gimple_bind_body (stmt));
2187 gimple_bind_set_body (copy, new_seq);
2188 gimple_bind_set_vars (copy, unshare_expr (gimple_bind_vars (stmt)));
2189 gimple_bind_set_block (copy, gimple_bind_block (stmt));
2190 break;
2191
2192 case GIMPLE_CATCH:
2193 new_seq = gimple_seq_copy (gimple_catch_handler (stmt));
2194 gimple_catch_set_handler (copy, new_seq);
2195 t = unshare_expr (gimple_catch_types (stmt));
2196 gimple_catch_set_types (copy, t);
2197 break;
2198
2199 case GIMPLE_EH_FILTER:
2200 new_seq = gimple_seq_copy (gimple_eh_filter_failure (stmt));
2201 gimple_eh_filter_set_failure (copy, new_seq);
2202 t = unshare_expr (gimple_eh_filter_types (stmt));
2203 gimple_eh_filter_set_types (copy, t);
2204 break;
2205
0a35513e
AH
2206 case GIMPLE_EH_ELSE:
2207 new_seq = gimple_seq_copy (gimple_eh_else_n_body (stmt));
2208 gimple_eh_else_set_n_body (copy, new_seq);
2209 new_seq = gimple_seq_copy (gimple_eh_else_e_body (stmt));
2210 gimple_eh_else_set_e_body (copy, new_seq);
2211 break;
2212
726a989a
RB
2213 case GIMPLE_TRY:
2214 new_seq = gimple_seq_copy (gimple_try_eval (stmt));
2215 gimple_try_set_eval (copy, new_seq);
2216 new_seq = gimple_seq_copy (gimple_try_cleanup (stmt));
2217 gimple_try_set_cleanup (copy, new_seq);
2218 break;
2219
2220 case GIMPLE_OMP_FOR:
2221 new_seq = gimple_seq_copy (gimple_omp_for_pre_body (stmt));
2222 gimple_omp_for_set_pre_body (copy, new_seq);
2223 t = unshare_expr (gimple_omp_for_clauses (stmt));
2224 gimple_omp_for_set_clauses (copy, t);
2225 copy->gimple_omp_for.iter
a9429e29
LB
2226 = ggc_alloc_vec_gimple_omp_for_iter
2227 (gimple_omp_for_collapse (stmt));
726a989a
RB
2228 for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
2229 {
2230 gimple_omp_for_set_cond (copy, i,
2231 gimple_omp_for_cond (stmt, i));
2232 gimple_omp_for_set_index (copy, i,
2233 gimple_omp_for_index (stmt, i));
2234 t = unshare_expr (gimple_omp_for_initial (stmt, i));
2235 gimple_omp_for_set_initial (copy, i, t);
2236 t = unshare_expr (gimple_omp_for_final (stmt, i));
2237 gimple_omp_for_set_final (copy, i, t);
2238 t = unshare_expr (gimple_omp_for_incr (stmt, i));
2239 gimple_omp_for_set_incr (copy, i, t);
2240 }
2241 goto copy_omp_body;
2242
2243 case GIMPLE_OMP_PARALLEL:
2244 t = unshare_expr (gimple_omp_parallel_clauses (stmt));
2245 gimple_omp_parallel_set_clauses (copy, t);
2246 t = unshare_expr (gimple_omp_parallel_child_fn (stmt));
2247 gimple_omp_parallel_set_child_fn (copy, t);
2248 t = unshare_expr (gimple_omp_parallel_data_arg (stmt));
2249 gimple_omp_parallel_set_data_arg (copy, t);
2250 goto copy_omp_body;
2251
2252 case GIMPLE_OMP_TASK:
2253 t = unshare_expr (gimple_omp_task_clauses (stmt));
2254 gimple_omp_task_set_clauses (copy, t);
2255 t = unshare_expr (gimple_omp_task_child_fn (stmt));
2256 gimple_omp_task_set_child_fn (copy, t);
2257 t = unshare_expr (gimple_omp_task_data_arg (stmt));
2258 gimple_omp_task_set_data_arg (copy, t);
2259 t = unshare_expr (gimple_omp_task_copy_fn (stmt));
2260 gimple_omp_task_set_copy_fn (copy, t);
2261 t = unshare_expr (gimple_omp_task_arg_size (stmt));
2262 gimple_omp_task_set_arg_size (copy, t);
2263 t = unshare_expr (gimple_omp_task_arg_align (stmt));
2264 gimple_omp_task_set_arg_align (copy, t);
2265 goto copy_omp_body;
2266
2267 case GIMPLE_OMP_CRITICAL:
2268 t = unshare_expr (gimple_omp_critical_name (stmt));
2269 gimple_omp_critical_set_name (copy, t);
2270 goto copy_omp_body;
2271
2272 case GIMPLE_OMP_SECTIONS:
2273 t = unshare_expr (gimple_omp_sections_clauses (stmt));
2274 gimple_omp_sections_set_clauses (copy, t);
2275 t = unshare_expr (gimple_omp_sections_control (stmt));
2276 gimple_omp_sections_set_control (copy, t);
2277 /* FALLTHRU */
2278
2279 case GIMPLE_OMP_SINGLE:
2280 case GIMPLE_OMP_SECTION:
2281 case GIMPLE_OMP_MASTER:
2282 case GIMPLE_OMP_ORDERED:
2283 copy_omp_body:
2284 new_seq = gimple_seq_copy (gimple_omp_body (stmt));
2285 gimple_omp_set_body (copy, new_seq);
2286 break;
2287
0a35513e
AH
2288 case GIMPLE_TRANSACTION:
2289 new_seq = gimple_seq_copy (gimple_transaction_body (stmt));
2290 gimple_transaction_set_body (copy, new_seq);
2291 break;
2292
726a989a
RB
2293 case GIMPLE_WITH_CLEANUP_EXPR:
2294 new_seq = gimple_seq_copy (gimple_wce_cleanup (stmt));
2295 gimple_wce_set_cleanup (copy, new_seq);
2296 break;
2297
2298 default:
2299 gcc_unreachable ();
2300 }
2301 }
2302
2303 /* Make copy of operands. */
483ef49f
RG
2304 for (i = 0; i < num_ops; i++)
2305 gimple_set_op (copy, i, unshare_expr (gimple_op (stmt, i)));
726a989a 2306
483ef49f
RG
2307 if (gimple_has_mem_ops (stmt))
2308 {
2309 gimple_set_vdef (copy, gimple_vdef (stmt));
2310 gimple_set_vuse (copy, gimple_vuse (stmt));
2311 }
726a989a 2312
483ef49f
RG
2313 /* Clear out SSA operand vectors on COPY. */
2314 if (gimple_has_ops (stmt))
2315 {
483ef49f 2316 gimple_set_use_ops (copy, NULL);
726a989a 2317
5006671f
RG
2318 /* SSA operands need to be updated. */
2319 gimple_set_modified (copy, true);
726a989a
RB
2320 }
2321
2322 return copy;
2323}
2324
2325
726a989a
RB
2326/* Return true if statement S has side-effects. We consider a
2327 statement to have side effects if:
2328
2329 - It is a GIMPLE_CALL not marked with ECF_PURE or ECF_CONST.
2330 - Any of its operands are marked TREE_THIS_VOLATILE or TREE_SIDE_EFFECTS. */
2331
2332bool
2333gimple_has_side_effects (const_gimple s)
2334{
b5b8b0ac
AO
2335 if (is_gimple_debug (s))
2336 return false;
2337
726a989a
RB
2338 /* We don't have to scan the arguments to check for
2339 volatile arguments, though, at present, we still
2340 do a scan to check for TREE_SIDE_EFFECTS. */
2341 if (gimple_has_volatile_ops (s))
2342 return true;
2343
179184e3
RG
2344 if (gimple_code (s) == GIMPLE_ASM
2345 && gimple_asm_volatile_p (s))
2346 return true;
2347
726a989a
RB
2348 if (is_gimple_call (s))
2349 {
723afc44 2350 int flags = gimple_call_flags (s);
726a989a 2351
723afc44
RG
2352 /* An infinite loop is considered a side effect. */
2353 if (!(flags & (ECF_CONST | ECF_PURE))
2354 || (flags & ECF_LOOPING_CONST_OR_PURE))
726a989a
RB
2355 return true;
2356
726a989a
RB
2357 return false;
2358 }
726a989a
RB
2359
2360 return false;
2361}
2362
726a989a 2363/* Helper for gimple_could_trap_p and gimple_assign_rhs_could_trap_p.
e1fd038a
SP
2364 Return true if S can trap. When INCLUDE_MEM is true, check whether
2365 the memory operations could trap. When INCLUDE_STORES is true and
2366 S is a GIMPLE_ASSIGN, the LHS of the assignment is also checked. */
726a989a 2367
e1fd038a
SP
2368bool
2369gimple_could_trap_p_1 (gimple s, bool include_mem, bool include_stores)
726a989a 2370{
726a989a
RB
2371 tree t, div = NULL_TREE;
2372 enum tree_code op;
2373
e1fd038a
SP
2374 if (include_mem)
2375 {
2376 unsigned i, start = (is_gimple_assign (s) && !include_stores) ? 1 : 0;
726a989a 2377
e1fd038a
SP
2378 for (i = start; i < gimple_num_ops (s); i++)
2379 if (tree_could_trap_p (gimple_op (s, i)))
2380 return true;
2381 }
726a989a
RB
2382
2383 switch (gimple_code (s))
2384 {
2385 case GIMPLE_ASM:
2386 return gimple_asm_volatile_p (s);
2387
2388 case GIMPLE_CALL:
2389 t = gimple_call_fndecl (s);
2390 /* Assume that calls to weak functions may trap. */
2391 if (!t || !DECL_P (t) || DECL_WEAK (t))
2392 return true;
2393 return false;
2394
2395 case GIMPLE_ASSIGN:
2396 t = gimple_expr_type (s);
2397 op = gimple_assign_rhs_code (s);
2398 if (get_gimple_rhs_class (op) == GIMPLE_BINARY_RHS)
2399 div = gimple_assign_rhs2 (s);
2400 return (operation_could_trap_p (op, FLOAT_TYPE_P (t),
2401 (INTEGRAL_TYPE_P (t)
2402 && TYPE_OVERFLOW_TRAPS (t)),
2403 div));
2404
2405 default:
2406 break;
2407 }
2408
2409 return false;
726a989a
RB
2410}
2411
726a989a
RB
2412/* Return true if statement S can trap. */
2413
2414bool
2415gimple_could_trap_p (gimple s)
2416{
e1fd038a 2417 return gimple_could_trap_p_1 (s, true, true);
726a989a
RB
2418}
2419
726a989a
RB
2420/* Return true if RHS of a GIMPLE_ASSIGN S can trap. */
2421
2422bool
2423gimple_assign_rhs_could_trap_p (gimple s)
2424{
2425 gcc_assert (is_gimple_assign (s));
e1fd038a 2426 return gimple_could_trap_p_1 (s, true, false);
726a989a
RB
2427}
2428
2429
2430/* Print debugging information for gimple stmts generated. */
2431
2432void
2433dump_gimple_statistics (void)
2434{
726a989a
RB
2435 int i, total_tuples = 0, total_bytes = 0;
2436
7aa6d18a
SB
2437 if (! GATHER_STATISTICS)
2438 {
2439 fprintf (stderr, "No gimple statistics\n");
2440 return;
2441 }
2442
726a989a
RB
2443 fprintf (stderr, "\nGIMPLE statements\n");
2444 fprintf (stderr, "Kind Stmts Bytes\n");
2445 fprintf (stderr, "---------------------------------------\n");
2446 for (i = 0; i < (int) gimple_alloc_kind_all; ++i)
2447 {
2448 fprintf (stderr, "%-20s %7d %10d\n", gimple_alloc_kind_names[i],
2449 gimple_alloc_counts[i], gimple_alloc_sizes[i]);
2450 total_tuples += gimple_alloc_counts[i];
2451 total_bytes += gimple_alloc_sizes[i];
2452 }
2453 fprintf (stderr, "---------------------------------------\n");
2454 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_tuples, total_bytes);
2455 fprintf (stderr, "---------------------------------------\n");
726a989a
RB
2456}
2457
2458
726a989a
RB
2459/* Return the number of operands needed on the RHS of a GIMPLE
2460 assignment for an expression with tree code CODE. */
2461
2462unsigned
2463get_gimple_rhs_num_ops (enum tree_code code)
2464{
2465 enum gimple_rhs_class rhs_class = get_gimple_rhs_class (code);
2466
2467 if (rhs_class == GIMPLE_UNARY_RHS || rhs_class == GIMPLE_SINGLE_RHS)
2468 return 1;
2469 else if (rhs_class == GIMPLE_BINARY_RHS)
2470 return 2;
0354c0c7
BS
2471 else if (rhs_class == GIMPLE_TERNARY_RHS)
2472 return 3;
726a989a
RB
2473 else
2474 gcc_unreachable ();
2475}
2476
2477#define DEFTREECODE(SYM, STRING, TYPE, NARGS) \
2478 (unsigned char) \
2479 ((TYPE) == tcc_unary ? GIMPLE_UNARY_RHS \
2480 : ((TYPE) == tcc_binary \
2481 || (TYPE) == tcc_comparison) ? GIMPLE_BINARY_RHS \
2482 : ((TYPE) == tcc_constant \
2483 || (TYPE) == tcc_declaration \
2484 || (TYPE) == tcc_reference) ? GIMPLE_SINGLE_RHS \
2485 : ((SYM) == TRUTH_AND_EXPR \
2486 || (SYM) == TRUTH_OR_EXPR \
2487 || (SYM) == TRUTH_XOR_EXPR) ? GIMPLE_BINARY_RHS \
2488 : (SYM) == TRUTH_NOT_EXPR ? GIMPLE_UNARY_RHS \
4e71066d
RG
2489 : ((SYM) == COND_EXPR \
2490 || (SYM) == WIDEN_MULT_PLUS_EXPR \
16949072 2491 || (SYM) == WIDEN_MULT_MINUS_EXPR \
f471fe72
RG
2492 || (SYM) == DOT_PROD_EXPR \
2493 || (SYM) == REALIGN_LOAD_EXPR \
4e71066d 2494 || (SYM) == VEC_COND_EXPR \
2205ed25 2495 || (SYM) == VEC_PERM_EXPR \
16949072 2496 || (SYM) == FMA_EXPR) ? GIMPLE_TERNARY_RHS \
4e71066d 2497 : ((SYM) == CONSTRUCTOR \
726a989a
RB
2498 || (SYM) == OBJ_TYPE_REF \
2499 || (SYM) == ASSERT_EXPR \
2500 || (SYM) == ADDR_EXPR \
2501 || (SYM) == WITH_SIZE_EXPR \
4e71066d 2502 || (SYM) == SSA_NAME) ? GIMPLE_SINGLE_RHS \
726a989a
RB
2503 : GIMPLE_INVALID_RHS),
2504#define END_OF_BASE_TREE_CODES (unsigned char) GIMPLE_INVALID_RHS,
2505
2506const unsigned char gimple_rhs_class_table[] = {
2507#include "all-tree.def"
2508};
2509
2510#undef DEFTREECODE
2511#undef END_OF_BASE_TREE_CODES
2512
2513/* For the definitive definition of GIMPLE, see doc/tree-ssa.texi. */
2514
2515/* Validation of GIMPLE expressions. */
2516
726a989a
RB
2517/* Return true if T is a valid LHS for a GIMPLE assignment expression. */
2518
2519bool
2520is_gimple_lvalue (tree t)
2521{
2522 return (is_gimple_addressable (t)
2523 || TREE_CODE (t) == WITH_SIZE_EXPR
2524 /* These are complex lvalues, but don't have addresses, so they
2525 go here. */
2526 || TREE_CODE (t) == BIT_FIELD_REF);
2527}
2528
2529/* Return true if T is a GIMPLE condition. */
2530
2531bool
2532is_gimple_condexpr (tree t)
2533{
2534 return (is_gimple_val (t) || (COMPARISON_CLASS_P (t)
f9613c9a 2535 && !tree_could_throw_p (t)
726a989a
RB
2536 && is_gimple_val (TREE_OPERAND (t, 0))
2537 && is_gimple_val (TREE_OPERAND (t, 1))));
2538}
2539
2540/* Return true if T is something whose address can be taken. */
2541
2542bool
2543is_gimple_addressable (tree t)
2544{
70f34814
RG
2545 return (is_gimple_id (t) || handled_component_p (t)
2546 || TREE_CODE (t) == MEM_REF);
726a989a
RB
2547}
2548
2549/* Return true if T is a valid gimple constant. */
2550
2551bool
2552is_gimple_constant (const_tree t)
2553{
2554 switch (TREE_CODE (t))
2555 {
2556 case INTEGER_CST:
2557 case REAL_CST:
2558 case FIXED_CST:
2559 case STRING_CST:
2560 case COMPLEX_CST:
2561 case VECTOR_CST:
2562 return true;
2563
726a989a
RB
2564 default:
2565 return false;
2566 }
2567}
2568
2569/* Return true if T is a gimple address. */
2570
2571bool
2572is_gimple_address (const_tree t)
2573{
2574 tree op;
2575
2576 if (TREE_CODE (t) != ADDR_EXPR)
2577 return false;
2578
2579 op = TREE_OPERAND (t, 0);
2580 while (handled_component_p (op))
2581 {
2582 if ((TREE_CODE (op) == ARRAY_REF
2583 || TREE_CODE (op) == ARRAY_RANGE_REF)
2584 && !is_gimple_val (TREE_OPERAND (op, 1)))
2585 return false;
2586
2587 op = TREE_OPERAND (op, 0);
2588 }
2589
70f34814 2590 if (CONSTANT_CLASS_P (op) || TREE_CODE (op) == MEM_REF)
726a989a
RB
2591 return true;
2592
2593 switch (TREE_CODE (op))
2594 {
2595 case PARM_DECL:
2596 case RESULT_DECL:
2597 case LABEL_DECL:
2598 case FUNCTION_DECL:
2599 case VAR_DECL:
2600 case CONST_DECL:
2601 return true;
2602
2603 default:
2604 return false;
2605 }
2606}
2607
00fc2333
JH
2608/* Return true if T is a gimple invariant address. */
2609
2610bool
2611is_gimple_invariant_address (const_tree t)
2612{
2613 const_tree op;
2614
2615 if (TREE_CODE (t) != ADDR_EXPR)
2616 return false;
2617
2618 op = strip_invariant_refs (TREE_OPERAND (t, 0));
70f34814
RG
2619 if (!op)
2620 return false;
00fc2333 2621
70f34814
RG
2622 if (TREE_CODE (op) == MEM_REF)
2623 {
2624 const_tree op0 = TREE_OPERAND (op, 0);
2625 return (TREE_CODE (op0) == ADDR_EXPR
2626 && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
2627 || decl_address_invariant_p (TREE_OPERAND (op0, 0))));
2628 }
2629
2630 return CONSTANT_CLASS_P (op) || decl_address_invariant_p (op);
00fc2333
JH
2631}
2632
2633/* Return true if T is a gimple invariant address at IPA level
2634 (so addresses of variables on stack are not allowed). */
2635
2636bool
2637is_gimple_ip_invariant_address (const_tree t)
2638{
2639 const_tree op;
2640
2641 if (TREE_CODE (t) != ADDR_EXPR)
2642 return false;
2643
2644 op = strip_invariant_refs (TREE_OPERAND (t, 0));
39cc8c3d
MJ
2645 if (!op)
2646 return false;
2647
2648 if (TREE_CODE (op) == MEM_REF)
2649 {
2650 const_tree op0 = TREE_OPERAND (op, 0);
2651 return (TREE_CODE (op0) == ADDR_EXPR
2652 && (CONSTANT_CLASS_P (TREE_OPERAND (op0, 0))
2653 || decl_address_ip_invariant_p (TREE_OPERAND (op0, 0))));
2654 }
00fc2333 2655
39cc8c3d 2656 return CONSTANT_CLASS_P (op) || decl_address_ip_invariant_p (op);
726a989a
RB
2657}
2658
2659/* Return true if T is a GIMPLE minimal invariant. It's a restricted
2660 form of function invariant. */
2661
2662bool
2663is_gimple_min_invariant (const_tree t)
2664{
2665 if (TREE_CODE (t) == ADDR_EXPR)
2666 return is_gimple_invariant_address (t);
2667
2668 return is_gimple_constant (t);
2669}
2670
00fc2333
JH
2671/* Return true if T is a GIMPLE interprocedural invariant. It's a restricted
2672 form of gimple minimal invariant. */
2673
2674bool
2675is_gimple_ip_invariant (const_tree t)
2676{
2677 if (TREE_CODE (t) == ADDR_EXPR)
2678 return is_gimple_ip_invariant_address (t);
2679
2680 return is_gimple_constant (t);
2681}
2682
726a989a
RB
2683/* Return true if T is a variable. */
2684
2685bool
2686is_gimple_variable (tree t)
2687{
2688 return (TREE_CODE (t) == VAR_DECL
2689 || TREE_CODE (t) == PARM_DECL
2690 || TREE_CODE (t) == RESULT_DECL
2691 || TREE_CODE (t) == SSA_NAME);
2692}
2693
2694/* Return true if T is a GIMPLE identifier (something with an address). */
2695
2696bool
2697is_gimple_id (tree t)
2698{
2699 return (is_gimple_variable (t)
2700 || TREE_CODE (t) == FUNCTION_DECL
2701 || TREE_CODE (t) == LABEL_DECL
2702 || TREE_CODE (t) == CONST_DECL
2703 /* Allow string constants, since they are addressable. */
2704 || TREE_CODE (t) == STRING_CST);
2705}
2706
726a989a
RB
2707/* Return true if T is a non-aggregate register variable. */
2708
2709bool
2710is_gimple_reg (tree t)
2711{
a471762f 2712 if (virtual_operand_p (t))
3828719a 2713 return false;
726a989a 2714
a471762f
RG
2715 if (TREE_CODE (t) == SSA_NAME)
2716 return true;
2717
726a989a
RB
2718 if (!is_gimple_variable (t))
2719 return false;
2720
2721 if (!is_gimple_reg_type (TREE_TYPE (t)))
2722 return false;
2723
2724 /* A volatile decl is not acceptable because we can't reuse it as
2725 needed. We need to copy it into a temp first. */
2726 if (TREE_THIS_VOLATILE (t))
2727 return false;
2728
2729 /* We define "registers" as things that can be renamed as needed,
2730 which with our infrastructure does not apply to memory. */
2731 if (needs_to_live_in_memory (t))
2732 return false;
2733
2734 /* Hard register variables are an interesting case. For those that
2735 are call-clobbered, we don't know where all the calls are, since
2736 we don't (want to) take into account which operations will turn
2737 into libcalls at the rtl level. For those that are call-saved,
2738 we don't currently model the fact that calls may in fact change
2739 global hard registers, nor do we examine ASM_CLOBBERS at the tree
2740 level, and so miss variable changes that might imply. All around,
2741 it seems safest to not do too much optimization with these at the
2742 tree level at all. We'll have to rely on the rtl optimizers to
2743 clean this up, as there we've got all the appropriate bits exposed. */
2744 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
2745 return false;
2746
4636b850
RG
2747 /* Complex and vector values must have been put into SSA-like form.
2748 That is, no assignments to the individual components. */
2749 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE
2750 || TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2751 return DECL_GIMPLE_REG_P (t);
2752
726a989a
RB
2753 return true;
2754}
2755
2756
726a989a
RB
2757/* Return true if T is a GIMPLE rvalue, i.e. an identifier or a constant. */
2758
2759bool
2760is_gimple_val (tree t)
2761{
2762 /* Make loads from volatiles and memory vars explicit. */
2763 if (is_gimple_variable (t)
2764 && is_gimple_reg_type (TREE_TYPE (t))
2765 && !is_gimple_reg (t))
2766 return false;
2767
726a989a
RB
2768 return (is_gimple_variable (t) || is_gimple_min_invariant (t));
2769}
2770
2771/* Similarly, but accept hard registers as inputs to asm statements. */
2772
2773bool
2774is_gimple_asm_val (tree t)
2775{
2776 if (TREE_CODE (t) == VAR_DECL && DECL_HARD_REGISTER (t))
2777 return true;
2778
2779 return is_gimple_val (t);
2780}
2781
2782/* Return true if T is a GIMPLE minimal lvalue. */
2783
2784bool
2785is_gimple_min_lval (tree t)
2786{
ba4d8f9d
RG
2787 if (!(t = CONST_CAST_TREE (strip_invariant_refs (t))))
2788 return false;
70f34814 2789 return (is_gimple_id (t) || TREE_CODE (t) == MEM_REF);
726a989a
RB
2790}
2791
726a989a
RB
2792/* Return true if T is a valid function operand of a CALL_EXPR. */
2793
2794bool
2795is_gimple_call_addr (tree t)
2796{
2797 return (TREE_CODE (t) == OBJ_TYPE_REF || is_gimple_val (t));
2798}
2799
70f34814
RG
2800/* Return true if T is a valid address operand of a MEM_REF. */
2801
2802bool
2803is_gimple_mem_ref_addr (tree t)
2804{
2805 return (is_gimple_reg (t)
2806 || TREE_CODE (t) == INTEGER_CST
2807 || (TREE_CODE (t) == ADDR_EXPR
2808 && (CONSTANT_CLASS_P (TREE_OPERAND (t, 0))
2809 || decl_address_invariant_p (TREE_OPERAND (t, 0)))));
2810}
2811
726a989a
RB
2812
2813/* Given a memory reference expression T, return its base address.
2814 The base address of a memory reference expression is the main
2815 object being referenced. For instance, the base address for
2816 'array[i].fld[j]' is 'array'. You can think of this as stripping
2817 away the offset part from a memory address.
2818
2819 This function calls handled_component_p to strip away all the inner
2820 parts of the memory reference until it reaches the base object. */
2821
2822tree
2823get_base_address (tree t)
2824{
2825 while (handled_component_p (t))
2826 t = TREE_OPERAND (t, 0);
b8698a0f 2827
4d948885
RG
2828 if ((TREE_CODE (t) == MEM_REF
2829 || TREE_CODE (t) == TARGET_MEM_REF)
70f34814
RG
2830 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR)
2831 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
2832
5a27a197
RG
2833 /* ??? Either the alias oracle or all callers need to properly deal
2834 with WITH_SIZE_EXPRs before we can look through those. */
2835 if (TREE_CODE (t) == WITH_SIZE_EXPR)
726a989a 2836 return NULL_TREE;
5a27a197
RG
2837
2838 return t;
726a989a
RB
2839}
2840
2841void
2842recalculate_side_effects (tree t)
2843{
2844 enum tree_code code = TREE_CODE (t);
2845 int len = TREE_OPERAND_LENGTH (t);
2846 int i;
2847
2848 switch (TREE_CODE_CLASS (code))
2849 {
2850 case tcc_expression:
2851 switch (code)
2852 {
2853 case INIT_EXPR:
2854 case MODIFY_EXPR:
2855 case VA_ARG_EXPR:
2856 case PREDECREMENT_EXPR:
2857 case PREINCREMENT_EXPR:
2858 case POSTDECREMENT_EXPR:
2859 case POSTINCREMENT_EXPR:
2860 /* All of these have side-effects, no matter what their
2861 operands are. */
2862 return;
2863
2864 default:
2865 break;
2866 }
2867 /* Fall through. */
2868
2869 case tcc_comparison: /* a comparison expression */
2870 case tcc_unary: /* a unary arithmetic expression */
2871 case tcc_binary: /* a binary arithmetic expression */
2872 case tcc_reference: /* a reference */
2873 case tcc_vl_exp: /* a function call */
2874 TREE_SIDE_EFFECTS (t) = TREE_THIS_VOLATILE (t);
2875 for (i = 0; i < len; ++i)
2876 {
2877 tree op = TREE_OPERAND (t, i);
2878 if (op && TREE_SIDE_EFFECTS (op))
2879 TREE_SIDE_EFFECTS (t) = 1;
2880 }
2881 break;
2882
13f95bdb
EB
2883 case tcc_constant:
2884 /* No side-effects. */
2885 return;
2886
726a989a 2887 default:
726a989a
RB
2888 gcc_unreachable ();
2889 }
2890}
2891
2892/* Canonicalize a tree T for use in a COND_EXPR as conditional. Returns
2893 a canonicalized tree that is valid for a COND_EXPR or NULL_TREE, if
2894 we failed to create one. */
2895
2896tree
2897canonicalize_cond_expr_cond (tree t)
2898{
b66a1bac
RG
2899 /* Strip conversions around boolean operations. */
2900 if (CONVERT_EXPR_P (t)
9b80d091
KT
2901 && (truth_value_p (TREE_CODE (TREE_OPERAND (t, 0)))
2902 || TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
2903 == BOOLEAN_TYPE))
b66a1bac
RG
2904 t = TREE_OPERAND (t, 0);
2905
726a989a 2906 /* For !x use x == 0. */
12430896 2907 if (TREE_CODE (t) == TRUTH_NOT_EXPR)
726a989a
RB
2908 {
2909 tree top0 = TREE_OPERAND (t, 0);
2910 t = build2 (EQ_EXPR, TREE_TYPE (t),
2911 top0, build_int_cst (TREE_TYPE (top0), 0));
2912 }
2913 /* For cmp ? 1 : 0 use cmp. */
2914 else if (TREE_CODE (t) == COND_EXPR
2915 && COMPARISON_CLASS_P (TREE_OPERAND (t, 0))
2916 && integer_onep (TREE_OPERAND (t, 1))
2917 && integer_zerop (TREE_OPERAND (t, 2)))
2918 {
2919 tree top0 = TREE_OPERAND (t, 0);
2920 t = build2 (TREE_CODE (top0), TREE_TYPE (t),
2921 TREE_OPERAND (top0, 0), TREE_OPERAND (top0, 1));
2922 }
4481581f
JL
2923 /* For x ^ y use x != y. */
2924 else if (TREE_CODE (t) == BIT_XOR_EXPR)
2925 t = build2 (NE_EXPR, TREE_TYPE (t),
2926 TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
2927
726a989a
RB
2928 if (is_gimple_condexpr (t))
2929 return t;
2930
2931 return NULL_TREE;
2932}
2933
e6c99067
DN
2934/* Build a GIMPLE_CALL identical to STMT but skipping the arguments in
2935 the positions marked by the set ARGS_TO_SKIP. */
2936
c6f7cfc1 2937gimple
5c0466b5 2938gimple_call_copy_skip_args (gimple stmt, bitmap args_to_skip)
c6f7cfc1
JH
2939{
2940 int i;
c6f7cfc1 2941 int nargs = gimple_call_num_args (stmt);
9771b263
DN
2942 vec<tree> vargs;
2943 vargs.create (nargs);
c6f7cfc1
JH
2944 gimple new_stmt;
2945
2946 for (i = 0; i < nargs; i++)
2947 if (!bitmap_bit_p (args_to_skip, i))
9771b263 2948 vargs.quick_push (gimple_call_arg (stmt, i));
c6f7cfc1 2949
25583c4f
RS
2950 if (gimple_call_internal_p (stmt))
2951 new_stmt = gimple_build_call_internal_vec (gimple_call_internal_fn (stmt),
2952 vargs);
2953 else
2954 new_stmt = gimple_build_call_vec (gimple_call_fn (stmt), vargs);
9771b263 2955 vargs.release ();
c6f7cfc1
JH
2956 if (gimple_call_lhs (stmt))
2957 gimple_call_set_lhs (new_stmt, gimple_call_lhs (stmt));
2958
5006671f
RG
2959 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
2960 gimple_set_vdef (new_stmt, gimple_vdef (stmt));
2961
c6f7cfc1
JH
2962 if (gimple_has_location (stmt))
2963 gimple_set_location (new_stmt, gimple_location (stmt));
8d2adc24 2964 gimple_call_copy_flags (new_stmt, stmt);
c6f7cfc1 2965 gimple_call_set_chain (new_stmt, gimple_call_chain (stmt));
5006671f
RG
2966
2967 gimple_set_modified (new_stmt, true);
2968
c6f7cfc1
JH
2969 return new_stmt;
2970}
2971
5006671f 2972
d7f09764 2973
d025732d
EB
2974/* Return true if the field decls F1 and F2 are at the same offset.
2975
91f2fae8 2976 This is intended to be used on GIMPLE types only. */
d7f09764 2977
1e4bc4eb 2978bool
d025732d 2979gimple_compare_field_offset (tree f1, tree f2)
d7f09764
DN
2980{
2981 if (DECL_OFFSET_ALIGN (f1) == DECL_OFFSET_ALIGN (f2))
d025732d
EB
2982 {
2983 tree offset1 = DECL_FIELD_OFFSET (f1);
2984 tree offset2 = DECL_FIELD_OFFSET (f2);
2985 return ((offset1 == offset2
2986 /* Once gimplification is done, self-referential offsets are
2987 instantiated as operand #2 of the COMPONENT_REF built for
2988 each access and reset. Therefore, they are not relevant
2989 anymore and fields are interchangeable provided that they
2990 represent the same access. */
2991 || (TREE_CODE (offset1) == PLACEHOLDER_EXPR
2992 && TREE_CODE (offset2) == PLACEHOLDER_EXPR
2993 && (DECL_SIZE (f1) == DECL_SIZE (f2)
2994 || (TREE_CODE (DECL_SIZE (f1)) == PLACEHOLDER_EXPR
2995 && TREE_CODE (DECL_SIZE (f2)) == PLACEHOLDER_EXPR)
2996 || operand_equal_p (DECL_SIZE (f1), DECL_SIZE (f2), 0))
2997 && DECL_ALIGN (f1) == DECL_ALIGN (f2))
2998 || operand_equal_p (offset1, offset2, 0))
2999 && tree_int_cst_equal (DECL_FIELD_BIT_OFFSET (f1),
3000 DECL_FIELD_BIT_OFFSET (f2)));
3001 }
d7f09764
DN
3002
3003 /* Fortran and C do not always agree on what DECL_OFFSET_ALIGN
3004 should be, so handle differing ones specially by decomposing
3005 the offset into a byte and bit offset manually. */
3006 if (host_integerp (DECL_FIELD_OFFSET (f1), 0)
3007 && host_integerp (DECL_FIELD_OFFSET (f2), 0))
3008 {
3009 unsigned HOST_WIDE_INT byte_offset1, byte_offset2;
3010 unsigned HOST_WIDE_INT bit_offset1, bit_offset2;
3011 bit_offset1 = TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (f1));
3012 byte_offset1 = (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (f1))
3013 + bit_offset1 / BITS_PER_UNIT);
3014 bit_offset2 = TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (f2));
3015 byte_offset2 = (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (f2))
3016 + bit_offset2 / BITS_PER_UNIT);
3017 if (byte_offset1 != byte_offset2)
3018 return false;
3019 return bit_offset1 % BITS_PER_UNIT == bit_offset2 % BITS_PER_UNIT;
3020 }
3021
3022 return false;
3023}
3024
825b27de
RG
3025/* Returning a hash value for gimple type TYPE combined with VAL.
3026
3027 The hash value returned is equal for types considered compatible
3028 by gimple_canonical_types_compatible_p. */
3029
3030static hashval_t
3031iterative_hash_canonical_type (tree type, hashval_t val)
3032{
3033 hashval_t v;
3034 void **slot;
3035 struct tree_int_map *mp, m;
3036
3037 m.base.from = type;
3038 if ((slot = htab_find_slot (canonical_type_hash_cache, &m, INSERT))
3039 && *slot)
d0340959 3040 return iterative_hash_hashval_t (((struct tree_int_map *) *slot)->to, val);
825b27de
RG
3041
3042 /* Combine a few common features of types so that types are grouped into
3043 smaller sets; when searching for existing matching types to merge,
3044 only existing types having the same features as the new type will be
3045 checked. */
3046 v = iterative_hash_hashval_t (TREE_CODE (type), 0);
825b27de 3047 v = iterative_hash_hashval_t (TREE_ADDRESSABLE (type), v);
61332f77
RG
3048 v = iterative_hash_hashval_t (TYPE_ALIGN (type), v);
3049 v = iterative_hash_hashval_t (TYPE_MODE (type), v);
825b27de
RG
3050
3051 /* Incorporate common features of numerical types. */
3052 if (INTEGRAL_TYPE_P (type)
3053 || SCALAR_FLOAT_TYPE_P (type)
61332f77 3054 || FIXED_POINT_TYPE_P (type)
61332f77
RG
3055 || TREE_CODE (type) == OFFSET_TYPE
3056 || POINTER_TYPE_P (type))
825b27de
RG
3057 {
3058 v = iterative_hash_hashval_t (TYPE_PRECISION (type), v);
825b27de
RG
3059 v = iterative_hash_hashval_t (TYPE_UNSIGNED (type), v);
3060 }
3061
a5e0cd1d
MG
3062 if (VECTOR_TYPE_P (type))
3063 {
3064 v = iterative_hash_hashval_t (TYPE_VECTOR_SUBPARTS (type), v);
3065 v = iterative_hash_hashval_t (TYPE_UNSIGNED (type), v);
3066 }
3067
3068 if (TREE_CODE (type) == COMPLEX_TYPE)
3069 v = iterative_hash_hashval_t (TYPE_UNSIGNED (type), v);
3070
825b27de
RG
3071 /* For pointer and reference types, fold in information about the type
3072 pointed to but do not recurse to the pointed-to type. */
3073 if (POINTER_TYPE_P (type))
3074 {
3075 v = iterative_hash_hashval_t (TYPE_REF_CAN_ALIAS_ALL (type), v);
61332f77
RG
3076 v = iterative_hash_hashval_t (TYPE_ADDR_SPACE (TREE_TYPE (type)), v);
3077 v = iterative_hash_hashval_t (TYPE_RESTRICT (type), v);
825b27de
RG
3078 v = iterative_hash_hashval_t (TREE_CODE (TREE_TYPE (type)), v);
3079 }
3080
2e745103 3081 /* For integer types hash only the string flag. */
825b27de 3082 if (TREE_CODE (type) == INTEGER_TYPE)
3ac8781c 3083 v = iterative_hash_hashval_t (TYPE_STRING_FLAG (type), v);
825b27de 3084
2e745103
EB
3085 /* For array types hash the domain bounds and the string flag. */
3086 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
825b27de
RG
3087 {
3088 v = iterative_hash_hashval_t (TYPE_STRING_FLAG (type), v);
2e745103
EB
3089 /* OMP lowering can introduce error_mark_node in place of
3090 random local decls in types. */
3091 if (TYPE_MIN_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
3092 v = iterative_hash_expr (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), v);
3093 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != error_mark_node)
3094 v = iterative_hash_expr (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), v);
825b27de
RG
3095 }
3096
3097 /* Recurse for aggregates with a single element type. */
3098 if (TREE_CODE (type) == ARRAY_TYPE
3099 || TREE_CODE (type) == COMPLEX_TYPE
3100 || TREE_CODE (type) == VECTOR_TYPE)
3101 v = iterative_hash_canonical_type (TREE_TYPE (type), v);
3102
3103 /* Incorporate function return and argument types. */
3104 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
3105 {
3106 unsigned na;
3107 tree p;
3108
3109 /* For method types also incorporate their parent class. */
3110 if (TREE_CODE (type) == METHOD_TYPE)
3111 v = iterative_hash_canonical_type (TYPE_METHOD_BASETYPE (type), v);
3112
6a20ce76 3113 v = iterative_hash_canonical_type (TREE_TYPE (type), v);
825b27de
RG
3114
3115 for (p = TYPE_ARG_TYPES (type), na = 0; p; p = TREE_CHAIN (p))
3116 {
6a20ce76 3117 v = iterative_hash_canonical_type (TREE_VALUE (p), v);
825b27de
RG
3118 na++;
3119 }
3120
3121 v = iterative_hash_hashval_t (na, v);
3122 }
3123
aa47290b 3124 if (RECORD_OR_UNION_TYPE_P (type))
825b27de
RG
3125 {
3126 unsigned nf;
3127 tree f;
3128
3129 for (f = TYPE_FIELDS (type), nf = 0; f; f = TREE_CHAIN (f))
e7cfe241
RG
3130 if (TREE_CODE (f) == FIELD_DECL)
3131 {
3132 v = iterative_hash_canonical_type (TREE_TYPE (f), v);
3133 nf++;
3134 }
825b27de
RG
3135
3136 v = iterative_hash_hashval_t (nf, v);
3137 }
3138
3139 /* Cache the just computed hash value. */
3140 mp = ggc_alloc_cleared_tree_int_map ();
3141 mp->base.from = type;
3142 mp->to = v;
3143 *slot = (void *) mp;
3144
3145 return iterative_hash_hashval_t (v, val);
3146}
3147
a844a60b
RG
3148static hashval_t
3149gimple_canonical_type_hash (const void *p)
3150{
825b27de
RG
3151 if (canonical_type_hash_cache == NULL)
3152 canonical_type_hash_cache = htab_create_ggc (512, tree_int_map_hash,
3153 tree_int_map_eq, NULL);
3154
3155 return iterative_hash_canonical_type (CONST_CAST_TREE ((const_tree) p), 0);
a844a60b
RG
3156}
3157
d7f09764 3158
93b2a207 3159
4490cae6 3160
825b27de
RG
3161/* The TYPE_CANONICAL merging machinery. It should closely resemble
3162 the middle-end types_compatible_p function. It needs to avoid
3163 claiming types are different for types that should be treated
3164 the same with respect to TBAA. Canonical types are also used
3165 for IL consistency checks via the useless_type_conversion_p
3166 predicate which does not handle all type kinds itself but falls
3167 back to pointer-comparison of TYPE_CANONICAL for aggregates
3168 for example. */
3169
3170/* Return true iff T1 and T2 are structurally identical for what
3171 TBAA is concerned. */
3172
3173static bool
3174gimple_canonical_types_compatible_p (tree t1, tree t2)
3175{
825b27de
RG
3176 /* Before starting to set up the SCC machinery handle simple cases. */
3177
3178 /* Check first for the obvious case of pointer identity. */
3179 if (t1 == t2)
3180 return true;
3181
3182 /* Check that we have two types to compare. */
3183 if (t1 == NULL_TREE || t2 == NULL_TREE)
3184 return false;
3185
3186 /* If the types have been previously registered and found equal
3187 they still are. */
3188 if (TYPE_CANONICAL (t1)
3189 && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
3190 return true;
3191
3192 /* Can't be the same type if the types don't have the same code. */
3193 if (TREE_CODE (t1) != TREE_CODE (t2))
3194 return false;
3195
61332f77 3196 if (TREE_ADDRESSABLE (t1) != TREE_ADDRESSABLE (t2))
825b27de
RG
3197 return false;
3198
61332f77
RG
3199 /* Qualifiers do not matter for canonical type comparison purposes. */
3200
3201 /* Void types and nullptr types are always the same. */
3202 if (TREE_CODE (t1) == VOID_TYPE
3203 || TREE_CODE (t1) == NULLPTR_TYPE)
825b27de
RG
3204 return true;
3205
61332f77
RG
3206 /* Can't be the same type if they have different alignment, or mode. */
3207 if (TYPE_ALIGN (t1) != TYPE_ALIGN (t2)
3208 || TYPE_MODE (t1) != TYPE_MODE (t2))
3209 return false;
3210
3211 /* Non-aggregate types can be handled cheaply. */
825b27de
RG
3212 if (INTEGRAL_TYPE_P (t1)
3213 || SCALAR_FLOAT_TYPE_P (t1)
3214 || FIXED_POINT_TYPE_P (t1)
3215 || TREE_CODE (t1) == VECTOR_TYPE
3216 || TREE_CODE (t1) == COMPLEX_TYPE
61332f77
RG
3217 || TREE_CODE (t1) == OFFSET_TYPE
3218 || POINTER_TYPE_P (t1))
825b27de 3219 {
61332f77
RG
3220 /* Can't be the same type if they have different sign or precision. */
3221 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2)
825b27de
RG
3222 || TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
3223 return false;
3224
3225 if (TREE_CODE (t1) == INTEGER_TYPE
3ac8781c 3226 && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
825b27de
RG
3227 return false;
3228
61332f77
RG
3229 /* For canonical type comparisons we do not want to build SCCs
3230 so we cannot compare pointed-to types. But we can, for now,
3231 require the same pointed-to type kind and match what
3232 useless_type_conversion_p would do. */
3233 if (POINTER_TYPE_P (t1))
3234 {
3235 /* If the two pointers have different ref-all attributes,
3236 they can't be the same type. */
3237 if (TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
3238 return false;
825b27de 3239
61332f77
RG
3240 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
3241 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
3242 return false;
825b27de 3243
61332f77
RG
3244 if (TYPE_RESTRICT (t1) != TYPE_RESTRICT (t2))
3245 return false;
3246
3247 if (TREE_CODE (TREE_TYPE (t1)) != TREE_CODE (TREE_TYPE (t2)))
3248 return false;
3249 }
3250
3251 /* Tail-recurse to components. */
3252 if (TREE_CODE (t1) == VECTOR_TYPE
3253 || TREE_CODE (t1) == COMPLEX_TYPE)
3254 return gimple_canonical_types_compatible_p (TREE_TYPE (t1),
3255 TREE_TYPE (t2));
3256
3257 return true;
825b27de
RG
3258 }
3259
825b27de
RG
3260 /* Do type-specific comparisons. */
3261 switch (TREE_CODE (t1))
3262 {
825b27de
RG
3263 case ARRAY_TYPE:
3264 /* Array types are the same if the element types are the same and
3265 the number of elements are the same. */
3266 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2))
3267 || TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2)
3268 || TYPE_NONALIASED_COMPONENT (t1) != TYPE_NONALIASED_COMPONENT (t2))
b8a71aed 3269 return false;
825b27de
RG
3270 else
3271 {
3272 tree i1 = TYPE_DOMAIN (t1);
3273 tree i2 = TYPE_DOMAIN (t2);
3274
3275 /* For an incomplete external array, the type domain can be
3276 NULL_TREE. Check this condition also. */
3277 if (i1 == NULL_TREE && i2 == NULL_TREE)
b8a71aed 3278 return true;
825b27de 3279 else if (i1 == NULL_TREE || i2 == NULL_TREE)
b8a71aed 3280 return false;
825b27de
RG
3281 else
3282 {
3283 tree min1 = TYPE_MIN_VALUE (i1);
3284 tree min2 = TYPE_MIN_VALUE (i2);
3285 tree max1 = TYPE_MAX_VALUE (i1);
3286 tree max2 = TYPE_MAX_VALUE (i2);
3287
3288 /* The minimum/maximum values have to be the same. */
3289 if ((min1 == min2
3290 || (min1 && min2
3291 && ((TREE_CODE (min1) == PLACEHOLDER_EXPR
3292 && TREE_CODE (min2) == PLACEHOLDER_EXPR)
3293 || operand_equal_p (min1, min2, 0))))
3294 && (max1 == max2
3295 || (max1 && max2
3296 && ((TREE_CODE (max1) == PLACEHOLDER_EXPR
3297 && TREE_CODE (max2) == PLACEHOLDER_EXPR)
3298 || operand_equal_p (max1, max2, 0)))))
b8a71aed 3299 return true;
825b27de 3300 else
b8a71aed 3301 return false;
825b27de
RG
3302 }
3303 }
3304
3305 case METHOD_TYPE:
825b27de
RG
3306 case FUNCTION_TYPE:
3307 /* Function types are the same if the return type and arguments types
3308 are the same. */
6a20ce76 3309 if (!gimple_canonical_types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
b8a71aed 3310 return false;
825b27de
RG
3311
3312 if (!comp_type_attributes (t1, t2))
b8a71aed 3313 return false;
825b27de
RG
3314
3315 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2))
b8a71aed 3316 return true;
825b27de
RG
3317 else
3318 {
3319 tree parms1, parms2;
3320
3321 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
3322 parms1 && parms2;
3323 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
3324 {
6a20ce76
RG
3325 if (!gimple_canonical_types_compatible_p
3326 (TREE_VALUE (parms1), TREE_VALUE (parms2)))
b8a71aed 3327 return false;
825b27de
RG
3328 }
3329
3330 if (parms1 || parms2)
b8a71aed 3331 return false;
825b27de 3332
b8a71aed 3333 return true;
825b27de
RG
3334 }
3335
825b27de
RG
3336 case RECORD_TYPE:
3337 case UNION_TYPE:
3338 case QUAL_UNION_TYPE:
3339 {
3340 tree f1, f2;
3341
3342 /* For aggregate types, all the fields must be the same. */
3343 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
4acd1c84 3344 f1 || f2;
825b27de
RG
3345 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
3346 {
e7cfe241
RG
3347 /* Skip non-fields. */
3348 while (f1 && TREE_CODE (f1) != FIELD_DECL)
3349 f1 = TREE_CHAIN (f1);
3350 while (f2 && TREE_CODE (f2) != FIELD_DECL)
3351 f2 = TREE_CHAIN (f2);
3352 if (!f1 || !f2)
3353 break;
825b27de
RG
3354 /* The fields must have the same name, offset and type. */
3355 if (DECL_NONADDRESSABLE_P (f1) != DECL_NONADDRESSABLE_P (f2)
3356 || !gimple_compare_field_offset (f1, f2)
3357 || !gimple_canonical_types_compatible_p
3358 (TREE_TYPE (f1), TREE_TYPE (f2)))
b8a71aed 3359 return false;
825b27de
RG
3360 }
3361
3362 /* If one aggregate has more fields than the other, they
3363 are not the same. */
3364 if (f1 || f2)
b8a71aed 3365 return false;
825b27de 3366
b8a71aed 3367 return true;
825b27de
RG
3368 }
3369
3370 default:
3371 gcc_unreachable ();
3372 }
825b27de
RG
3373}
3374
3375
4490cae6
RG
3376/* Returns nonzero if P1 and P2 are equal. */
3377
3378static int
3379gimple_canonical_type_eq (const void *p1, const void *p2)
3380{
3381 const_tree t1 = (const_tree) p1;
3382 const_tree t2 = (const_tree) p2;
825b27de
RG
3383 return gimple_canonical_types_compatible_p (CONST_CAST_TREE (t1),
3384 CONST_CAST_TREE (t2));
4490cae6
RG
3385}
3386
3387/* Register type T in the global type table gimple_types.
3388 If another type T', compatible with T, already existed in
3389 gimple_types then return T', otherwise return T. This is used by
96d91dcf
RG
3390 LTO to merge identical types read from different TUs.
3391
3392 ??? This merging does not exactly match how the tree.c middle-end
3393 functions will assign TYPE_CANONICAL when new types are created
3394 during optimization (which at least happens for pointer and array
3395 types). */
4490cae6
RG
3396
3397tree
3398gimple_register_canonical_type (tree t)
3399{
3400 void **slot;
3401
3402 gcc_assert (TYPE_P (t));
3403
61332f77
RG
3404 if (TYPE_CANONICAL (t))
3405 return TYPE_CANONICAL (t);
3406
4490cae6 3407 if (gimple_canonical_types == NULL)
a844a60b 3408 gimple_canonical_types = htab_create_ggc (16381, gimple_canonical_type_hash,
4490cae6
RG
3409 gimple_canonical_type_eq, 0);
3410
3411 slot = htab_find_slot (gimple_canonical_types, t, INSERT);
3412 if (*slot
3413 && *(tree *)slot != t)
3414 {
3415 tree new_type = (tree) *((tree *) slot);
3416
3417 TYPE_CANONICAL (t) = new_type;
3418 t = new_type;
3419 }
3420 else
3421 {
3422 TYPE_CANONICAL (t) = t;
4a2ac96f
RG
3423 *slot = (void *) t;
3424 }
d7f09764
DN
3425
3426 return t;
3427}
3428
3429
3430/* Show statistics on references to the global type table gimple_types. */
3431
3432void
b8f4e58f 3433print_gimple_types_stats (const char *pfx)
d7f09764 3434{
4490cae6 3435 if (gimple_canonical_types)
b8f4e58f
RG
3436 fprintf (stderr, "[%s] GIMPLE canonical type table: size %ld, "
3437 "%ld elements, %ld searches, %ld collisions (ratio: %f)\n", pfx,
4490cae6
RG
3438 (long) htab_size (gimple_canonical_types),
3439 (long) htab_elements (gimple_canonical_types),
3440 (long) gimple_canonical_types->searches,
3441 (long) gimple_canonical_types->collisions,
3442 htab_collisions (gimple_canonical_types));
3443 else
b8f4e58f 3444 fprintf (stderr, "[%s] GIMPLE canonical type table is empty\n", pfx);
a844a60b 3445 if (canonical_type_hash_cache)
b8f4e58f
RG
3446 fprintf (stderr, "[%s] GIMPLE canonical type hash table: size %ld, "
3447 "%ld elements, %ld searches, %ld collisions (ratio: %f)\n", pfx,
a844a60b
RG
3448 (long) htab_size (canonical_type_hash_cache),
3449 (long) htab_elements (canonical_type_hash_cache),
3450 (long) canonical_type_hash_cache->searches,
3451 (long) canonical_type_hash_cache->collisions,
3452 htab_collisions (canonical_type_hash_cache));
0f443ad0 3453 else
b8f4e58f 3454 fprintf (stderr, "[%s] GIMPLE canonical type hash table is empty\n", pfx);
d7f09764
DN
3455}
3456
0d0bfe17
RG
3457/* Free the gimple type hashtables used for LTO type merging. */
3458
3459void
3460free_gimple_type_tables (void)
3461{
4490cae6
RG
3462 if (gimple_canonical_types)
3463 {
3464 htab_delete (gimple_canonical_types);
3465 gimple_canonical_types = NULL;
3466 }
a844a60b
RG
3467 if (canonical_type_hash_cache)
3468 {
3469 htab_delete (canonical_type_hash_cache);
3470 canonical_type_hash_cache = NULL;
3471 }
0d0bfe17
RG
3472}
3473
d7f09764
DN
3474
3475/* Return a type the same as TYPE except unsigned or
3476 signed according to UNSIGNEDP. */
3477
3478static tree
3479gimple_signed_or_unsigned_type (bool unsignedp, tree type)
3480{
3481 tree type1;
3482
3483 type1 = TYPE_MAIN_VARIANT (type);
3484 if (type1 == signed_char_type_node
3485 || type1 == char_type_node
3486 || type1 == unsigned_char_type_node)
3487 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3488 if (type1 == integer_type_node || type1 == unsigned_type_node)
3489 return unsignedp ? unsigned_type_node : integer_type_node;
3490 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3491 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3492 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3493 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3494 if (type1 == long_long_integer_type_node
3495 || type1 == long_long_unsigned_type_node)
3496 return unsignedp
3497 ? long_long_unsigned_type_node
3498 : long_long_integer_type_node;
a6766312
KT
3499 if (int128_integer_type_node && (type1 == int128_integer_type_node || type1 == int128_unsigned_type_node))
3500 return unsignedp
3501 ? int128_unsigned_type_node
3502 : int128_integer_type_node;
d7f09764
DN
3503#if HOST_BITS_PER_WIDE_INT >= 64
3504 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3505 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3506#endif
3507 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3508 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3509 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3510 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3511 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3512 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3513 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3514 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3515
3516#define GIMPLE_FIXED_TYPES(NAME) \
3517 if (type1 == short_ ## NAME ## _type_node \
3518 || type1 == unsigned_short_ ## NAME ## _type_node) \
3519 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3520 : short_ ## NAME ## _type_node; \
3521 if (type1 == NAME ## _type_node \
3522 || type1 == unsigned_ ## NAME ## _type_node) \
3523 return unsignedp ? unsigned_ ## NAME ## _type_node \
3524 : NAME ## _type_node; \
3525 if (type1 == long_ ## NAME ## _type_node \
3526 || type1 == unsigned_long_ ## NAME ## _type_node) \
3527 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3528 : long_ ## NAME ## _type_node; \
3529 if (type1 == long_long_ ## NAME ## _type_node \
3530 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3531 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3532 : long_long_ ## NAME ## _type_node;
3533
3534#define GIMPLE_FIXED_MODE_TYPES(NAME) \
3535 if (type1 == NAME ## _type_node \
3536 || type1 == u ## NAME ## _type_node) \
3537 return unsignedp ? u ## NAME ## _type_node \
3538 : NAME ## _type_node;
3539
3540#define GIMPLE_FIXED_TYPES_SAT(NAME) \
3541 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3542 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3543 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3544 : sat_ ## short_ ## NAME ## _type_node; \
3545 if (type1 == sat_ ## NAME ## _type_node \
3546 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3547 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3548 : sat_ ## NAME ## _type_node; \
3549 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3550 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3551 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3552 : sat_ ## long_ ## NAME ## _type_node; \
3553 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3554 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3555 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3556 : sat_ ## long_long_ ## NAME ## _type_node;
3557
3558#define GIMPLE_FIXED_MODE_TYPES_SAT(NAME) \
3559 if (type1 == sat_ ## NAME ## _type_node \
3560 || type1 == sat_ ## u ## NAME ## _type_node) \
3561 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3562 : sat_ ## NAME ## _type_node;
3563
3564 GIMPLE_FIXED_TYPES (fract);
3565 GIMPLE_FIXED_TYPES_SAT (fract);
3566 GIMPLE_FIXED_TYPES (accum);
3567 GIMPLE_FIXED_TYPES_SAT (accum);
3568
3569 GIMPLE_FIXED_MODE_TYPES (qq);
3570 GIMPLE_FIXED_MODE_TYPES (hq);
3571 GIMPLE_FIXED_MODE_TYPES (sq);
3572 GIMPLE_FIXED_MODE_TYPES (dq);
3573 GIMPLE_FIXED_MODE_TYPES (tq);
3574 GIMPLE_FIXED_MODE_TYPES_SAT (qq);
3575 GIMPLE_FIXED_MODE_TYPES_SAT (hq);
3576 GIMPLE_FIXED_MODE_TYPES_SAT (sq);
3577 GIMPLE_FIXED_MODE_TYPES_SAT (dq);
3578 GIMPLE_FIXED_MODE_TYPES_SAT (tq);
3579 GIMPLE_FIXED_MODE_TYPES (ha);
3580 GIMPLE_FIXED_MODE_TYPES (sa);
3581 GIMPLE_FIXED_MODE_TYPES (da);
3582 GIMPLE_FIXED_MODE_TYPES (ta);
3583 GIMPLE_FIXED_MODE_TYPES_SAT (ha);
3584 GIMPLE_FIXED_MODE_TYPES_SAT (sa);
3585 GIMPLE_FIXED_MODE_TYPES_SAT (da);
3586 GIMPLE_FIXED_MODE_TYPES_SAT (ta);
3587
3588 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3589 the precision; they have precision set to match their range, but
3590 may use a wider mode to match an ABI. If we change modes, we may
3591 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3592 the precision as well, so as to yield correct results for
3593 bit-field types. C++ does not have these separate bit-field
3594 types, and producing a signed or unsigned variant of an
3595 ENUMERAL_TYPE may cause other problems as well. */
3596 if (!INTEGRAL_TYPE_P (type)
3597 || TYPE_UNSIGNED (type) == unsignedp)
3598 return type;
3599
3600#define TYPE_OK(node) \
3601 (TYPE_MODE (type) == TYPE_MODE (node) \
3602 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
3603 if (TYPE_OK (signed_char_type_node))
3604 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3605 if (TYPE_OK (integer_type_node))
3606 return unsignedp ? unsigned_type_node : integer_type_node;
3607 if (TYPE_OK (short_integer_type_node))
3608 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3609 if (TYPE_OK (long_integer_type_node))
3610 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3611 if (TYPE_OK (long_long_integer_type_node))
3612 return (unsignedp
3613 ? long_long_unsigned_type_node
3614 : long_long_integer_type_node);
a6766312
KT
3615 if (int128_integer_type_node && TYPE_OK (int128_integer_type_node))
3616 return (unsignedp
3617 ? int128_unsigned_type_node
3618 : int128_integer_type_node);
d7f09764
DN
3619
3620#if HOST_BITS_PER_WIDE_INT >= 64
3621 if (TYPE_OK (intTI_type_node))
3622 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3623#endif
3624 if (TYPE_OK (intDI_type_node))
3625 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3626 if (TYPE_OK (intSI_type_node))
3627 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3628 if (TYPE_OK (intHI_type_node))
3629 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3630 if (TYPE_OK (intQI_type_node))
3631 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3632
3633#undef GIMPLE_FIXED_TYPES
3634#undef GIMPLE_FIXED_MODE_TYPES
3635#undef GIMPLE_FIXED_TYPES_SAT
3636#undef GIMPLE_FIXED_MODE_TYPES_SAT
3637#undef TYPE_OK
3638
3639 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
3640}
3641
3642
3643/* Return an unsigned type the same as TYPE in other respects. */
3644
3645tree
3646gimple_unsigned_type (tree type)
3647{
3648 return gimple_signed_or_unsigned_type (true, type);
3649}
3650
3651
3652/* Return a signed type the same as TYPE in other respects. */
3653
3654tree
3655gimple_signed_type (tree type)
3656{
3657 return gimple_signed_or_unsigned_type (false, type);
3658}
3659
3660
3661/* Return the typed-based alias set for T, which may be an expression
3662 or a type. Return -1 if we don't do anything special. */
3663
3664alias_set_type
3665gimple_get_alias_set (tree t)
3666{
3667 tree u;
3668
3669 /* Permit type-punning when accessing a union, provided the access
3670 is directly through the union. For example, this code does not
3671 permit taking the address of a union member and then storing
3672 through it. Even the type-punning allowed here is a GCC
3673 extension, albeit a common and useful one; the C standard says
3674 that such accesses have implementation-defined behavior. */
3675 for (u = t;
3676 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
3677 u = TREE_OPERAND (u, 0))
3678 if (TREE_CODE (u) == COMPONENT_REF
3679 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
3680 return 0;
3681
3682 /* That's all the expressions we handle specially. */
3683 if (!TYPE_P (t))
3684 return -1;
3685
3686 /* For convenience, follow the C standard when dealing with
3687 character types. Any object may be accessed via an lvalue that
3688 has character type. */
3689 if (t == char_type_node
3690 || t == signed_char_type_node
3691 || t == unsigned_char_type_node)
3692 return 0;
3693
3694 /* Allow aliasing between signed and unsigned variants of the same
3695 type. We treat the signed variant as canonical. */
3696 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
3697 {
3698 tree t1 = gimple_signed_type (t);
3699
3700 /* t1 == t can happen for boolean nodes which are always unsigned. */
3701 if (t1 != t)
3702 return get_alias_set (t1);
3703 }
d7f09764
DN
3704
3705 return -1;
3706}
3707
3708
346ef3fa
RG
3709/* From a tree operand OP return the base of a load or store operation
3710 or NULL_TREE if OP is not a load or a store. */
3711
3712static tree
3713get_base_loadstore (tree op)
3714{
3715 while (handled_component_p (op))
3716 op = TREE_OPERAND (op, 0);
3717 if (DECL_P (op)
3718 || INDIRECT_REF_P (op)
70f34814 3719 || TREE_CODE (op) == MEM_REF
346ef3fa
RG
3720 || TREE_CODE (op) == TARGET_MEM_REF)
3721 return op;
3722 return NULL_TREE;
3723}
3724
3725/* For the statement STMT call the callbacks VISIT_LOAD, VISIT_STORE and
3726 VISIT_ADDR if non-NULL on loads, store and address-taken operands
3727 passing the STMT, the base of the operand and DATA to it. The base
3728 will be either a decl, an indirect reference (including TARGET_MEM_REF)
3729 or the argument of an address expression.
3730 Returns the results of these callbacks or'ed. */
3731
3732bool
3733walk_stmt_load_store_addr_ops (gimple stmt, void *data,
3734 bool (*visit_load)(gimple, tree, void *),
3735 bool (*visit_store)(gimple, tree, void *),
3736 bool (*visit_addr)(gimple, tree, void *))
3737{
3738 bool ret = false;
3739 unsigned i;
3740 if (gimple_assign_single_p (stmt))
3741 {
3742 tree lhs, rhs;
3743 if (visit_store)
3744 {
3745 lhs = get_base_loadstore (gimple_assign_lhs (stmt));
3746 if (lhs)
3747 ret |= visit_store (stmt, lhs, data);
3748 }
3749 rhs = gimple_assign_rhs1 (stmt);
ad8a1ac0
RG
3750 while (handled_component_p (rhs))
3751 rhs = TREE_OPERAND (rhs, 0);
346ef3fa
RG
3752 if (visit_addr)
3753 {
3754 if (TREE_CODE (rhs) == ADDR_EXPR)
3755 ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), data);
3756 else if (TREE_CODE (rhs) == TARGET_MEM_REF
3757 && TREE_CODE (TMR_BASE (rhs)) == ADDR_EXPR)
3758 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (rhs), 0), data);
3759 else if (TREE_CODE (rhs) == OBJ_TYPE_REF
3760 && TREE_CODE (OBJ_TYPE_REF_OBJECT (rhs)) == ADDR_EXPR)
3761 ret |= visit_addr (stmt, TREE_OPERAND (OBJ_TYPE_REF_OBJECT (rhs),
3762 0), data);
cb3d2e33
JJ
3763 else if (TREE_CODE (rhs) == CONSTRUCTOR)
3764 {
3765 unsigned int ix;
3766 tree val;
3767
3768 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), ix, val)
3769 if (TREE_CODE (val) == ADDR_EXPR)
3770 ret |= visit_addr (stmt, TREE_OPERAND (val, 0), data);
3771 else if (TREE_CODE (val) == OBJ_TYPE_REF
3772 && TREE_CODE (OBJ_TYPE_REF_OBJECT (val)) == ADDR_EXPR)
3773 ret |= visit_addr (stmt,
3774 TREE_OPERAND (OBJ_TYPE_REF_OBJECT (val),
3775 0), data);
3776 }
fff1894c
AB
3777 lhs = gimple_assign_lhs (stmt);
3778 if (TREE_CODE (lhs) == TARGET_MEM_REF
fff1894c
AB
3779 && TREE_CODE (TMR_BASE (lhs)) == ADDR_EXPR)
3780 ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (lhs), 0), data);
346ef3fa
RG
3781 }
3782 if (visit_load)
3783 {
3784 rhs = get_base_loadstore (rhs);
3785 if (rhs)
3786 ret |= visit_load (stmt, rhs, data);
3787 }
3788 }
3789 else if (visit_addr
3790 && (is_gimple_assign (stmt)
4d7a65ea 3791 || gimple_code (stmt) == GIMPLE_COND))
346ef3fa
RG
3792 {
3793 for (i = 0; i < gimple_num_ops (stmt); ++i)
9dd58aa4
JJ
3794 {
3795 tree op = gimple_op (stmt, i);
3796 if (op == NULL_TREE)
3797 ;
3798 else if (TREE_CODE (op) == ADDR_EXPR)
3799 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
3800 /* COND_EXPR and VCOND_EXPR rhs1 argument is a comparison
3801 tree with two operands. */
3802 else if (i == 1 && COMPARISON_CLASS_P (op))
3803 {
3804 if (TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
3805 ret |= visit_addr (stmt, TREE_OPERAND (TREE_OPERAND (op, 0),
3806 0), data);
3807 if (TREE_CODE (TREE_OPERAND (op, 1)) == ADDR_EXPR)
3808 ret |= visit_addr (stmt, TREE_OPERAND (TREE_OPERAND (op, 1),
3809 0), data);
3810 }
3811 }
346ef3fa
RG
3812 }
3813 else if (is_gimple_call (stmt))
3814 {
3815 if (visit_store)
3816 {
3817 tree lhs = gimple_call_lhs (stmt);
3818 if (lhs)
3819 {
3820 lhs = get_base_loadstore (lhs);
3821 if (lhs)
3822 ret |= visit_store (stmt, lhs, data);
3823 }
3824 }
3825 if (visit_load || visit_addr)
3826 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3827 {
3828 tree rhs = gimple_call_arg (stmt, i);
3829 if (visit_addr
3830 && TREE_CODE (rhs) == ADDR_EXPR)
3831 ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), data);
3832 else if (visit_load)
3833 {
3834 rhs = get_base_loadstore (rhs);
3835 if (rhs)
3836 ret |= visit_load (stmt, rhs, data);
3837 }
3838 }
3839 if (visit_addr
3840 && gimple_call_chain (stmt)
3841 && TREE_CODE (gimple_call_chain (stmt)) == ADDR_EXPR)
3842 ret |= visit_addr (stmt, TREE_OPERAND (gimple_call_chain (stmt), 0),
3843 data);
1d24fdd9
RG
3844 if (visit_addr
3845 && gimple_call_return_slot_opt_p (stmt)
3846 && gimple_call_lhs (stmt) != NULL_TREE
4d61856d 3847 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
1d24fdd9 3848 ret |= visit_addr (stmt, gimple_call_lhs (stmt), data);
346ef3fa
RG
3849 }
3850 else if (gimple_code (stmt) == GIMPLE_ASM)
3851 {
3852 unsigned noutputs;
3853 const char *constraint;
3854 const char **oconstraints;
3855 bool allows_mem, allows_reg, is_inout;
3856 noutputs = gimple_asm_noutputs (stmt);
3857 oconstraints = XALLOCAVEC (const char *, noutputs);
3858 if (visit_store || visit_addr)
3859 for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
3860 {
3861 tree link = gimple_asm_output_op (stmt, i);
3862 tree op = get_base_loadstore (TREE_VALUE (link));
3863 if (op && visit_store)
3864 ret |= visit_store (stmt, op, data);
3865 if (visit_addr)
3866 {
3867 constraint = TREE_STRING_POINTER
3868 (TREE_VALUE (TREE_PURPOSE (link)));
3869 oconstraints[i] = constraint;
3870 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
3871 &allows_reg, &is_inout);
3872 if (op && !allows_reg && allows_mem)
3873 ret |= visit_addr (stmt, op, data);
3874 }
3875 }
3876 if (visit_load || visit_addr)
3877 for (i = 0; i < gimple_asm_ninputs (stmt); ++i)
3878 {
3879 tree link = gimple_asm_input_op (stmt, i);
3880 tree op = TREE_VALUE (link);
3881 if (visit_addr
3882 && TREE_CODE (op) == ADDR_EXPR)
3883 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
3884 else if (visit_load || visit_addr)
3885 {
3886 op = get_base_loadstore (op);
3887 if (op)
3888 {
3889 if (visit_load)
3890 ret |= visit_load (stmt, op, data);
3891 if (visit_addr)
3892 {
3893 constraint = TREE_STRING_POINTER
3894 (TREE_VALUE (TREE_PURPOSE (link)));
3895 parse_input_constraint (&constraint, 0, 0, noutputs,
3896 0, oconstraints,
3897 &allows_mem, &allows_reg);
3898 if (!allows_reg && allows_mem)
3899 ret |= visit_addr (stmt, op, data);
3900 }
3901 }
3902 }
3903 }
3904 }
3905 else if (gimple_code (stmt) == GIMPLE_RETURN)
3906 {
3907 tree op = gimple_return_retval (stmt);
3908 if (op)
3909 {
3910 if (visit_addr
3911 && TREE_CODE (op) == ADDR_EXPR)
3912 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
3913 else if (visit_load)
3914 {
3915 op = get_base_loadstore (op);
3916 if (op)
3917 ret |= visit_load (stmt, op, data);
3918 }
3919 }
3920 }
3921 else if (visit_addr
3922 && gimple_code (stmt) == GIMPLE_PHI)
3923 {
3924 for (i = 0; i < gimple_phi_num_args (stmt); ++i)
3925 {
80560f95 3926 tree op = gimple_phi_arg_def (stmt, i);
346ef3fa
RG
3927 if (TREE_CODE (op) == ADDR_EXPR)
3928 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
3929 }
3930 }
639dc669
JJ
3931 else if (visit_addr
3932 && gimple_code (stmt) == GIMPLE_GOTO)
3933 {
3934 tree op = gimple_goto_dest (stmt);
3935 if (TREE_CODE (op) == ADDR_EXPR)
3936 ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
3937 }
346ef3fa
RG
3938
3939 return ret;
3940}
3941
3942/* Like walk_stmt_load_store_addr_ops but with NULL visit_addr. IPA-CP
3943 should make a faster clone for this case. */
3944
3945bool
3946walk_stmt_load_store_ops (gimple stmt, void *data,
3947 bool (*visit_load)(gimple, tree, void *),
3948 bool (*visit_store)(gimple, tree, void *))
3949{
3950 return walk_stmt_load_store_addr_ops (stmt, data,
3951 visit_load, visit_store, NULL);
3952}
3953
ccacdf06
RG
3954/* Helper for gimple_ior_addresses_taken_1. */
3955
3956static bool
3957gimple_ior_addresses_taken_1 (gimple stmt ATTRIBUTE_UNUSED,
3958 tree addr, void *data)
3959{
3960 bitmap addresses_taken = (bitmap)data;
2ea9dc64
RG
3961 addr = get_base_address (addr);
3962 if (addr
3963 && DECL_P (addr))
ccacdf06
RG
3964 {
3965 bitmap_set_bit (addresses_taken, DECL_UID (addr));
3966 return true;
3967 }
3968 return false;
3969}
3970
3971/* Set the bit for the uid of all decls that have their address taken
3972 in STMT in the ADDRESSES_TAKEN bitmap. Returns true if there
3973 were any in this stmt. */
3974
3975bool
3976gimple_ior_addresses_taken (bitmap addresses_taken, gimple stmt)
3977{
3978 return walk_stmt_load_store_addr_ops (stmt, addresses_taken, NULL, NULL,
3979 gimple_ior_addresses_taken_1);
3980}
3981
4537ec0c
DN
3982
3983/* Return a printable name for symbol DECL. */
3984
3985const char *
3986gimple_decl_printable_name (tree decl, int verbosity)
3987{
98b2dfbb
RG
3988 if (!DECL_NAME (decl))
3989 return NULL;
4537ec0c
DN
3990
3991 if (DECL_ASSEMBLER_NAME_SET_P (decl))
3992 {
3993 const char *str, *mangled_str;
3994 int dmgl_opts = DMGL_NO_OPTS;
3995
3996 if (verbosity >= 2)
3997 {
3998 dmgl_opts = DMGL_VERBOSE
4537ec0c
DN
3999 | DMGL_ANSI
4000 | DMGL_GNU_V3
4001 | DMGL_RET_POSTFIX;
4002 if (TREE_CODE (decl) == FUNCTION_DECL)
4003 dmgl_opts |= DMGL_PARAMS;
4004 }
4005
4006 mangled_str = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4007 str = cplus_demangle_v3 (mangled_str, dmgl_opts);
4008 return (str) ? str : mangled_str;
4009 }
4010
4011 return IDENTIFIER_POINTER (DECL_NAME (decl));
4012}
4013
25ae5027
DS
4014/* Return TRUE iff stmt is a call to a built-in function. */
4015
4016bool
4017is_gimple_builtin_call (gimple stmt)
4018{
4019 tree callee;
4020
4021 if (is_gimple_call (stmt)
4022 && (callee = gimple_call_fndecl (stmt))
4023 && is_builtin_fn (callee)
4024 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
4025 return true;
4026
4027 return false;
4028}
4029
3626621a
RB
4030/* Return true when STMTs arguments match those of FNDECL. */
4031
4032static bool
4033validate_call (gimple stmt, tree fndecl)
4034{
4035 tree targs = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
4036 unsigned nargs = gimple_call_num_args (stmt);
4037 for (unsigned i = 0; i < nargs; ++i)
4038 {
4039 /* Variadic args follow. */
4040 if (!targs)
4041 return true;
4042 tree arg = gimple_call_arg (stmt, i);
4043 if (INTEGRAL_TYPE_P (TREE_TYPE (arg))
4044 && INTEGRAL_TYPE_P (TREE_VALUE (targs)))
4045 ;
4046 else if (POINTER_TYPE_P (TREE_TYPE (arg))
4047 && POINTER_TYPE_P (TREE_VALUE (targs)))
4048 ;
4049 else if (TREE_CODE (TREE_TYPE (arg))
4050 != TREE_CODE (TREE_VALUE (targs)))
4051 return false;
4052 targs = TREE_CHAIN (targs);
4053 }
4054 if (targs && !VOID_TYPE_P (TREE_VALUE (targs)))
4055 return false;
4056 return true;
4057}
4058
4059/* Return true when STMT is builtins call to CLASS. */
4060
4061bool
4062gimple_call_builtin_p (gimple stmt, enum built_in_class klass)
4063{
4064 tree fndecl;
4065 if (is_gimple_call (stmt)
4066 && (fndecl = gimple_call_fndecl (stmt)) != NULL_TREE
4067 && DECL_BUILT_IN_CLASS (fndecl) == klass)
4068 return validate_call (stmt, fndecl);
4069 return false;
4070}
4071
4072/* Return true when STMT is builtins call to CODE of CLASS. */
c54c785d
JH
4073
4074bool
4075gimple_call_builtin_p (gimple stmt, enum built_in_function code)
4076{
4077 tree fndecl;
3626621a
RB
4078 if (is_gimple_call (stmt)
4079 && (fndecl = gimple_call_fndecl (stmt)) != NULL_TREE
4080 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
4081 && DECL_FUNCTION_CODE (fndecl) == code)
4082 return validate_call (stmt, fndecl);
4083 return false;
c54c785d
JH
4084}
4085
edcdea5b
NF
4086/* Return true if STMT clobbers memory. STMT is required to be a
4087 GIMPLE_ASM. */
4088
4089bool
4090gimple_asm_clobbers_memory_p (const_gimple stmt)
4091{
4092 unsigned i;
4093
4094 for (i = 0; i < gimple_asm_nclobbers (stmt); i++)
4095 {
4096 tree op = gimple_asm_clobber_op (stmt, i);
4097 if (strcmp (TREE_STRING_POINTER (TREE_VALUE (op)), "memory") == 0)
4098 return true;
4099 }
4100
4101 return false;
4102}
475b8f37
DN
4103
4104
7a300452
AM
4105/* Return true if the conversion from INNER_TYPE to OUTER_TYPE is a
4106 useless type conversion, otherwise return false.
4107
4108 This function implicitly defines the middle-end type system. With
4109 the notion of 'a < b' meaning that useless_type_conversion_p (a, b)
4110 holds and 'a > b' meaning that useless_type_conversion_p (b, a) holds,
4111 the following invariants shall be fulfilled:
4112
4113 1) useless_type_conversion_p is transitive.
4114 If a < b and b < c then a < c.
4115
4116 2) useless_type_conversion_p is not symmetric.
4117 From a < b does not follow a > b.
4118
4119 3) Types define the available set of operations applicable to values.
4120 A type conversion is useless if the operations for the target type
4121 is a subset of the operations for the source type. For example
4122 casts to void* are useless, casts from void* are not (void* can't
4123 be dereferenced or offsetted, but copied, hence its set of operations
4124 is a strict subset of that of all other data pointer types). Casts
4125 to const T* are useless (can't be written to), casts from const T*
4126 to T* are not. */
4127
4128bool
4129useless_type_conversion_p (tree outer_type, tree inner_type)
4130{
4131 /* Do the following before stripping toplevel qualifiers. */
4132 if (POINTER_TYPE_P (inner_type)
4133 && POINTER_TYPE_P (outer_type))
4134 {
4135 /* Do not lose casts between pointers to different address spaces. */
4136 if (TYPE_ADDR_SPACE (TREE_TYPE (outer_type))
4137 != TYPE_ADDR_SPACE (TREE_TYPE (inner_type)))
4138 return false;
4139 }
4140
4141 /* From now on qualifiers on value types do not matter. */
4142 inner_type = TYPE_MAIN_VARIANT (inner_type);
4143 outer_type = TYPE_MAIN_VARIANT (outer_type);
4144
4145 if (inner_type == outer_type)
4146 return true;
4147
4148 /* If we know the canonical types, compare them. */
4149 if (TYPE_CANONICAL (inner_type)
4150 && TYPE_CANONICAL (inner_type) == TYPE_CANONICAL (outer_type))
4151 return true;
4152
4153 /* Changes in machine mode are never useless conversions unless we
4154 deal with aggregate types in which case we defer to later checks. */
4155 if (TYPE_MODE (inner_type) != TYPE_MODE (outer_type)
4156 && !AGGREGATE_TYPE_P (inner_type))
4157 return false;
4158
4159 /* If both the inner and outer types are integral types, then the
4160 conversion is not necessary if they have the same mode and
4161 signedness and precision, and both or neither are boolean. */
4162 if (INTEGRAL_TYPE_P (inner_type)
4163 && INTEGRAL_TYPE_P (outer_type))
4164 {
4165 /* Preserve changes in signedness or precision. */
4166 if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
4167 || TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
4168 return false;
4169
4170 /* Preserve conversions to/from BOOLEAN_TYPE if types are not
4171 of precision one. */
4172 if (((TREE_CODE (inner_type) == BOOLEAN_TYPE)
4173 != (TREE_CODE (outer_type) == BOOLEAN_TYPE))
4174 && TYPE_PRECISION (outer_type) != 1)
4175 return false;
4176
4177 /* We don't need to preserve changes in the types minimum or
4178 maximum value in general as these do not generate code
4179 unless the types precisions are different. */
4180 return true;
4181 }
4182
4183 /* Scalar floating point types with the same mode are compatible. */
4184 else if (SCALAR_FLOAT_TYPE_P (inner_type)
4185 && SCALAR_FLOAT_TYPE_P (outer_type))
4186 return true;
4187
4188 /* Fixed point types with the same mode are compatible. */
4189 else if (FIXED_POINT_TYPE_P (inner_type)
4190 && FIXED_POINT_TYPE_P (outer_type))
4191 return true;
4192
4193 /* We need to take special care recursing to pointed-to types. */
4194 else if (POINTER_TYPE_P (inner_type)
4195 && POINTER_TYPE_P (outer_type))
4196 {
4197 /* Do not lose casts to function pointer types. */
4198 if ((TREE_CODE (TREE_TYPE (outer_type)) == FUNCTION_TYPE
4199 || TREE_CODE (TREE_TYPE (outer_type)) == METHOD_TYPE)
4200 && !(TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE
4201 || TREE_CODE (TREE_TYPE (inner_type)) == METHOD_TYPE))
4202 return false;
4203
4204 /* We do not care for const qualification of the pointed-to types
4205 as const qualification has no semantic value to the middle-end. */
4206
4207 /* Otherwise pointers/references are equivalent. */
4208 return true;
4209 }
4210
4211 /* Recurse for complex types. */
4212 else if (TREE_CODE (inner_type) == COMPLEX_TYPE
4213 && TREE_CODE (outer_type) == COMPLEX_TYPE)
4214 return useless_type_conversion_p (TREE_TYPE (outer_type),
4215 TREE_TYPE (inner_type));
4216
4217 /* Recurse for vector types with the same number of subparts. */
4218 else if (TREE_CODE (inner_type) == VECTOR_TYPE
4219 && TREE_CODE (outer_type) == VECTOR_TYPE
4220 && TYPE_PRECISION (inner_type) == TYPE_PRECISION (outer_type))
4221 return useless_type_conversion_p (TREE_TYPE (outer_type),
4222 TREE_TYPE (inner_type));
4223
4224 else if (TREE_CODE (inner_type) == ARRAY_TYPE
4225 && TREE_CODE (outer_type) == ARRAY_TYPE)
4226 {
4227 /* Preserve string attributes. */
4228 if (TYPE_STRING_FLAG (inner_type) != TYPE_STRING_FLAG (outer_type))
4229 return false;
4230
4231 /* Conversions from array types with unknown extent to
4232 array types with known extent are not useless. */
4233 if (!TYPE_DOMAIN (inner_type)
4234 && TYPE_DOMAIN (outer_type))
4235 return false;
4236
4237 /* Nor are conversions from array types with non-constant size to
4238 array types with constant size or to different size. */
4239 if (TYPE_SIZE (outer_type)
4240 && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST
4241 && (!TYPE_SIZE (inner_type)
4242 || TREE_CODE (TYPE_SIZE (inner_type)) != INTEGER_CST
4243 || !tree_int_cst_equal (TYPE_SIZE (outer_type),
4244 TYPE_SIZE (inner_type))))
4245 return false;
4246
4247 /* Check conversions between arrays with partially known extents.
4248 If the array min/max values are constant they have to match.
4249 Otherwise allow conversions to unknown and variable extents.
4250 In particular this declares conversions that may change the
4251 mode to BLKmode as useless. */
4252 if (TYPE_DOMAIN (inner_type)
4253 && TYPE_DOMAIN (outer_type)
4254 && TYPE_DOMAIN (inner_type) != TYPE_DOMAIN (outer_type))
4255 {
4256 tree inner_min = TYPE_MIN_VALUE (TYPE_DOMAIN (inner_type));
4257 tree outer_min = TYPE_MIN_VALUE (TYPE_DOMAIN (outer_type));
4258 tree inner_max = TYPE_MAX_VALUE (TYPE_DOMAIN (inner_type));
4259 tree outer_max = TYPE_MAX_VALUE (TYPE_DOMAIN (outer_type));
4260
4261 /* After gimplification a variable min/max value carries no
4262 additional information compared to a NULL value. All that
4263 matters has been lowered to be part of the IL. */
4264 if (inner_min && TREE_CODE (inner_min) != INTEGER_CST)
4265 inner_min = NULL_TREE;
4266 if (outer_min && TREE_CODE (outer_min) != INTEGER_CST)
4267 outer_min = NULL_TREE;
4268 if (inner_max && TREE_CODE (inner_max) != INTEGER_CST)
4269 inner_max = NULL_TREE;
4270 if (outer_max && TREE_CODE (outer_max) != INTEGER_CST)
4271 outer_max = NULL_TREE;
4272
4273 /* Conversions NULL / variable <- cst are useless, but not
4274 the other way around. */
4275 if (outer_min
4276 && (!inner_min
4277 || !tree_int_cst_equal (inner_min, outer_min)))
4278 return false;
4279 if (outer_max
4280 && (!inner_max
4281 || !tree_int_cst_equal (inner_max, outer_max)))
4282 return false;
4283 }
4284
4285 /* Recurse on the element check. */
4286 return useless_type_conversion_p (TREE_TYPE (outer_type),
4287 TREE_TYPE (inner_type));
4288 }
4289
4290 else if ((TREE_CODE (inner_type) == FUNCTION_TYPE
4291 || TREE_CODE (inner_type) == METHOD_TYPE)
4292 && TREE_CODE (inner_type) == TREE_CODE (outer_type))
4293 {
4294 tree outer_parm, inner_parm;
4295
4296 /* If the return types are not compatible bail out. */
4297 if (!useless_type_conversion_p (TREE_TYPE (outer_type),
4298 TREE_TYPE (inner_type)))
4299 return false;
4300
4301 /* Method types should belong to a compatible base class. */
4302 if (TREE_CODE (inner_type) == METHOD_TYPE
4303 && !useless_type_conversion_p (TYPE_METHOD_BASETYPE (outer_type),
4304 TYPE_METHOD_BASETYPE (inner_type)))
4305 return false;
4306
4307 /* A conversion to an unprototyped argument list is ok. */
4308 if (!prototype_p (outer_type))
4309 return true;
4310
4311 /* If the unqualified argument types are compatible the conversion
4312 is useless. */
4313 if (TYPE_ARG_TYPES (outer_type) == TYPE_ARG_TYPES (inner_type))
4314 return true;
4315
4316 for (outer_parm = TYPE_ARG_TYPES (outer_type),
4317 inner_parm = TYPE_ARG_TYPES (inner_type);
4318 outer_parm && inner_parm;
4319 outer_parm = TREE_CHAIN (outer_parm),
4320 inner_parm = TREE_CHAIN (inner_parm))
4321 if (!useless_type_conversion_p
4322 (TYPE_MAIN_VARIANT (TREE_VALUE (outer_parm)),
4323 TYPE_MAIN_VARIANT (TREE_VALUE (inner_parm))))
4324 return false;
4325
4326 /* If there is a mismatch in the number of arguments the functions
4327 are not compatible. */
4328 if (outer_parm || inner_parm)
4329 return false;
4330
4331 /* Defer to the target if necessary. */
4332 if (TYPE_ATTRIBUTES (inner_type) || TYPE_ATTRIBUTES (outer_type))
4333 return comp_type_attributes (outer_type, inner_type) != 0;
4334
4335 return true;
4336 }
4337
4338 /* For aggregates we rely on TYPE_CANONICAL exclusively and require
4339 explicit conversions for types involving to be structurally
4340 compared types. */
4341 else if (AGGREGATE_TYPE_P (inner_type)
4342 && TREE_CODE (inner_type) == TREE_CODE (outer_type))
4343 return false;
4344
4345 return false;
4346}
4347
4348/* Return true if a conversion from either type of TYPE1 and TYPE2
4349 to the other is not required. Otherwise return false. */
4350
4351bool
4352types_compatible_p (tree type1, tree type2)
4353{
4354 return (type1 == type2
4355 || (useless_type_conversion_p (type1, type2)
4356 && useless_type_conversion_p (type2, type1)));
4357}
4358
80560f95
AM
4359/* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE. */
4360
4361void
4362dump_decl_set (FILE *file, bitmap set)
4363{
4364 if (set)
4365 {
4366 bitmap_iterator bi;
4367 unsigned i;
4368
4369 fprintf (file, "{ ");
4370
4371 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
4372 {
4373 fprintf (file, "D.%u", i);
4374 fprintf (file, " ");
4375 }
4376
4377 fprintf (file, "}");
4378 }
4379 else
4380 fprintf (file, "NIL");
4381}
7a300452 4382
1df9f5a9
AM
4383/* Given SSA_NAMEs NAME1 and NAME2, return true if they are candidates for
4384 coalescing together, false otherwise.
4385
4386 This must stay consistent with var_map_base_init in tree-ssa-live.c. */
4387
4388bool
4389gimple_can_coalesce_p (tree name1, tree name2)
4390{
4391 /* First check the SSA_NAME's associated DECL. We only want to
4392 coalesce if they have the same DECL or both have no associated DECL. */
4393 tree var1 = SSA_NAME_VAR (name1);
4394 tree var2 = SSA_NAME_VAR (name2);
4395 var1 = (var1 && (!VAR_P (var1) || !DECL_IGNORED_P (var1))) ? var1 : NULL_TREE;
4396 var2 = (var2 && (!VAR_P (var2) || !DECL_IGNORED_P (var2))) ? var2 : NULL_TREE;
4397 if (var1 != var2)
4398 return false;
4399
4400 /* Now check the types. If the types are the same, then we should
4401 try to coalesce V1 and V2. */
4402 tree t1 = TREE_TYPE (name1);
4403 tree t2 = TREE_TYPE (name2);
4404 if (t1 == t2)
4405 return true;
4406
4407 /* If the types are not the same, check for a canonical type match. This
4408 (for example) allows coalescing when the types are fundamentally the
4409 same, but just have different names.
4410
4411 Note pointer types with different address spaces may have the same
4412 canonical type. Those are rejected for coalescing by the
4413 types_compatible_p check. */
4414 if (TYPE_CANONICAL (t1)
4415 && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2)
4416 && types_compatible_p (t1, t2))
4417 return true;
4418
4419 return false;
4420}
726a989a 4421#include "gt-gimple.h"
This page took 2.457028 seconds and 5 git commands to generate.