aboutsummaryrefslogtreecommitdiff
path: root/gcc/ipa-predicate.h
blob: a90321fdcfda8e1b479e59fce2fd92780d0d939a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* IPA predicates.
   Copyright (C) 2003-2024 Free Software Foundation, Inc.
   Contributed by Jan Hubicka

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

/* Representation of inline parameters that do depend on context function is
   inlined into (i.e. known constant values of function parameters.

   Conditions that are interesting for function body are collected into CONDS
   vector.  They are of simple as kind of a mathematical transformation on
   function parameter, T(function_param), in which the parameter occurs only
   once, and other operands are IPA invariant.  The conditions are then
   referred by predicates.  */


/* A simplified representation of tree node, for unary, binary and ternary
   operation.  Computations on parameter are decomposed to a series of this
   kind of structure.  */
struct GTY(()) expr_eval_op
{
  /* Result type of expression.  */
  tree type;
  /* Constant operands in expression, there are at most two.  */
  tree val[2];
  /* Index of parameter operand in expression.  */
  unsigned index : 2;
  /* Operation code of expression.  */
  ENUM_BITFIELD(tree_code) code : 16;
};

typedef vec<expr_eval_op, va_gc> *expr_eval_ops;

struct GTY(()) condition
{
  /* If agg_contents is set, this is the offset from which the used data was
     loaded.  */
  HOST_WIDE_INT offset;
  /* Type of the access reading the data (or the PARM_DECL SSA_NAME).  */
  tree type;
  tree val;
  int operand_num;
  ENUM_BITFIELD(tree_code) code : 16;
  /* Set if the used data were loaded from an aggregate parameter or from
     data received by reference.  */
  unsigned agg_contents : 1;
  /* If agg_contents is set, this differentiates between loads from data
     passed by reference and by value.  */
  unsigned by_ref : 1;
  /* A set of sequential operations on the parameter, which can be seen as
     a mathematical function on the parameter.  */
  expr_eval_ops param_ops;
};

/* Information kept about parameter of call site.  */
struct inline_param_summary
{
  /* REG_BR_PROB_BASE based probability that parameter will change in between
     two invocation of the calls.
     I.e. loop invariant parameters
     REG_BR_PROB_BASE/estimated_iterations and regular
     parameters REG_BR_PROB_BASE.

     Value 0 is reserved for compile time invariants. */
  short change_prob;
  unsigned points_to_local_or_readonly_memory : 1;
  unsigned points_to_possible_sra_candidate : 1;
  bool equal_to (const inline_param_summary &other) const
  {
    return change_prob == other.change_prob
	   && points_to_local_or_readonly_memory
	      == other.points_to_local_or_readonly_memory
	   && points_to_possible_sra_candidate
	      == other.points_to_possible_sra_candidate;
  }
  bool useless_p (void) const
  {
    return change_prob == REG_BR_PROB_BASE
	   && !points_to_local_or_readonly_memory
	   && !points_to_possible_sra_candidate;
  }
};

typedef vec<condition, va_gc> *conditions;

/* Predicates are used to represent function parameters (such as runtime)
   which depend on a context function is called in.

   Predicates are logical formulas in conjunctive-disjunctive form consisting
   of clauses which are bitmaps specifying a set of condition that must
   be true for a clause to be satisfied. Physically they are represented as
   array of clauses terminated by 0.

   In order to make predicate (possibly) true, all of its clauses must
   be (possibly) true. To make clause (possibly) true, one of conditions
   it mentions must be (possibly) true.

   There are fixed bounds on number of clauses and conditions and all the
   manipulation functions are conservative in positive direction. I.e. we
   may lose precision by thinking that predicate may be true even when it
   is not.  */

typedef uint32_t clause_t;
class ipa_predicate
{
public:
  enum predicate_conditions
    {
      false_condition = 0,
      not_inlined_condition = 1,
      first_dynamic_condition = 2
    };

  /* Maximal number of conditions predicate can refer to.  This is limited
     by using clause_t to be 32bit.  */
  static const int num_conditions = 32;

  /* Special condition code we use to represent test that operand is compile
     time constant.  */
  static const tree_code is_not_constant = ERROR_MARK;

  /* Special condition code we use to represent test that operand is not changed
     across invocation of the function.  When operand IS_NOT_CONSTANT it is
     always CHANGED, however i.e. loop invariants can be NOT_CHANGED given
     percentage of executions even when they are not compile time constants.  */
  static const tree_code changed = IDENTIFIER_NODE;

