ipa-fnsummary.h 10.4 KB
Newer Older
1
/* IPA function body analysis.
2
   Copyright (C) 2003-2018 Free Software Foundation, Inc.
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
   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/>.  */

#ifndef GCC_IPA_SUMMARY_H
#define GCC_IPA_SUMMARY_H

#include "sreal.h"
#include "ipa-predicate.h"


28
/* Hints are reasons why IPA heuristics should preffer specializing given
29
   function.  They are represtented as bitmap of the following values.  */
30 31
enum ipa_hints_vals {
  /* When specialization turns indirect call into a direct call,
32 33 34 35 36 37 38 39 40 41 42 43 44
     it is good idea to do so.  */
  INLINE_HINT_indirect_call = 1,
  /* Inlining may make loop iterations or loop stride known.  It is good idea
     to do so because it enables loop optimizatoins.  */
  INLINE_HINT_loop_iterations = 2,
  INLINE_HINT_loop_stride = 4,
  /* Inlining within same strongly connected component of callgraph is often
     a loss due to increased stack frame usage and prologue setup costs.  */
  INLINE_HINT_same_scc = 8,
  /* Inlining functions in strongly connected component is not such a great
     win.  */
  INLINE_HINT_in_scc = 16,
  /* If function is declared inline by user, it may be good idea to inline
45
     it.  Set by simple_edge_hints in ipa-inline-analysis.c.  */
46 47 48
  INLINE_HINT_declared_inline = 32,
  /* Programs are usually still organized for non-LTO compilation and thus
     if functions are in different modules, inlining may not be so important. 
49
     Set by simple_edge_hints in ipa-inline-analysis.c.   */
50 51 52 53 54 55 56 57
  INLINE_HINT_cross_module = 64,
  /* If array indexes of loads/stores become known there may be room for
     further optimization.  */
  INLINE_HINT_array_index = 128,
  /* We know that the callee is hot by profile.  */
  INLINE_HINT_known_hot = 256
};

58
typedef int ipa_hints;
59 60 61 62 63 64 65 66 67 68 69 70 71

/* Simple description of whether a memory load or a condition refers to a load
   from an aggregate and if so, how and where from in the aggregate.
   Individual fields have the same meaning like fields with the same name in
   struct condition.  */

struct agg_position_info
{
  HOST_WIDE_INT offset;
  bool agg_contents;
  bool by_ref;
};

72
/* Representation of function body size and time depending on the call
73
   context.  We keep simple array of record, every containing of predicate
74
   and time/size to account.  */
75 76 77 78 79 80 81 82 83 84 85 86 87
struct GTY(()) size_time_entry
{
  /* Predicate for code to be executed.  */
  predicate exec_predicate;
  /* Predicate for value to be constant and optimized out in a specialized copy.
     When deciding on specialization this makes it possible to see how much
     the executed code paths will simplify.  */
  predicate nonconst_predicate;
  int size;
  sreal GTY((skip)) time;
};

/* Function inlining information.  */
88
struct GTY(()) ipa_fn_summary
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
  /* Keep all field empty so summary dumping works during its computation.
     This is useful for debugging.  */
  ipa_fn_summary ()
    : estimated_self_stack_size (0), self_size (0), min_size (0),
      inlinable (false), single_caller (false),
      fp_expressions (false), estimated_stack_size (false),
      stack_frame_offset (false), time (0), size (0), conds (NULL),
      size_time_table (NULL), loop_iterations (NULL), loop_stride (NULL),
      array_index (NULL), growth (0), scc_no (0)
  {
  }

  /* Copy constructor.  */
  ipa_fn_summary (const ipa_fn_summary &s)
    : estimated_self_stack_size (s.estimated_self_stack_size),
    self_size (s.self_size), min_size (s.min_size),
    inlinable (s.inlinable), single_caller (s.single_caller),
    fp_expressions (s.fp_expressions),
    estimated_stack_size (s.estimated_stack_size),
    stack_frame_offset (s.stack_frame_offset), time (s.time), size (s.size),
    conds (s.conds), size_time_table (s.size_time_table),
    loop_iterations (s.loop_iterations), loop_stride (s.loop_stride),
    array_index (s.array_index), growth (s.growth), scc_no (s.scc_no)
  {}

  /* Default constructor.  */
  ~ipa_fn_summary ();

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
  /* Information about the function body itself.  */

  /* Estimated stack frame consumption by the function.  */
  HOST_WIDE_INT estimated_self_stack_size;
  /* Size of the function body.  */
  int self_size;
  /* Minimal size increase after inlining.  */
  int min_size;

  /* False when there something makes inlining impossible (such as va_arg).  */
  unsigned inlinable : 1;
  /* True wen there is only one caller of the function before small function
     inlining.  */
  unsigned int single_caller : 1;
  /* True if function contains any floating point expressions.  */
  unsigned int fp_expressions : 1;

  /* Information about function that will result after applying all the
     inline decisions present in the callgraph.  Generally kept up to
     date only for functions that are not inline clones. */

  /* Estimated stack frame consumption by the function.  */
  HOST_WIDE_INT estimated_stack_size;
141
  /* Expected offset of the stack frame of function.  */
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
  HOST_WIDE_INT stack_frame_offset;
  /* Estimated size of the function after inlining.  */
  sreal GTY((skip)) time;
  int size;

  /* Conditional size/time information.  The summaries are being
     merged during inlining.  */
  conditions conds;
  vec<size_time_entry, va_gc> *size_time_table;

  /* Predicate on when some loop in the function becomes to have known
     bounds.   */
  predicate * GTY((skip)) loop_iterations;
  /* Predicate on when some loop in the function becomes to have known
     stride.   */
  predicate * GTY((skip)) loop_stride;
  /* Predicate on when some array indexes become constants.  */
  predicate * GTY((skip)) array_index;
  /* Estimated growth for inlining all copies of the function before start
     of small functions inlining.
     This value will get out of date as the callers are duplicated, but
     using up-to-date value in the badness metric mean a lot of extra
     expenses.  */
  int growth;
  /* Number of SCC on the beginning of inlining process.  */
  int scc_no;

  /* Record time and size under given predicates.  */
  void account_size_time (int, sreal, const predicate &, const predicate &);

