bb-reorder.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804
  1. /* Basic block reordering routines for the GNU compiler.
  2. Copyright (C) 2000-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  11. License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. /* This (greedy) algorithm constructs traces in several rounds.
  16. The construction starts from "seeds". The seed for the first round
  17. is the entry point of the function. When there are more than one seed,
  18. the one with the lowest key in the heap is selected first (see bb_to_key).
  19. Then the algorithm repeatedly adds the most probable successor to the end
  20. of a trace. Finally it connects the traces.
  21. There are two parameters: Branch Threshold and Exec Threshold.
  22. If the probability of an edge to a successor of the current basic block is
  23. lower than Branch Threshold or its frequency is lower than Exec Threshold,
  24. then the successor will be the seed in one of the next rounds.
  25. Each round has these parameters lower than the previous one.
  26. The last round has to have these parameters set to zero so that the
  27. remaining blocks are picked up.
  28. The algorithm selects the most probable successor from all unvisited
  29. successors and successors that have been added to this trace.
  30. The other successors (that has not been "sent" to the next round) will be
  31. other seeds for this round and the secondary traces will start from them.
  32. If the successor has not been visited in this trace, it is added to the
  33. trace (however, there is some heuristic for simple branches).
  34. If the successor has been visited in this trace, a loop has been found.
  35. If the loop has many iterations, the loop is rotated so that the source
  36. block of the most probable edge going out of the loop is the last block
  37. of the trace.
  38. If the loop has few iterations and there is no edge from the last block of
  39. the loop going out of the loop, the loop header is duplicated.
  40. When connecting traces, the algorithm first checks whether there is an edge
  41. from the last block of a trace to the first block of another trace.
  42. When there are still some unconnected traces it checks whether there exists
  43. a basic block BB such that BB is a successor of the last block of a trace
  44. and BB is a predecessor of the first block of another trace. In this case,
  45. BB is duplicated, added at the end of the first trace and the traces are
  46. connected through it.
  47. The rest of traces are simply connected so there will be a jump to the
  48. beginning of the rest of traces.
  49. The above description is for the full algorithm, which is used when the
  50. function is optimized for speed. When the function is optimized for size,
  51. in order to reduce long jumps and connect more fallthru edges, the
  52. algorithm is modified as follows:
  53. (1) Break long traces to short ones. A trace is broken at a block that has
  54. multiple predecessors/ successors during trace discovery. When connecting
  55. traces, only connect Trace n with Trace n + 1. This change reduces most
  56. long jumps compared with the above algorithm.
  57. (2) Ignore the edge probability and frequency for fallthru edges.
  58. (3) Keep the original order of blocks when there is no chance to fall
  59. through. We rely on the results of cfg_cleanup.
  60. To implement the change for code size optimization, block's index is
  61. selected as the key and all traces are found in one round.
  62. References:
  63. "Software Trace Cache"
  64. A. Ramirez, J. Larriba-Pey, C. Navarro, J. Torrellas and M. Valero; 1999
  65. http://citeseer.nj.nec.com/15361.html
  66. */
  67. #include "config.h"
  68. #include "system.h"
  69. #include "coretypes.h"
  70. #include "tm.h"
  71. #include "hash-set.h"
  72. #include "machmode.h"
  73. #include "vec.h"
  74. #include "double-int.h"
  75. #include "input.h"
  76. #include "alias.h"
  77. #include "symtab.h"
  78. #include "wide-int.h"
  79. #include "inchash.h"
  80. #include "tree.h"
  81. #include "rtl.h"
  82. #include "regs.h"
  83. #include "flags.h"
  84. #include "output.h"
  85. #include "target.h"
  86. #include "hashtab.h"
  87. #include "hard-reg-set.h"
  88. #include "function.h"
  89. #include "tm_p.h"
  90. #include "obstack.h"
  91. #include "statistics.h"
  92. #include "real.h"
  93. #include "fixed-value.h"
  94. #include "insn-config.h"
  95. #include "expmed.h"
  96. #include "dojump.h"
  97. #include "explow.h"
  98. #include "calls.h"
  99. #include "emit-rtl.h"
  100. #include "varasm.h"
  101. #include "stmt.h"
  102. #include "expr.h"
  103. #include "optabs.h"
  104. #include "params.h"
  105. #include "diagnostic-core.h"
  106. #include "toplev.h" /* user_defined_section_attribute */
  107. #include "tree-pass.h"
  108. #include "dominance.h"
  109. #include "cfg.h"
  110. #include "cfgrtl.h"
  111. #include "cfganal.h"
  112. #include "cfgbuild.h"
  113. #include "cfgcleanup.h"
  114. #include "predict.h"
  115. #include "basic-block.h"
  116. #include "df.h"
  117. #include "bb-reorder.h"
  118. #include "hash-map.h"
  119. #include "is-a.h"
  120. #include "plugin-api.h"
  121. #include "ipa-ref.h"
  122. #include "cgraph.h"
  123. #include "except.h"
  124. #include "fibonacci_heap.h"
  125. /* The number of rounds. In most cases there will only be 4 rounds, but
  126. when partitioning hot and cold basic blocks into separate sections of
  127. the object file there will be an extra round. */
  128. #define N_ROUNDS 5
  129. /* Stubs in case we don't have a return insn.
  130. We have to check at run time too, not only compile time. */
  131. #ifndef HAVE_return
  132. #define HAVE_return 0
  133. #define gen_return() NULL_RTX
  134. #endif
  135. struct target_bb_reorder default_target_bb_reorder;
  136. #if SWITCHABLE_TARGET
  137. struct target_bb_reorder *this_target_bb_reorder = &default_target_bb_reorder;
  138. #endif
  139. #define uncond_jump_length \
  140. (this_target_bb_reorder->x_uncond_jump_length)
  141. /* Branch thresholds in thousandths (per mille) of the REG_BR_PROB_BASE. */
  142. static const int branch_threshold[N_ROUNDS] = {400, 200, 100, 0, 0};
  143. /* Exec thresholds in thousandths (per mille) of the frequency of bb 0. */
  144. static const int exec_threshold[N_ROUNDS] = {500, 200, 50, 0, 0};
  145. /* If edge frequency is lower than DUPLICATION_THRESHOLD per mille of entry
  146. block the edge destination is not duplicated while connecting traces. */
  147. #define DUPLICATION_THRESHOLD 100
  148. typedef fibonacci_heap <long, basic_block_def> bb_heap_t;
  149. typedef fibonacci_node <long, basic_block_def> bb_heap_node_t;
  150. /* Structure to hold needed information for each basic block. */
  151. typedef struct bbro_basic_block_data_def
  152. {
  153. /* Which trace is the bb start of (-1 means it is not a start of any). */
  154. int start_of_trace;
  155. /* Which trace is the bb end of (-1 means it is not an end of any). */
  156. int end_of_trace;
  157. /* Which trace is the bb in? */
  158. int in_trace;
  159. /* Which trace was this bb visited in? */
  160. int visited;
  161. /* Which heap is BB in (if any)? */
  162. bb_heap_t *heap;
  163. /* Which heap node is BB in (if any)? */
  164. bb_heap_node_t *node;
  165. } bbro_basic_block_data;
  166. /* The current size of the following dynamic array. */
  167. static int array_size;
  168. /* The array which holds needed information for basic blocks. */
  169. static bbro_basic_block_data *bbd;
  170. /* To avoid frequent reallocation the size of arrays is greater than needed,
  171. the number of elements is (not less than) 1.25 * size_wanted. */
  172. #define GET_ARRAY_SIZE(X) ((((X) / 4) + 1) * 5)
  173. /* Free the memory and set the pointer to NULL. */
  174. #define FREE(P) (gcc_assert (P), free (P), P = 0)
  175. /* Structure for holding information about a trace. */
  176. struct trace
  177. {
  178. /* First and last basic block of the trace. */
  179. basic_block first, last;
  180. /* The round of the STC creation which this trace was found in. */
  181. int round;
  182. /* The length (i.e. the number of basic blocks) of the trace. */
  183. int length;
  184. };
  185. /* Maximum frequency and count of one of the entry blocks. */
  186. static int max_entry_frequency;
  187. static gcov_type max_entry_count;
  188. /* Local function prototypes. */
  189. static void find_traces (int *, struct trace *);
  190. static basic_block rotate_loop (edge, struct trace *, int);
  191. static void mark_bb_visited (basic_block, int);
  192. static void find_traces_1_round (int, int, gcov_type, struct trace *, int *,
  193. int, bb_heap_t **, int);
  194. static basic_block copy_bb (basic_block, edge, basic_block, int);
  195. static long bb_to_key (basic_block);
  196. static bool better_edge_p (const_basic_block, const_edge, int, int, int, int,
  197. const_edge);
  198. static bool connect_better_edge_p (const_edge, bool, int, const_edge,
  199. struct trace *);
  200. static void connect_traces (int, struct trace *);
  201. static bool copy_bb_p (const_basic_block, int);
  202. static bool push_to_next_round_p (const_basic_block, int, int, int, gcov_type);
  203. /* Return the trace number in which BB was visited. */
  204. static int
  205. bb_visited_trace (const_basic_block bb)
  206. {
  207. gcc_assert (bb->index < array_size);
  208. return bbd[bb->index].visited;
  209. }
  210. /* This function marks BB that it was visited in trace number TRACE. */
  211. static void
  212. mark_bb_visited (basic_block bb, int trace)
  213. {
  214. bbd[bb->index].visited = trace;
  215. if (bbd[bb->index].heap)
  216. {
  217. bbd[bb->index].heap->delete_node (bbd[bb->index].node);
  218. bbd[bb->index].heap = NULL;
  219. bbd[bb->index].node = NULL;
  220. }
  221. }
  222. /* Check to see if bb should be pushed into the next round of trace
  223. collections or not. Reasons for pushing the block forward are 1).
  224. If the block is cold, we are doing partitioning, and there will be
  225. another round (cold partition blocks are not supposed to be
  226. collected into traces until the very last round); or 2). There will
  227. be another round, and the basic block is not "hot enough" for the
  228. current round of trace collection. */
  229. static bool
  230. push_to_next_round_p (const_basic_block bb, int round, int number_of_rounds,
  231. int exec_th, gcov_type count_th)
  232. {
  233. bool there_exists_another_round;
  234. bool block_not_hot_enough;
  235. there_exists_another_round = round < number_of_rounds - 1;
  236. block_not_hot_enough = (bb->frequency < exec_th
  237. || bb->count < count_th
  238. || probably_never_executed_bb_p (cfun, bb));
  239. if (there_exists_another_round
  240. && block_not_hot_enough)
  241. return true;
  242. else
  243. return false;
  244. }
  245. /* Find the traces for Software Trace Cache. Chain each trace through
  246. RBI()->next. Store the number of traces to N_TRACES and description of
  247. traces to TRACES. */
  248. static void
  249. find_traces (int *n_traces, struct trace *traces)
  250. {
  251. int i;
  252. int number_of_rounds;
  253. edge e;
  254. edge_iterator ei;
  255. bb_heap_t *heap = new bb_heap_t (LONG_MIN);
  256. /* Add one extra round of trace collection when partitioning hot/cold
  257. basic blocks into separate sections. The last round is for all the
  258. cold blocks (and ONLY the cold blocks). */
  259. number_of_rounds = N_ROUNDS - 1;
  260. /* Insert entry points of function into heap. */
  261. max_entry_frequency = 0;
  262. max_entry_count = 0;
  263. FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
  264. {
  265. bbd[e->dest->index].heap = heap;
  266. bbd[e->dest->index].node = heap->insert (bb_to_key (e->dest), e->dest);
  267. if (e->dest->frequency > max_entry_frequency)
  268. max_entry_frequency = e->dest->frequency;
  269. if (e->dest->count > max_entry_count)
  270. max_entry_count = e->dest->count;
  271. }
  272. /* Find the traces. */
  273. for (i = 0; i < number_of_rounds; i++)
  274. {
  275. gcov_type count_threshold;
  276. if (dump_file)
  277. fprintf (dump_file, "STC - round %d\n", i + 1);
  278. if (max_entry_count < INT_MAX / 1000)
  279. count_threshold = max_entry_count * exec_threshold[i] / 1000;
  280. else
  281. count_threshold = max_entry_count / 1000 * exec_threshold[i];
  282. find_traces_1_round (REG_BR_PROB_BASE * branch_threshold[i] / 1000,
  283. max_entry_frequency * exec_threshold[i] / 1000,
  284. count_threshold, traces, n_traces, i, &heap,
  285. number_of_rounds);
  286. }
  287. delete heap;
  288. if (dump_file)
  289. {
  290. for (i = 0; i < *n_traces; i++)
  291. {
  292. basic_block bb;
  293. fprintf (dump_file, "Trace %d (round %d): ", i + 1,
  294. traces[i].round + 1);
  295. for (bb = traces[i].first;
  296. bb != traces[i].last;
  297. bb = (basic_block) bb->aux)
  298. fprintf (dump_file, "%d [%d] ", bb->index, bb->frequency);
  299. fprintf (dump_file, "%d [%d]\n", bb->index, bb->frequency);
  300. }
  301. fflush (dump_file);
  302. }
  303. }
  304. /* Rotate loop whose back edge is BACK_EDGE in the tail of trace TRACE
  305. (with sequential number TRACE_N). */
  306. static basic_block
  307. rotate_loop (edge back_edge, struct trace *trace, int trace_n)
  308. {
  309. basic_block bb;
  310. /* Information about the best end (end after rotation) of the loop. */
  311. basic_block best_bb = NULL;
  312. edge best_edge = NULL;
  313. int best_freq = -1;
  314. gcov_type best_count = -1;
  315. /* The best edge is preferred when its destination is not visited yet
  316. or is a start block of some trace. */
  317. bool is_preferred = false;
  318. /* Find the most frequent edge that goes out from current trace. */
  319. bb = back_edge->dest;
  320. do
  321. {
  322. edge e;
  323. edge_iterator ei;
  324. FOR_EACH_EDGE (e, ei, bb->succs)
  325. if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
  326. && bb_visited_trace (e->dest) != trace_n
  327. && (e->flags & EDGE_CAN_FALLTHRU)
  328. && !(e->flags & EDGE_COMPLEX))
  329. {
  330. if (is_preferred)
  331. {
  332. /* The best edge is preferred. */
  333. if (!bb_visited_trace (e->dest)
  334. || bbd[e->dest->index].start_of_trace >= 0)
  335. {
  336. /* The current edge E is also preferred. */
  337. int freq = EDGE_FREQUENCY (e);
  338. if (freq > best_freq || e->count > best_count)
  339. {
  340. best_freq = freq;
  341. best_count = e->count;
  342. best_edge = e;
  343. best_bb = bb;
  344. }
  345. }
  346. }
  347. else
  348. {
  349. if (!bb_visited_trace (e->dest)
  350. || bbd[e->dest->index].start_of_trace >= 0)
  351. {
  352. /* The current edge E is preferred. */
  353. is_preferred = true;
  354. best_freq = EDGE_FREQUENCY (e);
  355. best_count = e->count;
  356. best_edge = e;
  357. best_bb = bb;
  358. }
  359. else
  360. {
  361. int freq = EDGE_FREQUENCY (e);
  362. if (!best_edge || freq > best_freq || e->count > best_count)
  363. {
  364. best_freq = freq;
  365. best_count = e->count;
  366. best_edge = e;
  367. best_bb = bb;
  368. }
  369. }
  370. }
  371. }
  372. bb = (basic_block) bb->aux;
  373. }
  374. while (bb != back_edge->dest);
  375. if (best_bb)
  376. {
  377. /* Rotate the loop so that the BEST_EDGE goes out from the last block of
  378. the trace. */
  379. if (back_edge->dest == trace->first)
  380. {
  381. trace->first = (basic_block) best_bb->aux;
  382. }
  383. else
  384. {
  385. basic_block prev_bb;
  386. for (prev_bb = trace->first;
  387. prev_bb->aux != back_edge->dest;
  388. prev_bb = (basic_block) prev_bb->aux)
  389. ;
  390. prev_bb->aux = best_bb->aux;
  391. /* Try to get rid of uncond jump to cond jump. */
  392. if (single_succ_p (prev_bb))
  393. {
  394. basic_block header = single_succ (prev_bb);
  395. /* Duplicate HEADER if it is a small block containing cond jump
  396. in the end. */
  397. if (any_condjump_p (BB_END (header)) && copy_bb_p (header, 0)
  398. && !CROSSING_JUMP_P (BB_END (header)))
  399. copy_bb (header, single_succ_edge (prev_bb), prev_bb, trace_n);
  400. }
  401. }
  402. }
  403. else
  404. {
  405. /* We have not found suitable loop tail so do no rotation. */
  406. best_bb = back_edge->src;
  407. }
  408. best_bb->aux = NULL;
  409. return best_bb;
  410. }
  411. /* One round of finding traces. Find traces for BRANCH_TH and EXEC_TH i.e. do
  412. not include basic blocks whose probability is lower than BRANCH_TH or whose
  413. frequency is lower than EXEC_TH into traces (or whose count is lower than
  414. COUNT_TH). Store the new traces into TRACES and modify the number of
  415. traces *N_TRACES. Set the round (which the trace belongs to) to ROUND.
  416. The function expects starting basic blocks to be in *HEAP and will delete
  417. *HEAP and store starting points for the next round into new *HEAP. */
  418. static void
  419. find_traces_1_round (int branch_th, int exec_th, gcov_type count_th,
  420. struct trace *traces, int *n_traces, int round,
  421. bb_heap_t **heap, int number_of_rounds)
  422. {
  423. /* Heap for discarded basic blocks which are possible starting points for
  424. the next round. */
  425. bb_heap_t *new_heap = new bb_heap_t (LONG_MIN);
  426. bool for_size = optimize_function_for_size_p (cfun);
  427. while (!(*heap)->empty ())
  428. {
  429. basic_block bb;
  430. struct trace *trace;
  431. edge best_edge, e;
  432. long key;
  433. edge_iterator ei;
  434. bb = (*heap)->extract_min ();
  435. bbd[bb->index].heap = NULL;
  436. bbd[bb->index].node = NULL;
  437. if (dump_file)
  438. fprintf (dump_file, "Getting bb %d\n", bb->index);
  439. /* If the BB's frequency is too low, send BB to the next round. When
  440. partitioning hot/cold blocks into separate sections, make sure all
  441. the cold blocks (and ONLY the cold blocks) go into the (extra) final
  442. round. When optimizing for size, do not push to next round. */
  443. if (!for_size
  444. && push_to_next_round_p (bb, round, number_of_rounds, exec_th,
  445. count_th))
  446. {
  447. int key = bb_to_key (bb);
  448. bbd[bb->index].heap = new_heap;
  449. bbd[bb->index].node = new_heap->insert (key, bb);
  450. if (dump_file)
  451. fprintf (dump_file,
  452. " Possible start point of next round: %d (key: %d)\n",
  453. bb->index, key);
  454. continue;
  455. }
  456. trace = traces + *n_traces;
  457. trace->first = bb;
  458. trace->round = round;
  459. trace->length = 0;
  460. bbd[bb->index].in_trace = *n_traces;
  461. (*n_traces)++;
  462. do
  463. {
  464. int prob, freq;
  465. bool ends_in_call;
  466. /* The probability and frequency of the best edge. */
  467. int best_prob = INT_MIN / 2;
  468. int best_freq = INT_MIN / 2;
  469. best_edge = NULL;
  470. mark_bb_visited (bb, *n_traces);
  471. trace->length++;
  472. if (dump_file)
  473. fprintf (dump_file, "Basic block %d was visited in trace %d\n",
  474. bb->index, *n_traces - 1);
  475. ends_in_call = block_ends_with_call_p (bb);
  476. /* Select the successor that will be placed after BB. */
  477. FOR_EACH_EDGE (e, ei, bb->succs)
  478. {
  479. gcc_assert (!(e->flags & EDGE_FAKE));
  480. if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
  481. continue;
  482. if (bb_visited_trace (e->dest)
  483. && bb_visited_trace (e->dest) != *n_traces)
  484. continue;
  485. if (BB_PARTITION (e->dest) != BB_PARTITION (bb))
  486. continue;
  487. prob = e->probability;
  488. freq = e->dest->frequency;
  489. /* The only sensible preference for a call instruction is the
  490. fallthru edge. Don't bother selecting anything else. */
  491. if (ends_in_call)
  492. {
  493. if (e->flags & EDGE_CAN_FALLTHRU)
  494. {
  495. best_edge = e;
  496. best_prob = prob;
  497. best_freq = freq;
  498. }
  499. continue;
  500. }
  501. /* Edge that cannot be fallthru or improbable or infrequent
  502. successor (i.e. it is unsuitable successor). When optimizing
  503. for size, ignore the probability and frequency. */
  504. if (!(e->flags & EDGE_CAN_FALLTHRU) || (e->flags & EDGE_COMPLEX)
  505. || ((prob < branch_th || EDGE_FREQUENCY (e) < exec_th
  506. || e->count < count_th) && (!for_size)))
  507. continue;
  508. /* If partitioning hot/cold basic blocks, don't consider edges
  509. that cross section boundaries. */
  510. if (better_edge_p (bb, e, prob, freq, best_prob, best_freq,
  511. best_edge))
  512. {
  513. best_edge = e;
  514. best_prob = prob;
  515. best_freq = freq;
  516. }
  517. }
  518. /* If the best destination has multiple predecessors, and can be
  519. duplicated cheaper than a jump, don't allow it to be added
  520. to a trace. We'll duplicate it when connecting traces. */
  521. if (best_edge && EDGE_COUNT (best_edge->dest->preds) >= 2
  522. && copy_bb_p (best_edge->dest, 0))
  523. best_edge = NULL;
  524. /* If the best destination has multiple successors or predecessors,
  525. don't allow it to be added when optimizing for size. This makes
  526. sure predecessors with smaller index are handled before the best
  527. destinarion. It breaks long trace and reduces long jumps.
  528. Take if-then-else as an example.
  529. A
  530. / \
  531. B C
  532. \ /
  533. D
  534. If we do not remove the best edge B->D/C->D, the final order might
  535. be A B D ... C. C is at the end of the program. If D's successors
  536. and D are complicated, might need long jumps for A->C and C->D.
  537. Similar issue for order: A C D ... B.
  538. After removing the best edge, the final result will be ABCD/ ACBD.
  539. It does not add jump compared with the previous order. But it
  540. reduces the possibility of long jumps. */
  541. if (best_edge && for_size
  542. && (EDGE_COUNT (best_edge->dest->succs) > 1
  543. || EDGE_COUNT (best_edge->dest->preds) > 1))
  544. best_edge = NULL;
  545. /* Add all non-selected successors to the heaps. */
  546. FOR_EACH_EDGE (e, ei, bb->succs)
  547. {
  548. if (e == best_edge
  549. || e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)
  550. || bb_visited_trace (e->dest))
  551. continue;
  552. key = bb_to_key (e->dest);
  553. if (bbd[e->dest->index].heap)
  554. {
  555. /* E->DEST is already in some heap. */
  556. if (key != bbd[e->dest->index].node->get_key ())
  557. {
  558. if (dump_file)
  559. {
  560. fprintf (dump_file,
  561. "Changing key for bb %d from %ld to %ld.\n",
  562. e->dest->index,
  563. (long) bbd[e->dest->index].node->get_key (),
  564. key);
  565. }
  566. bbd[e->dest->index].heap->replace_key
  567. (bbd[e->dest->index].node, key);
  568. }
  569. }
  570. else
  571. {
  572. bb_heap_t *which_heap = *heap;
  573. prob = e->probability;
  574. freq = EDGE_FREQUENCY (e);
  575. if (!(e->flags & EDGE_CAN_FALLTHRU)
  576. || (e->flags & EDGE_COMPLEX)
  577. || prob < branch_th || freq < exec_th
  578. || e->count < count_th)
  579. {
  580. /* When partitioning hot/cold basic blocks, make sure
  581. the cold blocks (and only the cold blocks) all get
  582. pushed to the last round of trace collection. When
  583. optimizing for size, do not push to next round. */
  584. if (!for_size && push_to_next_round_p (e->dest, round,
  585. number_of_rounds,
  586. exec_th, count_th))
  587. which_heap = new_heap;
  588. }
  589. bbd[e->dest->index].heap = which_heap;
  590. bbd[e->dest->index].node = which_heap->insert (key, e->dest);
  591. if (dump_file)
  592. {
  593. fprintf (dump_file,
  594. " Possible start of %s round: %d (key: %ld)\n",
  595. (which_heap == new_heap) ? "next" : "this",
  596. e->dest->index, (long) key);
  597. }
  598. }
  599. }
  600. if (best_edge) /* Suitable successor was found. */
  601. {
  602. if (bb_visited_trace (best_edge->dest) == *n_traces)
  603. {
  604. /* We do nothing with one basic block loops. */
  605. if (best_edge->dest != bb)
  606. {
  607. if (EDGE_FREQUENCY (best_edge)
  608. > 4 * best_edge->dest->frequency / 5)
  609. {
  610. /* The loop has at least 4 iterations. If the loop
  611. header is not the first block of the function
  612. we can rotate the loop. */
  613. if (best_edge->dest
  614. != ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
  615. {
  616. if (dump_file)
  617. {
  618. fprintf (dump_file,
  619. "Rotating loop %d - %d\n",
  620. best_edge->dest->index, bb->index);
  621. }
  622. bb->aux = best_edge->dest;
  623. bbd[best_edge->dest->index].in_trace =
  624. (*n_traces) - 1;
  625. bb = rotate_loop (best_edge, trace, *n_traces);
  626. }
  627. }
  628. else
  629. {
  630. /* The loop has less than 4 iterations. */
  631. if (single_succ_p (bb)
  632. && copy_bb_p (best_edge->dest,
  633. optimize_edge_for_speed_p
  634. (best_edge)))
  635. {
  636. bb = copy_bb (best_edge->dest, best_edge, bb,
  637. *n_traces);
  638. trace->length++;
  639. }
  640. }
  641. }
  642. /* Terminate the trace. */
  643. break;
  644. }
  645. else
  646. {
  647. /* Check for a situation
  648. A
  649. /|
  650. B |
  651. \|
  652. C
  653. where
  654. EDGE_FREQUENCY (AB) + EDGE_FREQUENCY (BC)
  655. >= EDGE_FREQUENCY (AC).
  656. (i.e. 2 * B->frequency >= EDGE_FREQUENCY (AC) )
  657. Best ordering is then A B C.
  658. When optimizing for size, A B C is always the best order.
  659. This situation is created for example by:
  660. if (A) B;
  661. C;
  662. */
  663. FOR_EACH_EDGE (e, ei, bb->succs)
  664. if (e != best_edge
  665. && (e->flags & EDGE_CAN_FALLTHRU)
  666. && !(e->flags & EDGE_COMPLEX)
  667. && !bb_visited_trace (e->dest)
  668. && single_pred_p (e->dest)
  669. && !(e->flags & EDGE_CROSSING)
  670. && single_succ_p (e->dest)
  671. && (single_succ_edge (e->dest)->flags
  672. & EDGE_CAN_FALLTHRU)
  673. && !(single_succ_edge (e->dest)->flags & EDGE_COMPLEX)
  674. && single_succ (e->dest) == best_edge->dest
  675. && (2 * e->dest->frequency >= EDGE_FREQUENCY (best_edge)
  676. || for_size))
  677. {
  678. best_edge = e;
  679. if (dump_file)
  680. fprintf (dump_file, "Selecting BB %d\n",
  681. best_edge->dest->index);
  682. break;
  683. }
  684. bb->aux = best_edge->dest;
  685. bbd[best_edge->dest->index].in_trace = (*n_traces) - 1;
  686. bb = best_edge->dest;
  687. }
  688. }
  689. }
  690. while (best_edge);
  691. trace->last = bb;
  692. bbd[trace->first->index].start_of_trace = *n_traces - 1;
  693. bbd[trace->last->index].end_of_trace = *n_traces - 1;
  694. /* The trace is terminated so we have to recount the keys in heap
  695. (some block can have a lower key because now one of its predecessors
  696. is an end of the trace). */
  697. FOR_EACH_EDGE (e, ei, bb->succs)
  698. {
  699. if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)
  700. || bb_visited_trace (e->dest))
  701. continue;
  702. if (bbd[e->dest->index].heap)
  703. {
  704. key = bb_to_key (e->dest);
  705. if (key != bbd[e->dest->index].node->get_key ())
  706. {
  707. if (dump_file)
  708. {
  709. fprintf (dump_file,
  710. "Changing key for bb %d from %ld to %ld.\n",
  711. e->dest->index,
  712. (long) bbd[e->dest->index].node->get_key (), key);
  713. }
  714. bbd[e->dest->index].heap->replace_key
  715. (bbd[e->dest->index].node, key);
  716. }
  717. }
  718. }
  719. }
  720. delete (*heap);
  721. /* "Return" the new heap. */
  722. *heap = new_heap;
  723. }
  724. /* Create a duplicate of the basic block OLD_BB and redirect edge E to it, add
  725. it to trace after BB, mark OLD_BB visited and update pass' data structures
  726. (TRACE is a number of trace which OLD_BB is duplicated to). */
  727. static basic_block
  728. copy_bb (basic_block old_bb, edge e, basic_block bb, int trace)
  729. {
  730. basic_block new_bb;
  731. new_bb = duplicate_block (old_bb, e, bb);
  732. BB_COPY_PARTITION (new_bb, old_bb);
  733. gcc_assert (e->dest == new_bb);
  734. if (dump_file)
  735. fprintf (dump_file,
  736. "Duplicated bb %d (created bb %d)\n",
  737. old_bb->index, new_bb->index);
  738. if (new_bb->index >= array_size
  739. || last_basic_block_for_fn (cfun) > array_size)
  740. {
  741. int i;
  742. int new_size;
  743. new_size = MAX (last_basic_block_for_fn (cfun), new_bb->index + 1);
  744. new_size = GET_ARRAY_SIZE (new_size);
  745. bbd = XRESIZEVEC (bbro_basic_block_data, bbd, new_size);
  746. for (i = array_size; i < new_size; i++)
  747. {
  748. bbd[i].start_of_trace = -1;
  749. bbd[i].end_of_trace = -1;
  750. bbd[i].in_trace = -1;
  751. bbd[i].visited = 0;
  752. bbd[i].heap = NULL;
  753. bbd[i].node = NULL;
  754. }
  755. array_size = new_size;
  756. if (dump_file)
  757. {
  758. fprintf (dump_file,
  759. "Growing the dynamic array to %d elements.\n",
  760. array_size);
  761. }
  762. }
  763. gcc_assert (!bb_visited_trace (e->dest));
  764. mark_bb_visited (new_bb, trace);
  765. new_bb->aux = bb->aux;
  766. bb->aux = new_bb;
  767. bbd[new_bb->index].in_trace = trace;
  768. return new_bb;
  769. }
  770. /* Compute and return the key (for the heap) of the basic block BB. */
  771. static long
  772. bb_to_key (basic_block bb)
  773. {
  774. edge e;
  775. edge_iterator ei;
  776. int priority = 0;
  777. /* Use index as key to align with its original order. */
  778. if (optimize_function_for_size_p (cfun))
  779. return bb->index;
  780. /* Do not start in probably never executed blocks. */
  781. if (BB_PARTITION (bb) == BB_COLD_PARTITION
  782. || probably_never_executed_bb_p (cfun, bb))
  783. return BB_FREQ_MAX;
  784. /* Prefer blocks whose predecessor is an end of some trace
  785. or whose predecessor edge is EDGE_DFS_BACK. */
  786. FOR_EACH_EDGE (e, ei, bb->preds)
  787. {
  788. if ((e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
  789. && bbd[e->src->index].end_of_trace >= 0)
  790. || (e->flags & EDGE_DFS_BACK))
  791. {
  792. int edge_freq = EDGE_FREQUENCY (e);
  793. if (edge_freq > priority)
  794. priority = edge_freq;
  795. }
  796. }
  797. if (priority)
  798. /* The block with priority should have significantly lower key. */
  799. return -(100 * BB_FREQ_MAX + 100 * priority + bb->frequency);
  800. return -bb->frequency;
  801. }
  802. /* Return true when the edge E from basic block BB is better than the temporary
  803. best edge (details are in function). The probability of edge E is PROB. The
  804. frequency of the successor is FREQ. The current best probability is
  805. BEST_PROB, the best frequency is BEST_FREQ.
  806. The edge is considered to be equivalent when PROB does not differ much from
  807. BEST_PROB; similarly for frequency. */
  808. static bool
  809. better_edge_p (const_basic_block bb, const_edge e, int prob, int freq,
  810. int best_prob, int best_freq, const_edge cur_best_edge)
  811. {
  812. bool is_better_edge;
  813. /* The BEST_* values do not have to be best, but can be a bit smaller than
  814. maximum values. */
  815. int diff_prob = best_prob / 10;
  816. int diff_freq = best_freq / 10;
  817. /* The smaller one is better to keep the original order. */
  818. if (optimize_function_for_size_p (cfun))
  819. return !cur_best_edge
  820. || cur_best_edge->dest->index > e->dest->index;
  821. if (prob > best_prob + diff_prob)
  822. /* The edge has higher probability than the temporary best edge. */
  823. is_better_edge = true;
  824. else if (prob < best_prob - diff_prob)
  825. /* The edge has lower probability than the temporary best edge. */
  826. is_better_edge = false;
  827. else if (freq < best_freq - diff_freq)
  828. /* The edge and the temporary best edge have almost equivalent
  829. probabilities. The higher frequency of a successor now means
  830. that there is another edge going into that successor.
  831. This successor has lower frequency so it is better. */
  832. is_better_edge = true;
  833. else if (freq > best_freq + diff_freq)
  834. /* This successor has higher frequency so it is worse. */
  835. is_better_edge = false;
  836. else if (e->dest->prev_bb == bb)
  837. /* The edges have equivalent probabilities and the successors
  838. have equivalent frequencies. Select the previous successor. */
  839. is_better_edge = true;
  840. else
  841. is_better_edge = false;
  842. /* If we are doing hot/cold partitioning, make sure that we always favor
  843. non-crossing edges over crossing edges. */
  844. if (!is_better_edge
  845. && flag_reorder_blocks_and_partition
  846. && cur_best_edge
  847. && (cur_best_edge->flags & EDGE_CROSSING)
  848. && !(e->flags & EDGE_CROSSING))
  849. is_better_edge = true;
  850. return is_better_edge;
  851. }
  852. /* Return true when the edge E is better than the temporary best edge
  853. CUR_BEST_EDGE. If SRC_INDEX_P is true, the function compares the src bb of
  854. E and CUR_BEST_EDGE; otherwise it will compare the dest bb.
  855. BEST_LEN is the trace length of src (or dest) bb in CUR_BEST_EDGE.
  856. TRACES record the information about traces.
  857. When optimizing for size, the edge with smaller index is better.
  858. When optimizing for speed, the edge with bigger probability or longer trace
  859. is better. */
  860. static bool
  861. connect_better_edge_p (const_edge e, bool src_index_p, int best_len,
  862. const_edge cur_best_edge, struct trace *traces)
  863. {
  864. int e_index;
  865. int b_index;
  866. bool is_better_edge;
  867. if (!cur_best_edge)
  868. return true;
  869. if (optimize_function_for_size_p (cfun))
  870. {
  871. e_index = src_index_p ? e->src->index : e->dest->index;
  872. b_index = src_index_p ? cur_best_edge->src->index
  873. : cur_best_edge->dest->index;
  874. /* The smaller one is better to keep the original order. */
  875. return b_index > e_index;
  876. }
  877. if (src_index_p)
  878. {
  879. e_index = e->src->index;
  880. if (e->probability > cur_best_edge->probability)
  881. /* The edge has higher probability than the temporary best edge. */
  882. is_better_edge = true;
  883. else if (e->probability < cur_best_edge->probability)
  884. /* The edge has lower probability than the temporary best edge. */
  885. is_better_edge = false;
  886. else if (traces[bbd[e_index].end_of_trace].length > best_len)
  887. /* The edge and the temporary best edge have equivalent probabilities.
  888. The edge with longer trace is better. */
  889. is_better_edge = true;
  890. else
  891. is_better_edge = false;
  892. }
  893. else
  894. {
  895. e_index = e->dest->index;
  896. if (e->probability > cur_best_edge->probability)
  897. /* The edge has higher probability than the temporary best edge. */
  898. is_better_edge = true;
  899. else if (e->probability < cur_best_edge->probability)
  900. /* The edge has lower probability than the temporary best edge. */
  901. is_better_edge = false;
  902. else if (traces[bbd[e_index].start_of_trace].length > best_len)
  903. /* The edge and the temporary best edge have equivalent probabilities.
  904. The edge with longer trace is better. */
  905. is_better_edge = true;
  906. else
  907. is_better_edge = false;
  908. }
  909. return is_better_edge;
  910. }
  911. /* Connect traces in array TRACES, N_TRACES is the count of traces. */
  912. static void
  913. connect_traces (int n_traces, struct trace *traces)
  914. {
  915. int i;
  916. bool *connected;
  917. bool two_passes;
  918. int last_trace;
  919. int current_pass;
  920. int current_partition;
  921. int freq_threshold;
  922. gcov_type count_threshold;
  923. bool for_size = optimize_function_for_size_p (cfun);
  924. freq_threshold = max_entry_frequency * DUPLICATION_THRESHOLD / 1000;
  925. if (max_entry_count < INT_MAX / 1000)
  926. count_threshold = max_entry_count * DUPLICATION_THRESHOLD / 1000;
  927. else
  928. count_threshold = max_entry_count / 1000 * DUPLICATION_THRESHOLD;
  929. connected = XCNEWVEC (bool, n_traces);
  930. last_trace = -1;
  931. current_pass = 1;
  932. current_partition = BB_PARTITION (traces[0].first);
  933. two_passes = false;
  934. if (crtl->has_bb_partition)
  935. for (i = 0; i < n_traces && !two_passes; i++)
  936. if (BB_PARTITION (traces[0].first)
  937. != BB_PARTITION (traces[i].first))
  938. two_passes = true;
  939. for (i = 0; i < n_traces || (two_passes && current_pass == 1) ; i++)
  940. {
  941. int t = i;
  942. int t2;
  943. edge e, best;
  944. int best_len;
  945. if (i >= n_traces)
  946. {
  947. gcc_assert (two_passes && current_pass == 1);
  948. i = 0;
  949. t = i;
  950. current_pass = 2;
  951. if (current_partition == BB_HOT_PARTITION)
  952. current_partition = BB_COLD_PARTITION;
  953. else
  954. current_partition = BB_HOT_PARTITION;
  955. }
  956. if (connected[t])
  957. continue;
  958. if (two_passes
  959. && BB_PARTITION (traces[t].first) != current_partition)
  960. continue;
  961. connected[t] = true;
  962. /* Find the predecessor traces. */
  963. for (t2 = t; t2 > 0;)
  964. {
  965. edge_iterator ei;
  966. best = NULL;
  967. best_len = 0;
  968. FOR_EACH_EDGE (e, ei, traces[t2].first->preds)
  969. {
  970. int si = e->src->index;
  971. if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
  972. && (e->flags & EDGE_CAN_FALLTHRU)
  973. && !(e->flags & EDGE_COMPLEX)
  974. && bbd[si].end_of_trace >= 0
  975. && !connected[bbd[si].end_of_trace]
  976. && (BB_PARTITION (e->src) == current_partition)
  977. && connect_better_edge_p (e, true, best_len, best, traces))
  978. {
  979. best = e;
  980. best_len = traces[bbd[si].end_of_trace].length;
  981. }
  982. }
  983. if (best)
  984. {
  985. best->src->aux = best->dest;
  986. t2 = bbd[best->src->index].end_of_trace;
  987. connected[t2] = true;
  988. if (dump_file)
  989. {
  990. fprintf (dump_file, "Connection: %d %d\n",
  991. best->src->index, best->dest->index);
  992. }
  993. }
  994. else
  995. break;
  996. }
  997. if (last_trace >= 0)
  998. traces[last_trace].last->aux = traces[t2].first;
  999. last_trace = t;
  1000. /* Find the successor traces. */
  1001. while (1)
  1002. {
  1003. /* Find the continuation of the chain. */
  1004. edge_iterator ei;
  1005. best = NULL;
  1006. best_len = 0;
  1007. FOR_EACH_EDGE (e, ei, traces[t].last->succs)
  1008. {
  1009. int di = e->dest->index;
  1010. if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
  1011. && (e->flags & EDGE_CAN_FALLTHRU)
  1012. && !(e->flags & EDGE_COMPLEX)
  1013. && bbd[di].start_of_trace >= 0
  1014. && !connected[bbd[di].start_of_trace]
  1015. && (BB_PARTITION (e->dest) == current_partition)
  1016. && connect_better_edge_p (e, false, best_len, best, traces))
  1017. {
  1018. best = e;
  1019. best_len = traces[bbd[di].start_of_trace].length;
  1020. }
  1021. }
  1022. if (for_size)
  1023. {
  1024. if (!best)
  1025. /* Stop finding the successor traces. */
  1026. break;
  1027. /* It is OK to connect block n with block n + 1 or a block
  1028. before n. For others, only connect to the loop header. */
  1029. if (best->dest->index > (traces[t].last->index + 1))
  1030. {
  1031. int count = EDGE_COUNT (best->dest->preds);
  1032. FOR_EACH_EDGE (e, ei, best->dest->preds)
  1033. if (e->flags & EDGE_DFS_BACK)
  1034. count--;
  1035. /* If dest has multiple predecessors, skip it. We expect
  1036. that one predecessor with smaller index connects with it
  1037. later. */
  1038. if (count != 1)
  1039. break;
  1040. }
  1041. /* Only connect Trace n with Trace n + 1. It is conservative
  1042. to keep the order as close as possible to the original order.
  1043. It also helps to reduce long jumps. */
  1044. if (last_trace != bbd[best->dest->index].start_of_trace - 1)
  1045. break;
  1046. if (dump_file)
  1047. fprintf (dump_file, "Connection: %d %d\n",
  1048. best->src->index, best->dest->index);
  1049. t = bbd[best->dest->index].start_of_trace;
  1050. traces[last_trace].last->aux = traces[t].first;
  1051. connected[t] = true;
  1052. last_trace = t;
  1053. }
  1054. else if (best)
  1055. {
  1056. if (dump_file)
  1057. {
  1058. fprintf (dump_file, "Connection: %d %d\n",
  1059. best->src->index, best->dest->index);
  1060. }
  1061. t = bbd[best->dest->index].start_of_trace;
  1062. traces[last_trace].last->aux = traces[t].first;
  1063. connected[t] = true;
  1064. last_trace = t;
  1065. }
  1066. else
  1067. {
  1068. /* Try to connect the traces by duplication of 1 block. */
  1069. edge e2;
  1070. basic_block next_bb = NULL;
  1071. bool try_copy = false;
  1072. FOR_EACH_EDGE (e, ei, traces[t].last->succs)
  1073. if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
  1074. && (e->flags & EDGE_CAN_FALLTHRU)
  1075. && !(e->flags & EDGE_COMPLEX)
  1076. && (!best || e->probability > best->probability))
  1077. {
  1078. edge_iterator ei;
  1079. edge best2 = NULL;
  1080. int best2_len = 0;
  1081. /* If the destination is a start of a trace which is only
  1082. one block long, then no need to search the successor
  1083. blocks of the trace. Accept it. */
  1084. if (bbd[e->dest->index].start_of_trace >= 0
  1085. && traces[bbd[e->dest->index].start_of_trace].length
  1086. == 1)
  1087. {
  1088. best = e;
  1089. try_copy = true;
  1090. continue;
  1091. }
  1092. FOR_EACH_EDGE (e2, ei, e->dest->succs)
  1093. {
  1094. int di = e2->dest->index;
  1095. if (e2->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)
  1096. || ((e2->flags & EDGE_CAN_FALLTHRU)
  1097. && !(e2->flags & EDGE_COMPLEX)
  1098. && bbd[di].start_of_trace >= 0
  1099. && !connected[bbd[di].start_of_trace]
  1100. && BB_PARTITION (e2->dest) == current_partition
  1101. && EDGE_FREQUENCY (e2) >= freq_threshold
  1102. && e2->count >= count_threshold
  1103. && (!best2
  1104. || e2->probability > best2->probability
  1105. || (e2->probability == best2->probability
  1106. && traces[bbd[di].start_of_trace].length
  1107. > best2_len))))
  1108. {
  1109. best = e;
  1110. best2 = e2;
  1111. if (e2->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
  1112. best2_len = traces[bbd[di].start_of_trace].length;
  1113. else
  1114. best2_len = INT_MAX;
  1115. next_bb = e2->dest;
  1116. try_copy = true;
  1117. }
  1118. }
  1119. }
  1120. if (crtl->has_bb_partition)
  1121. try_copy = false;
  1122. /* Copy tiny blocks always; copy larger blocks only when the
  1123. edge is traversed frequently enough. */
  1124. if (try_copy
  1125. && copy_bb_p (best->dest,
  1126. optimize_edge_for_speed_p (best)
  1127. && EDGE_FREQUENCY (best) >= freq_threshold
  1128. && best->count >= count_threshold))
  1129. {
  1130. basic_block new_bb;
  1131. if (dump_file)
  1132. {
  1133. fprintf (dump_file, "Connection: %d %d ",
  1134. traces[t].last->index, best->dest->index);
  1135. if (!next_bb)
  1136. fputc ('\n', dump_file);
  1137. else if (next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
  1138. fprintf (dump_file, "exit\n");
  1139. else
  1140. fprintf (dump_file, "%d\n", next_bb->index);
  1141. }
  1142. new_bb = copy_bb (best->dest, best, traces[t].last, t);
  1143. traces[t].last = new_bb;
  1144. if (next_bb && next_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
  1145. {
  1146. t = bbd[next_bb->index].start_of_trace;
  1147. traces[last_trace].last->aux = traces[t].first;
  1148. connected[t] = true;
  1149. last_trace = t;
  1150. }
  1151. else
  1152. break; /* Stop finding the successor traces. */
  1153. }
  1154. else
  1155. break; /* Stop finding the successor traces. */
  1156. }
  1157. }
  1158. }
  1159. if (dump_file)
  1160. {
  1161. basic_block bb;
  1162. fprintf (dump_file, "Final order:\n");
  1163. for (bb = traces[0].first; bb; bb = (basic_block) bb->aux)
  1164. fprintf (dump_file, "%d ", bb->index);
  1165. fprintf (dump_file, "\n");
  1166. fflush (dump_file);
  1167. }
  1168. FREE (connected);
  1169. }
  1170. /* Return true when BB can and should be copied. CODE_MAY_GROW is true
  1171. when code size is allowed to grow by duplication. */
  1172. static bool
  1173. copy_bb_p (const_basic_block bb, int code_may_grow)
  1174. {
  1175. int size = 0;
  1176. int max_size = uncond_jump_length;
  1177. rtx_insn *insn;
  1178. if (!bb->frequency)
  1179. return false;
  1180. if (EDGE_COUNT (bb->preds) < 2)
  1181. return false;
  1182. if (!can_duplicate_block_p (bb))
  1183. return false;
  1184. /* Avoid duplicating blocks which have many successors (PR/13430). */
  1185. if (EDGE_COUNT (bb->succs) > 8)
  1186. return false;
  1187. if (code_may_grow && optimize_bb_for_speed_p (bb))
  1188. max_size *= PARAM_VALUE (PARAM_MAX_GROW_COPY_BB_INSNS);
  1189. FOR_BB_INSNS (bb, insn)
  1190. {
  1191. if (INSN_P (insn))
  1192. size += get_attr_min_length (insn);
  1193. }
  1194. if (size <= max_size)
  1195. return true;
  1196. if (dump_file)
  1197. {
  1198. fprintf (dump_file,
  1199. "Block %d can't be copied because its size = %d.\n",
  1200. bb->index, size);
  1201. }
  1202. return false;
  1203. }
  1204. /* Return the length of unconditional jump instruction. */
  1205. int
  1206. get_uncond_jump_length (void)
  1207. {
  1208. rtx_insn *label, *jump;
  1209. int length;
  1210. start_sequence ();
  1211. label = emit_label (gen_label_rtx ());
  1212. jump = emit_jump_insn (gen_jump (label));
  1213. length = get_attr_min_length (jump);
  1214. end_sequence ();
  1215. return length;
  1216. }
  1217. /* The landing pad OLD_LP, in block OLD_BB, has edges from both partitions.
  1218. Duplicate the landing pad and split the edges so that no EH edge
  1219. crosses partitions. */
  1220. static void
  1221. fix_up_crossing_landing_pad (eh_landing_pad old_lp, basic_block old_bb)
  1222. {
  1223. eh_landing_pad new_lp;
  1224. basic_block new_bb, last_bb, post_bb;
  1225. rtx_insn *new_label, *jump;
  1226. rtx post_label;
  1227. unsigned new_partition;
  1228. edge_iterator ei;
  1229. edge e;
  1230. /* Generate the new landing-pad structure. */
  1231. new_lp = gen_eh_landing_pad (old_lp->region);
  1232. new_lp->post_landing_pad = old_lp->post_landing_pad;
  1233. new_lp->landing_pad = gen_label_rtx ();
  1234. LABEL_PRESERVE_P (new_lp->landing_pad) = 1;
  1235. /* Put appropriate instructions in new bb. */
  1236. new_label = emit_label (new_lp->landing_pad);
  1237. expand_dw2_landing_pad_for_region (old_lp->region);
  1238. post_bb = BLOCK_FOR_INSN (old_lp->landing_pad);
  1239. post_bb = single_succ (post_bb);
  1240. post_label = block_label (post_bb);
  1241. jump = emit_jump_insn (gen_jump (post_label));
  1242. JUMP_LABEL (jump) = post_label;
  1243. /* Create new basic block to be dest for lp. */
  1244. last_bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
  1245. new_bb = create_basic_block (new_label, jump, last_bb);
  1246. new_bb->aux = last_bb->aux;
  1247. last_bb->aux = new_bb;
  1248. emit_barrier_after_bb (new_bb);
  1249. make_edge (new_bb, post_bb, 0);
  1250. /* Make sure new bb is in the other partition. */
  1251. new_partition = BB_PARTITION (old_bb);
  1252. new_partition ^= BB_HOT_PARTITION | BB_COLD_PARTITION;
  1253. BB_SET_PARTITION (new_bb, new_partition);
  1254. /* Fix up the edges. */
  1255. for (ei = ei_start (old_bb->preds); (e = ei_safe_edge (ei)) != NULL; )
  1256. if (BB_PARTITION (e->src) == new_partition)
  1257. {
  1258. rtx_insn *insn = BB_END (e->src);
  1259. rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
  1260. gcc_assert (note != NULL);
  1261. gcc_checking_assert (INTVAL (XEXP (note, 0)) == old_lp->index);
  1262. XEXP (note, 0) = GEN_INT (new_lp->index);
  1263. /* Adjust the edge to the new destination. */
  1264. redirect_edge_succ (e, new_bb);
  1265. }
  1266. else
  1267. ei_next (&ei);
  1268. }
  1269. /* Ensure that all hot bbs are included in a hot path through the
  1270. procedure. This is done by calling this function twice, once
  1271. with WALK_UP true (to look for paths from the entry to hot bbs) and
  1272. once with WALK_UP false (to look for paths from hot bbs to the exit).
  1273. Returns the updated value of COLD_BB_COUNT and adds newly-hot bbs
  1274. to BBS_IN_HOT_PARTITION. */
  1275. static unsigned int
  1276. sanitize_hot_paths (bool walk_up, unsigned int cold_bb_count,
  1277. vec<basic_block> *bbs_in_hot_partition)
  1278. {
  1279. /* Callers check this. */
  1280. gcc_checking_assert (cold_bb_count);
  1281. /* Keep examining hot bbs while we still have some left to check
  1282. and there are remaining cold bbs. */
  1283. vec<basic_block> hot_bbs_to_check = bbs_in_hot_partition->copy ();
  1284. while (! hot_bbs_to_check.is_empty ()
  1285. && cold_bb_count)
  1286. {
  1287. basic_block bb = hot_bbs_to_check.pop ();
  1288. vec<edge, va_gc> *edges = walk_up ? bb->preds : bb->succs;
  1289. edge e;
  1290. edge_iterator ei;
  1291. int highest_probability = 0;
  1292. int highest_freq = 0;
  1293. gcov_type highest_count = 0;
  1294. bool found = false;
  1295. /* Walk the preds/succs and check if there is at least one already
  1296. marked hot. Keep track of the most frequent pred/succ so that we
  1297. can mark it hot if we don't find one. */
  1298. FOR_EACH_EDGE (e, ei, edges)
  1299. {
  1300. basic_block reach_bb = walk_up ? e->src : e->dest;
  1301. if (e->flags & EDGE_DFS_BACK)
  1302. continue;
  1303. if (BB_PARTITION (reach_bb) != BB_COLD_PARTITION)
  1304. {
  1305. found = true;
  1306. break;
  1307. }
  1308. /* The following loop will look for the hottest edge via
  1309. the edge count, if it is non-zero, then fallback to the edge
  1310. frequency and finally the edge probability. */
  1311. if (e->count > highest_count)
  1312. highest_count = e->count;
  1313. int edge_freq = EDGE_FREQUENCY (e);
  1314. if (edge_freq > highest_freq)
  1315. highest_freq = edge_freq;
  1316. if (e->probability > highest_probability)
  1317. highest_probability = e->probability;
  1318. }
  1319. /* If bb is reached by (or reaches, in the case of !WALK_UP) another hot
  1320. block (or unpartitioned, e.g. the entry block) then it is ok. If not,
  1321. then the most frequent pred (or succ) needs to be adjusted. In the
  1322. case where multiple preds/succs have the same frequency (e.g. a
  1323. 50-50 branch), then both will be adjusted. */
  1324. if (found)
  1325. continue;
  1326. FOR_EACH_EDGE (e, ei, edges)
  1327. {
  1328. if (e->flags & EDGE_DFS_BACK)
  1329. continue;
  1330. /* Select the hottest edge using the edge count, if it is non-zero,
  1331. then fallback to the edge frequency and finally the edge
  1332. probability. */
  1333. if (highest_count)
  1334. {
  1335. if (e->count < highest_count)
  1336. continue;
  1337. }
  1338. else if (highest_freq)
  1339. {
  1340. if (EDGE_FREQUENCY (e) < highest_freq)
  1341. continue;
  1342. }
  1343. else if (e->probability < highest_probability)
  1344. continue;
  1345. basic_block reach_bb = walk_up ? e->src : e->dest;
  1346. /* We have a hot bb with an immediate dominator that is cold.
  1347. The dominator needs to be re-marked hot. */
  1348. BB_SET_PARTITION (reach_bb, BB_HOT_PARTITION);
  1349. cold_bb_count--;
  1350. /* Now we need to examine newly-hot reach_bb to see if it is also
  1351. dominated by a cold bb. */
  1352. bbs_in_hot_partition->safe_push (reach_bb);
  1353. hot_bbs_to_check.safe_push (reach_bb);
  1354. }
  1355. }
  1356. return cold_bb_count;
  1357. }
  1358. /* Find the basic blocks that are rarely executed and need to be moved to
  1359. a separate section of the .o file (to cut down on paging and improve
  1360. cache locality). Return a vector of all edges that cross. */
  1361. static vec<edge>
  1362. find_rarely_executed_basic_blocks_and_crossing_edges (void)
  1363. {
  1364. vec<edge> crossing_edges = vNULL;
  1365. basic_block bb;
  1366. edge e;
  1367. edge_iterator ei;
  1368. unsigned int cold_bb_count = 0;
  1369. auto_vec<basic_block> bbs_in_hot_partition;
  1370. /* Mark which partition (hot/cold) each basic block belongs in. */
  1371. FOR_EACH_BB_FN (bb, cfun)
  1372. {
  1373. bool cold_bb = false;
  1374. if (probably_never_executed_bb_p (cfun, bb))
  1375. {
  1376. /* Handle profile insanities created by upstream optimizations
  1377. by also checking the incoming edge weights. If there is a non-cold
  1378. incoming edge, conservatively prevent this block from being split
  1379. into the cold section. */
  1380. cold_bb = true;
  1381. FOR_EACH_EDGE (e, ei, bb->preds)
  1382. if (!probably_never_executed_edge_p (cfun, e))
  1383. {
  1384. cold_bb = false;
  1385. break;
  1386. }
  1387. }
  1388. if (cold_bb)
  1389. {
  1390. BB_SET_PARTITION (bb, BB_COLD_PARTITION);
  1391. cold_bb_count++;
  1392. }
  1393. else
  1394. {
  1395. BB_SET_PARTITION (bb, BB_HOT_PARTITION);
  1396. bbs_in_hot_partition.safe_push (bb);
  1397. }
  1398. }
  1399. /* Ensure that hot bbs are included along a hot path from the entry to exit.
  1400. Several different possibilities may include cold bbs along all paths
  1401. to/from a hot bb. One is that there are edge weight insanities
  1402. due to optimization phases that do not properly update basic block profile
  1403. counts. The second is that the entry of the function may not be hot, because
  1404. it is entered fewer times than the number of profile training runs, but there
  1405. is a loop inside the function that causes blocks within the function to be
  1406. above the threshold for hotness. This is fixed by walking up from hot bbs
  1407. to the entry block, and then down from hot bbs to the exit, performing
  1408. partitioning fixups as necessary. */
  1409. if (cold_bb_count)
  1410. {
  1411. mark_dfs_back_edges ();
  1412. cold_bb_count = sanitize_hot_paths (true, cold_bb_count,
  1413. &bbs_in_hot_partition);
  1414. if (cold_bb_count)
  1415. sanitize_hot_paths (false, cold_bb_count, &bbs_in_hot_partition);
  1416. }
  1417. /* The format of .gcc_except_table does not allow landing pads to
  1418. be in a different partition as the throw. Fix this by either
  1419. moving or duplicating the landing pads. */
  1420. if (cfun->eh->lp_array)
  1421. {
  1422. unsigned i;
  1423. eh_landing_pad lp;
  1424. FOR_EACH_VEC_ELT (*cfun->eh->lp_array, i, lp)
  1425. {
  1426. bool all_same, all_diff;
  1427. if (lp == NULL
  1428. || lp->landing_pad == NULL_RTX
  1429. || !LABEL_P (lp->landing_pad))
  1430. continue;
  1431. all_same = all_diff = true;
  1432. bb = BLOCK_FOR_INSN (lp->landing_pad);
  1433. FOR_EACH_EDGE (e, ei, bb->preds)
  1434. {
  1435. gcc_assert (e->flags & EDGE_EH);
  1436. if (BB_PARTITION (bb) == BB_PARTITION (e->src))
  1437. all_diff = false;
  1438. else
  1439. all_same = false;
  1440. }
  1441. if (all_same)
  1442. ;
  1443. else if (all_diff)
  1444. {
  1445. int which = BB_PARTITION (bb);
  1446. which ^= BB_HOT_PARTITION | BB_COLD_PARTITION;
  1447. BB_SET_PARTITION (bb, which);
  1448. }
  1449. else
  1450. fix_up_crossing_landing_pad (lp, bb);
  1451. }
  1452. }
  1453. /* Mark every edge that crosses between sections. */
  1454. FOR_EACH_BB_FN (bb, cfun)
  1455. FOR_EACH_EDGE (e, ei, bb->succs)
  1456. {
  1457. unsigned int flags = e->flags;
  1458. /* We should never have EDGE_CROSSING set yet. */
  1459. gcc_checking_assert ((flags & EDGE_CROSSING) == 0);
  1460. if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
  1461. && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
  1462. && BB_PARTITION (e->src) != BB_PARTITION (e->dest))
  1463. {
  1464. crossing_edges.safe_push (e);
  1465. flags |= EDGE_CROSSING;
  1466. }
  1467. /* Now that we've split eh edges as appropriate, allow landing pads
  1468. to be merged with the post-landing pads. */
  1469. flags &= ~EDGE_PRESERVE;
  1470. e->flags = flags;
  1471. }
  1472. return crossing_edges;
  1473. }
  1474. /* Set the flag EDGE_CAN_FALLTHRU for edges that can be fallthru. */
  1475. static void
  1476. set_edge_can_fallthru_flag (void)
  1477. {
  1478. basic_block bb;
  1479. FOR_EACH_BB_FN (bb, cfun)
  1480. {
  1481. edge e;
  1482. edge_iterator ei;
  1483. FOR_EACH_EDGE (e, ei, bb->succs)
  1484. {
  1485. e->flags &= ~EDGE_CAN_FALLTHRU;
  1486. /* The FALLTHRU edge is also CAN_FALLTHRU edge. */
  1487. if (e->flags & EDGE_FALLTHRU)
  1488. e->flags |= EDGE_CAN_FALLTHRU;
  1489. }
  1490. /* If the BB ends with an invertible condjump all (2) edges are
  1491. CAN_FALLTHRU edges. */
  1492. if (EDGE_COUNT (bb->succs) != 2)
  1493. continue;
  1494. if (!any_condjump_p (BB_END (bb)))
  1495. continue;
  1496. if (!invert_jump (BB_END (bb), JUMP_LABEL (BB_END (bb)), 0))
  1497. continue;
  1498. invert_jump (BB_END (bb), JUMP_LABEL (BB_END (bb)), 0);
  1499. EDGE_SUCC (bb, 0)->flags |= EDGE_CAN_FALLTHRU;
  1500. EDGE_SUCC (bb, 1)->flags |= EDGE_CAN_FALLTHRU;
  1501. }
  1502. }
  1503. /* If any destination of a crossing edge does not have a label, add label;
  1504. Convert any easy fall-through crossing edges to unconditional jumps. */
  1505. static void
  1506. add_labels_and_missing_jumps (vec<edge> crossing_edges)
  1507. {
  1508. size_t i;
  1509. edge e;
  1510. FOR_EACH_VEC_ELT (crossing_edges, i, e)
  1511. {
  1512. basic_block src = e->src;
  1513. basic_block dest = e->dest;
  1514. rtx label;
  1515. rtx_insn *new_jump;
  1516. if (dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
  1517. continue;
  1518. /* Make sure dest has a label. */
  1519. label = block_label (dest);
  1520. /* Nothing to do for non-fallthru edges. */
  1521. if (src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
  1522. continue;
  1523. if ((e->flags & EDGE_FALLTHRU) == 0)
  1524. continue;
  1525. /* If the block does not end with a control flow insn, then we
  1526. can trivially add a jump to the end to fixup the crossing.
  1527. Otherwise the jump will have to go in a new bb, which will
  1528. be handled by fix_up_fall_thru_edges function. */
  1529. if (control_flow_insn_p (BB_END (src)))
  1530. continue;
  1531. /* Make sure there's only one successor. */
  1532. gcc_assert (single_succ_p (src));
  1533. new_jump = emit_jump_insn_after (gen_jump (label), BB_END (src));
  1534. BB_END (src) = new_jump;
  1535. JUMP_LABEL (new_jump) = label;
  1536. LABEL_NUSES (label) += 1;
  1537. emit_barrier_after_bb (src);
  1538. /* Mark edge as non-fallthru. */
  1539. e->flags &= ~EDGE_FALLTHRU;
  1540. }
  1541. }
  1542. /* Find any bb's where the fall-through edge is a crossing edge (note that
  1543. these bb's must also contain a conditional jump or end with a call
  1544. instruction; we've already dealt with fall-through edges for blocks
  1545. that didn't have a conditional jump or didn't end with call instruction
  1546. in the call to add_labels_and_missing_jumps). Convert the fall-through
  1547. edge to non-crossing edge by inserting a new bb to fall-through into.
  1548. The new bb will contain an unconditional jump (crossing edge) to the
  1549. original fall through destination. */
  1550. static void
  1551. fix_up_fall_thru_edges (void)
  1552. {
  1553. basic_block cur_bb;
  1554. basic_block new_bb;
  1555. edge succ1;
  1556. edge succ2;
  1557. edge fall_thru;
  1558. edge cond_jump = NULL;
  1559. edge e;
  1560. bool cond_jump_crosses;
  1561. int invert_worked;
  1562. rtx_insn *old_jump;
  1563. rtx fall_thru_label;
  1564. FOR_EACH_BB_FN (cur_bb, cfun)
  1565. {
  1566. fall_thru = NULL;
  1567. if (EDGE_COUNT (cur_bb->succs) > 0)
  1568. succ1 = EDGE_SUCC (cur_bb, 0);
  1569. else
  1570. succ1 = NULL;
  1571. if (EDGE_COUNT (cur_bb->succs) > 1)
  1572. succ2 = EDGE_SUCC (cur_bb, 1);
  1573. else
  1574. succ2 = NULL;
  1575. /* Find the fall-through edge. */
  1576. if (succ1
  1577. && (succ1->flags & EDGE_FALLTHRU))
  1578. {
  1579. fall_thru = succ1;
  1580. cond_jump = succ2;
  1581. }
  1582. else if (succ2
  1583. && (succ2->flags & EDGE_FALLTHRU))
  1584. {
  1585. fall_thru = succ2;
  1586. cond_jump = succ1;
  1587. }
  1588. else if (succ1
  1589. && (block_ends_with_call_p (cur_bb)
  1590. || can_throw_internal (BB_END (cur_bb))))
  1591. {
  1592. edge e;
  1593. edge_iterator ei;
  1594. FOR_EACH_EDGE (e, ei, cur_bb->succs)
  1595. if (e->flags & EDGE_FALLTHRU)
  1596. {
  1597. fall_thru = e;
  1598. break;
  1599. }
  1600. }
  1601. if (fall_thru && (fall_thru->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)))
  1602. {
  1603. /* Check to see if the fall-thru edge is a crossing edge. */
  1604. if (fall_thru->flags & EDGE_CROSSING)
  1605. {
  1606. /* The fall_thru edge crosses; now check the cond jump edge, if
  1607. it exists. */
  1608. cond_jump_crosses = true;
  1609. invert_worked = 0;
  1610. old_jump = BB_END (cur_bb);
  1611. /* Find the jump instruction, if there is one. */
  1612. if (cond_jump)
  1613. {
  1614. if (!(cond_jump->flags & EDGE_CROSSING))
  1615. cond_jump_crosses = false;
  1616. /* We know the fall-thru edge crosses; if the cond
  1617. jump edge does NOT cross, and its destination is the
  1618. next block in the bb order, invert the jump
  1619. (i.e. fix it so the fall through does not cross and
  1620. the cond jump does). */
  1621. if (!cond_jump_crosses)
  1622. {
  1623. /* Find label in fall_thru block. We've already added
  1624. any missing labels, so there must be one. */
  1625. fall_thru_label = block_label (fall_thru->dest);
  1626. if (old_jump && JUMP_P (old_jump) && fall_thru_label)
  1627. invert_worked = invert_jump (old_jump,
  1628. fall_thru_label,0);
  1629. if (invert_worked)
  1630. {
  1631. fall_thru->flags &= ~EDGE_FALLTHRU;
  1632. cond_jump->flags |= EDGE_FALLTHRU;
  1633. update_br_prob_note (cur_bb);
  1634. e = fall_thru;
  1635. fall_thru = cond_jump;
  1636. cond_jump = e;
  1637. cond_jump->flags |= EDGE_CROSSING;
  1638. fall_thru->flags &= ~EDGE_CROSSING;
  1639. }
  1640. }
  1641. }
  1642. if (cond_jump_crosses || !invert_worked)
  1643. {
  1644. /* This is the case where both edges out of the basic
  1645. block are crossing edges. Here we will fix up the
  1646. fall through edge. The jump edge will be taken care
  1647. of later. The EDGE_CROSSING flag of fall_thru edge
  1648. is unset before the call to force_nonfallthru
  1649. function because if a new basic-block is created
  1650. this edge remains in the current section boundary
  1651. while the edge between new_bb and the fall_thru->dest
  1652. becomes EDGE_CROSSING. */
  1653. fall_thru->flags &= ~EDGE_CROSSING;
  1654. new_bb = force_nonfallthru (fall_thru);
  1655. if (new_bb)
  1656. {
  1657. new_bb->aux = cur_bb->aux;
  1658. cur_bb->aux = new_bb;
  1659. /* This is done by force_nonfallthru_and_redirect. */
  1660. gcc_assert (BB_PARTITION (new_bb)
  1661. == BB_PARTITION (cur_bb));
  1662. single_succ_edge (new_bb)->flags |= EDGE_CROSSING;
  1663. }
  1664. else
  1665. {
  1666. /* If a new basic-block was not created; restore
  1667. the EDGE_CROSSING flag. */
  1668. fall_thru->flags |= EDGE_CROSSING;
  1669. }
  1670. /* Add barrier after new jump */
  1671. emit_barrier_after_bb (new_bb ? new_bb : cur_bb);
  1672. }
  1673. }
  1674. }
  1675. }
  1676. }
  1677. /* This function checks the destination block of a "crossing jump" to
  1678. see if it has any crossing predecessors that begin with a code label
  1679. and end with an unconditional jump. If so, it returns that predecessor
  1680. block. (This is to avoid creating lots of new basic blocks that all
  1681. contain unconditional jumps to the same destination). */
  1682. static basic_block
  1683. find_jump_block (basic_block jump_dest)
  1684. {
  1685. basic_block source_bb = NULL;
  1686. edge e;
  1687. rtx_insn *insn;
  1688. edge_iterator ei;
  1689. FOR_EACH_EDGE (e, ei, jump_dest->preds)
  1690. if (e->flags & EDGE_CROSSING)
  1691. {
  1692. basic_block src = e->src;
  1693. /* Check each predecessor to see if it has a label, and contains
  1694. only one executable instruction, which is an unconditional jump.
  1695. If so, we can use it. */
  1696. if (LABEL_P (BB_HEAD (src)))
  1697. for (insn = BB_HEAD (src);
  1698. !INSN_P (insn) && insn != NEXT_INSN (BB_END (src));
  1699. insn = NEXT_INSN (insn))
  1700. {
  1701. if (INSN_P (insn)
  1702. && insn == BB_END (src)
  1703. && JUMP_P (insn)
  1704. && !any_condjump_p (insn))
  1705. {
  1706. source_bb = src;
  1707. break;
  1708. }
  1709. }
  1710. if (source_bb)
  1711. break;
  1712. }
  1713. return source_bb;
  1714. }
  1715. /* Find all BB's with conditional jumps that are crossing edges;
  1716. insert a new bb and make the conditional jump branch to the new
  1717. bb instead (make the new bb same color so conditional branch won't
  1718. be a 'crossing' edge). Insert an unconditional jump from the
  1719. new bb to the original destination of the conditional jump. */
  1720. static void
  1721. fix_crossing_conditional_branches (void)
  1722. {
  1723. basic_block cur_bb;
  1724. basic_block new_bb;
  1725. basic_block dest;
  1726. edge succ1;
  1727. edge succ2;
  1728. edge crossing_edge;
  1729. edge new_edge;
  1730. rtx_insn *old_jump;
  1731. rtx set_src;
  1732. rtx old_label = NULL_RTX;
  1733. rtx new_label;
  1734. FOR_EACH_BB_FN (cur_bb, cfun)
  1735. {
  1736. crossing_edge = NULL;
  1737. if (EDGE_COUNT (cur_bb->succs) > 0)
  1738. succ1 = EDGE_SUCC (cur_bb, 0);
  1739. else
  1740. succ1 = NULL;
  1741. if (EDGE_COUNT (cur_bb->succs) > 1)
  1742. succ2 = EDGE_SUCC (cur_bb, 1);
  1743. else
  1744. succ2 = NULL;
  1745. /* We already took care of fall-through edges, so only one successor
  1746. can be a crossing edge. */
  1747. if (succ1 && (succ1->flags & EDGE_CROSSING))
  1748. crossing_edge = succ1;
  1749. else if (succ2 && (succ2->flags & EDGE_CROSSING))
  1750. crossing_edge = succ2;
  1751. if (crossing_edge)
  1752. {
  1753. old_jump = BB_END (cur_bb);
  1754. /* Check to make sure the jump instruction is a
  1755. conditional jump. */
  1756. set_src = NULL_RTX;
  1757. if (any_condjump_p (old_jump))
  1758. {
  1759. if (GET_CODE (PATTERN (old_jump)) == SET)
  1760. set_src = SET_SRC (PATTERN (old_jump));
  1761. else if (GET_CODE (PATTERN (old_jump)) == PARALLEL)
  1762. {
  1763. set_src = XVECEXP (PATTERN (old_jump), 0,0);
  1764. if (GET_CODE (set_src) == SET)
  1765. set_src = SET_SRC (set_src);
  1766. else
  1767. set_src = NULL_RTX;
  1768. }
  1769. }
  1770. if (set_src && (GET_CODE (set_src) == IF_THEN_ELSE))
  1771. {
  1772. if (GET_CODE (XEXP (set_src, 1)) == PC)
  1773. old_label = XEXP (set_src, 2);
  1774. else if (GET_CODE (XEXP (set_src, 2)) == PC)
  1775. old_label = XEXP (set_src, 1);
  1776. /* Check to see if new bb for jumping to that dest has
  1777. already been created; if so, use it; if not, create
  1778. a new one. */
  1779. new_bb = find_jump_block (crossing_edge->dest);
  1780. if (new_bb)
  1781. new_label = block_label (new_bb);
  1782. else
  1783. {
  1784. basic_block last_bb;
  1785. rtx_insn *new_jump;
  1786. /* Create new basic block to be dest for
  1787. conditional jump. */
  1788. /* Put appropriate instructions in new bb. */
  1789. new_label = gen_label_rtx ();
  1790. emit_label (new_label);
  1791. gcc_assert (GET_CODE (old_label) == LABEL_REF);
  1792. old_label = JUMP_LABEL (old_jump);
  1793. new_jump = emit_jump_insn (gen_jump (old_label));
  1794. JUMP_LABEL (new_jump) = old_label;
  1795. last_bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
  1796. new_bb = create_basic_block (new_label, new_jump, last_bb);
  1797. new_bb->aux = last_bb->aux;
  1798. last_bb->aux = new_bb;
  1799. emit_barrier_after_bb (new_bb);
  1800. /* Make sure new bb is in same partition as source
  1801. of conditional branch. */
  1802. BB_COPY_PARTITION (new_bb, cur_bb);
  1803. }
  1804. /* Make old jump branch to new bb. */
  1805. redirect_jump (old_jump, new_label, 0);
  1806. /* Remove crossing_edge as predecessor of 'dest'. */
  1807. dest = crossing_edge->dest;
  1808. redirect_edge_succ (crossing_edge, new_bb);
  1809. /* Make a new edge from new_bb to old dest; new edge
  1810. will be a successor for new_bb and a predecessor
  1811. for 'dest'. */
  1812. if (EDGE_COUNT (new_bb->succs) == 0)
  1813. new_edge = make_edge (new_bb, dest, 0);
  1814. else
  1815. new_edge = EDGE_SUCC (new_bb, 0);
  1816. crossing_edge->flags &= ~EDGE_CROSSING;
  1817. new_edge->flags |= EDGE_CROSSING;
  1818. }
  1819. }
  1820. }
  1821. }
  1822. /* Find any unconditional branches that cross between hot and cold
  1823. sections. Convert them into indirect jumps instead. */
  1824. static void
  1825. fix_crossing_unconditional_branches (void)
  1826. {
  1827. basic_block cur_bb;
  1828. rtx_insn *last_insn;
  1829. rtx label;
  1830. rtx label_addr;
  1831. rtx_insn *indirect_jump_sequence;
  1832. rtx_insn *jump_insn = NULL;
  1833. rtx new_reg;
  1834. rtx_insn *cur_insn;
  1835. edge succ;
  1836. FOR_EACH_BB_FN (cur_bb, cfun)
  1837. {
  1838. last_insn = BB_END (cur_bb);
  1839. if (EDGE_COUNT (cur_bb->succs) < 1)
  1840. continue;
  1841. succ = EDGE_SUCC (cur_bb, 0);
  1842. /* Check to see if bb ends in a crossing (unconditional) jump. At
  1843. this point, no crossing jumps should be conditional. */
  1844. if (JUMP_P (last_insn)
  1845. && (succ->flags & EDGE_CROSSING))
  1846. {
  1847. gcc_assert (!any_condjump_p (last_insn));
  1848. /* Make sure the jump is not already an indirect or table jump. */
  1849. if (!computed_jump_p (last_insn)
  1850. && !tablejump_p (last_insn, NULL, NULL))
  1851. {
  1852. /* We have found a "crossing" unconditional branch. Now
  1853. we must convert it to an indirect jump. First create
  1854. reference of label, as target for jump. */
  1855. label = JUMP_LABEL (last_insn);
  1856. label_addr = gen_rtx_LABEL_REF (Pmode, label);
  1857. LABEL_NUSES (label) += 1;
  1858. /* Get a register to use for the indirect jump. */
  1859. new_reg = gen_reg_rtx (Pmode);
  1860. /* Generate indirect the jump sequence. */
  1861. start_sequence ();
  1862. emit_move_insn (new_reg, label_addr);
  1863. emit_indirect_jump (new_reg);
  1864. indirect_jump_sequence = get_insns ();
  1865. end_sequence ();
  1866. /* Make sure every instruction in the new jump sequence has
  1867. its basic block set to be cur_bb. */
  1868. for (cur_insn = indirect_jump_sequence; cur_insn;
  1869. cur_insn = NEXT_INSN (cur_insn))
  1870. {
  1871. if (!BARRIER_P (cur_insn))
  1872. BLOCK_FOR_INSN (cur_insn) = cur_bb;
  1873. if (JUMP_P (cur_insn))
  1874. jump_insn = cur_insn;
  1875. }
  1876. /* Insert the new (indirect) jump sequence immediately before
  1877. the unconditional jump, then delete the unconditional jump. */
  1878. emit_insn_before (indirect_jump_sequence, last_insn);
  1879. delete_insn (last_insn);
  1880. JUMP_LABEL (jump_insn) = label;
  1881. LABEL_NUSES (label)++;
  1882. /* Make BB_END for cur_bb be the jump instruction (NOT the
  1883. barrier instruction at the end of the sequence...). */
  1884. BB_END (cur_bb) = jump_insn;
  1885. }
  1886. }
  1887. }
  1888. }
  1889. /* Update CROSSING_JUMP_P flags on all jump insns. */
  1890. static void
  1891. update_crossing_jump_flags (void)
  1892. {
  1893. basic_block bb;
  1894. edge e;
  1895. edge_iterator ei;
  1896. FOR_EACH_BB_FN (bb, cfun)
  1897. FOR_EACH_EDGE (e, ei, bb->succs)
  1898. if (e->flags & EDGE_CROSSING)
  1899. {
  1900. if (JUMP_P (BB_END (bb))
  1901. /* Some flags were added during fix_up_fall_thru_edges, via
  1902. force_nonfallthru_and_redirect. */
  1903. && !CROSSING_JUMP_P (BB_END (bb)))
  1904. CROSSING_JUMP_P (BB_END (bb)) = 1;
  1905. break;
  1906. }
  1907. }
  1908. /* Reorder basic blocks. The main entry point to this file. FLAGS is
  1909. the set of flags to pass to cfg_layout_initialize(). */
  1910. static void
  1911. reorder_basic_blocks (void)
  1912. {
  1913. int n_traces;
  1914. int i;
  1915. struct trace *traces;
  1916. gcc_assert (current_ir_type () == IR_RTL_CFGLAYOUT);
  1917. if (n_basic_blocks_for_fn (cfun) <= NUM_FIXED_BLOCKS + 1)
  1918. return;
  1919. set_edge_can_fallthru_flag ();
  1920. mark_dfs_back_edges ();
  1921. /* We are estimating the length of uncond jump insn only once since the code
  1922. for getting the insn length always returns the minimal length now. */
  1923. if (uncond_jump_length == 0)
  1924. uncond_jump_length = get_uncond_jump_length ();
  1925. /* We need to know some information for each basic block. */
  1926. array_size = GET_ARRAY_SIZE (last_basic_block_for_fn (cfun));
  1927. bbd = XNEWVEC (bbro_basic_block_data, array_size);
  1928. for (i = 0; i < array_size; i++)
  1929. {
  1930. bbd[i].start_of_trace = -1;
  1931. bbd[i].end_of_trace = -1;
  1932. bbd[i].in_trace = -1;
  1933. bbd[i].visited = 0;
  1934. bbd[i].heap = NULL;
  1935. bbd[i].node = NULL;
  1936. }
  1937. traces = XNEWVEC (struct trace, n_basic_blocks_for_fn (cfun));
  1938. n_traces = 0;
  1939. find_traces (&n_traces, traces);
  1940. connect_traces (n_traces, traces);
  1941. FREE (traces);
  1942. FREE (bbd);
  1943. relink_block_chain (/*stay_in_cfglayout_mode=*/true);
  1944. if (dump_file)
  1945. {
  1946. if (dump_flags & TDF_DETAILS)
  1947. dump_reg_info (dump_file);
  1948. dump_flow_info (dump_file, dump_flags);
  1949. }
  1950. /* Signal that rtl_verify_flow_info_1 can now verify that there
  1951. is at most one switch between hot/cold sections. */
  1952. crtl->bb_reorder_complete = true;
  1953. }
  1954. /* Determine which partition the first basic block in the function
  1955. belongs to, then find the first basic block in the current function
  1956. that belongs to a different section, and insert a
  1957. NOTE_INSN_SWITCH_TEXT_SECTIONS note immediately before it in the
  1958. instruction stream. When writing out the assembly code,
  1959. encountering this note will make the compiler switch between the
  1960. hot and cold text sections. */
  1961. void
  1962. insert_section_boundary_note (void)
  1963. {
  1964. basic_block bb;
  1965. bool switched_sections = false;
  1966. int current_partition = 0;
  1967. if (!crtl->has_bb_partition)
  1968. return;
  1969. FOR_EACH_BB_FN (bb, cfun)
  1970. {
  1971. if (!current_partition)
  1972. current_partition = BB_PARTITION (bb);
  1973. if (BB_PARTITION (bb) != current_partition)
  1974. {
  1975. gcc_assert (!switched_sections);
  1976. switched_sections = true;
  1977. emit_note_before (NOTE_INSN_SWITCH_TEXT_SECTIONS, BB_HEAD (bb));
  1978. current_partition = BB_PARTITION (bb);
  1979. }
  1980. }
  1981. }
  1982. namespace {
  1983. const pass_data pass_data_reorder_blocks =
  1984. {
  1985. RTL_PASS, /* type */
  1986. "bbro", /* name */
  1987. OPTGROUP_NONE, /* optinfo_flags */
  1988. TV_REORDER_BLOCKS, /* tv_id */
  1989. 0, /* properties_required */
  1990. 0, /* properties_provided */
  1991. 0, /* properties_destroyed */
  1992. 0, /* todo_flags_start */
  1993. 0, /* todo_flags_finish */
  1994. };
  1995. class pass_reorder_blocks : public rtl_opt_pass
  1996. {
  1997. public:
  1998. pass_reorder_blocks (gcc::context *ctxt)
  1999. : rtl_opt_pass (pass_data_reorder_blocks, ctxt)
  2000. {}
  2001. /* opt_pass methods: */
  2002. virtual bool gate (function *)
  2003. {
  2004. if (targetm.cannot_modify_jumps_p ())
  2005. return false;
  2006. return (optimize > 0
  2007. && (flag_reorder_blocks || flag_reorder_blocks_and_partition));
  2008. }
  2009. virtual unsigned int execute (function *);
  2010. }; // class pass_reorder_blocks
  2011. unsigned int
  2012. pass_reorder_blocks::execute (function *fun)
  2013. {
  2014. basic_block bb;
  2015. /* Last attempt to optimize CFG, as scheduling, peepholing and insn
  2016. splitting possibly introduced more crossjumping opportunities. */
  2017. cfg_layout_initialize (CLEANUP_EXPENSIVE);
  2018. reorder_basic_blocks ();
  2019. cleanup_cfg (CLEANUP_EXPENSIVE);
  2020. FOR_EACH_BB_FN (bb, fun)
  2021. if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (fun))
  2022. bb->aux = bb->next_bb;
  2023. cfg_layout_finalize ();
  2024. return 0;
  2025. }
  2026. } // anon namespace
  2027. rtl_opt_pass *
  2028. make_pass_reorder_blocks (gcc::context *ctxt)
  2029. {
  2030. return new pass_reorder_blocks (ctxt);
  2031. }
  2032. /* Duplicate the blocks containing computed gotos. This basically unfactors
  2033. computed gotos that were factored early on in the compilation process to
  2034. speed up edge based data flow. We used to not unfactoring them again,
  2035. which can seriously pessimize code with many computed jumps in the source
  2036. code, such as interpreters. See e.g. PR15242. */
  2037. namespace {
  2038. const pass_data pass_data_duplicate_computed_gotos =
  2039. {
  2040. RTL_PASS, /* type */
  2041. "compgotos", /* name */
  2042. OPTGROUP_NONE, /* optinfo_flags */
  2043. TV_REORDER_BLOCKS, /* tv_id */
  2044. 0, /* properties_required */
  2045. 0, /* properties_provided */
  2046. 0, /* properties_destroyed */
  2047. 0, /* todo_flags_start */
  2048. 0, /* todo_flags_finish */
  2049. };
  2050. class pass_duplicate_computed_gotos : public rtl_opt_pass
  2051. {
  2052. public:
  2053. pass_duplicate_computed_gotos (gcc::context *ctxt)
  2054. : rtl_opt_pass (pass_data_duplicate_computed_gotos, ctxt)
  2055. {}
  2056. /* opt_pass methods: */
  2057. virtual bool gate (function *);
  2058. virtual unsigned int execute (function *);
  2059. }; // class pass_duplicate_computed_gotos
  2060. bool
  2061. pass_duplicate_computed_gotos::gate (function *fun)
  2062. {
  2063. if (targetm.cannot_modify_jumps_p ())
  2064. return false;
  2065. return (optimize > 0
  2066. && flag_expensive_optimizations
  2067. && ! optimize_function_for_size_p (fun));
  2068. }
  2069. unsigned int
  2070. pass_duplicate_computed_gotos::execute (function *fun)
  2071. {
  2072. basic_block bb, new_bb;
  2073. bitmap candidates;
  2074. int max_size;
  2075. bool changed = false;
  2076. if (n_basic_blocks_for_fn (fun) <= NUM_FIXED_BLOCKS + 1)
  2077. return 0;
  2078. clear_bb_flags ();
  2079. cfg_layout_initialize (0);
  2080. /* We are estimating the length of uncond jump insn only once
  2081. since the code for getting the insn length always returns
  2082. the minimal length now. */
  2083. if (uncond_jump_length == 0)
  2084. uncond_jump_length = get_uncond_jump_length ();
  2085. max_size
  2086. = uncond_jump_length * PARAM_VALUE (PARAM_MAX_GOTO_DUPLICATION_INSNS);
  2087. candidates = BITMAP_ALLOC (NULL);
  2088. /* Look for blocks that end in a computed jump, and see if such blocks
  2089. are suitable for unfactoring. If a block is a candidate for unfactoring,
  2090. mark it in the candidates. */
  2091. FOR_EACH_BB_FN (bb, fun)
  2092. {
  2093. rtx_insn *insn;
  2094. edge e;
  2095. edge_iterator ei;
  2096. int size, all_flags;
  2097. /* Build the reorder chain for the original order of blocks. */
  2098. if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (fun))
  2099. bb->aux = bb->next_bb;
  2100. /* Obviously the block has to end in a computed jump. */
  2101. if (!computed_jump_p (BB_END (bb)))
  2102. continue;
  2103. /* Only consider blocks that can be duplicated. */
  2104. if (CROSSING_JUMP_P (BB_END (bb))
  2105. || !can_duplicate_block_p (bb))
  2106. continue;
  2107. /* Make sure that the block is small enough. */
  2108. size = 0;
  2109. FOR_BB_INSNS (bb, insn)
  2110. if (INSN_P (insn))
  2111. {
  2112. size += get_attr_min_length (insn);
  2113. if (size > max_size)
  2114. break;
  2115. }
  2116. if (size > max_size)
  2117. continue;
  2118. /* Final check: there must not be any incoming abnormal edges. */
  2119. all_flags = 0;
  2120. FOR_EACH_EDGE (e, ei, bb->preds)
  2121. all_flags |= e->flags;
  2122. if (all_flags & EDGE_COMPLEX)
  2123. continue;
  2124. bitmap_set_bit (candidates, bb->index);
  2125. }
  2126. /* Nothing to do if there is no computed jump here. */
  2127. if (bitmap_empty_p (candidates))
  2128. goto done;
  2129. /* Duplicate computed gotos. */
  2130. FOR_EACH_BB_FN (bb, fun)
  2131. {
  2132. if (bb->flags & BB_VISITED)
  2133. continue;
  2134. bb->flags |= BB_VISITED;
  2135. /* BB must have one outgoing edge. That edge must not lead to
  2136. the exit block or the next block.
  2137. The destination must have more than one predecessor. */
  2138. if (!single_succ_p (bb)
  2139. || single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (fun)
  2140. || single_succ (bb) == bb->next_bb
  2141. || single_pred_p (single_succ (bb)))
  2142. continue;
  2143. /* The successor block has to be a duplication candidate. */
  2144. if (!bitmap_bit_p (candidates, single_succ (bb)->index))
  2145. continue;
  2146. /* Don't duplicate a partition crossing edge, which requires difficult
  2147. fixup. */
  2148. if (JUMP_P (BB_END (bb)) && CROSSING_JUMP_P (BB_END (bb)))
  2149. continue;
  2150. new_bb = duplicate_block (single_succ (bb), single_succ_edge (bb), bb);
  2151. new_bb->aux = bb->aux;
  2152. bb->aux = new_bb;
  2153. new_bb->flags |= BB_VISITED;
  2154. changed = true;
  2155. }
  2156. done:
  2157. if (changed)
  2158. {
  2159. /* Duplicating blocks above will redirect edges and may cause hot
  2160. blocks previously reached by both hot and cold blocks to become
  2161. dominated only by cold blocks. */
  2162. fixup_partitions ();
  2163. /* Merge the duplicated blocks into predecessors, when possible. */
  2164. cfg_layout_finalize ();
  2165. cleanup_cfg (0);
  2166. }
  2167. else
  2168. cfg_layout_finalize ();
  2169. BITMAP_FREE (candidates);
  2170. return 0;
  2171. }
  2172. } // anon namespace
  2173. rtl_opt_pass *
  2174. make_pass_duplicate_computed_gotos (gcc::context *ctxt)
  2175. {
  2176. return new pass_duplicate_computed_gotos (ctxt);
  2177. }
  2178. /* This function is the main 'entrance' for the optimization that
  2179. partitions hot and cold basic blocks into separate sections of the
  2180. .o file (to improve performance and cache locality). Ideally it
  2181. would be called after all optimizations that rearrange the CFG have
  2182. been called. However part of this optimization may introduce new
  2183. register usage, so it must be called before register allocation has
  2184. occurred. This means that this optimization is actually called
  2185. well before the optimization that reorders basic blocks (see
  2186. function above).
  2187. This optimization checks the feedback information to determine
  2188. which basic blocks are hot/cold, updates flags on the basic blocks
  2189. to indicate which section they belong in. This information is
  2190. later used for writing out sections in the .o file. Because hot
  2191. and cold sections can be arbitrarily large (within the bounds of
  2192. memory), far beyond the size of a single function, it is necessary
  2193. to fix up all edges that cross section boundaries, to make sure the
  2194. instructions used can actually span the required distance. The
  2195. fixes are described below.
  2196. Fall-through edges must be changed into jumps; it is not safe or
  2197. legal to fall through across a section boundary. Whenever a
  2198. fall-through edge crossing a section boundary is encountered, a new
  2199. basic block is inserted (in the same section as the fall-through
  2200. source), and the fall through edge is redirected to the new basic
  2201. block. The new basic block contains an unconditional jump to the
  2202. original fall-through target. (If the unconditional jump is
  2203. insufficient to cross section boundaries, that is dealt with a
  2204. little later, see below).
  2205. In order to deal with architectures that have short conditional
  2206. branches (which cannot span all of memory) we take any conditional
  2207. jump that attempts to cross a section boundary and add a level of
  2208. indirection: it becomes a conditional jump to a new basic block, in
  2209. the same section. The new basic block contains an unconditional
  2210. jump to the original target, in the other section.
  2211. For those architectures whose unconditional branch is also
  2212. incapable of reaching all of memory, those unconditional jumps are
  2213. converted into indirect jumps, through a register.
  2214. IMPORTANT NOTE: This optimization causes some messy interactions
  2215. with the cfg cleanup optimizations; those optimizations want to
  2216. merge blocks wherever possible, and to collapse indirect jump
  2217. sequences (change "A jumps to B jumps to C" directly into "A jumps
  2218. to C"). Those optimizations can undo the jump fixes that
  2219. partitioning is required to make (see above), in order to ensure
  2220. that jumps attempting to cross section boundaries are really able
  2221. to cover whatever distance the jump requires (on many architectures
  2222. conditional or unconditional jumps are not able to reach all of
  2223. memory). Therefore tests have to be inserted into each such
  2224. optimization to make sure that it does not undo stuff necessary to
  2225. cross partition boundaries. This would be much less of a problem
  2226. if we could perform this optimization later in the compilation, but
  2227. unfortunately the fact that we may need to create indirect jumps
  2228. (through registers) requires that this optimization be performed
  2229. before register allocation.
  2230. Hot and cold basic blocks are partitioned and put in separate
  2231. sections of the .o file, to reduce paging and improve cache
  2232. performance (hopefully). This can result in bits of code from the
  2233. same function being widely separated in the .o file. However this
  2234. is not obvious to the current bb structure. Therefore we must take
  2235. care to ensure that: 1). There are no fall_thru edges that cross
  2236. between sections; 2). For those architectures which have "short"
  2237. conditional branches, all conditional branches that attempt to
  2238. cross between sections are converted to unconditional branches;
  2239. and, 3). For those architectures which have "short" unconditional
  2240. branches, all unconditional branches that attempt to cross between
  2241. sections are converted to indirect jumps.
  2242. The code for fixing up fall_thru edges that cross between hot and
  2243. cold basic blocks does so by creating new basic blocks containing
  2244. unconditional branches to the appropriate label in the "other"
  2245. section. The new basic block is then put in the same (hot or cold)
  2246. section as the original conditional branch, and the fall_thru edge
  2247. is modified to fall into the new basic block instead. By adding
  2248. this level of indirection we end up with only unconditional branches
  2249. crossing between hot and cold sections.
  2250. Conditional branches are dealt with by adding a level of indirection.
  2251. A new basic block is added in the same (hot/cold) section as the
  2252. conditional branch, and the conditional branch is retargeted to the
  2253. new basic block. The new basic block contains an unconditional branch
  2254. to the original target of the conditional branch (in the other section).
  2255. Unconditional branches are dealt with by converting them into
  2256. indirect jumps. */
  2257. namespace {
  2258. const pass_data pass_data_partition_blocks =
  2259. {
  2260. RTL_PASS, /* type */
  2261. "bbpart", /* name */
  2262. OPTGROUP_NONE, /* optinfo_flags */
  2263. TV_REORDER_BLOCKS, /* tv_id */
  2264. PROP_cfglayout, /* properties_required */
  2265. 0, /* properties_provided */
  2266. 0, /* properties_destroyed */
  2267. 0, /* todo_flags_start */
  2268. 0, /* todo_flags_finish */
  2269. };
  2270. class pass_partition_blocks : public rtl_opt_pass
  2271. {
  2272. public:
  2273. pass_partition_blocks (gcc::context *ctxt)
  2274. : rtl_opt_pass (pass_data_partition_blocks, ctxt)
  2275. {}
  2276. /* opt_pass methods: */
  2277. virtual bool gate (function *);
  2278. virtual unsigned int execute (function *);
  2279. }; // class pass_partition_blocks
  2280. bool
  2281. pass_partition_blocks::gate (function *fun)
  2282. {
  2283. /* The optimization to partition hot/cold basic blocks into separate
  2284. sections of the .o file does not work well with linkonce or with
  2285. user defined section attributes. Don't call it if either case
  2286. arises. */
  2287. return (flag_reorder_blocks_and_partition
  2288. && optimize
  2289. /* See gate_handle_reorder_blocks. We should not partition if
  2290. we are going to omit the reordering. */
  2291. && optimize_function_for_speed_p (fun)
  2292. && !DECL_COMDAT_GROUP (current_function_decl)
  2293. && !user_defined_section_attribute);
  2294. }
  2295. unsigned
  2296. pass_partition_blocks::execute (function *fun)
  2297. {
  2298. vec<edge> crossing_edges;
  2299. if (n_basic_blocks_for_fn (fun) <= NUM_FIXED_BLOCKS + 1)
  2300. return 0;
  2301. df_set_flags (DF_DEFER_INSN_RESCAN);
  2302. crossing_edges = find_rarely_executed_basic_blocks_and_crossing_edges ();
  2303. if (!crossing_edges.exists ())
  2304. return 0;
  2305. crtl->has_bb_partition = true;
  2306. /* Make sure the source of any crossing edge ends in a jump and the
  2307. destination of any crossing edge has a label. */
  2308. add_labels_and_missing_jumps (crossing_edges);
  2309. /* Convert all crossing fall_thru edges to non-crossing fall
  2310. thrus to unconditional jumps (that jump to the original fall
  2311. through dest). */
  2312. fix_up_fall_thru_edges ();
  2313. /* If the architecture does not have conditional branches that can
  2314. span all of memory, convert crossing conditional branches into
  2315. crossing unconditional branches. */
  2316. if (!HAS_LONG_COND_BRANCH)
  2317. fix_crossing_conditional_branches ();
  2318. /* If the architecture does not have unconditional branches that
  2319. can span all of memory, convert crossing unconditional branches
  2320. into indirect jumps. Since adding an indirect jump also adds
  2321. a new register usage, update the register usage information as
  2322. well. */
  2323. if (!HAS_LONG_UNCOND_BRANCH)
  2324. fix_crossing_unconditional_branches ();
  2325. update_crossing_jump_flags ();
  2326. /* Clear bb->aux fields that the above routines were using. */
  2327. clear_aux_for_blocks ();
  2328. crossing_edges.release ();
  2329. /* ??? FIXME: DF generates the bb info for a block immediately.
  2330. And by immediately, I mean *during* creation of the block.
  2331. #0 df_bb_refs_collect
  2332. #1 in df_bb_refs_record
  2333. #2 in create_basic_block_structure
  2334. Which means that the bb_has_eh_pred test in df_bb_refs_collect
  2335. will *always* fail, because no edges can have been added to the
  2336. block yet. Which of course means we don't add the right
  2337. artificial refs, which means we fail df_verify (much) later.
  2338. Cleanest solution would seem to make DF_DEFER_INSN_RESCAN imply
  2339. that we also shouldn't grab data from the new blocks those new
  2340. insns are in either. In this way one can create the block, link
  2341. it up properly, and have everything Just Work later, when deferred
  2342. insns are processed.
  2343. In the meantime, we have no other option but to throw away all
  2344. of the DF data and recompute it all. */
  2345. if (fun->eh->lp_array)
  2346. {
  2347. df_finish_pass (true);
  2348. df_scan_alloc (NULL);
  2349. df_scan_blocks ();
  2350. /* Not all post-landing pads use all of the EH_RETURN_DATA_REGNO
  2351. data. We blindly generated all of them when creating the new
  2352. landing pad. Delete those assignments we don't use. */
  2353. df_set_flags (DF_LR_RUN_DCE);
  2354. df_analyze ();
  2355. }
  2356. return 0;
  2357. }
  2358. } // anon namespace
  2359. rtl_opt_pass *
  2360. make_pass_partition_blocks (gcc::context *ctxt)
  2361. {
  2362. return new pass_partition_blocks (ctxt);
  2363. }