web.c 12.6 KB
Newer Older
Jan Hubicka committed
1
/* Web construction code for GNU compiler.
2
   Contributed by Jan Hubicka.
3
   Copyright (C) 2001-2016 Free Software Foundation, Inc.
Jan Hubicka committed
4 5 6 7 8

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
9
Software Foundation; either version 3, or (at your option) any later
Jan Hubicka committed
10 11 12 13 14 15 16 17
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
18 19
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */
Jan Hubicka committed
20

21
/* Simple optimization pass that splits independent uses of each pseudo,
22
   increasing effectiveness of other optimizations.  The optimization can
23
   serve as an example of use for the dataflow module.
Jan Hubicka committed
24

25 26 27
   We don't split registers with REG_USERVAR set unless -fmessy-debugging
   is specified, because debugging information about such split variables
   is almost unusable.
Jan Hubicka committed
28 29

   TODO
30 31 32 33 34
    - We may use profile information and ignore infrequent use for the
      purpose of web unifying, inserting the compensation code later to
      implement full induction variable expansion for loops (currently
      we expand only if the induction variable is dead afterward, which
      is often the case).  */
Jan Hubicka committed
35 36 37 38

#include "config.h"
#include "system.h"
#include "coretypes.h"
39 40 41
#include "backend.h"
#include "rtl.h"
#include "df.h"
42 43
#include "insn-config.h"
#include "recog.h"
Jan Hubicka committed
44

45
#include "tree-pass.h"
Jan Hubicka committed
46 47


48
/* Find the root of unionfind tree (the representative of set).  */
Jan Hubicka committed
49

50 51
web_entry_base *
web_entry_base::unionfind_root ()
Jan Hubicka committed
52
{
53
  web_entry_base *element = this, *element1 = this, *element2;
Jan Hubicka committed
54

55 56 57
  while (element->pred ())
    element = element->pred ();
  while (element1->pred ())
Jan Hubicka committed
58
    {
59 60
      element2 = element1->pred ();
      element1->set_pred (element);
Jan Hubicka committed
61 62 63 64 65
      element1 = element2;
    }
  return element;
}

H.J. Lu committed
66
/* Union sets.
Razya Ladelsky committed
67 68
   Return true if FIRST and SECOND points to the same web entry structure and
   nothing is done.  Otherwise, return false.  */
Jan Hubicka committed
69

Razya Ladelsky committed
70
bool
71
unionfind_union (web_entry_base *first, web_entry_base *second)
Jan Hubicka committed
72
{
73 74
  first = first->unionfind_root ();
  second = second->unionfind_root ();
Jan Hubicka committed
75
  if (first == second)
Razya Ladelsky committed
76
    return true;
77
  second->set_pred (first);
Razya Ladelsky committed
78
  return false;
Jan Hubicka committed
79 80
}

81 82 83 84 85 86 87 88 89 90
class web_entry : public web_entry_base
{
 private:
  rtx reg_pvt;

 public:
  rtx reg () { return reg_pvt; }
  void set_reg (rtx r) { reg_pvt = r; }
};

91 92 93 94
/* For INSN, union all defs and uses that are linked by match_dup.
   FUN is the function that does the union.  */

static void
David Malcolm committed
95
union_match_dups (rtx_insn *insn, web_entry *def_entry, web_entry *use_entry,
96
		  bool (*fun) (web_entry_base *, web_entry_base *))
