ipa-cp.c (constant_val_insert): Remove.
[gcc.git] / gcc / ipa-cp.c
1 /* Interprocedural constant propagation
2 Copyright (C) 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
3 Contributed by Razya Ladelsky <RAZYA@il.ibm.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 /* Interprocedural constant propagation. The aim of interprocedural constant
22 propagation (IPCP) is to find which function's argument has the same
23 constant value in each invocation throughout the whole program. For example,
24 consider the following program:
25
26 int g (int y)
27 {
28 printf ("value is %d",y);
29 }
30
31 int f (int x)
32 {
33 g (x);
34 }
35
36 int h (int y)
37 {
38 g (y);
39 }
40
41 void main (void)
42 {
43 f (3);
44 h (3);
45 }
46
47
48 The IPCP algorithm will find that g's formal argument y is always called
49 with the value 3.
50
51 The algorithm used is based on "Interprocedural Constant Propagation", by
52 Challahan David, Keith D Cooper, Ken Kennedy, Linda Torczon, Comp86, pg
53 152-161
54
55 The optimization is divided into three stages:
56
57 First stage - intraprocedural analysis
58 =======================================
59 This phase computes jump_function and modification flags.
60
61 A jump function for a callsite represents the values passed as an actual
62 arguments of a given callsite. There are three types of values:
63 Pass through - the caller's formal parameter is passed as an actual argument.
64 Constant - a constant is passed as an actual argument.
65 Unknown - neither of the above.
66
67 The jump function info, ipa_jump_func, is stored in ipa_edge_args
68 structure (defined in ipa_prop.h and pointed to by cgraph_node->aux)
69 modified_flags are defined in ipa_node_params structure
70 (defined in ipa_prop.h and pointed to by cgraph_edge->aux).
71
72 -ipcp_init_stage() is the first stage driver.
73
74 Second stage - interprocedural analysis
75 ========================================
76 This phase does the interprocedural constant propagation.
77 It computes lattices for all formal parameters in the program
78 and their value that may be:
79 TOP - unknown.
80 BOTTOM - non constant.
81 CONSTANT - constant value.
82
83 Lattice describing a formal parameter p will have a constant value if all
84 callsites invoking this function have the same constant value passed to p.
85
86 The lattices are stored in ipcp_lattice which is itself in ipa_node_params
87 structure (defined in ipa_prop.h and pointed to by cgraph_edge->aux).
88
89 -ipcp_iterate_stage() is the second stage driver.
90
91 Third phase - transformation of function code
92 ============================================
93 Propagates the constant-valued formals into the function.
94 For each function whose parameters are constants, we create its clone.
95
96 Then we process the clone in two ways:
97 1. We insert an assignment statement 'parameter = const' at the beginning
98 of the cloned function.
99 2. For read-only parameters that do not live in memory, we replace all their
100 uses with the constant.
101
102 We also need to modify some callsites to call the cloned functions instead
103 of the original ones. For a callsite passing an argument found to be a
104 constant by IPCP, there are two different cases to handle:
105 1. A constant is passed as an argument. In this case the callsite in the
106 should be redirected to call the cloned callee.
107 2. A parameter (of the caller) passed as an argument (pass through
108 argument). In such cases both the caller and the callee have clones and
109 only the callsite in the cloned caller is redirected to call to the
110 cloned callee.
111
112 This update is done in two steps: First all cloned functions are created
113 during a traversal of the call graph, during which all callsites are
114 redirected to call the cloned function. Then the callsites are traversed
115 and many calls redirected back to fit the description above.
116
117 -ipcp_insert_stage() is the third phase driver.
118
119 */
120
121 #include "config.h"
122 #include "system.h"
123 #include "coretypes.h"
124 #include "tree.h"
125 #include "target.h"
126 #include "cgraph.h"
127 #include "ipa-prop.h"
128 #include "tree-flow.h"
129 #include "tree-pass.h"
130 #include "flags.h"
131 #include "timevar.h"
132 #include "diagnostic.h"
133 #include "tree-dump.h"
134 #include "tree-inline.h"
135
136 /* Get the original node field of ipa_node_params associated with node NODE. */
137 static inline struct cgraph_node *
138 ipcp_get_orig_node (struct cgraph_node *node)
139 {
140 return IPA_NODE_REF (node)->ipcp_orig_node;
141 }
142
143 /* Return true if NODE describes a cloned/versioned function. */
144 static inline bool
145 ipcp_node_is_clone (struct cgraph_node *node)
146 {
147 return (ipcp_get_orig_node (node) != NULL);
148 }
149
150 /* Create ipa_node_params and its data structures for NEW_NODE. Set ORIG_NODE
151 as the ipcp_orig_node field in ipa_node_params. */
152 static void
153 ipcp_init_cloned_node (struct cgraph_node *orig_node,
154 struct cgraph_node *new_node)
155 {
156 ipa_check_create_node_params ();
157 IPA_NODE_REF (new_node)->ipcp_orig_node = orig_node;
158 ipa_count_formal_params (new_node);
159 ipa_create_param_decls_array (new_node);
160 }
161
162 /* Recompute all local information since node might've got new
163 direct calls after clonning. */
164 static void
165 ipcp_update_cloned_node (struct cgraph_node *new_node)
166 {
167 /* We might've introduced new direct calls. */
168 push_cfun (DECL_STRUCT_FUNCTION (new_node->decl));
169 current_function_decl = new_node->decl;
170 rebuild_cgraph_edges ();
171
172 if (flag_indirect_inlining)
173 {
174 struct cgraph_edge *cs;
175
176 ipa_check_create_node_params ();
177 ipa_count_formal_params (new_node);
178 ipa_create_param_decls_array (new_node);
179 ipa_detect_param_modifications (new_node);
180 ipa_analyze_params_uses (new_node);
181
182 for (cs = new_node->callees; cs; cs = cs->next_callee)
183 {
184 ipa_count_arguments (cs);
185 ipa_compute_jump_functions (cs);
186 }
187 }
188 pop_cfun ();
189 current_function_decl = NULL;
190 }
191
192 /* Return scale for NODE. */
193 static inline gcov_type
194 ipcp_get_node_scale (struct cgraph_node *node)
195 {
196 return IPA_NODE_REF (node)->count_scale;
197 }
198
199 /* Set COUNT as scale for NODE. */
200 static inline void
201 ipcp_set_node_scale (struct cgraph_node *node, gcov_type count)
202 {
203 IPA_NODE_REF (node)->count_scale = count;
204 }
205
206 /* Return whether LAT is a constant lattice. */
207 static inline bool
208 ipcp_lat_is_const (struct ipcp_lattice *lat)
209 {
210 if (lat->type == IPA_CONST_VALUE)
211 return true;
212 else
213 return false;
214 }
215
216 /* Return whether LAT is a constant lattice that ipa-cp can actually insert
217 into the code (i.e. constants excluding member pointers and pointers). */
218 static inline bool
219 ipcp_lat_is_insertable (struct ipcp_lattice *lat)
220 {
221 return lat->type == IPA_CONST_VALUE;
222 }
223
224 /* Return true if LAT1 and LAT2 are equal. */
225 static inline bool
226 ipcp_lats_are_equal (struct ipcp_lattice *lat1, struct ipcp_lattice *lat2)
227 {
228 gcc_assert (ipcp_lat_is_const (lat1) && ipcp_lat_is_const (lat2));
229 if (lat1->type != lat2->type)
230 return false;
231
232 if (operand_equal_p (lat1->constant, lat2->constant, 0))
233 return true;
234
235 return false;
236 }
237
238 /* Compute Meet arithmetics:
239 Meet (IPA_BOTTOM, x) = IPA_BOTTOM
240 Meet (IPA_TOP,x) = x
241 Meet (const_a,const_b) = IPA_BOTTOM, if const_a != const_b.
242 MEET (const_a,const_b) = const_a, if const_a == const_b.*/
243 static void
244 ipa_lattice_meet (struct ipcp_lattice *res, struct ipcp_lattice *lat1,
245 struct ipcp_lattice *lat2)
246 {
247 if (lat1->type == IPA_BOTTOM || lat2->type == IPA_BOTTOM)
248 {
249 res->type = IPA_BOTTOM;
250 return;
251 }
252 if (lat1->type == IPA_TOP)
253 {
254 res->type = lat2->type;
255 res->constant = lat2->constant;
256 return;
257 }
258 if (lat2->type == IPA_TOP)
259 {
260 res->type = lat1->type;
261 res->constant = lat1->constant;
262 return;
263 }
264 if (!ipcp_lats_are_equal (lat1, lat2))
265 {
266 res->type = IPA_BOTTOM;
267 return;
268 }
269 res->type = lat1->type;
270 res->constant = lat1->constant;
271 }
272
273 /* Return the lattice corresponding to the Ith formal parameter of the function
274 described by INFO. */
275 static inline struct ipcp_lattice *
276 ipcp_get_ith_lattice (struct ipa_node_params *info, int i)
277 {
278 return &(info->ipcp_lattices[i]);
279 }
280
281 /* Given the jump function JFUNC, compute the lattice LAT that describes the
282 value coming down the callsite. INFO describes the caller node so that
283 pass-through jump functions can be evaluated. */
284 static void
285 ipcp_lattice_from_jfunc (struct ipa_node_params *info, struct ipcp_lattice *lat,
286 struct ipa_jump_func *jfunc)
287 {
288 if (jfunc->type == IPA_CONST)
289 {
290 lat->type = IPA_CONST_VALUE;
291 lat->constant = jfunc->value.constant;
292 }
293 else if (jfunc->type == IPA_PASS_THROUGH)
294 {
295 struct ipcp_lattice *caller_lat;
296
297 caller_lat = ipcp_get_ith_lattice (info, jfunc->value.formal_id);
298 lat->type = caller_lat->type;
299 lat->constant = caller_lat->constant;
300 }
301 else
302 lat->type = IPA_BOTTOM;
303 }
304
305 /* True when OLD_LAT and NEW_LAT values are not the same. */
306
307 static bool
308 ipcp_lattice_changed (struct ipcp_lattice *old_lat,
309 struct ipcp_lattice *new_lat)
310 {
311 if (old_lat->type == new_lat->type)
312 {
313 if (!ipcp_lat_is_const (old_lat))
314 return false;
315 if (ipcp_lats_are_equal (old_lat, new_lat))
316 return false;
317 }
318 return true;
319 }
320
321 /* Print all ipcp_lattices of all functions to F. */
322 static void
323 ipcp_print_all_lattices (FILE * f)
324 {
325 struct cgraph_node *node;
326 int i, count;
327
328 fprintf (f, "\nLATTICE PRINT\n");
329 for (node = cgraph_nodes; node; node = node->next)
330 {
331 struct ipa_node_params *info;
332
333 if (!node->analyzed)
334 continue;
335 info = IPA_NODE_REF (node);
336 fprintf (f, "Printing lattices %s:\n", cgraph_node_name (node));
337 count = ipa_get_param_count (info);
338 for (i = 0; i < count; i++)
339 {
340 struct ipcp_lattice *lat = ipcp_get_ith_lattice (info, i);
341
342 fprintf (f, " param [%d]: ", i);
343 if (lat->type == IPA_CONST_VALUE)
344 {
345 fprintf (f, "type is CONST ");
346 print_generic_expr (f, lat->constant, 0);
347 fprintf (f, "\n");
348 }
349 else if (lat->type == IPA_TOP)
350 fprintf (f, "type is TOP\n");
351 else
352 fprintf (f, "type is BOTTOM\n");
353 }
354 }
355 }
356
357 /* Initialize ipcp_lattices array. The lattices corresponding to supported
358 types (integers, real types and Fortran constants defined as const_decls)
359 are initialized to IPA_TOP, the rest of them to IPA_BOTTOM. */
360 static void
361 ipcp_initialize_node_lattices (struct cgraph_node *node)
362 {
363 int i;
364 struct ipa_node_params *info = IPA_NODE_REF (node);
365
366 info->ipcp_lattices = XCNEWVEC (struct ipcp_lattice,
367 ipa_get_param_count (info));
368 for (i = 0; i < ipa_get_param_count (info) ; i++)
369 ipcp_get_ith_lattice (info, i)->type = IPA_TOP;
370 }
371
372 /* build INTEGER_CST tree with type TREE_TYPE and value according to LAT.
373 Return the tree. */
374 static tree
375 build_const_val (struct ipcp_lattice *lat, tree tree_type)
376 {
377 tree val;
378
379 gcc_assert (ipcp_lat_is_const (lat));
380 val = lat->constant;
381
382 if (!useless_type_conversion_p (tree_type, TREE_TYPE (val)))
383 {
384 if (fold_convertible_p (tree_type, val))
385 return fold_build1 (NOP_EXPR, tree_type, val);
386 else
387 return fold_build1 (VIEW_CONVERT_EXPR, tree_type, val);
388 }
389 return val;
390 }
391
392 /* Compute the proper scale for NODE. It is the ratio between the number of
393 direct calls (represented on the incoming cgraph_edges) and sum of all
394 invocations of NODE (represented as count in cgraph_node). */
395 static void
396 ipcp_compute_node_scale (struct cgraph_node *node)
397 {
398 gcov_type sum;
399 struct cgraph_edge *cs;
400
401 sum = 0;
402 /* Compute sum of all counts of callers. */
403 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
404 sum += cs->count;
405 if (node->count == 0)
406 ipcp_set_node_scale (node, 0);
407 else
408 ipcp_set_node_scale (node, sum * REG_BR_PROB_BASE / node->count);
409 }
410
411 /* Initialization and computation of IPCP data structures. This is the initial
412 intraprocedural analysis of functions, which gathers information to be
413 propagated later on. */
414 static void
415 ipcp_init_stage (void)
416 {
417 struct cgraph_node *node;
418 struct cgraph_edge *cs;
419
420 for (node = cgraph_nodes; node; node = node->next)
421 {
422 if (!node->analyzed)
423 continue;
424 /* Unreachable nodes should have been eliminated before ipcp. */
425 gcc_assert (node->needed || node->reachable);
426
427 ipa_count_formal_params (node);
428 ipa_create_param_decls_array (node);
429 ipcp_initialize_node_lattices (node);
430 ipa_detect_param_modifications (node);
431 ipcp_compute_node_scale (node);
432 }
433 for (node = cgraph_nodes; node; node = node->next)
434 {
435 if (!node->analyzed)
436 continue;
437 /* building jump functions */
438 for (cs = node->callees; cs; cs = cs->next_callee)
439 {
440 if (!cs->callee->analyzed)
441 continue;
442 ipa_count_arguments (cs);
443 if (ipa_get_cs_argument_count (IPA_EDGE_REF (cs))
444 != ipa_get_param_count (IPA_NODE_REF (cs->callee)))
445 {
446 /* Handle cases of functions with
447 a variable number of parameters. */
448 ipa_set_called_with_variable_arg (IPA_NODE_REF (cs->callee));
449 if (flag_indirect_inlining)
450 ipa_compute_jump_functions (cs);
451 }
452 else
453 ipa_compute_jump_functions (cs);
454 }
455 }
456 }
457
458 /* Return true if there are some formal parameters whose value is IPA_TOP (in
459 the whole compilation unit). Change their values to IPA_BOTTOM, since they
460 most probably get their values from outside of this compilation unit. */
461 static bool
462 ipcp_change_tops_to_bottom (void)
463 {
464 int i, count;
465 struct cgraph_node *node;
466 bool prop_again;
467
468 prop_again = false;
469 for (node = cgraph_nodes; node; node = node->next)
470 {
471 struct ipa_node_params *info = IPA_NODE_REF (node);
472 count = ipa_get_param_count (info);
473 for (i = 0; i < count; i++)
474 {
475 struct ipcp_lattice *lat = ipcp_get_ith_lattice (info, i);
476 if (lat->type == IPA_TOP)
477 {
478 prop_again = true;
479 lat->type = IPA_BOTTOM;
480 }
481 }
482 }
483 return prop_again;
484 }
485
486 /* Interprocedural analysis. The algorithm propagates constants from the
487 caller's parameters to the callee's arguments. */
488 static void
489 ipcp_propagate_stage (void)
490 {
491 int i;
492 struct ipcp_lattice inc_lat = { IPA_BOTTOM, NULL };
493 struct ipcp_lattice new_lat = { IPA_BOTTOM, NULL };
494 struct ipcp_lattice *dest_lat;
495 struct cgraph_edge *cs;
496 struct ipa_jump_func *jump_func;
497 struct ipa_func_list *wl;
498 int count;
499
500 ipa_check_create_node_params ();
501 ipa_check_create_edge_args ();
502 /* Initialize worklist to contain all functions. */
503 wl = ipa_init_func_list ();
504 while (wl)
505 {
506 struct cgraph_node *node = ipa_pop_func_from_list (&wl);
507 struct ipa_node_params *info = IPA_NODE_REF (node);
508
509 for (cs = node->callees; cs; cs = cs->next_callee)
510 {
511 struct ipa_node_params *callee_info = IPA_NODE_REF (cs->callee);
512 struct ipa_edge_args *args = IPA_EDGE_REF (cs);
513
514 if (ipa_is_called_with_var_arguments (callee_info))
515 continue;
516
517 count = ipa_get_cs_argument_count (args);
518 for (i = 0; i < count; i++)
519 {
520 jump_func = ipa_get_ith_jump_func (args, i);
521 ipcp_lattice_from_jfunc (info, &inc_lat, jump_func);
522 dest_lat = ipcp_get_ith_lattice (callee_info, i);
523 ipa_lattice_meet (&new_lat, &inc_lat, dest_lat);
524 if (ipcp_lattice_changed (&new_lat, dest_lat))
525 {
526 dest_lat->type = new_lat.type;
527 dest_lat->constant = new_lat.constant;
528 ipa_push_func_to_list (&wl, cs->callee);
529 }
530 }
531 }
532 }
533 }
534
535 /* Call the constant propagation algorithm and re-call it if necessary
536 (if there are undetermined values left). */
537 static void
538 ipcp_iterate_stage (void)
539 {
540 ipcp_propagate_stage ();
541 if (ipcp_change_tops_to_bottom ())
542 /* Some lattices have changed from IPA_TOP to IPA_BOTTOM.
543 This change should be propagated. */
544 ipcp_propagate_stage ();
545 }
546
547 /* Check conditions to forbid constant insertion to function described by
548 NODE. */
549 static inline bool
550 ipcp_node_not_modifiable_p (struct cgraph_node *node)
551 {
552 /* ??? Handle pending sizes case. */
553 if (DECL_UNINLINABLE (node->decl))
554 return true;
555 return false;
556 }
557
558 /* Print count scale data structures. */
559 static void
560 ipcp_function_scale_print (FILE * f)
561 {
562 struct cgraph_node *node;
563
564 for (node = cgraph_nodes; node; node = node->next)
565 {
566 if (!node->analyzed)
567 continue;
568 fprintf (f, "printing scale for %s: ", cgraph_node_name (node));
569 fprintf (f, "value is " HOST_WIDE_INT_PRINT_DEC
570 " \n", (HOST_WIDE_INT) ipcp_get_node_scale (node));
571 }
572 }
573
574 /* Print counts of all cgraph nodes. */
575 static void
576 ipcp_print_func_profile_counts (FILE * f)
577 {
578 struct cgraph_node *node;
579
580 for (node = cgraph_nodes; node; node = node->next)
581 {
582 fprintf (f, "function %s: ", cgraph_node_name (node));
583 fprintf (f, "count is " HOST_WIDE_INT_PRINT_DEC
584 " \n", (HOST_WIDE_INT) node->count);
585 }
586 }
587
588 /* Print counts of all cgraph edges. */
589 static void
590 ipcp_print_call_profile_counts (FILE * f)
591 {
592 struct cgraph_node *node;
593 struct cgraph_edge *cs;
594
595 for (node = cgraph_nodes; node; node = node->next)
596 {
597 for (cs = node->callees; cs; cs = cs->next_callee)
598 {
599 fprintf (f, "%s -> %s ", cgraph_node_name (cs->caller),
600 cgraph_node_name (cs->callee));
601 fprintf (f, "count is " HOST_WIDE_INT_PRINT_DEC " \n",
602 (HOST_WIDE_INT) cs->count);
603 }
604 }
605 }
606
607 /* Print all counts and probabilities of cfg edges of all functions. */
608 static void
609 ipcp_print_edge_profiles (FILE * f)
610 {
611 struct cgraph_node *node;
612 basic_block bb;
613 edge_iterator ei;
614 edge e;
615
616 for (node = cgraph_nodes; node; node = node->next)
617 {
618 fprintf (f, "function %s: \n", cgraph_node_name (node));
619 if (node->analyzed)
620 {
621 bb =
622 ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (node->decl));
623 fprintf (f, "ENTRY: ");
624 fprintf (f, " " HOST_WIDE_INT_PRINT_DEC
625 " %d\n", (HOST_WIDE_INT) bb->count, bb->frequency);
626
627 if (bb->succs)
628 FOR_EACH_EDGE (e, ei, bb->succs)
629 {
630 if (e->dest ==
631 EXIT_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION
632 (node->decl)))
633 fprintf (f, "edge ENTRY -> EXIT, Count");
634 else
635 fprintf (f, "edge ENTRY -> %d, Count", e->dest->index);
636 fprintf (f, " " HOST_WIDE_INT_PRINT_DEC
637 " Prob %d\n", (HOST_WIDE_INT) e->count,
638 e->probability);
639 }
640 FOR_EACH_BB_FN (bb, DECL_STRUCT_FUNCTION (node->decl))
641 {
642 fprintf (f, "bb[%d]: ", bb->index);
643 fprintf (f, " " HOST_WIDE_INT_PRINT_DEC
644 " %d\n", (HOST_WIDE_INT) bb->count, bb->frequency);
645 FOR_EACH_EDGE (e, ei, bb->succs)
646 {
647 if (e->dest ==
648 EXIT_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION
649 (node->decl)))
650 fprintf (f, "edge %d -> EXIT, Count", e->src->index);
651 else
652 fprintf (f, "edge %d -> %d, Count", e->src->index,
653 e->dest->index);
654 fprintf (f, " " HOST_WIDE_INT_PRINT_DEC " Prob %d\n",
655 (HOST_WIDE_INT) e->count, e->probability);
656 }
657 }
658 }
659 }
660 }
661
662 /* Print counts and frequencies for all basic blocks of all functions. */
663 static void
664 ipcp_print_bb_profiles (FILE * f)
665 {
666 basic_block bb;
667 struct cgraph_node *node;
668
669 for (node = cgraph_nodes; node; node = node->next)
670 {
671 fprintf (f, "function %s: \n", cgraph_node_name (node));
672 if (node->analyzed)
673 {
674 bb =
675 ENTRY_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (node->decl));
676 fprintf (f, "ENTRY: Count");
677 fprintf (f, " " HOST_WIDE_INT_PRINT_DEC
678 " Frequency %d\n", (HOST_WIDE_INT) bb->count,
679 bb->frequency);
680
681 FOR_EACH_BB_FN (bb, DECL_STRUCT_FUNCTION (node->decl))
682 {
683 fprintf (f, "bb[%d]: Count", bb->index);
684 fprintf (f, " " HOST_WIDE_INT_PRINT_DEC
685 " Frequency %d\n", (HOST_WIDE_INT) bb->count,
686 bb->frequency);
687 }
688 bb =
689 EXIT_BLOCK_PTR_FOR_FUNCTION (DECL_STRUCT_FUNCTION (node->decl));
690 fprintf (f, "EXIT: Count");
691 fprintf (f, " " HOST_WIDE_INT_PRINT_DEC
692 " Frequency %d\n", (HOST_WIDE_INT) bb->count,
693 bb->frequency);
694
695 }
696 }
697 }
698
699 /* Print all IPCP data structures to F. */
700 static void
701 ipcp_print_all_structures (FILE * f)
702 {
703 ipcp_print_all_lattices (f);
704 ipcp_function_scale_print (f);
705 ipa_print_all_tree_maps (f);
706 ipa_print_all_param_flags (f);
707 ipa_print_all_jump_functions (f);
708 }
709
710 /* Print profile info for all functions. */
711 static void
712 ipcp_print_profile_data (FILE * f)
713 {
714 fprintf (f, "\nNODE COUNTS :\n");
715 ipcp_print_func_profile_counts (f);
716 fprintf (f, "\nCS COUNTS stage:\n");
717 ipcp_print_call_profile_counts (f);
718 fprintf (f, "\nBB COUNTS and FREQUENCIES :\n");
719 ipcp_print_bb_profiles (f);
720 fprintf (f, "\nCFG EDGES COUNTS and PROBABILITIES :\n");
721 ipcp_print_edge_profiles (f);
722 }
723
724 /* Build and initialize ipa_replace_map struct according to LAT. This struct is
725 processed by versioning, which operates according to the flags set.
726 PARM_TREE is the formal parameter found to be constant. LAT represents the
727 constant. */
728 static struct ipa_replace_map *
729 ipcp_create_replace_map (tree parm_tree, struct ipcp_lattice *lat)
730 {
731 struct ipa_replace_map *replace_map;
732 tree const_val;
733
734 replace_map = XCNEW (struct ipa_replace_map);
735 if (dump_file)
736 fprintf (dump_file, "replacing param with const\n");
737 const_val = build_const_val (lat, TREE_TYPE (parm_tree));
738 replace_map->old_tree = parm_tree;
739 replace_map->new_tree = const_val;
740 replace_map->replace_p = true;
741 replace_map->ref_p = false;
742
743 return replace_map;
744 }
745
746 /* Return true if this callsite should be redirected to the original callee
747 (instead of the cloned one). */
748 static bool
749 ipcp_need_redirect_p (struct cgraph_edge *cs)
750 {
751 struct ipa_node_params *orig_callee_info;
752 int i, count;
753 struct ipa_jump_func *jump_func;
754
755 orig_callee_info = IPA_NODE_REF (ipcp_get_orig_node (cs->callee));
756 count = ipa_get_param_count (orig_callee_info);
757 for (i = 0; i < count; i++)
758 {
759 struct ipcp_lattice *lat = ipcp_get_ith_lattice (orig_callee_info, i);
760 if (ipcp_lat_is_const (lat))
761 {
762 jump_func = ipa_get_ith_jump_func (IPA_EDGE_REF (cs), i);
763 if (jump_func->type != IPA_CONST)
764 return true;
765 }
766 }
767
768 return false;
769 }
770
771 /* Fix the callsites and the call graph after function cloning was done. */
772 static void
773 ipcp_update_callgraph (void)
774 {
775 struct cgraph_node *node, *orig_callee;
776 struct cgraph_edge *cs;
777
778 for (node = cgraph_nodes; node; node = node->next)
779 {
780 /* want to fix only original nodes */
781 if (!node->analyzed || ipcp_node_is_clone (node))
782 continue;
783 for (cs = node->callees; cs; cs = cs->next_callee)
784 if (ipcp_node_is_clone (cs->callee))
785 {
786 /* Callee is a cloned node */
787 orig_callee = ipcp_get_orig_node (cs->callee);
788 if (ipcp_need_redirect_p (cs))
789 {
790 cgraph_redirect_edge_callee (cs, orig_callee);
791 gimple_call_set_fndecl (cs->call_stmt, orig_callee->decl);
792 }
793 }
794 }
795 }
796
797 /* Update all cfg basic blocks in NODE according to SCALE. */
798 static void
799 ipcp_update_bb_counts (struct cgraph_node *node, gcov_type scale)
800 {
801 basic_block bb;
802
803 FOR_ALL_BB_FN (bb, DECL_STRUCT_FUNCTION (node->decl))
804 bb->count = bb->count * scale / REG_BR_PROB_BASE;
805 }
806
807 /* Update all cfg edges in NODE according to SCALE. */
808 static void
809 ipcp_update_edges_counts (struct cgraph_node *node, gcov_type scale)
810 {
811 basic_block bb;
812 edge_iterator ei;
813 edge e;
814
815 FOR_ALL_BB_FN (bb, DECL_STRUCT_FUNCTION (node->decl))
816 FOR_EACH_EDGE (e, ei, bb->succs)
817 e->count = e->count * scale / REG_BR_PROB_BASE;
818 }
819
820 /* Update profiling info for versioned functions and the functions they were
821 versioned from. */
822 static void
823 ipcp_update_profiling (void)
824 {
825 struct cgraph_node *node, *orig_node;
826 gcov_type scale, scale_complement;
827 struct cgraph_edge *cs;
828
829 for (node = cgraph_nodes; node; node = node->next)
830 {
831 if (ipcp_node_is_clone (node))
832 {
833 orig_node = ipcp_get_orig_node (node);
834 scale = ipcp_get_node_scale (orig_node);
835 node->count = orig_node->count * scale / REG_BR_PROB_BASE;
836 scale_complement = REG_BR_PROB_BASE - scale;
837 orig_node->count =
838 orig_node->count * scale_complement / REG_BR_PROB_BASE;
839 for (cs = node->callees; cs; cs = cs->next_callee)
840 cs->count = cs->count * scale / REG_BR_PROB_BASE;
841 for (cs = orig_node->callees; cs; cs = cs->next_callee)
842 cs->count = cs->count * scale_complement / REG_BR_PROB_BASE;
843 ipcp_update_bb_counts (node, scale);
844 ipcp_update_bb_counts (orig_node, scale_complement);
845 ipcp_update_edges_counts (node, scale);
846 ipcp_update_edges_counts (orig_node, scale_complement);
847 }
848 }
849 }
850
851 /* Propagate the constant parameters found by ipcp_iterate_stage()
852 to the function's code. */
853 static void
854 ipcp_insert_stage (void)
855 {
856 struct cgraph_node *node, *node1 = NULL;
857 int i, const_param;
858 VEC (cgraph_edge_p, heap) * redirect_callers;
859 varray_type replace_trees;
860 struct cgraph_edge *cs;
861 int node_callers, count;
862 tree parm_tree;
863 struct ipa_replace_map *replace_param;
864
865 ipa_check_create_node_params ();
866 ipa_check_create_edge_args ();
867
868 for (node = cgraph_nodes; node; node = node->next)
869 {
870 struct ipa_node_params *info;
871 /* Propagation of the constant is forbidden in certain conditions. */
872 if (!node->analyzed || ipcp_node_not_modifiable_p (node))
873 continue;
874 info = IPA_NODE_REF (node);
875 if (ipa_is_called_with_var_arguments (info))
876 continue;
877 const_param = 0;
878 count = ipa_get_param_count (info);
879 for (i = 0; i < count; i++)
880 {
881 struct ipcp_lattice *lat = ipcp_get_ith_lattice (info, i);
882 tree parm_tree = ipa_get_ith_param (info, i);
883 if (ipcp_lat_is_insertable (lat)
884 /* Do not count obviously unused arguments. */
885 && (!is_gimple_reg (parm_tree)
886 || gimple_default_def (DECL_STRUCT_FUNCTION (node->decl), parm_tree)))
887 const_param++;
888 }
889 if (const_param == 0)
890 continue;
891 VARRAY_GENERIC_PTR_INIT (replace_trees, const_param, "replace_trees");
892 for (i = 0; i < count; i++)
893 {
894 struct ipcp_lattice *lat = ipcp_get_ith_lattice (info, i);
895 if (lat->type == IPA_CONST_VALUE)
896 {
897 parm_tree = ipa_get_ith_param (info, i);
898 replace_param =
899 ipcp_create_replace_map (parm_tree, lat);
900 VARRAY_PUSH_GENERIC_PTR (replace_trees, replace_param);
901 }
902 }
903 /* Compute how many callers node has. */
904 node_callers = 0;
905 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
906 node_callers++;
907 redirect_callers = VEC_alloc (cgraph_edge_p, heap, node_callers);
908 for (cs = node->callers; cs != NULL; cs = cs->next_caller)
909 VEC_quick_push (cgraph_edge_p, redirect_callers, cs);
910 /* Redirecting all the callers of the node to the
911 new versioned node. */
912 node1 =
913 cgraph_function_versioning (node, redirect_callers, replace_trees);
914 VEC_free (cgraph_edge_p, heap, redirect_callers);
915 VARRAY_CLEAR (replace_trees);
916 if (node1 == NULL)
917 continue;
918 if (dump_file)
919 fprintf (dump_file, "versioned function %s\n",
920 cgraph_node_name (node));
921 ipcp_init_cloned_node (node, node1);
922 /* We've possibly introduced direct calls. */
923 ipcp_update_cloned_node (node1);
924
925 if (dump_file)
926 dump_function_to_file (node1->decl, dump_file, dump_flags);
927 }
928 ipcp_update_callgraph ();
929 ipcp_update_profiling ();
930 }
931
932 /* The IPCP driver. */
933 static unsigned int
934 ipcp_driver (void)
935 {
936 /* 2. Do the interprocedural propagation. */
937 ipcp_iterate_stage ();
938 if (dump_file)
939 {
940 fprintf (dump_file, "\nIPA structures after propagation:\n");
941 ipcp_print_all_structures (dump_file);
942 fprintf (dump_file, "\nProfiling info before insert stage:\n");
943 ipcp_print_profile_data (dump_file);
944 }
945 /* 3. Insert the constants found to the functions. */
946 ipcp_insert_stage ();
947 if (dump_file)
948 {
949 fprintf (dump_file, "\nProfiling info after insert stage:\n");
950 ipcp_print_profile_data (dump_file);
951 }
952 /* Free all IPCP structures. */
953 free_all_ipa_structures_after_ipa_cp ();
954 if (dump_file)
955 fprintf (dump_file, "\nIPA constant propagation end\n");
956 cgraph_remove_unreachable_nodes (true, NULL);
957 return 0;
958 }
959
960 /* Note function body size. */
961 static void
962 ipcp_generate_summary (void)
963 {
964 if (dump_file)
965 fprintf (dump_file, "\nIPA constant propagation start:\n");
966 ipa_check_create_node_params ();
967 ipa_check_create_edge_args ();
968 ipa_register_cgraph_hooks ();
969 /* 1. Call the init stage to initialize
970 the ipa_node_params and ipa_edge_args structures. */
971 ipcp_init_stage ();
972 if (dump_file)
973 {
974 fprintf (dump_file, "\nIPA structures before propagation:\n");
975 ipcp_print_all_structures (dump_file);
976 }
977 }
978
979 /* Gate for IPCP optimization. */
980 static bool
981 cgraph_gate_cp (void)
982 {
983 return flag_ipa_cp;
984 }
985
986 struct ipa_opt_pass pass_ipa_cp =
987 {
988 {
989 IPA_PASS,
990 "cp", /* name */
991 cgraph_gate_cp, /* gate */
992 ipcp_driver, /* execute */
993 NULL, /* sub */
994 NULL, /* next */
995 0, /* static_pass_number */
996 TV_IPA_CONSTANT_PROP, /* tv_id */
997 0, /* properties_required */
998 PROP_trees, /* properties_provided */
999 0, /* properties_destroyed */
1000 0, /* todo_flags_start */
1001 TODO_dump_cgraph | TODO_dump_func /* todo_flags_finish */
1002 },
1003 ipcp_generate_summary, /* generate_summary */
1004 NULL, /* write_summary */
1005 NULL, /* read_summary */
1006 NULL, /* function_read_summary */
1007 0, /* TODOs */
1008 NULL, /* function_transform */
1009 NULL, /* variable_transform */
1010 };