sched-rgn.c 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866
  1. /* Instruction scheduling pass.
  2. Copyright (C) 1992-2015 Free Software Foundation, Inc.
  3. Contributed by Michael Tiemann (tiemann@cygnus.com) Enhanced by,
  4. and currently maintained by, Jim Wilson (wilson@cygnus.com)
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation; either version 3, or (at your option) any later
  9. version.
  10. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GCC; see the file COPYING3. If not see
  16. <http://www.gnu.org/licenses/>. */
  17. /* This pass implements list scheduling within basic blocks. It is
  18. run twice: (1) after flow analysis, but before register allocation,
  19. and (2) after register allocation.
  20. The first run performs interblock scheduling, moving insns between
  21. different blocks in the same "region", and the second runs only
  22. basic block scheduling.
  23. Interblock motions performed are useful motions and speculative
  24. motions, including speculative loads. Motions requiring code
  25. duplication are not supported. The identification of motion type
  26. and the check for validity of speculative motions requires
  27. construction and analysis of the function's control flow graph.
  28. The main entry point for this pass is schedule_insns(), called for
  29. each function. The work of the scheduler is organized in three
  30. levels: (1) function level: insns are subject to splitting,
  31. control-flow-graph is constructed, regions are computed (after
  32. reload, each region is of one block), (2) region level: control
  33. flow graph attributes required for interblock scheduling are
  34. computed (dominators, reachability, etc.), data dependences and
  35. priorities are computed, and (3) block level: insns in the block
  36. are actually scheduled. */
  37. #include "config.h"
  38. #include "system.h"
  39. #include "coretypes.h"
  40. #include "tm.h"
  41. #include "diagnostic-core.h"
  42. #include "rtl.h"
  43. #include "tm_p.h"
  44. #include "hard-reg-set.h"
  45. #include "regs.h"
  46. #include "hashtab.h"
  47. #include "hash-set.h"
  48. #include "vec.h"
  49. #include "machmode.h"
  50. #include "input.h"
  51. #include "function.h"
  52. #include "profile.h"
  53. #include "flags.h"
  54. #include "insn-config.h"
  55. #include "insn-attr.h"
  56. #include "except.h"
  57. #include "recog.h"
  58. #include "params.h"
  59. #include "dominance.h"
  60. #include "cfg.h"
  61. #include "cfganal.h"
  62. #include "predict.h"
  63. #include "basic-block.h"
  64. #include "sched-int.h"
  65. #include "sel-sched.h"
  66. #include "target.h"
  67. #include "tree-pass.h"
  68. #include "dbgcnt.h"
  69. #ifdef INSN_SCHEDULING
  70. /* Some accessor macros for h_i_d members only used within this file. */
  71. #define FED_BY_SPEC_LOAD(INSN) (HID (INSN)->fed_by_spec_load)
  72. #define IS_LOAD_INSN(INSN) (HID (insn)->is_load_insn)
  73. /* nr_inter/spec counts interblock/speculative motion for the function. */
  74. static int nr_inter, nr_spec;
  75. static int is_cfg_nonregular (void);
  76. /* Number of regions in the procedure. */
  77. int nr_regions = 0;
  78. /* Same as above before adding any new regions. */
  79. static int nr_regions_initial = 0;
  80. /* Table of region descriptions. */
  81. region *rgn_table = NULL;
  82. /* Array of lists of regions' blocks. */
  83. int *rgn_bb_table = NULL;
  84. /* Topological order of blocks in the region (if b2 is reachable from
  85. b1, block_to_bb[b2] > block_to_bb[b1]). Note: A basic block is
  86. always referred to by either block or b, while its topological
  87. order name (in the region) is referred to by bb. */
  88. int *block_to_bb = NULL;
  89. /* The number of the region containing a block. */
  90. int *containing_rgn = NULL;
  91. /* ebb_head [i] - is index in rgn_bb_table of the head basic block of i'th ebb.
  92. Currently we can get a ebb only through splitting of currently
  93. scheduling block, therefore, we don't need ebb_head array for every region,
  94. hence, its sufficient to hold it for current one only. */
  95. int *ebb_head = NULL;
  96. /* The minimum probability of reaching a source block so that it will be
  97. considered for speculative scheduling. */
  98. static int min_spec_prob;
  99. static void find_single_block_region (bool);
  100. static void find_rgns (void);
  101. static bool too_large (int, int *, int *);
  102. /* Blocks of the current region being scheduled. */
  103. int current_nr_blocks;
  104. int current_blocks;
  105. /* A speculative motion requires checking live information on the path
  106. from 'source' to 'target'. The split blocks are those to be checked.
  107. After a speculative motion, live information should be modified in
  108. the 'update' blocks.
  109. Lists of split and update blocks for each candidate of the current
  110. target are in array bblst_table. */
  111. static basic_block *bblst_table;
  112. static int bblst_size, bblst_last;
  113. /* Arrays that hold the DFA state at the end of a basic block, to re-use
  114. as the initial state at the start of successor blocks. The BB_STATE
  115. array holds the actual DFA state, and BB_STATE_ARRAY[I] is a pointer
  116. into BB_STATE for basic block I. FIXME: This should be a vec. */
  117. static char *bb_state_array = NULL;
  118. static state_t *bb_state = NULL;
  119. /* Target info declarations.
  120. The block currently being scheduled is referred to as the "target" block,
  121. while other blocks in the region from which insns can be moved to the
  122. target are called "source" blocks. The candidate structure holds info
  123. about such sources: are they valid? Speculative? Etc. */
  124. typedef struct
  125. {
  126. basic_block *first_member;
  127. int nr_members;
  128. }
  129. bblst;
  130. typedef struct
  131. {
  132. char is_valid;
  133. char is_speculative;
  134. int src_prob;
  135. bblst split_bbs;
  136. bblst update_bbs;
  137. }
  138. candidate;
  139. static candidate *candidate_table;
  140. #define IS_VALID(src) (candidate_table[src].is_valid)
  141. #define IS_SPECULATIVE(src) (candidate_table[src].is_speculative)
  142. #define IS_SPECULATIVE_INSN(INSN) \
  143. (IS_SPECULATIVE (BLOCK_TO_BB (BLOCK_NUM (INSN))))
  144. #define SRC_PROB(src) ( candidate_table[src].src_prob )
  145. /* The bb being currently scheduled. */
  146. int target_bb;
  147. /* List of edges. */
  148. typedef struct
  149. {
  150. edge *first_member;
  151. int nr_members;
  152. }
  153. edgelst;
  154. static edge *edgelst_table;
  155. static int edgelst_last;
  156. static void extract_edgelst (sbitmap, edgelst *);
  157. /* Target info functions. */
  158. static void split_edges (int, int, edgelst *);
  159. static void compute_trg_info (int);
  160. void debug_candidate (int);
  161. void debug_candidates (int);
  162. /* Dominators array: dom[i] contains the sbitmap of dominators of
  163. bb i in the region. */
  164. static sbitmap *dom;
  165. /* bb 0 is the only region entry. */
  166. #define IS_RGN_ENTRY(bb) (!bb)
  167. /* Is bb_src dominated by bb_trg. */
  168. #define IS_DOMINATED(bb_src, bb_trg) \
  169. ( bitmap_bit_p (dom[bb_src], bb_trg) )
  170. /* Probability: Prob[i] is an int in [0, REG_BR_PROB_BASE] which is
  171. the probability of bb i relative to the region entry. */
  172. static int *prob;
  173. /* Bit-set of edges, where bit i stands for edge i. */
  174. typedef sbitmap edgeset;
  175. /* Number of edges in the region. */
  176. static int rgn_nr_edges;
  177. /* Array of size rgn_nr_edges. */
  178. static edge *rgn_edges;
  179. /* Mapping from each edge in the graph to its number in the rgn. */
  180. #define EDGE_TO_BIT(edge) ((int)(size_t)(edge)->aux)
  181. #define SET_EDGE_TO_BIT(edge,nr) ((edge)->aux = (void *)(size_t)(nr))
  182. /* The split edges of a source bb is different for each target
  183. bb. In order to compute this efficiently, the 'potential-split edges'
  184. are computed for each bb prior to scheduling a region. This is actually
  185. the split edges of each bb relative to the region entry.
  186. pot_split[bb] is the set of potential split edges of bb. */
  187. static edgeset *pot_split;
  188. /* For every bb, a set of its ancestor edges. */
  189. static edgeset *ancestor_edges;
  190. #define INSN_PROBABILITY(INSN) (SRC_PROB (BLOCK_TO_BB (BLOCK_NUM (INSN))))
  191. /* Speculative scheduling functions. */
  192. static int check_live_1 (int, rtx);
  193. static void update_live_1 (int, rtx);
  194. static int is_pfree (rtx, int, int);
  195. static int find_conditional_protection (rtx, int);
  196. static int is_conditionally_protected (rtx, int, int);
  197. static int is_prisky (rtx, int, int);
  198. static int is_exception_free (rtx, int, int);
  199. static bool sets_likely_spilled (rtx);
  200. static void sets_likely_spilled_1 (rtx, const_rtx, void *);
  201. static void add_branch_dependences (rtx_insn *, rtx_insn *);
  202. static void compute_block_dependences (int);
  203. static void schedule_region (int);
  204. static void concat_insn_mem_list (rtx_insn_list *, rtx_expr_list *,
  205. rtx_insn_list **, rtx_expr_list **);
  206. static void propagate_deps (int, struct deps_desc *);
  207. static void free_pending_lists (void);
  208. /* Functions for construction of the control flow graph. */
  209. /* Return 1 if control flow graph should not be constructed, 0 otherwise.
  210. We decide not to build the control flow graph if there is possibly more
  211. than one entry to the function, if computed branches exist, if we
  212. have nonlocal gotos, or if we have an unreachable loop. */
  213. static int
  214. is_cfg_nonregular (void)
  215. {
  216. basic_block b;
  217. rtx_insn *insn;
  218. /* If we have a label that could be the target of a nonlocal goto, then
  219. the cfg is not well structured. */
  220. if (nonlocal_goto_handler_labels)
  221. return 1;
  222. /* If we have any forced labels, then the cfg is not well structured. */
  223. if (forced_labels)
  224. return 1;
  225. /* If we have exception handlers, then we consider the cfg not well
  226. structured. ?!? We should be able to handle this now that we
  227. compute an accurate cfg for EH. */
  228. if (current_function_has_exception_handlers ())
  229. return 1;
  230. /* If we have insns which refer to labels as non-jumped-to operands,
  231. then we consider the cfg not well structured. */
  232. FOR_EACH_BB_FN (b, cfun)
  233. FOR_BB_INSNS (b, insn)
  234. {
  235. rtx note, set, dest;
  236. rtx_insn *next;
  237. /* If this function has a computed jump, then we consider the cfg
  238. not well structured. */
  239. if (JUMP_P (insn) && computed_jump_p (insn))
  240. return 1;
  241. if (!INSN_P (insn))
  242. continue;
  243. note = find_reg_note (insn, REG_LABEL_OPERAND, NULL_RTX);
  244. if (note == NULL_RTX)
  245. continue;
  246. /* For that label not to be seen as a referred-to label, this
  247. must be a single-set which is feeding a jump *only*. This
  248. could be a conditional jump with the label split off for
  249. machine-specific reasons or a casesi/tablejump. */
  250. next = next_nonnote_insn (insn);
  251. if (next == NULL_RTX
  252. || !JUMP_P (next)
  253. || (JUMP_LABEL (next) != XEXP (note, 0)
  254. && find_reg_note (next, REG_LABEL_TARGET,
  255. XEXP (note, 0)) == NULL_RTX)
  256. || BLOCK_FOR_INSN (insn) != BLOCK_FOR_INSN (next))
  257. return 1;
  258. set = single_set (insn);
  259. if (set == NULL_RTX)
  260. return 1;
  261. dest = SET_DEST (set);
  262. if (!REG_P (dest) || !dead_or_set_p (next, dest))
  263. return 1;
  264. }
  265. /* Unreachable loops with more than one basic block are detected
  266. during the DFS traversal in find_rgns.
  267. Unreachable loops with a single block are detected here. This
  268. test is redundant with the one in find_rgns, but it's much
  269. cheaper to go ahead and catch the trivial case here. */
  270. FOR_EACH_BB_FN (b, cfun)
  271. {
  272. if (EDGE_COUNT (b->preds) == 0
  273. || (single_pred_p (b)
  274. && single_pred (b) == b))
  275. return 1;
  276. }
  277. /* All the tests passed. Consider the cfg well structured. */
  278. return 0;
  279. }
  280. /* Extract list of edges from a bitmap containing EDGE_TO_BIT bits. */
  281. static void
  282. extract_edgelst (sbitmap set, edgelst *el)
  283. {
  284. unsigned int i = 0;
  285. sbitmap_iterator sbi;
  286. /* edgelst table space is reused in each call to extract_edgelst. */
  287. edgelst_last = 0;
  288. el->first_member = &edgelst_table[edgelst_last];
  289. el->nr_members = 0;
  290. /* Iterate over each word in the bitset. */
  291. EXECUTE_IF_SET_IN_BITMAP (set, 0, i, sbi)
  292. {
  293. edgelst_table[edgelst_last++] = rgn_edges[i];
  294. el->nr_members++;
  295. }
  296. }
  297. /* Functions for the construction of regions. */
  298. /* Print the regions, for debugging purposes. Callable from debugger. */
  299. DEBUG_FUNCTION void
  300. debug_regions (void)
  301. {
  302. int rgn, bb;
  303. fprintf (sched_dump, "\n;; ------------ REGIONS ----------\n\n");
  304. for (rgn = 0; rgn < nr_regions; rgn++)
  305. {
  306. fprintf (sched_dump, ";;\trgn %d nr_blocks %d:\n", rgn,
  307. rgn_table[rgn].rgn_nr_blocks);
  308. fprintf (sched_dump, ";;\tbb/block: ");
  309. /* We don't have ebb_head initialized yet, so we can't use
  310. BB_TO_BLOCK (). */
  311. current_blocks = RGN_BLOCKS (rgn);
  312. for (bb = 0; bb < rgn_table[rgn].rgn_nr_blocks; bb++)
  313. fprintf (sched_dump, " %d/%d ", bb, rgn_bb_table[current_blocks + bb]);
  314. fprintf (sched_dump, "\n\n");
  315. }
  316. }
  317. /* Print the region's basic blocks. */
  318. DEBUG_FUNCTION void
  319. debug_region (int rgn)
  320. {
  321. int bb;
  322. fprintf (stderr, "\n;; ------------ REGION %d ----------\n\n", rgn);
  323. fprintf (stderr, ";;\trgn %d nr_blocks %d:\n", rgn,
  324. rgn_table[rgn].rgn_nr_blocks);
  325. fprintf (stderr, ";;\tbb/block: ");
  326. /* We don't have ebb_head initialized yet, so we can't use
  327. BB_TO_BLOCK (). */
  328. current_blocks = RGN_BLOCKS (rgn);
  329. for (bb = 0; bb < rgn_table[rgn].rgn_nr_blocks; bb++)
  330. fprintf (stderr, " %d/%d ", bb, rgn_bb_table[current_blocks + bb]);
  331. fprintf (stderr, "\n\n");
  332. for (bb = 0; bb < rgn_table[rgn].rgn_nr_blocks; bb++)
  333. {
  334. dump_bb (stderr,
  335. BASIC_BLOCK_FOR_FN (cfun, rgn_bb_table[current_blocks + bb]),
  336. 0, TDF_SLIM | TDF_BLOCKS);
  337. fprintf (stderr, "\n");
  338. }
  339. fprintf (stderr, "\n");
  340. }
  341. /* True when a bb with index BB_INDEX contained in region RGN. */
  342. static bool
  343. bb_in_region_p (int bb_index, int rgn)
  344. {
  345. int i;
  346. for (i = 0; i < rgn_table[rgn].rgn_nr_blocks; i++)
  347. if (rgn_bb_table[current_blocks + i] == bb_index)
  348. return true;
  349. return false;
  350. }
  351. /* Dump region RGN to file F using dot syntax. */
  352. void
  353. dump_region_dot (FILE *f, int rgn)
  354. {
  355. int i;
  356. fprintf (f, "digraph Region_%d {\n", rgn);
  357. /* We don't have ebb_head initialized yet, so we can't use
  358. BB_TO_BLOCK (). */
  359. current_blocks = RGN_BLOCKS (rgn);
  360. for (i = 0; i < rgn_table[rgn].rgn_nr_blocks; i++)
  361. {
  362. edge e;
  363. edge_iterator ei;
  364. int src_bb_num = rgn_bb_table[current_blocks + i];
  365. basic_block bb = BASIC_BLOCK_FOR_FN (cfun, src_bb_num);
  366. FOR_EACH_EDGE (e, ei, bb->succs)
  367. if (bb_in_region_p (e->dest->index, rgn))
  368. fprintf (f, "\t%d -> %d\n", src_bb_num, e->dest->index);
  369. }
  370. fprintf (f, "}\n");
  371. }
  372. /* The same, but first open a file specified by FNAME. */
  373. void
  374. dump_region_dot_file (const char *fname, int rgn)
  375. {
  376. FILE *f = fopen (fname, "wt");
  377. dump_region_dot (f, rgn);
  378. fclose (f);
  379. }
  380. /* Build a single block region for each basic block in the function.
  381. This allows for using the same code for interblock and basic block
  382. scheduling. */
  383. static void
  384. find_single_block_region (bool ebbs_p)
  385. {
  386. basic_block bb, ebb_start;
  387. int i = 0;
  388. nr_regions = 0;
  389. if (ebbs_p) {
  390. int probability_cutoff;
  391. if (profile_info && flag_branch_probabilities)
  392. probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY_FEEDBACK);
  393. else
  394. probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY);
  395. probability_cutoff = REG_BR_PROB_BASE / 100 * probability_cutoff;
  396. FOR_EACH_BB_FN (ebb_start, cfun)
  397. {
  398. RGN_NR_BLOCKS (nr_regions) = 0;
  399. RGN_BLOCKS (nr_regions) = i;
  400. RGN_DONT_CALC_DEPS (nr_regions) = 0;
  401. RGN_HAS_REAL_EBB (nr_regions) = 0;
  402. for (bb = ebb_start; ; bb = bb->next_bb)
  403. {
  404. edge e;
  405. rgn_bb_table[i] = bb->index;
  406. RGN_NR_BLOCKS (nr_regions)++;
  407. CONTAINING_RGN (bb->index) = nr_regions;
  408. BLOCK_TO_BB (bb->index) = i - RGN_BLOCKS (nr_regions);
  409. i++;
  410. if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
  411. || LABEL_P (BB_HEAD (bb->next_bb)))
  412. break;
  413. e = find_fallthru_edge (bb->succs);
  414. if (! e)
  415. break;
  416. if (e->probability <= probability_cutoff)
  417. break;
  418. }
  419. ebb_start = bb;
  420. nr_regions++;
  421. }
  422. }
  423. else
  424. FOR_EACH_BB_FN (bb, cfun)
  425. {
  426. rgn_bb_table[nr_regions] = bb->index;
  427. RGN_NR_BLOCKS (nr_regions) = 1;
  428. RGN_BLOCKS (nr_regions) = nr_regions;
  429. RGN_DONT_CALC_DEPS (nr_regions) = 0;
  430. RGN_HAS_REAL_EBB (nr_regions) = 0;
  431. CONTAINING_RGN (bb->index) = nr_regions;
  432. BLOCK_TO_BB (bb->index) = 0;
  433. nr_regions++;
  434. }
  435. }
  436. /* Estimate number of the insns in the BB. */
  437. static int
  438. rgn_estimate_number_of_insns (basic_block bb)
  439. {
  440. int count;
  441. count = INSN_LUID (BB_END (bb)) - INSN_LUID (BB_HEAD (bb));
  442. if (MAY_HAVE_DEBUG_INSNS)
  443. {
  444. rtx_insn *insn;
  445. FOR_BB_INSNS (bb, insn)
  446. if (DEBUG_INSN_P (insn))
  447. count--;
  448. }
  449. return count;
  450. }
  451. /* Update number of blocks and the estimate for number of insns
  452. in the region. Return true if the region is "too large" for interblock
  453. scheduling (compile time considerations). */
  454. static bool
  455. too_large (int block, int *num_bbs, int *num_insns)
  456. {
  457. (*num_bbs)++;
  458. (*num_insns) += (common_sched_info->estimate_number_of_insns
  459. (BASIC_BLOCK_FOR_FN (cfun, block)));
  460. return ((*num_bbs > PARAM_VALUE (PARAM_MAX_SCHED_REGION_BLOCKS))
  461. || (*num_insns > PARAM_VALUE (PARAM_MAX_SCHED_REGION_INSNS)));
  462. }
  463. /* Update_loop_relations(blk, hdr): Check if the loop headed by max_hdr[blk]
  464. is still an inner loop. Put in max_hdr[blk] the header of the most inner
  465. loop containing blk. */
  466. #define UPDATE_LOOP_RELATIONS(blk, hdr) \
  467. { \
  468. if (max_hdr[blk] == -1) \
  469. max_hdr[blk] = hdr; \
  470. else if (dfs_nr[max_hdr[blk]] > dfs_nr[hdr]) \
  471. bitmap_clear_bit (inner, hdr); \
  472. else if (dfs_nr[max_hdr[blk]] < dfs_nr[hdr]) \
  473. { \
  474. bitmap_clear_bit (inner,max_hdr[blk]); \
  475. max_hdr[blk] = hdr; \
  476. } \
  477. }
  478. /* Find regions for interblock scheduling.
  479. A region for scheduling can be:
  480. * A loop-free procedure, or
  481. * A reducible inner loop, or
  482. * A basic block not contained in any other region.
  483. ?!? In theory we could build other regions based on extended basic
  484. blocks or reverse extended basic blocks. Is it worth the trouble?
  485. Loop blocks that form a region are put into the region's block list
  486. in topological order.
  487. This procedure stores its results into the following global (ick) variables
  488. * rgn_nr
  489. * rgn_table
  490. * rgn_bb_table
  491. * block_to_bb
  492. * containing region
  493. We use dominator relationships to avoid making regions out of non-reducible
  494. loops.
  495. This procedure needs to be converted to work on pred/succ lists instead
  496. of edge tables. That would simplify it somewhat. */
  497. static void
  498. haifa_find_rgns (void)
  499. {
  500. int *max_hdr, *dfs_nr, *degree;
  501. char no_loops = 1;
  502. int node, child, loop_head, i, head, tail;
  503. int count = 0, sp, idx = 0;
  504. edge_iterator current_edge;
  505. edge_iterator *stack;
  506. int num_bbs, num_insns, unreachable;
  507. int too_large_failure;
  508. basic_block bb;
  509. /* Note if a block is a natural loop header. */
  510. sbitmap header;
  511. /* Note if a block is a natural inner loop header. */
  512. sbitmap inner;
  513. /* Note if a block is in the block queue. */
  514. sbitmap in_queue;
  515. /* Note if a block is in the block queue. */
  516. sbitmap in_stack;
  517. /* Perform a DFS traversal of the cfg. Identify loop headers, inner loops
  518. and a mapping from block to its loop header (if the block is contained
  519. in a loop, else -1).
  520. Store results in HEADER, INNER, and MAX_HDR respectively, these will
  521. be used as inputs to the second traversal.
  522. STACK, SP and DFS_NR are only used during the first traversal. */
  523. /* Allocate and initialize variables for the first traversal. */
  524. max_hdr = XNEWVEC (int, last_basic_block_for_fn (cfun));
  525. dfs_nr = XCNEWVEC (int, last_basic_block_for_fn (cfun));
  526. stack = XNEWVEC (edge_iterator, n_edges_for_fn (cfun));
  527. inner = sbitmap_alloc (last_basic_block_for_fn (cfun));
  528. bitmap_ones (inner);
  529. header = sbitmap_alloc (last_basic_block_for_fn (cfun));
  530. bitmap_clear (header);
  531. in_queue = sbitmap_alloc (last_basic_block_for_fn (cfun));
  532. bitmap_clear (in_queue);
  533. in_stack = sbitmap_alloc (last_basic_block_for_fn (cfun));
  534. bitmap_clear (in_stack);
  535. for (i = 0; i < last_basic_block_for_fn (cfun); i++)
  536. max_hdr[i] = -1;
  537. #define EDGE_PASSED(E) (ei_end_p ((E)) || ei_edge ((E))->aux)
  538. #define SET_EDGE_PASSED(E) (ei_edge ((E))->aux = ei_edge ((E)))
  539. /* DFS traversal to find inner loops in the cfg. */
  540. current_edge = ei_start (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun))->succs);
  541. sp = -1;
  542. while (1)
  543. {
  544. if (EDGE_PASSED (current_edge))
  545. {
  546. /* We have reached a leaf node or a node that was already
  547. processed. Pop edges off the stack until we find
  548. an edge that has not yet been processed. */
  549. while (sp >= 0 && EDGE_PASSED (current_edge))
  550. {
  551. /* Pop entry off the stack. */
  552. current_edge = stack[sp--];
  553. node = ei_edge (current_edge)->src->index;
  554. gcc_assert (node != ENTRY_BLOCK);
  555. child = ei_edge (current_edge)->dest->index;
  556. gcc_assert (child != EXIT_BLOCK);
  557. bitmap_clear_bit (in_stack, child);
  558. if (max_hdr[child] >= 0 && bitmap_bit_p (in_stack, max_hdr[child]))
  559. UPDATE_LOOP_RELATIONS (node, max_hdr[child]);
  560. ei_next (&current_edge);
  561. }
  562. /* See if have finished the DFS tree traversal. */
  563. if (sp < 0 && EDGE_PASSED (current_edge))
  564. break;
  565. /* Nope, continue the traversal with the popped node. */
  566. continue;
  567. }
  568. /* Process a node. */
  569. node = ei_edge (current_edge)->src->index;
  570. gcc_assert (node != ENTRY_BLOCK);
  571. bitmap_set_bit (in_stack, node);
  572. dfs_nr[node] = ++count;
  573. /* We don't traverse to the exit block. */
  574. child = ei_edge (current_edge)->dest->index;
  575. if (child == EXIT_BLOCK)
  576. {
  577. SET_EDGE_PASSED (current_edge);
  578. ei_next (&current_edge);
  579. continue;
  580. }
  581. /* If the successor is in the stack, then we've found a loop.
  582. Mark the loop, if it is not a natural loop, then it will
  583. be rejected during the second traversal. */
  584. if (bitmap_bit_p (in_stack, child))
  585. {
  586. no_loops = 0;
  587. bitmap_set_bit (header, child);
  588. UPDATE_LOOP_RELATIONS (node, child);
  589. SET_EDGE_PASSED (current_edge);
  590. ei_next (&current_edge);
  591. continue;
  592. }
  593. /* If the child was already visited, then there is no need to visit
  594. it again. Just update the loop relationships and restart
  595. with a new edge. */
  596. if (dfs_nr[child])
  597. {
  598. if (max_hdr[child] >= 0 && bitmap_bit_p (in_stack, max_hdr[child]))
  599. UPDATE_LOOP_RELATIONS (node, max_hdr[child]);
  600. SET_EDGE_PASSED (current_edge);
  601. ei_next (&current_edge);
  602. continue;
  603. }
  604. /* Push an entry on the stack and continue DFS traversal. */
  605. stack[++sp] = current_edge;
  606. SET_EDGE_PASSED (current_edge);
  607. current_edge = ei_start (ei_edge (current_edge)->dest->succs);
  608. }
  609. /* Reset ->aux field used by EDGE_PASSED. */
  610. FOR_ALL_BB_FN (bb, cfun)
  611. {
  612. edge_iterator ei;
  613. edge e;
  614. FOR_EACH_EDGE (e, ei, bb->succs)
  615. e->aux = NULL;
  616. }
  617. /* Another check for unreachable blocks. The earlier test in
  618. is_cfg_nonregular only finds unreachable blocks that do not
  619. form a loop.
  620. The DFS traversal will mark every block that is reachable from
  621. the entry node by placing a nonzero value in dfs_nr. Thus if
  622. dfs_nr is zero for any block, then it must be unreachable. */
  623. unreachable = 0;
  624. FOR_EACH_BB_FN (bb, cfun)
  625. if (dfs_nr[bb->index] == 0)
  626. {
  627. unreachable = 1;
  628. break;
  629. }
  630. /* Gross. To avoid wasting memory, the second pass uses the dfs_nr array
  631. to hold degree counts. */
  632. degree = dfs_nr;
  633. FOR_EACH_BB_FN (bb, cfun)
  634. degree[bb->index] = EDGE_COUNT (bb->preds);
  635. /* Do not perform region scheduling if there are any unreachable
  636. blocks. */
  637. if (!unreachable)
  638. {
  639. int *queue, *degree1 = NULL;
  640. /* We use EXTENDED_RGN_HEADER as an addition to HEADER and put
  641. there basic blocks, which are forced to be region heads.
  642. This is done to try to assemble few smaller regions
  643. from a too_large region. */
  644. sbitmap extended_rgn_header = NULL;
  645. bool extend_regions_p;
  646. if (no_loops)
  647. bitmap_set_bit (header, 0);
  648. /* Second traversal:find reducible inner loops and topologically sort
  649. block of each region. */
  650. queue = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
  651. extend_regions_p = PARAM_VALUE (PARAM_MAX_SCHED_EXTEND_REGIONS_ITERS) > 0;
  652. if (extend_regions_p)
  653. {
  654. degree1 = XNEWVEC (int, last_basic_block_for_fn (cfun));
  655. extended_rgn_header =
  656. sbitmap_alloc (last_basic_block_for_fn (cfun));
  657. bitmap_clear (extended_rgn_header);
  658. }
  659. /* Find blocks which are inner loop headers. We still have non-reducible
  660. loops to consider at this point. */
  661. FOR_EACH_BB_FN (bb, cfun)
  662. {
  663. if (bitmap_bit_p (header, bb->index) && bitmap_bit_p (inner, bb->index))
  664. {
  665. edge e;
  666. edge_iterator ei;
  667. basic_block jbb;
  668. /* Now check that the loop is reducible. We do this separate
  669. from finding inner loops so that we do not find a reducible
  670. loop which contains an inner non-reducible loop.
  671. A simple way to find reducible/natural loops is to verify
  672. that each block in the loop is dominated by the loop
  673. header.
  674. If there exists a block that is not dominated by the loop
  675. header, then the block is reachable from outside the loop
  676. and thus the loop is not a natural loop. */
  677. FOR_EACH_BB_FN (jbb, cfun)
  678. {
  679. /* First identify blocks in the loop, except for the loop
  680. entry block. */
  681. if (bb->index == max_hdr[jbb->index] && bb != jbb)
  682. {
  683. /* Now verify that the block is dominated by the loop
  684. header. */
  685. if (!dominated_by_p (CDI_DOMINATORS, jbb, bb))
  686. break;
  687. }
  688. }
  689. /* If we exited the loop early, then I is the header of
  690. a non-reducible loop and we should quit processing it
  691. now. */
  692. if (jbb != EXIT_BLOCK_PTR_FOR_FN (cfun))
  693. continue;
  694. /* I is a header of an inner loop, or block 0 in a subroutine
  695. with no loops at all. */
  696. head = tail = -1;
  697. too_large_failure = 0;
  698. loop_head = max_hdr[bb->index];
  699. if (extend_regions_p)
  700. /* We save degree in case when we meet a too_large region
  701. and cancel it. We need a correct degree later when
  702. calling extend_rgns. */
  703. memcpy (degree1, degree,
  704. last_basic_block_for_fn (cfun) * sizeof (int));
  705. /* Decrease degree of all I's successors for topological
  706. ordering. */
  707. FOR_EACH_EDGE (e, ei, bb->succs)
  708. if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
  709. --degree[e->dest->index];
  710. /* Estimate # insns, and count # blocks in the region. */
  711. num_bbs = 1;
  712. num_insns = common_sched_info->estimate_number_of_insns (bb);
  713. /* Find all loop latches (blocks with back edges to the loop
  714. header) or all the leaf blocks in the cfg has no loops.
  715. Place those blocks into the queue. */
  716. if (no_loops)
  717. {
  718. FOR_EACH_BB_FN (jbb, cfun)
  719. /* Leaf nodes have only a single successor which must
  720. be EXIT_BLOCK. */
  721. if (single_succ_p (jbb)
  722. && single_succ (jbb) == EXIT_BLOCK_PTR_FOR_FN (cfun))
  723. {
  724. queue[++tail] = jbb->index;
  725. bitmap_set_bit (in_queue, jbb->index);
  726. if (too_large (jbb->index, &num_bbs, &num_insns))
  727. {
  728. too_large_failure = 1;
  729. break;
  730. }
  731. }
  732. }
  733. else
  734. {
  735. edge e;
  736. FOR_EACH_EDGE (e, ei, bb->preds)
  737. {
  738. if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
  739. continue;
  740. node = e->src->index;
  741. if (max_hdr[node] == loop_head && node != bb->index)
  742. {
  743. /* This is a loop latch. */
  744. queue[++tail] = node;
  745. bitmap_set_bit (in_queue, node);
  746. if (too_large (node, &num_bbs, &num_insns))
  747. {
  748. too_large_failure = 1;
  749. break;
  750. }
  751. }
  752. }
  753. }
  754. /* Now add all the blocks in the loop to the queue.
  755. We know the loop is a natural loop; however the algorithm
  756. above will not always mark certain blocks as being in the
  757. loop. Consider:
  758. node children
  759. a b,c
  760. b c
  761. c a,d
  762. d b
  763. The algorithm in the DFS traversal may not mark B & D as part
  764. of the loop (i.e. they will not have max_hdr set to A).
  765. We know they can not be loop latches (else they would have
  766. had max_hdr set since they'd have a backedge to a dominator
  767. block). So we don't need them on the initial queue.
  768. We know they are part of the loop because they are dominated
  769. by the loop header and can be reached by a backwards walk of
  770. the edges starting with nodes on the initial queue.
  771. It is safe and desirable to include those nodes in the
  772. loop/scheduling region. To do so we would need to decrease
  773. the degree of a node if it is the target of a backedge
  774. within the loop itself as the node is placed in the queue.
  775. We do not do this because I'm not sure that the actual
  776. scheduling code will properly handle this case. ?!? */
  777. while (head < tail && !too_large_failure)
  778. {
  779. edge e;
  780. child = queue[++head];
  781. FOR_EACH_EDGE (e, ei,
  782. BASIC_BLOCK_FOR_FN (cfun, child)->preds)
  783. {
  784. node = e->src->index;
  785. /* See discussion above about nodes not marked as in
  786. this loop during the initial DFS traversal. */
  787. if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun)
  788. || max_hdr[node] != loop_head)
  789. {
  790. tail = -1;
  791. break;
  792. }
  793. else if (!bitmap_bit_p (in_queue, node) && node != bb->index)
  794. {
  795. queue[++tail] = node;
  796. bitmap_set_bit (in_queue, node);
  797. if (too_large (node, &num_bbs, &num_insns))
  798. {
  799. too_large_failure = 1;
  800. break;
  801. }
  802. }
  803. }
  804. }
  805. if (tail >= 0 && !too_large_failure)
  806. {
  807. /* Place the loop header into list of region blocks. */
  808. degree[bb->index] = -1;
  809. rgn_bb_table[idx] = bb->index;
  810. RGN_NR_BLOCKS (nr_regions) = num_bbs;
  811. RGN_BLOCKS (nr_regions) = idx++;
  812. RGN_DONT_CALC_DEPS (nr_regions) = 0;
  813. RGN_HAS_REAL_EBB (nr_regions) = 0;
  814. CONTAINING_RGN (bb->index) = nr_regions;
  815. BLOCK_TO_BB (bb->index) = count = 0;
  816. /* Remove blocks from queue[] when their in degree
  817. becomes zero. Repeat until no blocks are left on the
  818. list. This produces a topological list of blocks in
  819. the region. */
  820. while (tail >= 0)
  821. {
  822. if (head < 0)
  823. head = tail;
  824. child = queue[head];
  825. if (degree[child] == 0)
  826. {
  827. edge e;
  828. degree[child] = -1;
  829. rgn_bb_table[idx++] = child;
  830. BLOCK_TO_BB (child) = ++count;
  831. CONTAINING_RGN (child) = nr_regions;
  832. queue[head] = queue[tail--];
  833. FOR_EACH_EDGE (e, ei,
  834. BASIC_BLOCK_FOR_FN (cfun,
  835. child)->succs)
  836. if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
  837. --degree[e->dest->index];
  838. }
  839. else
  840. --head;
  841. }
  842. ++nr_regions;
  843. }
  844. else if (extend_regions_p)
  845. {
  846. /* Restore DEGREE. */
  847. int *t = degree;
  848. degree = degree1;
  849. degree1 = t;
  850. /* And force successors of BB to be region heads.
  851. This may provide several smaller regions instead
  852. of one too_large region. */
  853. FOR_EACH_EDGE (e, ei, bb->succs)
  854. if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
  855. bitmap_set_bit (extended_rgn_header, e->dest->index);
  856. }
  857. }
  858. }
  859. free (queue);
  860. if (extend_regions_p)
  861. {
  862. free (degree1);
  863. bitmap_ior (header, header, extended_rgn_header);
  864. sbitmap_free (extended_rgn_header);
  865. extend_rgns (degree, &idx, header, max_hdr);
  866. }
  867. }
  868. /* Any block that did not end up in a region is placed into a region
  869. by itself. */
  870. FOR_EACH_BB_FN (bb, cfun)
  871. if (degree[bb->index] >= 0)
  872. {
  873. rgn_bb_table[idx] = bb->index;
  874. RGN_NR_BLOCKS (nr_regions) = 1;
  875. RGN_BLOCKS (nr_regions) = idx++;
  876. RGN_DONT_CALC_DEPS (nr_regions) = 0;
  877. RGN_HAS_REAL_EBB (nr_regions) = 0;
  878. CONTAINING_RGN (bb->index) = nr_regions++;
  879. BLOCK_TO_BB (bb->index) = 0;
  880. }
  881. free (max_hdr);
  882. free (degree);
  883. free (stack);
  884. sbitmap_free (header);
  885. sbitmap_free (inner);
  886. sbitmap_free (in_queue);
  887. sbitmap_free (in_stack);
  888. }
  889. /* Wrapper function.
  890. If FLAG_SEL_SCHED_PIPELINING is set, then use custom function to form
  891. regions. Otherwise just call find_rgns_haifa. */
  892. static void
  893. find_rgns (void)
  894. {
  895. if (sel_sched_p () && flag_sel_sched_pipelining)
  896. sel_find_rgns ();
  897. else
  898. haifa_find_rgns ();
  899. }
  900. static int gather_region_statistics (int **);
  901. static void print_region_statistics (int *, int, int *, int);
  902. /* Calculate the histogram that shows the number of regions having the
  903. given number of basic blocks, and store it in the RSP array. Return
  904. the size of this array. */
  905. static int
  906. gather_region_statistics (int **rsp)
  907. {
  908. int i, *a = 0, a_sz = 0;
  909. /* a[i] is the number of regions that have (i + 1) basic blocks. */
  910. for (i = 0; i < nr_regions; i++)
  911. {
  912. int nr_blocks = RGN_NR_BLOCKS (i);
  913. gcc_assert (nr_blocks >= 1);
  914. if (nr_blocks > a_sz)
  915. {
  916. a = XRESIZEVEC (int, a, nr_blocks);
  917. do
  918. a[a_sz++] = 0;
  919. while (a_sz != nr_blocks);
  920. }
  921. a[nr_blocks - 1]++;
  922. }
  923. *rsp = a;
  924. return a_sz;
  925. }
  926. /* Print regions statistics. S1 and S2 denote the data before and after
  927. calling extend_rgns, respectively. */
  928. static void
  929. print_region_statistics (int *s1, int s1_sz, int *s2, int s2_sz)
  930. {
  931. int i;
  932. /* We iterate until s2_sz because extend_rgns does not decrease
  933. the maximal region size. */
  934. for (i = 1; i < s2_sz; i++)
  935. {
  936. int n1, n2;
  937. n2 = s2[i];
  938. if (n2 == 0)
  939. continue;
  940. if (i >= s1_sz)
  941. n1 = 0;
  942. else
  943. n1 = s1[i];
  944. fprintf (sched_dump, ";; Region extension statistics: size %d: " \
  945. "was %d + %d more\n", i + 1, n1, n2 - n1);
  946. }
  947. }
  948. /* Extend regions.
  949. DEGREE - Array of incoming edge count, considering only
  950. the edges, that don't have their sources in formed regions yet.
  951. IDXP - pointer to the next available index in rgn_bb_table.
  952. HEADER - set of all region heads.
  953. LOOP_HDR - mapping from block to the containing loop
  954. (two blocks can reside within one region if they have
  955. the same loop header). */
  956. void
  957. extend_rgns (int *degree, int *idxp, sbitmap header, int *loop_hdr)
  958. {
  959. int *order, i, rescan = 0, idx = *idxp, iter = 0, max_iter, *max_hdr;
  960. int nblocks = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS;
  961. max_iter = PARAM_VALUE (PARAM_MAX_SCHED_EXTEND_REGIONS_ITERS);
  962. max_hdr = XNEWVEC (int, last_basic_block_for_fn (cfun));
  963. order = XNEWVEC (int, last_basic_block_for_fn (cfun));
  964. post_order_compute (order, false, false);
  965. for (i = nblocks - 1; i >= 0; i--)
  966. {
  967. int bbn = order[i];
  968. if (degree[bbn] >= 0)
  969. {
  970. max_hdr[bbn] = bbn;
  971. rescan = 1;
  972. }
  973. else
  974. /* This block already was processed in find_rgns. */
  975. max_hdr[bbn] = -1;
  976. }
  977. /* The idea is to topologically walk through CFG in top-down order.
  978. During the traversal, if all the predecessors of a node are
  979. marked to be in the same region (they all have the same max_hdr),
  980. then current node is also marked to be a part of that region.
  981. Otherwise the node starts its own region.
  982. CFG should be traversed until no further changes are made. On each
  983. iteration the set of the region heads is extended (the set of those
  984. blocks that have max_hdr[bbi] == bbi). This set is upper bounded by the
  985. set of all basic blocks, thus the algorithm is guaranteed to
  986. terminate. */
  987. while (rescan && iter < max_iter)
  988. {
  989. rescan = 0;
  990. for (i = nblocks - 1; i >= 0; i--)
  991. {
  992. edge e;
  993. edge_iterator ei;
  994. int bbn = order[i];
  995. if (max_hdr[bbn] != -1 && !bitmap_bit_p (header, bbn))
  996. {
  997. int hdr = -1;
  998. FOR_EACH_EDGE (e, ei, BASIC_BLOCK_FOR_FN (cfun, bbn)->preds)
  999. {
  1000. int predn = e->src->index;
  1001. if (predn != ENTRY_BLOCK
  1002. /* If pred wasn't processed in find_rgns. */
  1003. && max_hdr[predn] != -1
  1004. /* And pred and bb reside in the same loop.
  1005. (Or out of any loop). */
  1006. && loop_hdr[bbn] == loop_hdr[predn])
  1007. {
  1008. if (hdr == -1)
  1009. /* Then bb extends the containing region of pred. */
  1010. hdr = max_hdr[predn];
  1011. else if (hdr != max_hdr[predn])
  1012. /* Too bad, there are at least two predecessors
  1013. that reside in different regions. Thus, BB should
  1014. begin its own region. */
  1015. {
  1016. hdr = bbn;
  1017. break;
  1018. }
  1019. }
  1020. else
  1021. /* BB starts its own region. */
  1022. {
  1023. hdr = bbn;
  1024. break;
  1025. }
  1026. }
  1027. if (hdr == bbn)
  1028. {
  1029. /* If BB start its own region,
  1030. update set of headers with BB. */
  1031. bitmap_set_bit (header, bbn);
  1032. rescan = 1;
  1033. }
  1034. else
  1035. gcc_assert (hdr != -1);
  1036. max_hdr[bbn] = hdr;
  1037. }
  1038. }
  1039. iter++;
  1040. }
  1041. /* Statistics were gathered on the SPEC2000 package of tests with
  1042. mainline weekly snapshot gcc-4.1-20051015 on ia64.
  1043. Statistics for SPECint:
  1044. 1 iteration : 1751 cases (38.7%)
  1045. 2 iterations: 2770 cases (61.3%)
  1046. Blocks wrapped in regions by find_rgns without extension: 18295 blocks
  1047. Blocks wrapped in regions by 2 iterations in extend_rgns: 23821 blocks
  1048. (We don't count single block regions here).
  1049. Statistics for SPECfp:
  1050. 1 iteration : 621 cases (35.9%)
  1051. 2 iterations: 1110 cases (64.1%)
  1052. Blocks wrapped in regions by find_rgns without extension: 6476 blocks
  1053. Blocks wrapped in regions by 2 iterations in extend_rgns: 11155 blocks
  1054. (We don't count single block regions here).
  1055. By default we do at most 2 iterations.
  1056. This can be overridden with max-sched-extend-regions-iters parameter:
  1057. 0 - disable region extension,
  1058. N > 0 - do at most N iterations. */
  1059. if (sched_verbose && iter != 0)
  1060. fprintf (sched_dump, ";; Region extension iterations: %d%s\n", iter,
  1061. rescan ? "... failed" : "");
  1062. if (!rescan && iter != 0)
  1063. {
  1064. int *s1 = NULL, s1_sz = 0;
  1065. /* Save the old statistics for later printout. */
  1066. if (sched_verbose >= 6)
  1067. s1_sz = gather_region_statistics (&s1);
  1068. /* We have succeeded. Now assemble the regions. */
  1069. for (i = nblocks - 1; i >= 0; i--)
  1070. {
  1071. int bbn = order[i];
  1072. if (max_hdr[bbn] == bbn)
  1073. /* BBN is a region head. */
  1074. {
  1075. edge e;
  1076. edge_iterator ei;
  1077. int num_bbs = 0, j, num_insns = 0, large;
  1078. large = too_large (bbn, &num_bbs, &num_insns);
  1079. degree[bbn] = -1;
  1080. rgn_bb_table[idx] = bbn;
  1081. RGN_BLOCKS (nr_regions) = idx++;
  1082. RGN_DONT_CALC_DEPS (nr_regions) = 0;
  1083. RGN_HAS_REAL_EBB (nr_regions) = 0;
  1084. CONTAINING_RGN (bbn) = nr_regions;
  1085. BLOCK_TO_BB (bbn) = 0;
  1086. FOR_EACH_EDGE (e, ei, BASIC_BLOCK_FOR_FN (cfun, bbn)->succs)
  1087. if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
  1088. degree[e->dest->index]--;
  1089. if (!large)
  1090. /* Here we check whether the region is too_large. */
  1091. for (j = i - 1; j >= 0; j--)
  1092. {
  1093. int succn = order[j];
  1094. if (max_hdr[succn] == bbn)
  1095. {
  1096. if ((large = too_large (succn, &num_bbs, &num_insns)))
  1097. break;
  1098. }
  1099. }
  1100. if (large)
  1101. /* If the region is too_large, then wrap every block of
  1102. the region into single block region.
  1103. Here we wrap region head only. Other blocks are
  1104. processed in the below cycle. */
  1105. {
  1106. RGN_NR_BLOCKS (nr_regions) = 1;
  1107. nr_regions++;
  1108. }
  1109. num_bbs = 1;
  1110. for (j = i - 1; j >= 0; j--)
  1111. {
  1112. int succn = order[j];
  1113. if (max_hdr[succn] == bbn)
  1114. /* This cycle iterates over all basic blocks, that
  1115. are supposed to be in the region with head BBN,
  1116. and wraps them into that region (or in single
  1117. block region). */
  1118. {
  1119. gcc_assert (degree[succn] == 0);
  1120. degree[succn] = -1;
  1121. rgn_bb_table[idx] = succn;
  1122. BLOCK_TO_BB (succn) = large ? 0 : num_bbs++;
  1123. CONTAINING_RGN (succn) = nr_regions;
  1124. if (large)
  1125. /* Wrap SUCCN into single block region. */
  1126. {
  1127. RGN_BLOCKS (nr_regions) = idx;
  1128. RGN_NR_BLOCKS (nr_regions) = 1;
  1129. RGN_DONT_CALC_DEPS (nr_regions) = 0;
  1130. RGN_HAS_REAL_EBB (nr_regions) = 0;
  1131. nr_regions++;
  1132. }
  1133. idx++;
  1134. FOR_EACH_EDGE (e, ei,
  1135. BASIC_BLOCK_FOR_FN (cfun, succn)->succs)
  1136. if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
  1137. degree[e->dest->index]--;
  1138. }
  1139. }
  1140. if (!large)
  1141. {
  1142. RGN_NR_BLOCKS (nr_regions) = num_bbs;
  1143. nr_regions++;
  1144. }
  1145. }
  1146. }
  1147. if (sched_verbose >= 6)
  1148. {
  1149. int *s2, s2_sz;
  1150. /* Get the new statistics and print the comparison with the
  1151. one before calling this function. */
  1152. s2_sz = gather_region_statistics (&s2);
  1153. print_region_statistics (s1, s1_sz, s2, s2_sz);
  1154. free (s1);
  1155. free (s2);
  1156. }
  1157. }
  1158. free (order);
  1159. free (max_hdr);
  1160. *idxp = idx;
  1161. }
  1162. /* Functions for regions scheduling information. */
  1163. /* Compute dominators, probability, and potential-split-edges of bb.
  1164. Assume that these values were already computed for bb's predecessors. */
  1165. static void
  1166. compute_dom_prob_ps (int bb)
  1167. {
  1168. edge_iterator in_ei;
  1169. edge in_edge;
  1170. /* We shouldn't have any real ebbs yet. */
  1171. gcc_assert (ebb_head [bb] == bb + current_blocks);
  1172. if (IS_RGN_ENTRY (bb))
  1173. {
  1174. bitmap_set_bit (dom[bb], 0);
  1175. prob[bb] = REG_BR_PROB_BASE;
  1176. return;
  1177. }
  1178. prob[bb] = 0;
  1179. /* Initialize dom[bb] to '111..1'. */
  1180. bitmap_ones (dom[bb]);
  1181. FOR_EACH_EDGE (in_edge, in_ei,
  1182. BASIC_BLOCK_FOR_FN (cfun, BB_TO_BLOCK (bb))->preds)
  1183. {
  1184. int pred_bb;
  1185. edge out_edge;
  1186. edge_iterator out_ei;
  1187. if (in_edge->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
  1188. continue;
  1189. pred_bb = BLOCK_TO_BB (in_edge->src->index);
  1190. bitmap_and (dom[bb], dom[bb], dom[pred_bb]);
  1191. bitmap_ior (ancestor_edges[bb],
  1192. ancestor_edges[bb], ancestor_edges[pred_bb]);
  1193. bitmap_set_bit (ancestor_edges[bb], EDGE_TO_BIT (in_edge));
  1194. bitmap_ior (pot_split[bb], pot_split[bb], pot_split[pred_bb]);
  1195. FOR_EACH_EDGE (out_edge, out_ei, in_edge->src->succs)
  1196. bitmap_set_bit (pot_split[bb], EDGE_TO_BIT (out_edge));
  1197. prob[bb] += combine_probabilities (prob[pred_bb], in_edge->probability);
  1198. // The rounding divide in combine_probabilities can result in an extra
  1199. // probability increment propagating along 50-50 edges. Eventually when
  1200. // the edges re-merge, the accumulated probability can go slightly above
  1201. // REG_BR_PROB_BASE.
  1202. if (prob[bb] > REG_BR_PROB_BASE)
  1203. prob[bb] = REG_BR_PROB_BASE;
  1204. }
  1205. bitmap_set_bit (dom[bb], bb);
  1206. bitmap_and_compl (pot_split[bb], pot_split[bb], ancestor_edges[bb]);
  1207. if (sched_verbose >= 2)
  1208. fprintf (sched_dump, ";; bb_prob(%d, %d) = %3d\n", bb, BB_TO_BLOCK (bb),
  1209. (100 * prob[bb]) / REG_BR_PROB_BASE);
  1210. }
  1211. /* Functions for target info. */
  1212. /* Compute in BL the list of split-edges of bb_src relatively to bb_trg.
  1213. Note that bb_trg dominates bb_src. */
  1214. static void
  1215. split_edges (int bb_src, int bb_trg, edgelst *bl)
  1216. {
  1217. sbitmap src = sbitmap_alloc (SBITMAP_SIZE (pot_split[bb_src]));
  1218. bitmap_copy (src, pot_split[bb_src]);
  1219. bitmap_and_compl (src, src, pot_split[bb_trg]);
  1220. extract_edgelst (src, bl);
  1221. sbitmap_free (src);
  1222. }
  1223. /* Find the valid candidate-source-blocks for the target block TRG, compute
  1224. their probability, and check if they are speculative or not.
  1225. For speculative sources, compute their update-blocks and split-blocks. */
  1226. static void
  1227. compute_trg_info (int trg)
  1228. {
  1229. candidate *sp;
  1230. edgelst el = { NULL, 0 };
  1231. int i, j, k, update_idx;
  1232. basic_block block;
  1233. sbitmap visited;
  1234. edge_iterator ei;
  1235. edge e;
  1236. candidate_table = XNEWVEC (candidate, current_nr_blocks);
  1237. bblst_last = 0;
  1238. /* bblst_table holds split blocks and update blocks for each block after
  1239. the current one in the region. split blocks and update blocks are
  1240. the TO blocks of region edges, so there can be at most rgn_nr_edges
  1241. of them. */
  1242. bblst_size = (current_nr_blocks - target_bb) * rgn_nr_edges;
  1243. bblst_table = XNEWVEC (basic_block, bblst_size);
  1244. edgelst_last = 0;
  1245. edgelst_table = XNEWVEC (edge, rgn_nr_edges);
  1246. /* Define some of the fields for the target bb as well. */
  1247. sp = candidate_table + trg;
  1248. sp->is_valid = 1;
  1249. sp->is_speculative = 0;
  1250. sp->src_prob = REG_BR_PROB_BASE;
  1251. visited = sbitmap_alloc (last_basic_block_for_fn (cfun));
  1252. for (i = trg + 1; i < current_nr_blocks; i++)
  1253. {
  1254. sp = candidate_table + i;
  1255. sp->is_valid = IS_DOMINATED (i, trg);
  1256. if (sp->is_valid)
  1257. {
  1258. int tf = prob[trg], cf = prob[i];
  1259. /* In CFGs with low probability edges TF can possibly be zero. */
  1260. sp->src_prob = (tf ? GCOV_COMPUTE_SCALE (cf, tf) : 0);
  1261. sp->is_valid = (sp->src_prob >= min_spec_prob);
  1262. }
  1263. if (sp->is_valid)
  1264. {
  1265. split_edges (i, trg, &el);
  1266. sp->is_speculative = (el.nr_members) ? 1 : 0;
  1267. if (sp->is_speculative && !flag_schedule_speculative)
  1268. sp->is_valid = 0;
  1269. }
  1270. if (sp->is_valid)
  1271. {
  1272. /* Compute split blocks and store them in bblst_table.
  1273. The TO block of every split edge is a split block. */
  1274. sp->split_bbs.first_member = &bblst_table[bblst_last];
  1275. sp->split_bbs.nr_members = el.nr_members;
  1276. for (j = 0; j < el.nr_members; bblst_last++, j++)
  1277. bblst_table[bblst_last] = el.first_member[j]->dest;
  1278. sp->update_bbs.first_member = &bblst_table[bblst_last];
  1279. /* Compute update blocks and store them in bblst_table.
  1280. For every split edge, look at the FROM block, and check
  1281. all out edges. For each out edge that is not a split edge,
  1282. add the TO block to the update block list. This list can end
  1283. up with a lot of duplicates. We need to weed them out to avoid
  1284. overrunning the end of the bblst_table. */
  1285. update_idx = 0;
  1286. bitmap_clear (visited);
  1287. for (j = 0; j < el.nr_members; j++)
  1288. {
  1289. block = el.first_member[j]->src;
  1290. FOR_EACH_EDGE (e, ei, block->succs)
  1291. {
  1292. if (!bitmap_bit_p (visited, e->dest->index))
  1293. {
  1294. for (k = 0; k < el.nr_members; k++)
  1295. if (e == el.first_member[k])
  1296. break;
  1297. if (k >= el.nr_members)
  1298. {
  1299. bblst_table[bblst_last++] = e->dest;
  1300. bitmap_set_bit (visited, e->dest->index);
  1301. update_idx++;
  1302. }
  1303. }
  1304. }
  1305. }
  1306. sp->update_bbs.nr_members = update_idx;
  1307. /* Make sure we didn't overrun the end of bblst_table. */
  1308. gcc_assert (bblst_last <= bblst_size);
  1309. }
  1310. else
  1311. {
  1312. sp->split_bbs.nr_members = sp->update_bbs.nr_members = 0;
  1313. sp->is_speculative = 0;
  1314. sp->src_prob = 0;
  1315. }
  1316. }
  1317. sbitmap_free (visited);
  1318. }
  1319. /* Free the computed target info. */
  1320. static void
  1321. free_trg_info (void)
  1322. {
  1323. free (candidate_table);
  1324. free (bblst_table);
  1325. free (edgelst_table);
  1326. }
  1327. /* Print candidates info, for debugging purposes. Callable from debugger. */
  1328. DEBUG_FUNCTION void
  1329. debug_candidate (int i)
  1330. {
  1331. if (!candidate_table[i].is_valid)
  1332. return;
  1333. if (candidate_table[i].is_speculative)
  1334. {
  1335. int j;
  1336. fprintf (sched_dump, "src b %d bb %d speculative \n", BB_TO_BLOCK (i), i);
  1337. fprintf (sched_dump, "split path: ");
  1338. for (j = 0; j < candidate_table[i].split_bbs.nr_members; j++)
  1339. {
  1340. int b = candidate_table[i].split_bbs.first_member[j]->index;
  1341. fprintf (sched_dump, " %d ", b);
  1342. }
  1343. fprintf (sched_dump, "\n");
  1344. fprintf (sched_dump, "update path: ");
  1345. for (j = 0; j < candidate_table[i].update_bbs.nr_members; j++)
  1346. {
  1347. int b = candidate_table[i].update_bbs.first_member[j]->index;
  1348. fprintf (sched_dump, " %d ", b);
  1349. }
  1350. fprintf (sched_dump, "\n");
  1351. }
  1352. else
  1353. {
  1354. fprintf (sched_dump, " src %d equivalent\n", BB_TO_BLOCK (i));
  1355. }
  1356. }
  1357. /* Print candidates info, for debugging purposes. Callable from debugger. */
  1358. DEBUG_FUNCTION void
  1359. debug_candidates (int trg)
  1360. {
  1361. int i;
  1362. fprintf (sched_dump, "----------- candidate table: target: b=%d bb=%d ---\n",
  1363. BB_TO_BLOCK (trg), trg);
  1364. for (i = trg + 1; i < current_nr_blocks; i++)
  1365. debug_candidate (i);
  1366. }
  1367. /* Functions for speculative scheduling. */
  1368. static bitmap_head not_in_df;
  1369. /* Return 0 if x is a set of a register alive in the beginning of one
  1370. of the split-blocks of src, otherwise return 1. */
  1371. static int
  1372. check_live_1 (int src, rtx x)
  1373. {
  1374. int i;
  1375. int regno;
  1376. rtx reg = SET_DEST (x);
  1377. if (reg == 0)
  1378. return 1;
  1379. while (GET_CODE (reg) == SUBREG
  1380. || GET_CODE (reg) == ZERO_EXTRACT
  1381. || GET_CODE (reg) == STRICT_LOW_PART)
  1382. reg = XEXP (reg, 0);
  1383. if (GET_CODE (reg) == PARALLEL)
  1384. {
  1385. int i;
  1386. for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
  1387. if (XEXP (XVECEXP (reg, 0, i), 0) != 0)
  1388. if (check_live_1 (src, XEXP (XVECEXP (reg, 0, i), 0)))
  1389. return 1;
  1390. return 0;
  1391. }
  1392. if (!REG_P (reg))
  1393. return 1;
  1394. regno = REGNO (reg);
  1395. if (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
  1396. {
  1397. /* Global registers are assumed live. */
  1398. return 0;
  1399. }
  1400. else
  1401. {
  1402. if (regno < FIRST_PSEUDO_REGISTER)
  1403. {
  1404. /* Check for hard registers. */
  1405. int j = hard_regno_nregs[regno][GET_MODE (reg)];
  1406. while (--j >= 0)
  1407. {
  1408. for (i = 0; i < candidate_table[src].split_bbs.nr_members; i++)
  1409. {
  1410. basic_block b = candidate_table[src].split_bbs.first_member[i];
  1411. int t = bitmap_bit_p (&not_in_df, b->index);
  1412. /* We can have split blocks, that were recently generated.
  1413. Such blocks are always outside current region. */
  1414. gcc_assert (!t || (CONTAINING_RGN (b->index)
  1415. != CONTAINING_RGN (BB_TO_BLOCK (src))));
  1416. if (t || REGNO_REG_SET_P (df_get_live_in (b), regno + j))
  1417. return 0;
  1418. }
  1419. }
  1420. }
  1421. else
  1422. {
  1423. /* Check for pseudo registers. */
  1424. for (i = 0; i < candidate_table[src].split_bbs.nr_members; i++)
  1425. {
  1426. basic_block b = candidate_table[src].split_bbs.first_member[i];
  1427. int t = bitmap_bit_p (&not_in_df, b->index);
  1428. gcc_assert (!t || (CONTAINING_RGN (b->index)
  1429. != CONTAINING_RGN (BB_TO_BLOCK (src))));
  1430. if (t || REGNO_REG_SET_P (df_get_live_in (b), regno))
  1431. return 0;
  1432. }
  1433. }
  1434. }
  1435. return 1;
  1436. }
  1437. /* If x is a set of a register R, mark that R is alive in the beginning
  1438. of every update-block of src. */
  1439. static void
  1440. update_live_1 (int src, rtx x)
  1441. {
  1442. int i;
  1443. int regno;
  1444. rtx reg = SET_DEST (x);
  1445. if (reg == 0)
  1446. return;
  1447. while (GET_CODE (reg) == SUBREG
  1448. || GET_CODE (reg) == ZERO_EXTRACT
  1449. || GET_CODE (reg) == STRICT_LOW_PART)
  1450. reg = XEXP (reg, 0);
  1451. if (GET_CODE (reg) == PARALLEL)
  1452. {
  1453. int i;
  1454. for (i = XVECLEN (reg, 0) - 1; i >= 0; i--)
  1455. if (XEXP (XVECEXP (reg, 0, i), 0) != 0)
  1456. update_live_1 (src, XEXP (XVECEXP (reg, 0, i), 0));
  1457. return;
  1458. }
  1459. if (!REG_P (reg))
  1460. return;
  1461. /* Global registers are always live, so the code below does not apply
  1462. to them. */
  1463. regno = REGNO (reg);
  1464. if (! HARD_REGISTER_NUM_P (regno)
  1465. || !global_regs[regno])
  1466. {
  1467. for (i = 0; i < candidate_table[src].update_bbs.nr_members; i++)
  1468. {
  1469. basic_block b = candidate_table[src].update_bbs.first_member[i];
  1470. if (HARD_REGISTER_NUM_P (regno))
  1471. bitmap_set_range (df_get_live_in (b), regno,
  1472. hard_regno_nregs[regno][GET_MODE (reg)]);
  1473. else
  1474. bitmap_set_bit (df_get_live_in (b), regno);
  1475. }
  1476. }
  1477. }
  1478. /* Return 1 if insn can be speculatively moved from block src to trg,
  1479. otherwise return 0. Called before first insertion of insn to
  1480. ready-list or before the scheduling. */
  1481. static int
  1482. check_live (rtx_insn *insn, int src)
  1483. {
  1484. /* Find the registers set by instruction. */
  1485. if (GET_CODE (PATTERN (insn)) == SET
  1486. || GET_CODE (PATTERN (insn)) == CLOBBER)
  1487. return check_live_1 (src, PATTERN (insn));
  1488. else if (GET_CODE (PATTERN (insn)) == PARALLEL)
  1489. {
  1490. int j;
  1491. for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
  1492. if ((GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
  1493. || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
  1494. && !check_live_1 (src, XVECEXP (PATTERN (insn), 0, j)))
  1495. return 0;
  1496. return 1;
  1497. }
  1498. return 1;
  1499. }
  1500. /* Update the live registers info after insn was moved speculatively from
  1501. block src to trg. */
  1502. static void
  1503. update_live (rtx insn, int src)
  1504. {
  1505. /* Find the registers set by instruction. */
  1506. if (GET_CODE (PATTERN (insn)) == SET
  1507. || GET_CODE (PATTERN (insn)) == CLOBBER)
  1508. update_live_1 (src, PATTERN (insn));
  1509. else if (GET_CODE (PATTERN (insn)) == PARALLEL)
  1510. {
  1511. int j;
  1512. for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
  1513. if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
  1514. || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
  1515. update_live_1 (src, XVECEXP (PATTERN (insn), 0, j));
  1516. }
  1517. }
  1518. /* Nonzero if block bb_to is equal to, or reachable from block bb_from. */
  1519. #define IS_REACHABLE(bb_from, bb_to) \
  1520. (bb_from == bb_to \
  1521. || IS_RGN_ENTRY (bb_from) \
  1522. || (bitmap_bit_p (ancestor_edges[bb_to], \
  1523. EDGE_TO_BIT (single_pred_edge (BASIC_BLOCK_FOR_FN (cfun, \
  1524. BB_TO_BLOCK (bb_from)))))))
  1525. /* Turns on the fed_by_spec_load flag for insns fed by load_insn. */
  1526. static void
  1527. set_spec_fed (rtx load_insn)
  1528. {
  1529. sd_iterator_def sd_it;
  1530. dep_t dep;
  1531. FOR_EACH_DEP (load_insn, SD_LIST_FORW, sd_it, dep)
  1532. if (DEP_TYPE (dep) == REG_DEP_TRUE)
  1533. FED_BY_SPEC_LOAD (DEP_CON (dep)) = 1;
  1534. }
  1535. /* On the path from the insn to load_insn_bb, find a conditional
  1536. branch depending on insn, that guards the speculative load. */
  1537. static int
  1538. find_conditional_protection (rtx insn, int load_insn_bb)
  1539. {
  1540. sd_iterator_def sd_it;
  1541. dep_t dep;
  1542. /* Iterate through DEF-USE forward dependences. */
  1543. FOR_EACH_DEP (insn, SD_LIST_FORW, sd_it, dep)
  1544. {
  1545. rtx_insn *next = DEP_CON (dep);
  1546. if ((CONTAINING_RGN (BLOCK_NUM (next)) ==
  1547. CONTAINING_RGN (BB_TO_BLOCK (load_insn_bb)))
  1548. && IS_REACHABLE (INSN_BB (next), load_insn_bb)
  1549. && load_insn_bb != INSN_BB (next)
  1550. && DEP_TYPE (dep) == REG_DEP_TRUE
  1551. && (JUMP_P (next)
  1552. || find_conditional_protection (next, load_insn_bb)))
  1553. return 1;
  1554. }
  1555. return 0;
  1556. } /* find_conditional_protection */
  1557. /* Returns 1 if the same insn1 that participates in the computation
  1558. of load_insn's address is feeding a conditional branch that is
  1559. guarding on load_insn. This is true if we find two DEF-USE
  1560. chains:
  1561. insn1 -> ... -> conditional-branch
  1562. insn1 -> ... -> load_insn,
  1563. and if a flow path exists:
  1564. insn1 -> ... -> conditional-branch -> ... -> load_insn,
  1565. and if insn1 is on the path
  1566. region-entry -> ... -> bb_trg -> ... load_insn.
  1567. Locate insn1 by climbing on INSN_BACK_DEPS from load_insn.
  1568. Locate the branch by following INSN_FORW_DEPS from insn1. */
  1569. static int
  1570. is_conditionally_protected (rtx load_insn, int bb_src, int bb_trg)
  1571. {
  1572. sd_iterator_def sd_it;
  1573. dep_t dep;
  1574. FOR_EACH_DEP (load_insn, SD_LIST_BACK, sd_it, dep)
  1575. {
  1576. rtx_insn *insn1 = DEP_PRO (dep);
  1577. /* Must be a DEF-USE dependence upon non-branch. */
  1578. if (DEP_TYPE (dep) != REG_DEP_TRUE
  1579. || JUMP_P (insn1))
  1580. continue;
  1581. /* Must exist a path: region-entry -> ... -> bb_trg -> ... load_insn. */
  1582. if (INSN_BB (insn1) == bb_src
  1583. || (CONTAINING_RGN (BLOCK_NUM (insn1))
  1584. != CONTAINING_RGN (BB_TO_BLOCK (bb_src)))
  1585. || (!IS_REACHABLE (bb_trg, INSN_BB (insn1))
  1586. && !IS_REACHABLE (INSN_BB (insn1), bb_trg)))
  1587. continue;
  1588. /* Now search for the conditional-branch. */
  1589. if (find_conditional_protection (insn1, bb_src))
  1590. return 1;
  1591. /* Recursive step: search another insn1, "above" current insn1. */
  1592. return is_conditionally_protected (insn1, bb_src, bb_trg);
  1593. }
  1594. /* The chain does not exist. */
  1595. return 0;
  1596. } /* is_conditionally_protected */
  1597. /* Returns 1 if a clue for "similar load" 'insn2' is found, and hence
  1598. load_insn can move speculatively from bb_src to bb_trg. All the
  1599. following must hold:
  1600. (1) both loads have 1 base register (PFREE_CANDIDATEs).
  1601. (2) load_insn and load1 have a def-use dependence upon
  1602. the same insn 'insn1'.
  1603. (3) either load2 is in bb_trg, or:
  1604. - there's only one split-block, and
  1605. - load1 is on the escape path, and
  1606. From all these we can conclude that the two loads access memory
  1607. addresses that differ at most by a constant, and hence if moving
  1608. load_insn would cause an exception, it would have been caused by
  1609. load2 anyhow. */
  1610. static int
  1611. is_pfree (rtx load_insn, int bb_src, int bb_trg)
  1612. {
  1613. sd_iterator_def back_sd_it;
  1614. dep_t back_dep;
  1615. candidate *candp = candidate_table + bb_src;
  1616. if (candp->split_bbs.nr_members != 1)
  1617. /* Must have exactly one escape block. */
  1618. return 0;
  1619. FOR_EACH_DEP (load_insn, SD_LIST_BACK, back_sd_it, back_dep)
  1620. {
  1621. rtx_insn *insn1 = DEP_PRO (back_dep);
  1622. if (DEP_TYPE (back_dep) == REG_DEP_TRUE)
  1623. /* Found a DEF-USE dependence (insn1, load_insn). */
  1624. {
  1625. sd_iterator_def fore_sd_it;
  1626. dep_t fore_dep;
  1627. FOR_EACH_DEP (insn1, SD_LIST_FORW, fore_sd_it, fore_dep)
  1628. {
  1629. rtx_insn *insn2 = DEP_CON (fore_dep);
  1630. if (DEP_TYPE (fore_dep) == REG_DEP_TRUE)
  1631. {
  1632. /* Found a DEF-USE dependence (insn1, insn2). */
  1633. if (haifa_classify_insn (insn2) != PFREE_CANDIDATE)
  1634. /* insn2 not guaranteed to be a 1 base reg load. */
  1635. continue;
  1636. if (INSN_BB (insn2) == bb_trg)
  1637. /* insn2 is the similar load, in the target block. */
  1638. return 1;
  1639. if (*(candp->split_bbs.first_member) == BLOCK_FOR_INSN (insn2))
  1640. /* insn2 is a similar load, in a split-block. */
  1641. return 1;
  1642. }
  1643. }
  1644. }
  1645. }
  1646. /* Couldn't find a similar load. */
  1647. return 0;
  1648. } /* is_pfree */
  1649. /* Return 1 if load_insn is prisky (i.e. if load_insn is fed by
  1650. a load moved speculatively, or if load_insn is protected by
  1651. a compare on load_insn's address). */
  1652. static int
  1653. is_prisky (rtx load_insn, int bb_src, int bb_trg)
  1654. {
  1655. if (FED_BY_SPEC_LOAD (load_insn))
  1656. return 1;
  1657. if (sd_lists_empty_p (load_insn, SD_LIST_BACK))
  1658. /* Dependence may 'hide' out of the region. */
  1659. return 1;
  1660. if (is_conditionally_protected (load_insn, bb_src, bb_trg))
  1661. return 1;
  1662. return 0;
  1663. }
  1664. /* Insn is a candidate to be moved speculatively from bb_src to bb_trg.
  1665. Return 1 if insn is exception-free (and the motion is valid)
  1666. and 0 otherwise. */
  1667. static int
  1668. is_exception_free (rtx insn, int bb_src, int bb_trg)
  1669. {
  1670. int insn_class = haifa_classify_insn (insn);
  1671. /* Handle non-load insns. */
  1672. switch (insn_class)
  1673. {
  1674. case TRAP_FREE:
  1675. return 1;
  1676. case TRAP_RISKY:
  1677. return 0;
  1678. default:;
  1679. }
  1680. /* Handle loads. */
  1681. if (!flag_schedule_speculative_load)
  1682. return 0;
  1683. IS_LOAD_INSN (insn) = 1;
  1684. switch (insn_class)
  1685. {
  1686. case IFREE:
  1687. return (1);
  1688. case IRISKY:
  1689. return 0;
  1690. case PFREE_CANDIDATE:
  1691. if (is_pfree (insn, bb_src, bb_trg))
  1692. return 1;
  1693. /* Don't 'break' here: PFREE-candidate is also PRISKY-candidate. */
  1694. case PRISKY_CANDIDATE:
  1695. if (!flag_schedule_speculative_load_dangerous
  1696. || is_prisky (insn, bb_src, bb_trg))
  1697. return 0;
  1698. break;
  1699. default:;
  1700. }
  1701. return flag_schedule_speculative_load_dangerous;
  1702. }
  1703. /* The number of insns from the current block scheduled so far. */
  1704. static int sched_target_n_insns;
  1705. /* The number of insns from the current block to be scheduled in total. */
  1706. static int target_n_insns;
  1707. /* The number of insns from the entire region scheduled so far. */
  1708. static int sched_n_insns;
  1709. /* Implementations of the sched_info functions for region scheduling. */
  1710. static void init_ready_list (void);
  1711. static int can_schedule_ready_p (rtx_insn *);
  1712. static void begin_schedule_ready (rtx_insn *);
  1713. static ds_t new_ready (rtx_insn *, ds_t);
  1714. static int schedule_more_p (void);
  1715. static const char *rgn_print_insn (const rtx_insn *, int);
  1716. static int rgn_rank (rtx_insn *, rtx_insn *);
  1717. static void compute_jump_reg_dependencies (rtx, regset);
  1718. /* Functions for speculative scheduling. */
  1719. static void rgn_add_remove_insn (rtx_insn *, int);
  1720. static void rgn_add_block (basic_block, basic_block);
  1721. static void rgn_fix_recovery_cfg (int, int, int);
  1722. static basic_block advance_target_bb (basic_block, rtx_insn *);
  1723. /* Return nonzero if there are more insns that should be scheduled. */
  1724. static int
  1725. schedule_more_p (void)
  1726. {
  1727. return sched_target_n_insns < target_n_insns;
  1728. }
  1729. /* Add all insns that are initially ready to the ready list READY. Called
  1730. once before scheduling a set of insns. */
  1731. static void
  1732. init_ready_list (void)
  1733. {
  1734. rtx_insn *prev_head = current_sched_info->prev_head;
  1735. rtx_insn *next_tail = current_sched_info->next_tail;
  1736. int bb_src;
  1737. rtx_insn *insn;
  1738. target_n_insns = 0;
  1739. sched_target_n_insns = 0;
  1740. sched_n_insns = 0;
  1741. /* Print debugging information. */
  1742. if (sched_verbose >= 5)
  1743. debug_rgn_dependencies (target_bb);
  1744. /* Prepare current target block info. */
  1745. if (current_nr_blocks > 1)
  1746. compute_trg_info (target_bb);
  1747. /* Initialize ready list with all 'ready' insns in target block.
  1748. Count number of insns in the target block being scheduled. */
  1749. for (insn = NEXT_INSN (prev_head); insn != next_tail; insn = NEXT_INSN (insn))
  1750. {
  1751. gcc_assert (TODO_SPEC (insn) == HARD_DEP || TODO_SPEC (insn) == DEP_POSTPONED);
  1752. TODO_SPEC (insn) = HARD_DEP;
  1753. try_ready (insn);
  1754. target_n_insns++;
  1755. gcc_assert (!(TODO_SPEC (insn) & BEGIN_CONTROL));
  1756. }
  1757. /* Add to ready list all 'ready' insns in valid source blocks.
  1758. For speculative insns, check-live, exception-free, and
  1759. issue-delay. */
  1760. for (bb_src = target_bb + 1; bb_src < current_nr_blocks; bb_src++)
  1761. if (IS_VALID (bb_src))
  1762. {
  1763. rtx_insn *src_head;
  1764. rtx_insn *src_next_tail;
  1765. rtx_insn *tail, *head;
  1766. get_ebb_head_tail (EBB_FIRST_BB (bb_src), EBB_LAST_BB (bb_src),
  1767. &head, &tail);
  1768. src_next_tail = NEXT_INSN (tail);
  1769. src_head = head;
  1770. for (insn = src_head; insn != src_next_tail; insn = NEXT_INSN (insn))
  1771. if (INSN_P (insn))
  1772. {
  1773. gcc_assert (TODO_SPEC (insn) == HARD_DEP || TODO_SPEC (insn) == DEP_POSTPONED);
  1774. TODO_SPEC (insn) = HARD_DEP;
  1775. try_ready (insn);
  1776. }
  1777. }
  1778. }
  1779. /* Called after taking INSN from the ready list. Returns nonzero if this
  1780. insn can be scheduled, nonzero if we should silently discard it. */
  1781. static int
  1782. can_schedule_ready_p (rtx_insn *insn)
  1783. {
  1784. /* An interblock motion? */
  1785. if (INSN_BB (insn) != target_bb
  1786. && IS_SPECULATIVE_INSN (insn)
  1787. && !check_live (insn, INSN_BB (insn)))
  1788. return 0;
  1789. else
  1790. return 1;
  1791. }
  1792. /* Updates counter and other information. Split from can_schedule_ready_p ()
  1793. because when we schedule insn speculatively then insn passed to
  1794. can_schedule_ready_p () differs from the one passed to
  1795. begin_schedule_ready (). */
  1796. static void
  1797. begin_schedule_ready (rtx_insn *insn)
  1798. {
  1799. /* An interblock motion? */
  1800. if (INSN_BB (insn) != target_bb)
  1801. {
  1802. if (IS_SPECULATIVE_INSN (insn))
  1803. {
  1804. gcc_assert (check_live (insn, INSN_BB (insn)));
  1805. update_live (insn, INSN_BB (insn));
  1806. /* For speculative load, mark insns fed by it. */
  1807. if (IS_LOAD_INSN (insn) || FED_BY_SPEC_LOAD (insn))
  1808. set_spec_fed (insn);
  1809. nr_spec++;
  1810. }
  1811. nr_inter++;
  1812. }
  1813. else
  1814. {
  1815. /* In block motion. */
  1816. sched_target_n_insns++;
  1817. }
  1818. sched_n_insns++;
  1819. }
  1820. /* Called after INSN has all its hard dependencies resolved and the speculation
  1821. of type TS is enough to overcome them all.
  1822. Return nonzero if it should be moved to the ready list or the queue, or zero
  1823. if we should silently discard it. */
  1824. static ds_t
  1825. new_ready (rtx_insn *next, ds_t ts)
  1826. {
  1827. if (INSN_BB (next) != target_bb)
  1828. {
  1829. int not_ex_free = 0;
  1830. /* For speculative insns, before inserting to ready/queue,
  1831. check live, exception-free, and issue-delay. */
  1832. if (!IS_VALID (INSN_BB (next))
  1833. || CANT_MOVE (next)
  1834. || (IS_SPECULATIVE_INSN (next)
  1835. && ((recog_memoized (next) >= 0
  1836. && min_insn_conflict_delay (curr_state, next, next)
  1837. > PARAM_VALUE (PARAM_MAX_SCHED_INSN_CONFLICT_DELAY))
  1838. || IS_SPECULATION_CHECK_P (next)
  1839. || !check_live (next, INSN_BB (next))
  1840. || (not_ex_free = !is_exception_free (next, INSN_BB (next),
  1841. target_bb)))))
  1842. {
  1843. if (not_ex_free
  1844. /* We are here because is_exception_free () == false.
  1845. But we possibly can handle that with control speculation. */
  1846. && sched_deps_info->generate_spec_deps
  1847. && spec_info->mask & BEGIN_CONTROL)
  1848. {
  1849. ds_t new_ds;
  1850. /* Add control speculation to NEXT's dependency type. */
  1851. new_ds = set_dep_weak (ts, BEGIN_CONTROL, MAX_DEP_WEAK);
  1852. /* Check if NEXT can be speculated with new dependency type. */
  1853. if (sched_insn_is_legitimate_for_speculation_p (next, new_ds))
  1854. /* Here we got new control-speculative instruction. */
  1855. ts = new_ds;
  1856. else
  1857. /* NEXT isn't ready yet. */
  1858. ts = DEP_POSTPONED;
  1859. }
  1860. else
  1861. /* NEXT isn't ready yet. */
  1862. ts = DEP_POSTPONED;
  1863. }
  1864. }
  1865. return ts;
  1866. }
  1867. /* Return a string that contains the insn uid and optionally anything else
  1868. necessary to identify this insn in an output. It's valid to use a
  1869. static buffer for this. The ALIGNED parameter should cause the string
  1870. to be formatted so that multiple output lines will line up nicely. */
  1871. static const char *
  1872. rgn_print_insn (const rtx_insn *insn, int aligned)
  1873. {
  1874. static char tmp[80];
  1875. if (aligned)
  1876. sprintf (tmp, "b%3d: i%4d", INSN_BB (insn), INSN_UID (insn));
  1877. else
  1878. {
  1879. if (current_nr_blocks > 1 && INSN_BB (insn) != target_bb)
  1880. sprintf (tmp, "%d/b%d", INSN_UID (insn), INSN_BB (insn));
  1881. else
  1882. sprintf (tmp, "%d", INSN_UID (insn));
  1883. }
  1884. return tmp;
  1885. }
  1886. /* Compare priority of two insns. Return a positive number if the second
  1887. insn is to be preferred for scheduling, and a negative one if the first
  1888. is to be preferred. Zero if they are equally good. */
  1889. static int
  1890. rgn_rank (rtx_insn *insn1, rtx_insn *insn2)
  1891. {
  1892. /* Some comparison make sense in interblock scheduling only. */
  1893. if (INSN_BB (insn1) != INSN_BB (insn2))
  1894. {
  1895. int spec_val, prob_val;
  1896. /* Prefer an inblock motion on an interblock motion. */
  1897. if ((INSN_BB (insn2) == target_bb) && (INSN_BB (insn1) != target_bb))
  1898. return 1;
  1899. if ((INSN_BB (insn1) == target_bb) && (INSN_BB (insn2) != target_bb))
  1900. return -1;
  1901. /* Prefer a useful motion on a speculative one. */
  1902. spec_val = IS_SPECULATIVE_INSN (insn1) - IS_SPECULATIVE_INSN (insn2);
  1903. if (spec_val)
  1904. return spec_val;
  1905. /* Prefer a more probable (speculative) insn. */
  1906. prob_val = INSN_PROBABILITY (insn2) - INSN_PROBABILITY (insn1);
  1907. if (prob_val)
  1908. return prob_val;
  1909. }
  1910. return 0;
  1911. }
  1912. /* NEXT is an instruction that depends on INSN (a backward dependence);
  1913. return nonzero if we should include this dependence in priority
  1914. calculations. */
  1915. int
  1916. contributes_to_priority (rtx_insn *next, rtx_insn *insn)
  1917. {
  1918. /* NEXT and INSN reside in one ebb. */
  1919. return BLOCK_TO_BB (BLOCK_NUM (next)) == BLOCK_TO_BB (BLOCK_NUM (insn));
  1920. }
  1921. /* INSN is a JUMP_INSN. Store the set of registers that must be
  1922. considered as used by this jump in USED. */
  1923. static void
  1924. compute_jump_reg_dependencies (rtx insn ATTRIBUTE_UNUSED,
  1925. regset used ATTRIBUTE_UNUSED)
  1926. {
  1927. /* Nothing to do here, since we postprocess jumps in
  1928. add_branch_dependences. */
  1929. }
  1930. /* This variable holds common_sched_info hooks and data relevant to
  1931. the interblock scheduler. */
  1932. static struct common_sched_info_def rgn_common_sched_info;
  1933. /* This holds data for the dependence analysis relevant to
  1934. the interblock scheduler. */
  1935. static struct sched_deps_info_def rgn_sched_deps_info;
  1936. /* This holds constant data used for initializing the above structure
  1937. for the Haifa scheduler. */
  1938. static const struct sched_deps_info_def rgn_const_sched_deps_info =
  1939. {
  1940. compute_jump_reg_dependencies,
  1941. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1942. 0, 0, 0
  1943. };
  1944. /* Same as above, but for the selective scheduler. */
  1945. static const struct sched_deps_info_def rgn_const_sel_sched_deps_info =
  1946. {
  1947. compute_jump_reg_dependencies,
  1948. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1949. 0, 0, 0
  1950. };
  1951. /* Return true if scheduling INSN will trigger finish of scheduling
  1952. current block. */
  1953. static bool
  1954. rgn_insn_finishes_block_p (rtx_insn *insn)
  1955. {
  1956. if (INSN_BB (insn) == target_bb
  1957. && sched_target_n_insns + 1 == target_n_insns)
  1958. /* INSN is the last not-scheduled instruction in the current block. */
  1959. return true;
  1960. return false;
  1961. }
  1962. /* Used in schedule_insns to initialize current_sched_info for scheduling
  1963. regions (or single basic blocks). */
  1964. static const struct haifa_sched_info rgn_const_sched_info =
  1965. {
  1966. init_ready_list,
  1967. can_schedule_ready_p,
  1968. schedule_more_p,
  1969. new_ready,
  1970. rgn_rank,
  1971. rgn_print_insn,
  1972. contributes_to_priority,
  1973. rgn_insn_finishes_block_p,
  1974. NULL, NULL,
  1975. NULL, NULL,
  1976. 0, 0,
  1977. rgn_add_remove_insn,
  1978. begin_schedule_ready,
  1979. NULL,
  1980. advance_target_bb,
  1981. NULL, NULL,
  1982. SCHED_RGN
  1983. };
  1984. /* This variable holds the data and hooks needed to the Haifa scheduler backend
  1985. for the interblock scheduler frontend. */
  1986. static struct haifa_sched_info rgn_sched_info;
  1987. /* Returns maximum priority that an insn was assigned to. */
  1988. int
  1989. get_rgn_sched_max_insns_priority (void)
  1990. {
  1991. return rgn_sched_info.sched_max_insns_priority;
  1992. }
  1993. /* Determine if PAT sets a TARGET_CLASS_LIKELY_SPILLED_P register. */
  1994. static bool
  1995. sets_likely_spilled (rtx pat)
  1996. {
  1997. bool ret = false;
  1998. note_stores (pat, sets_likely_spilled_1, &ret);
  1999. return ret;
  2000. }
  2001. static void
  2002. sets_likely_spilled_1 (rtx x, const_rtx pat, void *data)
  2003. {
  2004. bool *ret = (bool *) data;
  2005. if (GET_CODE (pat) == SET
  2006. && REG_P (x)
  2007. && HARD_REGISTER_P (x)
  2008. && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (x))))
  2009. *ret = true;
  2010. }
  2011. /* A bitmap to note insns that participate in any dependency. Used in
  2012. add_branch_dependences. */
  2013. static sbitmap insn_referenced;
  2014. /* Add dependences so that branches are scheduled to run last in their
  2015. block. */
  2016. static void
  2017. add_branch_dependences (rtx_insn *head, rtx_insn *tail)
  2018. {
  2019. rtx_insn *insn, *last;
  2020. /* For all branches, calls, uses, clobbers, cc0 setters, and instructions
  2021. that can throw exceptions, force them to remain in order at the end of
  2022. the block by adding dependencies and giving the last a high priority.
  2023. There may be notes present, and prev_head may also be a note.
  2024. Branches must obviously remain at the end. Calls should remain at the
  2025. end since moving them results in worse register allocation. Uses remain
  2026. at the end to ensure proper register allocation.
  2027. cc0 setters remain at the end because they can't be moved away from
  2028. their cc0 user.
  2029. Predecessors of SCHED_GROUP_P instructions at the end remain at the end.
  2030. COND_EXEC insns cannot be moved past a branch (see e.g. PR17808).
  2031. Insns setting TARGET_CLASS_LIKELY_SPILLED_P registers (usually return
  2032. values) are not moved before reload because we can wind up with register
  2033. allocation failures. */
  2034. while (tail != head && DEBUG_INSN_P (tail))
  2035. tail = PREV_INSN (tail);
  2036. insn = tail;
  2037. last = 0;
  2038. while (CALL_P (insn)
  2039. || JUMP_P (insn) || JUMP_TABLE_DATA_P (insn)
  2040. || (NONJUMP_INSN_P (insn)
  2041. && (GET_CODE (PATTERN (insn)) == USE
  2042. || GET_CODE (PATTERN (insn)) == CLOBBER
  2043. || can_throw_internal (insn)
  2044. #ifdef HAVE_cc0
  2045. || sets_cc0_p (PATTERN (insn))
  2046. #endif
  2047. || (!reload_completed
  2048. && sets_likely_spilled (PATTERN (insn)))))
  2049. || NOTE_P (insn)
  2050. || (last != 0 && SCHED_GROUP_P (last)))
  2051. {
  2052. if (!NOTE_P (insn))
  2053. {
  2054. if (last != 0
  2055. && sd_find_dep_between (insn, last, false) == NULL)
  2056. {
  2057. if (! sched_insns_conditions_mutex_p (last, insn))
  2058. add_dependence (last, insn, REG_DEP_ANTI);
  2059. bitmap_set_bit (insn_referenced, INSN_LUID (insn));
  2060. }
  2061. CANT_MOVE (insn) = 1;
  2062. last = insn;
  2063. }
  2064. /* Don't overrun the bounds of the basic block. */
  2065. if (insn == head)
  2066. break;
  2067. do
  2068. insn = PREV_INSN (insn);
  2069. while (insn != head && DEBUG_INSN_P (insn));
  2070. }
  2071. /* Make sure these insns are scheduled last in their block. */
  2072. insn = last;
  2073. if (insn != 0)
  2074. while (insn != head)
  2075. {
  2076. insn = prev_nonnote_insn (insn);
  2077. if (bitmap_bit_p (insn_referenced, INSN_LUID (insn))
  2078. || DEBUG_INSN_P (insn))
  2079. continue;
  2080. if (! sched_insns_conditions_mutex_p (last, insn))
  2081. add_dependence (last, insn, REG_DEP_ANTI);
  2082. }
  2083. if (!targetm.have_conditional_execution ())
  2084. return;
  2085. /* Finally, if the block ends in a jump, and we are doing intra-block
  2086. scheduling, make sure that the branch depends on any COND_EXEC insns
  2087. inside the block to avoid moving the COND_EXECs past the branch insn.
  2088. We only have to do this after reload, because (1) before reload there
  2089. are no COND_EXEC insns, and (2) the region scheduler is an intra-block
  2090. scheduler after reload.
  2091. FIXME: We could in some cases move COND_EXEC insns past the branch if
  2092. this scheduler would be a little smarter. Consider this code:
  2093. T = [addr]
  2094. C ? addr += 4
  2095. !C ? X += 12
  2096. C ? T += 1
  2097. C ? jump foo
  2098. On a target with a one cycle stall on a memory access the optimal
  2099. sequence would be:
  2100. T = [addr]
  2101. C ? addr += 4
  2102. C ? T += 1
  2103. C ? jump foo
  2104. !C ? X += 12
  2105. We don't want to put the 'X += 12' before the branch because it just
  2106. wastes a cycle of execution time when the branch is taken.
  2107. Note that in the example "!C" will always be true. That is another
  2108. possible improvement for handling COND_EXECs in this scheduler: it
  2109. could remove always-true predicates. */
  2110. if (!reload_completed || ! (JUMP_P (tail) || JUMP_TABLE_DATA_P (tail)))
  2111. return;
  2112. insn = tail;
  2113. while (insn != head)
  2114. {
  2115. insn = PREV_INSN (insn);
  2116. /* Note that we want to add this dependency even when
  2117. sched_insns_conditions_mutex_p returns true. The whole point
  2118. is that we _want_ this dependency, even if these insns really
  2119. are independent. */
  2120. if (INSN_P (insn) && GET_CODE (PATTERN (insn)) == COND_EXEC)
  2121. add_dependence (tail, insn, REG_DEP_ANTI);
  2122. }
  2123. }
  2124. /* Data structures for the computation of data dependences in a regions. We
  2125. keep one `deps' structure for every basic block. Before analyzing the
  2126. data dependences for a bb, its variables are initialized as a function of
  2127. the variables of its predecessors. When the analysis for a bb completes,
  2128. we save the contents to the corresponding bb_deps[bb] variable. */
  2129. static struct deps_desc *bb_deps;
  2130. static void
  2131. concat_insn_mem_list (rtx_insn_list *copy_insns,
  2132. rtx_expr_list *copy_mems,
  2133. rtx_insn_list **old_insns_p,
  2134. rtx_expr_list **old_mems_p)
  2135. {
  2136. rtx_insn_list *new_insns = *old_insns_p;
  2137. rtx_expr_list *new_mems = *old_mems_p;
  2138. while (copy_insns)
  2139. {
  2140. new_insns = alloc_INSN_LIST (copy_insns->insn (), new_insns);
  2141. new_mems = alloc_EXPR_LIST (VOIDmode, copy_mems->element (), new_mems);
  2142. copy_insns = copy_insns->next ();
  2143. copy_mems = copy_mems->next ();
  2144. }
  2145. *old_insns_p = new_insns;
  2146. *old_mems_p = new_mems;
  2147. }
  2148. /* Join PRED_DEPS to the SUCC_DEPS. */
  2149. void
  2150. deps_join (struct deps_desc *succ_deps, struct deps_desc *pred_deps)
  2151. {
  2152. unsigned reg;
  2153. reg_set_iterator rsi;
  2154. /* The reg_last lists are inherited by successor. */
  2155. EXECUTE_IF_SET_IN_REG_SET (&pred_deps->reg_last_in_use, 0, reg, rsi)
  2156. {
  2157. struct deps_reg *pred_rl = &pred_deps->reg_last[reg];
  2158. struct deps_reg *succ_rl = &succ_deps->reg_last[reg];
  2159. succ_rl->uses = concat_INSN_LIST (pred_rl->uses, succ_rl->uses);
  2160. succ_rl->sets = concat_INSN_LIST (pred_rl->sets, succ_rl->sets);
  2161. succ_rl->implicit_sets
  2162. = concat_INSN_LIST (pred_rl->implicit_sets, succ_rl->implicit_sets);
  2163. succ_rl->clobbers = concat_INSN_LIST (pred_rl->clobbers,
  2164. succ_rl->clobbers);
  2165. succ_rl->uses_length += pred_rl->uses_length;
  2166. succ_rl->clobbers_length += pred_rl->clobbers_length;
  2167. }
  2168. IOR_REG_SET (&succ_deps->reg_last_in_use, &pred_deps->reg_last_in_use);
  2169. /* Mem read/write lists are inherited by successor. */
  2170. concat_insn_mem_list (pred_deps->pending_read_insns,
  2171. pred_deps->pending_read_mems,
  2172. &succ_deps->pending_read_insns,
  2173. &succ_deps->pending_read_mems);
  2174. concat_insn_mem_list (pred_deps->pending_write_insns,
  2175. pred_deps->pending_write_mems,
  2176. &succ_deps->pending_write_insns,
  2177. &succ_deps->pending_write_mems);
  2178. succ_deps->pending_jump_insns
  2179. = concat_INSN_LIST (pred_deps->pending_jump_insns,
  2180. succ_deps->pending_jump_insns);
  2181. succ_deps->last_pending_memory_flush
  2182. = concat_INSN_LIST (pred_deps->last_pending_memory_flush,
  2183. succ_deps->last_pending_memory_flush);
  2184. succ_deps->pending_read_list_length += pred_deps->pending_read_list_length;
  2185. succ_deps->pending_write_list_length += pred_deps->pending_write_list_length;
  2186. succ_deps->pending_flush_length += pred_deps->pending_flush_length;
  2187. /* last_function_call is inherited by successor. */
  2188. succ_deps->last_function_call
  2189. = concat_INSN_LIST (pred_deps->last_function_call,
  2190. succ_deps->last_function_call);
  2191. /* last_function_call_may_noreturn is inherited by successor. */
  2192. succ_deps->last_function_call_may_noreturn
  2193. = concat_INSN_LIST (pred_deps->last_function_call_may_noreturn,
  2194. succ_deps->last_function_call_may_noreturn);
  2195. /* sched_before_next_call is inherited by successor. */
  2196. succ_deps->sched_before_next_call
  2197. = concat_INSN_LIST (pred_deps->sched_before_next_call,
  2198. succ_deps->sched_before_next_call);
  2199. }
  2200. /* After computing the dependencies for block BB, propagate the dependencies
  2201. found in TMP_DEPS to the successors of the block. */
  2202. static void
  2203. propagate_deps (int bb, struct deps_desc *pred_deps)
  2204. {
  2205. basic_block block = BASIC_BLOCK_FOR_FN (cfun, BB_TO_BLOCK (bb));
  2206. edge_iterator ei;
  2207. edge e;
  2208. /* bb's structures are inherited by its successors. */
  2209. FOR_EACH_EDGE (e, ei, block->succs)
  2210. {
  2211. /* Only bbs "below" bb, in the same region, are interesting. */
  2212. if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)
  2213. || CONTAINING_RGN (block->index) != CONTAINING_RGN (e->dest->index)
  2214. || BLOCK_TO_BB (e->dest->index) <= bb)
  2215. continue;
  2216. deps_join (bb_deps + BLOCK_TO_BB (e->dest->index), pred_deps);
  2217. }
  2218. /* These lists should point to the right place, for correct
  2219. freeing later. */
  2220. bb_deps[bb].pending_read_insns = pred_deps->pending_read_insns;
  2221. bb_deps[bb].pending_read_mems = pred_deps->pending_read_mems;
  2222. bb_deps[bb].pending_write_insns = pred_deps->pending_write_insns;
  2223. bb_deps[bb].pending_write_mems = pred_deps->pending_write_mems;
  2224. bb_deps[bb].pending_jump_insns = pred_deps->pending_jump_insns;
  2225. /* Can't allow these to be freed twice. */
  2226. pred_deps->pending_read_insns = 0;
  2227. pred_deps->pending_read_mems = 0;
  2228. pred_deps->pending_write_insns = 0;
  2229. pred_deps->pending_write_mems = 0;
  2230. pred_deps->pending_jump_insns = 0;
  2231. }
  2232. /* Compute dependences inside bb. In a multiple blocks region:
  2233. (1) a bb is analyzed after its predecessors, and (2) the lists in
  2234. effect at the end of bb (after analyzing for bb) are inherited by
  2235. bb's successors.
  2236. Specifically for reg-reg data dependences, the block insns are
  2237. scanned by sched_analyze () top-to-bottom. Three lists are
  2238. maintained by sched_analyze (): reg_last[].sets for register DEFs,
  2239. reg_last[].implicit_sets for implicit hard register DEFs, and
  2240. reg_last[].uses for register USEs.
  2241. When analysis is completed for bb, we update for its successors:
  2242. ; - DEFS[succ] = Union (DEFS [succ], DEFS [bb])
  2243. ; - IMPLICIT_DEFS[succ] = Union (IMPLICIT_DEFS [succ], IMPLICIT_DEFS [bb])
  2244. ; - USES[succ] = Union (USES [succ], DEFS [bb])
  2245. The mechanism for computing mem-mem data dependence is very
  2246. similar, and the result is interblock dependences in the region. */
  2247. static void
  2248. compute_block_dependences (int bb)
  2249. {
  2250. rtx_insn *head, *tail;
  2251. struct deps_desc tmp_deps;
  2252. tmp_deps = bb_deps[bb];
  2253. /* Do the analysis for this block. */
  2254. gcc_assert (EBB_FIRST_BB (bb) == EBB_LAST_BB (bb));
  2255. get_ebb_head_tail (EBB_FIRST_BB (bb), EBB_LAST_BB (bb), &head, &tail);
  2256. sched_analyze (&tmp_deps, head, tail);
  2257. /* Selective scheduling handles control dependencies by itself. */
  2258. if (!sel_sched_p ())
  2259. add_branch_dependences (head, tail);
  2260. if (current_nr_blocks > 1)
  2261. propagate_deps (bb, &tmp_deps);
  2262. /* Free up the INSN_LISTs. */
  2263. free_deps (&tmp_deps);
  2264. if (targetm.sched.dependencies_evaluation_hook)
  2265. targetm.sched.dependencies_evaluation_hook (head, tail);
  2266. }
  2267. /* Free dependencies of instructions inside BB. */
  2268. static void
  2269. free_block_dependencies (int bb)
  2270. {
  2271. rtx_insn *head;
  2272. rtx_insn *tail;
  2273. get_ebb_head_tail (EBB_FIRST_BB (bb), EBB_LAST_BB (bb), &head, &tail);
  2274. if (no_real_insns_p (head, tail))
  2275. return;
  2276. sched_free_deps (head, tail, true);
  2277. }
  2278. /* Remove all INSN_LISTs and EXPR_LISTs from the pending lists and add
  2279. them to the unused_*_list variables, so that they can be reused. */
  2280. static void
  2281. free_pending_lists (void)
  2282. {
  2283. int bb;
  2284. for (bb = 0; bb < current_nr_blocks; bb++)
  2285. {
  2286. free_INSN_LIST_list (&bb_deps[bb].pending_read_insns);
  2287. free_INSN_LIST_list (&bb_deps[bb].pending_write_insns);
  2288. free_EXPR_LIST_list (&bb_deps[bb].pending_read_mems);
  2289. free_EXPR_LIST_list (&bb_deps[bb].pending_write_mems);
  2290. free_INSN_LIST_list (&bb_deps[bb].pending_jump_insns);
  2291. }
  2292. }
  2293. /* Print dependences for debugging starting from FROM_BB.
  2294. Callable from debugger. */
  2295. /* Print dependences for debugging starting from FROM_BB.
  2296. Callable from debugger. */
  2297. DEBUG_FUNCTION void
  2298. debug_rgn_dependencies (int from_bb)
  2299. {
  2300. int bb;
  2301. fprintf (sched_dump,
  2302. ";; --------------- forward dependences: ------------ \n");
  2303. for (bb = from_bb; bb < current_nr_blocks; bb++)
  2304. {
  2305. rtx_insn *head, *tail;
  2306. get_ebb_head_tail (EBB_FIRST_BB (bb), EBB_LAST_BB (bb), &head, &tail);
  2307. fprintf (sched_dump, "\n;; --- Region Dependences --- b %d bb %d \n",
  2308. BB_TO_BLOCK (bb), bb);
  2309. debug_dependencies (head, tail);
  2310. }
  2311. }
  2312. /* Print dependencies information for instructions between HEAD and TAIL.
  2313. ??? This function would probably fit best in haifa-sched.c. */
  2314. void debug_dependencies (rtx_insn *head, rtx_insn *tail)
  2315. {
  2316. rtx_insn *insn;
  2317. rtx_insn *next_tail = NEXT_INSN (tail);
  2318. fprintf (sched_dump, ";; %7s%6s%6s%6s%6s%6s%14s\n",
  2319. "insn", "code", "bb", "dep", "prio", "cost",
  2320. "reservation");
  2321. fprintf (sched_dump, ";; %7s%6s%6s%6s%6s%6s%14s\n",
  2322. "----", "----", "--", "---", "----", "----",
  2323. "-----------");
  2324. for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
  2325. {
  2326. if (! INSN_P (insn))
  2327. {
  2328. int n;
  2329. fprintf (sched_dump, ";; %6d ", INSN_UID (insn));
  2330. if (NOTE_P (insn))
  2331. {
  2332. n = NOTE_KIND (insn);
  2333. fprintf (sched_dump, "%s\n", GET_NOTE_INSN_NAME (n));
  2334. }
  2335. else
  2336. fprintf (sched_dump, " {%s}\n", GET_RTX_NAME (GET_CODE (insn)));
  2337. continue;
  2338. }
  2339. fprintf (sched_dump,
  2340. ";; %s%5d%6d%6d%6d%6d%6d ",
  2341. (SCHED_GROUP_P (insn) ? "+" : " "),
  2342. INSN_UID (insn),
  2343. INSN_CODE (insn),
  2344. BLOCK_NUM (insn),
  2345. sched_emulate_haifa_p ? -1 : sd_lists_size (insn, SD_LIST_BACK),
  2346. (sel_sched_p () ? (sched_emulate_haifa_p ? -1
  2347. : INSN_PRIORITY (insn))
  2348. : INSN_PRIORITY (insn)),
  2349. (sel_sched_p () ? (sched_emulate_haifa_p ? -1
  2350. : insn_cost (insn))
  2351. : insn_cost (insn)));
  2352. if (recog_memoized (insn) < 0)
  2353. fprintf (sched_dump, "nothing");
  2354. else
  2355. print_reservation (sched_dump, insn);
  2356. fprintf (sched_dump, "\t: ");
  2357. {
  2358. sd_iterator_def sd_it;
  2359. dep_t dep;
  2360. FOR_EACH_DEP (insn, SD_LIST_FORW, sd_it, dep)
  2361. fprintf (sched_dump, "%d%s%s ", INSN_UID (DEP_CON (dep)),
  2362. DEP_NONREG (dep) ? "n" : "",
  2363. DEP_MULTIPLE (dep) ? "m" : "");
  2364. }
  2365. fprintf (sched_dump, "\n");
  2366. }
  2367. fprintf (sched_dump, "\n");
  2368. }
  2369. /* Returns true if all the basic blocks of the current region have
  2370. NOTE_DISABLE_SCHED_OF_BLOCK which means not to schedule that region. */
  2371. bool
  2372. sched_is_disabled_for_current_region_p (void)
  2373. {
  2374. int bb;
  2375. for (bb = 0; bb < current_nr_blocks; bb++)
  2376. if (!(BASIC_BLOCK_FOR_FN (cfun,
  2377. BB_TO_BLOCK (bb))->flags & BB_DISABLE_SCHEDULE))
  2378. return false;
  2379. return true;
  2380. }
  2381. /* Free all region dependencies saved in INSN_BACK_DEPS and
  2382. INSN_RESOLVED_BACK_DEPS. The Haifa scheduler does this on the fly
  2383. when scheduling, so this function is supposed to be called from
  2384. the selective scheduling only. */
  2385. void
  2386. free_rgn_deps (void)
  2387. {
  2388. int bb;
  2389. for (bb = 0; bb < current_nr_blocks; bb++)
  2390. {
  2391. rtx_insn *head, *tail;
  2392. gcc_assert (EBB_FIRST_BB (bb) == EBB_LAST_BB (bb));
  2393. get_ebb_head_tail (EBB_FIRST_BB (bb), EBB_LAST_BB (bb), &head, &tail);
  2394. sched_free_deps (head, tail, false);
  2395. }
  2396. }
  2397. static int rgn_n_insns;
  2398. /* Compute insn priority for a current region. */
  2399. void
  2400. compute_priorities (void)
  2401. {
  2402. int bb;
  2403. current_sched_info->sched_max_insns_priority = 0;
  2404. for (bb = 0; bb < current_nr_blocks; bb++)
  2405. {
  2406. rtx_insn *head, *tail;
  2407. gcc_assert (EBB_FIRST_BB (bb) == EBB_LAST_BB (bb));
  2408. get_ebb_head_tail (EBB_FIRST_BB (bb), EBB_LAST_BB (bb), &head, &tail);
  2409. if (no_real_insns_p (head, tail))
  2410. continue;
  2411. rgn_n_insns += set_priorities (head, tail);
  2412. }
  2413. current_sched_info->sched_max_insns_priority++;
  2414. }
  2415. /* (Re-)initialize the arrays of DFA states at the end of each basic block.
  2416. SAVED_LAST_BASIC_BLOCK is the previous length of the arrays. It must be
  2417. zero for the first call to this function, to allocate the arrays for the
  2418. first time.
  2419. This function is called once during initialization of the scheduler, and
  2420. called again to resize the arrays if new basic blocks have been created,
  2421. for example for speculation recovery code. */
  2422. static void
  2423. realloc_bb_state_array (int saved_last_basic_block)
  2424. {
  2425. char *old_bb_state_array = bb_state_array;
  2426. size_t lbb = (size_t) last_basic_block_for_fn (cfun);
  2427. size_t slbb = (size_t) saved_last_basic_block;
  2428. /* Nothing to do if nothing changed since the last time this was called. */
  2429. if (saved_last_basic_block == last_basic_block_for_fn (cfun))
  2430. return;
  2431. /* The selective scheduler doesn't use the state arrays. */
  2432. if (sel_sched_p ())
  2433. {
  2434. gcc_assert (bb_state_array == NULL && bb_state == NULL);
  2435. return;
  2436. }
  2437. gcc_checking_assert (saved_last_basic_block == 0
  2438. || (bb_state_array != NULL && bb_state != NULL));
  2439. bb_state_array = XRESIZEVEC (char, bb_state_array, lbb * dfa_state_size);
  2440. bb_state = XRESIZEVEC (state_t, bb_state, lbb);
  2441. /* If BB_STATE_ARRAY has moved, fixup all the state pointers array.
  2442. Otherwise only fixup the newly allocated ones. For the state
  2443. array itself, only initialize the new entries. */
  2444. bool bb_state_array_moved = (bb_state_array != old_bb_state_array);
  2445. for (size_t i = bb_state_array_moved ? 0 : slbb; i < lbb; i++)
  2446. bb_state[i] = (state_t) (bb_state_array + i * dfa_state_size);
  2447. for (size_t i = slbb; i < lbb; i++)
  2448. state_reset (bb_state[i]);
  2449. }
  2450. /* Free the arrays of DFA states at the end of each basic block. */
  2451. static void
  2452. free_bb_state_array (void)
  2453. {
  2454. free (bb_state_array);
  2455. free (bb_state);
  2456. bb_state_array = NULL;
  2457. bb_state = NULL;
  2458. }
  2459. /* Schedule a region. A region is either an inner loop, a loop-free
  2460. subroutine, or a single basic block. Each bb in the region is
  2461. scheduled after its flow predecessors. */
  2462. static void
  2463. schedule_region (int rgn)
  2464. {
  2465. int bb;
  2466. int sched_rgn_n_insns = 0;
  2467. rgn_n_insns = 0;
  2468. /* Do not support register pressure sensitive scheduling for the new regions
  2469. as we don't update the liveness info for them. */
  2470. if (sched_pressure != SCHED_PRESSURE_NONE
  2471. && rgn >= nr_regions_initial)
  2472. {
  2473. free_global_sched_pressure_data ();
  2474. sched_pressure = SCHED_PRESSURE_NONE;
  2475. }
  2476. rgn_setup_region (rgn);
  2477. /* Don't schedule region that is marked by
  2478. NOTE_DISABLE_SCHED_OF_BLOCK. */
  2479. if (sched_is_disabled_for_current_region_p ())
  2480. return;
  2481. sched_rgn_compute_dependencies (rgn);
  2482. sched_rgn_local_init (rgn);
  2483. /* Set priorities. */
  2484. compute_priorities ();
  2485. sched_extend_ready_list (rgn_n_insns);
  2486. if (sched_pressure == SCHED_PRESSURE_WEIGHTED)
  2487. {
  2488. sched_init_region_reg_pressure_info ();
  2489. for (bb = 0; bb < current_nr_blocks; bb++)
  2490. {
  2491. basic_block first_bb, last_bb;
  2492. rtx_insn *head, *tail;
  2493. first_bb = EBB_FIRST_BB (bb);
  2494. last_bb = EBB_LAST_BB (bb);
  2495. get_ebb_head_tail (first_bb, last_bb, &head, &tail);
  2496. if (no_real_insns_p (head, tail))
  2497. {
  2498. gcc_assert (first_bb == last_bb);
  2499. continue;
  2500. }
  2501. sched_setup_bb_reg_pressure_info (first_bb, PREV_INSN (head));
  2502. }
  2503. }
  2504. /* Now we can schedule all blocks. */
  2505. for (bb = 0; bb < current_nr_blocks; bb++)
  2506. {
  2507. basic_block first_bb, last_bb, curr_bb;
  2508. rtx_insn *head, *tail;
  2509. first_bb = EBB_FIRST_BB (bb);
  2510. last_bb = EBB_LAST_BB (bb);
  2511. get_ebb_head_tail (first_bb, last_bb, &head, &tail);
  2512. if (no_real_insns_p (head, tail))
  2513. {
  2514. gcc_assert (first_bb == last_bb);
  2515. continue;
  2516. }
  2517. current_sched_info->prev_head = PREV_INSN (head);
  2518. current_sched_info->next_tail = NEXT_INSN (tail);
  2519. remove_notes (head, tail);
  2520. unlink_bb_notes (first_bb, last_bb);
  2521. target_bb = bb;
  2522. gcc_assert (flag_schedule_interblock || current_nr_blocks == 1);
  2523. current_sched_info->queue_must_finish_empty = current_nr_blocks == 1;
  2524. curr_bb = first_bb;
  2525. if (dbg_cnt (sched_block))
  2526. {
  2527. edge f;
  2528. int saved_last_basic_block = last_basic_block_for_fn (cfun);
  2529. schedule_block (&curr_bb, bb_state[first_bb->index]);
  2530. gcc_assert (EBB_FIRST_BB (bb) == first_bb);
  2531. sched_rgn_n_insns += sched_n_insns;
  2532. realloc_bb_state_array (saved_last_basic_block);
  2533. f = find_fallthru_edge (last_bb->succs);
  2534. if (f && f->probability * 100 / REG_BR_PROB_BASE >=
  2535. PARAM_VALUE (PARAM_SCHED_STATE_EDGE_PROB_CUTOFF))
  2536. {
  2537. memcpy (bb_state[f->dest->index], curr_state,
  2538. dfa_state_size);
  2539. if (sched_verbose >= 5)
  2540. fprintf (sched_dump, "saving state for edge %d->%d\n",
  2541. f->src->index, f->dest->index);
  2542. }
  2543. }
  2544. else
  2545. {
  2546. sched_rgn_n_insns += rgn_n_insns;
  2547. }
  2548. /* Clean up. */
  2549. if (current_nr_blocks > 1)
  2550. free_trg_info ();
  2551. }
  2552. /* Sanity check: verify that all region insns were scheduled. */
  2553. gcc_assert (sched_rgn_n_insns == rgn_n_insns);
  2554. sched_finish_ready_list ();
  2555. /* Done with this region. */
  2556. sched_rgn_local_finish ();
  2557. /* Free dependencies. */
  2558. for (bb = 0; bb < current_nr_blocks; ++bb)
  2559. free_block_dependencies (bb);
  2560. gcc_assert (haifa_recovery_bb_ever_added_p
  2561. || deps_pools_are_empty_p ());
  2562. }
  2563. /* Initialize data structures for region scheduling. */
  2564. void
  2565. sched_rgn_init (bool single_blocks_p)
  2566. {
  2567. min_spec_prob = ((PARAM_VALUE (PARAM_MIN_SPEC_PROB) * REG_BR_PROB_BASE)
  2568. / 100);
  2569. nr_inter = 0;
  2570. nr_spec = 0;
  2571. extend_regions ();
  2572. CONTAINING_RGN (ENTRY_BLOCK) = -1;
  2573. CONTAINING_RGN (EXIT_BLOCK) = -1;
  2574. realloc_bb_state_array (0);
  2575. /* Compute regions for scheduling. */
  2576. if (single_blocks_p
  2577. || n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS + 1
  2578. || !flag_schedule_interblock
  2579. || is_cfg_nonregular ())
  2580. {
  2581. find_single_block_region (sel_sched_p ());
  2582. }
  2583. else
  2584. {
  2585. /* Compute the dominators and post dominators. */
  2586. if (!sel_sched_p ())
  2587. calculate_dominance_info (CDI_DOMINATORS);
  2588. /* Find regions. */
  2589. find_rgns ();
  2590. if (sched_verbose >= 3)
  2591. debug_regions ();
  2592. /* For now. This will move as more and more of haifa is converted
  2593. to using the cfg code. */
  2594. if (!sel_sched_p ())
  2595. free_dominance_info (CDI_DOMINATORS);
  2596. }
  2597. gcc_assert (0 < nr_regions && nr_regions <= n_basic_blocks_for_fn (cfun));
  2598. RGN_BLOCKS (nr_regions) = (RGN_BLOCKS (nr_regions - 1) +
  2599. RGN_NR_BLOCKS (nr_regions - 1));
  2600. nr_regions_initial = nr_regions;
  2601. }
  2602. /* Free data structures for region scheduling. */
  2603. void
  2604. sched_rgn_finish (void)
  2605. {
  2606. free_bb_state_array ();
  2607. /* Reposition the prologue and epilogue notes in case we moved the
  2608. prologue/epilogue insns. */
  2609. if (reload_completed)
  2610. reposition_prologue_and_epilogue_notes ();
  2611. if (sched_verbose)
  2612. {
  2613. if (reload_completed == 0
  2614. && flag_schedule_interblock)
  2615. {
  2616. fprintf (sched_dump,
  2617. "\n;; Procedure interblock/speculative motions == %d/%d \n",
  2618. nr_inter, nr_spec);
  2619. }
  2620. else
  2621. gcc_assert (nr_inter <= 0);
  2622. fprintf (sched_dump, "\n\n");
  2623. }
  2624. nr_regions = 0;
  2625. free (rgn_table);
  2626. rgn_table = NULL;
  2627. free (rgn_bb_table);
  2628. rgn_bb_table = NULL;
  2629. free (block_to_bb);
  2630. block_to_bb = NULL;
  2631. free (containing_rgn);
  2632. containing_rgn = NULL;
  2633. free (ebb_head);
  2634. ebb_head = NULL;
  2635. }
  2636. /* Setup global variables like CURRENT_BLOCKS and CURRENT_NR_BLOCK to
  2637. point to the region RGN. */
  2638. void
  2639. rgn_setup_region (int rgn)
  2640. {
  2641. int bb;
  2642. /* Set variables for the current region. */
  2643. current_nr_blocks = RGN_NR_BLOCKS (rgn);
  2644. current_blocks = RGN_BLOCKS (rgn);
  2645. /* EBB_HEAD is a region-scope structure. But we realloc it for
  2646. each region to save time/memory/something else.
  2647. See comments in add_block1, for what reasons we allocate +1 element. */
  2648. ebb_head = XRESIZEVEC (int, ebb_head, current_nr_blocks + 1);
  2649. for (bb = 0; bb <= current_nr_blocks; bb++)
  2650. ebb_head[bb] = current_blocks + bb;
  2651. }
  2652. /* Compute instruction dependencies in region RGN. */
  2653. void
  2654. sched_rgn_compute_dependencies (int rgn)
  2655. {
  2656. if (!RGN_DONT_CALC_DEPS (rgn))
  2657. {
  2658. int bb;
  2659. if (sel_sched_p ())
  2660. sched_emulate_haifa_p = 1;
  2661. init_deps_global ();
  2662. /* Initializations for region data dependence analysis. */
  2663. bb_deps = XNEWVEC (struct deps_desc, current_nr_blocks);
  2664. for (bb = 0; bb < current_nr_blocks; bb++)
  2665. init_deps (bb_deps + bb, false);
  2666. /* Initialize bitmap used in add_branch_dependences. */
  2667. insn_referenced = sbitmap_alloc (sched_max_luid);
  2668. bitmap_clear (insn_referenced);
  2669. /* Compute backward dependencies. */
  2670. for (bb = 0; bb < current_nr_blocks; bb++)
  2671. compute_block_dependences (bb);
  2672. sbitmap_free (insn_referenced);
  2673. free_pending_lists ();
  2674. finish_deps_global ();
  2675. free (bb_deps);
  2676. /* We don't want to recalculate this twice. */
  2677. RGN_DONT_CALC_DEPS (rgn) = 1;
  2678. if (sel_sched_p ())
  2679. sched_emulate_haifa_p = 0;
  2680. }
  2681. else
  2682. /* (This is a recovery block. It is always a single block region.)
  2683. OR (We use selective scheduling.) */
  2684. gcc_assert (current_nr_blocks == 1 || sel_sched_p ());
  2685. }
  2686. /* Init region data structures. Returns true if this region should
  2687. not be scheduled. */
  2688. void
  2689. sched_rgn_local_init (int rgn)
  2690. {
  2691. int bb;
  2692. /* Compute interblock info: probabilities, split-edges, dominators, etc. */
  2693. if (current_nr_blocks > 1)
  2694. {
  2695. basic_block block;
  2696. edge e;
  2697. edge_iterator ei;
  2698. prob = XNEWVEC (int, current_nr_blocks);
  2699. dom = sbitmap_vector_alloc (current_nr_blocks, current_nr_blocks);
  2700. bitmap_vector_clear (dom, current_nr_blocks);
  2701. /* Use ->aux to implement EDGE_TO_BIT mapping. */
  2702. rgn_nr_edges = 0;
  2703. FOR_EACH_BB_FN (block, cfun)
  2704. {
  2705. if (CONTAINING_RGN (block->index) != rgn)
  2706. continue;
  2707. FOR_EACH_EDGE (e, ei, block->succs)
  2708. SET_EDGE_TO_BIT (e, rgn_nr_edges++);
  2709. }
  2710. rgn_edges = XNEWVEC (edge, rgn_nr_edges);
  2711. rgn_nr_edges = 0;
  2712. FOR_EACH_BB_FN (block, cfun)
  2713. {
  2714. if (CONTAINING_RGN (block->index) != rgn)
  2715. continue;
  2716. FOR_EACH_EDGE (e, ei, block->succs)
  2717. rgn_edges[rgn_nr_edges++] = e;
  2718. }
  2719. /* Split edges. */
  2720. pot_split = sbitmap_vector_alloc (current_nr_blocks, rgn_nr_edges);
  2721. bitmap_vector_clear (pot_split, current_nr_blocks);
  2722. ancestor_edges = sbitmap_vector_alloc (current_nr_blocks, rgn_nr_edges);
  2723. bitmap_vector_clear (ancestor_edges, current_nr_blocks);
  2724. /* Compute probabilities, dominators, split_edges. */
  2725. for (bb = 0; bb < current_nr_blocks; bb++)
  2726. compute_dom_prob_ps (bb);
  2727. /* Cleanup ->aux used for EDGE_TO_BIT mapping. */
  2728. /* We don't need them anymore. But we want to avoid duplication of
  2729. aux fields in the newly created edges. */
  2730. FOR_EACH_BB_FN (block, cfun)
  2731. {
  2732. if (CONTAINING_RGN (block->index) != rgn)
  2733. continue;
  2734. FOR_EACH_EDGE (e, ei, block->succs)
  2735. e->aux = NULL;
  2736. }
  2737. }
  2738. }
  2739. /* Free data computed for the finished region. */
  2740. void
  2741. sched_rgn_local_free (void)
  2742. {
  2743. free (prob);
  2744. sbitmap_vector_free (dom);
  2745. sbitmap_vector_free (pot_split);
  2746. sbitmap_vector_free (ancestor_edges);
  2747. free (rgn_edges);
  2748. }
  2749. /* Free data computed for the finished region. */
  2750. void
  2751. sched_rgn_local_finish (void)
  2752. {
  2753. if (current_nr_blocks > 1 && !sel_sched_p ())
  2754. {
  2755. sched_rgn_local_free ();
  2756. }
  2757. }
  2758. /* Setup scheduler infos. */
  2759. void
  2760. rgn_setup_common_sched_info (void)
  2761. {
  2762. memcpy (&rgn_common_sched_info, &haifa_common_sched_info,
  2763. sizeof (rgn_common_sched_info));
  2764. rgn_common_sched_info.fix_recovery_cfg = rgn_fix_recovery_cfg;
  2765. rgn_common_sched_info.add_block = rgn_add_block;
  2766. rgn_common_sched_info.estimate_number_of_insns
  2767. = rgn_estimate_number_of_insns;
  2768. rgn_common_sched_info.sched_pass_id = SCHED_RGN_PASS;
  2769. common_sched_info = &rgn_common_sched_info;
  2770. }
  2771. /* Setup all *_sched_info structures (for the Haifa frontend
  2772. and for the dependence analysis) in the interblock scheduler. */
  2773. void
  2774. rgn_setup_sched_infos (void)
  2775. {
  2776. if (!sel_sched_p ())
  2777. memcpy (&rgn_sched_deps_info, &rgn_const_sched_deps_info,
  2778. sizeof (rgn_sched_deps_info));
  2779. else
  2780. memcpy (&rgn_sched_deps_info, &rgn_const_sel_sched_deps_info,
  2781. sizeof (rgn_sched_deps_info));
  2782. sched_deps_info = &rgn_sched_deps_info;
  2783. memcpy (&rgn_sched_info, &rgn_const_sched_info, sizeof (rgn_sched_info));
  2784. current_sched_info = &rgn_sched_info;
  2785. }
  2786. /* The one entry point in this file. */
  2787. void
  2788. schedule_insns (void)
  2789. {
  2790. int rgn;
  2791. /* Taking care of this degenerate case makes the rest of
  2792. this code simpler. */
  2793. if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS)
  2794. return;
  2795. rgn_setup_common_sched_info ();
  2796. rgn_setup_sched_infos ();
  2797. haifa_sched_init ();
  2798. sched_rgn_init (reload_completed);
  2799. bitmap_initialize (&not_in_df, 0);
  2800. bitmap_clear (&not_in_df);
  2801. /* Schedule every region in the subroutine. */
  2802. for (rgn = 0; rgn < nr_regions; rgn++)
  2803. if (dbg_cnt (sched_region))
  2804. schedule_region (rgn);
  2805. /* Clean up. */
  2806. sched_rgn_finish ();
  2807. bitmap_clear (&not_in_df);
  2808. haifa_sched_finish ();
  2809. }
  2810. /* INSN has been added to/removed from current region. */
  2811. static void
  2812. rgn_add_remove_insn (rtx_insn *insn, int remove_p)
  2813. {
  2814. if (!remove_p)
  2815. rgn_n_insns++;
  2816. else
  2817. rgn_n_insns--;
  2818. if (INSN_BB (insn) == target_bb)
  2819. {
  2820. if (!remove_p)
  2821. target_n_insns++;
  2822. else
  2823. target_n_insns--;
  2824. }
  2825. }
  2826. /* Extend internal data structures. */
  2827. void
  2828. extend_regions (void)
  2829. {
  2830. rgn_table = XRESIZEVEC (region, rgn_table, n_basic_blocks_for_fn (cfun));
  2831. rgn_bb_table = XRESIZEVEC (int, rgn_bb_table,
  2832. n_basic_blocks_for_fn (cfun));
  2833. block_to_bb = XRESIZEVEC (int, block_to_bb,
  2834. last_basic_block_for_fn (cfun));
  2835. containing_rgn = XRESIZEVEC (int, containing_rgn,
  2836. last_basic_block_for_fn (cfun));
  2837. }
  2838. void
  2839. rgn_make_new_region_out_of_new_block (basic_block bb)
  2840. {
  2841. int i;
  2842. i = RGN_BLOCKS (nr_regions);
  2843. /* I - first free position in rgn_bb_table. */
  2844. rgn_bb_table[i] = bb->index;
  2845. RGN_NR_BLOCKS (nr_regions) = 1;
  2846. RGN_HAS_REAL_EBB (nr_regions) = 0;
  2847. RGN_DONT_CALC_DEPS (nr_regions) = 0;
  2848. CONTAINING_RGN (bb->index) = nr_regions;
  2849. BLOCK_TO_BB (bb->index) = 0;
  2850. nr_regions++;
  2851. RGN_BLOCKS (nr_regions) = i + 1;
  2852. }
  2853. /* BB was added to ebb after AFTER. */
  2854. static void
  2855. rgn_add_block (basic_block bb, basic_block after)
  2856. {
  2857. extend_regions ();
  2858. bitmap_set_bit (&not_in_df, bb->index);
  2859. if (after == 0 || after == EXIT_BLOCK_PTR_FOR_FN (cfun))
  2860. {
  2861. rgn_make_new_region_out_of_new_block (bb);
  2862. RGN_DONT_CALC_DEPS (nr_regions - 1) = (after
  2863. == EXIT_BLOCK_PTR_FOR_FN (cfun));
  2864. }
  2865. else
  2866. {
  2867. int i, pos;
  2868. /* We need to fix rgn_table, block_to_bb, containing_rgn
  2869. and ebb_head. */
  2870. BLOCK_TO_BB (bb->index) = BLOCK_TO_BB (after->index);
  2871. /* We extend ebb_head to one more position to
  2872. easily find the last position of the last ebb in
  2873. the current region. Thus, ebb_head[BLOCK_TO_BB (after) + 1]
  2874. is _always_ valid for access. */
  2875. i = BLOCK_TO_BB (after->index) + 1;
  2876. pos = ebb_head[i] - 1;
  2877. /* Now POS is the index of the last block in the region. */
  2878. /* Find index of basic block AFTER. */
  2879. for (; rgn_bb_table[pos] != after->index; pos--)
  2880. ;
  2881. pos++;
  2882. gcc_assert (pos > ebb_head[i - 1]);
  2883. /* i - ebb right after "AFTER". */
  2884. /* ebb_head[i] - VALID. */
  2885. /* Source position: ebb_head[i]
  2886. Destination position: ebb_head[i] + 1
  2887. Last position:
  2888. RGN_BLOCKS (nr_regions) - 1
  2889. Number of elements to copy: (last_position) - (source_position) + 1
  2890. */
  2891. memmove (rgn_bb_table + pos + 1,
  2892. rgn_bb_table + pos,
  2893. ((RGN_BLOCKS (nr_regions) - 1) - (pos) + 1)
  2894. * sizeof (*rgn_bb_table));
  2895. rgn_bb_table[pos] = bb->index;
  2896. for (; i <= current_nr_blocks; i++)
  2897. ebb_head [i]++;
  2898. i = CONTAINING_RGN (after->index);
  2899. CONTAINING_RGN (bb->index) = i;
  2900. RGN_HAS_REAL_EBB (i) = 1;
  2901. for (++i; i <= nr_regions; i++)
  2902. RGN_BLOCKS (i)++;
  2903. }
  2904. }
  2905. /* Fix internal data after interblock movement of jump instruction.
  2906. For parameter meaning please refer to
  2907. sched-int.h: struct sched_info: fix_recovery_cfg. */
  2908. static void
  2909. rgn_fix_recovery_cfg (int bbi, int check_bbi, int check_bb_nexti)
  2910. {
  2911. int old_pos, new_pos, i;
  2912. BLOCK_TO_BB (check_bb_nexti) = BLOCK_TO_BB (bbi);
  2913. for (old_pos = ebb_head[BLOCK_TO_BB (check_bbi) + 1] - 1;
  2914. rgn_bb_table[old_pos] != check_bb_nexti;
  2915. old_pos--)
  2916. ;
  2917. gcc_assert (old_pos > ebb_head[BLOCK_TO_BB (check_bbi)]);
  2918. for (new_pos = ebb_head[BLOCK_TO_BB (bbi) + 1] - 1;
  2919. rgn_bb_table[new_pos] != bbi;
  2920. new_pos--)
  2921. ;
  2922. new_pos++;
  2923. gcc_assert (new_pos > ebb_head[BLOCK_TO_BB (bbi)]);
  2924. gcc_assert (new_pos < old_pos);
  2925. memmove (rgn_bb_table + new_pos + 1,
  2926. rgn_bb_table + new_pos,
  2927. (old_pos - new_pos) * sizeof (*rgn_bb_table));
  2928. rgn_bb_table[new_pos] = check_bb_nexti;
  2929. for (i = BLOCK_TO_BB (bbi) + 1; i <= BLOCK_TO_BB (check_bbi); i++)
  2930. ebb_head[i]++;
  2931. }
  2932. /* Return next block in ebb chain. For parameter meaning please refer to
  2933. sched-int.h: struct sched_info: advance_target_bb. */
  2934. static basic_block
  2935. advance_target_bb (basic_block bb, rtx_insn *insn)
  2936. {
  2937. if (insn)
  2938. return 0;
  2939. gcc_assert (BLOCK_TO_BB (bb->index) == target_bb
  2940. && BLOCK_TO_BB (bb->next_bb->index) == target_bb);
  2941. return bb->next_bb;
  2942. }
  2943. #endif
  2944. /* Run instruction scheduler. */
  2945. static unsigned int
  2946. rest_of_handle_live_range_shrinkage (void)
  2947. {
  2948. #ifdef INSN_SCHEDULING
  2949. int saved;
  2950. initialize_live_range_shrinkage ();
  2951. saved = flag_schedule_interblock;
  2952. flag_schedule_interblock = false;
  2953. schedule_insns ();
  2954. flag_schedule_interblock = saved;
  2955. finish_live_range_shrinkage ();
  2956. #endif
  2957. return 0;
  2958. }
  2959. /* Run instruction scheduler. */
  2960. static unsigned int
  2961. rest_of_handle_sched (void)
  2962. {
  2963. #ifdef INSN_SCHEDULING
  2964. if (flag_selective_scheduling
  2965. && ! maybe_skip_selective_scheduling ())
  2966. run_selective_scheduling ();
  2967. else
  2968. schedule_insns ();
  2969. #endif
  2970. return 0;
  2971. }
  2972. /* Run second scheduling pass after reload. */
  2973. static unsigned int
  2974. rest_of_handle_sched2 (void)
  2975. {
  2976. #ifdef INSN_SCHEDULING
  2977. if (flag_selective_scheduling2
  2978. && ! maybe_skip_selective_scheduling ())
  2979. run_selective_scheduling ();
  2980. else
  2981. {
  2982. /* Do control and data sched analysis again,
  2983. and write some more of the results to dump file. */
  2984. if (flag_sched2_use_superblocks)
  2985. schedule_ebbs ();
  2986. else
  2987. schedule_insns ();
  2988. }
  2989. #endif
  2990. return 0;
  2991. }
  2992. static unsigned int
  2993. rest_of_handle_sched_fusion (void)
  2994. {
  2995. #ifdef INSN_SCHEDULING
  2996. sched_fusion = true;
  2997. schedule_insns ();
  2998. sched_fusion = false;
  2999. #endif
  3000. return 0;
  3001. }
  3002. namespace {
  3003. const pass_data pass_data_live_range_shrinkage =
  3004. {
  3005. RTL_PASS, /* type */
  3006. "lr_shrinkage", /* name */
  3007. OPTGROUP_NONE, /* optinfo_flags */
  3008. TV_LIVE_RANGE_SHRINKAGE, /* tv_id */
  3009. 0, /* properties_required */
  3010. 0, /* properties_provided */
  3011. 0, /* properties_destroyed */
  3012. 0, /* todo_flags_start */
  3013. TODO_df_finish, /* todo_flags_finish */
  3014. };
  3015. class pass_live_range_shrinkage : public rtl_opt_pass
  3016. {
  3017. public:
  3018. pass_live_range_shrinkage(gcc::context *ctxt)
  3019. : rtl_opt_pass(pass_data_live_range_shrinkage, ctxt)
  3020. {}
  3021. /* opt_pass methods: */
  3022. virtual bool gate (function *)
  3023. {
  3024. #ifdef INSN_SCHEDULING
  3025. return flag_live_range_shrinkage;
  3026. #else
  3027. return 0;
  3028. #endif
  3029. }
  3030. virtual unsigned int execute (function *)
  3031. {
  3032. return rest_of_handle_live_range_shrinkage ();
  3033. }
  3034. }; // class pass_live_range_shrinkage
  3035. } // anon namespace
  3036. rtl_opt_pass *
  3037. make_pass_live_range_shrinkage (gcc::context *ctxt)
  3038. {
  3039. return new pass_live_range_shrinkage (ctxt);
  3040. }
  3041. namespace {
  3042. const pass_data pass_data_sched =
  3043. {
  3044. RTL_PASS, /* type */
  3045. "sched1", /* name */
  3046. OPTGROUP_NONE, /* optinfo_flags */
  3047. TV_SCHED, /* tv_id */
  3048. 0, /* properties_required */
  3049. 0, /* properties_provided */
  3050. 0, /* properties_destroyed */
  3051. 0, /* todo_flags_start */
  3052. TODO_df_finish, /* todo_flags_finish */
  3053. };
  3054. class pass_sched : public rtl_opt_pass
  3055. {
  3056. public:
  3057. pass_sched (gcc::context *ctxt)
  3058. : rtl_opt_pass (pass_data_sched, ctxt)
  3059. {}
  3060. /* opt_pass methods: */
  3061. virtual bool gate (function *);
  3062. virtual unsigned int execute (function *) { return rest_of_handle_sched (); }
  3063. }; // class pass_sched
  3064. bool
  3065. pass_sched::gate (function *)
  3066. {
  3067. #ifdef INSN_SCHEDULING
  3068. return optimize > 0 && flag_schedule_insns && dbg_cnt (sched_func);
  3069. #else
  3070. return 0;
  3071. #endif
  3072. }
  3073. } // anon namespace
  3074. rtl_opt_pass *
  3075. make_pass_sched (gcc::context *ctxt)
  3076. {
  3077. return new pass_sched (ctxt);
  3078. }
  3079. namespace {
  3080. const pass_data pass_data_sched2 =
  3081. {
  3082. RTL_PASS, /* type */
  3083. "sched2", /* name */
  3084. OPTGROUP_NONE, /* optinfo_flags */
  3085. TV_SCHED2, /* tv_id */
  3086. 0, /* properties_required */
  3087. 0, /* properties_provided */
  3088. 0, /* properties_destroyed */
  3089. 0, /* todo_flags_start */
  3090. TODO_df_finish, /* todo_flags_finish */
  3091. };
  3092. class pass_sched2 : public rtl_opt_pass
  3093. {
  3094. public:
  3095. pass_sched2 (gcc::context *ctxt)
  3096. : rtl_opt_pass (pass_data_sched2, ctxt)
  3097. {}
  3098. /* opt_pass methods: */
  3099. virtual bool gate (function *);
  3100. virtual unsigned int execute (function *)
  3101. {
  3102. return rest_of_handle_sched2 ();
  3103. }
  3104. }; // class pass_sched2
  3105. bool
  3106. pass_sched2::gate (function *)
  3107. {
  3108. #ifdef INSN_SCHEDULING
  3109. return optimize > 0 && flag_schedule_insns_after_reload
  3110. && !targetm.delay_sched2 && dbg_cnt (sched2_func);
  3111. #else
  3112. return 0;
  3113. #endif
  3114. }
  3115. } // anon namespace
  3116. rtl_opt_pass *
  3117. make_pass_sched2 (gcc::context *ctxt)
  3118. {
  3119. return new pass_sched2 (ctxt);
  3120. }
  3121. namespace {
  3122. const pass_data pass_data_sched_fusion =
  3123. {
  3124. RTL_PASS, /* type */
  3125. "sched_fusion", /* name */
  3126. OPTGROUP_NONE, /* optinfo_flags */
  3127. TV_SCHED_FUSION, /* tv_id */
  3128. 0, /* properties_required */
  3129. 0, /* properties_provided */
  3130. 0, /* properties_destroyed */
  3131. 0, /* todo_flags_start */
  3132. TODO_df_finish, /* todo_flags_finish */
  3133. };
  3134. class pass_sched_fusion : public rtl_opt_pass
  3135. {
  3136. public:
  3137. pass_sched_fusion (gcc::context *ctxt)
  3138. : rtl_opt_pass (pass_data_sched_fusion, ctxt)
  3139. {}
  3140. /* opt_pass methods: */
  3141. virtual bool gate (function *);
  3142. virtual unsigned int execute (function *)
  3143. {
  3144. return rest_of_handle_sched_fusion ();
  3145. }
  3146. }; // class pass_sched2
  3147. bool
  3148. pass_sched_fusion::gate (function *)
  3149. {
  3150. #ifdef INSN_SCHEDULING
  3151. /* Scheduling fusion relies on peephole2 to do real fusion work,
  3152. so only enable it if peephole2 is in effect. */
  3153. return (optimize > 0 && flag_peephole2
  3154. && flag_schedule_fusion && targetm.sched.fusion_priority != NULL);
  3155. #else
  3156. return 0;
  3157. #endif
  3158. }
  3159. } // anon namespace
  3160. rtl_opt_pass *
  3161. make_pass_sched_fusion (gcc::context *ctxt)
  3162. {
  3163. return new pass_sched_fusion (ctxt);
  3164. }