97 98
{
  struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
99 100
  df_ref use_link = DF_INSN_INFO_USES (insn_info);
  df_ref def_link = DF_INSN_INFO_DEFS (insn_info);
101
  struct web_entry *dup_entry;
102 103 104 105 106 107 108 109
  int i;

  extract_insn (insn);

  for (i = 0; i < recog_data.n_dups; i++)
    {
      int op = recog_data.dup_num[i];
      enum op_type type = recog_data.operand_type[op];
110
      df_ref ref, dupref;
111 112
      struct web_entry *entry;

113 114 115
      dup_entry = use_entry;
      for (dupref = use_link; dupref; dupref = DF_REF_NEXT_LOC (dupref))
	if (DF_REF_LOC (dupref) == recog_data.dup_loc[i])
116 117
	  break;

118
      if (dupref == NULL && type == OP_INOUT)
119
	{
120 121 122
	  dup_entry = def_entry;
	  for (dupref = def_link; dupref; dupref = DF_REF_NEXT_LOC (dupref))
	    if (DF_REF_LOC (dupref) == recog_data.dup_loc[i])
123 124 125 126 127 128 129 130 131
	      break;
	}
      /* ??? *DUPREF can still be zero, because when an operand matches
	 a memory, DF_REF_LOC (use_link[n]) points to the register part
	 of the address, whereas recog_data.dup_loc[m] points to the
	 entire memory ref, thus we fail to find the duplicate entry,
         even though it is there.
         Example: i686-pc-linux-gnu gcc.c-torture/compile/950607-1.c
		  -O3 -fomit-frame-pointer -funroll-loops  */
132 133
      if (dupref == NULL
	  || DF_REF_REGNO (dupref) < FIRST_PSEUDO_REGISTER)
134 135 136 137
	continue;

      ref = type == OP_IN ? use_link : def_link;
      entry = type == OP_IN ? use_entry : def_entry;
138
      for (; ref; ref = DF_REF_NEXT_LOC (ref))
139
	{
140
	  rtx *l = DF_REF_LOC (ref);
141
	  if (l == recog_data.operand_loc[op])
142
	    break;
143
	  if (l && DF_REF_REAL_LOC (ref) == recog_data.operand_loc[op])
144 145
	    break;
	}
146

147
      if (!ref && type == OP_INOUT)
148
	{
149 150
	  entry = use_entry;
	  for (ref = use_link; ref; ref = DF_REF_NEXT_LOC (ref))
151
	    {
152
	      rtx *l = DF_REF_LOC (ref);
153
	      if (l == recog_data.operand_loc[op])
154
		break;
155
	      if (l && DF_REF_REAL_LOC (ref) == recog_data.operand_loc[op])
156 157
		break;
	    }
158 159
	}

160 161
      gcc_assert (ref);
      (*fun) (dup_entry + DF_REF_ID (dupref), entry + DF_REF_ID (ref));
162 163 164
    }
}

165
/* For each use, all possible defs reaching it must come in the same
Razya Ladelsky committed
166
   register, union them.
167 168 169 170 171 172
   FUN is the function that does the union.

   In USED, we keep the DF_REF_ID of the first uninitialized uses of a
   register, so that all uninitialized uses of the register can be
   combined into a single web.  We actually offset it by 2, because
   the values 0 and 1 are reserved for use by entry_register.  */
Jan Hubicka committed
173

Razya Ladelsky committed
174
void
175 176 177
union_defs (df_ref use, web_entry *def_entry,
	    unsigned int *used, web_entry *use_entry,
 	    bool (*fun) (web_entry_base *, web_entry_base *))
Jan Hubicka committed
178
{
179
  struct df_insn_info *insn_info = DF_REF_INSN_INFO (use);
Jan Hubicka committed
180
  struct df_link *link = DF_REF_CHAIN (use);
Razya Ladelsky committed
181 182
  rtx set;

183
  if (insn_info)
Razya Ladelsky committed
184
    {
185 186 187 188 189 190 191
      df_ref eq_use;

      set = single_set (insn_info->insn);
      FOR_EACH_INSN_INFO_EQ_USE (eq_use, insn_info)
	if (use != eq_use
	    && DF_REF_REAL_REG (use) == DF_REF_REAL_REG (eq_use))
	  (*fun) (use_entry + DF_REF_ID (use), use_entry + DF_REF_ID (eq_use));
Razya Ladelsky committed
192 193
    }
  else
194
    set = NULL;
Jan Hubicka committed
195

196
  /* Union all occurrences of the same register in reg notes.  */
197

Steven Bosscher committed
198
  /* Recognize trivial noop moves and attempt to keep them as noop.  */
Jan Hubicka committed
199 200 201 202 203

  if (set
      && SET_SRC (set) == DF_REF_REG (use)
      && SET_SRC (set) == SET_DEST (set))
    {
204 205 206 207 208
      df_ref def;

      FOR_EACH_INSN_INFO_DEF (def, insn_info)
	if (DF_REF_REAL_REG (use) == DF_REF_REAL_REG (def))
	  (*fun) (use_entry + DF_REF_ID (use), def_entry + DF_REF_ID (def));
Jan Hubicka committed
209
    }
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

  /* UD chains of uninitialized REGs are empty.  Keeping all uses of
     the same uninitialized REG in a single web is not necessary for
     correctness, since the uses are undefined, but it's wasteful to
     allocate one register or slot for each reference.  Furthermore,
     creating new pseudos for uninitialized references in debug insns
     (see PR 42631) causes -fcompare-debug failures.  We record the
     number of the first uninitialized reference we found, and merge
     with it any other uninitialized references to the same
     register.  */
  if (!link)
    {
      int regno = REGNO (DF_REF_REAL_REG (use));
      if (used[regno])
	(*fun) (use_entry + DF_REF_ID (use), use_entry + used[regno] - 2);
      else
	used[regno] = DF_REF_ID (use) + 2;
    }

Jan Hubicka committed
229 230
  while (link)
    {
Razya Ladelsky committed
231 232
      (*fun) (use_entry + DF_REF_ID (use),
	      def_entry + DF_REF_ID (link->ref));
Jan Hubicka committed
233 234 235
      link = link->next;
    }

236
  /* A READ_WRITE use requires the corresponding def to be in the same
Jan Hubicka committed
237
     register.  Find it and union.  */
238
  if (DF_REF_FLAGS (use) & DF_REF_READ_WRITE)
239 240 241
    if (insn_info)
      {
	df_ref def;
Razya Ladelsky committed
242

243 244 245 246
	FOR_EACH_INSN_INFO_DEF (def, insn_info)
	  if (DF_REF_REAL_REG (use) == DF_REF_REAL_REG (def))
	    (*fun) (use_entry + DF_REF_ID (use), def_entry + DF_REF_ID (def));
      }
Jan Hubicka committed
247 248
}

