1 /* Interprocedural analyses.
2    Copyright (C) 2005-2021 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 /* The following definitions and interfaces are used by
24    interprocedural analyses or parameters.  */
25 
26 #define IPA_UNDESCRIBED_USE -1
27 
28 /* ipa-prop.c stuff (ipa-cp, indirect inlining):  */
29 
30 /* A jump function for a callsite represents the values passed as actual
31    arguments of the callsite.  They were originally proposed in a paper called
32    "Interprocedural Constant Propagation", by David Callahan, Keith D Cooper,
33    Ken Kennedy, Linda Torczon in Comp86, pg 152-161.  There are three main
34    types of values :
35 
36    Pass-through - the caller's formal parameter is passed as an actual
37                   argument, possibly one simple operation performed on it.
38    Constant     - a constant (is_gimple_ip_invariant)is passed as an actual
39                   argument.
40    Unknown      - neither of the above.
41 
42    IPA_JF_LOAD_AGG is a compound pass-through jump function, in which primary
43    operation on formal parameter is memory dereference that loads a value from
44    a part of an aggregate, which is represented or pointed to by the formal
45    parameter.  Moreover, an additional unary/binary operation can be applied on
46    the loaded value, and final result is passed as actual argument of callee
47    (e.g. *(param_1(D) + 4) op 24 ).  It is meant to describe usage of aggregate
48    parameter or by-reference parameter referenced in argument passing, commonly
49    found in C++ and Fortran.
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 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    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_CONST,             /* represented by field costant */
71   IPA_JF_PASS_THROUGH,	    /* represented by field pass_through */
72   IPA_JF_LOAD_AGG,	    /* represented by field load_agg */
73   IPA_JF_ANCESTOR	    /* represented by field ancestor */
74 };
75 
76 struct ipa_cst_ref_desc;
77 
78 /* Structure holding data required to describe a constant jump function.  */
79 struct GTY(()) ipa_constant_data
80 {
81   /* THe value of the constant.  */
82   tree value;
83   /* Pointer to the structure that describes the reference.  */
84   struct ipa_cst_ref_desc GTY((skip)) *rdesc;
85 };
86 
87 /* Structure holding data required to describe a pass-through jump function.  */
88 
89 struct GTY(()) ipa_pass_through_data
90 {
91   /* If an operation is to be performed on the original parameter, this is the
92      second (constant) operand.  */
93   tree operand;
94   /* Number of the caller's formal parameter being passed.  */
95   int formal_id;
96   /* Operation that is performed on the argument before it is passed on.
97      Special values which have other meaning than in normal contexts:
98        - NOP_EXPR means no operation, not even type conversion.
99        - ASSERT_EXPR means that only the value in operand is allowed to pass
100          through (without any change), for all other values the result is
101          unknown.
102      Otherwise operation must be a simple binary or unary arithmetic operation
103      where the caller's parameter is the first operand and (for binary
104      operations) the operand field from this structure is the second one.  */
105   enum tree_code operation;
106   /* When the passed value is a pointer, it is set to true only when we are
107      certain that no write to the object it points to has occurred since the
108      caller functions started execution, except for changes noted in the
109      aggregate part of the jump function (see description of
110      ipa_agg_jump_function).  The flag is used only when the operation is
111      NOP_EXPR.  */
112   unsigned agg_preserved : 1;
113 };
114 
115 /* Structure holding data required to describe a load-value-from-aggregate
116    jump function.  */
117 
118 struct GTY(()) ipa_load_agg_data
119 {
120   /* Inherit from pass through jump function, describing unary/binary
121      operation on the value loaded from aggregate that is represented or
122      pointed to by the formal parameter, specified by formal_id in this
123      pass_through jump function data structure.  */
124   struct ipa_pass_through_data pass_through;
125   /* Type of the value loaded from the aggregate.  */
126   tree type;
127   /* Offset at which the value is located within the aggregate.  */
128   HOST_WIDE_INT offset;
129   /* True if loaded by reference (the aggregate is pointed to by the formal
130      parameter) or false if loaded by value (the aggregate is represented
131      by the formal parameter).  */
132   bool by_ref;
133 };
134 
135 /* Structure holding data required to describe an ancestor pass-through
136    jump function.  */
137 
138 struct GTY(()) ipa_ancestor_jf_data
139 {
140   /* Offset of the field representing the ancestor.  */
141   HOST_WIDE_INT offset;
142   /* Number of the caller's formal parameter being passed.  */
143   int formal_id;
144   /* Flag with the same meaning like agg_preserve in ipa_pass_through_data.  */
145   unsigned agg_preserved : 1;
146 };
147 
148 /* A jump function for an aggregate part at a given offset, which describes how
149    it content value is generated.  All unlisted positions are assumed to have a
150    value defined in an unknown way.  */
151 
152 struct GTY(()) ipa_agg_jf_item
153 {
154   /* The offset for the aggregate part.  */
155   HOST_WIDE_INT offset;
156 
157   /* Data type of the aggregate part.  */
158   tree type;
159 
160   /* Jump function type.  */
161   enum jump_func_type jftype;
162 
163   /* Represents a value of jump function. constant represents the actual constant
164      in constant jump function content.  pass_through is used only in simple pass
165      through jump function context.  load_agg is for load-value-from-aggregate
166      jump function context.  */
167   union jump_func_agg_value
168   {
169     tree GTY ((tag ("IPA_JF_CONST"))) constant;
170     struct ipa_pass_through_data GTY ((tag ("IPA_JF_PASS_THROUGH"))) pass_through;
171     struct ipa_load_agg_data GTY ((tag ("IPA_JF_LOAD_AGG"))) load_agg;
172   } GTY ((desc ("%1.jftype"))) value;
173 };
174 
175 /* Jump functions describing a set of aggregate contents.  */
176 
177 struct GTY(()) ipa_agg_jump_function
178 {
179   /* Description of the individual jump function item.  */
180   vec<ipa_agg_jf_item, va_gc> *items;
181   /* True if the data was passed by reference (as opposed to by value).  */
182   bool by_ref;
183 };
184 
185 /* An element in an aggregate part describing a known value at a given offset.
186    All unlisted positions are assumed to be unknown and all listed values must
187    fulfill is_gimple_ip_invariant.  */
188 
189 struct ipa_agg_value
190 {
191   /* The offset at which the known value is located within the aggregate.  */
192   HOST_WIDE_INT offset;
193 
194   /* The known constant.  */
195   tree value;
196 
197   /* Return true if OTHER describes same agg value.  */
198   bool equal_to (const ipa_agg_value &other);
199 };
200 
201 /* Structure describing a set of known offset/value for aggregate.  */
202 
203 struct ipa_agg_value_set
204 {
205   /* Description of the individual item.  */
206   vec<ipa_agg_value> items;
207   /* True if the data was passed by reference (as opposed to by value).  */
208   bool by_ref;
209 
210   /* Return true if OTHER describes same agg values.  */
equal_toipa_agg_value_set211   bool equal_to (const ipa_agg_value_set &other)
212   {
213     if (by_ref != other.by_ref)
214       return false;
215     if (items.length () != other.items.length ())
216       return false;
217     for (unsigned int i = 0; i < items.length (); i++)
218       if (!items[i].equal_to (other.items[i]))
219 	return false;
220     return true;
221   }
222 
223   /* Return true if there is any value for aggregate.  */
is_emptyipa_agg_value_set224   bool is_empty () const
225   {
226     return items.is_empty ();
227   }
228 
copyipa_agg_value_set229   ipa_agg_value_set copy () const
230   {
231     ipa_agg_value_set new_copy;
232 
233     new_copy.items = items.copy ();
234     new_copy.by_ref = by_ref;
235 
236     return new_copy;
237   }
238 
releaseipa_agg_value_set239   void release ()
240   {
241     items.release ();
242   }
243 };
244 
245 /* Return copy of a vec<ipa_agg_value_set>.  */
246 
247 static inline vec<ipa_agg_value_set>
ipa_copy_agg_values(const vec<ipa_agg_value_set> & aggs)248 ipa_copy_agg_values (const vec<ipa_agg_value_set> &aggs)
249 {
250   vec<ipa_agg_value_set> aggs_copy = vNULL;
251 
252   if (!aggs.is_empty ())
253     {
254       ipa_agg_value_set *agg;
255       int i;
256 
257       aggs_copy.reserve_exact (aggs.length ());
258 
259       FOR_EACH_VEC_ELT (aggs, i, agg)
260 	aggs_copy.quick_push (agg->copy ());
261     }
262 
263   return aggs_copy;
264 }
265 
266 /* For vec<ipa_agg_value_set>, DO NOT call release(), use below function
267    instead.  Because ipa_agg_value_set contains a field of vector type, we
268    should release this child vector in each element before reclaiming the
269    whole vector.  */
270 
271 static inline void
272 ipa_release_agg_values (vec<ipa_agg_value_set> &aggs,
273 			bool release_vector = true)
274 {
275   ipa_agg_value_set *agg;
276   int i;
277 
278   FOR_EACH_VEC_ELT (aggs, i, agg)
279     agg->release ();
280   if (release_vector)
281     aggs.release ();
282 }
283 
284 /* Information about zero/non-zero bits.  */
class()285 class GTY(()) ipa_bits
286 {
287 public:
288   /* The propagated value.  */
289   widest_int value;
290   /* Mask corresponding to the value.
291      Similar to ccp_lattice_t, if xth bit of mask is 0,
292      implies xth bit of value is constant.  */
293   widest_int mask;
294 };
295 
296 /* Info about value ranges.  */
297 
class()298 class GTY(()) ipa_vr
299 {
300 public:
301   /* The data fields below are valid only if known is true.  */
302   bool known;
303   enum value_range_kind type;
304   wide_int min;
305   wide_int max;
306   bool nonzero_p (tree) const;
307 };
308 
309 /* A jump function for a callsite represents the values passed as actual
310    arguments of the callsite. See enum jump_func_type for the various
311    types of jump functions supported.  */
312 struct GTY (()) ipa_jump_func
313 {
314   /* Aggregate jump function description.  See struct ipa_agg_jump_function
315      and its description.  */
316   struct ipa_agg_jump_function agg;
317 
318   /* Information about zero/non-zero bits.  The pointed to structure is shared
319      betweed different jump functions.  Use ipa_set_jfunc_bits to set this
320      field.  */
321   class ipa_bits *bits;
322 
323   /* Information about value range, containing valid data only when vr_known is
324      true.  The pointed to structure is shared betweed different jump
325      functions.  Use ipa_set_jfunc_vr to set this field.  */
326   value_range *m_vr;
327 
328   enum jump_func_type type;
329   /* Represents a value of a jump function.  pass_through is used only in jump
330      function context.  constant represents the actual constant in constant jump
331      functions and member_cst holds constant c++ member functions.  */
332   union jump_func_value
333   {
334     struct ipa_constant_data GTY ((tag ("IPA_JF_CONST"))) constant;
335     struct ipa_pass_through_data GTY ((tag ("IPA_JF_PASS_THROUGH"))) pass_through;
336     struct ipa_ancestor_jf_data GTY ((tag ("IPA_JF_ANCESTOR"))) ancestor;
337   } GTY ((desc ("%1.type"))) value;
338 };
339 
340 
341 /* Return the constant stored in a constant jump functin JFUNC.  */
342 
343 static inline tree
ipa_get_jf_constant(struct ipa_jump_func * jfunc)344 ipa_get_jf_constant (struct ipa_jump_func *jfunc)
345 {
346   gcc_checking_assert (jfunc->type == IPA_JF_CONST);
347   return jfunc->value.constant.value;
348 }
349 
350 static inline struct ipa_cst_ref_desc *
ipa_get_jf_constant_rdesc(struct ipa_jump_func * jfunc)351 ipa_get_jf_constant_rdesc (struct ipa_jump_func *jfunc)
352 {
353   gcc_checking_assert (jfunc->type == IPA_JF_CONST);
354   return jfunc->value.constant.rdesc;
355 }
356 
357 /* Return the operand of a pass through jmp function JFUNC.  */
358 
359 static inline tree
ipa_get_jf_pass_through_operand(struct ipa_jump_func * jfunc)360 ipa_get_jf_pass_through_operand (struct ipa_jump_func *jfunc)
361 {
362   gcc_checking_assert (jfunc->type == IPA_JF_PASS_THROUGH);
363   return jfunc->value.pass_through.operand;
364 }
365 
366 /* Return the number of the caller's formal parameter that a pass through jump
367    function JFUNC refers to.  */
368 
369 static inline int
ipa_get_jf_pass_through_formal_id(struct ipa_jump_func * jfunc)370 ipa_get_jf_pass_through_formal_id (struct ipa_jump_func *jfunc)
371 {
372   gcc_checking_assert (jfunc->type == IPA_JF_PASS_THROUGH);
373   return jfunc->value.pass_through.formal_id;
374 }
375 
376 /* Return operation of a pass through jump function JFUNC.  */
377 
378 static inline enum tree_code
ipa_get_jf_pass_through_operation(struct ipa_jump_func * jfunc)379 ipa_get_jf_pass_through_operation (struct ipa_jump_func *jfunc)
380 {
381   gcc_checking_assert (jfunc->type == IPA_JF_PASS_THROUGH);
382   return jfunc->value.pass_through.operation;
383 }
384 
385 /* Return the agg_preserved flag of a pass through jump function JFUNC.  */
386 
387 static inline bool
ipa_get_jf_pass_through_agg_preserved(struct ipa_jump_func * jfunc)388 ipa_get_jf_pass_through_agg_preserved (struct ipa_jump_func *jfunc)
389 {
390   gcc_checking_assert (jfunc->type == IPA_JF_PASS_THROUGH);
391   return jfunc->value.pass_through.agg_preserved;
392 }
393 
394 /* Return true if pass through jump function JFUNC preserves type
395    information.  */
396 
397 static inline bool
ipa_get_jf_pass_through_type_preserved(struct ipa_jump_func * jfunc)398 ipa_get_jf_pass_through_type_preserved (struct ipa_jump_func *jfunc)
399 {
400   gcc_checking_assert (jfunc->type == IPA_JF_PASS_THROUGH);
401   return jfunc->value.pass_through.agg_preserved;
402 }
403 
404 /* Return the offset of an ancestor jump function JFUNC.  */
405 
406 static inline HOST_WIDE_INT
ipa_get_jf_ancestor_offset(struct ipa_jump_func * jfunc)407 ipa_get_jf_ancestor_offset (struct ipa_jump_func *jfunc)
408 {
409   gcc_checking_assert (jfunc->type == IPA_JF_ANCESTOR);
410   return jfunc->value.ancestor.offset;
411 }
412 
413 /* Return the number of the caller's formal parameter that an ancestor jump
414    function JFUNC refers to.  */
415 
416 static inline int
ipa_get_jf_ancestor_formal_id(struct ipa_jump_func * jfunc)417 ipa_get_jf_ancestor_formal_id (struct ipa_jump_func *jfunc)
418 {
419   gcc_checking_assert (jfunc->type == IPA_JF_ANCESTOR);
420   return jfunc->value.ancestor.formal_id;
421 }
422 
423 /* Return the agg_preserved flag of an ancestor jump function JFUNC.  */
424 
425 static inline bool
ipa_get_jf_ancestor_agg_preserved(struct ipa_jump_func * jfunc)426 ipa_get_jf_ancestor_agg_preserved (struct ipa_jump_func *jfunc)
427 {
428   gcc_checking_assert (jfunc->type == IPA_JF_ANCESTOR);
429   return jfunc->value.ancestor.agg_preserved;
430 }
431 
432 /* Return true if ancestor jump function JFUNC presrves type information.  */
433 
434 static inline bool
ipa_get_jf_ancestor_type_preserved(struct ipa_jump_func * jfunc)435 ipa_get_jf_ancestor_type_preserved (struct ipa_jump_func *jfunc)
436 {
437   gcc_checking_assert (jfunc->type == IPA_JF_ANCESTOR);
438   return jfunc->value.ancestor.agg_preserved;
439 }
440 
441 /* Class for allocating a bundle of various potentially known properties about
442    actual arguments of a particular call on stack for the usual case and on
443    heap only if there are unusually many arguments.  The data is deallocated
444    when the instance of this class goes out of scope or is otherwise
445    destructed.  */
446 
447 class ipa_auto_call_arg_values
448 {
449 public:
450   ~ipa_auto_call_arg_values ();
451 
452   /* If m_known_vals (vector of known "scalar" values) is sufficiantly long,
453      return its element at INDEX, otherwise return NULL.  */
safe_sval_at(int index)454   tree safe_sval_at (int index)
455   {
456     /* TODO: Assert non-negative index here and test.  */
457     if ((unsigned) index < m_known_vals.length ())
458       return m_known_vals[index];
459     return NULL;
460   }
461 
462   /* If m_known_aggs is sufficiantly long, return the pointer rto its element
463      at INDEX, otherwise return NULL.  */
safe_aggval_at(int index)464   ipa_agg_value_set *safe_aggval_at (int index)
465   {
466     /* TODO: Assert non-negative index here and test.  */
467     if ((unsigned) index < m_known_aggs.length ())
468       return &m_known_aggs[index];
469     return NULL;
470   }
471 
472   /* Vector describing known values of parameters.  */
473   auto_vec<tree, 32> m_known_vals;
474 
475   /* Vector describing known polymorphic call contexts.  */
476   auto_vec<ipa_polymorphic_call_context, 32> m_known_contexts;
477 
478   /* Vector describing known aggregate values.  */
479   auto_vec<ipa_agg_value_set, 32> m_known_aggs;
480 
481   /* Vector describing known value ranges of arguments.  */
482   auto_vec<value_range, 32> m_known_value_ranges;
483 };
484 
485 /* Class bundling the various potentially known properties about actual
486    arguments of a particular call.  This variant does not deallocate the
487    bundled data in any way.  */
488 
489 class ipa_call_arg_values
490 {
491 public:
492   /* Default constructor, setting the vectors to empty ones.  */
ipa_call_arg_values()493   ipa_call_arg_values ()
494   {}
495 
496   /* Construct this general variant of the bundle from the variant which uses
497      auto_vecs to hold the vectors.  This means that vectors of objects
498      constructed with this constructor should not be changed because if they
499      get reallocated, the member vectors and the underlying auto_vecs would get
500      out of sync.  */
ipa_call_arg_values(ipa_auto_call_arg_values * aavals)501   ipa_call_arg_values (ipa_auto_call_arg_values *aavals)
502     : m_known_vals (aavals->m_known_vals),
503       m_known_contexts (aavals->m_known_contexts),
504       m_known_aggs (aavals->m_known_aggs),
505       m_known_value_ranges (aavals->m_known_value_ranges)
506   {}
507 
508   /* If m_known_vals (vector of known "scalar" values) is sufficiantly long,
509      return its element at INDEX, otherwise return NULL.  */
safe_sval_at(int index)510   tree safe_sval_at (int index)
511   {
512     /* TODO: Assert non-negative index here and test.  */
513     if ((unsigned) index < m_known_vals.length ())
514       return m_known_vals[index];
515     return NULL;
516   }
517 
518   /* If m_known_aggs is sufficiantly long, return the pointer rto its element
519      at INDEX, otherwise return NULL.  */
safe_aggval_at(int index)520   ipa_agg_value_set *safe_aggval_at (int index)
521   {
522     /* TODO: Assert non-negative index here and test.  */
523     if ((unsigned) index < m_known_aggs.length ())
524       return &m_known_aggs[index];
525     return NULL;
526   }
527 
528   /* Vector describing known values of parameters.  */
529   vec<tree> m_known_vals = vNULL;
530 
531   /* Vector describing known polymorphic call contexts.  */
532   vec<ipa_polymorphic_call_context> m_known_contexts = vNULL;
533 
534   /* Vector describing known aggregate values.  */
535   vec<ipa_agg_value_set> m_known_aggs = vNULL;
536 
537   /* Vector describing known value ranges of arguments.  */
538   vec<value_range> m_known_value_ranges = vNULL;
539 };
540 
541 
542 /* Summary describing a single formal parameter.  */
543 
544 struct GTY(()) ipa_param_descriptor
545 {
546   /* In analysis and modification phase, this is the PARAM_DECL of this
547      parameter, in IPA LTO phase, this is the type of the described
548      parameter or NULL if not known.  Do not read this field directly but
549      through ipa_get_param and ipa_get_type as appropriate.  */
550   tree decl_or_type;
551   /* If all uses of the parameter are described by ipa-prop structures, this
552      says how many there are.  If any use could not be described by means of
553      ipa-prop structures, this is IPA_UNDESCRIBED_USE.  */
554   int controlled_uses;
555   unsigned int move_cost : 28;
556   /* The parameter is used.  */
557   unsigned used : 1;
558   unsigned used_by_ipa_predicates : 1;
559   unsigned used_by_indirect_call : 1;
560   unsigned used_by_polymorphic_call : 1;
561 };
562 
563 /* ipa_node_params stores information related to formal parameters of functions
564    and some other information for interprocedural passes that operate on
565    parameters (such as ipa-cp).  */
566 
class(for_user)567 class GTY((for_user)) ipa_node_params
568 {
569 public:
570   /* Default constructor.  */
571   ipa_node_params ();
572 
573   /* Default destructor.  */
574   ~ipa_node_params ();
575 
576   /* Information about individual formal parameters that are gathered when
577      summaries are generated. */
578   vec<ipa_param_descriptor, va_gc> *descriptors;
579   /* Pointer to an array of structures describing individual formal
580      parameters.  */
581   class ipcp_param_lattices * GTY((skip)) lattices;
582   /* Only for versioned nodes this field would not be NULL,
583      it points to the node that IPA cp cloned from.  */
584   struct cgraph_node * GTY((skip)) ipcp_orig_node;
585   /* If this node is an ipa-cp clone, these are the known constants that
586      describe what it has been specialized for.  */
587   vec<tree> GTY((skip)) known_csts;
588   /* If this node is an ipa-cp clone, these are the known polymorphic contexts
589      that describe what it has been specialized for.  */
590   vec<ipa_polymorphic_call_context> GTY((skip)) known_contexts;
591   /* Whether the param uses analysis and jump function computation has already
592      been performed.  */
593   unsigned analysis_done : 1;
594   /* Whether the function is enqueued in ipa-cp propagation stack.  */
595   unsigned node_enqueued : 1;
596   /* Whether we should create a specialized version based on values that are
597      known to be constant in all contexts.  */
598   unsigned do_clone_for_all_contexts : 1;
599   /* Set if this is an IPA-CP clone for all contexts.  */
600   unsigned is_all_contexts_clone : 1;
601   /* Node has been completely replaced by clones and will be removed after
602      ipa-cp is finished.  */
603   unsigned node_dead : 1;
604   /* Node is involved in a recursion, potentionally indirect.  */
605   unsigned node_within_scc : 1;
606   /* Node contains only direct recursion.  */
607   unsigned node_is_self_scc : 1;
608   /* Node is calling a private function called only once.  */
609   unsigned node_calling_single_call : 1;
610   /* False when there is something makes versioning impossible.  */
611   unsigned versionable : 1;
612 };
613 
614 inline
ipa_node_params()615 ipa_node_params::ipa_node_params ()
616 : descriptors (NULL), lattices (NULL), ipcp_orig_node (NULL),
617   known_csts (vNULL), known_contexts (vNULL), analysis_done (0),
618   node_enqueued (0), do_clone_for_all_contexts (0), is_all_contexts_clone (0),
619   node_dead (0), node_within_scc (0), node_calling_single_call (0),
620   versionable (0)
621 {
622 }
623 
624 inline
~ipa_node_params()625 ipa_node_params::~ipa_node_params ()
626 {
627   free (lattices);
628   vec_free (descriptors);
629   known_csts.release ();
630   known_contexts.release ();
631 }
632 
633 /* Intermediate information that we get from alias analysis about a particular
634    parameter in a particular basic_block.  When a parameter or the memory it
635    references is marked modified, we use that information in all dominated
636    blocks without consulting alias analysis oracle.  */
637 
638 struct ipa_param_aa_status
639 {
640   /* Set when this structure contains meaningful information.  If not, the
641      structure describing a dominating BB should be used instead.  */
642   bool valid;
643 
644   /* Whether we have seen something which might have modified the data in
645      question.  PARM is for the parameter itself, REF is for data it points to
646      but using the alias type of individual accesses and PT is the same thing
647      but for computing aggregate pass-through functions using a very inclusive
648      ao_ref.  */
649   bool parm_modified, ref_modified, pt_modified;
650 };
651 
652 /* Information related to a given BB that used only when looking at function
653    body.  */
654 
655 struct ipa_bb_info
656 {
657   /* Call graph edges going out of this BB.  */
658   vec<cgraph_edge *> cg_edges;
659   /* Alias analysis statuses of each formal parameter at this bb.  */
660   vec<ipa_param_aa_status> param_aa_statuses;
661 };
662 
663 /* Structure with global information that is only used when looking at function
664    body. */
665 
666 struct ipa_func_body_info
667 {
668   /* The node that is being analyzed.  */
669   cgraph_node *node;
670 
671   /* Its info.  */
672   class ipa_node_params *info;
673 
674   /* Information about individual BBs. */
675   vec<ipa_bb_info> bb_infos;
676 
677   /* Number of parameters.  */
678   int param_count;
679 
680   /* Number of statements we are still allowed to walked by when analyzing this
681      function.  */
682   unsigned int aa_walk_budget;
683 };
684 
685 /* ipa_node_params access functions.  Please use these to access fields that
686    are or will be shared among various passes.  */
687 
688 /* Return the number of formal parameters. */
689 
690 static inline int
ipa_get_param_count(class ipa_node_params * info)691 ipa_get_param_count (class ipa_node_params *info)
692 {
693   return vec_safe_length (info->descriptors);
694 }
695 
696 /* Return the declaration of Ith formal parameter of the function corresponding
697    to INFO.  Note there is no setter function as this array is built just once
698    using ipa_initialize_node_params.  This function should not be called in
699    WPA.  */
700 
701 static inline tree
ipa_get_param(class ipa_node_params * info,int i)702 ipa_get_param (class ipa_node_params *info, int i)
703 {
704   gcc_checking_assert (info->descriptors);
705   tree t = (*info->descriptors)[i].decl_or_type;
706   gcc_checking_assert (TREE_CODE (t) == PARM_DECL);
707   return t;
708 }
709 
710 /* Return the type of Ith formal parameter of the function corresponding
711    to INFO if it is known or NULL if not.  */
712 
713 static inline tree
ipa_get_type(class ipa_node_params * info,int i)714 ipa_get_type (class ipa_node_params *info, int i)
715 {
716   if (vec_safe_length (info->descriptors) <= (unsigned) i)
717     return NULL;
718   tree t = (*info->descriptors)[i].decl_or_type;
719   if (!t)
720     return NULL;
721   if (TYPE_P (t))
722     return t;
723   gcc_checking_assert (TREE_CODE (t) == PARM_DECL);
724   return TREE_TYPE (t);
725 }
726 
727 /* Return the move cost of Ith formal parameter of the function corresponding
728    to INFO.  */
729 
730 static inline int
ipa_get_param_move_cost(class ipa_node_params * info,int i)731 ipa_get_param_move_cost (class ipa_node_params *info, int i)
732 {
733   gcc_checking_assert (info->descriptors);
734   return (*info->descriptors)[i].move_cost;
735 }
736 
737 /* Set the used flag corresponding to the Ith formal parameter of the function
738    associated with INFO to VAL.  */
739 
740 static inline void
ipa_set_param_used(class ipa_node_params * info,int i,bool val)741 ipa_set_param_used (class ipa_node_params *info, int i, bool val)
742 {
743   gcc_checking_assert (info->descriptors);
744   (*info->descriptors)[i].used = val;
745 }
746 
747 /* Set the used_by_ipa_predicates flag corresponding to the Ith formal
748    parameter of the function associated with INFO to VAL.  */
749 
750 static inline void
ipa_set_param_used_by_ipa_predicates(class ipa_node_params * info,int i,bool val)751 ipa_set_param_used_by_ipa_predicates (class ipa_node_params *info, int i, bool val)
752 {
753   gcc_checking_assert (info->descriptors);
754   (*info->descriptors)[i].used_by_ipa_predicates = val;
755 }
756 
757 /* Set the used_by_indirect_call flag corresponding to the Ith formal
758    parameter of the function associated with INFO to VAL.  */
759 
760 static inline void
ipa_set_param_used_by_indirect_call(class ipa_node_params * info,int i,bool val)761 ipa_set_param_used_by_indirect_call (class ipa_node_params *info, int i, bool val)
762 {
763   gcc_checking_assert (info->descriptors);
764   (*info->descriptors)[i].used_by_indirect_call = val;
765 }
766 
767 /* Set the .used_by_polymorphic_call flag corresponding to the Ith formal
768    parameter of the function associated with INFO to VAL.  */
769 
770 static inline void
ipa_set_param_used_by_polymorphic_call(class ipa_node_params * info,int i,bool val)771 ipa_set_param_used_by_polymorphic_call (class ipa_node_params *info, int i, bool val)
772 {
773   gcc_checking_assert (info->descriptors);
774   (*info->descriptors)[i].used_by_polymorphic_call = val;
775 }
776 
777 /* Return how many uses described by ipa-prop a parameter has or
778    IPA_UNDESCRIBED_USE if there is a use that is not described by these
779    structures.  */
780 static inline int
ipa_get_controlled_uses(class ipa_node_params * info,int i)781 ipa_get_controlled_uses (class ipa_node_params *info, int i)
782 {
783   /* FIXME: introducing speculation causes out of bounds access here.  */
784   if (vec_safe_length (info->descriptors) > (unsigned)i)
785     return (*info->descriptors)[i].controlled_uses;
786   return IPA_UNDESCRIBED_USE;
787 }
788 
789 /* Set the controlled counter of a given parameter.  */
790 
791 static inline void
ipa_set_controlled_uses(class ipa_node_params * info,int i,int val)792 ipa_set_controlled_uses (class ipa_node_params *info, int i, int val)
793 {
794   gcc_checking_assert (info->descriptors);
795   (*info->descriptors)[i].controlled_uses = val;
796 }
797 
798 /* Return the used flag corresponding to the Ith formal parameter of the
799    function associated with INFO.  */
800 
801 static inline bool
ipa_is_param_used(class ipa_node_params * info,int i)802 ipa_is_param_used (class ipa_node_params *info, int i)
803 {
804   gcc_checking_assert (info->descriptors);
805   return (*info->descriptors)[i].used;
806 }
807 
808 /* Return the used_by_ipa_predicates flag corresponding to the Ith formal
809    parameter of the function associated with INFO.  */
810 
811 static inline bool
ipa_is_param_used_by_ipa_predicates(class ipa_node_params * info,int i)812 ipa_is_param_used_by_ipa_predicates (class ipa_node_params *info, int i)
813 {
814   gcc_checking_assert (info->descriptors);
815   return (*info->descriptors)[i].used_by_ipa_predicates;
816 }
817 
818 /* Return the used_by_indirect_call flag corresponding to the Ith formal
819    parameter of the function associated with INFO.  */
820 
821 static inline bool
ipa_is_param_used_by_indirect_call(class ipa_node_params * info,int i)822 ipa_is_param_used_by_indirect_call (class ipa_node_params *info, int i)
823 {
824   gcc_checking_assert (info->descriptors);
825   return (*info->descriptors)[i].used_by_indirect_call;
826 }
827 
828 /* Return the used_by_polymorphic_call flag corresponding to the Ith formal
829    parameter of the function associated with INFO.  */
830 
831 static inline bool
ipa_is_param_used_by_polymorphic_call(class ipa_node_params * info,int i)832 ipa_is_param_used_by_polymorphic_call (class ipa_node_params *info, int i)
833 {
834   gcc_checking_assert (info->descriptors);
835   return (*info->descriptors)[i].used_by_polymorphic_call;
836 }
837 
838 /* Information about replacements done in aggregates for a given node (each
839    node has its linked list).  */
840 struct GTY(()) ipa_agg_replacement_value
841 {
842   /* Next item in the linked list.  */
843   struct ipa_agg_replacement_value *next;
844   /* Offset within the aggregate.  */
845   HOST_WIDE_INT offset;
846   /* The constant value.  */
847   tree value;
848   /* The parameter index.  */
849   int index;
850   /* Whether the value was passed by reference.  */
851   bool by_ref;
852 };
853 
854 /* Structure holding information for the transformation phase of IPA-CP.  */
855 
856 struct GTY(()) ipcp_transformation
857 {
858   /* Linked list of known aggregate values.  */
859   ipa_agg_replacement_value *agg_values;
860   /* Known bits information.  */
861   vec<ipa_bits *, va_gc> *bits;
862   /* Value range information.  */
863   vec<ipa_vr, va_gc> *m_vr;
864 
865   /* Default constructor.  */
ipcp_transformationipcp_transformation866   ipcp_transformation ()
867   : agg_values (NULL), bits (NULL), m_vr (NULL)
868   { }
869 
870   /* Default destructor.  */
~ipcp_transformationipcp_transformation871   ~ipcp_transformation ()
872   {
873     ipa_agg_replacement_value *agg = agg_values;
874     while (agg)
875       {
876 	ipa_agg_replacement_value *next = agg->next;
877 	ggc_free (agg);
878 	agg = next;
879       }
880     vec_free (bits);
881     vec_free (m_vr);
882   }
883 };
884 
885 void ipa_set_node_agg_value_chain (struct cgraph_node *node,
886 				   struct ipa_agg_replacement_value *aggvals);
887 void ipcp_transformation_initialize (void);
888 void ipcp_free_transformation_sum (void);
889 
890 /* ipa_edge_args stores information related to a callsite and particularly its
891    arguments.  It can be accessed by the IPA_EDGE_REF macro.  */
892 
class(for_user)893 class GTY((for_user)) ipa_edge_args
894 {
895  public:
896 
897   /* Default constructor.  */
898   ipa_edge_args () : jump_functions (NULL), polymorphic_call_contexts (NULL)
899     {}
900 
901   /* Destructor.  */
902   ~ipa_edge_args ()
903     {
904       unsigned int i;
905       ipa_jump_func *jf;
906       FOR_EACH_VEC_SAFE_ELT (jump_functions, i, jf)
907 	vec_free (jf->agg.items);
908       vec_free (jump_functions);
909       vec_free (polymorphic_call_contexts);
910     }
911 
912   /* Vectors of the callsite's jump function and polymorphic context
913      information of each parameter.  */
914   vec<ipa_jump_func, va_gc> *jump_functions;
915   vec<ipa_polymorphic_call_context, va_gc> *polymorphic_call_contexts;
916 };
917 
918 /* ipa_edge_args access functions.  Please use these to access fields that
919    are or will be shared among various passes.  */
920 
921 /* Return the number of actual arguments. */
922 
923 static inline int
ipa_get_cs_argument_count(class ipa_edge_args * args)924 ipa_get_cs_argument_count (class ipa_edge_args *args)
925 {
926   return vec_safe_length (args->jump_functions);
927 }
928 
929 /* Returns a pointer to the jump function for the ith argument.  Please note
930    there is no setter function as jump functions are all set up in
931    ipa_compute_jump_functions. */
932 
933 static inline struct ipa_jump_func *
ipa_get_ith_jump_func(class ipa_edge_args * args,int i)934 ipa_get_ith_jump_func (class ipa_edge_args *args, int i)
935 {
936   return &(*args->jump_functions)[i];
937 }
938 
939 /* Returns a pointer to the polymorphic call context for the ith argument.
940    NULL if contexts are not computed.  */
941 static inline class ipa_polymorphic_call_context *
ipa_get_ith_polymorhic_call_context(class ipa_edge_args * args,int i)942 ipa_get_ith_polymorhic_call_context (class ipa_edge_args *args, int i)
943 {
944   if (!args->polymorphic_call_contexts)
945     return NULL;
946   return &(*args->polymorphic_call_contexts)[i];
947 }
948 
949 /* Function summary for ipa_node_params.  */
class(user)950 class GTY((user)) ipa_node_params_t: public function_summary <ipa_node_params *>
951 {
952 public:
953   ipa_node_params_t (symbol_table *table, bool ggc):
954     function_summary<ipa_node_params *> (table, ggc)
955   {
956     disable_insertion_hook ();
957   }
958 
959   /* Hook that is called by summary when a node is duplicated.  */
960   virtual void duplicate (cgraph_node *node,
961 			  cgraph_node *node2,
962 			  ipa_node_params *data,
963 			  ipa_node_params *data2);
964 };
965 
966 /* Summary to manange ipa_edge_args structures.  */
967 
class(user)968 class GTY((user)) ipa_edge_args_sum_t : public call_summary <ipa_edge_args *>
969 {
970  public:
971   ipa_edge_args_sum_t (symbol_table *table, bool ggc)
972     : call_summary<ipa_edge_args *> (table, ggc) { }
973 
974   void remove (cgraph_edge *edge)
975   {
976     call_summary <ipa_edge_args *>::remove (edge);
977   }
978 
979   /* Hook that is called by summary when an edge is removed.  */
980   virtual void remove (cgraph_edge *cs, ipa_edge_args *args);
981   /* Hook that is called by summary when an edge is duplicated.  */
982   virtual void duplicate (cgraph_edge *src,
983 			  cgraph_edge *dst,
984 			  ipa_edge_args *old_args,
985 			  ipa_edge_args *new_args);
986 };
987 
988 /* Function summary where the parameter infos are actually stored. */
989 extern GTY(()) ipa_node_params_t * ipa_node_params_sum;
990 /* Call summary to store information about edges such as jump functions.  */
991 extern GTY(()) ipa_edge_args_sum_t *ipa_edge_args_sum;
992 
993 /* Function summary for IPA-CP transformation.  */
994 class ipcp_transformation_t
995 : public function_summary<ipcp_transformation *>
996 {
997 public:
ipcp_transformation_t(symbol_table * table,bool ggc)998   ipcp_transformation_t (symbol_table *table, bool ggc):
999     function_summary<ipcp_transformation *> (table, ggc) {}
1000 
~ipcp_transformation_t()1001   ~ipcp_transformation_t () {}
1002 
create_ggc(symbol_table * symtab)1003   static ipcp_transformation_t *create_ggc (symbol_table *symtab)
1004   {
1005     ipcp_transformation_t *summary
1006       = new (ggc_alloc_no_dtor <ipcp_transformation_t> ())
1007       ipcp_transformation_t (symtab, true);
1008     return summary;
1009   }
1010   /* Hook that is called by summary when a node is duplicated.  */
1011   virtual void duplicate (cgraph_node *node,
1012 			  cgraph_node *node2,
1013 			  ipcp_transformation *data,
1014 			  ipcp_transformation *data2);
1015 };
1016 
1017 /* Function summary where the IPA CP transformations are actually stored.  */
1018 extern GTY(()) function_summary <ipcp_transformation *> *ipcp_transformation_sum;
1019 
1020 /* Return the associated parameter/argument info corresponding to the given
1021    node/edge.  */
1022 #define IPA_NODE_REF(NODE) (ipa_node_params_sum->get (NODE))
1023 #define IPA_NODE_REF_GET_CREATE(NODE) (ipa_node_params_sum->get_create (NODE))
1024 #define IPA_EDGE_REF(EDGE) (ipa_edge_args_sum->get (EDGE))
1025 #define IPA_EDGE_REF_GET_CREATE(EDGE) (ipa_edge_args_sum->get_create (EDGE))
1026 /* This macro checks validity of index returned by
1027    ipa_get_param_decl_index function.  */
1028 #define IS_VALID_JUMP_FUNC_INDEX(I) ((I) != -1)
1029 
1030 /* Creating and freeing ipa_node_params and ipa_edge_args.  */
1031 void ipa_create_all_node_params (void);
1032 void ipa_create_all_edge_args (void);
1033 void ipa_check_create_edge_args (void);
1034 void ipa_free_all_node_params (void);
1035 void ipa_free_all_edge_args (void);
1036 void ipa_free_all_structures_after_ipa_cp (void);
1037 void ipa_free_all_structures_after_iinln (void);
1038 
1039 void ipa_register_cgraph_hooks (void);
1040 int count_formal_params (tree fndecl);
1041 
1042 /* This function ensures the array of node param infos is big enough to
1043    accommodate a structure for all nodes and reallocates it if not.  */
1044 
1045 static inline void
ipa_check_create_node_params(void)1046 ipa_check_create_node_params (void)
1047 {
1048   if (!ipa_node_params_sum)
1049     ipa_node_params_sum
1050       = (new (ggc_alloc_no_dtor <ipa_node_params_t> ())
1051 	 ipa_node_params_t (symtab, true));
1052 }
1053 
1054 /* Returns true if edge summary contains a record for EDGE.  The main purpose
1055    of this function is that debug dumping function can check info availability
1056    without causing allocations.  */
1057 
1058 static inline bool
ipa_edge_args_info_available_for_edge_p(struct cgraph_edge * edge)1059 ipa_edge_args_info_available_for_edge_p (struct cgraph_edge *edge)
1060 {
1061   return ipa_edge_args_sum->exists (edge);
1062 }
1063 
1064 static inline ipcp_transformation *
ipcp_get_transformation_summary(cgraph_node * node)1065 ipcp_get_transformation_summary (cgraph_node *node)
1066 {
1067   if (ipcp_transformation_sum == NULL)
1068     return NULL;
1069 
1070   return ipcp_transformation_sum->get (node);
1071 }
1072 
1073 /* Return the aggregate replacements for NODE, if there are any.  */
1074 
1075 static inline struct ipa_agg_replacement_value *
ipa_get_agg_replacements_for_node(cgraph_node * node)1076 ipa_get_agg_replacements_for_node (cgraph_node *node)
1077 {
1078   ipcp_transformation *ts = ipcp_get_transformation_summary (node);
1079   return ts ? ts->agg_values : NULL;
1080 }
1081 
1082 /* Function formal parameters related computations.  */
1083 void ipa_initialize_node_params (struct cgraph_node *node);
1084 bool ipa_propagate_indirect_call_infos (struct cgraph_edge *cs,
1085 					vec<cgraph_edge *> *new_edges);
1086 
1087 /* Indirect edge processing and target discovery.  */
1088 tree ipa_get_indirect_edge_target (struct cgraph_edge *ie,
1089 				   ipa_call_arg_values *avals,
1090 				   bool *speculative);
1091 tree ipa_get_indirect_edge_target (struct cgraph_edge *ie,
1092 				   ipa_auto_call_arg_values *avals,
1093 				   bool *speculative);
1094 struct cgraph_edge *ipa_make_edge_direct_to_target (struct cgraph_edge *, tree,
1095 						    bool speculative = false);
1096 tree ipa_impossible_devirt_target (struct cgraph_edge *, tree);
1097 ipa_bits *ipa_get_ipa_bits_for_value (const widest_int &value,
1098 				      const widest_int &mask);
1099 
1100 
1101 /* Functions related to both.  */
1102 void ipa_analyze_node (struct cgraph_node *);
1103 
1104 /* Aggregate jump function related functions.  */
1105 tree ipa_find_agg_cst_for_param (struct ipa_agg_value_set *agg, tree scalar,
1106 				 HOST_WIDE_INT offset, bool by_ref,
1107 				 bool *from_global_constant = NULL);
1108 bool ipa_load_from_parm_agg (struct ipa_func_body_info *fbi,
1109 			     vec<ipa_param_descriptor, va_gc> *descriptors,
1110 			     gimple *stmt, tree op, int *index_p,
1111 			     HOST_WIDE_INT *offset_p, poly_int64 *size_p,
1112 			     bool *by_ref, bool *guaranteed_unmodified = NULL);
1113 
1114 /* Debugging interface.  */
1115 void ipa_print_node_params (FILE *, struct cgraph_node *node);
1116 void ipa_print_all_params (FILE *);
1117 void ipa_print_node_jump_functions (FILE *f, struct cgraph_node *node);
1118 void ipa_print_all_jump_functions (FILE * f);
1119 void ipcp_verify_propagated_values (void);
1120 
1121 template <typename value>
1122 class ipcp_value;
1123 
1124 extern object_allocator<ipcp_value<tree> > ipcp_cst_values_pool;
1125 extern object_allocator<ipcp_value<ipa_polymorphic_call_context> >
1126   ipcp_poly_ctx_values_pool;
1127 
1128 template <typename valtype>
1129 struct ipcp_value_source;
1130 
1131 extern object_allocator<ipcp_value_source<tree> > ipcp_sources_pool;
1132 
1133 struct ipcp_agg_lattice;
1134 
1135 extern object_allocator<ipcp_agg_lattice> ipcp_agg_lattice_pool;
1136 
1137 void ipa_dump_agg_replacement_values (FILE *f,
1138 				      struct ipa_agg_replacement_value *av);
1139 void ipa_prop_write_jump_functions (void);
1140 void ipa_prop_read_jump_functions (void);
1141 void ipcp_write_transformation_summaries (void);
1142 void ipcp_read_transformation_summaries (void);
1143 int ipa_get_param_decl_index (class ipa_node_params *, tree);
1144 tree ipa_value_from_jfunc (class ipa_node_params *info,
1145 			   struct ipa_jump_func *jfunc, tree type);
1146 unsigned int ipcp_transform_function (struct cgraph_node *node);
1147 ipa_polymorphic_call_context ipa_context_from_jfunc (ipa_node_params *,
1148 						     cgraph_edge *,
1149 						     int,
1150 						     ipa_jump_func *);
1151 value_range ipa_value_range_from_jfunc (ipa_node_params *, cgraph_edge *,
1152 					ipa_jump_func *, tree);
1153 ipa_agg_value_set ipa_agg_value_set_from_jfunc (ipa_node_params *,
1154 						cgraph_node *,
1155 						ipa_agg_jump_function *);
1156 void ipa_dump_param (FILE *, class ipa_node_params *info, int i);
1157 void ipa_release_body_info (struct ipa_func_body_info *);
1158 tree ipa_get_callee_param_type (struct cgraph_edge *e, int i);
1159 bool ipcp_get_parm_bits (tree, tree *, widest_int *);
1160 bool unadjusted_ptr_and_unit_offset (tree op, tree *ret,
1161 				     poly_int64 *offset_ret);
1162 
1163 /* From tree-sra.c:  */
1164 tree build_ref_for_offset (location_t, tree, poly_int64, bool, tree,
1165 			   gimple_stmt_iterator *, bool);
1166 
1167 /* In ipa-cp.c  */
1168 void ipa_cp_c_finalize (void);
1169 
1170 #endif /* IPA_PROP_H */
1171