web.c 12.6 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

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

#include "flags.h"
45 46
#include "insn-config.h"
#include "recog.h"
47
#include "tree-pass.h"
Jan Hubicka committed
48 49


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

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

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

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

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

83 84 85 86 87 88 89 90 91 92
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; }
};

93 94 95 96
/* 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
97
union_match_dups (rtx_insn *insn, web_entry *def_entry, web_entry *use_entry,
98
		  bool (*fun) (web_entry_base *, web_entry_base *))
99 100
{
  struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
101 102
  df_ref use_link = DF_INSN_INFO_USES (insn_info);
  df_ref def_link = DF_INSN_INFO_DEFS (insn_info);
103
  struct web_entry *dup_entry;
104 105 106 107 108 109 110 111
  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];
112
      df_ref ref, dupref;
113 114
      struct web_entry *entry;

115 116 117
      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])
118 119
	  break;

120
      if (dupref == NULL && type == OP_INOUT)
121
	{
122 123 124
	  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])
125 126 127 128 129 130 131 132 133
	      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  */
134 135
      if (dupref == NULL
	  || DF_REF_REGNO (dupref) < FIRST_PSEUDO_REGISTER)
136 137 138 139
	continue;

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

149
      if (!ref && type == OP_INOUT)
150
	{
151 152
	  entry = use_entry;
	  for (ref = use_link; 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
	}

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

167
/* For each use, all possible defs reaching it must come in the same
Razya Ladelsky committed
168
   register, union them.
169 170 171 172 173 174
   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
175

Razya Ladelsky committed
176
void
177 178 179
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
180
{
181
  struct df_insn_info *insn_info = DF_REF_INSN_INFO (use);
Jan Hubicka committed
182
  struct df_link *link = DF_REF_CHAIN (use);
Razya Ladelsky committed
183 184
  rtx set;

185
  if (insn_info)
Razya Ladelsky committed
186
    {
187 188 189 190 191 192 193
      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
194 195
    }
  else
196
    set = NULL;
Jan Hubicka committed
197

198
  /* Union all occurrences of the same register in reg notes.  */
199

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

  if (set
      && SET_SRC (set) == DF_REF_REG (use)
      && SET_SRC (set) == SET_DEST (set))
    {
206 207 208 209 210
      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
211
    }
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

  /* 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
231 232
  while (link)
    {
Razya Ladelsky committed
233 234
      (*fun) (use_entry + DF_REF_ID (use),
	      def_entry + DF_REF_ID (link->ref));
Jan Hubicka committed
235 236 237
      link = link->next;
    }

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

245 246 247 248
	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
249 250
}

251
/* Find the corresponding register for the given entry.  */
Jan Hubicka committed
252 253

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

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

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

267 268 269 270 271 272 273
  /* 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)
274
    newreg = reg, used[REGNO (reg)] = 1;
Jan Hubicka committed
275 276 277 278 279 280
  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);
281 282
      if (dump_file)
	fprintf (dump_file, "Web oldreg=%i newreg=%i\n", REGNO (reg),
Jan Hubicka committed
283 284 285
		 REGNO (newreg));
    }

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

/* Replace the reference by REG.  */

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

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


309 310 311 312 313 314 315 316 317 318 319 320
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 */
321
  TODO_df_finish, /* todo_flags_finish */
322 323 324 325 326 327 328 329 330 331 332 333
};

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
334

335 336 337 338
}; // class pass_web

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

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

  /* Assign ids to the uses.  */
355
  FOR_ALL_BB_FN (bb, fun)
356 357
    FOR_BB_INSNS (bb, insn)
    {
358
      if (NONDEBUG_INSN_P (insn))
359
	{
360 361 362 363 364 365 366 367
	  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++;
368 369
	}
    }
Jan Hubicka committed
370

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

  /* Produce the web.  */
377
  FOR_ALL_BB_FN (bb, fun)
378
    FOR_BB_INSNS (bb, insn)
379
      if (NONDEBUG_INSN_P (insn))
380
	{
381 382
	  struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
	  df_ref use;
383
	  union_match_dups (insn, def_entry, use_entry, unionfind_union);
384 385 386 387 388 389
	  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);
390
	}
Jan Hubicka committed
391 392 393

  /* Update the instruction stream, allocating new registers for split pseudos
     in progress.  */
394
  FOR_ALL_BB_FN (bb, fun)
395
    FOR_BB_INSNS (bb, insn)
396 397 398 399 400 401 402 403 404 405
      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)
406
	{
407 408 409 410 411 412 413 414 415 416 417 418 419 420
	  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));
421 422
	}

Jan Hubicka committed
423 424 425
  free (def_entry);
  free (use_entry);
  free (used);
426
  return 0;
427
}
428

429 430 431 432 433 434 435
} // anon namespace

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