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