OSDN Git Service

PR tree-optimization/15991
[pf3gnuchains/gcc-fork.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 = PHI_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 last label in
633    block BB.  */
634
635 static inline block_stmt_iterator
636 bsi_after_labels (basic_block bb)
637 {
638   block_stmt_iterator bsi;
639   tree_stmt_iterator next;
640
641   bsi.bb = bb;
642
643   if (!bb->stmt_list)
644     {
645 #ifdef ENABLE_CHECKING
646       if (bb->index >= 0)
647         abort ();
648 #endif
649       bsi.tsi.ptr = NULL;
650       bsi.tsi.container = NULL;
651       return bsi;
652     }
653
654   bsi.tsi = tsi_start (bb->stmt_list);
655   if (tsi_end_p (bsi.tsi))
656     return bsi;
657
658   /* Ensure that there are some labels.  The rationale is that we want
659      to insert after the bsi that is returned, and these insertions should
660      be placed at the start of the basic block.  This would not work if the
661      first statement was not label; rather fail here than enable the user
662      proceed in wrong way.  */
663   if (TREE_CODE (tsi_stmt (bsi.tsi)) != LABEL_EXPR)
664     abort ();
665
666   next = bsi.tsi;
667   tsi_next (&next);
668
669   while (!tsi_end_p (next)
670          && TREE_CODE (tsi_stmt (next)) == LABEL_EXPR)
671     {
672       bsi.tsi = next;
673       tsi_next (&next);
674     }
675
676   return bsi;
677 }
678
679 /* Return a block statement iterator that points to the end of basic
680    block BB.  */
681 static inline block_stmt_iterator
682 bsi_last (basic_block bb)
683 {
684   block_stmt_iterator bsi;
685   if (bb->stmt_list)
686     bsi.tsi = tsi_last (bb->stmt_list);
687   else
688     {
689 #ifdef ENABLE_CHECKING
690       if (bb->index >= 0)
691         abort ();
692 #endif
693       bsi.tsi.ptr = NULL;
694       bsi.tsi.container = NULL;
695     }
696   bsi.bb = bb;
697   return bsi;
698 }
699
700 /* Return true if block statement iterator I has reached the end of
701    the basic block.  */
702 static inline bool
703 bsi_end_p (block_stmt_iterator i)
704 {
705   return tsi_end_p (i.tsi);
706 }
707
708 /* Modify block statement iterator I so that it is at the next
709    statement in the basic block.  */
710 static inline void
711 bsi_next (block_stmt_iterator *i)
712 {
713   tsi_next (&i->tsi);
714 }
715
716 /* Modify block statement iterator I so that it is at the previous
717    statement in the basic block.  */
718 static inline void
719 bsi_prev (block_stmt_iterator *i)
720 {
721   tsi_prev (&i->tsi);
722 }
723
724 /* Return the statement that block statement iterator I is currently
725    at.  */
726 static inline tree
727 bsi_stmt (block_stmt_iterator i)
728 {
729   return tsi_stmt (i.tsi);
730 }
731
732 /* Return a pointer to the statement that block statement iterator I
733    is currently at.  */
734 static inline tree *
735 bsi_stmt_ptr (block_stmt_iterator i)
736 {
737   return tsi_stmt_ptr (i.tsi);
738 }
739
740 /* Return true if VAR may be aliased.  */
741 static inline bool
742 may_be_aliased (tree var)
743 {
744   return (TREE_ADDRESSABLE (var)
745           || decl_function_context (var) != current_function_decl);
746 }
747
748 /* Return true if VAR is a clobbered by function calls.  */
749 static inline bool
750 is_call_clobbered (tree var)
751 {
752   return needs_to_live_in_memory (var)
753          || bitmap_bit_p (call_clobbered_vars, var_ann (var)->uid);
754 }
755
756 /* Mark variable VAR as being clobbered by function calls.  */
757 static inline void
758 mark_call_clobbered (tree var)
759 {
760   var_ann_t ann = var_ann (var);
761   /* Call-clobbered variables need to live in memory.  */
762   DECL_NEEDS_TO_LIVE_IN_MEMORY_INTERNAL (var) = 1;
763   bitmap_set_bit (call_clobbered_vars, ann->uid);
764 }
765
766 /* Mark variable VAR as being non-addressable.  */
767 static inline void
768 mark_non_addressable (tree var)
769 {
770   bitmap_clear_bit (call_clobbered_vars, var_ann (var)->uid);
771   DECL_NEEDS_TO_LIVE_IN_MEMORY_INTERNAL (var) = 0;
772   TREE_ADDRESSABLE (var) = 0;
773 }
774
775 #endif /* _TREE_FLOW_INLINE_H  */