249
/* Find the corresponding register for the given entry.  */
Jan Hubicka committed
250 251

static rtx
252
entry_register (web_entry *entry, df_ref ref, unsigned int *used)
Jan Hubicka committed
253
{
254
  web_entry *root;
Jan Hubicka committed
255 256
  rtx reg, newreg;

257
  /* Find the corresponding web and see if it has been visited.  */
258 259 260
  root = (web_entry *)entry->unionfind_root ();
  if (root->reg ())
    return root->reg ();
Jan Hubicka committed
261

262
  /* We are seeing this web for the first time, do the assignment.  */
Jan Hubicka committed
263 264
  reg = DF_REF_REAL_REG (ref);

265 266 267 268 269 270 271
  /* In case the original register is already assigned, generate new
     one.  Since we use USED to merge uninitialized refs into a single
     web, we might found an element to be nonzero without our having
     used it.  Test for 1, because union_defs saves it for our use,
     and there won't be any use for the other values when we get to
     this point.  */
  if (used[REGNO (reg)] != 1)
272
    newreg = reg, used[REGNO (reg)] = 1;
Jan Hubicka committed
273 274 275 276 277 278
  else
    {
      newreg = gen_reg_rtx (GET_MODE (reg));
      REG_USERVAR_P (newreg) = REG_USERVAR_P (reg);
      REG_POINTER (newreg) = REG_POINTER (reg);
      REG_ATTRS (newreg) = REG_ATTRS (reg);
279 280
      if (dump_file)
	fprintf (dump_file, "Web oldreg=%i newreg=%i\n", REGNO (reg),
Jan Hubicka committed
281 282 283
		 REGNO (newreg));
    }

284
  root->set_reg (newreg);
Jan Hubicka committed
285 286 287 288 289 290
  return newreg;
}

/* Replace the reference by REG.  */

static void
291
replace_ref (df_ref ref, rtx reg)
Jan Hubicka committed
292 293 294
{
  rtx oldreg = DF_REF_REAL_REG (ref);
  rtx *loc = DF_REF_REAL_LOC (ref);
295
  unsigned int uid = DF_REF_INSN_UID (ref);
Jan Hubicka committed
296 297 298

  if (oldreg == reg)
    return;
299 300
  if (dump_file)
    fprintf (dump_file, "Updating insn %i (%i->%i)\n",
H.J. Lu committed
301
	     uid, REGNO (oldreg), REGNO (reg));
Jan Hubicka committed
302
  *loc = reg;
303 304 305 306
  df_insn_rescan (DF_REF_INSN (ref));
}


