aboutsummaryrefslogtreecommitdiff
path: root/gcc/misc.cc
blob: 5b056423600f8a5ecb1210cad0a50b1754bb83b4 (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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Miscellaneous support routines not intended for upstream merge.

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "tree.h"
#include "gimple.h"
#include "tree-pass.h"
#include "ssa.h"
#include "gimple-pretty-print.h"
#include "cfganal.h"
#include "gimple-fold.h"
#include "tree-eh.h"
#include "gimple-iterator.h"
#include "tree-cfg.h"
#include "tree-ssa-loop-manip.h"
#include "tree-ssa-loop.h"
#include "cfgloop.h"
#include "tree-scalar-evolution.h"
#include "tree-ssa-propagate.h"
#include "alloc-pool.h"
#include "domwalk.h"
#include "tree-cfgcleanup.h"
#include "vr-values.h"
#include "gimple-ssa-evrp-analyze.h"
#include "fold-const.h"
#include "misc.h"

//====================================================
// unseen_function_p
//====================================================

static const char *
get_fresh_function_name ()
{
  char *result;
  if (cfun->decl)
    {
      pretty_printer pp;
      dump_generic_node (&pp, cfun->decl, 0, TDF_VOPS|TDF_MEMSYMS, false);
      const char *str = pp_formatted_text (&pp);
      result = new char[strlen (str) + 1];
      return strcpy (result, str);
    }
  result = new char[sizeof("UNKNOWN") + 1];
  return strcpy (result, "UNKNOWN");
}

static bool
unseen_function_p ()
{
  bool unseen = true;
  static const char *prev_function_name;
  const char *curr_function_name = get_fresh_function_name ();

  if (prev_function_name && curr_function_name)
    unseen = strcmp (prev_function_name, curr_function_name);

  if (prev_function_name)
    delete prev_function_name;
  prev_function_name = curr_function_name;
  return unseen;
}

//====================================================
// highlighter
//====================================================

class highlighter highlighter;

#define INDENT(SPACE)							\
  do { int i; for (i = 0; i < SPACE; i++) pp_space (buffer); } while (0)

void
highlighter::on (pretty_printer *buffer, int spc, gimple *stmt)
{
  bool removal = untainted_stmt == stmt;
  bool found_orig_stmt = new_stmt == stmt;
  bool need_header = found_orig_stmt || removal;
  if (need_header)
    {
      pp_string (buffer, ";; (STATE) filename = ");
      pp_string (buffer, main_input_filename);
      pp_newline_and_flush (buffer);
      INDENT (spc);
    }
  if (removal)
    {
      pp_string (buffer, ";; Queued for removal LHS= ");
      dump_generic_node (buffer, lhs, spc, TDF_SLIM, false);
    }
  else if (found_orig_stmt)
    {
      pp_string (buffer, ";; Original statement was: ");
      pp_gimple_stmt_1 (buffer, old_stmt, spc, TDF_SLIM);
    }
  if (need_header)
    {
      pp_newline_and_flush (buffer);
      INDENT (spc);
      pp_string (buffer, ";; VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\n");
      INDENT (spc);
      // For PHI removal, dump the PHI as well as the statement.
      if (removal && is_a<gphi *> (new_stmt))
	{
	  pp_gimple_stmt_1 (buffer, new_stmt, spc, TDF_SLIM);
	  pp_newline_and_flush (buffer);
	  INDENT (spc);
	}
    }
}

void
highlighter::off (pretty_printer *buffer, int spc, gimple *stmt)
{
  bool removal = untainted_stmt == stmt;
  bool found_orig_stmt = new_stmt == stmt;
  if (found_orig_stmt || removal)
    {
      pp_newline_and_flush (buffer);
      INDENT (spc);
      pp_string (buffer, ";; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
    }
}

void
highlighter::set (gimple *untainted_stmt,
		  gimple *old_stmt, gimple *new_stmt, tree lhs)
{
  this->untainted_stmt = untainted_stmt;
  this->old_stmt = old_stmt;
  this->new_stmt = new_stmt;
  this->lhs = lhs;
}

//====================================================
// gimple_state
//====================================================

gimple_state::gimple_state ()
{
  orig_stmt = NULL;
  out = stderr;
  accumulate_changes = false;

  // Instead of trapping, accumulate changes into the filename stored
  // in EVRP_TRAPS.
  if (const char *filename = getenv ("EVRP_TRAPS"))
    {
      out = fopen (filename, "a");
      accumulate_changes = true;
    }
}

gimple_state::~gimple_state ()
{
  if (out != stderr)
    fclose (out);
}

gimple *
gimple_state::set_orig_stmt (gimple *stmt)
{
  untainted_stmt = stmt;
  gimple *prev = orig_stmt;
  orig_stmt = gimple_copy (stmt);
  return prev;
}

void
gimple_state::maybe_dump_differences_and_trap (gimple *new_stmt, tree lhs)
{
  if (flag_evrp_traps)
    {
      if (unseen_function_p ())
	{
	  fprintf (out, "===============================================\n");
	  highlighter.set (untainted_stmt, orig_stmt, new_stmt, lhs);
	  dump_function_to_file (current_function_decl, out, TDF_NONE);
	  highlighter.set ();
	}
      if (accumulate_changes)
	return;
      gcc_unreachable ();
    }
}

//====================================================
// Miscellaneous debugging aids.
//====================================================

DEBUG_FUNCTION char *
strcpy_gimple (char *dest, const gimple *g)
{
  pretty_printer pp;
  pp_gimple_stmt_1 (&pp, g, 0, TDF_NONE);
  const char *str = pp_formatted_text (&pp);
  return strcpy (dest, str);
}

DEBUG_FUNCTION int
strncmp_gimple (const gimple *g, const char *str, size_t n)
{
  char gstr[100];
  strcpy_gimple (gstr, g);
  return strncmp (gstr, str, n);
}

DEBUG_FUNCTION int
strcmp_gimple (const gimple *g, const char *str)
{
  char gstr[100];
  strcpy_gimple (gstr, g);
  return strcmp (gstr, str);
}

DEBUG_FUNCTION char *
strcpy_tree (char *dest, tree t)
{
  pretty_printer pp;
  dump_generic_node (&pp, t, 0, TDF_VOPS|TDF_MEMSYMS, false);
  const char *str = pp_formatted_text (&pp);
  return strcpy (dest, str);
}

DEBUG_FUNCTION int
strncmp_tree (tree t, const char *str, size_t n)
{
  char tstr[100];
  strcpy_tree (tstr, t);
  return strncmp (tstr, str, n);
}

DEBUG_FUNCTION int
strcmp_tree (tree t, const char *str)
{
  char tstr[100];
  strcpy_tree (tstr, t);
  return strcmp (tstr, str);
}

//=========================================
// vr_comparison
//=========================================

vr_comparison::vr_comparison (const irange *old_range,
			      const irange *new_range,
			      vr_values *vr)
{
  m_old_range = old_range;
  m_new_range = new_range;
  m_vr_values = vr;
}

void
vr_comparison::compare ()
{
  if (new_range_is_same ())
    return;
  if (new_range_is_better ())
    {
      if (dump_file)
	dump_improvements (dump_file);
      return;
    }
  dump_differences_and_trap ();
}

void
vr_comparison::compare (tree name, edge e)
{
  m_name = name;
  m_edge = e;
  m_stmt = NULL;
  compare ();
}

void
vr_comparison::compare (gimple *stmt)
{
  m_name = get_output_for_vrp (stmt);
  m_edge = NULL;
  m_stmt = stmt;
  compare ();
}

bool
vr_comparison::new_range_is_same () const
{
  // We may be able to normalize a symbolic to a [MIN,MAX] plus
  // or minus the end-points.  Don't count that as a win just yet.
  if (m_old_range && m_old_range->symbolic_p ())
    return true;
  widest_irange old;
  if (m_old_range)
    old = *m_old_range;
  // Treat UNDEFINED and VARYING as interchangeable.
  if (old.undefined_p () && m_new_range->varying_p ())
    return true;
  if (old.varying_p () && m_new_range->undefined_p ())
    return true;
  return old == *m_new_range;
}

bool
vr_comparison::new_range_is_better () const
{
  if (new_range_is_same ())
    return false;
  if (!m_old_range)
    return true;
  // ?? Sometimes we get an undefined because the ranger determined a
  // path was unexecutable.  Verify this is actually the case by
  // turning this off and analyzing all failures.
  if (m_new_range->undefined_p ())
    return true;
  if (!range_has_numeric_bounds_p (m_old_range))
    {
      gcc_checking_assert (range_has_numeric_bounds_p (m_new_range));
      return true;
    }
  widest_irange inter (*m_new_range);
  inter.intersect (*m_old_range);
  return inter == *m_new_range;
}

void
vr_comparison::dump_differences_and_trap () const
{
  bool dumping = getenv("GORI_DUMP_FILE") != NULL;
  if (dumping)
    {
      const char *filename = getenv("GORI_DUMP_FILE");
      FILE *out = fopen (filename, "a");
      fprintf (out, "=========FILE: %s ========\n",
	       main_input_filename ? main_input_filename : "UNKNOWN");
      dump_differences (out);
      fclose (out);
      return;
    }
  dump_differences (stderr);
  gcc_unreachable ();
}

void
vr_comparison::dump_differences (FILE *out) const
{
  dump_flags_t flags = TDF_NONE;
  if (m_stmt)
    {
      fprintf (out, "Different ranges for stmt: ");
      print_gimple_stmt (out, m_stmt, 0, flags);
    }
  else
    {
      fprintf (out, "Different ranges on edge (%d -> %d) for SSA: ",
	       m_edge->src->index, m_edge->dest->index);
      print_generic_stmt (out, m_name, flags);
    }
  fprintf (out, "\told range: ");
  m_old_range->dump (out);
  fprintf (out, "\n\tnew range: ");
  m_new_range->dump (out);
  if (m_edge)
    {
      fprintf (out, "\n\n");
      dump_bb (out, m_edge->src, 0, TDF_NONE);
    }
  fprintf (out, "\n");
  fprintf (out, "==============================================\n");
  dump_function_to_file (current_function_decl, out, TDF_NONE);

  if (m_vr_values)
    {
      dump_flags_t save = dump_flags;
      dump_flags |= TDF_GORI;
      m_vr_values->dump_all_value_ranges (out);
      dump_flags = save;
    }
}

void
vr_comparison::dump_improvements (FILE *out) const
{
  if (m_old_range && !range_has_numeric_bounds_p (m_old_range))
    return;
  if (new_range_is_better ())
    {
      fprintf (out, "New range improved: ");
      if (m_name)
	print_generic_expr (out, m_name);
      fprintf (out, " from: ");
      if (m_old_range)
	m_old_range->dump (out);
      else
	fprintf (out, "UNDEFINED");
      fprintf (out, " to: ");
      m_new_range->dump (out);
      fprintf (out, "\n");
    }
}