17eb6db7365e01b0f0b4eab736627a1036cf00eb
[gcc.git] / gcc / tree-flow-inline.h
1 /* Inline functions for tree-flow.h
2 Copyright (C) 2001, 2003 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #ifndef _TREE_FLOW_INLINE_H
23 #define _TREE_FLOW_INLINE_H 1
24
25 /* Inline functions for manipulating various data structures defined in
26 tree-flow.h. See tree-flow.h for documentation. */
27
28 /* Return the variable annotation for T, which must be a _DECL node.
29 Return NULL if the variable annotation doesn't already exist. */
30 static inline var_ann_t
31 var_ann (tree t)
32 {
33 #if defined ENABLE_CHECKING
34 if (t == NULL_TREE
35 || !DECL_P (t)
36 || (t->common.ann
37 && t->common.ann->common.type != VAR_ANN))
38 abort ();
39 #endif
40
41 return (var_ann_t) t->common.ann;
42 }
43
44 /* Return the variable annotation for T, which must be a _DECL node.
45 Create the variable annotation if it doesn't exist. */
46 static inline var_ann_t
47 get_var_ann (tree var)
48 {
49 var_ann_t ann = var_ann (var);
50 return (ann) ? ann : create_var_ann (var);
51 }
52
53
54 /* Return the constant annotation for T, which must be a _CST node.
55 Return NULL if the constant annotation doesn't already exist. */
56 static inline cst_ann_t
57 cst_ann (tree t)
58 {
59 #if defined ENABLE_CHECKING
60 if (TREE_CODE_CLASS (TREE_CODE (t)) != 'c'
61 || (t->common.ann
62 && t->common.ann->common.type != CST_ANN))
63 abort ();
64 #endif
65
66 return (cst_ann_t) t->common.ann;
67 }
68
69 /* Return the constant annotation for T, which must be a _CST node.
70 Create the constant annotation if it doesn't exist. */
71 static inline cst_ann_t
72 get_cst_ann (tree var)
73 {
74 cst_ann_t ann = cst_ann (var);
75 return (ann) ? ann : create_cst_ann (var);
76 }
77
78 /* Return the expression annotation for T, which must be an expression
79 node. Return NULL if the expression annotation doesn't already
80 exist. */
81 static inline expr_ann_t
82 expr_ann (tree t)
83 {
84 #if defined ENABLE_CHECKING
85 if (!EXPR_P (t)
86 || (t->common.ann
87 && t->common.ann->common.type != EXPR_ANN))
88 abort ();
89 #endif
90
91 return (expr_ann_t) t->common.ann;
92 }
93
94 /* Return the expression annotation for T, which must be an expression
95 node. Create the expression annotation if it doesn't exist. */
96 static inline expr_ann_t
97 get_expr_ann (tree t)
98 {
99 expr_ann_t ann = expr_ann (t);
100 return (ann) ? ann : create_expr_ann (t);
101 }
102
103 /* Return the statement annotation for T, which must be a statement
104 node. Return NULL if the statement annotation doesn't exist. */
105 static inline stmt_ann_t
106 stmt_ann (tree t)
107 {
108 #if defined ENABLE_CHECKING
109 if (!is_gimple_stmt (t))
110 abort ();
111 #endif
112
113 return (stmt_ann_t) t->common.ann;
114 }
115
116 /* Return the statement annotation for T, which must be a statement
117 node. Create the statement annotation if it doesn't exist. */
118 static inline stmt_ann_t
119 get_stmt_ann (tree stmt)
120 {
121 stmt_ann_t ann = stmt_ann (stmt);
122 return (ann) ? ann : create_stmt_ann (stmt);
123 }
124
125
126 /* Return the annotation type for annotation ANN. */
127 static inline enum tree_ann_type
128 ann_type (tree_ann ann)
129 {
130 return ann->common.type;
131 }
132
133 /* Return the basic block for statement T. */
134 static inline basic_block
135 bb_for_stmt (tree t)
136 {
137 stmt_ann_t ann = stmt_ann (t);
138 return ann ? ann->bb : NULL;
139 }
140
141 /* Return the may_aliases varray for variable VAR, or NULL if it has
142 no may aliases. */
143 static inline varray_type
144 may_aliases (tree var)
145 {
146 var_ann_t ann = var_ann (var);
147 return ann ? ann->may_aliases : NULL;
148 }
149
150 /* Return true if VAR has a hidden use, false if it does not. */
151 static inline bool
152 has_hidden_use (tree var)
153 {
154 var_ann_t ann = var_ann (var);
155 return ann ? ann->has_hidden_use : false;
156 }
157
158 /* Set the hidden use flag on VAR. */
159 static inline void
160 set_has_hidden_use (tree var)
161 {
162 var_ann_t ann = var_ann (var);
163 if (ann == NULL)
164 ann = create_var_ann (var);
165 ann->has_hidden_use = 1;
166 }
167
168 /* Return the line number for EXPR, or return -1 if we have no line
169 number information for it. */
170 static inline int
171 get_lineno (tree expr)
172 {
173 if (expr == NULL_TREE)
174 return -1;
175
176 if (TREE_CODE (expr) == COMPOUND_EXPR)
177 expr = TREE_OPERAND (expr, 0);
178
179 if (! EXPR_LOCUS (expr))
180 return -1;
181
182 return EXPR_LINENO (expr);
183 }
184
185 /* Return the file name for EXPR, or return "???" if we have no
186 filename information. */
187 static inline const char *
188 get_filename (tree expr)
189 {
190 if (expr == NULL_TREE)
191 return "???";
192
193 if (TREE_CODE (expr) == COMPOUND_EXPR)
194 expr = TREE_OPERAND (expr, 0);
195
196 if (EXPR_LOCUS (expr) && EXPR_FILENAME (expr))
197 return EXPR_FILENAME (expr);
198 else
199 return "???";
200 }
201
202 /* Mark statement T as modified. */
203 static inline void
204 modify_stmt (tree t)
205 {
206 stmt_ann_t ann = stmt_ann (t);
207 if (ann == NULL)
208 ann = create_stmt_ann (t);
209 ann->modified = 1;
210 }
211
212 /* Mark statement T as unmodified. */
213 static inline void
214 unmodify_stmt (tree t)
215 {
216 stmt_ann_t ann = stmt_ann (t);
217 if (ann == NULL)
218 ann = create_stmt_ann (t);
219 ann->modified = 0;
220 }
221
222 /* Return true if T is marked as modified, false otherwise. */
223 static inline bool
224 stmt_modified_p (tree t)
225 {
226 stmt_ann_t ann = stmt_ann (t);
227
228 /* Note that if the statement doesn't yet have an annotation, we consider it
229 modified. This will force the next call to get_stmt_operands to scan the
230 statement. */
231 return ann ? ann->modified : true;
232 }
233
234 /* Return the definitions present in ANN, a statement annotation.
235 Return NULL if this annotation contains no definitions. */
236 static inline def_optype
237 get_def_ops (stmt_ann_t ann)
238 {
239 return ann ? ann->def_ops : NULL;
240 }
241
242 /* Return the uses present in ANN, a statement annotation.
243 Return NULL if this annotation contains no uses. */
244 static inline use_optype
245 get_use_ops (stmt_ann_t ann)
246 {
247 return ann ? ann->use_ops : NULL;
248 }
249
250 /* Return the virtual may-defs present in ANN, a statement
251 annotation.
252 Return NULL if this annotation contains no virtual may-defs. */
253 static inline v_may_def_optype
254 get_v_may_def_ops (stmt_ann_t ann)
255 {
256 return ann ? ann->v_may_def_ops : NULL;
257 }
258
259 /* Return the virtual uses present in ANN, a statement annotation.
260 Return NULL if this annotation contains no virtual uses. */
261 static inline vuse_optype
262 get_vuse_ops (stmt_ann_t ann)
263 {
264 return ann ? ann->vuse_ops : NULL;
265 }
266
267 /* Return the virtual must-defs present in ANN, a statement
268 annotation. Return NULL if this annotation contains no must-defs.*/
269 static inline v_must_def_optype
270 get_v_must_def_ops (stmt_ann_t ann)
271 {
272 return ann ? ann->v_must_def_ops : NULL;
273 }
274
275 /* Return a pointer to the tree that is at INDEX in the USES array. */
276 static inline tree *
277 get_use_op_ptr (use_optype uses, unsigned int index)
278 {
279 #ifdef ENABLE_CHECKING
280 if (index >= uses->num_uses)
281 abort();
282 #endif
283 return uses->uses[index];
284 }
285
286 /* Return a pointer to the tree that is at INDEX in the DEFS array. */
287 static inline tree *
288 get_def_op_ptr (def_optype defs, unsigned int index)
289 {
290 #ifdef ENABLE_CHECKING
291 if (index >= defs->num_defs)
292 abort();
293 #endif
294 return defs->defs[index];
295 }
296
297
298 /* Return a pointer to the tree that is the V_MAY_DEF_RESULT for the V_MAY_DEF
299 at INDEX in the V_MAY_DEFS array. */
300 static inline tree *
301 get_v_may_def_result_ptr(v_may_def_optype v_may_defs, unsigned int index)
302 {
303 #ifdef ENABLE_CHECKING
304 if (index >= v_may_defs->num_v_may_defs)
305 abort();
306 #endif
307 return &(v_may_defs->v_may_defs[index * 2]);
308 }
309
310 /* Return a pointer to the tree that is the V_MAY_DEF_OP for the V_MAY_DEF at
311 INDEX in the V_MAY_DEFS array. */
312 static inline tree *
313 get_v_may_def_op_ptr(v_may_def_optype v_may_defs, unsigned int index)
314 {
315 #ifdef ENABLE_CHECKING
316 if (index >= v_may_defs->num_v_may_defs)
317 abort();
318 #endif
319 return &(v_may_defs->v_may_defs[index * 2 + 1]);
320 }
321
322 /* Return a pointer to the tree that is at INDEX in the VUSES array. */
323 static inline tree *
324 get_vuse_op_ptr(vuse_optype vuses, unsigned int index)
325 {
326 #ifdef ENABLE_CHECKING
327 if (index >= vuses->num_vuses)
328 abort();
329 #endif
330 return &(vuses->vuses[index]);
331 }
332
333 /* Return a pointer to the tree that is the V_MUST_DEF_OP for the
334 V_MUST_DEF at INDEX in the V_MUST_DEFS array. */
335 static inline tree *
336 get_v_must_def_op_ptr (v_must_def_optype v_must_defs, unsigned int index)
337 {
338 #ifdef ENABLE_CHECKING
339 if (index >= v_must_defs->num_v_must_defs)
340 abort();
341 #endif
342 return &(v_must_defs->v_must_defs[index]);
343 }
344
345 /* Mark the beginning of changes to the SSA operands for STMT. */
346 static inline void
347 start_ssa_stmt_operands (tree stmt ATTRIBUTE_UNUSED)
348 {
349 #ifdef ENABLE_CHECKING
350 verify_start_operands (stmt);
351 #endif
352 }
353
354 /* Return the bitmap of addresses taken by STMT, or NULL if it takes
355 no addresses. */
356 static inline bitmap
357 addresses_taken (tree stmt)
358 {
359 stmt_ann_t ann = stmt_ann (stmt);
360 return ann ? ann->addresses_taken : NULL;
361 }
362
363 /* Return the immediate uses of STMT, or NULL if this information is
364 not computed. */
365 static dataflow_t
366 get_immediate_uses (tree stmt)
367 {
368 stmt_ann_t ann = stmt_ann (stmt);
369 return ann ? ann->df : NULL;
370 }
371
372 /* Return the number of immediate uses present in the dataflow
373 information at DF. */
374 static inline int
375 num_immediate_uses (dataflow_t df)
376 {
377 varray_type imm;
378
379 if (!df)
380 return 0;
381
382 imm = df->immediate_uses;
383 if (!imm)
384 return df->uses[1] ? 2 : 1;
385
386 return VARRAY_ACTIVE_SIZE (imm) + 2;
387 }
388
389 /* Return the tree that is at NUM in the immediate use DF array. */
390 static inline tree
391 immediate_use (dataflow_t df, int num)
392 {
393 if (!df)
394 return NULL_TREE;
395
396 #ifdef ENABLE_CHECKING
397 if (num >= num_immediate_uses (df))
398 abort ();
399 #endif
400 if (num < 2)
401 return df->uses[num];
402 return VARRAY_TREE (df->immediate_uses, num - 2);
403 }
404
405 /* Return the basic_block annotation for BB. */
406 static inline bb_ann_t
407 bb_ann (basic_block bb)
408 {
409 return (bb_ann_t)bb->tree_annotations;
410 }
411
412 /* Return the PHI nodes for basic block BB, or NULL if there are no
413 PHI nodes. */
414 static inline tree
415 phi_nodes (basic_block bb)
416 {
417 if (bb->index < 0)
418 return NULL;
419 return bb_ann (bb)->phi_nodes;
420 }
421
422 /* Set list of phi nodes of a basic block BB to L. */
423
424 static inline void
425 set_phi_nodes (basic_block bb, tree l)
426 {
427 tree phi;
428
429 bb_ann (bb)->phi_nodes = l;
430 for (phi = l; phi; phi = TREE_CHAIN (phi))
431 set_bb_for_stmt (phi, bb);
432 }
433
434 /* Return the phi index number for an edge. */
435 static inline int
436 phi_arg_from_edge (tree phi, edge e)
437 {
438 int i;
439 #if defined ENABLE_CHECKING
440 if (!phi || TREE_CODE (phi) != PHI_NODE)
441 abort();
442 #endif
443
444 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
445 if (PHI_ARG_EDGE (phi, i) == e)
446 return i;
447
448 return -1;
449 }
450
451
452 /* Return the phi argument number for an edge. */
453 static inline struct phi_arg_d *
454 phi_element_for_edge (tree phi, edge e)
455 {
456 int i;
457
458 i = phi_arg_from_edge (phi, e);
459 if (i != -1)
460 return &(PHI_ARG_ELT (phi, i));
461 else
462 return (struct phi_arg_d *)NULL;
463 }
464
465 /* ----------------------------------------------------------------------- */
466
467 /* Return true if T is an executable statement. */
468 static inline bool
469 is_exec_stmt (tree t)
470 {
471 return (t && !IS_EMPTY_STMT (t) && t != error_mark_node);
472 }
473
474
475 /* Return true if this stmt can be the target of a control transfer stmt such
476 as a goto. */
477 static inline bool
478 is_label_stmt (tree t)
479 {
480 if (t)
481 switch (TREE_CODE (t))
482 {
483 case LABEL_DECL:
484 case LABEL_EXPR:
485 case CASE_LABEL_EXPR:
486 return true;
487 default:
488 return false;
489 }
490 return false;
491 }
492
493 /* Return true if we may propagate ORIG into DEST, false otherwise. */
494 static inline bool
495 may_propagate_copy (tree dest, tree orig)
496 {
497 /* FIXME. GIMPLE is allowing pointer assignments and comparisons of
498 pointers that have different alias sets. This means that these
499 pointers will have different memory tags associated to them.
500
501 If we allow copy propagation in these cases, statements de-referencing
502 the new pointer will now have a reference to a different memory tag
503 with potentially incorrect SSA information.
504
505 This was showing up in libjava/java/util/zip/ZipFile.java with code
506 like:
507
508 struct java.io.BufferedInputStream *T.660;
509 struct java.io.BufferedInputStream *T.647;
510 struct java.io.InputStream *is;
511 struct java.io.InputStream *is.662;
512 [ ... ]
513 T.660 = T.647;
514 is = T.660; <-- This ought to be type-casted
515 is.662 = is;
516
517 Also, f/name.c exposed a similar problem with a COND_EXPR predicate
518 that was causing DOM to generate and equivalence with two pointers of
519 alias-incompatible types:
520
521 struct _ffename_space *n;
522 struct _ffename *ns;
523 [ ... ]
524 if (n == ns)
525 goto lab;
526 ...
527 lab:
528 return n;
529
530 I think that GIMPLE should emit the appropriate type-casts. For the
531 time being, blocking copy-propagation in these cases is the safe thing
532 to do. */
533 if (TREE_CODE (dest) == SSA_NAME
534 && TREE_CODE (orig) == SSA_NAME
535 && POINTER_TYPE_P (TREE_TYPE (dest))
536 && POINTER_TYPE_P (TREE_TYPE (orig)))
537 {
538 tree mt_dest = var_ann (SSA_NAME_VAR (dest))->type_mem_tag;
539 tree mt_orig = var_ann (SSA_NAME_VAR (orig))->type_mem_tag;
540 if (mt_dest && mt_orig && mt_dest != mt_orig)
541 return false;
542 }
543
544 /* If the destination is a SSA_NAME for a virtual operand, then we have
545 some special cases to handle. */
546 if (TREE_CODE (dest) == SSA_NAME && !is_gimple_reg (dest))
547 {
548 /* If both operands are SSA_NAMEs referring to virtual operands, then
549 we can always propagate. */
550 if (TREE_CODE (orig) == SSA_NAME)
551 {
552 if (!is_gimple_reg (orig))
553 return true;
554
555 #ifdef ENABLE_CHECKING
556 /* If we have one real and one virtual operand, then something has
557 gone terribly wrong. */
558 if (is_gimple_reg (orig))
559 abort ();
560 #endif
561 }
562
563 /* We have a "copy" from something like a constant into a virtual
564 operand. Reject these. */
565 return false;
566 }
567
568 return (!SSA_NAME_OCCURS_IN_ABNORMAL_PHI (dest)
569 && (TREE_CODE (orig) != SSA_NAME
570 || !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (orig))
571 && !DECL_HARD_REGISTER (SSA_NAME_VAR (dest)));
572 }
573
574 /* Set the default definition for VAR to DEF. */
575 static inline void
576 set_default_def (tree var, tree def)
577 {
578 var_ann_t ann = var_ann (var);
579 if (ann == NULL)
580 ann = create_var_ann (var);
581 ann->default_def = def;
582 }
583
584 /* Return the default definition for variable VAR, or NULL if none
585 exists. */
586 static inline tree
587 default_def (tree var)
588 {
589 var_ann_t ann = var_ann (var);
590 return ann ? ann->default_def : NULL_TREE;
591 }
592
593 /* PHI nodes should contain only ssa_names and invariants. A test
594 for ssa_name is definitely simpler; don't let invalid contents
595 slip in in the meantime. */
596
597 static inline bool
598 phi_ssa_name_p (tree t)
599 {
600 if (TREE_CODE (t) == SSA_NAME)
601 return true;
602 #ifdef ENABLE_CHECKING
603 if (!is_gimple_min_invariant (t))
604 abort ();
605 #endif
606 return false;
607 }
608
609 /* ----------------------------------------------------------------------- */
610
611 /* Return a block_stmt_iterator that points to beginning of basic
612 block BB. */
613 static inline block_stmt_iterator
614 bsi_start (basic_block bb)
615 {
616 block_stmt_iterator bsi;
617 if (bb->stmt_list)
618 bsi.tsi = tsi_start (bb->stmt_list);
619 else
620 {
621 #ifdef ENABLE_CHECKING
622 if (bb->index >= 0)
623 abort ();
624 #endif
625 bsi.tsi.ptr = NULL;
626 bsi.tsi.container = NULL;
627 }
628 bsi.bb = bb;
629 return bsi;
630 }
631
632 /* Return a block statement iterator that points to the end of basic
633 block BB. */
634 static inline block_stmt_iterator
635 bsi_last (basic_block bb)
636 {
637 block_stmt_iterator bsi;
638 if (bb->stmt_list)
639 bsi.tsi = tsi_last (bb->stmt_list);
640 else
641 {
642 #ifdef ENABLE_CHECKING
643 if (bb->index >= 0)
644 abort ();
645 #endif
646 bsi.tsi.ptr = NULL;
647 bsi.tsi.container = NULL;
648 }
649 bsi.bb = bb;
650 return bsi;
651 }
652
653 /* Return true if block statement iterator I has reached the end of
654 the basic block. */
655 static inline bool
656 bsi_end_p (block_stmt_iterator i)
657 {
658 return tsi_end_p (i.tsi);
659 }
660
661 /* Modify block statement iterator I so that it is at the next
662 statement in the basic block. */
663 static inline void
664 bsi_next (block_stmt_iterator *i)
665 {
666 tsi_next (&i->tsi);
667 }
668
669 /* Modify block statement iterator I so that it is at the previous
670 statement in the basic block. */
671 static inline void
672 bsi_prev (block_stmt_iterator *i)
673 {
674 tsi_prev (&i->tsi);
675 }
676
677 /* Return the statement that block statement iterator I is currently
678 at. */
679 static inline tree
680 bsi_stmt (block_stmt_iterator i)
681 {
682 return tsi_stmt (i.tsi);
683 }
684
685 /* Return a pointer to the statement that block statement iterator I
686 is currently at. */
687 static inline tree *
688 bsi_stmt_ptr (block_stmt_iterator i)
689 {
690 return tsi_stmt_ptr (i.tsi);
691 }
692
693 /* Return true if VAR may be aliased. */
694 static inline bool
695 may_be_aliased (tree var)
696 {
697 return (TREE_ADDRESSABLE (var)
698 || decl_function_context (var) != current_function_decl);
699 }
700
701 /* Return true if VAR is a clobbered by function calls. */
702 static inline bool
703 is_call_clobbered (tree var)
704 {
705 return needs_to_live_in_memory (var)
706 || bitmap_bit_p (call_clobbered_vars, var_ann (var)->uid);
707 }
708
709 /* Mark variable VAR as being clobbered by function calls. */
710 static inline void
711 mark_call_clobbered (tree var)
712 {
713 var_ann_t ann = var_ann (var);
714 /* Call-clobbered variables need to live in memory. */
715 DECL_NEEDS_TO_LIVE_IN_MEMORY_INTERNAL (var) = 1;
716 bitmap_set_bit (call_clobbered_vars, ann->uid);
717 }
718
719 /* Mark variable VAR as being non-addressable. */
720 static inline void
721 mark_non_addressable (tree var)
722 {
723 bitmap_clear_bit (call_clobbered_vars, var_ann (var)->uid);
724 DECL_NEEDS_TO_LIVE_IN_MEMORY_INTERNAL (var) = 0;
725 TREE_ADDRESSABLE (var) = 0;
726 }
727
728 #endif /* _TREE_FLOW_INLINE_H */