OSDN Git Service

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