regcprop.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  1. /* Copy propagation on hard registers for the GNU compiler.
  2. Copyright (C) 2000-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  11. License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "config.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "tm.h"
  19. #include "rtl.h"
  20. #include "tm_p.h"
  21. #include "insn-config.h"
  22. #include "regs.h"
  23. #include "addresses.h"
  24. #include "hard-reg-set.h"
  25. #include "predict.h"
  26. #include "vec.h"
  27. #include "hashtab.h"
  28. #include "hash-set.h"
  29. #include "machmode.h"
  30. #include "input.h"
  31. #include "function.h"
  32. #include "dominance.h"
  33. #include "cfg.h"
  34. #include "basic-block.h"
  35. #include "reload.h"
  36. #include "recog.h"
  37. #include "flags.h"
  38. #include "diagnostic-core.h"
  39. #include "obstack.h"
  40. #include "tree-pass.h"
  41. #include "df.h"
  42. #include "rtl-iter.h"
  43. /* The following code does forward propagation of hard register copies.
  44. The object is to eliminate as many dependencies as possible, so that
  45. we have the most scheduling freedom. As a side effect, we also clean
  46. up some silly register allocation decisions made by reload. This
  47. code may be obsoleted by a new register allocator. */
  48. /* DEBUG_INSNs aren't changed right away, as doing so might extend the
  49. lifetime of a register and get the DEBUG_INSN subsequently reset.
  50. So they are queued instead, and updated only when the register is
  51. used in some subsequent real insn before it is set. */
  52. struct queued_debug_insn_change
  53. {
  54. struct queued_debug_insn_change *next;
  55. rtx_insn *insn;
  56. rtx *loc;
  57. rtx new_rtx;
  58. };
  59. /* For each register, we have a list of registers that contain the same
  60. value. The OLDEST_REGNO field points to the head of the list, and
  61. the NEXT_REGNO field runs through the list. The MODE field indicates
  62. what mode the data is known to be in; this field is VOIDmode when the
  63. register is not known to contain valid data. */
  64. struct value_data_entry
  65. {
  66. machine_mode mode;
  67. unsigned int oldest_regno;
  68. unsigned int next_regno;
  69. struct queued_debug_insn_change *debug_insn_changes;
  70. };
  71. struct value_data
  72. {
  73. struct value_data_entry e[FIRST_PSEUDO_REGISTER];
  74. unsigned int max_value_regs;
  75. unsigned int n_debug_insn_changes;
  76. };
  77. static alloc_pool debug_insn_changes_pool;
  78. static bool skip_debug_insn_p;
  79. static void kill_value_one_regno (unsigned, struct value_data *);
  80. static void kill_value_regno (unsigned, unsigned, struct value_data *);
  81. static void kill_value (const_rtx, struct value_data *);
  82. static void set_value_regno (unsigned, machine_mode, struct value_data *);
  83. static void init_value_data (struct value_data *);
  84. static void kill_clobbered_value (rtx, const_rtx, void *);
  85. static void kill_set_value (rtx, const_rtx, void *);
  86. static void copy_value (rtx, rtx, struct value_data *);
  87. static bool mode_change_ok (machine_mode, machine_mode,
  88. unsigned int);
  89. static rtx maybe_mode_change (machine_mode, machine_mode,
  90. machine_mode, unsigned int, unsigned int);
  91. static rtx find_oldest_value_reg (enum reg_class, rtx, struct value_data *);
  92. static bool replace_oldest_value_reg (rtx *, enum reg_class, rtx_insn *,
  93. struct value_data *);
  94. static bool replace_oldest_value_addr (rtx *, enum reg_class,
  95. machine_mode, addr_space_t,
  96. rtx_insn *, struct value_data *);
  97. static bool replace_oldest_value_mem (rtx, rtx_insn *, struct value_data *);
  98. static bool copyprop_hardreg_forward_1 (basic_block, struct value_data *);
  99. extern void debug_value_data (struct value_data *);
  100. #ifdef ENABLE_CHECKING
  101. static void validate_value_data (struct value_data *);
  102. #endif
  103. /* Free all queued updates for DEBUG_INSNs that change some reg to
  104. register REGNO. */
  105. static void
  106. free_debug_insn_changes (struct value_data *vd, unsigned int regno)
  107. {
  108. struct queued_debug_insn_change *cur, *next;
  109. for (cur = vd->e[regno].debug_insn_changes; cur; cur = next)
  110. {
  111. next = cur->next;
  112. --vd->n_debug_insn_changes;
  113. pool_free (debug_insn_changes_pool, cur);
  114. }
  115. vd->e[regno].debug_insn_changes = NULL;
  116. }
  117. /* Kill register REGNO. This involves removing it from any value
  118. lists, and resetting the value mode to VOIDmode. This is only a
  119. helper function; it does not handle any hard registers overlapping
  120. with REGNO. */
  121. static void
  122. kill_value_one_regno (unsigned int regno, struct value_data *vd)
  123. {
  124. unsigned int i, next;
  125. if (vd->e[regno].oldest_regno != regno)
  126. {
  127. for (i = vd->e[regno].oldest_regno;
  128. vd->e[i].next_regno != regno;
  129. i = vd->e[i].next_regno)
  130. continue;
  131. vd->e[i].next_regno = vd->e[regno].next_regno;
  132. }
  133. else if ((next = vd->e[regno].next_regno) != INVALID_REGNUM)
  134. {
  135. for (i = next; i != INVALID_REGNUM; i = vd->e[i].next_regno)
  136. vd->e[i].oldest_regno = next;
  137. }
  138. vd->e[regno].mode = VOIDmode;
  139. vd->e[regno].oldest_regno = regno;
  140. vd->e[regno].next_regno = INVALID_REGNUM;
  141. if (vd->e[regno].debug_insn_changes)
  142. free_debug_insn_changes (vd, regno);
  143. #ifdef ENABLE_CHECKING
  144. validate_value_data (vd);
  145. #endif
  146. }
  147. /* Kill the value in register REGNO for NREGS, and any other registers
  148. whose values overlap. */
  149. static void
  150. kill_value_regno (unsigned int regno, unsigned int nregs,
  151. struct value_data *vd)
  152. {
  153. unsigned int j;
  154. /* Kill the value we're told to kill. */
  155. for (j = 0; j < nregs; ++j)
  156. kill_value_one_regno (regno + j, vd);
  157. /* Kill everything that overlapped what we're told to kill. */
  158. if (regno < vd->max_value_regs)
  159. j = 0;
  160. else
  161. j = regno - vd->max_value_regs;
  162. for (; j < regno; ++j)
  163. {
  164. unsigned int i, n;
  165. if (vd->e[j].mode == VOIDmode)
  166. continue;
  167. n = hard_regno_nregs[j][vd->e[j].mode];
  168. if (j + n > regno)
  169. for (i = 0; i < n; ++i)
  170. kill_value_one_regno (j + i, vd);
  171. }
  172. }
  173. /* Kill X. This is a convenience function wrapping kill_value_regno
  174. so that we mind the mode the register is in. */
  175. static void
  176. kill_value (const_rtx x, struct value_data *vd)
  177. {
  178. if (GET_CODE (x) == SUBREG)
  179. {
  180. rtx tmp = simplify_subreg (GET_MODE (x), SUBREG_REG (x),
  181. GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
  182. x = tmp ? tmp : SUBREG_REG (x);
  183. }
  184. if (REG_P (x))
  185. {
  186. unsigned int regno = REGNO (x);
  187. unsigned int n = hard_regno_nregs[regno][GET_MODE (x)];
  188. kill_value_regno (regno, n, vd);
  189. }
  190. }
  191. /* Remember that REGNO is valid in MODE. */
  192. static void
  193. set_value_regno (unsigned int regno, machine_mode mode,
  194. struct value_data *vd)
  195. {
  196. unsigned int nregs;
  197. vd->e[regno].mode = mode;
  198. nregs = hard_regno_nregs[regno][mode];
  199. if (nregs > vd->max_value_regs)
  200. vd->max_value_regs = nregs;
  201. }
  202. /* Initialize VD such that there are no known relationships between regs. */
  203. static void
  204. init_value_data (struct value_data *vd)
  205. {
  206. int i;
  207. for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
  208. {
  209. vd->e[i].mode = VOIDmode;
  210. vd->e[i].oldest_regno = i;
  211. vd->e[i].next_regno = INVALID_REGNUM;
  212. vd->e[i].debug_insn_changes = NULL;
  213. }
  214. vd->max_value_regs = 0;
  215. vd->n_debug_insn_changes = 0;
  216. }
  217. /* Called through note_stores. If X is clobbered, kill its value. */
  218. static void
  219. kill_clobbered_value (rtx x, const_rtx set, void *data)
  220. {
  221. struct value_data *const vd = (struct value_data *) data;
  222. if (GET_CODE (set) == CLOBBER)
  223. kill_value (x, vd);
  224. }
  225. /* A structure passed as data to kill_set_value through note_stores. */
  226. struct kill_set_value_data
  227. {
  228. struct value_data *vd;
  229. rtx ignore_set_reg;
  230. };
  231. /* Called through note_stores. If X is set, not clobbered, kill its
  232. current value and install it as the root of its own value list. */
  233. static void
  234. kill_set_value (rtx x, const_rtx set, void *data)
  235. {
  236. struct kill_set_value_data *ksvd = (struct kill_set_value_data *) data;
  237. if (rtx_equal_p (x, ksvd->ignore_set_reg))
  238. return;
  239. if (GET_CODE (set) != CLOBBER)
  240. {
  241. kill_value (x, ksvd->vd);
  242. if (REG_P (x))
  243. set_value_regno (REGNO (x), GET_MODE (x), ksvd->vd);
  244. }
  245. }
  246. /* Kill any register used in X as the base of an auto-increment expression,
  247. and install that register as the root of its own value list. */
  248. static void
  249. kill_autoinc_value (rtx insn, struct value_data *vd)
  250. {
  251. subrtx_iterator::array_type array;
  252. FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
  253. {
  254. const_rtx x = *iter;
  255. if (GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
  256. {
  257. x = XEXP (x, 0);
  258. kill_value (x, vd);
  259. set_value_regno (REGNO (x), GET_MODE (x), vd);
  260. iter.skip_subrtxes ();
  261. }
  262. }
  263. }
  264. /* Assert that SRC has been copied to DEST. Adjust the data structures
  265. to reflect that SRC contains an older copy of the shared value. */
  266. static void
  267. copy_value (rtx dest, rtx src, struct value_data *vd)
  268. {
  269. unsigned int dr = REGNO (dest);
  270. unsigned int sr = REGNO (src);
  271. unsigned int dn, sn;
  272. unsigned int i;
  273. /* ??? At present, it's possible to see noop sets. It'd be nice if
  274. this were cleaned up beforehand... */
  275. if (sr == dr)
  276. return;
  277. /* Do not propagate copies to the stack pointer, as that can leave
  278. memory accesses with no scheduling dependency on the stack update. */
  279. if (dr == STACK_POINTER_REGNUM)
  280. return;
  281. /* Likewise with the frame pointer, if we're using one. */
  282. if (frame_pointer_needed && dr == HARD_FRAME_POINTER_REGNUM)
  283. return;
  284. /* Do not propagate copies to fixed or global registers, patterns
  285. can be relying to see particular fixed register or users can
  286. expect the chosen global register in asm. */
  287. if (fixed_regs[dr] || global_regs[dr])
  288. return;
  289. /* If SRC and DEST overlap, don't record anything. */
  290. dn = hard_regno_nregs[dr][GET_MODE (dest)];
  291. sn = hard_regno_nregs[sr][GET_MODE (dest)];
  292. if ((dr > sr && dr < sr + sn)
  293. || (sr > dr && sr < dr + dn))
  294. return;
  295. /* If SRC had no assigned mode (i.e. we didn't know it was live)
  296. assign it now and assume the value came from an input argument
  297. or somesuch. */
  298. if (vd->e[sr].mode == VOIDmode)
  299. set_value_regno (sr, vd->e[dr].mode, vd);
  300. /* If we are narrowing the input to a smaller number of hard regs,
  301. and it is in big endian, we are really extracting a high part.
  302. Since we generally associate a low part of a value with the value itself,
  303. we must not do the same for the high part.
  304. Note we can still get low parts for the same mode combination through
  305. a two-step copy involving differently sized hard regs.
  306. Assume hard regs fr* are 32 bits bits each, while r* are 64 bits each:
  307. (set (reg:DI r0) (reg:DI fr0))
  308. (set (reg:SI fr2) (reg:SI r0))
  309. loads the low part of (reg:DI fr0) - i.e. fr1 - into fr2, while:
  310. (set (reg:SI fr2) (reg:SI fr0))
  311. loads the high part of (reg:DI fr0) into fr2.
  312. We can't properly represent the latter case in our tables, so don't
  313. record anything then. */
  314. else if (sn < (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode]
  315. && (GET_MODE_SIZE (vd->e[sr].mode) > UNITS_PER_WORD
  316. ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
  317. return;
  318. /* If SRC had been assigned a mode narrower than the copy, we can't
  319. link DEST into the chain, because not all of the pieces of the
  320. copy came from oldest_regno. */
  321. else if (sn > (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode])
  322. return;
  323. /* Link DR at the end of the value chain used by SR. */
  324. vd->e[dr].oldest_regno = vd->e[sr].oldest_regno;
  325. for (i = sr; vd->e[i].next_regno != INVALID_REGNUM; i = vd->e[i].next_regno)
  326. continue;
  327. vd->e[i].next_regno = dr;
  328. #ifdef ENABLE_CHECKING
  329. validate_value_data (vd);
  330. #endif
  331. }
  332. /* Return true if a mode change from ORIG to NEW is allowed for REGNO. */
  333. static bool
  334. mode_change_ok (machine_mode orig_mode, machine_mode new_mode,
  335. unsigned int regno ATTRIBUTE_UNUSED)
  336. {
  337. if (GET_MODE_SIZE (orig_mode) < GET_MODE_SIZE (new_mode))
  338. return false;
  339. #ifdef CANNOT_CHANGE_MODE_CLASS
  340. return !REG_CANNOT_CHANGE_MODE_P (regno, orig_mode, new_mode);
  341. #endif
  342. return true;
  343. }
  344. /* Register REGNO was originally set in ORIG_MODE. It - or a copy of it -
  345. was copied in COPY_MODE to COPY_REGNO, and then COPY_REGNO was accessed
  346. in NEW_MODE.
  347. Return a NEW_MODE rtx for REGNO if that's OK, otherwise return NULL_RTX. */
  348. static rtx
  349. maybe_mode_change (machine_mode orig_mode, machine_mode copy_mode,
  350. machine_mode new_mode, unsigned int regno,
  351. unsigned int copy_regno ATTRIBUTE_UNUSED)
  352. {
  353. if (GET_MODE_SIZE (copy_mode) < GET_MODE_SIZE (orig_mode)
  354. && GET_MODE_SIZE (copy_mode) < GET_MODE_SIZE (new_mode))
  355. return NULL_RTX;
  356. if (orig_mode == new_mode)
  357. return gen_rtx_raw_REG (new_mode, regno);
  358. else if (mode_change_ok (orig_mode, new_mode, regno))
  359. {
  360. int copy_nregs = hard_regno_nregs[copy_regno][copy_mode];
  361. int use_nregs = hard_regno_nregs[copy_regno][new_mode];
  362. int copy_offset
  363. = GET_MODE_SIZE (copy_mode) / copy_nregs * (copy_nregs - use_nregs);
  364. int offset
  365. = GET_MODE_SIZE (orig_mode) - GET_MODE_SIZE (new_mode) - copy_offset;
  366. int byteoffset = offset % UNITS_PER_WORD;
  367. int wordoffset = offset - byteoffset;
  368. offset = ((WORDS_BIG_ENDIAN ? wordoffset : 0)
  369. + (BYTES_BIG_ENDIAN ? byteoffset : 0));
  370. regno += subreg_regno_offset (regno, orig_mode, offset, new_mode);
  371. if (HARD_REGNO_MODE_OK (regno, new_mode))
  372. return gen_rtx_raw_REG (new_mode, regno);
  373. }
  374. return NULL_RTX;
  375. }
  376. /* Find the oldest copy of the value contained in REGNO that is in
  377. register class CL and has mode MODE. If found, return an rtx
  378. of that oldest register, otherwise return NULL. */
  379. static rtx
  380. find_oldest_value_reg (enum reg_class cl, rtx reg, struct value_data *vd)
  381. {
  382. unsigned int regno = REGNO (reg);
  383. machine_mode mode = GET_MODE (reg);
  384. unsigned int i;
  385. /* If we are accessing REG in some mode other that what we set it in,
  386. make sure that the replacement is valid. In particular, consider
  387. (set (reg:DI r11) (...))
  388. (set (reg:SI r9) (reg:SI r11))
  389. (set (reg:SI r10) (...))
  390. (set (...) (reg:DI r9))
  391. Replacing r9 with r11 is invalid. */
  392. if (mode != vd->e[regno].mode)
  393. {
  394. if (hard_regno_nregs[regno][mode]
  395. > hard_regno_nregs[regno][vd->e[regno].mode])
  396. return NULL_RTX;
  397. }
  398. for (i = vd->e[regno].oldest_regno; i != regno; i = vd->e[i].next_regno)
  399. {
  400. machine_mode oldmode = vd->e[i].mode;
  401. rtx new_rtx;
  402. if (!in_hard_reg_set_p (reg_class_contents[cl], mode, i))
  403. continue;
  404. new_rtx = maybe_mode_change (oldmode, vd->e[regno].mode, mode, i, regno);
  405. if (new_rtx)
  406. {
  407. ORIGINAL_REGNO (new_rtx) = ORIGINAL_REGNO (reg);
  408. REG_ATTRS (new_rtx) = REG_ATTRS (reg);
  409. REG_POINTER (new_rtx) = REG_POINTER (reg);
  410. return new_rtx;
  411. }
  412. }
  413. return NULL_RTX;
  414. }
  415. /* If possible, replace the register at *LOC with the oldest register
  416. in register class CL. Return true if successfully replaced. */
  417. static bool
  418. replace_oldest_value_reg (rtx *loc, enum reg_class cl, rtx_insn *insn,
  419. struct value_data *vd)
  420. {
  421. rtx new_rtx = find_oldest_value_reg (cl, *loc, vd);
  422. if (new_rtx && (!DEBUG_INSN_P (insn) || !skip_debug_insn_p))
  423. {
  424. if (DEBUG_INSN_P (insn))
  425. {
  426. struct queued_debug_insn_change *change;
  427. if (dump_file)
  428. fprintf (dump_file, "debug_insn %u: queued replacing reg %u with %u\n",
  429. INSN_UID (insn), REGNO (*loc), REGNO (new_rtx));
  430. change = (struct queued_debug_insn_change *)
  431. pool_alloc (debug_insn_changes_pool);
  432. change->next = vd->e[REGNO (new_rtx)].debug_insn_changes;
  433. change->insn = insn;
  434. change->loc = loc;
  435. change->new_rtx = new_rtx;
  436. vd->e[REGNO (new_rtx)].debug_insn_changes = change;
  437. ++vd->n_debug_insn_changes;
  438. return true;
  439. }
  440. if (dump_file)
  441. fprintf (dump_file, "insn %u: replaced reg %u with %u\n",
  442. INSN_UID (insn), REGNO (*loc), REGNO (new_rtx));
  443. validate_change (insn, loc, new_rtx, 1);
  444. return true;
  445. }
  446. return false;
  447. }
  448. /* Similar to replace_oldest_value_reg, but *LOC contains an address.
  449. Adapted from find_reloads_address_1. CL is INDEX_REG_CLASS or
  450. BASE_REG_CLASS depending on how the register is being considered. */
  451. static bool
  452. replace_oldest_value_addr (rtx *loc, enum reg_class cl,
  453. machine_mode mode, addr_space_t as,
  454. rtx_insn *insn, struct value_data *vd)
  455. {
  456. rtx x = *loc;
  457. RTX_CODE code = GET_CODE (x);
  458. const char *fmt;
  459. int i, j;
  460. bool changed = false;
  461. switch (code)
  462. {
  463. case PLUS:
  464. if (DEBUG_INSN_P (insn))
  465. break;
  466. {
  467. rtx orig_op0 = XEXP (x, 0);
  468. rtx orig_op1 = XEXP (x, 1);
  469. RTX_CODE code0 = GET_CODE (orig_op0);
  470. RTX_CODE code1 = GET_CODE (orig_op1);
  471. rtx op0 = orig_op0;
  472. rtx op1 = orig_op1;
  473. rtx *locI = NULL;
  474. rtx *locB = NULL;
  475. enum rtx_code index_code = SCRATCH;
  476. if (GET_CODE (op0) == SUBREG)
  477. {
  478. op0 = SUBREG_REG (op0);
  479. code0 = GET_CODE (op0);
  480. }
  481. if (GET_CODE (op1) == SUBREG)
  482. {
  483. op1 = SUBREG_REG (op1);
  484. code1 = GET_CODE (op1);
  485. }
  486. if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
  487. || code0 == ZERO_EXTEND || code1 == MEM)
  488. {
  489. locI = &XEXP (x, 0);
  490. locB = &XEXP (x, 1);
  491. index_code = GET_CODE (*locI);
  492. }
  493. else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
  494. || code1 == ZERO_EXTEND || code0 == MEM)
  495. {
  496. locI = &XEXP (x, 1);
  497. locB = &XEXP (x, 0);
  498. index_code = GET_CODE (*locI);
  499. }
  500. else if (code0 == CONST_INT || code0 == CONST
  501. || code0 == SYMBOL_REF || code0 == LABEL_REF)
  502. {
  503. locB = &XEXP (x, 1);
  504. index_code = GET_CODE (XEXP (x, 0));
  505. }
  506. else if (code1 == CONST_INT || code1 == CONST
  507. || code1 == SYMBOL_REF || code1 == LABEL_REF)
  508. {
  509. locB = &XEXP (x, 0);
  510. index_code = GET_CODE (XEXP (x, 1));
  511. }
  512. else if (code0 == REG && code1 == REG)
  513. {
  514. int index_op;
  515. unsigned regno0 = REGNO (op0), regno1 = REGNO (op1);
  516. if (REGNO_OK_FOR_INDEX_P (regno1)
  517. && regno_ok_for_base_p (regno0, mode, as, PLUS, REG))
  518. index_op = 1;
  519. else if (REGNO_OK_FOR_INDEX_P (regno0)
  520. && regno_ok_for_base_p (regno1, mode, as, PLUS, REG))
  521. index_op = 0;
  522. else if (regno_ok_for_base_p (regno0, mode, as, PLUS, REG)
  523. || REGNO_OK_FOR_INDEX_P (regno1))
  524. index_op = 1;
  525. else if (regno_ok_for_base_p (regno1, mode, as, PLUS, REG))
  526. index_op = 0;
  527. else
  528. index_op = 1;
  529. locI = &XEXP (x, index_op);
  530. locB = &XEXP (x, !index_op);
  531. index_code = GET_CODE (*locI);
  532. }
  533. else if (code0 == REG)
  534. {
  535. locI = &XEXP (x, 0);
  536. locB = &XEXP (x, 1);
  537. index_code = GET_CODE (*locI);
  538. }
  539. else if (code1 == REG)
  540. {
  541. locI = &XEXP (x, 1);
  542. locB = &XEXP (x, 0);
  543. index_code = GET_CODE (*locI);
  544. }
  545. if (locI)
  546. changed |= replace_oldest_value_addr (locI, INDEX_REG_CLASS,
  547. mode, as, insn, vd);
  548. if (locB)
  549. changed |= replace_oldest_value_addr (locB,
  550. base_reg_class (mode, as, PLUS,
  551. index_code),
  552. mode, as, insn, vd);
  553. return changed;
  554. }
  555. case POST_INC:
  556. case POST_DEC:
  557. case POST_MODIFY:
  558. case PRE_INC:
  559. case PRE_DEC:
  560. case PRE_MODIFY:
  561. return false;
  562. case MEM:
  563. return replace_oldest_value_mem (x, insn, vd);
  564. case REG:
  565. return replace_oldest_value_reg (loc, cl, insn, vd);
  566. default:
  567. break;
  568. }
  569. fmt = GET_RTX_FORMAT (code);
  570. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  571. {
  572. if (fmt[i] == 'e')
  573. changed |= replace_oldest_value_addr (&XEXP (x, i), cl, mode, as,
  574. insn, vd);
  575. else if (fmt[i] == 'E')
  576. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  577. changed |= replace_oldest_value_addr (&XVECEXP (x, i, j), cl,
  578. mode, as, insn, vd);
  579. }
  580. return changed;
  581. }
  582. /* Similar to replace_oldest_value_reg, but X contains a memory. */
  583. static bool
  584. replace_oldest_value_mem (rtx x, rtx_insn *insn, struct value_data *vd)
  585. {
  586. enum reg_class cl;
  587. if (DEBUG_INSN_P (insn))
  588. cl = ALL_REGS;
  589. else
  590. cl = base_reg_class (GET_MODE (x), MEM_ADDR_SPACE (x), MEM, SCRATCH);
  591. return replace_oldest_value_addr (&XEXP (x, 0), cl,
  592. GET_MODE (x), MEM_ADDR_SPACE (x),
  593. insn, vd);
  594. }
  595. /* Apply all queued updates for DEBUG_INSNs that change some reg to
  596. register REGNO. */
  597. static void
  598. apply_debug_insn_changes (struct value_data *vd, unsigned int regno)
  599. {
  600. struct queued_debug_insn_change *change;
  601. rtx_insn *last_insn = vd->e[regno].debug_insn_changes->insn;
  602. for (change = vd->e[regno].debug_insn_changes;
  603. change;
  604. change = change->next)
  605. {
  606. if (last_insn != change->insn)
  607. {
  608. apply_change_group ();
  609. last_insn = change->insn;
  610. }
  611. validate_change (change->insn, change->loc, change->new_rtx, 1);
  612. }
  613. apply_change_group ();
  614. }
  615. /* Called via note_uses, for all used registers in a real insn
  616. apply DEBUG_INSN changes that change registers to the used
  617. registers. */
  618. static void
  619. cprop_find_used_regs (rtx *loc, void *data)
  620. {
  621. struct value_data *const vd = (struct value_data *) data;
  622. subrtx_iterator::array_type array;
  623. FOR_EACH_SUBRTX (iter, array, *loc, NONCONST)
  624. {
  625. const_rtx x = *iter;
  626. if (REG_P (x))
  627. {
  628. unsigned int regno = REGNO (x);
  629. if (vd->e[regno].debug_insn_changes)
  630. {
  631. apply_debug_insn_changes (vd, regno);
  632. free_debug_insn_changes (vd, regno);
  633. }
  634. }
  635. }
  636. }
  637. /* Apply clobbers of INSN in PATTERN and C_I_F_U to value_data VD. */
  638. static void
  639. kill_clobbered_values (rtx_insn *insn, struct value_data *vd)
  640. {
  641. note_stores (PATTERN (insn), kill_clobbered_value, vd);
  642. if (CALL_P (insn))
  643. {
  644. rtx exp;
  645. for (exp = CALL_INSN_FUNCTION_USAGE (insn); exp; exp = XEXP (exp, 1))
  646. {
  647. rtx x = XEXP (exp, 0);
  648. if (GET_CODE (x) == CLOBBER)
  649. kill_value (SET_DEST (x), vd);
  650. }
  651. }
  652. }
  653. /* Perform the forward copy propagation on basic block BB. */
  654. static bool
  655. copyprop_hardreg_forward_1 (basic_block bb, struct value_data *vd)
  656. {
  657. bool anything_changed = false;
  658. rtx_insn *insn;
  659. for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
  660. {
  661. int n_ops, i, predicated;
  662. bool is_asm, any_replacements;
  663. rtx set;
  664. rtx link;
  665. bool replaced[MAX_RECOG_OPERANDS];
  666. bool changed = false;
  667. struct kill_set_value_data ksvd;
  668. if (!NONDEBUG_INSN_P (insn))
  669. {
  670. if (DEBUG_INSN_P (insn))
  671. {
  672. rtx loc = INSN_VAR_LOCATION_LOC (insn);
  673. if (!VAR_LOC_UNKNOWN_P (loc))
  674. replace_oldest_value_addr (&INSN_VAR_LOCATION_LOC (insn),
  675. ALL_REGS, GET_MODE (loc),
  676. ADDR_SPACE_GENERIC, insn, vd);
  677. }
  678. if (insn == BB_END (bb))
  679. break;
  680. else
  681. continue;
  682. }
  683. set = single_set (insn);
  684. extract_constrain_insn (insn);
  685. preprocess_constraints (insn);
  686. const operand_alternative *op_alt = which_op_alt ();
  687. n_ops = recog_data.n_operands;
  688. is_asm = asm_noperands (PATTERN (insn)) >= 0;
  689. /* Simplify the code below by promoting OP_OUT to OP_INOUT
  690. in predicated instructions. */
  691. predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
  692. for (i = 0; i < n_ops; ++i)
  693. {
  694. int matches = op_alt[i].matches;
  695. if (matches >= 0 || op_alt[i].matched >= 0
  696. || (predicated && recog_data.operand_type[i] == OP_OUT))
  697. recog_data.operand_type[i] = OP_INOUT;
  698. }
  699. /* Apply changes to earlier DEBUG_INSNs if possible. */
  700. if (vd->n_debug_insn_changes)
  701. note_uses (&PATTERN (insn), cprop_find_used_regs, vd);
  702. /* For each earlyclobber operand, zap the value data. */
  703. for (i = 0; i < n_ops; i++)
  704. if (op_alt[i].earlyclobber)
  705. kill_value (recog_data.operand[i], vd);
  706. /* Within asms, a clobber cannot overlap inputs or outputs.
  707. I wouldn't think this were true for regular insns, but
  708. scan_rtx treats them like that... */
  709. kill_clobbered_values (insn, vd);
  710. /* Kill all auto-incremented values. */
  711. /* ??? REG_INC is useless, since stack pushes aren't done that way. */
  712. kill_autoinc_value (insn, vd);
  713. /* Kill all early-clobbered operands. */
  714. for (i = 0; i < n_ops; i++)
  715. if (op_alt[i].earlyclobber)
  716. kill_value (recog_data.operand[i], vd);
  717. /* If we have dead sets in the insn, then we need to note these as we
  718. would clobbers. */
  719. for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  720. {
  721. if (REG_NOTE_KIND (link) == REG_UNUSED)
  722. {
  723. kill_value (XEXP (link, 0), vd);
  724. /* Furthermore, if the insn looked like a single-set,
  725. but the dead store kills the source value of that
  726. set, then we can no-longer use the plain move
  727. special case below. */
  728. if (set
  729. && reg_overlap_mentioned_p (XEXP (link, 0), SET_SRC (set)))
  730. set = NULL;
  731. }
  732. }
  733. /* Special-case plain move instructions, since we may well
  734. be able to do the move from a different register class. */
  735. if (set && REG_P (SET_SRC (set)))
  736. {
  737. rtx src = SET_SRC (set);
  738. unsigned int regno = REGNO (src);
  739. machine_mode mode = GET_MODE (src);
  740. unsigned int i;
  741. rtx new_rtx;
  742. /* If we are accessing SRC in some mode other that what we
  743. set it in, make sure that the replacement is valid. */
  744. if (mode != vd->e[regno].mode)
  745. {
  746. if (hard_regno_nregs[regno][mode]
  747. > hard_regno_nregs[regno][vd->e[regno].mode])
  748. goto no_move_special_case;
  749. /* And likewise, if we are narrowing on big endian the transformation
  750. is also invalid. */
  751. if (hard_regno_nregs[regno][mode]
  752. < hard_regno_nregs[regno][vd->e[regno].mode]
  753. && (GET_MODE_SIZE (vd->e[regno].mode) > UNITS_PER_WORD
  754. ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
  755. goto no_move_special_case;
  756. }
  757. /* If the destination is also a register, try to find a source
  758. register in the same class. */
  759. if (REG_P (SET_DEST (set)))
  760. {
  761. new_rtx = find_oldest_value_reg (REGNO_REG_CLASS (regno), src, vd);
  762. if (new_rtx && validate_change (insn, &SET_SRC (set), new_rtx, 0))
  763. {
  764. if (dump_file)
  765. fprintf (dump_file,
  766. "insn %u: replaced reg %u with %u\n",
  767. INSN_UID (insn), regno, REGNO (new_rtx));
  768. changed = true;
  769. goto did_replacement;
  770. }
  771. /* We need to re-extract as validate_change clobbers
  772. recog_data. */
  773. extract_constrain_insn (insn);
  774. preprocess_constraints (insn);
  775. }
  776. /* Otherwise, try all valid registers and see if its valid. */
  777. for (i = vd->e[regno].oldest_regno; i != regno;
  778. i = vd->e[i].next_regno)
  779. {
  780. new_rtx = maybe_mode_change (vd->e[i].mode, vd->e[regno].mode,
  781. mode, i, regno);
  782. if (new_rtx != NULL_RTX)
  783. {
  784. if (validate_change (insn, &SET_SRC (set), new_rtx, 0))
  785. {
  786. ORIGINAL_REGNO (new_rtx) = ORIGINAL_REGNO (src);
  787. REG_ATTRS (new_rtx) = REG_ATTRS (src);
  788. REG_POINTER (new_rtx) = REG_POINTER (src);
  789. if (dump_file)
  790. fprintf (dump_file,
  791. "insn %u: replaced reg %u with %u\n",
  792. INSN_UID (insn), regno, REGNO (new_rtx));
  793. changed = true;
  794. goto did_replacement;
  795. }
  796. /* We need to re-extract as validate_change clobbers
  797. recog_data. */
  798. extract_constrain_insn (insn);
  799. preprocess_constraints (insn);
  800. }
  801. }
  802. }
  803. no_move_special_case:
  804. any_replacements = false;
  805. /* For each input operand, replace a hard register with the
  806. eldest live copy that's in an appropriate register class. */
  807. for (i = 0; i < n_ops; i++)
  808. {
  809. replaced[i] = false;
  810. /* Don't scan match_operand here, since we've no reg class
  811. information to pass down. Any operands that we could
  812. substitute in will be represented elsewhere. */
  813. if (recog_data.constraints[i][0] == '\0')
  814. continue;
  815. /* Don't replace in asms intentionally referencing hard regs. */
  816. if (is_asm && REG_P (recog_data.operand[i])
  817. && (REGNO (recog_data.operand[i])
  818. == ORIGINAL_REGNO (recog_data.operand[i])))
  819. continue;
  820. if (recog_data.operand_type[i] == OP_IN)
  821. {
  822. if (op_alt[i].is_address)
  823. replaced[i]
  824. = replace_oldest_value_addr (recog_data.operand_loc[i],
  825. alternative_class (op_alt, i),
  826. VOIDmode, ADDR_SPACE_GENERIC,
  827. insn, vd);
  828. else if (REG_P (recog_data.operand[i]))
  829. replaced[i]
  830. = replace_oldest_value_reg (recog_data.operand_loc[i],
  831. alternative_class (op_alt, i),
  832. insn, vd);
  833. else if (MEM_P (recog_data.operand[i]))
  834. replaced[i] = replace_oldest_value_mem (recog_data.operand[i],
  835. insn, vd);
  836. }
  837. else if (MEM_P (recog_data.operand[i]))
  838. replaced[i] = replace_oldest_value_mem (recog_data.operand[i],
  839. insn, vd);
  840. /* If we performed any replacement, update match_dups. */
  841. if (replaced[i])
  842. {
  843. int j;
  844. rtx new_rtx;
  845. new_rtx = *recog_data.operand_loc[i];
  846. recog_data.operand[i] = new_rtx;
  847. for (j = 0; j < recog_data.n_dups; j++)
  848. if (recog_data.dup_num[j] == i)
  849. validate_unshare_change (insn, recog_data.dup_loc[j], new_rtx, 1);
  850. any_replacements = true;
  851. }
  852. }
  853. if (any_replacements)
  854. {
  855. if (! apply_change_group ())
  856. {
  857. for (i = 0; i < n_ops; i++)
  858. if (replaced[i])
  859. {
  860. rtx old = *recog_data.operand_loc[i];
  861. recog_data.operand[i] = old;
  862. }
  863. if (dump_file)
  864. fprintf (dump_file,
  865. "insn %u: reg replacements not verified\n",
  866. INSN_UID (insn));
  867. }
  868. else
  869. changed = true;
  870. }
  871. did_replacement:
  872. if (changed)
  873. {
  874. anything_changed = true;
  875. /* If something changed, perhaps further changes to earlier
  876. DEBUG_INSNs can be applied. */
  877. if (vd->n_debug_insn_changes)
  878. note_uses (&PATTERN (insn), cprop_find_used_regs, vd);
  879. }
  880. ksvd.vd = vd;
  881. ksvd.ignore_set_reg = NULL_RTX;
  882. /* Clobber call-clobbered registers. */
  883. if (CALL_P (insn))
  884. {
  885. unsigned int set_regno = INVALID_REGNUM;
  886. unsigned int set_nregs = 0;
  887. unsigned int regno;
  888. rtx exp;
  889. HARD_REG_SET regs_invalidated_by_this_call;
  890. for (exp = CALL_INSN_FUNCTION_USAGE (insn); exp; exp = XEXP (exp, 1))
  891. {
  892. rtx x = XEXP (exp, 0);
  893. if (GET_CODE (x) == SET)
  894. {
  895. rtx dest = SET_DEST (x);
  896. kill_value (dest, vd);
  897. set_value_regno (REGNO (dest), GET_MODE (dest), vd);
  898. copy_value (dest, SET_SRC (x), vd);
  899. ksvd.ignore_set_reg = dest;
  900. set_regno = REGNO (dest);
  901. set_nregs
  902. = hard_regno_nregs[set_regno][GET_MODE (dest)];
  903. break;
  904. }
  905. }
  906. get_call_reg_set_usage (insn,
  907. &regs_invalidated_by_this_call,
  908. regs_invalidated_by_call);
  909. for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
  910. if ((TEST_HARD_REG_BIT (regs_invalidated_by_this_call, regno)
  911. || HARD_REGNO_CALL_PART_CLOBBERED (regno, vd->e[regno].mode))
  912. && (regno < set_regno || regno >= set_regno + set_nregs))
  913. kill_value_regno (regno, 1, vd);
  914. /* If SET was seen in CALL_INSN_FUNCTION_USAGE, and SET_SRC
  915. of the SET isn't in regs_invalidated_by_call hard reg set,
  916. but instead among CLOBBERs on the CALL_INSN, we could wrongly
  917. assume the value in it is still live. */
  918. if (ksvd.ignore_set_reg)
  919. kill_clobbered_values (insn, vd);
  920. }
  921. bool copy_p = (set
  922. && REG_P (SET_DEST (set))
  923. && REG_P (SET_SRC (set)));
  924. bool noop_p = (copy_p
  925. && rtx_equal_p (SET_DEST (set), SET_SRC (set)));
  926. if (!noop_p)
  927. {
  928. /* Notice stores. */
  929. note_stores (PATTERN (insn), kill_set_value, &ksvd);
  930. /* Notice copies. */
  931. if (copy_p)
  932. copy_value (SET_DEST (set), SET_SRC (set), vd);
  933. }
  934. if (insn == BB_END (bb))
  935. break;
  936. }
  937. return anything_changed;
  938. }
  939. /* Dump the value chain data to stderr. */
  940. DEBUG_FUNCTION void
  941. debug_value_data (struct value_data *vd)
  942. {
  943. HARD_REG_SET set;
  944. unsigned int i, j;
  945. CLEAR_HARD_REG_SET (set);
  946. for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
  947. if (vd->e[i].oldest_regno == i)
  948. {
  949. if (vd->e[i].mode == VOIDmode)
  950. {
  951. if (vd->e[i].next_regno != INVALID_REGNUM)
  952. fprintf (stderr, "[%u] Bad next_regno for empty chain (%u)\n",
  953. i, vd->e[i].next_regno);
  954. continue;
  955. }
  956. SET_HARD_REG_BIT (set, i);
  957. fprintf (stderr, "[%u %s] ", i, GET_MODE_NAME (vd->e[i].mode));
  958. for (j = vd->e[i].next_regno;
  959. j != INVALID_REGNUM;
  960. j = vd->e[j].next_regno)
  961. {
  962. if (TEST_HARD_REG_BIT (set, j))
  963. {
  964. fprintf (stderr, "[%u] Loop in regno chain\n", j);
  965. return;
  966. }
  967. if (vd->e[j].oldest_regno != i)
  968. {
  969. fprintf (stderr, "[%u] Bad oldest_regno (%u)\n",
  970. j, vd->e[j].oldest_regno);
  971. return;
  972. }
  973. SET_HARD_REG_BIT (set, j);
  974. fprintf (stderr, "[%u %s] ", j, GET_MODE_NAME (vd->e[j].mode));
  975. }
  976. fputc ('\n', stderr);
  977. }
  978. for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
  979. if (! TEST_HARD_REG_BIT (set, i)
  980. && (vd->e[i].mode != VOIDmode
  981. || vd->e[i].oldest_regno != i
  982. || vd->e[i].next_regno != INVALID_REGNUM))
  983. fprintf (stderr, "[%u] Non-empty reg in chain (%s %u %i)\n",
  984. i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
  985. vd->e[i].next_regno);
  986. }
  987. /* Do copyprop_hardreg_forward_1 for a single basic block BB.
  988. DEBUG_INSN is skipped since we do not want to involve DF related
  989. staff as how it is handled in function pass_cprop_hardreg::execute.
  990. NOTE: Currently it is only used for shrink-wrap. Maybe extend it
  991. to handle DEBUG_INSN for other uses. */
  992. void
  993. copyprop_hardreg_forward_bb_without_debug_insn (basic_block bb)
  994. {
  995. struct value_data *vd;
  996. vd = XNEWVEC (struct value_data, 1);
  997. init_value_data (vd);
  998. skip_debug_insn_p = true;
  999. copyprop_hardreg_forward_1 (bb, vd);
  1000. free (vd);
  1001. skip_debug_insn_p = false;
  1002. }
  1003. #ifdef ENABLE_CHECKING
  1004. static void
  1005. validate_value_data (struct value_data *vd)
  1006. {
  1007. HARD_REG_SET set;
  1008. unsigned int i, j;
  1009. CLEAR_HARD_REG_SET (set);
  1010. for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
  1011. if (vd->e[i].oldest_regno == i)
  1012. {
  1013. if (vd->e[i].mode == VOIDmode)
  1014. {
  1015. if (vd->e[i].next_regno != INVALID_REGNUM)
  1016. internal_error ("validate_value_data: [%u] Bad next_regno for empty chain (%u)",
  1017. i, vd->e[i].next_regno);
  1018. continue;
  1019. }
  1020. SET_HARD_REG_BIT (set, i);
  1021. for (j = vd->e[i].next_regno;
  1022. j != INVALID_REGNUM;
  1023. j = vd->e[j].next_regno)
  1024. {
  1025. if (TEST_HARD_REG_BIT (set, j))
  1026. internal_error ("validate_value_data: Loop in regno chain (%u)",
  1027. j);
  1028. if (vd->e[j].oldest_regno != i)
  1029. internal_error ("validate_value_data: [%u] Bad oldest_regno (%u)",
  1030. j, vd->e[j].oldest_regno);
  1031. SET_HARD_REG_BIT (set, j);
  1032. }
  1033. }
  1034. for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
  1035. if (! TEST_HARD_REG_BIT (set, i)
  1036. && (vd->e[i].mode != VOIDmode
  1037. || vd->e[i].oldest_regno != i
  1038. || vd->e[i].next_regno != INVALID_REGNUM))
  1039. internal_error ("validate_value_data: [%u] Non-empty reg in chain (%s %u %i)",
  1040. i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
  1041. vd->e[i].next_regno);
  1042. }
  1043. #endif
  1044. namespace {
  1045. const pass_data pass_data_cprop_hardreg =
  1046. {
  1047. RTL_PASS, /* type */
  1048. "cprop_hardreg", /* name */
  1049. OPTGROUP_NONE, /* optinfo_flags */
  1050. TV_CPROP_REGISTERS, /* tv_id */
  1051. 0, /* properties_required */
  1052. 0, /* properties_provided */
  1053. 0, /* properties_destroyed */
  1054. 0, /* todo_flags_start */
  1055. TODO_df_finish, /* todo_flags_finish */
  1056. };
  1057. class pass_cprop_hardreg : public rtl_opt_pass
  1058. {
  1059. public:
  1060. pass_cprop_hardreg (gcc::context *ctxt)
  1061. : rtl_opt_pass (pass_data_cprop_hardreg, ctxt)
  1062. {}
  1063. /* opt_pass methods: */
  1064. virtual bool gate (function *)
  1065. {
  1066. return (optimize > 0 && (flag_cprop_registers));
  1067. }
  1068. virtual unsigned int execute (function *);
  1069. }; // class pass_cprop_hardreg
  1070. unsigned int
  1071. pass_cprop_hardreg::execute (function *fun)
  1072. {
  1073. struct value_data *all_vd;
  1074. basic_block bb;
  1075. sbitmap visited;
  1076. bool analyze_called = false;
  1077. all_vd = XNEWVEC (struct value_data, last_basic_block_for_fn (fun));
  1078. visited = sbitmap_alloc (last_basic_block_for_fn (fun));
  1079. bitmap_clear (visited);
  1080. if (MAY_HAVE_DEBUG_INSNS)
  1081. debug_insn_changes_pool
  1082. = create_alloc_pool ("debug insn changes pool",
  1083. sizeof (struct queued_debug_insn_change), 256);
  1084. FOR_EACH_BB_FN (bb, fun)
  1085. {
  1086. bitmap_set_bit (visited, bb->index);
  1087. /* If a block has a single predecessor, that we've already
  1088. processed, begin with the value data that was live at
  1089. the end of the predecessor block. */
  1090. /* ??? Ought to use more intelligent queuing of blocks. */
  1091. if (single_pred_p (bb)
  1092. && bitmap_bit_p (visited, single_pred (bb)->index)
  1093. && ! (single_pred_edge (bb)->flags & (EDGE_ABNORMAL_CALL | EDGE_EH)))
  1094. {
  1095. all_vd[bb->index] = all_vd[single_pred (bb)->index];
  1096. if (all_vd[bb->index].n_debug_insn_changes)
  1097. {
  1098. unsigned int regno;
  1099. for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
  1100. {
  1101. if (all_vd[bb->index].e[regno].debug_insn_changes)
  1102. {
  1103. all_vd[bb->index].e[regno].debug_insn_changes = NULL;
  1104. if (--all_vd[bb->index].n_debug_insn_changes == 0)
  1105. break;
  1106. }
  1107. }
  1108. }
  1109. }
  1110. else
  1111. init_value_data (all_vd + bb->index);
  1112. copyprop_hardreg_forward_1 (bb, all_vd + bb->index);
  1113. }
  1114. if (MAY_HAVE_DEBUG_INSNS)
  1115. {
  1116. FOR_EACH_BB_FN (bb, fun)
  1117. if (bitmap_bit_p (visited, bb->index)
  1118. && all_vd[bb->index].n_debug_insn_changes)
  1119. {
  1120. unsigned int regno;
  1121. bitmap live;
  1122. if (!analyze_called)
  1123. {
  1124. df_analyze ();
  1125. analyze_called = true;
  1126. }
  1127. live = df_get_live_out (bb);
  1128. for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
  1129. if (all_vd[bb->index].e[regno].debug_insn_changes)
  1130. {
  1131. if (REGNO_REG_SET_P (live, regno))
  1132. apply_debug_insn_changes (all_vd + bb->index, regno);
  1133. if (all_vd[bb->index].n_debug_insn_changes == 0)
  1134. break;
  1135. }
  1136. }
  1137. free_alloc_pool (debug_insn_changes_pool);
  1138. }
  1139. sbitmap_free (visited);
  1140. free (all_vd);
  1141. return 0;
  1142. }
  1143. } // anon namespace
  1144. rtl_opt_pass *
  1145. make_pass_cprop_hardreg (gcc::context *ctxt)
  1146. {
  1147. return new pass_cprop_hardreg (ctxt);
  1148. }