web.c 12.9 KB
Newer Older
Jan Hubicka committed
1
/* Web construction code for GNU compiler.
2
   Contributed by Jan Hubicka.
Jakub Jelinek committed
3
   Copyright (C) 2001-2015 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 39

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
40
#include "diagnostic-core.h"
Jan Hubicka committed
41 42 43 44

#include "rtl.h"
#include "hard-reg-set.h"
#include "flags.h"
45
#include "obstack.h"
46 47
#include "predict.h"
#include "vec.h"
48 49 50 51
#include "hashtab.h"
#include "hash-set.h"
#include "machmode.h"
#include "input.h"
Jan Hubicka committed
52
#include "function.h"
53 54 55 56
#include "dominance.h"
#include "cfg.h"
#include "basic-block.h"
#include "df.h"
57 58
#include "insn-config.h"
#include "recog.h"
59
#include "tree-pass.h"
Jan Hubicka committed
60 61


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

64 65
web_entry_base *
web_entry_base::unionfind_root ()
Jan Hubicka committed
66
{
67
  web_entry_base *element = this, *element1 = this, *element2;
Jan Hubicka committed
68

69 70 71
  while (element->pred ())
    element = element->pred ();
  while (element1->pred ())
Jan Hubicka committed
72
    {
73 74
      element2 = element1->pred ();
      element1->set_pred (element);
Jan Hubicka committed
75 76 77 78 79
      element1 = element2;
    }
  return element;
}

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

Razya Ladelsky committed
84
bool
85
unionfind_union (web_entry_base *first, web_entry_base *second)
Jan Hubicka committed
86
{
87 88
  first = first->unionfind_root ();
  second = second->unionfind_root ();
Jan Hubicka committed
89
  if (first == second)
Razya Ladelsky committed
90
    return true;
91
  second->set_pred (first);
Razya Ladelsky committed
92
  return false;
Jan Hubicka committed
93 94
}

95 96 97 98 99 100 101 102 103 104
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; }
};

105 106 107 108
/* 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
109
union_match_dups (rtx_insn *insn, web_entry *def_entry, web_entry *use_entry,
110
		  bool (*fun) (web_entry_base *, web_entry_base *))
111 112
{
  struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
113 114
  df_ref use_link = DF_INSN_INFO_USES (insn_info);
  df_ref def_link = DF_INSN_INFO_DEFS (insn_info);
115
  struct web_entry *dup_entry;
116 117 118 119 120 121 122 123
  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];
124
      df_ref ref, dupref;
125 126
      struct web_entry *entry;

127 128 129
      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])
130 131
	  break;

132
      if (dupref == NULL && type == OP_INOUT)
133
	{
134 135 136
	  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])
137 138 139 140 141 142 143 144 145
	      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  */
146 147
      if (dupref == NULL
	  || DF_REF_REGNO (dupref) < FIRST_PSEUDO_REGISTER)
148 149 150 151
	continue;

      ref = type == OP_IN ? use_link : def_link;
      entry = type == OP_IN ? use_entry : def_entry;
152
      for (; ref; ref = DF_REF_NEXT_LOC (ref))
153
	{
154
	  rtx *l = DF_REF_LOC (ref);
155
	  if (l == recog_data.operand_loc[op])
156
	    break;
157
	  if (l && DF_REF_REAL_LOC (ref) == recog_data.operand_loc[op])
158 159
	    break;
	}
160

161
      if (!ref && type == OP_INOUT)
162
	{
163 164
	  entry = use_entry;
	  for (ref = use_link; ref; ref = DF_REF_NEXT_LOC (ref))
165
	    {
166
	      rtx *l = DF_REF_LOC (ref);
167
	      if (l == recog_data.operand_loc[op])
168
		break;
169
	      if (l && DF_REF_REAL_LOC (ref) == recog_data.operand_loc[op])
170 171
		break;
	    }
172 173
	}

174 175
      gcc_assert (ref);
      (*fun) (dup_entry + DF_REF_ID (dupref), entry + DF_REF_ID (ref));
176 177 178
    }
}

179
/* For each use, all possible defs reaching it must come in the same
Razya Ladelsky committed
180
   register, union them.
181 182 183 184 185 186
   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
187

Razya Ladelsky committed
188
void
189 190 191
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
192
{
193
  struct df_insn_info *insn_info = DF_REF_INSN_INFO (use);
Jan Hubicka committed
194
  struct df_link *link = DF_REF_CHAIN (use);
Razya Ladelsky committed
195 196
  rtx set;

197
  if (insn_info)
Razya Ladelsky committed
198
    {
199 200 201 202 203 204 205
      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
206 207
    }
  else
208
    set = NULL;
Jan Hubicka committed
209

210
  /* Union all occurrences of the same register in reg notes.  */
211

Steven Bosscher committed
212
  /* Recognize trivial noop moves and attempt to keep them as noop.  */
Jan Hubicka committed
213 214 215 216 217

  if (set
      && SET_SRC (set) == DF_REF_REG (use)
      && SET_SRC (set) == SET_DEST (set))
    {
218 219 220 221 222
      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
223
    }
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242

  /* 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
243 244
  while (link)
    {
Razya Ladelsky committed
245 246
      (*fun) (use_entry + DF_REF_ID (use),
	      def_entry + DF_REF_ID (link->ref));
Jan Hubicka committed
247 248 249
      link = link->next;
    }

250
  /* A READ_WRITE use requires the corresponding def to be in the same
Jan Hubicka committed
251
     register.  Find it and union.  */
