tree-cfg.c 234 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843
  1. /* Control flow functions for trees.
  2. Copyright (C) 2001-2015 Free Software Foundation, Inc.
  3. Contributed by Diego Novillo <dnovillo@redhat.com>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "hash-table.h"
  20. #include "hash-map.h"
  21. #include "tm.h"
  22. #include "hash-set.h"
  23. #include "machmode.h"
  24. #include "vec.h"
  25. #include "double-int.h"
  26. #include "input.h"
  27. #include "alias.h"
  28. #include "symtab.h"
  29. #include "wide-int.h"
  30. #include "inchash.h"
  31. #include "tree.h"
  32. #include "fold-const.h"
  33. #include "trans-mem.h"
  34. #include "stor-layout.h"
  35. #include "print-tree.h"
  36. #include "tm_p.h"
  37. #include "predict.h"
  38. #include "hard-reg-set.h"
  39. #include "function.h"
  40. #include "dominance.h"
  41. #include "cfg.h"
  42. #include "cfganal.h"
  43. #include "basic-block.h"
  44. #include "flags.h"
  45. #include "gimple-pretty-print.h"
  46. #include "tree-ssa-alias.h"
  47. #include "internal-fn.h"
  48. #include "gimple-fold.h"
  49. #include "tree-eh.h"
  50. #include "gimple-expr.h"
  51. #include "is-a.h"
  52. #include "gimple.h"
  53. #include "gimple-iterator.h"
  54. #include "gimplify-me.h"
  55. #include "gimple-walk.h"
  56. #include "gimple-ssa.h"
  57. #include "plugin-api.h"
  58. #include "ipa-ref.h"
  59. #include "cgraph.h"
  60. #include "tree-cfg.h"
  61. #include "tree-phinodes.h"
  62. #include "ssa-iterators.h"
  63. #include "stringpool.h"
  64. #include "tree-ssanames.h"
  65. #include "tree-ssa-loop-manip.h"
  66. #include "tree-ssa-loop-niter.h"
  67. #include "tree-into-ssa.h"
  68. #include "hashtab.h"
  69. #include "rtl.h"
  70. #include "statistics.h"
  71. #include "real.h"
  72. #include "fixed-value.h"
  73. #include "insn-config.h"
  74. #include "expmed.h"
  75. #include "dojump.h"
  76. #include "explow.h"
  77. #include "calls.h"
  78. #include "emit-rtl.h"
  79. #include "varasm.h"
  80. #include "stmt.h"
  81. #include "expr.h"
  82. #include "tree-dfa.h"
  83. #include "tree-ssa.h"
  84. #include "tree-dump.h"
  85. #include "tree-pass.h"
  86. #include "diagnostic-core.h"
  87. #include "except.h"
  88. #include "cfgloop.h"
  89. #include "tree-ssa-propagate.h"
  90. #include "value-prof.h"
  91. #include "tree-inline.h"
  92. #include "target.h"
  93. #include "tree-ssa-live.h"
  94. #include "omp-low.h"
  95. #include "tree-cfgcleanup.h"
  96. #include "wide-int-print.h"
  97. /* This file contains functions for building the Control Flow Graph (CFG)
  98. for a function tree. */
  99. /* Local declarations. */
  100. /* Initial capacity for the basic block array. */
  101. static const int initial_cfg_capacity = 20;
  102. /* This hash table allows us to efficiently lookup all CASE_LABEL_EXPRs
  103. which use a particular edge. The CASE_LABEL_EXPRs are chained together
  104. via their CASE_CHAIN field, which we clear after we're done with the
  105. hash table to prevent problems with duplication of GIMPLE_SWITCHes.
  106. Access to this list of CASE_LABEL_EXPRs allows us to efficiently
  107. update the case vector in response to edge redirections.
  108. Right now this table is set up and torn down at key points in the
  109. compilation process. It would be nice if we could make the table
  110. more persistent. The key is getting notification of changes to
  111. the CFG (particularly edge removal, creation and redirection). */
  112. static hash_map<edge, tree> *edge_to_cases;
  113. /* If we record edge_to_cases, this bitmap will hold indexes
  114. of basic blocks that end in a GIMPLE_SWITCH which we touched
  115. due to edge manipulations. */
  116. static bitmap touched_switch_bbs;
  117. /* CFG statistics. */
  118. struct cfg_stats_d
  119. {
  120. long num_merged_labels;
  121. };
  122. static struct cfg_stats_d cfg_stats;
  123. /* Hash table to store last discriminator assigned for each locus. */
  124. struct locus_discrim_map
  125. {
  126. location_t locus;
  127. int discriminator;
  128. };
  129. /* Hashtable helpers. */
  130. struct locus_discrim_hasher : typed_free_remove <locus_discrim_map>
  131. {
  132. typedef locus_discrim_map value_type;
  133. typedef locus_discrim_map compare_type;
  134. static inline hashval_t hash (const value_type *);
  135. static inline bool equal (const value_type *, const compare_type *);
  136. };
  137. /* Trivial hash function for a location_t. ITEM is a pointer to
  138. a hash table entry that maps a location_t to a discriminator. */
  139. inline hashval_t
  140. locus_discrim_hasher::hash (const value_type *item)
  141. {
  142. return LOCATION_LINE (item->locus);
  143. }
  144. /* Equality function for the locus-to-discriminator map. A and B
  145. point to the two hash table entries to compare. */
  146. inline bool
  147. locus_discrim_hasher::equal (const value_type *a, const compare_type *b)
  148. {
  149. return LOCATION_LINE (a->locus) == LOCATION_LINE (b->locus);
  150. }
  151. static hash_table<locus_discrim_hasher> *discriminator_per_locus;
  152. /* Basic blocks and flowgraphs. */
  153. static void make_blocks (gimple_seq);
  154. /* Edges. */
  155. static void make_edges (void);
  156. static void assign_discriminators (void);
  157. static void make_cond_expr_edges (basic_block);
  158. static void make_gimple_switch_edges (gswitch *, basic_block);
  159. static bool make_goto_expr_edges (basic_block);
  160. static void make_gimple_asm_edges (basic_block);
  161. static edge gimple_redirect_edge_and_branch (edge, basic_block);
  162. static edge gimple_try_redirect_by_replacing_jump (edge, basic_block);
  163. /* Various helpers. */
  164. static inline bool stmt_starts_bb_p (gimple, gimple);
  165. static int gimple_verify_flow_info (void);
  166. static void gimple_make_forwarder_block (edge);
  167. static gimple first_non_label_stmt (basic_block);
  168. static bool verify_gimple_transaction (gtransaction *);
  169. static bool call_can_make_abnormal_goto (gimple);
  170. /* Flowgraph optimization and cleanup. */
  171. static void gimple_merge_blocks (basic_block, basic_block);
  172. static bool gimple_can_merge_blocks_p (basic_block, basic_block);
  173. static void remove_bb (basic_block);
  174. static edge find_taken_edge_computed_goto (basic_block, tree);
  175. static edge find_taken_edge_cond_expr (basic_block, tree);
  176. static edge find_taken_edge_switch_expr (gswitch *, basic_block, tree);
  177. static tree find_case_label_for_value (gswitch *, tree);
  178. void
  179. init_empty_tree_cfg_for_function (struct function *fn)
  180. {
  181. /* Initialize the basic block array. */
  182. init_flow (fn);
  183. profile_status_for_fn (fn) = PROFILE_ABSENT;
  184. n_basic_blocks_for_fn (fn) = NUM_FIXED_BLOCKS;
  185. last_basic_block_for_fn (fn) = NUM_FIXED_BLOCKS;
  186. vec_alloc (basic_block_info_for_fn (fn), initial_cfg_capacity);
  187. vec_safe_grow_cleared (basic_block_info_for_fn (fn),
  188. initial_cfg_capacity);
  189. /* Build a mapping of labels to their associated blocks. */
  190. vec_alloc (label_to_block_map_for_fn (fn), initial_cfg_capacity);
  191. vec_safe_grow_cleared (label_to_block_map_for_fn (fn),
  192. initial_cfg_capacity);
  193. SET_BASIC_BLOCK_FOR_FN (fn, ENTRY_BLOCK, ENTRY_BLOCK_PTR_FOR_FN (fn));
  194. SET_BASIC_BLOCK_FOR_FN (fn, EXIT_BLOCK, EXIT_BLOCK_PTR_FOR_FN (fn));
  195. ENTRY_BLOCK_PTR_FOR_FN (fn)->next_bb
  196. = EXIT_BLOCK_PTR_FOR_FN (fn);
  197. EXIT_BLOCK_PTR_FOR_FN (fn)->prev_bb
  198. = ENTRY_BLOCK_PTR_FOR_FN (fn);
  199. }
  200. void
  201. init_empty_tree_cfg (void)
  202. {
  203. init_empty_tree_cfg_for_function (cfun);
  204. }
  205. /*---------------------------------------------------------------------------
  206. Create basic blocks
  207. ---------------------------------------------------------------------------*/
  208. /* Entry point to the CFG builder for trees. SEQ is the sequence of
  209. statements to be added to the flowgraph. */
  210. static void
  211. build_gimple_cfg (gimple_seq seq)
  212. {
  213. /* Register specific gimple functions. */
  214. gimple_register_cfg_hooks ();
  215. memset ((void *) &cfg_stats, 0, sizeof (cfg_stats));
  216. init_empty_tree_cfg ();
  217. make_blocks (seq);
  218. /* Make sure there is always at least one block, even if it's empty. */
  219. if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS)
  220. create_empty_bb (ENTRY_BLOCK_PTR_FOR_FN (cfun));
  221. /* Adjust the size of the array. */
  222. if (basic_block_info_for_fn (cfun)->length ()
  223. < (size_t) n_basic_blocks_for_fn (cfun))
  224. vec_safe_grow_cleared (basic_block_info_for_fn (cfun),
  225. n_basic_blocks_for_fn (cfun));
  226. /* To speed up statement iterator walks, we first purge dead labels. */
  227. cleanup_dead_labels ();
  228. /* Group case nodes to reduce the number of edges.
  229. We do this after cleaning up dead labels because otherwise we miss
  230. a lot of obvious case merging opportunities. */
  231. group_case_labels ();
  232. /* Create the edges of the flowgraph. */
  233. discriminator_per_locus = new hash_table<locus_discrim_hasher> (13);
  234. make_edges ();
  235. assign_discriminators ();
  236. cleanup_dead_labels ();
  237. delete discriminator_per_locus;
  238. discriminator_per_locus = NULL;
  239. }
  240. /* Look for ANNOTATE calls with loop annotation kind in BB; if found, remove
  241. them and propagate the information to LOOP. We assume that the annotations
  242. come immediately before the condition in BB, if any. */
  243. static void
  244. replace_loop_annotate_in_block (basic_block bb, struct loop *loop)
  245. {
  246. gimple_stmt_iterator gsi = gsi_last_bb (bb);
  247. gimple stmt = gsi_stmt (gsi);
  248. if (!(stmt && gimple_code (stmt) == GIMPLE_COND))
  249. return;
  250. for (gsi_prev_nondebug (&gsi); !gsi_end_p (gsi); gsi_prev (&gsi))
  251. {
  252. stmt = gsi_stmt (gsi);
  253. if (gimple_code (stmt) != GIMPLE_CALL)
  254. break;
  255. if (!gimple_call_internal_p (stmt)
  256. || gimple_call_internal_fn (stmt) != IFN_ANNOTATE)
  257. break;
  258. switch ((annot_expr_kind) tree_to_shwi (gimple_call_arg (stmt, 1)))
  259. {
  260. case annot_expr_ivdep_kind:
  261. loop->safelen = INT_MAX;
  262. break;
  263. case annot_expr_no_vector_kind:
  264. loop->dont_vectorize = true;
  265. break;
  266. case annot_expr_vector_kind:
  267. loop->force_vectorize = true;
  268. cfun->has_force_vectorize_loops = true;
  269. break;
  270. default:
  271. gcc_unreachable ();
  272. }
  273. stmt = gimple_build_assign (gimple_call_lhs (stmt),
  274. gimple_call_arg (stmt, 0));
  275. gsi_replace (&gsi, stmt, true);
  276. }
  277. }
  278. /* Look for ANNOTATE calls with loop annotation kind; if found, remove
  279. them and propagate the information to the loop. We assume that the
  280. annotations come immediately before the condition of the loop. */
  281. static void
  282. replace_loop_annotate (void)
  283. {
  284. struct loop *loop;
  285. basic_block bb;
  286. gimple_stmt_iterator gsi;
  287. gimple stmt;
  288. FOR_EACH_LOOP (loop, 0)
  289. {
  290. /* First look into the header. */
  291. replace_loop_annotate_in_block (loop->header, loop);
  292. /* Then look into the latch, if any. */
  293. if (loop->latch)
  294. replace_loop_annotate_in_block (loop->latch, loop);
  295. }
  296. /* Remove IFN_ANNOTATE. Safeguard for the case loop->latch == NULL. */
  297. FOR_EACH_BB_FN (bb, cfun)
  298. {
  299. for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
  300. {
  301. stmt = gsi_stmt (gsi);
  302. if (gimple_code (stmt) != GIMPLE_CALL)
  303. continue;
  304. if (!gimple_call_internal_p (stmt)
  305. || gimple_call_internal_fn (stmt) != IFN_ANNOTATE)
  306. continue;
  307. switch ((annot_expr_kind) tree_to_shwi (gimple_call_arg (stmt, 1)))
  308. {
  309. case annot_expr_ivdep_kind:
  310. case annot_expr_no_vector_kind:
  311. case annot_expr_vector_kind:
  312. break;
  313. default:
  314. gcc_unreachable ();
  315. }
  316. warning_at (gimple_location (stmt), 0, "ignoring loop annotation");
  317. stmt = gimple_build_assign (gimple_call_lhs (stmt),
  318. gimple_call_arg (stmt, 0));
  319. gsi_replace (&gsi, stmt, true);
  320. }
  321. }
  322. }
  323. static unsigned int
  324. execute_build_cfg (void)
  325. {
  326. gimple_seq body = gimple_body (current_function_decl);
  327. build_gimple_cfg (body);
  328. gimple_set_body (current_function_decl, NULL);
  329. if (dump_file && (dump_flags & TDF_DETAILS))
  330. {
  331. fprintf (dump_file, "Scope blocks:\n");
  332. dump_scope_blocks (dump_file, dump_flags);
  333. }
  334. cleanup_tree_cfg ();
  335. loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
  336. replace_loop_annotate ();
  337. return 0;
  338. }
  339. namespace {
  340. const pass_data pass_data_build_cfg =
  341. {
  342. GIMPLE_PASS, /* type */
  343. "cfg", /* name */
  344. OPTGROUP_NONE, /* optinfo_flags */
  345. TV_TREE_CFG, /* tv_id */
  346. PROP_gimple_leh, /* properties_required */
  347. ( PROP_cfg | PROP_loops ), /* properties_provided */
  348. 0, /* properties_destroyed */
  349. 0, /* todo_flags_start */
  350. 0, /* todo_flags_finish */
  351. };
  352. class pass_build_cfg : public gimple_opt_pass
  353. {
  354. public:
  355. pass_build_cfg (gcc::context *ctxt)
  356. : gimple_opt_pass (pass_data_build_cfg, ctxt)
  357. {}
  358. /* opt_pass methods: */
  359. virtual unsigned int execute (function *) { return execute_build_cfg (); }
  360. }; // class pass_build_cfg
  361. } // anon namespace
  362. gimple_opt_pass *
  363. make_pass_build_cfg (gcc::context *ctxt)
  364. {
  365. return new pass_build_cfg (ctxt);
  366. }
  367. /* Return true if T is a computed goto. */
  368. bool
  369. computed_goto_p (gimple t)
  370. {
  371. return (gimple_code (t) == GIMPLE_GOTO
  372. && TREE_CODE (gimple_goto_dest (t)) != LABEL_DECL);
  373. }
  374. /* Returns true for edge E where e->src ends with a GIMPLE_COND and
  375. the other edge points to a bb with just __builtin_unreachable ().
  376. I.e. return true for C->M edge in:
  377. <bb C>:
  378. ...
  379. if (something)
  380. goto <bb N>;
  381. else
  382. goto <bb M>;
  383. <bb N>:
  384. __builtin_unreachable ();
  385. <bb M>: */
  386. bool
  387. assert_unreachable_fallthru_edge_p (edge e)
  388. {
  389. basic_block pred_bb = e->src;
  390. gimple last = last_stmt (pred_bb);
  391. if (last && gimple_code (last) == GIMPLE_COND)
  392. {
  393. basic_block other_bb = EDGE_SUCC (pred_bb, 0)->dest;
  394. if (other_bb == e->dest)
  395. other_bb = EDGE_SUCC (pred_bb, 1)->dest;
  396. if (EDGE_COUNT (other_bb->succs) == 0)
  397. {
  398. gimple_stmt_iterator gsi = gsi_after_labels (other_bb);
  399. gimple stmt;
  400. if (gsi_end_p (gsi))
  401. return false;
  402. stmt = gsi_stmt (gsi);
  403. while (is_gimple_debug (stmt) || gimple_clobber_p (stmt))
  404. {
  405. gsi_next (&gsi);
  406. if (gsi_end_p (gsi))
  407. return false;
  408. stmt = gsi_stmt (gsi);
  409. }
  410. return gimple_call_builtin_p (stmt, BUILT_IN_UNREACHABLE);
  411. }
  412. }
  413. return false;
  414. }
  415. /* Initialize GF_CALL_CTRL_ALTERING flag, which indicates the call
  416. could alter control flow except via eh. We initialize the flag at
  417. CFG build time and only ever clear it later. */
  418. static void
  419. gimple_call_initialize_ctrl_altering (gimple stmt)
  420. {
  421. int flags = gimple_call_flags (stmt);
  422. /* A call alters control flow if it can make an abnormal goto. */
  423. if (call_can_make_abnormal_goto (stmt)
  424. /* A call also alters control flow if it does not return. */
  425. || flags & ECF_NORETURN
  426. /* TM ending statements have backedges out of the transaction.
  427. Return true so we split the basic block containing them.
  428. Note that the TM_BUILTIN test is merely an optimization. */
  429. || ((flags & ECF_TM_BUILTIN)
  430. && is_tm_ending_fndecl (gimple_call_fndecl (stmt)))
  431. /* BUILT_IN_RETURN call is same as return statement. */
  432. || gimple_call_builtin_p (stmt, BUILT_IN_RETURN))
  433. gimple_call_set_ctrl_altering (stmt, true);
  434. else
  435. gimple_call_set_ctrl_altering (stmt, false);
  436. }
  437. /* Build a flowgraph for the sequence of stmts SEQ. */
  438. static void
  439. make_blocks (gimple_seq seq)
  440. {
  441. gimple_stmt_iterator i = gsi_start (seq);
  442. gimple stmt = NULL;
  443. bool start_new_block = true;
  444. bool first_stmt_of_seq = true;
  445. basic_block bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
  446. while (!gsi_end_p (i))
  447. {
  448. gimple prev_stmt;
  449. prev_stmt = stmt;
  450. stmt = gsi_stmt (i);
  451. if (stmt && is_gimple_call (stmt))
  452. gimple_call_initialize_ctrl_altering (stmt);
  453. /* If the statement starts a new basic block or if we have determined
  454. in a previous pass that we need to create a new block for STMT, do
  455. so now. */
  456. if (start_new_block || stmt_starts_bb_p (stmt, prev_stmt))
  457. {
  458. if (!first_stmt_of_seq)
  459. gsi_split_seq_before (&i, &seq);
  460. bb = create_basic_block (seq, NULL, bb);
  461. start_new_block = false;
  462. }
  463. /* Now add STMT to BB and create the subgraphs for special statement
  464. codes. */
  465. gimple_set_bb (stmt, bb);
  466. /* If STMT is a basic block terminator, set START_NEW_BLOCK for the
  467. next iteration. */
  468. if (stmt_ends_bb_p (stmt))
  469. {
  470. /* If the stmt can make abnormal goto use a new temporary
  471. for the assignment to the LHS. This makes sure the old value
  472. of the LHS is available on the abnormal edge. Otherwise
  473. we will end up with overlapping life-ranges for abnormal
  474. SSA names. */
  475. if (gimple_has_lhs (stmt)
  476. && stmt_can_make_abnormal_goto (stmt)
  477. && is_gimple_reg_type (TREE_TYPE (gimple_get_lhs (stmt))))
  478. {
  479. tree lhs = gimple_get_lhs (stmt);
  480. tree tmp = create_tmp_var (TREE_TYPE (lhs));
  481. gimple s = gimple_build_assign (lhs, tmp);
  482. gimple_set_location (s, gimple_location (stmt));
  483. gimple_set_block (s, gimple_block (stmt));
  484. gimple_set_lhs (stmt, tmp);
  485. if (TREE_CODE (TREE_TYPE (tmp)) == COMPLEX_TYPE
  486. || TREE_CODE (TREE_TYPE (tmp)) == VECTOR_TYPE)
  487. DECL_GIMPLE_REG_P (tmp) = 1;
  488. gsi_insert_after (&i, s, GSI_SAME_STMT);
  489. }
  490. start_new_block = true;
  491. }
  492. gsi_next (&i);
  493. first_stmt_of_seq = false;
  494. }
  495. }
  496. /* Create and return a new empty basic block after bb AFTER. */
  497. static basic_block
  498. create_bb (void *h, void *e, basic_block after)
  499. {
  500. basic_block bb;
  501. gcc_assert (!e);
  502. /* Create and initialize a new basic block. Since alloc_block uses
  503. GC allocation that clears memory to allocate a basic block, we do
  504. not have to clear the newly allocated basic block here. */
  505. bb = alloc_block ();
  506. bb->index = last_basic_block_for_fn (cfun);
  507. bb->flags = BB_NEW;
  508. set_bb_seq (bb, h ? (gimple_seq) h : NULL);
  509. /* Add the new block to the linked list of blocks. */
  510. link_block (bb, after);
  511. /* Grow the basic block array if needed. */
  512. if ((size_t) last_basic_block_for_fn (cfun)
  513. == basic_block_info_for_fn (cfun)->length ())
  514. {
  515. size_t new_size =
  516. (last_basic_block_for_fn (cfun)
  517. + (last_basic_block_for_fn (cfun) + 3) / 4);
  518. vec_safe_grow_cleared (basic_block_info_for_fn (cfun), new_size);
  519. }
  520. /* Add the newly created block to the array. */
  521. SET_BASIC_BLOCK_FOR_FN (cfun, last_basic_block_for_fn (cfun), bb);
  522. n_basic_blocks_for_fn (cfun)++;
  523. last_basic_block_for_fn (cfun)++;
  524. return bb;
  525. }
  526. /*---------------------------------------------------------------------------
  527. Edge creation
  528. ---------------------------------------------------------------------------*/
  529. /* Fold COND_EXPR_COND of each COND_EXPR. */
  530. void
  531. fold_cond_expr_cond (void)
  532. {
  533. basic_block bb;
  534. FOR_EACH_BB_FN (bb, cfun)
  535. {
  536. gimple stmt = last_stmt (bb);
  537. if (stmt && gimple_code (stmt) == GIMPLE_COND)
  538. {
  539. gcond *cond_stmt = as_a <gcond *> (stmt);
  540. location_t loc = gimple_location (stmt);
  541. tree cond;
  542. bool zerop, onep;
  543. fold_defer_overflow_warnings ();
  544. cond = fold_binary_loc (loc, gimple_cond_code (cond_stmt),
  545. boolean_type_node,
  546. gimple_cond_lhs (cond_stmt),
  547. gimple_cond_rhs (cond_stmt));
  548. if (cond)
  549. {
  550. zerop = integer_zerop (cond);
  551. onep = integer_onep (cond);
  552. }
  553. else
  554. zerop = onep = false;
  555. fold_undefer_overflow_warnings (zerop || onep,
  556. stmt,
  557. WARN_STRICT_OVERFLOW_CONDITIONAL);
  558. if (zerop)
  559. gimple_cond_make_false (cond_stmt);
  560. else if (onep)
  561. gimple_cond_make_true (cond_stmt);
  562. }
  563. }
  564. }
  565. /* If basic block BB has an abnormal edge to a basic block
  566. containing IFN_ABNORMAL_DISPATCHER internal call, return
  567. that the dispatcher's basic block, otherwise return NULL. */
  568. basic_block
  569. get_abnormal_succ_dispatcher (basic_block bb)
  570. {
  571. edge e;
  572. edge_iterator ei;
  573. FOR_EACH_EDGE (e, ei, bb->succs)
  574. if ((e->flags & (EDGE_ABNORMAL | EDGE_EH)) == EDGE_ABNORMAL)
  575. {
  576. gimple_stmt_iterator gsi
  577. = gsi_start_nondebug_after_labels_bb (e->dest);
  578. gimple g = gsi_stmt (gsi);
  579. if (g
  580. && is_gimple_call (g)
  581. && gimple_call_internal_p (g)
  582. && gimple_call_internal_fn (g) == IFN_ABNORMAL_DISPATCHER)
  583. return e->dest;
  584. }
  585. return NULL;
  586. }
  587. /* Helper function for make_edges. Create a basic block with
  588. with ABNORMAL_DISPATCHER internal call in it if needed, and
  589. create abnormal edges from BBS to it and from it to FOR_BB
  590. if COMPUTED_GOTO is false, otherwise factor the computed gotos. */
  591. static void
  592. handle_abnormal_edges (basic_block *dispatcher_bbs,
  593. basic_block for_bb, int *bb_to_omp_idx,
  594. auto_vec<basic_block> *bbs, bool computed_goto)
  595. {
  596. basic_block *dispatcher = dispatcher_bbs + (computed_goto ? 1 : 0);
  597. unsigned int idx = 0;
  598. basic_block bb;
  599. bool inner = false;
  600. if (bb_to_omp_idx)
  601. {
  602. dispatcher = dispatcher_bbs + 2 * bb_to_omp_idx[for_bb->index];
  603. if (bb_to_omp_idx[for_bb->index] != 0)
  604. inner = true;
  605. }
  606. /* If the dispatcher has been created already, then there are basic
  607. blocks with abnormal edges to it, so just make a new edge to
  608. for_bb. */
  609. if (*dispatcher == NULL)
  610. {
  611. /* Check if there are any basic blocks that need to have
  612. abnormal edges to this dispatcher. If there are none, return
  613. early. */
  614. if (bb_to_omp_idx == NULL)
  615. {
  616. if (bbs->is_empty ())
  617. return;
  618. }
  619. else
  620. {
  621. FOR_EACH_VEC_ELT (*bbs, idx, bb)
  622. if (bb_to_omp_idx[bb->index] == bb_to_omp_idx[for_bb->index])
  623. break;
  624. if (bb == NULL)
  625. return;
  626. }
  627. /* Create the dispatcher bb. */
  628. *dispatcher = create_basic_block (NULL, NULL, for_bb);
  629. if (computed_goto)
  630. {
  631. /* Factor computed gotos into a common computed goto site. Also
  632. record the location of that site so that we can un-factor the
  633. gotos after we have converted back to normal form. */
  634. gimple_stmt_iterator gsi = gsi_start_bb (*dispatcher);
  635. /* Create the destination of the factored goto. Each original
  636. computed goto will put its desired destination into this
  637. variable and jump to the label we create immediately below. */
  638. tree var = create_tmp_var (ptr_type_node, "gotovar");
  639. /* Build a label for the new block which will contain the
  640. factored computed goto. */
  641. tree factored_label_decl
  642. = create_artificial_label (UNKNOWN_LOCATION);
  643. gimple factored_computed_goto_label
  644. = gimple_build_label (factored_label_decl);
  645. gsi_insert_after (&gsi, factored_computed_goto_label, GSI_NEW_STMT);
  646. /* Build our new computed goto. */
  647. gimple factored_computed_goto = gimple_build_goto (var);
  648. gsi_insert_after (&gsi, factored_computed_goto, GSI_NEW_STMT);
  649. FOR_EACH_VEC_ELT (*bbs, idx, bb)
  650. {
  651. if (bb_to_omp_idx
  652. && bb_to_omp_idx[bb->index] != bb_to_omp_idx[for_bb->index])
  653. continue;
  654. gsi = gsi_last_bb (bb);
  655. gimple last = gsi_stmt (gsi);
  656. gcc_assert (computed_goto_p (last));
  657. /* Copy the original computed goto's destination into VAR. */
  658. gimple assignment
  659. = gimple_build_assign (var, gimple_goto_dest (last));
  660. gsi_insert_before (&gsi, assignment, GSI_SAME_STMT);
  661. edge e = make_edge (bb, *dispatcher, EDGE_FALLTHRU);
  662. e->goto_locus = gimple_location (last);
  663. gsi_remove (&gsi, true);
  664. }
  665. }
  666. else
  667. {
  668. tree arg = inner ? boolean_true_node : boolean_false_node;
  669. gimple g = gimple_build_call_internal (IFN_ABNORMAL_DISPATCHER,
  670. 1, arg);
  671. gimple_stmt_iterator gsi = gsi_after_labels (*dispatcher);
  672. gsi_insert_after (&gsi, g, GSI_NEW_STMT);
  673. /* Create predecessor edges of the dispatcher. */
  674. FOR_EACH_VEC_ELT (*bbs, idx, bb)
  675. {
  676. if (bb_to_omp_idx
  677. && bb_to_omp_idx[bb->index] != bb_to_omp_idx[for_bb->index])
  678. continue;
  679. make_edge (bb, *dispatcher, EDGE_ABNORMAL);
  680. }
  681. }
  682. }
  683. make_edge (*dispatcher, for_bb, EDGE_ABNORMAL);
  684. }
  685. /* Join all the blocks in the flowgraph. */
  686. static void
  687. make_edges (void)
  688. {
  689. basic_block bb;
  690. struct omp_region *cur_region = NULL;
  691. auto_vec<basic_block> ab_edge_goto;
  692. auto_vec<basic_block> ab_edge_call;
  693. int *bb_to_omp_idx = NULL;
  694. int cur_omp_region_idx = 0;
  695. /* Create an edge from entry to the first block with executable
  696. statements in it. */
  697. make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun),
  698. BASIC_BLOCK_FOR_FN (cfun, NUM_FIXED_BLOCKS),
  699. EDGE_FALLTHRU);
  700. /* Traverse the basic block array placing edges. */
  701. FOR_EACH_BB_FN (bb, cfun)
  702. {
  703. gimple last = last_stmt (bb);
  704. bool fallthru;
  705. if (bb_to_omp_idx)
  706. bb_to_omp_idx[bb->index] = cur_omp_region_idx;
  707. if (last)
  708. {
  709. enum gimple_code code = gimple_code (last);
  710. switch (code)
  711. {
  712. case GIMPLE_GOTO:
  713. if (make_goto_expr_edges (bb))
  714. ab_edge_goto.safe_push (bb);
  715. fallthru = false;
  716. break;
  717. case GIMPLE_RETURN:
  718. {
  719. edge e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
  720. e->goto_locus = gimple_location (last);
  721. fallthru = false;
  722. }
  723. break;
  724. case GIMPLE_COND:
  725. make_cond_expr_edges (bb);
  726. fallthru = false;
  727. break;
  728. case GIMPLE_SWITCH:
  729. make_gimple_switch_edges (as_a <gswitch *> (last), bb);
  730. fallthru = false;
  731. break;
  732. case GIMPLE_RESX:
  733. make_eh_edges (last);
  734. fallthru = false;
  735. break;
  736. case GIMPLE_EH_DISPATCH:
  737. fallthru = make_eh_dispatch_edges (as_a <geh_dispatch *> (last));
  738. break;
  739. case GIMPLE_CALL:
  740. /* If this function receives a nonlocal goto, then we need to
  741. make edges from this call site to all the nonlocal goto
  742. handlers. */
  743. if (stmt_can_make_abnormal_goto (last))
  744. ab_edge_call.safe_push (bb);
  745. /* If this statement has reachable exception handlers, then
  746. create abnormal edges to them. */
  747. make_eh_edges (last);
  748. /* BUILTIN_RETURN is really a return statement. */
  749. if (gimple_call_builtin_p (last, BUILT_IN_RETURN))
  750. {
  751. make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
  752. fallthru = false;
  753. }
  754. /* Some calls are known not to return. */
  755. else
  756. fallthru = !(gimple_call_flags (last) & ECF_NORETURN);
  757. break;
  758. case GIMPLE_ASSIGN:
  759. /* A GIMPLE_ASSIGN may throw internally and thus be considered
  760. control-altering. */
  761. if (is_ctrl_altering_stmt (last))
  762. make_eh_edges (last);
  763. fallthru = true;
  764. break;
  765. case GIMPLE_ASM:
  766. make_gimple_asm_edges (bb);
  767. fallthru = true;
  768. break;
  769. CASE_GIMPLE_OMP:
  770. fallthru = make_gimple_omp_edges (bb, &cur_region,
  771. &cur_omp_region_idx);
  772. if (cur_region && bb_to_omp_idx == NULL)
  773. bb_to_omp_idx = XCNEWVEC (int, n_basic_blocks_for_fn (cfun));
  774. break;
  775. case GIMPLE_TRANSACTION:
  776. {
  777. tree abort_label
  778. = gimple_transaction_label (as_a <gtransaction *> (last));
  779. if (abort_label)
  780. make_edge (bb, label_to_block (abort_label), EDGE_TM_ABORT);
  781. fallthru = true;
  782. }
  783. break;
  784. default:
  785. gcc_assert (!stmt_ends_bb_p (last));
  786. fallthru = true;
  787. }
  788. }
  789. else
  790. fallthru = true;
  791. if (fallthru)
  792. make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
  793. }
  794. /* Computed gotos are hell to deal with, especially if there are
  795. lots of them with a large number of destinations. So we factor
  796. them to a common computed goto location before we build the
  797. edge list. After we convert back to normal form, we will un-factor
  798. the computed gotos since factoring introduces an unwanted jump.
  799. For non-local gotos and abnormal edges from calls to calls that return
  800. twice or forced labels, factor the abnormal edges too, by having all
  801. abnormal edges from the calls go to a common artificial basic block
  802. with ABNORMAL_DISPATCHER internal call and abnormal edges from that
  803. basic block to all forced labels and calls returning twice.
  804. We do this per-OpenMP structured block, because those regions
  805. are guaranteed to be single entry single exit by the standard,
  806. so it is not allowed to enter or exit such regions abnormally this way,
  807. thus all computed gotos, non-local gotos and setjmp/longjmp calls
  808. must not transfer control across SESE region boundaries. */
  809. if (!ab_edge_goto.is_empty () || !ab_edge_call.is_empty ())
  810. {
  811. gimple_stmt_iterator gsi;
  812. basic_block dispatcher_bb_array[2] = { NULL, NULL };
  813. basic_block *dispatcher_bbs = dispatcher_bb_array;
  814. int count = n_basic_blocks_for_fn (cfun);
  815. if (bb_to_omp_idx)
  816. dispatcher_bbs = XCNEWVEC (basic_block, 2 * count);
  817. FOR_EACH_BB_FN (bb, cfun)
  818. {
  819. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  820. {
  821. glabel *label_stmt = dyn_cast <glabel *> (gsi_stmt (gsi));
  822. tree target;
  823. if (!label_stmt)
  824. break;
  825. target = gimple_label_label (label_stmt);
  826. /* Make an edge to every label block that has been marked as a
  827. potential target for a computed goto or a non-local goto. */
  828. if (FORCED_LABEL (target))
  829. handle_abnormal_edges (dispatcher_bbs, bb, bb_to_omp_idx,
  830. &ab_edge_goto, true);
  831. if (DECL_NONLOCAL (target))
  832. {
  833. handle_abnormal_edges (dispatcher_bbs, bb, bb_to_omp_idx,
  834. &ab_edge_call, false);
  835. break;
  836. }
  837. }
  838. if (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
  839. gsi_next_nondebug (&gsi);
  840. if (!gsi_end_p (gsi))
  841. {
  842. /* Make an edge to every setjmp-like call. */
  843. gimple call_stmt = gsi_stmt (gsi);
  844. if (is_gimple_call (call_stmt)
  845. && ((gimple_call_flags (call_stmt) & ECF_RETURNS_TWICE)
  846. || gimple_call_builtin_p (call_stmt,
  847. BUILT_IN_SETJMP_RECEIVER)))
  848. handle_abnormal_edges (dispatcher_bbs, bb, bb_to_omp_idx,
  849. &ab_edge_call, false);
  850. }
  851. }
  852. if (bb_to_omp_idx)
  853. XDELETE (dispatcher_bbs);
  854. }
  855. XDELETE (bb_to_omp_idx);
  856. free_omp_regions ();
  857. /* Fold COND_EXPR_COND of each COND_EXPR. */
  858. fold_cond_expr_cond ();
  859. }
  860. /* Find the next available discriminator value for LOCUS. The
  861. discriminator distinguishes among several basic blocks that
  862. share a common locus, allowing for more accurate sample-based
  863. profiling. */
  864. static int
  865. next_discriminator_for_locus (location_t locus)
  866. {
  867. struct locus_discrim_map item;
  868. struct locus_discrim_map **slot;
  869. item.locus = locus;
  870. item.discriminator = 0;
  871. slot = discriminator_per_locus->find_slot_with_hash (
  872. &item, LOCATION_LINE (locus), INSERT);
  873. gcc_assert (slot);
  874. if (*slot == HTAB_EMPTY_ENTRY)
  875. {
  876. *slot = XNEW (struct locus_discrim_map);
  877. gcc_assert (*slot);
  878. (*slot)->locus = locus;
  879. (*slot)->discriminator = 0;
  880. }
  881. (*slot)->discriminator++;
  882. return (*slot)->discriminator;
  883. }
  884. /* Return TRUE if LOCUS1 and LOCUS2 refer to the same source line. */
  885. static bool
  886. same_line_p (location_t locus1, location_t locus2)
  887. {
  888. expanded_location from, to;
  889. if (locus1 == locus2)
  890. return true;
  891. from = expand_location (locus1);
  892. to = expand_location (locus2);
  893. if (from.line != to.line)
  894. return false;
  895. if (from.file == to.file)
  896. return true;
  897. return (from.file != NULL
  898. && to.file != NULL
  899. && filename_cmp (from.file, to.file) == 0);
  900. }
  901. /* Assign discriminators to each basic block. */
  902. static void
  903. assign_discriminators (void)
  904. {
  905. basic_block bb;
  906. FOR_EACH_BB_FN (bb, cfun)
  907. {
  908. edge e;
  909. edge_iterator ei;
  910. gimple last = last_stmt (bb);
  911. location_t locus = last ? gimple_location (last) : UNKNOWN_LOCATION;
  912. if (locus == UNKNOWN_LOCATION)
  913. continue;
  914. FOR_EACH_EDGE (e, ei, bb->succs)
  915. {
  916. gimple first = first_non_label_stmt (e->dest);
  917. gimple last = last_stmt (e->dest);
  918. if ((first && same_line_p (locus, gimple_location (first)))
  919. || (last && same_line_p (locus, gimple_location (last))))
  920. {
  921. if (e->dest->discriminator != 0 && bb->discriminator == 0)
  922. bb->discriminator = next_discriminator_for_locus (locus);
  923. else
  924. e->dest->discriminator = next_discriminator_for_locus (locus);
  925. }
  926. }
  927. }
  928. }
  929. /* Create the edges for a GIMPLE_COND starting at block BB. */
  930. static void
  931. make_cond_expr_edges (basic_block bb)
  932. {
  933. gcond *entry = as_a <gcond *> (last_stmt (bb));
  934. gimple then_stmt, else_stmt;
  935. basic_block then_bb, else_bb;
  936. tree then_label, else_label;
  937. edge e;
  938. gcc_assert (entry);
  939. gcc_assert (gimple_code (entry) == GIMPLE_COND);
  940. /* Entry basic blocks for each component. */
  941. then_label = gimple_cond_true_label (entry);
  942. else_label = gimple_cond_false_label (entry);
  943. then_bb = label_to_block (then_label);
  944. else_bb = label_to_block (else_label);
  945. then_stmt = first_stmt (then_bb);
  946. else_stmt = first_stmt (else_bb);
  947. e = make_edge (bb, then_bb, EDGE_TRUE_VALUE);
  948. e->goto_locus = gimple_location (then_stmt);
  949. e = make_edge (bb, else_bb, EDGE_FALSE_VALUE);
  950. if (e)
  951. e->goto_locus = gimple_location (else_stmt);
  952. /* We do not need the labels anymore. */
  953. gimple_cond_set_true_label (entry, NULL_TREE);
  954. gimple_cond_set_false_label (entry, NULL_TREE);
  955. }
  956. /* Called for each element in the hash table (P) as we delete the
  957. edge to cases hash table.
  958. Clear all the TREE_CHAINs to prevent problems with copying of
  959. SWITCH_EXPRs and structure sharing rules, then free the hash table
  960. element. */
  961. bool
  962. edge_to_cases_cleanup (edge const &, tree const &value, void *)
  963. {
  964. tree t, next;
  965. for (t = value; t; t = next)
  966. {
  967. next = CASE_CHAIN (t);
  968. CASE_CHAIN (t) = NULL;
  969. }
  970. return true;
  971. }
  972. /* Start recording information mapping edges to case labels. */
  973. void
  974. start_recording_case_labels (void)
  975. {
  976. gcc_assert (edge_to_cases == NULL);
  977. edge_to_cases = new hash_map<edge, tree>;
  978. touched_switch_bbs = BITMAP_ALLOC (NULL);
  979. }
  980. /* Return nonzero if we are recording information for case labels. */
  981. static bool
  982. recording_case_labels_p (void)
  983. {
  984. return (edge_to_cases != NULL);
  985. }
  986. /* Stop recording information mapping edges to case labels and
  987. remove any information we have recorded. */
  988. void
  989. end_recording_case_labels (void)
  990. {
  991. bitmap_iterator bi;
  992. unsigned i;
  993. edge_to_cases->traverse<void *, edge_to_cases_cleanup> (NULL);
  994. delete edge_to_cases;
  995. edge_to_cases = NULL;
  996. EXECUTE_IF_SET_IN_BITMAP (touched_switch_bbs, 0, i, bi)
  997. {
  998. basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
  999. if (bb)
  1000. {
  1001. gimple stmt = last_stmt (bb);
  1002. if (stmt && gimple_code (stmt) == GIMPLE_SWITCH)
  1003. group_case_labels_stmt (as_a <gswitch *> (stmt));
  1004. }
  1005. }
  1006. BITMAP_FREE (touched_switch_bbs);
  1007. }
  1008. /* If we are inside a {start,end}_recording_cases block, then return
  1009. a chain of CASE_LABEL_EXPRs from T which reference E.
  1010. Otherwise return NULL. */
  1011. static tree
  1012. get_cases_for_edge (edge e, gswitch *t)
  1013. {
  1014. tree *slot;
  1015. size_t i, n;
  1016. /* If we are not recording cases, then we do not have CASE_LABEL_EXPR
  1017. chains available. Return NULL so the caller can detect this case. */
  1018. if (!recording_case_labels_p ())
  1019. return NULL;
  1020. slot = edge_to_cases->get (e);
  1021. if (slot)
  1022. return *slot;
  1023. /* If we did not find E in the hash table, then this must be the first
  1024. time we have been queried for information about E & T. Add all the
  1025. elements from T to the hash table then perform the query again. */
  1026. n = gimple_switch_num_labels (t);
  1027. for (i = 0; i < n; i++)
  1028. {
  1029. tree elt = gimple_switch_label (t, i);
  1030. tree lab = CASE_LABEL (elt);
  1031. basic_block label_bb = label_to_block (lab);
  1032. edge this_edge = find_edge (e->src, label_bb);
  1033. /* Add it to the chain of CASE_LABEL_EXPRs referencing E, or create
  1034. a new chain. */
  1035. tree &s = edge_to_cases->get_or_insert (this_edge);
  1036. CASE_CHAIN (elt) = s;
  1037. s = elt;
  1038. }
  1039. return *edge_to_cases->get (e);
  1040. }
  1041. /* Create the edges for a GIMPLE_SWITCH starting at block BB. */
  1042. static void
  1043. make_gimple_switch_edges (gswitch *entry, basic_block bb)
  1044. {
  1045. size_t i, n;
  1046. n = gimple_switch_num_labels (entry);
  1047. for (i = 0; i < n; ++i)
  1048. {
  1049. tree lab = CASE_LABEL (gimple_switch_label (entry, i));
  1050. basic_block label_bb = label_to_block (lab);
  1051. make_edge (bb, label_bb, 0);
  1052. }
  1053. }
  1054. /* Return the basic block holding label DEST. */
  1055. basic_block
  1056. label_to_block_fn (struct function *ifun, tree dest)
  1057. {
  1058. int uid = LABEL_DECL_UID (dest);
  1059. /* We would die hard when faced by an undefined label. Emit a label to
  1060. the very first basic block. This will hopefully make even the dataflow
  1061. and undefined variable warnings quite right. */
  1062. if (seen_error () && uid < 0)
  1063. {
  1064. gimple_stmt_iterator gsi =
  1065. gsi_start_bb (BASIC_BLOCK_FOR_FN (cfun, NUM_FIXED_BLOCKS));
  1066. gimple stmt;
  1067. stmt = gimple_build_label (dest);
  1068. gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
  1069. uid = LABEL_DECL_UID (dest);
  1070. }
  1071. if (vec_safe_length (ifun->cfg->x_label_to_block_map) <= (unsigned int) uid)
  1072. return NULL;
  1073. return (*ifun->cfg->x_label_to_block_map)[uid];
  1074. }
  1075. /* Create edges for a goto statement at block BB. Returns true
  1076. if abnormal edges should be created. */
  1077. static bool
  1078. make_goto_expr_edges (basic_block bb)
  1079. {
  1080. gimple_stmt_iterator last = gsi_last_bb (bb);
  1081. gimple goto_t = gsi_stmt (last);
  1082. /* A simple GOTO creates normal edges. */
  1083. if (simple_goto_p (goto_t))
  1084. {
  1085. tree dest = gimple_goto_dest (goto_t);
  1086. basic_block label_bb = label_to_block (dest);
  1087. edge e = make_edge (bb, label_bb, EDGE_FALLTHRU);
  1088. e->goto_locus = gimple_location (goto_t);
  1089. gsi_remove (&last, true);
  1090. return false;
  1091. }
  1092. /* A computed GOTO creates abnormal edges. */
  1093. return true;
  1094. }
  1095. /* Create edges for an asm statement with labels at block BB. */
  1096. static void
  1097. make_gimple_asm_edges (basic_block bb)
  1098. {
  1099. gasm *stmt = as_a <gasm *> (last_stmt (bb));
  1100. int i, n = gimple_asm_nlabels (stmt);
  1101. for (i = 0; i < n; ++i)
  1102. {
  1103. tree label = TREE_VALUE (gimple_asm_label_op (stmt, i));
  1104. basic_block label_bb = label_to_block (label);
  1105. make_edge (bb, label_bb, 0);
  1106. }
  1107. }
  1108. /*---------------------------------------------------------------------------
  1109. Flowgraph analysis
  1110. ---------------------------------------------------------------------------*/
  1111. /* Cleanup useless labels in basic blocks. This is something we wish
  1112. to do early because it allows us to group case labels before creating
  1113. the edges for the CFG, and it speeds up block statement iterators in
  1114. all passes later on.
  1115. We rerun this pass after CFG is created, to get rid of the labels that
  1116. are no longer referenced. After then we do not run it any more, since
  1117. (almost) no new labels should be created. */
  1118. /* A map from basic block index to the leading label of that block. */
  1119. static struct label_record
  1120. {
  1121. /* The label. */
  1122. tree label;
  1123. /* True if the label is referenced from somewhere. */
  1124. bool used;
  1125. } *label_for_bb;
  1126. /* Given LABEL return the first label in the same basic block. */
  1127. static tree
  1128. main_block_label (tree label)
  1129. {
  1130. basic_block bb = label_to_block (label);
  1131. tree main_label = label_for_bb[bb->index].label;
  1132. /* label_to_block possibly inserted undefined label into the chain. */
  1133. if (!main_label)
  1134. {
  1135. label_for_bb[bb->index].label = label;
  1136. main_label = label;
  1137. }
  1138. label_for_bb[bb->index].used = true;
  1139. return main_label;
  1140. }
  1141. /* Clean up redundant labels within the exception tree. */
  1142. static void
  1143. cleanup_dead_labels_eh (void)
  1144. {
  1145. eh_landing_pad lp;
  1146. eh_region r;
  1147. tree lab;
  1148. int i;
  1149. if (cfun->eh == NULL)
  1150. return;
  1151. for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
  1152. if (lp && lp->post_landing_pad)
  1153. {
  1154. lab = main_block_label (lp->post_landing_pad);
  1155. if (lab != lp->post_landing_pad)
  1156. {
  1157. EH_LANDING_PAD_NR (lp->post_landing_pad) = 0;
  1158. EH_LANDING_PAD_NR (lab) = lp->index;
  1159. }
  1160. }
  1161. FOR_ALL_EH_REGION (r)
  1162. switch (r->type)
  1163. {
  1164. case ERT_CLEANUP:
  1165. case ERT_MUST_NOT_THROW:
  1166. break;
  1167. case ERT_TRY:
  1168. {
  1169. eh_catch c;
  1170. for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
  1171. {
  1172. lab = c->label;
  1173. if (lab)
  1174. c->label = main_block_label (lab);
  1175. }
  1176. }
  1177. break;
  1178. case ERT_ALLOWED_EXCEPTIONS:
  1179. lab = r->u.allowed.label;
  1180. if (lab)
  1181. r->u.allowed.label = main_block_label (lab);
  1182. break;
  1183. }
  1184. }
  1185. /* Cleanup redundant labels. This is a three-step process:
  1186. 1) Find the leading label for each block.
  1187. 2) Redirect all references to labels to the leading labels.
  1188. 3) Cleanup all useless labels. */
  1189. void
  1190. cleanup_dead_labels (void)
  1191. {
  1192. basic_block bb;
  1193. label_for_bb = XCNEWVEC (struct label_record, last_basic_block_for_fn (cfun));
  1194. /* Find a suitable label for each block. We use the first user-defined
  1195. label if there is one, or otherwise just the first label we see. */
  1196. FOR_EACH_BB_FN (bb, cfun)
  1197. {
  1198. gimple_stmt_iterator i;
  1199. for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
  1200. {
  1201. tree label;
  1202. glabel *label_stmt = dyn_cast <glabel *> (gsi_stmt (i));
  1203. if (!label_stmt)
  1204. break;
  1205. label = gimple_label_label (label_stmt);
  1206. /* If we have not yet seen a label for the current block,
  1207. remember this one and see if there are more labels. */
  1208. if (!label_for_bb[bb->index].label)
  1209. {
  1210. label_for_bb[bb->index].label = label;
  1211. continue;
  1212. }
  1213. /* If we did see a label for the current block already, but it
  1214. is an artificially created label, replace it if the current
  1215. label is a user defined label. */
  1216. if (!DECL_ARTIFICIAL (label)
  1217. && DECL_ARTIFICIAL (label_for_bb[bb->index].label))
  1218. {
  1219. label_for_bb[bb->index].label = label;
  1220. break;
  1221. }
  1222. }
  1223. }
  1224. /* Now redirect all jumps/branches to the selected label.
  1225. First do so for each block ending in a control statement. */
  1226. FOR_EACH_BB_FN (bb, cfun)
  1227. {
  1228. gimple stmt = last_stmt (bb);
  1229. tree label, new_label;
  1230. if (!stmt)
  1231. continue;
  1232. switch (gimple_code (stmt))
  1233. {
  1234. case GIMPLE_COND:
  1235. {
  1236. gcond *cond_stmt = as_a <gcond *> (stmt);
  1237. label = gimple_cond_true_label (cond_stmt);
  1238. if (label)
  1239. {
  1240. new_label = main_block_label (label);
  1241. if (new_label != label)
  1242. gimple_cond_set_true_label (cond_stmt, new_label);
  1243. }
  1244. label = gimple_cond_false_label (cond_stmt);
  1245. if (label)
  1246. {
  1247. new_label = main_block_label (label);
  1248. if (new_label != label)
  1249. gimple_cond_set_false_label (cond_stmt, new_label);
  1250. }
  1251. }
  1252. break;
  1253. case GIMPLE_SWITCH:
  1254. {
  1255. gswitch *switch_stmt = as_a <gswitch *> (stmt);
  1256. size_t i, n = gimple_switch_num_labels (switch_stmt);
  1257. /* Replace all destination labels. */
  1258. for (i = 0; i < n; ++i)
  1259. {
  1260. tree case_label = gimple_switch_label (switch_stmt, i);
  1261. label = CASE_LABEL (case_label);
  1262. new_label = main_block_label (label);
  1263. if (new_label != label)
  1264. CASE_LABEL (case_label) = new_label;
  1265. }
  1266. break;
  1267. }
  1268. case GIMPLE_ASM:
  1269. {
  1270. gasm *asm_stmt = as_a <gasm *> (stmt);
  1271. int i, n = gimple_asm_nlabels (asm_stmt);
  1272. for (i = 0; i < n; ++i)
  1273. {
  1274. tree cons = gimple_asm_label_op (asm_stmt, i);
  1275. tree label = main_block_label (TREE_VALUE (cons));
  1276. TREE_VALUE (cons) = label;
  1277. }
  1278. break;
  1279. }
  1280. /* We have to handle gotos until they're removed, and we don't
  1281. remove them until after we've created the CFG edges. */
  1282. case GIMPLE_GOTO:
  1283. if (!computed_goto_p (stmt))
  1284. {
  1285. ggoto *goto_stmt = as_a <ggoto *> (stmt);
  1286. label = gimple_goto_dest (goto_stmt);
  1287. new_label = main_block_label (label);
  1288. if (new_label != label)
  1289. gimple_goto_set_dest (goto_stmt, new_label);
  1290. }
  1291. break;
  1292. case GIMPLE_TRANSACTION:
  1293. {
  1294. gtransaction *trans_stmt = as_a <gtransaction *> (stmt);
  1295. tree label = gimple_transaction_label (trans_stmt);
  1296. if (label)
  1297. {
  1298. tree new_label = main_block_label (label);
  1299. if (new_label != label)
  1300. gimple_transaction_set_label (trans_stmt, new_label);
  1301. }
  1302. }
  1303. break;
  1304. default:
  1305. break;
  1306. }
  1307. }
  1308. /* Do the same for the exception region tree labels. */
  1309. cleanup_dead_labels_eh ();
  1310. /* Finally, purge dead labels. All user-defined labels and labels that
  1311. can be the target of non-local gotos and labels which have their
  1312. address taken are preserved. */
  1313. FOR_EACH_BB_FN (bb, cfun)
  1314. {
  1315. gimple_stmt_iterator i;
  1316. tree label_for_this_bb = label_for_bb[bb->index].label;
  1317. if (!label_for_this_bb)
  1318. continue;
  1319. /* If the main label of the block is unused, we may still remove it. */
  1320. if (!label_for_bb[bb->index].used)
  1321. label_for_this_bb = NULL;
  1322. for (i = gsi_start_bb (bb); !gsi_end_p (i); )
  1323. {
  1324. tree label;
  1325. glabel *label_stmt = dyn_cast <glabel *> (gsi_stmt (i));
  1326. if (!label_stmt)
  1327. break;
  1328. label = gimple_label_label (label_stmt);
  1329. if (label == label_for_this_bb
  1330. || !DECL_ARTIFICIAL (label)
  1331. || DECL_NONLOCAL (label)
  1332. || FORCED_LABEL (label))
  1333. gsi_next (&i);
  1334. else
  1335. gsi_remove (&i, true);
  1336. }
  1337. }
  1338. free (label_for_bb);
  1339. }
  1340. /* Scan the sorted vector of cases in STMT (a GIMPLE_SWITCH) and combine
  1341. the ones jumping to the same label.
  1342. Eg. three separate entries 1: 2: 3: become one entry 1..3: */
  1343. void
  1344. group_case_labels_stmt (gswitch *stmt)
  1345. {
  1346. int old_size = gimple_switch_num_labels (stmt);
  1347. int i, j, new_size = old_size;
  1348. basic_block default_bb = NULL;
  1349. default_bb = label_to_block (CASE_LABEL (gimple_switch_default_label (stmt)));
  1350. /* Look for possible opportunities to merge cases. */
  1351. i = 1;
  1352. while (i < old_size)
  1353. {
  1354. tree base_case, base_high;
  1355. basic_block base_bb;
  1356. base_case = gimple_switch_label (stmt, i);
  1357. gcc_assert (base_case);
  1358. base_bb = label_to_block (CASE_LABEL (base_case));
  1359. /* Discard cases that have the same destination as the
  1360. default case. */
  1361. if (base_bb == default_bb)
  1362. {
  1363. gimple_switch_set_label (stmt, i, NULL_TREE);
  1364. i++;
  1365. new_size--;
  1366. continue;
  1367. }
  1368. base_high = CASE_HIGH (base_case)
  1369. ? CASE_HIGH (base_case)
  1370. : CASE_LOW (base_case);
  1371. i++;
  1372. /* Try to merge case labels. Break out when we reach the end
  1373. of the label vector or when we cannot merge the next case
  1374. label with the current one. */
  1375. while (i < old_size)
  1376. {
  1377. tree merge_case = gimple_switch_label (stmt, i);
  1378. basic_block merge_bb = label_to_block (CASE_LABEL (merge_case));
  1379. wide_int bhp1 = wi::add (base_high, 1);
  1380. /* Merge the cases if they jump to the same place,
  1381. and their ranges are consecutive. */
  1382. if (merge_bb == base_bb
  1383. && wi::eq_p (CASE_LOW (merge_case), bhp1))
  1384. {
  1385. base_high = CASE_HIGH (merge_case) ?
  1386. CASE_HIGH (merge_case) : CASE_LOW (merge_case);
  1387. CASE_HIGH (base_case) = base_high;
  1388. gimple_switch_set_label (stmt, i, NULL_TREE);
  1389. new_size--;
  1390. i++;
  1391. }
  1392. else
  1393. break;
  1394. }
  1395. }
  1396. /* Compress the case labels in the label vector, and adjust the
  1397. length of the vector. */
  1398. for (i = 0, j = 0; i < new_size; i++)
  1399. {
  1400. while (! gimple_switch_label (stmt, j))
  1401. j++;
  1402. gimple_switch_set_label (stmt, i,
  1403. gimple_switch_label (stmt, j++));
  1404. }
  1405. gcc_assert (new_size <= old_size);
  1406. gimple_switch_set_num_labels (stmt, new_size);
  1407. }
  1408. /* Look for blocks ending in a multiway branch (a GIMPLE_SWITCH),
  1409. and scan the sorted vector of cases. Combine the ones jumping to the
  1410. same label. */
  1411. void
  1412. group_case_labels (void)
  1413. {
  1414. basic_block bb;
  1415. FOR_EACH_BB_FN (bb, cfun)
  1416. {
  1417. gimple stmt = last_stmt (bb);
  1418. if (stmt && gimple_code (stmt) == GIMPLE_SWITCH)
  1419. group_case_labels_stmt (as_a <gswitch *> (stmt));
  1420. }
  1421. }
  1422. /* Checks whether we can merge block B into block A. */
  1423. static bool
  1424. gimple_can_merge_blocks_p (basic_block a, basic_block b)
  1425. {
  1426. gimple stmt;
  1427. if (!single_succ_p (a))
  1428. return false;
  1429. if (single_succ_edge (a)->flags & EDGE_COMPLEX)
  1430. return false;
  1431. if (single_succ (a) != b)
  1432. return false;
  1433. if (!single_pred_p (b))
  1434. return false;
  1435. if (a == ENTRY_BLOCK_PTR_FOR_FN (cfun)
  1436. || b == EXIT_BLOCK_PTR_FOR_FN (cfun))
  1437. return false;
  1438. /* If A ends by a statement causing exceptions or something similar, we
  1439. cannot merge the blocks. */
  1440. stmt = last_stmt (a);
  1441. if (stmt && stmt_ends_bb_p (stmt))
  1442. return false;
  1443. /* Do not allow a block with only a non-local label to be merged. */
  1444. if (stmt)
  1445. if (glabel *label_stmt = dyn_cast <glabel *> (stmt))
  1446. if (DECL_NONLOCAL (gimple_label_label (label_stmt)))
  1447. return false;
  1448. /* Examine the labels at the beginning of B. */
  1449. for (gimple_stmt_iterator gsi = gsi_start_bb (b); !gsi_end_p (gsi);
  1450. gsi_next (&gsi))
  1451. {
  1452. tree lab;
  1453. glabel *label_stmt = dyn_cast <glabel *> (gsi_stmt (gsi));
  1454. if (!label_stmt)
  1455. break;
  1456. lab = gimple_label_label (label_stmt);
  1457. /* Do not remove user forced labels or for -O0 any user labels. */
  1458. if (!DECL_ARTIFICIAL (lab) && (!optimize || FORCED_LABEL (lab)))
  1459. return false;
  1460. }
  1461. /* Protect simple loop latches. We only want to avoid merging
  1462. the latch with the loop header or with a block in another
  1463. loop in this case. */
  1464. if (current_loops
  1465. && b->loop_father->latch == b
  1466. && loops_state_satisfies_p (LOOPS_HAVE_SIMPLE_LATCHES)
  1467. && (b->loop_father->header == a
  1468. || b->loop_father != a->loop_father))
  1469. return false;
  1470. /* It must be possible to eliminate all phi nodes in B. If ssa form
  1471. is not up-to-date and a name-mapping is registered, we cannot eliminate
  1472. any phis. Symbols marked for renaming are never a problem though. */
  1473. for (gphi_iterator gsi = gsi_start_phis (b); !gsi_end_p (gsi);
  1474. gsi_next (&gsi))
  1475. {
  1476. gphi *phi = gsi.phi ();
  1477. /* Technically only new names matter. */
  1478. if (name_registered_for_update_p (PHI_RESULT (phi)))
  1479. return false;
  1480. }
  1481. /* When not optimizing, don't merge if we'd lose goto_locus. */
  1482. if (!optimize
  1483. && single_succ_edge (a)->goto_locus != UNKNOWN_LOCATION)
  1484. {
  1485. location_t goto_locus = single_succ_edge (a)->goto_locus;
  1486. gimple_stmt_iterator prev, next;
  1487. prev = gsi_last_nondebug_bb (a);
  1488. next = gsi_after_labels (b);
  1489. if (!gsi_end_p (next) && is_gimple_debug (gsi_stmt (next)))
  1490. gsi_next_nondebug (&next);
  1491. if ((gsi_end_p (prev)
  1492. || gimple_location (gsi_stmt (prev)) != goto_locus)
  1493. && (gsi_end_p (next)
  1494. || gimple_location (gsi_stmt (next)) != goto_locus))
  1495. return false;
  1496. }
  1497. return true;
  1498. }
  1499. /* Replaces all uses of NAME by VAL. */
  1500. void
  1501. replace_uses_by (tree name, tree val)
  1502. {
  1503. imm_use_iterator imm_iter;
  1504. use_operand_p use;
  1505. gimple stmt;
  1506. edge e;
  1507. FOR_EACH_IMM_USE_STMT (stmt, imm_iter, name)
  1508. {
  1509. /* Mark the block if we change the last stmt in it. */
  1510. if (cfgcleanup_altered_bbs
  1511. && stmt_ends_bb_p (stmt))
  1512. bitmap_set_bit (cfgcleanup_altered_bbs, gimple_bb (stmt)->index);
  1513. FOR_EACH_IMM_USE_ON_STMT (use, imm_iter)
  1514. {
  1515. replace_exp (use, val);
  1516. if (gimple_code (stmt) == GIMPLE_PHI)
  1517. {
  1518. e = gimple_phi_arg_edge (as_a <gphi *> (stmt),
  1519. PHI_ARG_INDEX_FROM_USE (use));
  1520. if (e->flags & EDGE_ABNORMAL
  1521. && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val))
  1522. {
  1523. /* This can only occur for virtual operands, since
  1524. for the real ones SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
  1525. would prevent replacement. */
  1526. gcc_checking_assert (virtual_operand_p (name));
  1527. SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val) = 1;
  1528. }
  1529. }
  1530. }
  1531. if (gimple_code (stmt) != GIMPLE_PHI)
  1532. {
  1533. gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
  1534. gimple orig_stmt = stmt;
  1535. size_t i;
  1536. /* FIXME. It shouldn't be required to keep TREE_CONSTANT
  1537. on ADDR_EXPRs up-to-date on GIMPLE. Propagation will
  1538. only change sth from non-invariant to invariant, and only
  1539. when propagating constants. */
  1540. if (is_gimple_min_invariant (val))
  1541. for (i = 0; i < gimple_num_ops (stmt); i++)
  1542. {
  1543. tree op = gimple_op (stmt, i);
  1544. /* Operands may be empty here. For example, the labels
  1545. of a GIMPLE_COND are nulled out following the creation
  1546. of the corresponding CFG edges. */
  1547. if (op && TREE_CODE (op) == ADDR_EXPR)
  1548. recompute_tree_invariant_for_addr_expr (op);
  1549. }
  1550. if (fold_stmt (&gsi))
  1551. stmt = gsi_stmt (gsi);
  1552. if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
  1553. gimple_purge_dead_eh_edges (gimple_bb (stmt));
  1554. update_stmt (stmt);
  1555. }
  1556. }
  1557. gcc_checking_assert (has_zero_uses (name));
  1558. /* Also update the trees stored in loop structures. */
  1559. if (current_loops)
  1560. {
  1561. struct loop *loop;
  1562. FOR_EACH_LOOP (loop, 0)
  1563. {
  1564. substitute_in_loop_info (loop, name, val);
  1565. }
  1566. }
  1567. }
  1568. /* Merge block B into block A. */
  1569. static void
  1570. gimple_merge_blocks (basic_block a, basic_block b)
  1571. {
  1572. gimple_stmt_iterator last, gsi;
  1573. gphi_iterator psi;
  1574. if (dump_file)
  1575. fprintf (dump_file, "Merging blocks %d and %d\n", a->index, b->index);
  1576. /* Remove all single-valued PHI nodes from block B of the form
  1577. V_i = PHI <V_j> by propagating V_j to all the uses of V_i. */
  1578. gsi = gsi_last_bb (a);
  1579. for (psi = gsi_start_phis (b); !gsi_end_p (psi); )
  1580. {
  1581. gimple phi = gsi_stmt (psi);
  1582. tree def = gimple_phi_result (phi), use = gimple_phi_arg_def (phi, 0);
  1583. gimple copy;
  1584. bool may_replace_uses = (virtual_operand_p (def)
  1585. || may_propagate_copy (def, use));
  1586. /* In case we maintain loop closed ssa form, do not propagate arguments
  1587. of loop exit phi nodes. */
  1588. if (current_loops
  1589. && loops_state_satisfies_p (LOOP_CLOSED_SSA)
  1590. && !virtual_operand_p (def)
  1591. && TREE_CODE (use) == SSA_NAME
  1592. && a->loop_father != b->loop_father)
  1593. may_replace_uses = false;
  1594. if (!may_replace_uses)
  1595. {
  1596. gcc_assert (!virtual_operand_p (def));
  1597. /* Note that just emitting the copies is fine -- there is no problem
  1598. with ordering of phi nodes. This is because A is the single
  1599. predecessor of B, therefore results of the phi nodes cannot
  1600. appear as arguments of the phi nodes. */
  1601. copy = gimple_build_assign (def, use);
  1602. gsi_insert_after (&gsi, copy, GSI_NEW_STMT);
  1603. remove_phi_node (&psi, false);
  1604. }
  1605. else
  1606. {
  1607. /* If we deal with a PHI for virtual operands, we can simply
  1608. propagate these without fussing with folding or updating
  1609. the stmt. */
  1610. if (virtual_operand_p (def))
  1611. {
  1612. imm_use_iterator iter;
  1613. use_operand_p use_p;
  1614. gimple stmt;
  1615. FOR_EACH_IMM_USE_STMT (stmt, iter, def)
  1616. FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
  1617. SET_USE (use_p, use);
  1618. if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def))
  1619. SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use) = 1;
  1620. }
  1621. else
  1622. replace_uses_by (def, use);
  1623. remove_phi_node (&psi, true);
  1624. }
  1625. }
  1626. /* Ensure that B follows A. */
  1627. move_block_after (b, a);
  1628. gcc_assert (single_succ_edge (a)->flags & EDGE_FALLTHRU);
  1629. gcc_assert (!last_stmt (a) || !stmt_ends_bb_p (last_stmt (a)));
  1630. /* Remove labels from B and set gimple_bb to A for other statements. */
  1631. for (gsi = gsi_start_bb (b); !gsi_end_p (gsi);)
  1632. {
  1633. gimple stmt = gsi_stmt (gsi);
  1634. if (glabel *label_stmt = dyn_cast <glabel *> (stmt))
  1635. {
  1636. tree label = gimple_label_label (label_stmt);
  1637. int lp_nr;
  1638. gsi_remove (&gsi, false);
  1639. /* Now that we can thread computed gotos, we might have
  1640. a situation where we have a forced label in block B
  1641. However, the label at the start of block B might still be
  1642. used in other ways (think about the runtime checking for
  1643. Fortran assigned gotos). So we can not just delete the
  1644. label. Instead we move the label to the start of block A. */
  1645. if (FORCED_LABEL (label))
  1646. {
  1647. gimple_stmt_iterator dest_gsi = gsi_start_bb (a);
  1648. gsi_insert_before (&dest_gsi, stmt, GSI_NEW_STMT);
  1649. }
  1650. /* Other user labels keep around in a form of a debug stmt. */
  1651. else if (!DECL_ARTIFICIAL (label) && MAY_HAVE_DEBUG_STMTS)
  1652. {
  1653. gimple dbg = gimple_build_debug_bind (label,
  1654. integer_zero_node,
  1655. stmt);
  1656. gimple_debug_bind_reset_value (dbg);
  1657. gsi_insert_before (&gsi, dbg, GSI_SAME_STMT);
  1658. }
  1659. lp_nr = EH_LANDING_PAD_NR (label);
  1660. if (lp_nr)
  1661. {
  1662. eh_landing_pad lp = get_eh_landing_pad_from_number (lp_nr);
  1663. lp->post_landing_pad = NULL;
  1664. }
  1665. }
  1666. else
  1667. {
  1668. gimple_set_bb (stmt, a);
  1669. gsi_next (&gsi);
  1670. }
  1671. }
  1672. /* When merging two BBs, if their counts are different, the larger count
  1673. is selected as the new bb count. This is to handle inconsistent
  1674. profiles. */
  1675. if (a->loop_father == b->loop_father)
  1676. {
  1677. a->count = MAX (a->count, b->count);
  1678. a->frequency = MAX (a->frequency, b->frequency);
  1679. }
  1680. /* Merge the sequences. */
  1681. last = gsi_last_bb (a);
  1682. gsi_insert_seq_after (&last, bb_seq (b), GSI_NEW_STMT);
  1683. set_bb_seq (b, NULL);
  1684. if (cfgcleanup_altered_bbs)
  1685. bitmap_set_bit (cfgcleanup_altered_bbs, a->index);
  1686. }
  1687. /* Return the one of two successors of BB that is not reachable by a
  1688. complex edge, if there is one. Else, return BB. We use
  1689. this in optimizations that use post-dominators for their heuristics,
  1690. to catch the cases in C++ where function calls are involved. */
  1691. basic_block
  1692. single_noncomplex_succ (basic_block bb)
  1693. {
  1694. edge e0, e1;
  1695. if (EDGE_COUNT (bb->succs) != 2)
  1696. return bb;
  1697. e0 = EDGE_SUCC (bb, 0);
  1698. e1 = EDGE_SUCC (bb, 1);
  1699. if (e0->flags & EDGE_COMPLEX)
  1700. return e1->dest;
  1701. if (e1->flags & EDGE_COMPLEX)
  1702. return e0->dest;
  1703. return bb;
  1704. }
  1705. /* T is CALL_EXPR. Set current_function_calls_* flags. */
  1706. void
  1707. notice_special_calls (gcall *call)
  1708. {
  1709. int flags = gimple_call_flags (call);
  1710. if (flags & ECF_MAY_BE_ALLOCA)
  1711. cfun->calls_alloca = true;
  1712. if (flags & ECF_RETURNS_TWICE)
  1713. cfun->calls_setjmp = true;
  1714. }
  1715. /* Clear flags set by notice_special_calls. Used by dead code removal
  1716. to update the flags. */
  1717. void
  1718. clear_special_calls (void)
  1719. {
  1720. cfun->calls_alloca = false;
  1721. cfun->calls_setjmp = false;
  1722. }
  1723. /* Remove PHI nodes associated with basic block BB and all edges out of BB. */
  1724. static void
  1725. remove_phi_nodes_and_edges_for_unreachable_block (basic_block bb)
  1726. {
  1727. /* Since this block is no longer reachable, we can just delete all
  1728. of its PHI nodes. */
  1729. remove_phi_nodes (bb);
  1730. /* Remove edges to BB's successors. */
  1731. while (EDGE_COUNT (bb->succs) > 0)
  1732. remove_edge (EDGE_SUCC (bb, 0));
  1733. }
  1734. /* Remove statements of basic block BB. */
  1735. static void
  1736. remove_bb (basic_block bb)
  1737. {
  1738. gimple_stmt_iterator i;
  1739. if (dump_file)
  1740. {
  1741. fprintf (dump_file, "Removing basic block %d\n", bb->index);
  1742. if (dump_flags & TDF_DETAILS)
  1743. {
  1744. dump_bb (dump_file, bb, 0, TDF_BLOCKS);
  1745. fprintf (dump_file, "\n");
  1746. }
  1747. }
  1748. if (current_loops)
  1749. {
  1750. struct loop *loop = bb->loop_father;
  1751. /* If a loop gets removed, clean up the information associated
  1752. with it. */
  1753. if (loop->latch == bb
  1754. || loop->header == bb)
  1755. free_numbers_of_iterations_estimates_loop (loop);
  1756. }
  1757. /* Remove all the instructions in the block. */
  1758. if (bb_seq (bb) != NULL)
  1759. {
  1760. /* Walk backwards so as to get a chance to substitute all
  1761. released DEFs into debug stmts. See
  1762. eliminate_unnecessary_stmts() in tree-ssa-dce.c for more
  1763. details. */
  1764. for (i = gsi_last_bb (bb); !gsi_end_p (i);)
  1765. {
  1766. gimple stmt = gsi_stmt (i);
  1767. glabel *label_stmt = dyn_cast <glabel *> (stmt);
  1768. if (label_stmt
  1769. && (FORCED_LABEL (gimple_label_label (label_stmt))
  1770. || DECL_NONLOCAL (gimple_label_label (label_stmt))))
  1771. {
  1772. basic_block new_bb;
  1773. gimple_stmt_iterator new_gsi;
  1774. /* A non-reachable non-local label may still be referenced.
  1775. But it no longer needs to carry the extra semantics of
  1776. non-locality. */
  1777. if (DECL_NONLOCAL (gimple_label_label (label_stmt)))
  1778. {
  1779. DECL_NONLOCAL (gimple_label_label (label_stmt)) = 0;
  1780. FORCED_LABEL (gimple_label_label (label_stmt)) = 1;
  1781. }
  1782. new_bb = bb->prev_bb;
  1783. new_gsi = gsi_start_bb (new_bb);
  1784. gsi_remove (&i, false);
  1785. gsi_insert_before (&new_gsi, stmt, GSI_NEW_STMT);
  1786. }
  1787. else
  1788. {
  1789. /* Release SSA definitions if we are in SSA. Note that we
  1790. may be called when not in SSA. For example,
  1791. final_cleanup calls this function via
  1792. cleanup_tree_cfg. */
  1793. if (gimple_in_ssa_p (cfun))
  1794. release_defs (stmt);
  1795. gsi_remove (&i, true);
  1796. }
  1797. if (gsi_end_p (i))
  1798. i = gsi_last_bb (bb);
  1799. else
  1800. gsi_prev (&i);
  1801. }
  1802. }
  1803. remove_phi_nodes_and_edges_for_unreachable_block (bb);
  1804. bb->il.gimple.seq = NULL;
  1805. bb->il.gimple.phi_nodes = NULL;
  1806. }
  1807. /* Given a basic block BB ending with COND_EXPR or SWITCH_EXPR, and a
  1808. predicate VAL, return the edge that will be taken out of the block.
  1809. If VAL does not match a unique edge, NULL is returned. */
  1810. edge
  1811. find_taken_edge (basic_block bb, tree val)
  1812. {
  1813. gimple stmt;
  1814. stmt = last_stmt (bb);
  1815. gcc_assert (stmt);
  1816. gcc_assert (is_ctrl_stmt (stmt));
  1817. if (val == NULL)
  1818. return NULL;
  1819. if (!is_gimple_min_invariant (val))
  1820. return NULL;
  1821. if (gimple_code (stmt) == GIMPLE_COND)
  1822. return find_taken_edge_cond_expr (bb, val);
  1823. if (gimple_code (stmt) == GIMPLE_SWITCH)
  1824. return find_taken_edge_switch_expr (as_a <gswitch *> (stmt), bb, val);
  1825. if (computed_goto_p (stmt))
  1826. {
  1827. /* Only optimize if the argument is a label, if the argument is
  1828. not a label then we can not construct a proper CFG.
  1829. It may be the case that we only need to allow the LABEL_REF to
  1830. appear inside an ADDR_EXPR, but we also allow the LABEL_REF to
  1831. appear inside a LABEL_EXPR just to be safe. */
  1832. if ((TREE_CODE (val) == ADDR_EXPR || TREE_CODE (val) == LABEL_EXPR)
  1833. && TREE_CODE (TREE_OPERAND (val, 0)) == LABEL_DECL)
  1834. return find_taken_edge_computed_goto (bb, TREE_OPERAND (val, 0));
  1835. return NULL;
  1836. }
  1837. gcc_unreachable ();
  1838. }
  1839. /* Given a constant value VAL and the entry block BB to a GOTO_EXPR
  1840. statement, determine which of the outgoing edges will be taken out of the
  1841. block. Return NULL if either edge may be taken. */
  1842. static edge
  1843. find_taken_edge_computed_goto (basic_block bb, tree val)
  1844. {
  1845. basic_block dest;
  1846. edge e = NULL;
  1847. dest = label_to_block (val);
  1848. if (dest)
  1849. {
  1850. e = find_edge (bb, dest);
  1851. gcc_assert (e != NULL);
  1852. }
  1853. return e;
  1854. }
  1855. /* Given a constant value VAL and the entry block BB to a COND_EXPR
  1856. statement, determine which of the two edges will be taken out of the
  1857. block. Return NULL if either edge may be taken. */
  1858. static edge
  1859. find_taken_edge_cond_expr (basic_block bb, tree val)
  1860. {
  1861. edge true_edge, false_edge;
  1862. extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
  1863. gcc_assert (TREE_CODE (val) == INTEGER_CST);
  1864. return (integer_zerop (val) ? false_edge : true_edge);
  1865. }
  1866. /* Given an INTEGER_CST VAL and the entry block BB to a SWITCH_EXPR
  1867. statement, determine which edge will be taken out of the block. Return
  1868. NULL if any edge may be taken. */
  1869. static edge
  1870. find_taken_edge_switch_expr (gswitch *switch_stmt, basic_block bb,
  1871. tree val)
  1872. {
  1873. basic_block dest_bb;
  1874. edge e;
  1875. tree taken_case;
  1876. taken_case = find_case_label_for_value (switch_stmt, val);
  1877. dest_bb = label_to_block (CASE_LABEL (taken_case));
  1878. e = find_edge (bb, dest_bb);
  1879. gcc_assert (e);
  1880. return e;
  1881. }
  1882. /* Return the CASE_LABEL_EXPR that SWITCH_STMT will take for VAL.
  1883. We can make optimal use here of the fact that the case labels are
  1884. sorted: We can do a binary search for a case matching VAL. */
  1885. static tree
  1886. find_case_label_for_value (gswitch *switch_stmt, tree val)
  1887. {
  1888. size_t low, high, n = gimple_switch_num_labels (switch_stmt);
  1889. tree default_case = gimple_switch_default_label (switch_stmt);
  1890. for (low = 0, high = n; high - low > 1; )
  1891. {
  1892. size_t i = (high + low) / 2;
  1893. tree t = gimple_switch_label (switch_stmt, i);
  1894. int cmp;
  1895. /* Cache the result of comparing CASE_LOW and val. */
  1896. cmp = tree_int_cst_compare (CASE_LOW (t), val);
  1897. if (cmp > 0)
  1898. high = i;
  1899. else
  1900. low = i;
  1901. if (CASE_HIGH (t) == NULL)
  1902. {
  1903. /* A singe-valued case label. */
  1904. if (cmp == 0)
  1905. return t;
  1906. }
  1907. else
  1908. {
  1909. /* A case range. We can only handle integer ranges. */
  1910. if (cmp <= 0 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
  1911. return t;
  1912. }
  1913. }
  1914. return default_case;
  1915. }
  1916. /* Dump a basic block on stderr. */
  1917. void
  1918. gimple_debug_bb (basic_block bb)
  1919. {
  1920. dump_bb (stderr, bb, 0, TDF_VOPS|TDF_MEMSYMS|TDF_BLOCKS);
  1921. }
  1922. /* Dump basic block with index N on stderr. */
  1923. basic_block
  1924. gimple_debug_bb_n (int n)
  1925. {
  1926. gimple_debug_bb (BASIC_BLOCK_FOR_FN (cfun, n));
  1927. return BASIC_BLOCK_FOR_FN (cfun, n);
  1928. }
  1929. /* Dump the CFG on stderr.
  1930. FLAGS are the same used by the tree dumping functions
  1931. (see TDF_* in dumpfile.h). */
  1932. void
  1933. gimple_debug_cfg (int flags)
  1934. {
  1935. gimple_dump_cfg (stderr, flags);
  1936. }
  1937. /* Dump the program showing basic block boundaries on the given FILE.
  1938. FLAGS are the same used by the tree dumping functions (see TDF_* in
  1939. tree.h). */
  1940. void
  1941. gimple_dump_cfg (FILE *file, int flags)
  1942. {
  1943. if (flags & TDF_DETAILS)
  1944. {
  1945. dump_function_header (file, current_function_decl, flags);
  1946. fprintf (file, ";; \n%d basic blocks, %d edges, last basic block %d.\n\n",
  1947. n_basic_blocks_for_fn (cfun), n_edges_for_fn (cfun),
  1948. last_basic_block_for_fn (cfun));
  1949. brief_dump_cfg (file, flags | TDF_COMMENT);
  1950. fprintf (file, "\n");
  1951. }
  1952. if (flags & TDF_STATS)
  1953. dump_cfg_stats (file);
  1954. dump_function_to_file (current_function_decl, file, flags | TDF_BLOCKS);
  1955. }
  1956. /* Dump CFG statistics on FILE. */
  1957. void
  1958. dump_cfg_stats (FILE *file)
  1959. {
  1960. static long max_num_merged_labels = 0;
  1961. unsigned long size, total = 0;
  1962. long num_edges;
  1963. basic_block bb;
  1964. const char * const fmt_str = "%-30s%-13s%12s\n";
  1965. const char * const fmt_str_1 = "%-30s%13d%11lu%c\n";
  1966. const char * const fmt_str_2 = "%-30s%13ld%11lu%c\n";
  1967. const char * const fmt_str_3 = "%-43s%11lu%c\n";
  1968. const char *funcname = current_function_name ();
  1969. fprintf (file, "\nCFG Statistics for %s\n\n", funcname);
  1970. fprintf (file, "---------------------------------------------------------\n");
  1971. fprintf (file, fmt_str, "", " Number of ", "Memory");
  1972. fprintf (file, fmt_str, "", " instances ", "used ");
  1973. fprintf (file, "---------------------------------------------------------\n");
  1974. size = n_basic_blocks_for_fn (cfun) * sizeof (struct basic_block_def);
  1975. total += size;
  1976. fprintf (file, fmt_str_1, "Basic blocks", n_basic_blocks_for_fn (cfun),
  1977. SCALE (size), LABEL (size));
  1978. num_edges = 0;
  1979. FOR_EACH_BB_FN (bb, cfun)
  1980. num_edges += EDGE_COUNT (bb->succs);
  1981. size = num_edges * sizeof (struct edge_def);
  1982. total += size;
  1983. fprintf (file, fmt_str_2, "Edges", num_edges, SCALE (size), LABEL (size));
  1984. fprintf (file, "---------------------------------------------------------\n");
  1985. fprintf (file, fmt_str_3, "Total memory used by CFG data", SCALE (total),
  1986. LABEL (total));
  1987. fprintf (file, "---------------------------------------------------------\n");
  1988. fprintf (file, "\n");
  1989. if (cfg_stats.num_merged_labels > max_num_merged_labels)
  1990. max_num_merged_labels = cfg_stats.num_merged_labels;
  1991. fprintf (file, "Coalesced label blocks: %ld (Max so far: %ld)\n",
  1992. cfg_stats.num_merged_labels, max_num_merged_labels);
  1993. fprintf (file, "\n");
  1994. }
  1995. /* Dump CFG statistics on stderr. Keep extern so that it's always
  1996. linked in the final executable. */
  1997. DEBUG_FUNCTION void
  1998. debug_cfg_stats (void)
  1999. {
  2000. dump_cfg_stats (stderr);
  2001. }
  2002. /*---------------------------------------------------------------------------
  2003. Miscellaneous helpers
  2004. ---------------------------------------------------------------------------*/
  2005. /* Return true if T, a GIMPLE_CALL, can make an abnormal transfer of control
  2006. flow. Transfers of control flow associated with EH are excluded. */
  2007. static bool
  2008. call_can_make_abnormal_goto (gimple t)
  2009. {
  2010. /* If the function has no non-local labels, then a call cannot make an
  2011. abnormal transfer of control. */
  2012. if (!cfun->has_nonlocal_label
  2013. && !cfun->calls_setjmp)
  2014. return false;
  2015. /* Likewise if the call has no side effects. */
  2016. if (!gimple_has_side_effects (t))
  2017. return false;
  2018. /* Likewise if the called function is leaf. */
  2019. if (gimple_call_flags (t) & ECF_LEAF)
  2020. return false;
  2021. return true;
  2022. }
  2023. /* Return true if T can make an abnormal transfer of control flow.
  2024. Transfers of control flow associated with EH are excluded. */
  2025. bool
  2026. stmt_can_make_abnormal_goto (gimple t)
  2027. {
  2028. if (computed_goto_p (t))
  2029. return true;
  2030. if (is_gimple_call (t))
  2031. return call_can_make_abnormal_goto (t);
  2032. return false;
  2033. }
  2034. /* Return true if T represents a stmt that always transfers control. */
  2035. bool
  2036. is_ctrl_stmt (gimple t)
  2037. {
  2038. switch (gimple_code (t))
  2039. {
  2040. case GIMPLE_COND:
  2041. case GIMPLE_SWITCH:
  2042. case GIMPLE_GOTO:
  2043. case GIMPLE_RETURN:
  2044. case GIMPLE_RESX:
  2045. return true;
  2046. default:
  2047. return false;
  2048. }
  2049. }
  2050. /* Return true if T is a statement that may alter the flow of control
  2051. (e.g., a call to a non-returning function). */
  2052. bool
  2053. is_ctrl_altering_stmt (gimple t)
  2054. {
  2055. gcc_assert (t);
  2056. switch (gimple_code (t))
  2057. {
  2058. case GIMPLE_CALL:
  2059. /* Per stmt call flag indicates whether the call could alter
  2060. controlflow. */
  2061. if (gimple_call_ctrl_altering_p (t))
  2062. return true;
  2063. break;
  2064. case GIMPLE_EH_DISPATCH:
  2065. /* EH_DISPATCH branches to the individual catch handlers at
  2066. this level of a try or allowed-exceptions region. It can
  2067. fallthru to the next statement as well. */
  2068. return true;
  2069. case GIMPLE_ASM:
  2070. if (gimple_asm_nlabels (as_a <gasm *> (t)) > 0)
  2071. return true;
  2072. break;
  2073. CASE_GIMPLE_OMP:
  2074. /* OpenMP directives alter control flow. */
  2075. return true;
  2076. case GIMPLE_TRANSACTION:
  2077. /* A transaction start alters control flow. */
  2078. return true;
  2079. default:
  2080. break;
  2081. }
  2082. /* If a statement can throw, it alters control flow. */
  2083. return stmt_can_throw_internal (t);
  2084. }
  2085. /* Return true if T is a simple local goto. */
  2086. bool
  2087. simple_goto_p (gimple t)
  2088. {
  2089. return (gimple_code (t) == GIMPLE_GOTO
  2090. && TREE_CODE (gimple_goto_dest (t)) == LABEL_DECL);
  2091. }
  2092. /* Return true if STMT should start a new basic block. PREV_STMT is
  2093. the statement preceding STMT. It is used when STMT is a label or a
  2094. case label. Labels should only start a new basic block if their
  2095. previous statement wasn't a label. Otherwise, sequence of labels
  2096. would generate unnecessary basic blocks that only contain a single
  2097. label. */
  2098. static inline bool
  2099. stmt_starts_bb_p (gimple stmt, gimple prev_stmt)
  2100. {
  2101. if (stmt == NULL)
  2102. return false;
  2103. /* Labels start a new basic block only if the preceding statement
  2104. wasn't a label of the same type. This prevents the creation of
  2105. consecutive blocks that have nothing but a single label. */
  2106. if (glabel *label_stmt = dyn_cast <glabel *> (stmt))
  2107. {
  2108. /* Nonlocal and computed GOTO targets always start a new block. */
  2109. if (DECL_NONLOCAL (gimple_label_label (label_stmt))
  2110. || FORCED_LABEL (gimple_label_label (label_stmt)))
  2111. return true;
  2112. if (prev_stmt && gimple_code (prev_stmt) == GIMPLE_LABEL)
  2113. {
  2114. if (DECL_NONLOCAL (gimple_label_label (
  2115. as_a <glabel *> (prev_stmt))))
  2116. return true;
  2117. cfg_stats.num_merged_labels++;
  2118. return false;
  2119. }
  2120. else
  2121. return true;
  2122. }
  2123. else if (gimple_code (stmt) == GIMPLE_CALL
  2124. && gimple_call_flags (stmt) & ECF_RETURNS_TWICE)
  2125. /* setjmp acts similar to a nonlocal GOTO target and thus should
  2126. start a new block. */
  2127. return true;
  2128. return false;
  2129. }
  2130. /* Return true if T should end a basic block. */
  2131. bool
  2132. stmt_ends_bb_p (gimple t)
  2133. {
  2134. return is_ctrl_stmt (t) || is_ctrl_altering_stmt (t);
  2135. }
  2136. /* Remove block annotations and other data structures. */
  2137. void
  2138. delete_tree_cfg_annotations (void)
  2139. {
  2140. vec_free (label_to_block_map_for_fn (cfun));
  2141. }
  2142. /* Return the first statement in basic block BB. */
  2143. gimple
  2144. first_stmt (basic_block bb)
  2145. {
  2146. gimple_stmt_iterator i = gsi_start_bb (bb);
  2147. gimple stmt = NULL;
  2148. while (!gsi_end_p (i) && is_gimple_debug ((stmt = gsi_stmt (i))))
  2149. {
  2150. gsi_next (&i);
  2151. stmt = NULL;
  2152. }
  2153. return stmt;
  2154. }
  2155. /* Return the first non-label statement in basic block BB. */
  2156. static gimple
  2157. first_non_label_stmt (basic_block bb)
  2158. {
  2159. gimple_stmt_iterator i = gsi_start_bb (bb);
  2160. while (!gsi_end_p (i) && gimple_code (gsi_stmt (i)) == GIMPLE_LABEL)
  2161. gsi_next (&i);
  2162. return !gsi_end_p (i) ? gsi_stmt (i) : NULL;
  2163. }
  2164. /* Return the last statement in basic block BB. */
  2165. gimple
  2166. last_stmt (basic_block bb)
  2167. {
  2168. gimple_stmt_iterator i = gsi_last_bb (bb);
  2169. gimple stmt = NULL;
  2170. while (!gsi_end_p (i) && is_gimple_debug ((stmt = gsi_stmt (i))))
  2171. {
  2172. gsi_prev (&i);
  2173. stmt = NULL;
  2174. }
  2175. return stmt;
  2176. }
  2177. /* Return the last statement of an otherwise empty block. Return NULL
  2178. if the block is totally empty, or if it contains more than one
  2179. statement. */
  2180. gimple
  2181. last_and_only_stmt (basic_block bb)
  2182. {
  2183. gimple_stmt_iterator i = gsi_last_nondebug_bb (bb);
  2184. gimple last, prev;
  2185. if (gsi_end_p (i))
  2186. return NULL;
  2187. last = gsi_stmt (i);
  2188. gsi_prev_nondebug (&i);
  2189. if (gsi_end_p (i))
  2190. return last;
  2191. /* Empty statements should no longer appear in the instruction stream.
  2192. Everything that might have appeared before should be deleted by
  2193. remove_useless_stmts, and the optimizers should just gsi_remove
  2194. instead of smashing with build_empty_stmt.
  2195. Thus the only thing that should appear here in a block containing
  2196. one executable statement is a label. */
  2197. prev = gsi_stmt (i);
  2198. if (gimple_code (prev) == GIMPLE_LABEL)
  2199. return last;
  2200. else
  2201. return NULL;
  2202. }
  2203. /* Reinstall those PHI arguments queued in OLD_EDGE to NEW_EDGE. */
  2204. static void
  2205. reinstall_phi_args (edge new_edge, edge old_edge)
  2206. {
  2207. edge_var_map *vm;
  2208. int i;
  2209. gphi_iterator phis;
  2210. vec<edge_var_map> *v = redirect_edge_var_map_vector (old_edge);
  2211. if (!v)
  2212. return;
  2213. for (i = 0, phis = gsi_start_phis (new_edge->dest);
  2214. v->iterate (i, &vm) && !gsi_end_p (phis);
  2215. i++, gsi_next (&phis))
  2216. {
  2217. gphi *phi = phis.phi ();
  2218. tree result = redirect_edge_var_map_result (vm);
  2219. tree arg = redirect_edge_var_map_def (vm);
  2220. gcc_assert (result == gimple_phi_result (phi));
  2221. add_phi_arg (phi, arg, new_edge, redirect_edge_var_map_location (vm));
  2222. }
  2223. redirect_edge_var_map_clear (old_edge);
  2224. }
  2225. /* Returns the basic block after which the new basic block created
  2226. by splitting edge EDGE_IN should be placed. Tries to keep the new block
  2227. near its "logical" location. This is of most help to humans looking
  2228. at debugging dumps. */
  2229. basic_block
  2230. split_edge_bb_loc (edge edge_in)
  2231. {
  2232. basic_block dest = edge_in->dest;
  2233. basic_block dest_prev = dest->prev_bb;
  2234. if (dest_prev)
  2235. {
  2236. edge e = find_edge (dest_prev, dest);
  2237. if (e && !(e->flags & EDGE_COMPLEX))
  2238. return edge_in->src;
  2239. }
  2240. return dest_prev;
  2241. }
  2242. /* Split a (typically critical) edge EDGE_IN. Return the new block.
  2243. Abort on abnormal edges. */
  2244. static basic_block
  2245. gimple_split_edge (edge edge_in)
  2246. {
  2247. basic_block new_bb, after_bb, dest;
  2248. edge new_edge, e;
  2249. /* Abnormal edges cannot be split. */
  2250. gcc_assert (!(edge_in->flags & EDGE_ABNORMAL));
  2251. dest = edge_in->dest;
  2252. after_bb = split_edge_bb_loc (edge_in);
  2253. new_bb = create_empty_bb (after_bb);
  2254. new_bb->frequency = EDGE_FREQUENCY (edge_in);
  2255. new_bb->count = edge_in->count;
  2256. new_edge = make_edge (new_bb, dest, EDGE_FALLTHRU);
  2257. new_edge->probability = REG_BR_PROB_BASE;
  2258. new_edge->count = edge_in->count;
  2259. e = redirect_edge_and_branch (edge_in, new_bb);
  2260. gcc_assert (e == edge_in);
  2261. reinstall_phi_args (new_edge, e);
  2262. return new_bb;
  2263. }
  2264. /* Verify properties of the address expression T with base object BASE. */
  2265. static tree
  2266. verify_address (tree t, tree base)
  2267. {
  2268. bool old_constant;
  2269. bool old_side_effects;
  2270. bool new_constant;
  2271. bool new_side_effects;
  2272. old_constant = TREE_CONSTANT (t);
  2273. old_side_effects = TREE_SIDE_EFFECTS (t);
  2274. recompute_tree_invariant_for_addr_expr (t);
  2275. new_side_effects = TREE_SIDE_EFFECTS (t);
  2276. new_constant = TREE_CONSTANT (t);
  2277. if (old_constant != new_constant)
  2278. {
  2279. error ("constant not recomputed when ADDR_EXPR changed");
  2280. return t;
  2281. }
  2282. if (old_side_effects != new_side_effects)
  2283. {
  2284. error ("side effects not recomputed when ADDR_EXPR changed");
  2285. return t;
  2286. }
  2287. if (!(TREE_CODE (base) == VAR_DECL
  2288. || TREE_CODE (base) == PARM_DECL
  2289. || TREE_CODE (base) == RESULT_DECL))
  2290. return NULL_TREE;
  2291. if (DECL_GIMPLE_REG_P (base))
  2292. {
  2293. error ("DECL_GIMPLE_REG_P set on a variable with address taken");
  2294. return base;
  2295. }
  2296. return NULL_TREE;
  2297. }
  2298. /* Callback for walk_tree, check that all elements with address taken are
  2299. properly noticed as such. The DATA is an int* that is 1 if TP was seen
  2300. inside a PHI node. */
  2301. static tree
  2302. verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
  2303. {
  2304. tree t = *tp, x;
  2305. if (TYPE_P (t))
  2306. *walk_subtrees = 0;
  2307. /* Check operand N for being valid GIMPLE and give error MSG if not. */
  2308. #define CHECK_OP(N, MSG) \
  2309. do { if (!is_gimple_val (TREE_OPERAND (t, N))) \
  2310. { error (MSG); return TREE_OPERAND (t, N); }} while (0)
  2311. switch (TREE_CODE (t))
  2312. {
  2313. case SSA_NAME:
  2314. if (SSA_NAME_IN_FREE_LIST (t))
  2315. {
  2316. error ("SSA name in freelist but still referenced");
  2317. return *tp;
  2318. }
  2319. break;
  2320. case INDIRECT_REF:
  2321. error ("INDIRECT_REF in gimple IL");
  2322. return t;
  2323. case MEM_REF:
  2324. x = TREE_OPERAND (t, 0);
  2325. if (!POINTER_TYPE_P (TREE_TYPE (x))
  2326. || !is_gimple_mem_ref_addr (x))
  2327. {
  2328. error ("invalid first operand of MEM_REF");
  2329. return x;
  2330. }
  2331. if (TREE_CODE (TREE_OPERAND (t, 1)) != INTEGER_CST
  2332. || !POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 1))))
  2333. {
  2334. error ("invalid offset operand of MEM_REF");
  2335. return TREE_OPERAND (t, 1);
  2336. }
  2337. if (TREE_CODE (x) == ADDR_EXPR
  2338. && (x = verify_address (x, TREE_OPERAND (x, 0))))
  2339. return x;
  2340. *walk_subtrees = 0;
  2341. break;
  2342. case ASSERT_EXPR:
  2343. x = fold (ASSERT_EXPR_COND (t));
  2344. if (x == boolean_false_node)
  2345. {
  2346. error ("ASSERT_EXPR with an always-false condition");
  2347. return *tp;
  2348. }
  2349. break;
  2350. case MODIFY_EXPR:
  2351. error ("MODIFY_EXPR not expected while having tuples");
  2352. return *tp;
  2353. case ADDR_EXPR:
  2354. {
  2355. tree tem;
  2356. gcc_assert (is_gimple_address (t));
  2357. /* Skip any references (they will be checked when we recurse down the
  2358. tree) and ensure that any variable used as a prefix is marked
  2359. addressable. */
  2360. for (x = TREE_OPERAND (t, 0);
  2361. handled_component_p (x);
  2362. x = TREE_OPERAND (x, 0))
  2363. ;
  2364. if ((tem = verify_address (t, x)))
  2365. return tem;
  2366. if (!(TREE_CODE (x) == VAR_DECL
  2367. || TREE_CODE (x) == PARM_DECL
  2368. || TREE_CODE (x) == RESULT_DECL))
  2369. return NULL;
  2370. if (!TREE_ADDRESSABLE (x))
  2371. {
  2372. error ("address taken, but ADDRESSABLE bit not set");
  2373. return x;
  2374. }
  2375. break;
  2376. }
  2377. case COND_EXPR:
  2378. x = COND_EXPR_COND (t);
  2379. if (!INTEGRAL_TYPE_P (TREE_TYPE (x)))
  2380. {
  2381. error ("non-integral used in condition");
  2382. return x;
  2383. }
  2384. if (!is_gimple_condexpr (x))
  2385. {
  2386. error ("invalid conditional operand");
  2387. return x;
  2388. }
  2389. break;
  2390. case NON_LVALUE_EXPR:
  2391. case TRUTH_NOT_EXPR:
  2392. gcc_unreachable ();
  2393. CASE_CONVERT:
  2394. case FIX_TRUNC_EXPR:
  2395. case FLOAT_EXPR:
  2396. case NEGATE_EXPR:
  2397. case ABS_EXPR:
  2398. case BIT_NOT_EXPR:
  2399. CHECK_OP (0, "invalid operand to unary operator");
  2400. break;
  2401. case REALPART_EXPR:
  2402. case IMAGPART_EXPR:
  2403. case BIT_FIELD_REF:
  2404. if (!is_gimple_reg_type (TREE_TYPE (t)))
  2405. {
  2406. error ("non-scalar BIT_FIELD_REF, IMAGPART_EXPR or REALPART_EXPR");
  2407. return t;
  2408. }
  2409. if (TREE_CODE (t) == BIT_FIELD_REF)
  2410. {
  2411. tree t0 = TREE_OPERAND (t, 0);
  2412. tree t1 = TREE_OPERAND (t, 1);
  2413. tree t2 = TREE_OPERAND (t, 2);
  2414. if (!tree_fits_uhwi_p (t1)
  2415. || !tree_fits_uhwi_p (t2))
  2416. {
  2417. error ("invalid position or size operand to BIT_FIELD_REF");
  2418. return t;
  2419. }
  2420. if (INTEGRAL_TYPE_P (TREE_TYPE (t))
  2421. && (TYPE_PRECISION (TREE_TYPE (t))
  2422. != tree_to_uhwi (t1)))
  2423. {
  2424. error ("integral result type precision does not match "
  2425. "field size of BIT_FIELD_REF");
  2426. return t;
  2427. }
  2428. else if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
  2429. && TYPE_MODE (TREE_TYPE (t)) != BLKmode
  2430. && (GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (t)))
  2431. != tree_to_uhwi (t1)))
  2432. {
  2433. error ("mode precision of non-integral result does not "
  2434. "match field size of BIT_FIELD_REF");
  2435. return t;
  2436. }
  2437. if (!AGGREGATE_TYPE_P (TREE_TYPE (t0))
  2438. && (tree_to_uhwi (t1) + tree_to_uhwi (t2)
  2439. > tree_to_uhwi (TYPE_SIZE (TREE_TYPE (t0)))))
  2440. {
  2441. error ("position plus size exceeds size of referenced object in "
  2442. "BIT_FIELD_REF");
  2443. return t;
  2444. }
  2445. }
  2446. t = TREE_OPERAND (t, 0);
  2447. /* Fall-through. */
  2448. case COMPONENT_REF:
  2449. case ARRAY_REF:
  2450. case ARRAY_RANGE_REF:
  2451. case VIEW_CONVERT_EXPR:
  2452. /* We have a nest of references. Verify that each of the operands
  2453. that determine where to reference is either a constant or a variable,
  2454. verify that the base is valid, and then show we've already checked
  2455. the subtrees. */
  2456. while (handled_component_p (t))
  2457. {
  2458. if (TREE_CODE (t) == COMPONENT_REF && TREE_OPERAND (t, 2))
  2459. CHECK_OP (2, "invalid COMPONENT_REF offset operator");
  2460. else if (TREE_CODE (t) == ARRAY_REF
  2461. || TREE_CODE (t) == ARRAY_RANGE_REF)
  2462. {
  2463. CHECK_OP (1, "invalid array index");
  2464. if (TREE_OPERAND (t, 2))
  2465. CHECK_OP (2, "invalid array lower bound");
  2466. if (TREE_OPERAND (t, 3))
  2467. CHECK_OP (3, "invalid array stride");
  2468. }
  2469. else if (TREE_CODE (t) == BIT_FIELD_REF
  2470. || TREE_CODE (t) == REALPART_EXPR
  2471. || TREE_CODE (t) == IMAGPART_EXPR)
  2472. {
  2473. error ("non-top-level BIT_FIELD_REF, IMAGPART_EXPR or "
  2474. "REALPART_EXPR");
  2475. return t;
  2476. }
  2477. t = TREE_OPERAND (t, 0);
  2478. }
  2479. if (!is_gimple_min_invariant (t) && !is_gimple_lvalue (t))
  2480. {
  2481. error ("invalid reference prefix");
  2482. return t;
  2483. }
  2484. *walk_subtrees = 0;
  2485. break;
  2486. case PLUS_EXPR:
  2487. case MINUS_EXPR:
  2488. /* PLUS_EXPR and MINUS_EXPR don't work on pointers, they should be done using
  2489. POINTER_PLUS_EXPR. */
  2490. if (POINTER_TYPE_P (TREE_TYPE (t)))
  2491. {
  2492. error ("invalid operand to plus/minus, type is a pointer");
  2493. return t;
  2494. }
  2495. CHECK_OP (0, "invalid operand to binary operator");
  2496. CHECK_OP (1, "invalid operand to binary operator");
  2497. break;
  2498. case POINTER_PLUS_EXPR:
  2499. /* Check to make sure the first operand is a pointer or reference type. */
  2500. if (!POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
  2501. {
  2502. error ("invalid operand to pointer plus, first operand is not a pointer");
  2503. return t;
  2504. }
  2505. /* Check to make sure the second operand is a ptrofftype. */
  2506. if (!ptrofftype_p (TREE_TYPE (TREE_OPERAND (t, 1))))
  2507. {
  2508. error ("invalid operand to pointer plus, second operand is not an "
  2509. "integer type of appropriate width");
  2510. return t;
  2511. }
  2512. /* FALLTHROUGH */
  2513. case LT_EXPR:
  2514. case LE_EXPR:
  2515. case GT_EXPR:
  2516. case GE_EXPR:
  2517. case EQ_EXPR:
  2518. case NE_EXPR:
  2519. case UNORDERED_EXPR:
  2520. case ORDERED_EXPR:
  2521. case UNLT_EXPR:
  2522. case UNLE_EXPR:
  2523. case UNGT_EXPR:
  2524. case UNGE_EXPR:
  2525. case UNEQ_EXPR:
  2526. case LTGT_EXPR:
  2527. case MULT_EXPR:
  2528. case TRUNC_DIV_EXPR:
  2529. case CEIL_DIV_EXPR:
  2530. case FLOOR_DIV_EXPR:
  2531. case ROUND_DIV_EXPR:
  2532. case TRUNC_MOD_EXPR:
  2533. case CEIL_MOD_EXPR:
  2534. case FLOOR_MOD_EXPR:
  2535. case ROUND_MOD_EXPR:
  2536. case RDIV_EXPR:
  2537. case EXACT_DIV_EXPR:
  2538. case MIN_EXPR:
  2539. case MAX_EXPR:
  2540. case LSHIFT_EXPR:
  2541. case RSHIFT_EXPR:
  2542. case LROTATE_EXPR:
  2543. case RROTATE_EXPR:
  2544. case BIT_IOR_EXPR:
  2545. case BIT_XOR_EXPR:
  2546. case BIT_AND_EXPR:
  2547. CHECK_OP (0, "invalid operand to binary operator");
  2548. CHECK_OP (1, "invalid operand to binary operator");
  2549. break;
  2550. case CONSTRUCTOR:
  2551. if (TREE_CONSTANT (t) && TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
  2552. *walk_subtrees = 0;
  2553. break;
  2554. case CASE_LABEL_EXPR:
  2555. if (CASE_CHAIN (t))
  2556. {
  2557. error ("invalid CASE_CHAIN");
  2558. return t;
  2559. }
  2560. break;
  2561. default:
  2562. break;
  2563. }
  2564. return NULL;
  2565. #undef CHECK_OP
  2566. }
  2567. /* Verify if EXPR is either a GIMPLE ID or a GIMPLE indirect reference.
  2568. Returns true if there is an error, otherwise false. */
  2569. static bool
  2570. verify_types_in_gimple_min_lval (tree expr)
  2571. {
  2572. tree op;
  2573. if (is_gimple_id (expr))
  2574. return false;
  2575. if (TREE_CODE (expr) != TARGET_MEM_REF
  2576. && TREE_CODE (expr) != MEM_REF)
  2577. {
  2578. error ("invalid expression for min lvalue");
  2579. return true;
  2580. }
  2581. /* TARGET_MEM_REFs are strange beasts. */
  2582. if (TREE_CODE (expr) == TARGET_MEM_REF)
  2583. return false;
  2584. op = TREE_OPERAND (expr, 0);
  2585. if (!is_gimple_val (op))
  2586. {
  2587. error ("invalid operand in indirect reference");
  2588. debug_generic_stmt (op);
  2589. return true;
  2590. }
  2591. /* Memory references now generally can involve a value conversion. */
  2592. return false;
  2593. }
  2594. /* Verify if EXPR is a valid GIMPLE reference expression. If
  2595. REQUIRE_LVALUE is true verifies it is an lvalue. Returns true
  2596. if there is an error, otherwise false. */
  2597. static bool
  2598. verify_types_in_gimple_reference (tree expr, bool require_lvalue)
  2599. {
  2600. while (handled_component_p (expr))
  2601. {
  2602. tree op = TREE_OPERAND (expr, 0);
  2603. if (TREE_CODE (expr) == ARRAY_REF
  2604. || TREE_CODE (expr) == ARRAY_RANGE_REF)
  2605. {
  2606. if (!is_gimple_val (TREE_OPERAND (expr, 1))
  2607. || (TREE_OPERAND (expr, 2)
  2608. && !is_gimple_val (TREE_OPERAND (expr, 2)))
  2609. || (TREE_OPERAND (expr, 3)
  2610. && !is_gimple_val (TREE_OPERAND (expr, 3))))
  2611. {
  2612. error ("invalid operands to array reference");
  2613. debug_generic_stmt (expr);
  2614. return true;
  2615. }
  2616. }
  2617. /* Verify if the reference array element types are compatible. */
  2618. if (TREE_CODE (expr) == ARRAY_REF
  2619. && !useless_type_conversion_p (TREE_TYPE (expr),
  2620. TREE_TYPE (TREE_TYPE (op))))
  2621. {
  2622. error ("type mismatch in array reference");
  2623. debug_generic_stmt (TREE_TYPE (expr));
  2624. debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
  2625. return true;
  2626. }
  2627. if (TREE_CODE (expr) == ARRAY_RANGE_REF
  2628. && !useless_type_conversion_p (TREE_TYPE (TREE_TYPE (expr)),
  2629. TREE_TYPE (TREE_TYPE (op))))
  2630. {
  2631. error ("type mismatch in array range reference");
  2632. debug_generic_stmt (TREE_TYPE (TREE_TYPE (expr)));
  2633. debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
  2634. return true;
  2635. }
  2636. if ((TREE_CODE (expr) == REALPART_EXPR
  2637. || TREE_CODE (expr) == IMAGPART_EXPR)
  2638. && !useless_type_conversion_p (TREE_TYPE (expr),
  2639. TREE_TYPE (TREE_TYPE (op))))
  2640. {
  2641. error ("type mismatch in real/imagpart reference");
  2642. debug_generic_stmt (TREE_TYPE (expr));
  2643. debug_generic_stmt (TREE_TYPE (TREE_TYPE (op)));
  2644. return true;
  2645. }
  2646. if (TREE_CODE (expr) == COMPONENT_REF
  2647. && !useless_type_conversion_p (TREE_TYPE (expr),
  2648. TREE_TYPE (TREE_OPERAND (expr, 1))))
  2649. {
  2650. error ("type mismatch in component reference");
  2651. debug_generic_stmt (TREE_TYPE (expr));
  2652. debug_generic_stmt (TREE_TYPE (TREE_OPERAND (expr, 1)));
  2653. return true;
  2654. }
  2655. if (TREE_CODE (expr) == VIEW_CONVERT_EXPR)
  2656. {
  2657. /* For VIEW_CONVERT_EXPRs which are allowed here too, we only check
  2658. that their operand is not an SSA name or an invariant when
  2659. requiring an lvalue (this usually means there is a SRA or IPA-SRA
  2660. bug). Otherwise there is nothing to verify, gross mismatches at
  2661. most invoke undefined behavior. */
  2662. if (require_lvalue
  2663. && (TREE_CODE (op) == SSA_NAME
  2664. || is_gimple_min_invariant (op)))
  2665. {
  2666. error ("conversion of an SSA_NAME on the left hand side");
  2667. debug_generic_stmt (expr);
  2668. return true;
  2669. }
  2670. else if (TREE_CODE (op) == SSA_NAME
  2671. && TYPE_SIZE (TREE_TYPE (expr)) != TYPE_SIZE (TREE_TYPE (op)))
  2672. {
  2673. error ("conversion of register to a different size");
  2674. debug_generic_stmt (expr);
  2675. return true;
  2676. }
  2677. else if (!handled_component_p (op))
  2678. return false;
  2679. }
  2680. expr = op;
  2681. }
  2682. if (TREE_CODE (expr) == MEM_REF)
  2683. {
  2684. if (!is_gimple_mem_ref_addr (TREE_OPERAND (expr, 0)))
  2685. {
  2686. error ("invalid address operand in MEM_REF");
  2687. debug_generic_stmt (expr);
  2688. return true;
  2689. }
  2690. if (TREE_CODE (TREE_OPERAND (expr, 1)) != INTEGER_CST
  2691. || !POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 1))))
  2692. {
  2693. error ("invalid offset operand in MEM_REF");
  2694. debug_generic_stmt (expr);
  2695. return true;
  2696. }
  2697. }
  2698. else if (TREE_CODE (expr) == TARGET_MEM_REF)
  2699. {
  2700. if (!TMR_BASE (expr)
  2701. || !is_gimple_mem_ref_addr (TMR_BASE (expr)))
  2702. {
  2703. error ("invalid address operand in TARGET_MEM_REF");
  2704. return true;
  2705. }
  2706. if (!TMR_OFFSET (expr)
  2707. || TREE_CODE (TMR_OFFSET (expr)) != INTEGER_CST
  2708. || !POINTER_TYPE_P (TREE_TYPE (TMR_OFFSET (expr))))
  2709. {
  2710. error ("invalid offset operand in TARGET_MEM_REF");
  2711. debug_generic_stmt (expr);
  2712. return true;
  2713. }
  2714. }
  2715. return ((require_lvalue || !is_gimple_min_invariant (expr))
  2716. && verify_types_in_gimple_min_lval (expr));
  2717. }
  2718. /* Returns true if there is one pointer type in TYPE_POINTER_TO (SRC_OBJ)
  2719. list of pointer-to types that is trivially convertible to DEST. */
  2720. static bool
  2721. one_pointer_to_useless_type_conversion_p (tree dest, tree src_obj)
  2722. {
  2723. tree src;
  2724. if (!TYPE_POINTER_TO (src_obj))
  2725. return true;
  2726. for (src = TYPE_POINTER_TO (src_obj); src; src = TYPE_NEXT_PTR_TO (src))
  2727. if (useless_type_conversion_p (dest, src))
  2728. return true;
  2729. return false;
  2730. }
  2731. /* Return true if TYPE1 is a fixed-point type and if conversions to and
  2732. from TYPE2 can be handled by FIXED_CONVERT_EXPR. */
  2733. static bool
  2734. valid_fixed_convert_types_p (tree type1, tree type2)
  2735. {
  2736. return (FIXED_POINT_TYPE_P (type1)
  2737. && (INTEGRAL_TYPE_P (type2)
  2738. || SCALAR_FLOAT_TYPE_P (type2)
  2739. || FIXED_POINT_TYPE_P (type2)));
  2740. }
  2741. /* Verify the contents of a GIMPLE_CALL STMT. Returns true when there
  2742. is a problem, otherwise false. */
  2743. static bool
  2744. verify_gimple_call (gcall *stmt)
  2745. {
  2746. tree fn = gimple_call_fn (stmt);
  2747. tree fntype, fndecl;
  2748. unsigned i;
  2749. if (gimple_call_internal_p (stmt))
  2750. {
  2751. if (fn)
  2752. {
  2753. error ("gimple call has two targets");
  2754. debug_generic_stmt (fn);
  2755. return true;
  2756. }
  2757. }
  2758. else
  2759. {
  2760. if (!fn)
  2761. {
  2762. error ("gimple call has no target");
  2763. return true;
  2764. }
  2765. }
  2766. if (fn && !is_gimple_call_addr (fn))
  2767. {
  2768. error ("invalid function in gimple call");
  2769. debug_generic_stmt (fn);
  2770. return true;
  2771. }
  2772. if (fn
  2773. && (!POINTER_TYPE_P (TREE_TYPE (fn))
  2774. || (TREE_CODE (TREE_TYPE (TREE_TYPE (fn))) != FUNCTION_TYPE
  2775. && TREE_CODE (TREE_TYPE (TREE_TYPE (fn))) != METHOD_TYPE)))
  2776. {
  2777. error ("non-function in gimple call");
  2778. return true;
  2779. }
  2780. fndecl = gimple_call_fndecl (stmt);
  2781. if (fndecl
  2782. && TREE_CODE (fndecl) == FUNCTION_DECL
  2783. && DECL_LOOPING_CONST_OR_PURE_P (fndecl)
  2784. && !DECL_PURE_P (fndecl)
  2785. && !TREE_READONLY (fndecl))
  2786. {
  2787. error ("invalid pure const state for function");
  2788. return true;
  2789. }
  2790. if (gimple_call_lhs (stmt)
  2791. && (!is_gimple_lvalue (gimple_call_lhs (stmt))
  2792. || verify_types_in_gimple_reference (gimple_call_lhs (stmt), true)))
  2793. {
  2794. error ("invalid LHS in gimple call");
  2795. return true;
  2796. }
  2797. if (gimple_call_ctrl_altering_p (stmt)
  2798. && gimple_call_lhs (stmt)
  2799. && gimple_call_noreturn_p (stmt))
  2800. {
  2801. error ("LHS in noreturn call");
  2802. return true;
  2803. }
  2804. fntype = gimple_call_fntype (stmt);
  2805. if (fntype
  2806. && gimple_call_lhs (stmt)
  2807. && !useless_type_conversion_p (TREE_TYPE (gimple_call_lhs (stmt)),
  2808. TREE_TYPE (fntype))
  2809. /* ??? At least C++ misses conversions at assignments from
  2810. void * call results.
  2811. ??? Java is completely off. Especially with functions
  2812. returning java.lang.Object.
  2813. For now simply allow arbitrary pointer type conversions. */
  2814. && !(POINTER_TYPE_P (TREE_TYPE (gimple_call_lhs (stmt)))
  2815. && POINTER_TYPE_P (TREE_TYPE (fntype))))
  2816. {
  2817. error ("invalid conversion in gimple call");
  2818. debug_generic_stmt (TREE_TYPE (gimple_call_lhs (stmt)));
  2819. debug_generic_stmt (TREE_TYPE (fntype));
  2820. return true;
  2821. }
  2822. if (gimple_call_chain (stmt)
  2823. && !is_gimple_val (gimple_call_chain (stmt)))
  2824. {
  2825. error ("invalid static chain in gimple call");
  2826. debug_generic_stmt (gimple_call_chain (stmt));
  2827. return true;
  2828. }
  2829. /* If there is a static chain argument, the call should either be
  2830. indirect, or the decl should have DECL_STATIC_CHAIN set. */
  2831. if (gimple_call_chain (stmt)
  2832. && fndecl
  2833. && !DECL_STATIC_CHAIN (fndecl))
  2834. {
  2835. error ("static chain with function that doesn%'t use one");
  2836. return true;
  2837. }
  2838. /* ??? The C frontend passes unpromoted arguments in case it
  2839. didn't see a function declaration before the call. So for now
  2840. leave the call arguments mostly unverified. Once we gimplify
  2841. unit-at-a-time we have a chance to fix this. */
  2842. for (i = 0; i < gimple_call_num_args (stmt); ++i)
  2843. {
  2844. tree arg = gimple_call_arg (stmt, i);
  2845. if ((is_gimple_reg_type (TREE_TYPE (arg))
  2846. && !is_gimple_val (arg))
  2847. || (!is_gimple_reg_type (TREE_TYPE (arg))
  2848. && !is_gimple_lvalue (arg)))
  2849. {
  2850. error ("invalid argument to gimple call");
  2851. debug_generic_expr (arg);
  2852. return true;
  2853. }
  2854. }
  2855. return false;
  2856. }
  2857. /* Verifies the gimple comparison with the result type TYPE and
  2858. the operands OP0 and OP1. */
  2859. static bool
  2860. verify_gimple_comparison (tree type, tree op0, tree op1)
  2861. {
  2862. tree op0_type = TREE_TYPE (op0);
  2863. tree op1_type = TREE_TYPE (op1);
  2864. if (!is_gimple_val (op0) || !is_gimple_val (op1))
  2865. {
  2866. error ("invalid operands in gimple comparison");
  2867. return true;
  2868. }
  2869. /* For comparisons we do not have the operations type as the
  2870. effective type the comparison is carried out in. Instead
  2871. we require that either the first operand is trivially
  2872. convertible into the second, or the other way around.
  2873. Because we special-case pointers to void we allow
  2874. comparisons of pointers with the same mode as well. */
  2875. if (!useless_type_conversion_p (op0_type, op1_type)
  2876. && !useless_type_conversion_p (op1_type, op0_type)
  2877. && (!POINTER_TYPE_P (op0_type)
  2878. || !POINTER_TYPE_P (op1_type)
  2879. || TYPE_MODE (op0_type) != TYPE_MODE (op1_type)))
  2880. {
  2881. error ("mismatching comparison operand types");
  2882. debug_generic_expr (op0_type);
  2883. debug_generic_expr (op1_type);
  2884. return true;
  2885. }
  2886. /* The resulting type of a comparison may be an effective boolean type. */
  2887. if (INTEGRAL_TYPE_P (type)
  2888. && (TREE_CODE (type) == BOOLEAN_TYPE
  2889. || TYPE_PRECISION (type) == 1))
  2890. {
  2891. if (TREE_CODE (op0_type) == VECTOR_TYPE
  2892. || TREE_CODE (op1_type) == VECTOR_TYPE)
  2893. {
  2894. error ("vector comparison returning a boolean");
  2895. debug_generic_expr (op0_type);
  2896. debug_generic_expr (op1_type);
  2897. return true;
  2898. }
  2899. }
  2900. /* Or an integer vector type with the same size and element count
  2901. as the comparison operand types. */
  2902. else if (TREE_CODE (type) == VECTOR_TYPE
  2903. && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
  2904. {
  2905. if (TREE_CODE (op0_type) != VECTOR_TYPE
  2906. || TREE_CODE (op1_type) != VECTOR_TYPE)
  2907. {
  2908. error ("non-vector operands in vector comparison");
  2909. debug_generic_expr (op0_type);
  2910. debug_generic_expr (op1_type);
  2911. return true;
  2912. }
  2913. if (TYPE_VECTOR_SUBPARTS (type) != TYPE_VECTOR_SUBPARTS (op0_type)
  2914. || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (type)))
  2915. != GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0_type))))
  2916. /* The result of a vector comparison is of signed
  2917. integral type. */
  2918. || TYPE_UNSIGNED (TREE_TYPE (type)))
  2919. {
  2920. error ("invalid vector comparison resulting type");
  2921. debug_generic_expr (type);
  2922. return true;
  2923. }
  2924. }
  2925. else
  2926. {
  2927. error ("bogus comparison result type");
  2928. debug_generic_expr (type);
  2929. return true;
  2930. }
  2931. return false;
  2932. }
  2933. /* Verify a gimple assignment statement STMT with an unary rhs.
  2934. Returns true if anything is wrong. */
  2935. static bool
  2936. verify_gimple_assign_unary (gassign *stmt)
  2937. {
  2938. enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
  2939. tree lhs = gimple_assign_lhs (stmt);
  2940. tree lhs_type = TREE_TYPE (lhs);
  2941. tree rhs1 = gimple_assign_rhs1 (stmt);
  2942. tree rhs1_type = TREE_TYPE (rhs1);
  2943. if (!is_gimple_reg (lhs))
  2944. {
  2945. error ("non-register as LHS of unary operation");
  2946. return true;
  2947. }
  2948. if (!is_gimple_val (rhs1))
  2949. {
  2950. error ("invalid operand in unary operation");
  2951. return true;
  2952. }
  2953. /* First handle conversions. */
  2954. switch (rhs_code)
  2955. {
  2956. CASE_CONVERT:
  2957. {
  2958. /* Allow conversions from pointer type to integral type only if
  2959. there is no sign or zero extension involved.
  2960. For targets were the precision of ptrofftype doesn't match that
  2961. of pointers we need to allow arbitrary conversions to ptrofftype. */
  2962. if ((POINTER_TYPE_P (lhs_type)
  2963. && INTEGRAL_TYPE_P (rhs1_type))
  2964. || (POINTER_TYPE_P (rhs1_type)
  2965. && INTEGRAL_TYPE_P (lhs_type)
  2966. && (TYPE_PRECISION (rhs1_type) >= TYPE_PRECISION (lhs_type)
  2967. || ptrofftype_p (sizetype))))
  2968. return false;
  2969. /* Allow conversion from integral to offset type and vice versa. */
  2970. if ((TREE_CODE (lhs_type) == OFFSET_TYPE
  2971. && INTEGRAL_TYPE_P (rhs1_type))
  2972. || (INTEGRAL_TYPE_P (lhs_type)
  2973. && TREE_CODE (rhs1_type) == OFFSET_TYPE))
  2974. return false;
  2975. /* Otherwise assert we are converting between types of the
  2976. same kind. */
  2977. if (INTEGRAL_TYPE_P (lhs_type) != INTEGRAL_TYPE_P (rhs1_type))
  2978. {
  2979. error ("invalid types in nop conversion");
  2980. debug_generic_expr (lhs_type);
  2981. debug_generic_expr (rhs1_type);
  2982. return true;
  2983. }
  2984. return false;
  2985. }
  2986. case ADDR_SPACE_CONVERT_EXPR:
  2987. {
  2988. if (!POINTER_TYPE_P (rhs1_type) || !POINTER_TYPE_P (lhs_type)
  2989. || (TYPE_ADDR_SPACE (TREE_TYPE (rhs1_type))
  2990. == TYPE_ADDR_SPACE (TREE_TYPE (lhs_type))))
  2991. {
  2992. error ("invalid types in address space conversion");
  2993. debug_generic_expr (lhs_type);
  2994. debug_generic_expr (rhs1_type);
  2995. return true;
  2996. }
  2997. return false;
  2998. }
  2999. case FIXED_CONVERT_EXPR:
  3000. {
  3001. if (!valid_fixed_convert_types_p (lhs_type, rhs1_type)
  3002. && !valid_fixed_convert_types_p (rhs1_type, lhs_type))
  3003. {
  3004. error ("invalid types in fixed-point conversion");
  3005. debug_generic_expr (lhs_type);
  3006. debug_generic_expr (rhs1_type);
  3007. return true;
  3008. }
  3009. return false;
  3010. }
  3011. case FLOAT_EXPR:
  3012. {
  3013. if ((!INTEGRAL_TYPE_P (rhs1_type) || !SCALAR_FLOAT_TYPE_P (lhs_type))
  3014. && (!VECTOR_INTEGER_TYPE_P (rhs1_type)
  3015. || !VECTOR_FLOAT_TYPE_P (lhs_type)))
  3016. {
  3017. error ("invalid types in conversion to floating point");
  3018. debug_generic_expr (lhs_type);
  3019. debug_generic_expr (rhs1_type);
  3020. return true;
  3021. }
  3022. return false;
  3023. }
  3024. case FIX_TRUNC_EXPR:
  3025. {
  3026. if ((!INTEGRAL_TYPE_P (lhs_type) || !SCALAR_FLOAT_TYPE_P (rhs1_type))
  3027. && (!VECTOR_INTEGER_TYPE_P (lhs_type)
  3028. || !VECTOR_FLOAT_TYPE_P (rhs1_type)))
  3029. {
  3030. error ("invalid types in conversion to integer");
  3031. debug_generic_expr (lhs_type);
  3032. debug_generic_expr (rhs1_type);
  3033. return true;
  3034. }
  3035. return false;
  3036. }
  3037. case REDUC_MAX_EXPR:
  3038. case REDUC_MIN_EXPR:
  3039. case REDUC_PLUS_EXPR:
  3040. if (!VECTOR_TYPE_P (rhs1_type)
  3041. || !useless_type_conversion_p (lhs_type, TREE_TYPE (rhs1_type)))
  3042. {
  3043. error ("reduction should convert from vector to element type");
  3044. debug_generic_expr (lhs_type);
  3045. debug_generic_expr (rhs1_type);
  3046. return true;
  3047. }
  3048. return false;
  3049. case VEC_UNPACK_HI_EXPR:
  3050. case VEC_UNPACK_LO_EXPR:
  3051. case VEC_UNPACK_FLOAT_HI_EXPR:
  3052. case VEC_UNPACK_FLOAT_LO_EXPR:
  3053. /* FIXME. */
  3054. return false;
  3055. case NEGATE_EXPR:
  3056. case ABS_EXPR:
  3057. case BIT_NOT_EXPR:
  3058. case PAREN_EXPR:
  3059. case CONJ_EXPR:
  3060. break;
  3061. default:
  3062. gcc_unreachable ();
  3063. }
  3064. /* For the remaining codes assert there is no conversion involved. */
  3065. if (!useless_type_conversion_p (lhs_type, rhs1_type))
  3066. {
  3067. error ("non-trivial conversion in unary operation");
  3068. debug_generic_expr (lhs_type);
  3069. debug_generic_expr (rhs1_type);
  3070. return true;
  3071. }
  3072. return false;
  3073. }
  3074. /* Verify a gimple assignment statement STMT with a binary rhs.
  3075. Returns true if anything is wrong. */
  3076. static bool
  3077. verify_gimple_assign_binary (gassign *stmt)
  3078. {
  3079. enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
  3080. tree lhs = gimple_assign_lhs (stmt);
  3081. tree lhs_type = TREE_TYPE (lhs);
  3082. tree rhs1 = gimple_assign_rhs1 (stmt);
  3083. tree rhs1_type = TREE_TYPE (rhs1);
  3084. tree rhs2 = gimple_assign_rhs2 (stmt);
  3085. tree rhs2_type = TREE_TYPE (rhs2);
  3086. if (!is_gimple_reg (lhs))
  3087. {
  3088. error ("non-register as LHS of binary operation");
  3089. return true;
  3090. }
  3091. if (!is_gimple_val (rhs1)
  3092. || !is_gimple_val (rhs2))
  3093. {
  3094. error ("invalid operands in binary operation");
  3095. return true;
  3096. }
  3097. /* First handle operations that involve different types. */
  3098. switch (rhs_code)
  3099. {
  3100. case COMPLEX_EXPR:
  3101. {
  3102. if (TREE_CODE (lhs_type) != COMPLEX_TYPE
  3103. || !(INTEGRAL_TYPE_P (rhs1_type)
  3104. || SCALAR_FLOAT_TYPE_P (rhs1_type))
  3105. || !(INTEGRAL_TYPE_P (rhs2_type)
  3106. || SCALAR_FLOAT_TYPE_P (rhs2_type)))
  3107. {
  3108. error ("type mismatch in complex expression");
  3109. debug_generic_expr (lhs_type);
  3110. debug_generic_expr (rhs1_type);
  3111. debug_generic_expr (rhs2_type);
  3112. return true;
  3113. }
  3114. return false;
  3115. }
  3116. case LSHIFT_EXPR:
  3117. case RSHIFT_EXPR:
  3118. case LROTATE_EXPR:
  3119. case RROTATE_EXPR:
  3120. {
  3121. /* Shifts and rotates are ok on integral types, fixed point
  3122. types and integer vector types. */
  3123. if ((!INTEGRAL_TYPE_P (rhs1_type)
  3124. && !FIXED_POINT_TYPE_P (rhs1_type)
  3125. && !(TREE_CODE (rhs1_type) == VECTOR_TYPE
  3126. && INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))))
  3127. || (!INTEGRAL_TYPE_P (rhs2_type)
  3128. /* Vector shifts of vectors are also ok. */
  3129. && !(TREE_CODE (rhs1_type) == VECTOR_TYPE
  3130. && INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))
  3131. && TREE_CODE (rhs2_type) == VECTOR_TYPE
  3132. && INTEGRAL_TYPE_P (TREE_TYPE (rhs2_type))))
  3133. || !useless_type_conversion_p (lhs_type, rhs1_type))
  3134. {
  3135. error ("type mismatch in shift expression");
  3136. debug_generic_expr (lhs_type);
  3137. debug_generic_expr (rhs1_type);
  3138. debug_generic_expr (rhs2_type);
  3139. return true;
  3140. }
  3141. return false;
  3142. }
  3143. case WIDEN_LSHIFT_EXPR:
  3144. {
  3145. if (!INTEGRAL_TYPE_P (lhs_type)
  3146. || !INTEGRAL_TYPE_P (rhs1_type)
  3147. || TREE_CODE (rhs2) != INTEGER_CST
  3148. || (2 * TYPE_PRECISION (rhs1_type) > TYPE_PRECISION (lhs_type)))
  3149. {
  3150. error ("type mismatch in widening vector shift expression");
  3151. debug_generic_expr (lhs_type);
  3152. debug_generic_expr (rhs1_type);
  3153. debug_generic_expr (rhs2_type);
  3154. return true;
  3155. }
  3156. return false;
  3157. }
  3158. case VEC_WIDEN_LSHIFT_HI_EXPR:
  3159. case VEC_WIDEN_LSHIFT_LO_EXPR:
  3160. {
  3161. if (TREE_CODE (rhs1_type) != VECTOR_TYPE
  3162. || TREE_CODE (lhs_type) != VECTOR_TYPE
  3163. || !INTEGRAL_TYPE_P (TREE_TYPE (rhs1_type))
  3164. || !INTEGRAL_TYPE_P (TREE_TYPE (lhs_type))
  3165. || TREE_CODE (rhs2) != INTEGER_CST
  3166. || (2 * TYPE_PRECISION (TREE_TYPE (rhs1_type))
  3167. > TYPE_PRECISION (TREE_TYPE (lhs_type))))
  3168. {
  3169. error ("type mismatch in widening vector shift expression");
  3170. debug_generic_expr (lhs_type);
  3171. debug_generic_expr (rhs1_type);
  3172. debug_generic_expr (rhs2_type);
  3173. return true;
  3174. }
  3175. return false;
  3176. }
  3177. case PLUS_EXPR:
  3178. case MINUS_EXPR:
  3179. {
  3180. tree lhs_etype = lhs_type;
  3181. tree rhs1_etype = rhs1_type;
  3182. tree rhs2_etype = rhs2_type;
  3183. if (TREE_CODE (lhs_type) == VECTOR_TYPE)
  3184. {
  3185. if (TREE_CODE (rhs1_type) != VECTOR_TYPE
  3186. || TREE_CODE (rhs2_type) != VECTOR_TYPE)
  3187. {
  3188. error ("invalid non-vector operands to vector valued plus");
  3189. return true;
  3190. }
  3191. lhs_etype = TREE_TYPE (lhs_type);
  3192. rhs1_etype = TREE_TYPE (rhs1_type);
  3193. rhs2_etype = TREE_TYPE (rhs2_type);
  3194. }
  3195. if (POINTER_TYPE_P (lhs_etype)
  3196. || POINTER_TYPE_P (rhs1_etype)
  3197. || POINTER_TYPE_P (rhs2_etype))
  3198. {
  3199. error ("invalid (pointer) operands to plus/minus");
  3200. return true;
  3201. }
  3202. /* Continue with generic binary expression handling. */
  3203. break;
  3204. }
  3205. case POINTER_PLUS_EXPR:
  3206. {
  3207. if (!POINTER_TYPE_P (rhs1_type)
  3208. || !useless_type_conversion_p (lhs_type, rhs1_type)
  3209. || !ptrofftype_p (rhs2_type))
  3210. {
  3211. error ("type mismatch in pointer plus expression");
  3212. debug_generic_stmt (lhs_type);
  3213. debug_generic_stmt (rhs1_type);
  3214. debug_generic_stmt (rhs2_type);
  3215. return true;
  3216. }
  3217. return false;
  3218. }
  3219. case TRUTH_ANDIF_EXPR:
  3220. case TRUTH_ORIF_EXPR:
  3221. case TRUTH_AND_EXPR:
  3222. case TRUTH_OR_EXPR:
  3223. case TRUTH_XOR_EXPR:
  3224. gcc_unreachable ();
  3225. case LT_EXPR:
  3226. case LE_EXPR:
  3227. case GT_EXPR:
  3228. case GE_EXPR:
  3229. case EQ_EXPR:
  3230. case NE_EXPR:
  3231. case UNORDERED_EXPR:
  3232. case ORDERED_EXPR:
  3233. case UNLT_EXPR:
  3234. case UNLE_EXPR:
  3235. case UNGT_EXPR:
  3236. case UNGE_EXPR:
  3237. case UNEQ_EXPR:
  3238. case LTGT_EXPR:
  3239. /* Comparisons are also binary, but the result type is not
  3240. connected to the operand types. */
  3241. return verify_gimple_comparison (lhs_type, rhs1, rhs2);
  3242. case WIDEN_MULT_EXPR:
  3243. if (TREE_CODE (lhs_type) != INTEGER_TYPE)
  3244. return true;
  3245. return ((2 * TYPE_PRECISION (rhs1_type) > TYPE_PRECISION (lhs_type))
  3246. || (TYPE_PRECISION (rhs1_type) != TYPE_PRECISION (rhs2_type)));
  3247. case WIDEN_SUM_EXPR:
  3248. case VEC_WIDEN_MULT_HI_EXPR:
  3249. case VEC_WIDEN_MULT_LO_EXPR:
  3250. case VEC_WIDEN_MULT_EVEN_EXPR:
  3251. case VEC_WIDEN_MULT_ODD_EXPR:
  3252. case VEC_PACK_TRUNC_EXPR:
  3253. case VEC_PACK_SAT_EXPR:
  3254. case VEC_PACK_FIX_TRUNC_EXPR:
  3255. /* FIXME. */
  3256. return false;
  3257. case MULT_EXPR:
  3258. case MULT_HIGHPART_EXPR:
  3259. case TRUNC_DIV_EXPR:
  3260. case CEIL_DIV_EXPR:
  3261. case FLOOR_DIV_EXPR:
  3262. case ROUND_DIV_EXPR:
  3263. case TRUNC_MOD_EXPR:
  3264. case CEIL_MOD_EXPR:
  3265. case FLOOR_MOD_EXPR:
  3266. case ROUND_MOD_EXPR:
  3267. case RDIV_EXPR:
  3268. case EXACT_DIV_EXPR:
  3269. case MIN_EXPR:
  3270. case MAX_EXPR:
  3271. case BIT_IOR_EXPR:
  3272. case BIT_XOR_EXPR:
  3273. case BIT_AND_EXPR:
  3274. /* Continue with generic binary expression handling. */
  3275. break;
  3276. default:
  3277. gcc_unreachable ();
  3278. }
  3279. if (!useless_type_conversion_p (lhs_type, rhs1_type)
  3280. || !useless_type_conversion_p (lhs_type, rhs2_type))
  3281. {
  3282. error ("type mismatch in binary expression");
  3283. debug_generic_stmt (lhs_type);
  3284. debug_generic_stmt (rhs1_type);
  3285. debug_generic_stmt (rhs2_type);
  3286. return true;
  3287. }
  3288. return false;
  3289. }
  3290. /* Verify a gimple assignment statement STMT with a ternary rhs.
  3291. Returns true if anything is wrong. */
  3292. static bool
  3293. verify_gimple_assign_ternary (gassign *stmt)
  3294. {
  3295. enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
  3296. tree lhs = gimple_assign_lhs (stmt);
  3297. tree lhs_type = TREE_TYPE (lhs);
  3298. tree rhs1 = gimple_assign_rhs1 (stmt);
  3299. tree rhs1_type = TREE_TYPE (rhs1);
  3300. tree rhs2 = gimple_assign_rhs2 (stmt);
  3301. tree rhs2_type = TREE_TYPE (rhs2);
  3302. tree rhs3 = gimple_assign_rhs3 (stmt);
  3303. tree rhs3_type = TREE_TYPE (rhs3);
  3304. if (!is_gimple_reg (lhs))
  3305. {
  3306. error ("non-register as LHS of ternary operation");
  3307. return true;
  3308. }
  3309. if (((rhs_code == VEC_COND_EXPR || rhs_code == COND_EXPR)
  3310. ? !is_gimple_condexpr (rhs1) : !is_gimple_val (rhs1))
  3311. || !is_gimple_val (rhs2)
  3312. || !is_gimple_val (rhs3))
  3313. {
  3314. error ("invalid operands in ternary operation");
  3315. return true;
  3316. }
  3317. /* First handle operations that involve different types. */
  3318. switch (rhs_code)
  3319. {
  3320. case WIDEN_MULT_PLUS_EXPR:
  3321. case WIDEN_MULT_MINUS_EXPR:
  3322. if ((!INTEGRAL_TYPE_P (rhs1_type)
  3323. && !FIXED_POINT_TYPE_P (rhs1_type))
  3324. || !useless_type_conversion_p (rhs1_type, rhs2_type)
  3325. || !useless_type_conversion_p (lhs_type, rhs3_type)
  3326. || 2 * TYPE_PRECISION (rhs1_type) > TYPE_PRECISION (lhs_type)
  3327. || TYPE_PRECISION (rhs1_type) != TYPE_PRECISION (rhs2_type))
  3328. {
  3329. error ("type mismatch in widening multiply-accumulate expression");
  3330. debug_generic_expr (lhs_type);
  3331. debug_generic_expr (rhs1_type);
  3332. debug_generic_expr (rhs2_type);
  3333. debug_generic_expr (rhs3_type);
  3334. return true;
  3335. }
  3336. break;
  3337. case FMA_EXPR:
  3338. if (!useless_type_conversion_p (lhs_type, rhs1_type)
  3339. || !useless_type_conversion_p (lhs_type, rhs2_type)
  3340. || !useless_type_conversion_p (lhs_type, rhs3_type))
  3341. {
  3342. error ("type mismatch in fused multiply-add expression");
  3343. debug_generic_expr (lhs_type);
  3344. debug_generic_expr (rhs1_type);
  3345. debug_generic_expr (rhs2_type);
  3346. debug_generic_expr (rhs3_type);
  3347. return true;
  3348. }
  3349. break;
  3350. case COND_EXPR:
  3351. case VEC_COND_EXPR:
  3352. if (!useless_type_conversion_p (lhs_type, rhs2_type)
  3353. || !useless_type_conversion_p (lhs_type, rhs3_type))
  3354. {
  3355. error ("type mismatch in conditional expression");
  3356. debug_generic_expr (lhs_type);
  3357. debug_generic_expr (rhs2_type);
  3358. debug_generic_expr (rhs3_type);
  3359. return true;
  3360. }
  3361. break;
  3362. case VEC_PERM_EXPR:
  3363. if (!useless_type_conversion_p (lhs_type, rhs1_type)
  3364. || !useless_type_conversion_p (lhs_type, rhs2_type))
  3365. {
  3366. error ("type mismatch in vector permute expression");
  3367. debug_generic_expr (lhs_type);
  3368. debug_generic_expr (rhs1_type);
  3369. debug_generic_expr (rhs2_type);
  3370. debug_generic_expr (rhs3_type);
  3371. return true;
  3372. }
  3373. if (TREE_CODE (rhs1_type) != VECTOR_TYPE
  3374. || TREE_CODE (rhs2_type) != VECTOR_TYPE
  3375. || TREE_CODE (rhs3_type) != VECTOR_TYPE)
  3376. {
  3377. error ("vector types expected in vector permute expression");
  3378. debug_generic_expr (lhs_type);
  3379. debug_generic_expr (rhs1_type);
  3380. debug_generic_expr (rhs2_type);
  3381. debug_generic_expr (rhs3_type);
  3382. return true;
  3383. }
  3384. if (TYPE_VECTOR_SUBPARTS (rhs1_type) != TYPE_VECTOR_SUBPARTS (rhs2_type)
  3385. || TYPE_VECTOR_SUBPARTS (rhs2_type)
  3386. != TYPE_VECTOR_SUBPARTS (rhs3_type)
  3387. || TYPE_VECTOR_SUBPARTS (rhs3_type)
  3388. != TYPE_VECTOR_SUBPARTS (lhs_type))
  3389. {
  3390. error ("vectors with different element number found "
  3391. "in vector permute expression");
  3392. debug_generic_expr (lhs_type);
  3393. debug_generic_expr (rhs1_type);
  3394. debug_generic_expr (rhs2_type);
  3395. debug_generic_expr (rhs3_type);
  3396. return true;
  3397. }
  3398. if (TREE_CODE (TREE_TYPE (rhs3_type)) != INTEGER_TYPE
  3399. || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (rhs3_type)))
  3400. != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (rhs1_type))))
  3401. {
  3402. error ("invalid mask type in vector permute expression");
  3403. debug_generic_expr (lhs_type);
  3404. debug_generic_expr (rhs1_type);
  3405. debug_generic_expr (rhs2_type);
  3406. debug_generic_expr (rhs3_type);
  3407. return true;
  3408. }
  3409. return false;
  3410. case SAD_EXPR:
  3411. if (!useless_type_conversion_p (rhs1_type, rhs2_type)
  3412. || !useless_type_conversion_p (lhs_type, rhs3_type)
  3413. || 2 * GET_MODE_BITSIZE (GET_MODE_INNER
  3414. (TYPE_MODE (TREE_TYPE (rhs1_type))))
  3415. > GET_MODE_BITSIZE (GET_MODE_INNER
  3416. (TYPE_MODE (TREE_TYPE (lhs_type)))))
  3417. {
  3418. error ("type mismatch in sad expression");
  3419. debug_generic_expr (lhs_type);
  3420. debug_generic_expr (rhs1_type);
  3421. debug_generic_expr (rhs2_type);
  3422. debug_generic_expr (rhs3_type);
  3423. return true;
  3424. }
  3425. if (TREE_CODE (rhs1_type) != VECTOR_TYPE
  3426. || TREE_CODE (rhs2_type) != VECTOR_TYPE
  3427. || TREE_CODE (rhs3_type) != VECTOR_TYPE)
  3428. {
  3429. error ("vector types expected in sad expression");
  3430. debug_generic_expr (lhs_type);
  3431. debug_generic_expr (rhs1_type);
  3432. debug_generic_expr (rhs2_type);
  3433. debug_generic_expr (rhs3_type);
  3434. return true;
  3435. }
  3436. return false;
  3437. case DOT_PROD_EXPR:
  3438. case REALIGN_LOAD_EXPR:
  3439. /* FIXME. */
  3440. return false;
  3441. default:
  3442. gcc_unreachable ();
  3443. }
  3444. return false;
  3445. }
  3446. /* Verify a gimple assignment statement STMT with a single rhs.
  3447. Returns true if anything is wrong. */
  3448. static bool
  3449. verify_gimple_assign_single (gassign *stmt)
  3450. {
  3451. enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
  3452. tree lhs = gimple_assign_lhs (stmt);
  3453. tree lhs_type = TREE_TYPE (lhs);
  3454. tree rhs1 = gimple_assign_rhs1 (stmt);
  3455. tree rhs1_type = TREE_TYPE (rhs1);
  3456. bool res = false;
  3457. if (!useless_type_conversion_p (lhs_type, rhs1_type))
  3458. {
  3459. error ("non-trivial conversion at assignment");
  3460. debug_generic_expr (lhs_type);
  3461. debug_generic_expr (rhs1_type);
  3462. return true;
  3463. }
  3464. if (gimple_clobber_p (stmt)
  3465. && !(DECL_P (lhs) || TREE_CODE (lhs) == MEM_REF))
  3466. {
  3467. error ("non-decl/MEM_REF LHS in clobber statement");
  3468. debug_generic_expr (lhs);
  3469. return true;
  3470. }
  3471. if (handled_component_p (lhs)
  3472. || TREE_CODE (lhs) == MEM_REF
  3473. || TREE_CODE (lhs) == TARGET_MEM_REF)
  3474. res |= verify_types_in_gimple_reference (lhs, true);
  3475. /* Special codes we cannot handle via their class. */
  3476. switch (rhs_code)
  3477. {
  3478. case ADDR_EXPR:
  3479. {
  3480. tree op = TREE_OPERAND (rhs1, 0);
  3481. if (!is_gimple_addressable (op))
  3482. {
  3483. error ("invalid operand in unary expression");
  3484. return true;
  3485. }
  3486. /* Technically there is no longer a need for matching types, but
  3487. gimple hygiene asks for this check. In LTO we can end up
  3488. combining incompatible units and thus end up with addresses
  3489. of globals that change their type to a common one. */
  3490. if (!in_lto_p
  3491. && !types_compatible_p (TREE_TYPE (op),
  3492. TREE_TYPE (TREE_TYPE (rhs1)))
  3493. && !one_pointer_to_useless_type_conversion_p (TREE_TYPE (rhs1),
  3494. TREE_TYPE (op)))
  3495. {
  3496. error ("type mismatch in address expression");
  3497. debug_generic_stmt (TREE_TYPE (rhs1));
  3498. debug_generic_stmt (TREE_TYPE (op));
  3499. return true;
  3500. }
  3501. return verify_types_in_gimple_reference (op, true);
  3502. }
  3503. /* tcc_reference */
  3504. case INDIRECT_REF:
  3505. error ("INDIRECT_REF in gimple IL");
  3506. return true;
  3507. case COMPONENT_REF:
  3508. case BIT_FIELD_REF:
  3509. case ARRAY_REF:
  3510. case ARRAY_RANGE_REF:
  3511. case VIEW_CONVERT_EXPR:
  3512. case REALPART_EXPR:
  3513. case IMAGPART_EXPR:
  3514. case TARGET_MEM_REF:
  3515. case MEM_REF:
  3516. if (!is_gimple_reg (lhs)
  3517. && is_gimple_reg_type (TREE_TYPE (lhs)))
  3518. {
  3519. error ("invalid rhs for gimple memory store");
  3520. debug_generic_stmt (lhs);
  3521. debug_generic_stmt (rhs1);
  3522. return true;
  3523. }
  3524. return res || verify_types_in_gimple_reference (rhs1, false);
  3525. /* tcc_constant */
  3526. case SSA_NAME:
  3527. case INTEGER_CST:
  3528. case REAL_CST:
  3529. case FIXED_CST:
  3530. case COMPLEX_CST:
  3531. case VECTOR_CST:
  3532. case STRING_CST:
  3533. return res;
  3534. /* tcc_declaration */
  3535. case CONST_DECL:
  3536. return res;
  3537. case VAR_DECL:
  3538. case PARM_DECL:
  3539. if (!is_gimple_reg (lhs)
  3540. && !is_gimple_reg (rhs1)
  3541. && is_gimple_reg_type (TREE_TYPE (lhs)))
  3542. {
  3543. error ("invalid rhs for gimple memory store");
  3544. debug_generic_stmt (lhs);
  3545. debug_generic_stmt (rhs1);
  3546. return true;
  3547. }
  3548. return res;
  3549. case CONSTRUCTOR:
  3550. if (TREE_CODE (rhs1_type) == VECTOR_TYPE)
  3551. {
  3552. unsigned int i;
  3553. tree elt_i, elt_v, elt_t = NULL_TREE;
  3554. if (CONSTRUCTOR_NELTS (rhs1) == 0)
  3555. return res;
  3556. /* For vector CONSTRUCTORs we require that either it is empty
  3557. CONSTRUCTOR, or it is a CONSTRUCTOR of smaller vector elements
  3558. (then the element count must be correct to cover the whole
  3559. outer vector and index must be NULL on all elements, or it is
  3560. a CONSTRUCTOR of scalar elements, where we as an exception allow
  3561. smaller number of elements (assuming zero filling) and
  3562. consecutive indexes as compared to NULL indexes (such
  3563. CONSTRUCTORs can appear in the IL from FEs). */
  3564. FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (rhs1), i, elt_i, elt_v)
  3565. {
  3566. if (elt_t == NULL_TREE)
  3567. {
  3568. elt_t = TREE_TYPE (elt_v);
  3569. if (TREE_CODE (elt_t) == VECTOR_TYPE)
  3570. {
  3571. tree elt_t = TREE_TYPE (elt_v);
  3572. if (!useless_type_conversion_p (TREE_TYPE (rhs1_type),
  3573. TREE_TYPE (elt_t)))
  3574. {
  3575. error ("incorrect type of vector CONSTRUCTOR"
  3576. " elements");
  3577. debug_generic_stmt (rhs1);
  3578. return true;
  3579. }
  3580. else if (CONSTRUCTOR_NELTS (rhs1)
  3581. * TYPE_VECTOR_SUBPARTS (elt_t)
  3582. != TYPE_VECTOR_SUBPARTS (rhs1_type))
  3583. {
  3584. error ("incorrect number of vector CONSTRUCTOR"
  3585. " elements");
  3586. debug_generic_stmt (rhs1);
  3587. return true;
  3588. }
  3589. }
  3590. else if (!useless_type_conversion_p (TREE_TYPE (rhs1_type),
  3591. elt_t))
  3592. {
  3593. error ("incorrect type of vector CONSTRUCTOR elements");
  3594. debug_generic_stmt (rhs1);
  3595. return true;
  3596. }
  3597. else if (CONSTRUCTOR_NELTS (rhs1)
  3598. > TYPE_VECTOR_SUBPARTS (rhs1_type))
  3599. {
  3600. error ("incorrect number of vector CONSTRUCTOR elements");
  3601. debug_generic_stmt (rhs1);
  3602. return true;
  3603. }
  3604. }
  3605. else if (!useless_type_conversion_p (elt_t, TREE_TYPE (elt_v)))
  3606. {
  3607. error ("incorrect type of vector CONSTRUCTOR elements");
  3608. debug_generic_stmt (rhs1);
  3609. return true;
  3610. }
  3611. if (elt_i != NULL_TREE
  3612. && (TREE_CODE (elt_t) == VECTOR_TYPE
  3613. || TREE_CODE (elt_i) != INTEGER_CST
  3614. || compare_tree_int (elt_i, i) != 0))
  3615. {
  3616. error ("vector CONSTRUCTOR with non-NULL element index");
  3617. debug_generic_stmt (rhs1);
  3618. return true;
  3619. }
  3620. if (!is_gimple_val (elt_v))
  3621. {
  3622. error ("vector CONSTRUCTOR element is not a GIMPLE value");
  3623. debug_generic_stmt (rhs1);
  3624. return true;
  3625. }
  3626. }
  3627. }
  3628. else if (CONSTRUCTOR_NELTS (rhs1) != 0)
  3629. {
  3630. error ("non-vector CONSTRUCTOR with elements");
  3631. debug_generic_stmt (rhs1);
  3632. return true;
  3633. }
  3634. return res;
  3635. case OBJ_TYPE_REF:
  3636. case ASSERT_EXPR:
  3637. case WITH_SIZE_EXPR:
  3638. /* FIXME. */
  3639. return res;
  3640. default:;
  3641. }
  3642. return res;
  3643. }
  3644. /* Verify the contents of a GIMPLE_ASSIGN STMT. Returns true when there
  3645. is a problem, otherwise false. */
  3646. static bool
  3647. verify_gimple_assign (gassign *stmt)
  3648. {
  3649. switch (gimple_assign_rhs_class (stmt))
  3650. {
  3651. case GIMPLE_SINGLE_RHS:
  3652. return verify_gimple_assign_single (stmt);
  3653. case GIMPLE_UNARY_RHS:
  3654. return verify_gimple_assign_unary (stmt);
  3655. case GIMPLE_BINARY_RHS:
  3656. return verify_gimple_assign_binary (stmt);
  3657. case GIMPLE_TERNARY_RHS:
  3658. return verify_gimple_assign_ternary (stmt);
  3659. default:
  3660. gcc_unreachable ();
  3661. }
  3662. }
  3663. /* Verify the contents of a GIMPLE_RETURN STMT. Returns true when there
  3664. is a problem, otherwise false. */
  3665. static bool
  3666. verify_gimple_return (greturn *stmt)
  3667. {
  3668. tree op = gimple_return_retval (stmt);
  3669. tree restype = TREE_TYPE (TREE_TYPE (cfun->decl));
  3670. /* We cannot test for present return values as we do not fix up missing
  3671. return values from the original source. */
  3672. if (op == NULL)
  3673. return false;
  3674. if (!is_gimple_val (op)
  3675. && TREE_CODE (op) != RESULT_DECL)
  3676. {
  3677. error ("invalid operand in return statement");
  3678. debug_generic_stmt (op);
  3679. return true;
  3680. }
  3681. if ((TREE_CODE (op) == RESULT_DECL
  3682. && DECL_BY_REFERENCE (op))
  3683. || (TREE_CODE (op) == SSA_NAME
  3684. && SSA_NAME_VAR (op)
  3685. && TREE_CODE (SSA_NAME_VAR (op)) == RESULT_DECL
  3686. && DECL_BY_REFERENCE (SSA_NAME_VAR (op))))
  3687. op = TREE_TYPE (op);
  3688. if (!useless_type_conversion_p (restype, TREE_TYPE (op)))
  3689. {
  3690. error ("invalid conversion in return statement");
  3691. debug_generic_stmt (restype);
  3692. debug_generic_stmt (TREE_TYPE (op));
  3693. return true;
  3694. }
  3695. return false;
  3696. }
  3697. /* Verify the contents of a GIMPLE_GOTO STMT. Returns true when there
  3698. is a problem, otherwise false. */
  3699. static bool
  3700. verify_gimple_goto (ggoto *stmt)
  3701. {
  3702. tree dest = gimple_goto_dest (stmt);
  3703. /* ??? We have two canonical forms of direct goto destinations, a
  3704. bare LABEL_DECL and an ADDR_EXPR of a LABEL_DECL. */
  3705. if (TREE_CODE (dest) != LABEL_DECL
  3706. && (!is_gimple_val (dest)
  3707. || !POINTER_TYPE_P (TREE_TYPE (dest))))
  3708. {
  3709. error ("goto destination is neither a label nor a pointer");
  3710. return true;
  3711. }
  3712. return false;
  3713. }
  3714. /* Verify the contents of a GIMPLE_SWITCH STMT. Returns true when there
  3715. is a problem, otherwise false. */
  3716. static bool
  3717. verify_gimple_switch (gswitch *stmt)
  3718. {
  3719. unsigned int i, n;
  3720. tree elt, prev_upper_bound = NULL_TREE;
  3721. tree index_type, elt_type = NULL_TREE;
  3722. if (!is_gimple_val (gimple_switch_index (stmt)))
  3723. {
  3724. error ("invalid operand to switch statement");
  3725. debug_generic_stmt (gimple_switch_index (stmt));
  3726. return true;
  3727. }
  3728. index_type = TREE_TYPE (gimple_switch_index (stmt));
  3729. if (! INTEGRAL_TYPE_P (index_type))
  3730. {
  3731. error ("non-integral type switch statement");
  3732. debug_generic_expr (index_type);
  3733. return true;
  3734. }
  3735. elt = gimple_switch_label (stmt, 0);
  3736. if (CASE_LOW (elt) != NULL_TREE || CASE_HIGH (elt) != NULL_TREE)
  3737. {
  3738. error ("invalid default case label in switch statement");
  3739. debug_generic_expr (elt);
  3740. return true;
  3741. }
  3742. n = gimple_switch_num_labels (stmt);
  3743. for (i = 1; i < n; i++)
  3744. {
  3745. elt = gimple_switch_label (stmt, i);
  3746. if (! CASE_LOW (elt))
  3747. {
  3748. error ("invalid case label in switch statement");
  3749. debug_generic_expr (elt);
  3750. return true;
  3751. }
  3752. if (CASE_HIGH (elt)
  3753. && ! tree_int_cst_lt (CASE_LOW (elt), CASE_HIGH (elt)))
  3754. {
  3755. error ("invalid case range in switch statement");
  3756. debug_generic_expr (elt);
  3757. return true;
  3758. }
  3759. if (elt_type)
  3760. {
  3761. if (TREE_TYPE (CASE_LOW (elt)) != elt_type
  3762. || (CASE_HIGH (elt) && TREE_TYPE (CASE_HIGH (elt)) != elt_type))
  3763. {
  3764. error ("type mismatch for case label in switch statement");
  3765. debug_generic_expr (elt);
  3766. return true;
  3767. }
  3768. }
  3769. else
  3770. {
  3771. elt_type = TREE_TYPE (CASE_LOW (elt));
  3772. if (TYPE_PRECISION (index_type) < TYPE_PRECISION (elt_type))
  3773. {
  3774. error ("type precision mismatch in switch statement");
  3775. return true;
  3776. }
  3777. }
  3778. if (prev_upper_bound)
  3779. {
  3780. if (! tree_int_cst_lt (prev_upper_bound, CASE_LOW (elt)))
  3781. {
  3782. error ("case labels not sorted in switch statement");
  3783. return true;
  3784. }
  3785. }
  3786. prev_upper_bound = CASE_HIGH (elt);
  3787. if (! prev_upper_bound)
  3788. prev_upper_bound = CASE_LOW (elt);
  3789. }
  3790. return false;
  3791. }
  3792. /* Verify a gimple debug statement STMT.
  3793. Returns true if anything is wrong. */
  3794. static bool
  3795. verify_gimple_debug (gimple stmt ATTRIBUTE_UNUSED)
  3796. {
  3797. /* There isn't much that could be wrong in a gimple debug stmt. A
  3798. gimple debug bind stmt, for example, maps a tree, that's usually
  3799. a VAR_DECL or a PARM_DECL, but that could also be some scalarized
  3800. component or member of an aggregate type, to another tree, that
  3801. can be an arbitrary expression. These stmts expand into debug
  3802. insns, and are converted to debug notes by var-tracking.c. */
  3803. return false;
  3804. }
  3805. /* Verify a gimple label statement STMT.
  3806. Returns true if anything is wrong. */
  3807. static bool
  3808. verify_gimple_label (glabel *stmt)
  3809. {
  3810. tree decl = gimple_label_label (stmt);
  3811. int uid;
  3812. bool err = false;
  3813. if (TREE_CODE (decl) != LABEL_DECL)
  3814. return true;
  3815. if (!DECL_NONLOCAL (decl) && !FORCED_LABEL (decl)
  3816. && DECL_CONTEXT (decl) != current_function_decl)
  3817. {
  3818. error ("label's context is not the current function decl");
  3819. err |= true;
  3820. }
  3821. uid = LABEL_DECL_UID (decl);
  3822. if (cfun->cfg
  3823. && (uid == -1
  3824. || (*label_to_block_map_for_fn (cfun))[uid] != gimple_bb (stmt)))
  3825. {
  3826. error ("incorrect entry in label_to_block_map");
  3827. err |= true;
  3828. }
  3829. uid = EH_LANDING_PAD_NR (decl);
  3830. if (uid)
  3831. {
  3832. eh_landing_pad lp = get_eh_landing_pad_from_number (uid);
  3833. if (decl != lp->post_landing_pad)
  3834. {
  3835. error ("incorrect setting of landing pad number");
  3836. err |= true;
  3837. }
  3838. }
  3839. return err;
  3840. }
  3841. /* Verify a gimple cond statement STMT.
  3842. Returns true if anything is wrong. */
  3843. static bool
  3844. verify_gimple_cond (gcond *stmt)
  3845. {
  3846. if (TREE_CODE_CLASS (gimple_cond_code (stmt)) != tcc_comparison)
  3847. {
  3848. error ("invalid comparison code in gimple cond");
  3849. return true;
  3850. }
  3851. if (!(!gimple_cond_true_label (stmt)
  3852. || TREE_CODE (gimple_cond_true_label (stmt)) == LABEL_DECL)
  3853. || !(!gimple_cond_false_label (stmt)
  3854. || TREE_CODE (gimple_cond_false_label (stmt)) == LABEL_DECL))
  3855. {
  3856. error ("invalid labels in gimple cond");
  3857. return true;
  3858. }
  3859. return verify_gimple_comparison (boolean_type_node,
  3860. gimple_cond_lhs (stmt),
  3861. gimple_cond_rhs (stmt));
  3862. }
  3863. /* Verify the GIMPLE statement STMT. Returns true if there is an
  3864. error, otherwise false. */
  3865. static bool
  3866. verify_gimple_stmt (gimple stmt)
  3867. {
  3868. switch (gimple_code (stmt))
  3869. {
  3870. case GIMPLE_ASSIGN:
  3871. return verify_gimple_assign (as_a <gassign *> (stmt));
  3872. case GIMPLE_LABEL:
  3873. return verify_gimple_label (as_a <glabel *> (stmt));
  3874. case GIMPLE_CALL:
  3875. return verify_gimple_call (as_a <gcall *> (stmt));
  3876. case GIMPLE_COND:
  3877. return verify_gimple_cond (as_a <gcond *> (stmt));
  3878. case GIMPLE_GOTO:
  3879. return verify_gimple_goto (as_a <ggoto *> (stmt));
  3880. case GIMPLE_SWITCH:
  3881. return verify_gimple_switch (as_a <gswitch *> (stmt));
  3882. case GIMPLE_RETURN:
  3883. return verify_gimple_return (as_a <greturn *> (stmt));
  3884. case GIMPLE_ASM:
  3885. return false;
  3886. case GIMPLE_TRANSACTION:
  3887. return verify_gimple_transaction (as_a <gtransaction *> (stmt));
  3888. /* Tuples that do not have tree operands. */
  3889. case GIMPLE_NOP:
  3890. case GIMPLE_PREDICT:
  3891. case GIMPLE_RESX:
  3892. case GIMPLE_EH_DISPATCH:
  3893. case GIMPLE_EH_MUST_NOT_THROW:
  3894. return false;
  3895. CASE_GIMPLE_OMP:
  3896. /* OpenMP directives are validated by the FE and never operated
  3897. on by the optimizers. Furthermore, GIMPLE_OMP_FOR may contain
  3898. non-gimple expressions when the main index variable has had
  3899. its address taken. This does not affect the loop itself
  3900. because the header of an GIMPLE_OMP_FOR is merely used to determine
  3901. how to setup the parallel iteration. */
  3902. return false;
  3903. case GIMPLE_DEBUG:
  3904. return verify_gimple_debug (stmt);
  3905. default:
  3906. gcc_unreachable ();
  3907. }
  3908. }
  3909. /* Verify the contents of a GIMPLE_PHI. Returns true if there is a problem,
  3910. and false otherwise. */
  3911. static bool
  3912. verify_gimple_phi (gimple phi)
  3913. {
  3914. bool err = false;
  3915. unsigned i;
  3916. tree phi_result = gimple_phi_result (phi);
  3917. bool virtual_p;
  3918. if (!phi_result)
  3919. {
  3920. error ("invalid PHI result");
  3921. return true;
  3922. }
  3923. virtual_p = virtual_operand_p (phi_result);
  3924. if (TREE_CODE (phi_result) != SSA_NAME
  3925. || (virtual_p
  3926. && SSA_NAME_VAR (phi_result) != gimple_vop (cfun)))
  3927. {
  3928. error ("invalid PHI result");
  3929. err = true;
  3930. }
  3931. for (i = 0; i < gimple_phi_num_args (phi); i++)
  3932. {
  3933. tree t = gimple_phi_arg_def (phi, i);
  3934. if (!t)
  3935. {
  3936. error ("missing PHI def");
  3937. err |= true;
  3938. continue;
  3939. }
  3940. /* Addressable variables do have SSA_NAMEs but they
  3941. are not considered gimple values. */
  3942. else if ((TREE_CODE (t) == SSA_NAME
  3943. && virtual_p != virtual_operand_p (t))
  3944. || (virtual_p
  3945. && (TREE_CODE (t) != SSA_NAME
  3946. || SSA_NAME_VAR (t) != gimple_vop (cfun)))
  3947. || (!virtual_p
  3948. && !is_gimple_val (t)))
  3949. {
  3950. error ("invalid PHI argument");
  3951. debug_generic_expr (t);
  3952. err |= true;
  3953. }
  3954. #ifdef ENABLE_TYPES_CHECKING
  3955. if (!useless_type_conversion_p (TREE_TYPE (phi_result), TREE_TYPE (t)))
  3956. {
  3957. error ("incompatible types in PHI argument %u", i);
  3958. debug_generic_stmt (TREE_TYPE (phi_result));
  3959. debug_generic_stmt (TREE_TYPE (t));
  3960. err |= true;
  3961. }
  3962. #endif
  3963. }
  3964. return err;
  3965. }
  3966. /* Verify the GIMPLE statements inside the sequence STMTS. */
  3967. static bool
  3968. verify_gimple_in_seq_2 (gimple_seq stmts)
  3969. {
  3970. gimple_stmt_iterator ittr;
  3971. bool err = false;
  3972. for (ittr = gsi_start (stmts); !gsi_end_p (ittr); gsi_next (&ittr))
  3973. {
  3974. gimple stmt = gsi_stmt (ittr);
  3975. switch (gimple_code (stmt))
  3976. {
  3977. case GIMPLE_BIND:
  3978. err |= verify_gimple_in_seq_2 (
  3979. gimple_bind_body (as_a <gbind *> (stmt)));
  3980. break;
  3981. case GIMPLE_TRY:
  3982. err |= verify_gimple_in_seq_2 (gimple_try_eval (stmt));
  3983. err |= verify_gimple_in_seq_2 (gimple_try_cleanup (stmt));
  3984. break;
  3985. case GIMPLE_EH_FILTER:
  3986. err |= verify_gimple_in_seq_2 (gimple_eh_filter_failure (stmt));
  3987. break;
  3988. case GIMPLE_EH_ELSE:
  3989. {
  3990. geh_else *eh_else = as_a <geh_else *> (stmt);
  3991. err |= verify_gimple_in_seq_2 (gimple_eh_else_n_body (eh_else));
  3992. err |= verify_gimple_in_seq_2 (gimple_eh_else_e_body (eh_else));
  3993. }
  3994. break;
  3995. case GIMPLE_CATCH:
  3996. err |= verify_gimple_in_seq_2 (gimple_catch_handler (
  3997. as_a <gcatch *> (stmt)));
  3998. break;
  3999. case GIMPLE_TRANSACTION:
  4000. err |= verify_gimple_transaction (as_a <gtransaction *> (stmt));
  4001. break;
  4002. default:
  4003. {
  4004. bool err2 = verify_gimple_stmt (stmt);
  4005. if (err2)
  4006. debug_gimple_stmt (stmt);
  4007. err |= err2;
  4008. }
  4009. }
  4010. }
  4011. return err;
  4012. }
  4013. /* Verify the contents of a GIMPLE_TRANSACTION. Returns true if there
  4014. is a problem, otherwise false. */
  4015. static bool
  4016. verify_gimple_transaction (gtransaction *stmt)
  4017. {
  4018. tree lab = gimple_transaction_label (stmt);
  4019. if (lab != NULL && TREE_CODE (lab) != LABEL_DECL)
  4020. return true;
  4021. return verify_gimple_in_seq_2 (gimple_transaction_body (stmt));
  4022. }
  4023. /* Verify the GIMPLE statements inside the statement list STMTS. */
  4024. DEBUG_FUNCTION void
  4025. verify_gimple_in_seq (gimple_seq stmts)
  4026. {
  4027. timevar_push (TV_TREE_STMT_VERIFY);
  4028. if (verify_gimple_in_seq_2 (stmts))
  4029. internal_error ("verify_gimple failed");
  4030. timevar_pop (TV_TREE_STMT_VERIFY);
  4031. }
  4032. /* Return true when the T can be shared. */
  4033. static bool
  4034. tree_node_can_be_shared (tree t)
  4035. {
  4036. if (IS_TYPE_OR_DECL_P (t)
  4037. || is_gimple_min_invariant (t)
  4038. || TREE_CODE (t) == SSA_NAME
  4039. || t == error_mark_node
  4040. || TREE_CODE (t) == IDENTIFIER_NODE)
  4041. return true;
  4042. if (TREE_CODE (t) == CASE_LABEL_EXPR)
  4043. return true;
  4044. if (DECL_P (t))
  4045. return true;
  4046. return false;
  4047. }
  4048. /* Called via walk_tree. Verify tree sharing. */
  4049. static tree
  4050. verify_node_sharing_1 (tree *tp, int *walk_subtrees, void *data)
  4051. {
  4052. hash_set<void *> *visited = (hash_set<void *> *) data;
  4053. if (tree_node_can_be_shared (*tp))
  4054. {
  4055. *walk_subtrees = false;
  4056. return NULL;
  4057. }
  4058. if (visited->add (*tp))
  4059. return *tp;
  4060. return NULL;
  4061. }
  4062. /* Called via walk_gimple_stmt. Verify tree sharing. */
  4063. static tree
  4064. verify_node_sharing (tree *tp, int *walk_subtrees, void *data)
  4065. {
  4066. struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
  4067. return verify_node_sharing_1 (tp, walk_subtrees, wi->info);
  4068. }
  4069. static bool eh_error_found;
  4070. bool
  4071. verify_eh_throw_stmt_node (const gimple &stmt, const int &,
  4072. hash_set<gimple> *visited)
  4073. {
  4074. if (!visited->contains (stmt))
  4075. {
  4076. error ("dead STMT in EH table");
  4077. debug_gimple_stmt (stmt);
  4078. eh_error_found = true;
  4079. }
  4080. return true;
  4081. }
  4082. /* Verify if the location LOCs block is in BLOCKS. */
  4083. static bool
  4084. verify_location (hash_set<tree> *blocks, location_t loc)
  4085. {
  4086. tree block = LOCATION_BLOCK (loc);
  4087. if (block != NULL_TREE
  4088. && !blocks->contains (block))
  4089. {
  4090. error ("location references block not in block tree");
  4091. return true;
  4092. }
  4093. if (block != NULL_TREE)
  4094. return verify_location (blocks, BLOCK_SOURCE_LOCATION (block));
  4095. return false;
  4096. }
  4097. /* Called via walk_tree. Verify that expressions have no blocks. */
  4098. static tree
  4099. verify_expr_no_block (tree *tp, int *walk_subtrees, void *)
  4100. {
  4101. if (!EXPR_P (*tp))
  4102. {
  4103. *walk_subtrees = false;
  4104. return NULL;
  4105. }
  4106. location_t loc = EXPR_LOCATION (*tp);
  4107. if (LOCATION_BLOCK (loc) != NULL)
  4108. return *tp;
  4109. return NULL;
  4110. }
  4111. /* Called via walk_tree. Verify locations of expressions. */
  4112. static tree
  4113. verify_expr_location_1 (tree *tp, int *walk_subtrees, void *data)
  4114. {
  4115. hash_set<tree> *blocks = (hash_set<tree> *) data;
  4116. if (TREE_CODE (*tp) == VAR_DECL
  4117. && DECL_HAS_DEBUG_EXPR_P (*tp))
  4118. {
  4119. tree t = DECL_DEBUG_EXPR (*tp);
  4120. tree addr = walk_tree (&t, verify_expr_no_block, NULL, NULL);
  4121. if (addr)
  4122. return addr;
  4123. }
  4124. if ((TREE_CODE (*tp) == VAR_DECL
  4125. || TREE_CODE (*tp) == PARM_DECL
  4126. || TREE_CODE (*tp) == RESULT_DECL)
  4127. && DECL_HAS_VALUE_EXPR_P (*tp))
  4128. {
  4129. tree t = DECL_VALUE_EXPR (*tp);
  4130. tree addr = walk_tree (&t, verify_expr_no_block, NULL, NULL);
  4131. if (addr)
  4132. return addr;
  4133. }
  4134. if (!EXPR_P (*tp))
  4135. {
  4136. *walk_subtrees = false;
  4137. return NULL;
  4138. }
  4139. location_t loc = EXPR_LOCATION (*tp);
  4140. if (verify_location (blocks, loc))
  4141. return *tp;
  4142. return NULL;
  4143. }
  4144. /* Called via walk_gimple_op. Verify locations of expressions. */
  4145. static tree
  4146. verify_expr_location (tree *tp, int *walk_subtrees, void *data)
  4147. {
  4148. struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
  4149. return verify_expr_location_1 (tp, walk_subtrees, wi->info);
  4150. }
  4151. /* Insert all subblocks of BLOCK into BLOCKS and recurse. */
  4152. static void
  4153. collect_subblocks (hash_set<tree> *blocks, tree block)
  4154. {
  4155. tree t;
  4156. for (t = BLOCK_SUBBLOCKS (block); t; t = BLOCK_CHAIN (t))
  4157. {
  4158. blocks->add (t);
  4159. collect_subblocks (blocks, t);
  4160. }
  4161. }
  4162. /* Verify the GIMPLE statements in the CFG of FN. */
  4163. DEBUG_FUNCTION void
  4164. verify_gimple_in_cfg (struct function *fn, bool verify_nothrow)
  4165. {
  4166. basic_block bb;
  4167. bool err = false;
  4168. timevar_push (TV_TREE_STMT_VERIFY);
  4169. hash_set<void *> visited;
  4170. hash_set<gimple> visited_stmts;
  4171. /* Collect all BLOCKs referenced by the BLOCK tree of FN. */
  4172. hash_set<tree> blocks;
  4173. if (DECL_INITIAL (fn->decl))
  4174. {
  4175. blocks.add (DECL_INITIAL (fn->decl));
  4176. collect_subblocks (&blocks, DECL_INITIAL (fn->decl));
  4177. }
  4178. FOR_EACH_BB_FN (bb, fn)
  4179. {
  4180. gimple_stmt_iterator gsi;
  4181. for (gphi_iterator gpi = gsi_start_phis (bb);
  4182. !gsi_end_p (gpi);
  4183. gsi_next (&gpi))
  4184. {
  4185. gphi *phi = gpi.phi ();
  4186. bool err2 = false;
  4187. unsigned i;
  4188. visited_stmts.add (phi);
  4189. if (gimple_bb (phi) != bb)
  4190. {
  4191. error ("gimple_bb (phi) is set to a wrong basic block");
  4192. err2 = true;
  4193. }
  4194. err2 |= verify_gimple_phi (phi);
  4195. /* Only PHI arguments have locations. */
  4196. if (gimple_location (phi) != UNKNOWN_LOCATION)
  4197. {
  4198. error ("PHI node with location");
  4199. err2 = true;
  4200. }
  4201. for (i = 0; i < gimple_phi_num_args (phi); i++)
  4202. {
  4203. tree arg = gimple_phi_arg_def (phi, i);
  4204. tree addr = walk_tree (&arg, verify_node_sharing_1,
  4205. &visited, NULL);
  4206. if (addr)
  4207. {
  4208. error ("incorrect sharing of tree nodes");
  4209. debug_generic_expr (addr);
  4210. err2 |= true;
  4211. }
  4212. location_t loc = gimple_phi_arg_location (phi, i);
  4213. if (virtual_operand_p (gimple_phi_result (phi))
  4214. && loc != UNKNOWN_LOCATION)
  4215. {
  4216. error ("virtual PHI with argument locations");
  4217. err2 = true;
  4218. }
  4219. addr = walk_tree (&arg, verify_expr_location_1, &blocks, NULL);
  4220. if (addr)
  4221. {
  4222. debug_generic_expr (addr);
  4223. err2 = true;
  4224. }
  4225. err2 |= verify_location (&blocks, loc);
  4226. }
  4227. if (err2)
  4228. debug_gimple_stmt (phi);
  4229. err |= err2;
  4230. }
  4231. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  4232. {
  4233. gimple stmt = gsi_stmt (gsi);
  4234. bool err2 = false;
  4235. struct walk_stmt_info wi;
  4236. tree addr;
  4237. int lp_nr;
  4238. visited_stmts.add (stmt);
  4239. if (gimple_bb (stmt) != bb)
  4240. {
  4241. error ("gimple_bb (stmt) is set to a wrong basic block");
  4242. err2 = true;
  4243. }
  4244. err2 |= verify_gimple_stmt (stmt);
  4245. err2 |= verify_location (&blocks, gimple_location (stmt));
  4246. memset (&wi, 0, sizeof (wi));
  4247. wi.info = (void *) &visited;
  4248. addr = walk_gimple_op (stmt, verify_node_sharing, &wi);
  4249. if (addr)
  4250. {
  4251. error ("incorrect sharing of tree nodes");
  4252. debug_generic_expr (addr);
  4253. err2 |= true;
  4254. }
  4255. memset (&wi, 0, sizeof (wi));
  4256. wi.info = (void *) &blocks;
  4257. addr = walk_gimple_op (stmt, verify_expr_location, &wi);
  4258. if (addr)
  4259. {
  4260. debug_generic_expr (addr);
  4261. err2 |= true;
  4262. }
  4263. /* ??? Instead of not checking these stmts at all the walker
  4264. should know its context via wi. */
  4265. if (!is_gimple_debug (stmt)
  4266. && !is_gimple_omp (stmt))
  4267. {
  4268. memset (&wi, 0, sizeof (wi));
  4269. addr = walk_gimple_op (stmt, verify_expr, &wi);
  4270. if (addr)
  4271. {
  4272. debug_generic_expr (addr);
  4273. inform (gimple_location (stmt), "in statement");
  4274. err2 |= true;
  4275. }
  4276. }
  4277. /* If the statement is marked as part of an EH region, then it is
  4278. expected that the statement could throw. Verify that when we
  4279. have optimizations that simplify statements such that we prove
  4280. that they cannot throw, that we update other data structures
  4281. to match. */
  4282. lp_nr = lookup_stmt_eh_lp (stmt);
  4283. if (lp_nr > 0)
  4284. {
  4285. if (!stmt_could_throw_p (stmt))
  4286. {
  4287. if (verify_nothrow)
  4288. {
  4289. error ("statement marked for throw, but doesn%'t");
  4290. err2 |= true;
  4291. }
  4292. }
  4293. else if (!gsi_one_before_end_p (gsi))
  4294. {
  4295. error ("statement marked for throw in middle of block");
  4296. err2 |= true;
  4297. }
  4298. }
  4299. if (err2)
  4300. debug_gimple_stmt (stmt);
  4301. err |= err2;
  4302. }
  4303. }
  4304. eh_error_found = false;
  4305. hash_map<gimple, int> *eh_table = get_eh_throw_stmt_table (cfun);
  4306. if (eh_table)
  4307. eh_table->traverse<hash_set<gimple> *, verify_eh_throw_stmt_node>
  4308. (&visited_stmts);
  4309. if (err || eh_error_found)
  4310. internal_error ("verify_gimple failed");
  4311. verify_histograms ();
  4312. timevar_pop (TV_TREE_STMT_VERIFY);
  4313. }
  4314. /* Verifies that the flow information is OK. */
  4315. static int
  4316. gimple_verify_flow_info (void)
  4317. {
  4318. int err = 0;
  4319. basic_block bb;
  4320. gimple_stmt_iterator gsi;
  4321. gimple stmt;
  4322. edge e;
  4323. edge_iterator ei;
  4324. if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->il.gimple.seq
  4325. || ENTRY_BLOCK_PTR_FOR_FN (cfun)->il.gimple.phi_nodes)
  4326. {
  4327. error ("ENTRY_BLOCK has IL associated with it");
  4328. err = 1;
  4329. }
  4330. if (EXIT_BLOCK_PTR_FOR_FN (cfun)->il.gimple.seq
  4331. || EXIT_BLOCK_PTR_FOR_FN (cfun)->il.gimple.phi_nodes)
  4332. {
  4333. error ("EXIT_BLOCK has IL associated with it");
  4334. err = 1;
  4335. }
  4336. FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
  4337. if (e->flags & EDGE_FALLTHRU)
  4338. {
  4339. error ("fallthru to exit from bb %d", e->src->index);
  4340. err = 1;
  4341. }
  4342. FOR_EACH_BB_FN (bb, cfun)
  4343. {
  4344. bool found_ctrl_stmt = false;
  4345. stmt = NULL;
  4346. /* Skip labels on the start of basic block. */
  4347. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  4348. {
  4349. tree label;
  4350. gimple prev_stmt = stmt;
  4351. stmt = gsi_stmt (gsi);
  4352. if (gimple_code (stmt) != GIMPLE_LABEL)
  4353. break;
  4354. label = gimple_label_label (as_a <glabel *> (stmt));
  4355. if (prev_stmt && DECL_NONLOCAL (label))
  4356. {
  4357. error ("nonlocal label ");
  4358. print_generic_expr (stderr, label, 0);
  4359. fprintf (stderr, " is not first in a sequence of labels in bb %d",
  4360. bb->index);
  4361. err = 1;
  4362. }
  4363. if (prev_stmt && EH_LANDING_PAD_NR (label) != 0)
  4364. {
  4365. error ("EH landing pad label ");
  4366. print_generic_expr (stderr, label, 0);
  4367. fprintf (stderr, " is not first in a sequence of labels in bb %d",
  4368. bb->index);
  4369. err = 1;
  4370. }
  4371. if (label_to_block (label) != bb)
  4372. {
  4373. error ("label ");
  4374. print_generic_expr (stderr, label, 0);
  4375. fprintf (stderr, " to block does not match in bb %d",
  4376. bb->index);
  4377. err = 1;
  4378. }
  4379. if (decl_function_context (label) != current_function_decl)
  4380. {
  4381. error ("label ");
  4382. print_generic_expr (stderr, label, 0);
  4383. fprintf (stderr, " has incorrect context in bb %d",
  4384. bb->index);
  4385. err = 1;
  4386. }
  4387. }
  4388. /* Verify that body of basic block BB is free of control flow. */
  4389. for (; !gsi_end_p (gsi); gsi_next (&gsi))
  4390. {
  4391. gimple stmt = gsi_stmt (gsi);
  4392. if (found_ctrl_stmt)
  4393. {
  4394. error ("control flow in the middle of basic block %d",
  4395. bb->index);
  4396. err = 1;
  4397. }
  4398. if (stmt_ends_bb_p (stmt))
  4399. found_ctrl_stmt = true;
  4400. if (glabel *label_stmt = dyn_cast <glabel *> (stmt))
  4401. {
  4402. error ("label ");
  4403. print_generic_expr (stderr, gimple_label_label (label_stmt), 0);
  4404. fprintf (stderr, " in the middle of basic block %d", bb->index);
  4405. err = 1;
  4406. }
  4407. }
  4408. gsi = gsi_last_bb (bb);
  4409. if (gsi_end_p (gsi))
  4410. continue;
  4411. stmt = gsi_stmt (gsi);
  4412. if (gimple_code (stmt) == GIMPLE_LABEL)
  4413. continue;
  4414. err |= verify_eh_edges (stmt);
  4415. if (is_ctrl_stmt (stmt))
  4416. {
  4417. FOR_EACH_EDGE (e, ei, bb->succs)
  4418. if (e->flags & EDGE_FALLTHRU)
  4419. {
  4420. error ("fallthru edge after a control statement in bb %d",
  4421. bb->index);
  4422. err = 1;
  4423. }
  4424. }
  4425. if (gimple_code (stmt) != GIMPLE_COND)
  4426. {
  4427. /* Verify that there are no edges with EDGE_TRUE/FALSE_FLAG set
  4428. after anything else but if statement. */
  4429. FOR_EACH_EDGE (e, ei, bb->succs)
  4430. if (e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE))
  4431. {
  4432. error ("true/false edge after a non-GIMPLE_COND in bb %d",
  4433. bb->index);
  4434. err = 1;
  4435. }
  4436. }
  4437. switch (gimple_code (stmt))
  4438. {
  4439. case GIMPLE_COND:
  4440. {
  4441. edge true_edge;
  4442. edge false_edge;
  4443. extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
  4444. if (!true_edge
  4445. || !false_edge
  4446. || !(true_edge->flags & EDGE_TRUE_VALUE)
  4447. || !(false_edge->flags & EDGE_FALSE_VALUE)
  4448. || (true_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
  4449. || (false_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
  4450. || EDGE_COUNT (bb->succs) >= 3)
  4451. {
  4452. error ("wrong outgoing edge flags at end of bb %d",
  4453. bb->index);
  4454. err = 1;
  4455. }
  4456. }
  4457. break;
  4458. case GIMPLE_GOTO:
  4459. if (simple_goto_p (stmt))
  4460. {
  4461. error ("explicit goto at end of bb %d", bb->index);
  4462. err = 1;
  4463. }
  4464. else
  4465. {
  4466. /* FIXME. We should double check that the labels in the
  4467. destination blocks have their address taken. */
  4468. FOR_EACH_EDGE (e, ei, bb->succs)
  4469. if ((e->flags & (EDGE_FALLTHRU | EDGE_TRUE_VALUE
  4470. | EDGE_FALSE_VALUE))
  4471. || !(e->flags & EDGE_ABNORMAL))
  4472. {
  4473. error ("wrong outgoing edge flags at end of bb %d",
  4474. bb->index);
  4475. err = 1;
  4476. }
  4477. }
  4478. break;
  4479. case GIMPLE_CALL:
  4480. if (!gimple_call_builtin_p (stmt, BUILT_IN_RETURN))
  4481. break;
  4482. /* ... fallthru ... */
  4483. case GIMPLE_RETURN:
  4484. if (!single_succ_p (bb)
  4485. || (single_succ_edge (bb)->flags
  4486. & (EDGE_FALLTHRU | EDGE_ABNORMAL
  4487. | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
  4488. {
  4489. error ("wrong outgoing edge flags at end of bb %d", bb->index);
  4490. err = 1;
  4491. }
  4492. if (single_succ (bb) != EXIT_BLOCK_PTR_FOR_FN (cfun))
  4493. {
  4494. error ("return edge does not point to exit in bb %d",
  4495. bb->index);
  4496. err = 1;
  4497. }
  4498. break;
  4499. case GIMPLE_SWITCH:
  4500. {
  4501. gswitch *switch_stmt = as_a <gswitch *> (stmt);
  4502. tree prev;
  4503. edge e;
  4504. size_t i, n;
  4505. n = gimple_switch_num_labels (switch_stmt);
  4506. /* Mark all the destination basic blocks. */
  4507. for (i = 0; i < n; ++i)
  4508. {
  4509. tree lab = CASE_LABEL (gimple_switch_label (switch_stmt, i));
  4510. basic_block label_bb = label_to_block (lab);
  4511. gcc_assert (!label_bb->aux || label_bb->aux == (void *)1);
  4512. label_bb->aux = (void *)1;
  4513. }
  4514. /* Verify that the case labels are sorted. */
  4515. prev = gimple_switch_label (switch_stmt, 0);
  4516. for (i = 1; i < n; ++i)
  4517. {
  4518. tree c = gimple_switch_label (switch_stmt, i);
  4519. if (!CASE_LOW (c))
  4520. {
  4521. error ("found default case not at the start of "
  4522. "case vector");
  4523. err = 1;
  4524. continue;
  4525. }
  4526. if (CASE_LOW (prev)
  4527. && !tree_int_cst_lt (CASE_LOW (prev), CASE_LOW (c)))
  4528. {
  4529. error ("case labels not sorted: ");
  4530. print_generic_expr (stderr, prev, 0);
  4531. fprintf (stderr," is greater than ");
  4532. print_generic_expr (stderr, c, 0);
  4533. fprintf (stderr," but comes before it.\n");
  4534. err = 1;
  4535. }
  4536. prev = c;
  4537. }
  4538. /* VRP will remove the default case if it can prove it will
  4539. never be executed. So do not verify there always exists
  4540. a default case here. */
  4541. FOR_EACH_EDGE (e, ei, bb->succs)
  4542. {
  4543. if (!e->dest->aux)
  4544. {
  4545. error ("extra outgoing edge %d->%d",
  4546. bb->index, e->dest->index);
  4547. err = 1;
  4548. }
  4549. e->dest->aux = (void *)2;
  4550. if ((e->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL
  4551. | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
  4552. {
  4553. error ("wrong outgoing edge flags at end of bb %d",
  4554. bb->index);
  4555. err = 1;
  4556. }
  4557. }
  4558. /* Check that we have all of them. */
  4559. for (i = 0; i < n; ++i)
  4560. {
  4561. tree lab = CASE_LABEL (gimple_switch_label (switch_stmt, i));
  4562. basic_block label_bb = label_to_block (lab);
  4563. if (label_bb->aux != (void *)2)
  4564. {
  4565. error ("missing edge %i->%i", bb->index, label_bb->index);
  4566. err = 1;
  4567. }
  4568. }
  4569. FOR_EACH_EDGE (e, ei, bb->succs)
  4570. e->dest->aux = (void *)0;
  4571. }
  4572. break;
  4573. case GIMPLE_EH_DISPATCH:
  4574. err |= verify_eh_dispatch_edge (as_a <geh_dispatch *> (stmt));
  4575. break;
  4576. default:
  4577. break;
  4578. }
  4579. }
  4580. if (dom_info_state (CDI_DOMINATORS) >= DOM_NO_FAST_QUERY)
  4581. verify_dominators (CDI_DOMINATORS);
  4582. return err;
  4583. }
  4584. /* Updates phi nodes after creating a forwarder block joined
  4585. by edge FALLTHRU. */
  4586. static void
  4587. gimple_make_forwarder_block (edge fallthru)
  4588. {
  4589. edge e;
  4590. edge_iterator ei;
  4591. basic_block dummy, bb;
  4592. tree var;
  4593. gphi_iterator gsi;
  4594. dummy = fallthru->src;
  4595. bb = fallthru->dest;
  4596. if (single_pred_p (bb))
  4597. return;
  4598. /* If we redirected a branch we must create new PHI nodes at the
  4599. start of BB. */
  4600. for (gsi = gsi_start_phis (dummy); !gsi_end_p (gsi); gsi_next (&gsi))
  4601. {
  4602. gphi *phi, *new_phi;
  4603. phi = gsi.phi ();
  4604. var = gimple_phi_result (phi);
  4605. new_phi = create_phi_node (var, bb);
  4606. gimple_phi_set_result (phi, copy_ssa_name (var, phi));
  4607. add_phi_arg (new_phi, gimple_phi_result (phi), fallthru,
  4608. UNKNOWN_LOCATION);
  4609. }
  4610. /* Add the arguments we have stored on edges. */
  4611. FOR_EACH_EDGE (e, ei, bb->preds)
  4612. {
  4613. if (e == fallthru)
  4614. continue;
  4615. flush_pending_stmts (e);
  4616. }
  4617. }
  4618. /* Return a non-special label in the head of basic block BLOCK.
  4619. Create one if it doesn't exist. */
  4620. tree
  4621. gimple_block_label (basic_block bb)
  4622. {
  4623. gimple_stmt_iterator i, s = gsi_start_bb (bb);
  4624. bool first = true;
  4625. tree label;
  4626. glabel *stmt;
  4627. for (i = s; !gsi_end_p (i); first = false, gsi_next (&i))
  4628. {
  4629. stmt = dyn_cast <glabel *> (gsi_stmt (i));
  4630. if (!stmt)
  4631. break;
  4632. label = gimple_label_label (stmt);
  4633. if (!DECL_NONLOCAL (label))
  4634. {
  4635. if (!first)
  4636. gsi_move_before (&i, &s);
  4637. return label;
  4638. }
  4639. }
  4640. label = create_artificial_label (UNKNOWN_LOCATION);
  4641. stmt = gimple_build_label (label);
  4642. gsi_insert_before (&s, stmt, GSI_NEW_STMT);
  4643. return label;
  4644. }
  4645. /* Attempt to perform edge redirection by replacing a possibly complex
  4646. jump instruction by a goto or by removing the jump completely.
  4647. This can apply only if all edges now point to the same block. The
  4648. parameters and return values are equivalent to
  4649. redirect_edge_and_branch. */
  4650. static edge
  4651. gimple_try_redirect_by_replacing_jump (edge e, basic_block target)
  4652. {
  4653. basic_block src = e->src;
  4654. gimple_stmt_iterator i;
  4655. gimple stmt;
  4656. /* We can replace or remove a complex jump only when we have exactly
  4657. two edges. */
  4658. if (EDGE_COUNT (src->succs) != 2
  4659. /* Verify that all targets will be TARGET. Specifically, the
  4660. edge that is not E must also go to TARGET. */
  4661. || EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest != target)
  4662. return NULL;
  4663. i = gsi_last_bb (src);
  4664. if (gsi_end_p (i))
  4665. return NULL;
  4666. stmt = gsi_stmt (i);
  4667. if (gimple_code (stmt) == GIMPLE_COND || gimple_code (stmt) == GIMPLE_SWITCH)
  4668. {
  4669. gsi_remove (&i, true);
  4670. e = ssa_redirect_edge (e, target);
  4671. e->flags = EDGE_FALLTHRU;
  4672. return e;
  4673. }
  4674. return NULL;
  4675. }
  4676. /* Redirect E to DEST. Return NULL on failure. Otherwise, return the
  4677. edge representing the redirected branch. */
  4678. static edge
  4679. gimple_redirect_edge_and_branch (edge e, basic_block dest)
  4680. {
  4681. basic_block bb = e->src;
  4682. gimple_stmt_iterator gsi;
  4683. edge ret;
  4684. gimple stmt;
  4685. if (e->flags & EDGE_ABNORMAL)
  4686. return NULL;
  4687. if (e->dest == dest)
  4688. return NULL;
  4689. if (e->flags & EDGE_EH)
  4690. return redirect_eh_edge (e, dest);
  4691. if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
  4692. {
  4693. ret = gimple_try_redirect_by_replacing_jump (e, dest);
  4694. if (ret)
  4695. return ret;
  4696. }
  4697. gsi = gsi_last_bb (bb);
  4698. stmt = gsi_end_p (gsi) ? NULL : gsi_stmt (gsi);
  4699. switch (stmt ? gimple_code (stmt) : GIMPLE_ERROR_MARK)
  4700. {
  4701. case GIMPLE_COND:
  4702. /* For COND_EXPR, we only need to redirect the edge. */
  4703. break;
  4704. case GIMPLE_GOTO:
  4705. /* No non-abnormal edges should lead from a non-simple goto, and
  4706. simple ones should be represented implicitly. */
  4707. gcc_unreachable ();
  4708. case GIMPLE_SWITCH:
  4709. {
  4710. gswitch *switch_stmt = as_a <gswitch *> (stmt);
  4711. tree label = gimple_block_label (dest);
  4712. tree cases = get_cases_for_edge (e, switch_stmt);
  4713. /* If we have a list of cases associated with E, then use it
  4714. as it's a lot faster than walking the entire case vector. */
  4715. if (cases)
  4716. {
  4717. edge e2 = find_edge (e->src, dest);
  4718. tree last, first;
  4719. first = cases;
  4720. while (cases)
  4721. {
  4722. last = cases;
  4723. CASE_LABEL (cases) = label;
  4724. cases = CASE_CHAIN (cases);
  4725. }
  4726. /* If there was already an edge in the CFG, then we need
  4727. to move all the cases associated with E to E2. */
  4728. if (e2)
  4729. {
  4730. tree cases2 = get_cases_for_edge (e2, switch_stmt);
  4731. CASE_CHAIN (last) = CASE_CHAIN (cases2);
  4732. CASE_CHAIN (cases2) = first;
  4733. }
  4734. bitmap_set_bit (touched_switch_bbs, gimple_bb (stmt)->index);
  4735. }
  4736. else
  4737. {
  4738. size_t i, n = gimple_switch_num_labels (switch_stmt);
  4739. for (i = 0; i < n; i++)
  4740. {
  4741. tree elt = gimple_switch_label (switch_stmt, i);
  4742. if (label_to_block (CASE_LABEL (elt)) == e->dest)
  4743. CASE_LABEL (elt) = label;
  4744. }
  4745. }
  4746. }
  4747. break;
  4748. case GIMPLE_ASM:
  4749. {
  4750. gasm *asm_stmt = as_a <gasm *> (stmt);
  4751. int i, n = gimple_asm_nlabels (asm_stmt);
  4752. tree label = NULL;
  4753. for (i = 0; i < n; ++i)
  4754. {
  4755. tree cons = gimple_asm_label_op (asm_stmt, i);
  4756. if (label_to_block (TREE_VALUE (cons)) == e->dest)
  4757. {
  4758. if (!label)
  4759. label = gimple_block_label (dest);
  4760. TREE_VALUE (cons) = label;
  4761. }
  4762. }
  4763. /* If we didn't find any label matching the former edge in the
  4764. asm labels, we must be redirecting the fallthrough
  4765. edge. */
  4766. gcc_assert (label || (e->flags & EDGE_FALLTHRU));
  4767. }
  4768. break;
  4769. case GIMPLE_RETURN:
  4770. gsi_remove (&gsi, true);
  4771. e->flags |= EDGE_FALLTHRU;
  4772. break;
  4773. case GIMPLE_OMP_RETURN:
  4774. case GIMPLE_OMP_CONTINUE:
  4775. case GIMPLE_OMP_SECTIONS_SWITCH:
  4776. case GIMPLE_OMP_FOR:
  4777. /* The edges from OMP constructs can be simply redirected. */
  4778. break;
  4779. case GIMPLE_EH_DISPATCH:
  4780. if (!(e->flags & EDGE_FALLTHRU))
  4781. redirect_eh_dispatch_edge (as_a <geh_dispatch *> (stmt), e, dest);
  4782. break;
  4783. case GIMPLE_TRANSACTION:
  4784. /* The ABORT edge has a stored label associated with it, otherwise
  4785. the edges are simply redirectable. */
  4786. if (e->flags == 0)
  4787. gimple_transaction_set_label (as_a <gtransaction *> (stmt),
  4788. gimple_block_label (dest));
  4789. break;
  4790. default:
  4791. /* Otherwise it must be a fallthru edge, and we don't need to
  4792. do anything besides redirecting it. */
  4793. gcc_assert (e->flags & EDGE_FALLTHRU);
  4794. break;
  4795. }
  4796. /* Update/insert PHI nodes as necessary. */
  4797. /* Now update the edges in the CFG. */
  4798. e = ssa_redirect_edge (e, dest);
  4799. return e;
  4800. }
  4801. /* Returns true if it is possible to remove edge E by redirecting
  4802. it to the destination of the other edge from E->src. */
  4803. static bool
  4804. gimple_can_remove_branch_p (const_edge e)
  4805. {
  4806. if (e->flags & (EDGE_ABNORMAL | EDGE_EH))
  4807. return false;
  4808. return true;
  4809. }
  4810. /* Simple wrapper, as we can always redirect fallthru edges. */
  4811. static basic_block
  4812. gimple_redirect_edge_and_branch_force (edge e, basic_block dest)
  4813. {
  4814. e = gimple_redirect_edge_and_branch (e, dest);
  4815. gcc_assert (e);
  4816. return NULL;
  4817. }
  4818. /* Splits basic block BB after statement STMT (but at least after the
  4819. labels). If STMT is NULL, BB is split just after the labels. */
  4820. static basic_block
  4821. gimple_split_block (basic_block bb, void *stmt)
  4822. {
  4823. gimple_stmt_iterator gsi;
  4824. gimple_stmt_iterator gsi_tgt;
  4825. gimple_seq list;
  4826. basic_block new_bb;
  4827. edge e;
  4828. edge_iterator ei;
  4829. new_bb = create_empty_bb (bb);
  4830. /* Redirect the outgoing edges. */
  4831. new_bb->succs = bb->succs;
  4832. bb->succs = NULL;
  4833. FOR_EACH_EDGE (e, ei, new_bb->succs)
  4834. e->src = new_bb;
  4835. /* Get a stmt iterator pointing to the first stmt to move. */
  4836. if (!stmt || gimple_code ((gimple) stmt) == GIMPLE_LABEL)
  4837. gsi = gsi_after_labels (bb);
  4838. else
  4839. {
  4840. gsi = gsi_for_stmt ((gimple) stmt);
  4841. gsi_next (&gsi);
  4842. }
  4843. /* Move everything from GSI to the new basic block. */
  4844. if (gsi_end_p (gsi))
  4845. return new_bb;
  4846. /* Split the statement list - avoid re-creating new containers as this
  4847. brings ugly quadratic memory consumption in the inliner.
  4848. (We are still quadratic since we need to update stmt BB pointers,
  4849. sadly.) */
  4850. gsi_split_seq_before (&gsi, &list);
  4851. set_bb_seq (new_bb, list);
  4852. for (gsi_tgt = gsi_start (list);
  4853. !gsi_end_p (gsi_tgt); gsi_next (&gsi_tgt))
  4854. gimple_set_bb (gsi_stmt (gsi_tgt), new_bb);
  4855. return new_bb;
  4856. }
  4857. /* Moves basic block BB after block AFTER. */
  4858. static bool
  4859. gimple_move_block_after (basic_block bb, basic_block after)
  4860. {
  4861. if (bb->prev_bb == after)
  4862. return true;
  4863. unlink_block (bb);
  4864. link_block (bb, after);
  4865. return true;
  4866. }
  4867. /* Return TRUE if block BB has no executable statements, otherwise return
  4868. FALSE. */
  4869. static bool
  4870. gimple_empty_block_p (basic_block bb)
  4871. {
  4872. /* BB must have no executable statements. */
  4873. gimple_stmt_iterator gsi = gsi_after_labels (bb);
  4874. if (phi_nodes (bb))
  4875. return false;
  4876. if (gsi_end_p (gsi))
  4877. return true;
  4878. if (is_gimple_debug (gsi_stmt (gsi)))
  4879. gsi_next_nondebug (&gsi);
  4880. return gsi_end_p (gsi);
  4881. }
  4882. /* Split a basic block if it ends with a conditional branch and if the
  4883. other part of the block is not empty. */
  4884. static basic_block
  4885. gimple_split_block_before_cond_jump (basic_block bb)
  4886. {
  4887. gimple last, split_point;
  4888. gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
  4889. if (gsi_end_p (gsi))
  4890. return NULL;
  4891. last = gsi_stmt (gsi);
  4892. if (gimple_code (last) != GIMPLE_COND
  4893. && gimple_code (last) != GIMPLE_SWITCH)
  4894. return NULL;
  4895. gsi_prev_nondebug (&gsi);
  4896. split_point = gsi_stmt (gsi);
  4897. return split_block (bb, split_point)->dest;
  4898. }
  4899. /* Return true if basic_block can be duplicated. */
  4900. static bool
  4901. gimple_can_duplicate_bb_p (const_basic_block bb ATTRIBUTE_UNUSED)
  4902. {
  4903. return true;
  4904. }
  4905. /* Create a duplicate of the basic block BB. NOTE: This does not
  4906. preserve SSA form. */
  4907. static basic_block
  4908. gimple_duplicate_bb (basic_block bb)
  4909. {
  4910. basic_block new_bb;
  4911. gimple_stmt_iterator gsi_tgt;
  4912. new_bb = create_empty_bb (EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb);
  4913. /* Copy the PHI nodes. We ignore PHI node arguments here because
  4914. the incoming edges have not been setup yet. */
  4915. for (gphi_iterator gpi = gsi_start_phis (bb);
  4916. !gsi_end_p (gpi);
  4917. gsi_next (&gpi))
  4918. {
  4919. gphi *phi, *copy;
  4920. phi = gpi.phi ();
  4921. copy = create_phi_node (NULL_TREE, new_bb);
  4922. create_new_def_for (gimple_phi_result (phi), copy,
  4923. gimple_phi_result_ptr (copy));
  4924. gimple_set_uid (copy, gimple_uid (phi));
  4925. }
  4926. gsi_tgt = gsi_start_bb (new_bb);
  4927. for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
  4928. !gsi_end_p (gsi);
  4929. gsi_next (&gsi))
  4930. {
  4931. def_operand_p def_p;
  4932. ssa_op_iter op_iter;
  4933. tree lhs;
  4934. gimple stmt, copy;
  4935. stmt = gsi_stmt (gsi);
  4936. if (gimple_code (stmt) == GIMPLE_LABEL)
  4937. continue;
  4938. /* Don't duplicate label debug stmts. */
  4939. if (gimple_debug_bind_p (stmt)
  4940. && TREE_CODE (gimple_debug_bind_get_var (stmt))
  4941. == LABEL_DECL)
  4942. continue;
  4943. /* Create a new copy of STMT and duplicate STMT's virtual
  4944. operands. */
  4945. copy = gimple_copy (stmt);
  4946. gsi_insert_after (&gsi_tgt, copy, GSI_NEW_STMT);
  4947. maybe_duplicate_eh_stmt (copy, stmt);
  4948. gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt);
  4949. /* When copying around a stmt writing into a local non-user
  4950. aggregate, make sure it won't share stack slot with other
  4951. vars. */
  4952. lhs = gimple_get_lhs (stmt);
  4953. if (lhs && TREE_CODE (lhs) != SSA_NAME)
  4954. {
  4955. tree base = get_base_address (lhs);
  4956. if (base
  4957. && (TREE_CODE (base) == VAR_DECL
  4958. || TREE_CODE (base) == RESULT_DECL)
  4959. && DECL_IGNORED_P (base)
  4960. && !TREE_STATIC (base)
  4961. && !DECL_EXTERNAL (base)
  4962. && (TREE_CODE (base) != VAR_DECL
  4963. || !DECL_HAS_VALUE_EXPR_P (base)))
  4964. DECL_NONSHAREABLE (base) = 1;
  4965. }
  4966. /* Create new names for all the definitions created by COPY and
  4967. add replacement mappings for each new name. */
  4968. FOR_EACH_SSA_DEF_OPERAND (def_p, copy, op_iter, SSA_OP_ALL_DEFS)
  4969. create_new_def_for (DEF_FROM_PTR (def_p), copy, def_p);
  4970. }
  4971. return new_bb;
  4972. }
  4973. /* Adds phi node arguments for edge E_COPY after basic block duplication. */
  4974. static void
  4975. add_phi_args_after_copy_edge (edge e_copy)
  4976. {
  4977. basic_block bb, bb_copy = e_copy->src, dest;
  4978. edge e;
  4979. edge_iterator ei;
  4980. gphi *phi, *phi_copy;
  4981. tree def;
  4982. gphi_iterator psi, psi_copy;
  4983. if (gimple_seq_empty_p (phi_nodes (e_copy->dest)))
  4984. return;
  4985. bb = bb_copy->flags & BB_DUPLICATED ? get_bb_original (bb_copy) : bb_copy;
  4986. if (e_copy->dest->flags & BB_DUPLICATED)
  4987. dest = get_bb_original (e_copy->dest);
  4988. else
  4989. dest = e_copy->dest;
  4990. e = find_edge (bb, dest);
  4991. if (!e)
  4992. {
  4993. /* During loop unrolling the target of the latch edge is copied.
  4994. In this case we are not looking for edge to dest, but to
  4995. duplicated block whose original was dest. */
  4996. FOR_EACH_EDGE (e, ei, bb->succs)
  4997. {
  4998. if ((e->dest->flags & BB_DUPLICATED)
  4999. && get_bb_original (e->dest) == dest)
  5000. break;
  5001. }
  5002. gcc_assert (e != NULL);
  5003. }
  5004. for (psi = gsi_start_phis (e->dest),
  5005. psi_copy = gsi_start_phis (e_copy->dest);
  5006. !gsi_end_p (psi);
  5007. gsi_next (&psi), gsi_next (&psi_copy))
  5008. {
  5009. phi = psi.phi ();
  5010. phi_copy = psi_copy.phi ();
  5011. def = PHI_ARG_DEF_FROM_EDGE (phi, e);
  5012. add_phi_arg (phi_copy, def, e_copy,
  5013. gimple_phi_arg_location_from_edge (phi, e));
  5014. }
  5015. }
  5016. /* Basic block BB_COPY was created by code duplication. Add phi node
  5017. arguments for edges going out of BB_COPY. The blocks that were
  5018. duplicated have BB_DUPLICATED set. */
  5019. void
  5020. add_phi_args_after_copy_bb (basic_block bb_copy)
  5021. {
  5022. edge e_copy;
  5023. edge_iterator ei;
  5024. FOR_EACH_EDGE (e_copy, ei, bb_copy->succs)
  5025. {
  5026. add_phi_args_after_copy_edge (e_copy);
  5027. }
  5028. }
  5029. /* Blocks in REGION_COPY array of length N_REGION were created by
  5030. duplication of basic blocks. Add phi node arguments for edges
  5031. going from these blocks. If E_COPY is not NULL, also add
  5032. phi node arguments for its destination.*/
  5033. void
  5034. add_phi_args_after_copy (basic_block *region_copy, unsigned n_region,
  5035. edge e_copy)
  5036. {
  5037. unsigned i;
  5038. for (i = 0; i < n_region; i++)
  5039. region_copy[i]->flags |= BB_DUPLICATED;
  5040. for (i = 0; i < n_region; i++)
  5041. add_phi_args_after_copy_bb (region_copy[i]);
  5042. if (e_copy)
  5043. add_phi_args_after_copy_edge (e_copy);
  5044. for (i = 0; i < n_region; i++)
  5045. region_copy[i]->flags &= ~BB_DUPLICATED;
  5046. }
  5047. /* Duplicates a REGION (set of N_REGION basic blocks) with just a single
  5048. important exit edge EXIT. By important we mean that no SSA name defined
  5049. inside region is live over the other exit edges of the region. All entry
  5050. edges to the region must go to ENTRY->dest. The edge ENTRY is redirected
  5051. to the duplicate of the region. Dominance and loop information is
  5052. updated if UPDATE_DOMINANCE is true, but not the SSA web. If
  5053. UPDATE_DOMINANCE is false then we assume that the caller will update the
  5054. dominance information after calling this function. The new basic
  5055. blocks are stored to REGION_COPY in the same order as they had in REGION,
  5056. provided that REGION_COPY is not NULL.
  5057. The function returns false if it is unable to copy the region,
  5058. true otherwise. */
  5059. bool
  5060. gimple_duplicate_sese_region (edge entry, edge exit,
  5061. basic_block *region, unsigned n_region,
  5062. basic_block *region_copy,
  5063. bool update_dominance)
  5064. {
  5065. unsigned i;
  5066. bool free_region_copy = false, copying_header = false;
  5067. struct loop *loop = entry->dest->loop_father;
  5068. edge exit_copy;
  5069. vec<basic_block> doms;
  5070. edge redirected;
  5071. int total_freq = 0, entry_freq = 0;
  5072. gcov_type total_count = 0, entry_count = 0;
  5073. if (!can_copy_bbs_p (region, n_region))
  5074. return false;
  5075. /* Some sanity checking. Note that we do not check for all possible
  5076. missuses of the functions. I.e. if you ask to copy something weird,
  5077. it will work, but the state of structures probably will not be
  5078. correct. */
  5079. for (i = 0; i < n_region; i++)
  5080. {
  5081. /* We do not handle subloops, i.e. all the blocks must belong to the
  5082. same loop. */
  5083. if (region[i]->loop_father != loop)
  5084. return false;
  5085. if (region[i] != entry->dest
  5086. && region[i] == loop->header)
  5087. return false;
  5088. }
  5089. /* In case the function is used for loop header copying (which is the primary
  5090. use), ensure that EXIT and its copy will be new latch and entry edges. */
  5091. if (loop->header == entry->dest)
  5092. {
  5093. copying_header = true;
  5094. if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit->src))
  5095. return false;
  5096. for (i = 0; i < n_region; i++)
  5097. if (region[i] != exit->src
  5098. && dominated_by_p (CDI_DOMINATORS, region[i], exit->src))
  5099. return false;
  5100. }
  5101. initialize_original_copy_tables ();
  5102. if (copying_header)
  5103. set_loop_copy (loop, loop_outer (loop));
  5104. else
  5105. set_loop_copy (loop, loop);
  5106. if (!region_copy)
  5107. {
  5108. region_copy = XNEWVEC (basic_block, n_region);
  5109. free_region_copy = true;
  5110. }
  5111. /* Record blocks outside the region that are dominated by something
  5112. inside. */
  5113. if (update_dominance)
  5114. {
  5115. doms.create (0);
  5116. doms = get_dominated_by_region (CDI_DOMINATORS, region, n_region);
  5117. }
  5118. if (entry->dest->count)
  5119. {
  5120. total_count = entry->dest->count;
  5121. entry_count = entry->count;
  5122. /* Fix up corner cases, to avoid division by zero or creation of negative
  5123. frequencies. */
  5124. if (entry_count > total_count)
  5125. entry_count = total_count;
  5126. }
  5127. else
  5128. {
  5129. total_freq = entry->dest->frequency;
  5130. entry_freq = EDGE_FREQUENCY (entry);
  5131. /* Fix up corner cases, to avoid division by zero or creation of negative
  5132. frequencies. */
  5133. if (total_freq == 0)
  5134. total_freq = 1;
  5135. else if (entry_freq > total_freq)
  5136. entry_freq = total_freq;
  5137. }
  5138. copy_bbs (region, n_region, region_copy, &exit, 1, &exit_copy, loop,
  5139. split_edge_bb_loc (entry), update_dominance);
  5140. if (total_count)
  5141. {
  5142. scale_bbs_frequencies_gcov_type (region, n_region,
  5143. total_count - entry_count,
  5144. total_count);
  5145. scale_bbs_frequencies_gcov_type (region_copy, n_region, entry_count,
  5146. total_count);
  5147. }
  5148. else
  5149. {
  5150. scale_bbs_frequencies_int (region, n_region, total_freq - entry_freq,
  5151. total_freq);
  5152. scale_bbs_frequencies_int (region_copy, n_region, entry_freq, total_freq);
  5153. }
  5154. if (copying_header)
  5155. {
  5156. loop->header = exit->dest;
  5157. loop->latch = exit->src;
  5158. }
  5159. /* Redirect the entry and add the phi node arguments. */
  5160. redirected = redirect_edge_and_branch (entry, get_bb_copy (entry->dest));
  5161. gcc_assert (redirected != NULL);
  5162. flush_pending_stmts (entry);
  5163. /* Concerning updating of dominators: We must recount dominators
  5164. for entry block and its copy. Anything that is outside of the
  5165. region, but was dominated by something inside needs recounting as
  5166. well. */
  5167. if (update_dominance)
  5168. {
  5169. set_immediate_dominator (CDI_DOMINATORS, entry->dest, entry->src);
  5170. doms.safe_push (get_bb_original (entry->dest));
  5171. iterate_fix_dominators (CDI_DOMINATORS, doms, false);
  5172. doms.release ();
  5173. }
  5174. /* Add the other PHI node arguments. */
  5175. add_phi_args_after_copy (region_copy, n_region, NULL);
  5176. if (free_region_copy)
  5177. free (region_copy);
  5178. free_original_copy_tables ();
  5179. return true;
  5180. }
  5181. /* Checks if BB is part of the region defined by N_REGION BBS. */
  5182. static bool
  5183. bb_part_of_region_p (basic_block bb, basic_block* bbs, unsigned n_region)
  5184. {
  5185. unsigned int n;
  5186. for (n = 0; n < n_region; n++)
  5187. {
  5188. if (bb == bbs[n])
  5189. return true;
  5190. }
  5191. return false;
  5192. }
  5193. /* Duplicates REGION consisting of N_REGION blocks. The new blocks
  5194. are stored to REGION_COPY in the same order in that they appear
  5195. in REGION, if REGION_COPY is not NULL. ENTRY is the entry to
  5196. the region, EXIT an exit from it. The condition guarding EXIT
  5197. is moved to ENTRY. Returns true if duplication succeeds, false
  5198. otherwise.
  5199. For example,
  5200. some_code;
  5201. if (cond)
  5202. A;
  5203. else
  5204. B;
  5205. is transformed to
  5206. if (cond)
  5207. {
  5208. some_code;
  5209. A;
  5210. }
  5211. else
  5212. {
  5213. some_code;
  5214. B;
  5215. }
  5216. */
  5217. bool
  5218. gimple_duplicate_sese_tail (edge entry ATTRIBUTE_UNUSED, edge exit ATTRIBUTE_UNUSED,
  5219. basic_block *region ATTRIBUTE_UNUSED, unsigned n_region ATTRIBUTE_UNUSED,
  5220. basic_block *region_copy ATTRIBUTE_UNUSED)
  5221. {
  5222. unsigned i;
  5223. bool free_region_copy = false;
  5224. struct loop *loop = exit->dest->loop_father;
  5225. struct loop *orig_loop = entry->dest->loop_father;
  5226. basic_block switch_bb, entry_bb, nentry_bb;
  5227. vec<basic_block> doms;
  5228. int total_freq = 0, exit_freq = 0;
  5229. gcov_type total_count = 0, exit_count = 0;
  5230. edge exits[2], nexits[2], e;
  5231. gimple_stmt_iterator gsi;
  5232. gimple cond_stmt;
  5233. edge sorig, snew;
  5234. basic_block exit_bb;
  5235. gphi_iterator psi;
  5236. gphi *phi;
  5237. tree def;
  5238. struct loop *target, *aloop, *cloop;
  5239. gcc_assert (EDGE_COUNT (exit->src->succs) == 2);
  5240. exits[0] = exit;
  5241. exits[1] = EDGE_SUCC (exit->src, EDGE_SUCC (exit->src, 0) == exit);
  5242. if (!can_copy_bbs_p (region, n_region))
  5243. return false;
  5244. initialize_original_copy_tables ();
  5245. set_loop_copy (orig_loop, loop);
  5246. target= loop;
  5247. for (aloop = orig_loop->inner; aloop; aloop = aloop->next)
  5248. {
  5249. if (bb_part_of_region_p (aloop->header, region, n_region))
  5250. {
  5251. cloop = duplicate_loop (aloop, target);
  5252. duplicate_subloops (aloop, cloop);
  5253. }
  5254. }
  5255. if (!region_copy)
  5256. {
  5257. region_copy = XNEWVEC (basic_block, n_region);
  5258. free_region_copy = true;
  5259. }
  5260. gcc_assert (!need_ssa_update_p (cfun));
  5261. /* Record blocks outside the region that are dominated by something
  5262. inside. */
  5263. doms = get_dominated_by_region (CDI_DOMINATORS, region, n_region);
  5264. if (exit->src->count)
  5265. {
  5266. total_count = exit->src->count;
  5267. exit_count = exit->count;
  5268. /* Fix up corner cases, to avoid division by zero or creation of negative
  5269. frequencies. */
  5270. if (exit_count > total_count)
  5271. exit_count = total_count;
  5272. }
  5273. else
  5274. {
  5275. total_freq = exit->src->frequency;
  5276. exit_freq = EDGE_FREQUENCY (exit);
  5277. /* Fix up corner cases, to avoid division by zero or creation of negative
  5278. frequencies. */
  5279. if (total_freq == 0)
  5280. total_freq = 1;
  5281. if (exit_freq > total_freq)
  5282. exit_freq = total_freq;
  5283. }
  5284. copy_bbs (region, n_region, region_copy, exits, 2, nexits, orig_loop,
  5285. split_edge_bb_loc (exit), true);
  5286. if (total_count)
  5287. {
  5288. scale_bbs_frequencies_gcov_type (region, n_region,
  5289. total_count - exit_count,
  5290. total_count);
  5291. scale_bbs_frequencies_gcov_type (region_copy, n_region, exit_count,
  5292. total_count);
  5293. }
  5294. else
  5295. {
  5296. scale_bbs_frequencies_int (region, n_region, total_freq - exit_freq,
  5297. total_freq);
  5298. scale_bbs_frequencies_int (region_copy, n_region, exit_freq, total_freq);
  5299. }
  5300. /* Create the switch block, and put the exit condition to it. */
  5301. entry_bb = entry->dest;
  5302. nentry_bb = get_bb_copy (entry_bb);
  5303. if (!last_stmt (entry->src)
  5304. || !stmt_ends_bb_p (last_stmt (entry->src)))
  5305. switch_bb = entry->src;
  5306. else
  5307. switch_bb = split_edge (entry);
  5308. set_immediate_dominator (CDI_DOMINATORS, nentry_bb, switch_bb);
  5309. gsi = gsi_last_bb (switch_bb);
  5310. cond_stmt = last_stmt (exit->src);
  5311. gcc_assert (gimple_code (cond_stmt) == GIMPLE_COND);
  5312. cond_stmt = gimple_copy (cond_stmt);
  5313. gsi_insert_after (&gsi, cond_stmt, GSI_NEW_STMT);
  5314. sorig = single_succ_edge (switch_bb);
  5315. sorig->flags = exits[1]->flags;
  5316. snew = make_edge (switch_bb, nentry_bb, exits[0]->flags);
  5317. /* Register the new edge from SWITCH_BB in loop exit lists. */
  5318. rescan_loop_exit (snew, true, false);
  5319. /* Add the PHI node arguments. */
  5320. add_phi_args_after_copy (region_copy, n_region, snew);
  5321. /* Get rid of now superfluous conditions and associated edges (and phi node
  5322. arguments). */
  5323. exit_bb = exit->dest;
  5324. e = redirect_edge_and_branch (exits[0], exits[1]->dest);
  5325. PENDING_STMT (e) = NULL;
  5326. /* The latch of ORIG_LOOP was copied, and so was the backedge
  5327. to the original header. We redirect this backedge to EXIT_BB. */
  5328. for (i = 0; i < n_region; i++)
  5329. if (get_bb_original (region_copy[i]) == orig_loop->latch)
  5330. {
  5331. gcc_assert (single_succ_edge (region_copy[i]));
  5332. e = redirect_edge_and_branch (single_succ_edge (region_copy[i]), exit_bb);
  5333. PENDING_STMT (e) = NULL;
  5334. for (psi = gsi_start_phis (exit_bb);
  5335. !gsi_end_p (psi);
  5336. gsi_next (&psi))
  5337. {
  5338. phi = psi.phi ();
  5339. def = PHI_ARG_DEF (phi, nexits[0]->dest_idx);
  5340. add_phi_arg (phi, def, e, gimple_phi_arg_location_from_edge (phi, e));
  5341. }
  5342. }
  5343. e = redirect_edge_and_branch (nexits[1], nexits[0]->dest);
  5344. PENDING_STMT (e) = NULL;
  5345. /* Anything that is outside of the region, but was dominated by something
  5346. inside needs to update dominance info. */
  5347. iterate_fix_dominators (CDI_DOMINATORS, doms, false);
  5348. doms.release ();
  5349. /* Update the SSA web. */
  5350. update_ssa (TODO_update_ssa);
  5351. if (free_region_copy)
  5352. free (region_copy);
  5353. free_original_copy_tables ();
  5354. return true;
  5355. }
  5356. /* Add all the blocks dominated by ENTRY to the array BBS_P. Stop
  5357. adding blocks when the dominator traversal reaches EXIT. This
  5358. function silently assumes that ENTRY strictly dominates EXIT. */
  5359. void
  5360. gather_blocks_in_sese_region (basic_block entry, basic_block exit,
  5361. vec<basic_block> *bbs_p)
  5362. {
  5363. basic_block son;
  5364. for (son = first_dom_son (CDI_DOMINATORS, entry);
  5365. son;
  5366. son = next_dom_son (CDI_DOMINATORS, son))
  5367. {
  5368. bbs_p->safe_push (son);
  5369. if (son != exit)
  5370. gather_blocks_in_sese_region (son, exit, bbs_p);
  5371. }
  5372. }
  5373. /* Replaces *TP with a duplicate (belonging to function TO_CONTEXT).
  5374. The duplicates are recorded in VARS_MAP. */
  5375. static void
  5376. replace_by_duplicate_decl (tree *tp, hash_map<tree, tree> *vars_map,
  5377. tree to_context)
  5378. {
  5379. tree t = *tp, new_t;
  5380. struct function *f = DECL_STRUCT_FUNCTION (to_context);
  5381. if (DECL_CONTEXT (t) == to_context)
  5382. return;
  5383. bool existed;
  5384. tree &loc = vars_map->get_or_insert (t, &existed);
  5385. if (!existed)
  5386. {
  5387. if (SSA_VAR_P (t))
  5388. {
  5389. new_t = copy_var_decl (t, DECL_NAME (t), TREE_TYPE (t));
  5390. add_local_decl (f, new_t);
  5391. }
  5392. else
  5393. {
  5394. gcc_assert (TREE_CODE (t) == CONST_DECL);
  5395. new_t = copy_node (t);
  5396. }
  5397. DECL_CONTEXT (new_t) = to_context;
  5398. loc = new_t;
  5399. }
  5400. else
  5401. new_t = loc;
  5402. *tp = new_t;
  5403. }
  5404. /* Creates an ssa name in TO_CONTEXT equivalent to NAME.
  5405. VARS_MAP maps old ssa names and var_decls to the new ones. */
  5406. static tree
  5407. replace_ssa_name (tree name, hash_map<tree, tree> *vars_map,
  5408. tree to_context)
  5409. {
  5410. tree new_name;
  5411. gcc_assert (!virtual_operand_p (name));
  5412. tree *loc = vars_map->get (name);
  5413. if (!loc)
  5414. {
  5415. tree decl = SSA_NAME_VAR (name);
  5416. if (decl)
  5417. {
  5418. replace_by_duplicate_decl (&decl, vars_map, to_context);
  5419. new_name = make_ssa_name_fn (DECL_STRUCT_FUNCTION (to_context),
  5420. decl, SSA_NAME_DEF_STMT (name));
  5421. if (SSA_NAME_IS_DEFAULT_DEF (name))
  5422. set_ssa_default_def (DECL_STRUCT_FUNCTION (to_context),
  5423. decl, new_name);
  5424. }
  5425. else
  5426. new_name = copy_ssa_name_fn (DECL_STRUCT_FUNCTION (to_context),
  5427. name, SSA_NAME_DEF_STMT (name));
  5428. vars_map->put (name, new_name);
  5429. }
  5430. else
  5431. new_name = *loc;
  5432. return new_name;
  5433. }
  5434. struct move_stmt_d
  5435. {
  5436. tree orig_block;
  5437. tree new_block;
  5438. tree from_context;
  5439. tree to_context;
  5440. hash_map<tree, tree> *vars_map;
  5441. htab_t new_label_map;
  5442. hash_map<void *, void *> *eh_map;
  5443. bool remap_decls_p;
  5444. };
  5445. /* Helper for move_block_to_fn. Set TREE_BLOCK in every expression
  5446. contained in *TP if it has been ORIG_BLOCK previously and change the
  5447. DECL_CONTEXT of every local variable referenced in *TP. */
  5448. static tree
  5449. move_stmt_op (tree *tp, int *walk_subtrees, void *data)
  5450. {
  5451. struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
  5452. struct move_stmt_d *p = (struct move_stmt_d *) wi->info;
  5453. tree t = *tp;
  5454. if (EXPR_P (t))
  5455. {
  5456. tree block = TREE_BLOCK (t);
  5457. if (block == p->orig_block
  5458. || (p->orig_block == NULL_TREE
  5459. && block != NULL_TREE))
  5460. TREE_SET_BLOCK (t, p->new_block);
  5461. #ifdef ENABLE_CHECKING
  5462. else if (block != NULL_TREE)
  5463. {
  5464. while (block && TREE_CODE (block) == BLOCK && block != p->orig_block)
  5465. block = BLOCK_SUPERCONTEXT (block);
  5466. gcc_assert (block == p->orig_block);
  5467. }
  5468. #endif
  5469. }
  5470. else if (DECL_P (t) || TREE_CODE (t) == SSA_NAME)
  5471. {
  5472. if (TREE_CODE (t) == SSA_NAME)
  5473. *tp = replace_ssa_name (t, p->vars_map, p->to_context);
  5474. else if (TREE_CODE (t) == LABEL_DECL)
  5475. {
  5476. if (p->new_label_map)
  5477. {
  5478. struct tree_map in, *out;
  5479. in.base.from = t;
  5480. out = (struct tree_map *)
  5481. htab_find_with_hash (p->new_label_map, &in, DECL_UID (t));
  5482. if (out)
  5483. *tp = t = out->to;
  5484. }
  5485. DECL_CONTEXT (t) = p->to_context;
  5486. }
  5487. else if (p->remap_decls_p)
  5488. {
  5489. /* Replace T with its duplicate. T should no longer appear in the
  5490. parent function, so this looks wasteful; however, it may appear
  5491. in referenced_vars, and more importantly, as virtual operands of
  5492. statements, and in alias lists of other variables. It would be
  5493. quite difficult to expunge it from all those places. ??? It might
  5494. suffice to do this for addressable variables. */
  5495. if ((TREE_CODE (t) == VAR_DECL
  5496. && !is_global_var (t))
  5497. || TREE_CODE (t) == CONST_DECL)
  5498. replace_by_duplicate_decl (tp, p->vars_map, p->to_context);
  5499. }
  5500. *walk_subtrees = 0;
  5501. }
  5502. else if (TYPE_P (t))
  5503. *walk_subtrees = 0;
  5504. return NULL_TREE;
  5505. }
  5506. /* Helper for move_stmt_r. Given an EH region number for the source
  5507. function, map that to the duplicate EH regio number in the dest. */
  5508. static int
  5509. move_stmt_eh_region_nr (int old_nr, struct move_stmt_d *p)
  5510. {
  5511. eh_region old_r, new_r;
  5512. old_r = get_eh_region_from_number (old_nr);
  5513. new_r = static_cast<eh_region> (*p->eh_map->get (old_r));
  5514. return new_r->index;
  5515. }
  5516. /* Similar, but operate on INTEGER_CSTs. */
  5517. static tree
  5518. move_stmt_eh_region_tree_nr (tree old_t_nr, struct move_stmt_d *p)
  5519. {
  5520. int old_nr, new_nr;
  5521. old_nr = tree_to_shwi (old_t_nr);
  5522. new_nr = move_stmt_eh_region_nr (old_nr, p);
  5523. return build_int_cst (integer_type_node, new_nr);
  5524. }
  5525. /* Like move_stmt_op, but for gimple statements.
  5526. Helper for move_block_to_fn. Set GIMPLE_BLOCK in every expression
  5527. contained in the current statement in *GSI_P and change the
  5528. DECL_CONTEXT of every local variable referenced in the current
  5529. statement. */
  5530. static tree
  5531. move_stmt_r (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
  5532. struct walk_stmt_info *wi)
  5533. {
  5534. struct move_stmt_d *p = (struct move_stmt_d *) wi->info;
  5535. gimple stmt = gsi_stmt (*gsi_p);
  5536. tree block = gimple_block (stmt);
  5537. if (block == p->orig_block
  5538. || (p->orig_block == NULL_TREE
  5539. && block != NULL_TREE))
  5540. gimple_set_block (stmt, p->new_block);
  5541. switch (gimple_code (stmt))
  5542. {
  5543. case GIMPLE_CALL:
  5544. /* Remap the region numbers for __builtin_eh_{pointer,filter}. */
  5545. {
  5546. tree r, fndecl = gimple_call_fndecl (stmt);
  5547. if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
  5548. switch (DECL_FUNCTION_CODE (fndecl))
  5549. {
  5550. case BUILT_IN_EH_COPY_VALUES:
  5551. r = gimple_call_arg (stmt, 1);
  5552. r = move_stmt_eh_region_tree_nr (r, p);
  5553. gimple_call_set_arg (stmt, 1, r);
  5554. /* FALLTHRU */
  5555. case BUILT_IN_EH_POINTER:
  5556. case BUILT_IN_EH_FILTER:
  5557. r = gimple_call_arg (stmt, 0);
  5558. r = move_stmt_eh_region_tree_nr (r, p);
  5559. gimple_call_set_arg (stmt, 0, r);
  5560. break;
  5561. default:
  5562. break;
  5563. }
  5564. }
  5565. break;
  5566. case GIMPLE_RESX:
  5567. {
  5568. gresx *resx_stmt = as_a <gresx *> (stmt);
  5569. int r = gimple_resx_region (resx_stmt);
  5570. r = move_stmt_eh_region_nr (r, p);
  5571. gimple_resx_set_region (resx_stmt, r);
  5572. }
  5573. break;
  5574. case GIMPLE_EH_DISPATCH:
  5575. {
  5576. geh_dispatch *eh_dispatch_stmt = as_a <geh_dispatch *> (stmt);
  5577. int r = gimple_eh_dispatch_region (eh_dispatch_stmt);
  5578. r = move_stmt_eh_region_nr (r, p);
  5579. gimple_eh_dispatch_set_region (eh_dispatch_stmt, r);
  5580. }
  5581. break;
  5582. case GIMPLE_OMP_RETURN:
  5583. case GIMPLE_OMP_CONTINUE:
  5584. break;
  5585. default:
  5586. if (is_gimple_omp (stmt))
  5587. {
  5588. /* Do not remap variables inside OMP directives. Variables
  5589. referenced in clauses and directive header belong to the
  5590. parent function and should not be moved into the child
  5591. function. */
  5592. bool save_remap_decls_p = p->remap_decls_p;
  5593. p->remap_decls_p = false;
  5594. *handled_ops_p = true;
  5595. walk_gimple_seq_mod (gimple_omp_body_ptr (stmt), move_stmt_r,
  5596. move_stmt_op, wi);
  5597. p->remap_decls_p = save_remap_decls_p;
  5598. }
  5599. break;
  5600. }
  5601. return NULL_TREE;
  5602. }
  5603. /* Move basic block BB from function CFUN to function DEST_FN. The
  5604. block is moved out of the original linked list and placed after
  5605. block AFTER in the new list. Also, the block is removed from the
  5606. original array of blocks and placed in DEST_FN's array of blocks.
  5607. If UPDATE_EDGE_COUNT_P is true, the edge counts on both CFGs is
  5608. updated to reflect the moved edges.
  5609. The local variables are remapped to new instances, VARS_MAP is used
  5610. to record the mapping. */
  5611. static void
  5612. move_block_to_fn (struct function *dest_cfun, basic_block bb,
  5613. basic_block after, bool update_edge_count_p,
  5614. struct move_stmt_d *d)
  5615. {
  5616. struct control_flow_graph *cfg;
  5617. edge_iterator ei;
  5618. edge e;
  5619. gimple_stmt_iterator si;
  5620. unsigned old_len, new_len;
  5621. /* Remove BB from dominance structures. */
  5622. delete_from_dominance_info (CDI_DOMINATORS, bb);
  5623. /* Move BB from its current loop to the copy in the new function. */
  5624. if (current_loops)
  5625. {
  5626. struct loop *new_loop = (struct loop *)bb->loop_father->aux;
  5627. if (new_loop)
  5628. bb->loop_father = new_loop;
  5629. }
  5630. /* Link BB to the new linked list. */
  5631. move_block_after (bb, after);
  5632. /* Update the edge count in the corresponding flowgraphs. */
  5633. if (update_edge_count_p)
  5634. FOR_EACH_EDGE (e, ei, bb->succs)
  5635. {
  5636. cfun->cfg->x_n_edges--;
  5637. dest_cfun->cfg->x_n_edges++;
  5638. }
  5639. /* Remove BB from the original basic block array. */
  5640. (*cfun->cfg->x_basic_block_info)[bb->index] = NULL;
  5641. cfun->cfg->x_n_basic_blocks--;
  5642. /* Grow DEST_CFUN's basic block array if needed. */
  5643. cfg = dest_cfun->cfg;
  5644. cfg->x_n_basic_blocks++;
  5645. if (bb->index >= cfg->x_last_basic_block)
  5646. cfg->x_last_basic_block = bb->index + 1;
  5647. old_len = vec_safe_length (cfg->x_basic_block_info);
  5648. if ((unsigned) cfg->x_last_basic_block >= old_len)
  5649. {
  5650. new_len = cfg->x_last_basic_block + (cfg->x_last_basic_block + 3) / 4;
  5651. vec_safe_grow_cleared (cfg->x_basic_block_info, new_len);
  5652. }
  5653. (*cfg->x_basic_block_info)[bb->index] = bb;
  5654. /* Remap the variables in phi nodes. */
  5655. for (gphi_iterator psi = gsi_start_phis (bb);
  5656. !gsi_end_p (psi); )
  5657. {
  5658. gphi *phi = psi.phi ();
  5659. use_operand_p use;
  5660. tree op = PHI_RESULT (phi);
  5661. ssa_op_iter oi;
  5662. unsigned i;
  5663. if (virtual_operand_p (op))
  5664. {
  5665. /* Remove the phi nodes for virtual operands (alias analysis will be
  5666. run for the new function, anyway). */
  5667. remove_phi_node (&psi, true);
  5668. continue;
  5669. }
  5670. SET_PHI_RESULT (phi,
  5671. replace_ssa_name (op, d->vars_map, dest_cfun->decl));
  5672. FOR_EACH_PHI_ARG (use, phi, oi, SSA_OP_USE)
  5673. {
  5674. op = USE_FROM_PTR (use);
  5675. if (TREE_CODE (op) == SSA_NAME)
  5676. SET_USE (use, replace_ssa_name (op, d->vars_map, dest_cfun->decl));
  5677. }
  5678. for (i = 0; i < EDGE_COUNT (bb->preds); i++)
  5679. {
  5680. location_t locus = gimple_phi_arg_location (phi, i);
  5681. tree block = LOCATION_BLOCK (locus);
  5682. if (locus == UNKNOWN_LOCATION)
  5683. continue;
  5684. if (d->orig_block == NULL_TREE || block == d->orig_block)
  5685. {
  5686. if (d->new_block == NULL_TREE)
  5687. locus = LOCATION_LOCUS (locus);
  5688. else
  5689. locus = COMBINE_LOCATION_DATA (line_table, locus, d->new_block);
  5690. gimple_phi_arg_set_location (phi, i, locus);
  5691. }
  5692. }
  5693. gsi_next (&psi);
  5694. }
  5695. for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
  5696. {
  5697. gimple stmt = gsi_stmt (si);
  5698. struct walk_stmt_info wi;
  5699. memset (&wi, 0, sizeof (wi));
  5700. wi.info = d;
  5701. walk_gimple_stmt (&si, move_stmt_r, move_stmt_op, &wi);
  5702. if (glabel *label_stmt = dyn_cast <glabel *> (stmt))
  5703. {
  5704. tree label = gimple_label_label (label_stmt);
  5705. int uid = LABEL_DECL_UID (label);
  5706. gcc_assert (uid > -1);
  5707. old_len = vec_safe_length (cfg->x_label_to_block_map);
  5708. if (old_len <= (unsigned) uid)
  5709. {
  5710. new_len = 3 * uid / 2 + 1;
  5711. vec_safe_grow_cleared (cfg->x_label_to_block_map, new_len);
  5712. }
  5713. (*cfg->x_label_to_block_map)[uid] = bb;
  5714. (*cfun->cfg->x_label_to_block_map)[uid] = NULL;
  5715. gcc_assert (DECL_CONTEXT (label) == dest_cfun->decl);
  5716. if (uid >= dest_cfun->cfg->last_label_uid)
  5717. dest_cfun->cfg->last_label_uid = uid + 1;
  5718. }
  5719. maybe_duplicate_eh_stmt_fn (dest_cfun, stmt, cfun, stmt, d->eh_map, 0);
  5720. remove_stmt_from_eh_lp_fn (cfun, stmt);
  5721. gimple_duplicate_stmt_histograms (dest_cfun, stmt, cfun, stmt);
  5722. gimple_remove_stmt_histograms (cfun, stmt);
  5723. /* We cannot leave any operands allocated from the operand caches of
  5724. the current function. */
  5725. free_stmt_operands (cfun, stmt);
  5726. push_cfun (dest_cfun);
  5727. update_stmt (stmt);
  5728. pop_cfun ();
  5729. }
  5730. FOR_EACH_EDGE (e, ei, bb->succs)
  5731. if (e->goto_locus != UNKNOWN_LOCATION)
  5732. {
  5733. tree block = LOCATION_BLOCK (e->goto_locus);
  5734. if (d->orig_block == NULL_TREE
  5735. || block == d->orig_block)
  5736. e->goto_locus = d->new_block ?
  5737. COMBINE_LOCATION_DATA (line_table, e->goto_locus, d->new_block) :
  5738. LOCATION_LOCUS (e->goto_locus);
  5739. }
  5740. }
  5741. /* Examine the statements in BB (which is in SRC_CFUN); find and return
  5742. the outermost EH region. Use REGION as the incoming base EH region. */
  5743. static eh_region
  5744. find_outermost_region_in_block (struct function *src_cfun,
  5745. basic_block bb, eh_region region)
  5746. {
  5747. gimple_stmt_iterator si;
  5748. for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
  5749. {
  5750. gimple stmt = gsi_stmt (si);
  5751. eh_region stmt_region;
  5752. int lp_nr;
  5753. lp_nr = lookup_stmt_eh_lp_fn (src_cfun, stmt);
  5754. stmt_region = get_eh_region_from_lp_number_fn (src_cfun, lp_nr);
  5755. if (stmt_region)
  5756. {
  5757. if (region == NULL)
  5758. region = stmt_region;
  5759. else if (stmt_region != region)
  5760. {
  5761. region = eh_region_outermost (src_cfun, stmt_region, region);
  5762. gcc_assert (region != NULL);
  5763. }
  5764. }
  5765. }
  5766. return region;
  5767. }
  5768. static tree
  5769. new_label_mapper (tree decl, void *data)
  5770. {
  5771. htab_t hash = (htab_t) data;
  5772. struct tree_map *m;
  5773. void **slot;
  5774. gcc_assert (TREE_CODE (decl) == LABEL_DECL);
  5775. m = XNEW (struct tree_map);
  5776. m->hash = DECL_UID (decl);
  5777. m->base.from = decl;
  5778. m->to = create_artificial_label (UNKNOWN_LOCATION);
  5779. LABEL_DECL_UID (m->to) = LABEL_DECL_UID (decl);
  5780. if (LABEL_DECL_UID (m->to) >= cfun->cfg->last_label_uid)
  5781. cfun->cfg->last_label_uid = LABEL_DECL_UID (m->to) + 1;
  5782. slot = htab_find_slot_with_hash (hash, m, m->hash, INSERT);
  5783. gcc_assert (*slot == NULL);
  5784. *slot = m;
  5785. return m->to;
  5786. }
  5787. /* Change DECL_CONTEXT of all BLOCK_VARS in block, including
  5788. subblocks. */
  5789. static void
  5790. replace_block_vars_by_duplicates (tree block, hash_map<tree, tree> *vars_map,
  5791. tree to_context)
  5792. {
  5793. tree *tp, t;
  5794. for (tp = &BLOCK_VARS (block); *tp; tp = &DECL_CHAIN (*tp))
  5795. {
  5796. t = *tp;
  5797. if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != CONST_DECL)
  5798. continue;
  5799. replace_by_duplicate_decl (&t, vars_map, to_context);
  5800. if (t != *tp)
  5801. {
  5802. if (TREE_CODE (*tp) == VAR_DECL && DECL_HAS_VALUE_EXPR_P (*tp))
  5803. {
  5804. SET_DECL_VALUE_EXPR (t, DECL_VALUE_EXPR (*tp));
  5805. DECL_HAS_VALUE_EXPR_P (t) = 1;
  5806. }
  5807. DECL_CHAIN (t) = DECL_CHAIN (*tp);
  5808. *tp = t;
  5809. }
  5810. }
  5811. for (block = BLOCK_SUBBLOCKS (block); block; block = BLOCK_CHAIN (block))
  5812. replace_block_vars_by_duplicates (block, vars_map, to_context);
  5813. }
  5814. /* Fixup the loop arrays and numbers after moving LOOP and its subloops
  5815. from FN1 to FN2. */
  5816. static void
  5817. fixup_loop_arrays_after_move (struct function *fn1, struct function *fn2,
  5818. struct loop *loop)
  5819. {
  5820. /* Discard it from the old loop array. */
  5821. (*get_loops (fn1))[loop->num] = NULL;
  5822. /* Place it in the new loop array, assigning it a new number. */
  5823. loop->num = number_of_loops (fn2);
  5824. vec_safe_push (loops_for_fn (fn2)->larray, loop);
  5825. /* Recurse to children. */
  5826. for (loop = loop->inner; loop; loop = loop->next)
  5827. fixup_loop_arrays_after_move (fn1, fn2, loop);
  5828. }
  5829. /* Verify that the blocks in BBS_P are a single-entry, single-exit region
  5830. delimited by ENTRY_BB and EXIT_BB, possibly containing noreturn blocks. */
  5831. DEBUG_FUNCTION void
  5832. verify_sese (basic_block entry, basic_block exit, vec<basic_block> *bbs_p)
  5833. {
  5834. basic_block bb;
  5835. edge_iterator ei;
  5836. edge e;
  5837. bitmap bbs = BITMAP_ALLOC (NULL);
  5838. int i;
  5839. gcc_assert (entry != NULL);
  5840. gcc_assert (entry != exit);
  5841. gcc_assert (bbs_p != NULL);
  5842. gcc_assert (bbs_p->length () > 0);
  5843. FOR_EACH_VEC_ELT (*bbs_p, i, bb)
  5844. bitmap_set_bit (bbs, bb->index);
  5845. gcc_assert (bitmap_bit_p (bbs, entry->index));
  5846. gcc_assert (exit == NULL || bitmap_bit_p (bbs, exit->index));
  5847. FOR_EACH_VEC_ELT (*bbs_p, i, bb)
  5848. {
  5849. if (bb == entry)
  5850. {
  5851. gcc_assert (single_pred_p (entry));
  5852. gcc_assert (!bitmap_bit_p (bbs, single_pred (entry)->index));
  5853. }
  5854. else
  5855. for (ei = ei_start (bb->preds); !ei_end_p (ei); ei_next (&ei))
  5856. {
  5857. e = ei_edge (ei);
  5858. gcc_assert (bitmap_bit_p (bbs, e->src->index));
  5859. }
  5860. if (bb == exit)
  5861. {
  5862. gcc_assert (single_succ_p (exit));
  5863. gcc_assert (!bitmap_bit_p (bbs, single_succ (exit)->index));
  5864. }
  5865. else
  5866. for (ei = ei_start (bb->succs); !ei_end_p (ei); ei_next (&ei))
  5867. {
  5868. e = ei_edge (ei);
  5869. gcc_assert (bitmap_bit_p (bbs, e->dest->index));
  5870. }
  5871. }
  5872. BITMAP_FREE (bbs);
  5873. }
  5874. /* Move a single-entry, single-exit region delimited by ENTRY_BB and
  5875. EXIT_BB to function DEST_CFUN. The whole region is replaced by a
  5876. single basic block in the original CFG and the new basic block is
  5877. returned. DEST_CFUN must not have a CFG yet.
  5878. Note that the region need not be a pure SESE region. Blocks inside
  5879. the region may contain calls to abort/exit. The only restriction
  5880. is that ENTRY_BB should be the only entry point and it must
  5881. dominate EXIT_BB.
  5882. Change TREE_BLOCK of all statements in ORIG_BLOCK to the new
  5883. functions outermost BLOCK, move all subblocks of ORIG_BLOCK
  5884. to the new function.
  5885. All local variables referenced in the region are assumed to be in
  5886. the corresponding BLOCK_VARS and unexpanded variable lists
  5887. associated with DEST_CFUN. */
  5888. basic_block
  5889. move_sese_region_to_fn (struct function *dest_cfun, basic_block entry_bb,
  5890. basic_block exit_bb, tree orig_block)
  5891. {
  5892. vec<basic_block> bbs, dom_bbs;
  5893. basic_block dom_entry = get_immediate_dominator (CDI_DOMINATORS, entry_bb);
  5894. basic_block after, bb, *entry_pred, *exit_succ, abb;
  5895. struct function *saved_cfun = cfun;
  5896. int *entry_flag, *exit_flag;
  5897. unsigned *entry_prob, *exit_prob;
  5898. unsigned i, num_entry_edges, num_exit_edges, num_nodes;
  5899. edge e;
  5900. edge_iterator ei;
  5901. htab_t new_label_map;
  5902. hash_map<void *, void *> *eh_map;
  5903. struct loop *loop = entry_bb->loop_father;
  5904. struct loop *loop0 = get_loop (saved_cfun, 0);
  5905. struct move_stmt_d d;
  5906. /* If ENTRY does not strictly dominate EXIT, this cannot be an SESE
  5907. region. */
  5908. gcc_assert (entry_bb != exit_bb
  5909. && (!exit_bb
  5910. || dominated_by_p (CDI_DOMINATORS, exit_bb, entry_bb)));
  5911. /* Collect all the blocks in the region. Manually add ENTRY_BB
  5912. because it won't be added by dfs_enumerate_from. */
  5913. bbs.create (0);
  5914. bbs.safe_push (entry_bb);
  5915. gather_blocks_in_sese_region (entry_bb, exit_bb, &bbs);
  5916. #ifdef ENABLE_CHECKING
  5917. verify_sese (entry_bb, exit_bb, &bbs);
  5918. #endif
  5919. /* The blocks that used to be dominated by something in BBS will now be
  5920. dominated by the new block. */
  5921. dom_bbs = get_dominated_by_region (CDI_DOMINATORS,
  5922. bbs.address (),
  5923. bbs.length ());
  5924. /* Detach ENTRY_BB and EXIT_BB from CFUN->CFG. We need to remember
  5925. the predecessor edges to ENTRY_BB and the successor edges to
  5926. EXIT_BB so that we can re-attach them to the new basic block that
  5927. will replace the region. */
  5928. num_entry_edges = EDGE_COUNT (entry_bb->preds);
  5929. entry_pred = XNEWVEC (basic_block, num_entry_edges);
  5930. entry_flag = XNEWVEC (int, num_entry_edges);
  5931. entry_prob = XNEWVEC (unsigned, num_entry_edges);
  5932. i = 0;
  5933. for (ei = ei_start (entry_bb->preds); (e = ei_safe_edge (ei)) != NULL;)
  5934. {
  5935. entry_prob[i] = e->probability;
  5936. entry_flag[i] = e->flags;
  5937. entry_pred[i++] = e->src;
  5938. remove_edge (e);
  5939. }
  5940. if (exit_bb)
  5941. {
  5942. num_exit_edges = EDGE_COUNT (exit_bb->succs);
  5943. exit_succ = XNEWVEC (basic_block, num_exit_edges);
  5944. exit_flag = XNEWVEC (int, num_exit_edges);
  5945. exit_prob = XNEWVEC (unsigned, num_exit_edges);
  5946. i = 0;
  5947. for (ei = ei_start (exit_bb->succs); (e = ei_safe_edge (ei)) != NULL;)
  5948. {
  5949. exit_prob[i] = e->probability;
  5950. exit_flag[i] = e->flags;
  5951. exit_succ[i++] = e->dest;
  5952. remove_edge (e);
  5953. }
  5954. }
  5955. else
  5956. {
  5957. num_exit_edges = 0;
  5958. exit_succ = NULL;
  5959. exit_flag = NULL;
  5960. exit_prob = NULL;
  5961. }
  5962. /* Switch context to the child function to initialize DEST_FN's CFG. */
  5963. gcc_assert (dest_cfun->cfg == NULL);
  5964. push_cfun (dest_cfun);
  5965. init_empty_tree_cfg ();
  5966. /* Initialize EH information for the new function. */
  5967. eh_map = NULL;
  5968. new_label_map = NULL;
  5969. if (saved_cfun->eh)
  5970. {
  5971. eh_region region = NULL;
  5972. FOR_EACH_VEC_ELT (bbs, i, bb)
  5973. region = find_outermost_region_in_block (saved_cfun, bb, region);
  5974. init_eh_for_function ();
  5975. if (region != NULL)
  5976. {
  5977. new_label_map = htab_create (17, tree_map_hash, tree_map_eq, free);
  5978. eh_map = duplicate_eh_regions (saved_cfun, region, 0,
  5979. new_label_mapper, new_label_map);
  5980. }
  5981. }
  5982. /* Initialize an empty loop tree. */
  5983. struct loops *loops = ggc_cleared_alloc<struct loops> ();
  5984. init_loops_structure (dest_cfun, loops, 1);
  5985. loops->state = LOOPS_MAY_HAVE_MULTIPLE_LATCHES;
  5986. set_loops_for_fn (dest_cfun, loops);
  5987. /* Move the outlined loop tree part. */
  5988. num_nodes = bbs.length ();
  5989. FOR_EACH_VEC_ELT (bbs, i, bb)
  5990. {
  5991. if (bb->loop_father->header == bb)
  5992. {
  5993. struct loop *this_loop = bb->loop_father;
  5994. struct loop *outer = loop_outer (this_loop);
  5995. if (outer == loop
  5996. /* If the SESE region contains some bbs ending with
  5997. a noreturn call, those are considered to belong
  5998. to the outermost loop in saved_cfun, rather than
  5999. the entry_bb's loop_father. */
  6000. || outer == loop0)
  6001. {
  6002. if (outer != loop)
  6003. num_nodes -= this_loop->num_nodes;
  6004. flow_loop_tree_node_remove (bb->loop_father);
  6005. flow_loop_tree_node_add (get_loop (dest_cfun, 0), this_loop);
  6006. fixup_loop_arrays_after_move (saved_cfun, cfun, this_loop);
  6007. }
  6008. }
  6009. else if (bb->loop_father == loop0 && loop0 != loop)
  6010. num_nodes--;
  6011. /* Remove loop exits from the outlined region. */
  6012. if (loops_for_fn (saved_cfun)->exits)
  6013. FOR_EACH_EDGE (e, ei, bb->succs)
  6014. {
  6015. struct loops *l = loops_for_fn (saved_cfun);
  6016. loop_exit **slot
  6017. = l->exits->find_slot_with_hash (e, htab_hash_pointer (e),
  6018. NO_INSERT);
  6019. if (slot)
  6020. l->exits->clear_slot (slot);
  6021. }
  6022. }
  6023. /* Adjust the number of blocks in the tree root of the outlined part. */
  6024. get_loop (dest_cfun, 0)->num_nodes = bbs.length () + 2;
  6025. /* Setup a mapping to be used by move_block_to_fn. */
  6026. loop->aux = current_loops->tree_root;
  6027. loop0->aux = current_loops->tree_root;
  6028. pop_cfun ();
  6029. /* Move blocks from BBS into DEST_CFUN. */
  6030. gcc_assert (bbs.length () >= 2);
  6031. after = dest_cfun->cfg->x_entry_block_ptr;
  6032. hash_map<tree, tree> vars_map;
  6033. memset (&d, 0, sizeof (d));
  6034. d.orig_block = orig_block;
  6035. d.new_block = DECL_INITIAL (dest_cfun->decl);
  6036. d.from_context = cfun->decl;
  6037. d.to_context = dest_cfun->decl;
  6038. d.vars_map = &vars_map;
  6039. d.new_label_map = new_label_map;
  6040. d.eh_map = eh_map;
  6041. d.remap_decls_p = true;
  6042. FOR_EACH_VEC_ELT (bbs, i, bb)
  6043. {
  6044. /* No need to update edge counts on the last block. It has
  6045. already been updated earlier when we detached the region from
  6046. the original CFG. */
  6047. move_block_to_fn (dest_cfun, bb, after, bb != exit_bb, &d);
  6048. after = bb;
  6049. }
  6050. loop->aux = NULL;
  6051. loop0->aux = NULL;
  6052. /* Loop sizes are no longer correct, fix them up. */
  6053. loop->num_nodes -= num_nodes;
  6054. for (struct loop *outer = loop_outer (loop);
  6055. outer; outer = loop_outer (outer))
  6056. outer->num_nodes -= num_nodes;
  6057. loop0->num_nodes -= bbs.length () - num_nodes;
  6058. if (saved_cfun->has_simduid_loops || saved_cfun->has_force_vectorize_loops)
  6059. {
  6060. struct loop *aloop;
  6061. for (i = 0; vec_safe_iterate (loops->larray, i, &aloop); i++)
  6062. if (aloop != NULL)
  6063. {
  6064. if (aloop->simduid)
  6065. {
  6066. replace_by_duplicate_decl (&aloop->simduid, d.vars_map,
  6067. d.to_context);
  6068. dest_cfun->has_simduid_loops = true;
  6069. }
  6070. if (aloop->force_vectorize)
  6071. dest_cfun->has_force_vectorize_loops = true;
  6072. }
  6073. }
  6074. /* Rewire BLOCK_SUBBLOCKS of orig_block. */
  6075. if (orig_block)
  6076. {
  6077. tree block;
  6078. gcc_assert (BLOCK_SUBBLOCKS (DECL_INITIAL (dest_cfun->decl))
  6079. == NULL_TREE);
  6080. BLOCK_SUBBLOCKS (DECL_INITIAL (dest_cfun->decl))
  6081. = BLOCK_SUBBLOCKS (orig_block);
  6082. for (block = BLOCK_SUBBLOCKS (orig_block);
  6083. block; block = BLOCK_CHAIN (block))
  6084. BLOCK_SUPERCONTEXT (block) = DECL_INITIAL (dest_cfun->decl);
  6085. BLOCK_SUBBLOCKS (orig_block) = NULL_TREE;
  6086. }
  6087. replace_block_vars_by_duplicates (DECL_INITIAL (dest_cfun->decl),
  6088. &vars_map, dest_cfun->decl);
  6089. if (new_label_map)
  6090. htab_delete (new_label_map);
  6091. if (eh_map)
  6092. delete eh_map;
  6093. /* Rewire the entry and exit blocks. The successor to the entry
  6094. block turns into the successor of DEST_FN's ENTRY_BLOCK_PTR in
  6095. the child function. Similarly, the predecessor of DEST_FN's
  6096. EXIT_BLOCK_PTR turns into the predecessor of EXIT_BLOCK_PTR. We
  6097. need to switch CFUN between DEST_CFUN and SAVED_CFUN so that the
  6098. various CFG manipulation function get to the right CFG.
  6099. FIXME, this is silly. The CFG ought to become a parameter to
  6100. these helpers. */
  6101. push_cfun (dest_cfun);
  6102. make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), entry_bb, EDGE_FALLTHRU);
  6103. if (exit_bb)
  6104. make_edge (exit_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
  6105. pop_cfun ();
  6106. /* Back in the original function, the SESE region has disappeared,
  6107. create a new basic block in its place. */
  6108. bb = create_empty_bb (entry_pred[0]);
  6109. if (current_loops)
  6110. add_bb_to_loop (bb, loop);
  6111. for (i = 0; i < num_entry_edges; i++)
  6112. {
  6113. e = make_edge (entry_pred[i], bb, entry_flag[i]);
  6114. e->probability = entry_prob[i];
  6115. }
  6116. for (i = 0; i < num_exit_edges; i++)
  6117. {
  6118. e = make_edge (bb, exit_succ[i], exit_flag[i]);
  6119. e->probability = exit_prob[i];
  6120. }
  6121. set_immediate_dominator (CDI_DOMINATORS, bb, dom_entry);
  6122. FOR_EACH_VEC_ELT (dom_bbs, i, abb)
  6123. set_immediate_dominator (CDI_DOMINATORS, abb, bb);
  6124. dom_bbs.release ();
  6125. if (exit_bb)
  6126. {
  6127. free (exit_prob);
  6128. free (exit_flag);
  6129. free (exit_succ);
  6130. }
  6131. free (entry_prob);
  6132. free (entry_flag);
  6133. free (entry_pred);
  6134. bbs.release ();
  6135. return bb;
  6136. }
  6137. /* Dump FUNCTION_DECL FN to file FILE using FLAGS (see TDF_* in dumpfile.h)
  6138. */
  6139. void
  6140. dump_function_to_file (tree fndecl, FILE *file, int flags)
  6141. {
  6142. tree arg, var, old_current_fndecl = current_function_decl;
  6143. struct function *dsf;
  6144. bool ignore_topmost_bind = false, any_var = false;
  6145. basic_block bb;
  6146. tree chain;
  6147. bool tmclone = (TREE_CODE (fndecl) == FUNCTION_DECL
  6148. && decl_is_tm_clone (fndecl));
  6149. struct function *fun = DECL_STRUCT_FUNCTION (fndecl);
  6150. current_function_decl = fndecl;
  6151. fprintf (file, "%s %s(", function_name (fun), tmclone ? "[tm-clone] " : "");
  6152. arg = DECL_ARGUMENTS (fndecl);
  6153. while (arg)
  6154. {
  6155. print_generic_expr (file, TREE_TYPE (arg), dump_flags);
  6156. fprintf (file, " ");
  6157. print_generic_expr (file, arg, dump_flags);
  6158. if (flags & TDF_VERBOSE)
  6159. print_node (file, "", arg, 4);
  6160. if (DECL_CHAIN (arg))
  6161. fprintf (file, ", ");
  6162. arg = DECL_CHAIN (arg);
  6163. }
  6164. fprintf (file, ")\n");
  6165. if (flags & TDF_VERBOSE)
  6166. print_node (file, "", fndecl, 2);
  6167. dsf = DECL_STRUCT_FUNCTION (fndecl);
  6168. if (dsf && (flags & TDF_EH))
  6169. dump_eh_tree (file, dsf);
  6170. if (flags & TDF_RAW && !gimple_has_body_p (fndecl))
  6171. {
  6172. dump_node (fndecl, TDF_SLIM | flags, file);
  6173. current_function_decl = old_current_fndecl;
  6174. return;
  6175. }
  6176. /* When GIMPLE is lowered, the variables are no longer available in
  6177. BIND_EXPRs, so display them separately. */
  6178. if (fun && fun->decl == fndecl && (fun->curr_properties & PROP_gimple_lcf))
  6179. {
  6180. unsigned ix;
  6181. ignore_topmost_bind = true;
  6182. fprintf (file, "{\n");
  6183. if (!vec_safe_is_empty (fun->local_decls))
  6184. FOR_EACH_LOCAL_DECL (fun, ix, var)
  6185. {
  6186. print_generic_decl (file, var, flags);
  6187. if (flags & TDF_VERBOSE)
  6188. print_node (file, "", var, 4);
  6189. fprintf (file, "\n");
  6190. any_var = true;
  6191. }
  6192. if (gimple_in_ssa_p (cfun))
  6193. for (ix = 1; ix < num_ssa_names; ++ix)
  6194. {
  6195. tree name = ssa_name (ix);
  6196. if (name && !SSA_NAME_VAR (name))
  6197. {
  6198. fprintf (file, " ");
  6199. print_generic_expr (file, TREE_TYPE (name), flags);
  6200. fprintf (file, " ");
  6201. print_generic_expr (file, name, flags);
  6202. fprintf (file, ";\n");
  6203. any_var = true;
  6204. }
  6205. }
  6206. }
  6207. if (fun && fun->decl == fndecl
  6208. && fun->cfg
  6209. && basic_block_info_for_fn (fun))
  6210. {
  6211. /* If the CFG has been built, emit a CFG-based dump. */
  6212. if (!ignore_topmost_bind)
  6213. fprintf (file, "{\n");
  6214. if (any_var && n_basic_blocks_for_fn (fun))
  6215. fprintf (file, "\n");
  6216. FOR_EACH_BB_FN (bb, fun)
  6217. dump_bb (file, bb, 2, flags | TDF_COMMENT);
  6218. fprintf (file, "}\n");
  6219. }
  6220. else if (DECL_SAVED_TREE (fndecl) == NULL)
  6221. {
  6222. /* The function is now in GIMPLE form but the CFG has not been
  6223. built yet. Emit the single sequence of GIMPLE statements
  6224. that make up its body. */
  6225. gimple_seq body = gimple_body (fndecl);
  6226. if (gimple_seq_first_stmt (body)
  6227. && gimple_seq_first_stmt (body) == gimple_seq_last_stmt (body)
  6228. && gimple_code (gimple_seq_first_stmt (body)) == GIMPLE_BIND)
  6229. print_gimple_seq (file, body, 0, flags);
  6230. else
  6231. {
  6232. if (!ignore_topmost_bind)
  6233. fprintf (file, "{\n");
  6234. if (any_var)
  6235. fprintf (file, "\n");
  6236. print_gimple_seq (file, body, 2, flags);
  6237. fprintf (file, "}\n");
  6238. }
  6239. }
  6240. else
  6241. {
  6242. int indent;
  6243. /* Make a tree based dump. */
  6244. chain = DECL_SAVED_TREE (fndecl);
  6245. if (chain && TREE_CODE (chain) == BIND_EXPR)
  6246. {
  6247. if (ignore_topmost_bind)
  6248. {
  6249. chain = BIND_EXPR_BODY (chain);
  6250. indent = 2;
  6251. }
  6252. else
  6253. indent = 0;
  6254. }
  6255. else
  6256. {
  6257. if (!ignore_topmost_bind)
  6258. fprintf (file, "{\n");
  6259. indent = 2;
  6260. }
  6261. if (any_var)
  6262. fprintf (file, "\n");
  6263. print_generic_stmt_indented (file, chain, flags, indent);
  6264. if (ignore_topmost_bind)
  6265. fprintf (file, "}\n");
  6266. }
  6267. if (flags & TDF_ENUMERATE_LOCALS)
  6268. dump_enumerated_decls (file, flags);
  6269. fprintf (file, "\n\n");
  6270. current_function_decl = old_current_fndecl;
  6271. }
  6272. /* Dump FUNCTION_DECL FN to stderr using FLAGS (see TDF_* in tree.h) */
  6273. DEBUG_FUNCTION void
  6274. debug_function (tree fn, int flags)
  6275. {
  6276. dump_function_to_file (fn, stderr, flags);
  6277. }
  6278. /* Print on FILE the indexes for the predecessors of basic_block BB. */
  6279. static void
  6280. print_pred_bbs (FILE *file, basic_block bb)
  6281. {
  6282. edge e;
  6283. edge_iterator ei;
  6284. FOR_EACH_EDGE (e, ei, bb->preds)
  6285. fprintf (file, "bb_%d ", e->src->index);
  6286. }
  6287. /* Print on FILE the indexes for the successors of basic_block BB. */
  6288. static void
  6289. print_succ_bbs (FILE *file, basic_block bb)
  6290. {
  6291. edge e;
  6292. edge_iterator ei;
  6293. FOR_EACH_EDGE (e, ei, bb->succs)
  6294. fprintf (file, "bb_%d ", e->dest->index);
  6295. }
  6296. /* Print to FILE the basic block BB following the VERBOSITY level. */
  6297. void
  6298. print_loops_bb (FILE *file, basic_block bb, int indent, int verbosity)
  6299. {
  6300. char *s_indent = (char *) alloca ((size_t) indent + 1);
  6301. memset ((void *) s_indent, ' ', (size_t) indent);
  6302. s_indent[indent] = '\0';
  6303. /* Print basic_block's header. */
  6304. if (verbosity >= 2)
  6305. {
  6306. fprintf (file, "%s bb_%d (preds = {", s_indent, bb->index);
  6307. print_pred_bbs (file, bb);
  6308. fprintf (file, "}, succs = {");
  6309. print_succ_bbs (file, bb);
  6310. fprintf (file, "})\n");
  6311. }
  6312. /* Print basic_block's body. */
  6313. if (verbosity >= 3)
  6314. {
  6315. fprintf (file, "%s {\n", s_indent);
  6316. dump_bb (file, bb, indent + 4, TDF_VOPS|TDF_MEMSYMS);
  6317. fprintf (file, "%s }\n", s_indent);
  6318. }
  6319. }
  6320. static void print_loop_and_siblings (FILE *, struct loop *, int, int);
  6321. /* Pretty print LOOP on FILE, indented INDENT spaces. Following
  6322. VERBOSITY level this outputs the contents of the loop, or just its
  6323. structure. */
  6324. static void
  6325. print_loop (FILE *file, struct loop *loop, int indent, int verbosity)
  6326. {
  6327. char *s_indent;
  6328. basic_block bb;
  6329. if (loop == NULL)
  6330. return;
  6331. s_indent = (char *) alloca ((size_t) indent + 1);
  6332. memset ((void *) s_indent, ' ', (size_t) indent);
  6333. s_indent[indent] = '\0';
  6334. /* Print loop's header. */
  6335. fprintf (file, "%sloop_%d (", s_indent, loop->num);
  6336. if (loop->header)
  6337. fprintf (file, "header = %d", loop->header->index);
  6338. else
  6339. {
  6340. fprintf (file, "deleted)\n");
  6341. return;
  6342. }
  6343. if (loop->latch)
  6344. fprintf (file, ", latch = %d", loop->latch->index);
  6345. else
  6346. fprintf (file, ", multiple latches");
  6347. fprintf (file, ", niter = ");
  6348. print_generic_expr (file, loop->nb_iterations, 0);
  6349. if (loop->any_upper_bound)
  6350. {
  6351. fprintf (file, ", upper_bound = ");
  6352. print_decu (loop->nb_iterations_upper_bound, file);
  6353. }
  6354. if (loop->any_estimate)
  6355. {
  6356. fprintf (file, ", estimate = ");
  6357. print_decu (loop->nb_iterations_estimate, file);
  6358. }
  6359. fprintf (file, ")\n");
  6360. /* Print loop's body. */
  6361. if (verbosity >= 1)
  6362. {
  6363. fprintf (file, "%s{\n", s_indent);
  6364. FOR_EACH_BB_FN (bb, cfun)
  6365. if (bb->loop_father == loop)
  6366. print_loops_bb (file, bb, indent, verbosity);
  6367. print_loop_and_siblings (file, loop->inner, indent + 2, verbosity);
  6368. fprintf (file, "%s}\n", s_indent);
  6369. }
  6370. }
  6371. /* Print the LOOP and its sibling loops on FILE, indented INDENT
  6372. spaces. Following VERBOSITY level this outputs the contents of the
  6373. loop, or just its structure. */
  6374. static void
  6375. print_loop_and_siblings (FILE *file, struct loop *loop, int indent,
  6376. int verbosity)
  6377. {
  6378. if (loop == NULL)
  6379. return;
  6380. print_loop (file, loop, indent, verbosity);
  6381. print_loop_and_siblings (file, loop->next, indent, verbosity);
  6382. }
  6383. /* Follow a CFG edge from the entry point of the program, and on entry
  6384. of a loop, pretty print the loop structure on FILE. */
  6385. void
  6386. print_loops (FILE *file, int verbosity)
  6387. {
  6388. basic_block bb;
  6389. bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
  6390. if (bb && bb->loop_father)
  6391. print_loop_and_siblings (file, bb->loop_father, 0, verbosity);
  6392. }
  6393. /* Dump a loop. */
  6394. DEBUG_FUNCTION void
  6395. debug (struct loop &ref)
  6396. {
  6397. print_loop (stderr, &ref, 0, /*verbosity*/0);
  6398. }
  6399. DEBUG_FUNCTION void
  6400. debug (struct loop *ptr)
  6401. {
  6402. if (ptr)
  6403. debug (*ptr);
  6404. else
  6405. fprintf (stderr, "<nil>\n");
  6406. }
  6407. /* Dump a loop verbosely. */
  6408. DEBUG_FUNCTION void
  6409. debug_verbose (struct loop &ref)
  6410. {
  6411. print_loop (stderr, &ref, 0, /*verbosity*/3);
  6412. }
  6413. DEBUG_FUNCTION void
  6414. debug_verbose (struct loop *ptr)
  6415. {
  6416. if (ptr)
  6417. debug (*ptr);
  6418. else
  6419. fprintf (stderr, "<nil>\n");
  6420. }
  6421. /* Debugging loops structure at tree level, at some VERBOSITY level. */
  6422. DEBUG_FUNCTION void
  6423. debug_loops (int verbosity)
  6424. {
  6425. print_loops (stderr, verbosity);
  6426. }
  6427. /* Print on stderr the code of LOOP, at some VERBOSITY level. */
  6428. DEBUG_FUNCTION void
  6429. debug_loop (struct loop *loop, int verbosity)
  6430. {
  6431. print_loop (stderr, loop, 0, verbosity);
  6432. }
  6433. /* Print on stderr the code of loop number NUM, at some VERBOSITY
  6434. level. */
  6435. DEBUG_FUNCTION void
  6436. debug_loop_num (unsigned num, int verbosity)
  6437. {
  6438. debug_loop (get_loop (cfun, num), verbosity);
  6439. }
  6440. /* Return true if BB ends with a call, possibly followed by some
  6441. instructions that must stay with the call. Return false,
  6442. otherwise. */
  6443. static bool
  6444. gimple_block_ends_with_call_p (basic_block bb)
  6445. {
  6446. gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
  6447. return !gsi_end_p (gsi) && is_gimple_call (gsi_stmt (gsi));
  6448. }
  6449. /* Return true if BB ends with a conditional branch. Return false,
  6450. otherwise. */
  6451. static bool
  6452. gimple_block_ends_with_condjump_p (const_basic_block bb)
  6453. {
  6454. gimple stmt = last_stmt (CONST_CAST_BB (bb));
  6455. return (stmt && gimple_code (stmt) == GIMPLE_COND);
  6456. }
  6457. /* Return true if we need to add fake edge to exit at statement T.
  6458. Helper function for gimple_flow_call_edges_add. */
  6459. static bool
  6460. need_fake_edge_p (gimple t)
  6461. {
  6462. tree fndecl = NULL_TREE;
  6463. int call_flags = 0;
  6464. /* NORETURN and LONGJMP calls already have an edge to exit.
  6465. CONST and PURE calls do not need one.
  6466. We don't currently check for CONST and PURE here, although
  6467. it would be a good idea, because those attributes are
  6468. figured out from the RTL in mark_constant_function, and
  6469. the counter incrementation code from -fprofile-arcs
  6470. leads to different results from -fbranch-probabilities. */
  6471. if (is_gimple_call (t))
  6472. {
  6473. fndecl = gimple_call_fndecl (t);
  6474. call_flags = gimple_call_flags (t);
  6475. }
  6476. if (is_gimple_call (t)
  6477. && fndecl
  6478. && DECL_BUILT_IN (fndecl)
  6479. && (call_flags & ECF_NOTHROW)
  6480. && !(call_flags & ECF_RETURNS_TWICE)
  6481. /* fork() doesn't really return twice, but the effect of
  6482. wrapping it in __gcov_fork() which calls __gcov_flush()
  6483. and clears the counters before forking has the same
  6484. effect as returning twice. Force a fake edge. */
  6485. && !(DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
  6486. && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FORK))
  6487. return false;
  6488. if (is_gimple_call (t))
  6489. {
  6490. edge_iterator ei;
  6491. edge e;
  6492. basic_block bb;
  6493. if (!(call_flags & ECF_NORETURN))
  6494. return true;
  6495. bb = gimple_bb (t);
  6496. FOR_EACH_EDGE (e, ei, bb->succs)
  6497. if ((e->flags & EDGE_FAKE) == 0)
  6498. return true;
  6499. }
  6500. if (gasm *asm_stmt = dyn_cast <gasm *> (t))
  6501. if (gimple_asm_volatile_p (asm_stmt) || gimple_asm_input_p (asm_stmt))
  6502. return true;
  6503. return false;
  6504. }
  6505. /* Add fake edges to the function exit for any non constant and non
  6506. noreturn calls (or noreturn calls with EH/abnormal edges),
  6507. volatile inline assembly in the bitmap of blocks specified by BLOCKS
  6508. or to the whole CFG if BLOCKS is zero. Return the number of blocks
  6509. that were split.
  6510. The goal is to expose cases in which entering a basic block does
  6511. not imply that all subsequent instructions must be executed. */
  6512. static int
  6513. gimple_flow_call_edges_add (sbitmap blocks)
  6514. {
  6515. int i;
  6516. int blocks_split = 0;
  6517. int last_bb = last_basic_block_for_fn (cfun);
  6518. bool check_last_block = false;
  6519. if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS)
  6520. return 0;
  6521. if (! blocks)
  6522. check_last_block = true;
  6523. else
  6524. check_last_block = bitmap_bit_p (blocks,
  6525. EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb->index);
  6526. /* In the last basic block, before epilogue generation, there will be
  6527. a fallthru edge to EXIT. Special care is required if the last insn
  6528. of the last basic block is a call because make_edge folds duplicate
  6529. edges, which would result in the fallthru edge also being marked
  6530. fake, which would result in the fallthru edge being removed by
  6531. remove_fake_edges, which would result in an invalid CFG.
  6532. Moreover, we can't elide the outgoing fake edge, since the block
  6533. profiler needs to take this into account in order to solve the minimal
  6534. spanning tree in the case that the call doesn't return.
  6535. Handle this by adding a dummy instruction in a new last basic block. */
  6536. if (check_last_block)
  6537. {
  6538. basic_block bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
  6539. gimple_stmt_iterator gsi = gsi_last_nondebug_bb (bb);
  6540. gimple t = NULL;
  6541. if (!gsi_end_p (gsi))
  6542. t = gsi_stmt (gsi);
  6543. if (t && need_fake_edge_p (t))
  6544. {
  6545. edge e;
  6546. e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun));
  6547. if (e)
  6548. {
  6549. gsi_insert_on_edge (e, gimple_build_nop ());
  6550. gsi_commit_edge_inserts ();
  6551. }
  6552. }
  6553. }
  6554. /* Now add fake edges to the function exit for any non constant
  6555. calls since there is no way that we can determine if they will
  6556. return or not... */
  6557. for (i = 0; i < last_bb; i++)
  6558. {
  6559. basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
  6560. gimple_stmt_iterator gsi;
  6561. gimple stmt, last_stmt;
  6562. if (!bb)
  6563. continue;
  6564. if (blocks && !bitmap_bit_p (blocks, i))
  6565. continue;
  6566. gsi = gsi_last_nondebug_bb (bb);
  6567. if (!gsi_end_p (gsi))
  6568. {
  6569. last_stmt = gsi_stmt (gsi);
  6570. do
  6571. {
  6572. stmt = gsi_stmt (gsi);
  6573. if (need_fake_edge_p (stmt))
  6574. {
  6575. edge e;
  6576. /* The handling above of the final block before the
  6577. epilogue should be enough to verify that there is
  6578. no edge to the exit block in CFG already.
  6579. Calling make_edge in such case would cause us to
  6580. mark that edge as fake and remove it later. */
  6581. #ifdef ENABLE_CHECKING
  6582. if (stmt == last_stmt)
  6583. {
  6584. e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun));
  6585. gcc_assert (e == NULL);
  6586. }
  6587. #endif
  6588. /* Note that the following may create a new basic block
  6589. and renumber the existing basic blocks. */
  6590. if (stmt != last_stmt)
  6591. {
  6592. e = split_block (bb, stmt);
  6593. if (e)
  6594. blocks_split++;
  6595. }
  6596. make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FAKE);
  6597. }
  6598. gsi_prev (&gsi);
  6599. }
  6600. while (!gsi_end_p (gsi));
  6601. }
  6602. }
  6603. if (blocks_split)
  6604. verify_flow_info ();
  6605. return blocks_split;
  6606. }
  6607. /* Removes edge E and all the blocks dominated by it, and updates dominance
  6608. information. The IL in E->src needs to be updated separately.
  6609. If dominance info is not available, only the edge E is removed.*/
  6610. void
  6611. remove_edge_and_dominated_blocks (edge e)
  6612. {
  6613. vec<basic_block> bbs_to_remove = vNULL;
  6614. vec<basic_block> bbs_to_fix_dom = vNULL;
  6615. bitmap df, df_idom;
  6616. edge f;
  6617. edge_iterator ei;
  6618. bool none_removed = false;
  6619. unsigned i;
  6620. basic_block bb, dbb;
  6621. bitmap_iterator bi;
  6622. /* If we are removing a path inside a non-root loop that may change
  6623. loop ownership of blocks or remove loops. Mark loops for fixup. */
  6624. if (current_loops
  6625. && loop_outer (e->src->loop_father) != NULL
  6626. && e->src->loop_father == e->dest->loop_father)
  6627. loops_state_set (LOOPS_NEED_FIXUP);
  6628. if (!dom_info_available_p (CDI_DOMINATORS))
  6629. {
  6630. remove_edge (e);
  6631. return;
  6632. }
  6633. /* No updating is needed for edges to exit. */
  6634. if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
  6635. {
  6636. if (cfgcleanup_altered_bbs)
  6637. bitmap_set_bit (cfgcleanup_altered_bbs, e->src->index);
  6638. remove_edge (e);
  6639. return;
  6640. }
  6641. /* First, we find the basic blocks to remove. If E->dest has a predecessor
  6642. that is not dominated by E->dest, then this set is empty. Otherwise,
  6643. all the basic blocks dominated by E->dest are removed.
  6644. Also, to DF_IDOM we store the immediate dominators of the blocks in
  6645. the dominance frontier of E (i.e., of the successors of the
  6646. removed blocks, if there are any, and of E->dest otherwise). */
  6647. FOR_EACH_EDGE (f, ei, e->dest->preds)
  6648. {
  6649. if (f == e)
  6650. continue;
  6651. if (!dominated_by_p (CDI_DOMINATORS, f->src, e->dest))
  6652. {
  6653. none_removed = true;
  6654. break;
  6655. }
  6656. }
  6657. df = BITMAP_ALLOC (NULL);
  6658. df_idom = BITMAP_ALLOC (NULL);
  6659. if (none_removed)
  6660. bitmap_set_bit (df_idom,
  6661. get_immediate_dominator (CDI_DOMINATORS, e->dest)->index);
  6662. else
  6663. {
  6664. bbs_to_remove = get_all_dominated_blocks (CDI_DOMINATORS, e->dest);
  6665. FOR_EACH_VEC_ELT (bbs_to_remove, i, bb)
  6666. {
  6667. FOR_EACH_EDGE (f, ei, bb->succs)
  6668. {
  6669. if (f->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
  6670. bitmap_set_bit (df, f->dest->index);
  6671. }
  6672. }
  6673. FOR_EACH_VEC_ELT (bbs_to_remove, i, bb)
  6674. bitmap_clear_bit (df, bb->index);
  6675. EXECUTE_IF_SET_IN_BITMAP (df, 0, i, bi)
  6676. {
  6677. bb = BASIC_BLOCK_FOR_FN (cfun, i);
  6678. bitmap_set_bit (df_idom,
  6679. get_immediate_dominator (CDI_DOMINATORS, bb)->index);
  6680. }
  6681. }
  6682. if (cfgcleanup_altered_bbs)
  6683. {
  6684. /* Record the set of the altered basic blocks. */
  6685. bitmap_set_bit (cfgcleanup_altered_bbs, e->src->index);
  6686. bitmap_ior_into (cfgcleanup_altered_bbs, df);
  6687. }
  6688. /* Remove E and the cancelled blocks. */
  6689. if (none_removed)
  6690. remove_edge (e);
  6691. else
  6692. {
  6693. /* Walk backwards so as to get a chance to substitute all
  6694. released DEFs into debug stmts. See
  6695. eliminate_unnecessary_stmts() in tree-ssa-dce.c for more
  6696. details. */
  6697. for (i = bbs_to_remove.length (); i-- > 0; )
  6698. delete_basic_block (bbs_to_remove[i]);
  6699. }
  6700. /* Update the dominance information. The immediate dominator may change only
  6701. for blocks whose immediate dominator belongs to DF_IDOM:
  6702. Suppose that idom(X) = Y before removal of E and idom(X) != Y after the
  6703. removal. Let Z the arbitrary block such that idom(Z) = Y and
  6704. Z dominates X after the removal. Before removal, there exists a path P
  6705. from Y to X that avoids Z. Let F be the last edge on P that is
  6706. removed, and let W = F->dest. Before removal, idom(W) = Y (since Y
  6707. dominates W, and because of P, Z does not dominate W), and W belongs to
  6708. the dominance frontier of E. Therefore, Y belongs to DF_IDOM. */
  6709. EXECUTE_IF_SET_IN_BITMAP (df_idom, 0, i, bi)
  6710. {
  6711. bb = BASIC_BLOCK_FOR_FN (cfun, i);
  6712. for (dbb = first_dom_son (CDI_DOMINATORS, bb);
  6713. dbb;
  6714. dbb = next_dom_son (CDI_DOMINATORS, dbb))
  6715. bbs_to_fix_dom.safe_push (dbb);
  6716. }
  6717. iterate_fix_dominators (CDI_DOMINATORS, bbs_to_fix_dom, true);
  6718. BITMAP_FREE (df);
  6719. BITMAP_FREE (df_idom);
  6720. bbs_to_remove.release ();
  6721. bbs_to_fix_dom.release ();
  6722. }
  6723. /* Purge dead EH edges from basic block BB. */
  6724. bool
  6725. gimple_purge_dead_eh_edges (basic_block bb)
  6726. {
  6727. bool changed = false;
  6728. edge e;
  6729. edge_iterator ei;
  6730. gimple stmt = last_stmt (bb);
  6731. if (stmt && stmt_can_throw_internal (stmt))
  6732. return false;
  6733. for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
  6734. {
  6735. if (e->flags & EDGE_EH)
  6736. {
  6737. remove_edge_and_dominated_blocks (e);
  6738. changed = true;
  6739. }
  6740. else
  6741. ei_next (&ei);
  6742. }
  6743. return changed;
  6744. }
  6745. /* Purge dead EH edges from basic block listed in BLOCKS. */
  6746. bool
  6747. gimple_purge_all_dead_eh_edges (const_bitmap blocks)
  6748. {
  6749. bool changed = false;
  6750. unsigned i;
  6751. bitmap_iterator bi;
  6752. EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
  6753. {
  6754. basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
  6755. /* Earlier gimple_purge_dead_eh_edges could have removed
  6756. this basic block already. */
  6757. gcc_assert (bb || changed);
  6758. if (bb != NULL)
  6759. changed |= gimple_purge_dead_eh_edges (bb);
  6760. }
  6761. return changed;
  6762. }
  6763. /* Purge dead abnormal call edges from basic block BB. */
  6764. bool
  6765. gimple_purge_dead_abnormal_call_edges (basic_block bb)
  6766. {
  6767. bool changed = false;
  6768. edge e;
  6769. edge_iterator ei;
  6770. gimple stmt = last_stmt (bb);
  6771. if (!cfun->has_nonlocal_label
  6772. && !cfun->calls_setjmp)
  6773. return false;
  6774. if (stmt && stmt_can_make_abnormal_goto (stmt))
  6775. return false;
  6776. for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
  6777. {
  6778. if (e->flags & EDGE_ABNORMAL)
  6779. {
  6780. if (e->flags & EDGE_FALLTHRU)
  6781. e->flags &= ~EDGE_ABNORMAL;
  6782. else
  6783. remove_edge_and_dominated_blocks (e);
  6784. changed = true;
  6785. }
  6786. else
  6787. ei_next (&ei);
  6788. }
  6789. return changed;
  6790. }
  6791. /* Purge dead abnormal call edges from basic block listed in BLOCKS. */
  6792. bool
  6793. gimple_purge_all_dead_abnormal_call_edges (const_bitmap blocks)
  6794. {
  6795. bool changed = false;
  6796. unsigned i;
  6797. bitmap_iterator bi;
  6798. EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
  6799. {
  6800. basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
  6801. /* Earlier gimple_purge_dead_abnormal_call_edges could have removed
  6802. this basic block already. */
  6803. gcc_assert (bb || changed);
  6804. if (bb != NULL)
  6805. changed |= gimple_purge_dead_abnormal_call_edges (bb);
  6806. }
  6807. return changed;
  6808. }
  6809. /* This function is called whenever a new edge is created or
  6810. redirected. */
  6811. static void
  6812. gimple_execute_on_growing_pred (edge e)
  6813. {
  6814. basic_block bb = e->dest;
  6815. if (!gimple_seq_empty_p (phi_nodes (bb)))
  6816. reserve_phi_args_for_new_edge (bb);
  6817. }
  6818. /* This function is called immediately before edge E is removed from
  6819. the edge vector E->dest->preds. */
  6820. static void
  6821. gimple_execute_on_shrinking_pred (edge e)
  6822. {
  6823. if (!gimple_seq_empty_p (phi_nodes (e->dest)))
  6824. remove_phi_args (e);
  6825. }
  6826. /*---------------------------------------------------------------------------
  6827. Helper functions for Loop versioning
  6828. ---------------------------------------------------------------------------*/
  6829. /* Adjust phi nodes for 'first' basic block. 'second' basic block is a copy
  6830. of 'first'. Both of them are dominated by 'new_head' basic block. When
  6831. 'new_head' was created by 'second's incoming edge it received phi arguments
  6832. on the edge by split_edge(). Later, additional edge 'e' was created to
  6833. connect 'new_head' and 'first'. Now this routine adds phi args on this
  6834. additional edge 'e' that new_head to second edge received as part of edge
  6835. splitting. */
  6836. static void
  6837. gimple_lv_adjust_loop_header_phi (basic_block first, basic_block second,
  6838. basic_block new_head, edge e)
  6839. {
  6840. gphi *phi1, *phi2;
  6841. gphi_iterator psi1, psi2;
  6842. tree def;
  6843. edge e2 = find_edge (new_head, second);
  6844. /* Because NEW_HEAD has been created by splitting SECOND's incoming
  6845. edge, we should always have an edge from NEW_HEAD to SECOND. */
  6846. gcc_assert (e2 != NULL);
  6847. /* Browse all 'second' basic block phi nodes and add phi args to
  6848. edge 'e' for 'first' head. PHI args are always in correct order. */
  6849. for (psi2 = gsi_start_phis (second),
  6850. psi1 = gsi_start_phis (first);
  6851. !gsi_end_p (psi2) && !gsi_end_p (psi1);
  6852. gsi_next (&psi2), gsi_next (&psi1))
  6853. {
  6854. phi1 = psi1.phi ();
  6855. phi2 = psi2.phi ();
  6856. def = PHI_ARG_DEF (phi2, e2->dest_idx);
  6857. add_phi_arg (phi1, def, e, gimple_phi_arg_location_from_edge (phi2, e2));
  6858. }
  6859. }
  6860. /* Adds a if else statement to COND_BB with condition COND_EXPR.
  6861. SECOND_HEAD is the destination of the THEN and FIRST_HEAD is
  6862. the destination of the ELSE part. */
  6863. static void
  6864. gimple_lv_add_condition_to_bb (basic_block first_head ATTRIBUTE_UNUSED,
  6865. basic_block second_head ATTRIBUTE_UNUSED,
  6866. basic_block cond_bb, void *cond_e)
  6867. {
  6868. gimple_stmt_iterator gsi;
  6869. gimple new_cond_expr;
  6870. tree cond_expr = (tree) cond_e;
  6871. edge e0;
  6872. /* Build new conditional expr */
  6873. new_cond_expr = gimple_build_cond_from_tree (cond_expr,
  6874. NULL_TREE, NULL_TREE);
  6875. /* Add new cond in cond_bb. */
  6876. gsi = gsi_last_bb (cond_bb);
  6877. gsi_insert_after (&gsi, new_cond_expr, GSI_NEW_STMT);
  6878. /* Adjust edges appropriately to connect new head with first head
  6879. as well as second head. */
  6880. e0 = single_succ_edge (cond_bb);
  6881. e0->flags &= ~EDGE_FALLTHRU;
  6882. e0->flags |= EDGE_FALSE_VALUE;
  6883. }
  6884. /* Do book-keeping of basic block BB for the profile consistency checker.
  6885. If AFTER_PASS is 0, do pre-pass accounting, or if AFTER_PASS is 1
  6886. then do post-pass accounting. Store the counting in RECORD. */
  6887. static void
  6888. gimple_account_profile_record (basic_block bb, int after_pass,
  6889. struct profile_record *record)
  6890. {
  6891. gimple_stmt_iterator i;
  6892. for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
  6893. {
  6894. record->size[after_pass]
  6895. += estimate_num_insns (gsi_stmt (i), &eni_size_weights);
  6896. if (profile_status_for_fn (cfun) == PROFILE_READ)
  6897. record->time[after_pass]
  6898. += estimate_num_insns (gsi_stmt (i),
  6899. &eni_time_weights) * bb->count;
  6900. else if (profile_status_for_fn (cfun) == PROFILE_GUESSED)
  6901. record->time[after_pass]
  6902. += estimate_num_insns (gsi_stmt (i),
  6903. &eni_time_weights) * bb->frequency;
  6904. }
  6905. }
  6906. struct cfg_hooks gimple_cfg_hooks = {
  6907. "gimple",
  6908. gimple_verify_flow_info,
  6909. gimple_dump_bb, /* dump_bb */
  6910. gimple_dump_bb_for_graph, /* dump_bb_for_graph */
  6911. create_bb, /* create_basic_block */
  6912. gimple_redirect_edge_and_branch, /* redirect_edge_and_branch */
  6913. gimple_redirect_edge_and_branch_force, /* redirect_edge_and_branch_force */
  6914. gimple_can_remove_branch_p, /* can_remove_branch_p */
  6915. remove_bb, /* delete_basic_block */
  6916. gimple_split_block, /* split_block */
  6917. gimple_move_block_after, /* move_block_after */
  6918. gimple_can_merge_blocks_p, /* can_merge_blocks_p */
  6919. gimple_merge_blocks, /* merge_blocks */
  6920. gimple_predict_edge, /* predict_edge */
  6921. gimple_predicted_by_p, /* predicted_by_p */
  6922. gimple_can_duplicate_bb_p, /* can_duplicate_block_p */
  6923. gimple_duplicate_bb, /* duplicate_block */
  6924. gimple_split_edge, /* split_edge */
  6925. gimple_make_forwarder_block, /* make_forward_block */
  6926. NULL, /* tidy_fallthru_edge */
  6927. NULL, /* force_nonfallthru */
  6928. gimple_block_ends_with_call_p,/* block_ends_with_call_p */
  6929. gimple_block_ends_with_condjump_p, /* block_ends_with_condjump_p */
  6930. gimple_flow_call_edges_add, /* flow_call_edges_add */
  6931. gimple_execute_on_growing_pred, /* execute_on_growing_pred */
  6932. gimple_execute_on_shrinking_pred, /* execute_on_shrinking_pred */
  6933. gimple_duplicate_loop_to_header_edge, /* duplicate loop for trees */
  6934. gimple_lv_add_condition_to_bb, /* lv_add_condition_to_bb */
  6935. gimple_lv_adjust_loop_header_phi, /* lv_adjust_loop_header_phi*/
  6936. extract_true_false_edges_from_block, /* extract_cond_bb_edges */
  6937. flush_pending_stmts, /* flush_pending_stmts */
  6938. gimple_empty_block_p, /* block_empty_p */
  6939. gimple_split_block_before_cond_jump, /* split_block_before_cond_jump */
  6940. gimple_account_profile_record,
  6941. };
  6942. /* Split all critical edges. */
  6943. unsigned int
  6944. split_critical_edges (void)
  6945. {
  6946. basic_block bb;
  6947. edge e;
  6948. edge_iterator ei;
  6949. /* split_edge can redirect edges out of SWITCH_EXPRs, which can get
  6950. expensive. So we want to enable recording of edge to CASE_LABEL_EXPR
  6951. mappings around the calls to split_edge. */
  6952. start_recording_case_labels ();
  6953. FOR_ALL_BB_FN (bb, cfun)
  6954. {
  6955. FOR_EACH_EDGE (e, ei, bb->succs)
  6956. {
  6957. if (EDGE_CRITICAL_P (e) && !(e->flags & EDGE_ABNORMAL))
  6958. split_edge (e);
  6959. /* PRE inserts statements to edges and expects that
  6960. since split_critical_edges was done beforehand, committing edge
  6961. insertions will not split more edges. In addition to critical
  6962. edges we must split edges that have multiple successors and
  6963. end by control flow statements, such as RESX.
  6964. Go ahead and split them too. This matches the logic in
  6965. gimple_find_edge_insert_loc. */
  6966. else if ((!single_pred_p (e->dest)
  6967. || !gimple_seq_empty_p (phi_nodes (e->dest))
  6968. || e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
  6969. && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
  6970. && !(e->flags & EDGE_ABNORMAL))
  6971. {
  6972. gimple_stmt_iterator gsi;
  6973. gsi = gsi_last_bb (e->src);
  6974. if (!gsi_end_p (gsi)
  6975. && stmt_ends_bb_p (gsi_stmt (gsi))
  6976. && (gimple_code (gsi_stmt (gsi)) != GIMPLE_RETURN
  6977. && !gimple_call_builtin_p (gsi_stmt (gsi),
  6978. BUILT_IN_RETURN)))
  6979. split_edge (e);
  6980. }
  6981. }
  6982. }
  6983. end_recording_case_labels ();
  6984. return 0;
  6985. }
  6986. namespace {
  6987. const pass_data pass_data_split_crit_edges =
  6988. {
  6989. GIMPLE_PASS, /* type */
  6990. "crited", /* name */
  6991. OPTGROUP_NONE, /* optinfo_flags */
  6992. TV_TREE_SPLIT_EDGES, /* tv_id */
  6993. PROP_cfg, /* properties_required */
  6994. PROP_no_crit_edges, /* properties_provided */
  6995. 0, /* properties_destroyed */
  6996. 0, /* todo_flags_start */
  6997. 0, /* todo_flags_finish */
  6998. };
  6999. class pass_split_crit_edges : public gimple_opt_pass
  7000. {
  7001. public:
  7002. pass_split_crit_edges (gcc::context *ctxt)
  7003. : gimple_opt_pass (pass_data_split_crit_edges, ctxt)
  7004. {}
  7005. /* opt_pass methods: */
  7006. virtual unsigned int execute (function *) { return split_critical_edges (); }
  7007. opt_pass * clone () { return new pass_split_crit_edges (m_ctxt); }
  7008. }; // class pass_split_crit_edges
  7009. } // anon namespace
  7010. gimple_opt_pass *
  7011. make_pass_split_crit_edges (gcc::context *ctxt)
  7012. {
  7013. return new pass_split_crit_edges (ctxt);
  7014. }
  7015. /* Insert COND expression which is GIMPLE_COND after STMT
  7016. in basic block BB with appropriate basic block split
  7017. and creation of a new conditionally executed basic block.
  7018. Return created basic block. */
  7019. basic_block
  7020. insert_cond_bb (basic_block bb, gimple stmt, gimple cond)
  7021. {
  7022. edge fall = split_block (bb, stmt);
  7023. gimple_stmt_iterator iter = gsi_last_bb (bb);
  7024. basic_block new_bb;
  7025. /* Insert cond statement. */
  7026. gcc_assert (gimple_code (cond) == GIMPLE_COND);
  7027. if (gsi_end_p (iter))
  7028. gsi_insert_before (&iter, cond, GSI_CONTINUE_LINKING);
  7029. else
  7030. gsi_insert_after (&iter, cond, GSI_CONTINUE_LINKING);
  7031. /* Create conditionally executed block. */
  7032. new_bb = create_empty_bb (bb);
  7033. make_edge (bb, new_bb, EDGE_TRUE_VALUE);
  7034. make_single_succ_edge (new_bb, fall->dest, EDGE_FALLTHRU);
  7035. /* Fix edge for split bb. */
  7036. fall->flags = EDGE_FALSE_VALUE;
  7037. /* Update dominance info. */
  7038. if (dom_info_available_p (CDI_DOMINATORS))
  7039. {
  7040. set_immediate_dominator (CDI_DOMINATORS, new_bb, bb);
  7041. set_immediate_dominator (CDI_DOMINATORS, fall->dest, bb);
  7042. }
  7043. /* Update loop info. */
  7044. if (current_loops)
  7045. add_bb_to_loop (new_bb, bb->loop_father);
  7046. return new_bb;
  7047. }
  7048. /* Build a ternary operation and gimplify it. Emit code before GSI.
  7049. Return the gimple_val holding the result. */
  7050. tree
  7051. gimplify_build3 (gimple_stmt_iterator *gsi, enum tree_code code,
  7052. tree type, tree a, tree b, tree c)
  7053. {
  7054. tree ret;
  7055. location_t loc = gimple_location (gsi_stmt (*gsi));
  7056. ret = fold_build3_loc (loc, code, type, a, b, c);
  7057. STRIP_NOPS (ret);
  7058. return force_gimple_operand_gsi (gsi, ret, true, NULL, true,
  7059. GSI_SAME_STMT);
  7060. }
  7061. /* Build a binary operation and gimplify it. Emit code before GSI.
  7062. Return the gimple_val holding the result. */
  7063. tree
  7064. gimplify_build2 (gimple_stmt_iterator *gsi, enum tree_code code,
  7065. tree type, tree a, tree b)
  7066. {
  7067. tree ret;
  7068. ret = fold_build2_loc (gimple_location (gsi_stmt (*gsi)), code, type, a, b);
  7069. STRIP_NOPS (ret);
  7070. return force_gimple_operand_gsi (gsi, ret, true, NULL, true,
  7071. GSI_SAME_STMT);
  7072. }
  7073. /* Build a unary operation and gimplify it. Emit code before GSI.
  7074. Return the gimple_val holding the result. */
  7075. tree
  7076. gimplify_build1 (gimple_stmt_iterator *gsi, enum tree_code code, tree type,
  7077. tree a)
  7078. {
  7079. tree ret;
  7080. ret = fold_build1_loc (gimple_location (gsi_stmt (*gsi)), code, type, a);
  7081. STRIP_NOPS (ret);
  7082. return force_gimple_operand_gsi (gsi, ret, true, NULL, true,
  7083. GSI_SAME_STMT);
  7084. }
  7085. /* Given a basic block B which ends with a conditional and has
  7086. precisely two successors, determine which of the edges is taken if
  7087. the conditional is true and which is taken if the conditional is
  7088. false. Set TRUE_EDGE and FALSE_EDGE appropriately. */
  7089. void
  7090. extract_true_false_edges_from_block (basic_block b,
  7091. edge *true_edge,
  7092. edge *false_edge)
  7093. {
  7094. edge e = EDGE_SUCC (b, 0);
  7095. if (e->flags & EDGE_TRUE_VALUE)
  7096. {
  7097. *true_edge = e;
  7098. *false_edge = EDGE_SUCC (b, 1);
  7099. }
  7100. else
  7101. {
  7102. *false_edge = e;
  7103. *true_edge = EDGE_SUCC (b, 1);
  7104. }
  7105. }
  7106. /* Emit return warnings. */
  7107. namespace {
  7108. const pass_data pass_data_warn_function_return =
  7109. {
  7110. GIMPLE_PASS, /* type */
  7111. "*warn_function_return", /* name */
  7112. OPTGROUP_NONE, /* optinfo_flags */
  7113. TV_NONE, /* tv_id */
  7114. PROP_cfg, /* properties_required */
  7115. 0, /* properties_provided */
  7116. 0, /* properties_destroyed */
  7117. 0, /* todo_flags_start */
  7118. 0, /* todo_flags_finish */
  7119. };
  7120. class pass_warn_function_return : public gimple_opt_pass
  7121. {
  7122. public:
  7123. pass_warn_function_return (gcc::context *ctxt)
  7124. : gimple_opt_pass (pass_data_warn_function_return, ctxt)
  7125. {}
  7126. /* opt_pass methods: */
  7127. virtual unsigned int execute (function *);
  7128. }; // class pass_warn_function_return
  7129. unsigned int
  7130. pass_warn_function_return::execute (function *fun)
  7131. {
  7132. source_location location;
  7133. gimple last;
  7134. edge e;
  7135. edge_iterator ei;
  7136. if (!targetm.warn_func_return (fun->decl))
  7137. return 0;
  7138. /* If we have a path to EXIT, then we do return. */
  7139. if (TREE_THIS_VOLATILE (fun->decl)
  7140. && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) > 0)
  7141. {
  7142. location = UNKNOWN_LOCATION;
  7143. FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (fun)->preds)
  7144. {
  7145. last = last_stmt (e->src);
  7146. if ((gimple_code (last) == GIMPLE_RETURN
  7147. || gimple_call_builtin_p (last, BUILT_IN_RETURN))
  7148. && (location = gimple_location (last)) != UNKNOWN_LOCATION)
  7149. break;
  7150. }
  7151. if (location == UNKNOWN_LOCATION)
  7152. location = cfun->function_end_locus;
  7153. warning_at (location, 0, "%<noreturn%> function does return");
  7154. }
  7155. /* If we see "return;" in some basic block, then we do reach the end
  7156. without returning a value. */
  7157. else if (warn_return_type
  7158. && !TREE_NO_WARNING (fun->decl)
  7159. && EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (fun)->preds) > 0
  7160. && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fun->decl))))
  7161. {
  7162. FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (fun)->preds)
  7163. {
  7164. gimple last = last_stmt (e->src);
  7165. greturn *return_stmt = dyn_cast <greturn *> (last);
  7166. if (return_stmt
  7167. && gimple_return_retval (return_stmt) == NULL
  7168. && !gimple_no_warning_p (last))
  7169. {
  7170. location = gimple_location (last);
  7171. if (location == UNKNOWN_LOCATION)
  7172. location = fun->function_end_locus;
  7173. warning_at (location, OPT_Wreturn_type, "control reaches end of non-void function");
  7174. TREE_NO_WARNING (fun->decl) = 1;
  7175. break;
  7176. }
  7177. }
  7178. }
  7179. return 0;
  7180. }
  7181. } // anon namespace
  7182. gimple_opt_pass *
  7183. make_pass_warn_function_return (gcc::context *ctxt)
  7184. {
  7185. return new pass_warn_function_return (ctxt);
  7186. }
  7187. /* Walk a gimplified function and warn for functions whose return value is
  7188. ignored and attribute((warn_unused_result)) is set. This is done before
  7189. inlining, so we don't have to worry about that. */
  7190. static void
  7191. do_warn_unused_result (gimple_seq seq)
  7192. {
  7193. tree fdecl, ftype;
  7194. gimple_stmt_iterator i;
  7195. for (i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
  7196. {
  7197. gimple g = gsi_stmt (i);
  7198. switch (gimple_code (g))
  7199. {
  7200. case GIMPLE_BIND:
  7201. do_warn_unused_result (gimple_bind_body (as_a <gbind *>(g)));
  7202. break;
  7203. case GIMPLE_TRY:
  7204. do_warn_unused_result (gimple_try_eval (g));
  7205. do_warn_unused_result (gimple_try_cleanup (g));
  7206. break;
  7207. case GIMPLE_CATCH:
  7208. do_warn_unused_result (gimple_catch_handler (
  7209. as_a <gcatch *> (g)));
  7210. break;
  7211. case GIMPLE_EH_FILTER:
  7212. do_warn_unused_result (gimple_eh_filter_failure (g));
  7213. break;
  7214. case GIMPLE_CALL:
  7215. if (gimple_call_lhs (g))
  7216. break;
  7217. if (gimple_call_internal_p (g))
  7218. break;
  7219. /* This is a naked call, as opposed to a GIMPLE_CALL with an
  7220. LHS. All calls whose value is ignored should be
  7221. represented like this. Look for the attribute. */
  7222. fdecl = gimple_call_fndecl (g);
  7223. ftype = gimple_call_fntype (g);
  7224. if (lookup_attribute ("warn_unused_result", TYPE_ATTRIBUTES (ftype)))
  7225. {
  7226. location_t loc = gimple_location (g);
  7227. if (fdecl)
  7228. warning_at (loc, OPT_Wunused_result,
  7229. "ignoring return value of %qD, "
  7230. "declared with attribute warn_unused_result",
  7231. fdecl);
  7232. else
  7233. warning_at (loc, OPT_Wunused_result,
  7234. "ignoring return value of function "
  7235. "declared with attribute warn_unused_result");
  7236. }
  7237. break;
  7238. default:
  7239. /* Not a container, not a call, or a call whose value is used. */
  7240. break;
  7241. }
  7242. }
  7243. }
  7244. namespace {
  7245. const pass_data pass_data_warn_unused_result =
  7246. {
  7247. GIMPLE_PASS, /* type */
  7248. "*warn_unused_result", /* name */
  7249. OPTGROUP_NONE, /* optinfo_flags */
  7250. TV_NONE, /* tv_id */
  7251. PROP_gimple_any, /* properties_required */
  7252. 0, /* properties_provided */
  7253. 0, /* properties_destroyed */
  7254. 0, /* todo_flags_start */
  7255. 0, /* todo_flags_finish */
  7256. };
  7257. class pass_warn_unused_result : public gimple_opt_pass
  7258. {
  7259. public:
  7260. pass_warn_unused_result (gcc::context *ctxt)
  7261. : gimple_opt_pass (pass_data_warn_unused_result, ctxt)
  7262. {}
  7263. /* opt_pass methods: */
  7264. virtual bool gate (function *) { return flag_warn_unused_result; }
  7265. virtual unsigned int execute (function *)
  7266. {
  7267. do_warn_unused_result (gimple_body (current_function_decl));
  7268. return 0;
  7269. }
  7270. }; // class pass_warn_unused_result
  7271. } // anon namespace
  7272. gimple_opt_pass *
  7273. make_pass_warn_unused_result (gcc::context *ctxt)
  7274. {
  7275. return new pass_warn_unused_result (ctxt);
  7276. }
  7277. /* IPA passes, compilation of earlier functions or inlining
  7278. might have changed some properties, such as marked functions nothrow,
  7279. pure, const or noreturn.
  7280. Remove redundant edges and basic blocks, and create new ones if necessary.
  7281. This pass can't be executed as stand alone pass from pass manager, because
  7282. in between inlining and this fixup the verify_flow_info would fail. */
  7283. unsigned int
  7284. execute_fixup_cfg (void)
  7285. {
  7286. basic_block bb;
  7287. gimple_stmt_iterator gsi;
  7288. int todo = 0;
  7289. gcov_type count_scale;
  7290. edge e;
  7291. edge_iterator ei;
  7292. count_scale
  7293. = GCOV_COMPUTE_SCALE (cgraph_node::get (current_function_decl)->count,
  7294. ENTRY_BLOCK_PTR_FOR_FN (cfun)->count);
  7295. ENTRY_BLOCK_PTR_FOR_FN (cfun)->count =
  7296. cgraph_node::get (current_function_decl)->count;
  7297. EXIT_BLOCK_PTR_FOR_FN (cfun)->count =
  7298. apply_scale (EXIT_BLOCK_PTR_FOR_FN (cfun)->count,
  7299. count_scale);
  7300. FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
  7301. e->count = apply_scale (e->count, count_scale);
  7302. FOR_EACH_BB_FN (bb, cfun)
  7303. {
  7304. bb->count = apply_scale (bb->count, count_scale);
  7305. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
  7306. {
  7307. gimple stmt = gsi_stmt (gsi);
  7308. tree decl = is_gimple_call (stmt)
  7309. ? gimple_call_fndecl (stmt)
  7310. : NULL;
  7311. if (decl)
  7312. {
  7313. int flags = gimple_call_flags (stmt);
  7314. if (flags & (ECF_CONST | ECF_PURE | ECF_LOOPING_CONST_OR_PURE))
  7315. {
  7316. if (gimple_purge_dead_abnormal_call_edges (bb))
  7317. todo |= TODO_cleanup_cfg;
  7318. if (gimple_in_ssa_p (cfun))
  7319. {
  7320. todo |= TODO_update_ssa | TODO_cleanup_cfg;
  7321. update_stmt (stmt);
  7322. }
  7323. }
  7324. if (flags & ECF_NORETURN
  7325. && fixup_noreturn_call (stmt))
  7326. todo |= TODO_cleanup_cfg;
  7327. }
  7328. /* Remove stores to variables we marked write-only.
  7329. Keep access when store has side effect, i.e. in case when source
  7330. is volatile. */
  7331. if (gimple_store_p (stmt)
  7332. && !gimple_has_side_effects (stmt))
  7333. {
  7334. tree lhs = get_base_address (gimple_get_lhs (stmt));
  7335. if (TREE_CODE (lhs) == VAR_DECL
  7336. && (TREE_STATIC (lhs) || DECL_EXTERNAL (lhs))
  7337. && varpool_node::get (lhs)->writeonly)
  7338. {
  7339. unlink_stmt_vdef (stmt);
  7340. gsi_remove (&gsi, true);
  7341. release_defs (stmt);
  7342. todo |= TODO_update_ssa | TODO_cleanup_cfg;
  7343. continue;
  7344. }
  7345. }
  7346. /* For calls we can simply remove LHS when it is known
  7347. to be write-only. */
  7348. if (is_gimple_call (stmt)
  7349. && gimple_get_lhs (stmt))
  7350. {
  7351. tree lhs = get_base_address (gimple_get_lhs (stmt));
  7352. if (TREE_CODE (lhs) == VAR_DECL
  7353. && (TREE_STATIC (lhs) || DECL_EXTERNAL (lhs))
  7354. && varpool_node::get (lhs)->writeonly)
  7355. {
  7356. gimple_call_set_lhs (stmt, NULL);
  7357. update_stmt (stmt);
  7358. todo |= TODO_update_ssa | TODO_cleanup_cfg;
  7359. }
  7360. }
  7361. if (maybe_clean_eh_stmt (stmt)
  7362. && gimple_purge_dead_eh_edges (bb))
  7363. todo |= TODO_cleanup_cfg;
  7364. gsi_next (&gsi);
  7365. }
  7366. FOR_EACH_EDGE (e, ei, bb->succs)
  7367. e->count = apply_scale (e->count, count_scale);
  7368. /* If we have a basic block with no successors that does not
  7369. end with a control statement or a noreturn call end it with
  7370. a call to __builtin_unreachable. This situation can occur
  7371. when inlining a noreturn call that does in fact return. */
  7372. if (EDGE_COUNT (bb->succs) == 0)
  7373. {
  7374. gimple stmt = last_stmt (bb);
  7375. if (!stmt
  7376. || (!is_ctrl_stmt (stmt)
  7377. && (!is_gimple_call (stmt)
  7378. || (gimple_call_flags (stmt) & ECF_NORETURN) == 0)))
  7379. {
  7380. if (stmt && is_gimple_call (stmt))
  7381. gimple_call_set_ctrl_altering (stmt, false);
  7382. stmt = gimple_build_call
  7383. (builtin_decl_implicit (BUILT_IN_UNREACHABLE), 0);
  7384. gimple_stmt_iterator gsi = gsi_last_bb (bb);
  7385. gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
  7386. }
  7387. }
  7388. }
  7389. if (count_scale != REG_BR_PROB_BASE)
  7390. compute_function_frequency ();
  7391. if (current_loops
  7392. && (todo & TODO_cleanup_cfg))
  7393. loops_state_set (LOOPS_NEED_FIXUP);
  7394. return todo;
  7395. }
  7396. namespace {
  7397. const pass_data pass_data_fixup_cfg =
  7398. {
  7399. GIMPLE_PASS, /* type */
  7400. "fixup_cfg", /* name */
  7401. OPTGROUP_NONE, /* optinfo_flags */
  7402. TV_NONE, /* tv_id */
  7403. PROP_cfg, /* properties_required */
  7404. 0, /* properties_provided */
  7405. 0, /* properties_destroyed */
  7406. 0, /* todo_flags_start */
  7407. 0, /* todo_flags_finish */
  7408. };
  7409. class pass_fixup_cfg : public gimple_opt_pass
  7410. {
  7411. public:
  7412. pass_fixup_cfg (gcc::context *ctxt)
  7413. : gimple_opt_pass (pass_data_fixup_cfg, ctxt)
  7414. {}
  7415. /* opt_pass methods: */
  7416. opt_pass * clone () { return new pass_fixup_cfg (m_ctxt); }
  7417. virtual unsigned int execute (function *) { return execute_fixup_cfg (); }
  7418. }; // class pass_fixup_cfg
  7419. } // anon namespace
  7420. gimple_opt_pass *
  7421. make_pass_fixup_cfg (gcc::context *ctxt)
  7422. {
  7423. return new pass_fixup_cfg (ctxt);
  7424. }
  7425. /* Garbage collection support for edge_def. */
  7426. extern void gt_ggc_mx (tree&);
  7427. extern void gt_ggc_mx (gimple&);
  7428. extern void gt_ggc_mx (rtx&);
  7429. extern void gt_ggc_mx (basic_block&);
  7430. static void
  7431. gt_ggc_mx (rtx_insn *& x)
  7432. {
  7433. if (x)
  7434. gt_ggc_mx_rtx_def ((void *) x);
  7435. }
  7436. void
  7437. gt_ggc_mx (edge_def *e)
  7438. {
  7439. tree block = LOCATION_BLOCK (e->goto_locus);
  7440. gt_ggc_mx (e->src);
  7441. gt_ggc_mx (e->dest);
  7442. if (current_ir_type () == IR_GIMPLE)
  7443. gt_ggc_mx (e->insns.g);
  7444. else
  7445. gt_ggc_mx (e->insns.r);
  7446. gt_ggc_mx (block);
  7447. }
  7448. /* PCH support for edge_def. */
  7449. extern void gt_pch_nx (tree&);
  7450. extern void gt_pch_nx (gimple&);
  7451. extern void gt_pch_nx (rtx&);
  7452. extern void gt_pch_nx (basic_block&);
  7453. static void
  7454. gt_pch_nx (rtx_insn *& x)
  7455. {
  7456. if (x)
  7457. gt_pch_nx_rtx_def ((void *) x);
  7458. }
  7459. void
  7460. gt_pch_nx (edge_def *e)
  7461. {
  7462. tree block = LOCATION_BLOCK (e->goto_locus);
  7463. gt_pch_nx (e->src);
  7464. gt_pch_nx (e->dest);
  7465. if (current_ir_type () == IR_GIMPLE)
  7466. gt_pch_nx (e->insns.g);
  7467. else
  7468. gt_pch_nx (e->insns.r);
  7469. gt_pch_nx (block);
  7470. }
  7471. void
  7472. gt_pch_nx (edge_def *e, gt_pointer_operator op, void *cookie)
  7473. {
  7474. tree block = LOCATION_BLOCK (e->goto_locus);
  7475. op (&(e->src), cookie);
  7476. op (&(e->dest), cookie);
  7477. if (current_ir_type () == IR_GIMPLE)
  7478. op (&(e->insns.g), cookie);
  7479. else
  7480. op (&(e->insns.r), cookie);
  7481. op (&(block), cookie);
  7482. }