OSDN Git Service

2009-08-07 Martin Jambor <mjambor@suse.cz>
[pf3gnuchains/gcc-fork.git] / gcc / ipa-prop.h
1 /* Interprocedural analyses.
2    Copyright (C) 2005, 2007, 2008 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19
20 #ifndef IPA_PROP_H
21 #define IPA_PROP_H
22
23 #include "tree.h"
24 #include "vec.h"
25 #include "cgraph.h"
26
27 /* The following definitions and interfaces are used by
28    interprocedural analyses or parameters.  */
29
30 /* ipa-prop.c stuff (ipa-cp, indirect inlining):  */
31
32 /* A jump function for a callsite represents the values passed as actual
33    arguments of the callsite. There are three main types of values :
34    Formal - the caller's formal parameter is passed as an actual argument.
35    Constant - a constant is passed as an actual argument.
36    Unknown - neither of the above.
37    Integer and real constants are represented as IPA_JF_CONST.
38    Finally, IPA_JF_CONST_MEMBER_PTR stands for C++ member pointers
39    constants.  */
40 enum jump_func_type
41 {
42   IPA_JF_UNKNOWN = 0,  /* newly allocated and zeroed jump functions default */
43   IPA_JF_CONST,
44   IPA_JF_CONST_MEMBER_PTR,
45   IPA_JF_PASS_THROUGH
46 };
47
48 /* All formal parameters in the program have a lattice associated with it
49    computed by the interprocedural stage of IPCP.
50    There are three main values of the lattice:
51    IPA_TOP - unknown,
52    IPA_BOTTOM - non constant,
53    IPA_CONST_VALUE - simple scalar constant,
54    Cval of formal f will have a constant value if all callsites to this
55    function have the same constant value passed to f.
56    Integer and real constants are represented as IPA_CONST_VALUE.  */
57 enum ipa_lattice_type
58 {
59   IPA_BOTTOM,
60   IPA_CONST_VALUE,
61   IPA_TOP
62 };
63
64 /* Structure holding a C++ member pointer constant.  Holds a pointer to the
65    method and delta offset.  */
66 struct ipa_member_ptr_cst
67 {
68   tree pfn;
69   tree delta;
70 };
71
72 /* Represents a value of a jump function.  formal_id is used only in jump
73    function context and represents pass-through parameter (the formal parameter
74    of the caller is passed as argument).  constant represents the actual
75    constant in constant jump functions and member_cst holds constant c++ member
76    functions.  */
77 union jump_func_value
78 {
79   unsigned int formal_id;
80   tree constant;
81   struct ipa_member_ptr_cst member_cst;
82 };
83
84 /* A jump function for a callsite represents the values passed as actual
85    arguments of the callsite. See enum jump_func_type for the various
86    types of jump functions supported.  */
87 struct ipa_jump_func
88 {
89   enum jump_func_type type;
90   union jump_func_value value;
91 };
92
93 /* All formal parameters in the program have a cval computed by
94    the interprocedural stage of IPCP. See enum ipa_lattice_type for
95    the various types of lattices supported */
96 struct ipcp_lattice
97 {
98   enum ipa_lattice_type type;
99   tree constant;
100 };
101
102 /* Each instance of the following  structure describes a statement that calls a
103    function parameter.  Those referring  to statements within the same function
104    are linked in a list.  */
105 struct ipa_param_call_note
106 {
107   /* Linked list's next */
108   struct ipa_param_call_note *next;
109   /* Statement that contains the call to the parameter above.  */
110   gimple stmt;
111   /* Index of the parameter that is called.  */
112   unsigned int formal_id;
113   /* Expected number of executions: calculated in profile.c.  */
114   gcov_type count;
115   /* Expected frequency of executions within the function. see cgraph_edge in
116      cgraph.h for more on this. */
117   int frequency;
118   /* Depth of loop nest, 1 means no loop nest.  */
119   int loop_nest;
120   /* Set when we have already found the target to be a compile time constant
121      and turned this into an edge or when the note was found unusable for some
122      reason.  */
123   bool processed;
124 };
125
126 /* Structure describing a single formal parameter.  */
127 struct ipa_param_descriptor
128 {
129   /* IPA-CP lattice.  */
130   struct ipcp_lattice ipcp_lattice;
131   /* PARAM_DECL of this parameter.  */
132   tree decl;
133   /* Whether the value parameter has been modified within the function.  */
134   unsigned modified : 1;
135   /* Whether the parameter has been used as a call destination. */
136   unsigned called : 1;
137 };
138
139 /* ipa_node_params stores information related to formal parameters of functions
140    and some other information for interprocedural passes that operate on
141    parameters (such as ipa-cp).  */
142 struct ipa_node_params
143 {
144   /* Number of formal parameters of this function.  When set to 0,
145      this function's parameters would not be analyzed by the different
146      stages of IPA CP.  */
147   int param_count;
148   /* Pointer to an array of structures describing individual formal
149      parameters.  */
150   struct ipa_param_descriptor *params;
151   /* List of structures enumerating calls to a formal parameter.  */
152   struct ipa_param_call_note *param_calls;
153   /* Only for versioned nodes this field would not be NULL,
154      it points to the node that IPA cp cloned from.  */
155   struct cgraph_node *ipcp_orig_node;
156   /* Meaningful only for original functions.  Expresses the
157      ratio between the direct calls and sum of all invocations of
158      this function (given by profiling info).  It is used to calculate
159      the profiling information of the original function and the versioned
160      one.  */
161   gcov_type count_scale;
162
163   /* Whether this function is called with variable number of actual
164      arguments.  */
165   unsigned called_with_var_arguments : 1;
166   /* Whether the modification analysis has already been performed. */
167   unsigned modification_analysis_done : 1;
168   /* Whether the param uses analysis has already been performed.  */
169   unsigned uses_analysis_done : 1;
170   /* Whether the function is enqueued in an ipa_func_list.  */
171   unsigned node_enqueued : 1;
172 };
173
174 /* ipa_node_params access functions.  Please use these to access fields that
175    are or will be shared among various passes.  */
176
177 /* Set the number of formal parameters. */
178
179 static inline void
180 ipa_set_param_count (struct ipa_node_params *info, int count)
181 {
182   info->param_count = count;
183 }
184
185 /* Return the number of formal parameters. */
186
187 static inline int
188 ipa_get_param_count (struct ipa_node_params *info)
189 {
190   return info->param_count;
191 }
192
193 /* Return the declaration of Ith formal parameter of the function corresponding
194    to INFO.  Note there is no setter function as this array is built just once
195    using ipa_initialize_node_params. */
196
197 static inline tree
198 ipa_get_param (struct ipa_node_params *info, int i)
199 {
200   return info->params[i].decl;
201 }
202
203 /* Return the modification flag corresponding to the Ith formal parameter of
204    the function associated with INFO.  Note that there is no setter method as
205    the goal is to set all flags when building the array in
206    ipa_detect_param_modifications.  */
207
208 static inline bool
209 ipa_is_param_modified (struct ipa_node_params *info, int i)
210 {
211   return info->params[i].modified;
212 }
213
214 /* Return the called flag corresponding to the Ith formal parameter of the
215    function associated with INFO.  Note that there is no setter method as the
216    goal is to set all flags when building the array in
217    ipa_detect_called_params.  */
218
219 static inline bool
220 ipa_is_param_called (struct ipa_node_params *info, int i)
221 {
222   return info->params[i].called;
223 }
224
225 /* Flag this node as having callers with variable number of arguments.  */
226
227 static inline void
228 ipa_set_called_with_variable_arg (struct ipa_node_params *info)
229 {
230   info->called_with_var_arguments = 1;
231 }
232
233 /* Have we detected this node was called with variable number of arguments? */
234
235 static inline bool
236 ipa_is_called_with_var_arguments (struct ipa_node_params *info)
237 {
238   return info->called_with_var_arguments;
239 }
240
241
242
243 /* ipa_edge_args stores information related to a callsite and particularly
244    its arguments. It is pointed to by a field in the
245    callsite's corresponding cgraph_edge.  */
246 struct ipa_edge_args
247 {
248   /* Number of actual arguments in this callsite.  When set to 0,
249      this callsite's parameters would not be analyzed by the different
250      stages of IPA CP.  */
251   int argument_count;
252   /* Array of the callsite's jump function of each parameter.  */
253   struct ipa_jump_func *jump_functions;
254 };
255
256 /* ipa_edge_args access functions.  Please use these to access fields that
257    are or will be shared among various passes.  */
258
259 /* Set the number of actual arguments. */
260
261 static inline void
262 ipa_set_cs_argument_count (struct ipa_edge_args *args, int count)
263 {
264   args->argument_count = count;
265 }
266
267 /* Return the number of actual arguments. */
268
269 static inline int
270 ipa_get_cs_argument_count (struct ipa_edge_args *args)
271 {
272   return args->argument_count;
273 }
274
275 /* Returns a pointer to the jump function for the ith argument.  Please note
276    there is no setter function as jump functions are all set up in
277    ipa_compute_jump_functions. */
278
279 static inline struct ipa_jump_func *
280 ipa_get_ith_jump_func (struct ipa_edge_args *args, int i)
281 {
282   return &args->jump_functions[i];
283 }
284
285 /* Vectors need to have typedefs of structures.  */
286 typedef struct ipa_node_params ipa_node_params_t;
287 typedef struct ipa_edge_args ipa_edge_args_t;
288
289 /* Types of vectors holding the infos.  */
290 DEF_VEC_O (ipa_node_params_t);
291 DEF_VEC_ALLOC_O (ipa_node_params_t, heap);
292 DEF_VEC_O (ipa_edge_args_t);
293 DEF_VEC_ALLOC_O (ipa_edge_args_t, heap);
294
295 /* Vector where the parameter infos are actually stored. */
296 extern VEC (ipa_node_params_t, heap) *ipa_node_params_vector;
297 /* Vector where the parameter infos are actually stored. */
298 extern VEC (ipa_edge_args_t, heap) *ipa_edge_args_vector;
299
300 /* Return the associated parameter/argument info corresponding to the given
301    node/edge.  */
302 #define IPA_NODE_REF(NODE) (VEC_index (ipa_node_params_t, \
303                                        ipa_node_params_vector, (NODE)->uid))
304 #define IPA_EDGE_REF(EDGE) (VEC_index (ipa_edge_args_t, \
305                                        ipa_edge_args_vector, (EDGE)->uid))
306 /* This macro checks validity of index returned by
307    ipa_get_param_decl_index function.  */
308 #define IS_VALID_JUMP_FUNC_INDEX(I) ((I) != -1)
309
310 /* Creating and freeing ipa_node_params and ipa_edge_args.  */
311 void ipa_create_all_node_params (void);
312 void ipa_create_all_edge_args (void);
313 void ipa_free_edge_args_substructures (struct ipa_edge_args *);
314 void ipa_free_node_params_substructures (struct ipa_node_params *);
315 void ipa_free_all_node_params (void);
316 void ipa_free_all_edge_args (void);
317 void free_all_ipa_structures_after_ipa_cp (void);
318 void free_all_ipa_structures_after_iinln (void);
319 void ipa_register_cgraph_hooks (void);
320
321 /* This function ensures the array of node param infos is big enough to
322    accommodate a structure for all nodes and reallocates it if not.  */
323
324 static inline void
325 ipa_check_create_node_params (void)
326 {
327   if (!ipa_node_params_vector)
328     ipa_node_params_vector = VEC_alloc (ipa_node_params_t, heap,
329                                         cgraph_max_uid);
330
331   if (VEC_length (ipa_node_params_t, ipa_node_params_vector)
332       <= (unsigned) cgraph_max_uid)
333     VEC_safe_grow_cleared (ipa_node_params_t, heap,
334                            ipa_node_params_vector, cgraph_max_uid + 1);
335 }
336
337 /* This function ensures the array of edge arguments infos is big enough to
338    accommodate a structure for all edges and reallocates it if not.  */
339
340 static inline void
341 ipa_check_create_edge_args (void)
342 {
343   if (!ipa_edge_args_vector)
344     ipa_edge_args_vector = VEC_alloc (ipa_edge_args_t, heap,
345                                       cgraph_edge_max_uid);
346
347   if (VEC_length (ipa_edge_args_t, ipa_edge_args_vector)
348       <=  (unsigned) cgraph_edge_max_uid)
349     VEC_safe_grow_cleared (ipa_edge_args_t, heap, ipa_edge_args_vector,
350                            cgraph_edge_max_uid + 1);
351 }
352
353 /* Returns true if the array of edge infos is large enough to accommodate an
354    info for EDGE.  The main purpose of this function is that debug dumping
355    function can check info availability without causing reallocations.  */
356
357 static inline bool
358 ipa_edge_args_info_available_for_edge_p (struct cgraph_edge *edge)
359 {
360   return ((unsigned) edge->uid < VEC_length (ipa_edge_args_t,
361                                              ipa_edge_args_vector));
362 }
363
364 /* A function list element.  It is used to create a temporary worklist used in
365    the propagation stage of IPCP. (can be used for more IPA optimizations)  */
366 struct ipa_func_list
367 {
368   struct cgraph_node *node;
369   struct ipa_func_list *next;
370 };
371
372 /* ipa_func_list interface.  */
373 struct ipa_func_list *ipa_init_func_list (void);
374 void ipa_push_func_to_list_1 (struct ipa_func_list **, struct cgraph_node *,
375                               struct ipa_node_params *);
376 struct cgraph_node *ipa_pop_func_from_list (struct ipa_func_list **);
377
378 /* Add cgraph NODE to the worklist WL if it is not already in one.  */
379
380 static inline void
381 ipa_push_func_to_list (struct ipa_func_list **wl, struct cgraph_node *node)
382 {
383   struct ipa_node_params *info = IPA_NODE_REF (node);
384
385   if (!info->node_enqueued)
386     ipa_push_func_to_list_1 (wl, node, info);
387 }
388
389 /* Callsite related calculations.  */
390 void ipa_compute_jump_functions (struct cgraph_edge *);
391 void ipa_count_arguments (struct cgraph_edge *);
392
393 /* Function formal parameters related computations.  */
394 void ipa_initialize_node_params (struct cgraph_node *node);
395 void ipa_detect_param_modifications (struct cgraph_node *);
396 void ipa_analyze_params_uses (struct cgraph_node *);
397 bool ipa_propagate_indirect_call_infos (struct cgraph_edge *cs,
398                                         VEC (cgraph_edge_p, heap) **new_edges);
399
400 /* Debugging interface.  */
401 void ipa_print_node_params (FILE *, struct cgraph_node *node);
402 void ipa_print_all_params (FILE *);
403 void ipa_print_node_jump_functions (FILE *f, struct cgraph_node *node);
404 void ipa_print_all_jump_functions (FILE * f);
405
406 /* Structure to describe transformations of formal parameters and actual
407    arguments.  Each instance describes one new parameter and they are meant to
408    be stored in a vector.  Additionally, most users will probably want to store
409    adjustments about parameters that are being removed altogether so that SSA
410    names belonging to them can be replaced by SSA names of an artificial
411    variable.  */
412 struct ipa_parm_adjustment
413 {
414   /* The original PARM_DECL itself, helpful for processing of the body of the
415      function itself.  Intended for traversing function bodies.
416      ipa_modify_formal_parameters, ipa_modify_call_arguments and
417      ipa_combine_adjustments ignore this and use base_index.
418      ipa_modify_formal_parameters actually sets this.  */
419   tree base;
420
421   /* Type of the new parameter.  However, if by_ref is true, the real type will
422      be a pointer to this type.  */
423   tree type;
424
425   /* The new declaration when creating/replacing a parameter.  Created by
426      ipa_modify_formal_parameters, useful for functions modifying the body
427      accordingly. */
428   tree reduction;
429
430   /* New declaration of a substitute variable that we may use to replace all
431      non-default-def ssa names when a parm decl is going away.  */
432   tree new_ssa_base;
433
434   /* If non-NULL and the original parameter is to be removed (copy_param below
435      is NULL), this is going to be its nonlocalized vars value.  */
436   tree nonlocal_value;
437
438   /* Offset into the original parameter (for the cases when the new parameter
439      is a component of an original one).  */
440   HOST_WIDE_INT offset;
441
442   /* Zero based index of the original parameter this one is based on.  (ATM
443      there is no way to insert a new parameter out of the blue because there is
444      no need but if it arises the code can be easily exteded to do so.)  */
445   int base_index;
446
447   /* This new parameter is an unmodified parameter at index base_index. */
448   unsigned copy_param : 1;
449
450   /* This adjustment describes a parameter that is about to be removed
451      completely.  Most users will probably need to book keep those so that they
452      don't leave behinfd any non default def ssa names belonging to them.  */
453   unsigned remove_param : 1;
454
455   /* The parameter is to be passed by reference.  */
456   unsigned by_ref : 1;
457 };
458
459 typedef struct ipa_parm_adjustment ipa_parm_adjustment_t;
460 DEF_VEC_O (ipa_parm_adjustment_t);
461 DEF_VEC_ALLOC_O (ipa_parm_adjustment_t, heap);
462
463 typedef VEC (ipa_parm_adjustment_t, heap) *ipa_parm_adjustment_vec;
464
465 VEC(tree, heap) *ipa_get_vector_of_formal_parms (tree fndecl);
466 void ipa_modify_formal_parameters (tree fndecl, ipa_parm_adjustment_vec,
467                                    const char *);
468 void ipa_modify_call_arguments (struct cgraph_edge *, gimple,
469                                 ipa_parm_adjustment_vec);
470 ipa_parm_adjustment_vec ipa_combine_adjustments (ipa_parm_adjustment_vec,
471                                                  ipa_parm_adjustment_vec);
472 void ipa_dump_param_adjustments (FILE *, ipa_parm_adjustment_vec, tree);
473
474 /* From tree-sra.c:  */
475 bool build_ref_for_offset (tree *, tree, HOST_WIDE_INT, tree, bool);
476
477 #endif /* IPA_PROP_H */