OSDN Git Service

2011-09-02 Martin Jambor <mjambor@suse.cz>
[pf3gnuchains/gcc-fork.git] / gcc / ipa-prop.h
1 /* Interprocedural analyses.
2    Copyright (C) 2005, 2007, 2008, 2009, 2010
3    Free Software Foundation, Inc.
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 3, 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 COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #ifndef IPA_PROP_H
22 #define IPA_PROP_H
23
24 #include "tree.h"
25 #include "vec.h"
26 #include "cgraph.h"
27 #include "gimple.h"
28 #include "alloc-pool.h"
29
30 /* The following definitions and interfaces are used by
31    interprocedural analyses or parameters.  */
32
33 /* ipa-prop.c stuff (ipa-cp, indirect inlining):  */
34
35 /* A jump function for a callsite represents the values passed as actual
36    arguments of the callsite.  They were originally proposed in a paper called
37    "Interprocedural Constant Propagation", by David Callahan, Keith D Cooper,
38    Ken Kennedy, Linda Torczon in Comp86, pg 152-161.  There are three main
39    types of values :
40
41    Pass-through - the caller's formal parameter is passed as an actual
42                   argument, possibly one simple operation performed on it.
43    Constant     - a constant (is_gimple_ip_invariant)is passed as an actual
44                   argument.
45    Unknown      - neither of the above.
46
47    IPA_JF_CONST_MEMBER_PTR stands for C++ member pointers, it is a special
48    constant in this regard because it is in fact a structure consisting of two
49    values.  Other constants are represented with IPA_JF_CONST.
50
51    IPA_JF_ANCESTOR is a special pass-through jump function, which means that
52    the result is an address of a part of the object pointed to by the formal
53    parameter to which the function refers.  It is mainly intended to represent
54    getting addresses of of ancestor fields in C++
55    (e.g. &this_1(D)->D.1766.D.1756).  Note that if the original pointer is
56    NULL, ancestor jump function must behave like a simple pass-through.
57
58    Other pass-through functions can either simply pass on an unchanged formal
59    parameter or can apply one simple binary operation to it (such jump
60    functions are called polynomial).
61
62    IPA_JF_KNOWN_TYPE is a special type of an "unknown" function that applies
63    only to pointer parameters.  It means that even though we cannot prove that
64    the passed value is an interprocedural constant, we still know the exact
65    type of the containing object which may be valuable for devirtualization.
66
67    Jump functions are computed in ipa-prop.c by function
68    update_call_notes_after_inlining.  Some information can be lost and jump
69    functions degraded accordingly when inlining, see
70    update_call_notes_after_inlining in the same file.  */
71
72 enum jump_func_type
73 {
74   IPA_JF_UNKNOWN = 0,  /* newly allocated and zeroed jump functions default */
75   IPA_JF_KNOWN_TYPE,        /* represented by field base_binfo */
76   IPA_JF_CONST,             /* represented by field costant */
77   IPA_JF_CONST_MEMBER_PTR,  /* represented by field member_cst */
78   IPA_JF_PASS_THROUGH,      /* represented by field pass_through */
79   IPA_JF_ANCESTOR           /* represented by field ancestor */
80 };
81
82 /* Structure holding data required to describe a pass-through jump function.  */
83
84 struct GTY(()) ipa_pass_through_data
85 {
86   /* If an operation is to be performed on the original parameter, this is the
87      second (constant) operand.  */
88   tree operand;
89   /* Number of the caller's formal parameter being passed.  */
90   int formal_id;
91   /* Operation that is performed on the argument before it is passed on.
92      NOP_EXPR means no operation.  Otherwise oper must be a simple binary
93      arithmetic operation where the caller's parameter is the first operand and
94      operand field from this structure is the second one.  */
95   enum tree_code operation;
96 };
97
98 /* Structure holding data required to describe an ancestor pass-through
99    jump function.  */
100
101 struct GTY(()) ipa_ancestor_jf_data
102 {
103   /* Offset of the field representing the ancestor.  */
104   HOST_WIDE_INT offset;
105   /* TYpe of the result.  */
106   tree type;
107   /* Number of the caller's formal parameter being passed.  */
108   int formal_id;
109 };
110
111 /* Structure holding a C++ member pointer constant.  Holds a pointer to the
112    method and delta offset.  */
113 struct GTY(()) ipa_member_ptr_cst
114 {
115   tree pfn;
116   tree delta;
117 };
118
119 /* A jump function for a callsite represents the values passed as actual
120    arguments of the callsite. See enum jump_func_type for the various
121    types of jump functions supported.  */
122 struct GTY (()) ipa_jump_func
123 {
124   enum jump_func_type type;
125   /* Represents a value of a jump function.  pass_through is used only in jump
126      function context.  constant represents the actual constant in constant jump
127      functions and member_cst holds constant c++ member functions.  */
128   union jump_func_value
129   {
130     tree GTY ((tag ("IPA_JF_KNOWN_TYPE"))) base_binfo;
131     tree GTY ((tag ("IPA_JF_CONST"))) constant;
132     struct ipa_member_ptr_cst GTY ((tag ("IPA_JF_CONST_MEMBER_PTR"))) member_cst;
133     struct ipa_pass_through_data GTY ((tag ("IPA_JF_PASS_THROUGH"))) pass_through;
134     struct ipa_ancestor_jf_data GTY ((tag ("IPA_JF_ANCESTOR"))) ancestor;
135   } GTY ((desc ("%1.type"))) value;
136 };
137
138 /* Summary describing a single formal parameter.  */
139
140 struct ipa_param_descriptor
141 {
142   /* PARAM_DECL of this parameter.  */
143   tree decl;
144   /* The parameter is used.  */
145   unsigned used : 1;
146 };
147
148 typedef struct ipa_param_descriptor ipa_param_descriptor_t;
149 DEF_VEC_O (ipa_param_descriptor_t);
150 DEF_VEC_ALLOC_O (ipa_param_descriptor_t, heap);
151 struct ipcp_lattice;
152
153 /* ipa_node_params stores information related to formal parameters of functions
154    and some other information for interprocedural passes that operate on
155    parameters (such as ipa-cp).  */
156
157 struct ipa_node_params
158 {
159   /* Information about individual formal parameters that are gathered when
160      summaries are generated. */
161   VEC (ipa_param_descriptor_t, heap) *descriptors;
162   /* Pointer to an array of structures describing individual formal
163      parameters.  */
164   struct ipcp_lattice *lattices;
165   /* Only for versioned nodes this field would not be NULL,
166      it points to the node that IPA cp cloned from.  */
167   struct cgraph_node *ipcp_orig_node;
168   /* If this node is an ipa-cp clone, these are the known values that describe
169      what it has been specialized for.  */
170   VEC (tree, heap) *known_vals;
171   /* Whether this function is called with variable number of actual
172      arguments.  */
173   unsigned called_with_var_arguments : 1;
174   /* Set when it is possible to create specialized versions of this node.  */
175   unsigned node_versionable : 1;
176   /* Whether the param uses analysis has already been performed.  */
177   unsigned uses_analysis_done : 1;
178   /* Whether the function is enqueued in ipa-cp propagation stack.  */
179   unsigned node_enqueued : 1;
180   /* Whether we should create a specialized version based on values that are
181      known to be constant in all contexts.  */
182   unsigned clone_for_all_contexts : 1;
183   /* Node has been completely replaced by clones and will be removed after
184      ipa-cp is finished.  */
185   unsigned node_dead : 1;
186 };
187
188 /* ipa_node_params access functions.  Please use these to access fields that
189    are or will be shared among various passes.  */
190
191 /* Return the number of formal parameters. */
192
193 static inline int
194 ipa_get_param_count (struct ipa_node_params *info)
195 {
196   return VEC_length (ipa_param_descriptor_t, info->descriptors);
197 }
198
199 /* Return the declaration of Ith formal parameter of the function corresponding
200    to INFO.  Note there is no setter function as this array is built just once
201    using ipa_initialize_node_params. */
202
203 static inline tree
204 ipa_get_param (struct ipa_node_params *info, int i)
205 {
206   return VEC_index (ipa_param_descriptor_t, info->descriptors, i)->decl;
207 }
208
209 /* Set the used flag corresponding to the Ith formal parameter of the function
210    associated with INFO to VAL.  */
211
212 static inline void
213 ipa_set_param_used (struct ipa_node_params *info, int i, bool val)
214 {
215   VEC_index (ipa_param_descriptor_t, info->descriptors, i)->used = val;
216 }
217
218 /* Return the used flag corresponding to the Ith formal parameter of the
219    function associated with INFO.  */
220
221 static inline bool
222 ipa_is_param_used (struct ipa_node_params *info, int i)
223 {
224   return VEC_index (ipa_param_descriptor_t, info->descriptors, i)->used;
225 }
226
227 /* Flag this node as having callers with variable number of arguments.  */
228
229 static inline void
230 ipa_set_called_with_variable_arg (struct ipa_node_params *info)
231 {
232   info->called_with_var_arguments = 1;
233 }
234
235 /* Have we detected this node was called with variable number of arguments? */
236
237 static inline bool
238 ipa_is_called_with_var_arguments (struct ipa_node_params *info)
239 {
240   return info->called_with_var_arguments;
241 }
242
243 /* ipa_edge_args stores information related to a callsite and particularly its
244    arguments.  It can be accessed by the IPA_EDGE_REF macro.  */
245 typedef struct GTY(()) ipa_edge_args
246 {
247   /* Number of actual arguments in this callsite.  When set to 0,
248      this callsite's parameters would not be analyzed by the different
249      stages of IPA CP.  */
250   int argument_count;
251   /* Array of the callsite's jump function of each parameter.  */
252   struct ipa_jump_func GTY ((length ("%h.argument_count"))) *jump_functions;
253 } ipa_edge_args_t;
254
255 /* ipa_edge_args access functions.  Please use these to access fields that
256    are or will be shared among various passes.  */
257
258 /* Set the number of actual arguments. */
259
260 static inline void
261 ipa_set_cs_argument_count (struct ipa_edge_args *args, int count)
262 {
263   args->argument_count = count;
264 }
265
266 /* Return the number of actual arguments. */
267
268 static inline int
269 ipa_get_cs_argument_count (struct ipa_edge_args *args)
270 {
271   return args->argument_count;
272 }
273
274 /* Returns a pointer to the jump function for the ith argument.  Please note
275    there is no setter function as jump functions are all set up in
276    ipa_compute_jump_functions. */
277
278 static inline struct ipa_jump_func *
279 ipa_get_ith_jump_func (struct ipa_edge_args *args, int i)
280 {
281   gcc_assert (i >= 0 && i <= args->argument_count);
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
288 /* Types of vectors holding the infos.  */
289 DEF_VEC_O (ipa_node_params_t);
290 DEF_VEC_ALLOC_O (ipa_node_params_t, heap);
291 DEF_VEC_O (ipa_edge_args_t);
292 DEF_VEC_ALLOC_O (ipa_edge_args_t, gc);
293
294 /* Vector where the parameter infos are actually stored. */
295 extern VEC (ipa_node_params_t, heap) *ipa_node_params_vector;
296 /* Vector where the parameter infos are actually stored. */
297 extern GTY(()) VEC (ipa_edge_args_t, gc) *ipa_edge_args_vector;
298
299 /* Return the associated parameter/argument info corresponding to the given
300    node/edge.  */
301 #define IPA_NODE_REF(NODE) (VEC_index (ipa_node_params_t, \
302                                        ipa_node_params_vector, (NODE)->uid))
303 #define IPA_EDGE_REF(EDGE) (VEC_index (ipa_edge_args_t, \
304                                        ipa_edge_args_vector, (EDGE)->uid))
305 /* This macro checks validity of index returned by
306    ipa_get_param_decl_index function.  */
307 #define IS_VALID_JUMP_FUNC_INDEX(I) ((I) != -1)
308
309 /* Creating and freeing ipa_node_params and ipa_edge_args.  */
310 void ipa_create_all_node_params (void);
311 void ipa_create_all_edge_args (void);
312 void ipa_free_edge_args_substructures (struct ipa_edge_args *);
313 void ipa_free_node_params_substructures (struct ipa_node_params *);
314 void ipa_free_all_node_params (void);
315 void ipa_free_all_edge_args (void);
316 void ipa_create_all_structures_for_iinln (void);
317 void ipa_free_all_structures_after_ipa_cp (void);
318 void ipa_free_all_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, gc,
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, gc, 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 /* Function formal parameters related computations.  */
365 void ipa_initialize_node_params (struct cgraph_node *node);
366 bool ipa_propagate_indirect_call_infos (struct cgraph_edge *cs,
367                                         VEC (cgraph_edge_p, heap) **new_edges);
368
369 /* Indirect edge and binfo processing.  */
370 struct cgraph_edge *ipa_make_edge_direct_to_target (struct cgraph_edge *, tree);
371
372 /* Functions related to both.  */
373 void ipa_analyze_node (struct cgraph_node *);
374
375 /* Debugging interface.  */
376 void ipa_print_node_params (FILE *, struct cgraph_node *node);
377 void ipa_print_all_params (FILE *);
378 void ipa_print_node_jump_functions (FILE *f, struct cgraph_node *node);
379 void ipa_print_all_jump_functions (FILE * f);
380 void ipcp_verify_propagated_values (void);
381
382 extern alloc_pool ipcp_values_pool;
383 extern alloc_pool ipcp_sources_pool;
384
385 /* Structure to describe transformations of formal parameters and actual
386    arguments.  Each instance describes one new parameter and they are meant to
387    be stored in a vector.  Additionally, most users will probably want to store
388    adjustments about parameters that are being removed altogether so that SSA
389    names belonging to them can be replaced by SSA names of an artificial
390    variable.  */
391 struct ipa_parm_adjustment
392 {
393   /* The original PARM_DECL itself, helpful for processing of the body of the
394      function itself.  Intended for traversing function bodies.
395      ipa_modify_formal_parameters, ipa_modify_call_arguments and
396      ipa_combine_adjustments ignore this and use base_index.
397      ipa_modify_formal_parameters actually sets this.  */
398   tree base;
399
400   /* Type of the new parameter.  However, if by_ref is true, the real type will
401      be a pointer to this type.  */
402   tree type;
403
404   /* Alias refrerence type to be used in MEM_REFs when adjusting caller
405      arguments.  */
406   tree alias_ptr_type;
407
408   /* The new declaration when creating/replacing a parameter.  Created by
409      ipa_modify_formal_parameters, useful for functions modifying the body
410      accordingly. */
411   tree reduction;
412
413   /* New declaration of a substitute variable that we may use to replace all
414      non-default-def ssa names when a parm decl is going away.  */
415   tree new_ssa_base;
416
417   /* If non-NULL and the original parameter is to be removed (copy_param below
418      is NULL), this is going to be its nonlocalized vars value.  */
419   tree nonlocal_value;
420
421   /* Offset into the original parameter (for the cases when the new parameter
422      is a component of an original one).  */
423   HOST_WIDE_INT offset;
424
425   /* Zero based index of the original parameter this one is based on.  (ATM
426      there is no way to insert a new parameter out of the blue because there is
427      no need but if it arises the code can be easily exteded to do so.)  */
428   int base_index;
429
430   /* This new parameter is an unmodified parameter at index base_index. */
431   unsigned copy_param : 1;
432
433   /* This adjustment describes a parameter that is about to be removed
434      completely.  Most users will probably need to book keep those so that they
435      don't leave behinfd any non default def ssa names belonging to them.  */
436   unsigned remove_param : 1;
437
438   /* The parameter is to be passed by reference.  */
439   unsigned by_ref : 1;
440 };
441
442 typedef struct ipa_parm_adjustment ipa_parm_adjustment_t;
443 DEF_VEC_O (ipa_parm_adjustment_t);
444 DEF_VEC_ALLOC_O (ipa_parm_adjustment_t, heap);
445
446 typedef VEC (ipa_parm_adjustment_t, heap) *ipa_parm_adjustment_vec;
447
448 VEC(tree, heap) *ipa_get_vector_of_formal_parms (tree fndecl);
449 void ipa_modify_formal_parameters (tree fndecl, ipa_parm_adjustment_vec,
450                                    const char *);
451 void ipa_modify_call_arguments (struct cgraph_edge *, gimple,
452                                 ipa_parm_adjustment_vec);
453 ipa_parm_adjustment_vec ipa_combine_adjustments (ipa_parm_adjustment_vec,
454                                                  ipa_parm_adjustment_vec);
455 void ipa_dump_param_adjustments (FILE *, ipa_parm_adjustment_vec, tree);
456
457 void ipa_prop_write_jump_functions (cgraph_node_set set);
458 void ipa_prop_read_jump_functions (void);
459 void ipa_update_after_lto_read (void);
460 int ipa_get_param_decl_index (struct ipa_node_params *, tree);
461 tree ipa_cst_from_jfunc (struct ipa_node_params *info,
462                          struct ipa_jump_func *jfunc);
463
464
465 /* From tree-sra.c:  */
466 tree build_ref_for_offset (location_t, tree, HOST_WIDE_INT, tree,
467                            gimple_stmt_iterator *, bool);
468
469 #endif /* IPA_PROP_H */