307 308 309
namespace {

const pass_data pass_data_web =
310 311 312 313 314 315 316 317 318
{
  RTL_PASS, /* type */
  "web", /* name */
  OPTGROUP_NONE, /* optinfo_flags */
  TV_WEB, /* tv_id */
  0, /* properties_required */
  0, /* properties_provided */
  0, /* properties_destroyed */
  0, /* todo_flags_start */
319
  TODO_df_finish, /* todo_flags_finish */
320 321
};

322
class pass_web : public rtl_opt_pass
323 324 325 326 327 328 329 330 331
{
public:
  pass_web (gcc::context *ctxt)
    : rtl_opt_pass (pass_data_web, ctxt)
  {}

  /* opt_pass methods: */
  virtual bool gate (function *) { return (optimize > 0 && flag_web); }
  virtual unsigned int execute (function *);
Jan Hubicka committed
332

333 334 335 336
}; // class pass_web

unsigned int
pass_web::execute (function *fun)
Jan Hubicka committed
337
{
338 339
  web_entry *def_entry;
  web_entry *use_entry;
340
  unsigned int max = max_reg_num ();
341
  unsigned int *used;
342 343
  basic_block bb;
  unsigned int uses_num = 0;
David Malcolm committed
344
  rtx_insn *insn;
345 346

  df_set_flags (DF_NO_HARD_REGS + DF_EQ_NOTES);
347
  df_set_flags (DF_RD_PRUNE_DEAD_DEFS);
348 349 350 351 352
  df_chain_add_problem (DF_UD_CHAIN);
  df_analyze ();
  df_set_flags (DF_DEFER_INSN_RESCAN);

  /* Assign ids to the uses.  */
353
  FOR_ALL_BB_FN (bb, fun)
354 355
    FOR_BB_INSNS (bb, insn)
    {
356
      if (NONDEBUG_INSN_P (insn))
357
	{
358 359 360 361 362 363 364 365
	  struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
	  df_ref use;
	  FOR_EACH_INSN_INFO_USE (use, insn_info)
	    if (DF_REF_REGNO (use) >= FIRST_PSEUDO_REGISTER)
	      DF_REF_ID (use) = uses_num++;
	  FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
	    if (DF_REF_REGNO (use) >= FIRST_PSEUDO_REGISTER)
	      DF_REF_ID (use) = uses_num++;
366 367
	}
    }
Jan Hubicka committed
368

369
  /* Record the number of uses and defs at the beginning of the optimization.  */
370
  def_entry = XCNEWVEC (web_entry, DF_DEFS_TABLE_SIZE ());
371
  used = XCNEWVEC (unsigned, max);
372
  use_entry = XCNEWVEC (web_entry, uses_num);
Jan Hubicka committed
373 374

  /* Produce the web.  */
375
  FOR_ALL_BB_FN (bb, fun)
376
    FOR_BB_INSNS (bb, insn)
377
      if (NONDEBUG_INSN_P (insn))
378
	{
379 380
	  struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
	  df_ref use;
381
	  union_match_dups (insn, def_entry, use_entry, unionfind_union);
382 383 384 385 386 387
	  FOR_EACH_INSN_INFO_USE (use, insn_info)
	    if (DF_REF_REGNO (use) >= FIRST_PSEUDO_REGISTER)
	      union_defs (use, def_entry, used, use_entry, unionfind_union);
	  FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
	    if (DF_REF_REGNO (use) >= FIRST_PSEUDO_REGISTER)
	      union_defs (use, def_entry, used, use_entry, unionfind_union);
388
	}
Jan Hubicka committed
389 390 391

  /* Update the instruction stream, allocating new registers for split pseudos
     in progress.  */
392
  FOR_ALL_BB_FN (bb, fun)
393
    FOR_BB_INSNS (bb, insn)
394 395 396 397 398 399 400 401 402 403
      if (NONDEBUG_INSN_P (insn)
	  /* Ignore naked clobber.  For example, reg 134 in the second insn
	     of the following sequence will not be replaced.

	       (insn (clobber (reg:SI 134)))

	       (insn (set (reg:SI 0 r0) (reg:SI 134)))

	     Thus the later passes can optimize them away.  */
	  && GET_CODE (PATTERN (insn)) != CLOBBER)
404
	{
405 406 407 408 409 410 411 412 413 414 415 416 417 418
	  struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
	  df_ref def, use;
	  FOR_EACH_INSN_INFO_USE (use, insn_info)
	    if (DF_REF_REGNO (use) >= FIRST_PSEUDO_REGISTER)
	      replace_ref (use, entry_register (use_entry + DF_REF_ID (use),
						use, used));
	  FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
	    if (DF_REF_REGNO (use) >= FIRST_PSEUDO_REGISTER)
	      replace_ref (use, entry_register (use_entry + DF_REF_ID (use),
						use, used));
	  FOR_EACH_INSN_INFO_DEF (def, insn_info)
	    if (DF_REF_REGNO (def) >= FIRST_PSEUDO_REGISTER)
	      replace_ref (def, entry_register (def_entry + DF_REF_ID (def),
						def, used));
419 420
	}

Jan Hubicka committed
421 422 423
  free (def_entry);
  free (use_entry);
  free (used);
424
  return 0;
425
}
426

427 428
} // anon namespace

429 430 431 432 433
rtl_opt_pass *
make_pass_web (gcc::context *ctxt)
{
  return new pass_web (ctxt);
}