web.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /* Web construction code for GNU compiler.
  2. Contributed by Jan Hubicka.
  3. Copyright (C) 2001-2015 Free Software Foundation, Inc.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. /* Simple optimization pass that splits independent uses of each pseudo,
  17. increasing effectiveness of other optimizations. The optimization can
  18. serve as an example of use for the dataflow module.
  19. We don't split registers with REG_USERVAR set unless -fmessy-debugging
  20. is specified, because debugging information about such split variables
  21. is almost unusable.
  22. TODO
  23. - We may use profile information and ignore infrequent use for the
  24. purpose of web unifying, inserting the compensation code later to
  25. implement full induction variable expansion for loops (currently
  26. we expand only if the induction variable is dead afterward, which
  27. is often the case). */
  28. #include "config.h"
  29. #include "system.h"
  30. #include "coretypes.h"
  31. #include "tm.h"
  32. #include "diagnostic-core.h"
  33. #include "rtl.h"
  34. #include "hard-reg-set.h"
  35. #include "flags.h"
  36. #include "obstack.h"
  37. #include "predict.h"
  38. #include "vec.h"
  39. #include "hashtab.h"
  40. #include "hash-set.h"
  41. #include "machmode.h"
  42. #include "input.h"
  43. #include "function.h"
  44. #include "dominance.h"
  45. #include "cfg.h"
  46. #include "basic-block.h"
  47. #include "df.h"
  48. #include "insn-config.h"
  49. #include "recog.h"
  50. #include "tree-pass.h"
  51. /* Find the root of unionfind tree (the representative of set). */
  52. web_entry_base *
  53. web_entry_base::unionfind_root ()
  54. {
  55. web_entry_base *element = this, *element1 = this, *element2;
  56. while (element->pred ())
  57. element = element->pred ();
  58. while (element1->pred ())
  59. {
  60. element2 = element1->pred ();
  61. element1->set_pred (element);
  62. element1 = element2;
  63. }
  64. return element;
  65. }
  66. /* Union sets.
  67. Return true if FIRST and SECOND points to the same web entry structure and
  68. nothing is done. Otherwise, return false. */
  69. bool
  70. unionfind_union (web_entry_base *first, web_entry_base *second)
  71. {
  72. first = first->unionfind_root ();
  73. second = second->unionfind_root ();
  74. if (first == second)
  75. return true;
  76. second->set_pred (first);
  77. return false;
  78. }
  79. class web_entry : public web_entry_base
  80. {
  81. private:
  82. rtx reg_pvt;
  83. public:
  84. rtx reg () { return reg_pvt; }
  85. void set_reg (rtx r) { reg_pvt = r; }
  86. };
  87. /* For INSN, union all defs and uses that are linked by match_dup.
  88. FUN is the function that does the union. */
  89. static void
  90. union_match_dups (rtx_insn *insn, web_entry *def_entry, web_entry *use_entry,
  91. bool (*fun) (web_entry_base *, web_entry_base *))
  92. {
  93. struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
  94. df_ref use_link = DF_INSN_INFO_USES (insn_info);
  95. df_ref def_link = DF_INSN_INFO_DEFS (insn_info);
  96. struct web_entry *dup_entry;
  97. int i;
  98. extract_insn (insn);
  99. for (i = 0; i < recog_data.n_dups; i++)
  100. {
  101. int op = recog_data.dup_num[i];
  102. enum op_type type = recog_data.operand_type[op];
  103. df_ref ref, dupref;
  104. struct web_entry *entry;
  105. dup_entry = use_entry;
  106. for (dupref = use_link; dupref; dupref = DF_REF_NEXT_LOC (dupref))
  107. if (DF_REF_LOC (dupref) == recog_data.dup_loc[i])
  108. break;
  109. if (dupref == NULL && type == OP_INOUT)
  110. {
  111. dup_entry = def_entry;
  112. for (dupref = def_link; dupref; dupref = DF_REF_NEXT_LOC (dupref))
  113. if (DF_REF_LOC (dupref) == recog_data.dup_loc[i])
  114. break;
  115. }
  116. /* ??? *DUPREF can still be zero, because when an operand matches
  117. a memory, DF_REF_LOC (use_link[n]) points to the register part
  118. of the address, whereas recog_data.dup_loc[m] points to the
  119. entire memory ref, thus we fail to find the duplicate entry,
  120. even though it is there.
  121. Example: i686-pc-linux-gnu gcc.c-torture/compile/950607-1.c
  122. -O3 -fomit-frame-pointer -funroll-loops */
  123. if (dupref == NULL
  124. || DF_REF_REGNO (dupref) < FIRST_PSEUDO_REGISTER)
  125. continue;
  126. ref = type == OP_IN ? use_link : def_link;
  127. entry = type == OP_IN ? use_entry : def_entry;
  128. for (; ref; ref = DF_REF_NEXT_LOC (ref))
  129. {
  130. rtx *l = DF_REF_LOC (ref);
  131. if (l == recog_data.operand_loc[op])
  132. break;
  133. if (l && DF_REF_REAL_LOC (ref) == recog_data.operand_loc[op])
  134. break;
  135. }
  136. if (!ref && type == OP_INOUT)
  137. {
  138. entry = use_entry;
  139. for (ref = use_link; ref; ref = DF_REF_NEXT_LOC (ref))
  140. {
  141. rtx *l = DF_REF_LOC (ref);
  142. if (l == recog_data.operand_loc[op])
  143. break;
  144. if (l && DF_REF_REAL_LOC (ref) == recog_data.operand_loc[op])
  145. break;
  146. }
  147. }
  148. gcc_assert (ref);
  149. (*fun) (dup_entry + DF_REF_ID (dupref), entry + DF_REF_ID (ref));
  150. }
  151. }
  152. /* For each use, all possible defs reaching it must come in the same
  153. register, union them.
  154. FUN is the function that does the union.
  155. In USED, we keep the DF_REF_ID of the first uninitialized uses of a
  156. register, so that all uninitialized uses of the register can be
  157. combined into a single web. We actually offset it by 2, because
  158. the values 0 and 1 are reserved for use by entry_register. */
  159. void
  160. union_defs (df_ref use, web_entry *def_entry,
  161. unsigned int *used, web_entry *use_entry,
  162. bool (*fun) (web_entry_base *, web_entry_base *))
  163. {
  164. struct df_insn_info *insn_info = DF_REF_INSN_INFO (use);
  165. struct df_link *link = DF_REF_CHAIN (use);
  166. rtx set;
  167. if (insn_info)
  168. {
  169. df_ref eq_use;
  170. set = single_set (insn_info->insn);
  171. FOR_EACH_INSN_INFO_EQ_USE (eq_use, insn_info)
  172. if (use != eq_use
  173. && DF_REF_REAL_REG (use) == DF_REF_REAL_REG (eq_use))
  174. (*fun) (use_entry + DF_REF_ID (use), use_entry + DF_REF_ID (eq_use));
  175. }
  176. else
  177. set = NULL;
  178. /* Union all occurrences of the same register in reg notes. */
  179. /* Recognize trivial noop moves and attempt to keep them as noop. */
  180. if (set
  181. && SET_SRC (set) == DF_REF_REG (use)
  182. && SET_SRC (set) == SET_DEST (set))
  183. {
  184. df_ref def;
  185. FOR_EACH_INSN_INFO_DEF (def, insn_info)
  186. if (DF_REF_REAL_REG (use) == DF_REF_REAL_REG (def))
  187. (*fun) (use_entry + DF_REF_ID (use), def_entry + DF_REF_ID (def));
  188. }
  189. /* UD chains of uninitialized REGs are empty. Keeping all uses of
  190. the same uninitialized REG in a single web is not necessary for
  191. correctness, since the uses are undefined, but it's wasteful to
  192. allocate one register or slot for each reference. Furthermore,
  193. creating new pseudos for uninitialized references in debug insns
  194. (see PR 42631) causes -fcompare-debug failures. We record the
  195. number of the first uninitialized reference we found, and merge
  196. with it any other uninitialized references to the same
  197. register. */
  198. if (!link)
  199. {
  200. int regno = REGNO (DF_REF_REAL_REG (use));
  201. if (used[regno])
  202. (*fun) (use_entry + DF_REF_ID (use), use_entry + used[regno] - 2);
  203. else
  204. used[regno] = DF_REF_ID (use) + 2;
  205. }
  206. while (link)
  207. {
  208. (*fun) (use_entry + DF_REF_ID (use),
  209. def_entry + DF_REF_ID (link->ref));
  210. link = link->next;
  211. }
  212. /* A READ_WRITE use requires the corresponding def to be in the same
  213. register. Find it and union. */
  214. if (DF_REF_FLAGS (use) & DF_REF_READ_WRITE)
  215. if (insn_info)
  216. {
  217. df_ref def;
  218. FOR_EACH_INSN_INFO_DEF (def, insn_info)
  219. if (DF_REF_REAL_REG (use) == DF_REF_REAL_REG (def))
  220. (*fun) (use_entry + DF_REF_ID (use), def_entry + DF_REF_ID (def));
  221. }
  222. }
  223. /* Find the corresponding register for the given entry. */
  224. static rtx
  225. entry_register (web_entry *entry, df_ref ref, unsigned int *used)
  226. {
  227. web_entry *root;
  228. rtx reg, newreg;
  229. /* Find the corresponding web and see if it has been visited. */
  230. root = (web_entry *)entry->unionfind_root ();
  231. if (root->reg ())
  232. return root->reg ();
  233. /* We are seeing this web for the first time, do the assignment. */
  234. reg = DF_REF_REAL_REG (ref);
  235. /* In case the original register is already assigned, generate new
  236. one. Since we use USED to merge uninitialized refs into a single
  237. web, we might found an element to be nonzero without our having
  238. used it. Test for 1, because union_defs saves it for our use,
  239. and there won't be any use for the other values when we get to
  240. this point. */
  241. if (used[REGNO (reg)] != 1)
  242. newreg = reg, used[REGNO (reg)] = 1;
  243. else
  244. {
  245. newreg = gen_reg_rtx (GET_MODE (reg));
  246. REG_USERVAR_P (newreg) = REG_USERVAR_P (reg);
  247. REG_POINTER (newreg) = REG_POINTER (reg);
  248. REG_ATTRS (newreg) = REG_ATTRS (reg);
  249. if (dump_file)
  250. fprintf (dump_file, "Web oldreg=%i newreg=%i\n", REGNO (reg),
  251. REGNO (newreg));
  252. }
  253. root->set_reg (newreg);
  254. return newreg;
  255. }
  256. /* Replace the reference by REG. */
  257. static void
  258. replace_ref (df_ref ref, rtx reg)
  259. {
  260. rtx oldreg = DF_REF_REAL_REG (ref);
  261. rtx *loc = DF_REF_REAL_LOC (ref);
  262. unsigned int uid = DF_REF_INSN_UID (ref);
  263. if (oldreg == reg)
  264. return;
  265. if (dump_file)
  266. fprintf (dump_file, "Updating insn %i (%i->%i)\n",
  267. uid, REGNO (oldreg), REGNO (reg));
  268. *loc = reg;
  269. df_insn_rescan (DF_REF_INSN (ref));
  270. }
  271. namespace {
  272. const pass_data pass_data_web =
  273. {
  274. RTL_PASS, /* type */
  275. "web", /* name */
  276. OPTGROUP_NONE, /* optinfo_flags */
  277. TV_WEB, /* tv_id */
  278. 0, /* properties_required */
  279. 0, /* properties_provided */
  280. 0, /* properties_destroyed */
  281. 0, /* todo_flags_start */
  282. TODO_df_finish, /* todo_flags_finish */
  283. };
  284. class pass_web : public rtl_opt_pass
  285. {
  286. public:
  287. pass_web (gcc::context *ctxt)
  288. : rtl_opt_pass (pass_data_web, ctxt)
  289. {}
  290. /* opt_pass methods: */
  291. virtual bool gate (function *) { return (optimize > 0 && flag_web); }
  292. virtual unsigned int execute (function *);
  293. }; // class pass_web
  294. unsigned int
  295. pass_web::execute (function *fun)
  296. {
  297. web_entry *def_entry;
  298. web_entry *use_entry;
  299. unsigned int max = max_reg_num ();
  300. unsigned int *used;
  301. basic_block bb;
  302. unsigned int uses_num = 0;
  303. rtx_insn *insn;
  304. df_set_flags (DF_NO_HARD_REGS + DF_EQ_NOTES);
  305. df_set_flags (DF_RD_PRUNE_DEAD_DEFS);
  306. df_chain_add_problem (DF_UD_CHAIN);
  307. df_analyze ();
  308. df_set_flags (DF_DEFER_INSN_RESCAN);
  309. /* Assign ids to the uses. */
  310. FOR_ALL_BB_FN (bb, fun)
  311. FOR_BB_INSNS (bb, insn)
  312. {
  313. if (NONDEBUG_INSN_P (insn))
  314. {
  315. struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
  316. df_ref use;
  317. FOR_EACH_INSN_INFO_USE (use, insn_info)
  318. if (DF_REF_REGNO (use) >= FIRST_PSEUDO_REGISTER)
  319. DF_REF_ID (use) = uses_num++;
  320. FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
  321. if (DF_REF_REGNO (use) >= FIRST_PSEUDO_REGISTER)
  322. DF_REF_ID (use) = uses_num++;
  323. }
  324. }
  325. /* Record the number of uses and defs at the beginning of the optimization. */
  326. def_entry = XCNEWVEC (web_entry, DF_DEFS_TABLE_SIZE ());
  327. used = XCNEWVEC (unsigned, max);
  328. use_entry = XCNEWVEC (web_entry, uses_num);
  329. /* Produce the web. */
  330. FOR_ALL_BB_FN (bb, fun)
  331. FOR_BB_INSNS (bb, insn)
  332. if (NONDEBUG_INSN_P (insn))
  333. {
  334. struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
  335. df_ref use;
  336. union_match_dups (insn, def_entry, use_entry, unionfind_union);
  337. FOR_EACH_INSN_INFO_USE (use, insn_info)
  338. if (DF_REF_REGNO (use) >= FIRST_PSEUDO_REGISTER)
  339. union_defs (use, def_entry, used, use_entry, unionfind_union);
  340. FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
  341. if (DF_REF_REGNO (use) >= FIRST_PSEUDO_REGISTER)
  342. union_defs (use, def_entry, used, use_entry, unionfind_union);
  343. }
  344. /* Update the instruction stream, allocating new registers for split pseudos
  345. in progress. */
  346. FOR_ALL_BB_FN (bb, fun)
  347. FOR_BB_INSNS (bb, insn)
  348. if (NONDEBUG_INSN_P (insn)
  349. /* Ignore naked clobber. For example, reg 134 in the second insn
  350. of the following sequence will not be replaced.
  351. (insn (clobber (reg:SI 134)))
  352. (insn (set (reg:SI 0 r0) (reg:SI 134)))
  353. Thus the later passes can optimize them away. */
  354. && GET_CODE (PATTERN (insn)) != CLOBBER)
  355. {
  356. struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
  357. df_ref def, use;
  358. FOR_EACH_INSN_INFO_USE (use, insn_info)
  359. if (DF_REF_REGNO (use) >= FIRST_PSEUDO_REGISTER)
  360. replace_ref (use, entry_register (use_entry + DF_REF_ID (use),
  361. use, used));
  362. FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
  363. if (DF_REF_REGNO (use) >= FIRST_PSEUDO_REGISTER)
  364. replace_ref (use, entry_register (use_entry + DF_REF_ID (use),
  365. use, used));
  366. FOR_EACH_INSN_INFO_DEF (def, insn_info)
  367. if (DF_REF_REGNO (def) >= FIRST_PSEUDO_REGISTER)
  368. replace_ref (def, entry_register (def_entry + DF_REF_ID (def),
  369. def, used));
  370. }
  371. free (def_entry);
  372. free (use_entry);
  373. free (used);
  374. return 0;
  375. }
  376. } // anon namespace
  377. rtl_opt_pass *
  378. make_pass_web (gcc::context *ctxt)
  379. {
  380. return new pass_web (ctxt);
  381. }