  /* Special condition code we use to check that the parameter is likely SRA
     candidate.  */
  static const tree_code not_sra_candidate = TREE_LIST;


  /* Initialize predicate either to true of false depending on P.  */
  inline ipa_predicate (bool p = true)
    {
      if (p)
        /* True predicate.  */
        m_clause[0] = 0;
      else
        /* False predicate. */
        set_to_cond (false_condition);
    }

  /* Sanity check that we do not mix pointers to predicates with predicates.  */
  inline ipa_predicate (ipa_predicate *)
    {
      gcc_unreachable ();
    }

  /* Return predicate testing condition I.  */
  static inline ipa_predicate predicate_testing_cond (int i)
    {
      ipa_predicate p;
      p.set_to_cond (i + first_dynamic_condition);
      return p;
    }

  /* Return predicate testing that function was not inlined.  */
  static ipa_predicate not_inlined (void)
    {
      ipa_predicate p;
      p.set_to_cond (not_inlined_condition);
      return p;
    }

  /* Compute logical and of ipa_predicates.  */
  ipa_predicate & operator &= (const ipa_predicate &);
  inline ipa_predicate operator &(const ipa_predicate &p) const
    {
      ipa_predicate ret = *this;
      ret &= p;
      return ret;
    }

  /* Compute logical or of ipa_predicates.  This is not operator because
     extra parameter CONDITIONS is needed  */
  ipa_predicate or_with (conditions, const ipa_predicate &) const;

  /* Return true if ipa_predicates are known to be equal.  */
  inline bool operator==(const ipa_predicate &p2) const
    {
      int i;
      for (i = 0; m_clause[i]; i++)
	{
	  gcc_checking_assert (i < max_clauses);
	  gcc_checking_assert (m_clause[i] > m_clause[i + 1]);
	  gcc_checking_assert (!p2.m_clause[i]
			       || p2.m_clause[i] > p2.m_clause[i + 1]);
	  if (m_clause[i] != p2.m_clause[i])
	    return false;
	}
      return !p2.m_clause[i];
    }

  /* Return true if predicates are known to be true or false depending
     on COND.  */
  inline bool operator==(const bool cond) const
    {
      if (cond)
        return !m_clause[0];
      if (m_clause[0] == (1 << false_condition))
	{
	  gcc_checking_assert (!m_clause[1]
			       && m_clause[0] == 1
				  << false_condition);
	  return true;
	}
      return false;
    }

  inline bool operator!=(const ipa_predicate &p2) const
    {
      return !(*this == p2);
    }

  inline bool operator!=(const bool cond) const
    {
      return !(*this == cond);
    }

  /* Evaluate if predicate is known to be false given the clause of possible
     truths.  */
  bool evaluate (clause_t) const;

  /* Estimate probability that predicate will be true in a given context.  */
  int probability (conditions, clause_t, vec<inline_param_summary>) const;

  /* Dump predicate to F. Output newline if nl.  */
  void dump (FILE *f, conditions, bool nl=true) const;
  void DEBUG_FUNCTION debug (conditions) const;

  /* Return ipa_predicate equal to THIS after duplication.  */
  ipa_predicate remap_after_duplication (clause_t);

  /* Return ipa_predicate equal to THIS after inlining.  */
  ipa_predicate remap_after_inlining (class ipa_fn_summary *,
				      ipa_node_params *params_summary,
				      ipa_fn_summary *,
				      const vec<int> &,
				      const vec<HOST_WIDE_INT> &,
				      clause_t, const ipa_predicate &);

  void stream_in (lto_input_block *);
  void stream_out (output_block *);

private:
  static const int max_clauses = 8;
  clause_t m_clause[max_clauses + 1];

  /* Initialize predicate to one testing single condition number COND.  */
  inline void set_to_cond (int cond)
    {
      m_clause[0] = 1 << cond;
      m_clause[1] = 0;
    }

  void add_clause (conditions conditions, clause_t);
};

void dump_condition (FILE *f, conditions conditions, int cond);
ipa_predicate add_condition (ipa_fn_summary *summary,
			     ipa_node_params *params_summary,
			     int operand_num,
			     tree type, struct agg_position_info *aggpos,
			     enum tree_code code, tree val,
			     expr_eval_ops param_ops = NULL);