Bump date stamp to 20121110
[official-gcc.git] / gcc / ipa-prop.h
blob1fdd8333f018f057826123e2bbcce2d204bede0d
1 /* Interprocedural analyses.
2 Copyright (C) 2005, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
5 This file is part of GCC.
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.
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.
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/>. */
21 #ifndef IPA_PROP_H
22 #define IPA_PROP_H
24 #include "tree.h"
25 #include "vec.h"
26 #include "cgraph.h"
27 #include "gimple.h"
28 #include "alloc-pool.h"
30 /* The following definitions and interfaces are used by
31 interprocedural analyses or parameters. */
33 /* ipa-prop.c stuff (ipa-cp, indirect inlining): */
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 :
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.
47 IPA_JF_ANCESTOR is a special pass-through jump function, which means that
48 the result is an address of a part of the object pointed to by the formal
49 parameter to which the function refers. It is mainly intended to represent
50 getting addresses of of ancestor fields in C++
51 (e.g. &this_1(D)->D.1766.D.1756). Note that if the original pointer is
52 NULL, ancestor jump function must behave like a simple pass-through.
54 Other pass-through functions can either simply pass on an unchanged formal
55 parameter or can apply one simple binary operation to it (such jump
56 functions are called polynomial).
58 IPA_JF_KNOWN_TYPE is a special type of an "unknown" function that applies
59 only to pointer parameters. It means that even though we cannot prove that
60 the passed value is an interprocedural constant, we still know the exact
61 type of the containing object which may be valuable for devirtualization.
63 Jump functions are computed in ipa-prop.c by function
64 update_call_notes_after_inlining. Some information can be lost and jump
65 functions degraded accordingly when inlining, see
66 update_call_notes_after_inlining in the same file. */
68 enum jump_func_type
70 IPA_JF_UNKNOWN = 0, /* newly allocated and zeroed jump functions default */
71 IPA_JF_KNOWN_TYPE, /* represented by field known_type */
72 IPA_JF_CONST, /* represented by field costant */
73 IPA_JF_PASS_THROUGH, /* represented by field pass_through */
74 IPA_JF_ANCESTOR /* represented by field ancestor */
77 /* Structure holding data required to describe a known type jump function. */
78 struct GTY(()) ipa_known_type_data
80 /* Offset of the component of the base_type being described. */
81 HOST_WIDE_INT offset;
82 /* Type of the whole object. */
83 tree base_type;
84 /* Type of the component of the object that is being described. */
85 tree component_type;
88 /* Structure holding data required to describe a pass-through jump function. */
90 struct GTY(()) ipa_pass_through_data
92 /* If an operation is to be performed on the original parameter, this is the
93 second (constant) operand. */
94 tree operand;
95 /* Number of the caller's formal parameter being passed. */
96 int formal_id;
97 /* Operation that is performed on the argument before it is passed on.
98 NOP_EXPR means no operation. Otherwise oper must be a simple binary
99 arithmetic operation where the caller's parameter is the first operand and
100 operand field from this structure is the second one. */
101 enum tree_code operation;
102 /* When the passed value is a pointer, it is set to true only when we are
103 certain that no write to the object it points to has occurred since the
104 caller functions started execution, except for changes noted in the
105 aggregate part of the jump function (see description of
106 ipa_agg_jump_function). The flag is used only when the operation is
107 NOP_EXPR. */
108 bool agg_preserved;
111 /* Structure holding data required to describe an ancestor pass-through
112 jump function. */
114 struct GTY(()) ipa_ancestor_jf_data
116 /* Offset of the field representing the ancestor. */
117 HOST_WIDE_INT offset;
118 /* Type of the result. */
119 tree type;
120 /* Number of the caller's formal parameter being passed. */
121 int formal_id;
122 /* Flag with the same meaning like agg_preserve in ipa_pass_through_data. */
123 bool agg_preserved;
126 /* An element in an aggegate part of a jump function describing a known value
127 at a given offset. When it is part of a pass-through jump function with
128 agg_preserved set or an ancestor jump function with agg_preserved set, all
129 unlisted positions are assumed to be preserved but the value can be a type
130 node, which means that the particular piece (starting at offset and having
131 the size of the type) is clobbered with an unknown value. When
132 agg_preserved is false or the type of the containing jump function is
133 different, all unlisted parts are assumed to be unknown and all values must
134 fullfill is_gimple_ip_invariant. */
136 typedef struct GTY(()) ipa_agg_jf_item
138 /* The offset at which the known value is located within the aggregate. */
139 HOST_WIDE_INT offset;
141 /* The known constant or type if this is a clobber. */
142 tree value;
143 } ipa_agg_jf_item_t;
145 DEF_VEC_O (ipa_agg_jf_item_t);
146 DEF_VEC_ALLOC_O (ipa_agg_jf_item_t, gc);
148 /* Aggregate jump function - i.e. description of contents of aggregates passed
149 either by reference or value. */
151 struct GTY(()) ipa_agg_jump_function
153 /* Description of the individual items. */
154 VEC (ipa_agg_jf_item_t, gc) *items;
155 /* True if the data was passed by reference (as opposed to by value). */
156 bool by_ref;
159 typedef struct ipa_agg_jump_function *ipa_agg_jump_function_p;
160 DEF_VEC_P (ipa_agg_jump_function_p);
161 DEF_VEC_ALLOC_P (ipa_agg_jump_function_p, heap);
163 /* A jump function for a callsite represents the values passed as actual
164 arguments of the callsite. See enum jump_func_type for the various
165 types of jump functions supported. */
166 typedef struct GTY (()) ipa_jump_func
168 /* Aggregate contants description. See struct ipa_agg_jump_function and its
169 description. */
170 struct ipa_agg_jump_function agg;
172 enum jump_func_type type;
173 /* Represents a value of a jump function. pass_through is used only in jump
174 function context. constant represents the actual constant in constant jump
175 functions and member_cst holds constant c++ member functions. */
176 union jump_func_value
178 struct ipa_known_type_data GTY ((tag ("IPA_JF_KNOWN_TYPE"))) known_type;
179 tree GTY ((tag ("IPA_JF_CONST"))) constant;
180 struct ipa_pass_through_data GTY ((tag ("IPA_JF_PASS_THROUGH"))) pass_through;
181 struct ipa_ancestor_jf_data GTY ((tag ("IPA_JF_ANCESTOR"))) ancestor;
182 } GTY ((desc ("%1.type"))) value;
183 } ipa_jump_func_t;
185 DEF_VEC_O (ipa_jump_func_t);
186 DEF_VEC_ALLOC_O (ipa_jump_func_t, gc);
188 /* Return the offset of the component that is decribed by a known type jump
189 function JFUNC. */
191 static inline HOST_WIDE_INT
192 ipa_get_jf_known_type_offset (struct ipa_jump_func *jfunc)
194 gcc_checking_assert (jfunc->type == IPA_JF_KNOWN_TYPE);
195 return jfunc->value.known_type.offset;
198 /* Return the base type of a known type jump function JFUNC. */
200 static inline tree
201 ipa_get_jf_known_type_base_type (struct ipa_jump_func *jfunc)
203 gcc_checking_assert (jfunc->type == IPA_JF_KNOWN_TYPE);
204 return jfunc->value.known_type.base_type;
207 /* Return the component type of a known type jump function JFUNC. */
209 static inline tree
210 ipa_get_jf_known_type_component_type (struct ipa_jump_func *jfunc)
212 gcc_checking_assert (jfunc->type == IPA_JF_KNOWN_TYPE);
213 return jfunc->value.known_type.component_type;
216 /* Return the constant stored in a constant jump functin JFUNC. */
218 static inline tree
219 ipa_get_jf_constant (struct ipa_jump_func *jfunc)
221 gcc_checking_assert (jfunc->type == IPA_JF_CONST);
222 return jfunc->value.constant;
225 /* Return the operand of a pass through jmp function JFUNC. */
227 static inline tree
228 ipa_get_jf_pass_through_operand (struct ipa_jump_func *jfunc)
230 gcc_checking_assert (jfunc->type == IPA_JF_PASS_THROUGH);
231 return jfunc->value.pass_through.operand;
234 /* Return the number of the caller's formal parameter that a pass through jump
235 function JFUNC refers to. */
237 static inline int
238 ipa_get_jf_pass_through_formal_id (struct ipa_jump_func *jfunc)
240 gcc_checking_assert (jfunc->type == IPA_JF_PASS_THROUGH);
241 return jfunc->value.pass_through.formal_id;
244 /* Return operation of a pass through jump function JFUNC. */
246 static inline enum tree_code
247 ipa_get_jf_pass_through_operation (struct ipa_jump_func *jfunc)
249 gcc_checking_assert (jfunc->type == IPA_JF_PASS_THROUGH);
250 return jfunc->value.pass_through.operation;
253 /* Return the agg_preserved flag of a pass through jump functin JFUNC. */
255 static inline bool
256 ipa_get_jf_pass_through_agg_preserved (struct ipa_jump_func *jfunc)
258 gcc_checking_assert (jfunc->type == IPA_JF_PASS_THROUGH);
259 return jfunc->value.pass_through.agg_preserved;
262 /* Return the offset of an ancestor jump function JFUNC. */
264 static inline HOST_WIDE_INT
265 ipa_get_jf_ancestor_offset (struct ipa_jump_func *jfunc)
267 gcc_checking_assert (jfunc->type == IPA_JF_ANCESTOR);
268 return jfunc->value.ancestor.offset;
271 /* Return the result type of an ancestor jump function JFUNC. */
273 static inline tree
274 ipa_get_jf_ancestor_type (struct ipa_jump_func *jfunc)
276 gcc_checking_assert (jfunc->type == IPA_JF_ANCESTOR);
277 return jfunc->value.ancestor.type;
280 /* Return the number of the caller's formal parameter that an ancestor jump
281 function JFUNC refers to. */
283 static inline int
284 ipa_get_jf_ancestor_formal_id (struct ipa_jump_func *jfunc)
286 gcc_checking_assert (jfunc->type == IPA_JF_ANCESTOR);
287 return jfunc->value.ancestor.formal_id;
290 /* Return the agg_preserved flag of an ancestor jump functin JFUNC. */
292 static inline bool
293 ipa_get_jf_ancestor_agg_preserved (struct ipa_jump_func *jfunc)
295 gcc_checking_assert (jfunc->type == IPA_JF_ANCESTOR);
296 return jfunc->value.ancestor.agg_preserved;
299 /* Summary describing a single formal parameter. */
301 struct ipa_param_descriptor
303 /* PARAM_DECL of this parameter. */
304 tree decl;
305 /* The parameter is used. */
306 unsigned used : 1;
309 typedef struct ipa_param_descriptor ipa_param_descriptor_t;
310 DEF_VEC_O (ipa_param_descriptor_t);
311 DEF_VEC_ALLOC_O (ipa_param_descriptor_t, heap);
312 struct ipcp_lattice;
314 /* ipa_node_params stores information related to formal parameters of functions
315 and some other information for interprocedural passes that operate on
316 parameters (such as ipa-cp). */
318 struct ipa_node_params
320 /* Information about individual formal parameters that are gathered when
321 summaries are generated. */
322 VEC (ipa_param_descriptor_t, heap) *descriptors;
323 /* Pointer to an array of structures describing individual formal
324 parameters. */
325 struct ipcp_lattice *lattices;
326 /* Only for versioned nodes this field would not be NULL,
327 it points to the node that IPA cp cloned from. */
328 struct cgraph_node *ipcp_orig_node;
329 /* If this node is an ipa-cp clone, these are the known values that describe
330 what it has been specialized for. */
331 VEC (tree, heap) *known_vals;
332 /* Whether the param uses analysis has already been performed. */
333 unsigned uses_analysis_done : 1;
334 /* Whether the function is enqueued in ipa-cp propagation stack. */
335 unsigned node_enqueued : 1;
336 /* Whether we should create a specialized version based on values that are
337 known to be constant in all contexts. */
338 unsigned clone_for_all_contexts : 1;
339 /* Node has been completely replaced by clones and will be removed after
340 ipa-cp is finished. */
341 unsigned node_dead : 1;
344 /* ipa_node_params access functions. Please use these to access fields that
345 are or will be shared among various passes. */
347 /* Return the number of formal parameters. */
349 static inline int
350 ipa_get_param_count (struct ipa_node_params *info)
352 return VEC_length (ipa_param_descriptor_t, info->descriptors);
355 /* Return the declaration of Ith formal parameter of the function corresponding
356 to INFO. Note there is no setter function as this array is built just once
357 using ipa_initialize_node_params. */
359 static inline tree
360 ipa_get_param (struct ipa_node_params *info, int i)
362 return VEC_index (ipa_param_descriptor_t, info->descriptors, i).decl;
365 /* Set the used flag corresponding to the Ith formal parameter of the function
366 associated with INFO to VAL. */
368 static inline void
369 ipa_set_param_used (struct ipa_node_params *info, int i, bool val)
371 VEC_index (ipa_param_descriptor_t, info->descriptors, i).used = val;
374 /* Return the used flag corresponding to the Ith formal parameter of the
375 function associated with INFO. */
377 static inline bool
378 ipa_is_param_used (struct ipa_node_params *info, int i)
380 return VEC_index (ipa_param_descriptor_t, info->descriptors, i).used;
383 /* ipa_edge_args stores information related to a callsite and particularly its
384 arguments. It can be accessed by the IPA_EDGE_REF macro. */
385 typedef struct GTY(()) ipa_edge_args
387 /* Vector of the callsite's jump function of each parameter. */
388 VEC (ipa_jump_func_t, gc) *jump_functions;
389 } ipa_edge_args_t;
391 /* ipa_edge_args access functions. Please use these to access fields that
392 are or will be shared among various passes. */
394 /* Return the number of actual arguments. */
396 static inline int
397 ipa_get_cs_argument_count (struct ipa_edge_args *args)
399 return VEC_length (ipa_jump_func_t, args->jump_functions);
402 /* Returns a pointer to the jump function for the ith argument. Please note
403 there is no setter function as jump functions are all set up in
404 ipa_compute_jump_functions. */
406 static inline struct ipa_jump_func *
407 ipa_get_ith_jump_func (struct ipa_edge_args *args, int i)
409 return &VEC_index (ipa_jump_func_t, args->jump_functions, i);
412 /* Vectors need to have typedefs of structures. */
413 typedef struct ipa_node_params ipa_node_params_t;
415 /* Types of vectors holding the infos. */
416 DEF_VEC_O (ipa_node_params_t);
417 DEF_VEC_ALLOC_O (ipa_node_params_t, heap);
418 DEF_VEC_O (ipa_edge_args_t);
419 DEF_VEC_ALLOC_O (ipa_edge_args_t, gc);
421 /* Vector where the parameter infos are actually stored. */
422 extern VEC (ipa_node_params_t, heap) *ipa_node_params_vector;
423 /* Vector where the parameter infos are actually stored. */
424 extern GTY(()) VEC (ipa_edge_args_t, gc) *ipa_edge_args_vector;
426 /* Return the associated parameter/argument info corresponding to the given
427 node/edge. */
428 #define IPA_NODE_REF(NODE) (&VEC_index (ipa_node_params_t, \
429 ipa_node_params_vector, (NODE)->uid))
430 #define IPA_EDGE_REF(EDGE) (&VEC_index (ipa_edge_args_t, \
431 ipa_edge_args_vector, (EDGE)->uid))
432 /* This macro checks validity of index returned by
433 ipa_get_param_decl_index function. */
434 #define IS_VALID_JUMP_FUNC_INDEX(I) ((I) != -1)
436 /* Creating and freeing ipa_node_params and ipa_edge_args. */
437 void ipa_create_all_node_params (void);
438 void ipa_create_all_edge_args (void);
439 void ipa_free_edge_args_substructures (struct ipa_edge_args *);
440 void ipa_free_node_params_substructures (struct ipa_node_params *);
441 void ipa_free_all_node_params (void);
442 void ipa_free_all_edge_args (void);
443 void ipa_free_all_structures_after_ipa_cp (void);
444 void ipa_free_all_structures_after_iinln (void);
445 void ipa_register_cgraph_hooks (void);
447 /* This function ensures the array of node param infos is big enough to
448 accommodate a structure for all nodes and reallocates it if not. */
450 static inline void
451 ipa_check_create_node_params (void)
453 if (!ipa_node_params_vector)
454 ipa_node_params_vector = VEC_alloc (ipa_node_params_t, heap,
455 cgraph_max_uid);
457 if (VEC_length (ipa_node_params_t, ipa_node_params_vector)
458 <= (unsigned) cgraph_max_uid)
459 VEC_safe_grow_cleared (ipa_node_params_t, heap,
460 ipa_node_params_vector, cgraph_max_uid + 1);
463 /* This function ensures the array of edge arguments infos is big enough to
464 accommodate a structure for all edges and reallocates it if not. */
466 static inline void
467 ipa_check_create_edge_args (void)
469 if (!ipa_edge_args_vector)
470 ipa_edge_args_vector = VEC_alloc (ipa_edge_args_t, gc,
471 cgraph_edge_max_uid);
473 if (VEC_length (ipa_edge_args_t, ipa_edge_args_vector)
474 <= (unsigned) cgraph_edge_max_uid)
475 VEC_safe_grow_cleared (ipa_edge_args_t, gc, ipa_edge_args_vector,
476 cgraph_edge_max_uid + 1);
479 /* Returns true if the array of edge infos is large enough to accommodate an
480 info for EDGE. The main purpose of this function is that debug dumping
481 function can check info availability without causing reallocations. */
483 static inline bool
484 ipa_edge_args_info_available_for_edge_p (struct cgraph_edge *edge)
486 return ((unsigned) edge->uid < VEC_length (ipa_edge_args_t,
487 ipa_edge_args_vector));
490 /* Function formal parameters related computations. */
491 void ipa_initialize_node_params (struct cgraph_node *node);
492 bool ipa_propagate_indirect_call_infos (struct cgraph_edge *cs,
493 VEC (cgraph_edge_p, heap) **new_edges);
495 /* Indirect edge and binfo processing. */
496 tree ipa_get_indirect_edge_target (struct cgraph_edge *ie,
497 VEC (tree, heap) *,
498 VEC (tree, heap) *,
499 VEC (ipa_agg_jump_function_p, heap) *);
500 struct cgraph_edge *ipa_make_edge_direct_to_target (struct cgraph_edge *, tree);
502 /* Functions related to both. */
503 void ipa_analyze_node (struct cgraph_node *);
505 /* Aggregate jump function related functions. */
506 tree ipa_find_agg_cst_for_param (struct ipa_agg_jump_function *, HOST_WIDE_INT,
507 bool);
508 bool ipa_load_from_parm_agg (struct ipa_node_params *, gimple, tree, int *,
509 HOST_WIDE_INT *, bool *);
511 /* Debugging interface. */
512 void ipa_print_node_params (FILE *, struct cgraph_node *node);
513 void ipa_print_all_params (FILE *);
514 void ipa_print_node_jump_functions (FILE *f, struct cgraph_node *node);
515 void ipa_print_all_jump_functions (FILE * f);
516 void ipcp_verify_propagated_values (void);
518 extern alloc_pool ipcp_values_pool;
519 extern alloc_pool ipcp_sources_pool;
521 /* Structure to describe transformations of formal parameters and actual
522 arguments. Each instance describes one new parameter and they are meant to
523 be stored in a vector. Additionally, most users will probably want to store
524 adjustments about parameters that are being removed altogether so that SSA
525 names belonging to them can be replaced by SSA names of an artificial
526 variable. */
527 struct ipa_parm_adjustment
529 /* The original PARM_DECL itself, helpful for processing of the body of the
530 function itself. Intended for traversing function bodies.
531 ipa_modify_formal_parameters, ipa_modify_call_arguments and
532 ipa_combine_adjustments ignore this and use base_index.
533 ipa_modify_formal_parameters actually sets this. */
534 tree base;
536 /* Type of the new parameter. However, if by_ref is true, the real type will
537 be a pointer to this type. */
538 tree type;
540 /* Alias refrerence type to be used in MEM_REFs when adjusting caller
541 arguments. */
542 tree alias_ptr_type;
544 /* The new declaration when creating/replacing a parameter. Created by
545 ipa_modify_formal_parameters, useful for functions modifying the body
546 accordingly. */
547 tree reduction;
549 /* New declaration of a substitute variable that we may use to replace all
550 non-default-def ssa names when a parm decl is going away. */
551 tree new_ssa_base;
553 /* If non-NULL and the original parameter is to be removed (copy_param below
554 is NULL), this is going to be its nonlocalized vars value. */
555 tree nonlocal_value;
557 /* Offset into the original parameter (for the cases when the new parameter
558 is a component of an original one). */
559 HOST_WIDE_INT offset;
561 /* Zero based index of the original parameter this one is based on. (ATM
562 there is no way to insert a new parameter out of the blue because there is
563 no need but if it arises the code can be easily exteded to do so.) */
564 int base_index;
566 /* This new parameter is an unmodified parameter at index base_index. */
567 unsigned copy_param : 1;
569 /* This adjustment describes a parameter that is about to be removed
570 completely. Most users will probably need to book keep those so that they
571 don't leave behinfd any non default def ssa names belonging to them. */
572 unsigned remove_param : 1;
574 /* The parameter is to be passed by reference. */
575 unsigned by_ref : 1;
578 typedef struct ipa_parm_adjustment ipa_parm_adjustment_t;
579 DEF_VEC_O (ipa_parm_adjustment_t);
580 DEF_VEC_ALLOC_O (ipa_parm_adjustment_t, heap);
582 typedef VEC (ipa_parm_adjustment_t, heap) *ipa_parm_adjustment_vec;
584 VEC(tree, heap) *ipa_get_vector_of_formal_parms (tree fndecl);
585 void ipa_modify_formal_parameters (tree fndecl, ipa_parm_adjustment_vec,
586 const char *);
587 void ipa_modify_call_arguments (struct cgraph_edge *, gimple,
588 ipa_parm_adjustment_vec);
589 ipa_parm_adjustment_vec ipa_combine_adjustments (ipa_parm_adjustment_vec,
590 ipa_parm_adjustment_vec);
591 void ipa_dump_param_adjustments (FILE *, ipa_parm_adjustment_vec, tree);
593 void ipa_prop_write_jump_functions (void);
594 void ipa_prop_read_jump_functions (void);
595 void ipa_update_after_lto_read (void);
596 int ipa_get_param_decl_index (struct ipa_node_params *, tree);
597 tree ipa_value_from_jfunc (struct ipa_node_params *info,
598 struct ipa_jump_func *jfunc);
601 /* From tree-sra.c: */
602 tree build_ref_for_offset (location_t, tree, HOST_WIDE_INT, tree,
603 gimple_stmt_iterator *, bool);
605 #endif /* IPA_PROP_H */