252
  if (DF_REF_FLAGS (use) & DF_REF_READ_WRITE)
253 254 255
    if (insn_info)
      {
	df_ref def;
Razya Ladelsky committed
256

257 258 259 260
	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
261 262
}

263
/* Find the corresponding register for the given entry.  */
Jan Hubicka committed
264 265

static rtx
266
entry_register (web_entry *entry, df_ref ref, unsigned int *used)
Jan Hubicka committed
267
{
268
  web_entry *root;
Jan Hubicka committed
269 270
  rtx reg, newreg;

271
  /* Find the corresponding web and see if it has been visited.  */
272 273 274
  root = (web_entry *)entry->unionfind_root ();
  if (root->reg ())
    return root->reg ();
Jan Hubicka committed
275

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

279 280 281 282 283 284 285
  /* 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)
286
    newreg = reg, used[REGNO (reg)] = 1;
Jan Hubicka committed
287 288 289 290 291 292
  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);
293 294
      if (dump_file)
	fprintf (dump_file, "Web oldreg=%i newreg=%i\n", REGNO (reg),
Jan Hubicka committed
295 296 297
		 REGNO (newreg));
    }

298
  root->set_reg (newreg);
Jan Hubicka committed
299 300 301 302 303 304
  return newreg;
}

/* Replace the reference by REG.  */

static void
305
replace_ref (df_ref ref, rtx reg)
Jan Hubicka committed
306 307 308
{
  rtx oldreg = DF_REF_REAL_REG (ref);
  rtx *loc = DF_REF_REAL_LOC (ref);
309
  unsigned int uid = DF_REF_INSN_UID (ref);
Jan Hubicka committed
310 311 312

  if (oldreg == reg)
    return;
313 314
  if (dump_file)
    fprintf (dump_file, "Updating insn %i (%i->%i)\n",
H.J. Lu committed
315
	     uid, REGNO (oldreg), REGNO (reg));
Jan Hubicka committed
316
  *loc = reg;
317 318 319 320
  df_insn_rescan (DF_REF_INSN (ref));
}


321 322 323 324 325 326 327 328 329 330 331 332
namespace {

const pass_data pass_data_web =
{
  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 */
333
  TODO_df_finish, /* todo_flags_finish */
334 335 336 337 338 339 340 341 342 343 344 345
};

class pass_web : public rtl_opt_pass
{
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
346

347 348 349 350
}; // class pass_web

unsigned int
pass_web::execute (function *fun)
Jan Hubicka committed
351
{
352 353
  web_entry *def_entry;
  web_entry *use_entry;
354
  unsigned int max = max_reg_num ();
355
  unsigned int *used;
356 357
  basic_block bb;
  unsigned int uses_num = 0;
David Malcolm committed
358
  rtx_insn *insn;
359 360

  df_set_flags (DF_NO_HARD_REGS + DF_EQ_NOTES);
361
  df_set_flags (DF_RD_PRUNE_DEAD_DEFS);
362 363 364 365 366
  df_chain_add_problem (DF_UD_CHAIN);
  df_analyze ();
  df_set_flags (DF_DEFER_INSN_RESCAN);

  /* Assign ids to the uses.  */
367
  FOR_ALL_BB_FN (bb, fun)
368 369
    FOR_BB_INSNS (bb, insn)
    {
370
      if (NONDEBUG_INSN_P (insn))
371
	{
372 373 374 375 376 377 378 379
	  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++;
380 381
	}
    }
Jan Hubicka committed
382

383
  /* Record the number of uses and defs at the beginning of the optimization.  */
384
  def_entry = XCNEWVEC (web_entry, DF_DEFS_TABLE_SIZE ());
385
  used = XCNEWVEC (unsigned, max);
386
  use_entry = XCNEWVEC (web_entry, uses_num);
Jan Hubicka committed
387 388

  /* Produce the web.  */
389
  FOR_ALL_BB_FN (bb, fun)
390
    FOR_BB_INSNS (bb, insn)
391
      if (NONDEBUG_INSN_P (insn))
392
	{
393 394
	  struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
	  df_ref use;
395
	  union_match_dups (insn, def_entry, use_entry, unionfind_union);
396 397 398 399 400 401
	  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);
402
	}
Jan Hubicka committed
403 404 405

  /* Update the instruction stream, allocating new registers for split pseudos
     in progress.  */
406
  FOR_ALL_BB_FN (bb, fun)
407
    FOR_BB_INSNS (bb, insn)
408 409 410 411 412 413 414 415 416 417
      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)
418
	{
419 420 421 422 423 424 425 426 427 428 429 430 431 432
	  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));
433 434
	}

Jan Hubicka committed
435 436 437
  free (def_entry);
  free (use_entry);
  free (used);
438
  return 0;
439
}
440

441 442 443 444 445 446 447
} // anon namespace

rtl_opt_pass *
make_pass_web (gcc::context *ctxt)
{
  return new pass_web (ctxt);
}