Merge from trunk @ 138209
[official-gcc.git] / gcc / ipa-prop.h
blob908a97e7797899753ee064e3acb24b6ef63716da
1 /* Interprocedural analyses.
2 Copyright (C) 2005, 2007, 2008 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 #ifndef IPA_PROP_H
21 #define IPA_PROP_H
23 #include "tree.h"
24 #include "vec.h"
25 #include "cgraph.h"
27 /* The following definitions and interfaces are used by
28 interprocedural analyses. */
30 /* A jump function for a callsite represents the values passed as actual
31 arguments of the callsite. There are three main types of values :
32 Formal - the caller's formal parameter is passed as an actual argument.
33 Constant - a constant is passed as an actual argument.
34 Unknown - neither of the above.
35 Integer and real constants are represented as IPA_CONST and Fortran
36 constants are represented as IPA_CONST_REF. Finally, IPA_CONST_MEMBER_PTR
37 stands for C++ member pointers constants. */
38 enum jump_func_type
40 IPA_UNKNOWN = 0, /* newly allocated and zeroed jump functions default */
41 IPA_CONST,
42 IPA_CONST_REF,
43 IPA_CONST_MEMBER_PTR,
44 IPA_PASS_THROUGH
47 /* All formal parameters in the program have a lattice associated with it
48 computed by the interprocedural stage of IPCP.
49 There are three main values of the lattice:
50 IPA_TOP - unknown,
51 IPA_BOTTOM - non constant,
52 IPA_CONST_VALUE - simple scalar constant,
53 Cval of formal f will have a constant value if all callsites to this
54 function have the same constant value passed to f.
55 Integer and real constants are represented as IPA_CONST and Fortran
56 constants are represented as IPA_CONST_REF. */
57 enum ipa_lattice_type
59 IPA_BOTTOM,
60 IPA_CONST_VALUE,
61 IPA_CONST_VALUE_REF,
62 IPA_TOP
65 /* Structure holding a C++ member pointer constant. Holds a pointer to the
66 method and delta offset. */
67 struct ipa_member_ptr_cst
69 tree pfn;
70 tree delta;
73 /* Represents a value of a jump function. formal_id is used only in jump
74 function context and represents pass-through parameter (the formal parameter
75 of the caller is passed as argument). constant represents the actual
76 constant in constant jump functions and member_cst holds constant c++ member
77 functions. */
78 union jump_func_value
80 unsigned int formal_id;
81 tree constant;
82 struct ipa_member_ptr_cst member_cst;
85 /* A jump function for a callsite represents the values passed as actual
86 arguments of the callsite. See enum jump_func_type for the various
87 types of jump functions supported. */
88 struct ipa_jump_func
90 enum jump_func_type type;
91 union jump_func_value value;
94 /* All formal parameters in the program have a cval computed by
95 the interprocedural stage of IPCP. See enum ipa_lattice_type for
96 the various types of lattices supported */
97 struct ipcp_lattice
99 enum ipa_lattice_type type;
100 tree constant;
103 /* Represent which DECL tree (or reference to such tree)
104 will be replaced by another tree while versioning. */
105 struct ipa_replace_map
107 /* The tree that will be replaced. */
108 tree old_tree;
109 /* The new (replacing) tree. */
110 tree new_tree;
111 /* True when a substitution should be done, false otherwise. */
112 bool replace_p;
113 /* True when we replace a reference to old_tree. */
114 bool ref_p;
117 /* ipa_param_flags contains various flags that describe how the associated
118 parameter is treated within a function. */
119 struct ipa_param_flags
121 /* Whether the value parameter has been modified within the function. */
122 unsigned modified : 1;
123 /* Whether the parameter has been used as a call destination. */
124 unsigned called : 1;
127 /* Each instance of the following structure describes a statement that calls a
128 function parameter. Those referring to statements within the same function
129 are linked in a list. */
130 struct ipa_param_call_note
132 /* Linked list's next */
133 struct ipa_param_call_note *next;
134 /* Statement that contains the call to the parameter above. */
135 gimple stmt;
136 /* Index of the parameter that is called. */
137 unsigned int formal_id;
138 /* Expected number of executions: calculated in profile.c. */
139 gcov_type count;
140 /* Expected frequency of executions within the function. see cgraph_edge in
141 cgraph.h for more on this. */
142 int frequency;
143 /* Depth of loop nest, 1 means no loop nest. */
144 int loop_nest;
145 /* Set when we have already found the target to be a compile time constant
146 and turned this into an edge or when the note was found unusable for some
147 reason. */
148 bool processed;
151 /* ipa_node_params stores information related to formal parameters of functions
152 and some other information for interprocedural passes that operate on
153 parameters (such as ipa-cp). */
154 struct ipa_node_params
156 /* Number of formal parameters of this function. When set to 0,
157 this function's parameters would not be analyzed by the different
158 stages of IPA CP. */
159 int param_count;
160 /* Array of lattices. */
161 struct ipcp_lattice *ipcp_lattices;
162 /* Mapping each parameter to its PARM_DECL tree. */
163 tree *param_decls;
164 /* Various flags describing individual parameters. */
165 struct ipa_param_flags *param_flags;
166 /* List of structures enumerating calls to a formal parameter. */
167 struct ipa_param_call_note *param_calls;
168 /* Only for versioned nodes this field would not be NULL,
169 it points to the node that IPA cp cloned from. */
170 struct cgraph_node *ipcp_orig_node;
171 /* Meaningful only for original functions. Expresses the
172 ratio between the direct calls and sum of all invocations of
173 this function (given by profiling info). It is used to calculate
174 the profiling information of the original function and the versioned
175 one. */
176 gcov_type count_scale;
178 /* Whether this function is called with variable number of actual
179 arguments. */
180 unsigned called_with_var_arguments : 1;
181 /* Whether the modification analysis has already been performed. */
182 unsigned modification_analysis_done : 1;
183 /* Whether the param uses analysis has already been performed. */
184 unsigned uses_analysis_done : 1;
187 /* ipa_node_params access functions. Please use these to access fields that
188 are or will be shared among various passes. */
190 /* Set the number of formal parameters. */
191 static inline void
192 ipa_set_param_count (struct ipa_node_params *info, int count)
194 info->param_count = count;
197 /* Return the number of formal parameters. */
198 static inline int
199 ipa_get_param_count (struct ipa_node_params *info)
201 return info->param_count;
204 /* Returns the declaration of ith param of the corresponding node. Note there
205 is no setter function as this array is built just once using
206 ipa_create_param_decls_array. */
207 static inline tree
208 ipa_get_ith_param (struct ipa_node_params *info, int i)
210 return info->param_decls[i];
213 /* Returns the modification flag corresponding to the ith parameter. Note
214 there is no setter method as the goal is to set all flags when building the
215 array in ipa_detect_param_modifications. */
216 static inline bool
217 ipa_is_ith_param_modified (struct ipa_node_params *info, int i)
219 return info->param_flags[i].modified;
222 /* Returns the called flag corresponding o the ith paramterer. Note there is
223 no setter method as the goal is to set all flags when building the array in
224 ipa_detect_called_params. */
225 static inline bool
226 ipa_is_ith_param_called (struct ipa_node_params *info, int i)
228 return info->param_flags[i].called;
231 /* Flag this node as having callers with variable number of arguments. */
232 static inline void
233 ipa_set_called_with_variable_arg (struct ipa_node_params *info)
235 info->called_with_var_arguments = 1;
238 /* Have we detected this node was called with variable number of arguments? */
239 static inline bool
240 ipa_is_called_with_var_arguments (struct ipa_node_params *info)
242 return info->called_with_var_arguments;
247 /* ipa_edge_args stores information related to a callsite and particularly
248 its arguments. It is pointed to by a field in the
249 callsite's corresponding cgraph_edge. */
250 struct ipa_edge_args
252 /* Number of actual arguments in this callsite. When set to 0,
253 this callsite's parameters would not be analyzed by the different
254 stages of IPA CP. */
255 int argument_count;
256 /* Array of the callsite's jump function of each parameter. */
257 struct ipa_jump_func *jump_functions;
260 /* ipa_edge_args access functions. Please use these to access fields that
261 are or will be shared among various passes. */
263 /* Set the number of actual arguments. */
264 static inline void
265 ipa_set_cs_argument_count (struct ipa_edge_args *args, int count)
267 args->argument_count = count;
270 /* Return the number of actual arguments. */
271 static inline int
272 ipa_get_cs_argument_count (struct ipa_edge_args *args)
274 return args->argument_count;
277 /* Returns a pointer to the jump function for the ith argument. Please note
278 there is no setter function as jump functions are all set up in
279 ipa_compute_jump_functions. */
280 static inline struct ipa_jump_func *
281 ipa_get_ith_jump_func (struct ipa_edge_args *args, int i)
283 return &args->jump_functions[i];
286 /* Vectors need to have typedefs of structures. */
287 typedef struct ipa_node_params ipa_node_params_t;
288 typedef struct ipa_edge_args ipa_edge_args_t;
290 /* Types of vectors hodling the infos. */
291 DEF_VEC_O (ipa_node_params_t);
292 DEF_VEC_ALLOC_O (ipa_node_params_t, heap);
293 DEF_VEC_O (ipa_edge_args_t);
294 DEF_VEC_ALLOC_O (ipa_edge_args_t, heap);
296 /* Vector where the parameter infos are actually stored. */
297 extern VEC (ipa_node_params_t, heap) *ipa_node_params_vector;
298 /* Vector where the parameter infos are actually stored. */
299 extern VEC (ipa_edge_args_t, heap) *ipa_edge_args_vector;
301 /* Return the associated parameter/argument info corresponding to the given
302 node/edge. */
303 #define IPA_NODE_REF(NODE) (VEC_index (ipa_node_params_t, \
304 ipa_node_params_vector, (NODE)->uid))
305 #define IPA_EDGE_REF(EDGE) (VEC_index (ipa_edge_args_t, \
306 ipa_edge_args_vector, (EDGE)->uid))
307 /* This macro checks validity of index returned by
308 ipa_get_param_decl_index function. */
309 #define IS_VALID_JUMP_FUNC_INDEX(I) ((I) != -1)
311 /* Creating and freeing ipa_node_params and ipa_edge_args. */
312 void ipa_create_all_node_params (void);
313 void ipa_create_all_edge_args (void);
314 void ipa_free_edge_args_substructures (struct ipa_edge_args *);
315 void ipa_free_node_params_substructures (struct ipa_node_params *);
316 void ipa_free_all_node_params (void);
317 void ipa_free_all_edge_args (void);
318 void free_all_ipa_structures_after_ipa_cp (void);
319 void free_all_ipa_structures_after_iinln (void);
320 void ipa_register_cgraph_hooks (void);
322 /* This function ensures the array of node param infos is big enough to
323 accomdate a structure for all nodes and realloacates it if not. */
324 static inline void
325 ipa_check_create_node_params (void)
327 if (!ipa_node_params_vector)
328 ipa_node_params_vector = VEC_alloc (ipa_node_params_t, heap,
329 cgraph_max_uid);
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);
337 /* This function ensures the array of adge arguments infos is big enough to
338 accomdate a structure for all edges and realloacates it if not. */
339 static inline void
340 ipa_check_create_edge_args (void)
342 if (!ipa_edge_args_vector)
343 ipa_edge_args_vector = VEC_alloc (ipa_edge_args_t, heap,
344 cgraph_edge_max_uid);
346 if (VEC_length (ipa_edge_args_t, ipa_edge_args_vector)
347 <= (unsigned) cgraph_edge_max_uid)
348 VEC_safe_grow_cleared (ipa_edge_args_t, heap, ipa_edge_args_vector,
349 cgraph_edge_max_uid + 1);
352 /* Returns true if the array of edge infos is large enough to accomodate an
353 info for EDGE. The main purpose of this function is that debug dumping
354 function can check info availability without causing reallocations. */
355 static inline bool
356 ipa_edge_args_info_available_for_edge_p (struct cgraph_edge *edge)
358 return ((unsigned) edge->uid < VEC_length (ipa_edge_args_t,
359 ipa_edge_args_vector));
362 /* A function list element. It is used to create a temporary worklist used in
363 the propagation stage of IPCP. (can be used for more IPA optimizations) */
364 struct ipa_func_list
366 struct cgraph_node *node;
367 struct ipa_func_list *next;
370 /* ipa_func_list interface. */
371 struct ipa_func_list *ipa_init_func_list (void);
372 void ipa_push_func_to_list (struct ipa_func_list **, struct cgraph_node *);
373 struct cgraph_node *ipa_pop_func_from_list (struct ipa_func_list **);
375 /* Callsite related calculations. */
376 void ipa_compute_jump_functions (struct cgraph_edge *);
377 void ipa_count_arguments (struct cgraph_edge *);
379 /* Function parameters related computations. */
380 void ipa_count_formal_params (struct cgraph_node *);
381 void ipa_create_param_decls_array (struct cgraph_node *);
382 void ipa_detect_param_modifications (struct cgraph_node *);
383 void ipa_analyze_params_uses (struct cgraph_node *);
384 void ipa_propagate_indirect_call_infos (struct cgraph_edge *cs,
385 VEC (cgraph_edge_p, heap) *new_edges);
387 /* Debugging interface. */
388 void ipa_print_all_tree_maps (FILE *);
389 void ipa_print_node_param_flags (FILE * f, struct cgraph_node *node);
390 void ipa_print_all_param_flags (FILE *);
391 void ipa_print_node_jump_functions (FILE *f, struct cgraph_node *node);
392 void ipa_print_all_jump_functions (FILE * f);
394 #endif /* IPA_PROP_H */