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-2014 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"
Jan Hubicka committed
46 47 48
#include "basic-block.h"
#include "df.h"
#include "function.h"
49 50
#include "insn-config.h"
#include "recog.h"
51
#include "tree-pass.h"
Jan Hubicka committed
52 53


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

Razya Ladelsky committed
56
struct web_entry *
R. Kelley Cook committed
57
unionfind_root (struct web_entry *element)
Jan Hubicka committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71
{
  struct web_entry *element1 = element, *element2;

  while (element->pred)
    element = element->pred;
  while (element1->pred)
    {
      element2 = element1->pred;
      element1->pred = element;
      element1 = element2;
    }
  return element;
}

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

Razya Ladelsky committed
76
bool
R. Kelley Cook committed
77
unionfind_union (struct web_entry *first, struct web_entry *second)
Jan Hubicka committed
78 79 80 81
{
  first = unionfind_root (first);
  second = unionfind_root (second);
  if (first == second)
Razya Ladelsky committed
82
    return true;
Jan Hubicka committed
83
  second->pred = first;
Razya Ladelsky committed
84
  return false;
Jan Hubicka committed
85 86
}

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

static void
union_match_dups (rtx insn, struct web_entry *def_entry,
		  struct web_entry *use_entry,
		  bool (*fun) (struct web_entry *, struct web_entry *))
{
  struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
96 97
  df_ref use_link = DF_INSN_INFO_USES (insn_info);
  df_ref def_link = DF_INSN_INFO_DEFS (insn_info);
98
  struct web_entry *dup_entry;
99 100 101 102 103 104 105 106
  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];
107
      df_ref ref, dupref;
108 109
      struct web_entry *entry;

110 111 112
      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])
113 114
	  break;

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

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

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

157 158
      gcc_assert (ref);
      (*fun) (dup_entry + DF_REF_ID (dupref), entry + DF_REF_ID (ref));
159 160 161
    }
}

162
/* For each use, all possible defs reaching it must come in the same
Razya Ladelsky committed
163
   register, union them.
164 165 166 167 168 169
   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
170

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

180
  if (insn_info)
Razya Ladelsky committed
181
    {
182 183 184 185 186 187 188
      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
189 190
    }
  else
191
    set = NULL;
Jan Hubicka committed
192

193
  /* Union all occurrences of the same register in reg notes.  */
194

Steven Bosscher committed
195
  /* Recognize trivial noop moves and attempt to keep them as noop.  */
Jan Hubicka committed
196 197 198 199 200

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

  /* 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
226 227
  while (link)
    {
Razya Ladelsky committed
228 229
      (*fun) (use_entry + DF_REF_ID (use),
	      def_entry + DF_REF_ID (link->ref));
Jan Hubicka committed
230 231 232
      link = link->next;
    }

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

240 241 242 243
	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
244 245
}

246
/* Find the corresponding register for the given entry.  */
Jan Hubicka committed
247 248

static rtx
249
entry_register (struct web_entry *entry, df_ref ref, unsigned int *used)
Jan Hubicka committed
250 251 252 253
{
  struct web_entry *root;
  rtx reg, newreg;

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

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

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

  root->reg = newreg;
  return newreg;
}

/* Replace the reference by REG.  */

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

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


304 305 306 307 308 309 310 311 312 313 314 315
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 */
316
  TODO_df_finish, /* todo_flags_finish */
317 318 319 320 321 322 323 324 325 326 327 328
};

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
329

330 331 332 333
}; // class pass_web

unsigned int
pass_web::execute (function *fun)
Jan Hubicka committed
334 335 336
{
  struct web_entry *def_entry;
  struct web_entry *use_entry;
337
  unsigned int max = max_reg_num ();
338
  unsigned int *used;
339 340 341 342 343
  basic_block bb;
  unsigned int uses_num = 0;
  rtx insn;

  df_set_flags (DF_NO_HARD_REGS + DF_EQ_NOTES);
344
  df_set_flags (DF_RD_PRUNE_DEAD_DEFS);
345 346 347 348 349
  df_chain_add_problem (DF_UD_CHAIN);
  df_analyze ();
  df_set_flags (DF_DEFER_INSN_RESCAN);

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

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

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

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

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

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