GCC Middle and Back End API Reference
ipa-prop.h
Go to the documentation of this file.
1 /* Interprocedural analyses.
2  Copyright (C) 2005-2013 Free Software Foundation, Inc.
3 
4 This file is part of GCC.
5 
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.
10 
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.
15 
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/>. */
19 
20 #ifndef IPA_PROP_H
21 #define IPA_PROP_H
22 
23 #include "vec.h"
24 #include "cgraph.h"
25 #include "alloc-pool.h"
26 
27 /* The following definitions and interfaces are used by
28  interprocedural analyses or parameters. */
29 
30 #define IPA_UNDESCRIBED_USE -1
31 
32 /* ipa-prop.c stuff (ipa-cp, indirect inlining): */
33 
34 /* A jump function for a callsite represents the values passed as actual
35  arguments of the callsite. They were originally proposed in a paper called
36  "Interprocedural Constant Propagation", by David Callahan, Keith D Cooper,
37  Ken Kennedy, Linda Torczon in Comp86, pg 152-161. There are three main
38  types of values :
39 
40  Pass-through - the caller's formal parameter is passed as an actual
41  argument, possibly one simple operation performed on it.
42  Constant - a constant (is_gimple_ip_invariant)is passed as an actual
43  argument.
44  Unknown - neither of the above.
45 
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.
52 
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).
56 
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.
61 
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. */
66 
67 enum jump_func_type
68 {
69  IPA_JF_UNKNOWN = 0, /* newly allocated and zeroed jump functions default */
70  IPA_JF_KNOWN_TYPE, /* represented by field known_type */
71  IPA_JF_CONST, /* represented by field costant */
72  IPA_JF_PASS_THROUGH, /* represented by field pass_through */
73  IPA_JF_ANCESTOR /* represented by field ancestor */
74 };
75 
76 /* Structure holding data required to describe a known type jump function. */
78 {
79  /* Offset of the component of the base_type being described. */
81  /* Type of the whole object. */
82  tree base_type;
83  /* Type of the component of the object that is being described. */
84  tree component_type;
85 };
86 
87 struct ipa_cst_ref_desc;
88 
89 /* Structure holding data required to describe a constant jump function. */
90 struct GTY(()) ipa_constant_data
91 {
92  /* THe value of the constant. */
93  tree value;
94  /* Pointer to the structure that describes the reference. */
95  struct ipa_cst_ref_desc GTY((skip)) *rdesc;
96 };
97 
98 /* Structure holding data required to describe a pass-through jump function. */
99 
100 struct GTY(()) ipa_pass_through_data
101 {
102  /* If an operation is to be performed on the original parameter, this is the
103  second (constant) operand. */
104  tree operand;
105  /* Number of the caller's formal parameter being passed. */
106  int formal_id;
107  /* Operation that is performed on the argument before it is passed on.
108  NOP_EXPR means no operation. Otherwise oper must be a simple binary
109  arithmetic operation where the caller's parameter is the first operand and
110  operand field from this structure is the second one. */
111  enum tree_code operation;
112  /* When the passed value is a pointer, it is set to true only when we are
113  certain that no write to the object it points to has occurred since the
114  caller functions started execution, except for changes noted in the
115  aggregate part of the jump function (see description of
116  ipa_agg_jump_function). The flag is used only when the operation is
117  NOP_EXPR. */
118  unsigned agg_preserved : 1;
119 
120  /* When set to true, we guarantee that, if there is a C++ object pointed to
121  by this object, it does not undergo dynamic type change in the course of
122  functions decribed by this jump function. */
123  unsigned type_preserved : 1;
124 };
125 
126 /* Structure holding data required to describe an ancestor pass-through
127  jump function. */
128 
129 struct GTY(()) ipa_ancestor_jf_data
130 {
131  /* Offset of the field representing the ancestor. */
133  /* Type of the result. */
135  /* Number of the caller's formal parameter being passed. */
136  int formal_id;
137  /* Flag with the same meaning like agg_preserve in ipa_pass_through_data. */
138  unsigned agg_preserved : 1;
139  /* When set to true, we guarantee that, if there is a C++ object pointed to
140  by this object, it does not undergo dynamic type change in the course of
141  functions decribed by this jump function. */
142  unsigned type_preserved : 1;
143 };
144 
145 /* An element in an aggegate part of a jump function describing a known value
146  at a given offset. When it is part of a pass-through jump function with
147  agg_preserved set or an ancestor jump function with agg_preserved set, all
148  unlisted positions are assumed to be preserved but the value can be a type
149  node, which means that the particular piece (starting at offset and having
150  the size of the type) is clobbered with an unknown value. When
151  agg_preserved is false or the type of the containing jump function is
152  different, all unlisted parts are assumed to be unknown and all values must
153  fulfill is_gimple_ip_invariant. */
155 typedef struct GTY(()) ipa_agg_jf_item
156 {
157  /* The offset at which the known value is located within the aggregate. */
159 
160  /* The known constant or type if this is a clobber. */
161  tree value;
163 
164 
165 /* Aggregate jump function - i.e. description of contents of aggregates passed
166  either by reference or value. */
167 
168 struct GTY(()) ipa_agg_jump_function
169 {
170  /* Description of the individual items. */
172  /* True if the data was passed by reference (as opposed to by value). */
173  bool by_ref;
174 };
175 
178 
179 /* A jump function for a callsite represents the values passed as actual
180  arguments of the callsite. See enum jump_func_type for the various
181  types of jump functions supported. */
182 typedef struct GTY (()) ipa_jump_func
183 {
184  /* Aggregate contants description. See struct ipa_agg_jump_function and its
185  description. */
186  struct ipa_agg_jump_function agg;
188  enum jump_func_type type;
189  /* Represents a value of a jump function. pass_through is used only in jump
190  function context. constant represents the actual constant in constant jump
191  functions and member_cst holds constant c++ member functions. */
192  union jump_func_value
193  {
194  struct ipa_known_type_data GTY ((tag ("IPA_JF_KNOWN_TYPE"))) known_type;
195  struct ipa_constant_data GTY ((tag ("IPA_JF_CONST"))) constant;
196  struct ipa_pass_through_data GTY ((tag ("IPA_JF_PASS_THROUGH"))) pass_through;
197  struct ipa_ancestor_jf_data GTY ((tag ("IPA_JF_ANCESTOR"))) ancestor;
198  } GTY ((desc ("%1.type"))) value;
200 
201 
202 /* Return the offset of the component that is described by a known type jump
203  function JFUNC. */
204 
205 static inline HOST_WIDE_INT
207 {
208  gcc_checking_assert (jfunc->type == IPA_JF_KNOWN_TYPE);
209  return jfunc->value.known_type.offset;
210 }
211 
212 /* Return the base type of a known type jump function JFUNC. */
213 
214 static inline tree
216 {
218  return jfunc->value.known_type.base_type;
219 }
220 
221 /* Return the component type of a known type jump function JFUNC. */
222 
223 static inline tree
225 {
228 }
230 /* Return the constant stored in a constant jump functin JFUNC. */
231 
232 static inline tree
233 ipa_get_jf_constant (struct ipa_jump_func *jfunc)
234 {
236  return jfunc->value.constant.value;
237 }
238 
239 static inline struct ipa_cst_ref_desc *
241 {
243  return jfunc->value.constant.rdesc;
244 }
245 
246 /* Return the operand of a pass through jmp function JFUNC. */
247 
248 static inline tree
250 {
252  return jfunc->value.pass_through.operand;
253 }
254 
255 /* Return the number of the caller's formal parameter that a pass through jump
256  function JFUNC refers to. */
257 
258 static inline int
260 {
262  return jfunc->value.pass_through.formal_id;
263 }
264 
265 /* Return operation of a pass through jump function JFUNC. */
266 
267 static inline enum tree_code
269 {
271  return jfunc->value.pass_through.operation;
272 }
273 
274 /* Return the agg_preserved flag of a pass through jump function JFUNC. */
275 
276 static inline bool
278 {
280  return jfunc->value.pass_through.agg_preserved;
281 }
282 
283 /* Return the type_preserved flag of a pass through jump function JFUNC. */
284 
285 static inline bool
287 {
289  return jfunc->value.pass_through.type_preserved;
290 }
291 
292 /* Return the offset of an ancestor jump function JFUNC. */
293 
294 static inline HOST_WIDE_INT
296 {
298  return jfunc->value.ancestor.offset;
299 }
300 
301 /* Return the result type of an ancestor jump function JFUNC. */
302 
303 static inline tree
305 {
307  return jfunc->value.ancestor.type;
308 }
309 
310 /* Return the number of the caller's formal parameter that an ancestor jump
311  function JFUNC refers to. */
312 
313 static inline int
315 {
317  return jfunc->value.ancestor.formal_id;
318 }
319 
320 /* Return the agg_preserved flag of an ancestor jump function JFUNC. */
321 
322 static inline bool
324 {
326  return jfunc->value.ancestor.agg_preserved;
327 }
328 
329 /* Return the type_preserved flag of an ancestor jump function JFUNC. */
330 
331 static inline bool
333 {
335  return jfunc->value.ancestor.type_preserved;
336 }
338 /* Summary describing a single formal parameter. */
339 
341 {
342  /* PARAM_DECL of this parameter. */
343  tree decl;
344  /* If all uses of the parameter are described by ipa-prop structures, this
345  says how many there are. If any use could not be described by means of
346  ipa-prop structures, this is IPA_UNDESCRIBED_USE. */
347  int controlled_uses;
348  unsigned int move_cost : 31;
349  /* The parameter is used. */
350  unsigned used : 1;
351 };
352 
354 struct ipcp_lattice;
355 
356 /* ipa_node_params stores information related to formal parameters of functions
357  and some other information for interprocedural passes that operate on
358  parameters (such as ipa-cp). */
359 
360 struct ipa_node_params
361 {
362  /* Information about individual formal parameters that are gathered when
363  summaries are generated. */
364  vec<ipa_param_descriptor_t> descriptors;
365  /* Pointer to an array of structures describing individual formal
366  parameters. */
367  struct ipcp_param_lattices *lattices;
368  /* Only for versioned nodes this field would not be NULL,
369  it points to the node that IPA cp cloned from. */
370  struct cgraph_node *ipcp_orig_node;
371  /* If this node is an ipa-cp clone, these are the known values that describe
372  what it has been specialized for. */
373  vec<tree> known_vals;
374  /* Whether the param uses analysis has already been performed. */
375  unsigned uses_analysis_done : 1;
376  /* Whether the function is enqueued in ipa-cp propagation stack. */
377  unsigned node_enqueued : 1;
378  /* Whether we should create a specialized version based on values that are
379  known to be constant in all contexts. */
380  unsigned do_clone_for_all_contexts : 1;
381  /* Set if this is an IPA-CP clone for all contexts. */
382  unsigned is_all_contexts_clone : 1;
383  /* Node has been completely replaced by clones and will be removed after
384  ipa-cp is finished. */
385  unsigned node_dead : 1;
386 };
388 /* ipa_node_params access functions. Please use these to access fields that
389  are or will be shared among various passes. */
390 
391 /* Return the number of formal parameters. */
392 
393 static inline int
395 {
396  return info->descriptors.length ();
397 }
398 
399 /* Return the declaration of Ith formal parameter of the function corresponding
400  to INFO. Note there is no setter function as this array is built just once
401  using ipa_initialize_node_params. */
402 
403 static inline tree
404 ipa_get_param (struct ipa_node_params *info, int i)
405 {
406  gcc_checking_assert (!flag_wpa);
407  return info->descriptors[i].decl;
408 }
409 
410 /* Return the move cost of Ith formal parameter of the function corresponding
411  to INFO. */
412 
413 static inline int
414 ipa_get_param_move_cost (struct ipa_node_params *info, int i)
415 {
416  return info->descriptors[i].move_cost;
417 }
418 
419 /* Set the used flag corresponding to the Ith formal parameter of the function
420  associated with INFO to VAL. */
421 
422 static inline void
423 ipa_set_param_used (struct ipa_node_params *info, int i, bool val)
424 {
425  info->descriptors[i].used = val;
426 }
427 
428 /* Return how many uses described by ipa-prop a parameter has or
429  IPA_UNDESCRIBED_USE if there is a use that is not described by these
430  structures. */
431 static inline int
432 ipa_get_controlled_uses (struct ipa_node_params *info, int i)
433 {
434  return info->descriptors[i].controlled_uses;
435 }
436 
437 /* Set the controlled counter of a given parameter. */
439 static inline void
440 ipa_set_controlled_uses (struct ipa_node_params *info, int i, int val)
441 {
442  info->descriptors[i].controlled_uses = val;
443 }
444 
445 /* Return the used flag corresponding to the Ith formal parameter of the
446  function associated with INFO. */
447 
448 static inline bool
449 ipa_is_param_used (struct ipa_node_params *info, int i)
450 {
451  return info->descriptors[i].used;
452 }
453 
454 /* Information about replacements done in aggregates for a given node (each
455  node has its linked list). */
457 {
458  /* Next item in the linked list. */
460  /* Offset within the aggregate. */
462  /* The constant value. */
463  tree value;
464  /* The paramter index. */
465  int index;
466  /* Whether the value was passed by reference. */
467  bool by_ref;
468 };
469 
471 
472 void ipa_set_node_agg_value_chain (struct cgraph_node *node,
473  struct ipa_agg_replacement_value *aggvals);
474 
475 /* ipa_edge_args stores information related to a callsite and particularly its
476  arguments. It can be accessed by the IPA_EDGE_REF macro. */
477 typedef struct GTY(()) ipa_edge_args
478 {
479  /* Vector of the callsite's jump function of each parameter. */
480  vec<ipa_jump_func_t, va_gc> *jump_functions;
482 
483 /* ipa_edge_args access functions. Please use these to access fields that
484  are or will be shared among various passes. */
485 
486 /* Return the number of actual arguments. */
487 
488 static inline int
490 {
491  return vec_safe_length (args->jump_functions);
492 }
493 
494 /* Returns a pointer to the jump function for the ith argument. Please note
495  there is no setter function as jump functions are all set up in
496  ipa_compute_jump_functions. */
497 
498 static inline struct ipa_jump_func *
499 ipa_get_ith_jump_func (struct ipa_edge_args *args, int i)
500 {
501  return &(*args->jump_functions)[i];
502 }
503 
504 /* Vectors need to have typedefs of structures. */
505 typedef struct ipa_node_params ipa_node_params_t;
506 
507 /* Types of vectors holding the infos. */
508 
509 /* Vector where the parameter infos are actually stored. */
511 /* Vector of known aggregate values in cloned nodes. */
513 /* Vector where the parameter infos are actually stored. */
515 
516 /* Return the associated parameter/argument info corresponding to the given
517  node/edge. */
518 #define IPA_NODE_REF(NODE) (&ipa_node_params_vector[(NODE)->uid])
519 #define IPA_EDGE_REF(EDGE) (&(*ipa_edge_args_vector)[(EDGE)->uid])
520 /* This macro checks validity of index returned by
521  ipa_get_param_decl_index function. */
522 #define IS_VALID_JUMP_FUNC_INDEX(I) ((I) != -1)
523 
524 /* Creating and freeing ipa_node_params and ipa_edge_args. */
526 void ipa_create_all_edge_args (void);
530 void ipa_free_all_edge_args (void);
533 void ipa_register_cgraph_hooks (void);
534 
535 /* This function ensures the array of node param infos is big enough to
536  accommodate a structure for all nodes and reallocates it if not. */
537 
538 static inline void
540 {
541  if (!ipa_node_params_vector.exists ())
543 
544  if (ipa_node_params_vector.length () <= (unsigned) cgraph_max_uid)
545  ipa_node_params_vector.safe_grow_cleared (cgraph_max_uid + 1);
546 }
547 
548 /* This function ensures the array of edge arguments infos is big enough to
549  accommodate a structure for all edges and reallocates it if not. */
550 
551 static inline void
553 {
554  if (vec_safe_length (ipa_edge_args_vector) <= (unsigned) cgraph_edge_max_uid)
555  vec_safe_grow_cleared (ipa_edge_args_vector, cgraph_edge_max_uid + 1);
556 }
557 
558 /* Returns true if the array of edge infos is large enough to accommodate an
559  info for EDGE. The main purpose of this function is that debug dumping
560  function can check info availability without causing reallocations. */
561 
562 static inline bool
564 {
565  return ((unsigned) edge->uid < vec_safe_length (ipa_edge_args_vector));
566 }
568 /* Return the aggregate replacements for NODE, if there are any. */
569 
570 static inline struct ipa_agg_replacement_value *
572 {
573  if ((unsigned) node->uid >= vec_safe_length (ipa_node_agg_replacements))
574  return NULL;
575  return (*ipa_node_agg_replacements)[node->uid];
576 }
577 
578 /* Function formal parameters related computations. */
579 void ipa_initialize_node_params (struct cgraph_node *node);
581  vec<cgraph_edge_p> *new_edges);
582 
583 /* Indirect edge and binfo processing. */
586  vec<tree> ,
591 
592 /* Functions related to both. */
593 void ipa_analyze_node (struct cgraph_node *);
594 
595 /* Aggregate jump function related functions. */
597  bool);
598 bool ipa_load_from_parm_agg (struct ipa_node_params *, gimple, tree, int *,
599  HOST_WIDE_INT *, bool *);
600 
601 /* Debugging interface. */
602 void ipa_print_node_params (FILE *, struct cgraph_node *node);
603 void ipa_print_all_params (FILE *);
604 void ipa_print_node_jump_functions (FILE *f, struct cgraph_node *node);
605 void ipa_print_all_jump_functions (FILE * f);
607 
611 
612 /* Structure to describe transformations of formal parameters and actual
613  arguments. Each instance describes one new parameter and they are meant to
614  be stored in a vector. Additionally, most users will probably want to store
615  adjustments about parameters that are being removed altogether so that SSA
616  names belonging to them can be replaced by SSA names of an artificial
617  variable. */
618 struct ipa_parm_adjustment
619 {
620  /* The original PARM_DECL itself, helpful for processing of the body of the
621  function itself. Intended for traversing function bodies.
622  ipa_modify_formal_parameters, ipa_modify_call_arguments and
623  ipa_combine_adjustments ignore this and use base_index.
624  ipa_modify_formal_parameters actually sets this. */
625  tree base;
626 
627  /* Type of the new parameter. However, if by_ref is true, the real type will
628  be a pointer to this type. */
629  tree type;
630 
631  /* Alias refrerence type to be used in MEM_REFs when adjusting caller
632  arguments. */
633  tree alias_ptr_type;
634 
635  /* The new declaration when creating/replacing a parameter. Created by
636  ipa_modify_formal_parameters, useful for functions modifying the body
637  accordingly. */
638  tree reduction;
639 
640  /* New declaration of a substitute variable that we may use to replace all
641  non-default-def ssa names when a parm decl is going away. */
642  tree new_ssa_base;
643 
644  /* If non-NULL and the original parameter is to be removed (copy_param below
645  is NULL), this is going to be its nonlocalized vars value. */
646  tree nonlocal_value;
647 
648  /* Offset into the original parameter (for the cases when the new parameter
649  is a component of an original one). */
651 
652  /* Zero based index of the original parameter this one is based on. (ATM
653  there is no way to insert a new parameter out of the blue because there is
654  no need but if it arises the code can be easily exteded to do so.) */
655  int base_index;
656 
657  /* This new parameter is an unmodified parameter at index base_index. */
658  unsigned copy_param : 1;
659 
660  /* This adjustment describes a parameter that is about to be removed
661  completely. Most users will probably need to book keep those so that they
662  don't leave behinfd any non default def ssa names belonging to them. */
663  unsigned remove_param : 1;
664 
665  /* The parameter is to be passed by reference. */
666  unsigned by_ref : 1;
667 };
668 
670 
672 
675  const char *);
681 void ipa_dump_agg_replacement_values (FILE *f,
682  struct ipa_agg_replacement_value *av);
684 void ipa_prop_read_jump_functions (void);
687 void ipa_update_after_lto_read (void);
690  struct ipa_jump_func *jfunc);
691 unsigned int ipcp_transform_function (struct cgraph_node *node);
692 void ipa_dump_param (FILE *, struct ipa_node_params *info, int i);
693 
694 
695 /* From tree-sra.c: */
697  gimple_stmt_iterator *, bool);
698 
699 #endif /* IPA_PROP_H */