cfgrtl.c 140 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171
  1. /* Control flow graph manipulation code for GNU compiler.
  2. Copyright (C) 1987-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. /* This file contains low level functions to manipulate the CFG and analyze it
  16. that are aware of the RTL intermediate language.
  17. Available functionality:
  18. - Basic CFG/RTL manipulation API documented in cfghooks.h
  19. - CFG-aware instruction chain manipulation
  20. delete_insn, delete_insn_chain
  21. - Edge splitting and committing to edges
  22. insert_insn_on_edge, commit_edge_insertions
  23. - CFG updating after insn simplification
  24. purge_dead_edges, purge_all_dead_edges
  25. - CFG fixing after coarse manipulation
  26. fixup_abnormal_edges
  27. Functions not supposed for generic use:
  28. - Infrastructure to determine quickly basic block for insn
  29. compute_bb_for_insn, update_bb_for_insn, set_block_for_insn,
  30. - Edge redirection with updating and optimizing of insn chain
  31. block_label, tidy_fallthru_edge, force_nonfallthru */
  32. #include "config.h"
  33. #include "system.h"
  34. #include "coretypes.h"
  35. #include "tm.h"
  36. #include "hash-set.h"
  37. #include "machmode.h"
  38. #include "vec.h"
  39. #include "double-int.h"
  40. #include "input.h"
  41. #include "alias.h"
  42. #include "symtab.h"
  43. #include "wide-int.h"
  44. #include "inchash.h"
  45. #include "tree.h"
  46. #include "hard-reg-set.h"
  47. #include "predict.h"
  48. #include "hashtab.h"
  49. #include "function.h"
  50. #include "dominance.h"
  51. #include "cfg.h"
  52. #include "cfgrtl.h"
  53. #include "cfganal.h"
  54. #include "cfgbuild.h"
  55. #include "cfgcleanup.h"
  56. #include "basic-block.h"
  57. #include "bb-reorder.h"
  58. #include "regs.h"
  59. #include "flags.h"
  60. #include "except.h"
  61. #include "rtl-error.h"
  62. #include "tm_p.h"
  63. #include "obstack.h"
  64. #include "insn-attr.h"
  65. #include "insn-config.h"
  66. #include "rtl.h"
  67. #include "statistics.h"
  68. #include "real.h"
  69. #include "fixed-value.h"
  70. #include "expmed.h"
  71. #include "dojump.h"
  72. #include "explow.h"
  73. #include "calls.h"
  74. #include "emit-rtl.h"
  75. #include "varasm.h"
  76. #include "stmt.h"
  77. #include "expr.h"
  78. #include "target.h"
  79. #include "common/common-target.h"
  80. #include "cfgloop.h"
  81. #include "ggc.h"
  82. #include "tree-pass.h"
  83. #include "df.h"
  84. /* Holds the interesting leading and trailing notes for the function.
  85. Only applicable if the CFG is in cfglayout mode. */
  86. static GTY(()) rtx_insn *cfg_layout_function_footer;
  87. static GTY(()) rtx_insn *cfg_layout_function_header;
  88. static rtx_insn *skip_insns_after_block (basic_block);
  89. static void record_effective_endpoints (void);
  90. static rtx label_for_bb (basic_block);
  91. static void fixup_reorder_chain (void);
  92. void verify_insn_chain (void);
  93. static void fixup_fallthru_exit_predecessor (void);
  94. static int can_delete_note_p (const rtx_note *);
  95. static int can_delete_label_p (const rtx_code_label *);
  96. static basic_block rtl_split_edge (edge);
  97. static bool rtl_move_block_after (basic_block, basic_block);
  98. static int rtl_verify_flow_info (void);
  99. static basic_block cfg_layout_split_block (basic_block, void *);
  100. static edge cfg_layout_redirect_edge_and_branch (edge, basic_block);
  101. static basic_block cfg_layout_redirect_edge_and_branch_force (edge, basic_block);
  102. static void cfg_layout_delete_block (basic_block);
  103. static void rtl_delete_block (basic_block);
  104. static basic_block rtl_redirect_edge_and_branch_force (edge, basic_block);
  105. static edge rtl_redirect_edge_and_branch (edge, basic_block);
  106. static basic_block rtl_split_block (basic_block, void *);
  107. static void rtl_dump_bb (FILE *, basic_block, int, int);
  108. static int rtl_verify_flow_info_1 (void);
  109. static void rtl_make_forwarder_block (edge);
  110. /* Return true if NOTE is not one of the ones that must be kept paired,
  111. so that we may simply delete it. */
  112. static int
  113. can_delete_note_p (const rtx_note *note)
  114. {
  115. switch (NOTE_KIND (note))
  116. {
  117. case NOTE_INSN_DELETED:
  118. case NOTE_INSN_BASIC_BLOCK:
  119. case NOTE_INSN_EPILOGUE_BEG:
  120. return true;
  121. default:
  122. return false;
  123. }
  124. }
  125. /* True if a given label can be deleted. */
  126. static int
  127. can_delete_label_p (const rtx_code_label *label)
  128. {
  129. return (!LABEL_PRESERVE_P (label)
  130. /* User declared labels must be preserved. */
  131. && LABEL_NAME (label) == 0
  132. && !in_expr_list_p (forced_labels, label));
  133. }
  134. /* Delete INSN by patching it out. */
  135. void
  136. delete_insn (rtx uncast_insn)
  137. {
  138. rtx_insn *insn = as_a <rtx_insn *> (uncast_insn);
  139. rtx note;
  140. bool really_delete = true;
  141. if (LABEL_P (insn))
  142. {
  143. /* Some labels can't be directly removed from the INSN chain, as they
  144. might be references via variables, constant pool etc.
  145. Convert them to the special NOTE_INSN_DELETED_LABEL note. */
  146. if (! can_delete_label_p (as_a <rtx_code_label *> (insn)))
  147. {
  148. const char *name = LABEL_NAME (insn);
  149. basic_block bb = BLOCK_FOR_INSN (insn);
  150. rtx_insn *bb_note = NEXT_INSN (insn);
  151. really_delete = false;
  152. PUT_CODE (insn, NOTE);
  153. NOTE_KIND (insn) = NOTE_INSN_DELETED_LABEL;
  154. NOTE_DELETED_LABEL_NAME (insn) = name;
  155. /* If the note following the label starts a basic block, and the
  156. label is a member of the same basic block, interchange the two. */
  157. if (bb_note != NULL_RTX
  158. && NOTE_INSN_BASIC_BLOCK_P (bb_note)
  159. && bb != NULL
  160. && bb == BLOCK_FOR_INSN (bb_note))
  161. {
  162. reorder_insns_nobb (insn, insn, bb_note);
  163. BB_HEAD (bb) = bb_note;
  164. if (BB_END (bb) == bb_note)
  165. BB_END (bb) = insn;
  166. }
  167. }
  168. remove_node_from_insn_list (insn, &nonlocal_goto_handler_labels);
  169. }
  170. if (really_delete)
  171. {
  172. /* If this insn has already been deleted, something is very wrong. */
  173. gcc_assert (!insn->deleted ());
  174. if (INSN_P (insn))
  175. df_insn_delete (insn);
  176. remove_insn (insn);
  177. insn->set_deleted ();
  178. }
  179. /* If deleting a jump, decrement the use count of the label. Deleting
  180. the label itself should happen in the normal course of block merging. */
  181. if (JUMP_P (insn))
  182. {
  183. if (JUMP_LABEL (insn)
  184. && LABEL_P (JUMP_LABEL (insn)))
  185. LABEL_NUSES (JUMP_LABEL (insn))--;
  186. /* If there are more targets, remove them too. */
  187. while ((note
  188. = find_reg_note (insn, REG_LABEL_TARGET, NULL_RTX)) != NULL_RTX
  189. && LABEL_P (XEXP (note, 0)))
  190. {
  191. LABEL_NUSES (XEXP (note, 0))--;
  192. remove_note (insn, note);
  193. }
  194. }
  195. /* Also if deleting any insn that references a label as an operand. */
  196. while ((note = find_reg_note (insn, REG_LABEL_OPERAND, NULL_RTX)) != NULL_RTX
  197. && LABEL_P (XEXP (note, 0)))
  198. {
  199. LABEL_NUSES (XEXP (note, 0))--;
  200. remove_note (insn, note);
  201. }
  202. if (rtx_jump_table_data *table = dyn_cast <rtx_jump_table_data *> (insn))
  203. {
  204. rtvec vec = table->get_labels ();
  205. int len = GET_NUM_ELEM (vec);
  206. int i;
  207. for (i = 0; i < len; i++)
  208. {
  209. rtx label = XEXP (RTVEC_ELT (vec, i), 0);
  210. /* When deleting code in bulk (e.g. removing many unreachable
  211. blocks) we can delete a label that's a target of the vector
  212. before deleting the vector itself. */
  213. if (!NOTE_P (label))
  214. LABEL_NUSES (label)--;
  215. }
  216. }
  217. }
  218. /* Like delete_insn but also purge dead edges from BB. */
  219. void
  220. delete_insn_and_edges (rtx_insn *insn)
  221. {
  222. bool purge = false;
  223. if (INSN_P (insn)
  224. && BLOCK_FOR_INSN (insn)
  225. && BB_END (BLOCK_FOR_INSN (insn)) == insn)
  226. purge = true;
  227. delete_insn (insn);
  228. if (purge)
  229. purge_dead_edges (BLOCK_FOR_INSN (insn));
  230. }
  231. /* Unlink a chain of insns between START and FINISH, leaving notes
  232. that must be paired. If CLEAR_BB is true, we set bb field for
  233. insns that cannot be removed to NULL. */
  234. void
  235. delete_insn_chain (rtx start, rtx finish, bool clear_bb)
  236. {
  237. rtx_insn *prev, *current;
  238. /* Unchain the insns one by one. It would be quicker to delete all of these
  239. with a single unchaining, rather than one at a time, but we need to keep
  240. the NOTE's. */
  241. current = safe_as_a <rtx_insn *> (finish);
  242. while (1)
  243. {
  244. prev = PREV_INSN (current);
  245. if (NOTE_P (current) && !can_delete_note_p (as_a <rtx_note *> (current)))
  246. ;
  247. else
  248. delete_insn (current);
  249. if (clear_bb && !current->deleted ())
  250. set_block_for_insn (current, NULL);
  251. if (current == start)
  252. break;
  253. current = prev;
  254. }
  255. }
  256. /* Create a new basic block consisting of the instructions between HEAD and END
  257. inclusive. This function is designed to allow fast BB construction - reuses
  258. the note and basic block struct in BB_NOTE, if any and do not grow
  259. BASIC_BLOCK chain and should be used directly only by CFG construction code.
  260. END can be NULL in to create new empty basic block before HEAD. Both END
  261. and HEAD can be NULL to create basic block at the end of INSN chain.
  262. AFTER is the basic block we should be put after. */
  263. basic_block
  264. create_basic_block_structure (rtx_insn *head, rtx_insn *end, rtx_note *bb_note,
  265. basic_block after)
  266. {
  267. basic_block bb;
  268. if (bb_note
  269. && (bb = NOTE_BASIC_BLOCK (bb_note)) != NULL
  270. && bb->aux == NULL)
  271. {
  272. /* If we found an existing note, thread it back onto the chain. */
  273. rtx_insn *after;
  274. if (LABEL_P (head))
  275. after = head;
  276. else
  277. {
  278. after = PREV_INSN (head);
  279. head = bb_note;
  280. }
  281. if (after != bb_note && NEXT_INSN (after) != bb_note)
  282. reorder_insns_nobb (bb_note, bb_note, after);
  283. }
  284. else
  285. {
  286. /* Otherwise we must create a note and a basic block structure. */
  287. bb = alloc_block ();
  288. init_rtl_bb_info (bb);
  289. if (!head && !end)
  290. head = end = bb_note
  291. = emit_note_after (NOTE_INSN_BASIC_BLOCK, get_last_insn ());
  292. else if (LABEL_P (head) && end)
  293. {
  294. bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, head);
  295. if (head == end)
  296. end = bb_note;
  297. }
  298. else
  299. {
  300. bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, head);
  301. head = bb_note;
  302. if (!end)
  303. end = head;
  304. }
  305. NOTE_BASIC_BLOCK (bb_note) = bb;
  306. }
  307. /* Always include the bb note in the block. */
  308. if (NEXT_INSN (end) == bb_note)
  309. end = bb_note;
  310. BB_HEAD (bb) = head;
  311. BB_END (bb) = end;
  312. bb->index = last_basic_block_for_fn (cfun)++;
  313. bb->flags = BB_NEW | BB_RTL;
  314. link_block (bb, after);
  315. SET_BASIC_BLOCK_FOR_FN (cfun, bb->index, bb);
  316. df_bb_refs_record (bb->index, false);
  317. update_bb_for_insn (bb);
  318. BB_SET_PARTITION (bb, BB_UNPARTITIONED);
  319. /* Tag the block so that we know it has been used when considering
  320. other basic block notes. */
  321. bb->aux = bb;
  322. return bb;
  323. }
  324. /* Create new basic block consisting of instructions in between HEAD and END
  325. and place it to the BB chain after block AFTER. END can be NULL to
  326. create a new empty basic block before HEAD. Both END and HEAD can be
  327. NULL to create basic block at the end of INSN chain. */
  328. static basic_block
  329. rtl_create_basic_block (void *headp, void *endp, basic_block after)
  330. {
  331. rtx_insn *head = (rtx_insn *) headp;
  332. rtx_insn *end = (rtx_insn *) endp;
  333. basic_block bb;
  334. /* Grow the basic block array if needed. */
  335. if ((size_t) last_basic_block_for_fn (cfun)
  336. >= basic_block_info_for_fn (cfun)->length ())
  337. {
  338. size_t new_size =
  339. (last_basic_block_for_fn (cfun)
  340. + (last_basic_block_for_fn (cfun) + 3) / 4);
  341. vec_safe_grow_cleared (basic_block_info_for_fn (cfun), new_size);
  342. }
  343. n_basic_blocks_for_fn (cfun)++;
  344. bb = create_basic_block_structure (head, end, NULL, after);
  345. bb->aux = NULL;
  346. return bb;
  347. }
  348. static basic_block
  349. cfg_layout_create_basic_block (void *head, void *end, basic_block after)
  350. {
  351. basic_block newbb = rtl_create_basic_block (head, end, after);
  352. return newbb;
  353. }
  354. /* Delete the insns in a (non-live) block. We physically delete every
  355. non-deleted-note insn, and update the flow graph appropriately.
  356. Return nonzero if we deleted an exception handler. */
  357. /* ??? Preserving all such notes strikes me as wrong. It would be nice
  358. to post-process the stream to remove empty blocks, loops, ranges, etc. */
  359. static void
  360. rtl_delete_block (basic_block b)
  361. {
  362. rtx_insn *insn, *end;
  363. /* If the head of this block is a CODE_LABEL, then it might be the
  364. label for an exception handler which can't be reached. We need
  365. to remove the label from the exception_handler_label list. */
  366. insn = BB_HEAD (b);
  367. end = get_last_bb_insn (b);
  368. /* Selectively delete the entire chain. */
  369. BB_HEAD (b) = NULL;
  370. delete_insn_chain (insn, end, true);
  371. if (dump_file)
  372. fprintf (dump_file, "deleting block %d\n", b->index);
  373. df_bb_delete (b->index);
  374. }
  375. /* Records the basic block struct in BLOCK_FOR_INSN for every insn. */
  376. void
  377. compute_bb_for_insn (void)
  378. {
  379. basic_block bb;
  380. FOR_EACH_BB_FN (bb, cfun)
  381. {
  382. rtx_insn *end = BB_END (bb);
  383. rtx_insn *insn;
  384. for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
  385. {
  386. BLOCK_FOR_INSN (insn) = bb;
  387. if (insn == end)
  388. break;
  389. }
  390. }
  391. }
  392. /* Release the basic_block_for_insn array. */
  393. unsigned int
  394. free_bb_for_insn (void)
  395. {
  396. rtx_insn *insn;
  397. for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
  398. if (!BARRIER_P (insn))
  399. BLOCK_FOR_INSN (insn) = NULL;
  400. return 0;
  401. }
  402. namespace {
  403. const pass_data pass_data_free_cfg =
  404. {
  405. RTL_PASS, /* type */
  406. "*free_cfg", /* name */
  407. OPTGROUP_NONE, /* optinfo_flags */
  408. TV_NONE, /* tv_id */
  409. 0, /* properties_required */
  410. 0, /* properties_provided */
  411. PROP_cfg, /* properties_destroyed */
  412. 0, /* todo_flags_start */
  413. 0, /* todo_flags_finish */
  414. };
  415. class pass_free_cfg : public rtl_opt_pass
  416. {
  417. public:
  418. pass_free_cfg (gcc::context *ctxt)
  419. : rtl_opt_pass (pass_data_free_cfg, ctxt)
  420. {}
  421. /* opt_pass methods: */
  422. virtual unsigned int execute (function *);
  423. }; // class pass_free_cfg
  424. unsigned int
  425. pass_free_cfg::execute (function *)
  426. {
  427. #ifdef DELAY_SLOTS
  428. /* The resource.c machinery uses DF but the CFG isn't guaranteed to be
  429. valid at that point so it would be too late to call df_analyze. */
  430. if (optimize > 0 && flag_delayed_branch)
  431. {
  432. df_note_add_problem ();
  433. df_analyze ();
  434. }
  435. #endif
  436. if (crtl->has_bb_partition)
  437. insert_section_boundary_note ();
  438. free_bb_for_insn ();
  439. return 0;
  440. }
  441. } // anon namespace
  442. rtl_opt_pass *
  443. make_pass_free_cfg (gcc::context *ctxt)
  444. {
  445. return new pass_free_cfg (ctxt);
  446. }
  447. /* Return RTX to emit after when we want to emit code on the entry of function. */
  448. rtx_insn *
  449. entry_of_function (void)
  450. {
  451. return (n_basic_blocks_for_fn (cfun) > NUM_FIXED_BLOCKS ?
  452. BB_HEAD (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) : get_insns ());
  453. }
  454. /* Emit INSN at the entry point of the function, ensuring that it is only
  455. executed once per function. */
  456. void
  457. emit_insn_at_entry (rtx insn)
  458. {
  459. edge_iterator ei = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
  460. edge e = ei_safe_edge (ei);
  461. gcc_assert (e->flags & EDGE_FALLTHRU);
  462. insert_insn_on_edge (insn, e);
  463. commit_edge_insertions ();
  464. }
  465. /* Update BLOCK_FOR_INSN of insns between BEGIN and END
  466. (or BARRIER if found) and notify df of the bb change.
  467. The insn chain range is inclusive
  468. (i.e. both BEGIN and END will be updated. */
  469. static void
  470. update_bb_for_insn_chain (rtx_insn *begin, rtx_insn *end, basic_block bb)
  471. {
  472. rtx_insn *insn;
  473. end = NEXT_INSN (end);
  474. for (insn = begin; insn != end; insn = NEXT_INSN (insn))
  475. if (!BARRIER_P (insn))
  476. df_insn_change_bb (insn, bb);
  477. }
  478. /* Update BLOCK_FOR_INSN of insns in BB to BB,
  479. and notify df of the change. */
  480. void
  481. update_bb_for_insn (basic_block bb)
  482. {
  483. update_bb_for_insn_chain (BB_HEAD (bb), BB_END (bb), bb);
  484. }
  485. /* Like active_insn_p, except keep the return value clobber around
  486. even after reload. */
  487. static bool
  488. flow_active_insn_p (const rtx_insn *insn)
  489. {
  490. if (active_insn_p (insn))
  491. return true;
  492. /* A clobber of the function return value exists for buggy
  493. programs that fail to return a value. Its effect is to
  494. keep the return value from being live across the entire
  495. function. If we allow it to be skipped, we introduce the
  496. possibility for register lifetime confusion. */
  497. if (GET_CODE (PATTERN (insn)) == CLOBBER
  498. && REG_P (XEXP (PATTERN (insn), 0))
  499. && REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn), 0)))
  500. return true;
  501. return false;
  502. }
  503. /* Return true if the block has no effect and only forwards control flow to
  504. its single destination. */
  505. bool
  506. contains_no_active_insn_p (const_basic_block bb)
  507. {
  508. rtx_insn *insn;
  509. if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun) || bb == ENTRY_BLOCK_PTR_FOR_FN (cfun)
  510. || !single_succ_p (bb))
  511. return false;
  512. for (insn = BB_HEAD (bb); insn != BB_END (bb); insn = NEXT_INSN (insn))
  513. if (INSN_P (insn) && flow_active_insn_p (insn))
  514. return false;
  515. return (!INSN_P (insn)
  516. || (JUMP_P (insn) && simplejump_p (insn))
  517. || !flow_active_insn_p (insn));
  518. }
  519. /* Likewise, but protect loop latches, headers and preheaders. */
  520. /* FIXME: Make this a cfg hook. */
  521. bool
  522. forwarder_block_p (const_basic_block bb)
  523. {
  524. if (!contains_no_active_insn_p (bb))
  525. return false;
  526. /* Protect loop latches, headers and preheaders. */
  527. if (current_loops)
  528. {
  529. basic_block dest;
  530. if (bb->loop_father->header == bb)
  531. return false;
  532. dest = EDGE_SUCC (bb, 0)->dest;
  533. if (dest->loop_father->header == dest)
  534. return false;
  535. }
  536. return true;
  537. }
  538. /* Return nonzero if we can reach target from src by falling through. */
  539. /* FIXME: Make this a cfg hook, the result is only valid in cfgrtl mode. */
  540. bool
  541. can_fallthru (basic_block src, basic_block target)
  542. {
  543. rtx_insn *insn = BB_END (src);
  544. rtx_insn *insn2;
  545. edge e;
  546. edge_iterator ei;
  547. if (target == EXIT_BLOCK_PTR_FOR_FN (cfun))
  548. return true;
  549. if (src->next_bb != target)
  550. return false;
  551. /* ??? Later we may add code to move jump tables offline. */
  552. if (tablejump_p (insn, NULL, NULL))
  553. return false;
  554. FOR_EACH_EDGE (e, ei, src->succs)
  555. if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)
  556. && e->flags & EDGE_FALLTHRU)
  557. return false;
  558. insn2 = BB_HEAD (target);
  559. if (!active_insn_p (insn2))
  560. insn2 = next_active_insn (insn2);
  561. return next_active_insn (insn) == insn2;
  562. }
  563. /* Return nonzero if we could reach target from src by falling through,
  564. if the target was made adjacent. If we already have a fall-through
  565. edge to the exit block, we can't do that. */
  566. static bool
  567. could_fall_through (basic_block src, basic_block target)
  568. {
  569. edge e;
  570. edge_iterator ei;
  571. if (target == EXIT_BLOCK_PTR_FOR_FN (cfun))
  572. return true;
  573. FOR_EACH_EDGE (e, ei, src->succs)
  574. if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)
  575. && e->flags & EDGE_FALLTHRU)
  576. return 0;
  577. return true;
  578. }
  579. /* Return the NOTE_INSN_BASIC_BLOCK of BB. */
  580. rtx_note *
  581. bb_note (basic_block bb)
  582. {
  583. rtx_insn *note;
  584. note = BB_HEAD (bb);
  585. if (LABEL_P (note))
  586. note = NEXT_INSN (note);
  587. gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note));
  588. return as_a <rtx_note *> (note);
  589. }
  590. /* Return the INSN immediately following the NOTE_INSN_BASIC_BLOCK
  591. note associated with the BLOCK. */
  592. static rtx_insn *
  593. first_insn_after_basic_block_note (basic_block block)
  594. {
  595. rtx_insn *insn;
  596. /* Get the first instruction in the block. */
  597. insn = BB_HEAD (block);
  598. if (insn == NULL_RTX)
  599. return NULL;
  600. if (LABEL_P (insn))
  601. insn = NEXT_INSN (insn);
  602. gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
  603. return NEXT_INSN (insn);
  604. }
  605. /* Creates a new basic block just after basic block BB by splitting
  606. everything after specified instruction INSNP. */
  607. static basic_block
  608. rtl_split_block (basic_block bb, void *insnp)
  609. {
  610. basic_block new_bb;
  611. rtx_insn *insn = (rtx_insn *) insnp;
  612. edge e;
  613. edge_iterator ei;
  614. if (!insn)
  615. {
  616. insn = first_insn_after_basic_block_note (bb);
  617. if (insn)
  618. {
  619. rtx_insn *next = insn;
  620. insn = PREV_INSN (insn);
  621. /* If the block contains only debug insns, insn would have
  622. been NULL in a non-debug compilation, and then we'd end
  623. up emitting a DELETED note. For -fcompare-debug
  624. stability, emit the note too. */
  625. if (insn != BB_END (bb)
  626. && DEBUG_INSN_P (next)
  627. && DEBUG_INSN_P (BB_END (bb)))
  628. {
  629. while (next != BB_END (bb) && DEBUG_INSN_P (next))
  630. next = NEXT_INSN (next);
  631. if (next == BB_END (bb))
  632. emit_note_after (NOTE_INSN_DELETED, next);
  633. }
  634. }
  635. else
  636. insn = get_last_insn ();
  637. }
  638. /* We probably should check type of the insn so that we do not create
  639. inconsistent cfg. It is checked in verify_flow_info anyway, so do not
  640. bother. */
  641. if (insn == BB_END (bb))
  642. emit_note_after (NOTE_INSN_DELETED, insn);
  643. /* Create the new basic block. */
  644. new_bb = create_basic_block (NEXT_INSN (insn), BB_END (bb), bb);
  645. BB_COPY_PARTITION (new_bb, bb);
  646. BB_END (bb) = insn;
  647. /* Redirect the outgoing edges. */
  648. new_bb->succs = bb->succs;
  649. bb->succs = NULL;
  650. FOR_EACH_EDGE (e, ei, new_bb->succs)
  651. e->src = new_bb;
  652. /* The new block starts off being dirty. */
  653. df_set_bb_dirty (bb);
  654. return new_bb;
  655. }
  656. /* Return true if the single edge between blocks A and B is the only place
  657. in RTL which holds some unique locus. */
  658. static bool
  659. unique_locus_on_edge_between_p (basic_block a, basic_block b)
  660. {
  661. const location_t goto_locus = EDGE_SUCC (a, 0)->goto_locus;
  662. rtx_insn *insn, *end;
  663. if (LOCATION_LOCUS (goto_locus) == UNKNOWN_LOCATION)
  664. return false;
  665. /* First scan block A backward. */
  666. insn = BB_END (a);
  667. end = PREV_INSN (BB_HEAD (a));
  668. while (insn != end && (!NONDEBUG_INSN_P (insn) || !INSN_HAS_LOCATION (insn)))
  669. insn = PREV_INSN (insn);
  670. if (insn != end && INSN_LOCATION (insn) == goto_locus)
  671. return false;
  672. /* Then scan block B forward. */
  673. insn = BB_HEAD (b);
  674. if (insn)
  675. {
  676. end = NEXT_INSN (BB_END (b));
  677. while (insn != end && !NONDEBUG_INSN_P (insn))
  678. insn = NEXT_INSN (insn);
  679. if (insn != end && INSN_HAS_LOCATION (insn)
  680. && INSN_LOCATION (insn) == goto_locus)
  681. return false;
  682. }
  683. return true;
  684. }
  685. /* If the single edge between blocks A and B is the only place in RTL which
  686. holds some unique locus, emit a nop with that locus between the blocks. */
  687. static void
  688. emit_nop_for_unique_locus_between (basic_block a, basic_block b)
  689. {
  690. if (!unique_locus_on_edge_between_p (a, b))
  691. return;
  692. BB_END (a) = emit_insn_after_noloc (gen_nop (), BB_END (a), a);
  693. INSN_LOCATION (BB_END (a)) = EDGE_SUCC (a, 0)->goto_locus;
  694. }
  695. /* Blocks A and B are to be merged into a single block A. The insns
  696. are already contiguous. */
  697. static void
  698. rtl_merge_blocks (basic_block a, basic_block b)
  699. {
  700. rtx_insn *b_head = BB_HEAD (b), *b_end = BB_END (b), *a_end = BB_END (a);
  701. rtx_insn *del_first = NULL, *del_last = NULL;
  702. rtx_insn *b_debug_start = b_end, *b_debug_end = b_end;
  703. bool forwarder_p = (b->flags & BB_FORWARDER_BLOCK) != 0;
  704. int b_empty = 0;
  705. if (dump_file)
  706. fprintf (dump_file, "Merging block %d into block %d...\n", b->index,
  707. a->index);
  708. while (DEBUG_INSN_P (b_end))
  709. b_end = PREV_INSN (b_debug_start = b_end);
  710. /* If there was a CODE_LABEL beginning B, delete it. */
  711. if (LABEL_P (b_head))
  712. {
  713. /* Detect basic blocks with nothing but a label. This can happen
  714. in particular at the end of a function. */
  715. if (b_head == b_end)
  716. b_empty = 1;
  717. del_first = del_last = b_head;
  718. b_head = NEXT_INSN (b_head);
  719. }
  720. /* Delete the basic block note and handle blocks containing just that
  721. note. */
  722. if (NOTE_INSN_BASIC_BLOCK_P (b_head))
  723. {
  724. if (b_head == b_end)
  725. b_empty = 1;
  726. if (! del_last)
  727. del_first = b_head;
  728. del_last = b_head;
  729. b_head = NEXT_INSN (b_head);
  730. }
  731. /* If there was a jump out of A, delete it. */
  732. if (JUMP_P (a_end))
  733. {
  734. rtx_insn *prev;
  735. for (prev = PREV_INSN (a_end); ; prev = PREV_INSN (prev))
  736. if (!NOTE_P (prev)
  737. || NOTE_INSN_BASIC_BLOCK_P (prev)
  738. || prev == BB_HEAD (a))
  739. break;
  740. del_first = a_end;
  741. #ifdef HAVE_cc0
  742. /* If this was a conditional jump, we need to also delete
  743. the insn that set cc0. */
  744. if (only_sets_cc0_p (prev))
  745. {
  746. rtx_insn *tmp = prev;
  747. prev = prev_nonnote_insn (prev);
  748. if (!prev)
  749. prev = BB_HEAD (a);
  750. del_first = tmp;
  751. }
  752. #endif
  753. a_end = PREV_INSN (del_first);
  754. }
  755. else if (BARRIER_P (NEXT_INSN (a_end)))
  756. del_first = NEXT_INSN (a_end);
  757. /* Delete everything marked above as well as crap that might be
  758. hanging out between the two blocks. */
  759. BB_END (a) = a_end;
  760. BB_HEAD (b) = b_empty ? NULL : b_head;
  761. delete_insn_chain (del_first, del_last, true);
  762. /* When not optimizing and the edge is the only place in RTL which holds
  763. some unique locus, emit a nop with that locus in between. */
  764. if (!optimize)
  765. {
  766. emit_nop_for_unique_locus_between (a, b);
  767. a_end = BB_END (a);
  768. }
  769. /* Reassociate the insns of B with A. */
  770. if (!b_empty)
  771. {
  772. update_bb_for_insn_chain (a_end, b_debug_end, a);
  773. BB_END (a) = b_debug_end;
  774. BB_HEAD (b) = NULL;
  775. }
  776. else if (b_end != b_debug_end)
  777. {
  778. /* Move any deleted labels and other notes between the end of A
  779. and the debug insns that make up B after the debug insns,
  780. bringing the debug insns into A while keeping the notes after
  781. the end of A. */
  782. if (NEXT_INSN (a_end) != b_debug_start)
  783. reorder_insns_nobb (NEXT_INSN (a_end), PREV_INSN (b_debug_start),
  784. b_debug_end);
  785. update_bb_for_insn_chain (b_debug_start, b_debug_end, a);
  786. BB_END (a) = b_debug_end;
  787. }
  788. df_bb_delete (b->index);
  789. /* If B was a forwarder block, propagate the locus on the edge. */
  790. if (forwarder_p
  791. && LOCATION_LOCUS (EDGE_SUCC (b, 0)->goto_locus) == UNKNOWN_LOCATION)
  792. EDGE_SUCC (b, 0)->goto_locus = EDGE_SUCC (a, 0)->goto_locus;
  793. if (dump_file)
  794. fprintf (dump_file, "Merged blocks %d and %d.\n", a->index, b->index);
  795. }
  796. /* Return true when block A and B can be merged. */
  797. static bool
  798. rtl_can_merge_blocks (basic_block a, basic_block b)
  799. {
  800. /* If we are partitioning hot/cold basic blocks, we don't want to
  801. mess up unconditional or indirect jumps that cross between hot
  802. and cold sections.
  803. Basic block partitioning may result in some jumps that appear to
  804. be optimizable (or blocks that appear to be mergeable), but which really
  805. must be left untouched (they are required to make it safely across
  806. partition boundaries). See the comments at the top of
  807. bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
  808. if (BB_PARTITION (a) != BB_PARTITION (b))
  809. return false;
  810. /* Protect the loop latches. */
  811. if (current_loops && b->loop_father->latch == b)
  812. return false;
  813. /* There must be exactly one edge in between the blocks. */
  814. return (single_succ_p (a)
  815. && single_succ (a) == b
  816. && single_pred_p (b)
  817. && a != b
  818. /* Must be simple edge. */
  819. && !(single_succ_edge (a)->flags & EDGE_COMPLEX)
  820. && a->next_bb == b
  821. && a != ENTRY_BLOCK_PTR_FOR_FN (cfun)
  822. && b != EXIT_BLOCK_PTR_FOR_FN (cfun)
  823. /* If the jump insn has side effects,
  824. we can't kill the edge. */
  825. && (!JUMP_P (BB_END (a))
  826. || (reload_completed
  827. ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
  828. }
  829. /* Return the label in the head of basic block BLOCK. Create one if it doesn't
  830. exist. */
  831. rtx
  832. block_label (basic_block block)
  833. {
  834. if (block == EXIT_BLOCK_PTR_FOR_FN (cfun))
  835. return NULL_RTX;
  836. if (!LABEL_P (BB_HEAD (block)))
  837. {
  838. BB_HEAD (block) = emit_label_before (gen_label_rtx (), BB_HEAD (block));
  839. }
  840. return BB_HEAD (block);
  841. }
  842. /* Attempt to perform edge redirection by replacing possibly complex jump
  843. instruction by unconditional jump or removing jump completely. This can
  844. apply only if all edges now point to the same block. The parameters and
  845. return values are equivalent to redirect_edge_and_branch. */
  846. edge
  847. try_redirect_by_replacing_jump (edge e, basic_block target, bool in_cfglayout)
  848. {
  849. basic_block src = e->src;
  850. rtx_insn *insn = BB_END (src), *kill_from;
  851. rtx set;
  852. int fallthru = 0;
  853. /* If we are partitioning hot/cold basic blocks, we don't want to
  854. mess up unconditional or indirect jumps that cross between hot
  855. and cold sections.
  856. Basic block partitioning may result in some jumps that appear to
  857. be optimizable (or blocks that appear to be mergeable), but which really
  858. must be left untouched (they are required to make it safely across
  859. partition boundaries). See the comments at the top of
  860. bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
  861. if (BB_PARTITION (src) != BB_PARTITION (target))
  862. return NULL;
  863. /* We can replace or remove a complex jump only when we have exactly
  864. two edges. Also, if we have exactly one outgoing edge, we can
  865. redirect that. */
  866. if (EDGE_COUNT (src->succs) >= 3
  867. /* Verify that all targets will be TARGET. Specifically, the
  868. edge that is not E must also go to TARGET. */
  869. || (EDGE_COUNT (src->succs) == 2
  870. && EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest != target))
  871. return NULL;
  872. if (!onlyjump_p (insn))
  873. return NULL;
  874. if ((!optimize || reload_completed) && tablejump_p (insn, NULL, NULL))
  875. return NULL;
  876. /* Avoid removing branch with side effects. */
  877. set = single_set (insn);
  878. if (!set || side_effects_p (set))
  879. return NULL;
  880. /* In case we zap a conditional jump, we'll need to kill
  881. the cc0 setter too. */
  882. kill_from = insn;
  883. #ifdef HAVE_cc0
  884. if (reg_mentioned_p (cc0_rtx, PATTERN (insn))
  885. && only_sets_cc0_p (PREV_INSN (insn)))
  886. kill_from = PREV_INSN (insn);
  887. #endif
  888. /* See if we can create the fallthru edge. */
  889. if (in_cfglayout || can_fallthru (src, target))
  890. {
  891. if (dump_file)
  892. fprintf (dump_file, "Removing jump %i.\n", INSN_UID (insn));
  893. fallthru = 1;
  894. /* Selectively unlink whole insn chain. */
  895. if (in_cfglayout)
  896. {
  897. rtx_insn *insn = BB_FOOTER (src);
  898. delete_insn_chain (kill_from, BB_END (src), false);
  899. /* Remove barriers but keep jumptables. */
  900. while (insn)
  901. {
  902. if (BARRIER_P (insn))
  903. {
  904. if (PREV_INSN (insn))
  905. SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
  906. else
  907. BB_FOOTER (src) = NEXT_INSN (insn);
  908. if (NEXT_INSN (insn))
  909. SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
  910. }
  911. if (LABEL_P (insn))
  912. break;
  913. insn = NEXT_INSN (insn);
  914. }
  915. }
  916. else
  917. delete_insn_chain (kill_from, PREV_INSN (BB_HEAD (target)),
  918. false);
  919. }
  920. /* If this already is simplejump, redirect it. */
  921. else if (simplejump_p (insn))
  922. {
  923. if (e->dest == target)
  924. return NULL;
  925. if (dump_file)
  926. fprintf (dump_file, "Redirecting jump %i from %i to %i.\n",
  927. INSN_UID (insn), e->dest->index, target->index);
  928. if (!redirect_jump (insn, block_label (target), 0))
  929. {
  930. gcc_assert (target == EXIT_BLOCK_PTR_FOR_FN (cfun));
  931. return NULL;
  932. }
  933. }
  934. /* Cannot do anything for target exit block. */
  935. else if (target == EXIT_BLOCK_PTR_FOR_FN (cfun))
  936. return NULL;
  937. /* Or replace possibly complicated jump insn by simple jump insn. */
  938. else
  939. {
  940. rtx target_label = block_label (target);
  941. rtx_insn *barrier;
  942. rtx label;
  943. rtx_jump_table_data *table;
  944. emit_jump_insn_after_noloc (gen_jump (target_label), insn);
  945. JUMP_LABEL (BB_END (src)) = target_label;
  946. LABEL_NUSES (target_label)++;
  947. if (dump_file)
  948. fprintf (dump_file, "Replacing insn %i by jump %i\n",
  949. INSN_UID (insn), INSN_UID (BB_END (src)));
  950. delete_insn_chain (kill_from, insn, false);
  951. /* Recognize a tablejump that we are converting to a
  952. simple jump and remove its associated CODE_LABEL
  953. and ADDR_VEC or ADDR_DIFF_VEC. */
  954. if (tablejump_p (insn, &label, &table))
  955. delete_insn_chain (label, table, false);
  956. barrier = next_nonnote_insn (BB_END (src));
  957. if (!barrier || !BARRIER_P (barrier))
  958. emit_barrier_after (BB_END (src));
  959. else
  960. {
  961. if (barrier != NEXT_INSN (BB_END (src)))
  962. {
  963. /* Move the jump before barrier so that the notes
  964. which originally were or were created before jump table are
  965. inside the basic block. */
  966. rtx_insn *new_insn = BB_END (src);
  967. update_bb_for_insn_chain (NEXT_INSN (BB_END (src)),
  968. PREV_INSN (barrier), src);
  969. SET_NEXT_INSN (PREV_INSN (new_insn)) = NEXT_INSN (new_insn);
  970. SET_PREV_INSN (NEXT_INSN (new_insn)) = PREV_INSN (new_insn);
  971. SET_NEXT_INSN (new_insn) = barrier;
  972. SET_NEXT_INSN (PREV_INSN (barrier)) = new_insn;
  973. SET_PREV_INSN (new_insn) = PREV_INSN (barrier);
  974. SET_PREV_INSN (barrier) = new_insn;
  975. }
  976. }
  977. }
  978. /* Keep only one edge out and set proper flags. */
  979. if (!single_succ_p (src))
  980. remove_edge (e);
  981. gcc_assert (single_succ_p (src));
  982. e = single_succ_edge (src);
  983. if (fallthru)
  984. e->flags = EDGE_FALLTHRU;
  985. else
  986. e->flags = 0;
  987. e->probability = REG_BR_PROB_BASE;
  988. e->count = src->count;
  989. if (e->dest != target)
  990. redirect_edge_succ (e, target);
  991. return e;
  992. }
  993. /* Subroutine of redirect_branch_edge that tries to patch the jump
  994. instruction INSN so that it reaches block NEW. Do this
  995. only when it originally reached block OLD. Return true if this
  996. worked or the original target wasn't OLD, return false if redirection
  997. doesn't work. */
  998. static bool
  999. patch_jump_insn (rtx_insn *insn, rtx_insn *old_label, basic_block new_bb)
  1000. {
  1001. rtx_jump_table_data *table;
  1002. rtx tmp;
  1003. /* Recognize a tablejump and adjust all matching cases. */
  1004. if (tablejump_p (insn, NULL, &table))
  1005. {
  1006. rtvec vec;
  1007. int j;
  1008. rtx new_label = block_label (new_bb);
  1009. if (new_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
  1010. return false;
  1011. vec = table->get_labels ();
  1012. for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
  1013. if (XEXP (RTVEC_ELT (vec, j), 0) == old_label)
  1014. {
  1015. RTVEC_ELT (vec, j) = gen_rtx_LABEL_REF (Pmode, new_label);
  1016. --LABEL_NUSES (old_label);
  1017. ++LABEL_NUSES (new_label);
  1018. }
  1019. /* Handle casesi dispatch insns. */
  1020. if ((tmp = single_set (insn)) != NULL
  1021. && SET_DEST (tmp) == pc_rtx
  1022. && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
  1023. && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF
  1024. && LABEL_REF_LABEL (XEXP (SET_SRC (tmp), 2)) == old_label)
  1025. {
  1026. XEXP (SET_SRC (tmp), 2) = gen_rtx_LABEL_REF (Pmode,
  1027. new_label);
  1028. --LABEL_NUSES (old_label);
  1029. ++LABEL_NUSES (new_label);
  1030. }
  1031. }
  1032. else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL)
  1033. {
  1034. int i, n = ASM_OPERANDS_LABEL_LENGTH (tmp);
  1035. rtx new_label, note;
  1036. if (new_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
  1037. return false;
  1038. new_label = block_label (new_bb);
  1039. for (i = 0; i < n; ++i)
  1040. {
  1041. rtx old_ref = ASM_OPERANDS_LABEL (tmp, i);
  1042. gcc_assert (GET_CODE (old_ref) == LABEL_REF);
  1043. if (XEXP (old_ref, 0) == old_label)
  1044. {
  1045. ASM_OPERANDS_LABEL (tmp, i)
  1046. = gen_rtx_LABEL_REF (Pmode, new_label);
  1047. --LABEL_NUSES (old_label);
  1048. ++LABEL_NUSES (new_label);
  1049. }
  1050. }
  1051. if (JUMP_LABEL (insn) == old_label)
  1052. {
  1053. JUMP_LABEL (insn) = new_label;
  1054. note = find_reg_note (insn, REG_LABEL_TARGET, new_label);
  1055. if (note)
  1056. remove_note (insn, note);
  1057. }
  1058. else
  1059. {
  1060. note = find_reg_note (insn, REG_LABEL_TARGET, old_label);
  1061. if (note)
  1062. remove_note (insn, note);
  1063. if (JUMP_LABEL (insn) != new_label
  1064. && !find_reg_note (insn, REG_LABEL_TARGET, new_label))
  1065. add_reg_note (insn, REG_LABEL_TARGET, new_label);
  1066. }
  1067. while ((note = find_reg_note (insn, REG_LABEL_OPERAND, old_label))
  1068. != NULL_RTX)
  1069. XEXP (note, 0) = new_label;
  1070. }
  1071. else
  1072. {
  1073. /* ?? We may play the games with moving the named labels from
  1074. one basic block to the other in case only one computed_jump is
  1075. available. */
  1076. if (computed_jump_p (insn)
  1077. /* A return instruction can't be redirected. */
  1078. || returnjump_p (insn))
  1079. return false;
  1080. if (!currently_expanding_to_rtl || JUMP_LABEL (insn) == old_label)
  1081. {
  1082. /* If the insn doesn't go where we think, we're confused. */
  1083. gcc_assert (JUMP_LABEL (insn) == old_label);
  1084. /* If the substitution doesn't succeed, die. This can happen
  1085. if the back end emitted unrecognizable instructions or if
  1086. target is exit block on some arches. */
  1087. if (!redirect_jump (insn, block_label (new_bb), 0))
  1088. {
  1089. gcc_assert (new_bb == EXIT_BLOCK_PTR_FOR_FN (cfun));
  1090. return false;
  1091. }
  1092. }
  1093. }
  1094. return true;
  1095. }
  1096. /* Redirect edge representing branch of (un)conditional jump or tablejump,
  1097. NULL on failure */
  1098. static edge
  1099. redirect_branch_edge (edge e, basic_block target)
  1100. {
  1101. rtx_insn *old_label = BB_HEAD (e->dest);
  1102. basic_block src = e->src;
  1103. rtx_insn *insn = BB_END (src);
  1104. /* We can only redirect non-fallthru edges of jump insn. */
  1105. if (e->flags & EDGE_FALLTHRU)
  1106. return NULL;
  1107. else if (!JUMP_P (insn) && !currently_expanding_to_rtl)
  1108. return NULL;
  1109. if (!currently_expanding_to_rtl)
  1110. {
  1111. if (!patch_jump_insn (insn, old_label, target))
  1112. return NULL;
  1113. }
  1114. else
  1115. /* When expanding this BB might actually contain multiple
  1116. jumps (i.e. not yet split by find_many_sub_basic_blocks).
  1117. Redirect all of those that match our label. */
  1118. FOR_BB_INSNS (src, insn)
  1119. if (JUMP_P (insn) && !patch_jump_insn (insn, old_label, target))
  1120. return NULL;
  1121. if (dump_file)
  1122. fprintf (dump_file, "Edge %i->%i redirected to %i\n",
  1123. e->src->index, e->dest->index, target->index);
  1124. if (e->dest != target)
  1125. e = redirect_edge_succ_nodup (e, target);
  1126. return e;
  1127. }
  1128. /* Called when edge E has been redirected to a new destination,
  1129. in order to update the region crossing flag on the edge and
  1130. jump. */
  1131. static void
  1132. fixup_partition_crossing (edge e)
  1133. {
  1134. if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun) || e->dest
  1135. == EXIT_BLOCK_PTR_FOR_FN (cfun))
  1136. return;
  1137. /* If we redirected an existing edge, it may already be marked
  1138. crossing, even though the new src is missing a reg crossing note.
  1139. But make sure reg crossing note doesn't already exist before
  1140. inserting. */
  1141. if (BB_PARTITION (e->src) != BB_PARTITION (e->dest))
  1142. {
  1143. e->flags |= EDGE_CROSSING;
  1144. if (JUMP_P (BB_END (e->src))
  1145. && !CROSSING_JUMP_P (BB_END (e->src)))
  1146. CROSSING_JUMP_P (BB_END (e->src)) = 1;
  1147. }
  1148. else if (BB_PARTITION (e->src) == BB_PARTITION (e->dest))
  1149. {
  1150. e->flags &= ~EDGE_CROSSING;
  1151. /* Remove the section crossing note from jump at end of
  1152. src if it exists, and if no other successors are
  1153. still crossing. */
  1154. if (JUMP_P (BB_END (e->src)) && CROSSING_JUMP_P (BB_END (e->src)))
  1155. {
  1156. bool has_crossing_succ = false;
  1157. edge e2;
  1158. edge_iterator ei;
  1159. FOR_EACH_EDGE (e2, ei, e->src->succs)
  1160. {
  1161. has_crossing_succ |= (e2->flags & EDGE_CROSSING);
  1162. if (has_crossing_succ)
  1163. break;
  1164. }
  1165. if (!has_crossing_succ)
  1166. CROSSING_JUMP_P (BB_END (e->src)) = 0;
  1167. }
  1168. }
  1169. }
  1170. /* Called when block BB has been reassigned to the cold partition,
  1171. because it is now dominated by another cold block,
  1172. to ensure that the region crossing attributes are updated. */
  1173. static void
  1174. fixup_new_cold_bb (basic_block bb)
  1175. {
  1176. edge e;
  1177. edge_iterator ei;
  1178. /* This is called when a hot bb is found to now be dominated
  1179. by a cold bb and therefore needs to become cold. Therefore,
  1180. its preds will no longer be region crossing. Any non-dominating
  1181. preds that were previously hot would also have become cold
  1182. in the caller for the same region. Any preds that were previously
  1183. region-crossing will be adjusted in fixup_partition_crossing. */
  1184. FOR_EACH_EDGE (e, ei, bb->preds)
  1185. {
  1186. fixup_partition_crossing (e);
  1187. }
  1188. /* Possibly need to make bb's successor edges region crossing,
  1189. or remove stale region crossing. */
  1190. FOR_EACH_EDGE (e, ei, bb->succs)
  1191. {
  1192. /* We can't have fall-through edges across partition boundaries.
  1193. Note that force_nonfallthru will do any necessary partition
  1194. boundary fixup by calling fixup_partition_crossing itself. */
  1195. if ((e->flags & EDGE_FALLTHRU)
  1196. && BB_PARTITION (bb) != BB_PARTITION (e->dest)
  1197. && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
  1198. force_nonfallthru (e);
  1199. else
  1200. fixup_partition_crossing (e);
  1201. }
  1202. }
  1203. /* Attempt to change code to redirect edge E to TARGET. Don't do that on
  1204. expense of adding new instructions or reordering basic blocks.
  1205. Function can be also called with edge destination equivalent to the TARGET.
  1206. Then it should try the simplifications and do nothing if none is possible.
  1207. Return edge representing the branch if transformation succeeded. Return NULL
  1208. on failure.
  1209. We still return NULL in case E already destinated TARGET and we didn't
  1210. managed to simplify instruction stream. */
  1211. static edge
  1212. rtl_redirect_edge_and_branch (edge e, basic_block target)
  1213. {
  1214. edge ret;
  1215. basic_block src = e->src;
  1216. basic_block dest = e->dest;
  1217. if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
  1218. return NULL;
  1219. if (dest == target)
  1220. return e;
  1221. if ((ret = try_redirect_by_replacing_jump (e, target, false)) != NULL)
  1222. {
  1223. df_set_bb_dirty (src);
  1224. fixup_partition_crossing (ret);
  1225. return ret;
  1226. }
  1227. ret = redirect_branch_edge (e, target);
  1228. if (!ret)
  1229. return NULL;
  1230. df_set_bb_dirty (src);
  1231. fixup_partition_crossing (ret);
  1232. return ret;
  1233. }
  1234. /* Emit a barrier after BB, into the footer if we are in CFGLAYOUT mode. */
  1235. void
  1236. emit_barrier_after_bb (basic_block bb)
  1237. {
  1238. rtx_barrier *barrier = emit_barrier_after (BB_END (bb));
  1239. gcc_assert (current_ir_type () == IR_RTL_CFGRTL
  1240. || current_ir_type () == IR_RTL_CFGLAYOUT);
  1241. if (current_ir_type () == IR_RTL_CFGLAYOUT)
  1242. {
  1243. rtx_insn *insn = unlink_insn_chain (barrier, barrier);
  1244. if (BB_FOOTER (bb))
  1245. {
  1246. rtx_insn *footer_tail = BB_FOOTER (bb);
  1247. while (NEXT_INSN (footer_tail))
  1248. footer_tail = NEXT_INSN (footer_tail);
  1249. if (!BARRIER_P (footer_tail))
  1250. {
  1251. SET_NEXT_INSN (footer_tail) = insn;
  1252. SET_PREV_INSN (insn) = footer_tail;
  1253. }
  1254. }
  1255. else
  1256. BB_FOOTER (bb) = insn;
  1257. }
  1258. }
  1259. /* Like force_nonfallthru below, but additionally performs redirection
  1260. Used by redirect_edge_and_branch_force. JUMP_LABEL is used only
  1261. when redirecting to the EXIT_BLOCK, it is either ret_rtx or
  1262. simple_return_rtx, indicating which kind of returnjump to create.
  1263. It should be NULL otherwise. */
  1264. basic_block
  1265. force_nonfallthru_and_redirect (edge e, basic_block target, rtx jump_label)
  1266. {
  1267. basic_block jump_block, new_bb = NULL, src = e->src;
  1268. rtx note;
  1269. edge new_edge;
  1270. int abnormal_edge_flags = 0;
  1271. bool asm_goto_edge = false;
  1272. int loc;
  1273. /* In the case the last instruction is conditional jump to the next
  1274. instruction, first redirect the jump itself and then continue
  1275. by creating a basic block afterwards to redirect fallthru edge. */
  1276. if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
  1277. && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
  1278. && any_condjump_p (BB_END (e->src))
  1279. && JUMP_LABEL (BB_END (e->src)) == BB_HEAD (e->dest))
  1280. {
  1281. rtx note;
  1282. edge b = unchecked_make_edge (e->src, target, 0);
  1283. bool redirected;
  1284. redirected = redirect_jump (BB_END (e->src), block_label (target), 0);
  1285. gcc_assert (redirected);
  1286. note = find_reg_note (BB_END (e->src), REG_BR_PROB, NULL_RTX);
  1287. if (note)
  1288. {
  1289. int prob = XINT (note, 0);
  1290. b->probability = prob;
  1291. /* Update this to use GCOV_COMPUTE_SCALE. */
  1292. b->count = e->count * prob / REG_BR_PROB_BASE;
  1293. e->probability -= e->probability;
  1294. e->count -= b->count;
  1295. if (e->probability < 0)
  1296. e->probability = 0;
  1297. if (e->count < 0)
  1298. e->count = 0;
  1299. }
  1300. }
  1301. if (e->flags & EDGE_ABNORMAL)
  1302. {
  1303. /* Irritating special case - fallthru edge to the same block as abnormal
  1304. edge.
  1305. We can't redirect abnormal edge, but we still can split the fallthru
  1306. one and create separate abnormal edge to original destination.
  1307. This allows bb-reorder to make such edge non-fallthru. */
  1308. gcc_assert (e->dest == target);
  1309. abnormal_edge_flags = e->flags & ~EDGE_FALLTHRU;
  1310. e->flags &= EDGE_FALLTHRU;
  1311. }
  1312. else
  1313. {
  1314. gcc_assert (e->flags & EDGE_FALLTHRU);
  1315. if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
  1316. {
  1317. /* We can't redirect the entry block. Create an empty block
  1318. at the start of the function which we use to add the new
  1319. jump. */
  1320. edge tmp;
  1321. edge_iterator ei;
  1322. bool found = false;
  1323. basic_block bb = create_basic_block (BB_HEAD (e->dest), NULL,
  1324. ENTRY_BLOCK_PTR_FOR_FN (cfun));
  1325. /* Change the existing edge's source to be the new block, and add
  1326. a new edge from the entry block to the new block. */
  1327. e->src = bb;
  1328. for (ei = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs);
  1329. (tmp = ei_safe_edge (ei)); )
  1330. {
  1331. if (tmp == e)
  1332. {
  1333. ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs->unordered_remove (ei.index);
  1334. found = true;
  1335. break;
  1336. }
  1337. else
  1338. ei_next (&ei);
  1339. }
  1340. gcc_assert (found);
  1341. vec_safe_push (bb->succs, e);
  1342. make_single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb,
  1343. EDGE_FALLTHRU);
  1344. }
  1345. }
  1346. /* If e->src ends with asm goto, see if any of the ASM_OPERANDS_LABELs
  1347. don't point to the target or fallthru label. */
  1348. if (JUMP_P (BB_END (e->src))
  1349. && target != EXIT_BLOCK_PTR_FOR_FN (cfun)
  1350. && (e->flags & EDGE_FALLTHRU)
  1351. && (note = extract_asm_operands (PATTERN (BB_END (e->src)))))
  1352. {
  1353. int i, n = ASM_OPERANDS_LABEL_LENGTH (note);
  1354. bool adjust_jump_target = false;
  1355. for (i = 0; i < n; ++i)
  1356. {
  1357. if (XEXP (ASM_OPERANDS_LABEL (note, i), 0) == BB_HEAD (e->dest))
  1358. {
  1359. LABEL_NUSES (XEXP (ASM_OPERANDS_LABEL (note, i), 0))--;
  1360. XEXP (ASM_OPERANDS_LABEL (note, i), 0) = block_label (target);
  1361. LABEL_NUSES (XEXP (ASM_OPERANDS_LABEL (note, i), 0))++;
  1362. adjust_jump_target = true;
  1363. }
  1364. if (XEXP (ASM_OPERANDS_LABEL (note, i), 0) == BB_HEAD (target))
  1365. asm_goto_edge = true;
  1366. }
  1367. if (adjust_jump_target)
  1368. {
  1369. rtx_insn *insn = BB_END (e->src);
  1370. rtx note;
  1371. rtx_insn *old_label = BB_HEAD (e->dest);
  1372. rtx_insn *new_label = BB_HEAD (target);
  1373. if (JUMP_LABEL (insn) == old_label)
  1374. {
  1375. JUMP_LABEL (insn) = new_label;
  1376. note = find_reg_note (insn, REG_LABEL_TARGET, new_label);
  1377. if (note)
  1378. remove_note (insn, note);
  1379. }
  1380. else
  1381. {
  1382. note = find_reg_note (insn, REG_LABEL_TARGET, old_label);
  1383. if (note)
  1384. remove_note (insn, note);
  1385. if (JUMP_LABEL (insn) != new_label
  1386. && !find_reg_note (insn, REG_LABEL_TARGET, new_label))
  1387. add_reg_note (insn, REG_LABEL_TARGET, new_label);
  1388. }
  1389. while ((note = find_reg_note (insn, REG_LABEL_OPERAND, old_label))
  1390. != NULL_RTX)
  1391. XEXP (note, 0) = new_label;
  1392. }
  1393. }
  1394. if (EDGE_COUNT (e->src->succs) >= 2 || abnormal_edge_flags || asm_goto_edge)
  1395. {
  1396. rtx_insn *new_head;
  1397. gcov_type count = e->count;
  1398. int probability = e->probability;
  1399. /* Create the new structures. */
  1400. /* If the old block ended with a tablejump, skip its table
  1401. by searching forward from there. Otherwise start searching
  1402. forward from the last instruction of the old block. */
  1403. rtx_jump_table_data *table;
  1404. if (tablejump_p (BB_END (e->src), NULL, &table))
  1405. new_head = table;
  1406. else
  1407. new_head = BB_END (e->src);
  1408. new_head = NEXT_INSN (new_head);
  1409. jump_block = create_basic_block (new_head, NULL, e->src);
  1410. jump_block->count = count;
  1411. jump_block->frequency = EDGE_FREQUENCY (e);
  1412. /* Make sure new block ends up in correct hot/cold section. */
  1413. BB_COPY_PARTITION (jump_block, e->src);
  1414. /* Wire edge in. */
  1415. new_edge = make_edge (e->src, jump_block, EDGE_FALLTHRU);
  1416. new_edge->probability = probability;
  1417. new_edge->count = count;
  1418. /* Redirect old edge. */
  1419. redirect_edge_pred (e, jump_block);
  1420. e->probability = REG_BR_PROB_BASE;
  1421. /* If e->src was previously region crossing, it no longer is
  1422. and the reg crossing note should be removed. */
  1423. fixup_partition_crossing (new_edge);
  1424. /* If asm goto has any label refs to target's label,
  1425. add also edge from asm goto bb to target. */
  1426. if (asm_goto_edge)
  1427. {
  1428. new_edge->probability /= 2;
  1429. new_edge->count /= 2;
  1430. jump_block->count /= 2;
  1431. jump_block->frequency /= 2;
  1432. new_edge = make_edge (new_edge->src, target,
  1433. e->flags & ~EDGE_FALLTHRU);
  1434. new_edge->probability = probability - probability / 2;
  1435. new_edge->count = count - count / 2;
  1436. }
  1437. new_bb = jump_block;
  1438. }
  1439. else
  1440. jump_block = e->src;
  1441. loc = e->goto_locus;
  1442. e->flags &= ~EDGE_FALLTHRU;
  1443. if (target == EXIT_BLOCK_PTR_FOR_FN (cfun))
  1444. {
  1445. if (jump_label == ret_rtx)
  1446. {
  1447. #ifdef HAVE_return
  1448. emit_jump_insn_after_setloc (gen_return (), BB_END (jump_block), loc);
  1449. #else
  1450. gcc_unreachable ();
  1451. #endif
  1452. }
  1453. else
  1454. {
  1455. gcc_assert (jump_label == simple_return_rtx);
  1456. #ifdef HAVE_simple_return
  1457. emit_jump_insn_after_setloc (gen_simple_return (),
  1458. BB_END (jump_block), loc);
  1459. #else
  1460. gcc_unreachable ();
  1461. #endif
  1462. }
  1463. set_return_jump_label (BB_END (jump_block));
  1464. }
  1465. else
  1466. {
  1467. rtx label = block_label (target);
  1468. emit_jump_insn_after_setloc (gen_jump (label), BB_END (jump_block), loc);
  1469. JUMP_LABEL (BB_END (jump_block)) = label;
  1470. LABEL_NUSES (label)++;
  1471. }
  1472. /* We might be in cfg layout mode, and if so, the following routine will
  1473. insert the barrier correctly. */
  1474. emit_barrier_after_bb (jump_block);
  1475. redirect_edge_succ_nodup (e, target);
  1476. if (abnormal_edge_flags)
  1477. make_edge (src, target, abnormal_edge_flags);
  1478. df_mark_solutions_dirty ();
  1479. fixup_partition_crossing (e);
  1480. return new_bb;
  1481. }
  1482. /* Edge E is assumed to be fallthru edge. Emit needed jump instruction
  1483. (and possibly create new basic block) to make edge non-fallthru.
  1484. Return newly created BB or NULL if none. */
  1485. static basic_block
  1486. rtl_force_nonfallthru (edge e)
  1487. {
  1488. return force_nonfallthru_and_redirect (e, e->dest, NULL_RTX);
  1489. }
  1490. /* Redirect edge even at the expense of creating new jump insn or
  1491. basic block. Return new basic block if created, NULL otherwise.
  1492. Conversion must be possible. */
  1493. static basic_block
  1494. rtl_redirect_edge_and_branch_force (edge e, basic_block target)
  1495. {
  1496. if (redirect_edge_and_branch (e, target)
  1497. || e->dest == target)
  1498. return NULL;
  1499. /* In case the edge redirection failed, try to force it to be non-fallthru
  1500. and redirect newly created simplejump. */
  1501. df_set_bb_dirty (e->src);
  1502. return force_nonfallthru_and_redirect (e, target, NULL_RTX);
  1503. }
  1504. /* The given edge should potentially be a fallthru edge. If that is in
  1505. fact true, delete the jump and barriers that are in the way. */
  1506. static void
  1507. rtl_tidy_fallthru_edge (edge e)
  1508. {
  1509. rtx_insn *q;
  1510. basic_block b = e->src, c = b->next_bb;
  1511. /* ??? In a late-running flow pass, other folks may have deleted basic
  1512. blocks by nopping out blocks, leaving multiple BARRIERs between here
  1513. and the target label. They ought to be chastised and fixed.
  1514. We can also wind up with a sequence of undeletable labels between
  1515. one block and the next.
  1516. So search through a sequence of barriers, labels, and notes for
  1517. the head of block C and assert that we really do fall through. */
  1518. for (q = NEXT_INSN (BB_END (b)); q != BB_HEAD (c); q = NEXT_INSN (q))
  1519. if (INSN_P (q))
  1520. return;
  1521. /* Remove what will soon cease being the jump insn from the source block.
  1522. If block B consisted only of this single jump, turn it into a deleted
  1523. note. */
  1524. q = BB_END (b);
  1525. if (JUMP_P (q)
  1526. && onlyjump_p (q)
  1527. && (any_uncondjump_p (q)
  1528. || single_succ_p (b)))
  1529. {
  1530. rtx label;
  1531. rtx_jump_table_data *table;
  1532. if (tablejump_p (q, &label, &table))
  1533. {
  1534. /* The label is likely mentioned in some instruction before
  1535. the tablejump and might not be DCEd, so turn it into
  1536. a note instead and move before the tablejump that is going to
  1537. be deleted. */
  1538. const char *name = LABEL_NAME (label);
  1539. PUT_CODE (label, NOTE);
  1540. NOTE_KIND (label) = NOTE_INSN_DELETED_LABEL;
  1541. NOTE_DELETED_LABEL_NAME (label) = name;
  1542. rtx_insn *lab = safe_as_a <rtx_insn *> (label);
  1543. reorder_insns (lab, lab, PREV_INSN (q));
  1544. delete_insn (table);
  1545. }
  1546. #ifdef HAVE_cc0
  1547. /* If this was a conditional jump, we need to also delete
  1548. the insn that set cc0. */
  1549. if (any_condjump_p (q) && only_sets_cc0_p (PREV_INSN (q)))
  1550. q = PREV_INSN (q);
  1551. #endif
  1552. q = PREV_INSN (q);
  1553. }
  1554. /* Selectively unlink the sequence. */
  1555. if (q != PREV_INSN (BB_HEAD (c)))
  1556. delete_insn_chain (NEXT_INSN (q), PREV_INSN (BB_HEAD (c)), false);
  1557. e->flags |= EDGE_FALLTHRU;
  1558. }
  1559. /* Should move basic block BB after basic block AFTER. NIY. */
  1560. static bool
  1561. rtl_move_block_after (basic_block bb ATTRIBUTE_UNUSED,
  1562. basic_block after ATTRIBUTE_UNUSED)
  1563. {
  1564. return false;
  1565. }
  1566. /* Locate the last bb in the same partition as START_BB. */
  1567. static basic_block
  1568. last_bb_in_partition (basic_block start_bb)
  1569. {
  1570. basic_block bb;
  1571. FOR_BB_BETWEEN (bb, start_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
  1572. {
  1573. if (BB_PARTITION (start_bb) != BB_PARTITION (bb->next_bb))
  1574. return bb;
  1575. }
  1576. /* Return bb before the exit block. */
  1577. return bb->prev_bb;
  1578. }
  1579. /* Split a (typically critical) edge. Return the new block.
  1580. The edge must not be abnormal.
  1581. ??? The code generally expects to be called on critical edges.
  1582. The case of a block ending in an unconditional jump to a
  1583. block with multiple predecessors is not handled optimally. */
  1584. static basic_block
  1585. rtl_split_edge (edge edge_in)
  1586. {
  1587. basic_block bb, new_bb;
  1588. rtx_insn *before;
  1589. /* Abnormal edges cannot be split. */
  1590. gcc_assert (!(edge_in->flags & EDGE_ABNORMAL));
  1591. /* We are going to place the new block in front of edge destination.
  1592. Avoid existence of fallthru predecessors. */
  1593. if ((edge_in->flags & EDGE_FALLTHRU) == 0)
  1594. {
  1595. edge e = find_fallthru_edge (edge_in->dest->preds);
  1596. if (e)
  1597. force_nonfallthru (e);
  1598. }
  1599. /* Create the basic block note. */
  1600. if (edge_in->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
  1601. before = BB_HEAD (edge_in->dest);
  1602. else
  1603. before = NULL;
  1604. /* If this is a fall through edge to the exit block, the blocks might be
  1605. not adjacent, and the right place is after the source. */
  1606. if ((edge_in->flags & EDGE_FALLTHRU)
  1607. && edge_in->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
  1608. {
  1609. before = NEXT_INSN (BB_END (edge_in->src));
  1610. bb = create_basic_block (before, NULL, edge_in->src);
  1611. BB_COPY_PARTITION (bb, edge_in->src);
  1612. }
  1613. else
  1614. {
  1615. if (edge_in->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
  1616. {
  1617. bb = create_basic_block (before, NULL, edge_in->dest->prev_bb);
  1618. BB_COPY_PARTITION (bb, edge_in->dest);
  1619. }
  1620. else
  1621. {
  1622. basic_block after = edge_in->dest->prev_bb;
  1623. /* If this is post-bb reordering, and the edge crosses a partition
  1624. boundary, the new block needs to be inserted in the bb chain
  1625. at the end of the src partition (since we put the new bb into
  1626. that partition, see below). Otherwise we may end up creating
  1627. an extra partition crossing in the chain, which is illegal.
  1628. It can't go after the src, because src may have a fall-through
  1629. to a different block. */
  1630. if (crtl->bb_reorder_complete
  1631. && (edge_in->flags & EDGE_CROSSING))
  1632. {
  1633. after = last_bb_in_partition (edge_in->src);
  1634. before = get_last_bb_insn (after);
  1635. /* The instruction following the last bb in partition should
  1636. be a barrier, since it cannot end in a fall-through. */
  1637. gcc_checking_assert (BARRIER_P (before));
  1638. before = NEXT_INSN (before);
  1639. }
  1640. bb = create_basic_block (before, NULL, after);
  1641. /* Put the split bb into the src partition, to avoid creating
  1642. a situation where a cold bb dominates a hot bb, in the case
  1643. where src is cold and dest is hot. The src will dominate
  1644. the new bb (whereas it might not have dominated dest). */
  1645. BB_COPY_PARTITION (bb, edge_in->src);
  1646. }
  1647. }
  1648. make_single_succ_edge (bb, edge_in->dest, EDGE_FALLTHRU);
  1649. /* Can't allow a region crossing edge to be fallthrough. */
  1650. if (BB_PARTITION (bb) != BB_PARTITION (edge_in->dest)
  1651. && edge_in->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
  1652. {
  1653. new_bb = force_nonfallthru (single_succ_edge (bb));
  1654. gcc_assert (!new_bb);
  1655. }
  1656. /* For non-fallthru edges, we must adjust the predecessor's
  1657. jump instruction to target our new block. */
  1658. if ((edge_in->flags & EDGE_FALLTHRU) == 0)
  1659. {
  1660. edge redirected = redirect_edge_and_branch (edge_in, bb);
  1661. gcc_assert (redirected);
  1662. }
  1663. else
  1664. {
  1665. if (edge_in->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
  1666. {
  1667. /* For asm goto even splitting of fallthru edge might
  1668. need insn patching, as other labels might point to the
  1669. old label. */
  1670. rtx_insn *last = BB_END (edge_in->src);
  1671. if (last
  1672. && JUMP_P (last)
  1673. && edge_in->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
  1674. && extract_asm_operands (PATTERN (last)) != NULL_RTX
  1675. && patch_jump_insn (last, before, bb))
  1676. df_set_bb_dirty (edge_in->src);
  1677. }
  1678. redirect_edge_succ (edge_in, bb);
  1679. }
  1680. return bb;
  1681. }
  1682. /* Queue instructions for insertion on an edge between two basic blocks.
  1683. The new instructions and basic blocks (if any) will not appear in the
  1684. CFG until commit_edge_insertions is called. */
  1685. void
  1686. insert_insn_on_edge (rtx pattern, edge e)
  1687. {
  1688. /* We cannot insert instructions on an abnormal critical edge.
  1689. It will be easier to find the culprit if we die now. */
  1690. gcc_assert (!((e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e)));
  1691. if (e->insns.r == NULL_RTX)
  1692. start_sequence ();
  1693. else
  1694. push_to_sequence (e->insns.r);
  1695. emit_insn (pattern);
  1696. e->insns.r = get_insns ();
  1697. end_sequence ();
  1698. }
  1699. /* Update the CFG for the instructions queued on edge E. */
  1700. void
  1701. commit_one_edge_insertion (edge e)
  1702. {
  1703. rtx_insn *before = NULL, *after = NULL, *insns, *tmp, *last;
  1704. basic_block bb;
  1705. /* Pull the insns off the edge now since the edge might go away. */
  1706. insns = e->insns.r;
  1707. e->insns.r = NULL;
  1708. /* Figure out where to put these insns. If the destination has
  1709. one predecessor, insert there. Except for the exit block. */
  1710. if (single_pred_p (e->dest) && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
  1711. {
  1712. bb = e->dest;
  1713. /* Get the location correct wrt a code label, and "nice" wrt
  1714. a basic block note, and before everything else. */
  1715. tmp = BB_HEAD (bb);
  1716. if (LABEL_P (tmp))
  1717. tmp = NEXT_INSN (tmp);
  1718. if (NOTE_INSN_BASIC_BLOCK_P (tmp))
  1719. tmp = NEXT_INSN (tmp);
  1720. if (tmp == BB_HEAD (bb))
  1721. before = tmp;
  1722. else if (tmp)
  1723. after = PREV_INSN (tmp);
  1724. else
  1725. after = get_last_insn ();
  1726. }
  1727. /* If the source has one successor and the edge is not abnormal,
  1728. insert there. Except for the entry block.
  1729. Don't do this if the predecessor ends in a jump other than
  1730. unconditional simple jump. E.g. for asm goto that points all
  1731. its labels at the fallthru basic block, we can't insert instructions
  1732. before the asm goto, as the asm goto can have various of side effects,
  1733. and can't emit instructions after the asm goto, as it must end
  1734. the basic block. */
  1735. else if ((e->flags & EDGE_ABNORMAL) == 0
  1736. && single_succ_p (e->src)
  1737. && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
  1738. && (!JUMP_P (BB_END (e->src))
  1739. || simplejump_p (BB_END (e->src))))
  1740. {
  1741. bb = e->src;
  1742. /* It is possible to have a non-simple jump here. Consider a target
  1743. where some forms of unconditional jumps clobber a register. This
  1744. happens on the fr30 for example.
  1745. We know this block has a single successor, so we can just emit
  1746. the queued insns before the jump. */
  1747. if (JUMP_P (BB_END (bb)))
  1748. before = BB_END (bb);
  1749. else
  1750. {
  1751. /* We'd better be fallthru, or we've lost track of what's what. */
  1752. gcc_assert (e->flags & EDGE_FALLTHRU);
  1753. after = BB_END (bb);
  1754. }
  1755. }
  1756. /* Otherwise we must split the edge. */
  1757. else
  1758. {
  1759. bb = split_edge (e);
  1760. /* If E crossed a partition boundary, we needed to make bb end in
  1761. a region-crossing jump, even though it was originally fallthru. */
  1762. if (JUMP_P (BB_END (bb)))
  1763. before = BB_END (bb);
  1764. else
  1765. after = BB_END (bb);
  1766. }
  1767. /* Now that we've found the spot, do the insertion. */
  1768. if (before)
  1769. {
  1770. emit_insn_before_noloc (insns, before, bb);
  1771. last = prev_nonnote_insn (before);
  1772. }
  1773. else
  1774. last = emit_insn_after_noloc (insns, after, bb);
  1775. if (returnjump_p (last))
  1776. {
  1777. /* ??? Remove all outgoing edges from BB and add one for EXIT.
  1778. This is not currently a problem because this only happens
  1779. for the (single) epilogue, which already has a fallthru edge
  1780. to EXIT. */
  1781. e = single_succ_edge (bb);
  1782. gcc_assert (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)
  1783. && single_succ_p (bb) && (e->flags & EDGE_FALLTHRU));
  1784. e->flags &= ~EDGE_FALLTHRU;
  1785. emit_barrier_after (last);
  1786. if (before)
  1787. delete_insn (before);
  1788. }
  1789. else
  1790. gcc_assert (!JUMP_P (last));
  1791. }
  1792. /* Update the CFG for all queued instructions. */
  1793. void
  1794. commit_edge_insertions (void)
  1795. {
  1796. basic_block bb;
  1797. /* Optimization passes that invoke this routine can cause hot blocks
  1798. previously reached by both hot and cold blocks to become dominated only
  1799. by cold blocks. This will cause the verification below to fail,
  1800. and lead to now cold code in the hot section. In some cases this
  1801. may only be visible after newly unreachable blocks are deleted,
  1802. which will be done by fixup_partitions. */
  1803. fixup_partitions ();
  1804. #ifdef ENABLE_CHECKING
  1805. verify_flow_info ();
  1806. #endif
  1807. FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
  1808. EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
  1809. {
  1810. edge e;
  1811. edge_iterator ei;
  1812. FOR_EACH_EDGE (e, ei, bb->succs)
  1813. if (e->insns.r)
  1814. commit_one_edge_insertion (e);
  1815. }
  1816. }
  1817. /* Print out RTL-specific basic block information (live information
  1818. at start and end with TDF_DETAILS). FLAGS are the TDF_* masks
  1819. documented in dumpfile.h. */
  1820. static void
  1821. rtl_dump_bb (FILE *outf, basic_block bb, int indent, int flags)
  1822. {
  1823. rtx_insn *insn;
  1824. rtx_insn *last;
  1825. char *s_indent;
  1826. s_indent = (char *) alloca ((size_t) indent + 1);
  1827. memset (s_indent, ' ', (size_t) indent);
  1828. s_indent[indent] = '\0';
  1829. if (df && (flags & TDF_DETAILS))
  1830. {
  1831. df_dump_top (bb, outf);
  1832. putc ('\n', outf);
  1833. }
  1834. if (bb->index != ENTRY_BLOCK && bb->index != EXIT_BLOCK)
  1835. for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb)); insn != last;
  1836. insn = NEXT_INSN (insn))
  1837. {
  1838. if (flags & TDF_DETAILS)
  1839. df_dump_insn_top (insn, outf);
  1840. if (! (flags & TDF_SLIM))
  1841. print_rtl_single (outf, insn);
  1842. else
  1843. dump_insn_slim (outf, insn);
  1844. if (flags & TDF_DETAILS)
  1845. df_dump_insn_bottom (insn, outf);
  1846. }
  1847. if (df && (flags & TDF_DETAILS))
  1848. {
  1849. df_dump_bottom (bb, outf);
  1850. putc ('\n', outf);
  1851. }
  1852. }
  1853. /* Like dump_function_to_file, but for RTL. Print out dataflow information
  1854. for the start of each basic block. FLAGS are the TDF_* masks documented
  1855. in dumpfile.h. */
  1856. void
  1857. print_rtl_with_bb (FILE *outf, const rtx_insn *rtx_first, int flags)
  1858. {
  1859. const rtx_insn *tmp_rtx;
  1860. if (rtx_first == 0)
  1861. fprintf (outf, "(nil)\n");
  1862. else
  1863. {
  1864. enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
  1865. int max_uid = get_max_uid ();
  1866. basic_block *start = XCNEWVEC (basic_block, max_uid);
  1867. basic_block *end = XCNEWVEC (basic_block, max_uid);
  1868. enum bb_state *in_bb_p = XCNEWVEC (enum bb_state, max_uid);
  1869. basic_block bb;
  1870. /* After freeing the CFG, we still have BLOCK_FOR_INSN set on most
  1871. insns, but the CFG is not maintained so the basic block info
  1872. is not reliable. Therefore it's omitted from the dumps. */
  1873. if (! (cfun->curr_properties & PROP_cfg))
  1874. flags &= ~TDF_BLOCKS;
  1875. if (df)
  1876. df_dump_start (outf);
  1877. if (flags & TDF_BLOCKS)
  1878. {
  1879. FOR_EACH_BB_REVERSE_FN (bb, cfun)
  1880. {
  1881. rtx_insn *x;
  1882. start[INSN_UID (BB_HEAD (bb))] = bb;
  1883. end[INSN_UID (BB_END (bb))] = bb;
  1884. for (x = BB_HEAD (bb); x != NULL_RTX; x = NEXT_INSN (x))
  1885. {
  1886. enum bb_state state = IN_MULTIPLE_BB;
  1887. if (in_bb_p[INSN_UID (x)] == NOT_IN_BB)
  1888. state = IN_ONE_BB;
  1889. in_bb_p[INSN_UID (x)] = state;
  1890. if (x == BB_END (bb))
  1891. break;
  1892. }
  1893. }
  1894. }
  1895. for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
  1896. {
  1897. if (flags & TDF_BLOCKS)
  1898. {
  1899. bb = start[INSN_UID (tmp_rtx)];
  1900. if (bb != NULL)
  1901. {
  1902. dump_bb_info (outf, bb, 0, dump_flags | TDF_COMMENT, true, false);
  1903. if (df && (flags & TDF_DETAILS))
  1904. df_dump_top (bb, outf);
  1905. }
  1906. if (in_bb_p[INSN_UID (tmp_rtx)] == NOT_IN_BB
  1907. && !NOTE_P (tmp_rtx)
  1908. && !BARRIER_P (tmp_rtx))
  1909. fprintf (outf, ";; Insn is not within a basic block\n");
  1910. else if (in_bb_p[INSN_UID (tmp_rtx)] == IN_MULTIPLE_BB)
  1911. fprintf (outf, ";; Insn is in multiple basic blocks\n");
  1912. }
  1913. if (flags & TDF_DETAILS)
  1914. df_dump_insn_top (tmp_rtx, outf);
  1915. if (! (flags & TDF_SLIM))
  1916. print_rtl_single (outf, tmp_rtx);
  1917. else
  1918. dump_insn_slim (outf, tmp_rtx);
  1919. if (flags & TDF_DETAILS)
  1920. df_dump_insn_bottom (tmp_rtx, outf);
  1921. if (flags & TDF_BLOCKS)
  1922. {
  1923. bb = end[INSN_UID (tmp_rtx)];
  1924. if (bb != NULL)
  1925. {
  1926. dump_bb_info (outf, bb, 0, dump_flags | TDF_COMMENT, false, true);
  1927. if (df && (flags & TDF_DETAILS))
  1928. df_dump_bottom (bb, outf);
  1929. putc ('\n', outf);
  1930. }
  1931. }
  1932. }
  1933. free (start);
  1934. free (end);
  1935. free (in_bb_p);
  1936. }
  1937. }
  1938. /* Update the branch probability of BB if a REG_BR_PROB is present. */
  1939. void
  1940. update_br_prob_note (basic_block bb)
  1941. {
  1942. rtx note;
  1943. if (!JUMP_P (BB_END (bb)))
  1944. return;
  1945. note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX);
  1946. if (!note || XINT (note, 0) == BRANCH_EDGE (bb)->probability)
  1947. return;
  1948. XINT (note, 0) = BRANCH_EDGE (bb)->probability;
  1949. }
  1950. /* Get the last insn associated with block BB (that includes barriers and
  1951. tablejumps after BB). */
  1952. rtx_insn *
  1953. get_last_bb_insn (basic_block bb)
  1954. {
  1955. rtx_jump_table_data *table;
  1956. rtx_insn *tmp;
  1957. rtx_insn *end = BB_END (bb);
  1958. /* Include any jump table following the basic block. */
  1959. if (tablejump_p (end, NULL, &table))
  1960. end = table;
  1961. /* Include any barriers that may follow the basic block. */
  1962. tmp = next_nonnote_insn_bb (end);
  1963. while (tmp && BARRIER_P (tmp))
  1964. {
  1965. end = tmp;
  1966. tmp = next_nonnote_insn_bb (end);
  1967. }
  1968. return end;
  1969. }
  1970. /* Sanity check partition hotness to ensure that basic blocks in
  1971.   the cold partition don't dominate basic blocks in the hot partition.
  1972. If FLAG_ONLY is true, report violations as errors. Otherwise
  1973. re-mark the dominated blocks as cold, since this is run after
  1974. cfg optimizations that may make hot blocks previously reached
  1975. by both hot and cold blocks now only reachable along cold paths. */
  1976. static vec<basic_block>
  1977. find_partition_fixes (bool flag_only)
  1978. {
  1979. basic_block bb;
  1980. vec<basic_block> bbs_in_cold_partition = vNULL;
  1981. vec<basic_block> bbs_to_fix = vNULL;
  1982. /* Callers check this. */
  1983. gcc_checking_assert (crtl->has_bb_partition);
  1984. FOR_EACH_BB_FN (bb, cfun)
  1985. if ((BB_PARTITION (bb) == BB_COLD_PARTITION))
  1986. bbs_in_cold_partition.safe_push (bb);
  1987. if (bbs_in_cold_partition.is_empty ())
  1988. return vNULL;
  1989. bool dom_calculated_here = !dom_info_available_p (CDI_DOMINATORS);
  1990. if (dom_calculated_here)
  1991. calculate_dominance_info (CDI_DOMINATORS);
  1992. while (! bbs_in_cold_partition.is_empty ())
  1993. {
  1994. bb = bbs_in_cold_partition.pop ();
  1995. /* Any blocks dominated by a block in the cold section
  1996. must also be cold. */
  1997. basic_block son;
  1998. for (son = first_dom_son (CDI_DOMINATORS, bb);
  1999. son;
  2000. son = next_dom_son (CDI_DOMINATORS, son))
  2001. {
  2002. /* If son is not yet cold, then mark it cold here and
  2003. enqueue it for further processing. */
  2004. if ((BB_PARTITION (son) != BB_COLD_PARTITION))
  2005. {
  2006. if (flag_only)
  2007. error ("non-cold basic block %d dominated "
  2008. "by a block in the cold partition (%d)", son->index, bb->index);
  2009. else
  2010. BB_SET_PARTITION (son, BB_COLD_PARTITION);
  2011. bbs_to_fix.safe_push (son);
  2012. bbs_in_cold_partition.safe_push (son);
  2013. }
  2014. }
  2015. }
  2016. if (dom_calculated_here)
  2017. free_dominance_info (CDI_DOMINATORS);
  2018. return bbs_to_fix;
  2019. }
  2020. /* Perform cleanup on the hot/cold bb partitioning after optimization
  2021. passes that modify the cfg. */
  2022. void
  2023. fixup_partitions (void)
  2024. {
  2025. basic_block bb;
  2026. if (!crtl->has_bb_partition)
  2027. return;
  2028. /* Delete any blocks that became unreachable and weren't
  2029. already cleaned up, for example during edge forwarding
  2030. and convert_jumps_to_returns. This will expose more
  2031. opportunities for fixing the partition boundaries here.
  2032. Also, the calculation of the dominance graph during verification
  2033. will assert if there are unreachable nodes. */
  2034. delete_unreachable_blocks ();
  2035. /* If there are partitions, do a sanity check on them: A basic block in
  2036.   a cold partition cannot dominate a basic block in a hot partition.
  2037. Fixup any that now violate this requirement, as a result of edge
  2038. forwarding and unreachable block deletion.  */
  2039. vec<basic_block> bbs_to_fix = find_partition_fixes (false);
  2040. /* Do the partition fixup after all necessary blocks have been converted to
  2041. cold, so that we only update the region crossings the minimum number of
  2042. places, which can require forcing edges to be non fallthru. */
  2043. while (! bbs_to_fix.is_empty ())
  2044. {
  2045. bb = bbs_to_fix.pop ();
  2046. fixup_new_cold_bb (bb);
  2047. }
  2048. }
  2049. /* Verify, in the basic block chain, that there is at most one switch
  2050. between hot/cold partitions. This condition will not be true until
  2051. after reorder_basic_blocks is called. */
  2052. static int
  2053. verify_hot_cold_block_grouping (void)
  2054. {
  2055. basic_block bb;
  2056. int err = 0;
  2057. bool switched_sections = false;
  2058. int current_partition = BB_UNPARTITIONED;
  2059. /* Even after bb reordering is complete, we go into cfglayout mode
  2060. again (in compgoto). Ensure we don't call this before going back
  2061. into linearized RTL when any layout fixes would have been committed. */
  2062. if (!crtl->bb_reorder_complete
  2063. || current_ir_type () != IR_RTL_CFGRTL)
  2064. return err;
  2065. FOR_EACH_BB_FN (bb, cfun)
  2066. {
  2067. if (current_partition != BB_UNPARTITIONED
  2068. && BB_PARTITION (bb) != current_partition)
  2069. {
  2070. if (switched_sections)
  2071. {
  2072. error ("multiple hot/cold transitions found (bb %i)",
  2073. bb->index);
  2074. err = 1;
  2075. }
  2076. else
  2077. switched_sections = true;
  2078. if (!crtl->has_bb_partition)
  2079. error ("partition found but function partition flag not set");
  2080. }
  2081. current_partition = BB_PARTITION (bb);
  2082. }
  2083. return err;
  2084. }
  2085. /* Perform several checks on the edges out of each block, such as
  2086. the consistency of the branch probabilities, the correctness
  2087. of hot/cold partition crossing edges, and the number of expected
  2088. successor edges. Also verify that the dominance relationship
  2089. between hot/cold blocks is sane. */
  2090. static int
  2091. rtl_verify_edges (void)
  2092. {
  2093. int err = 0;
  2094. basic_block bb;
  2095. FOR_EACH_BB_REVERSE_FN (bb, cfun)
  2096. {
  2097. int n_fallthru = 0, n_branch = 0, n_abnormal_call = 0, n_sibcall = 0;
  2098. int n_eh = 0, n_abnormal = 0;
  2099. edge e, fallthru = NULL;
  2100. edge_iterator ei;
  2101. rtx note;
  2102. bool has_crossing_edge = false;
  2103. if (JUMP_P (BB_END (bb))
  2104. && (note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX))
  2105. && EDGE_COUNT (bb->succs) >= 2
  2106. && any_condjump_p (BB_END (bb)))
  2107. {
  2108. if (XINT (note, 0) != BRANCH_EDGE (bb)->probability
  2109. && profile_status_for_fn (cfun) != PROFILE_ABSENT)
  2110. {
  2111. error ("verify_flow_info: REG_BR_PROB does not match cfg %i %i",
  2112. XINT (note, 0), BRANCH_EDGE (bb)->probability);
  2113. err = 1;
  2114. }
  2115. }
  2116. FOR_EACH_EDGE (e, ei, bb->succs)
  2117. {
  2118. bool is_crossing;
  2119. if (e->flags & EDGE_FALLTHRU)
  2120. n_fallthru++, fallthru = e;
  2121. is_crossing = (BB_PARTITION (e->src) != BB_PARTITION (e->dest)
  2122. && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
  2123. && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun));
  2124. has_crossing_edge |= is_crossing;
  2125. if (e->flags & EDGE_CROSSING)
  2126. {
  2127. if (!is_crossing)
  2128. {
  2129. error ("EDGE_CROSSING incorrectly set across same section");
  2130. err = 1;
  2131. }
  2132. if (e->flags & EDGE_FALLTHRU)
  2133. {
  2134. error ("fallthru edge crosses section boundary in bb %i",
  2135. e->src->index);
  2136. err = 1;
  2137. }
  2138. if (e->flags & EDGE_EH)
  2139. {
  2140. error ("EH edge crosses section boundary in bb %i",
  2141. e->src->index);
  2142. err = 1;
  2143. }
  2144. if (JUMP_P (BB_END (bb)) && !CROSSING_JUMP_P (BB_END (bb)))
  2145. {
  2146. error ("No region crossing jump at section boundary in bb %i",
  2147. bb->index);
  2148. err = 1;
  2149. }
  2150. }
  2151. else if (is_crossing)
  2152. {
  2153. error ("EDGE_CROSSING missing across section boundary");
  2154. err = 1;
  2155. }
  2156. if ((e->flags & ~(EDGE_DFS_BACK
  2157. | EDGE_CAN_FALLTHRU
  2158. | EDGE_IRREDUCIBLE_LOOP
  2159. | EDGE_LOOP_EXIT
  2160. | EDGE_CROSSING
  2161. | EDGE_PRESERVE)) == 0)
  2162. n_branch++;
  2163. if (e->flags & EDGE_ABNORMAL_CALL)
  2164. n_abnormal_call++;
  2165. if (e->flags & EDGE_SIBCALL)
  2166. n_sibcall++;
  2167. if (e->flags & EDGE_EH)
  2168. n_eh++;
  2169. if (e->flags & EDGE_ABNORMAL)
  2170. n_abnormal++;
  2171. }
  2172. if (!has_crossing_edge
  2173. && JUMP_P (BB_END (bb))
  2174. && CROSSING_JUMP_P (BB_END (bb)))
  2175. {
  2176. print_rtl_with_bb (stderr, get_insns (), TDF_RTL | TDF_BLOCKS | TDF_DETAILS);
  2177. error ("Region crossing jump across same section in bb %i",
  2178. bb->index);
  2179. err = 1;
  2180. }
  2181. if (n_eh && !find_reg_note (BB_END (bb), REG_EH_REGION, NULL_RTX))
  2182. {
  2183. error ("missing REG_EH_REGION note at the end of bb %i", bb->index);
  2184. err = 1;
  2185. }
  2186. if (n_eh > 1)
  2187. {
  2188. error ("too many exception handling edges in bb %i", bb->index);
  2189. err = 1;
  2190. }
  2191. if (n_branch
  2192. && (!JUMP_P (BB_END (bb))
  2193. || (n_branch > 1 && (any_uncondjump_p (BB_END (bb))
  2194. || any_condjump_p (BB_END (bb))))))
  2195. {
  2196. error ("too many outgoing branch edges from bb %i", bb->index);
  2197. err = 1;
  2198. }
  2199. if (n_fallthru && any_uncondjump_p (BB_END (bb)))
  2200. {
  2201. error ("fallthru edge after unconditional jump in bb %i", bb->index);
  2202. err = 1;
  2203. }
  2204. if (n_branch != 1 && any_uncondjump_p (BB_END (bb)))
  2205. {
  2206. error ("wrong number of branch edges after unconditional jump"
  2207. " in bb %i", bb->index);
  2208. err = 1;
  2209. }
  2210. if (n_branch != 1 && any_condjump_p (BB_END (bb))
  2211. && JUMP_LABEL (BB_END (bb)) != BB_HEAD (fallthru->dest))
  2212. {
  2213. error ("wrong amount of branch edges after conditional jump"
  2214. " in bb %i", bb->index);
  2215. err = 1;
  2216. }
  2217. if (n_abnormal_call && !CALL_P (BB_END (bb)))
  2218. {
  2219. error ("abnormal call edges for non-call insn in bb %i", bb->index);
  2220. err = 1;
  2221. }
  2222. if (n_sibcall && !CALL_P (BB_END (bb)))
  2223. {
  2224. error ("sibcall edges for non-call insn in bb %i", bb->index);
  2225. err = 1;
  2226. }
  2227. if (n_abnormal > n_eh
  2228. && !(CALL_P (BB_END (bb))
  2229. && n_abnormal == n_abnormal_call + n_sibcall)
  2230. && (!JUMP_P (BB_END (bb))
  2231. || any_condjump_p (BB_END (bb))
  2232. || any_uncondjump_p (BB_END (bb))))
  2233. {
  2234. error ("abnormal edges for no purpose in bb %i", bb->index);
  2235. err = 1;
  2236. }
  2237. }
  2238. /* If there are partitions, do a sanity check on them: A basic block in
  2239.   a cold partition cannot dominate a basic block in a hot partition.  */
  2240. if (crtl->has_bb_partition && !err)
  2241. {
  2242. vec<basic_block> bbs_to_fix = find_partition_fixes (true);
  2243. err = !bbs_to_fix.is_empty ();
  2244. }
  2245. /* Clean up. */
  2246. return err;
  2247. }
  2248. /* Checks on the instructions within blocks. Currently checks that each
  2249. block starts with a basic block note, and that basic block notes and
  2250. control flow jumps are not found in the middle of the block. */
  2251. static int
  2252. rtl_verify_bb_insns (void)
  2253. {
  2254. rtx_insn *x;
  2255. int err = 0;
  2256. basic_block bb;
  2257. FOR_EACH_BB_REVERSE_FN (bb, cfun)
  2258. {
  2259. /* Now check the header of basic
  2260. block. It ought to contain optional CODE_LABEL followed
  2261. by NOTE_BASIC_BLOCK. */
  2262. x = BB_HEAD (bb);
  2263. if (LABEL_P (x))
  2264. {
  2265. if (BB_END (bb) == x)
  2266. {
  2267. error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
  2268. bb->index);
  2269. err = 1;
  2270. }
  2271. x = NEXT_INSN (x);
  2272. }
  2273. if (!NOTE_INSN_BASIC_BLOCK_P (x) || NOTE_BASIC_BLOCK (x) != bb)
  2274. {
  2275. error ("NOTE_INSN_BASIC_BLOCK is missing for block %d",
  2276. bb->index);
  2277. err = 1;
  2278. }
  2279. if (BB_END (bb) == x)
  2280. /* Do checks for empty blocks here. */
  2281. ;
  2282. else
  2283. for (x = NEXT_INSN (x); x; x = NEXT_INSN (x))
  2284. {
  2285. if (NOTE_INSN_BASIC_BLOCK_P (x))
  2286. {
  2287. error ("NOTE_INSN_BASIC_BLOCK %d in middle of basic block %d",
  2288. INSN_UID (x), bb->index);
  2289. err = 1;
  2290. }
  2291. if (x == BB_END (bb))
  2292. break;
  2293. if (control_flow_insn_p (x))
  2294. {
  2295. error ("in basic block %d:", bb->index);
  2296. fatal_insn ("flow control insn inside a basic block", x);
  2297. }
  2298. }
  2299. }
  2300. /* Clean up. */
  2301. return err;
  2302. }
  2303. /* Verify that block pointers for instructions in basic blocks, headers and
  2304. footers are set appropriately. */
  2305. static int
  2306. rtl_verify_bb_pointers (void)
  2307. {
  2308. int err = 0;
  2309. basic_block bb;
  2310. /* Check the general integrity of the basic blocks. */
  2311. FOR_EACH_BB_REVERSE_FN (bb, cfun)
  2312. {
  2313. rtx_insn *insn;
  2314. if (!(bb->flags & BB_RTL))
  2315. {
  2316. error ("BB_RTL flag not set for block %d", bb->index);
  2317. err = 1;
  2318. }
  2319. FOR_BB_INSNS (bb, insn)
  2320. if (BLOCK_FOR_INSN (insn) != bb)
  2321. {
  2322. error ("insn %d basic block pointer is %d, should be %d",
  2323. INSN_UID (insn),
  2324. BLOCK_FOR_INSN (insn) ? BLOCK_FOR_INSN (insn)->index : 0,
  2325. bb->index);
  2326. err = 1;
  2327. }
  2328. for (insn = BB_HEADER (bb); insn; insn = NEXT_INSN (insn))
  2329. if (!BARRIER_P (insn)
  2330. && BLOCK_FOR_INSN (insn) != NULL)
  2331. {
  2332. error ("insn %d in header of bb %d has non-NULL basic block",
  2333. INSN_UID (insn), bb->index);
  2334. err = 1;
  2335. }
  2336. for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
  2337. if (!BARRIER_P (insn)
  2338. && BLOCK_FOR_INSN (insn) != NULL)
  2339. {
  2340. error ("insn %d in footer of bb %d has non-NULL basic block",
  2341. INSN_UID (insn), bb->index);
  2342. err = 1;
  2343. }
  2344. }
  2345. /* Clean up. */
  2346. return err;
  2347. }
  2348. /* Verify the CFG and RTL consistency common for both underlying RTL and
  2349. cfglayout RTL.
  2350. Currently it does following checks:
  2351. - overlapping of basic blocks
  2352. - insns with wrong BLOCK_FOR_INSN pointers
  2353. - headers of basic blocks (the NOTE_INSN_BASIC_BLOCK note)
  2354. - tails of basic blocks (ensure that boundary is necessary)
  2355. - scans body of the basic block for JUMP_INSN, CODE_LABEL
  2356. and NOTE_INSN_BASIC_BLOCK
  2357. - verify that no fall_thru edge crosses hot/cold partition boundaries
  2358. - verify that there are no pending RTL branch predictions
  2359. - verify that hot blocks are not dominated by cold blocks
  2360. In future it can be extended check a lot of other stuff as well
  2361. (reachability of basic blocks, life information, etc. etc.). */
  2362. static int
  2363. rtl_verify_flow_info_1 (void)
  2364. {
  2365. int err = 0;
  2366. err |= rtl_verify_bb_pointers ();
  2367. err |= rtl_verify_bb_insns ();
  2368. err |= rtl_verify_edges ();
  2369. return err;
  2370. }
  2371. /* Walk the instruction chain and verify that bb head/end pointers
  2372. are correct, and that instructions are in exactly one bb and have
  2373. correct block pointers. */
  2374. static int
  2375. rtl_verify_bb_insn_chain (void)
  2376. {
  2377. basic_block bb;
  2378. int err = 0;
  2379. rtx_insn *x;
  2380. rtx_insn *last_head = get_last_insn ();
  2381. basic_block *bb_info;
  2382. const int max_uid = get_max_uid ();
  2383. bb_info = XCNEWVEC (basic_block, max_uid);
  2384. FOR_EACH_BB_REVERSE_FN (bb, cfun)
  2385. {
  2386. rtx_insn *head = BB_HEAD (bb);
  2387. rtx_insn *end = BB_END (bb);
  2388. for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
  2389. {
  2390. /* Verify the end of the basic block is in the INSN chain. */
  2391. if (x == end)
  2392. break;
  2393. /* And that the code outside of basic blocks has NULL bb field. */
  2394. if (!BARRIER_P (x)
  2395. && BLOCK_FOR_INSN (x) != NULL)
  2396. {
  2397. error ("insn %d outside of basic blocks has non-NULL bb field",
  2398. INSN_UID (x));
  2399. err = 1;
  2400. }
  2401. }
  2402. if (!x)
  2403. {
  2404. error ("end insn %d for block %d not found in the insn stream",
  2405. INSN_UID (end), bb->index);
  2406. err = 1;
  2407. }
  2408. /* Work backwards from the end to the head of the basic block
  2409. to verify the head is in the RTL chain. */
  2410. for (; x != NULL_RTX; x = PREV_INSN (x))
  2411. {
  2412. /* While walking over the insn chain, verify insns appear
  2413. in only one basic block. */
  2414. if (bb_info[INSN_UID (x)] != NULL)
  2415. {
  2416. error ("insn %d is in multiple basic blocks (%d and %d)",
  2417. INSN_UID (x), bb->index, bb_info[INSN_UID (x)]->index);
  2418. err = 1;
  2419. }
  2420. bb_info[INSN_UID (x)] = bb;
  2421. if (x == head)
  2422. break;
  2423. }
  2424. if (!x)
  2425. {
  2426. error ("head insn %d for block %d not found in the insn stream",
  2427. INSN_UID (head), bb->index);
  2428. err = 1;
  2429. }
  2430. last_head = PREV_INSN (x);
  2431. }
  2432. for (x = last_head; x != NULL_RTX; x = PREV_INSN (x))
  2433. {
  2434. /* Check that the code before the first basic block has NULL
  2435. bb field. */
  2436. if (!BARRIER_P (x)
  2437. && BLOCK_FOR_INSN (x) != NULL)
  2438. {
  2439. error ("insn %d outside of basic blocks has non-NULL bb field",
  2440. INSN_UID (x));
  2441. err = 1;
  2442. }
  2443. }
  2444. free (bb_info);
  2445. return err;
  2446. }
  2447. /* Verify that fallthru edges point to adjacent blocks in layout order and
  2448. that barriers exist after non-fallthru blocks. */
  2449. static int
  2450. rtl_verify_fallthru (void)
  2451. {
  2452. basic_block bb;
  2453. int err = 0;
  2454. FOR_EACH_BB_REVERSE_FN (bb, cfun)
  2455. {
  2456. edge e;
  2457. e = find_fallthru_edge (bb->succs);
  2458. if (!e)
  2459. {
  2460. rtx_insn *insn;
  2461. /* Ensure existence of barrier in BB with no fallthru edges. */
  2462. for (insn = NEXT_INSN (BB_END (bb)); ; insn = NEXT_INSN (insn))
  2463. {
  2464. if (!insn || NOTE_INSN_BASIC_BLOCK_P (insn))
  2465. {
  2466. error ("missing barrier after block %i", bb->index);
  2467. err = 1;
  2468. break;
  2469. }
  2470. if (BARRIER_P (insn))
  2471. break;
  2472. }
  2473. }
  2474. else if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
  2475. && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
  2476. {
  2477. rtx_insn *insn;
  2478. if (e->src->next_bb != e->dest)
  2479. {
  2480. error
  2481. ("verify_flow_info: Incorrect blocks for fallthru %i->%i",
  2482. e->src->index, e->dest->index);
  2483. err = 1;
  2484. }
  2485. else
  2486. for (insn = NEXT_INSN (BB_END (e->src)); insn != BB_HEAD (e->dest);
  2487. insn = NEXT_INSN (insn))
  2488. if (BARRIER_P (insn) || INSN_P (insn))
  2489. {
  2490. error ("verify_flow_info: Incorrect fallthru %i->%i",
  2491. e->src->index, e->dest->index);
  2492. fatal_insn ("wrong insn in the fallthru edge", insn);
  2493. err = 1;
  2494. }
  2495. }
  2496. }
  2497. return err;
  2498. }
  2499. /* Verify that blocks are laid out in consecutive order. While walking the
  2500. instructions, verify that all expected instructions are inside the basic
  2501. blocks, and that all returns are followed by barriers. */
  2502. static int
  2503. rtl_verify_bb_layout (void)
  2504. {
  2505. basic_block bb;
  2506. int err = 0;
  2507. rtx_insn *x;
  2508. int num_bb_notes;
  2509. rtx_insn * const rtx_first = get_insns ();
  2510. basic_block last_bb_seen = ENTRY_BLOCK_PTR_FOR_FN (cfun), curr_bb = NULL;
  2511. num_bb_notes = 0;
  2512. last_bb_seen = ENTRY_BLOCK_PTR_FOR_FN (cfun);
  2513. for (x = rtx_first; x; x = NEXT_INSN (x))
  2514. {
  2515. if (NOTE_INSN_BASIC_BLOCK_P (x))
  2516. {
  2517. bb = NOTE_BASIC_BLOCK (x);
  2518. num_bb_notes++;
  2519. if (bb != last_bb_seen->next_bb)
  2520. internal_error ("basic blocks not laid down consecutively");
  2521. curr_bb = last_bb_seen = bb;
  2522. }
  2523. if (!curr_bb)
  2524. {
  2525. switch (GET_CODE (x))
  2526. {
  2527. case BARRIER:
  2528. case NOTE:
  2529. break;
  2530. case CODE_LABEL:
  2531. /* An ADDR_VEC is placed outside any basic block. */
  2532. if (NEXT_INSN (x)
  2533. && JUMP_TABLE_DATA_P (NEXT_INSN (x)))
  2534. x = NEXT_INSN (x);
  2535. /* But in any case, non-deletable labels can appear anywhere. */
  2536. break;
  2537. default:
  2538. fatal_insn ("insn outside basic block", x);
  2539. }
  2540. }
  2541. if (JUMP_P (x)
  2542. && returnjump_p (x) && ! condjump_p (x)
  2543. && ! (next_nonnote_insn (x) && BARRIER_P (next_nonnote_insn (x))))
  2544. fatal_insn ("return not followed by barrier", x);
  2545. if (curr_bb && x == BB_END (curr_bb))
  2546. curr_bb = NULL;
  2547. }
  2548. if (num_bb_notes != n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS)
  2549. internal_error
  2550. ("number of bb notes in insn chain (%d) != n_basic_blocks (%d)",
  2551. num_bb_notes, n_basic_blocks_for_fn (cfun));
  2552. return err;
  2553. }
  2554. /* Verify the CFG and RTL consistency common for both underlying RTL and
  2555. cfglayout RTL, plus consistency checks specific to linearized RTL mode.
  2556. Currently it does following checks:
  2557. - all checks of rtl_verify_flow_info_1
  2558. - test head/end pointers
  2559. - check that blocks are laid out in consecutive order
  2560. - check that all insns are in the basic blocks
  2561. (except the switch handling code, barriers and notes)
  2562. - check that all returns are followed by barriers
  2563. - check that all fallthru edge points to the adjacent blocks
  2564. - verify that there is a single hot/cold partition boundary after bbro */
  2565. static int
  2566. rtl_verify_flow_info (void)
  2567. {
  2568. int err = 0;
  2569. err |= rtl_verify_flow_info_1 ();
  2570. err |= rtl_verify_bb_insn_chain ();
  2571. err |= rtl_verify_fallthru ();
  2572. err |= rtl_verify_bb_layout ();
  2573. err |= verify_hot_cold_block_grouping ();
  2574. return err;
  2575. }
  2576. /* Assume that the preceding pass has possibly eliminated jump instructions
  2577. or converted the unconditional jumps. Eliminate the edges from CFG.
  2578. Return true if any edges are eliminated. */
  2579. bool
  2580. purge_dead_edges (basic_block bb)
  2581. {
  2582. edge e;
  2583. rtx_insn *insn = BB_END (bb);
  2584. rtx note;
  2585. bool purged = false;
  2586. bool found;
  2587. edge_iterator ei;
  2588. if (DEBUG_INSN_P (insn) && insn != BB_HEAD (bb))
  2589. do
  2590. insn = PREV_INSN (insn);
  2591. while ((DEBUG_INSN_P (insn) || NOTE_P (insn)) && insn != BB_HEAD (bb));
  2592. /* If this instruction cannot trap, remove REG_EH_REGION notes. */
  2593. if (NONJUMP_INSN_P (insn)
  2594. && (note = find_reg_note (insn, REG_EH_REGION, NULL)))
  2595. {
  2596. rtx eqnote;
  2597. if (! may_trap_p (PATTERN (insn))
  2598. || ((eqnote = find_reg_equal_equiv_note (insn))
  2599. && ! may_trap_p (XEXP (eqnote, 0))))
  2600. remove_note (insn, note);
  2601. }
  2602. /* Cleanup abnormal edges caused by exceptions or non-local gotos. */
  2603. for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
  2604. {
  2605. bool remove = false;
  2606. /* There are three types of edges we need to handle correctly here: EH
  2607. edges, abnormal call EH edges, and abnormal call non-EH edges. The
  2608. latter can appear when nonlocal gotos are used. */
  2609. if (e->flags & EDGE_ABNORMAL_CALL)
  2610. {
  2611. if (!CALL_P (insn))
  2612. remove = true;
  2613. else if (can_nonlocal_goto (insn))
  2614. ;
  2615. else if ((e->flags & EDGE_EH) && can_throw_internal (insn))
  2616. ;
  2617. else if (flag_tm && find_reg_note (insn, REG_TM, NULL))
  2618. ;
  2619. else
  2620. remove = true;
  2621. }
  2622. else if (e->flags & EDGE_EH)
  2623. remove = !can_throw_internal (insn);
  2624. if (remove)
  2625. {
  2626. remove_edge (e);
  2627. df_set_bb_dirty (bb);
  2628. purged = true;
  2629. }
  2630. else
  2631. ei_next (&ei);
  2632. }
  2633. if (JUMP_P (insn))
  2634. {
  2635. rtx note;
  2636. edge b,f;
  2637. edge_iterator ei;
  2638. /* We do care only about conditional jumps and simplejumps. */
  2639. if (!any_condjump_p (insn)
  2640. && !returnjump_p (insn)
  2641. && !simplejump_p (insn))
  2642. return purged;
  2643. /* Branch probability/prediction notes are defined only for
  2644. condjumps. We've possibly turned condjump into simplejump. */
  2645. if (simplejump_p (insn))
  2646. {
  2647. note = find_reg_note (insn, REG_BR_PROB, NULL);
  2648. if (note)
  2649. remove_note (insn, note);
  2650. while ((note = find_reg_note (insn, REG_BR_PRED, NULL)))
  2651. remove_note (insn, note);
  2652. }
  2653. for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
  2654. {
  2655. /* Avoid abnormal flags to leak from computed jumps turned
  2656. into simplejumps. */
  2657. e->flags &= ~EDGE_ABNORMAL;
  2658. /* See if this edge is one we should keep. */
  2659. if ((e->flags & EDGE_FALLTHRU) && any_condjump_p (insn))
  2660. /* A conditional jump can fall through into the next
  2661. block, so we should keep the edge. */
  2662. {
  2663. ei_next (&ei);
  2664. continue;
  2665. }
  2666. else if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
  2667. && BB_HEAD (e->dest) == JUMP_LABEL (insn))
  2668. /* If the destination block is the target of the jump,
  2669. keep the edge. */
  2670. {
  2671. ei_next (&ei);
  2672. continue;
  2673. }
  2674. else if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)
  2675. && returnjump_p (insn))
  2676. /* If the destination block is the exit block, and this
  2677. instruction is a return, then keep the edge. */
  2678. {
  2679. ei_next (&ei);
  2680. continue;
  2681. }
  2682. else if ((e->flags & EDGE_EH) && can_throw_internal (insn))
  2683. /* Keep the edges that correspond to exceptions thrown by
  2684. this instruction and rematerialize the EDGE_ABNORMAL
  2685. flag we just cleared above. */
  2686. {
  2687. e->flags |= EDGE_ABNORMAL;
  2688. ei_next (&ei);
  2689. continue;
  2690. }
  2691. /* We do not need this edge. */
  2692. df_set_bb_dirty (bb);
  2693. purged = true;
  2694. remove_edge (e);
  2695. }
  2696. if (EDGE_COUNT (bb->succs) == 0 || !purged)
  2697. return purged;
  2698. if (dump_file)
  2699. fprintf (dump_file, "Purged edges from bb %i\n", bb->index);
  2700. if (!optimize)
  2701. return purged;
  2702. /* Redistribute probabilities. */
  2703. if (single_succ_p (bb))
  2704. {
  2705. single_succ_edge (bb)->probability = REG_BR_PROB_BASE;
  2706. single_succ_edge (bb)->count = bb->count;
  2707. }
  2708. else
  2709. {
  2710. note = find_reg_note (insn, REG_BR_PROB, NULL);
  2711. if (!note)
  2712. return purged;
  2713. b = BRANCH_EDGE (bb);
  2714. f = FALLTHRU_EDGE (bb);
  2715. b->probability = XINT (note, 0);
  2716. f->probability = REG_BR_PROB_BASE - b->probability;
  2717. /* Update these to use GCOV_COMPUTE_SCALE. */
  2718. b->count = bb->count * b->probability / REG_BR_PROB_BASE;
  2719. f->count = bb->count * f->probability / REG_BR_PROB_BASE;
  2720. }
  2721. return purged;
  2722. }
  2723. else if (CALL_P (insn) && SIBLING_CALL_P (insn))
  2724. {
  2725. /* First, there should not be any EH or ABCALL edges resulting
  2726. from non-local gotos and the like. If there were, we shouldn't
  2727. have created the sibcall in the first place. Second, there
  2728. should of course never have been a fallthru edge. */
  2729. gcc_assert (single_succ_p (bb));
  2730. gcc_assert (single_succ_edge (bb)->flags
  2731. == (EDGE_SIBCALL | EDGE_ABNORMAL));
  2732. return 0;
  2733. }
  2734. /* If we don't see a jump insn, we don't know exactly why the block would
  2735. have been broken at this point. Look for a simple, non-fallthru edge,
  2736. as these are only created by conditional branches. If we find such an
  2737. edge we know that there used to be a jump here and can then safely
  2738. remove all non-fallthru edges. */
  2739. found = false;
  2740. FOR_EACH_EDGE (e, ei, bb->succs)
  2741. if (! (e->flags & (EDGE_COMPLEX | EDGE_FALLTHRU)))
  2742. {
  2743. found = true;
  2744. break;
  2745. }
  2746. if (!found)
  2747. return purged;
  2748. /* Remove all but the fake and fallthru edges. The fake edge may be
  2749. the only successor for this block in the case of noreturn
  2750. calls. */
  2751. for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
  2752. {
  2753. if (!(e->flags & (EDGE_FALLTHRU | EDGE_FAKE)))
  2754. {
  2755. df_set_bb_dirty (bb);
  2756. remove_edge (e);
  2757. purged = true;
  2758. }
  2759. else
  2760. ei_next (&ei);
  2761. }
  2762. gcc_assert (single_succ_p (bb));
  2763. single_succ_edge (bb)->probability = REG_BR_PROB_BASE;
  2764. single_succ_edge (bb)->count = bb->count;
  2765. if (dump_file)
  2766. fprintf (dump_file, "Purged non-fallthru edges from bb %i\n",
  2767. bb->index);
  2768. return purged;
  2769. }
  2770. /* Search all basic blocks for potentially dead edges and purge them. Return
  2771. true if some edge has been eliminated. */
  2772. bool
  2773. purge_all_dead_edges (void)
  2774. {
  2775. int purged = false;
  2776. basic_block bb;
  2777. FOR_EACH_BB_FN (bb, cfun)
  2778. {
  2779. bool purged_here = purge_dead_edges (bb);
  2780. purged |= purged_here;
  2781. }
  2782. return purged;
  2783. }
  2784. /* This is used by a few passes that emit some instructions after abnormal
  2785. calls, moving the basic block's end, while they in fact do want to emit
  2786. them on the fallthru edge. Look for abnormal call edges, find backward
  2787. the call in the block and insert the instructions on the edge instead.
  2788. Similarly, handle instructions throwing exceptions internally.
  2789. Return true when instructions have been found and inserted on edges. */
  2790. bool
  2791. fixup_abnormal_edges (void)
  2792. {
  2793. bool inserted = false;
  2794. basic_block bb;
  2795. FOR_EACH_BB_FN (bb, cfun)
  2796. {
  2797. edge e;
  2798. edge_iterator ei;
  2799. /* Look for cases we are interested in - calls or instructions causing
  2800. exceptions. */
  2801. FOR_EACH_EDGE (e, ei, bb->succs)
  2802. if ((e->flags & EDGE_ABNORMAL_CALL)
  2803. || ((e->flags & (EDGE_ABNORMAL | EDGE_EH))
  2804. == (EDGE_ABNORMAL | EDGE_EH)))
  2805. break;
  2806. if (e && !CALL_P (BB_END (bb)) && !can_throw_internal (BB_END (bb)))
  2807. {
  2808. rtx_insn *insn;
  2809. /* Get past the new insns generated. Allow notes, as the insns
  2810. may be already deleted. */
  2811. insn = BB_END (bb);
  2812. while ((NONJUMP_INSN_P (insn) || NOTE_P (insn))
  2813. && !can_throw_internal (insn)
  2814. && insn != BB_HEAD (bb))
  2815. insn = PREV_INSN (insn);
  2816. if (CALL_P (insn) || can_throw_internal (insn))
  2817. {
  2818. rtx_insn *stop, *next;
  2819. e = find_fallthru_edge (bb->succs);
  2820. stop = NEXT_INSN (BB_END (bb));
  2821. BB_END (bb) = insn;
  2822. for (insn = NEXT_INSN (insn); insn != stop; insn = next)
  2823. {
  2824. next = NEXT_INSN (insn);
  2825. if (INSN_P (insn))
  2826. {
  2827. delete_insn (insn);
  2828. /* Sometimes there's still the return value USE.
  2829. If it's placed after a trapping call (i.e. that
  2830. call is the last insn anyway), we have no fallthru
  2831. edge. Simply delete this use and don't try to insert
  2832. on the non-existent edge. */
  2833. if (GET_CODE (PATTERN (insn)) != USE)
  2834. {
  2835. /* We're not deleting it, we're moving it. */
  2836. insn->set_undeleted ();
  2837. SET_PREV_INSN (insn) = NULL_RTX;
  2838. SET_NEXT_INSN (insn) = NULL_RTX;
  2839. insert_insn_on_edge (insn, e);
  2840. inserted = true;
  2841. }
  2842. }
  2843. else if (!BARRIER_P (insn))
  2844. set_block_for_insn (insn, NULL);
  2845. }
  2846. }
  2847. /* It may be that we don't find any trapping insn. In this
  2848. case we discovered quite late that the insn that had been
  2849. marked as can_throw_internal in fact couldn't trap at all.
  2850. So we should in fact delete the EH edges out of the block. */
  2851. else
  2852. purge_dead_edges (bb);
  2853. }
  2854. }
  2855. return inserted;
  2856. }
  2857. /* Cut the insns from FIRST to LAST out of the insns stream. */
  2858. rtx_insn *
  2859. unlink_insn_chain (rtx_insn *first, rtx_insn *last)
  2860. {
  2861. rtx_insn *prevfirst = PREV_INSN (first);
  2862. rtx_insn *nextlast = NEXT_INSN (last);
  2863. SET_PREV_INSN (first) = NULL;
  2864. SET_NEXT_INSN (last) = NULL;
  2865. if (prevfirst)
  2866. SET_NEXT_INSN (prevfirst) = nextlast;
  2867. if (nextlast)
  2868. SET_PREV_INSN (nextlast) = prevfirst;
  2869. else
  2870. set_last_insn (prevfirst);
  2871. if (!prevfirst)
  2872. set_first_insn (nextlast);
  2873. return first;
  2874. }
  2875. /* Skip over inter-block insns occurring after BB which are typically
  2876. associated with BB (e.g., barriers). If there are any such insns,
  2877. we return the last one. Otherwise, we return the end of BB. */
  2878. static rtx_insn *
  2879. skip_insns_after_block (basic_block bb)
  2880. {
  2881. rtx_insn *insn, *last_insn, *next_head, *prev;
  2882. next_head = NULL;
  2883. if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
  2884. next_head = BB_HEAD (bb->next_bb);
  2885. for (last_insn = insn = BB_END (bb); (insn = NEXT_INSN (insn)) != 0; )
  2886. {
  2887. if (insn == next_head)
  2888. break;
  2889. switch (GET_CODE (insn))
  2890. {
  2891. case BARRIER:
  2892. last_insn = insn;
  2893. continue;
  2894. case NOTE:
  2895. switch (NOTE_KIND (insn))
  2896. {
  2897. case NOTE_INSN_BLOCK_END:
  2898. gcc_unreachable ();
  2899. continue;
  2900. default:
  2901. continue;
  2902. break;
  2903. }
  2904. break;
  2905. case CODE_LABEL:
  2906. if (NEXT_INSN (insn)
  2907. && JUMP_TABLE_DATA_P (NEXT_INSN (insn)))
  2908. {
  2909. insn = NEXT_INSN (insn);
  2910. last_insn = insn;
  2911. continue;
  2912. }
  2913. break;
  2914. default:
  2915. break;
  2916. }
  2917. break;
  2918. }
  2919. /* It is possible to hit contradictory sequence. For instance:
  2920. jump_insn
  2921. NOTE_INSN_BLOCK_BEG
  2922. barrier
  2923. Where barrier belongs to jump_insn, but the note does not. This can be
  2924. created by removing the basic block originally following
  2925. NOTE_INSN_BLOCK_BEG. In such case reorder the notes. */
  2926. for (insn = last_insn; insn != BB_END (bb); insn = prev)
  2927. {
  2928. prev = PREV_INSN (insn);
  2929. if (NOTE_P (insn))
  2930. switch (NOTE_KIND (insn))
  2931. {
  2932. case NOTE_INSN_BLOCK_END:
  2933. gcc_unreachable ();
  2934. break;
  2935. case NOTE_INSN_DELETED:
  2936. case NOTE_INSN_DELETED_LABEL:
  2937. case NOTE_INSN_DELETED_DEBUG_LABEL:
  2938. continue;
  2939. default:
  2940. reorder_insns (insn, insn, last_insn);
  2941. }
  2942. }
  2943. return last_insn;
  2944. }
  2945. /* Locate or create a label for a given basic block. */
  2946. static rtx
  2947. label_for_bb (basic_block bb)
  2948. {
  2949. rtx label = BB_HEAD (bb);
  2950. if (!LABEL_P (label))
  2951. {
  2952. if (dump_file)
  2953. fprintf (dump_file, "Emitting label for block %d\n", bb->index);
  2954. label = block_label (bb);
  2955. }
  2956. return label;
  2957. }
  2958. /* Locate the effective beginning and end of the insn chain for each
  2959. block, as defined by skip_insns_after_block above. */
  2960. static void
  2961. record_effective_endpoints (void)
  2962. {
  2963. rtx_insn *next_insn;
  2964. basic_block bb;
  2965. rtx_insn *insn;
  2966. for (insn = get_insns ();
  2967. insn
  2968. && NOTE_P (insn)
  2969. && NOTE_KIND (insn) != NOTE_INSN_BASIC_BLOCK;
  2970. insn = NEXT_INSN (insn))
  2971. continue;
  2972. /* No basic blocks at all? */
  2973. gcc_assert (insn);
  2974. if (PREV_INSN (insn))
  2975. cfg_layout_function_header =
  2976. unlink_insn_chain (get_insns (), PREV_INSN (insn));
  2977. else
  2978. cfg_layout_function_header = NULL;
  2979. next_insn = get_insns ();
  2980. FOR_EACH_BB_FN (bb, cfun)
  2981. {
  2982. rtx_insn *end;
  2983. if (PREV_INSN (BB_HEAD (bb)) && next_insn != BB_HEAD (bb))
  2984. BB_HEADER (bb) = unlink_insn_chain (next_insn,
  2985. PREV_INSN (BB_HEAD (bb)));
  2986. end = skip_insns_after_block (bb);
  2987. if (NEXT_INSN (BB_END (bb)) && BB_END (bb) != end)
  2988. BB_FOOTER (bb) = unlink_insn_chain (NEXT_INSN (BB_END (bb)), end);
  2989. next_insn = NEXT_INSN (BB_END (bb));
  2990. }
  2991. cfg_layout_function_footer = next_insn;
  2992. if (cfg_layout_function_footer)
  2993. cfg_layout_function_footer = unlink_insn_chain (cfg_layout_function_footer, get_last_insn ());
  2994. }
  2995. namespace {
  2996. const pass_data pass_data_into_cfg_layout_mode =
  2997. {
  2998. RTL_PASS, /* type */
  2999. "into_cfglayout", /* name */
  3000. OPTGROUP_NONE, /* optinfo_flags */
  3001. TV_CFG, /* tv_id */
  3002. 0, /* properties_required */
  3003. PROP_cfglayout, /* properties_provided */
  3004. 0, /* properties_destroyed */
  3005. 0, /* todo_flags_start */
  3006. 0, /* todo_flags_finish */
  3007. };
  3008. class pass_into_cfg_layout_mode : public rtl_opt_pass
  3009. {
  3010. public:
  3011. pass_into_cfg_layout_mode (gcc::context *ctxt)
  3012. : rtl_opt_pass (pass_data_into_cfg_layout_mode, ctxt)
  3013. {}
  3014. /* opt_pass methods: */
  3015. virtual unsigned int execute (function *)
  3016. {
  3017. cfg_layout_initialize (0);
  3018. return 0;
  3019. }
  3020. }; // class pass_into_cfg_layout_mode
  3021. } // anon namespace
  3022. rtl_opt_pass *
  3023. make_pass_into_cfg_layout_mode (gcc::context *ctxt)
  3024. {
  3025. return new pass_into_cfg_layout_mode (ctxt);
  3026. }
  3027. namespace {
  3028. const pass_data pass_data_outof_cfg_layout_mode =
  3029. {
  3030. RTL_PASS, /* type */
  3031. "outof_cfglayout", /* name */
  3032. OPTGROUP_NONE, /* optinfo_flags */
  3033. TV_CFG, /* tv_id */
  3034. 0, /* properties_required */
  3035. 0, /* properties_provided */
  3036. PROP_cfglayout, /* properties_destroyed */
  3037. 0, /* todo_flags_start */
  3038. 0, /* todo_flags_finish */
  3039. };
  3040. class pass_outof_cfg_layout_mode : public rtl_opt_pass
  3041. {
  3042. public:
  3043. pass_outof_cfg_layout_mode (gcc::context *ctxt)
  3044. : rtl_opt_pass (pass_data_outof_cfg_layout_mode, ctxt)
  3045. {}
  3046. /* opt_pass methods: */
  3047. virtual unsigned int execute (function *);
  3048. }; // class pass_outof_cfg_layout_mode
  3049. unsigned int
  3050. pass_outof_cfg_layout_mode::execute (function *fun)
  3051. {
  3052. basic_block bb;
  3053. FOR_EACH_BB_FN (bb, fun)
  3054. if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (fun))
  3055. bb->aux = bb->next_bb;
  3056. cfg_layout_finalize ();
  3057. return 0;
  3058. }
  3059. } // anon namespace
  3060. rtl_opt_pass *
  3061. make_pass_outof_cfg_layout_mode (gcc::context *ctxt)
  3062. {
  3063. return new pass_outof_cfg_layout_mode (ctxt);
  3064. }
  3065. /* Link the basic blocks in the correct order, compacting the basic
  3066. block queue while at it. If STAY_IN_CFGLAYOUT_MODE is false, this
  3067. function also clears the basic block header and footer fields.
  3068. This function is usually called after a pass (e.g. tracer) finishes
  3069. some transformations while in cfglayout mode. The required sequence
  3070. of the basic blocks is in a linked list along the bb->aux field.
  3071. This functions re-links the basic block prev_bb and next_bb pointers
  3072. accordingly, and it compacts and renumbers the blocks.
  3073. FIXME: This currently works only for RTL, but the only RTL-specific
  3074. bits are the STAY_IN_CFGLAYOUT_MODE bits. The tracer pass was moved
  3075. to GIMPLE a long time ago, but it doesn't relink the basic block
  3076. chain. It could do that (to give better initial RTL) if this function
  3077. is made IR-agnostic (and moved to cfganal.c or cfg.c while at it). */
  3078. void
  3079. relink_block_chain (bool stay_in_cfglayout_mode)
  3080. {
  3081. basic_block bb, prev_bb;
  3082. int index;
  3083. /* Maybe dump the re-ordered sequence. */
  3084. if (dump_file)
  3085. {
  3086. fprintf (dump_file, "Reordered sequence:\n");
  3087. for (bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb, index =
  3088. NUM_FIXED_BLOCKS;
  3089. bb;
  3090. bb = (basic_block) bb->aux, index++)
  3091. {
  3092. fprintf (dump_file, " %i ", index);
  3093. if (get_bb_original (bb))
  3094. fprintf (dump_file, "duplicate of %i ",
  3095. get_bb_original (bb)->index);
  3096. else if (forwarder_block_p (bb)
  3097. && !LABEL_P (BB_HEAD (bb)))
  3098. fprintf (dump_file, "compensation ");
  3099. else
  3100. fprintf (dump_file, "bb %i ", bb->index);
  3101. fprintf (dump_file, " [%i]\n", bb->frequency);
  3102. }
  3103. }
  3104. /* Now reorder the blocks. */
  3105. prev_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
  3106. bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb;
  3107. for (; bb; prev_bb = bb, bb = (basic_block) bb->aux)
  3108. {
  3109. bb->prev_bb = prev_bb;
  3110. prev_bb->next_bb = bb;
  3111. }
  3112. prev_bb->next_bb = EXIT_BLOCK_PTR_FOR_FN (cfun);
  3113. EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb = prev_bb;
  3114. /* Then, clean up the aux fields. */
  3115. FOR_ALL_BB_FN (bb, cfun)
  3116. {
  3117. bb->aux = NULL;
  3118. if (!stay_in_cfglayout_mode)
  3119. BB_HEADER (bb) = BB_FOOTER (bb) = NULL;
  3120. }
  3121. /* Maybe reset the original copy tables, they are not valid anymore
  3122. when we renumber the basic blocks in compact_blocks. If we are
  3123. are going out of cfglayout mode, don't re-allocate the tables. */
  3124. free_original_copy_tables ();
  3125. if (stay_in_cfglayout_mode)
  3126. initialize_original_copy_tables ();
  3127. /* Finally, put basic_block_info in the new order. */
  3128. compact_blocks ();
  3129. }
  3130. /* Given a reorder chain, rearrange the code to match. */
  3131. static void
  3132. fixup_reorder_chain (void)
  3133. {
  3134. basic_block bb;
  3135. rtx_insn *insn = NULL;
  3136. if (cfg_layout_function_header)
  3137. {
  3138. set_first_insn (cfg_layout_function_header);
  3139. insn = cfg_layout_function_header;
  3140. while (NEXT_INSN (insn))
  3141. insn = NEXT_INSN (insn);
  3142. }
  3143. /* First do the bulk reordering -- rechain the blocks without regard to
  3144. the needed changes to jumps and labels. */
  3145. for (bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; bb; bb = (basic_block)
  3146. bb->aux)
  3147. {
  3148. if (BB_HEADER (bb))
  3149. {
  3150. if (insn)
  3151. SET_NEXT_INSN (insn) = BB_HEADER (bb);
  3152. else
  3153. set_first_insn (BB_HEADER (bb));
  3154. SET_PREV_INSN (BB_HEADER (bb)) = insn;
  3155. insn = BB_HEADER (bb);
  3156. while (NEXT_INSN (insn))
  3157. insn = NEXT_INSN (insn);
  3158. }
  3159. if (insn)
  3160. SET_NEXT_INSN (insn) = BB_HEAD (bb);
  3161. else
  3162. set_first_insn (BB_HEAD (bb));
  3163. SET_PREV_INSN (BB_HEAD (bb)) = insn;
  3164. insn = BB_END (bb);
  3165. if (BB_FOOTER (bb))
  3166. {
  3167. SET_NEXT_INSN (insn) = BB_FOOTER (bb);
  3168. SET_PREV_INSN (BB_FOOTER (bb)) = insn;
  3169. while (NEXT_INSN (insn))
  3170. insn = NEXT_INSN (insn);
  3171. }
  3172. }
  3173. SET_NEXT_INSN (insn) = cfg_layout_function_footer;
  3174. if (cfg_layout_function_footer)
  3175. SET_PREV_INSN (cfg_layout_function_footer) = insn;
  3176. while (NEXT_INSN (insn))
  3177. insn = NEXT_INSN (insn);
  3178. set_last_insn (insn);
  3179. #ifdef ENABLE_CHECKING
  3180. verify_insn_chain ();
  3181. #endif
  3182. /* Now add jumps and labels as needed to match the blocks new
  3183. outgoing edges. */
  3184. for (bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; bb ; bb = (basic_block)
  3185. bb->aux)
  3186. {
  3187. edge e_fall, e_taken, e;
  3188. rtx_insn *bb_end_insn;
  3189. rtx ret_label = NULL_RTX;
  3190. basic_block nb;
  3191. edge_iterator ei;
  3192. if (EDGE_COUNT (bb->succs) == 0)
  3193. continue;
  3194. /* Find the old fallthru edge, and another non-EH edge for
  3195. a taken jump. */
  3196. e_taken = e_fall = NULL;
  3197. FOR_EACH_EDGE (e, ei, bb->succs)
  3198. if (e->flags & EDGE_FALLTHRU)
  3199. e_fall = e;
  3200. else if (! (e->flags & EDGE_EH))
  3201. e_taken = e;
  3202. bb_end_insn = BB_END (bb);
  3203. if (JUMP_P (bb_end_insn))
  3204. {
  3205. ret_label = JUMP_LABEL (bb_end_insn);
  3206. if (any_condjump_p (bb_end_insn))
  3207. {
  3208. /* This might happen if the conditional jump has side
  3209. effects and could therefore not be optimized away.
  3210. Make the basic block to end with a barrier in order
  3211. to prevent rtl_verify_flow_info from complaining. */
  3212. if (!e_fall)
  3213. {
  3214. gcc_assert (!onlyjump_p (bb_end_insn)
  3215. || returnjump_p (bb_end_insn)
  3216. || (e_taken->flags & EDGE_CROSSING));
  3217. emit_barrier_after (bb_end_insn);
  3218. continue;
  3219. }
  3220. /* If the old fallthru is still next, nothing to do. */
  3221. if (bb->aux == e_fall->dest
  3222. || e_fall->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
  3223. continue;
  3224. /* The degenerated case of conditional jump jumping to the next
  3225. instruction can happen for jumps with side effects. We need
  3226. to construct a forwarder block and this will be done just
  3227. fine by force_nonfallthru below. */
  3228. if (!e_taken)
  3229. ;
  3230. /* There is another special case: if *neither* block is next,
  3231. such as happens at the very end of a function, then we'll
  3232. need to add a new unconditional jump. Choose the taken
  3233. edge based on known or assumed probability. */
  3234. else if (bb->aux != e_taken->dest)
  3235. {
  3236. rtx note = find_reg_note (bb_end_insn, REG_BR_PROB, 0);
  3237. if (note
  3238. && XINT (note, 0) < REG_BR_PROB_BASE / 2
  3239. && invert_jump (bb_end_insn,
  3240. (e_fall->dest
  3241. == EXIT_BLOCK_PTR_FOR_FN (cfun)
  3242. ? NULL_RTX
  3243. : label_for_bb (e_fall->dest)), 0))
  3244. {
  3245. e_fall->flags &= ~EDGE_FALLTHRU;
  3246. gcc_checking_assert (could_fall_through
  3247. (e_taken->src, e_taken->dest));
  3248. e_taken->flags |= EDGE_FALLTHRU;
  3249. update_br_prob_note (bb);
  3250. e = e_fall, e_fall = e_taken, e_taken = e;
  3251. }
  3252. }
  3253. /* If the "jumping" edge is a crossing edge, and the fall
  3254. through edge is non-crossing, leave things as they are. */
  3255. else if ((e_taken->flags & EDGE_CROSSING)
  3256. && !(e_fall->flags & EDGE_CROSSING))
  3257. continue;
  3258. /* Otherwise we can try to invert the jump. This will
  3259. basically never fail, however, keep up the pretense. */
  3260. else if (invert_jump (bb_end_insn,
  3261. (e_fall->dest
  3262. == EXIT_BLOCK_PTR_FOR_FN (cfun)
  3263. ? NULL_RTX
  3264. : label_for_bb (e_fall->dest)), 0))
  3265. {
  3266. e_fall->flags &= ~EDGE_FALLTHRU;
  3267. gcc_checking_assert (could_fall_through
  3268. (e_taken->src, e_taken->dest));
  3269. e_taken->flags |= EDGE_FALLTHRU;
  3270. update_br_prob_note (bb);
  3271. if (LABEL_NUSES (ret_label) == 0
  3272. && single_pred_p (e_taken->dest))
  3273. delete_insn (ret_label);
  3274. continue;
  3275. }
  3276. }
  3277. else if (extract_asm_operands (PATTERN (bb_end_insn)) != NULL)
  3278. {
  3279. /* If the old fallthru is still next or if
  3280. asm goto doesn't have a fallthru (e.g. when followed by
  3281. __builtin_unreachable ()), nothing to do. */
  3282. if (! e_fall
  3283. || bb->aux == e_fall->dest
  3284. || e_fall->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
  3285. continue;
  3286. /* Otherwise we'll have to use the fallthru fixup below. */
  3287. }
  3288. else
  3289. {
  3290. /* Otherwise we have some return, switch or computed
  3291. jump. In the 99% case, there should not have been a
  3292. fallthru edge. */
  3293. gcc_assert (returnjump_p (bb_end_insn) || !e_fall);
  3294. continue;
  3295. }
  3296. }
  3297. else
  3298. {
  3299. /* No fallthru implies a noreturn function with EH edges, or
  3300. something similarly bizarre. In any case, we don't need to
  3301. do anything. */
  3302. if (! e_fall)
  3303. continue;
  3304. /* If the fallthru block is still next, nothing to do. */
  3305. if (bb->aux == e_fall->dest)
  3306. continue;
  3307. /* A fallthru to exit block. */
  3308. if (e_fall->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
  3309. continue;
  3310. }
  3311. /* We got here if we need to add a new jump insn.
  3312. Note force_nonfallthru can delete E_FALL and thus we have to
  3313. save E_FALL->src prior to the call to force_nonfallthru. */
  3314. nb = force_nonfallthru_and_redirect (e_fall, e_fall->dest, ret_label);
  3315. if (nb)
  3316. {
  3317. nb->aux = bb->aux;
  3318. bb->aux = nb;
  3319. /* Don't process this new block. */
  3320. bb = nb;
  3321. }
  3322. }
  3323. relink_block_chain (/*stay_in_cfglayout_mode=*/false);
  3324. /* Annoying special case - jump around dead jumptables left in the code. */
  3325. FOR_EACH_BB_FN (bb, cfun)
  3326. {
  3327. edge e = find_fallthru_edge (bb->succs);
  3328. if (e && !can_fallthru (e->src, e->dest))
  3329. force_nonfallthru (e);
  3330. }
  3331. /* Ensure goto_locus from edges has some instructions with that locus
  3332. in RTL. */
  3333. if (!optimize)
  3334. FOR_EACH_BB_FN (bb, cfun)
  3335. {
  3336. edge e;
  3337. edge_iterator ei;
  3338. FOR_EACH_EDGE (e, ei, bb->succs)
  3339. if (LOCATION_LOCUS (e->goto_locus) != UNKNOWN_LOCATION
  3340. && !(e->flags & EDGE_ABNORMAL))
  3341. {
  3342. edge e2;
  3343. edge_iterator ei2;
  3344. basic_block dest, nb;
  3345. rtx_insn *end;
  3346. insn = BB_END (e->src);
  3347. end = PREV_INSN (BB_HEAD (e->src));
  3348. while (insn != end
  3349. && (!NONDEBUG_INSN_P (insn) || !INSN_HAS_LOCATION (insn)))
  3350. insn = PREV_INSN (insn);
  3351. if (insn != end
  3352. && INSN_LOCATION (insn) == e->goto_locus)
  3353. continue;
  3354. if (simplejump_p (BB_END (e->src))
  3355. && !INSN_HAS_LOCATION (BB_END (e->src)))
  3356. {
  3357. INSN_LOCATION (BB_END (e->src)) = e->goto_locus;
  3358. continue;
  3359. }
  3360. dest = e->dest;
  3361. if (dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
  3362. {
  3363. /* Non-fallthru edges to the exit block cannot be split. */
  3364. if (!(e->flags & EDGE_FALLTHRU))
  3365. continue;
  3366. }
  3367. else
  3368. {
  3369. insn = BB_HEAD (dest);
  3370. end = NEXT_INSN (BB_END (dest));
  3371. while (insn != end && !NONDEBUG_INSN_P (insn))
  3372. insn = NEXT_INSN (insn);
  3373. if (insn != end && INSN_HAS_LOCATION (insn)
  3374. && INSN_LOCATION (insn) == e->goto_locus)
  3375. continue;
  3376. }
  3377. nb = split_edge (e);
  3378. if (!INSN_P (BB_END (nb)))
  3379. BB_END (nb) = emit_insn_after_noloc (gen_nop (), BB_END (nb),
  3380. nb);
  3381. INSN_LOCATION (BB_END (nb)) = e->goto_locus;
  3382. /* If there are other incoming edges to the destination block
  3383. with the same goto locus, redirect them to the new block as
  3384. well, this can prevent other such blocks from being created
  3385. in subsequent iterations of the loop. */
  3386. for (ei2 = ei_start (dest->preds); (e2 = ei_safe_edge (ei2)); )
  3387. if (LOCATION_LOCUS (e2->goto_locus) != UNKNOWN_LOCATION
  3388. && !(e2->flags & (EDGE_ABNORMAL | EDGE_FALLTHRU))
  3389. && e->goto_locus == e2->goto_locus)
  3390. redirect_edge_and_branch (e2, nb);
  3391. else
  3392. ei_next (&ei2);
  3393. }
  3394. }
  3395. }
  3396. /* Perform sanity checks on the insn chain.
  3397. 1. Check that next/prev pointers are consistent in both the forward and
  3398. reverse direction.
  3399. 2. Count insns in chain, going both directions, and check if equal.
  3400. 3. Check that get_last_insn () returns the actual end of chain. */
  3401. DEBUG_FUNCTION void
  3402. verify_insn_chain (void)
  3403. {
  3404. rtx_insn *x, *prevx, *nextx;
  3405. int insn_cnt1, insn_cnt2;
  3406. for (prevx = NULL, insn_cnt1 = 1, x = get_insns ();
  3407. x != 0;
  3408. prevx = x, insn_cnt1++, x = NEXT_INSN (x))
  3409. gcc_assert (PREV_INSN (x) == prevx);
  3410. gcc_assert (prevx == get_last_insn ());
  3411. for (nextx = NULL, insn_cnt2 = 1, x = get_last_insn ();
  3412. x != 0;
  3413. nextx = x, insn_cnt2++, x = PREV_INSN (x))
  3414. gcc_assert (NEXT_INSN (x) == nextx);
  3415. gcc_assert (insn_cnt1 == insn_cnt2);
  3416. }
  3417. /* If we have assembler epilogues, the block falling through to exit must
  3418. be the last one in the reordered chain when we reach final. Ensure
  3419. that this condition is met. */
  3420. static void
  3421. fixup_fallthru_exit_predecessor (void)
  3422. {
  3423. edge e;
  3424. basic_block bb = NULL;
  3425. /* This transformation is not valid before reload, because we might
  3426. separate a call from the instruction that copies the return
  3427. value. */
  3428. gcc_assert (reload_completed);
  3429. e = find_fallthru_edge (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
  3430. if (e)
  3431. bb = e->src;
  3432. if (bb && bb->aux)
  3433. {
  3434. basic_block c = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb;
  3435. /* If the very first block is the one with the fall-through exit
  3436. edge, we have to split that block. */
  3437. if (c == bb)
  3438. {
  3439. bb = split_block (bb, NULL)->dest;
  3440. bb->aux = c->aux;
  3441. c->aux = bb;
  3442. BB_FOOTER (bb) = BB_FOOTER (c);
  3443. BB_FOOTER (c) = NULL;
  3444. }
  3445. while (c->aux != bb)
  3446. c = (basic_block) c->aux;
  3447. c->aux = bb->aux;
  3448. while (c->aux)
  3449. c = (basic_block) c->aux;
  3450. c->aux = bb;
  3451. bb->aux = NULL;
  3452. }
  3453. }
  3454. /* In case there are more than one fallthru predecessors of exit, force that
  3455. there is only one. */
  3456. static void
  3457. force_one_exit_fallthru (void)
  3458. {
  3459. edge e, predecessor = NULL;
  3460. bool more = false;
  3461. edge_iterator ei;
  3462. basic_block forwarder, bb;
  3463. FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
  3464. if (e->flags & EDGE_FALLTHRU)
  3465. {
  3466. if (predecessor == NULL)
  3467. predecessor = e;
  3468. else
  3469. {
  3470. more = true;
  3471. break;
  3472. }
  3473. }
  3474. if (!more)
  3475. return;
  3476. /* Exit has several fallthru predecessors. Create a forwarder block for
  3477. them. */
  3478. forwarder = split_edge (predecessor);
  3479. for (ei = ei_start (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds);
  3480. (e = ei_safe_edge (ei)); )
  3481. {
  3482. if (e->src == forwarder
  3483. || !(e->flags & EDGE_FALLTHRU))
  3484. ei_next (&ei);
  3485. else
  3486. redirect_edge_and_branch_force (e, forwarder);
  3487. }
  3488. /* Fix up the chain of blocks -- make FORWARDER immediately precede the
  3489. exit block. */
  3490. FOR_EACH_BB_FN (bb, cfun)
  3491. {
  3492. if (bb->aux == NULL && bb != forwarder)
  3493. {
  3494. bb->aux = forwarder;
  3495. break;
  3496. }
  3497. }
  3498. }
  3499. /* Return true in case it is possible to duplicate the basic block BB. */
  3500. static bool
  3501. cfg_layout_can_duplicate_bb_p (const_basic_block bb)
  3502. {
  3503. /* Do not attempt to duplicate tablejumps, as we need to unshare
  3504. the dispatch table. This is difficult to do, as the instructions
  3505. computing jump destination may be hoisted outside the basic block. */
  3506. if (tablejump_p (BB_END (bb), NULL, NULL))
  3507. return false;
  3508. /* Do not duplicate blocks containing insns that can't be copied. */
  3509. if (targetm.cannot_copy_insn_p)
  3510. {
  3511. rtx_insn *insn = BB_HEAD (bb);
  3512. while (1)
  3513. {
  3514. if (INSN_P (insn) && targetm.cannot_copy_insn_p (insn))
  3515. return false;
  3516. if (insn == BB_END (bb))
  3517. break;
  3518. insn = NEXT_INSN (insn);
  3519. }
  3520. }
  3521. return true;
  3522. }
  3523. rtx_insn *
  3524. duplicate_insn_chain (rtx_insn *from, rtx_insn *to)
  3525. {
  3526. rtx_insn *insn, *next, *copy;
  3527. rtx_note *last;
  3528. /* Avoid updating of boundaries of previous basic block. The
  3529. note will get removed from insn stream in fixup. */
  3530. last = emit_note (NOTE_INSN_DELETED);
  3531. /* Create copy at the end of INSN chain. The chain will
  3532. be reordered later. */
  3533. for (insn = from; insn != NEXT_INSN (to); insn = NEXT_INSN (insn))
  3534. {
  3535. switch (GET_CODE (insn))
  3536. {
  3537. case DEBUG_INSN:
  3538. /* Don't duplicate label debug insns. */
  3539. if (TREE_CODE (INSN_VAR_LOCATION_DECL (insn)) == LABEL_DECL)
  3540. break;
  3541. /* FALLTHRU */
  3542. case INSN:
  3543. case CALL_INSN:
  3544. case JUMP_INSN:
  3545. copy = emit_copy_of_insn_after (insn, get_last_insn ());
  3546. if (JUMP_P (insn) && JUMP_LABEL (insn) != NULL_RTX
  3547. && ANY_RETURN_P (JUMP_LABEL (insn)))
  3548. JUMP_LABEL (copy) = JUMP_LABEL (insn);
  3549. maybe_copy_prologue_epilogue_insn (insn, copy);
  3550. break;
  3551. case JUMP_TABLE_DATA:
  3552. /* Avoid copying of dispatch tables. We never duplicate
  3553. tablejumps, so this can hit only in case the table got
  3554. moved far from original jump.
  3555. Avoid copying following barrier as well if any
  3556. (and debug insns in between). */
  3557. for (next = NEXT_INSN (insn);
  3558. next != NEXT_INSN (to);
  3559. next = NEXT_INSN (next))
  3560. if (!DEBUG_INSN_P (next))
  3561. break;
  3562. if (next != NEXT_INSN (to) && BARRIER_P (next))
  3563. insn = next;
  3564. break;
  3565. case CODE_LABEL:
  3566. break;
  3567. case BARRIER:
  3568. emit_barrier ();
  3569. break;
  3570. case NOTE:
  3571. switch (NOTE_KIND (insn))
  3572. {
  3573. /* In case prologue is empty and function contain label
  3574. in first BB, we may want to copy the block. */
  3575. case NOTE_INSN_PROLOGUE_END:
  3576. case NOTE_INSN_DELETED:
  3577. case NOTE_INSN_DELETED_LABEL:
  3578. case NOTE_INSN_DELETED_DEBUG_LABEL:
  3579. /* No problem to strip these. */
  3580. case NOTE_INSN_FUNCTION_BEG:
  3581. /* There is always just single entry to function. */
  3582. case NOTE_INSN_BASIC_BLOCK:
  3583. /* We should only switch text sections once. */
  3584. case NOTE_INSN_SWITCH_TEXT_SECTIONS:
  3585. break;
  3586. case NOTE_INSN_EPILOGUE_BEG:
  3587. emit_note_copy (as_a <rtx_note *> (insn));
  3588. break;
  3589. default:
  3590. /* All other notes should have already been eliminated. */
  3591. gcc_unreachable ();
  3592. }
  3593. break;
  3594. default:
  3595. gcc_unreachable ();
  3596. }
  3597. }
  3598. insn = NEXT_INSN (last);
  3599. delete_insn (last);
  3600. return insn;
  3601. }
  3602. /* Create a duplicate of the basic block BB. */
  3603. static basic_block
  3604. cfg_layout_duplicate_bb (basic_block bb)
  3605. {
  3606. rtx_insn *insn;
  3607. basic_block new_bb;
  3608. insn = duplicate_insn_chain (BB_HEAD (bb), BB_END (bb));
  3609. new_bb = create_basic_block (insn,
  3610. insn ? get_last_insn () : NULL,
  3611. EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb);
  3612. BB_COPY_PARTITION (new_bb, bb);
  3613. if (BB_HEADER (bb))
  3614. {
  3615. insn = BB_HEADER (bb);
  3616. while (NEXT_INSN (insn))
  3617. insn = NEXT_INSN (insn);
  3618. insn = duplicate_insn_chain (BB_HEADER (bb), insn);
  3619. if (insn)
  3620. BB_HEADER (new_bb) = unlink_insn_chain (insn, get_last_insn ());
  3621. }
  3622. if (BB_FOOTER (bb))
  3623. {
  3624. insn = BB_FOOTER (bb);
  3625. while (NEXT_INSN (insn))
  3626. insn = NEXT_INSN (insn);
  3627. insn = duplicate_insn_chain (BB_FOOTER (bb), insn);
  3628. if (insn)
  3629. BB_FOOTER (new_bb) = unlink_insn_chain (insn, get_last_insn ());
  3630. }
  3631. return new_bb;
  3632. }
  3633. /* Main entry point to this module - initialize the datastructures for
  3634. CFG layout changes. It keeps LOOPS up-to-date if not null.
  3635. FLAGS is a set of additional flags to pass to cleanup_cfg(). */
  3636. void
  3637. cfg_layout_initialize (unsigned int flags)
  3638. {
  3639. rtx_insn_list *x;
  3640. basic_block bb;
  3641. /* Once bb partitioning is complete, cfg layout mode should not be
  3642. re-entered. Entering cfg layout mode may require fixups. As an
  3643. example, if edge forwarding performed when optimizing the cfg
  3644. layout required moving a block from the hot to the cold
  3645. section. This would create an illegal partitioning unless some
  3646. manual fixup was performed. */
  3647. gcc_assert (!(crtl->bb_reorder_complete
  3648. && flag_reorder_blocks_and_partition));
  3649. initialize_original_copy_tables ();
  3650. cfg_layout_rtl_register_cfg_hooks ();
  3651. record_effective_endpoints ();
  3652. /* Make sure that the targets of non local gotos are marked. */
  3653. for (x = nonlocal_goto_handler_labels; x; x = x->next ())
  3654. {
  3655. bb = BLOCK_FOR_INSN (x->insn ());
  3656. bb->flags |= BB_NON_LOCAL_GOTO_TARGET;
  3657. }
  3658. cleanup_cfg (CLEANUP_CFGLAYOUT | flags);
  3659. }
  3660. /* Splits superblocks. */
  3661. void
  3662. break_superblocks (void)
  3663. {
  3664. sbitmap superblocks;
  3665. bool need = false;
  3666. basic_block bb;
  3667. superblocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
  3668. bitmap_clear (superblocks);
  3669. FOR_EACH_BB_FN (bb, cfun)
  3670. if (bb->flags & BB_SUPERBLOCK)
  3671. {
  3672. bb->flags &= ~BB_SUPERBLOCK;
  3673. bitmap_set_bit (superblocks, bb->index);
  3674. need = true;
  3675. }
  3676. if (need)
  3677. {
  3678. rebuild_jump_labels (get_insns ());
  3679. find_many_sub_basic_blocks (superblocks);
  3680. }
  3681. free (superblocks);
  3682. }
  3683. /* Finalize the changes: reorder insn list according to the sequence specified
  3684. by aux pointers, enter compensation code, rebuild scope forest. */
  3685. void
  3686. cfg_layout_finalize (void)
  3687. {
  3688. #ifdef ENABLE_CHECKING
  3689. verify_flow_info ();
  3690. #endif
  3691. force_one_exit_fallthru ();
  3692. rtl_register_cfg_hooks ();
  3693. if (reload_completed
  3694. #ifdef HAVE_epilogue
  3695. && !HAVE_epilogue
  3696. #endif
  3697. )
  3698. fixup_fallthru_exit_predecessor ();
  3699. fixup_reorder_chain ();
  3700. rebuild_jump_labels (get_insns ());
  3701. delete_dead_jumptables ();
  3702. #ifdef ENABLE_CHECKING
  3703. verify_insn_chain ();
  3704. verify_flow_info ();
  3705. #endif
  3706. }
  3707. /* Same as split_block but update cfg_layout structures. */
  3708. static basic_block
  3709. cfg_layout_split_block (basic_block bb, void *insnp)
  3710. {
  3711. rtx insn = (rtx) insnp;
  3712. basic_block new_bb = rtl_split_block (bb, insn);
  3713. BB_FOOTER (new_bb) = BB_FOOTER (bb);
  3714. BB_FOOTER (bb) = NULL;
  3715. return new_bb;
  3716. }
  3717. /* Redirect Edge to DEST. */
  3718. static edge
  3719. cfg_layout_redirect_edge_and_branch (edge e, basic_block dest)
  3720. {
  3721. basic_block src = e->src;
  3722. edge ret;
  3723. if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
  3724. return NULL;
  3725. if (e->dest == dest)
  3726. return e;
  3727. if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
  3728. && (ret = try_redirect_by_replacing_jump (e, dest, true)))
  3729. {
  3730. df_set_bb_dirty (src);
  3731. return ret;
  3732. }
  3733. if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun)
  3734. && (e->flags & EDGE_FALLTHRU) && !(e->flags & EDGE_COMPLEX))
  3735. {
  3736. if (dump_file)
  3737. fprintf (dump_file, "Redirecting entry edge from bb %i to %i\n",
  3738. e->src->index, dest->index);
  3739. df_set_bb_dirty (e->src);
  3740. redirect_edge_succ (e, dest);
  3741. return e;
  3742. }
  3743. /* Redirect_edge_and_branch may decide to turn branch into fallthru edge
  3744. in the case the basic block appears to be in sequence. Avoid this
  3745. transformation. */
  3746. if (e->flags & EDGE_FALLTHRU)
  3747. {
  3748. /* Redirect any branch edges unified with the fallthru one. */
  3749. if (JUMP_P (BB_END (src))
  3750. && label_is_jump_target_p (BB_HEAD (e->dest),
  3751. BB_END (src)))
  3752. {
  3753. edge redirected;
  3754. if (dump_file)
  3755. fprintf (dump_file, "Fallthru edge unified with branch "
  3756. "%i->%i redirected to %i\n",
  3757. e->src->index, e->dest->index, dest->index);
  3758. e->flags &= ~EDGE_FALLTHRU;
  3759. redirected = redirect_branch_edge (e, dest);
  3760. gcc_assert (redirected);
  3761. redirected->flags |= EDGE_FALLTHRU;
  3762. df_set_bb_dirty (redirected->src);
  3763. return redirected;
  3764. }
  3765. /* In case we are redirecting fallthru edge to the branch edge
  3766. of conditional jump, remove it. */
  3767. if (EDGE_COUNT (src->succs) == 2)
  3768. {
  3769. /* Find the edge that is different from E. */
  3770. edge s = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e);
  3771. if (s->dest == dest
  3772. && any_condjump_p (BB_END (src))
  3773. && onlyjump_p (BB_END (src)))
  3774. delete_insn (BB_END (src));
  3775. }
  3776. if (dump_file)
  3777. fprintf (dump_file, "Redirecting fallthru edge %i->%i to %i\n",
  3778. e->src->index, e->dest->index, dest->index);
  3779. ret = redirect_edge_succ_nodup (e, dest);
  3780. }
  3781. else
  3782. ret = redirect_branch_edge (e, dest);
  3783. /* We don't want simplejumps in the insn stream during cfglayout. */
  3784. gcc_assert (!simplejump_p (BB_END (src)));
  3785. df_set_bb_dirty (src);
  3786. return ret;
  3787. }
  3788. /* Simple wrapper as we always can redirect fallthru edges. */
  3789. static basic_block
  3790. cfg_layout_redirect_edge_and_branch_force (edge e, basic_block dest)
  3791. {
  3792. edge redirected = cfg_layout_redirect_edge_and_branch (e, dest);
  3793. gcc_assert (redirected);
  3794. return NULL;
  3795. }
  3796. /* Same as delete_basic_block but update cfg_layout structures. */
  3797. static void
  3798. cfg_layout_delete_block (basic_block bb)
  3799. {
  3800. rtx_insn *insn, *next, *prev = PREV_INSN (BB_HEAD (bb)), *remaints;
  3801. rtx_insn **to;
  3802. if (BB_HEADER (bb))
  3803. {
  3804. next = BB_HEAD (bb);
  3805. if (prev)
  3806. SET_NEXT_INSN (prev) = BB_HEADER (bb);
  3807. else
  3808. set_first_insn (BB_HEADER (bb));
  3809. SET_PREV_INSN (BB_HEADER (bb)) = prev;
  3810. insn = BB_HEADER (bb);
  3811. while (NEXT_INSN (insn))
  3812. insn = NEXT_INSN (insn);
  3813. SET_NEXT_INSN (insn) = next;
  3814. SET_PREV_INSN (next) = insn;
  3815. }
  3816. next = NEXT_INSN (BB_END (bb));
  3817. if (BB_FOOTER (bb))
  3818. {
  3819. insn = BB_FOOTER (bb);
  3820. while (insn)
  3821. {
  3822. if (BARRIER_P (insn))
  3823. {
  3824. if (PREV_INSN (insn))
  3825. SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
  3826. else
  3827. BB_FOOTER (bb) = NEXT_INSN (insn);
  3828. if (NEXT_INSN (insn))
  3829. SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
  3830. }
  3831. if (LABEL_P (insn))
  3832. break;
  3833. insn = NEXT_INSN (insn);
  3834. }
  3835. if (BB_FOOTER (bb))
  3836. {
  3837. insn = BB_END (bb);
  3838. SET_NEXT_INSN (insn) = BB_FOOTER (bb);
  3839. SET_PREV_INSN (BB_FOOTER (bb)) = insn;
  3840. while (NEXT_INSN (insn))
  3841. insn = NEXT_INSN (insn);
  3842. SET_NEXT_INSN (insn) = next;
  3843. if (next)
  3844. SET_PREV_INSN (next) = insn;
  3845. else
  3846. set_last_insn (insn);
  3847. }
  3848. }
  3849. if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
  3850. to = &BB_HEADER (bb->next_bb);
  3851. else
  3852. to = &cfg_layout_function_footer;
  3853. rtl_delete_block (bb);
  3854. if (prev)
  3855. prev = NEXT_INSN (prev);
  3856. else
  3857. prev = get_insns ();
  3858. if (next)
  3859. next = PREV_INSN (next);
  3860. else
  3861. next = get_last_insn ();
  3862. if (next && NEXT_INSN (next) != prev)
  3863. {
  3864. remaints = unlink_insn_chain (prev, next);
  3865. insn = remaints;
  3866. while (NEXT_INSN (insn))
  3867. insn = NEXT_INSN (insn);
  3868. SET_NEXT_INSN (insn) = *to;
  3869. if (*to)
  3870. SET_PREV_INSN (*to) = insn;
  3871. *to = remaints;
  3872. }
  3873. }
  3874. /* Return true when blocks A and B can be safely merged. */
  3875. static bool
  3876. cfg_layout_can_merge_blocks_p (basic_block a, basic_block b)
  3877. {
  3878. /* If we are partitioning hot/cold basic blocks, we don't want to
  3879. mess up unconditional or indirect jumps that cross between hot
  3880. and cold sections.
  3881. Basic block partitioning may result in some jumps that appear to
  3882. be optimizable (or blocks that appear to be mergeable), but which really
  3883. must be left untouched (they are required to make it safely across
  3884. partition boundaries). See the comments at the top of
  3885. bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */
  3886. if (BB_PARTITION (a) != BB_PARTITION (b))
  3887. return false;
  3888. /* Protect the loop latches. */
  3889. if (current_loops && b->loop_father->latch == b)
  3890. return false;
  3891. /* If we would end up moving B's instructions, make sure it doesn't fall
  3892. through into the exit block, since we cannot recover from a fallthrough
  3893. edge into the exit block occurring in the middle of a function. */
  3894. if (NEXT_INSN (BB_END (a)) != BB_HEAD (b))
  3895. {
  3896. edge e = find_fallthru_edge (b->succs);
  3897. if (e && e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
  3898. return false;
  3899. }
  3900. /* There must be exactly one edge in between the blocks. */
  3901. return (single_succ_p (a)
  3902. && single_succ (a) == b
  3903. && single_pred_p (b) == 1
  3904. && a != b
  3905. /* Must be simple edge. */
  3906. && !(single_succ_edge (a)->flags & EDGE_COMPLEX)
  3907. && a != ENTRY_BLOCK_PTR_FOR_FN (cfun)
  3908. && b != EXIT_BLOCK_PTR_FOR_FN (cfun)
  3909. /* If the jump insn has side effects, we can't kill the edge.
  3910. When not optimizing, try_redirect_by_replacing_jump will
  3911. not allow us to redirect an edge by replacing a table jump. */
  3912. && (!JUMP_P (BB_END (a))
  3913. || ((!optimize || reload_completed)
  3914. ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a)))));
  3915. }
  3916. /* Merge block A and B. The blocks must be mergeable. */
  3917. static void
  3918. cfg_layout_merge_blocks (basic_block a, basic_block b)
  3919. {
  3920. bool forwarder_p = (b->flags & BB_FORWARDER_BLOCK) != 0;
  3921. rtx_insn *insn;
  3922. gcc_checking_assert (cfg_layout_can_merge_blocks_p (a, b));
  3923. if (dump_file)
  3924. fprintf (dump_file, "Merging block %d into block %d...\n", b->index,
  3925. a->index);
  3926. /* If there was a CODE_LABEL beginning B, delete it. */
  3927. if (LABEL_P (BB_HEAD (b)))
  3928. {
  3929. delete_insn (BB_HEAD (b));
  3930. }
  3931. /* We should have fallthru edge in a, or we can do dummy redirection to get
  3932. it cleaned up. */
  3933. if (JUMP_P (BB_END (a)))
  3934. try_redirect_by_replacing_jump (EDGE_SUCC (a, 0), b, true);
  3935. gcc_assert (!JUMP_P (BB_END (a)));
  3936. /* When not optimizing and the edge is the only place in RTL which holds
  3937. some unique locus, emit a nop with that locus in between. */
  3938. if (!optimize)
  3939. emit_nop_for_unique_locus_between (a, b);
  3940. /* Move things from b->footer after a->footer. */
  3941. if (BB_FOOTER (b))
  3942. {
  3943. if (!BB_FOOTER (a))
  3944. BB_FOOTER (a) = BB_FOOTER (b);
  3945. else
  3946. {
  3947. rtx_insn *last = BB_FOOTER (a);
  3948. while (NEXT_INSN (last))
  3949. last = NEXT_INSN (last);
  3950. SET_NEXT_INSN (last) = BB_FOOTER (b);
  3951. SET_PREV_INSN (BB_FOOTER (b)) = last;
  3952. }
  3953. BB_FOOTER (b) = NULL;
  3954. }
  3955. /* Move things from b->header before a->footer.
  3956. Note that this may include dead tablejump data, but we don't clean
  3957. those up until we go out of cfglayout mode. */
  3958. if (BB_HEADER (b))
  3959. {
  3960. if (! BB_FOOTER (a))
  3961. BB_FOOTER (a) = BB_HEADER (b);
  3962. else
  3963. {
  3964. rtx_insn *last = BB_HEADER (b);
  3965. while (NEXT_INSN (last))
  3966. last = NEXT_INSN (last);
  3967. SET_NEXT_INSN (last) = BB_FOOTER (a);
  3968. SET_PREV_INSN (BB_FOOTER (a)) = last;
  3969. BB_FOOTER (a) = BB_HEADER (b);
  3970. }
  3971. BB_HEADER (b) = NULL;
  3972. }
  3973. /* In the case basic blocks are not adjacent, move them around. */
  3974. if (NEXT_INSN (BB_END (a)) != BB_HEAD (b))
  3975. {
  3976. insn = unlink_insn_chain (BB_HEAD (b), BB_END (b));
  3977. emit_insn_after_noloc (insn, BB_END (a), a);
  3978. }
  3979. /* Otherwise just re-associate the instructions. */
  3980. else
  3981. {
  3982. insn = BB_HEAD (b);
  3983. BB_END (a) = BB_END (b);
  3984. }
  3985. /* emit_insn_after_noloc doesn't call df_insn_change_bb.
  3986. We need to explicitly call. */
  3987. update_bb_for_insn_chain (insn, BB_END (b), a);
  3988. /* Skip possible DELETED_LABEL insn. */
  3989. if (!NOTE_INSN_BASIC_BLOCK_P (insn))
  3990. insn = NEXT_INSN (insn);
  3991. gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn));
  3992. BB_HEAD (b) = BB_END (b) = NULL;
  3993. delete_insn (insn);
  3994. df_bb_delete (b->index);
  3995. /* If B was a forwarder block, propagate the locus on the edge. */
  3996. if (forwarder_p
  3997. && LOCATION_LOCUS (EDGE_SUCC (b, 0)->goto_locus) == UNKNOWN_LOCATION)
  3998. EDGE_SUCC (b, 0)->goto_locus = EDGE_SUCC (a, 0)->goto_locus;
  3999. if (dump_file)
  4000. fprintf (dump_file, "Merged blocks %d and %d.\n", a->index, b->index);
  4001. }
  4002. /* Split edge E. */
  4003. static basic_block
  4004. cfg_layout_split_edge (edge e)
  4005. {
  4006. basic_block new_bb =
  4007. create_basic_block (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
  4008. ? NEXT_INSN (BB_END (e->src)) : get_insns (),
  4009. NULL_RTX, e->src);
  4010. if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
  4011. BB_COPY_PARTITION (new_bb, e->src);
  4012. else
  4013. BB_COPY_PARTITION (new_bb, e->dest);
  4014. make_edge (new_bb, e->dest, EDGE_FALLTHRU);
  4015. redirect_edge_and_branch_force (e, new_bb);
  4016. return new_bb;
  4017. }
  4018. /* Do postprocessing after making a forwarder block joined by edge FALLTHRU. */
  4019. static void
  4020. rtl_make_forwarder_block (edge fallthru ATTRIBUTE_UNUSED)
  4021. {
  4022. }
  4023. /* Return true if BB contains only labels or non-executable
  4024. instructions. */
  4025. static bool
  4026. rtl_block_empty_p (basic_block bb)
  4027. {
  4028. rtx_insn *insn;
  4029. if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun)
  4030. || bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
  4031. return true;
  4032. FOR_BB_INSNS (bb, insn)
  4033. if (NONDEBUG_INSN_P (insn) && !any_uncondjump_p (insn))
  4034. return false;
  4035. return true;
  4036. }
  4037. /* Split a basic block if it ends with a conditional branch and if
  4038. the other part of the block is not empty. */
  4039. static basic_block
  4040. rtl_split_block_before_cond_jump (basic_block bb)
  4041. {
  4042. rtx_insn *insn;
  4043. rtx_insn *split_point = NULL;
  4044. rtx_insn *last = NULL;
  4045. bool found_code = false;
  4046. FOR_BB_INSNS (bb, insn)
  4047. {
  4048. if (any_condjump_p (insn))
  4049. split_point = last;
  4050. else if (NONDEBUG_INSN_P (insn))
  4051. found_code = true;
  4052. last = insn;
  4053. }
  4054. /* Did not find everything. */
  4055. if (found_code && split_point)
  4056. return split_block (bb, split_point)->dest;
  4057. else
  4058. return NULL;
  4059. }
  4060. /* Return 1 if BB ends with a call, possibly followed by some
  4061. instructions that must stay with the call, 0 otherwise. */
  4062. static bool
  4063. rtl_block_ends_with_call_p (basic_block bb)
  4064. {
  4065. rtx_insn *insn = BB_END (bb);
  4066. while (!CALL_P (insn)
  4067. && insn != BB_HEAD (bb)
  4068. && (keep_with_call_p (insn)
  4069. || NOTE_P (insn)
  4070. || DEBUG_INSN_P (insn)))
  4071. insn = PREV_INSN (insn);
  4072. return (CALL_P (insn));
  4073. }
  4074. /* Return 1 if BB ends with a conditional branch, 0 otherwise. */
  4075. static bool
  4076. rtl_block_ends_with_condjump_p (const_basic_block bb)
  4077. {
  4078. return any_condjump_p (BB_END (bb));
  4079. }
  4080. /* Return true if we need to add fake edge to exit.
  4081. Helper function for rtl_flow_call_edges_add. */
  4082. static bool
  4083. need_fake_edge_p (const rtx_insn *insn)
  4084. {
  4085. if (!INSN_P (insn))
  4086. return false;
  4087. if ((CALL_P (insn)
  4088. && !SIBLING_CALL_P (insn)
  4089. && !find_reg_note (insn, REG_NORETURN, NULL)
  4090. && !(RTL_CONST_OR_PURE_CALL_P (insn))))
  4091. return true;
  4092. return ((GET_CODE (PATTERN (insn)) == ASM_OPERANDS
  4093. && MEM_VOLATILE_P (PATTERN (insn)))
  4094. || (GET_CODE (PATTERN (insn)) == PARALLEL
  4095. && asm_noperands (insn) != -1
  4096. && MEM_VOLATILE_P (XVECEXP (PATTERN (insn), 0, 0)))
  4097. || GET_CODE (PATTERN (insn)) == ASM_INPUT);
  4098. }
  4099. /* Add fake edges to the function exit for any non constant and non noreturn
  4100. calls, volatile inline assembly in the bitmap of blocks specified by
  4101. BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks
  4102. that were split.
  4103. The goal is to expose cases in which entering a basic block does not imply
  4104. that all subsequent instructions must be executed. */
  4105. static int
  4106. rtl_flow_call_edges_add (sbitmap blocks)
  4107. {
  4108. int i;
  4109. int blocks_split = 0;
  4110. int last_bb = last_basic_block_for_fn (cfun);
  4111. bool check_last_block = false;
  4112. if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS)
  4113. return 0;
  4114. if (! blocks)
  4115. check_last_block = true;
  4116. else
  4117. check_last_block = bitmap_bit_p (blocks,
  4118. EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb->index);
  4119. /* In the last basic block, before epilogue generation, there will be
  4120. a fallthru edge to EXIT. Special care is required if the last insn
  4121. of the last basic block is a call because make_edge folds duplicate
  4122. edges, which would result in the fallthru edge also being marked
  4123. fake, which would result in the fallthru edge being removed by
  4124. remove_fake_edges, which would result in an invalid CFG.
  4125. Moreover, we can't elide the outgoing fake edge, since the block
  4126. profiler needs to take this into account in order to solve the minimal
  4127. spanning tree in the case that the call doesn't return.
  4128. Handle this by adding a dummy instruction in a new last basic block. */
  4129. if (check_last_block)
  4130. {
  4131. basic_block bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
  4132. rtx_insn *insn = BB_END (bb);
  4133. /* Back up past insns that must be kept in the same block as a call. */
  4134. while (insn != BB_HEAD (bb)
  4135. && keep_with_call_p (insn))
  4136. insn = PREV_INSN (insn);
  4137. if (need_fake_edge_p (insn))
  4138. {
  4139. edge e;
  4140. e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun));
  4141. if (e)
  4142. {
  4143. insert_insn_on_edge (gen_use (const0_rtx), e);
  4144. commit_edge_insertions ();
  4145. }
  4146. }
  4147. }
  4148. /* Now add fake edges to the function exit for any non constant
  4149. calls since there is no way that we can determine if they will
  4150. return or not... */
  4151. for (i = NUM_FIXED_BLOCKS; i < last_bb; i++)
  4152. {
  4153. basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
  4154. rtx_insn *insn;
  4155. rtx_insn *prev_insn;
  4156. if (!bb)
  4157. continue;
  4158. if (blocks && !bitmap_bit_p (blocks, i))
  4159. continue;
  4160. for (insn = BB_END (bb); ; insn = prev_insn)
  4161. {
  4162. prev_insn = PREV_INSN (insn);
  4163. if (need_fake_edge_p (insn))
  4164. {
  4165. edge e;
  4166. rtx_insn *split_at_insn = insn;
  4167. /* Don't split the block between a call and an insn that should
  4168. remain in the same block as the call. */
  4169. if (CALL_P (insn))
  4170. while (split_at_insn != BB_END (bb)
  4171. && keep_with_call_p (NEXT_INSN (split_at_insn)))
  4172. split_at_insn = NEXT_INSN (split_at_insn);
  4173. /* The handling above of the final block before the epilogue
  4174. should be enough to verify that there is no edge to the exit
  4175. block in CFG already. Calling make_edge in such case would
  4176. cause us to mark that edge as fake and remove it later. */
  4177. #ifdef ENABLE_CHECKING
  4178. if (split_at_insn == BB_END (bb))
  4179. {
  4180. e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun));
  4181. gcc_assert (e == NULL);
  4182. }
  4183. #endif
  4184. /* Note that the following may create a new basic block
  4185. and renumber the existing basic blocks. */
  4186. if (split_at_insn != BB_END (bb))
  4187. {
  4188. e = split_block (bb, split_at_insn);
  4189. if (e)
  4190. blocks_split++;
  4191. }
  4192. make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FAKE);
  4193. }
  4194. if (insn == BB_HEAD (bb))
  4195. break;
  4196. }
  4197. }
  4198. if (blocks_split)
  4199. verify_flow_info ();
  4200. return blocks_split;
  4201. }
  4202. /* Add COMP_RTX as a condition at end of COND_BB. FIRST_HEAD is
  4203. the conditional branch target, SECOND_HEAD should be the fall-thru
  4204. there is no need to handle this here the loop versioning code handles
  4205. this. the reason for SECON_HEAD is that it is needed for condition
  4206. in trees, and this should be of the same type since it is a hook. */
  4207. static void
  4208. rtl_lv_add_condition_to_bb (basic_block first_head ,
  4209. basic_block second_head ATTRIBUTE_UNUSED,
  4210. basic_block cond_bb, void *comp_rtx)
  4211. {
  4212. rtx label;
  4213. rtx_insn *seq, *jump;
  4214. rtx op0 = XEXP ((rtx)comp_rtx, 0);
  4215. rtx op1 = XEXP ((rtx)comp_rtx, 1);
  4216. enum rtx_code comp = GET_CODE ((rtx)comp_rtx);
  4217. machine_mode mode;
  4218. label = block_label (first_head);
  4219. mode = GET_MODE (op0);
  4220. if (mode == VOIDmode)
  4221. mode = GET_MODE (op1);
  4222. start_sequence ();
  4223. op0 = force_operand (op0, NULL_RTX);
  4224. op1 = force_operand (op1, NULL_RTX);
  4225. do_compare_rtx_and_jump (op0, op1, comp, 0,
  4226. mode, NULL_RTX, NULL_RTX, label, -1);
  4227. jump = get_last_insn ();
  4228. JUMP_LABEL (jump) = label;
  4229. LABEL_NUSES (label)++;
  4230. seq = get_insns ();
  4231. end_sequence ();
  4232. /* Add the new cond, in the new head. */
  4233. emit_insn_after (seq, BB_END (cond_bb));
  4234. }
  4235. /* Given a block B with unconditional branch at its end, get the
  4236. store the return the branch edge and the fall-thru edge in
  4237. BRANCH_EDGE and FALLTHRU_EDGE respectively. */
  4238. static void
  4239. rtl_extract_cond_bb_edges (basic_block b, edge *branch_edge,
  4240. edge *fallthru_edge)
  4241. {
  4242. edge e = EDGE_SUCC (b, 0);
  4243. if (e->flags & EDGE_FALLTHRU)
  4244. {
  4245. *fallthru_edge = e;
  4246. *branch_edge = EDGE_SUCC (b, 1);
  4247. }
  4248. else
  4249. {
  4250. *branch_edge = e;
  4251. *fallthru_edge = EDGE_SUCC (b, 1);
  4252. }
  4253. }
  4254. void
  4255. init_rtl_bb_info (basic_block bb)
  4256. {
  4257. gcc_assert (!bb->il.x.rtl);
  4258. bb->il.x.head_ = NULL;
  4259. bb->il.x.rtl = ggc_cleared_alloc<rtl_bb_info> ();
  4260. }
  4261. /* Returns true if it is possible to remove edge E by redirecting
  4262. it to the destination of the other edge from E->src. */
  4263. static bool
  4264. rtl_can_remove_branch_p (const_edge e)
  4265. {
  4266. const_basic_block src = e->src;
  4267. const_basic_block target = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest;
  4268. const rtx_insn *insn = BB_END (src);
  4269. rtx set;
  4270. /* The conditions are taken from try_redirect_by_replacing_jump. */
  4271. if (target == EXIT_BLOCK_PTR_FOR_FN (cfun))
  4272. return false;
  4273. if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
  4274. return false;
  4275. if (BB_PARTITION (src) != BB_PARTITION (target))
  4276. return false;
  4277. if (!onlyjump_p (insn)
  4278. || tablejump_p (insn, NULL, NULL))
  4279. return false;
  4280. set = single_set (insn);
  4281. if (!set || side_effects_p (set))
  4282. return false;
  4283. return true;
  4284. }
  4285. static basic_block
  4286. rtl_duplicate_bb (basic_block bb)
  4287. {
  4288. bb = cfg_layout_duplicate_bb (bb);
  4289. bb->aux = NULL;
  4290. return bb;
  4291. }
  4292. /* Do book-keeping of basic block BB for the profile consistency checker.
  4293. If AFTER_PASS is 0, do pre-pass accounting, or if AFTER_PASS is 1
  4294. then do post-pass accounting. Store the counting in RECORD. */
  4295. static void
  4296. rtl_account_profile_record (basic_block bb, int after_pass,
  4297. struct profile_record *record)
  4298. {
  4299. rtx_insn *insn;
  4300. FOR_BB_INSNS (bb, insn)
  4301. if (INSN_P (insn))
  4302. {
  4303. record->size[after_pass]
  4304. += insn_rtx_cost (PATTERN (insn), false);
  4305. if (profile_status_for_fn (cfun) == PROFILE_READ)
  4306. record->time[after_pass]
  4307. += insn_rtx_cost (PATTERN (insn), true) * bb->count;
  4308. else if (profile_status_for_fn (cfun) == PROFILE_GUESSED)
  4309. record->time[after_pass]
  4310. += insn_rtx_cost (PATTERN (insn), true) * bb->frequency;
  4311. }
  4312. }
  4313. /* Implementation of CFG manipulation for linearized RTL. */
  4314. struct cfg_hooks rtl_cfg_hooks = {
  4315. "rtl",
  4316. rtl_verify_flow_info,
  4317. rtl_dump_bb,
  4318. rtl_dump_bb_for_graph,
  4319. rtl_create_basic_block,
  4320. rtl_redirect_edge_and_branch,
  4321. rtl_redirect_edge_and_branch_force,
  4322. rtl_can_remove_branch_p,
  4323. rtl_delete_block,
  4324. rtl_split_block,
  4325. rtl_move_block_after,
  4326. rtl_can_merge_blocks, /* can_merge_blocks_p */
  4327. rtl_merge_blocks,
  4328. rtl_predict_edge,
  4329. rtl_predicted_by_p,
  4330. cfg_layout_can_duplicate_bb_p,
  4331. rtl_duplicate_bb,
  4332. rtl_split_edge,
  4333. rtl_make_forwarder_block,
  4334. rtl_tidy_fallthru_edge,
  4335. rtl_force_nonfallthru,
  4336. rtl_block_ends_with_call_p,
  4337. rtl_block_ends_with_condjump_p,
  4338. rtl_flow_call_edges_add,
  4339. NULL, /* execute_on_growing_pred */
  4340. NULL, /* execute_on_shrinking_pred */
  4341. NULL, /* duplicate loop for trees */
  4342. NULL, /* lv_add_condition_to_bb */
  4343. NULL, /* lv_adjust_loop_header_phi*/
  4344. NULL, /* extract_cond_bb_edges */
  4345. NULL, /* flush_pending_stmts */
  4346. rtl_block_empty_p, /* block_empty_p */
  4347. rtl_split_block_before_cond_jump, /* split_block_before_cond_jump */
  4348. rtl_account_profile_record,
  4349. };
  4350. /* Implementation of CFG manipulation for cfg layout RTL, where
  4351. basic block connected via fallthru edges does not have to be adjacent.
  4352. This representation will hopefully become the default one in future
  4353. version of the compiler. */
  4354. struct cfg_hooks cfg_layout_rtl_cfg_hooks = {
  4355. "cfglayout mode",
  4356. rtl_verify_flow_info_1,
  4357. rtl_dump_bb,
  4358. rtl_dump_bb_for_graph,
  4359. cfg_layout_create_basic_block,
  4360. cfg_layout_redirect_edge_and_branch,
  4361. cfg_layout_redirect_edge_and_branch_force,
  4362. rtl_can_remove_branch_p,
  4363. cfg_layout_delete_block,
  4364. cfg_layout_split_block,
  4365. rtl_move_block_after,
  4366. cfg_layout_can_merge_blocks_p,
  4367. cfg_layout_merge_blocks,
  4368. rtl_predict_edge,
  4369. rtl_predicted_by_p,
  4370. cfg_layout_can_duplicate_bb_p,
  4371. cfg_layout_duplicate_bb,
  4372. cfg_layout_split_edge,
  4373. rtl_make_forwarder_block,
  4374. NULL, /* tidy_fallthru_edge */
  4375. rtl_force_nonfallthru,
  4376. rtl_block_ends_with_call_p,
  4377. rtl_block_ends_with_condjump_p,
  4378. rtl_flow_call_edges_add,
  4379. NULL, /* execute_on_growing_pred */
  4380. NULL, /* execute_on_shrinking_pred */
  4381. duplicate_loop_to_header_edge, /* duplicate loop for trees */
  4382. rtl_lv_add_condition_to_bb, /* lv_add_condition_to_bb */
  4383. NULL, /* lv_adjust_loop_header_phi*/
  4384. rtl_extract_cond_bb_edges, /* extract_cond_bb_edges */
  4385. NULL, /* flush_pending_stmts */
  4386. rtl_block_empty_p, /* block_empty_p */
  4387. rtl_split_block_before_cond_jump, /* split_block_before_cond_jump */
  4388. rtl_account_profile_record,
  4389. };
  4390. #include "gt-cfgrtl.h"