* g++.dg/cpp0x/nullptr21.c: Remove printfs, make self-checking.
[gcc.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 known_type */
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 known type jump function. */
83 struct GTY(()) ipa_known_type_data
84 {
85 /* Offset of the component of the base_type being described. */
86 HOST_WIDE_INT offset;
87 /* Type of the whole object. */
88 tree base_type;
89 /* Type of the component of the object that is being described. */
90 tree component_type;
91 };
92
93 /* Structure holding data required to describe a pass-through jump function. */
94
95 struct GTY(()) ipa_pass_through_data
96 {
97 /* If an operation is to be performed on the original parameter, this is the
98 second (constant) operand. */
99 tree operand;
100 /* Number of the caller's formal parameter being passed. */
101 int formal_id;
102 /* Operation that is performed on the argument before it is passed on.
103 NOP_EXPR means no operation. Otherwise oper must be a simple binary
104 arithmetic operation where the caller's parameter is the first operand and
105 operand field from this structure is the second one. */
106 enum tree_code operation;
107 };
108
109 /* Structure holding data required to describe an ancestor pass-through
110 jump function. */
111
112 struct GTY(()) ipa_ancestor_jf_data
113 {
114 /* Offset of the field representing the ancestor. */
115 HOST_WIDE_INT offset;
116 /* Type of the result. */
117 tree type;
118 /* Number of the caller's formal parameter being passed. */
119 int formal_id;
120 };
121
122 /* Structure holding a C++ member pointer constant. Holds a pointer to the
123 method and delta offset. */
124 struct GTY(()) ipa_member_ptr_cst
125 {
126 tree pfn;
127 tree delta;
128 };
129
130 /* A jump function for a callsite represents the values passed as actual
131 arguments of the callsite. See enum jump_func_type for the various
132 types of jump functions supported. */
133 typedef struct GTY (()) ipa_jump_func
134 {
135 enum jump_func_type type;
136 /* Represents a value of a jump function. pass_through is used only in jump
137 function context. constant represents the actual constant in constant jump
138 functions and member_cst holds constant c++ member functions. */
139 union jump_func_value
140 {
141 struct ipa_known_type_data GTY ((tag ("IPA_JF_KNOWN_TYPE"))) known_type;
142 tree GTY ((tag ("IPA_JF_CONST"))) constant;
143 struct ipa_member_ptr_cst GTY ((tag ("IPA_JF_CONST_MEMBER_PTR"))) member_cst;
144 struct ipa_pass_through_data GTY ((tag ("IPA_JF_PASS_THROUGH"))) pass_through;
145 struct ipa_ancestor_jf_data GTY ((tag ("IPA_JF_ANCESTOR"))) ancestor;
146 } GTY ((desc ("%1.type"))) value;
147 } ipa_jump_func_t;
148
149 DEF_VEC_O (ipa_jump_func_t);
150 DEF_VEC_ALLOC_O (ipa_jump_func_t, gc);
151
152 /* Return the offset of the component that is decribed by a known type jump
153 function JFUNC. */
154
155 static inline HOST_WIDE_INT
156 ipa_get_jf_known_type_offset (struct ipa_jump_func *jfunc)
157 {
158 gcc_checking_assert (jfunc->type == IPA_JF_KNOWN_TYPE);
159 return jfunc->value.known_type.offset;
160 }
161
162 /* Return the base type of a known type jump function JFUNC. */
163
164 static inline tree
165 ipa_get_jf_known_type_base_type (struct ipa_jump_func *jfunc)
166 {
167 gcc_checking_assert (jfunc->type == IPA_JF_KNOWN_TYPE);
168 return jfunc->value.known_type.base_type;
169 }
170
171 /* Return the component type of a known type jump function JFUNC. */
172
173 static inline tree
174 ipa_get_jf_known_type_component_type (struct ipa_jump_func *jfunc)
175 {
176 gcc_checking_assert (jfunc->type == IPA_JF_KNOWN_TYPE);
177 return jfunc->value.known_type.component_type;
178 }
179
180 /* Return the constant stored in a constant jump functin JFUNC. */
181
182 static inline tree
183 ipa_get_jf_constant (struct ipa_jump_func *jfunc)
184 {
185 gcc_checking_assert (jfunc->type == IPA_JF_CONST);
186 return jfunc->value.constant;
187 }
188
189 /* Return the operand of a pass through jmp function JFUNC. */
190
191 static inline tree
192 ipa_get_jf_pass_through_operand (struct ipa_jump_func *jfunc)
193 {
194 gcc_checking_assert (jfunc->type == IPA_JF_PASS_THROUGH);
195 return jfunc->value.pass_through.operand;
196 }
197
198 /* Return the number of the caller's formal parameter that a pass through jump
199 function JFUNC refers to. */
200
201 static inline int
202 ipa_get_jf_pass_through_formal_id (struct ipa_jump_func *jfunc)
203 {
204 gcc_checking_assert (jfunc->type == IPA_JF_PASS_THROUGH);
205 return jfunc->value.pass_through.formal_id;
206 }
207
208 /* Return operation of a pass through jump function JFUNC. */
209
210 static inline enum tree_code
211 ipa_get_jf_pass_through_operation (struct ipa_jump_func *jfunc)
212 {
213 gcc_checking_assert (jfunc->type == IPA_JF_PASS_THROUGH);
214 return jfunc->value.pass_through.operation;
215 }
216
217 /* Return the offset of an ancestor jump function JFUNC. */
218
219 static inline HOST_WIDE_INT
220 ipa_get_jf_ancestor_offset (struct ipa_jump_func *jfunc)
221 {
222 gcc_checking_assert (jfunc->type == IPA_JF_ANCESTOR);
223 return jfunc->value.ancestor.offset;
224 }
225
226 /* Return the result type of an ancestor jump function JFUNC. */
227
228 static inline tree
229 ipa_get_jf_ancestor_type (struct ipa_jump_func *jfunc)
230 {
231 gcc_checking_assert (jfunc->type == IPA_JF_ANCESTOR);
232 return jfunc->value.ancestor.type;
233 }
234
235 /* Return the number of the caller's formal parameter that an ancestor jump
236 function JFUNC refers to. */
237
238 static inline int
239 ipa_get_jf_ancestor_formal_id (struct ipa_jump_func *jfunc)
240 {
241 gcc_checking_assert (jfunc->type == IPA_JF_ANCESTOR);
242 return jfunc->value.ancestor.formal_id;
243 }
244
245 /* Return the pfn part of a member pointer constant jump function JFUNC. */
246
247 static inline tree
248 ipa_get_jf_member_ptr_pfn (struct ipa_jump_func *jfunc)
249 {
250 gcc_checking_assert (jfunc->type == IPA_JF_CONST_MEMBER_PTR);
251 return jfunc->value.member_cst.pfn;
252 }
253
254 /* Summary describing a single formal parameter. */
255
256 struct ipa_param_descriptor
257 {
258 /* PARAM_DECL of this parameter. */
259 tree decl;
260 /* The parameter is used. */
261 unsigned used : 1;
262 };
263
264 typedef struct ipa_param_descriptor ipa_param_descriptor_t;
265 DEF_VEC_O (ipa_param_descriptor_t);
266 DEF_VEC_ALLOC_O (ipa_param_descriptor_t, heap);
267 struct ipcp_lattice;
268
269 /* ipa_node_params stores information related to formal parameters of functions
270 and some other information for interprocedural passes that operate on
271 parameters (such as ipa-cp). */
272
273 struct ipa_node_params
274 {
275 /* Information about individual formal parameters that are gathered when
276 summaries are generated. */
277 VEC (ipa_param_descriptor_t, heap) *descriptors;
278 /* Pointer to an array of structures describing individual formal
279 parameters. */
280 struct ipcp_lattice *lattices;
281 /* Only for versioned nodes this field would not be NULL,
282 it points to the node that IPA cp cloned from. */
283 struct cgraph_node *ipcp_orig_node;
284 /* If this node is an ipa-cp clone, these are the known values that describe
285 what it has been specialized for. */
286 VEC (tree, heap) *known_vals;
287 /* Whether the param uses analysis has already been performed. */
288 unsigned uses_analysis_done : 1;
289 /* Whether the function is enqueued in ipa-cp propagation stack. */
290 unsigned node_enqueued : 1;
291 /* Whether we should create a specialized version based on values that are
292 known to be constant in all contexts. */
293 unsigned clone_for_all_contexts : 1;
294 /* Node has been completely replaced by clones and will be removed after
295 ipa-cp is finished. */
296 unsigned node_dead : 1;
297 };
298
299 /* ipa_node_params access functions. Please use these to access fields that
300 are or will be shared among various passes. */
301
302 /* Return the number of formal parameters. */
303
304 static inline int
305 ipa_get_param_count (struct ipa_node_params *info)
306 {
307 return VEC_length (ipa_param_descriptor_t, info->descriptors);
308 }
309
310 /* Return the declaration of Ith formal parameter of the function corresponding
311 to INFO. Note there is no setter function as this array is built just once
312 using ipa_initialize_node_params. */
313
314 static inline tree
315 ipa_get_param (struct ipa_node_params *info, int i)
316 {
317 return VEC_index (ipa_param_descriptor_t, info->descriptors, i)->decl;
318 }
319
320 /* Set the used flag corresponding to the Ith formal parameter of the function
321 associated with INFO to VAL. */
322
323 static inline void
324 ipa_set_param_used (struct ipa_node_params *info, int i, bool val)
325 {
326 VEC_index (ipa_param_descriptor_t, info->descriptors, i)->used = val;
327 }
328
329 /* Return the used flag corresponding to the Ith formal parameter of the
330 function associated with INFO. */
331
332 static inline bool
333 ipa_is_param_used (struct ipa_node_params *info, int i)
334 {
335 return VEC_index (ipa_param_descriptor_t, info->descriptors, i)->used;
336 }
337
338 /* ipa_edge_args stores information related to a callsite and particularly its
339 arguments. It can be accessed by the IPA_EDGE_REF macro. */
340 typedef struct GTY(()) ipa_edge_args
341 {
342 /* Vector of the callsite's jump function of each parameter. */
343 VEC (ipa_jump_func_t, gc) *jump_functions;
344 } ipa_edge_args_t;
345
346 /* ipa_edge_args access functions. Please use these to access fields that
347 are or will be shared among various passes. */
348
349 /* Return the number of actual arguments. */
350
351 static inline int
352 ipa_get_cs_argument_count (struct ipa_edge_args *args)
353 {
354 return VEC_length (ipa_jump_func_t, args->jump_functions);
355 }
356
357 /* Returns a pointer to the jump function for the ith argument. Please note
358 there is no setter function as jump functions are all set up in
359 ipa_compute_jump_functions. */
360
361 static inline struct ipa_jump_func *
362 ipa_get_ith_jump_func (struct ipa_edge_args *args, int i)
363 {
364 return VEC_index (ipa_jump_func_t, args->jump_functions, i);
365 }
366
367 /* Vectors need to have typedefs of structures. */
368 typedef struct ipa_node_params ipa_node_params_t;
369
370 /* Types of vectors holding the infos. */
371 DEF_VEC_O (ipa_node_params_t);
372 DEF_VEC_ALLOC_O (ipa_node_params_t, heap);
373 DEF_VEC_O (ipa_edge_args_t);
374 DEF_VEC_ALLOC_O (ipa_edge_args_t, gc);
375
376 /* Vector where the parameter infos are actually stored. */
377 extern VEC (ipa_node_params_t, heap) *ipa_node_params_vector;
378 /* Vector where the parameter infos are actually stored. */
379 extern GTY(()) VEC (ipa_edge_args_t, gc) *ipa_edge_args_vector;
380
381 /* Return the associated parameter/argument info corresponding to the given
382 node/edge. */
383 #define IPA_NODE_REF(NODE) (VEC_index (ipa_node_params_t, \
384 ipa_node_params_vector, (NODE)->uid))
385 #define IPA_EDGE_REF(EDGE) (VEC_index (ipa_edge_args_t, \
386 ipa_edge_args_vector, (EDGE)->uid))
387 /* This macro checks validity of index returned by
388 ipa_get_param_decl_index function. */
389 #define IS_VALID_JUMP_FUNC_INDEX(I) ((I) != -1)
390
391 /* Creating and freeing ipa_node_params and ipa_edge_args. */
392 void ipa_create_all_node_params (void);
393 void ipa_create_all_edge_args (void);
394 void ipa_free_edge_args_substructures (struct ipa_edge_args *);
395 void ipa_free_node_params_substructures (struct ipa_node_params *);
396 void ipa_free_all_node_params (void);
397 void ipa_free_all_edge_args (void);
398 void ipa_free_all_structures_after_ipa_cp (void);
399 void ipa_free_all_structures_after_iinln (void);
400 void ipa_register_cgraph_hooks (void);
401
402 /* This function ensures the array of node param infos is big enough to
403 accommodate a structure for all nodes and reallocates it if not. */
404
405 static inline void
406 ipa_check_create_node_params (void)
407 {
408 if (!ipa_node_params_vector)
409 ipa_node_params_vector = VEC_alloc (ipa_node_params_t, heap,
410 cgraph_max_uid);
411
412 if (VEC_length (ipa_node_params_t, ipa_node_params_vector)
413 <= (unsigned) cgraph_max_uid)
414 VEC_safe_grow_cleared (ipa_node_params_t, heap,
415 ipa_node_params_vector, cgraph_max_uid + 1);
416 }
417
418 /* This function ensures the array of edge arguments infos is big enough to
419 accommodate a structure for all edges and reallocates it if not. */
420
421 static inline void
422 ipa_check_create_edge_args (void)
423 {
424 if (!ipa_edge_args_vector)
425 ipa_edge_args_vector = VEC_alloc (ipa_edge_args_t, gc,
426 cgraph_edge_max_uid);
427
428 if (VEC_length (ipa_edge_args_t, ipa_edge_args_vector)
429 <= (unsigned) cgraph_edge_max_uid)
430 VEC_safe_grow_cleared (ipa_edge_args_t, gc, ipa_edge_args_vector,
431 cgraph_edge_max_uid + 1);
432 }
433
434 /* Returns true if the array of edge infos is large enough to accommodate an
435 info for EDGE. The main purpose of this function is that debug dumping
436 function can check info availability without causing reallocations. */
437
438 static inline bool
439 ipa_edge_args_info_available_for_edge_p (struct cgraph_edge *edge)
440 {
441 return ((unsigned) edge->uid < VEC_length (ipa_edge_args_t,
442 ipa_edge_args_vector));
443 }
444
445 /* Function formal parameters related computations. */
446 void ipa_initialize_node_params (struct cgraph_node *node);
447 bool ipa_propagate_indirect_call_infos (struct cgraph_edge *cs,
448 VEC (cgraph_edge_p, heap) **new_edges);
449
450 /* Indirect edge and binfo processing. */
451 tree ipa_get_indirect_edge_target (struct cgraph_edge *ie,
452 VEC (tree, heap) *known_csts,
453 VEC (tree, heap) *known_binfs);
454 struct cgraph_edge *ipa_make_edge_direct_to_target (struct cgraph_edge *, tree);
455
456 /* Functions related to both. */
457 void ipa_analyze_node (struct cgraph_node *);
458
459 /* Debugging interface. */
460 void ipa_print_node_params (FILE *, struct cgraph_node *node);
461 void ipa_print_all_params (FILE *);
462 void ipa_print_node_jump_functions (FILE *f, struct cgraph_node *node);
463 void ipa_print_all_jump_functions (FILE * f);
464 void ipcp_verify_propagated_values (void);
465
466 extern alloc_pool ipcp_values_pool;
467 extern alloc_pool ipcp_sources_pool;
468
469 /* Structure to describe transformations of formal parameters and actual
470 arguments. Each instance describes one new parameter and they are meant to
471 be stored in a vector. Additionally, most users will probably want to store
472 adjustments about parameters that are being removed altogether so that SSA
473 names belonging to them can be replaced by SSA names of an artificial
474 variable. */
475 struct ipa_parm_adjustment
476 {
477 /* The original PARM_DECL itself, helpful for processing of the body of the
478 function itself. Intended for traversing function bodies.
479 ipa_modify_formal_parameters, ipa_modify_call_arguments and
480 ipa_combine_adjustments ignore this and use base_index.
481 ipa_modify_formal_parameters actually sets this. */
482 tree base;
483
484 /* Type of the new parameter. However, if by_ref is true, the real type will
485 be a pointer to this type. */
486 tree type;
487
488 /* Alias refrerence type to be used in MEM_REFs when adjusting caller
489 arguments. */
490 tree alias_ptr_type;
491
492 /* The new declaration when creating/replacing a parameter. Created by
493 ipa_modify_formal_parameters, useful for functions modifying the body
494 accordingly. */
495 tree reduction;
496
497 /* New declaration of a substitute variable that we may use to replace all
498 non-default-def ssa names when a parm decl is going away. */
499 tree new_ssa_base;
500
501 /* If non-NULL and the original parameter is to be removed (copy_param below
502 is NULL), this is going to be its nonlocalized vars value. */
503 tree nonlocal_value;
504
505 /* Offset into the original parameter (for the cases when the new parameter
506 is a component of an original one). */
507 HOST_WIDE_INT offset;
508
509 /* Zero based index of the original parameter this one is based on. (ATM
510 there is no way to insert a new parameter out of the blue because there is
511 no need but if it arises the code can be easily exteded to do so.) */
512 int base_index;
513
514 /* This new parameter is an unmodified parameter at index base_index. */
515 unsigned copy_param : 1;
516
517 /* This adjustment describes a parameter that is about to be removed
518 completely. Most users will probably need to book keep those so that they
519 don't leave behinfd any non default def ssa names belonging to them. */
520 unsigned remove_param : 1;
521
522 /* The parameter is to be passed by reference. */
523 unsigned by_ref : 1;
524 };
525
526 typedef struct ipa_parm_adjustment ipa_parm_adjustment_t;
527 DEF_VEC_O (ipa_parm_adjustment_t);
528 DEF_VEC_ALLOC_O (ipa_parm_adjustment_t, heap);
529
530 typedef VEC (ipa_parm_adjustment_t, heap) *ipa_parm_adjustment_vec;
531
532 VEC(tree, heap) *ipa_get_vector_of_formal_parms (tree fndecl);
533 void ipa_modify_formal_parameters (tree fndecl, ipa_parm_adjustment_vec,
534 const char *);
535 void ipa_modify_call_arguments (struct cgraph_edge *, gimple,
536 ipa_parm_adjustment_vec);
537 ipa_parm_adjustment_vec ipa_combine_adjustments (ipa_parm_adjustment_vec,
538 ipa_parm_adjustment_vec);
539 void ipa_dump_param_adjustments (FILE *, ipa_parm_adjustment_vec, tree);
540
541 void ipa_prop_write_jump_functions (cgraph_node_set set);
542 void ipa_prop_read_jump_functions (void);
543 void ipa_update_after_lto_read (void);
544 int ipa_get_param_decl_index (struct ipa_node_params *, tree);
545 tree ipa_value_from_jfunc (struct ipa_node_params *info,
546 struct ipa_jump_func *jfunc);
547
548
549 /* From tree-sra.c: */
550 tree build_ref_for_offset (location_t, tree, HOST_WIDE_INT, tree,
551 gimple_stmt_iterator *, bool);
552
553 #endif /* IPA_PROP_H */