cfgcleanup.c 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191
  1. /* Control flow optimization code for GNU compiler.
  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. /* This file contains optimizer of the control flow. The main entry point is
  16. cleanup_cfg. Following optimizations are performed:
  17. - Unreachable blocks removal
  18. - Edge forwarding (edge to the forwarder block is forwarded to its
  19. successor. Simplification of the branch instruction is performed by
  20. underlying infrastructure so branch can be converted to simplejump or
  21. eliminated).
  22. - Cross jumping (tail merging)
  23. - Conditional jump-around-simplejump simplification
  24. - Basic block merging. */
  25. #include "config.h"
  26. #include "system.h"
  27. #include "coretypes.h"
  28. #include "tm.h"
  29. #include "rtl.h"
  30. #include "hash-set.h"
  31. #include "machmode.h"
  32. #include "vec.h"
  33. #include "double-int.h"
  34. #include "input.h"
  35. #include "alias.h"
  36. #include "symtab.h"
  37. #include "wide-int.h"
  38. #include "inchash.h"
  39. #include "tree.h"
  40. #include "hard-reg-set.h"
  41. #include "regs.h"
  42. #include "insn-config.h"
  43. #include "flags.h"
  44. #include "recog.h"
  45. #include "diagnostic-core.h"
  46. #include "cselib.h"
  47. #include "params.h"
  48. #include "tm_p.h"
  49. #include "target.h"
  50. #include "hashtab.h"
  51. #include "function.h" /* For inline functions in emit-rtl.h they need crtl. */
  52. #include "emit-rtl.h"
  53. #include "tree-pass.h"
  54. #include "cfgloop.h"
  55. #include "function.h"
  56. #include "statistics.h"
  57. #include "real.h"
  58. #include "fixed-value.h"
  59. #include "expmed.h"
  60. #include "dojump.h"
  61. #include "explow.h"
  62. #include "calls.h"
  63. #include "varasm.h"
  64. #include "stmt.h"
  65. #include "expr.h"
  66. #include "dominance.h"
  67. #include "cfg.h"
  68. #include "cfgrtl.h"
  69. #include "cfganal.h"
  70. #include "cfgbuild.h"
  71. #include "cfgcleanup.h"
  72. #include "predict.h"
  73. #include "basic-block.h"
  74. #include "df.h"
  75. #include "dce.h"
  76. #include "dbgcnt.h"
  77. #include "rtl-iter.h"
  78. #define FORWARDER_BLOCK_P(BB) ((BB)->flags & BB_FORWARDER_BLOCK)
  79. /* Set to true when we are running first pass of try_optimize_cfg loop. */
  80. static bool first_pass;
  81. /* Set to true if crossjumps occurred in the latest run of try_optimize_cfg. */
  82. static bool crossjumps_occured;
  83. /* Set to true if we couldn't run an optimization due to stale liveness
  84. information; we should run df_analyze to enable more opportunities. */
  85. static bool block_was_dirty;
  86. static bool try_crossjump_to_edge (int, edge, edge, enum replace_direction);
  87. static bool try_crossjump_bb (int, basic_block);
  88. static bool outgoing_edges_match (int, basic_block, basic_block);
  89. static enum replace_direction old_insns_match_p (int, rtx_insn *, rtx_insn *);
  90. static void merge_blocks_move_predecessor_nojumps (basic_block, basic_block);
  91. static void merge_blocks_move_successor_nojumps (basic_block, basic_block);
  92. static bool try_optimize_cfg (int);
  93. static bool try_simplify_condjump (basic_block);
  94. static bool try_forward_edges (int, basic_block);
  95. static edge thread_jump (edge, basic_block);
  96. static bool mark_effect (rtx, bitmap);
  97. static void notice_new_block (basic_block);
  98. static void update_forwarder_flag (basic_block);
  99. static void merge_memattrs (rtx, rtx);
  100. /* Set flags for newly created block. */
  101. static void
  102. notice_new_block (basic_block bb)
  103. {
  104. if (!bb)
  105. return;
  106. if (forwarder_block_p (bb))
  107. bb->flags |= BB_FORWARDER_BLOCK;
  108. }
  109. /* Recompute forwarder flag after block has been modified. */
  110. static void
  111. update_forwarder_flag (basic_block bb)
  112. {
  113. if (forwarder_block_p (bb))
  114. bb->flags |= BB_FORWARDER_BLOCK;
  115. else
  116. bb->flags &= ~BB_FORWARDER_BLOCK;
  117. }
  118. /* Simplify a conditional jump around an unconditional jump.
  119. Return true if something changed. */
  120. static bool
  121. try_simplify_condjump (basic_block cbranch_block)
  122. {
  123. basic_block jump_block, jump_dest_block, cbranch_dest_block;
  124. edge cbranch_jump_edge, cbranch_fallthru_edge;
  125. rtx_insn *cbranch_insn;
  126. /* Verify that there are exactly two successors. */
  127. if (EDGE_COUNT (cbranch_block->succs) != 2)
  128. return false;
  129. /* Verify that we've got a normal conditional branch at the end
  130. of the block. */
  131. cbranch_insn = BB_END (cbranch_block);
  132. if (!any_condjump_p (cbranch_insn))
  133. return false;
  134. cbranch_fallthru_edge = FALLTHRU_EDGE (cbranch_block);
  135. cbranch_jump_edge = BRANCH_EDGE (cbranch_block);
  136. /* The next block must not have multiple predecessors, must not
  137. be the last block in the function, and must contain just the
  138. unconditional jump. */
  139. jump_block = cbranch_fallthru_edge->dest;
  140. if (!single_pred_p (jump_block)
  141. || jump_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
  142. || !FORWARDER_BLOCK_P (jump_block))
  143. return false;
  144. jump_dest_block = single_succ (jump_block);
  145. /* If we are partitioning hot/cold basic blocks, we don't want to
  146. mess up unconditional or indirect jumps that cross between hot
  147. and cold sections.
  148. Basic block partitioning may result in some jumps that appear to
  149. be optimizable (or blocks that appear to be mergeable), but which really
  150. must be left untouched (they are required to make it safely across
  151. partition boundaries). See the comments at the top of
  152. bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
  153. if (BB_PARTITION (jump_block) != BB_PARTITION (jump_dest_block)
  154. || (cbranch_jump_edge->flags & EDGE_CROSSING))
  155. return false;
  156. /* The conditional branch must target the block after the
  157. unconditional branch. */
  158. cbranch_dest_block = cbranch_jump_edge->dest;
  159. if (cbranch_dest_block == EXIT_BLOCK_PTR_FOR_FN (cfun)
  160. || !can_fallthru (jump_block, cbranch_dest_block))
  161. return false;
  162. /* Invert the conditional branch. */
  163. if (!invert_jump (cbranch_insn, block_label (jump_dest_block), 0))
  164. return false;
  165. if (dump_file)
  166. fprintf (dump_file, "Simplifying condjump %i around jump %i\n",
  167. INSN_UID (cbranch_insn), INSN_UID (BB_END (jump_block)));
  168. /* Success. Update the CFG to match. Note that after this point
  169. the edge variable names appear backwards; the redirection is done
  170. this way to preserve edge profile data. */
  171. cbranch_jump_edge = redirect_edge_succ_nodup (cbranch_jump_edge,
  172. cbranch_dest_block);
  173. cbranch_fallthru_edge = redirect_edge_succ_nodup (cbranch_fallthru_edge,
  174. jump_dest_block);
  175. cbranch_jump_edge->flags |= EDGE_FALLTHRU;
  176. cbranch_fallthru_edge->flags &= ~EDGE_FALLTHRU;
  177. update_br_prob_note (cbranch_block);
  178. /* Delete the block with the unconditional jump, and clean up the mess. */
  179. delete_basic_block (jump_block);
  180. tidy_fallthru_edge (cbranch_jump_edge);
  181. update_forwarder_flag (cbranch_block);
  182. return true;
  183. }
  184. /* Attempt to prove that operation is NOOP using CSElib or mark the effect
  185. on register. Used by jump threading. */
  186. static bool
  187. mark_effect (rtx exp, regset nonequal)
  188. {
  189. int regno;
  190. rtx dest;
  191. switch (GET_CODE (exp))
  192. {
  193. /* In case we do clobber the register, mark it as equal, as we know the
  194. value is dead so it don't have to match. */
  195. case CLOBBER:
  196. if (REG_P (XEXP (exp, 0)))
  197. {
  198. dest = XEXP (exp, 0);
  199. regno = REGNO (dest);
  200. if (HARD_REGISTER_NUM_P (regno))
  201. bitmap_clear_range (nonequal, regno,
  202. hard_regno_nregs[regno][GET_MODE (dest)]);
  203. else
  204. bitmap_clear_bit (nonequal, regno);
  205. }
  206. return false;
  207. case SET:
  208. if (rtx_equal_for_cselib_p (SET_DEST (exp), SET_SRC (exp)))
  209. return false;
  210. dest = SET_DEST (exp);
  211. if (dest == pc_rtx)
  212. return false;
  213. if (!REG_P (dest))
  214. return true;
  215. regno = REGNO (dest);
  216. if (HARD_REGISTER_NUM_P (regno))
  217. bitmap_set_range (nonequal, regno,
  218. hard_regno_nregs[regno][GET_MODE (dest)]);
  219. else
  220. bitmap_set_bit (nonequal, regno);
  221. return false;
  222. default:
  223. return false;
  224. }
  225. }
  226. /* Return true if X contains a register in NONEQUAL. */
  227. static bool
  228. mentions_nonequal_regs (const_rtx x, regset nonequal)
  229. {
  230. subrtx_iterator::array_type array;
  231. FOR_EACH_SUBRTX (iter, array, x, NONCONST)
  232. {
  233. const_rtx x = *iter;
  234. if (REG_P (x))
  235. {
  236. unsigned int regno = REGNO (x);
  237. if (REGNO_REG_SET_P (nonequal, regno))
  238. return true;
  239. if (regno < FIRST_PSEUDO_REGISTER)
  240. {
  241. int n = hard_regno_nregs[regno][GET_MODE (x)];
  242. while (--n > 0)
  243. if (REGNO_REG_SET_P (nonequal, regno + n))
  244. return true;
  245. }
  246. }
  247. }
  248. return false;
  249. }
  250. /* Attempt to prove that the basic block B will have no side effects and
  251. always continues in the same edge if reached via E. Return the edge
  252. if exist, NULL otherwise. */
  253. static edge
  254. thread_jump (edge e, basic_block b)
  255. {
  256. rtx set1, set2, cond1, cond2;
  257. rtx_insn *insn;
  258. enum rtx_code code1, code2, reversed_code2;
  259. bool reverse1 = false;
  260. unsigned i;
  261. regset nonequal;
  262. bool failed = false;
  263. reg_set_iterator rsi;
  264. if (b->flags & BB_NONTHREADABLE_BLOCK)
  265. return NULL;
  266. /* At the moment, we do handle only conditional jumps, but later we may
  267. want to extend this code to tablejumps and others. */
  268. if (EDGE_COUNT (e->src->succs) != 2)
  269. return NULL;
  270. if (EDGE_COUNT (b->succs) != 2)
  271. {
  272. b->flags |= BB_NONTHREADABLE_BLOCK;
  273. return NULL;
  274. }
  275. /* Second branch must end with onlyjump, as we will eliminate the jump. */
  276. if (!any_condjump_p (BB_END (e->src)))
  277. return NULL;
  278. if (!any_condjump_p (BB_END (b)) || !onlyjump_p (BB_END (b)))
  279. {
  280. b->flags |= BB_NONTHREADABLE_BLOCK;
  281. return NULL;
  282. }
  283. set1 = pc_set (BB_END (e->src));
  284. set2 = pc_set (BB_END (b));
  285. if (((e->flags & EDGE_FALLTHRU) != 0)
  286. != (XEXP (SET_SRC (set1), 1) == pc_rtx))
  287. reverse1 = true;
  288. cond1 = XEXP (SET_SRC (set1), 0);
  289. cond2 = XEXP (SET_SRC (set2), 0);
  290. if (reverse1)
  291. code1 = reversed_comparison_code (cond1, BB_END (e->src));
  292. else
  293. code1 = GET_CODE (cond1);
  294. code2 = GET_CODE (cond2);
  295. reversed_code2 = reversed_comparison_code (cond2, BB_END (b));
  296. if (!comparison_dominates_p (code1, code2)
  297. && !comparison_dominates_p (code1, reversed_code2))
  298. return NULL;
  299. /* Ensure that the comparison operators are equivalent.
  300. ??? This is far too pessimistic. We should allow swapped operands,
  301. different CCmodes, or for example comparisons for interval, that
  302. dominate even when operands are not equivalent. */
  303. if (!rtx_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
  304. || !rtx_equal_p (XEXP (cond1, 1), XEXP (cond2, 1)))
  305. return NULL;
  306. /* Short circuit cases where block B contains some side effects, as we can't
  307. safely bypass it. */
  308. for (insn = NEXT_INSN (BB_HEAD (b)); insn != NEXT_INSN (BB_END (b));
  309. insn = NEXT_INSN (insn))
  310. if (INSN_P (insn) && side_effects_p (PATTERN (insn)))
  311. {
  312. b->flags |= BB_NONTHREADABLE_BLOCK;
  313. return NULL;
  314. }
  315. cselib_init (0);
  316. /* First process all values computed in the source basic block. */
  317. for (insn = NEXT_INSN (BB_HEAD (e->src));
  318. insn != NEXT_INSN (BB_END (e->src));
  319. insn = NEXT_INSN (insn))
  320. if (INSN_P (insn))
  321. cselib_process_insn (insn);
  322. nonequal = BITMAP_ALLOC (NULL);
  323. CLEAR_REG_SET (nonequal);
  324. /* Now assume that we've continued by the edge E to B and continue
  325. processing as if it were same basic block.
  326. Our goal is to prove that whole block is an NOOP. */
  327. for (insn = NEXT_INSN (BB_HEAD (b));
  328. insn != NEXT_INSN (BB_END (b)) && !failed;
  329. insn = NEXT_INSN (insn))
  330. {
  331. if (INSN_P (insn))
  332. {
  333. rtx pat = PATTERN (insn);
  334. if (GET_CODE (pat) == PARALLEL)
  335. {
  336. for (i = 0; i < (unsigned)XVECLEN (pat, 0); i++)
  337. failed |= mark_effect (XVECEXP (pat, 0, i), nonequal);
  338. }
  339. else
  340. failed |= mark_effect (pat, nonequal);
  341. }
  342. cselib_process_insn (insn);
  343. }
  344. /* Later we should clear nonequal of dead registers. So far we don't
  345. have life information in cfg_cleanup. */
  346. if (failed)
  347. {
  348. b->flags |= BB_NONTHREADABLE_BLOCK;
  349. goto failed_exit;
  350. }
  351. /* cond2 must not mention any register that is not equal to the
  352. former block. */
  353. if (mentions_nonequal_regs (cond2, nonequal))
  354. goto failed_exit;
  355. EXECUTE_IF_SET_IN_REG_SET (nonequal, 0, i, rsi)
  356. goto failed_exit;
  357. BITMAP_FREE (nonequal);
  358. cselib_finish ();
  359. if ((comparison_dominates_p (code1, code2) != 0)
  360. != (XEXP (SET_SRC (set2), 1) == pc_rtx))
  361. return BRANCH_EDGE (b);
  362. else
  363. return FALLTHRU_EDGE (b);
  364. failed_exit:
  365. BITMAP_FREE (nonequal);
  366. cselib_finish ();
  367. return NULL;
  368. }
  369. /* Attempt to forward edges leaving basic block B.
  370. Return true if successful. */
  371. static bool
  372. try_forward_edges (int mode, basic_block b)
  373. {
  374. bool changed = false;
  375. edge_iterator ei;
  376. edge e, *threaded_edges = NULL;
  377. /* If we are partitioning hot/cold basic blocks, we don't want to
  378. mess up unconditional or indirect jumps that cross between hot
  379. and cold sections.
  380. Basic block partitioning may result in some jumps that appear to
  381. be optimizable (or blocks that appear to be mergeable), but which really
  382. must be left untouched (they are required to make it safely across
  383. partition boundaries). See the comments at the top of
  384. bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
  385. if (JUMP_P (BB_END (b)) && CROSSING_JUMP_P (BB_END (b)))
  386. return false;
  387. for (ei = ei_start (b->succs); (e = ei_safe_edge (ei)); )
  388. {
  389. basic_block target, first;
  390. location_t goto_locus;
  391. int counter;
  392. bool threaded = false;
  393. int nthreaded_edges = 0;
  394. bool may_thread = first_pass || (b->flags & BB_MODIFIED) != 0;
  395. /* Skip complex edges because we don't know how to update them.
  396. Still handle fallthru edges, as we can succeed to forward fallthru
  397. edge to the same place as the branch edge of conditional branch
  398. and turn conditional branch to an unconditional branch. */
  399. if (e->flags & EDGE_COMPLEX)
  400. {
  401. ei_next (&ei);
  402. continue;
  403. }
  404. target = first = e->dest;
  405. counter = NUM_FIXED_BLOCKS;
  406. goto_locus = e->goto_locus;
  407. /* If we are partitioning hot/cold basic_blocks, we don't want to mess
  408. up jumps that cross between hot/cold sections.
  409. Basic block partitioning may result in some jumps that appear
  410. to be optimizable (or blocks that appear to be mergeable), but which
  411. really must be left untouched (they are required to make it safely
  412. across partition boundaries). See the comments at the top of
  413. bb-reorder.c:partition_hot_cold_basic_blocks for complete
  414. details. */
  415. if (first != EXIT_BLOCK_PTR_FOR_FN (cfun)
  416. && JUMP_P (BB_END (first))
  417. && CROSSING_JUMP_P (BB_END (first)))
  418. return changed;
  419. while (counter < n_basic_blocks_for_fn (cfun))
  420. {
  421. basic_block new_target = NULL;
  422. bool new_target_threaded = false;
  423. may_thread |= (target->flags & BB_MODIFIED) != 0;
  424. if (FORWARDER_BLOCK_P (target)
  425. && !(single_succ_edge (target)->flags & EDGE_CROSSING)
  426. && single_succ (target) != EXIT_BLOCK_PTR_FOR_FN (cfun))
  427. {
  428. /* Bypass trivial infinite loops. */
  429. new_target = single_succ (target);
  430. if (target == new_target)
  431. counter = n_basic_blocks_for_fn (cfun);
  432. else if (!optimize)
  433. {
  434. /* When not optimizing, ensure that edges or forwarder
  435. blocks with different locus are not optimized out. */
  436. location_t new_locus = single_succ_edge (target)->goto_locus;
  437. location_t locus = goto_locus;
  438. if (LOCATION_LOCUS (new_locus) != UNKNOWN_LOCATION
  439. && LOCATION_LOCUS (locus) != UNKNOWN_LOCATION
  440. && new_locus != locus)
  441. new_target = NULL;
  442. else
  443. {
  444. if (LOCATION_LOCUS (new_locus) != UNKNOWN_LOCATION)
  445. locus = new_locus;
  446. rtx_insn *last = BB_END (target);
  447. if (DEBUG_INSN_P (last))
  448. last = prev_nondebug_insn (last);
  449. if (last && INSN_P (last))
  450. new_locus = INSN_LOCATION (last);
  451. else
  452. new_locus = UNKNOWN_LOCATION;
  453. if (LOCATION_LOCUS (new_locus) != UNKNOWN_LOCATION
  454. && LOCATION_LOCUS (locus) != UNKNOWN_LOCATION
  455. && new_locus != locus)
  456. new_target = NULL;
  457. else
  458. {
  459. if (LOCATION_LOCUS (new_locus) != UNKNOWN_LOCATION)
  460. locus = new_locus;
  461. goto_locus = locus;
  462. }
  463. }
  464. }
  465. }
  466. /* Allow to thread only over one edge at time to simplify updating
  467. of probabilities. */
  468. else if ((mode & CLEANUP_THREADING) && may_thread)
  469. {
  470. edge t = thread_jump (e, target);
  471. if (t)
  472. {
  473. if (!threaded_edges)
  474. threaded_edges = XNEWVEC (edge,
  475. n_basic_blocks_for_fn (cfun));
  476. else
  477. {
  478. int i;
  479. /* Detect an infinite loop across blocks not
  480. including the start block. */
  481. for (i = 0; i < nthreaded_edges; ++i)
  482. if (threaded_edges[i] == t)
  483. break;
  484. if (i < nthreaded_edges)
  485. {
  486. counter = n_basic_blocks_for_fn (cfun);
  487. break;
  488. }
  489. }
  490. /* Detect an infinite loop across the start block. */
  491. if (t->dest == b)
  492. break;
  493. gcc_assert (nthreaded_edges
  494. < (n_basic_blocks_for_fn (cfun)
  495. - NUM_FIXED_BLOCKS));
  496. threaded_edges[nthreaded_edges++] = t;
  497. new_target = t->dest;
  498. new_target_threaded = true;
  499. }
  500. }
  501. if (!new_target)
  502. break;
  503. counter++;
  504. target = new_target;
  505. threaded |= new_target_threaded;
  506. }
  507. if (counter >= n_basic_blocks_for_fn (cfun))
  508. {
  509. if (dump_file)
  510. fprintf (dump_file, "Infinite loop in BB %i.\n",
  511. target->index);
  512. }
  513. else if (target == first)
  514. ; /* We didn't do anything. */
  515. else
  516. {
  517. /* Save the values now, as the edge may get removed. */
  518. gcov_type edge_count = e->count;
  519. int edge_probability = e->probability;
  520. int edge_frequency;
  521. int n = 0;
  522. e->goto_locus = goto_locus;
  523. /* Don't force if target is exit block. */
  524. if (threaded && target != EXIT_BLOCK_PTR_FOR_FN (cfun))
  525. {
  526. notice_new_block (redirect_edge_and_branch_force (e, target));
  527. if (dump_file)
  528. fprintf (dump_file, "Conditionals threaded.\n");
  529. }
  530. else if (!redirect_edge_and_branch (e, target))
  531. {
  532. if (dump_file)
  533. fprintf (dump_file,
  534. "Forwarding edge %i->%i to %i failed.\n",
  535. b->index, e->dest->index, target->index);
  536. ei_next (&ei);
  537. continue;
  538. }
  539. /* We successfully forwarded the edge. Now update profile
  540. data: for each edge we traversed in the chain, remove
  541. the original edge's execution count. */
  542. edge_frequency = apply_probability (b->frequency, edge_probability);
  543. do
  544. {
  545. edge t;
  546. if (!single_succ_p (first))
  547. {
  548. gcc_assert (n < nthreaded_edges);
  549. t = threaded_edges [n++];
  550. gcc_assert (t->src == first);
  551. update_bb_profile_for_threading (first, edge_frequency,
  552. edge_count, t);
  553. update_br_prob_note (first);
  554. }
  555. else
  556. {
  557. first->count -= edge_count;
  558. if (first->count < 0)
  559. first->count = 0;
  560. first->frequency -= edge_frequency;
  561. if (first->frequency < 0)
  562. first->frequency = 0;
  563. /* It is possible that as the result of
  564. threading we've removed edge as it is
  565. threaded to the fallthru edge. Avoid
  566. getting out of sync. */
  567. if (n < nthreaded_edges
  568. && first == threaded_edges [n]->src)
  569. n++;
  570. t = single_succ_edge (first);
  571. }
  572. t->count -= edge_count;
  573. if (t->count < 0)
  574. t->count = 0;
  575. first = t->dest;
  576. }
  577. while (first != target);
  578. changed = true;
  579. continue;
  580. }
  581. ei_next (&ei);
  582. }
  583. free (threaded_edges);
  584. return changed;
  585. }
  586. /* Blocks A and B are to be merged into a single block. A has no incoming
  587. fallthru edge, so it can be moved before B without adding or modifying
  588. any jumps (aside from the jump from A to B). */
  589. static void
  590. merge_blocks_move_predecessor_nojumps (basic_block a, basic_block b)
  591. {
  592. rtx_insn *barrier;
  593. /* If we are partitioning hot/cold basic blocks, we don't want to
  594. mess up unconditional or indirect jumps that cross between hot
  595. and cold sections.
  596. Basic block partitioning may result in some jumps that appear to
  597. be optimizable (or blocks that appear to be mergeable), but which really
  598. must be left untouched (they are required to make it safely across
  599. partition boundaries). See the comments at the top of
  600. bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
  601. if (BB_PARTITION (a) != BB_PARTITION (b))
  602. return;
  603. barrier = next_nonnote_insn (BB_END (a));
  604. gcc_assert (BARRIER_P (barrier));
  605. delete_insn (barrier);
  606. /* Scramble the insn chain. */
  607. if (BB_END (a) != PREV_INSN (BB_HEAD (b)))
  608. reorder_insns_nobb (BB_HEAD (a), BB_END (a), PREV_INSN (BB_HEAD (b)));
  609. df_set_bb_dirty (a);
  610. if (dump_file)
  611. fprintf (dump_file, "Moved block %d before %d and merged.\n",
  612. a->index, b->index);
  613. /* Swap the records for the two blocks around. */
  614. unlink_block (a);
  615. link_block (a, b->prev_bb);
  616. /* Now blocks A and B are contiguous. Merge them. */
  617. merge_blocks (a, b);
  618. }
  619. /* Blocks A and B are to be merged into a single block. B has no outgoing
  620. fallthru edge, so it can be moved after A without adding or modifying
  621. any jumps (aside from the jump from A to B). */
  622. static void
  623. merge_blocks_move_successor_nojumps (basic_block a, basic_block b)
  624. {
  625. rtx_insn *barrier, *real_b_end;
  626. rtx label;
  627. rtx_jump_table_data *table;
  628. /* If we are partitioning hot/cold basic blocks, we don't want to
  629. mess up unconditional or indirect jumps that cross between hot
  630. and cold sections.
  631. Basic block partitioning may result in some jumps that appear to
  632. be optimizable (or blocks that appear to be mergeable), but which really
  633. must be left untouched (they are required to make it safely across
  634. partition boundaries). See the comments at the top of
  635. bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
  636. if (BB_PARTITION (a) != BB_PARTITION (b))
  637. return;
  638. real_b_end = BB_END (b);
  639. /* If there is a jump table following block B temporarily add the jump table
  640. to block B so that it will also be moved to the correct location. */
  641. if (tablejump_p (BB_END (b), &label, &table)
  642. && prev_active_insn (label) == BB_END (b))
  643. {
  644. BB_END (b) = table;
  645. }
  646. /* There had better have been a barrier there. Delete it. */
  647. barrier = NEXT_INSN (BB_END (b));
  648. if (barrier && BARRIER_P (barrier))
  649. delete_insn (barrier);
  650. /* Scramble the insn chain. */
  651. reorder_insns_nobb (BB_HEAD (b), BB_END (b), BB_END (a));
  652. /* Restore the real end of b. */
  653. BB_END (b) = real_b_end;
  654. if (dump_file)
  655. fprintf (dump_file, "Moved block %d after %d and merged.\n",
  656. b->index, a->index);
  657. /* Now blocks A and B are contiguous. Merge them. */
  658. merge_blocks (a, b);
  659. }
  660. /* Attempt to merge basic blocks that are potentially non-adjacent.
  661. Return NULL iff the attempt failed, otherwise return basic block
  662. where cleanup_cfg should continue. Because the merging commonly
  663. moves basic block away or introduces another optimization
  664. possibility, return basic block just before B so cleanup_cfg don't
  665. need to iterate.
  666. It may be good idea to return basic block before C in the case
  667. C has been moved after B and originally appeared earlier in the
  668. insn sequence, but we have no information available about the
  669. relative ordering of these two. Hopefully it is not too common. */
  670. static basic_block
  671. merge_blocks_move (edge e, basic_block b, basic_block c, int mode)
  672. {
  673. basic_block next;
  674. /* If we are partitioning hot/cold basic blocks, we don't want to
  675. mess up unconditional or indirect jumps that cross between hot
  676. and cold sections.
  677. Basic block partitioning may result in some jumps that appear to
  678. be optimizable (or blocks that appear to be mergeable), but which really
  679. must be left untouched (they are required to make it safely across
  680. partition boundaries). See the comments at the top of
  681. bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
  682. if (BB_PARTITION (b) != BB_PARTITION (c))
  683. return NULL;
  684. /* If B has a fallthru edge to C, no need to move anything. */
  685. if (e->flags & EDGE_FALLTHRU)
  686. {
  687. int b_index = b->index, c_index = c->index;
  688. /* Protect the loop latches. */
  689. if (current_loops && c->loop_father->latch == c)
  690. return NULL;
  691. merge_blocks (b, c);
  692. update_forwarder_flag (b);
  693. if (dump_file)
  694. fprintf (dump_file, "Merged %d and %d without moving.\n",
  695. b_index, c_index);
  696. return b->prev_bb == ENTRY_BLOCK_PTR_FOR_FN (cfun) ? b : b->prev_bb;
  697. }
  698. /* Otherwise we will need to move code around. Do that only if expensive
  699. transformations are allowed. */
  700. else if (mode & CLEANUP_EXPENSIVE)
  701. {
  702. edge tmp_edge, b_fallthru_edge;
  703. bool c_has_outgoing_fallthru;
  704. bool b_has_incoming_fallthru;
  705. /* Avoid overactive code motion, as the forwarder blocks should be
  706. eliminated by edge redirection instead. One exception might have
  707. been if B is a forwarder block and C has no fallthru edge, but
  708. that should be cleaned up by bb-reorder instead. */
  709. if (FORWARDER_BLOCK_P (b) || FORWARDER_BLOCK_P (c))
  710. return NULL;
  711. /* We must make sure to not munge nesting of lexical blocks,
  712. and loop notes. This is done by squeezing out all the notes
  713. and leaving them there to lie. Not ideal, but functional. */
  714. tmp_edge = find_fallthru_edge (c->succs);
  715. c_has_outgoing_fallthru = (tmp_edge != NULL);
  716. tmp_edge = find_fallthru_edge (b->preds);
  717. b_has_incoming_fallthru = (tmp_edge != NULL);
  718. b_fallthru_edge = tmp_edge;
  719. next = b->prev_bb;
  720. if (next == c)
  721. next = next->prev_bb;
  722. /* Otherwise, we're going to try to move C after B. If C does
  723. not have an outgoing fallthru, then it can be moved
  724. immediately after B without introducing or modifying jumps. */
  725. if (! c_has_outgoing_fallthru)
  726. {
  727. merge_blocks_move_successor_nojumps (b, c);
  728. return next == ENTRY_BLOCK_PTR_FOR_FN (cfun) ? next->next_bb : next;
  729. }
  730. /* If B does not have an incoming fallthru, then it can be moved
  731. immediately before C without introducing or modifying jumps.
  732. C cannot be the first block, so we do not have to worry about
  733. accessing a non-existent block. */
  734. if (b_has_incoming_fallthru)
  735. {
  736. basic_block bb;
  737. if (b_fallthru_edge->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
  738. return NULL;
  739. bb = force_nonfallthru (b_fallthru_edge);
  740. if (bb)
  741. notice_new_block (bb);
  742. }
  743. merge_blocks_move_predecessor_nojumps (b, c);
  744. return next == ENTRY_BLOCK_PTR_FOR_FN (cfun) ? next->next_bb : next;
  745. }
  746. return NULL;
  747. }
  748. /* Removes the memory attributes of MEM expression
  749. if they are not equal. */
  750. static void
  751. merge_memattrs (rtx x, rtx y)
  752. {
  753. int i;
  754. int j;
  755. enum rtx_code code;
  756. const char *fmt;
  757. if (x == y)
  758. return;
  759. if (x == 0 || y == 0)
  760. return;
  761. code = GET_CODE (x);
  762. if (code != GET_CODE (y))
  763. return;
  764. if (GET_MODE (x) != GET_MODE (y))
  765. return;
  766. if (code == MEM && !mem_attrs_eq_p (MEM_ATTRS (x), MEM_ATTRS (y)))
  767. {
  768. if (! MEM_ATTRS (x))
  769. MEM_ATTRS (y) = 0;
  770. else if (! MEM_ATTRS (y))
  771. MEM_ATTRS (x) = 0;
  772. else
  773. {
  774. HOST_WIDE_INT mem_size;
  775. if (MEM_ALIAS_SET (x) != MEM_ALIAS_SET (y))
  776. {
  777. set_mem_alias_set (x, 0);
  778. set_mem_alias_set (y, 0);
  779. }
  780. if (! mem_expr_equal_p (MEM_EXPR (x), MEM_EXPR (y)))
  781. {
  782. set_mem_expr (x, 0);
  783. set_mem_expr (y, 0);
  784. clear_mem_offset (x);
  785. clear_mem_offset (y);
  786. }
  787. else if (MEM_OFFSET_KNOWN_P (x) != MEM_OFFSET_KNOWN_P (y)
  788. || (MEM_OFFSET_KNOWN_P (x)
  789. && MEM_OFFSET (x) != MEM_OFFSET (y)))
  790. {
  791. clear_mem_offset (x);
  792. clear_mem_offset (y);
  793. }
  794. if (MEM_SIZE_KNOWN_P (x) && MEM_SIZE_KNOWN_P (y))
  795. {
  796. mem_size = MAX (MEM_SIZE (x), MEM_SIZE (y));
  797. set_mem_size (x, mem_size);
  798. set_mem_size (y, mem_size);
  799. }
  800. else
  801. {
  802. clear_mem_size (x);
  803. clear_mem_size (y);
  804. }
  805. set_mem_align (x, MIN (MEM_ALIGN (x), MEM_ALIGN (y)));
  806. set_mem_align (y, MEM_ALIGN (x));
  807. }
  808. }
  809. if (code == MEM)
  810. {
  811. if (MEM_READONLY_P (x) != MEM_READONLY_P (y))
  812. {
  813. MEM_READONLY_P (x) = 0;
  814. MEM_READONLY_P (y) = 0;
  815. }
  816. if (MEM_NOTRAP_P (x) != MEM_NOTRAP_P (y))
  817. {
  818. MEM_NOTRAP_P (x) = 0;
  819. MEM_NOTRAP_P (y) = 0;
  820. }
  821. if (MEM_VOLATILE_P (x) != MEM_VOLATILE_P (y))
  822. {
  823. MEM_VOLATILE_P (x) = 1;
  824. MEM_VOLATILE_P (y) = 1;
  825. }
  826. }
  827. fmt = GET_RTX_FORMAT (code);
  828. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  829. {
  830. switch (fmt[i])
  831. {
  832. case 'E':
  833. /* Two vectors must have the same length. */
  834. if (XVECLEN (x, i) != XVECLEN (y, i))
  835. return;
  836. for (j = 0; j < XVECLEN (x, i); j++)
  837. merge_memattrs (XVECEXP (x, i, j), XVECEXP (y, i, j));
  838. break;
  839. case 'e':
  840. merge_memattrs (XEXP (x, i), XEXP (y, i));
  841. }
  842. }
  843. return;
  844. }
  845. /* Checks if patterns P1 and P2 are equivalent, apart from the possibly
  846. different single sets S1 and S2. */
  847. static bool
  848. equal_different_set_p (rtx p1, rtx s1, rtx p2, rtx s2)
  849. {
  850. int i;
  851. rtx e1, e2;
  852. if (p1 == s1 && p2 == s2)
  853. return true;
  854. if (GET_CODE (p1) != PARALLEL || GET_CODE (p2) != PARALLEL)
  855. return false;
  856. if (XVECLEN (p1, 0) != XVECLEN (p2, 0))
  857. return false;
  858. for (i = 0; i < XVECLEN (p1, 0); i++)
  859. {
  860. e1 = XVECEXP (p1, 0, i);
  861. e2 = XVECEXP (p2, 0, i);
  862. if (e1 == s1 && e2 == s2)
  863. continue;
  864. if (reload_completed
  865. ? rtx_renumbered_equal_p (e1, e2) : rtx_equal_p (e1, e2))
  866. continue;
  867. return false;
  868. }
  869. return true;
  870. }
  871. /* Examine register notes on I1 and I2 and return:
  872. - dir_forward if I1 can be replaced by I2, or
  873. - dir_backward if I2 can be replaced by I1, or
  874. - dir_both if both are the case. */
  875. static enum replace_direction
  876. can_replace_by (rtx_insn *i1, rtx_insn *i2)
  877. {
  878. rtx s1, s2, d1, d2, src1, src2, note1, note2;
  879. bool c1, c2;
  880. /* Check for 2 sets. */
  881. s1 = single_set (i1);
  882. s2 = single_set (i2);
  883. if (s1 == NULL_RTX || s2 == NULL_RTX)
  884. return dir_none;
  885. /* Check that the 2 sets set the same dest. */
  886. d1 = SET_DEST (s1);
  887. d2 = SET_DEST (s2);
  888. if (!(reload_completed
  889. ? rtx_renumbered_equal_p (d1, d2) : rtx_equal_p (d1, d2)))
  890. return dir_none;
  891. /* Find identical req_equiv or reg_equal note, which implies that the 2 sets
  892. set dest to the same value. */
  893. note1 = find_reg_equal_equiv_note (i1);
  894. note2 = find_reg_equal_equiv_note (i2);
  895. if (!note1 || !note2 || !rtx_equal_p (XEXP (note1, 0), XEXP (note2, 0))
  896. || !CONST_INT_P (XEXP (note1, 0)))
  897. return dir_none;
  898. if (!equal_different_set_p (PATTERN (i1), s1, PATTERN (i2), s2))
  899. return dir_none;
  900. /* Although the 2 sets set dest to the same value, we cannot replace
  901. (set (dest) (const_int))
  902. by
  903. (set (dest) (reg))
  904. because we don't know if the reg is live and has the same value at the
  905. location of replacement. */
  906. src1 = SET_SRC (s1);
  907. src2 = SET_SRC (s2);
  908. c1 = CONST_INT_P (src1);
  909. c2 = CONST_INT_P (src2);
  910. if (c1 && c2)
  911. return dir_both;
  912. else if (c2)
  913. return dir_forward;
  914. else if (c1)
  915. return dir_backward;
  916. return dir_none;
  917. }
  918. /* Merges directions A and B. */
  919. static enum replace_direction
  920. merge_dir (enum replace_direction a, enum replace_direction b)
  921. {
  922. /* Implements the following table:
  923. |bo fw bw no
  924. ---+-----------
  925. bo |bo fw bw no
  926. fw |-- fw no no
  927. bw |-- -- bw no
  928. no |-- -- -- no. */
  929. if (a == b)
  930. return a;
  931. if (a == dir_both)
  932. return b;
  933. if (b == dir_both)
  934. return a;
  935. return dir_none;
  936. }
  937. /* Examine I1 and I2 and return:
  938. - dir_forward if I1 can be replaced by I2, or
  939. - dir_backward if I2 can be replaced by I1, or
  940. - dir_both if both are the case. */
  941. static enum replace_direction
  942. old_insns_match_p (int mode ATTRIBUTE_UNUSED, rtx_insn *i1, rtx_insn *i2)
  943. {
  944. rtx p1, p2;
  945. /* Verify that I1 and I2 are equivalent. */
  946. if (GET_CODE (i1) != GET_CODE (i2))
  947. return dir_none;
  948. /* __builtin_unreachable() may lead to empty blocks (ending with
  949. NOTE_INSN_BASIC_BLOCK). They may be crossjumped. */
  950. if (NOTE_INSN_BASIC_BLOCK_P (i1) && NOTE_INSN_BASIC_BLOCK_P (i2))
  951. return dir_both;
  952. /* ??? Do not allow cross-jumping between different stack levels. */
  953. p1 = find_reg_note (i1, REG_ARGS_SIZE, NULL);
  954. p2 = find_reg_note (i2, REG_ARGS_SIZE, NULL);
  955. if (p1 && p2)
  956. {
  957. p1 = XEXP (p1, 0);
  958. p2 = XEXP (p2, 0);
  959. if (!rtx_equal_p (p1, p2))
  960. return dir_none;
  961. /* ??? Worse, this adjustment had better be constant lest we
  962. have differing incoming stack levels. */
  963. if (!frame_pointer_needed
  964. && find_args_size_adjust (i1) == HOST_WIDE_INT_MIN)
  965. return dir_none;
  966. }
  967. else if (p1 || p2)
  968. return dir_none;
  969. p1 = PATTERN (i1);
  970. p2 = PATTERN (i2);
  971. if (GET_CODE (p1) != GET_CODE (p2))
  972. return dir_none;
  973. /* If this is a CALL_INSN, compare register usage information.
  974. If we don't check this on stack register machines, the two
  975. CALL_INSNs might be merged leaving reg-stack.c with mismatching
  976. numbers of stack registers in the same basic block.
  977. If we don't check this on machines with delay slots, a delay slot may
  978. be filled that clobbers a parameter expected by the subroutine.
  979. ??? We take the simple route for now and assume that if they're
  980. equal, they were constructed identically.
  981. Also check for identical exception regions. */
  982. if (CALL_P (i1))
  983. {
  984. /* Ensure the same EH region. */
  985. rtx n1 = find_reg_note (i1, REG_EH_REGION, 0);
  986. rtx n2 = find_reg_note (i2, REG_EH_REGION, 0);
  987. if (!n1 && n2)
  988. return dir_none;
  989. if (n1 && (!n2 || XEXP (n1, 0) != XEXP (n2, 0)))
  990. return dir_none;
  991. if (!rtx_equal_p (CALL_INSN_FUNCTION_USAGE (i1),
  992. CALL_INSN_FUNCTION_USAGE (i2))
  993. || SIBLING_CALL_P (i1) != SIBLING_CALL_P (i2))
  994. return dir_none;
  995. /* For address sanitizer, never crossjump __asan_report_* builtins,
  996. otherwise errors might be reported on incorrect lines. */
  997. if (flag_sanitize & SANITIZE_ADDRESS)
  998. {
  999. rtx call = get_call_rtx_from (i1);
  1000. if (call && GET_CODE (XEXP (XEXP (call, 0), 0)) == SYMBOL_REF)
  1001. {
  1002. rtx symbol = XEXP (XEXP (call, 0), 0);
  1003. if (SYMBOL_REF_DECL (symbol)
  1004. && TREE_CODE (SYMBOL_REF_DECL (symbol)) == FUNCTION_DECL)
  1005. {
  1006. if ((DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol))
  1007. == BUILT_IN_NORMAL)
  1008. && DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol))
  1009. >= BUILT_IN_ASAN_REPORT_LOAD1
  1010. && DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol))
  1011. <= BUILT_IN_ASAN_STOREN)
  1012. return dir_none;
  1013. }
  1014. }
  1015. }
  1016. }
  1017. #ifdef STACK_REGS
  1018. /* If cross_jump_death_matters is not 0, the insn's mode
  1019. indicates whether or not the insn contains any stack-like
  1020. regs. */
  1021. if ((mode & CLEANUP_POST_REGSTACK) && stack_regs_mentioned (i1))
  1022. {
  1023. /* If register stack conversion has already been done, then
  1024. death notes must also be compared before it is certain that
  1025. the two instruction streams match. */
  1026. rtx note;
  1027. HARD_REG_SET i1_regset, i2_regset;
  1028. CLEAR_HARD_REG_SET (i1_regset);
  1029. CLEAR_HARD_REG_SET (i2_regset);
  1030. for (note = REG_NOTES (i1); note; note = XEXP (note, 1))
  1031. if (REG_NOTE_KIND (note) == REG_DEAD && STACK_REG_P (XEXP (note, 0)))
  1032. SET_HARD_REG_BIT (i1_regset, REGNO (XEXP (note, 0)));
  1033. for (note = REG_NOTES (i2); note; note = XEXP (note, 1))
  1034. if (REG_NOTE_KIND (note) == REG_DEAD && STACK_REG_P (XEXP (note, 0)))
  1035. SET_HARD_REG_BIT (i2_regset, REGNO (XEXP (note, 0)));
  1036. if (!hard_reg_set_equal_p (i1_regset, i2_regset))
  1037. return dir_none;
  1038. }
  1039. #endif
  1040. if (reload_completed
  1041. ? rtx_renumbered_equal_p (p1, p2) : rtx_equal_p (p1, p2))
  1042. return dir_both;
  1043. return can_replace_by (i1, i2);
  1044. }
  1045. /* When comparing insns I1 and I2 in flow_find_cross_jump or
  1046. flow_find_head_matching_sequence, ensure the notes match. */
  1047. static void
  1048. merge_notes (rtx_insn *i1, rtx_insn *i2)
  1049. {
  1050. /* If the merged insns have different REG_EQUAL notes, then
  1051. remove them. */
  1052. rtx equiv1 = find_reg_equal_equiv_note (i1);
  1053. rtx equiv2 = find_reg_equal_equiv_note (i2);
  1054. if (equiv1 && !equiv2)
  1055. remove_note (i1, equiv1);
  1056. else if (!equiv1 && equiv2)
  1057. remove_note (i2, equiv2);
  1058. else if (equiv1 && equiv2
  1059. && !rtx_equal_p (XEXP (equiv1, 0), XEXP (equiv2, 0)))
  1060. {
  1061. remove_note (i1, equiv1);
  1062. remove_note (i2, equiv2);
  1063. }
  1064. }
  1065. /* Walks from I1 in BB1 backward till the next non-debug insn, and returns the
  1066. resulting insn in I1, and the corresponding bb in BB1. At the head of a
  1067. bb, if there is a predecessor bb that reaches this bb via fallthru, and
  1068. FOLLOW_FALLTHRU, walks further in the predecessor bb and registers this in
  1069. DID_FALLTHRU. Otherwise, stops at the head of the bb. */
  1070. static void
  1071. walk_to_nondebug_insn (rtx_insn **i1, basic_block *bb1, bool follow_fallthru,
  1072. bool *did_fallthru)
  1073. {
  1074. edge fallthru;
  1075. *did_fallthru = false;
  1076. /* Ignore notes. */
  1077. while (!NONDEBUG_INSN_P (*i1))
  1078. {
  1079. if (*i1 != BB_HEAD (*bb1))
  1080. {
  1081. *i1 = PREV_INSN (*i1);
  1082. continue;
  1083. }
  1084. if (!follow_fallthru)
  1085. return;
  1086. fallthru = find_fallthru_edge ((*bb1)->preds);
  1087. if (!fallthru || fallthru->src == ENTRY_BLOCK_PTR_FOR_FN (cfun)
  1088. || !single_succ_p (fallthru->src))
  1089. return;
  1090. *bb1 = fallthru->src;
  1091. *i1 = BB_END (*bb1);
  1092. *did_fallthru = true;
  1093. }
  1094. }
  1095. /* Look through the insns at the end of BB1 and BB2 and find the longest
  1096. sequence that are either equivalent, or allow forward or backward
  1097. replacement. Store the first insns for that sequence in *F1 and *F2 and
  1098. return the sequence length.
  1099. DIR_P indicates the allowed replacement direction on function entry, and
  1100. the actual replacement direction on function exit. If NULL, only equivalent
  1101. sequences are allowed.
  1102. To simplify callers of this function, if the blocks match exactly,
  1103. store the head of the blocks in *F1 and *F2. */
  1104. int
  1105. flow_find_cross_jump (basic_block bb1, basic_block bb2, rtx_insn **f1,
  1106. rtx_insn **f2, enum replace_direction *dir_p)
  1107. {
  1108. rtx_insn *i1, *i2, *last1, *last2, *afterlast1, *afterlast2;
  1109. int ninsns = 0;
  1110. enum replace_direction dir, last_dir, afterlast_dir;
  1111. bool follow_fallthru, did_fallthru;
  1112. if (dir_p)
  1113. dir = *dir_p;
  1114. else
  1115. dir = dir_both;
  1116. afterlast_dir = dir;
  1117. last_dir = afterlast_dir;
  1118. /* Skip simple jumps at the end of the blocks. Complex jumps still
  1119. need to be compared for equivalence, which we'll do below. */
  1120. i1 = BB_END (bb1);
  1121. last1 = afterlast1 = last2 = afterlast2 = NULL;
  1122. if (onlyjump_p (i1)
  1123. || (returnjump_p (i1) && !side_effects_p (PATTERN (i1))))
  1124. {
  1125. last1 = i1;
  1126. i1 = PREV_INSN (i1);
  1127. }
  1128. i2 = BB_END (bb2);
  1129. if (onlyjump_p (i2)
  1130. || (returnjump_p (i2) && !side_effects_p (PATTERN (i2))))
  1131. {
  1132. last2 = i2;
  1133. /* Count everything except for unconditional jump as insn.
  1134. Don't count any jumps if dir_p is NULL. */
  1135. if (!simplejump_p (i2) && !returnjump_p (i2) && last1 && dir_p)
  1136. ninsns++;
  1137. i2 = PREV_INSN (i2);
  1138. }
  1139. while (true)
  1140. {
  1141. /* In the following example, we can replace all jumps to C by jumps to A.
  1142. This removes 4 duplicate insns.
  1143. [bb A] insn1 [bb C] insn1
  1144. insn2 insn2
  1145. [bb B] insn3 insn3
  1146. insn4 insn4
  1147. jump_insn jump_insn
  1148. We could also replace all jumps to A by jumps to C, but that leaves B
  1149. alive, and removes only 2 duplicate insns. In a subsequent crossjump
  1150. step, all jumps to B would be replaced with jumps to the middle of C,
  1151. achieving the same result with more effort.
  1152. So we allow only the first possibility, which means that we don't allow
  1153. fallthru in the block that's being replaced. */
  1154. follow_fallthru = dir_p && dir != dir_forward;
  1155. walk_to_nondebug_insn (&i1, &bb1, follow_fallthru, &did_fallthru);
  1156. if (did_fallthru)
  1157. dir = dir_backward;
  1158. follow_fallthru = dir_p && dir != dir_backward;
  1159. walk_to_nondebug_insn (&i2, &bb2, follow_fallthru, &did_fallthru);
  1160. if (did_fallthru)
  1161. dir = dir_forward;
  1162. if (i1 == BB_HEAD (bb1) || i2 == BB_HEAD (bb2))
  1163. break;
  1164. dir = merge_dir (dir, old_insns_match_p (0, i1, i2));
  1165. if (dir == dir_none || (!dir_p && dir != dir_both))
  1166. break;
  1167. merge_memattrs (i1, i2);
  1168. /* Don't begin a cross-jump with a NOTE insn. */
  1169. if (INSN_P (i1))
  1170. {
  1171. merge_notes (i1, i2);
  1172. afterlast1 = last1, afterlast2 = last2;
  1173. last1 = i1, last2 = i2;
  1174. afterlast_dir = last_dir;
  1175. last_dir = dir;
  1176. if (active_insn_p (i1))
  1177. ninsns++;
  1178. }
  1179. i1 = PREV_INSN (i1);
  1180. i2 = PREV_INSN (i2);
  1181. }
  1182. #ifdef HAVE_cc0
  1183. /* Don't allow the insn after a compare to be shared by
  1184. cross-jumping unless the compare is also shared. */
  1185. if (ninsns && reg_mentioned_p (cc0_rtx, last1) && ! sets_cc0_p (last1))
  1186. last1 = afterlast1, last2 = afterlast2, last_dir = afterlast_dir, ninsns--;
  1187. #endif
  1188. /* Include preceding notes and labels in the cross-jump. One,
  1189. this may bring us to the head of the blocks as requested above.
  1190. Two, it keeps line number notes as matched as may be. */
  1191. if (ninsns)
  1192. {
  1193. bb1 = BLOCK_FOR_INSN (last1);
  1194. while (last1 != BB_HEAD (bb1) && !NONDEBUG_INSN_P (PREV_INSN (last1)))
  1195. last1 = PREV_INSN (last1);
  1196. if (last1 != BB_HEAD (bb1) && LABEL_P (PREV_INSN (last1)))
  1197. last1 = PREV_INSN (last1);
  1198. bb2 = BLOCK_FOR_INSN (last2);
  1199. while (last2 != BB_HEAD (bb2) && !NONDEBUG_INSN_P (PREV_INSN (last2)))
  1200. last2 = PREV_INSN (last2);
  1201. if (last2 != BB_HEAD (bb2) && LABEL_P (PREV_INSN (last2)))
  1202. last2 = PREV_INSN (last2);
  1203. *f1 = last1;
  1204. *f2 = last2;
  1205. }
  1206. if (dir_p)
  1207. *dir_p = last_dir;
  1208. return ninsns;
  1209. }
  1210. /* Like flow_find_cross_jump, except start looking for a matching sequence from
  1211. the head of the two blocks. Do not include jumps at the end.
  1212. If STOP_AFTER is nonzero, stop after finding that many matching
  1213. instructions. If STOP_AFTER is zero, count all INSN_P insns, if it is
  1214. non-zero, only count active insns. */
  1215. int
  1216. flow_find_head_matching_sequence (basic_block bb1, basic_block bb2, rtx_insn **f1,
  1217. rtx_insn **f2, int stop_after)
  1218. {
  1219. rtx_insn *i1, *i2, *last1, *last2, *beforelast1, *beforelast2;
  1220. int ninsns = 0;
  1221. edge e;
  1222. edge_iterator ei;
  1223. int nehedges1 = 0, nehedges2 = 0;
  1224. FOR_EACH_EDGE (e, ei, bb1->succs)
  1225. if (e->flags & EDGE_EH)
  1226. nehedges1++;
  1227. FOR_EACH_EDGE (e, ei, bb2->succs)
  1228. if (e->flags & EDGE_EH)
  1229. nehedges2++;
  1230. i1 = BB_HEAD (bb1);
  1231. i2 = BB_HEAD (bb2);
  1232. last1 = beforelast1 = last2 = beforelast2 = NULL;
  1233. while (true)
  1234. {
  1235. /* Ignore notes, except NOTE_INSN_EPILOGUE_BEG. */
  1236. while (!NONDEBUG_INSN_P (i1) && i1 != BB_END (bb1))
  1237. {
  1238. if (NOTE_P (i1) && NOTE_KIND (i1) == NOTE_INSN_EPILOGUE_BEG)
  1239. break;
  1240. i1 = NEXT_INSN (i1);
  1241. }
  1242. while (!NONDEBUG_INSN_P (i2) && i2 != BB_END (bb2))
  1243. {
  1244. if (NOTE_P (i2) && NOTE_KIND (i2) == NOTE_INSN_EPILOGUE_BEG)
  1245. break;
  1246. i2 = NEXT_INSN (i2);
  1247. }
  1248. if ((i1 == BB_END (bb1) && !NONDEBUG_INSN_P (i1))
  1249. || (i2 == BB_END (bb2) && !NONDEBUG_INSN_P (i2)))
  1250. break;
  1251. if (NOTE_P (i1) || NOTE_P (i2)
  1252. || JUMP_P (i1) || JUMP_P (i2))
  1253. break;
  1254. /* A sanity check to make sure we're not merging insns with different
  1255. effects on EH. If only one of them ends a basic block, it shouldn't
  1256. have an EH edge; if both end a basic block, there should be the same
  1257. number of EH edges. */
  1258. if ((i1 == BB_END (bb1) && i2 != BB_END (bb2)
  1259. && nehedges1 > 0)
  1260. || (i2 == BB_END (bb2) && i1 != BB_END (bb1)
  1261. && nehedges2 > 0)
  1262. || (i1 == BB_END (bb1) && i2 == BB_END (bb2)
  1263. && nehedges1 != nehedges2))
  1264. break;
  1265. if (old_insns_match_p (0, i1, i2) != dir_both)
  1266. break;
  1267. merge_memattrs (i1, i2);
  1268. /* Don't begin a cross-jump with a NOTE insn. */
  1269. if (INSN_P (i1))
  1270. {
  1271. merge_notes (i1, i2);
  1272. beforelast1 = last1, beforelast2 = last2;
  1273. last1 = i1, last2 = i2;
  1274. if (!stop_after || active_insn_p (i1))
  1275. ninsns++;
  1276. }
  1277. if (i1 == BB_END (bb1) || i2 == BB_END (bb2)
  1278. || (stop_after > 0 && ninsns == stop_after))
  1279. break;
  1280. i1 = NEXT_INSN (i1);
  1281. i2 = NEXT_INSN (i2);
  1282. }
  1283. #ifdef HAVE_cc0
  1284. /* Don't allow a compare to be shared by cross-jumping unless the insn
  1285. after the compare is also shared. */
  1286. if (ninsns && reg_mentioned_p (cc0_rtx, last1) && sets_cc0_p (last1))
  1287. last1 = beforelast1, last2 = beforelast2, ninsns--;
  1288. #endif
  1289. if (ninsns)
  1290. {
  1291. *f1 = last1;
  1292. *f2 = last2;
  1293. }
  1294. return ninsns;
  1295. }
  1296. /* Return true iff outgoing edges of BB1 and BB2 match, together with
  1297. the branch instruction. This means that if we commonize the control
  1298. flow before end of the basic block, the semantic remains unchanged.
  1299. We may assume that there exists one edge with a common destination. */
  1300. static bool
  1301. outgoing_edges_match (int mode, basic_block bb1, basic_block bb2)
  1302. {
  1303. int nehedges1 = 0, nehedges2 = 0;
  1304. edge fallthru1 = 0, fallthru2 = 0;
  1305. edge e1, e2;
  1306. edge_iterator ei;
  1307. /* If we performed shrink-wrapping, edges to the exit block can
  1308. only be distinguished for JUMP_INSNs. The two paths may differ in
  1309. whether they went through the prologue. Sibcalls are fine, we know
  1310. that we either didn't need or inserted an epilogue before them. */
  1311. if (crtl->shrink_wrapped
  1312. && single_succ_p (bb1)
  1313. && single_succ (bb1) == EXIT_BLOCK_PTR_FOR_FN (cfun)
  1314. && !JUMP_P (BB_END (bb1))
  1315. && !(CALL_P (BB_END (bb1)) && SIBLING_CALL_P (BB_END (bb1))))
  1316. return false;
  1317. /* If BB1 has only one successor, we may be looking at either an
  1318. unconditional jump, or a fake edge to exit. */
  1319. if (single_succ_p (bb1)
  1320. && (single_succ_edge (bb1)->flags & (EDGE_COMPLEX | EDGE_FAKE)) == 0
  1321. && (!JUMP_P (BB_END (bb1)) || simplejump_p (BB_END (bb1))))
  1322. return (single_succ_p (bb2)
  1323. && (single_succ_edge (bb2)->flags
  1324. & (EDGE_COMPLEX | EDGE_FAKE)) == 0
  1325. && (!JUMP_P (BB_END (bb2)) || simplejump_p (BB_END (bb2))));
  1326. /* Match conditional jumps - this may get tricky when fallthru and branch
  1327. edges are crossed. */
  1328. if (EDGE_COUNT (bb1->succs) == 2
  1329. && any_condjump_p (BB_END (bb1))
  1330. && onlyjump_p (BB_END (bb1)))
  1331. {
  1332. edge b1, f1, b2, f2;
  1333. bool reverse, match;
  1334. rtx set1, set2, cond1, cond2;
  1335. enum rtx_code code1, code2;
  1336. if (EDGE_COUNT (bb2->succs) != 2
  1337. || !any_condjump_p (BB_END (bb2))
  1338. || !onlyjump_p (BB_END (bb2)))
  1339. return false;
  1340. b1 = BRANCH_EDGE (bb1);
  1341. b2 = BRANCH_EDGE (bb2);
  1342. f1 = FALLTHRU_EDGE (bb1);
  1343. f2 = FALLTHRU_EDGE (bb2);
  1344. /* Get around possible forwarders on fallthru edges. Other cases
  1345. should be optimized out already. */
  1346. if (FORWARDER_BLOCK_P (f1->dest))
  1347. f1 = single_succ_edge (f1->dest);
  1348. if (FORWARDER_BLOCK_P (f2->dest))
  1349. f2 = single_succ_edge (f2->dest);
  1350. /* To simplify use of this function, return false if there are
  1351. unneeded forwarder blocks. These will get eliminated later
  1352. during cleanup_cfg. */
  1353. if (FORWARDER_BLOCK_P (f1->dest)
  1354. || FORWARDER_BLOCK_P (f2->dest)
  1355. || FORWARDER_BLOCK_P (b1->dest)
  1356. || FORWARDER_BLOCK_P (b2->dest))
  1357. return false;
  1358. if (f1->dest == f2->dest && b1->dest == b2->dest)
  1359. reverse = false;
  1360. else if (f1->dest == b2->dest && b1->dest == f2->dest)
  1361. reverse = true;
  1362. else
  1363. return false;
  1364. set1 = pc_set (BB_END (bb1));
  1365. set2 = pc_set (BB_END (bb2));
  1366. if ((XEXP (SET_SRC (set1), 1) == pc_rtx)
  1367. != (XEXP (SET_SRC (set2), 1) == pc_rtx))
  1368. reverse = !reverse;
  1369. cond1 = XEXP (SET_SRC (set1), 0);
  1370. cond2 = XEXP (SET_SRC (set2), 0);
  1371. code1 = GET_CODE (cond1);
  1372. if (reverse)
  1373. code2 = reversed_comparison_code (cond2, BB_END (bb2));
  1374. else
  1375. code2 = GET_CODE (cond2);
  1376. if (code2 == UNKNOWN)
  1377. return false;
  1378. /* Verify codes and operands match. */
  1379. match = ((code1 == code2
  1380. && rtx_renumbered_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
  1381. && rtx_renumbered_equal_p (XEXP (cond1, 1), XEXP (cond2, 1)))
  1382. || (code1 == swap_condition (code2)
  1383. && rtx_renumbered_equal_p (XEXP (cond1, 1),
  1384. XEXP (cond2, 0))
  1385. && rtx_renumbered_equal_p (XEXP (cond1, 0),
  1386. XEXP (cond2, 1))));
  1387. /* If we return true, we will join the blocks. Which means that
  1388. we will only have one branch prediction bit to work with. Thus
  1389. we require the existing branches to have probabilities that are
  1390. roughly similar. */
  1391. if (match
  1392. && optimize_bb_for_speed_p (bb1)
  1393. && optimize_bb_for_speed_p (bb2))
  1394. {
  1395. int prob2;
  1396. if (b1->dest == b2->dest)
  1397. prob2 = b2->probability;
  1398. else
  1399. /* Do not use f2 probability as f2 may be forwarded. */
  1400. prob2 = REG_BR_PROB_BASE - b2->probability;
  1401. /* Fail if the difference in probabilities is greater than 50%.
  1402. This rules out two well-predicted branches with opposite
  1403. outcomes. */
  1404. if (abs (b1->probability - prob2) > REG_BR_PROB_BASE / 2)
  1405. {
  1406. if (dump_file)
  1407. fprintf (dump_file,
  1408. "Outcomes of branch in bb %i and %i differ too much (%i %i)\n",
  1409. bb1->index, bb2->index, b1->probability, prob2);
  1410. return false;
  1411. }
  1412. }
  1413. if (dump_file && match)
  1414. fprintf (dump_file, "Conditionals in bb %i and %i match.\n",
  1415. bb1->index, bb2->index);
  1416. return match;
  1417. }
  1418. /* Generic case - we are seeing a computed jump, table jump or trapping
  1419. instruction. */
  1420. /* Check whether there are tablejumps in the end of BB1 and BB2.
  1421. Return true if they are identical. */
  1422. {
  1423. rtx label1, label2;
  1424. rtx_jump_table_data *table1, *table2;
  1425. if (tablejump_p (BB_END (bb1), &label1, &table1)
  1426. && tablejump_p (BB_END (bb2), &label2, &table2)
  1427. && GET_CODE (PATTERN (table1)) == GET_CODE (PATTERN (table2)))
  1428. {
  1429. /* The labels should never be the same rtx. If they really are same
  1430. the jump tables are same too. So disable crossjumping of blocks BB1
  1431. and BB2 because when deleting the common insns in the end of BB1
  1432. by delete_basic_block () the jump table would be deleted too. */
  1433. /* If LABEL2 is referenced in BB1->END do not do anything
  1434. because we would loose information when replacing
  1435. LABEL1 by LABEL2 and then LABEL2 by LABEL1 in BB1->END. */
  1436. if (label1 != label2 && !rtx_referenced_p (label2, BB_END (bb1)))
  1437. {
  1438. /* Set IDENTICAL to true when the tables are identical. */
  1439. bool identical = false;
  1440. rtx p1, p2;
  1441. p1 = PATTERN (table1);
  1442. p2 = PATTERN (table2);
  1443. if (GET_CODE (p1) == ADDR_VEC && rtx_equal_p (p1, p2))
  1444. {
  1445. identical = true;
  1446. }
  1447. else if (GET_CODE (p1) == ADDR_DIFF_VEC
  1448. && (XVECLEN (p1, 1) == XVECLEN (p2, 1))
  1449. && rtx_equal_p (XEXP (p1, 2), XEXP (p2, 2))
  1450. && rtx_equal_p (XEXP (p1, 3), XEXP (p2, 3)))
  1451. {
  1452. int i;
  1453. identical = true;
  1454. for (i = XVECLEN (p1, 1) - 1; i >= 0 && identical; i--)
  1455. if (!rtx_equal_p (XVECEXP (p1, 1, i), XVECEXP (p2, 1, i)))
  1456. identical = false;
  1457. }
  1458. if (identical)
  1459. {
  1460. bool match;
  1461. /* Temporarily replace references to LABEL1 with LABEL2
  1462. in BB1->END so that we could compare the instructions. */
  1463. replace_label_in_insn (BB_END (bb1), label1, label2, false);
  1464. match = (old_insns_match_p (mode, BB_END (bb1), BB_END (bb2))
  1465. == dir_both);
  1466. if (dump_file && match)
  1467. fprintf (dump_file,
  1468. "Tablejumps in bb %i and %i match.\n",
  1469. bb1->index, bb2->index);
  1470. /* Set the original label in BB1->END because when deleting
  1471. a block whose end is a tablejump, the tablejump referenced
  1472. from the instruction is deleted too. */
  1473. replace_label_in_insn (BB_END (bb1), label2, label1, false);
  1474. return match;
  1475. }
  1476. }
  1477. return false;
  1478. }
  1479. }
  1480. /* Find the last non-debug non-note instruction in each bb, except
  1481. stop when we see the NOTE_INSN_BASIC_BLOCK, as old_insns_match_p
  1482. handles that case specially. old_insns_match_p does not handle
  1483. other types of instruction notes. */
  1484. rtx_insn *last1 = BB_END (bb1);
  1485. rtx_insn *last2 = BB_END (bb2);
  1486. while (!NOTE_INSN_BASIC_BLOCK_P (last1) &&
  1487. (DEBUG_INSN_P (last1) || NOTE_P (last1)))
  1488. last1 = PREV_INSN (last1);
  1489. while (!NOTE_INSN_BASIC_BLOCK_P (last2) &&
  1490. (DEBUG_INSN_P (last2) || NOTE_P (last2)))
  1491. last2 = PREV_INSN (last2);
  1492. gcc_assert (last1 && last2);
  1493. /* First ensure that the instructions match. There may be many outgoing
  1494. edges so this test is generally cheaper. */
  1495. if (old_insns_match_p (mode, last1, last2) != dir_both)
  1496. return false;
  1497. /* Search the outgoing edges, ensure that the counts do match, find possible
  1498. fallthru and exception handling edges since these needs more
  1499. validation. */
  1500. if (EDGE_COUNT (bb1->succs) != EDGE_COUNT (bb2->succs))
  1501. return false;
  1502. bool nonfakeedges = false;
  1503. FOR_EACH_EDGE (e1, ei, bb1->succs)
  1504. {
  1505. e2 = EDGE_SUCC (bb2, ei.index);
  1506. if ((e1->flags & EDGE_FAKE) == 0)
  1507. nonfakeedges = true;
  1508. if (e1->flags & EDGE_EH)
  1509. nehedges1++;
  1510. if (e2->flags & EDGE_EH)
  1511. nehedges2++;
  1512. if (e1->flags & EDGE_FALLTHRU)
  1513. fallthru1 = e1;
  1514. if (e2->flags & EDGE_FALLTHRU)
  1515. fallthru2 = e2;
  1516. }
  1517. /* If number of edges of various types does not match, fail. */
  1518. if (nehedges1 != nehedges2
  1519. || (fallthru1 != 0) != (fallthru2 != 0))
  1520. return false;
  1521. /* If !ACCUMULATE_OUTGOING_ARGS, bb1 (and bb2) have no successors
  1522. and the last real insn doesn't have REG_ARGS_SIZE note, don't
  1523. attempt to optimize, as the two basic blocks might have different
  1524. REG_ARGS_SIZE depths. For noreturn calls and unconditional
  1525. traps there should be REG_ARG_SIZE notes, they could be missing
  1526. for __builtin_unreachable () uses though. */
  1527. if (!nonfakeedges
  1528. && !ACCUMULATE_OUTGOING_ARGS
  1529. && (!INSN_P (last1)
  1530. || !find_reg_note (last1, REG_ARGS_SIZE, NULL)))
  1531. return false;
  1532. /* fallthru edges must be forwarded to the same destination. */
  1533. if (fallthru1)
  1534. {
  1535. basic_block d1 = (forwarder_block_p (fallthru1->dest)
  1536. ? single_succ (fallthru1->dest): fallthru1->dest);
  1537. basic_block d2 = (forwarder_block_p (fallthru2->dest)
  1538. ? single_succ (fallthru2->dest): fallthru2->dest);
  1539. if (d1 != d2)
  1540. return false;
  1541. }
  1542. /* Ensure the same EH region. */
  1543. {
  1544. rtx n1 = find_reg_note (BB_END (bb1), REG_EH_REGION, 0);
  1545. rtx n2 = find_reg_note (BB_END (bb2), REG_EH_REGION, 0);
  1546. if (!n1 && n2)
  1547. return false;
  1548. if (n1 && (!n2 || XEXP (n1, 0) != XEXP (n2, 0)))
  1549. return false;
  1550. }
  1551. /* The same checks as in try_crossjump_to_edge. It is required for RTL
  1552. version of sequence abstraction. */
  1553. FOR_EACH_EDGE (e1, ei, bb2->succs)
  1554. {
  1555. edge e2;
  1556. edge_iterator ei;
  1557. basic_block d1 = e1->dest;
  1558. if (FORWARDER_BLOCK_P (d1))
  1559. d1 = EDGE_SUCC (d1, 0)->dest;
  1560. FOR_EACH_EDGE (e2, ei, bb1->succs)
  1561. {
  1562. basic_block d2 = e2->dest;
  1563. if (FORWARDER_BLOCK_P (d2))
  1564. d2 = EDGE_SUCC (d2, 0)->dest;
  1565. if (d1 == d2)
  1566. break;
  1567. }
  1568. if (!e2)
  1569. return false;
  1570. }
  1571. return true;
  1572. }
  1573. /* Returns true if BB basic block has a preserve label. */
  1574. static bool
  1575. block_has_preserve_label (basic_block bb)
  1576. {
  1577. return (bb
  1578. && block_label (bb)
  1579. && LABEL_PRESERVE_P (block_label (bb)));
  1580. }
  1581. /* E1 and E2 are edges with the same destination block. Search their
  1582. predecessors for common code. If found, redirect control flow from
  1583. (maybe the middle of) E1->SRC to (maybe the middle of) E2->SRC (dir_forward),
  1584. or the other way around (dir_backward). DIR specifies the allowed
  1585. replacement direction. */
  1586. static bool
  1587. try_crossjump_to_edge (int mode, edge e1, edge e2,
  1588. enum replace_direction dir)
  1589. {
  1590. int nmatch;
  1591. basic_block src1 = e1->src, src2 = e2->src;
  1592. basic_block redirect_to, redirect_from, to_remove;
  1593. basic_block osrc1, osrc2, redirect_edges_to, tmp;
  1594. rtx_insn *newpos1, *newpos2;
  1595. edge s;
  1596. edge_iterator ei;
  1597. newpos1 = newpos2 = NULL;
  1598. /* If we have partitioned hot/cold basic blocks, it is a bad idea
  1599. to try this optimization.
  1600. Basic block partitioning may result in some jumps that appear to
  1601. be optimizable (or blocks that appear to be mergeable), but which really
  1602. must be left untouched (they are required to make it safely across
  1603. partition boundaries). See the comments at the top of
  1604. bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
  1605. if (crtl->has_bb_partition && reload_completed)
  1606. return false;
  1607. /* Search backward through forwarder blocks. We don't need to worry
  1608. about multiple entry or chained forwarders, as they will be optimized
  1609. away. We do this to look past the unconditional jump following a
  1610. conditional jump that is required due to the current CFG shape. */
  1611. if (single_pred_p (src1)
  1612. && FORWARDER_BLOCK_P (src1))
  1613. e1 = single_pred_edge (src1), src1 = e1->src;
  1614. if (single_pred_p (src2)
  1615. && FORWARDER_BLOCK_P (src2))
  1616. e2 = single_pred_edge (src2), src2 = e2->src;
  1617. /* Nothing to do if we reach ENTRY, or a common source block. */
  1618. if (src1 == ENTRY_BLOCK_PTR_FOR_FN (cfun) || src2
  1619. == ENTRY_BLOCK_PTR_FOR_FN (cfun))
  1620. return false;
  1621. if (src1 == src2)
  1622. return false;
  1623. /* Seeing more than 1 forwarder blocks would confuse us later... */
  1624. if (FORWARDER_BLOCK_P (e1->dest)
  1625. && FORWARDER_BLOCK_P (single_succ (e1->dest)))
  1626. return false;
  1627. if (FORWARDER_BLOCK_P (e2->dest)
  1628. && FORWARDER_BLOCK_P (single_succ (e2->dest)))
  1629. return false;
  1630. /* Likewise with dead code (possibly newly created by the other optimizations
  1631. of cfg_cleanup). */
  1632. if (EDGE_COUNT (src1->preds) == 0 || EDGE_COUNT (src2->preds) == 0)
  1633. return false;
  1634. /* Look for the common insn sequence, part the first ... */
  1635. if (!outgoing_edges_match (mode, src1, src2))
  1636. return false;
  1637. /* ... and part the second. */
  1638. nmatch = flow_find_cross_jump (src1, src2, &newpos1, &newpos2, &dir);
  1639. osrc1 = src1;
  1640. osrc2 = src2;
  1641. if (newpos1 != NULL_RTX)
  1642. src1 = BLOCK_FOR_INSN (newpos1);
  1643. if (newpos2 != NULL_RTX)
  1644. src2 = BLOCK_FOR_INSN (newpos2);
  1645. if (dir == dir_backward)
  1646. {
  1647. #define SWAP(T, X, Y) do { T tmp = (X); (X) = (Y); (Y) = tmp; } while (0)
  1648. SWAP (basic_block, osrc1, osrc2);
  1649. SWAP (basic_block, src1, src2);
  1650. SWAP (edge, e1, e2);
  1651. SWAP (rtx_insn *, newpos1, newpos2);
  1652. #undef SWAP
  1653. }
  1654. /* Don't proceed with the crossjump unless we found a sufficient number
  1655. of matching instructions or the 'from' block was totally matched
  1656. (such that its predecessors will hopefully be redirected and the
  1657. block removed). */
  1658. if ((nmatch < PARAM_VALUE (PARAM_MIN_CROSSJUMP_INSNS))
  1659. && (newpos1 != BB_HEAD (src1)))
  1660. return false;
  1661. /* Avoid deleting preserve label when redirecting ABNORMAL edges. */
  1662. if (block_has_preserve_label (e1->dest)
  1663. && (e1->flags & EDGE_ABNORMAL))
  1664. return false;
  1665. /* Here we know that the insns in the end of SRC1 which are common with SRC2
  1666. will be deleted.
  1667. If we have tablejumps in the end of SRC1 and SRC2
  1668. they have been already compared for equivalence in outgoing_edges_match ()
  1669. so replace the references to TABLE1 by references to TABLE2. */
  1670. {
  1671. rtx label1, label2;
  1672. rtx_jump_table_data *table1, *table2;
  1673. if (tablejump_p (BB_END (osrc1), &label1, &table1)
  1674. && tablejump_p (BB_END (osrc2), &label2, &table2)
  1675. && label1 != label2)
  1676. {
  1677. rtx_insn *insn;
  1678. /* Replace references to LABEL1 with LABEL2. */
  1679. for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
  1680. {
  1681. /* Do not replace the label in SRC1->END because when deleting
  1682. a block whose end is a tablejump, the tablejump referenced
  1683. from the instruction is deleted too. */
  1684. if (insn != BB_END (osrc1))
  1685. replace_label_in_insn (insn, label1, label2, true);
  1686. }
  1687. }
  1688. }
  1689. /* Avoid splitting if possible. We must always split when SRC2 has
  1690. EH predecessor edges, or we may end up with basic blocks with both
  1691. normal and EH predecessor edges. */
  1692. if (newpos2 == BB_HEAD (src2)
  1693. && !(EDGE_PRED (src2, 0)->flags & EDGE_EH))
  1694. redirect_to = src2;
  1695. else
  1696. {
  1697. if (newpos2 == BB_HEAD (src2))
  1698. {
  1699. /* Skip possible basic block header. */
  1700. if (LABEL_P (newpos2))
  1701. newpos2 = NEXT_INSN (newpos2);
  1702. while (DEBUG_INSN_P (newpos2))
  1703. newpos2 = NEXT_INSN (newpos2);
  1704. if (NOTE_P (newpos2))
  1705. newpos2 = NEXT_INSN (newpos2);
  1706. while (DEBUG_INSN_P (newpos2))
  1707. newpos2 = NEXT_INSN (newpos2);
  1708. }
  1709. if (dump_file)
  1710. fprintf (dump_file, "Splitting bb %i before %i insns\n",
  1711. src2->index, nmatch);
  1712. redirect_to = split_block (src2, PREV_INSN (newpos2))->dest;
  1713. }
  1714. if (dump_file)
  1715. fprintf (dump_file,
  1716. "Cross jumping from bb %i to bb %i; %i common insns\n",
  1717. src1->index, src2->index, nmatch);
  1718. /* We may have some registers visible through the block. */
  1719. df_set_bb_dirty (redirect_to);
  1720. if (osrc2 == src2)
  1721. redirect_edges_to = redirect_to;
  1722. else
  1723. redirect_edges_to = osrc2;
  1724. /* Recompute the frequencies and counts of outgoing edges. */
  1725. FOR_EACH_EDGE (s, ei, redirect_edges_to->succs)
  1726. {
  1727. edge s2;
  1728. edge_iterator ei;
  1729. basic_block d = s->dest;
  1730. if (FORWARDER_BLOCK_P (d))
  1731. d = single_succ (d);
  1732. FOR_EACH_EDGE (s2, ei, src1->succs)
  1733. {
  1734. basic_block d2 = s2->dest;
  1735. if (FORWARDER_BLOCK_P (d2))
  1736. d2 = single_succ (d2);
  1737. if (d == d2)
  1738. break;
  1739. }
  1740. s->count += s2->count;
  1741. /* Take care to update possible forwarder blocks. We verified
  1742. that there is no more than one in the chain, so we can't run
  1743. into infinite loop. */
  1744. if (FORWARDER_BLOCK_P (s->dest))
  1745. {
  1746. single_succ_edge (s->dest)->count += s2->count;
  1747. s->dest->count += s2->count;
  1748. s->dest->frequency += EDGE_FREQUENCY (s);
  1749. }
  1750. if (FORWARDER_BLOCK_P (s2->dest))
  1751. {
  1752. single_succ_edge (s2->dest)->count -= s2->count;
  1753. if (single_succ_edge (s2->dest)->count < 0)
  1754. single_succ_edge (s2->dest)->count = 0;
  1755. s2->dest->count -= s2->count;
  1756. s2->dest->frequency -= EDGE_FREQUENCY (s);
  1757. if (s2->dest->frequency < 0)
  1758. s2->dest->frequency = 0;
  1759. if (s2->dest->count < 0)
  1760. s2->dest->count = 0;
  1761. }
  1762. if (!redirect_edges_to->frequency && !src1->frequency)
  1763. s->probability = (s->probability + s2->probability) / 2;
  1764. else
  1765. s->probability
  1766. = ((s->probability * redirect_edges_to->frequency +
  1767. s2->probability * src1->frequency)
  1768. / (redirect_edges_to->frequency + src1->frequency));
  1769. }
  1770. /* Adjust count and frequency for the block. An earlier jump
  1771. threading pass may have left the profile in an inconsistent
  1772. state (see update_bb_profile_for_threading) so we must be
  1773. prepared for overflows. */
  1774. tmp = redirect_to;
  1775. do
  1776. {
  1777. tmp->count += src1->count;
  1778. tmp->frequency += src1->frequency;
  1779. if (tmp->frequency > BB_FREQ_MAX)
  1780. tmp->frequency = BB_FREQ_MAX;
  1781. if (tmp == redirect_edges_to)
  1782. break;
  1783. tmp = find_fallthru_edge (tmp->succs)->dest;
  1784. }
  1785. while (true);
  1786. update_br_prob_note (redirect_edges_to);
  1787. /* Edit SRC1 to go to REDIRECT_TO at NEWPOS1. */
  1788. /* Skip possible basic block header. */
  1789. if (LABEL_P (newpos1))
  1790. newpos1 = NEXT_INSN (newpos1);
  1791. while (DEBUG_INSN_P (newpos1))
  1792. newpos1 = NEXT_INSN (newpos1);
  1793. if (NOTE_INSN_BASIC_BLOCK_P (newpos1))
  1794. newpos1 = NEXT_INSN (newpos1);
  1795. while (DEBUG_INSN_P (newpos1))
  1796. newpos1 = NEXT_INSN (newpos1);
  1797. redirect_from = split_block (src1, PREV_INSN (newpos1))->src;
  1798. to_remove = single_succ (redirect_from);
  1799. redirect_edge_and_branch_force (single_succ_edge (redirect_from), redirect_to);
  1800. delete_basic_block (to_remove);
  1801. update_forwarder_flag (redirect_from);
  1802. if (redirect_to != src2)
  1803. update_forwarder_flag (src2);
  1804. return true;
  1805. }
  1806. /* Search the predecessors of BB for common insn sequences. When found,
  1807. share code between them by redirecting control flow. Return true if
  1808. any changes made. */
  1809. static bool
  1810. try_crossjump_bb (int mode, basic_block bb)
  1811. {
  1812. edge e, e2, fallthru;
  1813. bool changed;
  1814. unsigned max, ix, ix2;
  1815. /* Nothing to do if there is not at least two incoming edges. */
  1816. if (EDGE_COUNT (bb->preds) < 2)
  1817. return false;
  1818. /* Don't crossjump if this block ends in a computed jump,
  1819. unless we are optimizing for size. */
  1820. if (optimize_bb_for_size_p (bb)
  1821. && bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
  1822. && computed_jump_p (BB_END (bb)))
  1823. return false;
  1824. /* If we are partitioning hot/cold basic blocks, we don't want to
  1825. mess up unconditional or indirect jumps that cross between hot
  1826. and cold sections.
  1827. Basic block partitioning may result in some jumps that appear to
  1828. be optimizable (or blocks that appear to be mergeable), but which really
  1829. must be left untouched (they are required to make it safely across
  1830. partition boundaries). See the comments at the top of
  1831. bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
  1832. if (BB_PARTITION (EDGE_PRED (bb, 0)->src) !=
  1833. BB_PARTITION (EDGE_PRED (bb, 1)->src)
  1834. || (EDGE_PRED (bb, 0)->flags & EDGE_CROSSING))
  1835. return false;
  1836. /* It is always cheapest to redirect a block that ends in a branch to
  1837. a block that falls through into BB, as that adds no branches to the
  1838. program. We'll try that combination first. */
  1839. fallthru = NULL;
  1840. max = PARAM_VALUE (PARAM_MAX_CROSSJUMP_EDGES);
  1841. if (EDGE_COUNT (bb->preds) > max)
  1842. return false;
  1843. fallthru = find_fallthru_edge (bb->preds);
  1844. changed = false;
  1845. for (ix = 0; ix < EDGE_COUNT (bb->preds);)
  1846. {
  1847. e = EDGE_PRED (bb, ix);
  1848. ix++;
  1849. /* As noted above, first try with the fallthru predecessor (or, a
  1850. fallthru predecessor if we are in cfglayout mode). */
  1851. if (fallthru)
  1852. {
  1853. /* Don't combine the fallthru edge into anything else.
  1854. If there is a match, we'll do it the other way around. */
  1855. if (e == fallthru)
  1856. continue;
  1857. /* If nothing changed since the last attempt, there is nothing
  1858. we can do. */
  1859. if (!first_pass
  1860. && !((e->src->flags & BB_MODIFIED)
  1861. || (fallthru->src->flags & BB_MODIFIED)))
  1862. continue;
  1863. if (try_crossjump_to_edge (mode, e, fallthru, dir_forward))
  1864. {
  1865. changed = true;
  1866. ix = 0;
  1867. continue;
  1868. }
  1869. }
  1870. /* Non-obvious work limiting check: Recognize that we're going
  1871. to call try_crossjump_bb on every basic block. So if we have
  1872. two blocks with lots of outgoing edges (a switch) and they
  1873. share lots of common destinations, then we would do the
  1874. cross-jump check once for each common destination.
  1875. Now, if the blocks actually are cross-jump candidates, then
  1876. all of their destinations will be shared. Which means that
  1877. we only need check them for cross-jump candidacy once. We
  1878. can eliminate redundant checks of crossjump(A,B) by arbitrarily
  1879. choosing to do the check from the block for which the edge
  1880. in question is the first successor of A. */
  1881. if (EDGE_SUCC (e->src, 0) != e)
  1882. continue;
  1883. for (ix2 = 0; ix2 < EDGE_COUNT (bb->preds); ix2++)
  1884. {
  1885. e2 = EDGE_PRED (bb, ix2);
  1886. if (e2 == e)
  1887. continue;
  1888. /* We've already checked the fallthru edge above. */
  1889. if (e2 == fallthru)
  1890. continue;
  1891. /* The "first successor" check above only prevents multiple
  1892. checks of crossjump(A,B). In order to prevent redundant
  1893. checks of crossjump(B,A), require that A be the block
  1894. with the lowest index. */
  1895. if (e->src->index > e2->src->index)
  1896. continue;
  1897. /* If nothing changed since the last attempt, there is nothing
  1898. we can do. */
  1899. if (!first_pass
  1900. && !((e->src->flags & BB_MODIFIED)
  1901. || (e2->src->flags & BB_MODIFIED)))
  1902. continue;
  1903. /* Both e and e2 are not fallthru edges, so we can crossjump in either
  1904. direction. */
  1905. if (try_crossjump_to_edge (mode, e, e2, dir_both))
  1906. {
  1907. changed = true;
  1908. ix = 0;
  1909. break;
  1910. }
  1911. }
  1912. }
  1913. if (changed)
  1914. crossjumps_occured = true;
  1915. return changed;
  1916. }
  1917. /* Search the successors of BB for common insn sequences. When found,
  1918. share code between them by moving it across the basic block
  1919. boundary. Return true if any changes made. */
  1920. static bool
  1921. try_head_merge_bb (basic_block bb)
  1922. {
  1923. basic_block final_dest_bb = NULL;
  1924. int max_match = INT_MAX;
  1925. edge e0;
  1926. rtx_insn **headptr, **currptr, **nextptr;
  1927. bool changed, moveall;
  1928. unsigned ix;
  1929. rtx_insn *e0_last_head;
  1930. rtx cond;
  1931. rtx_insn *move_before;
  1932. unsigned nedges = EDGE_COUNT (bb->succs);
  1933. rtx_insn *jump = BB_END (bb);
  1934. regset live, live_union;
  1935. /* Nothing to do if there is not at least two outgoing edges. */
  1936. if (nedges < 2)
  1937. return false;
  1938. /* Don't crossjump if this block ends in a computed jump,
  1939. unless we are optimizing for size. */
  1940. if (optimize_bb_for_size_p (bb)
  1941. && bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
  1942. && computed_jump_p (BB_END (bb)))
  1943. return false;
  1944. cond = get_condition (jump, &move_before, true, false);
  1945. if (cond == NULL_RTX)
  1946. {
  1947. #ifdef HAVE_cc0
  1948. if (reg_mentioned_p (cc0_rtx, jump))
  1949. move_before = prev_nonnote_nondebug_insn (jump);
  1950. else
  1951. #endif
  1952. move_before = jump;
  1953. }
  1954. for (ix = 0; ix < nedges; ix++)
  1955. if (EDGE_SUCC (bb, ix)->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
  1956. return false;
  1957. for (ix = 0; ix < nedges; ix++)
  1958. {
  1959. edge e = EDGE_SUCC (bb, ix);
  1960. basic_block other_bb = e->dest;
  1961. if (df_get_bb_dirty (other_bb))
  1962. {
  1963. block_was_dirty = true;
  1964. return false;
  1965. }
  1966. if (e->flags & EDGE_ABNORMAL)
  1967. return false;
  1968. /* Normally, all destination blocks must only be reachable from this
  1969. block, i.e. they must have one incoming edge.
  1970. There is one special case we can handle, that of multiple consecutive
  1971. jumps where the first jumps to one of the targets of the second jump.
  1972. This happens frequently in switch statements for default labels.
  1973. The structure is as follows:
  1974. FINAL_DEST_BB
  1975. ....
  1976. if (cond) jump A;
  1977. fall through
  1978. BB
  1979. jump with targets A, B, C, D...
  1980. A
  1981. has two incoming edges, from FINAL_DEST_BB and BB
  1982. In this case, we can try to move the insns through BB and into
  1983. FINAL_DEST_BB. */
  1984. if (EDGE_COUNT (other_bb->preds) != 1)
  1985. {
  1986. edge incoming_edge, incoming_bb_other_edge;
  1987. edge_iterator ei;
  1988. if (final_dest_bb != NULL
  1989. || EDGE_COUNT (other_bb->preds) != 2)
  1990. return false;
  1991. /* We must be able to move the insns across the whole block. */
  1992. move_before = BB_HEAD (bb);
  1993. while (!NONDEBUG_INSN_P (move_before))
  1994. move_before = NEXT_INSN (move_before);
  1995. if (EDGE_COUNT (bb->preds) != 1)
  1996. return false;
  1997. incoming_edge = EDGE_PRED (bb, 0);
  1998. final_dest_bb = incoming_edge->src;
  1999. if (EDGE_COUNT (final_dest_bb->succs) != 2)
  2000. return false;
  2001. FOR_EACH_EDGE (incoming_bb_other_edge, ei, final_dest_bb->succs)
  2002. if (incoming_bb_other_edge != incoming_edge)
  2003. break;
  2004. if (incoming_bb_other_edge->dest != other_bb)
  2005. return false;
  2006. }
  2007. }
  2008. e0 = EDGE_SUCC (bb, 0);
  2009. e0_last_head = NULL;
  2010. changed = false;
  2011. for (ix = 1; ix < nedges; ix++)
  2012. {
  2013. edge e = EDGE_SUCC (bb, ix);
  2014. rtx_insn *e0_last, *e_last;
  2015. int nmatch;
  2016. nmatch = flow_find_head_matching_sequence (e0->dest, e->dest,
  2017. &e0_last, &e_last, 0);
  2018. if (nmatch == 0)
  2019. return false;
  2020. if (nmatch < max_match)
  2021. {
  2022. max_match = nmatch;
  2023. e0_last_head = e0_last;
  2024. }
  2025. }
  2026. /* If we matched an entire block, we probably have to avoid moving the
  2027. last insn. */
  2028. if (max_match > 0
  2029. && e0_last_head == BB_END (e0->dest)
  2030. && (find_reg_note (e0_last_head, REG_EH_REGION, 0)
  2031. || control_flow_insn_p (e0_last_head)))
  2032. {
  2033. max_match--;
  2034. if (max_match == 0)
  2035. return false;
  2036. do
  2037. e0_last_head = prev_real_insn (e0_last_head);
  2038. while (DEBUG_INSN_P (e0_last_head));
  2039. }
  2040. if (max_match == 0)
  2041. return false;
  2042. /* We must find a union of the live registers at each of the end points. */
  2043. live = BITMAP_ALLOC (NULL);
  2044. live_union = BITMAP_ALLOC (NULL);
  2045. currptr = XNEWVEC (rtx_insn *, nedges);
  2046. headptr = XNEWVEC (rtx_insn *, nedges);
  2047. nextptr = XNEWVEC (rtx_insn *, nedges);
  2048. for (ix = 0; ix < nedges; ix++)
  2049. {
  2050. int j;
  2051. basic_block merge_bb = EDGE_SUCC (bb, ix)->dest;
  2052. rtx_insn *head = BB_HEAD (merge_bb);
  2053. while (!NONDEBUG_INSN_P (head))
  2054. head = NEXT_INSN (head);
  2055. headptr[ix] = head;
  2056. currptr[ix] = head;
  2057. /* Compute the end point and live information */
  2058. for (j = 1; j < max_match; j++)
  2059. do
  2060. head = NEXT_INSN (head);
  2061. while (!NONDEBUG_INSN_P (head));
  2062. simulate_backwards_to_point (merge_bb, live, head);
  2063. IOR_REG_SET (live_union, live);
  2064. }
  2065. /* If we're moving across two blocks, verify the validity of the
  2066. first move, then adjust the target and let the loop below deal
  2067. with the final move. */
  2068. if (final_dest_bb != NULL)
  2069. {
  2070. rtx_insn *move_upto;
  2071. moveall = can_move_insns_across (currptr[0], e0_last_head, move_before,
  2072. jump, e0->dest, live_union,
  2073. NULL, &move_upto);
  2074. if (!moveall)
  2075. {
  2076. if (move_upto == NULL_RTX)
  2077. goto out;
  2078. while (e0_last_head != move_upto)
  2079. {
  2080. df_simulate_one_insn_backwards (e0->dest, e0_last_head,
  2081. live_union);
  2082. e0_last_head = PREV_INSN (e0_last_head);
  2083. }
  2084. }
  2085. if (e0_last_head == NULL_RTX)
  2086. goto out;
  2087. jump = BB_END (final_dest_bb);
  2088. cond = get_condition (jump, &move_before, true, false);
  2089. if (cond == NULL_RTX)
  2090. {
  2091. #ifdef HAVE_cc0
  2092. if (reg_mentioned_p (cc0_rtx, jump))
  2093. move_before = prev_nonnote_nondebug_insn (jump);
  2094. else
  2095. #endif
  2096. move_before = jump;
  2097. }
  2098. }
  2099. do
  2100. {
  2101. rtx_insn *move_upto;
  2102. moveall = can_move_insns_across (currptr[0], e0_last_head,
  2103. move_before, jump, e0->dest, live_union,
  2104. NULL, &move_upto);
  2105. if (!moveall && move_upto == NULL_RTX)
  2106. {
  2107. if (jump == move_before)
  2108. break;
  2109. /* Try again, using a different insertion point. */
  2110. move_before = jump;
  2111. #ifdef HAVE_cc0
  2112. /* Don't try moving before a cc0 user, as that may invalidate
  2113. the cc0. */
  2114. if (reg_mentioned_p (cc0_rtx, jump))
  2115. break;
  2116. #endif
  2117. continue;
  2118. }
  2119. if (final_dest_bb && !moveall)
  2120. /* We haven't checked whether a partial move would be OK for the first
  2121. move, so we have to fail this case. */
  2122. break;
  2123. changed = true;
  2124. for (;;)
  2125. {
  2126. if (currptr[0] == move_upto)
  2127. break;
  2128. for (ix = 0; ix < nedges; ix++)
  2129. {
  2130. rtx_insn *curr = currptr[ix];
  2131. do
  2132. curr = NEXT_INSN (curr);
  2133. while (!NONDEBUG_INSN_P (curr));
  2134. currptr[ix] = curr;
  2135. }
  2136. }
  2137. /* If we can't currently move all of the identical insns, remember
  2138. each insn after the range that we'll merge. */
  2139. if (!moveall)
  2140. for (ix = 0; ix < nedges; ix++)
  2141. {
  2142. rtx_insn *curr = currptr[ix];
  2143. do
  2144. curr = NEXT_INSN (curr);
  2145. while (!NONDEBUG_INSN_P (curr));
  2146. nextptr[ix] = curr;
  2147. }
  2148. reorder_insns (headptr[0], currptr[0], PREV_INSN (move_before));
  2149. df_set_bb_dirty (EDGE_SUCC (bb, 0)->dest);
  2150. if (final_dest_bb != NULL)
  2151. df_set_bb_dirty (final_dest_bb);
  2152. df_set_bb_dirty (bb);
  2153. for (ix = 1; ix < nedges; ix++)
  2154. {
  2155. df_set_bb_dirty (EDGE_SUCC (bb, ix)->dest);
  2156. delete_insn_chain (headptr[ix], currptr[ix], false);
  2157. }
  2158. if (!moveall)
  2159. {
  2160. if (jump == move_before)
  2161. break;
  2162. /* For the unmerged insns, try a different insertion point. */
  2163. move_before = jump;
  2164. #ifdef HAVE_cc0
  2165. /* Don't try moving before a cc0 user, as that may invalidate
  2166. the cc0. */
  2167. if (reg_mentioned_p (cc0_rtx, jump))
  2168. break;
  2169. #endif
  2170. for (ix = 0; ix < nedges; ix++)
  2171. currptr[ix] = headptr[ix] = nextptr[ix];
  2172. }
  2173. }
  2174. while (!moveall);
  2175. out:
  2176. free (currptr);
  2177. free (headptr);
  2178. free (nextptr);
  2179. crossjumps_occured |= changed;
  2180. return changed;
  2181. }
  2182. /* Return true if BB contains just bb note, or bb note followed
  2183. by only DEBUG_INSNs. */
  2184. static bool
  2185. trivially_empty_bb_p (basic_block bb)
  2186. {
  2187. rtx_insn *insn = BB_END (bb);
  2188. while (1)
  2189. {
  2190. if (insn == BB_HEAD (bb))
  2191. return true;
  2192. if (!DEBUG_INSN_P (insn))
  2193. return false;
  2194. insn = PREV_INSN (insn);
  2195. }
  2196. }
  2197. /* Do simple CFG optimizations - basic block merging, simplifying of jump
  2198. instructions etc. Return nonzero if changes were made. */
  2199. static bool
  2200. try_optimize_cfg (int mode)
  2201. {
  2202. bool changed_overall = false;
  2203. bool changed;
  2204. int iterations = 0;
  2205. basic_block bb, b, next;
  2206. if (mode & (CLEANUP_CROSSJUMP | CLEANUP_THREADING))
  2207. clear_bb_flags ();
  2208. crossjumps_occured = false;
  2209. FOR_EACH_BB_FN (bb, cfun)
  2210. update_forwarder_flag (bb);
  2211. if (! targetm.cannot_modify_jumps_p ())
  2212. {
  2213. first_pass = true;
  2214. /* Attempt to merge blocks as made possible by edge removal. If
  2215. a block has only one successor, and the successor has only
  2216. one predecessor, they may be combined. */
  2217. do
  2218. {
  2219. block_was_dirty = false;
  2220. changed = false;
  2221. iterations++;
  2222. if (dump_file)
  2223. fprintf (dump_file,
  2224. "\n\ntry_optimize_cfg iteration %i\n\n",
  2225. iterations);
  2226. for (b = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; b
  2227. != EXIT_BLOCK_PTR_FOR_FN (cfun);)
  2228. {
  2229. basic_block c;
  2230. edge s;
  2231. bool changed_here = false;
  2232. /* Delete trivially dead basic blocks. This is either
  2233. blocks with no predecessors, or empty blocks with no
  2234. successors. However if the empty block with no
  2235. successors is the successor of the ENTRY_BLOCK, it is
  2236. kept. This ensures that the ENTRY_BLOCK will have a
  2237. successor which is a precondition for many RTL
  2238. passes. Empty blocks may result from expanding
  2239. __builtin_unreachable (). */
  2240. if (EDGE_COUNT (b->preds) == 0
  2241. || (EDGE_COUNT (b->succs) == 0
  2242. && trivially_empty_bb_p (b)
  2243. && single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun))->dest
  2244. != b))
  2245. {
  2246. c = b->prev_bb;
  2247. if (EDGE_COUNT (b->preds) > 0)
  2248. {
  2249. edge e;
  2250. edge_iterator ei;
  2251. if (current_ir_type () == IR_RTL_CFGLAYOUT)
  2252. {
  2253. if (BB_FOOTER (b)
  2254. && BARRIER_P (BB_FOOTER (b)))
  2255. FOR_EACH_EDGE (e, ei, b->preds)
  2256. if ((e->flags & EDGE_FALLTHRU)
  2257. && BB_FOOTER (e->src) == NULL)
  2258. {
  2259. if (BB_FOOTER (b))
  2260. {
  2261. BB_FOOTER (e->src) = BB_FOOTER (b);
  2262. BB_FOOTER (b) = NULL;
  2263. }
  2264. else
  2265. {
  2266. start_sequence ();
  2267. BB_FOOTER (e->src) = emit_barrier ();
  2268. end_sequence ();
  2269. }
  2270. }
  2271. }
  2272. else
  2273. {
  2274. rtx_insn *last = get_last_bb_insn (b);
  2275. if (last && BARRIER_P (last))
  2276. FOR_EACH_EDGE (e, ei, b->preds)
  2277. if ((e->flags & EDGE_FALLTHRU))
  2278. emit_barrier_after (BB_END (e->src));
  2279. }
  2280. }
  2281. delete_basic_block (b);
  2282. changed = true;
  2283. /* Avoid trying to remove the exit block. */
  2284. b = (c == ENTRY_BLOCK_PTR_FOR_FN (cfun) ? c->next_bb : c);
  2285. continue;
  2286. }
  2287. /* Remove code labels no longer used. */
  2288. if (single_pred_p (b)
  2289. && (single_pred_edge (b)->flags & EDGE_FALLTHRU)
  2290. && !(single_pred_edge (b)->flags & EDGE_COMPLEX)
  2291. && LABEL_P (BB_HEAD (b))
  2292. && !LABEL_PRESERVE_P (BB_HEAD (b))
  2293. /* If the previous block ends with a branch to this
  2294. block, we can't delete the label. Normally this
  2295. is a condjump that is yet to be simplified, but
  2296. if CASE_DROPS_THRU, this can be a tablejump with
  2297. some element going to the same place as the
  2298. default (fallthru). */
  2299. && (single_pred (b) == ENTRY_BLOCK_PTR_FOR_FN (cfun)
  2300. || !JUMP_P (BB_END (single_pred (b)))
  2301. || ! label_is_jump_target_p (BB_HEAD (b),
  2302. BB_END (single_pred (b)))))
  2303. {
  2304. delete_insn (BB_HEAD (b));
  2305. if (dump_file)
  2306. fprintf (dump_file, "Deleted label in block %i.\n",
  2307. b->index);
  2308. }
  2309. /* If we fall through an empty block, we can remove it. */
  2310. if (!(mode & (CLEANUP_CFGLAYOUT | CLEANUP_NO_INSN_DEL))
  2311. && single_pred_p (b)
  2312. && (single_pred_edge (b)->flags & EDGE_FALLTHRU)
  2313. && !LABEL_P (BB_HEAD (b))
  2314. && FORWARDER_BLOCK_P (b)
  2315. /* Note that forwarder_block_p true ensures that
  2316. there is a successor for this block. */
  2317. && (single_succ_edge (b)->flags & EDGE_FALLTHRU)
  2318. && n_basic_blocks_for_fn (cfun) > NUM_FIXED_BLOCKS + 1)
  2319. {
  2320. if (dump_file)
  2321. fprintf (dump_file,
  2322. "Deleting fallthru block %i.\n",
  2323. b->index);
  2324. c = ((b->prev_bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
  2325. ? b->next_bb : b->prev_bb);
  2326. redirect_edge_succ_nodup (single_pred_edge (b),
  2327. single_succ (b));
  2328. delete_basic_block (b);
  2329. changed = true;
  2330. b = c;
  2331. continue;
  2332. }
  2333. /* Merge B with its single successor, if any. */
  2334. if (single_succ_p (b)
  2335. && (s = single_succ_edge (b))
  2336. && !(s->flags & EDGE_COMPLEX)
  2337. && (c = s->dest) != EXIT_BLOCK_PTR_FOR_FN (cfun)
  2338. && single_pred_p (c)
  2339. && b != c)
  2340. {
  2341. /* When not in cfg_layout mode use code aware of reordering
  2342. INSN. This code possibly creates new basic blocks so it
  2343. does not fit merge_blocks interface and is kept here in
  2344. hope that it will become useless once more of compiler
  2345. is transformed to use cfg_layout mode. */
  2346. if ((mode & CLEANUP_CFGLAYOUT)
  2347. && can_merge_blocks_p (b, c))
  2348. {
  2349. merge_blocks (b, c);
  2350. update_forwarder_flag (b);
  2351. changed_here = true;
  2352. }
  2353. else if (!(mode & CLEANUP_CFGLAYOUT)
  2354. /* If the jump insn has side effects,
  2355. we can't kill the edge. */
  2356. && (!JUMP_P (BB_END (b))
  2357. || (reload_completed
  2358. ? simplejump_p (BB_END (b))
  2359. : (onlyjump_p (BB_END (b))
  2360. && !tablejump_p (BB_END (b),
  2361. NULL, NULL))))
  2362. && (next = merge_blocks_move (s, b, c, mode)))
  2363. {
  2364. b = next;
  2365. changed_here = true;
  2366. }
  2367. }
  2368. /* Simplify branch over branch. */
  2369. if ((mode & CLEANUP_EXPENSIVE)
  2370. && !(mode & CLEANUP_CFGLAYOUT)
  2371. && try_simplify_condjump (b))
  2372. changed_here = true;
  2373. /* If B has a single outgoing edge, but uses a
  2374. non-trivial jump instruction without side-effects, we
  2375. can either delete the jump entirely, or replace it
  2376. with a simple unconditional jump. */
  2377. if (single_succ_p (b)
  2378. && single_succ (b) != EXIT_BLOCK_PTR_FOR_FN (cfun)
  2379. && onlyjump_p (BB_END (b))
  2380. && !CROSSING_JUMP_P (BB_END (b))
  2381. && try_redirect_by_replacing_jump (single_succ_edge (b),
  2382. single_succ (b),
  2383. (mode & CLEANUP_CFGLAYOUT) != 0))
  2384. {
  2385. update_forwarder_flag (b);
  2386. changed_here = true;
  2387. }
  2388. /* Simplify branch to branch. */
  2389. if (try_forward_edges (mode, b))
  2390. {
  2391. update_forwarder_flag (b);
  2392. changed_here = true;
  2393. }
  2394. /* Look for shared code between blocks. */
  2395. if ((mode & CLEANUP_CROSSJUMP)
  2396. && try_crossjump_bb (mode, b))
  2397. changed_here = true;
  2398. if ((mode & CLEANUP_CROSSJUMP)
  2399. /* This can lengthen register lifetimes. Do it only after
  2400. reload. */
  2401. && reload_completed
  2402. && try_head_merge_bb (b))
  2403. changed_here = true;
  2404. /* Don't get confused by the index shift caused by
  2405. deleting blocks. */
  2406. if (!changed_here)
  2407. b = b->next_bb;
  2408. else
  2409. changed = true;
  2410. }
  2411. if ((mode & CLEANUP_CROSSJUMP)
  2412. && try_crossjump_bb (mode, EXIT_BLOCK_PTR_FOR_FN (cfun)))
  2413. changed = true;
  2414. if (block_was_dirty)
  2415. {
  2416. /* This should only be set by head-merging. */
  2417. gcc_assert (mode & CLEANUP_CROSSJUMP);
  2418. df_analyze ();
  2419. }
  2420. if (changed)
  2421. {
  2422. /* Edge forwarding in particular can cause hot blocks previously
  2423. reached by both hot and cold blocks to become dominated only
  2424. by cold blocks. This will cause the verification below to fail,
  2425. and lead to now cold code in the hot section. This is not easy
  2426. to detect and fix during edge forwarding, and in some cases
  2427. is only visible after newly unreachable blocks are deleted,
  2428. which will be done in fixup_partitions. */
  2429. fixup_partitions ();
  2430. #ifdef ENABLE_CHECKING
  2431. verify_flow_info ();
  2432. #endif
  2433. }
  2434. changed_overall |= changed;
  2435. first_pass = false;
  2436. }
  2437. while (changed);
  2438. }
  2439. FOR_ALL_BB_FN (b, cfun)
  2440. b->flags &= ~(BB_FORWARDER_BLOCK | BB_NONTHREADABLE_BLOCK);
  2441. return changed_overall;
  2442. }
  2443. /* Delete all unreachable basic blocks. */
  2444. bool
  2445. delete_unreachable_blocks (void)
  2446. {
  2447. bool changed = false;
  2448. basic_block b, prev_bb;
  2449. find_unreachable_blocks ();
  2450. /* When we're in GIMPLE mode and there may be debug insns, we should
  2451. delete blocks in reverse dominator order, so as to get a chance
  2452. to substitute all released DEFs into debug stmts. If we don't
  2453. have dominators information, walking blocks backward gets us a
  2454. better chance of retaining most debug information than
  2455. otherwise. */
  2456. if (MAY_HAVE_DEBUG_INSNS && current_ir_type () == IR_GIMPLE
  2457. && dom_info_available_p (CDI_DOMINATORS))
  2458. {
  2459. for (b = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
  2460. b != ENTRY_BLOCK_PTR_FOR_FN (cfun); b = prev_bb)
  2461. {
  2462. prev_bb = b->prev_bb;
  2463. if (!(b->flags & BB_REACHABLE))
  2464. {
  2465. /* Speed up the removal of blocks that don't dominate
  2466. others. Walking backwards, this should be the common
  2467. case. */
  2468. if (!first_dom_son (CDI_DOMINATORS, b))
  2469. delete_basic_block (b);
  2470. else
  2471. {
  2472. vec<basic_block> h
  2473. = get_all_dominated_blocks (CDI_DOMINATORS, b);
  2474. while (h.length ())
  2475. {
  2476. b = h.pop ();
  2477. prev_bb = b->prev_bb;
  2478. gcc_assert (!(b->flags & BB_REACHABLE));
  2479. delete_basic_block (b);
  2480. }
  2481. h.release ();
  2482. }
  2483. changed = true;
  2484. }
  2485. }
  2486. }
  2487. else
  2488. {
  2489. for (b = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
  2490. b != ENTRY_BLOCK_PTR_FOR_FN (cfun); b = prev_bb)
  2491. {
  2492. prev_bb = b->prev_bb;
  2493. if (!(b->flags & BB_REACHABLE))
  2494. {
  2495. delete_basic_block (b);
  2496. changed = true;
  2497. }
  2498. }
  2499. }
  2500. if (changed)
  2501. tidy_fallthru_edges ();
  2502. return changed;
  2503. }
  2504. /* Delete any jump tables never referenced. We can't delete them at the
  2505. time of removing tablejump insn as they are referenced by the preceding
  2506. insns computing the destination, so we delay deleting and garbagecollect
  2507. them once life information is computed. */
  2508. void
  2509. delete_dead_jumptables (void)
  2510. {
  2511. basic_block bb;
  2512. /* A dead jump table does not belong to any basic block. Scan insns
  2513. between two adjacent basic blocks. */
  2514. FOR_EACH_BB_FN (bb, cfun)
  2515. {
  2516. rtx_insn *insn, *next;
  2517. for (insn = NEXT_INSN (BB_END (bb));
  2518. insn && !NOTE_INSN_BASIC_BLOCK_P (insn);
  2519. insn = next)
  2520. {
  2521. next = NEXT_INSN (insn);
  2522. if (LABEL_P (insn)
  2523. && LABEL_NUSES (insn) == LABEL_PRESERVE_P (insn)
  2524. && JUMP_TABLE_DATA_P (next))
  2525. {
  2526. rtx_insn *label = insn, *jump = next;
  2527. if (dump_file)
  2528. fprintf (dump_file, "Dead jumptable %i removed\n",
  2529. INSN_UID (insn));
  2530. next = NEXT_INSN (next);
  2531. delete_insn (jump);
  2532. delete_insn (label);
  2533. }
  2534. }
  2535. }
  2536. }
  2537. /* Tidy the CFG by deleting unreachable code and whatnot. */
  2538. bool
  2539. cleanup_cfg (int mode)
  2540. {
  2541. bool changed = false;
  2542. /* Set the cfglayout mode flag here. We could update all the callers
  2543. but that is just inconvenient, especially given that we eventually
  2544. want to have cfglayout mode as the default. */
  2545. if (current_ir_type () == IR_RTL_CFGLAYOUT)
  2546. mode |= CLEANUP_CFGLAYOUT;
  2547. timevar_push (TV_CLEANUP_CFG);
  2548. if (delete_unreachable_blocks ())
  2549. {
  2550. changed = true;
  2551. /* We've possibly created trivially dead code. Cleanup it right
  2552. now to introduce more opportunities for try_optimize_cfg. */
  2553. if (!(mode & (CLEANUP_NO_INSN_DEL))
  2554. && !reload_completed)
  2555. delete_trivially_dead_insns (get_insns (), max_reg_num ());
  2556. }
  2557. compact_blocks ();
  2558. /* To tail-merge blocks ending in the same noreturn function (e.g.
  2559. a call to abort) we have to insert fake edges to exit. Do this
  2560. here once. The fake edges do not interfere with any other CFG
  2561. cleanups. */
  2562. if (mode & CLEANUP_CROSSJUMP)
  2563. add_noreturn_fake_exit_edges ();
  2564. if (!dbg_cnt (cfg_cleanup))
  2565. return changed;
  2566. while (try_optimize_cfg (mode))
  2567. {
  2568. delete_unreachable_blocks (), changed = true;
  2569. if (!(mode & CLEANUP_NO_INSN_DEL))
  2570. {
  2571. /* Try to remove some trivially dead insns when doing an expensive
  2572. cleanup. But delete_trivially_dead_insns doesn't work after
  2573. reload (it only handles pseudos) and run_fast_dce is too costly
  2574. to run in every iteration.
  2575. For effective cross jumping, we really want to run a fast DCE to
  2576. clean up any dead conditions, or they get in the way of performing
  2577. useful tail merges.
  2578. Other transformations in cleanup_cfg are not so sensitive to dead
  2579. code, so delete_trivially_dead_insns or even doing nothing at all
  2580. is good enough. */
  2581. if ((mode & CLEANUP_EXPENSIVE) && !reload_completed
  2582. && !delete_trivially_dead_insns (get_insns (), max_reg_num ()))
  2583. break;
  2584. if ((mode & CLEANUP_CROSSJUMP) && crossjumps_occured)
  2585. run_fast_dce ();
  2586. }
  2587. else
  2588. break;
  2589. }
  2590. if (mode & CLEANUP_CROSSJUMP)
  2591. remove_fake_exit_edges ();
  2592. /* Don't call delete_dead_jumptables in cfglayout mode, because
  2593. that function assumes that jump tables are in the insns stream.
  2594. But we also don't _have_ to delete dead jumptables in cfglayout
  2595. mode because we shouldn't even be looking at things that are
  2596. not in a basic block. Dead jumptables are cleaned up when
  2597. going out of cfglayout mode. */
  2598. if (!(mode & CLEANUP_CFGLAYOUT))
  2599. delete_dead_jumptables ();
  2600. /* ??? We probably do this way too often. */
  2601. if (current_loops
  2602. && (changed
  2603. || (mode & CLEANUP_CFG_CHANGED)))
  2604. {
  2605. timevar_push (TV_REPAIR_LOOPS);
  2606. /* The above doesn't preserve dominance info if available. */
  2607. gcc_assert (!dom_info_available_p (CDI_DOMINATORS));
  2608. calculate_dominance_info (CDI_DOMINATORS);
  2609. fix_loop_structure (NULL);
  2610. free_dominance_info (CDI_DOMINATORS);
  2611. timevar_pop (TV_REPAIR_LOOPS);
  2612. }
  2613. timevar_pop (TV_CLEANUP_CFG);
  2614. return changed;
  2615. }
  2616. namespace {
  2617. const pass_data pass_data_jump =
  2618. {
  2619. RTL_PASS, /* type */
  2620. "jump", /* name */
  2621. OPTGROUP_NONE, /* optinfo_flags */
  2622. TV_JUMP, /* tv_id */
  2623. 0, /* properties_required */
  2624. 0, /* properties_provided */
  2625. 0, /* properties_destroyed */
  2626. 0, /* todo_flags_start */
  2627. 0, /* todo_flags_finish */
  2628. };
  2629. class pass_jump : public rtl_opt_pass
  2630. {
  2631. public:
  2632. pass_jump (gcc::context *ctxt)
  2633. : rtl_opt_pass (pass_data_jump, ctxt)
  2634. {}
  2635. /* opt_pass methods: */
  2636. virtual unsigned int execute (function *);
  2637. }; // class pass_jump
  2638. unsigned int
  2639. pass_jump::execute (function *)
  2640. {
  2641. delete_trivially_dead_insns (get_insns (), max_reg_num ());
  2642. if (dump_file)
  2643. dump_flow_info (dump_file, dump_flags);
  2644. cleanup_cfg ((optimize ? CLEANUP_EXPENSIVE : 0)
  2645. | (flag_thread_jumps ? CLEANUP_THREADING : 0));
  2646. return 0;
  2647. }
  2648. } // anon namespace
  2649. rtl_opt_pass *
  2650. make_pass_jump (gcc::context *ctxt)
  2651. {
  2652. return new pass_jump (ctxt);
  2653. }
  2654. namespace {
  2655. const pass_data pass_data_jump2 =
  2656. {
  2657. RTL_PASS, /* type */
  2658. "jump2", /* name */
  2659. OPTGROUP_NONE, /* optinfo_flags */
  2660. TV_JUMP, /* tv_id */
  2661. 0, /* properties_required */
  2662. 0, /* properties_provided */
  2663. 0, /* properties_destroyed */
  2664. 0, /* todo_flags_start */
  2665. 0, /* todo_flags_finish */
  2666. };
  2667. class pass_jump2 : public rtl_opt_pass
  2668. {
  2669. public:
  2670. pass_jump2 (gcc::context *ctxt)
  2671. : rtl_opt_pass (pass_data_jump2, ctxt)
  2672. {}
  2673. /* opt_pass methods: */
  2674. virtual unsigned int execute (function *)
  2675. {
  2676. cleanup_cfg (flag_crossjumping ? CLEANUP_CROSSJUMP : 0);
  2677. return 0;
  2678. }
  2679. }; // class pass_jump2
  2680. } // anon namespace
  2681. rtl_opt_pass *
  2682. make_pass_jump2 (gcc::context *ctxt)
  2683. {
  2684. return new pass_jump2 (ctxt);
  2685. }