172 173
  /* We keep values scaled up, so fractional sizes can be accounted.  */
  static const int size_scale = 2;
174 175
};

176
class GTY((user)) ipa_fn_summary_t: public function_summary <ipa_fn_summary *>
177 178
{
public:
179 180
  ipa_fn_summary_t (symbol_table *symtab, bool ggc):
    function_summary <ipa_fn_summary *> (symtab, ggc) {}
181

182
  static ipa_fn_summary_t *create_ggc (symbol_table *symtab)
183
  {
184 185
    struct ipa_fn_summary_t *summary = new (ggc_alloc <ipa_fn_summary_t> ())
      ipa_fn_summary_t(symtab, true);
186 187 188 189
    summary->disable_insertion_hook ();
    return summary;
  }

190 191
  /* Remove ipa_fn_summary for all callees of NODE.  */
  void remove_callees (cgraph_node *node);
192

193
  virtual void insert (cgraph_node *, ipa_fn_summary *);
194 195 196 197 198
  virtual void remove (cgraph_node *node, ipa_fn_summary *)
  {
    remove_callees (node);
  }

199
  virtual void duplicate (cgraph_node *src, cgraph_node *dst,
200
			  ipa_fn_summary *src_data, ipa_fn_summary *dst_data);
201 202
};

203
extern GTY(()) function_summary <ipa_fn_summary *> *ipa_fn_summaries;
204 205 206 207

/* Information kept about callgraph edges.  */
struct ipa_call_summary
{
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
  /* Keep all field empty so summary dumping works during its computation.
     This is useful for debugging.  */
  ipa_call_summary ()
    : predicate (NULL), param (vNULL), call_stmt_size (0), call_stmt_time (0),
      loop_depth (0), is_return_callee_uncaptured (false)
    {
    }

  /* Copy constructor.  */
  ipa_call_summary (const ipa_call_summary &s):
    predicate (s.predicate), param (s.param), call_stmt_size (s.call_stmt_size),
    call_stmt_time (s.call_stmt_time), loop_depth (s.loop_depth),
    is_return_callee_uncaptured (s.is_return_callee_uncaptured)
  {
  }

  /* Default destructor.  */
  ~ipa_call_summary ();

227 228 229 230 231 232 233 234
  class predicate *predicate;
  /* Vector indexed by parameters.  */
  vec<inline_param_summary> param;
  /* Estimated size and time of the call statement.  */
  int call_stmt_size;
  int call_stmt_time;
  /* Depth of loop nest, 0 means no nesting.  */
  unsigned int loop_depth;
235 236
  /* Indicates whether the caller returns the value of it's callee.  */
  bool is_return_callee_uncaptured;
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
};

class ipa_call_summary_t: public call_summary <ipa_call_summary *>
{
public:
  ipa_call_summary_t (symbol_table *symtab, bool ggc):
    call_summary <ipa_call_summary *> (symtab, ggc) {}

  /* Hook that is called by summary when an edge is duplicated.  */
  virtual void duplicate (cgraph_edge *src, cgraph_edge *dst,
			  ipa_call_summary *src_data,
			  ipa_call_summary *dst_data);
};

extern call_summary <ipa_call_summary *> *ipa_call_summaries;

/* In ipa-fnsummary.c  */
254 255 256 257
void ipa_debug_fn_summary (struct cgraph_node *);
void ipa_dump_fn_summaries (FILE *f);
void ipa_dump_fn_summary (FILE *f, struct cgraph_node *node);
void ipa_dump_hints (FILE *f, ipa_hints);
258
void ipa_free_fn_summary (void);
259 260 261 262 263 264
void inline_analyze_function (struct cgraph_node *node);
void estimate_ipcp_clone_size_and_time (struct cgraph_node *,
					vec<tree>,
					vec<ipa_polymorphic_call_context>,
					vec<ipa_agg_jump_function_p>,
					int *, sreal *, sreal *,
265 266 267 268
				        ipa_hints *);
void ipa_merge_fn_summary_after_inlining (struct cgraph_edge *edge);
void ipa_update_overall_fn_summary (struct cgraph_node *node);
void compute_fn_summary (struct cgraph_node *, bool);
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286


void evaluate_properties_for_edge (struct cgraph_edge *e, bool inline_p,
				   clause_t *clause_ptr,
				   clause_t *nonspec_clause_ptr,
				   vec<tree> *known_vals_ptr,
				   vec<ipa_polymorphic_call_context>
				   *known_contexts_ptr,
				   vec<ipa_agg_jump_function_p> *);
void estimate_node_size_and_time (struct cgraph_node *node,
				  clause_t possible_truths,
				  clause_t nonspec_possible_truths,
				  vec<tree> known_vals,
				  vec<ipa_polymorphic_call_context>,
				  vec<ipa_agg_jump_function_p> known_aggs,
				  int *ret_size, int *ret_min_size,
				  sreal *ret_time,
				  sreal *ret_nonspecialized_time,
287
				  ipa_hints *ret_hints,
288 289 290
				  vec<inline_param_summary>
				  inline_param_summary);

291 292
void ipa_fnsummary_c_finalize (void);

293
#endif /* GCC_IPA_FNSUMMARY_H */