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