2 Copyright (C) 2003-2024 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
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
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
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/>. */
23 #include "coretypes.h"
28 #include "alloc-pool.h"
29 #include "symbol-summary.h"
33 #include "ipa-fnsummary.h"
35 #include "fold-const.h"
36 #include "tree-pretty-print.h"
39 #include "data-streamer.h"
42 /* Check whether two set of operations have same effects. */
44 expr_eval_ops_equal_p (expr_eval_ops ops1
, expr_eval_ops ops2
)
48 if (!ops2
|| ops1
->length () != ops2
->length ())
51 for (unsigned i
= 0; i
< ops1
->length (); i
++)
53 expr_eval_op
&op1
= (*ops1
)[i
];
54 expr_eval_op
&op2
= (*ops2
)[i
];
56 if (op1
.code
!= op2
.code
57 || op1
.index
!= op2
.index
58 || !vrp_operand_equal_p (op1
.val
[0], op2
.val
[0])
59 || !vrp_operand_equal_p (op1
.val
[1], op2
.val
[1])
60 || !types_compatible_p (op1
.type
, op2
.type
))
68 /* Add clause CLAUSE into the predicate P.
69 When CONDITIONS is NULL do not perform checking whether NEW_CLAUSE
70 is obviously true. This is useful only when NEW_CLAUSE is known to be
74 ipa_predicate::add_clause (conditions conditions
, clause_t new_clause
)
85 /* False clause makes the whole predicate false. Kill the other variants. */
86 if (new_clause
== (1 << ipa_predicate::false_condition
))
94 /* No one should be silly enough to add false into nontrivial clauses. */
95 gcc_checking_assert (!(new_clause
& (1 << ipa_predicate::false_condition
)));
97 /* Look where to insert the new_clause. At the same time prune out
98 new_clauses of P that are implied by the new new_clause and thus
100 for (i
= 0, i2
= 0; i
<= max_clauses
; i
++)
102 m_clause
[i2
] = m_clause
[i
];
107 /* If m_clause[i] implies new_clause, there is nothing to add. */
108 if ((m_clause
[i
] & new_clause
) == m_clause
[i
])
110 /* We had nothing to add, none of clauses should've become
112 gcc_checking_assert (i
== i2
);
116 if (m_clause
[i
] < new_clause
&& insert_here
< 0)
119 /* If new_clause implies clause[i], then clause[i] becomes redundant.
120 Otherwise the clause[i] has to stay. */
121 if ((m_clause
[i
] & new_clause
) != new_clause
)
125 /* Look for clauses that are obviously true. I.e.
126 op0 == 5 || op0 != 5. */
128 for (c1
= ipa_predicate::first_dynamic_condition
;
129 c1
< num_conditions
; c1
++)
132 if (!(new_clause
& (1 << c1
)))
134 cc1
= &(*conditions
)[c1
- ipa_predicate::first_dynamic_condition
];
135 /* We have no way to represent !changed and !is_not_constant
136 and thus there is no point for looking for them. */
137 if (cc1
->code
== changed
|| cc1
->code
== is_not_constant
|| cc1
->code
== not_sra_candidate
)
139 for (c2
= c1
+ 1; c2
< num_conditions
; c2
++)
140 if (new_clause
& (1 << c2
))
143 &(*conditions
)[c2
- ipa_predicate::first_dynamic_condition
];
144 if (cc1
->operand_num
== cc2
->operand_num
145 && vrp_operand_equal_p (cc1
->val
, cc2
->val
)
146 && cc2
->code
!= is_not_constant
147 && cc2
->code
!= not_sra_candidate
148 && cc2
->code
!= changed
149 && expr_eval_ops_equal_p (cc1
->param_ops
, cc2
->param_ops
)
150 && cc2
->agg_contents
== cc1
->agg_contents
151 && cc2
->by_ref
== cc1
->by_ref
152 && types_compatible_p (cc2
->type
, cc1
->type
)
153 && cc1
->code
== invert_tree_comparison (cc2
->code
,
154 HONOR_NANS (cc1
->val
)))
160 /* We run out of variants. Be conservative in positive direction. */
161 if (i2
== max_clauses
)
163 /* Keep clauses in decreasing order. This makes equivalence testing easy. */
164 m_clause
[i2
+ 1] = 0;
165 if (insert_here
>= 0)
166 for (; i2
> insert_here
; i2
--)
167 m_clause
[i2
] = m_clause
[i2
- 1];
170 m_clause
[insert_here
] = new_clause
;
177 ipa_predicate::operator &= (const ipa_predicate
&p
)
179 /* Avoid busy work. */
180 if (p
== false || *this == true)
185 if (*this == false || p
== true || this == &p
)
190 /* See how far ipa_predicates match. */
191 for (i
= 0; m_clause
[i
] && m_clause
[i
] == p
.m_clause
[i
]; i
++)
193 gcc_checking_assert (i
< max_clauses
);
196 /* Combine the ipa_predicates rest. */
197 for (; p
.m_clause
[i
]; i
++)
199 gcc_checking_assert (i
< max_clauses
);
200 add_clause (NULL
, p
.m_clause
[i
]);
207 /* Return THIS | P2. */
210 ipa_predicate::or_with (conditions conditions
,
211 const ipa_predicate
&p
) const
213 /* Avoid busy work. */
214 if (p
== false || *this == true || *this == p
)
216 if (*this == false || p
== true)
219 /* OK, combine the predicates. */
220 ipa_predicate out
= true;
222 for (int i
= 0; m_clause
[i
]; i
++)
223 for (int j
= 0; p
.m_clause
[j
]; j
++)
225 gcc_checking_assert (i
< max_clauses
&& j
< max_clauses
);
226 out
.add_clause (conditions
, m_clause
[i
] | p
.m_clause
[j
]);
232 /* Having partial truth assignment in POSSIBLE_TRUTHS, return false
233 if predicate P is known to be false. */
236 ipa_predicate::evaluate (clause_t possible_truths
) const
240 /* True remains true. */
244 gcc_assert (!(possible_truths
& (1 << ipa_predicate::false_condition
)));
246 /* See if we can find clause we can disprove. */
247 for (i
= 0; m_clause
[i
]; i
++)
249 gcc_checking_assert (i
< max_clauses
);
250 if (!(m_clause
[i
] & possible_truths
))
256 /* Return the probability in range 0...REG_BR_PROB_BASE that the predicated
257 instruction will be recomputed per invocation of the inlined call. */
260 ipa_predicate::probability (conditions conds
,
261 clause_t possible_truths
,
262 vec
<inline_param_summary
> inline_param_summary
) const
265 int combined_prob
= REG_BR_PROB_BASE
;
267 /* True remains true. */
269 return REG_BR_PROB_BASE
;
274 gcc_assert (!(possible_truths
& (1 << ipa_predicate::false_condition
)));
276 /* See if we can find clause we can disprove. */
277 for (i
= 0; m_clause
[i
]; i
++)
279 gcc_checking_assert (i
< max_clauses
);
280 if (!(m_clause
[i
] & possible_truths
))
286 if (!inline_param_summary
.exists ())
287 return REG_BR_PROB_BASE
;
288 for (i2
= 0; i2
< num_conditions
; i2
++)
289 if ((m_clause
[i
] & possible_truths
) & (1 << i2
))
291 if (i2
>= ipa_predicate::first_dynamic_condition
)
294 &(*conds
)[i2
- ipa_predicate::first_dynamic_condition
];
295 if (c
->code
== ipa_predicate::changed
297 (int) inline_param_summary
.length ()))
300 inline_param_summary
[c
->operand_num
].change_prob
;
301 this_prob
= MAX (this_prob
, iprob
);
304 this_prob
= REG_BR_PROB_BASE
;
307 this_prob
= REG_BR_PROB_BASE
;
309 combined_prob
= MIN (this_prob
, combined_prob
);
314 return combined_prob
;
318 /* Dump conditional COND. */
321 dump_condition (FILE *f
, conditions conditions
, int cond
)
324 if (cond
== ipa_predicate::false_condition
)
325 fprintf (f
, "false");
326 else if (cond
== ipa_predicate::not_inlined_condition
)
327 fprintf (f
, "not inlined");
330 c
= &(*conditions
)[cond
- ipa_predicate::first_dynamic_condition
];
331 fprintf (f
, "op%i", c
->operand_num
);
333 fprintf (f
, "[%soffset: " HOST_WIDE_INT_PRINT_DEC
"]",
334 c
->by_ref
? "ref " : "", c
->offset
);
336 for (unsigned i
= 0; i
< vec_safe_length (c
->param_ops
); i
++)
338 expr_eval_op
&op
= (*(c
->param_ops
))[i
];
339 const char *op_name
= op_symbol_code (op
.code
);
341 if (op_name
== op_symbol_code (ERROR_MARK
))
342 op_name
= get_tree_code_name (op
.code
);
352 case FIXED_CONVERT_EXPR
:
353 case VIEW_CONVERT_EXPR
:
355 if (op
.code
== VIEW_CONVERT_EXPR
)
358 print_generic_expr (f
, op
.type
);
363 fprintf (f
, "%s", op_name
);
371 print_generic_expr (f
, op
.val
[0]);
372 fprintf (f
, " %s #", op_name
);
376 fprintf (f
, "# %s ", op_name
);
377 print_generic_expr (f
, op
.val
[0]);
382 fprintf (f
, "%s ", op_name
);
387 print_generic_expr (f
, op
.val
[0]);
389 print_generic_expr (f
, op
.val
[1]);
393 print_generic_expr (f
, op
.val
[0]);
394 fprintf (f
, ", #, ");
395 print_generic_expr (f
, op
.val
[1]);
399 print_generic_expr (f
, op
.val
[0]);
401 print_generic_expr (f
, op
.val
[1]);
406 fprintf (f
, "*, *, *");
412 if (c
->code
== ipa_predicate::is_not_constant
)
414 fprintf (f
, " not constant");
417 if (c
->code
== ipa_predicate::changed
)
419 fprintf (f
, " changed");
422 if (c
->code
== ipa_predicate::not_sra_candidate
)
424 fprintf (f
, " not sra candidate");
427 fprintf (f
, " %s ", op_symbol_code (c
->code
));
428 print_generic_expr (f
, c
->val
);
433 /* Dump clause CLAUSE. */
436 dump_clause (FILE *f
, conditions conds
, clause_t clause
)
443 for (i
= 0; i
< ipa_predicate::num_conditions
; i
++)
444 if (clause
& (1 << i
))
449 dump_condition (f
, conds
, i
);
455 /* Dump THIS to F. CONDS a vector of conditions used when evaluating
456 ipa_predicates. When NL is true new line is output at the end of dump. */
459 ipa_predicate::dump (FILE *f
, conditions conds
, bool nl
) const
463 dump_clause (f
, conds
, 0);
465 for (i
= 0; m_clause
[i
]; i
++)
469 dump_clause (f
, conds
, m_clause
[i
]);
477 ipa_predicate::debug (conditions conds
) const
479 dump (stderr
, conds
);
483 /* Remap predicate THIS of former function to be predicate of duplicated function.
484 POSSIBLE_TRUTHS is clause of possible truths in the duplicated node,
485 INFO is inline summary of the duplicated node. */
488 ipa_predicate::remap_after_duplication (clause_t possible_truths
)
491 ipa_predicate out
= true;
492 for (j
= 0; m_clause
[j
]; j
++)
493 if (!(possible_truths
& m_clause
[j
]))
496 out
.add_clause (NULL
, possible_truths
& m_clause
[j
]);
501 /* Translate all conditions from callee representation into caller
502 representation and symbolically evaluate predicate THIS into new predicate.
504 INFO is ipa_fn_summary of function we are adding predicate into, CALLEE_INFO
505 is summary of function predicate P is from. OPERAND_MAP is array giving
506 callee formal IDs the caller formal IDs. POSSSIBLE_TRUTHS is clause of all
507 callee conditions that may be true in caller context. TOPLEV_PREDICATE is
508 predicate under which callee is executed. OFFSET_MAP is an array of
509 offsets that need to be added to conditions, negative offset means that
510 conditions relying on values passed by reference have to be discarded
511 because they might not be preserved (and should be considered offset zero
512 for other purposes). */
515 ipa_predicate::remap_after_inlining (class ipa_fn_summary
*info
,
516 class ipa_node_params
*params_summary
,
517 class ipa_fn_summary
*callee_info
,
518 const vec
<int> &operand_map
,
519 const vec
<HOST_WIDE_INT
> &offset_map
,
520 clause_t possible_truths
,
521 const ipa_predicate
&toplev_predicate
)
524 ipa_predicate out
= true;
526 /* True ipa_predicate is easy. */
528 return toplev_predicate
;
529 for (i
= 0; m_clause
[i
]; i
++)
531 clause_t clause
= m_clause
[i
];
533 ipa_predicate clause_predicate
= false;
535 gcc_assert (i
< max_clauses
);
537 for (cond
= 0; cond
< num_conditions
; cond
++)
538 /* Do we have condition we can't disprove? */
539 if (clause
& possible_truths
& (1 << cond
))
541 ipa_predicate cond_predicate
;
542 /* Work out if the condition can translate to predicate in the
544 if (cond
>= ipa_predicate::first_dynamic_condition
)
548 int index
= cond
- ipa_predicate::first_dynamic_condition
;
549 c
= &(*callee_info
->conds
)[index
];
550 /* See if we can remap condition operand to caller's operand.
551 Otherwise give up. */
552 if (!operand_map
.exists ()
553 || (int) operand_map
.length () <= c
->operand_num
554 || operand_map
[c
->operand_num
] == -1
555 /* TODO: For non-aggregate conditions, adding an offset is
556 basically an arithmetic jump function processing which
557 we should support in future. */
558 || ((!c
->agg_contents
|| !c
->by_ref
)
559 && offset_map
[c
->operand_num
] > 0)
560 || (c
->agg_contents
&& c
->by_ref
561 && offset_map
[c
->operand_num
] < 0))
562 cond_predicate
= true;
565 struct agg_position_info ap
;
566 HOST_WIDE_INT offset_delta
= offset_map
[c
->operand_num
];
567 if (offset_delta
< 0)
569 gcc_checking_assert (!c
->agg_contents
|| !c
->by_ref
);
572 gcc_assert (!c
->agg_contents
573 || c
->by_ref
|| offset_delta
== 0);
574 ap
.offset
= c
->offset
+ offset_delta
;
575 ap
.agg_contents
= c
->agg_contents
;
576 ap
.by_ref
= c
->by_ref
;
577 cond_predicate
= add_condition (info
, params_summary
,
578 operand_map
[c
->operand_num
],
579 c
->type
, &ap
, c
->code
,
580 c
->val
, c
->param_ops
);
583 /* Fixed conditions remains same, construct single
584 condition predicate. */
586 cond_predicate
= ipa_predicate::predicate_testing_cond (cond
);
587 clause_predicate
= clause_predicate
.or_with (info
->conds
,
590 out
&= clause_predicate
;
592 out
&= toplev_predicate
;
597 /* Read predicate from IB. */
600 ipa_predicate::stream_in (class lto_input_block
*ib
)
607 gcc_assert (k
<= max_clauses
);
608 clause
= m_clause
[k
++] = streamer_read_uhwi (ib
);
612 /* Zero-initialize the remaining clauses in OUT. */
613 while (k
<= max_clauses
)
618 /* Write predicate P to OB. */
621 ipa_predicate::stream_out (struct output_block
*ob
)
624 for (j
= 0; m_clause
[j
]; j
++)
626 gcc_assert (j
< max_clauses
);
627 streamer_write_uhwi (ob
, m_clause
[j
]);
629 streamer_write_uhwi (ob
, 0);
633 /* Add condition to condition list SUMMARY. OPERAND_NUM, TYPE, CODE, VAL and
634 PARAM_OPS correspond to fields of condition structure. AGGPOS describes
635 whether the used operand is loaded from an aggregate and where in the
636 aggregate it is. It can be NULL, which means this not a load from an
640 add_condition (class ipa_fn_summary
*summary
,
641 class ipa_node_params
*params_summary
,
643 tree type
, struct agg_position_info
*aggpos
,
644 enum tree_code code
, tree val
, expr_eval_ops param_ops
)
648 struct condition new_cond
;
649 HOST_WIDE_INT offset
;
650 bool agg_contents
, by_ref
;
654 ipa_set_param_used_by_ipa_predicates (params_summary
, operand_num
, true);
658 offset
= aggpos
->offset
;
659 agg_contents
= aggpos
->agg_contents
;
660 by_ref
= aggpos
->by_ref
;
665 agg_contents
= false;
669 gcc_checking_assert (operand_num
>= 0);
670 for (i
= 0; vec_safe_iterate (summary
->conds
, i
, &c
); i
++)
672 if (c
->operand_num
== operand_num
674 && types_compatible_p (c
->type
, type
)
675 && vrp_operand_equal_p (c
->val
, val
)
676 && c
->agg_contents
== agg_contents
677 && expr_eval_ops_equal_p (c
->param_ops
, param_ops
)
678 && (!agg_contents
|| (c
->offset
== offset
&& c
->by_ref
== by_ref
)))
679 return ipa_predicate::predicate_testing_cond (i
);
681 /* Too many conditions. Give up and return constant true. */
682 if (i
== ipa_predicate::num_conditions
- ipa_predicate::first_dynamic_condition
)
685 new_cond
.operand_num
= operand_num
;
686 new_cond
.code
= code
;
687 new_cond
.type
= unshare_expr_without_location (type
);
688 new_cond
.val
= val
? unshare_expr_without_location (val
) : val
;
689 new_cond
.agg_contents
= agg_contents
;
690 new_cond
.by_ref
= by_ref
;
691 new_cond
.offset
= offset
;
692 new_cond
.param_ops
= vec_safe_copy (param_ops
);
694 for (j
= 0; vec_safe_iterate (new_cond
.param_ops
, j
, &op
); j
++)
697 op
->val
[0] = unshare_expr_without_location (op
->val
[0]);
699 op
->val
[1] = unshare_expr_without_location (op
->val
[1]);
702 vec_safe_push (summary
->conds
, new_cond
);
704 return ipa_predicate::predicate_testing_cond (i
);