OSDN Git Service

* ipa-pure-const.c, ipa-reference.c, ipa-reference.h,
[pf3gnuchains/gcc-fork.git] / gcc / ipa-pure-const.c
1 /* Callgraph based analysis of static variables.
2    Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3    Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 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 the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA.  */
21
22 /* This file mark functions as being either const (TREE_READONLY) or
23    pure (DECL_IS_PURE).
24
25    This must be run after inlining decisions have been made since
26    otherwise, the local sets will not contain information that is
27    consistent with post inlined state.  The global sets are not prone
28    to this problem since they are by definition transitive.  */
29
30 /* The code in this module is called by the ipa pass manager. It
31    should be one of the later passes since it's information is used by
32    the rest of the compilation. */
33
34 #include "config.h"
35 #include "system.h"
36 #include "coretypes.h"
37 #include "tm.h"
38 #include "tree.h"
39 #include "tree-flow.h"
40 #include "tree-inline.h"
41 #include "tree-pass.h"
42 #include "langhooks.h"
43 #include "pointer-set.h"
44 #include "ggc.h"
45 #include "ipa-utils.h"
46 #include "c-common.h"
47 #include "tree-gimple.h"
48 #include "cgraph.h"
49 #include "output.h"
50 #include "flags.h"
51 #include "timevar.h"
52 #include "diagnostic.h"
53 #include "langhooks.h"
54
55 static struct pointer_set_t *visited_nodes;
56
57 /* Lattice values for const and pure functions.  Everything starts out
58    being const, then may drop to pure and then neither depending on
59    what is found.  */
60 enum pure_const_state_e
61 {
62   IPA_CONST,
63   IPA_PURE,
64   IPA_NEITHER
65 };
66
67 /* Holder inserted into the ipa_dfs_info aux field to hold the
68    const_state.  */
69 struct funct_state_d 
70 {
71   enum pure_const_state_e pure_const_state;
72   bool state_set_in_source;
73 };
74
75 typedef struct funct_state_d * funct_state;
76
77 /* Return the function state from NODE.  */ 
78
79 static inline funct_state
80 get_function_state (struct cgraph_node *node)
81 {
82   struct ipa_dfs_info * info = node->aux;
83   return info->aux;
84 }
85
86 /* Check to see if the use (or definition when CHECHING_WRITE is true) 
87    variable T is legal in a function that is either pure or const.  */
88
89 static inline void 
90 check_decl (funct_state local, 
91             tree t, bool checking_write)
92 {
93   /* If the variable has the "used" attribute, treat it as if it had a
94      been touched by the devil.  */
95   if (lookup_attribute ("used", DECL_ATTRIBUTES (t)))
96     {
97       local->pure_const_state = IPA_NEITHER;
98       return;
99     }
100
101   /* Do not want to do anything with volatile except mark any
102      function that uses one to be not const or pure.  */
103   if (TREE_THIS_VOLATILE (t)) 
104     { 
105       local->pure_const_state = IPA_NEITHER;
106       return;
107     }
108
109   /* Do not care about a local automatic that is not static.  */
110   if (!TREE_STATIC (t) && !DECL_EXTERNAL (t))
111     return;
112
113   /* Since we have dealt with the locals and params cases above, if we
114      are CHECKING_WRITE, this cannot be a pure or constant
115      function.  */
116   if (checking_write) 
117     local->pure_const_state = IPA_NEITHER;
118
119   if (DECL_EXTERNAL (t) || TREE_PUBLIC (t))
120     {
121       /* If the front end set the variable to be READONLY and
122          constant, we can allow this variable in pure or const
123          functions but the scope is too large for our analysis to set
124          these bits ourselves.  */
125       
126       if (TREE_READONLY (t)
127           && DECL_INITIAL (t)
128           && is_gimple_min_invariant (DECL_INITIAL (t)))
129         ; /* Read of a constant, do not change the function state.  */
130       else 
131         {
132           /* Just a regular read.  */
133           if (local->pure_const_state == IPA_CONST)
134             local->pure_const_state = IPA_PURE;
135         }
136     }
137   
138   /* Compilation level statics can be read if they are readonly
139      variables.  */
140   if (TREE_READONLY (t))
141     return;
142
143   /* Just a regular read.  */
144   if (local->pure_const_state == IPA_CONST)
145     local->pure_const_state = IPA_PURE;
146 }
147
148 /* If T is a VAR_DECL check to see if it is an allowed reference.  */
149
150 static void
151 check_operand (funct_state local, 
152                tree t, bool checking_write)
153 {
154   if (!t) return;
155
156   if (TREE_CODE (t) == VAR_DECL)
157     check_decl (local, t, checking_write); 
158 }
159
160 /* Examine tree T for references.  */
161
162 static void
163 check_tree (funct_state local, tree t, bool checking_write)
164 {
165   if ((TREE_CODE (t) == EXC_PTR_EXPR) || (TREE_CODE (t) == FILTER_EXPR))
166     return;
167
168   while (TREE_CODE (t) == REALPART_EXPR 
169          || TREE_CODE (t) == IMAGPART_EXPR
170          || handled_component_p (t))
171     {
172       if (TREE_CODE (t) == ARRAY_REF)
173         check_operand (local, TREE_OPERAND (t, 1), false);
174       t = TREE_OPERAND (t, 0);
175     }
176
177   /* The bottom of an indirect reference can only be read, not
178      written.  */
179   if (INDIRECT_REF_P (t))
180     {
181       check_tree (local, TREE_OPERAND (t, 0), false);
182       
183       /* Any indirect reference that occurs on the lhs
184          disqualifies the function from being pure or const. Any
185          indirect reference that occurs on the rhs disqualifies
186          the function from being const.  */
187       if (checking_write) 
188         local->pure_const_state = IPA_NEITHER;
189       else 
190         if (local->pure_const_state == IPA_CONST)
191           local->pure_const_state = IPA_PURE;
192     }
193
194   if (SSA_VAR_P (t))
195     check_operand (local, t, checking_write);
196 }
197
198 /* Scan tree T to see if there are any addresses taken in within T.  */
199
200 static void 
201 look_for_address_of (funct_state local, tree t)
202 {
203   if (TREE_CODE (t) == ADDR_EXPR)
204     {
205       tree x = get_base_var (t);
206       if (TREE_CODE (x) == VAR_DECL) 
207         {
208           check_decl (local, x, false);
209           
210           /* Taking the address of something appears to be reasonable
211              in PURE code.  Not allowed in const.  */
212           if (local->pure_const_state == IPA_CONST)
213             local->pure_const_state = IPA_PURE;
214         }
215     }
216 }
217
218 /* Check to see if T is a read or address of operation on a var we are
219    interested in analyzing.  LOCAL is passed in to get access to its
220    bit vectors.  */
221
222 static void
223 check_rhs_var (funct_state local, tree t)
224 {
225   look_for_address_of (local, t);
226
227   /* Memcmp and strlen can both trap and they are declared pure.  */
228   if (tree_could_trap_p (t)
229       && local->pure_const_state == IPA_CONST)
230     local->pure_const_state = IPA_PURE;
231
232   check_tree(local, t, false);
233 }
234
235 /* Check to see if T is an assignment to a var we are interested in
236    analyzing.  LOCAL is passed in to get access to its bit vectors. */
237
238 static void
239 check_lhs_var (funct_state local, tree t)
240 {
241   /* Memcmp and strlen can both trap and they are declared pure.
242      Which seems to imply that we can apply the same rule here.  */
243   if (tree_could_trap_p (t)
244       && local->pure_const_state == IPA_CONST)
245     local->pure_const_state = IPA_PURE;
246     
247   check_tree(local, t, true);
248 }
249
250 /* This is a scaled down version of get_asm_expr_operands from
251    tree_ssa_operands.c.  The version there runs much later and assumes
252    that aliasing information is already available. Here we are just
253    trying to find if the set of inputs and outputs contain references
254    or address of operations to local static variables.  STMT is the
255    actual asm statement.  */
256
257 static void
258 get_asm_expr_operands (funct_state local, tree stmt)
259 {
260   int noutputs = list_length (ASM_OUTPUTS (stmt));
261   const char **oconstraints
262     = (const char **) alloca ((noutputs) * sizeof (const char *));
263   int i;
264   tree link;
265   const char *constraint;
266   bool allows_mem, allows_reg, is_inout;
267   
268   for (i=0, link = ASM_OUTPUTS (stmt); link; ++i, link = TREE_CHAIN (link))
269     {
270       oconstraints[i] = constraint
271         = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
272       parse_output_constraint (&constraint, i, 0, 0,
273                                &allows_mem, &allows_reg, &is_inout);
274       
275       check_lhs_var (local, TREE_VALUE (link));
276     }
277
278   for (link = ASM_INPUTS (stmt); link; link = TREE_CHAIN (link))
279     {
280       constraint
281         = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
282       parse_input_constraint (&constraint, 0, 0, noutputs, 0,
283                               oconstraints, &allows_mem, &allows_reg);
284       
285       check_rhs_var (local, TREE_VALUE (link));
286     }
287   
288   for (link = ASM_CLOBBERS (stmt); link; link = TREE_CHAIN (link))
289     if (simple_cst_equal(TREE_VALUE (link), memory_identifier_string) == 1) 
290       /* Abandon all hope, ye who enter here. */
291       local->pure_const_state = IPA_NEITHER;
292
293   if (ASM_VOLATILE_P (stmt))
294     local->pure_const_state = IPA_NEITHER;
295 }
296
297 /* Check the parameters of a function call to CALL_EXPR to see if
298    there are any references in the parameters that are not allowed for
299    pure or const functions.  Also check to see if this is either an
300    indirect call, a call outside the compilation unit, or has special
301    attributes that may also effect the purity.  The CALL_EXPR node for
302    the entire call expression.  */
303
304 static void
305 check_call (funct_state local, tree call_expr) 
306 {
307   int flags = call_expr_flags(call_expr);
308   tree operand_list = TREE_OPERAND (call_expr, 1);
309   tree operand;
310   tree callee_t = get_callee_fndecl (call_expr);
311   struct cgraph_node* callee;
312   enum availability avail = AVAIL_NOT_AVAILABLE;
313
314   for (operand = operand_list;
315        operand != NULL_TREE;
316        operand = TREE_CHAIN (operand))
317     {
318       tree argument = TREE_VALUE (operand);
319       check_rhs_var (local, argument);
320     }
321   
322   /* The const and pure flags are set by a variety of places in the
323      compiler (including here).  If someone has already set the flags
324      for the callee, (such as for some of the builtins) we will use
325      them, otherwise we will compute our own information. 
326   
327      Const and pure functions have less clobber effects than other
328      functions so we process these first.  Otherwise if it is a call
329      outside the compilation unit or an indirect call we punt.  This
330      leaves local calls which will be processed by following the call
331      graph.  */  
332   if (callee_t)
333     {
334       callee = cgraph_node(callee_t);
335       avail = cgraph_function_body_availability (callee);
336
337       /* When bad things happen to bad functions, they cannot be const
338          or pure.  */
339       if (setjmp_call_p (callee_t))
340         local->pure_const_state = IPA_NEITHER;
341
342       if (DECL_BUILT_IN_CLASS (callee_t) == BUILT_IN_NORMAL)
343         switch (DECL_FUNCTION_CODE (callee_t))
344           {
345           case BUILT_IN_LONGJMP:
346           case BUILT_IN_NONLOCAL_GOTO:
347             local->pure_const_state = IPA_NEITHER;
348             break;
349           default:
350             break;
351           }
352     }
353
354   /* The callee is either unknown (indirect call) or there is just no
355      scannable code for it (external call) .  We look to see if there
356      are any bits available for the callee (such as by declaration or
357      because it is builtin) and process solely on the basis of those
358      bits. */
359   if (avail == AVAIL_NOT_AVAILABLE || avail == AVAIL_OVERWRITABLE)
360     {
361       if (flags & ECF_PURE) 
362         {
363           if (local->pure_const_state == IPA_CONST)
364             local->pure_const_state = IPA_PURE;
365         }
366       else 
367         local->pure_const_state = IPA_NEITHER;
368     }
369   else
370     {
371       /* We have the code and we will scan it for the effects. */
372       if (flags & ECF_PURE) 
373         {
374           if (local->pure_const_state == IPA_CONST)
375             local->pure_const_state = IPA_PURE;
376         }
377     }
378 }
379
380 /* TP is the part of the tree currently under the microscope.
381    WALK_SUBTREES is part of the walk_tree api but is unused here.
382    DATA is cgraph_node of the function being walked.  */
383
384 /* FIXME: When this is converted to run over SSA form, this code
385    should be converted to use the operand scanner.  */
386
387 static tree
388 scan_function (tree *tp, 
389                       int *walk_subtrees, 
390                       void *data)
391 {
392   struct cgraph_node *fn = data;
393   tree t = *tp;
394   funct_state local = get_function_state (fn);
395
396   switch (TREE_CODE (t))  
397     {
398     case VAR_DECL:
399       if (DECL_INITIAL (t))
400         walk_tree (&DECL_INITIAL (t), scan_function, fn, visited_nodes);
401       *walk_subtrees = 0;
402       break;
403
404     case MODIFY_EXPR:
405       {
406         /* First look on the lhs and see what variable is stored to */
407         tree lhs = TREE_OPERAND (t, 0);
408         tree rhs = TREE_OPERAND (t, 1);
409         check_lhs_var (local, lhs);
410
411         /* For the purposes of figuring out what the cast affects */
412
413         /* Next check the operands on the rhs to see if they are ok. */
414         switch (TREE_CODE_CLASS (TREE_CODE (rhs))) 
415           {
416           case tcc_binary:          
417             {
418               tree op0 = TREE_OPERAND (rhs, 0);
419               tree op1 = TREE_OPERAND (rhs, 1);
420               check_rhs_var (local, op0);
421               check_rhs_var (local, op1);
422             }
423             break;
424           case tcc_unary:
425             {
426               tree op0 = TREE_OPERAND (rhs, 0);
427               check_rhs_var (local, op0);
428             }
429
430             break;
431           case tcc_reference:
432             check_rhs_var (local, rhs);
433             break;
434           case tcc_declaration:
435             check_rhs_var (local, rhs);
436             break;
437           case tcc_expression:
438             switch (TREE_CODE (rhs)) 
439               {
440               case ADDR_EXPR:
441                 check_rhs_var (local, rhs);
442                 break;
443               case CALL_EXPR: 
444                 check_call (local, rhs);
445                 break;
446               default:
447                 break;
448               }
449             break;
450           default:
451             break;
452           }
453         *walk_subtrees = 0;
454       }
455       break;
456
457     case ADDR_EXPR:
458       /* This case is here to find addresses on rhs of constructors in
459          decl_initial of static variables. */
460       check_rhs_var (local, t);
461       *walk_subtrees = 0;
462       break;
463
464     case LABEL_EXPR:
465       if (DECL_NONLOCAL (TREE_OPERAND (t, 0)))
466         /* Target of long jump. */
467         local->pure_const_state = IPA_NEITHER;
468       break;
469
470     case CALL_EXPR: 
471       check_call (local, t);
472       *walk_subtrees = 0;
473       break;
474       
475     case ASM_EXPR:
476       get_asm_expr_operands (local, t);
477       *walk_subtrees = 0;
478       break;
479       
480     default:
481       break;
482     }
483   return NULL;
484 }
485
486 /* This is the main routine for finding the reference patterns for
487    global variables within a function FN.  */
488
489 static void
490 analyze_function (struct cgraph_node *fn)
491 {
492   funct_state l = xcalloc (1, sizeof (struct funct_state_d));
493   tree decl = fn->decl;
494   struct ipa_dfs_info * w_info = fn->aux;
495
496   w_info->aux = l;
497
498   l->pure_const_state = IPA_CONST;
499   l->state_set_in_source = false;
500
501   /* If this is a volatile function, do not touch this unless it has
502      been marked as const or pure by the front end.  */
503   if (TREE_THIS_VOLATILE (decl))
504     {
505       l->pure_const_state = IPA_NEITHER;
506       return;
507     }
508
509   if (TREE_READONLY (decl))
510     {
511       l->pure_const_state = IPA_CONST;
512       l->state_set_in_source = true;
513     }
514   if (DECL_IS_PURE (decl))
515     {
516       l->pure_const_state = IPA_PURE;
517       l->state_set_in_source = true;
518     }
519
520   if (dump_file)
521     {
522       fprintf (dump_file, "\n local analysis of %s with initial value = %d\n ", 
523                cgraph_node_name (fn),
524                l->pure_const_state);
525     }
526   
527   if (!l->state_set_in_source)
528     {
529       struct function *this_cfun = DECL_STRUCT_FUNCTION (decl);
530       basic_block this_block;
531       
532       FOR_EACH_BB_FN (this_block, this_cfun)
533         {
534           block_stmt_iterator bsi;
535           for (bsi = bsi_start (this_block); !bsi_end_p (bsi); bsi_next (&bsi))
536             {
537               walk_tree (bsi_stmt_ptr (bsi), scan_function, 
538                          fn, visited_nodes);
539               if (l->pure_const_state == IPA_NEITHER) 
540                 return;
541             }
542         }
543
544       if (l->pure_const_state != IPA_NEITHER)
545         {
546           tree old_decl = current_function_decl;
547           /* Const functions cannot have back edges (an
548              indication of possible infinite loop side
549              effect.  */
550             
551           current_function_decl = fn->decl;
552
553           /* The C++ front end, has a tendency to some times jerk away
554              a function after it has created it.  This should have
555              been fixed.  */
556           gcc_assert (DECL_STRUCT_FUNCTION (fn->decl));
557           
558           push_cfun (DECL_STRUCT_FUNCTION (fn->decl));
559           
560           if (mark_dfs_back_edges ())
561             l->pure_const_state = IPA_NEITHER;
562           
563           current_function_decl = old_decl;
564           pop_cfun ();
565         }
566     }
567 }
568
569 \f
570 /* Produce the global information by preforming a transitive closure
571    on the local information that was produced by ipa_analyze_function
572    and ipa_analyze_variable.  */
573
574 static void
575 static_execute (void)
576 {
577   struct cgraph_node *node;
578   struct cgraph_node *w;
579   struct cgraph_node **order =
580     xcalloc (cgraph_n_nodes, sizeof (struct cgraph_node *));
581   int order_pos = order_pos = ipa_utils_reduced_inorder (order, true, false);
582   int i;
583   struct ipa_dfs_info * w_info;
584
585   if (!memory_identifier_string)
586     memory_identifier_string = build_string(7, "memory");
587
588   /* There are some shared nodes, in particular the initializers on
589      static declarations.  We do not need to scan them more than once
590      since all we would be interested in are the addressof
591      operations.  */
592   visited_nodes = pointer_set_create ();
593
594   /* Process all of the functions. 
595
596      We do not want to process any of the clones so we check that this
597      is a master clone.  However, we do NOT process any
598      AVAIL_OVERWRITABLE functions (these are never clones) we cannot
599      guarantee that what we learn about the one we see will be true
600      for the one that overriders it.
601   */
602   for (node = cgraph_nodes; node; node = node->next)
603     if (node->analyzed && cgraph_is_master_clone (node))
604       analyze_function (node);
605
606   pointer_set_destroy (visited_nodes);
607   visited_nodes = NULL;
608   if (dump_file)
609     {
610       dump_cgraph (dump_file);
611       ipa_utils_print_order(dump_file, "reduced", order, order_pos);
612     }
613
614   /* Propagate the local information thru the call graph to produce
615      the global information.  All the nodes within a cycle will have
616      the same info so we collapse cycles first.  Then we can do the
617      propagation in one pass from the leaves to the roots.  */
618   for (i = 0; i < order_pos; i++ )
619     {
620       enum pure_const_state_e pure_const_state = IPA_CONST;
621       node = order[i];
622
623       /* Find the worst state for any node in the cycle.  */
624       w = node;
625       while (w)
626         {
627           funct_state w_l = get_function_state (w);
628           if (pure_const_state < w_l->pure_const_state)
629             pure_const_state = w_l->pure_const_state;
630
631           if (pure_const_state == IPA_NEITHER) 
632             break;
633
634           if (!w_l->state_set_in_source)
635             {
636               struct cgraph_edge *e;
637               for (e = w->callees; e; e = e->next_callee) 
638                 {
639                   struct cgraph_node *y = e->callee;
640                   /* Only look at the master nodes and skip external nodes.  */
641                   y = cgraph_master_clone (y);
642                   if (y)
643                     {
644                       funct_state y_l = get_function_state (y);
645                       if (pure_const_state < y_l->pure_const_state)
646                         pure_const_state = y_l->pure_const_state;
647                       if (pure_const_state == IPA_NEITHER) 
648                         break;
649                     }
650                 }
651             }
652           w_info = w->aux;
653           w = w_info->next_cycle;
654         }
655
656       /* Copy back the region's pure_const_state which is shared by
657          all nodes in the region.  */
658       w = node;
659       while (w)
660         {
661           funct_state w_l = get_function_state (w);
662
663           /* All nodes within a cycle share the same info.  */
664           if (!w_l->state_set_in_source)
665             {
666               w_l->pure_const_state = pure_const_state;
667               switch (pure_const_state)
668                 {
669                 case IPA_CONST:
670                   TREE_READONLY (w->decl) = 1;
671                   if (dump_file)
672                     fprintf (dump_file, "Function found to be const: %s\n",  
673                              lang_hooks.decl_printable_name(w->decl, 2)); 
674                   break;
675                   
676                 case IPA_PURE:
677                   DECL_IS_PURE (w->decl) = 1;
678                   if (dump_file)
679                     fprintf (dump_file, "Function found to be pure: %s\n",  
680                              lang_hooks.decl_printable_name(w->decl, 2)); 
681                   break;
682                   
683                 default:
684                   break;
685                 }
686             }
687           w_info = w->aux;
688           w = w_info->next_cycle;
689         }
690     }
691
692   /* Cleanup. */
693   for (node = cgraph_nodes; node; node = node->next)
694     /* Get rid of the aux information.  */
695     if (node->aux)
696       {
697         free (node->aux);
698         node->aux = NULL;
699       }
700
701   free (order);
702 }
703
704 static bool
705 gate_pure_const (void)
706 {
707   return (flag_unit_at_a_time != 0 && flag_ipa_pure_const 
708           /* Don't bother doing anything if the program has errors.  */
709           && !(errorcount || sorrycount));
710 }
711
712 struct tree_opt_pass pass_ipa_pure_const =
713 {
714   "ipa-pure-const",                     /* name */
715   gate_pure_const,                      /* gate */
716   static_execute,                       /* execute */
717   NULL,                                 /* sub */
718   NULL,                                 /* next */
719   0,                                    /* static_pass_number */
720   TV_IPA_PURE_CONST,                    /* tv_id */
721   0,                                    /* properties_required */
722   0,                                    /* properties_provided */
723   0,                                    /* properties_destroyed */
724   0,                                    /* todo_flags_start */
725   0,                                    /* todo_flags_finish */
726   0                                     /* letter */
727 };
728
729