]>
Commit | Line | Data |
---|---|---|
518dc859 | 1 | /* Interprocedural constant propagation |
85ec4feb | 2 | Copyright (C) 2005-2018 Free Software Foundation, Inc. |
310bc633 MJ |
3 | |
4 | Contributed by Razya Ladelsky <RAZYA@il.ibm.com> and Martin Jambor | |
5 | <mjambor@suse.cz> | |
b8698a0f | 6 | |
518dc859 | 7 | This file is part of GCC. |
b8698a0f | 8 | |
518dc859 RL |
9 | GCC is free software; you can redistribute it and/or modify it under |
10 | the terms of the GNU General Public License as published by the Free | |
9dcd6f09 | 11 | Software Foundation; either version 3, or (at your option) any later |
518dc859 | 12 | version. |
b8698a0f | 13 | |
518dc859 RL |
14 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
15 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 | for more details. | |
b8698a0f | 18 | |
518dc859 | 19 | You should have received a copy of the GNU General Public License |
9dcd6f09 NC |
20 | along with GCC; see the file COPYING3. If not see |
21 | <http://www.gnu.org/licenses/>. */ | |
518dc859 | 22 | |
310bc633 | 23 | /* Interprocedural constant propagation (IPA-CP). |
b8698a0f | 24 | |
310bc633 | 25 | The goal of this transformation is to |
c43f07af | 26 | |
310bc633 MJ |
27 | 1) discover functions which are always invoked with some arguments with the |
28 | same known constant values and modify the functions so that the | |
29 | subsequent optimizations can take advantage of the knowledge, and | |
c43f07af | 30 | |
310bc633 MJ |
31 | 2) partial specialization - create specialized versions of functions |
32 | transformed in this way if some parameters are known constants only in | |
33 | certain contexts but the estimated tradeoff between speedup and cost size | |
34 | is deemed good. | |
b8698a0f | 35 | |
310bc633 MJ |
36 | The algorithm also propagates types and attempts to perform type based |
37 | devirtualization. Types are propagated much like constants. | |
b8698a0f | 38 | |
310bc633 MJ |
39 | The algorithm basically consists of three stages. In the first, functions |
40 | are analyzed one at a time and jump functions are constructed for all known | |
41 | call-sites. In the second phase, the pass propagates information from the | |
42 | jump functions across the call to reveal what values are available at what | |
43 | call sites, performs estimations of effects of known values on functions and | |
44 | their callees, and finally decides what specialized extra versions should be | |
45 | created. In the third, the special versions materialize and appropriate | |
46 | calls are redirected. | |
c43f07af | 47 | |
310bc633 MJ |
48 | The algorithm used is to a certain extent based on "Interprocedural Constant |
49 | Propagation", by David Callahan, Keith D Cooper, Ken Kennedy, Linda Torczon, | |
50 | Comp86, pg 152-161 and "A Methodology for Procedure Cloning" by Keith D | |
51 | Cooper, Mary W. Hall, and Ken Kennedy. | |
b8698a0f | 52 | |
518dc859 RL |
53 | |
54 | First stage - intraprocedural analysis | |
55 | ======================================= | |
310bc633 | 56 | |
c43f07af | 57 | This phase computes jump_function and modification flags. |
b8698a0f | 58 | |
310bc633 MJ |
59 | A jump function for a call-site represents the values passed as an actual |
60 | arguments of a given call-site. In principle, there are three types of | |
61 | values: | |
62 | ||
63 | Pass through - the caller's formal parameter is passed as an actual | |
155c9907 | 64 | argument, plus an operation on it can be performed. |
ea2c620c | 65 | Constant - a constant is passed as an actual argument. |
518dc859 | 66 | Unknown - neither of the above. |
b8698a0f | 67 | |
310bc633 MJ |
68 | All jump function types are described in detail in ipa-prop.h, together with |
69 | the data structures that represent them and methods of accessing them. | |
b8698a0f | 70 | |
310bc633 | 71 | ipcp_generate_summary() is the main function of the first stage. |
518dc859 RL |
72 | |
73 | Second stage - interprocedural analysis | |
74 | ======================================== | |
b8698a0f | 75 | |
310bc633 MJ |
76 | This stage is itself divided into two phases. In the first, we propagate |
77 | known values over the call graph, in the second, we make cloning decisions. | |
78 | It uses a different algorithm than the original Callahan's paper. | |
b8698a0f | 79 | |
310bc633 MJ |
80 | First, we traverse the functions topologically from callers to callees and, |
81 | for each strongly connected component (SCC), we propagate constants | |
82 | according to previously computed jump functions. We also record what known | |
83 | values depend on other known values and estimate local effects. Finally, we | |
073a8998 | 84 | propagate cumulative information about these effects from dependent values |
310bc633 | 85 | to those on which they depend. |
518dc859 | 86 | |
310bc633 MJ |
87 | Second, we again traverse the call graph in the same topological order and |
88 | make clones for functions which we know are called with the same values in | |
89 | all contexts and decide about extra specialized clones of functions just for | |
90 | some contexts - these decisions are based on both local estimates and | |
91 | cumulative estimates propagated from callees. | |
518dc859 | 92 | |
310bc633 MJ |
93 | ipcp_propagate_stage() and ipcp_decision_stage() together constitute the |
94 | third stage. | |
95 | ||
96 | Third phase - materialization of clones, call statement updates. | |
518dc859 | 97 | ============================================ |
310bc633 MJ |
98 | |
99 | This stage is currently performed by call graph code (mainly in cgraphunit.c | |
100 | and tree-inline.c) according to instructions inserted to the call graph by | |
101 | the second stage. */ | |
518dc859 RL |
102 | |
103 | #include "config.h" | |
104 | #include "system.h" | |
105 | #include "coretypes.h" | |
957060b5 | 106 | #include "backend.h" |
518dc859 | 107 | #include "tree.h" |
2fb9a547 | 108 | #include "gimple-expr.h" |
9fdcd34e | 109 | #include "predict.h" |
c582198b | 110 | #include "alloc-pool.h" |
957060b5 AM |
111 | #include "tree-pass.h" |
112 | #include "cgraph.h" | |
113 | #include "diagnostic.h" | |
957060b5 AM |
114 | #include "fold-const.h" |
115 | #include "gimple-fold.h" | |
dd912cb8 | 116 | #include "symbol-summary.h" |
8bc5448f | 117 | #include "tree-vrp.h" |
518dc859 | 118 | #include "ipa-prop.h" |
cf835838 | 119 | #include "tree-pretty-print.h" |
3cc1cccc | 120 | #include "tree-inline.h" |
5e45130d | 121 | #include "params.h" |
27d020cf | 122 | #include "ipa-fnsummary.h" |
310bc633 | 123 | #include "ipa-utils.h" |
209ca542 | 124 | #include "tree-ssa-ccp.h" |
314e6352 ML |
125 | #include "stringpool.h" |
126 | #include "attribs.h" | |
518dc859 | 127 | |
c0cb5055 | 128 | template <typename valtype> class ipcp_value; |
ca30a539 | 129 | |
310bc633 | 130 | /* Describes a particular source for an IPA-CP value. */ |
ca30a539 | 131 | |
c0cb5055 MJ |
132 | template <typename valtype> |
133 | class ipcp_value_source | |
310bc633 | 134 | { |
c0cb5055 | 135 | public: |
2c9561b5 MJ |
136 | /* Aggregate offset of the source, negative if the source is scalar value of |
137 | the argument itself. */ | |
138 | HOST_WIDE_INT offset; | |
310bc633 | 139 | /* The incoming edge that brought the value. */ |
c0cb5055 | 140 | cgraph_edge *cs; |
310bc633 MJ |
141 | /* If the jump function that resulted into his value was a pass-through or an |
142 | ancestor, this is the ipcp_value of the caller from which the described | |
143 | value has been derived. Otherwise it is NULL. */ | |
c0cb5055 | 144 | ipcp_value<valtype> *val; |
310bc633 | 145 | /* Next pointer in a linked list of sources of a value. */ |
c0cb5055 | 146 | ipcp_value_source *next; |
310bc633 MJ |
147 | /* If the jump function that resulted into his value was a pass-through or an |
148 | ancestor, this is the index of the parameter of the caller the jump | |
149 | function references. */ | |
150 | int index; | |
151 | }; | |
ca30a539 | 152 | |
c0cb5055 MJ |
153 | /* Common ancestor for all ipcp_value instantiations. */ |
154 | ||
155 | class ipcp_value_base | |
156 | { | |
157 | public: | |
158 | /* Time benefit and size cost that specializing the function for this value | |
159 | would bring about in this function alone. */ | |
160 | int local_time_benefit, local_size_cost; | |
161 | /* Time benefit and size cost that specializing the function for this value | |
162 | can bring about in it's callees (transitively). */ | |
163 | int prop_time_benefit, prop_size_cost; | |
c8fb20d8 YG |
164 | |
165 | ipcp_value_base () | |
166 | : local_time_benefit (0), local_size_cost (0), | |
167 | prop_time_benefit (0), prop_size_cost (0) {} | |
c0cb5055 MJ |
168 | }; |
169 | ||
310bc633 | 170 | /* Describes one particular value stored in struct ipcp_lattice. */ |
ca30a539 | 171 | |
c0cb5055 MJ |
172 | template <typename valtype> |
173 | class ipcp_value : public ipcp_value_base | |
518dc859 | 174 | { |
c0cb5055 MJ |
175 | public: |
176 | /* The actual value for the given parameter. */ | |
177 | valtype value; | |
310bc633 | 178 | /* The list of sources from which this value originates. */ |
c0cb5055 | 179 | ipcp_value_source <valtype> *sources; |
310bc633 | 180 | /* Next pointers in a linked list of all values in a lattice. */ |
c0cb5055 | 181 | ipcp_value *next; |
310bc633 MJ |
182 | /* Next pointers in a linked list of values in a strongly connected component |
183 | of values. */ | |
c0cb5055 | 184 | ipcp_value *scc_next; |
310bc633 MJ |
185 | /* Next pointers in a linked list of SCCs of values sorted topologically |
186 | according their sources. */ | |
c0cb5055 | 187 | ipcp_value *topo_next; |
310bc633 MJ |
188 | /* A specialized node created for this value, NULL if none has been (so far) |
189 | created. */ | |
c0cb5055 | 190 | cgraph_node *spec_node; |
310bc633 MJ |
191 | /* Depth first search number and low link for topological sorting of |
192 | values. */ | |
193 | int dfs, low_link; | |
310bc633 MJ |
194 | /* True if this valye is currently on the topo-sort stack. */ |
195 | bool on_stack; | |
c0cb5055 | 196 | |
c8fb20d8 YG |
197 | ipcp_value() |
198 | : sources (0), next (0), scc_next (0), topo_next (0), | |
199 | spec_node (0), dfs (0), low_link (0), on_stack (false) {} | |
200 | ||
c0cb5055 MJ |
201 | void add_source (cgraph_edge *cs, ipcp_value *src_val, int src_idx, |
202 | HOST_WIDE_INT offset); | |
310bc633 | 203 | }; |
518dc859 | 204 | |
2c9561b5 | 205 | /* Lattice describing potential values of a formal parameter of a function, or |
5764ee3c | 206 | a part of an aggregate. TOP is represented by a lattice with zero values |
2c9561b5 MJ |
207 | and with contains_variable and bottom flags cleared. BOTTOM is represented |
208 | by a lattice with the bottom flag set. In that case, values and | |
310bc633 MJ |
209 | contains_variable flag should be disregarded. */ |
210 | ||
c0cb5055 MJ |
211 | template <typename valtype> |
212 | class ipcp_lattice | |
518dc859 | 213 | { |
c0cb5055 | 214 | public: |
310bc633 MJ |
215 | /* The list of known values and types in this lattice. Note that values are |
216 | not deallocated if a lattice is set to bottom because there may be value | |
217 | sources referencing them. */ | |
c0cb5055 | 218 | ipcp_value<valtype> *values; |
310bc633 MJ |
219 | /* Number of known values and types in this lattice. */ |
220 | int values_count; | |
2c9561b5 | 221 | /* The lattice contains a variable component (in addition to values). */ |
310bc633 MJ |
222 | bool contains_variable; |
223 | /* The value of the lattice is bottom (i.e. variable and unusable for any | |
224 | propagation). */ | |
225 | bool bottom; | |
c0cb5055 MJ |
226 | |
227 | inline bool is_single_const (); | |
228 | inline bool set_to_bottom (); | |
229 | inline bool set_contains_variable (); | |
230 | bool add_value (valtype newval, cgraph_edge *cs, | |
231 | ipcp_value<valtype> *src_val = NULL, | |
232 | int src_idx = 0, HOST_WIDE_INT offset = -1); | |
233 | void print (FILE * f, bool dump_sources, bool dump_benefits); | |
2c9561b5 MJ |
234 | }; |
235 | ||
c0cb5055 MJ |
236 | /* Lattice of tree values with an offset to describe a part of an |
237 | aggregate. */ | |
2c9561b5 | 238 | |
c0cb5055 | 239 | class ipcp_agg_lattice : public ipcp_lattice<tree> |
2c9561b5 | 240 | { |
c0cb5055 | 241 | public: |
2c9561b5 MJ |
242 | /* Offset that is being described by this lattice. */ |
243 | HOST_WIDE_INT offset; | |
244 | /* Size so that we don't have to re-compute it every time we traverse the | |
245 | list. Must correspond to TYPE_SIZE of all lat values. */ | |
246 | HOST_WIDE_INT size; | |
247 | /* Next element of the linked list. */ | |
248 | struct ipcp_agg_lattice *next; | |
249 | }; | |
250 | ||
209ca542 PK |
251 | /* Lattice of known bits, only capable of holding one value. |
252 | Bitwise constant propagation propagates which bits of a | |
253 | value are constant. | |
254 | For eg: | |
255 | int f(int x) | |
256 | { | |
257 | return some_op (x); | |
258 | } | |
259 | ||
260 | int f1(int y) | |
261 | { | |
262 | if (cond) | |
263 | return f (y & 0xff); | |
264 | else | |
265 | return f (y & 0xf); | |
266 | } | |
267 | ||
268 | In the above case, the param 'x' will always have all | |
269 | the bits (except the bits in lsb) set to 0. | |
270 | Hence the mask of 'x' would be 0xff. The mask | |
271 | reflects that the bits in lsb are unknown. | |
272 | The actual propagated value is given by m_value & ~m_mask. */ | |
273 | ||
274 | class ipcp_bits_lattice | |
275 | { | |
276 | public: | |
277 | bool bottom_p () { return m_lattice_val == IPA_BITS_VARYING; } | |
278 | bool top_p () { return m_lattice_val == IPA_BITS_UNDEFINED; } | |
279 | bool constant_p () { return m_lattice_val == IPA_BITS_CONSTANT; } | |
280 | bool set_to_bottom (); | |
155c9907 JJ |
281 | bool set_to_constant (widest_int, widest_int); |
282 | ||
209ca542 PK |
283 | widest_int get_value () { return m_value; } |
284 | widest_int get_mask () { return m_mask; } | |
285 | ||
286 | bool meet_with (ipcp_bits_lattice& other, unsigned, signop, | |
287 | enum tree_code, tree); | |
288 | ||
289 | bool meet_with (widest_int, widest_int, unsigned); | |
290 | ||
291 | void print (FILE *); | |
292 | ||
293 | private: | |
294 | enum { IPA_BITS_UNDEFINED, IPA_BITS_CONSTANT, IPA_BITS_VARYING } m_lattice_val; | |
295 | ||
296 | /* Similar to ccp_lattice_t, mask represents which bits of value are constant. | |
297 | If a bit in mask is set to 0, then the corresponding bit in | |
298 | value is known to be constant. */ | |
299 | widest_int m_value, m_mask; | |
300 | ||
155c9907 | 301 | bool meet_with_1 (widest_int, widest_int, unsigned); |
209ca542 | 302 | void get_value_and_mask (tree, widest_int *, widest_int *); |
155c9907 | 303 | }; |
209ca542 | 304 | |
8bc5448f KV |
305 | /* Lattice of value ranges. */ |
306 | ||
307 | class ipcp_vr_lattice | |
308 | { | |
309 | public: | |
310 | value_range m_vr; | |
311 | ||
312 | inline bool bottom_p () const; | |
313 | inline bool top_p () const; | |
314 | inline bool set_to_bottom (); | |
315 | bool meet_with (const value_range *p_vr); | |
316 | bool meet_with (const ipcp_vr_lattice &other); | |
317 | void init () { m_vr.type = VR_UNDEFINED; } | |
318 | void print (FILE * f); | |
319 | ||
320 | private: | |
321 | bool meet_with_1 (const value_range *other_vr); | |
322 | }; | |
323 | ||
2c9561b5 MJ |
324 | /* Structure containing lattices for a parameter itself and for pieces of |
325 | aggregates that are passed in the parameter or by a reference in a parameter | |
326 | plus some other useful flags. */ | |
327 | ||
c0cb5055 | 328 | class ipcp_param_lattices |
2c9561b5 | 329 | { |
c0cb5055 | 330 | public: |
2c9561b5 | 331 | /* Lattice describing the value of the parameter itself. */ |
c0cb5055 | 332 | ipcp_lattice<tree> itself; |
026c3cfd | 333 | /* Lattice describing the polymorphic contexts of a parameter. */ |
44210a96 | 334 | ipcp_lattice<ipa_polymorphic_call_context> ctxlat; |
2c9561b5 | 335 | /* Lattices describing aggregate parts. */ |
c0cb5055 | 336 | ipcp_agg_lattice *aggs; |
209ca542 PK |
337 | /* Lattice describing known bits. */ |
338 | ipcp_bits_lattice bits_lattice; | |
8bc5448f KV |
339 | /* Lattice describing value range. */ |
340 | ipcp_vr_lattice m_value_range; | |
2c9561b5 MJ |
341 | /* Number of aggregate lattices */ |
342 | int aggs_count; | |
343 | /* True if aggregate data were passed by reference (as opposed to by | |
344 | value). */ | |
345 | bool aggs_by_ref; | |
346 | /* All aggregate lattices contain a variable component (in addition to | |
347 | values). */ | |
348 | bool aggs_contain_variable; | |
349 | /* The value of all aggregate lattices is bottom (i.e. variable and unusable | |
350 | for any propagation). */ | |
351 | bool aggs_bottom; | |
352 | ||
310bc633 MJ |
353 | /* There is a virtual call based on this parameter. */ |
354 | bool virt_call; | |
355 | }; | |
518dc859 | 356 | |
2c9561b5 MJ |
357 | /* Allocation pools for values and their sources in ipa-cp. */ |
358 | ||
fb0b2914 | 359 | object_allocator<ipcp_value<tree> > ipcp_cst_values_pool |
fcb87c50 | 360 | ("IPA-CP constant values"); |
2651e637 | 361 | |
fb0b2914 | 362 | object_allocator<ipcp_value<ipa_polymorphic_call_context> > |
fcb87c50 | 363 | ipcp_poly_ctx_values_pool ("IPA-CP polymorphic contexts"); |
2651e637 | 364 | |
fb0b2914 | 365 | object_allocator<ipcp_value_source<tree> > ipcp_sources_pool |
fcb87c50 | 366 | ("IPA-CP value sources"); |
2651e637 | 367 | |
fb0b2914 | 368 | object_allocator<ipcp_agg_lattice> ipcp_agg_lattice_pool |
fcb87c50 | 369 | ("IPA_CP aggregate lattices"); |
2c9561b5 | 370 | |
310bc633 MJ |
371 | /* Maximal count found in program. */ |
372 | ||
3995f3a2 | 373 | static profile_count max_count; |
310bc633 MJ |
374 | |
375 | /* Original overall size of the program. */ | |
376 | ||
377 | static long overall_size, max_new_size; | |
378 | ||
2c9561b5 MJ |
379 | /* Return the param lattices structure corresponding to the Ith formal |
380 | parameter of the function described by INFO. */ | |
381 | static inline struct ipcp_param_lattices * | |
382 | ipa_get_parm_lattices (struct ipa_node_params *info, int i) | |
518dc859 | 383 | { |
d7da5cc8 | 384 | gcc_assert (i >= 0 && i < ipa_get_param_count (info)); |
310bc633 MJ |
385 | gcc_checking_assert (!info->ipcp_orig_node); |
386 | gcc_checking_assert (info->lattices); | |
387 | return &(info->lattices[i]); | |
518dc859 RL |
388 | } |
389 | ||
2c9561b5 MJ |
390 | /* Return the lattice corresponding to the scalar value of the Ith formal |
391 | parameter of the function described by INFO. */ | |
c0cb5055 | 392 | static inline ipcp_lattice<tree> * |
2c9561b5 MJ |
393 | ipa_get_scalar_lat (struct ipa_node_params *info, int i) |
394 | { | |
395 | struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i); | |
396 | return &plats->itself; | |
397 | } | |
398 | ||
44210a96 MJ |
399 | /* Return the lattice corresponding to the scalar value of the Ith formal |
400 | parameter of the function described by INFO. */ | |
401 | static inline ipcp_lattice<ipa_polymorphic_call_context> * | |
402 | ipa_get_poly_ctx_lat (struct ipa_node_params *info, int i) | |
403 | { | |
404 | struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i); | |
405 | return &plats->ctxlat; | |
406 | } | |
407 | ||
8bc5448f KV |
408 | /* Return the lattice corresponding to the value range of the Ith formal |
409 | parameter of the function described by INFO. */ | |
410 | ||
411 | static inline ipcp_vr_lattice * | |
412 | ipa_get_vr_lat (struct ipa_node_params *info, int i) | |
413 | { | |
414 | struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i); | |
415 | return &plats->m_value_range; | |
416 | } | |
417 | ||
310bc633 MJ |
418 | /* Return whether LAT is a lattice with a single constant and without an |
419 | undefined value. */ | |
420 | ||
c0cb5055 MJ |
421 | template <typename valtype> |
422 | inline bool | |
423 | ipcp_lattice<valtype>::is_single_const () | |
518dc859 | 424 | { |
c0cb5055 | 425 | if (bottom || contains_variable || values_count != 1) |
518dc859 | 426 | return false; |
310bc633 MJ |
427 | else |
428 | return true; | |
518dc859 RL |
429 | } |
430 | ||
310bc633 MJ |
431 | /* Print V which is extracted from a value in a lattice to F. */ |
432 | ||
518dc859 | 433 | static void |
310bc633 | 434 | print_ipcp_constant_value (FILE * f, tree v) |
518dc859 | 435 | { |
3b97a5c7 | 436 | if (TREE_CODE (v) == ADDR_EXPR |
155c9907 | 437 | && TREE_CODE (TREE_OPERAND (v, 0)) == CONST_DECL) |
518dc859 | 438 | { |
310bc633 | 439 | fprintf (f, "& "); |
ef6cb4c7 | 440 | print_generic_expr (f, DECL_INITIAL (TREE_OPERAND (v, 0))); |
518dc859 | 441 | } |
310bc633 | 442 | else |
ef6cb4c7 | 443 | print_generic_expr (f, v); |
518dc859 RL |
444 | } |
445 | ||
44210a96 MJ |
446 | /* Print V which is extracted from a value in a lattice to F. */ |
447 | ||
448 | static void | |
449 | print_ipcp_constant_value (FILE * f, ipa_polymorphic_call_context v) | |
450 | { | |
451 | v.dump(f, false); | |
452 | } | |
453 | ||
2c9561b5 MJ |
454 | /* Print a lattice LAT to F. */ |
455 | ||
c0cb5055 MJ |
456 | template <typename valtype> |
457 | void | |
458 | ipcp_lattice<valtype>::print (FILE * f, bool dump_sources, bool dump_benefits) | |
2c9561b5 | 459 | { |
c0cb5055 | 460 | ipcp_value<valtype> *val; |
2c9561b5 MJ |
461 | bool prev = false; |
462 | ||
c0cb5055 | 463 | if (bottom) |
2c9561b5 MJ |
464 | { |
465 | fprintf (f, "BOTTOM\n"); | |
466 | return; | |
467 | } | |
468 | ||
c0cb5055 | 469 | if (!values_count && !contains_variable) |
2c9561b5 MJ |
470 | { |
471 | fprintf (f, "TOP\n"); | |
472 | return; | |
473 | } | |
474 | ||
c0cb5055 | 475 | if (contains_variable) |
2c9561b5 MJ |
476 | { |
477 | fprintf (f, "VARIABLE"); | |
478 | prev = true; | |
479 | if (dump_benefits) | |
480 | fprintf (f, "\n"); | |
481 | } | |
482 | ||
c0cb5055 | 483 | for (val = values; val; val = val->next) |
2c9561b5 MJ |
484 | { |
485 | if (dump_benefits && prev) | |
486 | fprintf (f, " "); | |
487 | else if (!dump_benefits && prev) | |
488 | fprintf (f, ", "); | |
489 | else | |
490 | prev = true; | |
491 | ||
492 | print_ipcp_constant_value (f, val->value); | |
493 | ||
494 | if (dump_sources) | |
495 | { | |
c0cb5055 | 496 | ipcp_value_source<valtype> *s; |
2c9561b5 MJ |
497 | |
498 | fprintf (f, " [from:"); | |
499 | for (s = val->sources; s; s = s->next) | |
e3951b03 JH |
500 | fprintf (f, " %i(%f)", s->cs->caller->order, |
501 | s->cs->sreal_frequency ().to_double ()); | |
2c9561b5 MJ |
502 | fprintf (f, "]"); |
503 | } | |
504 | ||
505 | if (dump_benefits) | |
506 | fprintf (f, " [loc_time: %i, loc_size: %i, " | |
507 | "prop_time: %i, prop_size: %i]\n", | |
508 | val->local_time_benefit, val->local_size_cost, | |
509 | val->prop_time_benefit, val->prop_size_cost); | |
510 | } | |
511 | if (!dump_benefits) | |
512 | fprintf (f, "\n"); | |
513 | } | |
514 | ||
209ca542 PK |
515 | void |
516 | ipcp_bits_lattice::print (FILE *f) | |
517 | { | |
518 | if (top_p ()) | |
519 | fprintf (f, " Bits unknown (TOP)\n"); | |
520 | else if (bottom_p ()) | |
521 | fprintf (f, " Bits unusable (BOTTOM)\n"); | |
522 | else | |
523 | { | |
524 | fprintf (f, " Bits: value = "); print_hex (get_value (), f); | |
525 | fprintf (f, ", mask = "); print_hex (get_mask (), f); | |
526 | fprintf (f, "\n"); | |
527 | } | |
528 | } | |
529 | ||
8bc5448f KV |
530 | /* Print value range lattice to F. */ |
531 | ||
532 | void | |
533 | ipcp_vr_lattice::print (FILE * f) | |
534 | { | |
535 | dump_value_range (f, &m_vr); | |
536 | } | |
537 | ||
c43f07af | 538 | /* Print all ipcp_lattices of all functions to F. */ |
310bc633 | 539 | |
518dc859 | 540 | static void |
310bc633 | 541 | print_all_lattices (FILE * f, bool dump_sources, bool dump_benefits) |
518dc859 RL |
542 | { |
543 | struct cgraph_node *node; | |
544 | int i, count; | |
3cc1cccc | 545 | |
310bc633 MJ |
546 | fprintf (f, "\nLattices:\n"); |
547 | FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node) | |
518dc859 | 548 | { |
0eae6bab MJ |
549 | struct ipa_node_params *info; |
550 | ||
0eae6bab | 551 | info = IPA_NODE_REF (node); |
464d0118 | 552 | fprintf (f, " Node: %s:\n", node->dump_name ()); |
c43f07af | 553 | count = ipa_get_param_count (info); |
518dc859 RL |
554 | for (i = 0; i < count; i++) |
555 | { | |
2c9561b5 MJ |
556 | struct ipcp_agg_lattice *aglat; |
557 | struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i); | |
ca30a539 | 558 | fprintf (f, " param [%d]: ", i); |
c0cb5055 | 559 | plats->itself.print (f, dump_sources, dump_benefits); |
44210a96 MJ |
560 | fprintf (f, " ctxs: "); |
561 | plats->ctxlat.print (f, dump_sources, dump_benefits); | |
209ca542 | 562 | plats->bits_lattice.print (f); |
8bc5448f KV |
563 | fprintf (f, " "); |
564 | plats->m_value_range.print (f); | |
565 | fprintf (f, "\n"); | |
2c9561b5 MJ |
566 | if (plats->virt_call) |
567 | fprintf (f, " virt_call flag set\n"); | |
568 | ||
569 | if (plats->aggs_bottom) | |
310bc633 | 570 | { |
2c9561b5 | 571 | fprintf (f, " AGGS BOTTOM\n"); |
310bc633 MJ |
572 | continue; |
573 | } | |
2c9561b5 MJ |
574 | if (plats->aggs_contain_variable) |
575 | fprintf (f, " AGGS VARIABLE\n"); | |
576 | for (aglat = plats->aggs; aglat; aglat = aglat->next) | |
310bc633 | 577 | { |
2c9561b5 MJ |
578 | fprintf (f, " %soffset " HOST_WIDE_INT_PRINT_DEC ": ", |
579 | plats->aggs_by_ref ? "ref " : "", aglat->offset); | |
c0cb5055 | 580 | aglat->print (f, dump_sources, dump_benefits); |
310bc633 | 581 | } |
518dc859 RL |
582 | } |
583 | } | |
584 | } | |
585 | ||
310bc633 MJ |
586 | /* Determine whether it is at all technically possible to create clones of NODE |
587 | and store this information in the ipa_node_params structure associated | |
588 | with NODE. */ | |
27dbd3ac | 589 | |
310bc633 | 590 | static void |
7e729474 ML |
591 | determine_versionability (struct cgraph_node *node, |
592 | struct ipa_node_params *info) | |
27dbd3ac | 593 | { |
310bc633 | 594 | const char *reason = NULL; |
0818c24c | 595 | |
aa229804 MJ |
596 | /* There are a number of generic reasons functions cannot be versioned. We |
597 | also cannot remove parameters if there are type attributes such as fnspec | |
598 | present. */ | |
67348ccc | 599 | if (node->alias || node->thunk.thunk_p) |
310bc633 | 600 | reason = "alias or thunk"; |
124f1be6 | 601 | else if (!node->local.versionable) |
d7da5cc8 | 602 | reason = "not a tree_versionable_function"; |
d52f5295 | 603 | else if (node->get_availability () <= AVAIL_INTERPOSABLE) |
310bc633 | 604 | reason = "insufficient body availability"; |
d31d42c7 JJ |
605 | else if (!opt_for_fn (node->decl, optimize) |
606 | || !opt_for_fn (node->decl, flag_ipa_cp)) | |
607 | reason = "non-optimized function"; | |
0136f8f0 AH |
608 | else if (lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (node->decl))) |
609 | { | |
610 | /* Ideally we should clone the SIMD clones themselves and create | |
611 | vector copies of them, so IPA-cp and SIMD clones can happily | |
612 | coexist, but that may not be worth the effort. */ | |
613 | reason = "function has SIMD clones"; | |
614 | } | |
58b3986e ES |
615 | else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (node->decl))) |
616 | { | |
617 | /* Ideally we should clone the target clones themselves and create | |
618 | copies of them, so IPA-cp and target clones can happily | |
619 | coexist, but that may not be worth the effort. */ | |
620 | reason = "function target_clones attribute"; | |
621 | } | |
1f26ac87 JM |
622 | /* Don't clone decls local to a comdat group; it breaks and for C++ |
623 | decloned constructors, inlining is always better anyway. */ | |
d52f5295 | 624 | else if (node->comdat_local_p ()) |
1f26ac87 | 625 | reason = "comdat-local function"; |
58928b35 ML |
626 | else if (node->calls_comdat_local) |
627 | { | |
628 | /* TODO: call is versionable if we make sure that all | |
629 | callers are inside of a comdat group. */ | |
630 | reason = "calls comdat-local function"; | |
631 | } | |
27dbd3ac | 632 | |
ea49d40b | 633 | /* Functions calling BUILT_IN_VA_ARG_PACK and BUILT_IN_VA_ARG_PACK_LEN |
5d4991da JH |
634 | work only when inlined. Cloning them may still lead to better code |
635 | because ipa-cp will not give up on cloning further. If the function is | |
636 | external this however leads to wrong code because we may end up producing | |
ea49d40b JH |
637 | offline copy of the function. */ |
638 | if (DECL_EXTERNAL (node->decl)) | |
639 | for (cgraph_edge *edge = node->callees; !reason && edge; | |
640 | edge = edge->next_callee) | |
641 | if (DECL_BUILT_IN (edge->callee->decl) | |
642 | && DECL_BUILT_IN_CLASS (edge->callee->decl) == BUILT_IN_NORMAL) | |
643 | { | |
644 | if (DECL_FUNCTION_CODE (edge->callee->decl) == BUILT_IN_VA_ARG_PACK) | |
645 | reason = "external function which calls va_arg_pack"; | |
646 | if (DECL_FUNCTION_CODE (edge->callee->decl) | |
647 | == BUILT_IN_VA_ARG_PACK_LEN) | |
648 | reason = "external function which calls va_arg_pack_len"; | |
649 | } | |
650 | ||
67348ccc | 651 | if (reason && dump_file && !node->alias && !node->thunk.thunk_p) |
464d0118 ML |
652 | fprintf (dump_file, "Function %s is not versionable, reason: %s.\n", |
653 | node->dump_name (), reason); | |
27dbd3ac | 654 | |
7e729474 | 655 | info->versionable = (reason == NULL); |
27dbd3ac RH |
656 | } |
657 | ||
310bc633 MJ |
658 | /* Return true if it is at all technically possible to create clones of a |
659 | NODE. */ | |
660 | ||
ca30a539 | 661 | static bool |
310bc633 | 662 | ipcp_versionable_function_p (struct cgraph_node *node) |
ca30a539 | 663 | { |
7e729474 | 664 | return IPA_NODE_REF (node)->versionable; |
310bc633 | 665 | } |
ca30a539 | 666 | |
310bc633 | 667 | /* Structure holding accumulated information about callers of a node. */ |
749f25d8 | 668 | |
310bc633 MJ |
669 | struct caller_statistics |
670 | { | |
3995f3a2 | 671 | profile_count count_sum; |
310bc633 MJ |
672 | int n_calls, n_hot_calls, freq_sum; |
673 | }; | |
ca30a539 | 674 | |
310bc633 | 675 | /* Initialize fields of STAT to zeroes. */ |
530f3a1b | 676 | |
310bc633 MJ |
677 | static inline void |
678 | init_caller_stats (struct caller_statistics *stats) | |
679 | { | |
3995f3a2 | 680 | stats->count_sum = profile_count::zero (); |
310bc633 MJ |
681 | stats->n_calls = 0; |
682 | stats->n_hot_calls = 0; | |
683 | stats->freq_sum = 0; | |
684 | } | |
685 | ||
686 | /* Worker callback of cgraph_for_node_and_aliases accumulating statistics of | |
687 | non-thunk incoming edges to NODE. */ | |
688 | ||
689 | static bool | |
690 | gather_caller_stats (struct cgraph_node *node, void *data) | |
691 | { | |
692 | struct caller_statistics *stats = (struct caller_statistics *) data; | |
693 | struct cgraph_edge *cs; | |
694 | ||
695 | for (cs = node->callers; cs; cs = cs->next_caller) | |
94a2f772 | 696 | if (!cs->caller->thunk.thunk_p) |
310bc633 | 697 | { |
1bad9c18 JH |
698 | if (cs->count.ipa ().initialized_p ()) |
699 | stats->count_sum += cs->count.ipa (); | |
700 | stats->freq_sum += cs->frequency (); | |
310bc633 | 701 | stats->n_calls++; |
3dafb85c | 702 | if (cs->maybe_hot_p ()) |
310bc633 MJ |
703 | stats->n_hot_calls ++; |
704 | } | |
705 | return false; | |
706 | ||
707 | } | |
708 | ||
709 | /* Return true if this NODE is viable candidate for cloning. */ | |
710 | ||
711 | static bool | |
712 | ipcp_cloning_candidate_p (struct cgraph_node *node) | |
713 | { | |
714 | struct caller_statistics stats; | |
715 | ||
d52f5295 | 716 | gcc_checking_assert (node->has_gimple_body_p ()); |
b8698a0f | 717 | |
2bf86c84 | 718 | if (!opt_for_fn (node->decl, flag_ipa_cp_clone)) |
ca30a539 JH |
719 | { |
720 | if (dump_file) | |
155c9907 | 721 | fprintf (dump_file, "Not considering %s for cloning; " |
310bc633 | 722 | "-fipa-cp-clone disabled.\n", |
155c9907 | 723 | node->name ()); |
ca30a539 JH |
724 | return false; |
725 | } | |
ca30a539 | 726 | |
5af56ae8 | 727 | if (node->optimize_for_size_p ()) |
ca30a539 JH |
728 | { |
729 | if (dump_file) | |
155c9907 | 730 | fprintf (dump_file, "Not considering %s for cloning; " |
310bc633 | 731 | "optimizing it for size.\n", |
155c9907 | 732 | node->name ()); |
ca30a539 JH |
733 | return false; |
734 | } | |
735 | ||
310bc633 | 736 | init_caller_stats (&stats); |
d52f5295 | 737 | node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats, false); |
310bc633 | 738 | |
0bceb671 | 739 | if (ipa_fn_summaries->get (node)->self_size < stats.n_calls) |
ca30a539 JH |
740 | { |
741 | if (dump_file) | |
155c9907 JJ |
742 | fprintf (dump_file, "Considering %s for cloning; code might shrink.\n", |
743 | node->name ()); | |
310bc633 | 744 | return true; |
ca30a539 JH |
745 | } |
746 | ||
747 | /* When profile is available and function is hot, propagate into it even if | |
748 | calls seems cold; constant propagation can improve function's speed | |
61502ca8 | 749 | significantly. */ |
3995f3a2 | 750 | if (max_count > profile_count::zero ()) |
ca30a539 | 751 | { |
1bad9c18 | 752 | if (stats.count_sum > node->count.ipa ().apply_scale (90, 100)) |
ca30a539 JH |
753 | { |
754 | if (dump_file) | |
310bc633 MJ |
755 | fprintf (dump_file, "Considering %s for cloning; " |
756 | "usually called directly.\n", | |
fec39fa6 | 757 | node->name ()); |
ca30a539 | 758 | return true; |
155c9907 | 759 | } |
ca30a539 | 760 | } |
310bc633 | 761 | if (!stats.n_hot_calls) |
ca30a539 JH |
762 | { |
763 | if (dump_file) | |
764 | fprintf (dump_file, "Not considering %s for cloning; no hot calls.\n", | |
fec39fa6 | 765 | node->name ()); |
ed102b70 | 766 | return false; |
ca30a539 JH |
767 | } |
768 | if (dump_file) | |
769 | fprintf (dump_file, "Considering %s for cloning.\n", | |
fec39fa6 | 770 | node->name ()); |
ca30a539 JH |
771 | return true; |
772 | } | |
773 | ||
c0cb5055 MJ |
774 | template <typename valtype> |
775 | class value_topo_info | |
776 | { | |
777 | public: | |
778 | /* Head of the linked list of topologically sorted values. */ | |
779 | ipcp_value<valtype> *values_topo; | |
780 | /* Stack for creating SCCs, represented by a linked list too. */ | |
781 | ipcp_value<valtype> *stack; | |
782 | /* Counter driving the algorithm in add_val_to_toposort. */ | |
783 | int dfs_counter; | |
784 | ||
785 | value_topo_info () : values_topo (NULL), stack (NULL), dfs_counter (0) | |
786 | {} | |
787 | void add_val (ipcp_value<valtype> *cur_val); | |
788 | void propagate_effects (); | |
789 | }; | |
790 | ||
310bc633 | 791 | /* Arrays representing a topological ordering of call graph nodes and a stack |
c0cb5055 MJ |
792 | of nodes used during constant propagation and also data required to perform |
793 | topological sort of values and propagation of benefits in the determined | |
794 | order. */ | |
3949c4a7 | 795 | |
c0cb5055 | 796 | class ipa_topo_info |
3949c4a7 | 797 | { |
c0cb5055 MJ |
798 | public: |
799 | /* Array with obtained topological order of cgraph nodes. */ | |
310bc633 | 800 | struct cgraph_node **order; |
c0cb5055 MJ |
801 | /* Stack of cgraph nodes used during propagation within SCC until all values |
802 | in the SCC stabilize. */ | |
310bc633 MJ |
803 | struct cgraph_node **stack; |
804 | int nnodes, stack_top; | |
c0cb5055 MJ |
805 | |
806 | value_topo_info<tree> constants; | |
44210a96 | 807 | value_topo_info<ipa_polymorphic_call_context> contexts; |
c0cb5055 MJ |
808 | |
809 | ipa_topo_info () : order(NULL), stack(NULL), nnodes(0), stack_top(0), | |
810 | constants () | |
811 | {} | |
310bc633 MJ |
812 | }; |
813 | ||
814 | /* Allocate the arrays in TOPO and topologically sort the nodes into order. */ | |
815 | ||
816 | static void | |
11478306 | 817 | build_toporder_info (struct ipa_topo_info *topo) |
310bc633 | 818 | { |
3dafb85c ML |
819 | topo->order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count); |
820 | topo->stack = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count); | |
821 | ||
c0cb5055 | 822 | gcc_checking_assert (topo->stack_top == 0); |
310bc633 | 823 | topo->nnodes = ipa_reduced_postorder (topo->order, true, true, NULL); |
3949c4a7 MJ |
824 | } |
825 | ||
310bc633 MJ |
826 | /* Free information about strongly connected components and the arrays in |
827 | TOPO. */ | |
828 | ||
518dc859 | 829 | static void |
11478306 | 830 | free_toporder_info (struct ipa_topo_info *topo) |
310bc633 MJ |
831 | { |
832 | ipa_free_postorder_info (); | |
833 | free (topo->order); | |
834 | free (topo->stack); | |
835 | } | |
836 | ||
837 | /* Add NODE to the stack in TOPO, unless it is already there. */ | |
838 | ||
839 | static inline void | |
11478306 | 840 | push_node_to_stack (struct ipa_topo_info *topo, struct cgraph_node *node) |
518dc859 | 841 | { |
c43f07af | 842 | struct ipa_node_params *info = IPA_NODE_REF (node); |
310bc633 MJ |
843 | if (info->node_enqueued) |
844 | return; | |
845 | info->node_enqueued = 1; | |
846 | topo->stack[topo->stack_top++] = node; | |
847 | } | |
518dc859 | 848 | |
310bc633 MJ |
849 | /* Pop a node from the stack in TOPO and return it or return NULL if the stack |
850 | is empty. */ | |
ca30a539 | 851 | |
310bc633 | 852 | static struct cgraph_node * |
11478306 | 853 | pop_node_from_stack (struct ipa_topo_info *topo) |
310bc633 MJ |
854 | { |
855 | if (topo->stack_top) | |
3949c4a7 | 856 | { |
310bc633 MJ |
857 | struct cgraph_node *node; |
858 | topo->stack_top--; | |
859 | node = topo->stack[topo->stack_top]; | |
860 | IPA_NODE_REF (node)->node_enqueued = 0; | |
861 | return node; | |
3949c4a7 | 862 | } |
310bc633 MJ |
863 | else |
864 | return NULL; | |
518dc859 RL |
865 | } |
866 | ||
310bc633 MJ |
867 | /* Set lattice LAT to bottom and return true if it previously was not set as |
868 | such. */ | |
869 | ||
c0cb5055 MJ |
870 | template <typename valtype> |
871 | inline bool | |
872 | ipcp_lattice<valtype>::set_to_bottom () | |
518dc859 | 873 | { |
c0cb5055 MJ |
874 | bool ret = !bottom; |
875 | bottom = true; | |
310bc633 MJ |
876 | return ret; |
877 | } | |
518dc859 | 878 | |
310bc633 MJ |
879 | /* Mark lattice as containing an unknown value and return true if it previously |
880 | was not marked as such. */ | |
129a37fc | 881 | |
c0cb5055 MJ |
882 | template <typename valtype> |
883 | inline bool | |
884 | ipcp_lattice<valtype>::set_contains_variable () | |
310bc633 | 885 | { |
c0cb5055 MJ |
886 | bool ret = !contains_variable; |
887 | contains_variable = true; | |
310bc633 | 888 | return ret; |
518dc859 RL |
889 | } |
890 | ||
2c9561b5 MJ |
891 | /* Set all aggegate lattices in PLATS to bottom and return true if they were |
892 | not previously set as such. */ | |
893 | ||
894 | static inline bool | |
895 | set_agg_lats_to_bottom (struct ipcp_param_lattices *plats) | |
896 | { | |
897 | bool ret = !plats->aggs_bottom; | |
898 | plats->aggs_bottom = true; | |
899 | return ret; | |
900 | } | |
901 | ||
902 | /* Mark all aggegate lattices in PLATS as containing an unknown value and | |
903 | return true if they were not previously marked as such. */ | |
904 | ||
905 | static inline bool | |
906 | set_agg_lats_contain_variable (struct ipcp_param_lattices *plats) | |
907 | { | |
908 | bool ret = !plats->aggs_contain_variable; | |
909 | plats->aggs_contain_variable = true; | |
910 | return ret; | |
911 | } | |
912 | ||
8bc5448f KV |
913 | bool |
914 | ipcp_vr_lattice::meet_with (const ipcp_vr_lattice &other) | |
915 | { | |
916 | return meet_with_1 (&other.m_vr); | |
917 | } | |
918 | ||
919 | /* Meet the current value of the lattice with value ranfge described by VR | |
920 | lattice. */ | |
921 | ||
922 | bool | |
923 | ipcp_vr_lattice::meet_with (const value_range *p_vr) | |
924 | { | |
925 | return meet_with_1 (p_vr); | |
926 | } | |
927 | ||
928 | /* Meet the current value of the lattice with value ranfge described by | |
929 | OTHER_VR lattice. */ | |
930 | ||
931 | bool | |
932 | ipcp_vr_lattice::meet_with_1 (const value_range *other_vr) | |
933 | { | |
934 | tree min = m_vr.min, max = m_vr.max; | |
935 | value_range_type type = m_vr.type; | |
936 | ||
937 | if (bottom_p ()) | |
938 | return false; | |
939 | ||
940 | if (other_vr->type == VR_VARYING) | |
941 | return set_to_bottom (); | |
942 | ||
943 | vrp_meet (&m_vr, other_vr); | |
944 | if (type != m_vr.type | |
945 | || min != m_vr.min | |
946 | || max != m_vr.max) | |
947 | return true; | |
948 | else | |
949 | return false; | |
950 | } | |
951 | ||
952 | /* Return true if value range information in the lattice is yet unknown. */ | |
953 | ||
954 | bool | |
955 | ipcp_vr_lattice::top_p () const | |
956 | { | |
957 | return m_vr.type == VR_UNDEFINED; | |
958 | } | |
959 | ||
960 | /* Return true if value range information in the lattice is known to be | |
961 | unusable. */ | |
962 | ||
963 | bool | |
964 | ipcp_vr_lattice::bottom_p () const | |
965 | { | |
966 | return m_vr.type == VR_VARYING; | |
967 | } | |
968 | ||
969 | /* Set value range information in the lattice to bottom. Return true if it | |
970 | previously was in a different state. */ | |
971 | ||
972 | bool | |
973 | ipcp_vr_lattice::set_to_bottom () | |
974 | { | |
975 | if (m_vr.type == VR_VARYING) | |
976 | return false; | |
977 | m_vr.type = VR_VARYING; | |
978 | return true; | |
979 | } | |
980 | ||
209ca542 PK |
981 | /* Set lattice value to bottom, if it already isn't the case. */ |
982 | ||
983 | bool | |
984 | ipcp_bits_lattice::set_to_bottom () | |
985 | { | |
986 | if (bottom_p ()) | |
987 | return false; | |
988 | m_lattice_val = IPA_BITS_VARYING; | |
989 | m_value = 0; | |
990 | m_mask = -1; | |
991 | return true; | |
992 | } | |
993 | ||
994 | /* Set to constant if it isn't already. Only meant to be called | |
995 | when switching state from TOP. */ | |
996 | ||
997 | bool | |
998 | ipcp_bits_lattice::set_to_constant (widest_int value, widest_int mask) | |
999 | { | |
1000 | gcc_assert (top_p ()); | |
1001 | m_lattice_val = IPA_BITS_CONSTANT; | |
1002 | m_value = value; | |
1003 | m_mask = mask; | |
1004 | return true; | |
1005 | } | |
1006 | ||
1007 | /* Convert operand to value, mask form. */ | |
1008 | ||
1009 | void | |
1010 | ipcp_bits_lattice::get_value_and_mask (tree operand, widest_int *valuep, widest_int *maskp) | |
1011 | { | |
1012 | wide_int get_nonzero_bits (const_tree); | |
1013 | ||
1014 | if (TREE_CODE (operand) == INTEGER_CST) | |
1015 | { | |
155c9907 | 1016 | *valuep = wi::to_widest (operand); |
209ca542 PK |
1017 | *maskp = 0; |
1018 | } | |
1019 | else | |
1020 | { | |
1021 | *valuep = 0; | |
1022 | *maskp = -1; | |
1023 | } | |
1024 | } | |
1025 | ||
1026 | /* Meet operation, similar to ccp_lattice_meet, we xor values | |
1027 | if this->value, value have different values at same bit positions, we want | |
1028 | to drop that bit to varying. Return true if mask is changed. | |
1029 | This function assumes that the lattice value is in CONSTANT state */ | |
1030 | ||
1031 | bool | |
1032 | ipcp_bits_lattice::meet_with_1 (widest_int value, widest_int mask, | |
1033 | unsigned precision) | |
1034 | { | |
1035 | gcc_assert (constant_p ()); | |
155c9907 JJ |
1036 | |
1037 | widest_int old_mask = m_mask; | |
209ca542 PK |
1038 | m_mask = (m_mask | mask) | (m_value ^ value); |
1039 | ||
1040 | if (wi::sext (m_mask, precision) == -1) | |
1041 | return set_to_bottom (); | |
1042 | ||
1043 | return m_mask != old_mask; | |
1044 | } | |
1045 | ||
1046 | /* Meet the bits lattice with operand | |
1047 | described by <value, mask, sgn, precision. */ | |
1048 | ||
1049 | bool | |
1050 | ipcp_bits_lattice::meet_with (widest_int value, widest_int mask, | |
1051 | unsigned precision) | |
1052 | { | |
1053 | if (bottom_p ()) | |
1054 | return false; | |
1055 | ||
1056 | if (top_p ()) | |
1057 | { | |
1058 | if (wi::sext (mask, precision) == -1) | |
1059 | return set_to_bottom (); | |
155c9907 | 1060 | return set_to_constant (value, mask); |
209ca542 PK |
1061 | } |
1062 | ||
1063 | return meet_with_1 (value, mask, precision); | |
1064 | } | |
1065 | ||
1066 | /* Meet bits lattice with the result of bit_value_binop (other, operand) | |
1067 | if code is binary operation or bit_value_unop (other) if code is unary op. | |
1068 | In the case when code is nop_expr, no adjustment is required. */ | |
1069 | ||
1070 | bool | |
1071 | ipcp_bits_lattice::meet_with (ipcp_bits_lattice& other, unsigned precision, | |
1072 | signop sgn, enum tree_code code, tree operand) | |
1073 | { | |
1074 | if (other.bottom_p ()) | |
1075 | return set_to_bottom (); | |
1076 | ||
1077 | if (bottom_p () || other.top_p ()) | |
1078 | return false; | |
1079 | ||
1080 | widest_int adjusted_value, adjusted_mask; | |
1081 | ||
1082 | if (TREE_CODE_CLASS (code) == tcc_binary) | |
1083 | { | |
1084 | tree type = TREE_TYPE (operand); | |
1085 | gcc_assert (INTEGRAL_TYPE_P (type)); | |
1086 | widest_int o_value, o_mask; | |
1087 | get_value_and_mask (operand, &o_value, &o_mask); | |
1088 | ||
1089 | bit_value_binop (code, sgn, precision, &adjusted_value, &adjusted_mask, | |
1090 | sgn, precision, other.get_value (), other.get_mask (), | |
1091 | TYPE_SIGN (type), TYPE_PRECISION (type), o_value, o_mask); | |
1092 | ||
1093 | if (wi::sext (adjusted_mask, precision) == -1) | |
1094 | return set_to_bottom (); | |
1095 | } | |
1096 | ||
1097 | else if (TREE_CODE_CLASS (code) == tcc_unary) | |
1098 | { | |
1099 | bit_value_unop (code, sgn, precision, &adjusted_value, | |
1100 | &adjusted_mask, sgn, precision, other.get_value (), | |
1101 | other.get_mask ()); | |
1102 | ||
1103 | if (wi::sext (adjusted_mask, precision) == -1) | |
1104 | return set_to_bottom (); | |
1105 | } | |
1106 | ||
209ca542 PK |
1107 | else |
1108 | return set_to_bottom (); | |
1109 | ||
1110 | if (top_p ()) | |
1111 | { | |
1112 | if (wi::sext (adjusted_mask, precision) == -1) | |
1113 | return set_to_bottom (); | |
155c9907 | 1114 | return set_to_constant (adjusted_value, adjusted_mask); |
209ca542 PK |
1115 | } |
1116 | else | |
1117 | return meet_with_1 (adjusted_value, adjusted_mask, precision); | |
1118 | } | |
1119 | ||
2c9561b5 MJ |
1120 | /* Mark bot aggregate and scalar lattices as containing an unknown variable, |
1121 | return true is any of them has not been marked as such so far. */ | |
1122 | ||
1123 | static inline bool | |
1124 | set_all_contains_variable (struct ipcp_param_lattices *plats) | |
1125 | { | |
44210a96 MJ |
1126 | bool ret; |
1127 | ret = plats->itself.set_contains_variable (); | |
1128 | ret |= plats->ctxlat.set_contains_variable (); | |
1129 | ret |= set_agg_lats_contain_variable (plats); | |
209ca542 | 1130 | ret |= plats->bits_lattice.set_to_bottom (); |
8bc5448f | 1131 | ret |= plats->m_value_range.set_to_bottom (); |
2c9561b5 MJ |
1132 | return ret; |
1133 | } | |
1134 | ||
af21714c MJ |
1135 | /* Worker of call_for_symbol_thunks_and_aliases, increment the integer DATA |
1136 | points to by the number of callers to NODE. */ | |
1137 | ||
1138 | static bool | |
1139 | count_callers (cgraph_node *node, void *data) | |
1140 | { | |
1141 | int *caller_count = (int *) data; | |
1142 | ||
1143 | for (cgraph_edge *cs = node->callers; cs; cs = cs->next_caller) | |
1144 | /* Local thunks can be handled transparently, but if the thunk can not | |
1145 | be optimized out, count it as a real use. */ | |
1146 | if (!cs->caller->thunk.thunk_p || !cs->caller->local.local) | |
1147 | ++*caller_count; | |
1148 | return false; | |
1149 | } | |
1150 | ||
1151 | /* Worker of call_for_symbol_thunks_and_aliases, it is supposed to be called on | |
1152 | the one caller of some other node. Set the caller's corresponding flag. */ | |
1153 | ||
1154 | static bool | |
1155 | set_single_call_flag (cgraph_node *node, void *) | |
1156 | { | |
1157 | cgraph_edge *cs = node->callers; | |
1158 | /* Local thunks can be handled transparently, skip them. */ | |
1159 | while (cs && cs->caller->thunk.thunk_p && cs->caller->local.local) | |
1160 | cs = cs->next_caller; | |
1161 | if (cs) | |
1162 | { | |
af21714c MJ |
1163 | IPA_NODE_REF (cs->caller)->node_calling_single_call = true; |
1164 | return true; | |
1165 | } | |
1166 | return false; | |
1167 | } | |
1168 | ||
310bc633 | 1169 | /* Initialize ipcp_lattices. */ |
43558bcc | 1170 | |
518dc859 | 1171 | static void |
310bc633 | 1172 | initialize_node_lattices (struct cgraph_node *node) |
518dc859 | 1173 | { |
310bc633 MJ |
1174 | struct ipa_node_params *info = IPA_NODE_REF (node); |
1175 | struct cgraph_edge *ie; | |
1176 | bool disable = false, variable = false; | |
1177 | int i; | |
518dc859 | 1178 | |
d52f5295 | 1179 | gcc_checking_assert (node->has_gimple_body_p ()); |
31db0fe0 | 1180 | if (node->local.local) |
af21714c MJ |
1181 | { |
1182 | int caller_count = 0; | |
1183 | node->call_for_symbol_thunks_and_aliases (count_callers, &caller_count, | |
1184 | true); | |
1185 | gcc_checking_assert (caller_count > 0); | |
1186 | if (caller_count == 1) | |
1187 | node->call_for_symbol_thunks_and_aliases (set_single_call_flag, | |
1188 | NULL, true); | |
1189 | } | |
1190 | else | |
310bc633 MJ |
1191 | { |
1192 | /* When cloning is allowed, we can assume that externally visible | |
1193 | functions are not called. We will compensate this by cloning | |
1194 | later. */ | |
1195 | if (ipcp_versionable_function_p (node) | |
1196 | && ipcp_cloning_candidate_p (node)) | |
1197 | variable = true; | |
1198 | else | |
1199 | disable = true; | |
1200 | } | |
518dc859 | 1201 | |
155c9907 | 1202 | for (i = 0; i < ipa_get_param_count (info); i++) |
8bc5448f KV |
1203 | { |
1204 | struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i); | |
1205 | plats->m_value_range.init (); | |
1206 | } | |
1207 | ||
310bc633 MJ |
1208 | if (disable || variable) |
1209 | { | |
155c9907 | 1210 | for (i = 0; i < ipa_get_param_count (info); i++) |
310bc633 | 1211 | { |
2c9561b5 | 1212 | struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i); |
310bc633 | 1213 | if (disable) |
2c9561b5 | 1214 | { |
c0cb5055 | 1215 | plats->itself.set_to_bottom (); |
44210a96 | 1216 | plats->ctxlat.set_to_bottom (); |
2c9561b5 | 1217 | set_agg_lats_to_bottom (plats); |
209ca542 | 1218 | plats->bits_lattice.set_to_bottom (); |
8bc5448f | 1219 | plats->m_value_range.set_to_bottom (); |
2c9561b5 | 1220 | } |
310bc633 | 1221 | else |
2c9561b5 | 1222 | set_all_contains_variable (plats); |
310bc633 MJ |
1223 | } |
1224 | if (dump_file && (dump_flags & TDF_DETAILS) | |
67348ccc | 1225 | && !node->alias && !node->thunk.thunk_p) |
464d0118 ML |
1226 | fprintf (dump_file, "Marking all lattices of %s as %s\n", |
1227 | node->dump_name (), disable ? "BOTTOM" : "VARIABLE"); | |
310bc633 | 1228 | } |
518dc859 | 1229 | |
310bc633 | 1230 | for (ie = node->indirect_calls; ie; ie = ie->next_callee) |
1d5755ef | 1231 | if (ie->indirect_info->polymorphic |
155c9907 | 1232 | && ie->indirect_info->param_index >= 0) |
0818c24c | 1233 | { |
310bc633 | 1234 | gcc_checking_assert (ie->indirect_info->param_index >= 0); |
2c9561b5 MJ |
1235 | ipa_get_parm_lattices (info, |
1236 | ie->indirect_info->param_index)->virt_call = 1; | |
0818c24c | 1237 | } |
518dc859 RL |
1238 | } |
1239 | ||
310bc633 | 1240 | /* Return the result of a (possibly arithmetic) pass through jump function |
e5cf5e11 PK |
1241 | JFUNC on the constant value INPUT. RES_TYPE is the type of the parameter |
1242 | to which the result is passed. Return NULL_TREE if that cannot be | |
b8f6e610 | 1243 | determined or be considered an interprocedural invariant. */ |
3949c4a7 | 1244 | |
310bc633 | 1245 | static tree |
e5cf5e11 PK |
1246 | ipa_get_jf_pass_through_result (struct ipa_jump_func *jfunc, tree input, |
1247 | tree res_type) | |
3949c4a7 | 1248 | { |
e5cf5e11 | 1249 | tree res; |
3949c4a7 | 1250 | |
7b872d9e | 1251 | if (ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR) |
310bc633 | 1252 | return input; |
04643334 MJ |
1253 | if (!is_gimple_ip_invariant (input)) |
1254 | return NULL_TREE; | |
3949c4a7 | 1255 | |
e5cf5e11 PK |
1256 | tree_code opcode = ipa_get_jf_pass_through_operation (jfunc); |
1257 | if (!res_type) | |
a2b4c188 | 1258 | { |
e5cf5e11 PK |
1259 | if (TREE_CODE_CLASS (opcode) == tcc_comparison) |
1260 | res_type = boolean_type_node; | |
1261 | else if (expr_type_first_operand_type_p (opcode)) | |
1262 | res_type = TREE_TYPE (input); | |
a2b4c188 | 1263 | else |
e5cf5e11 | 1264 | return NULL_TREE; |
a2b4c188 | 1265 | } |
e5cf5e11 PK |
1266 | |
1267 | if (TREE_CODE_CLASS (opcode) == tcc_unary) | |
1268 | res = fold_unary (opcode, res_type, input); | |
1269 | else | |
1270 | res = fold_binary (opcode, res_type, input, | |
1271 | ipa_get_jf_pass_through_operand (jfunc)); | |
1272 | ||
310bc633 MJ |
1273 | if (res && !is_gimple_ip_invariant (res)) |
1274 | return NULL_TREE; | |
3949c4a7 | 1275 | |
310bc633 | 1276 | return res; |
3949c4a7 MJ |
1277 | } |
1278 | ||
310bc633 MJ |
1279 | /* Return the result of an ancestor jump function JFUNC on the constant value |
1280 | INPUT. Return NULL_TREE if that cannot be determined. */ | |
3949c4a7 | 1281 | |
310bc633 MJ |
1282 | static tree |
1283 | ipa_get_jf_ancestor_result (struct ipa_jump_func *jfunc, tree input) | |
3949c4a7 | 1284 | { |
44210a96 MJ |
1285 | gcc_checking_assert (TREE_CODE (input) != TREE_BINFO); |
1286 | if (TREE_CODE (input) == ADDR_EXPR) | |
3949c4a7 | 1287 | { |
310bc633 MJ |
1288 | tree t = TREE_OPERAND (input, 0); |
1289 | t = build_ref_for_offset (EXPR_LOCATION (t), t, | |
ee45a32d | 1290 | ipa_get_jf_ancestor_offset (jfunc), false, |
3b97a5c7 | 1291 | ptr_type_node, NULL, false); |
310bc633 | 1292 | return build_fold_addr_expr (t); |
3949c4a7 MJ |
1293 | } |
1294 | else | |
310bc633 MJ |
1295 | return NULL_TREE; |
1296 | } | |
3949c4a7 | 1297 | |
44210a96 MJ |
1298 | /* Determine whether JFUNC evaluates to a single known constant value and if |
1299 | so, return it. Otherwise return NULL. INFO describes the caller node or | |
1300 | the one it is inlined to, so that pass-through jump functions can be | |
e5cf5e11 PK |
1301 | evaluated. PARM_TYPE is the type of the parameter to which the result is |
1302 | passed. */ | |
310bc633 | 1303 | |
d2d668fb | 1304 | tree |
e5cf5e11 PK |
1305 | ipa_value_from_jfunc (struct ipa_node_params *info, struct ipa_jump_func *jfunc, |
1306 | tree parm_type) | |
310bc633 MJ |
1307 | { |
1308 | if (jfunc->type == IPA_JF_CONST) | |
7b872d9e | 1309 | return ipa_get_jf_constant (jfunc); |
310bc633 MJ |
1310 | else if (jfunc->type == IPA_JF_PASS_THROUGH |
1311 | || jfunc->type == IPA_JF_ANCESTOR) | |
3949c4a7 | 1312 | { |
310bc633 MJ |
1313 | tree input; |
1314 | int idx; | |
3949c4a7 | 1315 | |
310bc633 | 1316 | if (jfunc->type == IPA_JF_PASS_THROUGH) |
7b872d9e | 1317 | idx = ipa_get_jf_pass_through_formal_id (jfunc); |
310bc633 | 1318 | else |
7b872d9e | 1319 | idx = ipa_get_jf_ancestor_formal_id (jfunc); |
3949c4a7 | 1320 | |
310bc633 | 1321 | if (info->ipcp_orig_node) |
44210a96 | 1322 | input = info->known_csts[idx]; |
310bc633 | 1323 | else |
3949c4a7 | 1324 | { |
c0cb5055 | 1325 | ipcp_lattice<tree> *lat; |
310bc633 | 1326 | |
370a7814 JH |
1327 | if (!info->lattices |
1328 | || idx >= ipa_get_param_count (info)) | |
2bf86c84 | 1329 | return NULL_TREE; |
2c9561b5 | 1330 | lat = ipa_get_scalar_lat (info, idx); |
c0cb5055 | 1331 | if (!lat->is_single_const ()) |
310bc633 MJ |
1332 | return NULL_TREE; |
1333 | input = lat->values->value; | |
1334 | } | |
1335 | ||
1336 | if (!input) | |
1337 | return NULL_TREE; | |
1338 | ||
1339 | if (jfunc->type == IPA_JF_PASS_THROUGH) | |
e5cf5e11 | 1340 | return ipa_get_jf_pass_through_result (jfunc, input, parm_type); |
310bc633 | 1341 | else |
7b872d9e | 1342 | return ipa_get_jf_ancestor_result (jfunc, input); |
3949c4a7 | 1343 | } |
310bc633 MJ |
1344 | else |
1345 | return NULL_TREE; | |
3949c4a7 MJ |
1346 | } |
1347 | ||
44210a96 MJ |
1348 | /* Determie whether JFUNC evaluates to single known polymorphic context, given |
1349 | that INFO describes the caller node or the one it is inlined to, CS is the | |
1350 | call graph edge corresponding to JFUNC and CSIDX index of the described | |
1351 | parameter. */ | |
1352 | ||
1353 | ipa_polymorphic_call_context | |
1354 | ipa_context_from_jfunc (ipa_node_params *info, cgraph_edge *cs, int csidx, | |
1355 | ipa_jump_func *jfunc) | |
1356 | { | |
1357 | ipa_edge_args *args = IPA_EDGE_REF (cs); | |
1358 | ipa_polymorphic_call_context ctx; | |
1359 | ipa_polymorphic_call_context *edge_ctx | |
1360 | = cs ? ipa_get_ith_polymorhic_call_context (args, csidx) : NULL; | |
1361 | ||
1362 | if (edge_ctx && !edge_ctx->useless_p ()) | |
1363 | ctx = *edge_ctx; | |
1364 | ||
1365 | if (jfunc->type == IPA_JF_PASS_THROUGH | |
1366 | || jfunc->type == IPA_JF_ANCESTOR) | |
1367 | { | |
1368 | ipa_polymorphic_call_context srcctx; | |
1369 | int srcidx; | |
df0d8136 | 1370 | bool type_preserved = true; |
44210a96 MJ |
1371 | if (jfunc->type == IPA_JF_PASS_THROUGH) |
1372 | { | |
df0d8136 | 1373 | if (ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR) |
44210a96 | 1374 | return ctx; |
df0d8136 | 1375 | type_preserved = ipa_get_jf_pass_through_type_preserved (jfunc); |
44210a96 MJ |
1376 | srcidx = ipa_get_jf_pass_through_formal_id (jfunc); |
1377 | } | |
1378 | else | |
1379 | { | |
df0d8136 | 1380 | type_preserved = ipa_get_jf_ancestor_type_preserved (jfunc); |
44210a96 MJ |
1381 | srcidx = ipa_get_jf_ancestor_formal_id (jfunc); |
1382 | } | |
1383 | if (info->ipcp_orig_node) | |
1384 | { | |
1385 | if (info->known_contexts.exists ()) | |
1386 | srcctx = info->known_contexts[srcidx]; | |
1387 | } | |
1388 | else | |
1389 | { | |
370a7814 JH |
1390 | if (!info->lattices |
1391 | || srcidx >= ipa_get_param_count (info)) | |
2bf86c84 | 1392 | return ctx; |
44210a96 MJ |
1393 | ipcp_lattice<ipa_polymorphic_call_context> *lat; |
1394 | lat = ipa_get_poly_ctx_lat (info, srcidx); | |
1395 | if (!lat->is_single_const ()) | |
1396 | return ctx; | |
1397 | srcctx = lat->values->value; | |
1398 | } | |
1399 | if (srcctx.useless_p ()) | |
1400 | return ctx; | |
1401 | if (jfunc->type == IPA_JF_ANCESTOR) | |
1402 | srcctx.offset_by (ipa_get_jf_ancestor_offset (jfunc)); | |
df0d8136 JH |
1403 | if (!type_preserved) |
1404 | srcctx.possible_dynamic_type_change (cs->in_polymorphic_cdtor); | |
1405 | srcctx.combine_with (ctx); | |
1406 | return srcctx; | |
44210a96 MJ |
1407 | } |
1408 | ||
1409 | return ctx; | |
1410 | } | |
3949c4a7 | 1411 | |
310bc633 MJ |
1412 | /* If checking is enabled, verify that no lattice is in the TOP state, i.e. not |
1413 | bottom, not containing a variable component and without any known value at | |
1414 | the same time. */ | |
3949c4a7 | 1415 | |
310bc633 MJ |
1416 | DEBUG_FUNCTION void |
1417 | ipcp_verify_propagated_values (void) | |
518dc859 | 1418 | { |
310bc633 | 1419 | struct cgraph_node *node; |
ca30a539 | 1420 | |
310bc633 | 1421 | FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node) |
518dc859 | 1422 | { |
c43f07af | 1423 | struct ipa_node_params *info = IPA_NODE_REF (node); |
310bc633 | 1424 | int i, count = ipa_get_param_count (info); |
c43f07af | 1425 | |
310bc633 | 1426 | for (i = 0; i < count; i++) |
518dc859 | 1427 | { |
c0cb5055 | 1428 | ipcp_lattice<tree> *lat = ipa_get_scalar_lat (info, i); |
c43f07af | 1429 | |
310bc633 MJ |
1430 | if (!lat->bottom |
1431 | && !lat->contains_variable | |
1432 | && lat->values_count == 0) | |
518dc859 | 1433 | { |
310bc633 | 1434 | if (dump_file) |
518dc859 | 1435 | { |
6c52831d | 1436 | symtab->dump (dump_file); |
310bc633 | 1437 | fprintf (dump_file, "\nIPA lattices after constant " |
5bed50e8 | 1438 | "propagation, before gcc_unreachable:\n"); |
310bc633 | 1439 | print_all_lattices (dump_file, true, false); |
518dc859 | 1440 | } |
3949c4a7 | 1441 | |
310bc633 | 1442 | gcc_unreachable (); |
518dc859 RL |
1443 | } |
1444 | } | |
1445 | } | |
1446 | } | |
1447 | ||
310bc633 MJ |
1448 | /* Return true iff X and Y should be considered equal values by IPA-CP. */ |
1449 | ||
1450 | static bool | |
1451 | values_equal_for_ipcp_p (tree x, tree y) | |
1452 | { | |
1453 | gcc_checking_assert (x != NULL_TREE && y != NULL_TREE); | |
1454 | ||
1455 | if (x == y) | |
1456 | return true; | |
1457 | ||
310bc633 MJ |
1458 | if (TREE_CODE (x) == ADDR_EXPR |
1459 | && TREE_CODE (y) == ADDR_EXPR | |
1460 | && TREE_CODE (TREE_OPERAND (x, 0)) == CONST_DECL | |
1461 | && TREE_CODE (TREE_OPERAND (y, 0)) == CONST_DECL) | |
1462 | return operand_equal_p (DECL_INITIAL (TREE_OPERAND (x, 0)), | |
1463 | DECL_INITIAL (TREE_OPERAND (y, 0)), 0); | |
1464 | else | |
1465 | return operand_equal_p (x, y, 0); | |
1466 | } | |
1467 | ||
44210a96 MJ |
1468 | /* Return true iff X and Y should be considered equal contexts by IPA-CP. */ |
1469 | ||
1470 | static bool | |
1471 | values_equal_for_ipcp_p (ipa_polymorphic_call_context x, | |
1472 | ipa_polymorphic_call_context y) | |
1473 | { | |
1474 | return x.equal_to (y); | |
1475 | } | |
1476 | ||
1477 | ||
c0cb5055 MJ |
1478 | /* Add a new value source to the value represented by THIS, marking that a |
1479 | value comes from edge CS and (if the underlying jump function is a | |
1480 | pass-through or an ancestor one) from a caller value SRC_VAL of a caller | |
1481 | parameter described by SRC_INDEX. OFFSET is negative if the source was the | |
1482 | scalar value of the parameter itself or the offset within an aggregate. */ | |
310bc633 | 1483 | |
c0cb5055 MJ |
1484 | template <typename valtype> |
1485 | void | |
1486 | ipcp_value<valtype>::add_source (cgraph_edge *cs, ipcp_value *src_val, | |
1487 | int src_idx, HOST_WIDE_INT offset) | |
518dc859 | 1488 | { |
c0cb5055 | 1489 | ipcp_value_source<valtype> *src; |
ca30a539 | 1490 | |
2651e637 | 1491 | src = new (ipcp_sources_pool.allocate ()) ipcp_value_source<valtype>; |
2c9561b5 | 1492 | src->offset = offset; |
310bc633 MJ |
1493 | src->cs = cs; |
1494 | src->val = src_val; | |
1495 | src->index = src_idx; | |
fb3f88cc | 1496 | |
c0cb5055 MJ |
1497 | src->next = sources; |
1498 | sources = src; | |
310bc633 MJ |
1499 | } |
1500 | ||
c0cb5055 MJ |
1501 | /* Allocate a new ipcp_value holding a tree constant, initialize its value to |
1502 | SOURCE and clear all other fields. */ | |
310bc633 | 1503 | |
c0cb5055 MJ |
1504 | static ipcp_value<tree> * |
1505 | allocate_and_init_ipcp_value (tree source) | |
310bc633 | 1506 | { |
c0cb5055 | 1507 | ipcp_value<tree> *val; |
310bc633 | 1508 | |
c3684b7b | 1509 | val = new (ipcp_cst_values_pool.allocate ()) ipcp_value<tree>(); |
44210a96 MJ |
1510 | val->value = source; |
1511 | return val; | |
1512 | } | |
1513 | ||
1514 | /* Allocate a new ipcp_value holding a polymorphic context, initialize its | |
1515 | value to SOURCE and clear all other fields. */ | |
1516 | ||
1517 | static ipcp_value<ipa_polymorphic_call_context> * | |
1518 | allocate_and_init_ipcp_value (ipa_polymorphic_call_context source) | |
1519 | { | |
1520 | ipcp_value<ipa_polymorphic_call_context> *val; | |
1521 | ||
2651e637 | 1522 | // TODO |
c3684b7b MS |
1523 | val = new (ipcp_poly_ctx_values_pool.allocate ()) |
1524 | ipcp_value<ipa_polymorphic_call_context>(); | |
c0cb5055 MJ |
1525 | val->value = source; |
1526 | return val; | |
1527 | } | |
1528 | ||
1529 | /* Try to add NEWVAL to LAT, potentially creating a new ipcp_value for it. CS, | |
1530 | SRC_VAL SRC_INDEX and OFFSET are meant for add_source and have the same | |
1531 | meaning. OFFSET -1 means the source is scalar and not a part of an | |
1532 | aggregate. */ | |
1533 | ||
1534 | template <typename valtype> | |
1535 | bool | |
1536 | ipcp_lattice<valtype>::add_value (valtype newval, cgraph_edge *cs, | |
1537 | ipcp_value<valtype> *src_val, | |
1538 | int src_idx, HOST_WIDE_INT offset) | |
1539 | { | |
1540 | ipcp_value<valtype> *val; | |
1541 | ||
1542 | if (bottom) | |
310bc633 MJ |
1543 | return false; |
1544 | ||
c0cb5055 | 1545 | for (val = values; val; val = val->next) |
310bc633 MJ |
1546 | if (values_equal_for_ipcp_p (val->value, newval)) |
1547 | { | |
4cb13597 | 1548 | if (ipa_edge_within_scc (cs)) |
310bc633 | 1549 | { |
c0cb5055 | 1550 | ipcp_value_source<valtype> *s; |
155c9907 | 1551 | for (s = val->sources; s; s = s->next) |
310bc633 MJ |
1552 | if (s->cs == cs) |
1553 | break; | |
1554 | if (s) | |
1555 | return false; | |
1556 | } | |
1557 | ||
c0cb5055 | 1558 | val->add_source (cs, src_val, src_idx, offset); |
310bc633 MJ |
1559 | return false; |
1560 | } | |
1561 | ||
c0cb5055 | 1562 | if (values_count == PARAM_VALUE (PARAM_IPA_CP_VALUE_LIST_SIZE)) |
310bc633 MJ |
1563 | { |
1564 | /* We can only free sources, not the values themselves, because sources | |
026c3cfd | 1565 | of other values in this SCC might point to them. */ |
c0cb5055 | 1566 | for (val = values; val; val = val->next) |
310bc633 MJ |
1567 | { |
1568 | while (val->sources) | |
1569 | { | |
c0cb5055 | 1570 | ipcp_value_source<valtype> *src = val->sources; |
310bc633 | 1571 | val->sources = src->next; |
2651e637 | 1572 | ipcp_sources_pool.remove ((ipcp_value_source<tree>*)src); |
310bc633 MJ |
1573 | } |
1574 | } | |
1575 | ||
c0cb5055 MJ |
1576 | values = NULL; |
1577 | return set_to_bottom (); | |
310bc633 MJ |
1578 | } |
1579 | ||
c0cb5055 MJ |
1580 | values_count++; |
1581 | val = allocate_and_init_ipcp_value (newval); | |
1582 | val->add_source (cs, src_val, src_idx, offset); | |
1583 | val->next = values; | |
1584 | values = val; | |
310bc633 MJ |
1585 | return true; |
1586 | } | |
fb3f88cc | 1587 | |
310bc633 MJ |
1588 | /* Propagate values through a pass-through jump function JFUNC associated with |
1589 | edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX | |
e5cf5e11 PK |
1590 | is the index of the source parameter. PARM_TYPE is the type of the |
1591 | parameter to which the result is passed. */ | |
310bc633 MJ |
1592 | |
1593 | static bool | |
155c9907 JJ |
1594 | propagate_vals_across_pass_through (cgraph_edge *cs, ipa_jump_func *jfunc, |
1595 | ipcp_lattice<tree> *src_lat, | |
e5cf5e11 PK |
1596 | ipcp_lattice<tree> *dest_lat, int src_idx, |
1597 | tree parm_type) | |
310bc633 | 1598 | { |
c0cb5055 | 1599 | ipcp_value<tree> *src_val; |
310bc633 MJ |
1600 | bool ret = false; |
1601 | ||
310bc633 | 1602 | /* Do not create new values when propagating within an SCC because if there |
7b872d9e | 1603 | are arithmetic functions with circular dependencies, there is infinite |
7b668576 MJ |
1604 | number of them and we would just make lattices bottom. If this condition |
1605 | is ever relaxed we have to detect self-feeding recursive calls in | |
1606 | cgraph_edge_brings_value_p in a smarter way. */ | |
b8f6e610 | 1607 | if ((ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR) |
4cb13597 | 1608 | && ipa_edge_within_scc (cs)) |
c0cb5055 | 1609 | ret = dest_lat->set_contains_variable (); |
310bc633 MJ |
1610 | else |
1611 | for (src_val = src_lat->values; src_val; src_val = src_val->next) | |
0818c24c | 1612 | { |
e5cf5e11 PK |
1613 | tree cstval = ipa_get_jf_pass_through_result (jfunc, src_val->value, |
1614 | parm_type); | |
310bc633 MJ |
1615 | |
1616 | if (cstval) | |
c0cb5055 | 1617 | ret |= dest_lat->add_value (cstval, cs, src_val, src_idx); |
310bc633 | 1618 | else |
c0cb5055 | 1619 | ret |= dest_lat->set_contains_variable (); |
0818c24c | 1620 | } |
310bc633 MJ |
1621 | |
1622 | return ret; | |
1623 | } | |
1624 | ||
1625 | /* Propagate values through an ancestor jump function JFUNC associated with | |
1626 | edge CS, taking values from SRC_LAT and putting them into DEST_LAT. SRC_IDX | |
1627 | is the index of the source parameter. */ | |
1628 | ||
1629 | static bool | |
155c9907 JJ |
1630 | propagate_vals_across_ancestor (struct cgraph_edge *cs, |
1631 | struct ipa_jump_func *jfunc, | |
1632 | ipcp_lattice<tree> *src_lat, | |
1633 | ipcp_lattice<tree> *dest_lat, int src_idx) | |
310bc633 | 1634 | { |
c0cb5055 | 1635 | ipcp_value<tree> *src_val; |
310bc633 MJ |
1636 | bool ret = false; |
1637 | ||
4cb13597 | 1638 | if (ipa_edge_within_scc (cs)) |
c0cb5055 | 1639 | return dest_lat->set_contains_variable (); |
310bc633 MJ |
1640 | |
1641 | for (src_val = src_lat->values; src_val; src_val = src_val->next) | |
1642 | { | |
7b872d9e | 1643 | tree t = ipa_get_jf_ancestor_result (jfunc, src_val->value); |
310bc633 MJ |
1644 | |
1645 | if (t) | |
c0cb5055 | 1646 | ret |= dest_lat->add_value (t, cs, src_val, src_idx); |
310bc633 | 1647 | else |
c0cb5055 | 1648 | ret |= dest_lat->set_contains_variable (); |
310bc633 MJ |
1649 | } |
1650 | ||
1651 | return ret; | |
1652 | } | |
1653 | ||
2c9561b5 | 1654 | /* Propagate scalar values across jump function JFUNC that is associated with |
e5cf5e11 PK |
1655 | edge CS and put the values into DEST_LAT. PARM_TYPE is the type of the |
1656 | parameter to which the result is passed. */ | |
310bc633 MJ |
1657 | |
1658 | static bool | |
155c9907 JJ |
1659 | propagate_scalar_across_jump_function (struct cgraph_edge *cs, |
1660 | struct ipa_jump_func *jfunc, | |
e5cf5e11 PK |
1661 | ipcp_lattice<tree> *dest_lat, |
1662 | tree param_type) | |
310bc633 MJ |
1663 | { |
1664 | if (dest_lat->bottom) | |
1665 | return false; | |
1666 | ||
44210a96 | 1667 | if (jfunc->type == IPA_JF_CONST) |
310bc633 | 1668 | { |
44210a96 | 1669 | tree val = ipa_get_jf_constant (jfunc); |
c0cb5055 | 1670 | return dest_lat->add_value (val, cs, NULL, 0); |
310bc633 MJ |
1671 | } |
1672 | else if (jfunc->type == IPA_JF_PASS_THROUGH | |
1673 | || jfunc->type == IPA_JF_ANCESTOR) | |
1674 | { | |
1675 | struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller); | |
c0cb5055 | 1676 | ipcp_lattice<tree> *src_lat; |
310bc633 MJ |
1677 | int src_idx; |
1678 | bool ret; | |
1679 | ||
1680 | if (jfunc->type == IPA_JF_PASS_THROUGH) | |
7b872d9e | 1681 | src_idx = ipa_get_jf_pass_through_formal_id (jfunc); |
310bc633 | 1682 | else |
7b872d9e | 1683 | src_idx = ipa_get_jf_ancestor_formal_id (jfunc); |
310bc633 | 1684 | |
2c9561b5 | 1685 | src_lat = ipa_get_scalar_lat (caller_info, src_idx); |
310bc633 | 1686 | if (src_lat->bottom) |
c0cb5055 | 1687 | return dest_lat->set_contains_variable (); |
310bc633 MJ |
1688 | |
1689 | /* If we would need to clone the caller and cannot, do not propagate. */ | |
1690 | if (!ipcp_versionable_function_p (cs->caller) | |
1691 | && (src_lat->contains_variable | |
1692 | || (src_lat->values_count > 1))) | |
c0cb5055 | 1693 | return dest_lat->set_contains_variable (); |
310bc633 MJ |
1694 | |
1695 | if (jfunc->type == IPA_JF_PASS_THROUGH) | |
155c9907 | 1696 | ret = propagate_vals_across_pass_through (cs, jfunc, src_lat, |
e5cf5e11 | 1697 | dest_lat, src_idx, param_type); |
310bc633 | 1698 | else |
155c9907 JJ |
1699 | ret = propagate_vals_across_ancestor (cs, jfunc, src_lat, dest_lat, |
1700 | src_idx); | |
310bc633 MJ |
1701 | |
1702 | if (src_lat->contains_variable) | |
c0cb5055 | 1703 | ret |= dest_lat->set_contains_variable (); |
310bc633 MJ |
1704 | |
1705 | return ret; | |
1706 | } | |
1707 | ||
1708 | /* TODO: We currently do not handle member method pointers in IPA-CP (we only | |
1709 | use it for indirect inlining), we should propagate them too. */ | |
c0cb5055 | 1710 | return dest_lat->set_contains_variable (); |
310bc633 MJ |
1711 | } |
1712 | ||
44210a96 MJ |
1713 | /* Propagate scalar values across jump function JFUNC that is associated with |
1714 | edge CS and describes argument IDX and put the values into DEST_LAT. */ | |
1715 | ||
1716 | static bool | |
155c9907 | 1717 | propagate_context_across_jump_function (cgraph_edge *cs, |
44210a96 MJ |
1718 | ipa_jump_func *jfunc, int idx, |
1719 | ipcp_lattice<ipa_polymorphic_call_context> *dest_lat) | |
1720 | { | |
1721 | ipa_edge_args *args = IPA_EDGE_REF (cs); | |
1722 | if (dest_lat->bottom) | |
1723 | return false; | |
1724 | bool ret = false; | |
1725 | bool added_sth = false; | |
df0d8136 | 1726 | bool type_preserved = true; |
44210a96 MJ |
1727 | |
1728 | ipa_polymorphic_call_context edge_ctx, *edge_ctx_ptr | |
1729 | = ipa_get_ith_polymorhic_call_context (args, idx); | |
1730 | ||
1731 | if (edge_ctx_ptr) | |
df0d8136 | 1732 | edge_ctx = *edge_ctx_ptr; |
44210a96 MJ |
1733 | |
1734 | if (jfunc->type == IPA_JF_PASS_THROUGH | |
1735 | || jfunc->type == IPA_JF_ANCESTOR) | |
1736 | { | |
1737 | struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller); | |
1738 | int src_idx; | |
1739 | ipcp_lattice<ipa_polymorphic_call_context> *src_lat; | |
1740 | ||
1741 | /* TODO: Once we figure out how to propagate speculations, it will | |
1742 | probably be a good idea to switch to speculation if type_preserved is | |
1743 | not set instead of punting. */ | |
1744 | if (jfunc->type == IPA_JF_PASS_THROUGH) | |
1745 | { | |
df0d8136 | 1746 | if (ipa_get_jf_pass_through_operation (jfunc) != NOP_EXPR) |
44210a96 | 1747 | goto prop_fail; |
df0d8136 | 1748 | type_preserved = ipa_get_jf_pass_through_type_preserved (jfunc); |
44210a96 MJ |
1749 | src_idx = ipa_get_jf_pass_through_formal_id (jfunc); |
1750 | } | |
1751 | else | |
1752 | { | |
df0d8136 | 1753 | type_preserved = ipa_get_jf_ancestor_type_preserved (jfunc); |
44210a96 MJ |
1754 | src_idx = ipa_get_jf_ancestor_formal_id (jfunc); |
1755 | } | |
1756 | ||
1757 | src_lat = ipa_get_poly_ctx_lat (caller_info, src_idx); | |
1758 | /* If we would need to clone the caller and cannot, do not propagate. */ | |
1759 | if (!ipcp_versionable_function_p (cs->caller) | |
1760 | && (src_lat->contains_variable | |
1761 | || (src_lat->values_count > 1))) | |
1762 | goto prop_fail; | |
44210a96 MJ |
1763 | |
1764 | ipcp_value<ipa_polymorphic_call_context> *src_val; | |
1765 | for (src_val = src_lat->values; src_val; src_val = src_val->next) | |
1766 | { | |
1767 | ipa_polymorphic_call_context cur = src_val->value; | |
df0d8136 JH |
1768 | |
1769 | if (!type_preserved) | |
1770 | cur.possible_dynamic_type_change (cs->in_polymorphic_cdtor); | |
44210a96 MJ |
1771 | if (jfunc->type == IPA_JF_ANCESTOR) |
1772 | cur.offset_by (ipa_get_jf_ancestor_offset (jfunc)); | |
df0d8136 JH |
1773 | /* TODO: In cases we know how the context is going to be used, |
1774 | we can improve the result by passing proper OTR_TYPE. */ | |
1775 | cur.combine_with (edge_ctx); | |
44210a96 MJ |
1776 | if (!cur.useless_p ()) |
1777 | { | |
df0d8136 JH |
1778 | if (src_lat->contains_variable |
1779 | && !edge_ctx.equal_to (cur)) | |
1780 | ret |= dest_lat->set_contains_variable (); | |
44210a96 MJ |
1781 | ret |= dest_lat->add_value (cur, cs, src_val, src_idx); |
1782 | added_sth = true; | |
1783 | } | |
1784 | } | |
1785 | ||
1786 | } | |
1787 | ||
1788 | prop_fail: | |
1789 | if (!added_sth) | |
1790 | { | |
1791 | if (!edge_ctx.useless_p ()) | |
1792 | ret |= dest_lat->add_value (edge_ctx, cs); | |
1793 | else | |
1794 | ret |= dest_lat->set_contains_variable (); | |
1795 | } | |
1796 | ||
1797 | return ret; | |
1798 | } | |
1799 | ||
209ca542 PK |
1800 | /* Propagate bits across jfunc that is associated with |
1801 | edge cs and update dest_lattice accordingly. */ | |
1802 | ||
1803 | bool | |
155c9907 JJ |
1804 | propagate_bits_across_jump_function (cgraph_edge *cs, int idx, |
1805 | ipa_jump_func *jfunc, | |
1806 | ipcp_bits_lattice *dest_lattice) | |
209ca542 PK |
1807 | { |
1808 | if (dest_lattice->bottom_p ()) | |
1809 | return false; | |
1810 | ||
1811 | enum availability availability; | |
1812 | cgraph_node *callee = cs->callee->function_symbol (&availability); | |
1813 | struct ipa_node_params *callee_info = IPA_NODE_REF (callee); | |
1814 | tree parm_type = ipa_get_type (callee_info, idx); | |
1815 | ||
b93f25ad ML |
1816 | /* For K&R C programs, ipa_get_type() could return NULL_TREE. Avoid the |
1817 | transform for these cases. Similarly, we can have bad type mismatches | |
1818 | with LTO, avoid doing anything with those too. */ | |
1819 | if (!parm_type | |
1820 | || (!INTEGRAL_TYPE_P (parm_type) && !POINTER_TYPE_P (parm_type))) | |
209ca542 PK |
1821 | { |
1822 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
b93f25ad ML |
1823 | fprintf (dump_file, "Setting dest_lattice to bottom, because type of " |
1824 | "param %i of %s is NULL or unsuitable for bits propagation\n", | |
1825 | idx, cs->callee->name ()); | |
209ca542 PK |
1826 | |
1827 | return dest_lattice->set_to_bottom (); | |
1828 | } | |
1829 | ||
1830 | unsigned precision = TYPE_PRECISION (parm_type); | |
1831 | signop sgn = TYPE_SIGN (parm_type); | |
1832 | ||
67b97478 PK |
1833 | if (jfunc->type == IPA_JF_PASS_THROUGH |
1834 | || jfunc->type == IPA_JF_ANCESTOR) | |
209ca542 PK |
1835 | { |
1836 | struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller); | |
209ca542 | 1837 | tree operand = NULL_TREE; |
67b97478 PK |
1838 | enum tree_code code; |
1839 | unsigned src_idx; | |
209ca542 | 1840 | |
67b97478 PK |
1841 | if (jfunc->type == IPA_JF_PASS_THROUGH) |
1842 | { | |
1843 | code = ipa_get_jf_pass_through_operation (jfunc); | |
1844 | src_idx = ipa_get_jf_pass_through_formal_id (jfunc); | |
1845 | if (code != NOP_EXPR) | |
1846 | operand = ipa_get_jf_pass_through_operand (jfunc); | |
1847 | } | |
1848 | else | |
1849 | { | |
155c9907 | 1850 | code = POINTER_PLUS_EXPR; |
67b97478 PK |
1851 | src_idx = ipa_get_jf_ancestor_formal_id (jfunc); |
1852 | unsigned HOST_WIDE_INT offset = ipa_get_jf_ancestor_offset (jfunc) / BITS_PER_UNIT; | |
1853 | operand = build_int_cstu (size_type_node, offset); | |
1854 | } | |
209ca542 | 1855 | |
209ca542 PK |
1856 | struct ipcp_param_lattices *src_lats |
1857 | = ipa_get_parm_lattices (caller_info, src_idx); | |
1858 | ||
1859 | /* Try to propagate bits if src_lattice is bottom, but jfunc is known. | |
1860 | for eg consider: | |
1861 | int f(int x) | |
1862 | { | |
1863 | g (x & 0xff); | |
1864 | } | |
1865 | Assume lattice for x is bottom, however we can still propagate | |
1866 | result of x & 0xff == 0xff, which gets computed during ccp1 pass | |
1867 | and we store it in jump function during analysis stage. */ | |
1868 | ||
1869 | if (src_lats->bits_lattice.bottom_p () | |
86cd0334 MJ |
1870 | && jfunc->bits) |
1871 | return dest_lattice->meet_with (jfunc->bits->value, jfunc->bits->mask, | |
209ca542 PK |
1872 | precision); |
1873 | else | |
1874 | return dest_lattice->meet_with (src_lats->bits_lattice, precision, sgn, | |
1875 | code, operand); | |
1876 | } | |
1877 | ||
1878 | else if (jfunc->type == IPA_JF_ANCESTOR) | |
1879 | return dest_lattice->set_to_bottom (); | |
86cd0334 MJ |
1880 | else if (jfunc->bits) |
1881 | return dest_lattice->meet_with (jfunc->bits->value, jfunc->bits->mask, | |
1882 | precision); | |
209ca542 PK |
1883 | else |
1884 | return dest_lattice->set_to_bottom (); | |
1885 | } | |
1886 | ||
a5e14a42 MJ |
1887 | /* Emulate effects of unary OPERATION and/or conversion from SRC_TYPE to |
1888 | DST_TYPE on value range in SRC_VR and store it to DST_VR. Return true if | |
1889 | the result is a range or an anti-range. */ | |
1890 | ||
1891 | static bool | |
1892 | ipa_vr_operation_and_type_effects (value_range *dst_vr, value_range *src_vr, | |
1893 | enum tree_code operation, | |
1894 | tree dst_type, tree src_type) | |
1895 | { | |
1896 | memset (dst_vr, 0, sizeof (*dst_vr)); | |
1897 | extract_range_from_unary_expr (dst_vr, operation, dst_type, src_vr, src_type); | |
1898 | if (dst_vr->type == VR_RANGE || dst_vr->type == VR_ANTI_RANGE) | |
1899 | return true; | |
1900 | else | |
1901 | return false; | |
1902 | } | |
1903 | ||
8bc5448f | 1904 | /* Propagate value range across jump function JFUNC that is associated with |
5d5f1e95 KV |
1905 | edge CS with param of callee of PARAM_TYPE and update DEST_PLATS |
1906 | accordingly. */ | |
8bc5448f KV |
1907 | |
1908 | static bool | |
155c9907 JJ |
1909 | propagate_vr_across_jump_function (cgraph_edge *cs, ipa_jump_func *jfunc, |
1910 | struct ipcp_param_lattices *dest_plats, | |
1911 | tree param_type) | |
8bc5448f | 1912 | { |
8bc5448f KV |
1913 | ipcp_vr_lattice *dest_lat = &dest_plats->m_value_range; |
1914 | ||
1915 | if (dest_lat->bottom_p ()) | |
1916 | return false; | |
1917 | ||
5d5f1e95 KV |
1918 | if (!param_type |
1919 | || (!INTEGRAL_TYPE_P (param_type) | |
1920 | && !POINTER_TYPE_P (param_type))) | |
1921 | return dest_lat->set_to_bottom (); | |
1922 | ||
8bc5448f KV |
1923 | if (jfunc->type == IPA_JF_PASS_THROUGH) |
1924 | { | |
a5e14a42 | 1925 | enum tree_code operation = ipa_get_jf_pass_through_operation (jfunc); |
8bc5448f | 1926 | |
a5e14a42 | 1927 | if (TREE_CODE_CLASS (operation) == tcc_unary) |
a2b4c188 | 1928 | { |
a5e14a42 MJ |
1929 | struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller); |
1930 | int src_idx = ipa_get_jf_pass_through_formal_id (jfunc); | |
a2b4c188 | 1931 | tree operand_type = ipa_get_type (caller_info, src_idx); |
a5e14a42 MJ |
1932 | struct ipcp_param_lattices *src_lats |
1933 | = ipa_get_parm_lattices (caller_info, src_idx); | |
a2b4c188 KV |
1934 | |
1935 | if (src_lats->m_value_range.bottom_p ()) | |
1936 | return dest_lat->set_to_bottom (); | |
a5e14a42 MJ |
1937 | value_range vr; |
1938 | if (ipa_vr_operation_and_type_effects (&vr, | |
1939 | &src_lats->m_value_range.m_vr, | |
1940 | operation, param_type, | |
1941 | operand_type)) | |
a2b4c188 KV |
1942 | return dest_lat->meet_with (&vr); |
1943 | } | |
8bc5448f KV |
1944 | } |
1945 | else if (jfunc->type == IPA_JF_CONST) | |
1946 | { | |
1947 | tree val = ipa_get_jf_constant (jfunc); | |
1948 | if (TREE_CODE (val) == INTEGER_CST) | |
1949 | { | |
7d22d5a3 | 1950 | val = fold_convert (param_type, val); |
1e401340 KV |
1951 | if (TREE_OVERFLOW_P (val)) |
1952 | val = drop_tree_overflow (val); | |
86cd0334 MJ |
1953 | |
1954 | value_range tmpvr; | |
1955 | memset (&tmpvr, 0, sizeof (tmpvr)); | |
1956 | tmpvr.type = VR_RANGE; | |
1957 | tmpvr.min = val; | |
1958 | tmpvr.max = val; | |
1959 | return dest_lat->meet_with (&tmpvr); | |
8bc5448f KV |
1960 | } |
1961 | } | |
1962 | ||
a5e14a42 | 1963 | value_range vr; |
86cd0334 MJ |
1964 | if (jfunc->m_vr |
1965 | && ipa_vr_operation_and_type_effects (&vr, jfunc->m_vr, NOP_EXPR, | |
a5e14a42 | 1966 | param_type, |
86cd0334 | 1967 | TREE_TYPE (jfunc->m_vr->min))) |
a5e14a42 | 1968 | return dest_lat->meet_with (&vr); |
8bc5448f KV |
1969 | else |
1970 | return dest_lat->set_to_bottom (); | |
1971 | } | |
1972 | ||
2c9561b5 MJ |
1973 | /* If DEST_PLATS already has aggregate items, check that aggs_by_ref matches |
1974 | NEW_AGGS_BY_REF and if not, mark all aggs as bottoms and return true (in all | |
1975 | other cases, return false). If there are no aggregate items, set | |
1976 | aggs_by_ref to NEW_AGGS_BY_REF. */ | |
1977 | ||
1978 | static bool | |
1979 | set_check_aggs_by_ref (struct ipcp_param_lattices *dest_plats, | |
1980 | bool new_aggs_by_ref) | |
1981 | { | |
1982 | if (dest_plats->aggs) | |
1983 | { | |
1984 | if (dest_plats->aggs_by_ref != new_aggs_by_ref) | |
1985 | { | |
1986 | set_agg_lats_to_bottom (dest_plats); | |
1987 | return true; | |
1988 | } | |
1989 | } | |
1990 | else | |
1991 | dest_plats->aggs_by_ref = new_aggs_by_ref; | |
1992 | return false; | |
1993 | } | |
1994 | ||
1995 | /* Walk aggregate lattices in DEST_PLATS from ***AGLAT on, until ***aglat is an | |
1996 | already existing lattice for the given OFFSET and SIZE, marking all skipped | |
1997 | lattices as containing variable and checking for overlaps. If there is no | |
1998 | already existing lattice for the OFFSET and VAL_SIZE, create one, initialize | |
1999 | it with offset, size and contains_variable to PRE_EXISTING, and return true, | |
2000 | unless there are too many already. If there are two many, return false. If | |
2001 | there are overlaps turn whole DEST_PLATS to bottom and return false. If any | |
2002 | skipped lattices were newly marked as containing variable, set *CHANGE to | |
2003 | true. */ | |
2004 | ||
2005 | static bool | |
2006 | merge_agg_lats_step (struct ipcp_param_lattices *dest_plats, | |
2007 | HOST_WIDE_INT offset, HOST_WIDE_INT val_size, | |
2008 | struct ipcp_agg_lattice ***aglat, | |
2009 | bool pre_existing, bool *change) | |
2010 | { | |
2011 | gcc_checking_assert (offset >= 0); | |
2012 | ||
2013 | while (**aglat && (**aglat)->offset < offset) | |
2014 | { | |
2015 | if ((**aglat)->offset + (**aglat)->size > offset) | |
2016 | { | |
2017 | set_agg_lats_to_bottom (dest_plats); | |
2018 | return false; | |
2019 | } | |
c0cb5055 | 2020 | *change |= (**aglat)->set_contains_variable (); |
2c9561b5 MJ |
2021 | *aglat = &(**aglat)->next; |
2022 | } | |
2023 | ||
2024 | if (**aglat && (**aglat)->offset == offset) | |
2025 | { | |
2026 | if ((**aglat)->size != val_size | |
155c9907 JJ |
2027 | || ((**aglat)->next |
2028 | && (**aglat)->next->offset < offset + val_size)) | |
2c9561b5 MJ |
2029 | { |
2030 | set_agg_lats_to_bottom (dest_plats); | |
2031 | return false; | |
2032 | } | |
2033 | gcc_checking_assert (!(**aglat)->next | |
2034 | || (**aglat)->next->offset >= offset + val_size); | |
2035 | return true; | |
2036 | } | |
2037 | else | |
2038 | { | |
2039 | struct ipcp_agg_lattice *new_al; | |
2040 | ||
2041 | if (**aglat && (**aglat)->offset < offset + val_size) | |
2042 | { | |
2043 | set_agg_lats_to_bottom (dest_plats); | |
2044 | return false; | |
2045 | } | |
2046 | if (dest_plats->aggs_count == PARAM_VALUE (PARAM_IPA_MAX_AGG_ITEMS)) | |
2047 | return false; | |
2048 | dest_plats->aggs_count++; | |
2651e637 | 2049 | new_al = ipcp_agg_lattice_pool.allocate (); |
2c9561b5 MJ |
2050 | memset (new_al, 0, sizeof (*new_al)); |
2051 | ||
2052 | new_al->offset = offset; | |
2053 | new_al->size = val_size; | |
2054 | new_al->contains_variable = pre_existing; | |
2055 | ||
2056 | new_al->next = **aglat; | |
2057 | **aglat = new_al; | |
2058 | return true; | |
2059 | } | |
2060 | } | |
2061 | ||
2062 | /* Set all AGLAT and all other aggregate lattices reachable by next pointers as | |
2063 | containing an unknown value. */ | |
2064 | ||
2065 | static bool | |
2066 | set_chain_of_aglats_contains_variable (struct ipcp_agg_lattice *aglat) | |
2067 | { | |
2068 | bool ret = false; | |
2069 | while (aglat) | |
2070 | { | |
c0cb5055 | 2071 | ret |= aglat->set_contains_variable (); |
2c9561b5 MJ |
2072 | aglat = aglat->next; |
2073 | } | |
2074 | return ret; | |
2075 | } | |
2076 | ||
2077 | /* Merge existing aggregate lattices in SRC_PLATS to DEST_PLATS, subtracting | |
2078 | DELTA_OFFSET. CS is the call graph edge and SRC_IDX the index of the source | |
2079 | parameter used for lattice value sources. Return true if DEST_PLATS changed | |
2080 | in any way. */ | |
2081 | ||
2082 | static bool | |
2083 | merge_aggregate_lattices (struct cgraph_edge *cs, | |
2084 | struct ipcp_param_lattices *dest_plats, | |
2085 | struct ipcp_param_lattices *src_plats, | |
2086 | int src_idx, HOST_WIDE_INT offset_delta) | |
2087 | { | |
2088 | bool pre_existing = dest_plats->aggs != NULL; | |
2089 | struct ipcp_agg_lattice **dst_aglat; | |
2090 | bool ret = false; | |
2091 | ||
2092 | if (set_check_aggs_by_ref (dest_plats, src_plats->aggs_by_ref)) | |
2093 | return true; | |
2094 | if (src_plats->aggs_bottom) | |
2095 | return set_agg_lats_contain_variable (dest_plats); | |
3e452a28 MJ |
2096 | if (src_plats->aggs_contain_variable) |
2097 | ret |= set_agg_lats_contain_variable (dest_plats); | |
2c9561b5 MJ |
2098 | dst_aglat = &dest_plats->aggs; |
2099 | ||
2100 | for (struct ipcp_agg_lattice *src_aglat = src_plats->aggs; | |
2101 | src_aglat; | |
2102 | src_aglat = src_aglat->next) | |
2103 | { | |
2104 | HOST_WIDE_INT new_offset = src_aglat->offset - offset_delta; | |
2105 | ||
2106 | if (new_offset < 0) | |
2107 | continue; | |
2108 | if (merge_agg_lats_step (dest_plats, new_offset, src_aglat->size, | |
2109 | &dst_aglat, pre_existing, &ret)) | |
2110 | { | |
2111 | struct ipcp_agg_lattice *new_al = *dst_aglat; | |
2112 | ||
2113 | dst_aglat = &(*dst_aglat)->next; | |
2114 | if (src_aglat->bottom) | |
2115 | { | |
c0cb5055 | 2116 | ret |= new_al->set_contains_variable (); |
2c9561b5 MJ |
2117 | continue; |
2118 | } | |
2119 | if (src_aglat->contains_variable) | |
c0cb5055 MJ |
2120 | ret |= new_al->set_contains_variable (); |
2121 | for (ipcp_value<tree> *val = src_aglat->values; | |
2c9561b5 MJ |
2122 | val; |
2123 | val = val->next) | |
c0cb5055 MJ |
2124 | ret |= new_al->add_value (val->value, cs, val, src_idx, |
2125 | src_aglat->offset); | |
2c9561b5 MJ |
2126 | } |
2127 | else if (dest_plats->aggs_bottom) | |
2128 | return true; | |
2129 | } | |
2130 | ret |= set_chain_of_aglats_contains_variable (*dst_aglat); | |
2131 | return ret; | |
2132 | } | |
2133 | ||
324e93f1 MJ |
2134 | /* Determine whether there is anything to propagate FROM SRC_PLATS through a |
2135 | pass-through JFUNC and if so, whether it has conform and conforms to the | |
2136 | rules about propagating values passed by reference. */ | |
2137 | ||
2138 | static bool | |
2139 | agg_pass_through_permissible_p (struct ipcp_param_lattices *src_plats, | |
2140 | struct ipa_jump_func *jfunc) | |
2141 | { | |
2142 | return src_plats->aggs | |
2143 | && (!src_plats->aggs_by_ref | |
2144 | || ipa_get_jf_pass_through_agg_preserved (jfunc)); | |
2145 | } | |
2146 | ||
2c9561b5 MJ |
2147 | /* Propagate scalar values across jump function JFUNC that is associated with |
2148 | edge CS and put the values into DEST_LAT. */ | |
2149 | ||
2150 | static bool | |
155c9907 JJ |
2151 | propagate_aggs_across_jump_function (struct cgraph_edge *cs, |
2152 | struct ipa_jump_func *jfunc, | |
2153 | struct ipcp_param_lattices *dest_plats) | |
2c9561b5 MJ |
2154 | { |
2155 | bool ret = false; | |
2156 | ||
2157 | if (dest_plats->aggs_bottom) | |
2158 | return false; | |
2159 | ||
2160 | if (jfunc->type == IPA_JF_PASS_THROUGH | |
2161 | && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR) | |
2162 | { | |
2163 | struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller); | |
2164 | int src_idx = ipa_get_jf_pass_through_formal_id (jfunc); | |
2165 | struct ipcp_param_lattices *src_plats; | |
2166 | ||
2167 | src_plats = ipa_get_parm_lattices (caller_info, src_idx); | |
324e93f1 | 2168 | if (agg_pass_through_permissible_p (src_plats, jfunc)) |
2c9561b5 MJ |
2169 | { |
2170 | /* Currently we do not produce clobber aggregate jump | |
2171 | functions, replace with merging when we do. */ | |
2172 | gcc_assert (!jfunc->agg.items); | |
2173 | ret |= merge_aggregate_lattices (cs, dest_plats, src_plats, | |
2174 | src_idx, 0); | |
2175 | } | |
2176 | else | |
2177 | ret |= set_agg_lats_contain_variable (dest_plats); | |
2178 | } | |
2179 | else if (jfunc->type == IPA_JF_ANCESTOR | |
2180 | && ipa_get_jf_ancestor_agg_preserved (jfunc)) | |
2181 | { | |
2182 | struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller); | |
2183 | int src_idx = ipa_get_jf_ancestor_formal_id (jfunc); | |
2184 | struct ipcp_param_lattices *src_plats; | |
2185 | ||
2186 | src_plats = ipa_get_parm_lattices (caller_info, src_idx); | |
2187 | if (src_plats->aggs && src_plats->aggs_by_ref) | |
2188 | { | |
2189 | /* Currently we do not produce clobber aggregate jump | |
2190 | functions, replace with merging when we do. */ | |
2191 | gcc_assert (!jfunc->agg.items); | |
2192 | ret |= merge_aggregate_lattices (cs, dest_plats, src_plats, src_idx, | |
2193 | ipa_get_jf_ancestor_offset (jfunc)); | |
2194 | } | |
2195 | else if (!src_plats->aggs_by_ref) | |
2196 | ret |= set_agg_lats_to_bottom (dest_plats); | |
2197 | else | |
2198 | ret |= set_agg_lats_contain_variable (dest_plats); | |
2199 | } | |
2200 | else if (jfunc->agg.items) | |
2201 | { | |
2202 | bool pre_existing = dest_plats->aggs != NULL; | |
2203 | struct ipcp_agg_lattice **aglat = &dest_plats->aggs; | |
2204 | struct ipa_agg_jf_item *item; | |
2205 | int i; | |
2206 | ||
2207 | if (set_check_aggs_by_ref (dest_plats, jfunc->agg.by_ref)) | |
2208 | return true; | |
2209 | ||
9771b263 | 2210 | FOR_EACH_VEC_ELT (*jfunc->agg.items, i, item) |
2c9561b5 MJ |
2211 | { |
2212 | HOST_WIDE_INT val_size; | |
2213 | ||
2214 | if (item->offset < 0) | |
2215 | continue; | |
2216 | gcc_checking_assert (is_gimple_ip_invariant (item->value)); | |
ae7e9ddd | 2217 | val_size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (item->value))); |
2c9561b5 MJ |
2218 | |
2219 | if (merge_agg_lats_step (dest_plats, item->offset, val_size, | |
2220 | &aglat, pre_existing, &ret)) | |
2221 | { | |
c0cb5055 | 2222 | ret |= (*aglat)->add_value (item->value, cs, NULL, 0, 0); |
2c9561b5 MJ |
2223 | aglat = &(*aglat)->next; |
2224 | } | |
2225 | else if (dest_plats->aggs_bottom) | |
2226 | return true; | |
2227 | } | |
2228 | ||
2229 | ret |= set_chain_of_aglats_contains_variable (*aglat); | |
2230 | } | |
2231 | else | |
2232 | ret |= set_agg_lats_contain_variable (dest_plats); | |
2233 | ||
2234 | return ret; | |
2235 | } | |
2236 | ||
173b7355 MJ |
2237 | /* Return true if on the way cfrom CS->caller to the final (non-alias and |
2238 | non-thunk) destination, the call passes through a thunk. */ | |
2239 | ||
2240 | static bool | |
2241 | call_passes_through_thunk_p (cgraph_edge *cs) | |
2242 | { | |
2243 | cgraph_node *alias_or_thunk = cs->callee; | |
2244 | while (alias_or_thunk->alias) | |
2245 | alias_or_thunk = alias_or_thunk->get_alias_target (); | |
2246 | return alias_or_thunk->thunk.thunk_p; | |
2247 | } | |
2248 | ||
310bc633 MJ |
2249 | /* Propagate constants from the caller to the callee of CS. INFO describes the |
2250 | caller. */ | |
2251 | ||
2252 | static bool | |
155c9907 | 2253 | propagate_constants_across_call (struct cgraph_edge *cs) |
310bc633 MJ |
2254 | { |
2255 | struct ipa_node_params *callee_info; | |
2256 | enum availability availability; | |
173b7355 | 2257 | cgraph_node *callee; |
310bc633 MJ |
2258 | struct ipa_edge_args *args; |
2259 | bool ret = false; | |
d7da5cc8 | 2260 | int i, args_count, parms_count; |
310bc633 | 2261 | |
d52f5295 | 2262 | callee = cs->callee->function_symbol (&availability); |
67348ccc | 2263 | if (!callee->definition) |
310bc633 | 2264 | return false; |
d52f5295 | 2265 | gcc_checking_assert (callee->has_gimple_body_p ()); |
310bc633 | 2266 | callee_info = IPA_NODE_REF (callee); |
310bc633 MJ |
2267 | |
2268 | args = IPA_EDGE_REF (cs); | |
d7da5cc8 MJ |
2269 | args_count = ipa_get_cs_argument_count (args); |
2270 | parms_count = ipa_get_param_count (callee_info); | |
f3fec19f MJ |
2271 | if (parms_count == 0) |
2272 | return false; | |
310bc633 MJ |
2273 | |
2274 | /* If this call goes through a thunk we must not propagate to the first (0th) | |
2275 | parameter. However, we might need to uncover a thunk from below a series | |
2276 | of aliases first. */ | |
173b7355 | 2277 | if (call_passes_through_thunk_p (cs)) |
310bc633 | 2278 | { |
2c9561b5 MJ |
2279 | ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info, |
2280 | 0)); | |
310bc633 MJ |
2281 | i = 1; |
2282 | } | |
2283 | else | |
2284 | i = 0; | |
2285 | ||
d7da5cc8 | 2286 | for (; (i < args_count) && (i < parms_count); i++) |
310bc633 MJ |
2287 | { |
2288 | struct ipa_jump_func *jump_func = ipa_get_ith_jump_func (args, i); | |
2c9561b5 | 2289 | struct ipcp_param_lattices *dest_plats; |
a5e14a42 | 2290 | tree param_type = ipa_get_type (callee_info, i); |
310bc633 | 2291 | |
2c9561b5 | 2292 | dest_plats = ipa_get_parm_lattices (callee_info, i); |
d52f5295 | 2293 | if (availability == AVAIL_INTERPOSABLE) |
2c9561b5 | 2294 | ret |= set_all_contains_variable (dest_plats); |
310bc633 | 2295 | else |
2c9561b5 | 2296 | { |
155c9907 | 2297 | ret |= propagate_scalar_across_jump_function (cs, jump_func, |
e5cf5e11 PK |
2298 | &dest_plats->itself, |
2299 | param_type); | |
155c9907 JJ |
2300 | ret |= propagate_context_across_jump_function (cs, jump_func, i, |
2301 | &dest_plats->ctxlat); | |
2302 | ret | |
2303 | |= propagate_bits_across_jump_function (cs, i, jump_func, | |
2304 | &dest_plats->bits_lattice); | |
2305 | ret |= propagate_aggs_across_jump_function (cs, jump_func, | |
2306 | dest_plats); | |
8bc5448f | 2307 | if (opt_for_fn (callee->decl, flag_ipa_vrp)) |
155c9907 JJ |
2308 | ret |= propagate_vr_across_jump_function (cs, jump_func, |
2309 | dest_plats, param_type); | |
8bc5448f KV |
2310 | else |
2311 | ret |= dest_plats->m_value_range.set_to_bottom (); | |
2c9561b5 | 2312 | } |
310bc633 | 2313 | } |
d7da5cc8 | 2314 | for (; i < parms_count; i++) |
2c9561b5 | 2315 | ret |= set_all_contains_variable (ipa_get_parm_lattices (callee_info, i)); |
d7da5cc8 | 2316 | |
310bc633 MJ |
2317 | return ret; |
2318 | } | |
2319 | ||
2320 | /* If an indirect edge IE can be turned into a direct one based on KNOWN_VALS | |
3b97a5c7 MJ |
2321 | KNOWN_CONTEXTS, KNOWN_AGGS or AGG_REPS return the destination. The latter |
2322 | three can be NULL. If AGG_REPS is not NULL, KNOWN_AGGS is ignored. */ | |
310bc633 | 2323 | |
162712de MJ |
2324 | static tree |
2325 | ipa_get_indirect_edge_target_1 (struct cgraph_edge *ie, | |
44210a96 MJ |
2326 | vec<tree> known_csts, |
2327 | vec<ipa_polymorphic_call_context> known_contexts, | |
162712de | 2328 | vec<ipa_agg_jump_function_p> known_aggs, |
231b4916 JH |
2329 | struct ipa_agg_replacement_value *agg_reps, |
2330 | bool *speculative) | |
310bc633 MJ |
2331 | { |
2332 | int param_index = ie->indirect_info->param_index; | |
44210a96 | 2333 | HOST_WIDE_INT anc_offset; |
310bc633 | 2334 | tree t; |
85942f45 | 2335 | tree target = NULL; |
310bc633 | 2336 | |
231b4916 JH |
2337 | *speculative = false; |
2338 | ||
97756c0e | 2339 | if (param_index == -1 |
44210a96 | 2340 | || known_csts.length () <= (unsigned int) param_index) |
310bc633 MJ |
2341 | return NULL_TREE; |
2342 | ||
2343 | if (!ie->indirect_info->polymorphic) | |
2344 | { | |
8810cc52 MJ |
2345 | tree t; |
2346 | ||
2347 | if (ie->indirect_info->agg_contents) | |
2348 | { | |
91bb9f80 MJ |
2349 | t = NULL; |
2350 | if (agg_reps && ie->indirect_info->guaranteed_unmodified) | |
162712de | 2351 | { |
162712de MJ |
2352 | while (agg_reps) |
2353 | { | |
2354 | if (agg_reps->index == param_index | |
7b920a9a MJ |
2355 | && agg_reps->offset == ie->indirect_info->offset |
2356 | && agg_reps->by_ref == ie->indirect_info->by_ref) | |
162712de MJ |
2357 | { |
2358 | t = agg_reps->value; | |
2359 | break; | |
2360 | } | |
2361 | agg_reps = agg_reps->next; | |
2362 | } | |
2363 | } | |
91bb9f80 | 2364 | if (!t) |
8810cc52 MJ |
2365 | { |
2366 | struct ipa_agg_jump_function *agg; | |
91bb9f80 MJ |
2367 | if (known_aggs.length () > (unsigned int) param_index) |
2368 | agg = known_aggs[param_index]; | |
2369 | else | |
2370 | agg = NULL; | |
2371 | bool from_global_constant; | |
2372 | t = ipa_find_agg_cst_for_param (agg, known_csts[param_index], | |
2373 | ie->indirect_info->offset, | |
2374 | ie->indirect_info->by_ref, | |
2375 | &from_global_constant); | |
44a71f36 MJ |
2376 | if (t |
2377 | && !from_global_constant | |
91bb9f80 MJ |
2378 | && !ie->indirect_info->guaranteed_unmodified) |
2379 | t = NULL_TREE; | |
8810cc52 | 2380 | } |
8810cc52 MJ |
2381 | } |
2382 | else | |
44210a96 | 2383 | t = known_csts[param_index]; |
8810cc52 | 2384 | |
155c9907 JJ |
2385 | if (t |
2386 | && TREE_CODE (t) == ADDR_EXPR | |
310bc633 | 2387 | && TREE_CODE (TREE_OPERAND (t, 0)) == FUNCTION_DECL) |
81fa35bd | 2388 | return TREE_OPERAND (t, 0); |
310bc633 MJ |
2389 | else |
2390 | return NULL_TREE; | |
2391 | } | |
2392 | ||
2bf86c84 | 2393 | if (!opt_for_fn (ie->caller->decl, flag_devirtualize)) |
85942f45 JH |
2394 | return NULL_TREE; |
2395 | ||
8810cc52 | 2396 | gcc_assert (!ie->indirect_info->agg_contents); |
8b7773a4 | 2397 | anc_offset = ie->indirect_info->offset; |
310bc633 | 2398 | |
85942f45 JH |
2399 | t = NULL; |
2400 | ||
2401 | /* Try to work out value of virtual table pointer value in replacemnets. */ | |
231b4916 | 2402 | if (!t && agg_reps && !ie->indirect_info->by_ref) |
85942f45 JH |
2403 | { |
2404 | while (agg_reps) | |
2405 | { | |
2406 | if (agg_reps->index == param_index | |
2407 | && agg_reps->offset == ie->indirect_info->offset | |
2408 | && agg_reps->by_ref) | |
2409 | { | |
2410 | t = agg_reps->value; | |
2411 | break; | |
2412 | } | |
2413 | agg_reps = agg_reps->next; | |
2414 | } | |
2415 | } | |
2416 | ||
2417 | /* Try to work out value of virtual table pointer value in known | |
2418 | aggregate values. */ | |
2419 | if (!t && known_aggs.length () > (unsigned int) param_index | |
231b4916 | 2420 | && !ie->indirect_info->by_ref) |
85942f45 | 2421 | { |
155c9907 JJ |
2422 | struct ipa_agg_jump_function *agg; |
2423 | agg = known_aggs[param_index]; | |
2424 | t = ipa_find_agg_cst_for_param (agg, known_csts[param_index], | |
2425 | ie->indirect_info->offset, true); | |
85942f45 JH |
2426 | } |
2427 | ||
9de2f554 | 2428 | /* If we found the virtual table pointer, lookup the target. */ |
85942f45 | 2429 | if (t) |
9de2f554 JH |
2430 | { |
2431 | tree vtable; | |
2432 | unsigned HOST_WIDE_INT offset; | |
2433 | if (vtable_pointer_value_to_vtable (t, &vtable, &offset)) | |
2434 | { | |
2994ab20 | 2435 | bool can_refer; |
9de2f554 | 2436 | target = gimple_get_virt_method_for_vtable (ie->indirect_info->otr_token, |
2994ab20 JH |
2437 | vtable, offset, &can_refer); |
2438 | if (can_refer) | |
9de2f554 | 2439 | { |
2994ab20 JH |
2440 | if (!target |
2441 | || (TREE_CODE (TREE_TYPE (target)) == FUNCTION_TYPE | |
2442 | && DECL_FUNCTION_CODE (target) == BUILT_IN_UNREACHABLE) | |
8472fa80 | 2443 | || !possible_polymorphic_call_target_p |
d52f5295 | 2444 | (ie, cgraph_node::get (target))) |
2994ab20 JH |
2445 | { |
2446 | /* Do not speculate builtin_unreachable, it is stupid! */ | |
2447 | if (ie->indirect_info->vptr_changed) | |
2448 | return NULL; | |
2449 | target = ipa_impossible_devirt_target (ie, target); | |
2450 | } | |
155c9907 | 2451 | *speculative = ie->indirect_info->vptr_changed; |
231b4916 | 2452 | if (!*speculative) |
155c9907 | 2453 | return target; |
9de2f554 | 2454 | } |
9de2f554 JH |
2455 | } |
2456 | } | |
85942f45 | 2457 | |
44210a96 | 2458 | /* Do we know the constant value of pointer? */ |
85942f45 | 2459 | if (!t) |
44210a96 | 2460 | t = known_csts[param_index]; |
310bc633 | 2461 | |
44210a96 MJ |
2462 | gcc_checking_assert (!t || TREE_CODE (t) != TREE_BINFO); |
2463 | ||
2464 | ipa_polymorphic_call_context context; | |
2465 | if (known_contexts.length () > (unsigned int) param_index) | |
310bc633 | 2466 | { |
44210a96 | 2467 | context = known_contexts[param_index]; |
df0d8136 JH |
2468 | context.offset_by (anc_offset); |
2469 | if (ie->indirect_info->vptr_changed) | |
2470 | context.possible_dynamic_type_change (ie->in_polymorphic_cdtor, | |
2471 | ie->indirect_info->otr_type); | |
44210a96 MJ |
2472 | if (t) |
2473 | { | |
2474 | ipa_polymorphic_call_context ctx2 = ipa_polymorphic_call_context | |
2475 | (t, ie->indirect_info->otr_type, anc_offset); | |
2476 | if (!ctx2.useless_p ()) | |
2477 | context.combine_with (ctx2, ie->indirect_info->otr_type); | |
2478 | } | |
310bc633 | 2479 | } |
44210a96 | 2480 | else if (t) |
33c3b6be JH |
2481 | { |
2482 | context = ipa_polymorphic_call_context (t, ie->indirect_info->otr_type, | |
2483 | anc_offset); | |
2484 | if (ie->indirect_info->vptr_changed) | |
2485 | context.possible_dynamic_type_change (ie->in_polymorphic_cdtor, | |
2486 | ie->indirect_info->otr_type); | |
2487 | } | |
310bc633 | 2488 | else |
44210a96 | 2489 | return NULL_TREE; |
310bc633 | 2490 | |
44210a96 MJ |
2491 | vec <cgraph_node *>targets; |
2492 | bool final; | |
2493 | ||
2494 | targets = possible_polymorphic_call_targets | |
2495 | (ie->indirect_info->otr_type, | |
2496 | ie->indirect_info->otr_token, | |
2497 | context, &final); | |
2498 | if (!final || targets.length () > 1) | |
231b4916 JH |
2499 | { |
2500 | struct cgraph_node *node; | |
2501 | if (*speculative) | |
2502 | return target; | |
2bf86c84 JH |
2503 | if (!opt_for_fn (ie->caller->decl, flag_devirtualize_speculatively) |
2504 | || ie->speculative || !ie->maybe_hot_p ()) | |
231b4916 JH |
2505 | return NULL; |
2506 | node = try_speculative_devirtualization (ie->indirect_info->otr_type, | |
2507 | ie->indirect_info->otr_token, | |
2508 | context); | |
2509 | if (node) | |
2510 | { | |
2511 | *speculative = true; | |
2512 | target = node->decl; | |
2513 | } | |
2514 | else | |
2515 | return NULL; | |
2516 | } | |
44210a96 | 2517 | else |
231b4916 JH |
2518 | { |
2519 | *speculative = false; | |
2520 | if (targets.length () == 1) | |
2521 | target = targets[0]->decl; | |
2522 | else | |
2523 | target = ipa_impossible_devirt_target (ie, NULL_TREE); | |
2524 | } | |
b5165eb0 MJ |
2525 | |
2526 | if (target && !possible_polymorphic_call_target_p (ie, | |
d52f5295 | 2527 | cgraph_node::get (target))) |
2994ab20 JH |
2528 | { |
2529 | if (*speculative) | |
2530 | return NULL; | |
2531 | target = ipa_impossible_devirt_target (ie, target); | |
2532 | } | |
450ad0cd JH |
2533 | |
2534 | return target; | |
310bc633 MJ |
2535 | } |
2536 | ||
162712de | 2537 | |
44210a96 MJ |
2538 | /* If an indirect edge IE can be turned into a direct one based on KNOWN_CSTS, |
2539 | KNOWN_CONTEXTS (which can be vNULL) or KNOWN_AGGS (which also can be vNULL) | |
2540 | return the destination. */ | |
162712de MJ |
2541 | |
2542 | tree | |
2543 | ipa_get_indirect_edge_target (struct cgraph_edge *ie, | |
44210a96 MJ |
2544 | vec<tree> known_csts, |
2545 | vec<ipa_polymorphic_call_context> known_contexts, | |
231b4916 JH |
2546 | vec<ipa_agg_jump_function_p> known_aggs, |
2547 | bool *speculative) | |
162712de | 2548 | { |
44210a96 | 2549 | return ipa_get_indirect_edge_target_1 (ie, known_csts, known_contexts, |
231b4916 | 2550 | known_aggs, NULL, speculative); |
162712de MJ |
2551 | } |
2552 | ||
310bc633 | 2553 | /* Calculate devirtualization time bonus for NODE, assuming we know KNOWN_CSTS |
44210a96 | 2554 | and KNOWN_CONTEXTS. */ |
310bc633 MJ |
2555 | |
2556 | static int | |
2557 | devirtualization_time_bonus (struct cgraph_node *node, | |
9771b263 | 2558 | vec<tree> known_csts, |
44210a96 | 2559 | vec<ipa_polymorphic_call_context> known_contexts, |
162712de | 2560 | vec<ipa_agg_jump_function_p> known_aggs) |
310bc633 MJ |
2561 | { |
2562 | struct cgraph_edge *ie; | |
2563 | int res = 0; | |
2564 | ||
2565 | for (ie = node->indirect_calls; ie; ie = ie->next_callee) | |
2566 | { | |
2567 | struct cgraph_node *callee; | |
0bceb671 | 2568 | struct ipa_fn_summary *isummary; |
8ad274d2 | 2569 | enum availability avail; |
81fa35bd | 2570 | tree target; |
231b4916 | 2571 | bool speculative; |
310bc633 | 2572 | |
44210a96 | 2573 | target = ipa_get_indirect_edge_target (ie, known_csts, known_contexts, |
231b4916 | 2574 | known_aggs, &speculative); |
310bc633 MJ |
2575 | if (!target) |
2576 | continue; | |
2577 | ||
2578 | /* Only bare minimum benefit for clearly un-inlineable targets. */ | |
2579 | res += 1; | |
d52f5295 | 2580 | callee = cgraph_node::get (target); |
67348ccc | 2581 | if (!callee || !callee->definition) |
310bc633 | 2582 | continue; |
d52f5295 | 2583 | callee = callee->function_symbol (&avail); |
8ad274d2 JH |
2584 | if (avail < AVAIL_AVAILABLE) |
2585 | continue; | |
0bceb671 | 2586 | isummary = ipa_fn_summaries->get (callee); |
310bc633 MJ |
2587 | if (!isummary->inlinable) |
2588 | continue; | |
2589 | ||
2590 | /* FIXME: The values below need re-considering and perhaps also | |
2591 | integrating into the cost metrics, at lest in some very basic way. */ | |
2592 | if (isummary->size <= MAX_INLINE_INSNS_AUTO / 4) | |
231b4916 | 2593 | res += 31 / ((int)speculative + 1); |
310bc633 | 2594 | else if (isummary->size <= MAX_INLINE_INSNS_AUTO / 2) |
231b4916 | 2595 | res += 15 / ((int)speculative + 1); |
310bc633 | 2596 | else if (isummary->size <= MAX_INLINE_INSNS_AUTO |
67348ccc | 2597 | || DECL_DECLARED_INLINE_P (callee->decl)) |
231b4916 | 2598 | res += 7 / ((int)speculative + 1); |
310bc633 MJ |
2599 | } |
2600 | ||
2601 | return res; | |
2602 | } | |
2603 | ||
2c9561b5 MJ |
2604 | /* Return time bonus incurred because of HINTS. */ |
2605 | ||
2606 | static int | |
0bceb671 | 2607 | hint_time_bonus (ipa_hints hints) |
2c9561b5 | 2608 | { |
19321415 | 2609 | int result = 0; |
2c9561b5 | 2610 | if (hints & (INLINE_HINT_loop_iterations | INLINE_HINT_loop_stride)) |
19321415 MJ |
2611 | result += PARAM_VALUE (PARAM_IPA_CP_LOOP_HINT_BONUS); |
2612 | if (hints & INLINE_HINT_array_index) | |
2613 | result += PARAM_VALUE (PARAM_IPA_CP_ARRAY_INDEX_HINT_BONUS); | |
2614 | return result; | |
2c9561b5 MJ |
2615 | } |
2616 | ||
af21714c MJ |
2617 | /* If there is a reason to penalize the function described by INFO in the |
2618 | cloning goodness evaluation, do so. */ | |
2619 | ||
2620 | static inline int64_t | |
2621 | incorporate_penalties (ipa_node_params *info, int64_t evaluation) | |
2622 | { | |
2623 | if (info->node_within_scc) | |
2624 | evaluation = (evaluation | |
2625 | * (100 - PARAM_VALUE (PARAM_IPA_CP_RECURSION_PENALTY))) / 100; | |
2626 | ||
2627 | if (info->node_calling_single_call) | |
2628 | evaluation = (evaluation | |
2629 | * (100 - PARAM_VALUE (PARAM_IPA_CP_SINGLE_CALL_PENALTY))) | |
2630 | / 100; | |
2631 | ||
2632 | return evaluation; | |
2633 | } | |
2634 | ||
310bc633 MJ |
2635 | /* Return true if cloning NODE is a good idea, given the estimated TIME_BENEFIT |
2636 | and SIZE_COST and with the sum of frequencies of incoming edges to the | |
2637 | potential new clone in FREQUENCIES. */ | |
2638 | ||
2639 | static bool | |
2640 | good_cloning_opportunity_p (struct cgraph_node *node, int time_benefit, | |
3995f3a2 | 2641 | int freq_sum, profile_count count_sum, int size_cost) |
310bc633 MJ |
2642 | { |
2643 | if (time_benefit == 0 | |
2bf86c84 | 2644 | || !opt_for_fn (node->decl, flag_ipa_cp_clone) |
5af56ae8 | 2645 | || node->optimize_for_size_p ()) |
310bc633 MJ |
2646 | return false; |
2647 | ||
df0227c4 | 2648 | gcc_assert (size_cost > 0); |
310bc633 | 2649 | |
af21714c | 2650 | struct ipa_node_params *info = IPA_NODE_REF (node); |
3995f3a2 | 2651 | if (max_count > profile_count::zero ()) |
310bc633 | 2652 | { |
357067f2 JH |
2653 | int factor = RDIV (count_sum.probability_in |
2654 | (max_count).to_reg_br_prob_base () | |
3995f3a2 | 2655 | * 1000, REG_BR_PROB_BASE); |
a9243bfc | 2656 | int64_t evaluation = (((int64_t) time_benefit * factor) |
df0227c4 | 2657 | / size_cost); |
af21714c | 2658 | evaluation = incorporate_penalties (info, evaluation); |
310bc633 MJ |
2659 | |
2660 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3995f3a2 JH |
2661 | { |
2662 | fprintf (dump_file, " good_cloning_opportunity_p (time: %i, " | |
2663 | "size: %i, count_sum: ", time_benefit, size_cost); | |
2664 | count_sum.dump (dump_file); | |
2665 | fprintf (dump_file, "%s%s) -> evaluation: " "%" PRId64 | |
df0227c4 | 2666 | ", threshold: %i\n", |
af21714c MJ |
2667 | info->node_within_scc ? ", scc" : "", |
2668 | info->node_calling_single_call ? ", single_call" : "", | |
7a92038b | 2669 | evaluation, PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD)); |
3995f3a2 | 2670 | } |
310bc633 MJ |
2671 | |
2672 | return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD); | |
2673 | } | |
2674 | else | |
2675 | { | |
a9243bfc | 2676 | int64_t evaluation = (((int64_t) time_benefit * freq_sum) |
df0227c4 | 2677 | / size_cost); |
af21714c | 2678 | evaluation = incorporate_penalties (info, evaluation); |
310bc633 MJ |
2679 | |
2680 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2681 | fprintf (dump_file, " good_cloning_opportunity_p (time: %i, " | |
af21714c | 2682 | "size: %i, freq_sum: %i%s%s) -> evaluation: " |
16998094 | 2683 | "%" PRId64 ", threshold: %i\n", |
af21714c MJ |
2684 | time_benefit, size_cost, freq_sum, |
2685 | info->node_within_scc ? ", scc" : "", | |
2686 | info->node_calling_single_call ? ", single_call" : "", | |
2687 | evaluation, PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD)); | |
310bc633 MJ |
2688 | |
2689 | return evaluation >= PARAM_VALUE (PARAM_IPA_CP_EVAL_THRESHOLD); | |
2690 | } | |
2691 | } | |
2692 | ||
2c9561b5 MJ |
2693 | /* Return all context independent values from aggregate lattices in PLATS in a |
2694 | vector. Return NULL if there are none. */ | |
2695 | ||
84562394 | 2696 | static vec<ipa_agg_jf_item, va_gc> * |
2c9561b5 MJ |
2697 | context_independent_aggregate_values (struct ipcp_param_lattices *plats) |
2698 | { | |
84562394 | 2699 | vec<ipa_agg_jf_item, va_gc> *res = NULL; |
2c9561b5 MJ |
2700 | |
2701 | if (plats->aggs_bottom | |
2702 | || plats->aggs_contain_variable | |
2703 | || plats->aggs_count == 0) | |
2704 | return NULL; | |
2705 | ||
2706 | for (struct ipcp_agg_lattice *aglat = plats->aggs; | |
2707 | aglat; | |
2708 | aglat = aglat->next) | |
c0cb5055 | 2709 | if (aglat->is_single_const ()) |
2c9561b5 MJ |
2710 | { |
2711 | struct ipa_agg_jf_item item; | |
2712 | item.offset = aglat->offset; | |
2713 | item.value = aglat->values->value; | |
9771b263 | 2714 | vec_safe_push (res, item); |
2c9561b5 MJ |
2715 | } |
2716 | return res; | |
2717 | } | |
310bc633 | 2718 | |
44210a96 MJ |
2719 | /* Allocate KNOWN_CSTS, KNOWN_CONTEXTS and, if non-NULL, KNOWN_AGGS and |
2720 | populate them with values of parameters that are known independent of the | |
2721 | context. INFO describes the function. If REMOVABLE_PARAMS_COST is | |
2722 | non-NULL, the movement cost of all removable parameters will be stored in | |
2723 | it. */ | |
310bc633 MJ |
2724 | |
2725 | static bool | |
2726 | gather_context_independent_values (struct ipa_node_params *info, | |
44210a96 MJ |
2727 | vec<tree> *known_csts, |
2728 | vec<ipa_polymorphic_call_context> | |
2729 | *known_contexts, | |
2730 | vec<ipa_agg_jump_function> *known_aggs, | |
2731 | int *removable_params_cost) | |
310bc633 MJ |
2732 | { |
2733 | int i, count = ipa_get_param_count (info); | |
2734 | bool ret = false; | |
2735 | ||
9771b263 | 2736 | known_csts->create (0); |
44210a96 | 2737 | known_contexts->create (0); |
9771b263 | 2738 | known_csts->safe_grow_cleared (count); |
44210a96 | 2739 | known_contexts->safe_grow_cleared (count); |
2c9561b5 MJ |
2740 | if (known_aggs) |
2741 | { | |
9771b263 DN |
2742 | known_aggs->create (0); |
2743 | known_aggs->safe_grow_cleared (count); | |
2c9561b5 | 2744 | } |
310bc633 MJ |
2745 | |
2746 | if (removable_params_cost) | |
2747 | *removable_params_cost = 0; | |
2748 | ||
155c9907 | 2749 | for (i = 0; i < count; i++) |
310bc633 | 2750 | { |
2c9561b5 | 2751 | struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i); |
c0cb5055 | 2752 | ipcp_lattice<tree> *lat = &plats->itself; |
310bc633 | 2753 | |
c0cb5055 | 2754 | if (lat->is_single_const ()) |
310bc633 | 2755 | { |
c0cb5055 | 2756 | ipcp_value<tree> *val = lat->values; |
44210a96 MJ |
2757 | gcc_checking_assert (TREE_CODE (val->value) != TREE_BINFO); |
2758 | (*known_csts)[i] = val->value; | |
2759 | if (removable_params_cost) | |
2760 | *removable_params_cost | |
2761 | += estimate_move_cost (TREE_TYPE (val->value), false); | |
2762 | ret = true; | |
310bc633 MJ |
2763 | } |
2764 | else if (removable_params_cost | |
2765 | && !ipa_is_param_used (info, i)) | |
2766 | *removable_params_cost | |
0e8853ee | 2767 | += ipa_get_param_move_cost (info, i); |
2c9561b5 | 2768 | |
5af56ae8 JH |
2769 | if (!ipa_is_param_used (info, i)) |
2770 | continue; | |
2771 | ||
44210a96 | 2772 | ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat; |
5af56ae8 JH |
2773 | /* Do not account known context as reason for cloning. We can see |
2774 | if it permits devirtualization. */ | |
44210a96 | 2775 | if (ctxlat->is_single_const ()) |
5af56ae8 | 2776 | (*known_contexts)[i] = ctxlat->values->value; |
44210a96 | 2777 | |
2c9561b5 MJ |
2778 | if (known_aggs) |
2779 | { | |
84562394 | 2780 | vec<ipa_agg_jf_item, va_gc> *agg_items; |
2c9561b5 MJ |
2781 | struct ipa_agg_jump_function *ajf; |
2782 | ||
2783 | agg_items = context_independent_aggregate_values (plats); | |
9771b263 | 2784 | ajf = &(*known_aggs)[i]; |
2c9561b5 MJ |
2785 | ajf->items = agg_items; |
2786 | ajf->by_ref = plats->aggs_by_ref; | |
2787 | ret |= agg_items != NULL; | |
2788 | } | |
310bc633 MJ |
2789 | } |
2790 | ||
2791 | return ret; | |
2792 | } | |
2793 | ||
2c9561b5 MJ |
2794 | /* The current interface in ipa-inline-analysis requires a pointer vector. |
2795 | Create it. | |
2796 | ||
2797 | FIXME: That interface should be re-worked, this is slightly silly. Still, | |
2798 | I'd like to discuss how to change it first and this demonstrates the | |
2799 | issue. */ | |
2800 | ||
9771b263 | 2801 | static vec<ipa_agg_jump_function_p> |
84562394 | 2802 | agg_jmp_p_vec_for_t_vec (vec<ipa_agg_jump_function> known_aggs) |
2c9561b5 | 2803 | { |
9771b263 | 2804 | vec<ipa_agg_jump_function_p> ret; |
2c9561b5 MJ |
2805 | struct ipa_agg_jump_function *ajf; |
2806 | int i; | |
2807 | ||
9771b263 DN |
2808 | ret.create (known_aggs.length ()); |
2809 | FOR_EACH_VEC_ELT (known_aggs, i, ajf) | |
2810 | ret.quick_push (ajf); | |
2c9561b5 MJ |
2811 | return ret; |
2812 | } | |
2813 | ||
c0cb5055 | 2814 | /* Perform time and size measurement of NODE with the context given in |
44210a96 | 2815 | KNOWN_CSTS, KNOWN_CONTEXTS and KNOWN_AGGS, calculate the benefit and cost |
c0cb5055 MJ |
2816 | given BASE_TIME of the node without specialization, REMOVABLE_PARAMS_COST of |
2817 | all context-independent removable parameters and EST_MOVE_COST of estimated | |
2818 | movement of the considered parameter and store it into VAL. */ | |
2819 | ||
2820 | static void | |
2821 | perform_estimation_of_a_value (cgraph_node *node, vec<tree> known_csts, | |
44210a96 | 2822 | vec<ipa_polymorphic_call_context> known_contexts, |
c0cb5055 | 2823 | vec<ipa_agg_jump_function_p> known_aggs_ptrs, |
26f1a658 | 2824 | int removable_params_cost, |
c0cb5055 MJ |
2825 | int est_move_cost, ipcp_value_base *val) |
2826 | { | |
ab38481c | 2827 | int size, time_benefit; |
26f1a658 | 2828 | sreal time, base_time; |
0bceb671 | 2829 | ipa_hints hints; |
c0cb5055 | 2830 | |
44210a96 | 2831 | estimate_ipcp_clone_size_and_time (node, known_csts, known_contexts, |
c0cb5055 | 2832 | known_aggs_ptrs, &size, &time, |
26f1a658 | 2833 | &base_time, &hints); |
ab38481c JH |
2834 | base_time -= time; |
2835 | if (base_time > 65535) | |
2836 | base_time = 65535; | |
2837 | time_benefit = base_time.to_int () | |
44210a96 | 2838 | + devirtualization_time_bonus (node, known_csts, known_contexts, |
c0cb5055 MJ |
2839 | known_aggs_ptrs) |
2840 | + hint_time_bonus (hints) | |
2841 | + removable_params_cost + est_move_cost; | |
2842 | ||
2843 | gcc_checking_assert (size >=0); | |
2844 | /* The inliner-heuristics based estimates may think that in certain | |
2845 | contexts some functions do not have any size at all but we want | |
2846 | all specializations to have at least a tiny cost, not least not to | |
2847 | divide by zero. */ | |
2848 | if (size == 0) | |
2849 | size = 1; | |
2850 | ||
2851 | val->local_time_benefit = time_benefit; | |
2852 | val->local_size_cost = size; | |
2853 | } | |
2854 | ||
310bc633 MJ |
2855 | /* Iterate over known values of parameters of NODE and estimate the local |
2856 | effects in terms of time and size they have. */ | |
2857 | ||
2858 | static void | |
2859 | estimate_local_effects (struct cgraph_node *node) | |
2860 | { | |
2861 | struct ipa_node_params *info = IPA_NODE_REF (node); | |
2862 | int i, count = ipa_get_param_count (info); | |
44210a96 MJ |
2863 | vec<tree> known_csts; |
2864 | vec<ipa_polymorphic_call_context> known_contexts; | |
84562394 | 2865 | vec<ipa_agg_jump_function> known_aggs; |
9771b263 | 2866 | vec<ipa_agg_jump_function_p> known_aggs_ptrs; |
310bc633 | 2867 | bool always_const; |
310bc633 MJ |
2868 | int removable_params_cost; |
2869 | ||
2870 | if (!count || !ipcp_versionable_function_p (node)) | |
2871 | return; | |
2872 | ||
ca30a539 | 2873 | if (dump_file && (dump_flags & TDF_DETAILS)) |
464d0118 | 2874 | fprintf (dump_file, "\nEstimating effects for %s.\n", node->dump_name ()); |
310bc633 MJ |
2875 | |
2876 | always_const = gather_context_independent_values (info, &known_csts, | |
44210a96 | 2877 | &known_contexts, &known_aggs, |
310bc633 | 2878 | &removable_params_cost); |
2c9561b5 | 2879 | known_aggs_ptrs = agg_jmp_p_vec_for_t_vec (known_aggs); |
5af56ae8 JH |
2880 | int devirt_bonus = devirtualization_time_bonus (node, known_csts, |
2881 | known_contexts, known_aggs_ptrs); | |
dcf89d57 MJ |
2882 | if (always_const || devirt_bonus |
2883 | || (removable_params_cost && node->local.can_change_signature)) | |
ca30a539 | 2884 | { |
310bc633 | 2885 | struct caller_statistics stats; |
0bceb671 | 2886 | ipa_hints hints; |
26f1a658 | 2887 | sreal time, base_time; |
ab38481c | 2888 | int size; |
310bc633 MJ |
2889 | |
2890 | init_caller_stats (&stats); | |
d52f5295 ML |
2891 | node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats, |
2892 | false); | |
44210a96 | 2893 | estimate_ipcp_clone_size_and_time (node, known_csts, known_contexts, |
26f1a658 JH |
2894 | known_aggs_ptrs, &size, &time, |
2895 | &base_time, &hints); | |
5af56ae8 | 2896 | time -= devirt_bonus; |
2c9561b5 | 2897 | time -= hint_time_bonus (hints); |
310bc633 MJ |
2898 | time -= removable_params_cost; |
2899 | size -= stats.n_calls * removable_params_cost; | |
2900 | ||
2901 | if (dump_file) | |
2902 | fprintf (dump_file, " - context independent values, size: %i, " | |
ab38481c | 2903 | "time_benefit: %f\n", size, (base_time - time).to_double ()); |
310bc633 | 2904 | |
5af56ae8 | 2905 | if (size <= 0 || node->local.local) |
310bc633 | 2906 | { |
eb20b778 | 2907 | info->do_clone_for_all_contexts = true; |
310bc633 MJ |
2908 | |
2909 | if (dump_file) | |
2910 | fprintf (dump_file, " Decided to specialize for all " | |
2911 | "known contexts, code not going to grow.\n"); | |
2912 | } | |
26f1a658 JH |
2913 | else if (good_cloning_opportunity_p (node, |
2914 | MAX ((base_time - time).to_int (), | |
2915 | 65536), | |
310bc633 MJ |
2916 | stats.freq_sum, stats.count_sum, |
2917 | size)) | |
2918 | { | |
2919 | if (size + overall_size <= max_new_size) | |
2920 | { | |
eb20b778 | 2921 | info->do_clone_for_all_contexts = true; |
310bc633 MJ |
2922 | overall_size += size; |
2923 | ||
2924 | if (dump_file) | |
2925 | fprintf (dump_file, " Decided to specialize for all " | |
2926 | "known contexts, growth deemed beneficial.\n"); | |
2927 | } | |
2928 | else if (dump_file && (dump_flags & TDF_DETAILS)) | |
2929 | fprintf (dump_file, " Not cloning for all contexts because " | |
2930 | "max_new_size would be reached with %li.\n", | |
2931 | size + overall_size); | |
2932 | } | |
5af56ae8 JH |
2933 | else if (dump_file && (dump_flags & TDF_DETAILS)) |
2934 | fprintf (dump_file, " Not cloning for all contexts because " | |
2935 | "!good_cloning_opportunity_p.\n"); | |
155c9907 | 2936 | |
ca30a539 JH |
2937 | } |
2938 | ||
155c9907 | 2939 | for (i = 0; i < count; i++) |
ca30a539 | 2940 | { |
2c9561b5 | 2941 | struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i); |
c0cb5055 MJ |
2942 | ipcp_lattice<tree> *lat = &plats->itself; |
2943 | ipcp_value<tree> *val; | |
310bc633 MJ |
2944 | |
2945 | if (lat->bottom | |
2946 | || !lat->values | |
44210a96 | 2947 | || known_csts[i]) |
310bc633 MJ |
2948 | continue; |
2949 | ||
2950 | for (val = lat->values; val; val = val->next) | |
2951 | { | |
44210a96 MJ |
2952 | gcc_checking_assert (TREE_CODE (val->value) != TREE_BINFO); |
2953 | known_csts[i] = val->value; | |
310bc633 | 2954 | |
44210a96 MJ |
2955 | int emc = estimate_move_cost (TREE_TYPE (val->value), true); |
2956 | perform_estimation_of_a_value (node, known_csts, known_contexts, | |
26f1a658 | 2957 | known_aggs_ptrs, |
c0cb5055 | 2958 | removable_params_cost, emc, val); |
0318fc77 | 2959 | |
310bc633 MJ |
2960 | if (dump_file && (dump_flags & TDF_DETAILS)) |
2961 | { | |
2962 | fprintf (dump_file, " - estimates for value "); | |
2963 | print_ipcp_constant_value (dump_file, val->value); | |
0e8853ee JH |
2964 | fprintf (dump_file, " for "); |
2965 | ipa_dump_param (dump_file, info, i); | |
310bc633 | 2966 | fprintf (dump_file, ": time_benefit: %i, size: %i\n", |
c0cb5055 | 2967 | val->local_time_benefit, val->local_size_cost); |
310bc633 | 2968 | } |
310bc633 | 2969 | } |
9771b263 | 2970 | known_csts[i] = NULL_TREE; |
2c9561b5 MJ |
2971 | } |
2972 | ||
44210a96 MJ |
2973 | for (i = 0; i < count; i++) |
2974 | { | |
2975 | struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i); | |
2976 | ||
2977 | if (!plats->virt_call) | |
2978 | continue; | |
2979 | ||
2980 | ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat; | |
2981 | ipcp_value<ipa_polymorphic_call_context> *val; | |
2982 | ||
2983 | if (ctxlat->bottom | |
2984 | || !ctxlat->values | |
2985 | || !known_contexts[i].useless_p ()) | |
2986 | continue; | |
2987 | ||
2988 | for (val = ctxlat->values; val; val = val->next) | |
2989 | { | |
2990 | known_contexts[i] = val->value; | |
2991 | perform_estimation_of_a_value (node, known_csts, known_contexts, | |
26f1a658 | 2992 | known_aggs_ptrs, |
44210a96 MJ |
2993 | removable_params_cost, 0, val); |
2994 | ||
2995 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2996 | { | |
2997 | fprintf (dump_file, " - estimates for polymorphic context "); | |
2998 | print_ipcp_constant_value (dump_file, val->value); | |
2999 | fprintf (dump_file, " for "); | |
3000 | ipa_dump_param (dump_file, info, i); | |
3001 | fprintf (dump_file, ": time_benefit: %i, size: %i\n", | |
3002 | val->local_time_benefit, val->local_size_cost); | |
3003 | } | |
3004 | } | |
3005 | known_contexts[i] = ipa_polymorphic_call_context (); | |
3006 | } | |
3007 | ||
155c9907 | 3008 | for (i = 0; i < count; i++) |
2c9561b5 MJ |
3009 | { |
3010 | struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i); | |
3011 | struct ipa_agg_jump_function *ajf; | |
3012 | struct ipcp_agg_lattice *aglat; | |
3013 | ||
3014 | if (plats->aggs_bottom || !plats->aggs) | |
3015 | continue; | |
3016 | ||
9771b263 | 3017 | ajf = &known_aggs[i]; |
2c9561b5 MJ |
3018 | for (aglat = plats->aggs; aglat; aglat = aglat->next) |
3019 | { | |
c0cb5055 | 3020 | ipcp_value<tree> *val; |
2c9561b5 MJ |
3021 | if (aglat->bottom || !aglat->values |
3022 | /* If the following is true, the one value is in known_aggs. */ | |
3023 | || (!plats->aggs_contain_variable | |
c0cb5055 | 3024 | && aglat->is_single_const ())) |
2c9561b5 MJ |
3025 | continue; |
3026 | ||
3027 | for (val = aglat->values; val; val = val->next) | |
3028 | { | |
2c9561b5 | 3029 | struct ipa_agg_jf_item item; |
2c9561b5 MJ |
3030 | |
3031 | item.offset = aglat->offset; | |
3032 | item.value = val->value; | |
9771b263 | 3033 | vec_safe_push (ajf->items, item); |
2c9561b5 | 3034 | |
44210a96 | 3035 | perform_estimation_of_a_value (node, known_csts, known_contexts, |
26f1a658 | 3036 | known_aggs_ptrs, |
c0cb5055 | 3037 | removable_params_cost, 0, val); |
2c9561b5 MJ |
3038 | |
3039 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3040 | { | |
3041 | fprintf (dump_file, " - estimates for value "); | |
3042 | print_ipcp_constant_value (dump_file, val->value); | |
0e8853ee | 3043 | fprintf (dump_file, " for "); |
155c9907 | 3044 | ipa_dump_param (dump_file, info, i); |
2c9561b5 | 3045 | fprintf (dump_file, "[%soffset: " HOST_WIDE_INT_PRINT_DEC |
c0cb5055 MJ |
3046 | "]: time_benefit: %i, size: %i\n", |
3047 | plats->aggs_by_ref ? "ref " : "", | |
3048 | aglat->offset, | |
3049 | val->local_time_benefit, val->local_size_cost); | |
2c9561b5 MJ |
3050 | } |
3051 | ||
9771b263 | 3052 | ajf->items->pop (); |
2c9561b5 MJ |
3053 | } |
3054 | } | |
3055 | } | |
3056 | ||
155c9907 | 3057 | for (i = 0; i < count; i++) |
9771b263 | 3058 | vec_free (known_aggs[i].items); |
310bc633 | 3059 | |
9771b263 | 3060 | known_csts.release (); |
44210a96 | 3061 | known_contexts.release (); |
9771b263 DN |
3062 | known_aggs.release (); |
3063 | known_aggs_ptrs.release (); | |
310bc633 MJ |
3064 | } |
3065 | ||
3066 | ||
3067 | /* Add value CUR_VAL and all yet-unsorted values it is dependent on to the | |
3068 | topological sort of values. */ | |
3069 | ||
c0cb5055 MJ |
3070 | template <typename valtype> |
3071 | void | |
3072 | value_topo_info<valtype>::add_val (ipcp_value<valtype> *cur_val) | |
310bc633 | 3073 | { |
c0cb5055 | 3074 | ipcp_value_source<valtype> *src; |
310bc633 MJ |
3075 | |
3076 | if (cur_val->dfs) | |
3077 | return; | |
3078 | ||
3079 | dfs_counter++; | |
3080 | cur_val->dfs = dfs_counter; | |
3081 | cur_val->low_link = dfs_counter; | |
3082 | ||
3083 | cur_val->topo_next = stack; | |
3084 | stack = cur_val; | |
3085 | cur_val->on_stack = true; | |
3086 | ||
3087 | for (src = cur_val->sources; src; src = src->next) | |
3088 | if (src->val) | |
3089 | { | |
3090 | if (src->val->dfs == 0) | |
3091 | { | |
c0cb5055 | 3092 | add_val (src->val); |
310bc633 MJ |
3093 | if (src->val->low_link < cur_val->low_link) |
3094 | cur_val->low_link = src->val->low_link; | |
3095 | } | |
3096 | else if (src->val->on_stack | |
3097 | && src->val->dfs < cur_val->low_link) | |
3098 | cur_val->low_link = src->val->dfs; | |
3099 | } | |
3100 | ||
3101 | if (cur_val->dfs == cur_val->low_link) | |
ca30a539 | 3102 | { |
c0cb5055 | 3103 | ipcp_value<valtype> *v, *scc_list = NULL; |
310bc633 MJ |
3104 | |
3105 | do | |
3106 | { | |
3107 | v = stack; | |
3108 | stack = v->topo_next; | |
3109 | v->on_stack = false; | |
3110 | ||
3111 | v->scc_next = scc_list; | |
3112 | scc_list = v; | |
3113 | } | |
3114 | while (v != cur_val); | |
3115 | ||
3116 | cur_val->topo_next = values_topo; | |
3117 | values_topo = cur_val; | |
ca30a539 | 3118 | } |
518dc859 RL |
3119 | } |
3120 | ||
310bc633 MJ |
3121 | /* Add all values in lattices associated with NODE to the topological sort if |
3122 | they are not there yet. */ | |
3123 | ||
3124 | static void | |
c0cb5055 | 3125 | add_all_node_vals_to_toposort (cgraph_node *node, ipa_topo_info *topo) |
518dc859 | 3126 | { |
310bc633 MJ |
3127 | struct ipa_node_params *info = IPA_NODE_REF (node); |
3128 | int i, count = ipa_get_param_count (info); | |
3129 | ||
155c9907 | 3130 | for (i = 0; i < count; i++) |
310bc633 | 3131 | { |
2c9561b5 | 3132 | struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i); |
c0cb5055 | 3133 | ipcp_lattice<tree> *lat = &plats->itself; |
2c9561b5 | 3134 | struct ipcp_agg_lattice *aglat; |
310bc633 | 3135 | |
2c9561b5 | 3136 | if (!lat->bottom) |
44210a96 MJ |
3137 | { |
3138 | ipcp_value<tree> *val; | |
3139 | for (val = lat->values; val; val = val->next) | |
3140 | topo->constants.add_val (val); | |
3141 | } | |
2c9561b5 MJ |
3142 | |
3143 | if (!plats->aggs_bottom) | |
3144 | for (aglat = plats->aggs; aglat; aglat = aglat->next) | |
3145 | if (!aglat->bottom) | |
44210a96 MJ |
3146 | { |
3147 | ipcp_value<tree> *val; | |
3148 | for (val = aglat->values; val; val = val->next) | |
3149 | topo->constants.add_val (val); | |
3150 | } | |
3151 | ||
3152 | ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat; | |
3153 | if (!ctxlat->bottom) | |
3154 | { | |
3155 | ipcp_value<ipa_polymorphic_call_context> *ctxval; | |
3156 | for (ctxval = ctxlat->values; ctxval; ctxval = ctxval->next) | |
3157 | topo->contexts.add_val (ctxval); | |
3158 | } | |
310bc633 | 3159 | } |
518dc859 RL |
3160 | } |
3161 | ||
310bc633 MJ |
3162 | /* One pass of constants propagation along the call graph edges, from callers |
3163 | to callees (requires topological ordering in TOPO), iterate over strongly | |
3164 | connected components. */ | |
3165 | ||
518dc859 | 3166 | static void |
11478306 | 3167 | propagate_constants_topo (struct ipa_topo_info *topo) |
518dc859 | 3168 | { |
310bc633 | 3169 | int i; |
518dc859 | 3170 | |
310bc633 | 3171 | for (i = topo->nnodes - 1; i >= 0; i--) |
518dc859 | 3172 | { |
39e87baf | 3173 | unsigned j; |
310bc633 | 3174 | struct cgraph_node *v, *node = topo->order[i]; |
d52f5295 | 3175 | vec<cgraph_node *> cycle_nodes = ipa_get_nodes_in_cycle (node); |
310bc633 | 3176 | |
310bc633 MJ |
3177 | /* First, iteratively propagate within the strongly connected component |
3178 | until all lattices stabilize. */ | |
39e87baf | 3179 | FOR_EACH_VEC_ELT (cycle_nodes, j, v) |
d52f5295 | 3180 | if (v->has_gimple_body_p ()) |
310bc633 | 3181 | push_node_to_stack (topo, v); |
310bc633 | 3182 | |
39e87baf | 3183 | v = pop_node_from_stack (topo); |
310bc633 MJ |
3184 | while (v) |
3185 | { | |
3186 | struct cgraph_edge *cs; | |
3187 | ||
3188 | for (cs = v->callees; cs; cs = cs->next_callee) | |
af21714c MJ |
3189 | if (ipa_edge_within_scc (cs)) |
3190 | { | |
3191 | IPA_NODE_REF (v)->node_within_scc = true; | |
155c9907 | 3192 | if (propagate_constants_across_call (cs)) |
af21714c MJ |
3193 | push_node_to_stack (topo, cs->callee->function_symbol ()); |
3194 | } | |
310bc633 MJ |
3195 | v = pop_node_from_stack (topo); |
3196 | } | |
3197 | ||
3198 | /* Afterwards, propagate along edges leading out of the SCC, calculates | |
3199 | the local effects of the discovered constants and all valid values to | |
3200 | their topological sort. */ | |
39e87baf | 3201 | FOR_EACH_VEC_ELT (cycle_nodes, j, v) |
d52f5295 | 3202 | if (v->has_gimple_body_p ()) |
39e87baf MJ |
3203 | { |
3204 | struct cgraph_edge *cs; | |
310bc633 | 3205 | |
39e87baf | 3206 | estimate_local_effects (v); |
c0cb5055 | 3207 | add_all_node_vals_to_toposort (v, topo); |
39e87baf | 3208 | for (cs = v->callees; cs; cs = cs->next_callee) |
4cb13597 | 3209 | if (!ipa_edge_within_scc (cs)) |
155c9907 | 3210 | propagate_constants_across_call (cs); |
39e87baf MJ |
3211 | } |
3212 | cycle_nodes.release (); | |
518dc859 RL |
3213 | } |
3214 | } | |
3215 | ||
df0227c4 MJ |
3216 | |
3217 | /* Return the sum of A and B if none of them is bigger than INT_MAX/2, return | |
3218 | the bigger one if otherwise. */ | |
3219 | ||
3220 | static int | |
3221 | safe_add (int a, int b) | |
3222 | { | |
3223 | if (a > INT_MAX/2 || b > INT_MAX/2) | |
3224 | return a > b ? a : b; | |
3225 | else | |
3226 | return a + b; | |
3227 | } | |
3228 | ||
3229 | ||
310bc633 | 3230 | /* Propagate the estimated effects of individual values along the topological |
073a8998 | 3231 | from the dependent values to those they depend on. */ |
310bc633 | 3232 | |
c0cb5055 MJ |
3233 | template <typename valtype> |
3234 | void | |
3235 | value_topo_info<valtype>::propagate_effects () | |
518dc859 | 3236 | { |
c0cb5055 | 3237 | ipcp_value<valtype> *base; |
518dc859 | 3238 | |
310bc633 | 3239 | for (base = values_topo; base; base = base->topo_next) |
518dc859 | 3240 | { |
c0cb5055 MJ |
3241 | ipcp_value_source<valtype> *src; |
3242 | ipcp_value<valtype> *val; | |
310bc633 MJ |
3243 | int time = 0, size = 0; |
3244 | ||
3245 | for (val = base; val; val = val->scc_next) | |
3246 | { | |
df0227c4 MJ |
3247 | time = safe_add (time, |
3248 | val->local_time_benefit + val->prop_time_benefit); | |
3249 | size = safe_add (size, val->local_size_cost + val->prop_size_cost); | |
310bc633 MJ |
3250 | } |
3251 | ||
3252 | for (val = base; val; val = val->scc_next) | |
3253 | for (src = val->sources; src; src = src->next) | |
3254 | if (src->val | |
3dafb85c | 3255 | && src->cs->maybe_hot_p ()) |
310bc633 | 3256 | { |
df0227c4 MJ |
3257 | src->val->prop_time_benefit = safe_add (time, |
3258 | src->val->prop_time_benefit); | |
3259 | src->val->prop_size_cost = safe_add (size, | |
3260 | src->val->prop_size_cost); | |
310bc633 | 3261 | } |
518dc859 RL |
3262 | } |
3263 | } | |
3264 | ||
310bc633 | 3265 | |
44210a96 MJ |
3266 | /* Propagate constants, polymorphic contexts and their effects from the |
3267 | summaries interprocedurally. */ | |
310bc633 | 3268 | |
518dc859 | 3269 | static void |
11478306 | 3270 | ipcp_propagate_stage (struct ipa_topo_info *topo) |
518dc859 RL |
3271 | { |
3272 | struct cgraph_node *node; | |
518dc859 | 3273 | |
310bc633 MJ |
3274 | if (dump_file) |
3275 | fprintf (dump_file, "\n Propagating constants:\n\n"); | |
3276 | ||
e7a74006 JH |
3277 | max_count = profile_count::uninitialized (); |
3278 | ||
310bc633 MJ |
3279 | FOR_EACH_DEFINED_FUNCTION (node) |
3280 | { | |
3281 | struct ipa_node_params *info = IPA_NODE_REF (node); | |
3282 | ||
7e729474 | 3283 | determine_versionability (node, info); |
d52f5295 | 3284 | if (node->has_gimple_body_p ()) |
310bc633 | 3285 | { |
2c9561b5 | 3286 | info->lattices = XCNEWVEC (struct ipcp_param_lattices, |
310bc633 MJ |
3287 | ipa_get_param_count (info)); |
3288 | initialize_node_lattices (node); | |
3289 | } | |
67348ccc | 3290 | if (node->definition && !node->alias) |
0bceb671 | 3291 | overall_size += ipa_fn_summaries->get (node)->self_size; |
1bad9c18 | 3292 | max_count = max_count.max (node->count.ipa ()); |
310bc633 MJ |
3293 | } |
3294 | ||
3295 | max_new_size = overall_size; | |
3296 | if (max_new_size < PARAM_VALUE (PARAM_LARGE_UNIT_INSNS)) | |
3297 | max_new_size = PARAM_VALUE (PARAM_LARGE_UNIT_INSNS); | |
3298 | max_new_size += max_new_size * PARAM_VALUE (PARAM_IPCP_UNIT_GROWTH) / 100 + 1; | |
3299 | ||
3300 | if (dump_file) | |
3301 | fprintf (dump_file, "\noverall_size: %li, max_new_size: %li\n", | |
3302 | overall_size, max_new_size); | |
3303 | ||
3304 | propagate_constants_topo (topo); | |
b2b29377 MM |
3305 | if (flag_checking) |
3306 | ipcp_verify_propagated_values (); | |
c0cb5055 | 3307 | topo->constants.propagate_effects (); |
44210a96 | 3308 | topo->contexts.propagate_effects (); |
310bc633 MJ |
3309 | |
3310 | if (dump_file) | |
3311 | { | |
3312 | fprintf (dump_file, "\nIPA lattices after all propagation:\n"); | |
3313 | print_all_lattices (dump_file, (dump_flags & TDF_DETAILS), true); | |
3314 | } | |
3315 | } | |
3316 | ||
3317 | /* Discover newly direct outgoing edges from NODE which is a new clone with | |
44210a96 | 3318 | known KNOWN_CSTS and make them direct. */ |
310bc633 MJ |
3319 | |
3320 | static void | |
3321 | ipcp_discover_new_direct_edges (struct cgraph_node *node, | |
44210a96 MJ |
3322 | vec<tree> known_csts, |
3323 | vec<ipa_polymorphic_call_context> | |
3324 | known_contexts, | |
162712de | 3325 | struct ipa_agg_replacement_value *aggvals) |
310bc633 MJ |
3326 | { |
3327 | struct cgraph_edge *ie, *next_ie; | |
0f378cb5 | 3328 | bool found = false; |
310bc633 MJ |
3329 | |
3330 | for (ie = node->indirect_calls; ie; ie = next_ie) | |
3331 | { | |
81fa35bd | 3332 | tree target; |
231b4916 | 3333 | bool speculative; |
310bc633 MJ |
3334 | |
3335 | next_ie = ie->next_callee; | |
44210a96 | 3336 | target = ipa_get_indirect_edge_target_1 (ie, known_csts, known_contexts, |
231b4916 | 3337 | vNULL, aggvals, &speculative); |
310bc633 | 3338 | if (target) |
0f378cb5 | 3339 | { |
042ae7d2 JH |
3340 | bool agg_contents = ie->indirect_info->agg_contents; |
3341 | bool polymorphic = ie->indirect_info->polymorphic; | |
a4e33812 | 3342 | int param_index = ie->indirect_info->param_index; |
231b4916 JH |
3343 | struct cgraph_edge *cs = ipa_make_edge_direct_to_target (ie, target, |
3344 | speculative); | |
0f378cb5 | 3345 | found = true; |
4502fe8d | 3346 | |
042ae7d2 | 3347 | if (cs && !agg_contents && !polymorphic) |
4502fe8d MJ |
3348 | { |
3349 | struct ipa_node_params *info = IPA_NODE_REF (node); | |
4502fe8d MJ |
3350 | int c = ipa_get_controlled_uses (info, param_index); |
3351 | if (c != IPA_UNDESCRIBED_USE) | |
3352 | { | |
3353 | struct ipa_ref *to_del; | |
3354 | ||
3355 | c--; | |
3356 | ipa_set_controlled_uses (info, param_index, c); | |
3357 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3358 | fprintf (dump_file, " controlled uses count of param " | |
3359 | "%i bumped down to %i\n", param_index, c); | |
3360 | if (c == 0 | |
d122681a | 3361 | && (to_del = node->find_reference (cs->callee, NULL, 0))) |
4502fe8d MJ |
3362 | { |
3363 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3364 | fprintf (dump_file, " and even removing its " | |
3365 | "cloning-created reference\n"); | |
d122681a | 3366 | to_del->remove_reference (); |
4502fe8d MJ |
3367 | } |
3368 | } | |
3369 | } | |
0f378cb5 | 3370 | } |
310bc633 | 3371 | } |
0f378cb5 JH |
3372 | /* Turning calls to direct calls will improve overall summary. */ |
3373 | if (found) | |
0bceb671 | 3374 | ipa_update_overall_fn_summary (node); |
310bc633 MJ |
3375 | } |
3376 | ||
3377 | /* Vector of pointers which for linked lists of clones of an original crgaph | |
3378 | edge. */ | |
3379 | ||
d52f5295 ML |
3380 | static vec<cgraph_edge *> next_edge_clone; |
3381 | static vec<cgraph_edge *> prev_edge_clone; | |
310bc633 MJ |
3382 | |
3383 | static inline void | |
aef83682 | 3384 | grow_edge_clone_vectors (void) |
310bc633 | 3385 | { |
9771b263 | 3386 | if (next_edge_clone.length () |
3dafb85c ML |
3387 | <= (unsigned) symtab->edges_max_uid) |
3388 | next_edge_clone.safe_grow_cleared (symtab->edges_max_uid + 1); | |
aef83682 | 3389 | if (prev_edge_clone.length () |
3dafb85c ML |
3390 | <= (unsigned) symtab->edges_max_uid) |
3391 | prev_edge_clone.safe_grow_cleared (symtab->edges_max_uid + 1); | |
310bc633 MJ |
3392 | } |
3393 | ||
3394 | /* Edge duplication hook to grow the appropriate linked list in | |
3395 | next_edge_clone. */ | |
3396 | ||
3397 | static void | |
3398 | ipcp_edge_duplication_hook (struct cgraph_edge *src, struct cgraph_edge *dst, | |
aef83682 | 3399 | void *) |
310bc633 | 3400 | { |
aef83682 MJ |
3401 | grow_edge_clone_vectors (); |
3402 | ||
3403 | struct cgraph_edge *old_next = next_edge_clone[src->uid]; | |
3404 | if (old_next) | |
3405 | prev_edge_clone[old_next->uid] = dst; | |
3406 | prev_edge_clone[dst->uid] = src; | |
3407 | ||
3408 | next_edge_clone[dst->uid] = old_next; | |
9771b263 | 3409 | next_edge_clone[src->uid] = dst; |
310bc633 MJ |
3410 | } |
3411 | ||
aef83682 MJ |
3412 | /* Hook that is called by cgraph.c when an edge is removed. */ |
3413 | ||
3414 | static void | |
3415 | ipcp_edge_removal_hook (struct cgraph_edge *cs, void *) | |
3416 | { | |
3417 | grow_edge_clone_vectors (); | |
3418 | ||
3419 | struct cgraph_edge *prev = prev_edge_clone[cs->uid]; | |
3420 | struct cgraph_edge *next = next_edge_clone[cs->uid]; | |
3421 | if (prev) | |
3422 | next_edge_clone[prev->uid] = next; | |
3423 | if (next) | |
3424 | prev_edge_clone[next->uid] = prev; | |
3425 | } | |
3426 | ||
2c9561b5 MJ |
3427 | /* See if NODE is a clone with a known aggregate value at a given OFFSET of a |
3428 | parameter with the given INDEX. */ | |
310bc633 | 3429 | |
2c9561b5 | 3430 | static tree |
a9243bfc | 3431 | get_clone_agg_value (struct cgraph_node *node, HOST_WIDE_INT offset, |
2c9561b5 | 3432 | int index) |
310bc633 | 3433 | { |
2c9561b5 MJ |
3434 | struct ipa_agg_replacement_value *aggval; |
3435 | ||
3436 | aggval = ipa_get_agg_replacements_for_node (node); | |
3437 | while (aggval) | |
3438 | { | |
3439 | if (aggval->offset == offset | |
3440 | && aggval->index == index) | |
3441 | return aggval->value; | |
3442 | aggval = aggval->next; | |
3443 | } | |
3444 | return NULL_TREE; | |
310bc633 MJ |
3445 | } |
3446 | ||
47f4756e | 3447 | /* Return true is NODE is DEST or its clone for all contexts. */ |
310bc633 MJ |
3448 | |
3449 | static bool | |
47f4756e MJ |
3450 | same_node_or_its_all_contexts_clone_p (cgraph_node *node, cgraph_node *dest) |
3451 | { | |
3452 | if (node == dest) | |
3453 | return true; | |
3454 | ||
3455 | struct ipa_node_params *info = IPA_NODE_REF (node); | |
3456 | return info->is_all_contexts_clone && info->ipcp_orig_node == dest; | |
3457 | } | |
3458 | ||
7b668576 MJ |
3459 | /* Return true if edge CS does bring about the value described by SRC to |
3460 | DEST_VAL of node DEST or its clone for all contexts. */ | |
47f4756e MJ |
3461 | |
3462 | static bool | |
3463 | cgraph_edge_brings_value_p (cgraph_edge *cs, ipcp_value_source<tree> *src, | |
7b668576 | 3464 | cgraph_node *dest, ipcp_value<tree> *dest_val) |
310bc633 MJ |
3465 | { |
3466 | struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller); | |
47f4756e MJ |
3467 | enum availability availability; |
3468 | cgraph_node *real_dest = cs->callee->function_symbol (&availability); | |
310bc633 | 3469 | |
47f4756e MJ |
3470 | if (!same_node_or_its_all_contexts_clone_p (real_dest, dest) |
3471 | || availability <= AVAIL_INTERPOSABLE | |
310bc633 MJ |
3472 | || caller_info->node_dead) |
3473 | return false; | |
2f1f3ac4 MJ |
3474 | |
3475 | if (!src->val) | |
310bc633 MJ |
3476 | return true; |
3477 | ||
3478 | if (caller_info->ipcp_orig_node) | |
3479 | { | |
2c9561b5 MJ |
3480 | tree t; |
3481 | if (src->offset == -1) | |
44210a96 | 3482 | t = caller_info->known_csts[src->index]; |
2c9561b5 MJ |
3483 | else |
3484 | t = get_clone_agg_value (cs->caller, src->offset, src->index); | |
310bc633 MJ |
3485 | return (t != NULL_TREE |
3486 | && values_equal_for_ipcp_p (src->val->value, t)); | |
3487 | } | |
3488 | else | |
518dc859 | 3489 | { |
2f1f3ac4 MJ |
3490 | /* At the moment we do not propagate over arithmetic jump functions in |
3491 | SCCs, so it is safe to detect self-feeding recursive calls in this | |
3492 | way. */ | |
3493 | if (src->val == dest_val) | |
3494 | return true; | |
3495 | ||
2c9561b5 MJ |
3496 | struct ipcp_agg_lattice *aglat; |
3497 | struct ipcp_param_lattices *plats = ipa_get_parm_lattices (caller_info, | |
3498 | src->index); | |
3499 | if (src->offset == -1) | |
c0cb5055 | 3500 | return (plats->itself.is_single_const () |
2c9561b5 MJ |
3501 | && values_equal_for_ipcp_p (src->val->value, |
3502 | plats->itself.values->value)); | |
310bc633 | 3503 | else |
2c9561b5 MJ |
3504 | { |
3505 | if (plats->aggs_bottom || plats->aggs_contain_variable) | |
3506 | return false; | |
3507 | for (aglat = plats->aggs; aglat; aglat = aglat->next) | |
3508 | if (aglat->offset == src->offset) | |
c0cb5055 | 3509 | return (aglat->is_single_const () |
2c9561b5 MJ |
3510 | && values_equal_for_ipcp_p (src->val->value, |
3511 | aglat->values->value)); | |
3512 | } | |
3513 | return false; | |
310bc633 MJ |
3514 | } |
3515 | } | |
3516 | ||
7b668576 MJ |
3517 | /* Return true if edge CS does bring about the value described by SRC to |
3518 | DST_VAL of node DEST or its clone for all contexts. */ | |
44210a96 MJ |
3519 | |
3520 | static bool | |
47f4756e MJ |
3521 | cgraph_edge_brings_value_p (cgraph_edge *cs, |
3522 | ipcp_value_source<ipa_polymorphic_call_context> *src, | |
7b668576 MJ |
3523 | cgraph_node *dest, |
3524 | ipcp_value<ipa_polymorphic_call_context> *) | |
44210a96 MJ |
3525 | { |
3526 | struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller); | |
3527 | cgraph_node *real_dest = cs->callee->function_symbol (); | |
44210a96 | 3528 | |
47f4756e | 3529 | if (!same_node_or_its_all_contexts_clone_p (real_dest, dest) |
44210a96 MJ |
3530 | || caller_info->node_dead) |
3531 | return false; | |
3532 | if (!src->val) | |
3533 | return true; | |
3534 | ||
3535 | if (caller_info->ipcp_orig_node) | |
3536 | return (caller_info->known_contexts.length () > (unsigned) src->index) | |
3537 | && values_equal_for_ipcp_p (src->val->value, | |
3538 | caller_info->known_contexts[src->index]); | |
3539 | ||
3540 | struct ipcp_param_lattices *plats = ipa_get_parm_lattices (caller_info, | |
3541 | src->index); | |
3542 | return plats->ctxlat.is_single_const () | |
3543 | && values_equal_for_ipcp_p (src->val->value, | |
3544 | plats->ctxlat.values->value); | |
3545 | } | |
3546 | ||
2c9561b5 MJ |
3547 | /* Get the next clone in the linked list of clones of an edge. */ |
3548 | ||
3549 | static inline struct cgraph_edge * | |
3550 | get_next_cgraph_edge_clone (struct cgraph_edge *cs) | |
3551 | { | |
9771b263 | 3552 | return next_edge_clone[cs->uid]; |
2c9561b5 MJ |
3553 | } |
3554 | ||
7b668576 MJ |
3555 | /* Given VAL that is intended for DEST, iterate over all its sources and if any |
3556 | of them is viable and hot, return true. In that case, for those that still | |
3557 | hold, add their edge frequency and their number into *FREQUENCY and | |
3558 | *CALLER_COUNT respectively. */ | |
310bc633 | 3559 | |
c0cb5055 | 3560 | template <typename valtype> |
310bc633 | 3561 | static bool |
47f4756e MJ |
3562 | get_info_about_necessary_edges (ipcp_value<valtype> *val, cgraph_node *dest, |
3563 | int *freq_sum, | |
3995f3a2 | 3564 | profile_count *count_sum, int *caller_count) |
310bc633 | 3565 | { |
c0cb5055 | 3566 | ipcp_value_source<valtype> *src; |
310bc633 | 3567 | int freq = 0, count = 0; |
3995f3a2 | 3568 | profile_count cnt = profile_count::zero (); |
310bc633 | 3569 | bool hot = false; |
7b668576 | 3570 | bool non_self_recursive = false; |
310bc633 MJ |
3571 | |
3572 | for (src = val->sources; src; src = src->next) | |
3573 | { | |
3574 | struct cgraph_edge *cs = src->cs; | |
3575 | while (cs) | |
518dc859 | 3576 | { |
7b668576 | 3577 | if (cgraph_edge_brings_value_p (cs, src, dest, val)) |
310bc633 MJ |
3578 | { |
3579 | count++; | |
1bad9c18 JH |
3580 | freq += cs->frequency (); |
3581 | if (cs->count.ipa ().initialized_p ()) | |
3582 | cnt += cs->count.ipa (); | |
3dafb85c | 3583 | hot |= cs->maybe_hot_p (); |
7b668576 MJ |
3584 | if (cs->caller != dest) |
3585 | non_self_recursive = true; | |
310bc633 MJ |
3586 | } |
3587 | cs = get_next_cgraph_edge_clone (cs); | |
518dc859 RL |
3588 | } |
3589 | } | |
310bc633 | 3590 | |
7b668576 MJ |
3591 | /* If the only edges bringing a value are self-recursive ones, do not bother |
3592 | evaluating it. */ | |
3593 | if (!non_self_recursive) | |
3594 | return false; | |
3595 | ||
310bc633 MJ |
3596 | *freq_sum = freq; |
3597 | *count_sum = cnt; | |
3598 | *caller_count = count; | |
3599 | return hot; | |
518dc859 RL |
3600 | } |
3601 | ||
47f4756e MJ |
3602 | /* Return a vector of incoming edges that do bring value VAL to node DEST. It |
3603 | is assumed their number is known and equal to CALLER_COUNT. */ | |
310bc633 | 3604 | |
c0cb5055 | 3605 | template <typename valtype> |
d52f5295 | 3606 | static vec<cgraph_edge *> |
47f4756e MJ |
3607 | gather_edges_for_value (ipcp_value<valtype> *val, cgraph_node *dest, |
3608 | int caller_count) | |
518dc859 | 3609 | { |
c0cb5055 | 3610 | ipcp_value_source<valtype> *src; |
d52f5295 | 3611 | vec<cgraph_edge *> ret; |
310bc633 | 3612 | |
9771b263 | 3613 | ret.create (caller_count); |
310bc633 MJ |
3614 | for (src = val->sources; src; src = src->next) |
3615 | { | |
3616 | struct cgraph_edge *cs = src->cs; | |
3617 | while (cs) | |
3618 | { | |
7b668576 | 3619 | if (cgraph_edge_brings_value_p (cs, src, dest, val)) |
9771b263 | 3620 | ret.quick_push (cs); |
310bc633 MJ |
3621 | cs = get_next_cgraph_edge_clone (cs); |
3622 | } | |
3623 | } | |
3624 | ||
3625 | return ret; | |
518dc859 RL |
3626 | } |
3627 | ||
310bc633 MJ |
3628 | /* Construct a replacement map for a know VALUE for a formal parameter PARAM. |
3629 | Return it or NULL if for some reason it cannot be created. */ | |
3630 | ||
518dc859 | 3631 | static struct ipa_replace_map * |
0e8853ee | 3632 | get_replacement_map (struct ipa_node_params *info, tree value, int parm_num) |
518dc859 RL |
3633 | { |
3634 | struct ipa_replace_map *replace_map; | |
518dc859 | 3635 | |
310bc633 | 3636 | |
766090c2 | 3637 | replace_map = ggc_alloc<ipa_replace_map> (); |
c6f7cfc1 JH |
3638 | if (dump_file) |
3639 | { | |
0e8853ee JH |
3640 | fprintf (dump_file, " replacing "); |
3641 | ipa_dump_param (dump_file, info, parm_num); | |
155c9907 | 3642 | |
c6f7cfc1 | 3643 | fprintf (dump_file, " with const "); |
ef6cb4c7 | 3644 | print_generic_expr (dump_file, value); |
c6f7cfc1 JH |
3645 | fprintf (dump_file, "\n"); |
3646 | } | |
49bde175 JH |
3647 | replace_map->old_tree = NULL; |
3648 | replace_map->parm_num = parm_num; | |
310bc633 | 3649 | replace_map->new_tree = value; |
0f1961a2 JH |
3650 | replace_map->replace_p = true; |
3651 | replace_map->ref_p = false; | |
518dc859 RL |
3652 | |
3653 | return replace_map; | |
3654 | } | |
3655 | ||
310bc633 | 3656 | /* Dump new profiling counts */ |
518dc859 | 3657 | |
518dc859 | 3658 | static void |
310bc633 MJ |
3659 | dump_profile_updates (struct cgraph_node *orig_node, |
3660 | struct cgraph_node *new_node) | |
518dc859 | 3661 | { |
310bc633 | 3662 | struct cgraph_edge *cs; |
518dc859 | 3663 | |
3995f3a2 JH |
3664 | fprintf (dump_file, " setting count of the specialized node to "); |
3665 | new_node->count.dump (dump_file); | |
3666 | fprintf (dump_file, "\n"); | |
155c9907 | 3667 | for (cs = new_node->callees; cs; cs = cs->next_callee) |
3995f3a2 JH |
3668 | { |
3669 | fprintf (dump_file, " edge to %s has count ", | |
3670 | cs->callee->name ()); | |
3671 | cs->count.dump (dump_file); | |
3672 | fprintf (dump_file, "\n"); | |
3673 | } | |
310bc633 | 3674 | |
3995f3a2 JH |
3675 | fprintf (dump_file, " setting count of the original node to "); |
3676 | orig_node->count.dump (dump_file); | |
3677 | fprintf (dump_file, "\n"); | |
155c9907 | 3678 | for (cs = orig_node->callees; cs; cs = cs->next_callee) |
3995f3a2 JH |
3679 | { |
3680 | fprintf (dump_file, " edge to %s is left with ", | |
3681 | cs->callee->name ()); | |
3682 | cs->count.dump (dump_file); | |
3683 | fprintf (dump_file, "\n"); | |
3684 | } | |
310bc633 | 3685 | } |
c6f7cfc1 | 3686 | |
310bc633 MJ |
3687 | /* After a specialized NEW_NODE version of ORIG_NODE has been created, update |
3688 | their profile information to reflect this. */ | |
518dc859 | 3689 | |
518dc859 | 3690 | static void |
310bc633 MJ |
3691 | update_profiling_info (struct cgraph_node *orig_node, |
3692 | struct cgraph_node *new_node) | |
518dc859 | 3693 | { |
518dc859 | 3694 | struct cgraph_edge *cs; |
310bc633 | 3695 | struct caller_statistics stats; |
3995f3a2 JH |
3696 | profile_count new_sum, orig_sum; |
3697 | profile_count remainder, orig_node_count = orig_node->count; | |
310bc633 | 3698 | |
1bad9c18 | 3699 | if (!(orig_node_count.ipa () > profile_count::zero ())) |
310bc633 | 3700 | return; |
518dc859 | 3701 | |
310bc633 | 3702 | init_caller_stats (&stats); |
d52f5295 ML |
3703 | orig_node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats, |
3704 | false); | |
310bc633 MJ |
3705 | orig_sum = stats.count_sum; |
3706 | init_caller_stats (&stats); | |
d52f5295 ML |
3707 | new_node->call_for_symbol_thunks_and_aliases (gather_caller_stats, &stats, |
3708 | false); | |
310bc633 MJ |
3709 | new_sum = stats.count_sum; |
3710 | ||
3711 | if (orig_node_count < orig_sum + new_sum) | |
518dc859 | 3712 | { |
310bc633 | 3713 | if (dump_file) |
3995f3a2 JH |
3714 | { |
3715 | fprintf (dump_file, " Problem: node %s has too low count ", | |
3716 | orig_node->dump_name ()); | |
3717 | orig_node_count.dump (dump_file); | |
3718 | fprintf (dump_file, "while the sum of incoming count is "); | |
3719 | (orig_sum + new_sum).dump (dump_file); | |
3720 | fprintf (dump_file, "\n"); | |
3721 | } | |
3722 | ||
3723 | orig_node_count = (orig_sum + new_sum).apply_scale (12, 10); | |
310bc633 | 3724 | if (dump_file) |
3995f3a2 JH |
3725 | { |
3726 | fprintf (dump_file, " proceeding by pretending it was "); | |
3727 | orig_node_count.dump (dump_file); | |
3728 | fprintf (dump_file, "\n"); | |
3729 | } | |
518dc859 | 3730 | } |
310bc633 | 3731 | |
517048ce JH |
3732 | remainder = orig_node_count.combine_with_ipa_count (orig_node_count.ipa () |
3733 | - new_sum.ipa ()); | |
3734 | new_sum = orig_node_count.combine_with_ipa_count (new_sum); | |
310bc633 MJ |
3735 | orig_node->count = remainder; |
3736 | ||
155c9907 | 3737 | for (cs = new_node->callees; cs; cs = cs->next_callee) |
35cd23eb | 3738 | cs->count = cs->count.apply_scale (new_sum, orig_node_count); |
310bc633 | 3739 | |
155c9907 | 3740 | for (cs = orig_node->callees; cs; cs = cs->next_callee) |
3995f3a2 | 3741 | cs->count = cs->count.apply_scale (remainder, orig_node_count); |
310bc633 MJ |
3742 | |
3743 | if (dump_file) | |
3744 | dump_profile_updates (orig_node, new_node); | |
518dc859 RL |
3745 | } |
3746 | ||
310bc633 MJ |
3747 | /* Update the respective profile of specialized NEW_NODE and the original |
3748 | ORIG_NODE after additional edges with cumulative count sum REDIRECTED_SUM | |
3749 | have been redirected to the specialized version. */ | |
3750 | ||
3751 | static void | |
3752 | update_specialized_profile (struct cgraph_node *new_node, | |
3753 | struct cgraph_node *orig_node, | |
3995f3a2 | 3754 | profile_count redirected_sum) |
5e45130d | 3755 | { |
a065d52e | 3756 | struct cgraph_edge *cs; |
3995f3a2 | 3757 | profile_count new_node_count, orig_node_count = orig_node->count; |
5e45130d | 3758 | |
310bc633 | 3759 | if (dump_file) |
3995f3a2 JH |
3760 | { |
3761 | fprintf (dump_file, " the sum of counts of redirected edges is "); | |
3762 | redirected_sum.dump (dump_file); | |
3763 | fprintf (dump_file, "\n"); | |
3764 | } | |
3765 | if (!(orig_node_count > profile_count::zero ())) | |
310bc633 | 3766 | return; |
a065d52e | 3767 | |
310bc633 | 3768 | gcc_assert (orig_node_count >= redirected_sum); |
5e45130d | 3769 | |
310bc633 MJ |
3770 | new_node_count = new_node->count; |
3771 | new_node->count += redirected_sum; | |
3772 | orig_node->count -= redirected_sum; | |
a065d52e | 3773 | |
155c9907 | 3774 | for (cs = new_node->callees; cs; cs = cs->next_callee) |
e3951b03 | 3775 | cs->count += cs->count.apply_scale (redirected_sum, new_node_count); |
a065d52e | 3776 | |
155c9907 | 3777 | for (cs = orig_node->callees; cs; cs = cs->next_callee) |
310bc633 | 3778 | { |
3995f3a2 JH |
3779 | profile_count dec = cs->count.apply_scale (redirected_sum, |
3780 | orig_node_count); | |
3781 | cs->count -= dec; | |
310bc633 | 3782 | } |
a065d52e | 3783 | |
310bc633 MJ |
3784 | if (dump_file) |
3785 | dump_profile_updates (orig_node, new_node); | |
5e45130d JH |
3786 | } |
3787 | ||
44210a96 MJ |
3788 | /* Create a specialized version of NODE with known constants in KNOWN_CSTS, |
3789 | known contexts in KNOWN_CONTEXTS and known aggregate values in AGGVALS and | |
3790 | redirect all edges in CALLERS to it. */ | |
a065d52e | 3791 | |
310bc633 MJ |
3792 | static struct cgraph_node * |
3793 | create_specialized_node (struct cgraph_node *node, | |
44210a96 MJ |
3794 | vec<tree> known_csts, |
3795 | vec<ipa_polymorphic_call_context> known_contexts, | |
2c9561b5 | 3796 | struct ipa_agg_replacement_value *aggvals, |
d52f5295 | 3797 | vec<cgraph_edge *> callers) |
5e45130d | 3798 | { |
310bc633 | 3799 | struct ipa_node_params *new_info, *info = IPA_NODE_REF (node); |
d52f5295 | 3800 | vec<ipa_replace_map *, va_gc> *replace_trees = NULL; |
79ee9826 | 3801 | struct ipa_agg_replacement_value *av; |
310bc633 MJ |
3802 | struct cgraph_node *new_node; |
3803 | int i, count = ipa_get_param_count (info); | |
3804 | bitmap args_to_skip; | |
5e45130d | 3805 | |
310bc633 MJ |
3806 | gcc_assert (!info->ipcp_orig_node); |
3807 | ||
3808 | if (node->local.can_change_signature) | |
5e45130d | 3809 | { |
310bc633 MJ |
3810 | args_to_skip = BITMAP_GGC_ALLOC (); |
3811 | for (i = 0; i < count; i++) | |
3812 | { | |
44210a96 | 3813 | tree t = known_csts[i]; |
310bc633 | 3814 | |
44210a96 | 3815 | if (t || !ipa_is_param_used (info, i)) |
310bc633 MJ |
3816 | bitmap_set_bit (args_to_skip, i); |
3817 | } | |
3818 | } | |
3819 | else | |
d7da5cc8 MJ |
3820 | { |
3821 | args_to_skip = NULL; | |
3822 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3823 | fprintf (dump_file, " cannot change function signature\n"); | |
3824 | } | |
310bc633 | 3825 | |
155c9907 | 3826 | for (i = 0; i < count; i++) |
310bc633 | 3827 | { |
44210a96 MJ |
3828 | tree t = known_csts[i]; |
3829 | if (t) | |
310bc633 MJ |
3830 | { |
3831 | struct ipa_replace_map *replace_map; | |
3832 | ||
44210a96 | 3833 | gcc_checking_assert (TREE_CODE (t) != TREE_BINFO); |
0e8853ee | 3834 | replace_map = get_replacement_map (info, t, i); |
310bc633 | 3835 | if (replace_map) |
9771b263 | 3836 | vec_safe_push (replace_trees, replace_map); |
310bc633 | 3837 | } |
5e45130d | 3838 | } |
7b668576 MJ |
3839 | auto_vec<cgraph_edge *, 2> self_recursive_calls; |
3840 | for (i = callers.length () - 1; i >= 0; i--) | |
3841 | { | |
3842 | cgraph_edge *cs = callers[i]; | |
3843 | if (cs->caller == node) | |
3844 | { | |
3845 | self_recursive_calls.safe_push (cs); | |
3846 | callers.unordered_remove (i); | |
3847 | } | |
3848 | } | |
5e45130d | 3849 | |
d52f5295 ML |
3850 | new_node = node->create_virtual_clone (callers, replace_trees, |
3851 | args_to_skip, "constprop"); | |
7b668576 | 3852 | |
5bf31c64 | 3853 | bool have_self_recursive_calls = !self_recursive_calls.is_empty (); |
7b668576 MJ |
3854 | for (unsigned j = 0; j < self_recursive_calls.length (); j++) |
3855 | { | |
3856 | cgraph_edge *cs = next_edge_clone[self_recursive_calls[j]->uid]; | |
5fc1b920 MJ |
3857 | /* Cloned edges can disappear during cloning as speculation can be |
3858 | resolved, check that we have one and that it comes from the last | |
3859 | cloning. */ | |
3860 | if (cs && cs->caller == new_node) | |
3861 | cs->redirect_callee_duplicating_thunks (new_node); | |
3862 | /* Any future code that would make more than one clone of an outgoing | |
3863 | edge would confuse this mechanism, so let's check that does not | |
3864 | happen. */ | |
3865 | gcc_checking_assert (!cs | |
3866 | || !next_edge_clone[cs->uid] | |
3867 | || next_edge_clone[cs->uid]->caller != new_node); | |
7b668576 | 3868 | } |
5bf31c64 MJ |
3869 | if (have_self_recursive_calls) |
3870 | new_node->expand_all_artificial_thunks (); | |
7b668576 | 3871 | |
2c9561b5 | 3872 | ipa_set_node_agg_value_chain (new_node, aggvals); |
79ee9826 | 3873 | for (av = aggvals; av; av = av->next) |
2d8d3ae2 | 3874 | new_node->maybe_create_reference (av->value, NULL); |
79ee9826 | 3875 | |
310bc633 | 3876 | if (dump_file && (dump_flags & TDF_DETAILS)) |
2c9561b5 | 3877 | { |
464d0118 | 3878 | fprintf (dump_file, " the new node is %s.\n", new_node->dump_name ()); |
44210a96 MJ |
3879 | if (known_contexts.exists ()) |
3880 | { | |
155c9907 | 3881 | for (i = 0; i < count; i++) |
44210a96 MJ |
3882 | if (!known_contexts[i].useless_p ()) |
3883 | { | |
3884 | fprintf (dump_file, " known ctx %i is ", i); | |
3885 | known_contexts[i].dump (dump_file); | |
3886 | } | |
3887 | } | |
2c9561b5 MJ |
3888 | if (aggvals) |
3889 | ipa_dump_agg_replacement_values (dump_file, aggvals); | |
3890 | } | |
9de6f6c3 | 3891 | ipa_check_create_node_params (); |
310bc633 MJ |
3892 | update_profiling_info (node, new_node); |
3893 | new_info = IPA_NODE_REF (new_node); | |
3894 | new_info->ipcp_orig_node = node; | |
44210a96 MJ |
3895 | new_info->known_csts = known_csts; |
3896 | new_info->known_contexts = known_contexts; | |
5e45130d | 3897 | |
44210a96 | 3898 | ipcp_discover_new_direct_edges (new_node, known_csts, known_contexts, aggvals); |
310bc633 | 3899 | |
9771b263 | 3900 | callers.release (); |
310bc633 | 3901 | return new_node; |
5e45130d JH |
3902 | } |
3903 | ||
7b668576 MJ |
3904 | /* Return true, if JFUNC, which describes a i-th parameter of call CS, is a |
3905 | simple no-operation pass-through function to itself. */ | |
3906 | ||
3907 | static bool | |
3908 | self_recursive_pass_through_p (cgraph_edge *cs, ipa_jump_func *jfunc, int i) | |
3909 | { | |
3910 | enum availability availability; | |
3911 | if (cs->caller == cs->callee->function_symbol (&availability) | |
3912 | && availability > AVAIL_INTERPOSABLE | |
3913 | && jfunc->type == IPA_JF_PASS_THROUGH | |
3914 | && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR | |
3915 | && ipa_get_jf_pass_through_formal_id (jfunc) == i) | |
3916 | return true; | |
3917 | return false; | |
3918 | } | |
3919 | ||
310bc633 | 3920 | /* Given a NODE, and a subset of its CALLERS, try to populate blanks slots in |
44210a96 | 3921 | KNOWN_CSTS with constants that are also known for all of the CALLERS. */ |
3949c4a7 MJ |
3922 | |
3923 | static void | |
2c9561b5 | 3924 | find_more_scalar_values_for_callers_subset (struct cgraph_node *node, |
44210a96 | 3925 | vec<tree> known_csts, |
d52f5295 | 3926 | vec<cgraph_edge *> callers) |
3949c4a7 MJ |
3927 | { |
3928 | struct ipa_node_params *info = IPA_NODE_REF (node); | |
310bc633 | 3929 | int i, count = ipa_get_param_count (info); |
3949c4a7 | 3930 | |
155c9907 | 3931 | for (i = 0; i < count; i++) |
3949c4a7 | 3932 | { |
310bc633 MJ |
3933 | struct cgraph_edge *cs; |
3934 | tree newval = NULL_TREE; | |
3935 | int j; | |
df0d8136 | 3936 | bool first = true; |
e5cf5e11 | 3937 | tree type = ipa_get_type (info, i); |
3949c4a7 | 3938 | |
44210a96 | 3939 | if (ipa_get_scalar_lat (info, i)->bottom || known_csts[i]) |
3949c4a7 MJ |
3940 | continue; |
3941 | ||
9771b263 | 3942 | FOR_EACH_VEC_ELT (callers, j, cs) |
49c471e3 | 3943 | { |
310bc633 MJ |
3944 | struct ipa_jump_func *jump_func; |
3945 | tree t; | |
40591473 | 3946 | |
7b668576 MJ |
3947 | if (IPA_NODE_REF (cs->caller)->node_dead) |
3948 | continue; | |
3949 | ||
155c9907 | 3950 | if (i >= ipa_get_cs_argument_count (IPA_EDGE_REF (cs)) |
173b7355 | 3951 | || (i == 0 |
31db0fe0 | 3952 | && call_passes_through_thunk_p (cs))) |
155c9907 JJ |
3953 | { |
3954 | newval = NULL_TREE; | |
3955 | break; | |
3956 | } | |
310bc633 | 3957 | jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i); |
7b668576 MJ |
3958 | if (self_recursive_pass_through_p (cs, jump_func, i)) |
3959 | continue; | |
3960 | ||
e5cf5e11 | 3961 | t = ipa_value_from_jfunc (IPA_NODE_REF (cs->caller), jump_func, type); |
310bc633 MJ |
3962 | if (!t |
3963 | || (newval | |
df0d8136 JH |
3964 | && !values_equal_for_ipcp_p (t, newval)) |
3965 | || (!first && !newval)) | |
3949c4a7 | 3966 | { |
310bc633 MJ |
3967 | newval = NULL_TREE; |
3968 | break; | |
3949c4a7 | 3969 | } |
310bc633 MJ |
3970 | else |
3971 | newval = t; | |
df0d8136 | 3972 | first = false; |
3949c4a7 MJ |
3973 | } |
3974 | ||
310bc633 MJ |
3975 | if (newval) |
3976 | { | |
3977 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3978 | { | |
2c9561b5 | 3979 | fprintf (dump_file, " adding an extra known scalar value "); |
310bc633 | 3980 | print_ipcp_constant_value (dump_file, newval); |
0e8853ee JH |
3981 | fprintf (dump_file, " for "); |
3982 | ipa_dump_param (dump_file, info, i); | |
310bc633 MJ |
3983 | fprintf (dump_file, "\n"); |
3984 | } | |
5e45130d | 3985 | |
44210a96 | 3986 | known_csts[i] = newval; |
310bc633 | 3987 | } |
5e45130d | 3988 | } |
5e45130d JH |
3989 | } |
3990 | ||
44210a96 MJ |
3991 | /* Given a NODE and a subset of its CALLERS, try to populate plank slots in |
3992 | KNOWN_CONTEXTS with polymorphic contexts that are also known for all of the | |
3993 | CALLERS. */ | |
3994 | ||
3995 | static void | |
3996 | find_more_contexts_for_caller_subset (cgraph_node *node, | |
3997 | vec<ipa_polymorphic_call_context> | |
3998 | *known_contexts, | |
3999 | vec<cgraph_edge *> callers) | |
4000 | { | |
4001 | ipa_node_params *info = IPA_NODE_REF (node); | |
4002 | int i, count = ipa_get_param_count (info); | |
4003 | ||
155c9907 | 4004 | for (i = 0; i < count; i++) |
44210a96 MJ |
4005 | { |
4006 | cgraph_edge *cs; | |
4007 | ||
4008 | if (ipa_get_poly_ctx_lat (info, i)->bottom | |
4009 | || (known_contexts->exists () | |
4010 | && !(*known_contexts)[i].useless_p ())) | |
4011 | continue; | |
4012 | ||
4013 | ipa_polymorphic_call_context newval; | |
df0d8136 | 4014 | bool first = true; |
44210a96 MJ |
4015 | int j; |
4016 | ||
4017 | FOR_EACH_VEC_ELT (callers, j, cs) | |
4018 | { | |
4019 | if (i >= ipa_get_cs_argument_count (IPA_EDGE_REF (cs))) | |
4020 | return; | |
4021 | ipa_jump_func *jfunc = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), | |
4022 | i); | |
4023 | ipa_polymorphic_call_context ctx; | |
4024 | ctx = ipa_context_from_jfunc (IPA_NODE_REF (cs->caller), cs, i, | |
4025 | jfunc); | |
df0d8136 | 4026 | if (first) |
44210a96 | 4027 | { |
44210a96 | 4028 | newval = ctx; |
df0d8136 | 4029 | first = false; |
44210a96 | 4030 | } |
df0d8136 JH |
4031 | else |
4032 | newval.meet_with (ctx); | |
4033 | if (newval.useless_p ()) | |
4034 | break; | |
44210a96 MJ |
4035 | } |
4036 | ||
df0d8136 | 4037 | if (!newval.useless_p ()) |
44210a96 MJ |
4038 | { |
4039 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
4040 | { | |
4041 | fprintf (dump_file, " adding an extra known polymorphic " | |
4042 | "context "); | |
4043 | print_ipcp_constant_value (dump_file, newval); | |
4044 | fprintf (dump_file, " for "); | |
4045 | ipa_dump_param (dump_file, info, i); | |
4046 | fprintf (dump_file, "\n"); | |
4047 | } | |
4048 | ||
4049 | if (!known_contexts->exists ()) | |
4050 | known_contexts->safe_grow_cleared (ipa_get_param_count (info)); | |
4051 | (*known_contexts)[i] = newval; | |
4052 | } | |
4053 | ||
4054 | } | |
4055 | } | |
4056 | ||
2c9561b5 MJ |
4057 | /* Go through PLATS and create a vector of values consisting of values and |
4058 | offsets (minus OFFSET) of lattices that contain only a single value. */ | |
4059 | ||
84562394 | 4060 | static vec<ipa_agg_jf_item> |
2c9561b5 MJ |
4061 | copy_plats_to_inter (struct ipcp_param_lattices *plats, HOST_WIDE_INT offset) |
4062 | { | |
84562394 | 4063 | vec<ipa_agg_jf_item> res = vNULL; |
2c9561b5 MJ |
4064 | |
4065 | if (!plats->aggs || plats->aggs_contain_variable || plats->aggs_bottom) | |
6e1aa848 | 4066 | return vNULL; |
2c9561b5 MJ |
4067 | |
4068 | for (struct ipcp_agg_lattice *aglat = plats->aggs; aglat; aglat = aglat->next) | |
c0cb5055 | 4069 | if (aglat->is_single_const ()) |
2c9561b5 MJ |
4070 | { |
4071 | struct ipa_agg_jf_item ti; | |
4072 | ti.offset = aglat->offset - offset; | |
4073 | ti.value = aglat->values->value; | |
9771b263 | 4074 | res.safe_push (ti); |
2c9561b5 MJ |
4075 | } |
4076 | return res; | |
4077 | } | |
4078 | ||
4079 | /* Intersect all values in INTER with single value lattices in PLATS (while | |
4080 | subtracting OFFSET). */ | |
4081 | ||
4082 | static void | |
4083 | intersect_with_plats (struct ipcp_param_lattices *plats, | |
84562394 | 4084 | vec<ipa_agg_jf_item> *inter, |
2c9561b5 MJ |
4085 | HOST_WIDE_INT offset) |
4086 | { | |
4087 | struct ipcp_agg_lattice *aglat; | |
4088 | struct ipa_agg_jf_item *item; | |
4089 | int k; | |
4090 | ||
4091 | if (!plats->aggs || plats->aggs_contain_variable || plats->aggs_bottom) | |
4092 | { | |
9771b263 | 4093 | inter->release (); |
2c9561b5 MJ |
4094 | return; |
4095 | } | |
4096 | ||
4097 | aglat = plats->aggs; | |
9771b263 | 4098 | FOR_EACH_VEC_ELT (*inter, k, item) |
2c9561b5 MJ |
4099 | { |
4100 | bool found = false; | |
4101 | if (!item->value) | |
4102 | continue; | |
4103 | while (aglat) | |
4104 | { | |
4105 | if (aglat->offset - offset > item->offset) | |
4106 | break; | |
4107 | if (aglat->offset - offset == item->offset) | |
4108 | { | |
4109 | gcc_checking_assert (item->value); | |
063c5529 MJ |
4110 | if (aglat->is_single_const () |
4111 | && values_equal_for_ipcp_p (item->value, | |
4112 | aglat->values->value)) | |
2c9561b5 MJ |
4113 | found = true; |
4114 | break; | |
4115 | } | |
4116 | aglat = aglat->next; | |
4117 | } | |
4118 | if (!found) | |
4119 | item->value = NULL_TREE; | |
4120 | } | |
4121 | } | |
4122 | ||
5764ee3c | 4123 | /* Copy aggregate replacement values of NODE (which is an IPA-CP clone) to the |
2c9561b5 MJ |
4124 | vector result while subtracting OFFSET from the individual value offsets. */ |
4125 | ||
84562394 | 4126 | static vec<ipa_agg_jf_item> |
0fd44da3 MJ |
4127 | agg_replacements_to_vector (struct cgraph_node *node, int index, |
4128 | HOST_WIDE_INT offset) | |
2c9561b5 MJ |
4129 | { |
4130 | struct ipa_agg_replacement_value *av; | |
84562394 | 4131 | vec<ipa_agg_jf_item> res = vNULL; |
2c9561b5 MJ |
4132 | |
4133 | for (av = ipa_get_agg_replacements_for_node (node); av; av = av->next) | |
0fd44da3 MJ |
4134 | if (av->index == index |
4135 | && (av->offset - offset) >= 0) | |
2c9561b5 MJ |
4136 | { |
4137 | struct ipa_agg_jf_item item; | |
4138 | gcc_checking_assert (av->value); | |
4139 | item.offset = av->offset - offset; | |
4140 | item.value = av->value; | |
9771b263 | 4141 | res.safe_push (item); |
2c9561b5 MJ |
4142 | } |
4143 | ||
4144 | return res; | |
4145 | } | |
4146 | ||
4147 | /* Intersect all values in INTER with those that we have already scheduled to | |
4148 | be replaced in parameter number INDEX of NODE, which is an IPA-CP clone | |
4149 | (while subtracting OFFSET). */ | |
4150 | ||
4151 | static void | |
4152 | intersect_with_agg_replacements (struct cgraph_node *node, int index, | |
84562394 | 4153 | vec<ipa_agg_jf_item> *inter, |
2c9561b5 MJ |
4154 | HOST_WIDE_INT offset) |
4155 | { | |
4156 | struct ipa_agg_replacement_value *srcvals; | |
4157 | struct ipa_agg_jf_item *item; | |
4158 | int i; | |
4159 | ||
4160 | srcvals = ipa_get_agg_replacements_for_node (node); | |
4161 | if (!srcvals) | |
4162 | { | |
9771b263 | 4163 | inter->release (); |
2c9561b5 MJ |
4164 | return; |
4165 | } | |
4166 | ||
9771b263 | 4167 | FOR_EACH_VEC_ELT (*inter, i, item) |
2c9561b5 MJ |
4168 | { |
4169 | struct ipa_agg_replacement_value *av; | |
4170 | bool found = false; | |
4171 | if (!item->value) | |
4172 | continue; | |
4173 | for (av = srcvals; av; av = av->next) | |
4174 | { | |
4175 | gcc_checking_assert (av->value); | |
4176 | if (av->index == index | |
4177 | && av->offset - offset == item->offset) | |
4178 | { | |
4179 | if (values_equal_for_ipcp_p (item->value, av->value)) | |
4180 | found = true; | |
4181 | break; | |
4182 | } | |
4183 | } | |
4184 | if (!found) | |
4185 | item->value = NULL_TREE; | |
4186 | } | |
4187 | } | |
4188 | ||
7e9f2b6e MJ |
4189 | /* Intersect values in INTER with aggregate values that come along edge CS to |
4190 | parameter number INDEX and return it. If INTER does not actually exist yet, | |
4191 | copy all incoming values to it. If we determine we ended up with no values | |
4192 | whatsoever, return a released vector. */ | |
4193 | ||
84562394 | 4194 | static vec<ipa_agg_jf_item> |
7e9f2b6e | 4195 | intersect_aggregates_with_edge (struct cgraph_edge *cs, int index, |
84562394 | 4196 | vec<ipa_agg_jf_item> inter) |
7e9f2b6e MJ |
4197 | { |
4198 | struct ipa_jump_func *jfunc; | |
4199 | jfunc = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), index); | |
4200 | if (jfunc->type == IPA_JF_PASS_THROUGH | |
4201 | && ipa_get_jf_pass_through_operation (jfunc) == NOP_EXPR) | |
4202 | { | |
4203 | struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller); | |
4204 | int src_idx = ipa_get_jf_pass_through_formal_id (jfunc); | |
4205 | ||
4206 | if (caller_info->ipcp_orig_node) | |
4207 | { | |
4208 | struct cgraph_node *orig_node = caller_info->ipcp_orig_node; | |
4209 | struct ipcp_param_lattices *orig_plats; | |
4210 | orig_plats = ipa_get_parm_lattices (IPA_NODE_REF (orig_node), | |
4211 | src_idx); | |
4212 | if (agg_pass_through_permissible_p (orig_plats, jfunc)) | |
4213 | { | |
4214 | if (!inter.exists ()) | |
0fd44da3 | 4215 | inter = agg_replacements_to_vector (cs->caller, src_idx, 0); |
7e9f2b6e MJ |
4216 | else |
4217 | intersect_with_agg_replacements (cs->caller, src_idx, | |
4218 | &inter, 0); | |
4219 | } | |
c8f40352 MJ |
4220 | else |
4221 | { | |
4222 | inter.release (); | |
4223 | return vNULL; | |
4224 | } | |
7e9f2b6e MJ |
4225 | } |
4226 | else | |
4227 | { | |
4228 | struct ipcp_param_lattices *src_plats; | |
4229 | src_plats = ipa_get_parm_lattices (caller_info, src_idx); | |
4230 | if (agg_pass_through_permissible_p (src_plats, jfunc)) | |
4231 | { | |
4232 | /* Currently we do not produce clobber aggregate jump | |
4233 | functions, adjust when we do. */ | |
4234 | gcc_checking_assert (!jfunc->agg.items); | |
4235 | if (!inter.exists ()) | |
4236 | inter = copy_plats_to_inter (src_plats, 0); | |
4237 | else | |
4238 | intersect_with_plats (src_plats, &inter, 0); | |
4239 | } | |
c8f40352 MJ |
4240 | else |
4241 | { | |
4242 | inter.release (); | |
4243 | return vNULL; | |
4244 | } | |
7e9f2b6e MJ |
4245 | } |
4246 | } | |
4247 | else if (jfunc->type == IPA_JF_ANCESTOR | |
4248 | && ipa_get_jf_ancestor_agg_preserved (jfunc)) | |
4249 | { | |
4250 | struct ipa_node_params *caller_info = IPA_NODE_REF (cs->caller); | |
4251 | int src_idx = ipa_get_jf_ancestor_formal_id (jfunc); | |
4252 | struct ipcp_param_lattices *src_plats; | |
4253 | HOST_WIDE_INT delta = ipa_get_jf_ancestor_offset (jfunc); | |
4254 | ||
4255 | if (caller_info->ipcp_orig_node) | |
4256 | { | |
4257 | if (!inter.exists ()) | |
0fd44da3 | 4258 | inter = agg_replacements_to_vector (cs->caller, src_idx, delta); |
7e9f2b6e | 4259 | else |
0fd44da3 | 4260 | intersect_with_agg_replacements (cs->caller, src_idx, &inter, |
7e9f2b6e MJ |
4261 | delta); |
4262 | } | |
4263 | else | |
4264 | { | |
5de73c05 | 4265 | src_plats = ipa_get_parm_lattices (caller_info, src_idx); |
7e9f2b6e MJ |
4266 | /* Currently we do not produce clobber aggregate jump |
4267 | functions, adjust when we do. */ | |
4268 | gcc_checking_assert (!src_plats->aggs || !jfunc->agg.items); | |
4269 | if (!inter.exists ()) | |
4270 | inter = copy_plats_to_inter (src_plats, delta); | |
4271 | else | |
4272 | intersect_with_plats (src_plats, &inter, delta); | |
4273 | } | |
4274 | } | |
4275 | else if (jfunc->agg.items) | |
4276 | { | |
4277 | struct ipa_agg_jf_item *item; | |
4278 | int k; | |
4279 | ||
4280 | if (!inter.exists ()) | |
4281 | for (unsigned i = 0; i < jfunc->agg.items->length (); i++) | |
4282 | inter.safe_push ((*jfunc->agg.items)[i]); | |
4283 | else | |
4284 | FOR_EACH_VEC_ELT (inter, k, item) | |
4285 | { | |
4286 | int l = 0; | |
5de73c05 | 4287 | bool found = false; |
7e9f2b6e MJ |
4288 | |
4289 | if (!item->value) | |
4290 | continue; | |
4291 | ||
4292 | while ((unsigned) l < jfunc->agg.items->length ()) | |
4293 | { | |
4294 | struct ipa_agg_jf_item *ti; | |
4295 | ti = &(*jfunc->agg.items)[l]; | |
4296 | if (ti->offset > item->offset) | |
4297 | break; | |
4298 | if (ti->offset == item->offset) | |
4299 | { | |
4300 | gcc_checking_assert (ti->value); | |
4301 | if (values_equal_for_ipcp_p (item->value, | |
4302 | ti->value)) | |
4303 | found = true; | |
4304 | break; | |
4305 | } | |
4306 | l++; | |
4307 | } | |
4308 | if (!found) | |
4309 | item->value = NULL; | |
4310 | } | |
4311 | } | |
4312 | else | |
4313 | { | |
c3284718 | 4314 | inter.release (); |
84562394 | 4315 | return vec<ipa_agg_jf_item>(); |
7e9f2b6e MJ |
4316 | } |
4317 | return inter; | |
4318 | } | |
4319 | ||
2c9561b5 MJ |
4320 | /* Look at edges in CALLERS and collect all known aggregate values that arrive |
4321 | from all of them. */ | |
4322 | ||
4323 | static struct ipa_agg_replacement_value * | |
4324 | find_aggregate_values_for_callers_subset (struct cgraph_node *node, | |
d52f5295 | 4325 | vec<cgraph_edge *> callers) |
2c9561b5 | 4326 | { |
dffdd6e5 | 4327 | struct ipa_node_params *dest_info = IPA_NODE_REF (node); |
6f9549ee MJ |
4328 | struct ipa_agg_replacement_value *res; |
4329 | struct ipa_agg_replacement_value **tail = &res; | |
2c9561b5 | 4330 | struct cgraph_edge *cs; |
dffdd6e5 | 4331 | int i, j, count = ipa_get_param_count (dest_info); |
2c9561b5 | 4332 | |
9771b263 | 4333 | FOR_EACH_VEC_ELT (callers, j, cs) |
2c9561b5 MJ |
4334 | { |
4335 | int c = ipa_get_cs_argument_count (IPA_EDGE_REF (cs)); | |
4336 | if (c < count) | |
4337 | count = c; | |
4338 | } | |
4339 | ||
155c9907 | 4340 | for (i = 0; i < count; i++) |
2c9561b5 MJ |
4341 | { |
4342 | struct cgraph_edge *cs; | |
84562394 | 4343 | vec<ipa_agg_jf_item> inter = vNULL; |
2c9561b5 | 4344 | struct ipa_agg_jf_item *item; |
7b920a9a | 4345 | struct ipcp_param_lattices *plats = ipa_get_parm_lattices (dest_info, i); |
2c9561b5 MJ |
4346 | int j; |
4347 | ||
4348 | /* Among other things, the following check should deal with all by_ref | |
4349 | mismatches. */ | |
7b920a9a | 4350 | if (plats->aggs_bottom) |
2c9561b5 MJ |
4351 | continue; |
4352 | ||
9771b263 | 4353 | FOR_EACH_VEC_ELT (callers, j, cs) |
2c9561b5 | 4354 | { |
7b668576 MJ |
4355 | struct ipa_jump_func *jfunc |
4356 | = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i); | |
cf254442 MJ |
4357 | if (self_recursive_pass_through_p (cs, jfunc, i) |
4358 | && (!plats->aggs_by_ref | |
4359 | || ipa_get_jf_pass_through_agg_preserved (jfunc))) | |
7b668576 | 4360 | continue; |
7e9f2b6e | 4361 | inter = intersect_aggregates_with_edge (cs, i, inter); |
2c9561b5 | 4362 | |
9771b263 | 4363 | if (!inter.exists ()) |
2c9561b5 MJ |
4364 | goto next_param; |
4365 | } | |
4366 | ||
9771b263 | 4367 | FOR_EACH_VEC_ELT (inter, j, item) |
2c9561b5 MJ |
4368 | { |
4369 | struct ipa_agg_replacement_value *v; | |
4370 | ||
4371 | if (!item->value) | |
4372 | continue; | |
4373 | ||
766090c2 | 4374 | v = ggc_alloc<ipa_agg_replacement_value> (); |
2c9561b5 MJ |
4375 | v->index = i; |
4376 | v->offset = item->offset; | |
4377 | v->value = item->value; | |
7b920a9a | 4378 | v->by_ref = plats->aggs_by_ref; |
6f9549ee MJ |
4379 | *tail = v; |
4380 | tail = &v->next; | |
2c9561b5 MJ |
4381 | } |
4382 | ||
4383 | next_param: | |
9771b263 DN |
4384 | if (inter.exists ()) |
4385 | inter.release (); | |
2c9561b5 | 4386 | } |
6f9549ee | 4387 | *tail = NULL; |
2c9561b5 MJ |
4388 | return res; |
4389 | } | |
4390 | ||
2c9561b5 MJ |
4391 | /* Determine whether CS also brings all scalar values that the NODE is |
4392 | specialized for. */ | |
4393 | ||
4394 | static bool | |
4395 | cgraph_edge_brings_all_scalars_for_node (struct cgraph_edge *cs, | |
4396 | struct cgraph_node *node) | |
4397 | { | |
4398 | struct ipa_node_params *dest_info = IPA_NODE_REF (node); | |
4399 | int count = ipa_get_param_count (dest_info); | |
4400 | struct ipa_node_params *caller_info; | |
4401 | struct ipa_edge_args *args; | |
4402 | int i; | |
4403 | ||
4404 | caller_info = IPA_NODE_REF (cs->caller); | |
4405 | args = IPA_EDGE_REF (cs); | |
4406 | for (i = 0; i < count; i++) | |
4407 | { | |
4408 | struct ipa_jump_func *jump_func; | |
4409 | tree val, t; | |
4410 | ||
44210a96 | 4411 | val = dest_info->known_csts[i]; |
2c9561b5 MJ |
4412 | if (!val) |
4413 | continue; | |
4414 | ||
4415 | if (i >= ipa_get_cs_argument_count (args)) | |
4416 | return false; | |
4417 | jump_func = ipa_get_ith_jump_func (args, i); | |
e5cf5e11 PK |
4418 | t = ipa_value_from_jfunc (caller_info, jump_func, |
4419 | ipa_get_type (dest_info, i)); | |
2c9561b5 MJ |
4420 | if (!t || !values_equal_for_ipcp_p (val, t)) |
4421 | return false; | |
4422 | } | |
4423 | return true; | |
4424 | } | |
4425 | ||
4426 | /* Determine whether CS also brings all aggregate values that NODE is | |
4427 | specialized for. */ | |
4428 | static bool | |
4429 | cgraph_edge_brings_all_agg_vals_for_node (struct cgraph_edge *cs, | |
4430 | struct cgraph_node *node) | |
4431 | { | |
7e9f2b6e | 4432 | struct ipa_node_params *orig_caller_info = IPA_NODE_REF (cs->caller); |
9576e7b1 | 4433 | struct ipa_node_params *orig_node_info; |
2c9561b5 | 4434 | struct ipa_agg_replacement_value *aggval; |
7e9f2b6e | 4435 | int i, ec, count; |
2c9561b5 MJ |
4436 | |
4437 | aggval = ipa_get_agg_replacements_for_node (node); | |
7e9f2b6e MJ |
4438 | if (!aggval) |
4439 | return true; | |
4440 | ||
4441 | count = ipa_get_param_count (IPA_NODE_REF (node)); | |
4442 | ec = ipa_get_cs_argument_count (IPA_EDGE_REF (cs)); | |
4443 | if (ec < count) | |
4444 | for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next) | |
4445 | if (aggval->index >= ec) | |
4446 | return false; | |
4447 | ||
9576e7b1 | 4448 | orig_node_info = IPA_NODE_REF (IPA_NODE_REF (node)->ipcp_orig_node); |
7e9f2b6e MJ |
4449 | if (orig_caller_info->ipcp_orig_node) |
4450 | orig_caller_info = IPA_NODE_REF (orig_caller_info->ipcp_orig_node); | |
4451 | ||
4452 | for (i = 0; i < count; i++) | |
2c9561b5 | 4453 | { |
84562394 | 4454 | static vec<ipa_agg_jf_item> values = vec<ipa_agg_jf_item>(); |
2c9561b5 | 4455 | struct ipcp_param_lattices *plats; |
7e9f2b6e MJ |
4456 | bool interesting = false; |
4457 | for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next) | |
4458 | if (aggval->index == i) | |
4459 | { | |
4460 | interesting = true; | |
4461 | break; | |
4462 | } | |
4463 | if (!interesting) | |
4464 | continue; | |
4465 | ||
9576e7b1 | 4466 | plats = ipa_get_parm_lattices (orig_node_info, aggval->index); |
7e9f2b6e | 4467 | if (plats->aggs_bottom) |
2c9561b5 | 4468 | return false; |
2c9561b5 | 4469 | |
7e9f2b6e | 4470 | values = intersect_aggregates_with_edge (cs, i, values); |
c3284718 | 4471 | if (!values.exists ()) |
2c9561b5 MJ |
4472 | return false; |
4473 | ||
7e9f2b6e MJ |
4474 | for (struct ipa_agg_replacement_value *av = aggval; av; av = av->next) |
4475 | if (aggval->index == i) | |
4476 | { | |
4477 | struct ipa_agg_jf_item *item; | |
4478 | int j; | |
4479 | bool found = false; | |
4480 | FOR_EACH_VEC_ELT (values, j, item) | |
4481 | if (item->value | |
4482 | && item->offset == av->offset | |
4483 | && values_equal_for_ipcp_p (item->value, av->value)) | |
c3272a92 PCC |
4484 | { |
4485 | found = true; | |
4486 | break; | |
4487 | } | |
7e9f2b6e MJ |
4488 | if (!found) |
4489 | { | |
c3284718 | 4490 | values.release (); |
7e9f2b6e MJ |
4491 | return false; |
4492 | } | |
4493 | } | |
2c9561b5 MJ |
4494 | } |
4495 | return true; | |
4496 | } | |
4497 | ||
310bc633 MJ |
4498 | /* Given an original NODE and a VAL for which we have already created a |
4499 | specialized clone, look whether there are incoming edges that still lead | |
4500 | into the old node but now also bring the requested value and also conform to | |
026c3cfd | 4501 | all other criteria such that they can be redirected the special node. |
310bc633 | 4502 | This function can therefore redirect the final edge in a SCC. */ |
3e66255c | 4503 | |
c0cb5055 | 4504 | template <typename valtype> |
3e66255c | 4505 | static void |
c0cb5055 | 4506 | perhaps_add_new_callers (cgraph_node *node, ipcp_value<valtype> *val) |
3e66255c | 4507 | { |
c0cb5055 | 4508 | ipcp_value_source<valtype> *src; |
3995f3a2 | 4509 | profile_count redirected_sum = profile_count::zero (); |
3e66255c | 4510 | |
310bc633 | 4511 | for (src = val->sources; src; src = src->next) |
3e66255c | 4512 | { |
310bc633 MJ |
4513 | struct cgraph_edge *cs = src->cs; |
4514 | while (cs) | |
4515 | { | |
7b668576 | 4516 | if (cgraph_edge_brings_value_p (cs, src, node, val) |
47f4756e MJ |
4517 | && cgraph_edge_brings_all_scalars_for_node (cs, val->spec_node) |
4518 | && cgraph_edge_brings_all_agg_vals_for_node (cs, val->spec_node)) | |
310bc633 | 4519 | { |
47f4756e | 4520 | if (dump_file) |
464d0118 ML |
4521 | fprintf (dump_file, " - adding an extra caller %s of %s\n", |
4522 | cs->caller->dump_name (), | |
4523 | val->spec_node->dump_name ()); | |
47f4756e | 4524 | |
6a4bad95 MJ |
4525 | cs->redirect_callee_duplicating_thunks (val->spec_node); |
4526 | val->spec_node->expand_all_artificial_thunks (); | |
1bad9c18 JH |
4527 | if (cs->count.ipa ().initialized_p ()) |
4528 | redirected_sum = redirected_sum + cs->count.ipa (); | |
310bc633 MJ |
4529 | } |
4530 | cs = get_next_cgraph_edge_clone (cs); | |
4531 | } | |
3e66255c | 4532 | } |
310bc633 | 4533 | |
e3951b03 | 4534 | if (redirected_sum.nonzero_p ()) |
310bc633 | 4535 | update_specialized_profile (val->spec_node, node, redirected_sum); |
3e66255c MJ |
4536 | } |
4537 | ||
44210a96 | 4538 | /* Return true if KNOWN_CONTEXTS contain at least one useful context. */ |
3e66255c | 4539 | |
44210a96 MJ |
4540 | static bool |
4541 | known_contexts_useful_p (vec<ipa_polymorphic_call_context> known_contexts) | |
4542 | { | |
4543 | ipa_polymorphic_call_context *ctx; | |
4544 | int i; | |
4545 | ||
4546 | FOR_EACH_VEC_ELT (known_contexts, i, ctx) | |
4547 | if (!ctx->useless_p ()) | |
4548 | return true; | |
4549 | return false; | |
4550 | } | |
4551 | ||
4552 | /* Return a copy of KNOWN_CSTS if it is not empty, otherwise return vNULL. */ | |
4553 | ||
4554 | static vec<ipa_polymorphic_call_context> | |
4555 | copy_useful_known_contexts (vec<ipa_polymorphic_call_context> known_contexts) | |
4556 | { | |
4557 | if (known_contexts_useful_p (known_contexts)) | |
4558 | return known_contexts.copy (); | |
4559 | else | |
4560 | return vNULL; | |
4561 | } | |
4562 | ||
4563 | /* Copy KNOWN_CSTS and modify the copy according to VAL and INDEX. If | |
4564 | non-empty, replace KNOWN_CONTEXTS with its copy too. */ | |
310bc633 | 4565 | |
518dc859 | 4566 | static void |
44210a96 MJ |
4567 | modify_known_vectors_with_val (vec<tree> *known_csts, |
4568 | vec<ipa_polymorphic_call_context> *known_contexts, | |
4569 | ipcp_value<tree> *val, | |
4570 | int index) | |
518dc859 | 4571 | { |
44210a96 MJ |
4572 | *known_csts = known_csts->copy (); |
4573 | *known_contexts = copy_useful_known_contexts (*known_contexts); | |
4574 | (*known_csts)[index] = val->value; | |
4575 | } | |
518dc859 | 4576 | |
44210a96 MJ |
4577 | /* Replace KNOWN_CSTS with its copy. Also copy KNOWN_CONTEXTS and modify the |
4578 | copy according to VAL and INDEX. */ | |
4579 | ||
4580 | static void | |
4581 | modify_known_vectors_with_val (vec<tree> *known_csts, | |
4582 | vec<ipa_polymorphic_call_context> *known_contexts, | |
4583 | ipcp_value<ipa_polymorphic_call_context> *val, | |
4584 | int index) | |
4585 | { | |
4586 | *known_csts = known_csts->copy (); | |
4587 | *known_contexts = known_contexts->copy (); | |
4588 | (*known_contexts)[index] = val->value; | |
310bc633 | 4589 | } |
5e45130d | 4590 | |
44210a96 MJ |
4591 | /* Return true if OFFSET indicates this was not an aggregate value or there is |
4592 | a replacement equivalent to VALUE, INDEX and OFFSET among those in the | |
4593 | AGGVALS list. */ | |
2c9561b5 MJ |
4594 | |
4595 | DEBUG_FUNCTION bool | |
44210a96 MJ |
4596 | ipcp_val_agg_replacement_ok_p (ipa_agg_replacement_value *aggvals, |
4597 | int index, HOST_WIDE_INT offset, tree value) | |
2c9561b5 | 4598 | { |
44210a96 MJ |
4599 | if (offset == -1) |
4600 | return true; | |
4601 | ||
2c9561b5 MJ |
4602 | while (aggvals) |
4603 | { | |
4604 | if (aggvals->index == index | |
4605 | && aggvals->offset == offset | |
4606 | && values_equal_for_ipcp_p (aggvals->value, value)) | |
4607 | return true; | |
4608 | aggvals = aggvals->next; | |
4609 | } | |
4610 | return false; | |
4611 | } | |
4612 | ||
44210a96 MJ |
4613 | /* Return true if offset is minus one because source of a polymorphic contect |
4614 | cannot be an aggregate value. */ | |
4615 | ||
4616 | DEBUG_FUNCTION bool | |
4617 | ipcp_val_agg_replacement_ok_p (ipa_agg_replacement_value *, | |
4618 | int , HOST_WIDE_INT offset, | |
4619 | ipa_polymorphic_call_context) | |
4620 | { | |
4621 | return offset == -1; | |
4622 | } | |
4623 | ||
2c9561b5 MJ |
4624 | /* Decide wheter to create a special version of NODE for value VAL of parameter |
4625 | at the given INDEX. If OFFSET is -1, the value is for the parameter itself, | |
4626 | otherwise it is stored at the given OFFSET of the parameter. KNOWN_CSTS, | |
44210a96 | 4627 | KNOWN_CONTEXTS and KNOWN_AGGS describe the other already known values. */ |
2c9561b5 | 4628 | |
c0cb5055 | 4629 | template <typename valtype> |
2c9561b5 MJ |
4630 | static bool |
4631 | decide_about_value (struct cgraph_node *node, int index, HOST_WIDE_INT offset, | |
c0cb5055 | 4632 | ipcp_value<valtype> *val, vec<tree> known_csts, |
44210a96 | 4633 | vec<ipa_polymorphic_call_context> known_contexts) |
2c9561b5 MJ |
4634 | { |
4635 | struct ipa_agg_replacement_value *aggvals; | |
4636 | int freq_sum, caller_count; | |
3995f3a2 | 4637 | profile_count count_sum; |
d52f5295 | 4638 | vec<cgraph_edge *> callers; |
2c9561b5 MJ |
4639 | |
4640 | if (val->spec_node) | |
4641 | { | |
4642 | perhaps_add_new_callers (node, val); | |
4643 | return false; | |
4644 | } | |
4645 | else if (val->local_size_cost + overall_size > max_new_size) | |
4646 | { | |
4647 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
4648 | fprintf (dump_file, " Ignoring candidate value because " | |
4649 | "max_new_size would be reached with %li.\n", | |
4650 | val->local_size_cost + overall_size); | |
4651 | return false; | |
4652 | } | |
47f4756e | 4653 | else if (!get_info_about_necessary_edges (val, node, &freq_sum, &count_sum, |
2c9561b5 MJ |
4654 | &caller_count)) |
4655 | return false; | |
4656 | ||
4657 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
4658 | { | |
4659 | fprintf (dump_file, " - considering value "); | |
4660 | print_ipcp_constant_value (dump_file, val->value); | |
0e8853ee JH |
4661 | fprintf (dump_file, " for "); |
4662 | ipa_dump_param (dump_file, IPA_NODE_REF (node), index); | |
2c9561b5 MJ |
4663 | if (offset != -1) |
4664 | fprintf (dump_file, ", offset: " HOST_WIDE_INT_PRINT_DEC, offset); | |
4665 | fprintf (dump_file, " (caller_count: %i)\n", caller_count); | |
4666 | } | |
4667 | ||
4668 | if (!good_cloning_opportunity_p (node, val->local_time_benefit, | |
4669 | freq_sum, count_sum, | |
4670 | val->local_size_cost) | |
4671 | && !good_cloning_opportunity_p (node, | |
4672 | val->local_time_benefit | |
4673 | + val->prop_time_benefit, | |
4674 | freq_sum, count_sum, | |
4675 | val->local_size_cost | |
4676 | + val->prop_size_cost)) | |
4677 | return false; | |
4678 | ||
4679 | if (dump_file) | |
464d0118 ML |
4680 | fprintf (dump_file, " Creating a specialized node of %s.\n", |
4681 | node->dump_name ()); | |
2c9561b5 | 4682 | |
47f4756e | 4683 | callers = gather_edges_for_value (val, node, caller_count); |
2c9561b5 | 4684 | if (offset == -1) |
44210a96 MJ |
4685 | modify_known_vectors_with_val (&known_csts, &known_contexts, val, index); |
4686 | else | |
4687 | { | |
4688 | known_csts = known_csts.copy (); | |
4689 | known_contexts = copy_useful_known_contexts (known_contexts); | |
4690 | } | |
4691 | find_more_scalar_values_for_callers_subset (node, known_csts, callers); | |
4692 | find_more_contexts_for_caller_subset (node, &known_contexts, callers); | |
2c9561b5 | 4693 | aggvals = find_aggregate_values_for_callers_subset (node, callers); |
44210a96 MJ |
4694 | gcc_checking_assert (ipcp_val_agg_replacement_ok_p (aggvals, index, |
4695 | offset, val->value)); | |
4696 | val->spec_node = create_specialized_node (node, known_csts, known_contexts, | |
4697 | aggvals, callers); | |
2c9561b5 MJ |
4698 | overall_size += val->local_size_cost; |
4699 | ||
4700 | /* TODO: If for some lattice there is only one other known value | |
4701 | left, make a special node for it too. */ | |
4702 | ||
4703 | return true; | |
4704 | } | |
5e45130d | 4705 | |
310bc633 | 4706 | /* Decide whether and what specialized clones of NODE should be created. */ |
5e45130d | 4707 | |
310bc633 MJ |
4708 | static bool |
4709 | decide_whether_version_node (struct cgraph_node *node) | |
4710 | { | |
4711 | struct ipa_node_params *info = IPA_NODE_REF (node); | |
4712 | int i, count = ipa_get_param_count (info); | |
44210a96 MJ |
4713 | vec<tree> known_csts; |
4714 | vec<ipa_polymorphic_call_context> known_contexts; | |
84562394 | 4715 | vec<ipa_agg_jump_function> known_aggs = vNULL; |
310bc633 | 4716 | bool ret = false; |
5e45130d | 4717 | |
310bc633 MJ |
4718 | if (count == 0) |
4719 | return false; | |
5e45130d | 4720 | |
310bc633 | 4721 | if (dump_file && (dump_flags & TDF_DETAILS)) |
464d0118 ML |
4722 | fprintf (dump_file, "\nEvaluating opportunities for %s.\n", |
4723 | node->dump_name ()); | |
5e45130d | 4724 | |
44210a96 | 4725 | gather_context_independent_values (info, &known_csts, &known_contexts, |
eb20b778 MJ |
4726 | info->do_clone_for_all_contexts ? &known_aggs |
4727 | : NULL, NULL); | |
5e45130d | 4728 | |
155c9907 | 4729 | for (i = 0; i < count;i++) |
310bc633 | 4730 | { |
2c9561b5 | 4731 | struct ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i); |
c0cb5055 | 4732 | ipcp_lattice<tree> *lat = &plats->itself; |
44210a96 | 4733 | ipcp_lattice<ipa_polymorphic_call_context> *ctxlat = &plats->ctxlat; |
5e45130d | 4734 | |
2c9561b5 | 4735 | if (!lat->bottom |
44210a96 MJ |
4736 | && !known_csts[i]) |
4737 | { | |
4738 | ipcp_value<tree> *val; | |
4739 | for (val = lat->values; val; val = val->next) | |
4740 | ret |= decide_about_value (node, i, -1, val, known_csts, | |
4741 | known_contexts); | |
4742 | } | |
61e03ffc | 4743 | |
eb20b778 | 4744 | if (!plats->aggs_bottom) |
518dc859 | 4745 | { |
2c9561b5 | 4746 | struct ipcp_agg_lattice *aglat; |
c0cb5055 | 4747 | ipcp_value<tree> *val; |
2c9561b5 MJ |
4748 | for (aglat = plats->aggs; aglat; aglat = aglat->next) |
4749 | if (!aglat->bottom && aglat->values | |
4750 | /* If the following is false, the one value is in | |
4751 | known_aggs. */ | |
4752 | && (plats->aggs_contain_variable | |
c0cb5055 | 4753 | || !aglat->is_single_const ())) |
2c9561b5 MJ |
4754 | for (val = aglat->values; val; val = val->next) |
4755 | ret |= decide_about_value (node, i, aglat->offset, val, | |
44210a96 | 4756 | known_csts, known_contexts); |
cc58ceee | 4757 | } |
44210a96 MJ |
4758 | |
4759 | if (!ctxlat->bottom | |
4760 | && known_contexts[i].useless_p ()) | |
4761 | { | |
4762 | ipcp_value<ipa_polymorphic_call_context> *val; | |
4763 | for (val = ctxlat->values; val; val = val->next) | |
4764 | ret |= decide_about_value (node, i, -1, val, known_csts, | |
4765 | known_contexts); | |
4766 | } | |
4767 | ||
155c9907 | 4768 | info = IPA_NODE_REF (node); |
310bc633 | 4769 | } |
cc58ceee | 4770 | |
eb20b778 | 4771 | if (info->do_clone_for_all_contexts) |
310bc633 | 4772 | { |
eb20b778 | 4773 | struct cgraph_node *clone; |
d52f5295 | 4774 | vec<cgraph_edge *> callers; |
cc58ceee | 4775 | |
310bc633 | 4776 | if (dump_file) |
464d0118 ML |
4777 | fprintf (dump_file, " - Creating a specialized node of %s " |
4778 | "for all known contexts.\n", node->dump_name ()); | |
5e45130d | 4779 | |
d52f5295 | 4780 | callers = node->collect_callers (); |
7b668576 MJ |
4781 | find_more_scalar_values_for_callers_subset (node, known_csts, callers); |
4782 | find_more_contexts_for_caller_subset (node, &known_contexts, callers); | |
4783 | ipa_agg_replacement_value *aggvals | |
4784 | = find_aggregate_values_for_callers_subset (node, callers); | |
44210a96 MJ |
4785 | |
4786 | if (!known_contexts_useful_p (known_contexts)) | |
4787 | { | |
4788 | known_contexts.release (); | |
4789 | known_contexts = vNULL; | |
4790 | } | |
4791 | clone = create_specialized_node (node, known_csts, known_contexts, | |
7b668576 | 4792 | aggvals, callers); |
310bc633 | 4793 | info = IPA_NODE_REF (node); |
eb20b778 MJ |
4794 | info->do_clone_for_all_contexts = false; |
4795 | IPA_NODE_REF (clone)->is_all_contexts_clone = true; | |
155c9907 | 4796 | for (i = 0; i < count; i++) |
90e709fd JJ |
4797 | vec_free (known_aggs[i].items); |
4798 | known_aggs.release (); | |
310bc633 MJ |
4799 | ret = true; |
4800 | } | |
4801 | else | |
44210a96 MJ |
4802 | { |
4803 | known_csts.release (); | |
4804 | known_contexts.release (); | |
4805 | } | |
5e45130d | 4806 | |
310bc633 MJ |
4807 | return ret; |
4808 | } | |
9187e02d | 4809 | |
310bc633 | 4810 | /* Transitively mark all callees of NODE within the same SCC as not dead. */ |
3949c4a7 | 4811 | |
310bc633 MJ |
4812 | static void |
4813 | spread_undeadness (struct cgraph_node *node) | |
4814 | { | |
4815 | struct cgraph_edge *cs; | |
5e45130d | 4816 | |
310bc633 | 4817 | for (cs = node->callees; cs; cs = cs->next_callee) |
4cb13597 | 4818 | if (ipa_edge_within_scc (cs)) |
310bc633 MJ |
4819 | { |
4820 | struct cgraph_node *callee; | |
4821 | struct ipa_node_params *info; | |
129a37fc | 4822 | |
d52f5295 | 4823 | callee = cs->callee->function_symbol (NULL); |
310bc633 | 4824 | info = IPA_NODE_REF (callee); |
5e45130d | 4825 | |
310bc633 MJ |
4826 | if (info->node_dead) |
4827 | { | |
4828 | info->node_dead = 0; | |
4829 | spread_undeadness (callee); | |
4830 | } | |
4831 | } | |
4832 | } | |
4833 | ||
4834 | /* Return true if NODE has a caller from outside of its SCC that is not | |
4835 | dead. Worker callback for cgraph_for_node_and_aliases. */ | |
4836 | ||
4837 | static bool | |
4838 | has_undead_caller_from_outside_scc_p (struct cgraph_node *node, | |
155c9907 | 4839 | void *data ATTRIBUTE_UNUSED) |
310bc633 MJ |
4840 | { |
4841 | struct cgraph_edge *cs; | |
4842 | ||
4843 | for (cs = node->callers; cs; cs = cs->next_caller) | |
4844 | if (cs->caller->thunk.thunk_p | |
d52f5295 ML |
4845 | && cs->caller->call_for_symbol_thunks_and_aliases |
4846 | (has_undead_caller_from_outside_scc_p, NULL, true)) | |
310bc633 | 4847 | return true; |
4cb13597 | 4848 | else if (!ipa_edge_within_scc (cs) |
310bc633 MJ |
4849 | && !IPA_NODE_REF (cs->caller)->node_dead) |
4850 | return true; | |
4851 | return false; | |
4852 | } | |
4853 | ||
4854 | ||
4855 | /* Identify nodes within the same SCC as NODE which are no longer needed | |
4856 | because of new clones and will be removed as unreachable. */ | |
4857 | ||
4858 | static void | |
4859 | identify_dead_nodes (struct cgraph_node *node) | |
4860 | { | |
4861 | struct cgraph_node *v; | |
155c9907 | 4862 | for (v = node; v; v = ((struct ipa_dfs_info *) v->aux)->next_cycle) |
5af56ae8 | 4863 | if (v->local.local |
d52f5295 ML |
4864 | && !v->call_for_symbol_thunks_and_aliases |
4865 | (has_undead_caller_from_outside_scc_p, NULL, true)) | |
310bc633 MJ |
4866 | IPA_NODE_REF (v)->node_dead = 1; |
4867 | ||
155c9907 | 4868 | for (v = node; v; v = ((struct ipa_dfs_info *) v->aux)->next_cycle) |
310bc633 MJ |
4869 | if (!IPA_NODE_REF (v)->node_dead) |
4870 | spread_undeadness (v); | |
4871 | ||
4872 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
4873 | { | |
155c9907 | 4874 | for (v = node; v; v = ((struct ipa_dfs_info *) v->aux)->next_cycle) |
310bc633 | 4875 | if (IPA_NODE_REF (v)->node_dead) |
464d0118 | 4876 | fprintf (dump_file, " Marking node as dead: %s.\n", v->dump_name ()); |
5e45130d | 4877 | } |
310bc633 MJ |
4878 | } |
4879 | ||
4880 | /* The decision stage. Iterate over the topological order of call graph nodes | |
4881 | TOPO and make specialized clones if deemed beneficial. */ | |
4882 | ||
4883 | static void | |
11478306 | 4884 | ipcp_decision_stage (struct ipa_topo_info *topo) |
310bc633 MJ |
4885 | { |
4886 | int i; | |
4887 | ||
4888 | if (dump_file) | |
4889 | fprintf (dump_file, "\nIPA decision stage:\n\n"); | |
5e45130d | 4890 | |
310bc633 | 4891 | for (i = topo->nnodes - 1; i >= 0; i--) |
5e45130d | 4892 | { |
310bc633 MJ |
4893 | struct cgraph_node *node = topo->order[i]; |
4894 | bool change = false, iterate = true; | |
4895 | ||
4896 | while (iterate) | |
4897 | { | |
4898 | struct cgraph_node *v; | |
4899 | iterate = false; | |
155c9907 | 4900 | for (v = node; v; v = ((struct ipa_dfs_info *) v->aux)->next_cycle) |
d52f5295 | 4901 | if (v->has_gimple_body_p () |
310bc633 MJ |
4902 | && ipcp_versionable_function_p (v)) |
4903 | iterate |= decide_whether_version_node (v); | |
4904 | ||
4905 | change |= iterate; | |
4906 | } | |
4907 | if (change) | |
4908 | identify_dead_nodes (node); | |
518dc859 | 4909 | } |
518dc859 RL |
4910 | } |
4911 | ||
209ca542 PK |
4912 | /* Look up all the bits information that we have discovered and copy it over |
4913 | to the transformation summary. */ | |
4914 | ||
4915 | static void | |
4916 | ipcp_store_bits_results (void) | |
4917 | { | |
4918 | cgraph_node *node; | |
4919 | ||
4920 | FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node) | |
4921 | { | |
4922 | ipa_node_params *info = IPA_NODE_REF (node); | |
4923 | bool dumped_sth = false; | |
4924 | bool found_useful_result = false; | |
4925 | ||
4926 | if (!opt_for_fn (node->decl, flag_ipa_bit_cp)) | |
4927 | { | |
4928 | if (dump_file) | |
4929 | fprintf (dump_file, "Not considering %s for ipa bitwise propagation " | |
15bbb5cc | 4930 | "; -fipa-bit-cp: disabled.\n", |
209ca542 PK |
4931 | node->name ()); |
4932 | continue; | |
4933 | } | |
4934 | ||
4935 | if (info->ipcp_orig_node) | |
4936 | info = IPA_NODE_REF (info->ipcp_orig_node); | |
4937 | ||
4938 | unsigned count = ipa_get_param_count (info); | |
4939 | for (unsigned i = 0; i < count; i++) | |
4940 | { | |
4941 | ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i); | |
4942 | if (plats->bits_lattice.constant_p ()) | |
4943 | { | |
4944 | found_useful_result = true; | |
4945 | break; | |
4946 | } | |
4947 | } | |
4948 | ||
155c9907 JJ |
4949 | if (!found_useful_result) |
4950 | continue; | |
209ca542 | 4951 | |
155c9907 JJ |
4952 | ipcp_grow_transformations_if_necessary (); |
4953 | ipcp_transformation_summary *ts = ipcp_get_transformation_summary (node); | |
4954 | vec_safe_reserve_exact (ts->bits, count); | |
209ca542 | 4955 | |
155c9907 JJ |
4956 | for (unsigned i = 0; i < count; i++) |
4957 | { | |
4958 | ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i); | |
86cd0334 | 4959 | ipa_bits *jfbits; |
209ca542 | 4960 | |
155c9907 | 4961 | if (plats->bits_lattice.constant_p ()) |
86cd0334 MJ |
4962 | jfbits |
4963 | = ipa_get_ipa_bits_for_value (plats->bits_lattice.get_value (), | |
4964 | plats->bits_lattice.get_mask ()); | |
155c9907 | 4965 | else |
86cd0334 | 4966 | jfbits = NULL; |
209ca542 | 4967 | |
86cd0334 MJ |
4968 | ts->bits->quick_push (jfbits); |
4969 | if (!dump_file || !jfbits) | |
155c9907 JJ |
4970 | continue; |
4971 | if (!dumped_sth) | |
4972 | { | |
464d0118 ML |
4973 | fprintf (dump_file, "Propagated bits info for function %s:\n", |
4974 | node->dump_name ()); | |
155c9907 JJ |
4975 | dumped_sth = true; |
4976 | } | |
4977 | fprintf (dump_file, " param %i: value = ", i); | |
86cd0334 | 4978 | print_hex (jfbits->value, dump_file); |
155c9907 | 4979 | fprintf (dump_file, ", mask = "); |
86cd0334 | 4980 | print_hex (jfbits->mask, dump_file); |
155c9907 JJ |
4981 | fprintf (dump_file, "\n"); |
4982 | } | |
209ca542 PK |
4983 | } |
4984 | } | |
8bc5448f KV |
4985 | |
4986 | /* Look up all VR information that we have discovered and copy it over | |
4987 | to the transformation summary. */ | |
4988 | ||
4989 | static void | |
4990 | ipcp_store_vr_results (void) | |
4991 | { | |
4992 | cgraph_node *node; | |
4993 | ||
4994 | FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node) | |
155c9907 JJ |
4995 | { |
4996 | ipa_node_params *info = IPA_NODE_REF (node); | |
4997 | bool found_useful_result = false; | |
8bc5448f | 4998 | |
155c9907 JJ |
4999 | if (!opt_for_fn (node->decl, flag_ipa_vrp)) |
5000 | { | |
5001 | if (dump_file) | |
5002 | fprintf (dump_file, "Not considering %s for VR discovery " | |
5003 | "and propagate; -fipa-ipa-vrp: disabled.\n", | |
5004 | node->name ()); | |
5005 | continue; | |
5006 | } | |
8bc5448f | 5007 | |
155c9907 JJ |
5008 | if (info->ipcp_orig_node) |
5009 | info = IPA_NODE_REF (info->ipcp_orig_node); | |
8bc5448f | 5010 | |
155c9907 JJ |
5011 | unsigned count = ipa_get_param_count (info); |
5012 | for (unsigned i = 0; i < count; i++) | |
5013 | { | |
5014 | ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i); | |
5015 | if (!plats->m_value_range.bottom_p () | |
5016 | && !plats->m_value_range.top_p ()) | |
5017 | { | |
5018 | found_useful_result = true; | |
5019 | break; | |
5020 | } | |
5021 | } | |
5022 | if (!found_useful_result) | |
5023 | continue; | |
8bc5448f | 5024 | |
155c9907 JJ |
5025 | ipcp_grow_transformations_if_necessary (); |
5026 | ipcp_transformation_summary *ts = ipcp_get_transformation_summary (node); | |
5027 | vec_safe_reserve_exact (ts->m_vr, count); | |
8bc5448f | 5028 | |
155c9907 JJ |
5029 | for (unsigned i = 0; i < count; i++) |
5030 | { | |
5031 | ipcp_param_lattices *plats = ipa_get_parm_lattices (info, i); | |
5032 | ipa_vr vr; | |
8bc5448f | 5033 | |
155c9907 JJ |
5034 | if (!plats->m_value_range.bottom_p () |
5035 | && !plats->m_value_range.top_p ()) | |
5036 | { | |
5037 | vr.known = true; | |
5038 | vr.type = plats->m_value_range.m_vr.type; | |
8e6cdc90 RS |
5039 | vr.min = wi::to_wide (plats->m_value_range.m_vr.min); |
5040 | vr.max = wi::to_wide (plats->m_value_range.m_vr.max); | |
155c9907 JJ |
5041 | } |
5042 | else | |
5043 | { | |
5044 | vr.known = false; | |
5045 | vr.type = VR_VARYING; | |
5046 | vr.min = vr.max = wi::zero (INT_TYPE_SIZE); | |
5047 | } | |
5048 | ts->m_vr->quick_push (vr); | |
5049 | } | |
5050 | } | |
8bc5448f KV |
5051 | } |
5052 | ||
518dc859 | 5053 | /* The IPCP driver. */ |
310bc633 | 5054 | |
3cc1cccc | 5055 | static unsigned int |
518dc859 RL |
5056 | ipcp_driver (void) |
5057 | { | |
310bc633 | 5058 | struct cgraph_2edge_hook_list *edge_duplication_hook_holder; |
aef83682 | 5059 | struct cgraph_edge_hook_list *edge_removal_hook_holder; |
11478306 | 5060 | struct ipa_topo_info topo; |
310bc633 | 5061 | |
310bc633 MJ |
5062 | ipa_check_create_node_params (); |
5063 | ipa_check_create_edge_args (); | |
aef83682 | 5064 | grow_edge_clone_vectors (); |
155c9907 JJ |
5065 | edge_duplication_hook_holder |
5066 | = symtab->add_edge_duplication_hook (&ipcp_edge_duplication_hook, NULL); | |
5067 | edge_removal_hook_holder | |
5068 | = symtab->add_edge_removal_hook (&ipcp_edge_removal_hook, NULL); | |
aef83682 | 5069 | |
518dc859 RL |
5070 | if (dump_file) |
5071 | { | |
ca30a539 JH |
5072 | fprintf (dump_file, "\nIPA structures before propagation:\n"); |
5073 | if (dump_flags & TDF_DETAILS) | |
155c9907 | 5074 | ipa_print_all_params (dump_file); |
ca30a539 | 5075 | ipa_print_all_jump_functions (dump_file); |
518dc859 | 5076 | } |
310bc633 MJ |
5077 | |
5078 | /* Topological sort. */ | |
5079 | build_toporder_info (&topo); | |
5080 | /* Do the interprocedural propagation. */ | |
5081 | ipcp_propagate_stage (&topo); | |
5082 | /* Decide what constant propagation and cloning should be performed. */ | |
5083 | ipcp_decision_stage (&topo); | |
209ca542 PK |
5084 | /* Store results of bits propagation. */ |
5085 | ipcp_store_bits_results (); | |
8bc5448f KV |
5086 | /* Store results of value range propagation. */ |
5087 | ipcp_store_vr_results (); | |
310bc633 | 5088 | |
518dc859 | 5089 | /* Free all IPCP structures. */ |
310bc633 | 5090 | free_toporder_info (&topo); |
9771b263 | 5091 | next_edge_clone.release (); |
31b27938 | 5092 | prev_edge_clone.release (); |
3dafb85c ML |
5093 | symtab->remove_edge_removal_hook (edge_removal_hook_holder); |
5094 | symtab->remove_edge_duplication_hook (edge_duplication_hook_holder); | |
e33c6cd6 | 5095 | ipa_free_all_structures_after_ipa_cp (); |
518dc859 RL |
5096 | if (dump_file) |
5097 | fprintf (dump_file, "\nIPA constant propagation end\n"); | |
c2924966 | 5098 | return 0; |
518dc859 RL |
5099 | } |
5100 | ||
3949c4a7 MJ |
5101 | /* Initialization and computation of IPCP data structures. This is the initial |
5102 | intraprocedural analysis of functions, which gathers information to be | |
5103 | propagated later on. */ | |
5104 | ||
129a37fc JH |
5105 | static void |
5106 | ipcp_generate_summary (void) | |
5107 | { | |
3949c4a7 MJ |
5108 | struct cgraph_node *node; |
5109 | ||
129a37fc JH |
5110 | if (dump_file) |
5111 | fprintf (dump_file, "\nIPA constant propagation start:\n"); | |
129a37fc | 5112 | ipa_register_cgraph_hooks (); |
3949c4a7 | 5113 | |
c47d0034 | 5114 | FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node) |
7e729474 | 5115 | ipa_analyze_node (node); |
129a37fc JH |
5116 | } |
5117 | ||
fb3f88cc | 5118 | /* Write ipcp summary for nodes in SET. */ |
310bc633 | 5119 | |
fb3f88cc | 5120 | static void |
f27c1867 | 5121 | ipcp_write_summary (void) |
fb3f88cc | 5122 | { |
f27c1867 | 5123 | ipa_prop_write_jump_functions (); |
fb3f88cc JH |
5124 | } |
5125 | ||
5126 | /* Read ipcp summary. */ | |
310bc633 | 5127 | |
fb3f88cc JH |
5128 | static void |
5129 | ipcp_read_summary (void) | |
5130 | { | |
5131 | ipa_prop_read_jump_functions (); | |
5132 | } | |
5133 | ||
27a4cd48 DM |
5134 | namespace { |
5135 | ||
5136 | const pass_data pass_data_ipa_cp = | |
5137 | { | |
5138 | IPA_PASS, /* type */ | |
5139 | "cp", /* name */ | |
5140 | OPTGROUP_NONE, /* optinfo_flags */ | |
27a4cd48 DM |
5141 | TV_IPA_CONSTANT_PROP, /* tv_id */ |
5142 | 0, /* properties_required */ | |
5143 | 0, /* properties_provided */ | |
5144 | 0, /* properties_destroyed */ | |
5145 | 0, /* todo_flags_start */ | |
5146 | ( TODO_dump_symtab | TODO_remove_functions ), /* todo_flags_finish */ | |
518dc859 | 5147 | }; |
27a4cd48 DM |
5148 | |
5149 | class pass_ipa_cp : public ipa_opt_pass_d | |
5150 | { | |
5151 | public: | |
c3284718 RS |
5152 | pass_ipa_cp (gcc::context *ctxt) |
5153 | : ipa_opt_pass_d (pass_data_ipa_cp, ctxt, | |
5154 | ipcp_generate_summary, /* generate_summary */ | |
5155 | ipcp_write_summary, /* write_summary */ | |
5156 | ipcp_read_summary, /* read_summary */ | |
04be694e | 5157 | ipcp_write_transformation_summaries, /* |
c3284718 | 5158 | write_optimization_summary */ |
04be694e | 5159 | ipcp_read_transformation_summaries, /* |
c3284718 RS |
5160 | read_optimization_summary */ |
5161 | NULL, /* stmt_fixup */ | |
5162 | 0, /* function_transform_todo_flags_start */ | |
5163 | ipcp_transform_function, /* function_transform */ | |
5164 | NULL) /* variable_transform */ | |
27a4cd48 DM |
5165 | {} |
5166 | ||
5167 | /* opt_pass methods: */ | |
1a3d085c TS |
5168 | virtual bool gate (function *) |
5169 | { | |
5170 | /* FIXME: We should remove the optimize check after we ensure we never run | |
5171 | IPA passes when not optimizing. */ | |
2bf86c84 | 5172 | return (flag_ipa_cp && optimize) || in_lto_p; |
1a3d085c TS |
5173 | } |
5174 | ||
be55bfe6 | 5175 | virtual unsigned int execute (function *) { return ipcp_driver (); } |
27a4cd48 DM |
5176 | |
5177 | }; // class pass_ipa_cp | |
5178 | ||
5179 | } // anon namespace | |
5180 | ||
5181 | ipa_opt_pass_d * | |
5182 | make_pass_ipa_cp (gcc::context *ctxt) | |
5183 | { | |
5184 | return new pass_ipa_cp (ctxt); | |
5185 | } | |
3edf64aa DM |
5186 | |
5187 | /* Reset all state within ipa-cp.c so that we can rerun the compiler | |
5188 | within the same process. For use by toplev::finalize. */ | |
5189 | ||
5190 | void | |
5191 | ipa_cp_c_finalize (void) | |
5192 | { | |
e7a74006 | 5193 | max_count = profile_count::uninitialized (); |
3edf64aa DM |
5194 | overall_size = 0; |
5195 | max_new_size = 0; | |
3edf64aa | 5196 | } |