postreload.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387
  1. /* Perform simple optimizations to clean up the result of reload.
  2. Copyright (C) 1987-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 under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. 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 "machmode.h"
  20. #include "hard-reg-set.h"
  21. #include "rtl.h"
  22. #include "tm_p.h"
  23. #include "obstack.h"
  24. #include "insn-config.h"
  25. #include "flags.h"
  26. #include "hashtab.h"
  27. #include "hash-set.h"
  28. #include "vec.h"
  29. #include "input.h"
  30. #include "function.h"
  31. #include "symtab.h"
  32. #include "statistics.h"
  33. #include "double-int.h"
  34. #include "real.h"
  35. #include "fixed-value.h"
  36. #include "alias.h"
  37. #include "wide-int.h"
  38. #include "inchash.h"
  39. #include "tree.h"
  40. #include "expmed.h"
  41. #include "dojump.h"
  42. #include "explow.h"
  43. #include "calls.h"
  44. #include "emit-rtl.h"
  45. #include "varasm.h"
  46. #include "stmt.h"
  47. #include "expr.h"
  48. #include "insn-codes.h"
  49. #include "optabs.h"
  50. #include "regs.h"
  51. #include "predict.h"
  52. #include "dominance.h"
  53. #include "cfg.h"
  54. #include "cfgrtl.h"
  55. #include "cfgbuild.h"
  56. #include "cfgcleanup.h"
  57. #include "basic-block.h"
  58. #include "reload.h"
  59. #include "recog.h"
  60. #include "cselib.h"
  61. #include "diagnostic-core.h"
  62. #include "except.h"
  63. #include "target.h"
  64. #include "tree-pass.h"
  65. #include "df.h"
  66. #include "dbgcnt.h"
  67. static int reload_cse_noop_set_p (rtx);
  68. static bool reload_cse_simplify (rtx_insn *, rtx);
  69. static void reload_cse_regs_1 (void);
  70. static int reload_cse_simplify_set (rtx, rtx_insn *);
  71. static int reload_cse_simplify_operands (rtx_insn *, rtx);
  72. static void reload_combine (void);
  73. static void reload_combine_note_use (rtx *, rtx_insn *, int, rtx);
  74. static void reload_combine_note_store (rtx, const_rtx, void *);
  75. static bool reload_cse_move2add (rtx_insn *);
  76. static void move2add_note_store (rtx, const_rtx, void *);
  77. /* Call cse / combine like post-reload optimization phases.
  78. FIRST is the first instruction. */
  79. static void
  80. reload_cse_regs (rtx_insn *first ATTRIBUTE_UNUSED)
  81. {
  82. bool moves_converted;
  83. reload_cse_regs_1 ();
  84. reload_combine ();
  85. moves_converted = reload_cse_move2add (first);
  86. if (flag_expensive_optimizations)
  87. {
  88. if (moves_converted)
  89. reload_combine ();
  90. reload_cse_regs_1 ();
  91. }
  92. }
  93. /* See whether a single set SET is a noop. */
  94. static int
  95. reload_cse_noop_set_p (rtx set)
  96. {
  97. if (cselib_reg_set_mode (SET_DEST (set)) != GET_MODE (SET_DEST (set)))
  98. return 0;
  99. return rtx_equal_for_cselib_p (SET_DEST (set), SET_SRC (set));
  100. }
  101. /* Try to simplify INSN. Return true if the CFG may have changed. */
  102. static bool
  103. reload_cse_simplify (rtx_insn *insn, rtx testreg)
  104. {
  105. rtx body = PATTERN (insn);
  106. basic_block insn_bb = BLOCK_FOR_INSN (insn);
  107. unsigned insn_bb_succs = EDGE_COUNT (insn_bb->succs);
  108. if (GET_CODE (body) == SET)
  109. {
  110. int count = 0;
  111. /* Simplify even if we may think it is a no-op.
  112. We may think a memory load of a value smaller than WORD_SIZE
  113. is redundant because we haven't taken into account possible
  114. implicit extension. reload_cse_simplify_set() will bring
  115. this out, so it's safer to simplify before we delete. */
  116. count += reload_cse_simplify_set (body, insn);
  117. if (!count && reload_cse_noop_set_p (body))
  118. {
  119. rtx value = SET_DEST (body);
  120. if (REG_P (value)
  121. && ! REG_FUNCTION_VALUE_P (value))
  122. value = 0;
  123. if (check_for_inc_dec (insn))
  124. delete_insn_and_edges (insn);
  125. /* We're done with this insn. */
  126. goto done;
  127. }
  128. if (count > 0)
  129. apply_change_group ();
  130. else
  131. reload_cse_simplify_operands (insn, testreg);
  132. }
  133. else if (GET_CODE (body) == PARALLEL)
  134. {
  135. int i;
  136. int count = 0;
  137. rtx value = NULL_RTX;
  138. /* Registers mentioned in the clobber list for an asm cannot be reused
  139. within the body of the asm. Invalidate those registers now so that
  140. we don't try to substitute values for them. */
  141. if (asm_noperands (body) >= 0)
  142. {
  143. for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
  144. {
  145. rtx part = XVECEXP (body, 0, i);
  146. if (GET_CODE (part) == CLOBBER && REG_P (XEXP (part, 0)))
  147. cselib_invalidate_rtx (XEXP (part, 0));
  148. }
  149. }
  150. /* If every action in a PARALLEL is a noop, we can delete
  151. the entire PARALLEL. */
  152. for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
  153. {
  154. rtx part = XVECEXP (body, 0, i);
  155. if (GET_CODE (part) == SET)
  156. {
  157. if (! reload_cse_noop_set_p (part))
  158. break;
  159. if (REG_P (SET_DEST (part))
  160. && REG_FUNCTION_VALUE_P (SET_DEST (part)))
  161. {
  162. if (value)
  163. break;
  164. value = SET_DEST (part);
  165. }
  166. }
  167. else if (GET_CODE (part) != CLOBBER)
  168. break;
  169. }
  170. if (i < 0)
  171. {
  172. if (check_for_inc_dec (insn))
  173. delete_insn_and_edges (insn);
  174. /* We're done with this insn. */
  175. goto done;
  176. }
  177. /* It's not a no-op, but we can try to simplify it. */
  178. for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
  179. if (GET_CODE (XVECEXP (body, 0, i)) == SET)
  180. count += reload_cse_simplify_set (XVECEXP (body, 0, i), insn);
  181. if (count > 0)
  182. apply_change_group ();
  183. else
  184. reload_cse_simplify_operands (insn, testreg);
  185. }
  186. done:
  187. return (EDGE_COUNT (insn_bb->succs) != insn_bb_succs);
  188. }
  189. /* Do a very simple CSE pass over the hard registers.
  190. This function detects no-op moves where we happened to assign two
  191. different pseudo-registers to the same hard register, and then
  192. copied one to the other. Reload will generate a useless
  193. instruction copying a register to itself.
  194. This function also detects cases where we load a value from memory
  195. into two different registers, and (if memory is more expensive than
  196. registers) changes it to simply copy the first register into the
  197. second register.
  198. Another optimization is performed that scans the operands of each
  199. instruction to see whether the value is already available in a
  200. hard register. It then replaces the operand with the hard register
  201. if possible, much like an optional reload would. */
  202. static void
  203. reload_cse_regs_1 (void)
  204. {
  205. bool cfg_changed = false;
  206. basic_block bb;
  207. rtx_insn *insn;
  208. rtx testreg = gen_rtx_REG (VOIDmode, -1);
  209. cselib_init (CSELIB_RECORD_MEMORY);
  210. init_alias_analysis ();
  211. FOR_EACH_BB_FN (bb, cfun)
  212. FOR_BB_INSNS (bb, insn)
  213. {
  214. if (INSN_P (insn))
  215. cfg_changed |= reload_cse_simplify (insn, testreg);
  216. cselib_process_insn (insn);
  217. }
  218. /* Clean up. */
  219. end_alias_analysis ();
  220. cselib_finish ();
  221. if (cfg_changed)
  222. cleanup_cfg (0);
  223. }
  224. /* Try to simplify a single SET instruction. SET is the set pattern.
  225. INSN is the instruction it came from.
  226. This function only handles one case: if we set a register to a value
  227. which is not a register, we try to find that value in some other register
  228. and change the set into a register copy. */
  229. static int
  230. reload_cse_simplify_set (rtx set, rtx_insn *insn)
  231. {
  232. int did_change = 0;
  233. int dreg;
  234. rtx src;
  235. reg_class_t dclass;
  236. int old_cost;
  237. cselib_val *val;
  238. struct elt_loc_list *l;
  239. #ifdef LOAD_EXTEND_OP
  240. enum rtx_code extend_op = UNKNOWN;
  241. #endif
  242. bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
  243. dreg = true_regnum (SET_DEST (set));
  244. if (dreg < 0)
  245. return 0;
  246. src = SET_SRC (set);
  247. if (side_effects_p (src) || true_regnum (src) >= 0)
  248. return 0;
  249. dclass = REGNO_REG_CLASS (dreg);
  250. #ifdef LOAD_EXTEND_OP
  251. /* When replacing a memory with a register, we need to honor assumptions
  252. that combine made wrt the contents of sign bits. We'll do this by
  253. generating an extend instruction instead of a reg->reg copy. Thus
  254. the destination must be a register that we can widen. */
  255. if (MEM_P (src)
  256. && GET_MODE_BITSIZE (GET_MODE (src)) < BITS_PER_WORD
  257. && (extend_op = LOAD_EXTEND_OP (GET_MODE (src))) != UNKNOWN
  258. && !REG_P (SET_DEST (set)))
  259. return 0;
  260. #endif
  261. val = cselib_lookup (src, GET_MODE (SET_DEST (set)), 0, VOIDmode);
  262. if (! val)
  263. return 0;
  264. /* If memory loads are cheaper than register copies, don't change them. */
  265. if (MEM_P (src))
  266. old_cost = memory_move_cost (GET_MODE (src), dclass, true);
  267. else if (REG_P (src))
  268. old_cost = register_move_cost (GET_MODE (src),
  269. REGNO_REG_CLASS (REGNO (src)), dclass);
  270. else
  271. old_cost = set_src_cost (src, speed);
  272. for (l = val->locs; l; l = l->next)
  273. {
  274. rtx this_rtx = l->loc;
  275. int this_cost;
  276. if (CONSTANT_P (this_rtx) && ! references_value_p (this_rtx, 0))
  277. {
  278. #ifdef LOAD_EXTEND_OP
  279. if (extend_op != UNKNOWN)
  280. {
  281. wide_int result;
  282. if (!CONST_SCALAR_INT_P (this_rtx))
  283. continue;
  284. switch (extend_op)
  285. {
  286. case ZERO_EXTEND:
  287. result = wide_int::from (std::make_pair (this_rtx,
  288. GET_MODE (src)),
  289. BITS_PER_WORD, UNSIGNED);
  290. break;
  291. case SIGN_EXTEND:
  292. result = wide_int::from (std::make_pair (this_rtx,
  293. GET_MODE (src)),
  294. BITS_PER_WORD, SIGNED);
  295. break;
  296. default:
  297. gcc_unreachable ();
  298. }
  299. this_rtx = immed_wide_int_const (result, word_mode);
  300. }
  301. #endif
  302. this_cost = set_src_cost (this_rtx, speed);
  303. }
  304. else if (REG_P (this_rtx))
  305. {
  306. #ifdef LOAD_EXTEND_OP
  307. if (extend_op != UNKNOWN)
  308. {
  309. this_rtx = gen_rtx_fmt_e (extend_op, word_mode, this_rtx);
  310. this_cost = set_src_cost (this_rtx, speed);
  311. }
  312. else
  313. #endif
  314. this_cost = register_move_cost (GET_MODE (this_rtx),
  315. REGNO_REG_CLASS (REGNO (this_rtx)),
  316. dclass);
  317. }
  318. else
  319. continue;
  320. /* If equal costs, prefer registers over anything else. That
  321. tends to lead to smaller instructions on some machines. */
  322. if (this_cost < old_cost
  323. || (this_cost == old_cost
  324. && REG_P (this_rtx)
  325. && !REG_P (SET_SRC (set))))
  326. {
  327. #ifdef LOAD_EXTEND_OP
  328. if (GET_MODE_BITSIZE (GET_MODE (SET_DEST (set))) < BITS_PER_WORD
  329. && extend_op != UNKNOWN
  330. #ifdef CANNOT_CHANGE_MODE_CLASS
  331. && !CANNOT_CHANGE_MODE_CLASS (GET_MODE (SET_DEST (set)),
  332. word_mode,
  333. REGNO_REG_CLASS (REGNO (SET_DEST (set))))
  334. #endif
  335. )
  336. {
  337. rtx wide_dest = gen_rtx_REG (word_mode, REGNO (SET_DEST (set)));
  338. ORIGINAL_REGNO (wide_dest) = ORIGINAL_REGNO (SET_DEST (set));
  339. validate_change (insn, &SET_DEST (set), wide_dest, 1);
  340. }
  341. #endif
  342. validate_unshare_change (insn, &SET_SRC (set), this_rtx, 1);
  343. old_cost = this_cost, did_change = 1;
  344. }
  345. }
  346. return did_change;
  347. }
  348. /* Try to replace operands in INSN with equivalent values that are already
  349. in registers. This can be viewed as optional reloading.
  350. For each non-register operand in the insn, see if any hard regs are
  351. known to be equivalent to that operand. Record the alternatives which
  352. can accept these hard registers. Among all alternatives, select the
  353. ones which are better or equal to the one currently matching, where
  354. "better" is in terms of '?' and '!' constraints. Among the remaining
  355. alternatives, select the one which replaces most operands with
  356. hard registers. */
  357. static int
  358. reload_cse_simplify_operands (rtx_insn *insn, rtx testreg)
  359. {
  360. int i, j;
  361. /* For each operand, all registers that are equivalent to it. */
  362. HARD_REG_SET equiv_regs[MAX_RECOG_OPERANDS];
  363. const char *constraints[MAX_RECOG_OPERANDS];
  364. /* Vector recording how bad an alternative is. */
  365. int *alternative_reject;
  366. /* Vector recording how many registers can be introduced by choosing
  367. this alternative. */
  368. int *alternative_nregs;
  369. /* Array of vectors recording, for each operand and each alternative,
  370. which hard register to substitute, or -1 if the operand should be
  371. left as it is. */
  372. int *op_alt_regno[MAX_RECOG_OPERANDS];
  373. /* Array of alternatives, sorted in order of decreasing desirability. */
  374. int *alternative_order;
  375. extract_constrain_insn (insn);
  376. if (recog_data.n_alternatives == 0 || recog_data.n_operands == 0)
  377. return 0;
  378. alternative_reject = XALLOCAVEC (int, recog_data.n_alternatives);
  379. alternative_nregs = XALLOCAVEC (int, recog_data.n_alternatives);
  380. alternative_order = XALLOCAVEC (int, recog_data.n_alternatives);
  381. memset (alternative_reject, 0, recog_data.n_alternatives * sizeof (int));
  382. memset (alternative_nregs, 0, recog_data.n_alternatives * sizeof (int));
  383. /* For each operand, find out which regs are equivalent. */
  384. for (i = 0; i < recog_data.n_operands; i++)
  385. {
  386. cselib_val *v;
  387. struct elt_loc_list *l;
  388. rtx op;
  389. CLEAR_HARD_REG_SET (equiv_regs[i]);
  390. /* cselib blows up on CODE_LABELs. Trying to fix that doesn't seem
  391. right, so avoid the problem here. Likewise if we have a constant
  392. and the insn pattern doesn't tell us the mode we need. */
  393. if (LABEL_P (recog_data.operand[i])
  394. || (CONSTANT_P (recog_data.operand[i])
  395. && recog_data.operand_mode[i] == VOIDmode))
  396. continue;
  397. op = recog_data.operand[i];
  398. #ifdef LOAD_EXTEND_OP
  399. if (MEM_P (op)
  400. && GET_MODE_BITSIZE (GET_MODE (op)) < BITS_PER_WORD
  401. && LOAD_EXTEND_OP (GET_MODE (op)) != UNKNOWN)
  402. {
  403. rtx set = single_set (insn);
  404. /* We might have multiple sets, some of which do implicit
  405. extension. Punt on this for now. */
  406. if (! set)
  407. continue;
  408. /* If the destination is also a MEM or a STRICT_LOW_PART, no
  409. extension applies.
  410. Also, if there is an explicit extension, we don't have to
  411. worry about an implicit one. */
  412. else if (MEM_P (SET_DEST (set))
  413. || GET_CODE (SET_DEST (set)) == STRICT_LOW_PART
  414. || GET_CODE (SET_SRC (set)) == ZERO_EXTEND
  415. || GET_CODE (SET_SRC (set)) == SIGN_EXTEND)
  416. ; /* Continue ordinary processing. */
  417. #ifdef CANNOT_CHANGE_MODE_CLASS
  418. /* If the register cannot change mode to word_mode, it follows that
  419. it cannot have been used in word_mode. */
  420. else if (REG_P (SET_DEST (set))
  421. && CANNOT_CHANGE_MODE_CLASS (GET_MODE (SET_DEST (set)),
  422. word_mode,
  423. REGNO_REG_CLASS (REGNO (SET_DEST (set)))))
  424. ; /* Continue ordinary processing. */
  425. #endif
  426. /* If this is a straight load, make the extension explicit. */
  427. else if (REG_P (SET_DEST (set))
  428. && recog_data.n_operands == 2
  429. && SET_SRC (set) == op
  430. && SET_DEST (set) == recog_data.operand[1-i])
  431. {
  432. validate_change (insn, recog_data.operand_loc[i],
  433. gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (op)),
  434. word_mode, op),
  435. 1);
  436. validate_change (insn, recog_data.operand_loc[1-i],
  437. gen_rtx_REG (word_mode, REGNO (SET_DEST (set))),
  438. 1);
  439. if (! apply_change_group ())
  440. return 0;
  441. return reload_cse_simplify_operands (insn, testreg);
  442. }
  443. else
  444. /* ??? There might be arithmetic operations with memory that are
  445. safe to optimize, but is it worth the trouble? */
  446. continue;
  447. }
  448. #endif /* LOAD_EXTEND_OP */
  449. if (side_effects_p (op))
  450. continue;
  451. v = cselib_lookup (op, recog_data.operand_mode[i], 0, VOIDmode);
  452. if (! v)
  453. continue;
  454. for (l = v->locs; l; l = l->next)
  455. if (REG_P (l->loc))
  456. SET_HARD_REG_BIT (equiv_regs[i], REGNO (l->loc));
  457. }
  458. alternative_mask preferred = get_preferred_alternatives (insn);
  459. for (i = 0; i < recog_data.n_operands; i++)
  460. {
  461. machine_mode mode;
  462. int regno;
  463. const char *p;
  464. op_alt_regno[i] = XALLOCAVEC (int, recog_data.n_alternatives);
  465. for (j = 0; j < recog_data.n_alternatives; j++)
  466. op_alt_regno[i][j] = -1;
  467. p = constraints[i] = recog_data.constraints[i];
  468. mode = recog_data.operand_mode[i];
  469. /* Add the reject values for each alternative given by the constraints
  470. for this operand. */
  471. j = 0;
  472. while (*p != '\0')
  473. {
  474. char c = *p++;
  475. if (c == ',')
  476. j++;
  477. else if (c == '?')
  478. alternative_reject[j] += 3;
  479. else if (c == '!')
  480. alternative_reject[j] += 300;
  481. }
  482. /* We won't change operands which are already registers. We
  483. also don't want to modify output operands. */
  484. regno = true_regnum (recog_data.operand[i]);
  485. if (regno >= 0
  486. || constraints[i][0] == '='
  487. || constraints[i][0] == '+')
  488. continue;
  489. for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
  490. {
  491. enum reg_class rclass = NO_REGS;
  492. if (! TEST_HARD_REG_BIT (equiv_regs[i], regno))
  493. continue;
  494. SET_REGNO_RAW (testreg, regno);
  495. PUT_MODE (testreg, mode);
  496. /* We found a register equal to this operand. Now look for all
  497. alternatives that can accept this register and have not been
  498. assigned a register they can use yet. */
  499. j = 0;
  500. p = constraints[i];
  501. for (;;)
  502. {
  503. char c = *p;
  504. switch (c)
  505. {
  506. case 'g':
  507. rclass = reg_class_subunion[rclass][GENERAL_REGS];
  508. break;
  509. default:
  510. rclass
  511. = (reg_class_subunion
  512. [rclass]
  513. [reg_class_for_constraint (lookup_constraint (p))]);
  514. break;
  515. case ',': case '\0':
  516. /* See if REGNO fits this alternative, and set it up as the
  517. replacement register if we don't have one for this
  518. alternative yet and the operand being replaced is not
  519. a cheap CONST_INT. */
  520. if (op_alt_regno[i][j] == -1
  521. && TEST_BIT (preferred, j)
  522. && reg_fits_class_p (testreg, rclass, 0, mode)
  523. && (!CONST_INT_P (recog_data.operand[i])
  524. || (set_src_cost (recog_data.operand[i],
  525. optimize_bb_for_speed_p
  526. (BLOCK_FOR_INSN (insn)))
  527. > set_src_cost (testreg,
  528. optimize_bb_for_speed_p
  529. (BLOCK_FOR_INSN (insn))))))
  530. {
  531. alternative_nregs[j]++;
  532. op_alt_regno[i][j] = regno;
  533. }
  534. j++;
  535. rclass = NO_REGS;
  536. break;
  537. }
  538. p += CONSTRAINT_LEN (c, p);
  539. if (c == '\0')
  540. break;
  541. }
  542. }
  543. }
  544. /* Record all alternatives which are better or equal to the currently
  545. matching one in the alternative_order array. */
  546. for (i = j = 0; i < recog_data.n_alternatives; i++)
  547. if (alternative_reject[i] <= alternative_reject[which_alternative])
  548. alternative_order[j++] = i;
  549. recog_data.n_alternatives = j;
  550. /* Sort it. Given a small number of alternatives, a dumb algorithm
  551. won't hurt too much. */
  552. for (i = 0; i < recog_data.n_alternatives - 1; i++)
  553. {
  554. int best = i;
  555. int best_reject = alternative_reject[alternative_order[i]];
  556. int best_nregs = alternative_nregs[alternative_order[i]];
  557. int tmp;
  558. for (j = i + 1; j < recog_data.n_alternatives; j++)
  559. {
  560. int this_reject = alternative_reject[alternative_order[j]];
  561. int this_nregs = alternative_nregs[alternative_order[j]];
  562. if (this_reject < best_reject
  563. || (this_reject == best_reject && this_nregs > best_nregs))
  564. {
  565. best = j;
  566. best_reject = this_reject;
  567. best_nregs = this_nregs;
  568. }
  569. }
  570. tmp = alternative_order[best];
  571. alternative_order[best] = alternative_order[i];
  572. alternative_order[i] = tmp;
  573. }
  574. /* Substitute the operands as determined by op_alt_regno for the best
  575. alternative. */
  576. j = alternative_order[0];
  577. for (i = 0; i < recog_data.n_operands; i++)
  578. {
  579. machine_mode mode = recog_data.operand_mode[i];
  580. if (op_alt_regno[i][j] == -1)
  581. continue;
  582. validate_change (insn, recog_data.operand_loc[i],
  583. gen_rtx_REG (mode, op_alt_regno[i][j]), 1);
  584. }
  585. for (i = recog_data.n_dups - 1; i >= 0; i--)
  586. {
  587. int op = recog_data.dup_num[i];
  588. machine_mode mode = recog_data.operand_mode[op];
  589. if (op_alt_regno[op][j] == -1)
  590. continue;
  591. validate_change (insn, recog_data.dup_loc[i],
  592. gen_rtx_REG (mode, op_alt_regno[op][j]), 1);
  593. }
  594. return apply_change_group ();
  595. }
  596. /* If reload couldn't use reg+reg+offset addressing, try to use reg+reg
  597. addressing now.
  598. This code might also be useful when reload gave up on reg+reg addressing
  599. because of clashes between the return register and INDEX_REG_CLASS. */
  600. /* The maximum number of uses of a register we can keep track of to
  601. replace them with reg+reg addressing. */
  602. #define RELOAD_COMBINE_MAX_USES 16
  603. /* Describes a recorded use of a register. */
  604. struct reg_use
  605. {
  606. /* The insn where a register has been used. */
  607. rtx_insn *insn;
  608. /* Points to the memory reference enclosing the use, if any, NULL_RTX
  609. otherwise. */
  610. rtx containing_mem;
  611. /* Location of the register within INSN. */
  612. rtx *usep;
  613. /* The reverse uid of the insn. */
  614. int ruid;
  615. };
  616. /* If the register is used in some unknown fashion, USE_INDEX is negative.
  617. If it is dead, USE_INDEX is RELOAD_COMBINE_MAX_USES, and STORE_RUID
  618. indicates where it is first set or clobbered.
  619. Otherwise, USE_INDEX is the index of the last encountered use of the
  620. register (which is first among these we have seen since we scan backwards).
  621. USE_RUID indicates the first encountered, i.e. last, of these uses.
  622. If ALL_OFFSETS_MATCH is true, all encountered uses were inside a PLUS
  623. with a constant offset; OFFSET contains this constant in that case.
  624. STORE_RUID is always meaningful if we only want to use a value in a
  625. register in a different place: it denotes the next insn in the insn
  626. stream (i.e. the last encountered) that sets or clobbers the register.
  627. REAL_STORE_RUID is similar, but clobbers are ignored when updating it. */
  628. static struct
  629. {
  630. struct reg_use reg_use[RELOAD_COMBINE_MAX_USES];
  631. rtx offset;
  632. int use_index;
  633. int store_ruid;
  634. int real_store_ruid;
  635. int use_ruid;
  636. bool all_offsets_match;
  637. } reg_state[FIRST_PSEUDO_REGISTER];
  638. /* Reverse linear uid. This is increased in reload_combine while scanning
  639. the instructions from last to first. It is used to set last_label_ruid
  640. and the store_ruid / use_ruid fields in reg_state. */
  641. static int reload_combine_ruid;
  642. /* The RUID of the last label we encountered in reload_combine. */
  643. static int last_label_ruid;
  644. /* The RUID of the last jump we encountered in reload_combine. */
  645. static int last_jump_ruid;
  646. /* The register numbers of the first and last index register. A value of
  647. -1 in LAST_INDEX_REG indicates that we've previously computed these
  648. values and found no suitable index registers. */
  649. static int first_index_reg = -1;
  650. static int last_index_reg;
  651. #define LABEL_LIVE(LABEL) \
  652. (label_live[CODE_LABEL_NUMBER (LABEL) - min_labelno])
  653. /* Subroutine of reload_combine_split_ruids, called to fix up a single
  654. ruid pointed to by *PRUID if it is higher than SPLIT_RUID. */
  655. static inline void
  656. reload_combine_split_one_ruid (int *pruid, int split_ruid)
  657. {
  658. if (*pruid > split_ruid)
  659. (*pruid)++;
  660. }
  661. /* Called when we insert a new insn in a position we've already passed in
  662. the scan. Examine all our state, increasing all ruids that are higher
  663. than SPLIT_RUID by one in order to make room for a new insn. */
  664. static void
  665. reload_combine_split_ruids (int split_ruid)
  666. {
  667. unsigned i;
  668. reload_combine_split_one_ruid (&reload_combine_ruid, split_ruid);
  669. reload_combine_split_one_ruid (&last_label_ruid, split_ruid);
  670. reload_combine_split_one_ruid (&last_jump_ruid, split_ruid);
  671. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  672. {
  673. int j, idx = reg_state[i].use_index;
  674. reload_combine_split_one_ruid (&reg_state[i].use_ruid, split_ruid);
  675. reload_combine_split_one_ruid (&reg_state[i].store_ruid, split_ruid);
  676. reload_combine_split_one_ruid (&reg_state[i].real_store_ruid,
  677. split_ruid);
  678. if (idx < 0)
  679. continue;
  680. for (j = idx; j < RELOAD_COMBINE_MAX_USES; j++)
  681. {
  682. reload_combine_split_one_ruid (&reg_state[i].reg_use[j].ruid,
  683. split_ruid);
  684. }
  685. }
  686. }
  687. /* Called when we are about to rescan a previously encountered insn with
  688. reload_combine_note_use after modifying some part of it. This clears all
  689. information about uses in that particular insn. */
  690. static void
  691. reload_combine_purge_insn_uses (rtx_insn *insn)
  692. {
  693. unsigned i;
  694. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  695. {
  696. int j, k, idx = reg_state[i].use_index;
  697. if (idx < 0)
  698. continue;
  699. j = k = RELOAD_COMBINE_MAX_USES;
  700. while (j-- > idx)
  701. {
  702. if (reg_state[i].reg_use[j].insn != insn)
  703. {
  704. k--;
  705. if (k != j)
  706. reg_state[i].reg_use[k] = reg_state[i].reg_use[j];
  707. }
  708. }
  709. reg_state[i].use_index = k;
  710. }
  711. }
  712. /* Called when we need to forget about all uses of REGNO after an insn
  713. which is identified by RUID. */
  714. static void
  715. reload_combine_purge_reg_uses_after_ruid (unsigned regno, int ruid)
  716. {
  717. int j, k, idx = reg_state[regno].use_index;
  718. if (idx < 0)
  719. return;
  720. j = k = RELOAD_COMBINE_MAX_USES;
  721. while (j-- > idx)
  722. {
  723. if (reg_state[regno].reg_use[j].ruid >= ruid)
  724. {
  725. k--;
  726. if (k != j)
  727. reg_state[regno].reg_use[k] = reg_state[regno].reg_use[j];
  728. }
  729. }
  730. reg_state[regno].use_index = k;
  731. }
  732. /* Find the use of REGNO with the ruid that is highest among those
  733. lower than RUID_LIMIT, and return it if it is the only use of this
  734. reg in the insn. Return NULL otherwise. */
  735. static struct reg_use *
  736. reload_combine_closest_single_use (unsigned regno, int ruid_limit)
  737. {
  738. int i, best_ruid = 0;
  739. int use_idx = reg_state[regno].use_index;
  740. struct reg_use *retval;
  741. if (use_idx < 0)
  742. return NULL;
  743. retval = NULL;
  744. for (i = use_idx; i < RELOAD_COMBINE_MAX_USES; i++)
  745. {
  746. struct reg_use *use = reg_state[regno].reg_use + i;
  747. int this_ruid = use->ruid;
  748. if (this_ruid >= ruid_limit)
  749. continue;
  750. if (this_ruid > best_ruid)
  751. {
  752. best_ruid = this_ruid;
  753. retval = use;
  754. }
  755. else if (this_ruid == best_ruid)
  756. retval = NULL;
  757. }
  758. if (last_label_ruid >= best_ruid)
  759. return NULL;
  760. return retval;
  761. }
  762. /* After we've moved an add insn, fix up any debug insns that occur
  763. between the old location of the add and the new location. REG is
  764. the destination register of the add insn; REPLACEMENT is the
  765. SET_SRC of the add. FROM and TO specify the range in which we
  766. should make this change on debug insns. */
  767. static void
  768. fixup_debug_insns (rtx reg, rtx replacement, rtx_insn *from, rtx_insn *to)
  769. {
  770. rtx_insn *insn;
  771. for (insn = from; insn != to; insn = NEXT_INSN (insn))
  772. {
  773. rtx t;
  774. if (!DEBUG_INSN_P (insn))
  775. continue;
  776. t = INSN_VAR_LOCATION_LOC (insn);
  777. t = simplify_replace_rtx (t, reg, replacement);
  778. validate_change (insn, &INSN_VAR_LOCATION_LOC (insn), t, 0);
  779. }
  780. }
  781. /* Subroutine of reload_combine_recognize_const_pattern. Try to replace REG
  782. with SRC in the insn described by USE, taking costs into account. Return
  783. true if we made the replacement. */
  784. static bool
  785. try_replace_in_use (struct reg_use *use, rtx reg, rtx src)
  786. {
  787. rtx_insn *use_insn = use->insn;
  788. rtx mem = use->containing_mem;
  789. bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (use_insn));
  790. if (mem != NULL_RTX)
  791. {
  792. addr_space_t as = MEM_ADDR_SPACE (mem);
  793. rtx oldaddr = XEXP (mem, 0);
  794. rtx newaddr = NULL_RTX;
  795. int old_cost = address_cost (oldaddr, GET_MODE (mem), as, speed);
  796. int new_cost;
  797. newaddr = simplify_replace_rtx (oldaddr, reg, src);
  798. if (memory_address_addr_space_p (GET_MODE (mem), newaddr, as))
  799. {
  800. XEXP (mem, 0) = newaddr;
  801. new_cost = address_cost (newaddr, GET_MODE (mem), as, speed);
  802. XEXP (mem, 0) = oldaddr;
  803. if (new_cost <= old_cost
  804. && validate_change (use_insn,
  805. &XEXP (mem, 0), newaddr, 0))
  806. return true;
  807. }
  808. }
  809. else
  810. {
  811. rtx new_set = single_set (use_insn);
  812. if (new_set
  813. && REG_P (SET_DEST (new_set))
  814. && GET_CODE (SET_SRC (new_set)) == PLUS
  815. && REG_P (XEXP (SET_SRC (new_set), 0))
  816. && CONSTANT_P (XEXP (SET_SRC (new_set), 1)))
  817. {
  818. rtx new_src;
  819. int old_cost = set_src_cost (SET_SRC (new_set), speed);
  820. gcc_assert (rtx_equal_p (XEXP (SET_SRC (new_set), 0), reg));
  821. new_src = simplify_replace_rtx (SET_SRC (new_set), reg, src);
  822. if (set_src_cost (new_src, speed) <= old_cost
  823. && validate_change (use_insn, &SET_SRC (new_set),
  824. new_src, 0))
  825. return true;
  826. }
  827. }
  828. return false;
  829. }
  830. /* Called by reload_combine when scanning INSN. This function tries to detect
  831. patterns where a constant is added to a register, and the result is used
  832. in an address.
  833. Return true if no further processing is needed on INSN; false if it wasn't
  834. recognized and should be handled normally. */
  835. static bool
  836. reload_combine_recognize_const_pattern (rtx_insn *insn)
  837. {
  838. int from_ruid = reload_combine_ruid;
  839. rtx set, pat, reg, src, addreg;
  840. unsigned int regno;
  841. struct reg_use *use;
  842. bool must_move_add;
  843. rtx_insn *add_moved_after_insn = NULL;
  844. int add_moved_after_ruid = 0;
  845. int clobbered_regno = -1;
  846. set = single_set (insn);
  847. if (set == NULL_RTX)
  848. return false;
  849. reg = SET_DEST (set);
  850. src = SET_SRC (set);
  851. if (!REG_P (reg)
  852. || hard_regno_nregs[REGNO (reg)][GET_MODE (reg)] != 1
  853. || GET_MODE (reg) != Pmode
  854. || reg == stack_pointer_rtx)
  855. return false;
  856. regno = REGNO (reg);
  857. /* We look for a REG1 = REG2 + CONSTANT insn, followed by either
  858. uses of REG1 inside an address, or inside another add insn. If
  859. possible and profitable, merge the addition into subsequent
  860. uses. */
  861. if (GET_CODE (src) != PLUS
  862. || !REG_P (XEXP (src, 0))
  863. || !CONSTANT_P (XEXP (src, 1)))
  864. return false;
  865. addreg = XEXP (src, 0);
  866. must_move_add = rtx_equal_p (reg, addreg);
  867. pat = PATTERN (insn);
  868. if (must_move_add && set != pat)
  869. {
  870. /* We have to be careful when moving the add; apart from the
  871. single_set there may also be clobbers. Recognize one special
  872. case, that of one clobber alongside the set (likely a clobber
  873. of the CC register). */
  874. gcc_assert (GET_CODE (PATTERN (insn)) == PARALLEL);
  875. if (XVECLEN (pat, 0) != 2 || XVECEXP (pat, 0, 0) != set
  876. || GET_CODE (XVECEXP (pat, 0, 1)) != CLOBBER
  877. || !REG_P (XEXP (XVECEXP (pat, 0, 1), 0)))
  878. return false;
  879. clobbered_regno = REGNO (XEXP (XVECEXP (pat, 0, 1), 0));
  880. }
  881. do
  882. {
  883. use = reload_combine_closest_single_use (regno, from_ruid);
  884. if (use)
  885. /* Start the search for the next use from here. */
  886. from_ruid = use->ruid;
  887. if (use && GET_MODE (*use->usep) == Pmode)
  888. {
  889. bool delete_add = false;
  890. rtx_insn *use_insn = use->insn;
  891. int use_ruid = use->ruid;
  892. /* Avoid moving the add insn past a jump. */
  893. if (must_move_add && use_ruid <= last_jump_ruid)
  894. break;
  895. /* If the add clobbers another hard reg in parallel, don't move
  896. it past a real set of this hard reg. */
  897. if (must_move_add && clobbered_regno >= 0
  898. && reg_state[clobbered_regno].real_store_ruid >= use_ruid)
  899. break;
  900. #ifdef HAVE_cc0
  901. /* Do not separate cc0 setter and cc0 user on HAVE_cc0 targets. */
  902. if (must_move_add && sets_cc0_p (PATTERN (use_insn)))
  903. break;
  904. #endif
  905. gcc_assert (reg_state[regno].store_ruid <= use_ruid);
  906. /* Avoid moving a use of ADDREG past a point where it is stored. */
  907. if (reg_state[REGNO (addreg)].store_ruid > use_ruid)
  908. break;
  909. /* We also must not move the addition past an insn that sets
  910. the same register, unless we can combine two add insns. */
  911. if (must_move_add && reg_state[regno].store_ruid == use_ruid)
  912. {
  913. if (use->containing_mem == NULL_RTX)
  914. delete_add = true;
  915. else
  916. break;
  917. }
  918. if (try_replace_in_use (use, reg, src))
  919. {
  920. reload_combine_purge_insn_uses (use_insn);
  921. reload_combine_note_use (&PATTERN (use_insn), use_insn,
  922. use_ruid, NULL_RTX);
  923. if (delete_add)
  924. {
  925. fixup_debug_insns (reg, src, insn, use_insn);
  926. delete_insn (insn);
  927. return true;
  928. }
  929. if (must_move_add)
  930. {
  931. add_moved_after_insn = use_insn;
  932. add_moved_after_ruid = use_ruid;
  933. }
  934. continue;
  935. }
  936. }
  937. /* If we get here, we couldn't handle this use. */
  938. if (must_move_add)
  939. break;
  940. }
  941. while (use);
  942. if (!must_move_add || add_moved_after_insn == NULL_RTX)
  943. /* Process the add normally. */
  944. return false;
  945. fixup_debug_insns (reg, src, insn, add_moved_after_insn);
  946. reorder_insns (insn, insn, add_moved_after_insn);
  947. reload_combine_purge_reg_uses_after_ruid (regno, add_moved_after_ruid);
  948. reload_combine_split_ruids (add_moved_after_ruid - 1);
  949. reload_combine_note_use (&PATTERN (insn), insn,
  950. add_moved_after_ruid, NULL_RTX);
  951. reg_state[regno].store_ruid = add_moved_after_ruid;
  952. return true;
  953. }
  954. /* Called by reload_combine when scanning INSN. Try to detect a pattern we
  955. can handle and improve. Return true if no further processing is needed on
  956. INSN; false if it wasn't recognized and should be handled normally. */
  957. static bool
  958. reload_combine_recognize_pattern (rtx_insn *insn)
  959. {
  960. rtx set, reg, src;
  961. unsigned int regno;
  962. set = single_set (insn);
  963. if (set == NULL_RTX)
  964. return false;
  965. reg = SET_DEST (set);
  966. src = SET_SRC (set);
  967. if (!REG_P (reg)
  968. || hard_regno_nregs[REGNO (reg)][GET_MODE (reg)] != 1)
  969. return false;
  970. regno = REGNO (reg);
  971. /* Look for (set (REGX) (CONST_INT))
  972. (set (REGX) (PLUS (REGX) (REGY)))
  973. ...
  974. ... (MEM (REGX)) ...
  975. and convert it to
  976. (set (REGZ) (CONST_INT))
  977. ...
  978. ... (MEM (PLUS (REGZ) (REGY)))... .
  979. First, check that we have (set (REGX) (PLUS (REGX) (REGY)))
  980. and that we know all uses of REGX before it dies.
  981. Also, explicitly check that REGX != REGY; our life information
  982. does not yet show whether REGY changes in this insn. */
  983. if (GET_CODE (src) == PLUS
  984. && reg_state[regno].all_offsets_match
  985. && last_index_reg != -1
  986. && REG_P (XEXP (src, 1))
  987. && rtx_equal_p (XEXP (src, 0), reg)
  988. && !rtx_equal_p (XEXP (src, 1), reg)
  989. && reg_state[regno].use_index >= 0
  990. && reg_state[regno].use_index < RELOAD_COMBINE_MAX_USES
  991. && last_label_ruid < reg_state[regno].use_ruid)
  992. {
  993. rtx base = XEXP (src, 1);
  994. rtx_insn *prev = prev_nonnote_nondebug_insn (insn);
  995. rtx prev_set = prev ? single_set (prev) : NULL_RTX;
  996. rtx index_reg = NULL_RTX;
  997. rtx reg_sum = NULL_RTX;
  998. int i;
  999. /* Now we need to set INDEX_REG to an index register (denoted as
  1000. REGZ in the illustration above) and REG_SUM to the expression
  1001. register+register that we want to use to substitute uses of REG
  1002. (typically in MEMs) with. First check REG and BASE for being
  1003. index registers; we can use them even if they are not dead. */
  1004. if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], regno)
  1005. || TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS],
  1006. REGNO (base)))
  1007. {
  1008. index_reg = reg;
  1009. reg_sum = src;
  1010. }
  1011. else
  1012. {
  1013. /* Otherwise, look for a free index register. Since we have
  1014. checked above that neither REG nor BASE are index registers,
  1015. if we find anything at all, it will be different from these
  1016. two registers. */
  1017. for (i = first_index_reg; i <= last_index_reg; i++)
  1018. {
  1019. if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], i)
  1020. && reg_state[i].use_index == RELOAD_COMBINE_MAX_USES
  1021. && reg_state[i].store_ruid <= reg_state[regno].use_ruid
  1022. && (call_used_regs[i] || df_regs_ever_live_p (i))
  1023. && (!frame_pointer_needed || i != HARD_FRAME_POINTER_REGNUM)
  1024. && !fixed_regs[i] && !global_regs[i]
  1025. && hard_regno_nregs[i][GET_MODE (reg)] == 1
  1026. && targetm.hard_regno_scratch_ok (i))
  1027. {
  1028. index_reg = gen_rtx_REG (GET_MODE (reg), i);
  1029. reg_sum = gen_rtx_PLUS (GET_MODE (reg), index_reg, base);
  1030. break;
  1031. }
  1032. }
  1033. }
  1034. /* Check that PREV_SET is indeed (set (REGX) (CONST_INT)) and that
  1035. (REGY), i.e. BASE, is not clobbered before the last use we'll
  1036. create. */
  1037. if (reg_sum
  1038. && prev_set
  1039. && CONST_INT_P (SET_SRC (prev_set))
  1040. && rtx_equal_p (SET_DEST (prev_set), reg)
  1041. && (reg_state[REGNO (base)].store_ruid
  1042. <= reg_state[regno].use_ruid))
  1043. {
  1044. /* Change destination register and, if necessary, the constant
  1045. value in PREV, the constant loading instruction. */
  1046. validate_change (prev, &SET_DEST (prev_set), index_reg, 1);
  1047. if (reg_state[regno].offset != const0_rtx)
  1048. validate_change (prev,
  1049. &SET_SRC (prev_set),
  1050. GEN_INT (INTVAL (SET_SRC (prev_set))
  1051. + INTVAL (reg_state[regno].offset)),
  1052. 1);
  1053. /* Now for every use of REG that we have recorded, replace REG
  1054. with REG_SUM. */
  1055. for (i = reg_state[regno].use_index;
  1056. i < RELOAD_COMBINE_MAX_USES; i++)
  1057. validate_unshare_change (reg_state[regno].reg_use[i].insn,
  1058. reg_state[regno].reg_use[i].usep,
  1059. /* Each change must have its own
  1060. replacement. */
  1061. reg_sum, 1);
  1062. if (apply_change_group ())
  1063. {
  1064. struct reg_use *lowest_ruid = NULL;
  1065. /* For every new use of REG_SUM, we have to record the use
  1066. of BASE therein, i.e. operand 1. */
  1067. for (i = reg_state[regno].use_index;
  1068. i < RELOAD_COMBINE_MAX_USES; i++)
  1069. {
  1070. struct reg_use *use = reg_state[regno].reg_use + i;
  1071. reload_combine_note_use (&XEXP (*use->usep, 1), use->insn,
  1072. use->ruid, use->containing_mem);
  1073. if (lowest_ruid == NULL || use->ruid < lowest_ruid->ruid)
  1074. lowest_ruid = use;
  1075. }
  1076. fixup_debug_insns (reg, reg_sum, insn, lowest_ruid->insn);
  1077. /* Delete the reg-reg addition. */
  1078. delete_insn (insn);
  1079. if (reg_state[regno].offset != const0_rtx)
  1080. /* Previous REG_EQUIV / REG_EQUAL notes for PREV
  1081. are now invalid. */
  1082. remove_reg_equal_equiv_notes (prev);
  1083. reg_state[regno].use_index = RELOAD_COMBINE_MAX_USES;
  1084. return true;
  1085. }
  1086. }
  1087. }
  1088. return false;
  1089. }
  1090. static void
  1091. reload_combine (void)
  1092. {
  1093. rtx_insn *insn, *prev;
  1094. basic_block bb;
  1095. unsigned int r;
  1096. int min_labelno, n_labels;
  1097. HARD_REG_SET ever_live_at_start, *label_live;
  1098. /* To avoid wasting too much time later searching for an index register,
  1099. determine the minimum and maximum index register numbers. */
  1100. if (INDEX_REG_CLASS == NO_REGS)
  1101. last_index_reg = -1;
  1102. else if (first_index_reg == -1 && last_index_reg == 0)
  1103. {
  1104. for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
  1105. if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], r))
  1106. {
  1107. if (first_index_reg == -1)
  1108. first_index_reg = r;
  1109. last_index_reg = r;
  1110. }
  1111. /* If no index register is available, we can quit now. Set LAST_INDEX_REG
  1112. to -1 so we'll know to quit early the next time we get here. */
  1113. if (first_index_reg == -1)
  1114. {
  1115. last_index_reg = -1;
  1116. return;
  1117. }
  1118. }
  1119. /* Set up LABEL_LIVE and EVER_LIVE_AT_START. The register lifetime
  1120. information is a bit fuzzy immediately after reload, but it's
  1121. still good enough to determine which registers are live at a jump
  1122. destination. */
  1123. min_labelno = get_first_label_num ();
  1124. n_labels = max_label_num () - min_labelno;
  1125. label_live = XNEWVEC (HARD_REG_SET, n_labels);
  1126. CLEAR_HARD_REG_SET (ever_live_at_start);
  1127. FOR_EACH_BB_REVERSE_FN (bb, cfun)
  1128. {
  1129. insn = BB_HEAD (bb);
  1130. if (LABEL_P (insn))
  1131. {
  1132. HARD_REG_SET live;
  1133. bitmap live_in = df_get_live_in (bb);
  1134. REG_SET_TO_HARD_REG_SET (live, live_in);
  1135. compute_use_by_pseudos (&live, live_in);
  1136. COPY_HARD_REG_SET (LABEL_LIVE (insn), live);
  1137. IOR_HARD_REG_SET (ever_live_at_start, live);
  1138. }
  1139. }
  1140. /* Initialize last_label_ruid, reload_combine_ruid and reg_state. */
  1141. last_label_ruid = last_jump_ruid = reload_combine_ruid = 0;
  1142. for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
  1143. {
  1144. reg_state[r].store_ruid = 0;
  1145. reg_state[r].real_store_ruid = 0;
  1146. if (fixed_regs[r])
  1147. reg_state[r].use_index = -1;
  1148. else
  1149. reg_state[r].use_index = RELOAD_COMBINE_MAX_USES;
  1150. }
  1151. for (insn = get_last_insn (); insn; insn = prev)
  1152. {
  1153. bool control_flow_insn;
  1154. rtx note;
  1155. prev = PREV_INSN (insn);
  1156. /* We cannot do our optimization across labels. Invalidating all the use
  1157. information we have would be costly, so we just note where the label
  1158. is and then later disable any optimization that would cross it. */
  1159. if (LABEL_P (insn))
  1160. last_label_ruid = reload_combine_ruid;
  1161. else if (BARRIER_P (insn))
  1162. {
  1163. /* Crossing a barrier resets all the use information. */
  1164. for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
  1165. if (! fixed_regs[r])
  1166. reg_state[r].use_index = RELOAD_COMBINE_MAX_USES;
  1167. }
  1168. else if (INSN_P (insn) && volatile_insn_p (PATTERN (insn)))
  1169. /* Optimizations across insns being marked as volatile must be
  1170. prevented. All the usage information is invalidated
  1171. here. */
  1172. for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
  1173. if (! fixed_regs[r]
  1174. && reg_state[r].use_index != RELOAD_COMBINE_MAX_USES)
  1175. reg_state[r].use_index = -1;
  1176. if (! NONDEBUG_INSN_P (insn))
  1177. continue;
  1178. reload_combine_ruid++;
  1179. control_flow_insn = control_flow_insn_p (insn);
  1180. if (control_flow_insn)
  1181. last_jump_ruid = reload_combine_ruid;
  1182. if (reload_combine_recognize_const_pattern (insn)
  1183. || reload_combine_recognize_pattern (insn))
  1184. continue;
  1185. note_stores (PATTERN (insn), reload_combine_note_store, NULL);
  1186. if (CALL_P (insn))
  1187. {
  1188. rtx link;
  1189. HARD_REG_SET used_regs;
  1190. get_call_reg_set_usage (insn, &used_regs, call_used_reg_set);
  1191. for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
  1192. if (TEST_HARD_REG_BIT (used_regs, r))
  1193. {
  1194. reg_state[r].use_index = RELOAD_COMBINE_MAX_USES;
  1195. reg_state[r].store_ruid = reload_combine_ruid;
  1196. }
  1197. for (link = CALL_INSN_FUNCTION_USAGE (insn); link;
  1198. link = XEXP (link, 1))
  1199. {
  1200. rtx setuse = XEXP (link, 0);
  1201. rtx usage_rtx = XEXP (setuse, 0);
  1202. if ((GET_CODE (setuse) == USE || GET_CODE (setuse) == CLOBBER)
  1203. && REG_P (usage_rtx))
  1204. {
  1205. unsigned int i;
  1206. unsigned int start_reg = REGNO (usage_rtx);
  1207. unsigned int num_regs
  1208. = hard_regno_nregs[start_reg][GET_MODE (usage_rtx)];
  1209. unsigned int end_reg = start_reg + num_regs - 1;
  1210. for (i = start_reg; i <= end_reg; i++)
  1211. if (GET_CODE (XEXP (link, 0)) == CLOBBER)
  1212. {
  1213. reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
  1214. reg_state[i].store_ruid = reload_combine_ruid;
  1215. }
  1216. else
  1217. reg_state[i].use_index = -1;
  1218. }
  1219. }
  1220. }
  1221. if (control_flow_insn && !ANY_RETURN_P (PATTERN (insn)))
  1222. {
  1223. /* Non-spill registers might be used at the call destination in
  1224. some unknown fashion, so we have to mark the unknown use. */
  1225. HARD_REG_SET *live;
  1226. if ((condjump_p (insn) || condjump_in_parallel_p (insn))
  1227. && JUMP_LABEL (insn))
  1228. {
  1229. if (ANY_RETURN_P (JUMP_LABEL (insn)))
  1230. live = NULL;
  1231. else
  1232. live = &LABEL_LIVE (JUMP_LABEL (insn));
  1233. }
  1234. else
  1235. live = &ever_live_at_start;
  1236. if (live)
  1237. for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
  1238. if (TEST_HARD_REG_BIT (*live, r))
  1239. reg_state[r].use_index = -1;
  1240. }
  1241. reload_combine_note_use (&PATTERN (insn), insn, reload_combine_ruid,
  1242. NULL_RTX);
  1243. for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
  1244. {
  1245. if (REG_NOTE_KIND (note) == REG_INC && REG_P (XEXP (note, 0)))
  1246. {
  1247. int regno = REGNO (XEXP (note, 0));
  1248. reg_state[regno].store_ruid = reload_combine_ruid;
  1249. reg_state[regno].real_store_ruid = reload_combine_ruid;
  1250. reg_state[regno].use_index = -1;
  1251. }
  1252. }
  1253. }
  1254. free (label_live);
  1255. }
  1256. /* Check if DST is a register or a subreg of a register; if it is,
  1257. update store_ruid, real_store_ruid and use_index in the reg_state
  1258. structure accordingly. Called via note_stores from reload_combine. */
  1259. static void
  1260. reload_combine_note_store (rtx dst, const_rtx set, void *data ATTRIBUTE_UNUSED)
  1261. {
  1262. int regno = 0;
  1263. int i;
  1264. machine_mode mode = GET_MODE (dst);
  1265. if (GET_CODE (dst) == SUBREG)
  1266. {
  1267. regno = subreg_regno_offset (REGNO (SUBREG_REG (dst)),
  1268. GET_MODE (SUBREG_REG (dst)),
  1269. SUBREG_BYTE (dst),
  1270. GET_MODE (dst));
  1271. dst = SUBREG_REG (dst);
  1272. }
  1273. /* Some targets do argument pushes without adding REG_INC notes. */
  1274. if (MEM_P (dst))
  1275. {
  1276. dst = XEXP (dst, 0);
  1277. if (GET_CODE (dst) == PRE_INC || GET_CODE (dst) == POST_INC
  1278. || GET_CODE (dst) == PRE_DEC || GET_CODE (dst) == POST_DEC
  1279. || GET_CODE (dst) == PRE_MODIFY || GET_CODE (dst) == POST_MODIFY)
  1280. {
  1281. regno = REGNO (XEXP (dst, 0));
  1282. mode = GET_MODE (XEXP (dst, 0));
  1283. for (i = hard_regno_nregs[regno][mode] - 1 + regno; i >= regno; i--)
  1284. {
  1285. /* We could probably do better, but for now mark the register
  1286. as used in an unknown fashion and set/clobbered at this
  1287. insn. */
  1288. reg_state[i].use_index = -1;
  1289. reg_state[i].store_ruid = reload_combine_ruid;
  1290. reg_state[i].real_store_ruid = reload_combine_ruid;
  1291. }
  1292. }
  1293. else
  1294. return;
  1295. }
  1296. if (!REG_P (dst))
  1297. return;
  1298. regno += REGNO (dst);
  1299. /* note_stores might have stripped a STRICT_LOW_PART, so we have to be
  1300. careful with registers / register parts that are not full words.
  1301. Similarly for ZERO_EXTRACT. */
  1302. if (GET_CODE (SET_DEST (set)) == ZERO_EXTRACT
  1303. || GET_CODE (SET_DEST (set)) == STRICT_LOW_PART)
  1304. {
  1305. for (i = hard_regno_nregs[regno][mode] - 1 + regno; i >= regno; i--)
  1306. {
  1307. reg_state[i].use_index = -1;
  1308. reg_state[i].store_ruid = reload_combine_ruid;
  1309. reg_state[i].real_store_ruid = reload_combine_ruid;
  1310. }
  1311. }
  1312. else
  1313. {
  1314. for (i = hard_regno_nregs[regno][mode] - 1 + regno; i >= regno; i--)
  1315. {
  1316. reg_state[i].store_ruid = reload_combine_ruid;
  1317. if (GET_CODE (set) == SET)
  1318. reg_state[i].real_store_ruid = reload_combine_ruid;
  1319. reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
  1320. }
  1321. }
  1322. }
  1323. /* XP points to a piece of rtl that has to be checked for any uses of
  1324. registers.
  1325. *XP is the pattern of INSN, or a part of it.
  1326. Called from reload_combine, and recursively by itself. */
  1327. static void
  1328. reload_combine_note_use (rtx *xp, rtx_insn *insn, int ruid, rtx containing_mem)
  1329. {
  1330. rtx x = *xp;
  1331. enum rtx_code code = x->code;
  1332. const char *fmt;
  1333. int i, j;
  1334. rtx offset = const0_rtx; /* For the REG case below. */
  1335. switch (code)
  1336. {
  1337. case SET:
  1338. if (REG_P (SET_DEST (x)))
  1339. {
  1340. reload_combine_note_use (&SET_SRC (x), insn, ruid, NULL_RTX);
  1341. return;
  1342. }
  1343. break;
  1344. case USE:
  1345. /* If this is the USE of a return value, we can't change it. */
  1346. if (REG_P (XEXP (x, 0)) && REG_FUNCTION_VALUE_P (XEXP (x, 0)))
  1347. {
  1348. /* Mark the return register as used in an unknown fashion. */
  1349. rtx reg = XEXP (x, 0);
  1350. int regno = REGNO (reg);
  1351. int nregs = hard_regno_nregs[regno][GET_MODE (reg)];
  1352. while (--nregs >= 0)
  1353. reg_state[regno + nregs].use_index = -1;
  1354. return;
  1355. }
  1356. break;
  1357. case CLOBBER:
  1358. if (REG_P (SET_DEST (x)))
  1359. {
  1360. /* No spurious CLOBBERs of pseudo registers may remain. */
  1361. gcc_assert (REGNO (SET_DEST (x)) < FIRST_PSEUDO_REGISTER);
  1362. return;
  1363. }
  1364. break;
  1365. case PLUS:
  1366. /* We are interested in (plus (reg) (const_int)) . */
  1367. if (!REG_P (XEXP (x, 0))
  1368. || !CONST_INT_P (XEXP (x, 1)))
  1369. break;
  1370. offset = XEXP (x, 1);
  1371. x = XEXP (x, 0);
  1372. /* Fall through. */
  1373. case REG:
  1374. {
  1375. int regno = REGNO (x);
  1376. int use_index;
  1377. int nregs;
  1378. /* No spurious USEs of pseudo registers may remain. */
  1379. gcc_assert (regno < FIRST_PSEUDO_REGISTER);
  1380. nregs = hard_regno_nregs[regno][GET_MODE (x)];
  1381. /* We can't substitute into multi-hard-reg uses. */
  1382. if (nregs > 1)
  1383. {
  1384. while (--nregs >= 0)
  1385. reg_state[regno + nregs].use_index = -1;
  1386. return;
  1387. }
  1388. /* We may be called to update uses in previously seen insns.
  1389. Don't add uses beyond the last store we saw. */
  1390. if (ruid < reg_state[regno].store_ruid)
  1391. return;
  1392. /* If this register is already used in some unknown fashion, we
  1393. can't do anything.
  1394. If we decrement the index from zero to -1, we can't store more
  1395. uses, so this register becomes used in an unknown fashion. */
  1396. use_index = --reg_state[regno].use_index;
  1397. if (use_index < 0)
  1398. return;
  1399. if (use_index == RELOAD_COMBINE_MAX_USES - 1)
  1400. {
  1401. /* This is the first use of this register we have seen since we
  1402. marked it as dead. */
  1403. reg_state[regno].offset = offset;
  1404. reg_state[regno].all_offsets_match = true;
  1405. reg_state[regno].use_ruid = ruid;
  1406. }
  1407. else
  1408. {
  1409. if (reg_state[regno].use_ruid > ruid)
  1410. reg_state[regno].use_ruid = ruid;
  1411. if (! rtx_equal_p (offset, reg_state[regno].offset))
  1412. reg_state[regno].all_offsets_match = false;
  1413. }
  1414. reg_state[regno].reg_use[use_index].insn = insn;
  1415. reg_state[regno].reg_use[use_index].ruid = ruid;
  1416. reg_state[regno].reg_use[use_index].containing_mem = containing_mem;
  1417. reg_state[regno].reg_use[use_index].usep = xp;
  1418. return;
  1419. }
  1420. case MEM:
  1421. containing_mem = x;
  1422. break;
  1423. default:
  1424. break;
  1425. }
  1426. /* Recursively process the components of X. */
  1427. fmt = GET_RTX_FORMAT (code);
  1428. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1429. {
  1430. if (fmt[i] == 'e')
  1431. reload_combine_note_use (&XEXP (x, i), insn, ruid, containing_mem);
  1432. else if (fmt[i] == 'E')
  1433. {
  1434. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  1435. reload_combine_note_use (&XVECEXP (x, i, j), insn, ruid,
  1436. containing_mem);
  1437. }
  1438. }
  1439. }
  1440. /* See if we can reduce the cost of a constant by replacing a move
  1441. with an add. We track situations in which a register is set to a
  1442. constant or to a register plus a constant. */
  1443. /* We cannot do our optimization across labels. Invalidating all the
  1444. information about register contents we have would be costly, so we
  1445. use move2add_last_label_luid to note where the label is and then
  1446. later disable any optimization that would cross it.
  1447. reg_offset[n] / reg_base_reg[n] / reg_symbol_ref[n] / reg_mode[n]
  1448. are only valid if reg_set_luid[n] is greater than
  1449. move2add_last_label_luid.
  1450. For a set that established a new (potential) base register with
  1451. non-constant value, we use move2add_luid from the place where the
  1452. setting insn is encountered; registers based off that base then
  1453. get the same reg_set_luid. Constants all get
  1454. move2add_last_label_luid + 1 as their reg_set_luid. */
  1455. static int reg_set_luid[FIRST_PSEUDO_REGISTER];
  1456. /* If reg_base_reg[n] is negative, register n has been set to
  1457. reg_offset[n] or reg_symbol_ref[n] + reg_offset[n] in mode reg_mode[n].
  1458. If reg_base_reg[n] is non-negative, register n has been set to the
  1459. sum of reg_offset[n] and the value of register reg_base_reg[n]
  1460. before reg_set_luid[n], calculated in mode reg_mode[n] .
  1461. For multi-hard-register registers, all but the first one are
  1462. recorded as BLKmode in reg_mode. Setting reg_mode to VOIDmode
  1463. marks it as invalid. */
  1464. static HOST_WIDE_INT reg_offset[FIRST_PSEUDO_REGISTER];
  1465. static int reg_base_reg[FIRST_PSEUDO_REGISTER];
  1466. static rtx reg_symbol_ref[FIRST_PSEUDO_REGISTER];
  1467. static machine_mode reg_mode[FIRST_PSEUDO_REGISTER];
  1468. /* move2add_luid is linearly increased while scanning the instructions
  1469. from first to last. It is used to set reg_set_luid in
  1470. reload_cse_move2add and move2add_note_store. */
  1471. static int move2add_luid;
  1472. /* move2add_last_label_luid is set whenever a label is found. Labels
  1473. invalidate all previously collected reg_offset data. */
  1474. static int move2add_last_label_luid;
  1475. /* ??? We don't know how zero / sign extension is handled, hence we
  1476. can't go from a narrower to a wider mode. */
  1477. #define MODES_OK_FOR_MOVE2ADD(OUTMODE, INMODE) \
  1478. (GET_MODE_SIZE (OUTMODE) == GET_MODE_SIZE (INMODE) \
  1479. || (GET_MODE_SIZE (OUTMODE) <= GET_MODE_SIZE (INMODE) \
  1480. && TRULY_NOOP_TRUNCATION_MODES_P (OUTMODE, INMODE)))
  1481. /* Record that REG is being set to a value with the mode of REG. */
  1482. static void
  1483. move2add_record_mode (rtx reg)
  1484. {
  1485. int regno, nregs;
  1486. machine_mode mode = GET_MODE (reg);
  1487. if (GET_CODE (reg) == SUBREG)
  1488. {
  1489. regno = subreg_regno (reg);
  1490. nregs = subreg_nregs (reg);
  1491. }
  1492. else if (REG_P (reg))
  1493. {
  1494. regno = REGNO (reg);
  1495. nregs = hard_regno_nregs[regno][mode];
  1496. }
  1497. else
  1498. gcc_unreachable ();
  1499. for (int i = nregs - 1; i > 0; i--)
  1500. reg_mode[regno + i] = BLKmode;
  1501. reg_mode[regno] = mode;
  1502. }
  1503. /* Record that REG is being set to the sum of SYM and OFF. */
  1504. static void
  1505. move2add_record_sym_value (rtx reg, rtx sym, rtx off)
  1506. {
  1507. int regno = REGNO (reg);
  1508. move2add_record_mode (reg);
  1509. reg_set_luid[regno] = move2add_luid;
  1510. reg_base_reg[regno] = -1;
  1511. reg_symbol_ref[regno] = sym;
  1512. reg_offset[regno] = INTVAL (off);
  1513. }
  1514. /* Check if REGNO contains a valid value in MODE. */
  1515. static bool
  1516. move2add_valid_value_p (int regno, machine_mode mode)
  1517. {
  1518. if (reg_set_luid[regno] <= move2add_last_label_luid)
  1519. return false;
  1520. if (mode != reg_mode[regno])
  1521. {
  1522. if (!MODES_OK_FOR_MOVE2ADD (mode, reg_mode[regno]))
  1523. return false;
  1524. /* The value loaded into regno in reg_mode[regno] is also valid in
  1525. mode after truncation only if (REG:mode regno) is the lowpart of
  1526. (REG:reg_mode[regno] regno). Now, for big endian, the starting
  1527. regno of the lowpart might be different. */
  1528. int s_off = subreg_lowpart_offset (mode, reg_mode[regno]);
  1529. s_off = subreg_regno_offset (regno, reg_mode[regno], s_off, mode);
  1530. if (s_off != 0)
  1531. /* We could in principle adjust regno, check reg_mode[regno] to be
  1532. BLKmode, and return s_off to the caller (vs. -1 for failure),
  1533. but we currently have no callers that could make use of this
  1534. information. */
  1535. return false;
  1536. }
  1537. for (int i = hard_regno_nregs[regno][mode] - 1; i > 0; i--)
  1538. if (reg_mode[regno + i] != BLKmode)
  1539. return false;
  1540. return true;
  1541. }
  1542. /* This function is called with INSN that sets REG to (SYM + OFF),
  1543. while REG is known to already have value (SYM + offset).
  1544. This function tries to change INSN into an add instruction
  1545. (set (REG) (plus (REG) (OFF - offset))) using the known value.
  1546. It also updates the information about REG's known value.
  1547. Return true if we made a change. */
  1548. static bool
  1549. move2add_use_add2_insn (rtx reg, rtx sym, rtx off, rtx_insn *insn)
  1550. {
  1551. rtx pat = PATTERN (insn);
  1552. rtx src = SET_SRC (pat);
  1553. int regno = REGNO (reg);
  1554. rtx new_src = gen_int_mode (UINTVAL (off) - reg_offset[regno],
  1555. GET_MODE (reg));
  1556. bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
  1557. bool changed = false;
  1558. /* (set (reg) (plus (reg) (const_int 0))) is not canonical;
  1559. use (set (reg) (reg)) instead.
  1560. We don't delete this insn, nor do we convert it into a
  1561. note, to avoid losing register notes or the return
  1562. value flag. jump2 already knows how to get rid of
  1563. no-op moves. */
  1564. if (new_src == const0_rtx)
  1565. {
  1566. /* If the constants are different, this is a
  1567. truncation, that, if turned into (set (reg)
  1568. (reg)), would be discarded. Maybe we should
  1569. try a truncMN pattern? */
  1570. if (INTVAL (off) == reg_offset [regno])
  1571. changed = validate_change (insn, &SET_SRC (pat), reg, 0);
  1572. }
  1573. else
  1574. {
  1575. struct full_rtx_costs oldcst, newcst;
  1576. rtx tem = gen_rtx_PLUS (GET_MODE (reg), reg, new_src);
  1577. get_full_set_rtx_cost (pat, &oldcst);
  1578. SET_SRC (pat) = tem;
  1579. get_full_set_rtx_cost (pat, &newcst);
  1580. SET_SRC (pat) = src;
  1581. if (costs_lt_p (&newcst, &oldcst, speed)
  1582. && have_add2_insn (reg, new_src))
  1583. changed = validate_change (insn, &SET_SRC (pat), tem, 0);
  1584. else if (sym == NULL_RTX && GET_MODE (reg) != BImode)
  1585. {
  1586. machine_mode narrow_mode;
  1587. for (narrow_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
  1588. narrow_mode != VOIDmode
  1589. && narrow_mode != GET_MODE (reg);
  1590. narrow_mode = GET_MODE_WIDER_MODE (narrow_mode))
  1591. {
  1592. if (have_insn_for (STRICT_LOW_PART, narrow_mode)
  1593. && ((reg_offset[regno] & ~GET_MODE_MASK (narrow_mode))
  1594. == (INTVAL (off) & ~GET_MODE_MASK (narrow_mode))))
  1595. {
  1596. rtx narrow_reg = gen_lowpart_common (narrow_mode, reg);
  1597. rtx narrow_src = gen_int_mode (INTVAL (off),
  1598. narrow_mode);
  1599. rtx new_set
  1600. = gen_rtx_SET (VOIDmode,
  1601. gen_rtx_STRICT_LOW_PART (VOIDmode,
  1602. narrow_reg),
  1603. narrow_src);
  1604. get_full_set_rtx_cost (new_set, &newcst);
  1605. if (costs_lt_p (&newcst, &oldcst, speed))
  1606. {
  1607. changed = validate_change (insn, &PATTERN (insn),
  1608. new_set, 0);
  1609. if (changed)
  1610. break;
  1611. }
  1612. }
  1613. }
  1614. }
  1615. }
  1616. move2add_record_sym_value (reg, sym, off);
  1617. return changed;
  1618. }
  1619. /* This function is called with INSN that sets REG to (SYM + OFF),
  1620. but REG doesn't have known value (SYM + offset). This function
  1621. tries to find another register which is known to already have
  1622. value (SYM + offset) and change INSN into an add instruction
  1623. (set (REG) (plus (the found register) (OFF - offset))) if such
  1624. a register is found. It also updates the information about
  1625. REG's known value.
  1626. Return true iff we made a change. */
  1627. static bool
  1628. move2add_use_add3_insn (rtx reg, rtx sym, rtx off, rtx_insn *insn)
  1629. {
  1630. rtx pat = PATTERN (insn);
  1631. rtx src = SET_SRC (pat);
  1632. int regno = REGNO (reg);
  1633. int min_regno = 0;
  1634. bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
  1635. int i;
  1636. bool changed = false;
  1637. struct full_rtx_costs oldcst, newcst, mincst;
  1638. rtx plus_expr;
  1639. init_costs_to_max (&mincst);
  1640. get_full_set_rtx_cost (pat, &oldcst);
  1641. plus_expr = gen_rtx_PLUS (GET_MODE (reg), reg, const0_rtx);
  1642. SET_SRC (pat) = plus_expr;
  1643. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  1644. if (move2add_valid_value_p (i, GET_MODE (reg))
  1645. && reg_base_reg[i] < 0
  1646. && reg_symbol_ref[i] != NULL_RTX
  1647. && rtx_equal_p (sym, reg_symbol_ref[i]))
  1648. {
  1649. rtx new_src = gen_int_mode (UINTVAL (off) - reg_offset[i],
  1650. GET_MODE (reg));
  1651. /* (set (reg) (plus (reg) (const_int 0))) is not canonical;
  1652. use (set (reg) (reg)) instead.
  1653. We don't delete this insn, nor do we convert it into a
  1654. note, to avoid losing register notes or the return
  1655. value flag. jump2 already knows how to get rid of
  1656. no-op moves. */
  1657. if (new_src == const0_rtx)
  1658. {
  1659. init_costs_to_zero (&mincst);
  1660. min_regno = i;
  1661. break;
  1662. }
  1663. else
  1664. {
  1665. XEXP (plus_expr, 1) = new_src;
  1666. get_full_set_rtx_cost (pat, &newcst);
  1667. if (costs_lt_p (&newcst, &mincst, speed))
  1668. {
  1669. mincst = newcst;
  1670. min_regno = i;
  1671. }
  1672. }
  1673. }
  1674. SET_SRC (pat) = src;
  1675. if (costs_lt_p (&mincst, &oldcst, speed))
  1676. {
  1677. rtx tem;
  1678. tem = gen_rtx_REG (GET_MODE (reg), min_regno);
  1679. if (i != min_regno)
  1680. {
  1681. rtx new_src = gen_int_mode (UINTVAL (off) - reg_offset[min_regno],
  1682. GET_MODE (reg));
  1683. tem = gen_rtx_PLUS (GET_MODE (reg), tem, new_src);
  1684. }
  1685. if (validate_change (insn, &SET_SRC (pat), tem, 0))
  1686. changed = true;
  1687. }
  1688. reg_set_luid[regno] = move2add_luid;
  1689. move2add_record_sym_value (reg, sym, off);
  1690. return changed;
  1691. }
  1692. /* Convert move insns with constant inputs to additions if they are cheaper.
  1693. Return true if any changes were made. */
  1694. static bool
  1695. reload_cse_move2add (rtx_insn *first)
  1696. {
  1697. int i;
  1698. rtx_insn *insn;
  1699. bool changed = false;
  1700. for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
  1701. {
  1702. reg_set_luid[i] = 0;
  1703. reg_offset[i] = 0;
  1704. reg_base_reg[i] = 0;
  1705. reg_symbol_ref[i] = NULL_RTX;
  1706. reg_mode[i] = VOIDmode;
  1707. }
  1708. move2add_last_label_luid = 0;
  1709. move2add_luid = 2;
  1710. for (insn = first; insn; insn = NEXT_INSN (insn), move2add_luid++)
  1711. {
  1712. rtx pat, note;
  1713. if (LABEL_P (insn))
  1714. {
  1715. move2add_last_label_luid = move2add_luid;
  1716. /* We're going to increment move2add_luid twice after a
  1717. label, so that we can use move2add_last_label_luid + 1 as
  1718. the luid for constants. */
  1719. move2add_luid++;
  1720. continue;
  1721. }
  1722. if (! INSN_P (insn))
  1723. continue;
  1724. pat = PATTERN (insn);
  1725. /* For simplicity, we only perform this optimization on
  1726. straightforward SETs. */
  1727. if (GET_CODE (pat) == SET
  1728. && REG_P (SET_DEST (pat)))
  1729. {
  1730. rtx reg = SET_DEST (pat);
  1731. int regno = REGNO (reg);
  1732. rtx src = SET_SRC (pat);
  1733. /* Check if we have valid information on the contents of this
  1734. register in the mode of REG. */
  1735. if (move2add_valid_value_p (regno, GET_MODE (reg))
  1736. && dbg_cnt (cse2_move2add))
  1737. {
  1738. /* Try to transform (set (REGX) (CONST_INT A))
  1739. ...
  1740. (set (REGX) (CONST_INT B))
  1741. to
  1742. (set (REGX) (CONST_INT A))
  1743. ...
  1744. (set (REGX) (plus (REGX) (CONST_INT B-A)))
  1745. or
  1746. (set (REGX) (CONST_INT A))
  1747. ...
  1748. (set (STRICT_LOW_PART (REGX)) (CONST_INT B))
  1749. */
  1750. if (CONST_INT_P (src)
  1751. && reg_base_reg[regno] < 0
  1752. && reg_symbol_ref[regno] == NULL_RTX)
  1753. {
  1754. changed |= move2add_use_add2_insn (reg, NULL_RTX, src, insn);
  1755. continue;
  1756. }
  1757. /* Try to transform (set (REGX) (REGY))
  1758. (set (REGX) (PLUS (REGX) (CONST_INT A)))
  1759. ...
  1760. (set (REGX) (REGY))
  1761. (set (REGX) (PLUS (REGX) (CONST_INT B)))
  1762. to
  1763. (set (REGX) (REGY))
  1764. (set (REGX) (PLUS (REGX) (CONST_INT A)))
  1765. ...
  1766. (set (REGX) (plus (REGX) (CONST_INT B-A))) */
  1767. else if (REG_P (src)
  1768. && reg_set_luid[regno] == reg_set_luid[REGNO (src)]
  1769. && reg_base_reg[regno] == reg_base_reg[REGNO (src)]
  1770. && move2add_valid_value_p (REGNO (src), GET_MODE (reg)))
  1771. {
  1772. rtx_insn *next = next_nonnote_nondebug_insn (insn);
  1773. rtx set = NULL_RTX;
  1774. if (next)
  1775. set = single_set (next);
  1776. if (set
  1777. && SET_DEST (set) == reg
  1778. && GET_CODE (SET_SRC (set)) == PLUS
  1779. && XEXP (SET_SRC (set), 0) == reg
  1780. && CONST_INT_P (XEXP (SET_SRC (set), 1)))
  1781. {
  1782. rtx src3 = XEXP (SET_SRC (set), 1);
  1783. unsigned HOST_WIDE_INT added_offset = UINTVAL (src3);
  1784. HOST_WIDE_INT base_offset = reg_offset[REGNO (src)];
  1785. HOST_WIDE_INT regno_offset = reg_offset[regno];
  1786. rtx new_src =
  1787. gen_int_mode (added_offset
  1788. + base_offset
  1789. - regno_offset,
  1790. GET_MODE (reg));
  1791. bool success = false;
  1792. bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
  1793. if (new_src == const0_rtx)
  1794. /* See above why we create (set (reg) (reg)) here. */
  1795. success
  1796. = validate_change (next, &SET_SRC (set), reg, 0);
  1797. else
  1798. {
  1799. rtx old_src = SET_SRC (set);
  1800. struct full_rtx_costs oldcst, newcst;
  1801. rtx tem = gen_rtx_PLUS (GET_MODE (reg), reg, new_src);
  1802. get_full_set_rtx_cost (set, &oldcst);
  1803. SET_SRC (set) = tem;
  1804. get_full_set_src_cost (tem, &newcst);
  1805. SET_SRC (set) = old_src;
  1806. costs_add_n_insns (&oldcst, 1);
  1807. if (costs_lt_p (&newcst, &oldcst, speed)
  1808. && have_add2_insn (reg, new_src))
  1809. {
  1810. rtx newpat = gen_rtx_SET (VOIDmode, reg, tem);
  1811. success
  1812. = validate_change (next, &PATTERN (next),
  1813. newpat, 0);
  1814. }
  1815. }
  1816. if (success)
  1817. delete_insn (insn);
  1818. changed |= success;
  1819. insn = next;
  1820. move2add_record_mode (reg);
  1821. reg_offset[regno]
  1822. = trunc_int_for_mode (added_offset + base_offset,
  1823. GET_MODE (reg));
  1824. continue;
  1825. }
  1826. }
  1827. }
  1828. /* Try to transform
  1829. (set (REGX) (CONST (PLUS (SYMBOL_REF) (CONST_INT A))))
  1830. ...
  1831. (set (REGY) (CONST (PLUS (SYMBOL_REF) (CONST_INT B))))
  1832. to
  1833. (set (REGX) (CONST (PLUS (SYMBOL_REF) (CONST_INT A))))
  1834. ...
  1835. (set (REGY) (CONST (PLUS (REGX) (CONST_INT B-A)))) */
  1836. if ((GET_CODE (src) == SYMBOL_REF
  1837. || (GET_CODE (src) == CONST
  1838. && GET_CODE (XEXP (src, 0)) == PLUS
  1839. && GET_CODE (XEXP (XEXP (src, 0), 0)) == SYMBOL_REF
  1840. && CONST_INT_P (XEXP (XEXP (src, 0), 1))))
  1841. && dbg_cnt (cse2_move2add))
  1842. {
  1843. rtx sym, off;
  1844. if (GET_CODE (src) == SYMBOL_REF)
  1845. {
  1846. sym = src;
  1847. off = const0_rtx;
  1848. }
  1849. else
  1850. {
  1851. sym = XEXP (XEXP (src, 0), 0);
  1852. off = XEXP (XEXP (src, 0), 1);
  1853. }
  1854. /* If the reg already contains the value which is sum of
  1855. sym and some constant value, we can use an add2 insn. */
  1856. if (move2add_valid_value_p (regno, GET_MODE (reg))
  1857. && reg_base_reg[regno] < 0
  1858. && reg_symbol_ref[regno] != NULL_RTX
  1859. && rtx_equal_p (sym, reg_symbol_ref[regno]))
  1860. changed |= move2add_use_add2_insn (reg, sym, off, insn);
  1861. /* Otherwise, we have to find a register whose value is sum
  1862. of sym and some constant value. */
  1863. else
  1864. changed |= move2add_use_add3_insn (reg, sym, off, insn);
  1865. continue;
  1866. }
  1867. }
  1868. for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
  1869. {
  1870. if (REG_NOTE_KIND (note) == REG_INC
  1871. && REG_P (XEXP (note, 0)))
  1872. {
  1873. /* Reset the information about this register. */
  1874. int regno = REGNO (XEXP (note, 0));
  1875. if (regno < FIRST_PSEUDO_REGISTER)
  1876. {
  1877. move2add_record_mode (XEXP (note, 0));
  1878. reg_mode[regno] = VOIDmode;
  1879. }
  1880. }
  1881. }
  1882. note_stores (PATTERN (insn), move2add_note_store, insn);
  1883. /* If INSN is a conditional branch, we try to extract an
  1884. implicit set out of it. */
  1885. if (any_condjump_p (insn))
  1886. {
  1887. rtx cnd = fis_get_condition (insn);
  1888. if (cnd != NULL_RTX
  1889. && GET_CODE (cnd) == NE
  1890. && REG_P (XEXP (cnd, 0))
  1891. && !reg_set_p (XEXP (cnd, 0), insn)
  1892. /* The following two checks, which are also in
  1893. move2add_note_store, are intended to reduce the
  1894. number of calls to gen_rtx_SET to avoid memory
  1895. allocation if possible. */
  1896. && SCALAR_INT_MODE_P (GET_MODE (XEXP (cnd, 0)))
  1897. && hard_regno_nregs[REGNO (XEXP (cnd, 0))][GET_MODE (XEXP (cnd, 0))] == 1
  1898. && CONST_INT_P (XEXP (cnd, 1)))
  1899. {
  1900. rtx implicit_set =
  1901. gen_rtx_SET (VOIDmode, XEXP (cnd, 0), XEXP (cnd, 1));
  1902. move2add_note_store (SET_DEST (implicit_set), implicit_set, insn);
  1903. }
  1904. }
  1905. /* If this is a CALL_INSN, all call used registers are stored with
  1906. unknown values. */
  1907. if (CALL_P (insn))
  1908. {
  1909. for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
  1910. {
  1911. if (call_used_regs[i])
  1912. /* Reset the information about this register. */
  1913. reg_mode[i] = VOIDmode;
  1914. }
  1915. }
  1916. }
  1917. return changed;
  1918. }
  1919. /* SET is a SET or CLOBBER that sets DST. DATA is the insn which
  1920. contains SET.
  1921. Update reg_set_luid, reg_offset and reg_base_reg accordingly.
  1922. Called from reload_cse_move2add via note_stores. */
  1923. static void
  1924. move2add_note_store (rtx dst, const_rtx set, void *data)
  1925. {
  1926. rtx_insn *insn = (rtx_insn *) data;
  1927. unsigned int regno = 0;
  1928. machine_mode mode = GET_MODE (dst);
  1929. /* Some targets do argument pushes without adding REG_INC notes. */
  1930. if (MEM_P (dst))
  1931. {
  1932. dst = XEXP (dst, 0);
  1933. if (GET_CODE (dst) == PRE_INC || GET_CODE (dst) == POST_INC
  1934. || GET_CODE (dst) == PRE_DEC || GET_CODE (dst) == POST_DEC)
  1935. reg_mode[REGNO (XEXP (dst, 0))] = VOIDmode;
  1936. return;
  1937. }
  1938. if (GET_CODE (dst) == SUBREG)
  1939. regno = subreg_regno (dst);
  1940. else if (REG_P (dst))
  1941. regno = REGNO (dst);
  1942. else
  1943. return;
  1944. if (SCALAR_INT_MODE_P (mode)
  1945. && GET_CODE (set) == SET)
  1946. {
  1947. rtx note, sym = NULL_RTX;
  1948. rtx off;
  1949. note = find_reg_equal_equiv_note (insn);
  1950. if (note && GET_CODE (XEXP (note, 0)) == SYMBOL_REF)
  1951. {
  1952. sym = XEXP (note, 0);
  1953. off = const0_rtx;
  1954. }
  1955. else if (note && GET_CODE (XEXP (note, 0)) == CONST
  1956. && GET_CODE (XEXP (XEXP (note, 0), 0)) == PLUS
  1957. && GET_CODE (XEXP (XEXP (XEXP (note, 0), 0), 0)) == SYMBOL_REF
  1958. && CONST_INT_P (XEXP (XEXP (XEXP (note, 0), 0), 1)))
  1959. {
  1960. sym = XEXP (XEXP (XEXP (note, 0), 0), 0);
  1961. off = XEXP (XEXP (XEXP (note, 0), 0), 1);
  1962. }
  1963. if (sym != NULL_RTX)
  1964. {
  1965. move2add_record_sym_value (dst, sym, off);
  1966. return;
  1967. }
  1968. }
  1969. if (SCALAR_INT_MODE_P (mode)
  1970. && GET_CODE (set) == SET
  1971. && GET_CODE (SET_DEST (set)) != ZERO_EXTRACT
  1972. && GET_CODE (SET_DEST (set)) != STRICT_LOW_PART)
  1973. {
  1974. rtx src = SET_SRC (set);
  1975. rtx base_reg;
  1976. unsigned HOST_WIDE_INT offset;
  1977. int base_regno;
  1978. switch (GET_CODE (src))
  1979. {
  1980. case PLUS:
  1981. if (REG_P (XEXP (src, 0)))
  1982. {
  1983. base_reg = XEXP (src, 0);
  1984. if (CONST_INT_P (XEXP (src, 1)))
  1985. offset = UINTVAL (XEXP (src, 1));
  1986. else if (REG_P (XEXP (src, 1))
  1987. && move2add_valid_value_p (REGNO (XEXP (src, 1)), mode))
  1988. {
  1989. if (reg_base_reg[REGNO (XEXP (src, 1))] < 0
  1990. && reg_symbol_ref[REGNO (XEXP (src, 1))] == NULL_RTX)
  1991. offset = reg_offset[REGNO (XEXP (src, 1))];
  1992. /* Maybe the first register is known to be a
  1993. constant. */
  1994. else if (move2add_valid_value_p (REGNO (base_reg), mode)
  1995. && reg_base_reg[REGNO (base_reg)] < 0
  1996. && reg_symbol_ref[REGNO (base_reg)] == NULL_RTX)
  1997. {
  1998. offset = reg_offset[REGNO (base_reg)];
  1999. base_reg = XEXP (src, 1);
  2000. }
  2001. else
  2002. goto invalidate;
  2003. }
  2004. else
  2005. goto invalidate;
  2006. break;
  2007. }
  2008. goto invalidate;
  2009. case REG:
  2010. base_reg = src;
  2011. offset = 0;
  2012. break;
  2013. case CONST_INT:
  2014. /* Start tracking the register as a constant. */
  2015. reg_base_reg[regno] = -1;
  2016. reg_symbol_ref[regno] = NULL_RTX;
  2017. reg_offset[regno] = INTVAL (SET_SRC (set));
  2018. /* We assign the same luid to all registers set to constants. */
  2019. reg_set_luid[regno] = move2add_last_label_luid + 1;
  2020. move2add_record_mode (dst);
  2021. return;
  2022. default:
  2023. goto invalidate;
  2024. }
  2025. base_regno = REGNO (base_reg);
  2026. /* If information about the base register is not valid, set it
  2027. up as a new base register, pretending its value is known
  2028. starting from the current insn. */
  2029. if (!move2add_valid_value_p (base_regno, mode))
  2030. {
  2031. reg_base_reg[base_regno] = base_regno;
  2032. reg_symbol_ref[base_regno] = NULL_RTX;
  2033. reg_offset[base_regno] = 0;
  2034. reg_set_luid[base_regno] = move2add_luid;
  2035. gcc_assert (GET_MODE (base_reg) == mode);
  2036. move2add_record_mode (base_reg);
  2037. }
  2038. /* Copy base information from our base register. */
  2039. reg_set_luid[regno] = reg_set_luid[base_regno];
  2040. reg_base_reg[regno] = reg_base_reg[base_regno];
  2041. reg_symbol_ref[regno] = reg_symbol_ref[base_regno];
  2042. /* Compute the sum of the offsets or constants. */
  2043. reg_offset[regno]
  2044. = trunc_int_for_mode (offset + reg_offset[base_regno], mode);
  2045. move2add_record_mode (dst);
  2046. }
  2047. else
  2048. {
  2049. invalidate:
  2050. /* Invalidate the contents of the register. */
  2051. move2add_record_mode (dst);
  2052. reg_mode[regno] = VOIDmode;
  2053. }
  2054. }
  2055. namespace {
  2056. const pass_data pass_data_postreload_cse =
  2057. {
  2058. RTL_PASS, /* type */
  2059. "postreload", /* name */
  2060. OPTGROUP_NONE, /* optinfo_flags */
  2061. TV_RELOAD_CSE_REGS, /* tv_id */
  2062. 0, /* properties_required */
  2063. 0, /* properties_provided */
  2064. 0, /* properties_destroyed */
  2065. 0, /* todo_flags_start */
  2066. TODO_df_finish, /* todo_flags_finish */
  2067. };
  2068. class pass_postreload_cse : public rtl_opt_pass
  2069. {
  2070. public:
  2071. pass_postreload_cse (gcc::context *ctxt)
  2072. : rtl_opt_pass (pass_data_postreload_cse, ctxt)
  2073. {}
  2074. /* opt_pass methods: */
  2075. virtual bool gate (function *) { return (optimize > 0 && reload_completed); }
  2076. virtual unsigned int execute (function *);
  2077. }; // class pass_postreload_cse
  2078. unsigned int
  2079. pass_postreload_cse::execute (function *fun)
  2080. {
  2081. if (!dbg_cnt (postreload_cse))
  2082. return 0;
  2083. /* Do a very simple CSE pass over just the hard registers. */
  2084. reload_cse_regs (get_insns ());
  2085. /* Reload_cse_regs can eliminate potentially-trapping MEMs.
  2086. Remove any EH edges associated with them. */
  2087. if (fun->can_throw_non_call_exceptions
  2088. && purge_all_dead_edges ())
  2089. cleanup_cfg (0);
  2090. return 0;
  2091. }
  2092. } // anon namespace
  2093. rtl_opt_pass *
  2094. make_pass_postreload_cse (gcc::context *ctxt)
  2095. {
  2096. return new pass_postreload_cse (ctxt);
  2097. }