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
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
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/>. */
29 /* The following definitions and interfaces are used by
30 interprocedural analyses or parameters. */
32 /* ipa-prop.c stuff (ipa-cp, indirect inlining): */
34 /* A jump function for a callsite represents the values passed as actual
35 arguments of the callsite. There are three main types of values :
37 Pass-through - the caller's formal parameter is passed as an actual
38 argument, possibly one simple operation performed on it.
39 Constant - a constant (is_gimple_ip_invariant)is passed as an actual
41 Unknown - neither of the above.
43 IPA_JF_CONST_MEMBER_PTR stands for C++ member pointers, it is a special
44 constant in this regard. Other constants are represented with IPA_JF_CONST.
46 IPA_JF_ANCESTOR is a special pass-through jump function, which means that
47 the result is an address of a part of the object pointed to by the formal
48 parameter to which the function refers. It is mainly intended to represent
49 getting addresses of of ancestor fields in C++
50 (e.g. &this_1(D)->D.1766.D.1756). Note that if the original pointer is
51 NULL, ancestor jump function must behave like a simple pass-through.
53 Other pass-through functions can either simply pass on an unchanged formal
54 parameter or can apply one simple binary operation to it (such jump
55 functions are called polynomial).
57 IPA_JF_KNOWN_TYPE is a special type of an "unknown" function that applies
58 only to pointer parameters. It means that even though we cannot prove that
59 the passed value is an interprocedural constant, we still know the exact
60 type of the containing object which may be valuable for devirtualization.
62 Jump functions are computed in ipa-prop.c by function
63 update_call_notes_after_inlining. Some information can be lost and jump
64 functions degraded accordingly when inlining, see
65 update_call_notes_after_inlining in the same file. */
69 IPA_JF_UNKNOWN
= 0, /* newly allocated and zeroed jump functions default */
70 IPA_JF_KNOWN_TYPE
, /* represented by field base_binfo */
71 IPA_JF_CONST
, /* represented by field costant */
72 IPA_JF_CONST_MEMBER_PTR
, /* represented by field member_cst */
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 pass-through jump function. */
79 struct GTY(()) ipa_pass_through_data
81 /* If an operation is to be performed on the original parameter, this is the
82 second (constant) operand. */
84 /* Number of the caller's formal parameter being passed. */
86 /* Operation that is performed on the argument before it is passed on.
87 NOP_EXPR means no operation. Otherwise oper must be a simple binary
88 arithmetic operation where the caller's parameter is the first operand and
89 operand field from this structure is the second one. */
90 enum tree_code operation
;
93 /* Structure holding data required to describe an ancestor pass-through
96 struct GTY(()) ipa_ancestor_jf_data
98 /* Offset of the field representing the ancestor. */
100 /* TYpe of the result. */
102 /* Number of the caller's formal parameter being passed. */
106 /* Structure holding a C++ member pointer constant. Holds a pointer to the
107 method and delta offset. */
108 struct GTY(()) ipa_member_ptr_cst
114 /* A jump function for a callsite represents the values passed as actual
115 arguments of the callsite. See enum jump_func_type for the various
116 types of jump functions supported. */
117 struct GTY (()) ipa_jump_func
119 enum jump_func_type type
;
120 /* Represents a value of a jump function. pass_through is used only in jump
121 function context. constant represents the actual constant in constant jump
122 functions and member_cst holds constant c++ member functions. */
123 union jump_func_value
125 tree
GTY ((tag ("IPA_JF_KNOWN_TYPE"))) base_binfo
;
126 tree
GTY ((tag ("IPA_JF_CONST"))) constant
;
127 struct ipa_member_ptr_cst
GTY ((tag ("IPA_JF_CONST_MEMBER_PTR"))) member_cst
;
128 struct ipa_pass_through_data
GTY ((tag ("IPA_JF_PASS_THROUGH"))) pass_through
;
129 struct ipa_ancestor_jf_data
GTY ((tag ("IPA_JF_ANCESTOR"))) ancestor
;
130 } GTY ((desc ("%1.type"))) value
;
133 /* All formal parameters in the program have a lattice associated with it
134 computed by the interprocedural stage of IPCP.
135 There are three main values of the lattice:
137 IPA_BOTTOM - variable,
138 IPA_CONST_VALUE - simple scalar constant,
140 We also use this type to propagate types accross the call graph for the
141 purpose of devirtualization. In that case, IPA_CONST_VALUE denotes a known
142 type, rather than a constant. */
143 enum ipa_lattice_type
150 /* All formal parameters in the program have a cval computed by
151 the interprocedural stage of IPCP. See enum ipa_lattice_type for
152 the various types of lattices supported */
155 enum ipa_lattice_type type
;
159 /* Structure describing a single formal parameter. */
160 struct ipa_param_descriptor
162 /* IPA-CP lattice. */
163 struct ipcp_lattice ipcp_lattice
;
164 /* PARAM_DECL of this parameter. */
166 /* Vector of BINFOs of types that this argument might encounter. NULL
167 basically means a top value, bottom is marked by the cannot_devirtualize
169 VEC (tree
, heap
) *types
;
170 /* The parameter is used. */
172 /* Set when parameter type cannot be used for devirtualization. */
173 unsigned cannot_devirtualize
: 1;
176 /* ipa_node_params stores information related to formal parameters of functions
177 and some other information for interprocedural passes that operate on
178 parameters (such as ipa-cp). */
179 struct ipa_node_params
181 /* Number of formal parameters of this function. When set to 0, this
182 function's parameters would not be analyzed by IPA CP. */
184 /* Whether this function is called with variable number of actual
186 unsigned called_with_var_arguments
: 1;
187 /* Whether the param uses analysis has already been performed. */
188 unsigned uses_analysis_done
: 1;
189 /* Whether the function is enqueued in an ipa_func_list. */
190 unsigned node_enqueued
: 1;
191 /* Pointer to an array of structures describing individual formal
193 struct ipa_param_descriptor
*params
;
194 /* Only for versioned nodes this field would not be NULL,
195 it points to the node that IPA cp cloned from. */
196 struct cgraph_node
*ipcp_orig_node
;
197 /* Meaningful only for original functions. Expresses the
198 ratio between the direct calls and sum of all invocations of
199 this function (given by profiling info). It is used to calculate
200 the profiling information of the original function and the versioned
202 gcov_type count_scale
;
205 /* ipa_node_params access functions. Please use these to access fields that
206 are or will be shared among various passes. */
208 /* Set the number of formal parameters. */
211 ipa_set_param_count (struct ipa_node_params
*info
, int count
)
213 info
->param_count
= count
;
216 /* Return the number of formal parameters. */
219 ipa_get_param_count (struct ipa_node_params
*info
)
221 return info
->param_count
;
224 /* Return the declaration of Ith formal parameter of the function corresponding
225 to INFO. Note there is no setter function as this array is built just once
226 using ipa_initialize_node_params. */
229 ipa_get_param (struct ipa_node_params
*info
, int i
)
231 gcc_assert (i
>= 0 && i
<= info
->param_count
);
232 return info
->params
[i
].decl
;
235 /* Return the used flag corresponding to the Ith formal parameter of
236 the function associated with INFO. */
239 ipa_is_param_used (struct ipa_node_params
*info
, int i
)
241 gcc_assert (i
>= 0 && i
<= info
->param_count
);
242 return info
->params
[i
].used
;
245 /* Return the cannot_devirtualize flag corresponding to the Ith formal
246 parameter of the function associated with INFO. The corresponding function
247 to set the flag is ipa_set_param_cannot_devirtualize. */
250 ipa_param_cannot_devirtualize_p (struct ipa_node_params
*info
, int i
)
252 gcc_assert (i
>= 0 && i
<= info
->param_count
);
253 return info
->params
[i
].cannot_devirtualize
;
256 /* Return true iff the vector of possible types of the Ith formal parameter of
257 the function associated with INFO is empty. */
260 ipa_param_types_vec_empty (struct ipa_node_params
*info
, int i
)
262 gcc_assert (i
>= 0 && i
<= info
->param_count
);
263 return info
->params
[i
].types
== NULL
;
266 /* Flag this node as having callers with variable number of arguments. */
269 ipa_set_called_with_variable_arg (struct ipa_node_params
*info
)
271 info
->called_with_var_arguments
= 1;
274 /* Have we detected this node was called with variable number of arguments? */
277 ipa_is_called_with_var_arguments (struct ipa_node_params
*info
)
279 return info
->called_with_var_arguments
;
284 /* ipa_edge_args stores information related to a callsite and particularly its
285 arguments. It can be accessed by the IPA_EDGE_REF macro. */
286 typedef struct GTY(()) ipa_edge_args
288 /* Number of actual arguments in this callsite. When set to 0,
289 this callsite's parameters would not be analyzed by the different
292 /* Array of the callsite's jump function of each parameter. */
293 struct ipa_jump_func
GTY ((length ("%h.argument_count"))) *jump_functions
;
296 /* ipa_edge_args access functions. Please use these to access fields that
297 are or will be shared among various passes. */
299 /* Set the number of actual arguments. */
302 ipa_set_cs_argument_count (struct ipa_edge_args
*args
, int count
)
304 args
->argument_count
= count
;
307 /* Return the number of actual arguments. */
310 ipa_get_cs_argument_count (struct ipa_edge_args
*args
)
312 return args
->argument_count
;
315 /* Returns a pointer to the jump function for the ith argument. Please note
316 there is no setter function as jump functions are all set up in
317 ipa_compute_jump_functions. */
319 static inline struct ipa_jump_func
*
320 ipa_get_ith_jump_func (struct ipa_edge_args
*args
, int i
)
322 gcc_assert (i
>= 0 && i
<= args
->argument_count
);
323 return &args
->jump_functions
[i
];
326 /* Vectors need to have typedefs of structures. */
327 typedef struct ipa_node_params ipa_node_params_t
;
329 /* Types of vectors holding the infos. */
330 DEF_VEC_O (ipa_node_params_t
);
331 DEF_VEC_ALLOC_O (ipa_node_params_t
, heap
);
332 DEF_VEC_O (ipa_edge_args_t
);
333 DEF_VEC_ALLOC_O (ipa_edge_args_t
, gc
);
335 /* Vector where the parameter infos are actually stored. */
336 extern VEC (ipa_node_params_t
, heap
) *ipa_node_params_vector
;
337 /* Vector where the parameter infos are actually stored. */
338 extern GTY(()) VEC (ipa_edge_args_t
, gc
) *ipa_edge_args_vector
;
340 /* Return the associated parameter/argument info corresponding to the given
342 #define IPA_NODE_REF(NODE) (VEC_index (ipa_node_params_t, \
343 ipa_node_params_vector, (NODE)->uid))
344 #define IPA_EDGE_REF(EDGE) (VEC_index (ipa_edge_args_t, \
345 ipa_edge_args_vector, (EDGE)->uid))
346 /* This macro checks validity of index returned by
347 ipa_get_param_decl_index function. */
348 #define IS_VALID_JUMP_FUNC_INDEX(I) ((I) != -1)
350 /* Creating and freeing ipa_node_params and ipa_edge_args. */
351 void ipa_create_all_node_params (void);
352 void ipa_create_all_edge_args (void);
353 void ipa_free_edge_args_substructures (struct ipa_edge_args
*);
354 void ipa_free_node_params_substructures (struct ipa_node_params
*);
355 void ipa_free_all_node_params (void);
356 void ipa_free_all_edge_args (void);
357 void ipa_create_all_structures_for_iinln (void);
358 void ipa_free_all_structures_after_ipa_cp (void);
359 void ipa_free_all_structures_after_iinln (void);
360 void ipa_register_cgraph_hooks (void);
362 /* This function ensures the array of node param infos is big enough to
363 accommodate a structure for all nodes and reallocates it if not. */
366 ipa_check_create_node_params (void)
368 if (!ipa_node_params_vector
)
369 ipa_node_params_vector
= VEC_alloc (ipa_node_params_t
, heap
,
372 if (VEC_length (ipa_node_params_t
, ipa_node_params_vector
)
373 <= (unsigned) cgraph_max_uid
)
374 VEC_safe_grow_cleared (ipa_node_params_t
, heap
,
375 ipa_node_params_vector
, cgraph_max_uid
+ 1);
378 /* This function ensures the array of edge arguments infos is big enough to
379 accommodate a structure for all edges and reallocates it if not. */
382 ipa_check_create_edge_args (void)
384 if (!ipa_edge_args_vector
)
385 ipa_edge_args_vector
= VEC_alloc (ipa_edge_args_t
, gc
,
386 cgraph_edge_max_uid
);
388 if (VEC_length (ipa_edge_args_t
, ipa_edge_args_vector
)
389 <= (unsigned) cgraph_edge_max_uid
)
390 VEC_safe_grow_cleared (ipa_edge_args_t
, gc
, ipa_edge_args_vector
,
391 cgraph_edge_max_uid
+ 1);
394 /* Returns true if the array of edge infos is large enough to accommodate an
395 info for EDGE. The main purpose of this function is that debug dumping
396 function can check info availability without causing reallocations. */
399 ipa_edge_args_info_available_for_edge_p (struct cgraph_edge
*edge
)
401 return ((unsigned) edge
->uid
< VEC_length (ipa_edge_args_t
,
402 ipa_edge_args_vector
));
405 /* A function list element. It is used to create a temporary worklist used in
406 the propagation stage of IPCP. (can be used for more IPA optimizations) */
409 struct cgraph_node
*node
;
410 struct ipa_func_list
*next
;
413 /* ipa_func_list interface. */
414 struct ipa_func_list
*ipa_init_func_list (void);
415 void ipa_push_func_to_list_1 (struct ipa_func_list
**, struct cgraph_node
*,
416 struct ipa_node_params
*);
417 struct cgraph_node
*ipa_pop_func_from_list (struct ipa_func_list
**);
419 /* Add cgraph NODE to the worklist WL if it is not already in one. */
422 ipa_push_func_to_list (struct ipa_func_list
**wl
, struct cgraph_node
*node
)
424 struct ipa_node_params
*info
= IPA_NODE_REF (node
);
426 if (!info
->node_enqueued
)
427 ipa_push_func_to_list_1 (wl
, node
, info
);
430 void ipa_analyze_node (struct cgraph_node
*);
432 /* Function formal parameters related computations. */
433 void ipa_initialize_node_params (struct cgraph_node
*node
);
434 bool ipa_propagate_indirect_call_infos (struct cgraph_edge
*cs
,
435 VEC (cgraph_edge_p
, heap
) **new_edges
);
437 /* Indirect edge and binfo processing. */
438 struct cgraph_edge
*ipa_make_edge_direct_to_target (struct cgraph_edge
*, tree
,
442 /* Debugging interface. */
443 void ipa_print_node_params (FILE *, struct cgraph_node
*node
);
444 void ipa_print_all_params (FILE *);
445 void ipa_print_node_jump_functions (FILE *f
, struct cgraph_node
*node
);
446 void ipa_print_all_jump_functions (FILE * f
);
448 /* Structure to describe transformations of formal parameters and actual
449 arguments. Each instance describes one new parameter and they are meant to
450 be stored in a vector. Additionally, most users will probably want to store
451 adjustments about parameters that are being removed altogether so that SSA
452 names belonging to them can be replaced by SSA names of an artificial
454 struct ipa_parm_adjustment
456 /* The original PARM_DECL itself, helpful for processing of the body of the
457 function itself. Intended for traversing function bodies.
458 ipa_modify_formal_parameters, ipa_modify_call_arguments and
459 ipa_combine_adjustments ignore this and use base_index.
460 ipa_modify_formal_parameters actually sets this. */
463 /* Type of the new parameter. However, if by_ref is true, the real type will
464 be a pointer to this type. */
467 /* Alias refrerence type to be used in MEM_REFs when adjusting caller
471 /* The new declaration when creating/replacing a parameter. Created by
472 ipa_modify_formal_parameters, useful for functions modifying the body
476 /* New declaration of a substitute variable that we may use to replace all
477 non-default-def ssa names when a parm decl is going away. */
480 /* If non-NULL and the original parameter is to be removed (copy_param below
481 is NULL), this is going to be its nonlocalized vars value. */
484 /* Offset into the original parameter (for the cases when the new parameter
485 is a component of an original one). */
486 HOST_WIDE_INT offset
;
488 /* Zero based index of the original parameter this one is based on. (ATM
489 there is no way to insert a new parameter out of the blue because there is
490 no need but if it arises the code can be easily exteded to do so.) */
493 /* This new parameter is an unmodified parameter at index base_index. */
494 unsigned copy_param
: 1;
496 /* This adjustment describes a parameter that is about to be removed
497 completely. Most users will probably need to book keep those so that they
498 don't leave behinfd any non default def ssa names belonging to them. */
499 unsigned remove_param
: 1;
501 /* The parameter is to be passed by reference. */
505 typedef struct ipa_parm_adjustment ipa_parm_adjustment_t
;
506 DEF_VEC_O (ipa_parm_adjustment_t
);
507 DEF_VEC_ALLOC_O (ipa_parm_adjustment_t
, heap
);
509 typedef VEC (ipa_parm_adjustment_t
, heap
) *ipa_parm_adjustment_vec
;
511 VEC(tree
, heap
) *ipa_get_vector_of_formal_parms (tree fndecl
);
512 void ipa_modify_formal_parameters (tree fndecl
, ipa_parm_adjustment_vec
,
514 void ipa_modify_call_arguments (struct cgraph_edge
*, gimple
,
515 ipa_parm_adjustment_vec
);
516 ipa_parm_adjustment_vec
ipa_combine_adjustments (ipa_parm_adjustment_vec
,
517 ipa_parm_adjustment_vec
);
518 void ipa_dump_param_adjustments (FILE *, ipa_parm_adjustment_vec
, tree
);
520 void ipa_prop_write_jump_functions (cgraph_node_set set
);
521 void ipa_prop_read_jump_functions (void);
522 void ipa_update_after_lto_read (void);
523 int ipa_get_param_decl_index (struct ipa_node_params
*, tree
);
524 void ipa_lattice_from_jfunc (struct ipa_node_params
*info
, struct ipcp_lattice
*lat
,
525 struct ipa_jump_func
*jfunc
);
527 /* From tree-sra.c: */
528 tree
build_ref_for_offset (location_t
, tree
, HOST_WIDE_INT
, tree
,
529 gimple_stmt_iterator
*, bool);
531 /* Return the lattice corresponding to the Ith formal parameter of the function
532 described by INFO. */
533 static inline struct ipcp_lattice
*
534 ipa_get_lattice (struct ipa_node_params
*info
, int i
)
536 gcc_assert (i
>= 0 && i
<= info
->param_count
);
537 return &(info
->params
[i
].ipcp_lattice
);
540 #endif /* IPA_PROP_H */